Source

pytis / pytis / lib / printer.py

The default branch has multiple heads

Full commit
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
## -*- coding: utf-8 -*-

from pytis.lib.app_globals import Globals
from pytis.model.invoice import Invoice, InvoiceCorrect, InvoicePosition
from pytis.model.order import Order

from sqlalchemy.orm import eagerload
from pylons import request, response, session, tmpl_context as c
from decimal import Decimal

import StringIO
import xlwt
import codecs
from xml.dom.minidom import Document
from pytis.model.company import Company

'''
Klasa odpowiedzialna za wydruk dokumentów
'''
class Printer(object):

    def export_orders(self, date_from, date_to):
        """Export orders into Excel file"""
        doc = Document()
        buffer = StringIO.StringIO()

        invoices = Invoice.query.filter(Invoice.sellDate.between(date_from, date_to)).order_by(Invoice.series_number).all()

        columns = [
            'Nr faktury',
            'Kontrahent',
            'Fracht netto',
            'Przewoznik',
            'Nr faktury',
            'Fracht netto',
            'Nr faktury',
            'Kontrahent',
            'Fracht netto',
            'Kierowca'
        ]

        wb = xlwt.Workbook()
        ws = wb.add_sheet('Rejestr')

        font0 = xlwt.Font()
        font0.name = 'Times New Roman'
        font0.colour_index = 0
        font0.bold = True

        font1 = xlwt.Font()
        font1.name = 'Times New Roman'
        font1.colour_index = 0
        font1.bold = False

        style0 = xlwt.XFStyle()
        style0.font = font0

        style1 = xlwt.XFStyle()
        style1.font = font1

        for i, column in enumerate(columns):
            ws.write(0, i, column, style0)

        i = 0
        for invoice in invoices:
            for item in invoice.elements:
                order = item.order

                if order.transport_order:
                    ws.write(i + 1, 0, invoice.number, style1)
                    ws.write(i + 1, 1, order.company.name, style1)
                    ws.write(i + 1, 2, order.real_value, style1)
                    ws.write(i + 1, 4, order.transport_order.company.name, style1)
                    ws.write(i + 1, 5, order.transport_order.real_value, style1)
                else:
                    ws.write(i + 1, 6, invoice.number, style1)
                    ws.write(i + 1, 7, order.company.name, style1)
                    ws.write(i + 1, 8, order.real_value, style1)
                    ws.write(i + 1, 9, order.delegation.driver.full_name, style1)
                i = i + 1

        wb.save(buffer)
        return buffer

    def sell_registry(self):
        date_from = request.params.get('from')
        date_to = request.params.get('to')
                
        buffer = StringIO.StringIO()               

        columns = ['Numer', 
                   'NIP', 
                   'Kontrahent', 
                   'Data wyst.', 
                   'Data sprzed.', 
                   'Razem brutto', 
                   'Netto bez ZW, BO, np.', 
                   'Razem VAT',
                   'Stawka 22% netto',
                   'Stawka 22% VAT',
                   'NPO']
        invoices = Invoice.query.options(eagerload('elements')).filter(Invoice.issueDate.between(date_from , date_to)).order_by(Invoice.series_number).all()
        corrects = InvoiceCorrect.query.options(eagerload('positions')).filter(InvoiceCorrect.correct_date.between(date_from, date_to)).order_by(InvoiceCorrect.series_number).all()

        wb = xlwt.Workbook()
        ws = wb.add_sheet('Rejestr')        
        
        font0 = xlwt.Font()
        font0.name = 'Times New Roman'
        font0.colour_index = 0
        font0.bold = True
        
        font1 = xlwt.Font()
        font1.name = 'Times New Roman'
        font1.colour_index = 0
        font1.bold = False 
        
        style0 = xlwt.XFStyle()
        style0.font = font0
        
        style1 = xlwt.XFStyle()        
        style1.font = font1        
        
        for i, column in enumerate(columns):
            ws.write(0, i, column, style0)
        
        for i, invoice in enumerate(invoices):
            ws.write(i + 1, 0, invoice.number, style1)
            ws.write(i + 1, 1, invoice.company.nip, style1)
            ws.write(i + 1, 2, invoice.company.name, style1)
            ws.write(i + 1, 3, str(invoice.issueDate), style1)
            ws.write(i + 1, 4, str(invoice.sellDate), style1)
                                  
            # Kontrahent 22%
            if invoice.tax.name != 'NPO':
                ws.write(i + 1, 5, invoice.brutto_value, style1)
                ws.write(i + 1, 6, invoice.netto_value, style1)
                ws.write(i + 1, 7, invoice.tax_value, style1)
                ws.write(i + 1, 8, invoice.netto_value, style1)
                ws.write(i + 1, 9, invoice.tax_value, style1)
                ws.write(i + 1, 10, '-', style1)
            else:
                ws.write(i + 1, 5, invoice.netto_value * Decimal(repr(invoice.currencyValue or 1)), style1)
                ws.write(i + 1, 6, invoice.netto_value * Decimal(repr(invoice.currencyValue or 1)), style1)
                ws.write(i + 1, 7, 0, style1)
                ws.write(i + 1, 8, '-', style1)
                ws.write(i + 1, 9, '-', style1)
                ws.write(i + 1, 10, invoice.netto_value * Decimal(repr(invoice.currencyValue or 1)), style1)          
                
        rows_count = len(ws.rows)

        for i, invoice in enumerate(corrects):
            ws.write(i + rows_count, 0, invoice.number, style1)
            ws.write(i + rows_count, 1, invoice.company.nip, style1)
            ws.write(i + rows_count, 2, invoice.company.name, style1)
            ws.write(i + rows_count, 3, str(invoice.correct_date), style1)
            ws.write(i + rows_count, 4, str(invoice.sell_date), style1)
                                  
            # Kontrahent 22%
            if invoice.positions[0].tax.name != 'NPO':
                ws.write(i + rows_count, 5, invoice.diff_brutto_value, style1)
                ws.write(i + rows_count, 6, invoice.diff_netto_value, style1)
                ws.write(i + rows_count, 7, invoice.diff_tax_value, style1)
                ws.write(i + rows_count, 8, invoice.diff_netto_value, style1)
                ws.write(i + rows_count, 9, invoice.diff_tax_value, style1)
                ws.write(i + rows_count, 10, '-', style1)
            else:
                ws.write(i + rows_count, 5, invoice.diff_netto_value * (invoice.currency_value or 1), style1)
                ws.write(i + rows_count, 6, invoice.diff_netto_value * (invoice.currency_value or 1), style1)
                ws.write(i + rows_count, 7, 0, style1)
                ws.write(i + rows_count, 8, '-', style1)
                ws.write(i + rows_count, 9, '-', style1)
                ws.write(i + rows_count, 10, invoice.diff_netto_value * (invoice.currency_value or 1), style1)        
                
        wb.save(buffer)
        return buffer

    def _add_element(self, document, key, value, cdata = False):
        element = document.createElement(key)

        if cdata:
            text = document.createCDATASection(value)
        else:
            text = document.createTextNode(value)        

        element.appendChild(text)

        return element

    def _add_correct(self, document, invoice):
        """Export invoice correct"""
        invoiceElement = document.createElement('REJESTR_SPRZEDAZY_VAT')
        invoiceElement.appendChild(self._add_element(document, 'MODUL', 'Rejestr VAT'))
        invoiceElement.appendChild(self._add_element(document, 'REJESTR', u'SPRZEDAŻ', True))
        invoiceElement.appendChild(self._add_element(document, 'DATA_WYSTAWIENIA', str(invoice.correct_date), True))
        invoiceElement.appendChild(self._add_element(document, 'DATA_SPRZEDAZY', str(invoice.sell_date), True))
        invoiceElement.appendChild(self._add_element(document, 'TERMIN', str(invoice.payment_date), True))
        invoiceElement.appendChild(self._add_element(document, 'NUMER', invoice.number, True))
        invoiceElement.appendChild(self._add_element(document, 'KOREKTA', 'Nie'))
        invoiceElement.appendChild(self._add_element(document, 'FISKALNA', 'Nie'))
        invoiceElement.appendChild(self._add_element(document, 'DETALICZNA', 'Nie'))
        invoiceElement.appendChild(self._add_element(document, 'TYP_PODMIOTU', 'kontrahent', True))
        invoiceElement.appendChild(self._add_element(document, 'PODMIOT', invoice.company.shortName, True))
        invoiceElement.appendChild(self._add_element(document, 'NOTOWANIE_WALUTY_ILE', '1', True))
        invoiceElement.appendChild(self._add_element(document, 'NOTOWANIE_WALUTY_ZA_ILE', '1', True))
        invoiceElement.appendChild(self._add_element(document, 'KATEGORIA', u'SPRZEDAŻ', True))

        invoiceElement.appendChild(self._add_element(document, 'OPIS', invoice.number, True))
        invoiceElement.appendChild(self._add_element(document, 'FORMA_PLATNOSCI', invoice.company.payment.value, True))

        if invoice.company.tax.name == 'NPO':
            invoiceElement.appendChild(self._add_element(document, 'EKSPORT', u'Wewnątrzunijny', False))
            invoiceElement.appendChild(self._add_element(document, 'DEKLARACJA_VATUE', 'Tak', True))

        positions = document.createElement('POZYCJE')
        for position in invoice.positions:
            positionElement = document.createElement('POZYCJA')
            positionElement.appendChild(self._add_element(document, 'STAWKA_VAT', str(position.tax.value)))

            if invoice.company.tax.name == 'NPO':
                #add currency value
                positionElement.appendChild(self._add_element(document, 'NETTO', str(position.netto_value * (invoice.currency_value or 1))))
                positionElement.appendChild(self._add_element(document, 'VAT', str(position.tax_value * (invoice.currency_value or 1))))
                positionElement.appendChild(self._add_element(document, 'NETTO_SYS', str(position.netto_value * (invoice.currency_value or 1))))
                positionElement.appendChild(self._add_element(document, 'VAT_SYS', str(position.tax_value * (invoice.currency_value or 1))))
                positionElement.appendChild(self._add_element(document, 'NETTO_SYS2', str(position.netto_value * (invoice.currency_value or 1))))
                positionElement.appendChild(self._add_element(document, 'VAT_SYS2', str(position.tax_value * (invoice.currency_value or 1))))
                positionElement.appendChild(self._add_element(document, 'STATUS_VAT', 'nie podlega'))

            else:
                positionElement.appendChild(self._add_element(document, 'NETTO', str(position.netto_value)))
                positionElement.appendChild(self._add_element(document, 'VAT', str(position.tax_value)))
                positionElement.appendChild(self._add_element(document, 'NETTO_SYS', str(position.netto_value)))
                positionElement.appendChild(self._add_element(document, 'VAT_SYS', str(position.tax_value)))
                positionElement.appendChild(self._add_element(document, 'NETTO_SYS2', str(position.netto_value)))
                positionElement.appendChild(self._add_element(document, 'VAT_SYS2', str(position.tax_value)))
                positionElement.appendChild(self._add_element(document, 'STATUS_VAT', 'opodatkowana'))

            positions.appendChild(positionElement)

        if invoice.invoice.tax.name == 'NPO':
            payment = document.createElement('PLATNOSCI')
            paymentElement = document.createElement('PLATNOSC')
            paymentElement.appendChild(self._add_element(document, 'TERMIN_PLAT', str(invoice.payment_date), True))
            paymentElement.appendChild(self._add_element(document, 'FORMA_PLATNOSCI_PLAT', invoice.company.payment.value, True))
            paymentElement.appendChild(self._add_element(document, 'KWOTA_PLN_PLAT', str(invoice.brutto_value), True))
            paymentElement.appendChild(self._add_element(document, 'KWOTA_PLAT', str(invoice.brutto_value * invoice.currency_value), True))
            paymentElement.appendChild(self._add_element(document, 'KIERUNEK', u'przychód', True))
            paymentElement.appendChild(self._add_element(document, 'WALUTA_PLAT', invoice.currency_symbol, True))
            paymentElement.appendChild(self._add_element(document, 'NOTOWANIE_WALUTY_ILE_PLAT', str(invoice.currency_value), True))
            paymentElement.appendChild(self._add_element(document, 'DATA_KURSU', str(invoice.currency_date), True))
            payment.appendChild(paymentElement)
        else:
            payment = document.createElement('PLATNOSCI')
            paymentElement = document.createElement('PLATNOSC')
            paymentElement.appendChild(self._add_element(document, 'TERMIN_PLAT', str(invoice.payment_date), True))
            paymentElement.appendChild(self._add_element(document, 'FORMA_PLATNOSCI_PLAT', invoice.company.payment.value, True))
            paymentElement.appendChild(self._add_element(document, 'KWOTA_PLN_PLAT', str(invoice.brutto_value), True))
            paymentElement.appendChild(self._add_element(document, 'KWOTA_PLAT', str(invoice.brutto_value), True))
            paymentElement.appendChild(self._add_element(document, 'KIERUNEK', u'przychód', True))
            payment.appendChild(paymentElement)


        invoiceElement.appendChild(payment)
        invoiceElement.appendChild(positions)

        return invoiceElement

    def _add_invoice(self, document, invoice):
        """Export invoice"""
        invoiceElement = document.createElement('REJESTR_SPRZEDAZY_VAT')
        invoiceElement.appendChild(self._add_element(document, 'MODUL', 'Rejestr VAT'))
        invoiceElement.appendChild(self._add_element(document, 'REJESTR', u'SPRZEDAŻ', True))
        invoiceElement.appendChild(self._add_element(document, 'DATA_WYSTAWIENIA', str(invoice.issueDate), True))
        invoiceElement.appendChild(self._add_element(document, 'DATA_SPRZEDAZY', str(invoice.sellDate), True))
        invoiceElement.appendChild(self._add_element(document, 'TERMIN', str(invoice.payment_date), True))
        invoiceElement.appendChild(self._add_element(document, 'NUMER', invoice.number, True))
        invoiceElement.appendChild(self._add_element(document, 'KOREKTA', 'Nie'))
        invoiceElement.appendChild(self._add_element(document, 'FISKALNA', 'Nie'))
        invoiceElement.appendChild(self._add_element(document, 'DETALICZNA', 'Nie'))
        invoiceElement.appendChild(self._add_element(document, 'TYP_PODMIOTU', 'kontrahent', True))
        invoiceElement.appendChild(self._add_element(document, 'PODMIOT', invoice.company.shortName, True))
        invoiceElement.appendChild(self._add_element(document, 'NIP_KRAJ', invoice.company.nip_code, True))
        invoiceElement.appendChild(self._add_element(document, 'NIP', invoice.company.nip, True))
        invoiceElement.appendChild(self._add_element(document, 'NAZWA1', invoice.company.name, True))
        invoiceElement.appendChild(self._add_element(document, 'ULICA', invoice.company.address, True))
        invoiceElement.appendChild(self._add_element(document, 'MIASTO', invoice.company.city, True))
        invoiceElement.appendChild(self._add_element(document, 'KOD_POCZTOWY', invoice.company.zip, True))

        if invoice.tax.name == 'NPO':
            if invoice.elements[0].currency.value == 'EUR':
                invoiceElement.appendChild(self._add_element(document, 'WALUTA', invoice.elements[0].currency.value, True))
                invoiceElement.appendChild(self._add_element(document, 'NOTOWANIE_WALUTY_ILE', str(invoice.currencyValue or 1), True))
                invoiceElement.appendChild(self._add_element(document, 'NOTOWANIE_WALUTY_ZA_ILE', u'1', True))
                invoiceElement.appendChild(self._add_element(document, 'KURS_WALUTY', u'NBP', True))
                invoiceElement.appendChild(self._add_element(document, 'KURS_DO_KSIEGOWANIA', 'Nie', True))
                invoiceElement.appendChild(self._add_element(document, 'DATA_KURSU', str(invoice.currencyDate or ''), True))
                invoiceElement.appendChild(self._add_element(document, 'DATA_KURSU_2', str(invoice.currencyDate or ''), True))
                invoiceElement.appendChild(self._add_element(document, 'DATA_KURSU_PLAT', str(invoice.currencyDate or ''), True))
                invoiceElement.appendChild(self._add_element(document, 'DATA_KURSU_KD', str(''), True))
            invoiceElement.appendChild(self._add_element(document, 'KATEGORIA', u'EKSPORT USŁUG PRZEWO', True))
        else:
            invoiceElement.appendChild(self._add_element(document, 'DATA_KURSU', str(invoice.sellDate), True))
            invoiceElement.appendChild(self._add_element(document, 'KURS_DO_KSIEGOWANIA', 'Nie', True))
            invoiceElement.appendChild(self._add_element(document, 'NOTOWANIE_WALUTY_ILE', '1', True))
            invoiceElement.appendChild(self._add_element(document, 'NOTOWANIE_WALUTY_ZA_ILE', '1', True))
            invoiceElement.appendChild(self._add_element(document, 'KATEGORIA', u'SPRZEDAŻ USŁUG KRAJ', True))

        invoiceElement.appendChild(self._add_element(document, 'FORMA_PLATNOSCI', invoice.company.payment.value, True))
        
        if invoice.tax.name == 'NPO':
            invoiceElement.appendChild(self._add_element(document, 'EKSPORT', u'Wewnątrzunijny', False))
            invoiceElement.appendChild(self._add_element(document, 'DEKLARACJA_VATUE', 'Tak', True))

        positions = document.createElement('POZYCJE')
        for position in invoice.elements:
            positionElement = document.createElement('POZYCJA')
            positionElement.appendChild(self._add_element(document, 'STAWKA_VAT', str(position.tax.value)))

            if invoice.tax.name == 'NPO':
                positionElement.appendChild(self._add_element(document, 'NETTO', str(position.netto_value)))
                positionElement.appendChild(self._add_element(document, 'VAT', str(position.tax_value)))
                positionElement.appendChild(self._add_element(document, 'NETTO_SYS', str(position.netto_value * Decimal(repr((invoice.currencyValue or 1))))))
                positionElement.appendChild(self._add_element(document, 'VAT_SYS', str(0)))
                #positionElement.appendChild(self._add_element(document, 'NETTO_SYS2', str(position.netto_value)))
                positionElement.appendChild(self._add_element(document, 'NETTO_SYS2', str(position.netto_value * Decimal(repr((invoice.currencyValue or 1))))))
                positionElement.appendChild(self._add_element(document, 'VAT_SYS2', str(0)))
            else:
                positionElement.appendChild(self._add_element(document, 'NETTO', str(position.netto_value)))
                positionElement.appendChild(self._add_element(document, 'VAT', str(position.tax_value)))
                positionElement.appendChild(self._add_element(document, 'NETTO_SYS', str(position.netto_value)))
                positionElement.appendChild(self._add_element(document, 'VAT_SYS', str(position.tax_value)))
                #positionElement.appendChild(self._add_element(document, 'NETTO_SYS2', str(position.netto_value)))
                positionElement.appendChild(self._add_element(document, 'NETTO_SYS2', str(position.netto_value)))
                positionElement.appendChild(self._add_element(document, 'VAT_SYS2', str(position.tax_value)))

            if invoice.company.tax.name == 'NPO':
                positionElement.appendChild(self._add_element(document, 'STATUS_VAT', 'nie podlega'))
                positionElement.appendChild(self._add_element(document, 'KATEGORIA_POS', u'EKSPORT USŁUG PRZEWO', True))
            else:
                positionElement.appendChild(self._add_element(document, 'STATUS_VAT', 'opodatkowana'))
                positionElement.appendChild(self._add_element(document, 'KATEGORIA_POS', u'SPRZEDAŻ USŁUG KRAJ', True))
            
            positions.appendChild(positionElement)

        if invoice.tax.name == 'NPO':
            payment = document.createElement('PLATNOSCI')
            paymentElement = document.createElement('PLATNOSC')
            paymentElement.appendChild(self._add_element(document, 'TERMIN_PLAT', str(invoice.payment_date), True))
            paymentElement.appendChild(self._add_element(document, 'FORMA_PLATNOSCI_PLAT', invoice.company.payment.value, True))
            paymentElement.appendChild(self._add_element(document, 'KWOTA_PLN_PLAT', str(invoice.brutto_value * Decimal(repr(invoice.currencyValue or 1))), True))
            paymentElement.appendChild(self._add_element(document, 'KWOTA_PLAT', str(invoice.brutto_value), True))
            paymentElement.appendChild(self._add_element(document, 'KIERUNEK', u'przychód', True))
            paymentElement.appendChild(self._add_element(document, 'WALUTA_PLAT', invoice.currencySymbol or '', True))
            paymentElement.appendChild(self._add_element(document, 'NOTOWANIE_WALUTY_ILE_PLAT', str(invoice.currencyValue), True))
            paymentElement.appendChild(self._add_element(document, 'DATA_KURSU', str(invoice.currencyDate), True))
            paymentElement.appendChild(self._add_element(document, 'DATA_KURSU_PLAT', str(invoice.currencyDate), True))
            payment.appendChild(paymentElement)
        else:
            payment = document.createElement('PLATNOSCI')
            paymentElement = document.createElement('PLATNOSC')
            paymentElement.appendChild(self._add_element(document, 'TERMIN_PLAT', str(invoice.payment_date), True))
            paymentElement.appendChild(self._add_element(document, 'FORMA_PLATNOSCI_PLAT', invoice.company.payment.value, True))
            paymentElement.appendChild(self._add_element(document, 'KWOTA_PLN_PLAT', str(invoice.brutto_value), True))
            paymentElement.appendChild(self._add_element(document, 'KWOTA_PLAT', str(invoice.brutto_value), True))
            paymentElement.appendChild(self._add_element(document, 'KIERUNEK', u'przychód', True))
            paymentElement.appendChild(self._add_element(document, 'DATA_KURSU', str(invoice.sellDate), True))
            paymentElement.appendChild(self._add_element(document, 'DATA_KURSU_PLAT', str(invoice.sellDate), True))
            payment.appendChild(paymentElement)

        invoiceElement.appendChild(payment)
        invoiceElement.appendChild(positions)

        return invoiceElement

    def _add_company(self, document, company):
        element = document.createElement('KONTRAHENT')

        element.appendChild(self._add_element(document, 'AKRONIM', company.shortName, True))
        element.appendChild(self._add_element(document, 'RODZAJ', 'odbiorca dostawca', True))
        element.appendChild(self._add_element(document, 'PLATNIK_VAT', 'Tak', True))
        element.appendChild(self._add_element(document, 'ODBIORCA', company.shortName, True))
        element.appendChild(self._add_element(document, 'INDYWIDUALNY_TERMIN', 'Tak'))
        element.appendChild(self._add_element(document, 'TERMIN', company.paymentForm.value.split()[0], True))
        element.appendChild(self._add_element(document, 'MAX_ZWLOKA', '5', True))
        element.appendChild(self._add_element(document, 'FORMA_PLATNOSCI', company.payment.value, True))        

        addressElement = document.createElement('ADRESY')
        addElement = document.createElement('ADRES')
        addElement.appendChild(self._add_element(document, 'STATUS', 'aktualny'))
        addElement.appendChild(self._add_element(document, 'NAZWA1', company.name, True))
        addElement.appendChild(self._add_element(document, 'ULICA', company.address, True))
        addElement.appendChild(self._add_element(document, 'NR_DOMU', '', True))
        addElement.appendChild(self._add_element(document, 'MIASTO', company.city, True))
        addElement.appendChild(self._add_element(document, 'KOD_POCZTOWY', company.zip, True))
        addElement.appendChild(self._add_element(document, 'REGON', company.regon, True))
        addElement.appendChild(self._add_element(document, 'NIP', company.nip, True))
        addElement.appendChild(self._add_element(document, 'NIP_KRAJ', company.nip_code, True))
        addressElement.appendChild(addElement)
        
        element.appendChild(addressElement)

        return element

    def export_companies(self):
        """Export companies to OPTIMA"""
        doc = Document()
        companies = Company.query.all()
        buffer = StringIO.StringIO()

        root = doc.createElement('ROOT')
        root.setAttribute('xmlns', 'http://www.cdn.com.pl/optima/offline')
        doc.appendChild(root)

        companiesElement = doc.createElement('KONTRAHENCI')
        companiesElement.appendChild(self._add_element(doc, 'WERSJA', '2.00'))
        companiesElement.appendChild(self._add_element(doc, 'BAZA_ZRD_ID', 'SPRZ'))
        companiesElement.appendChild(self._add_element(doc, 'BAZA_DOC_ID', 'SPRZ'))

        for company in companies:
            companiesElement.appendChild(self._add_company(doc, company))

        root.appendChild(companiesElement)

        buffer.write(doc.toprettyxml(indent='', newl=''))        
        return buffer

    def export_to_cdn(self, date_from, date_to):
        """Export invoices to OPTIMA"""
        doc = Document()
        from pytis.model import meta
        buffer = StringIO.StringIO()
        
        invoices = Invoice.query.options(eagerload('elements')).filter(Invoice.issueDate.between(date_from , date_to)).order_by(Invoice.series_number).all()
        corrects = InvoiceCorrect.query.options(eagerload('positions')).filter(InvoiceCorrect.correct_date.between(date_from, date_to)).order_by(InvoiceCorrect.series_number).all()

        root = doc.createElement('ROOT')
        root.setAttribute('xmlns', 'http://www.cdn.com.pl/optima/offline')
        doc.appendChild(root)

        companiesElement = doc.createElement('KONTRAHENCI')
        companiesElement.appendChild(self._add_element(doc, 'WERSJA', '2.00'))
        companiesElement.appendChild(self._add_element(doc, 'BAZA_ZRD_ID', 'SPRZ'))
        companiesElement.appendChild(self._add_element(doc, 'BAZA_DOC_ID', 'SPRZ'))
        root.appendChild(companiesElement)

        companies = []
        for invoice in invoices:
            """fetch sets of companies"""
            if invoice.company not in companies:
                companies.append(invoice.company)

        for correct in corrects:
            """fetch sets of companies"""
            if correct.company not in companies:
                companies.append(correct.company)

        for company in companies:            
            companiesElement.appendChild(self._add_company(doc, company))
        root.appendChild(companiesElement)

        invoicesElement = doc.createElement('REJESTRY_SPRZEDAZY_VAT')
        invoicesElement.appendChild(self._add_element(doc, 'WERSJA', '2.00'))
        invoicesElement.appendChild(self._add_element(doc, 'BAZA_ZRD_ID', 'SPRZ'))
        invoicesElement.appendChild(self._add_element(doc, 'BAZA_DOC_ID', 'SPRZ'))
        for invoice in invoices:
            invoicesElement.appendChild(self._add_invoice(doc, invoice))

            if not invoice.is_exported:
                invoice.mark_as_exported()
        meta.Session.commit()
        
        for correct in corrects:
            invoicesElement.appendChild(self._add_correct(doc, correct))

        root.appendChild(invoicesElement)

        buffer.write(doc.toprettyxml(indent='', newl=''))        
        return buffer