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
|
from odoo import api, fields, models, _
import time
import csv
import datetime
from odoo.modules import get_modules, get_module_path
from odoo.exceptions import UserError
import copy
import logging
from io import StringIO
import base64
_logger = logging.getLogger(__name__)
class efaktur_pk_wizard(models.TransientModel):
_name = 'vit.efaktur_pk'
export_file = fields.Binary(string="Export File", )
export_filename = fields.Char(string="Export File", )
# @api.multi
def confirm_button(self):
"""
export pk yang is_efaktur_exported = False
update setelah export
:return:
"""
cr = self.env.cr
headers = [
'FK',
'KD_JENIS_TRANSAKSI',
'FG_PENGGANTI',
'NOMOR_FAKTUR',
'MASA_PAJAK',
'TAHUN_PAJAK',
'TANGGAL_FAKTUR',
'NPWP',
'NAMA',
'ALAMAT_LENGKAP',
'JUMLAH_DPP',
'JUMLAH_PPN',
'JUMLAH_PPNBM',
'ID_KETERANGAN_TAMBAHAN',
'FG_UANG_MUKA',
'UANG_MUKA_DPP',
'UANG_MUKA_PPN',
'UANG_MUKA_PPNBM',
'REFERENSI',
'KODE_DOKUMEN_PENDUKUNG'
]
mpath = get_module_path('vit_efaktur')
# csvfile = open(mpath + '/static/fpk.csv', 'wb')
csvfile = StringIO()
csvwriter = csv.writer(csvfile, delimiter=',')
csvwriter.writerow([h.upper() for h in headers])
inv_obj = self.env['account.move']
invoices = inv_obj.search([('is_efaktur_exported','!=',True),
('state','=','posted'),
('efaktur_id','!=', False),
('move_type','=','out_invoice')])
company = self.env.user.company_id.partner_id
i=0
self.baris2(headers, csvwriter)
self.baris3(headers, csvwriter)
combined_invoices = self.gabung_by_efaktur(invoices)
for invoice in combined_invoices:
self.baris4(headers, csvwriter, invoice)
self.baris5(headers, csvwriter, company )
for line in invoice['invoice_line_ids']:
self.baris6(headers, csvwriter, line)
i+=1
for id in invoices.mapped('id'):
invoice = inv_obj.browse(id)
invoice.is_efaktur_exported=True
invoice.date_efaktur_exported=time.strftime("%Y-%m-%d %H:%M:%S")
cr.commit()
# csvfile.close()
# _logger.info(csvfile.getvalue().encode() )
self.export_file = base64.b64encode(csvfile.getvalue().encode())
self.export_filename = 'Export-%s.csv' % time.strftime("%Y%m%d_%H%M%S")
return {
'name': "Export E-Faktur Complete, total %s records" % i,
'type': 'ir.actions.act_window',
'res_model': 'vit.efaktur_pk',
'view_mode': 'form',
'res_id': self.id,
'views': [(False, 'form')],
'target': 'new',
}
# raise UserError("Export %s record(s) Done!" % i)
def gabung_by_efaktur(self, invoices):
old_efaktur = None
inv_obj = self.env['account.move']
i=0
combines=[]
# list of tuples of invoices to be combined by efaktur
# [(inv1,inv2,inv5),(inv3,inv4)]
final_invoices = []
# list of final combined invoices, totallized
# [inv125, inv34]
for inv in sorted(invoices, key=lambda inv: inv.efaktur_id):
if old_efaktur == inv.efaktur_id:
combines[i-1].append(inv_obj.search_read([('id','=',inv.id)])[0])
else:
combines.append([inv_obj.search_read([('id','=',inv.id)])[0]])
i += 1
old_efaktur = inv.efaktur_id
# comb = invoices per efaktur yg sama
for comb in combines:
i=0
for inv in comb:
if i == 0:
i+=1
continue
line_ids = inv['invoice_line_ids']
for id in line_ids:
comb[0]['invoice_line_ids'].append(id)
comb[0]['amount_untaxed'] += inv['amount_untaxed']
comb[0]['amount_tax'] += inv['amount_tax']
comb[0]['amount_total'] += inv['amount_total']
comb[0]['number'] += "," + inv['number']
i+=1
comb[0]['invoice_line_ids'] = self.merge_lines(comb[0]['invoice_line_ids'])
final_invoices.append(comb[0])
return final_invoices
def merge_lines(self, lines):
old_line = None
final_lines = []
i=0
for line in self.env['account.move.line'].search_read([('id','in',lines)]):
if old_line and (old_line['product_id'] == line['product_id'] and old_line['price_unit'] == line['price_unit']):
old_line['price_unit'] += line['price_unit']
old_line['quantity'] += line['quantity']
final_lines.append(old_line)
else:
final_lines.append(line)
old_line = line
i+=1
return final_lines
def baris2(self, headers, csvwriter):
data = {
'FK': 'LT',
'KD_JENIS_TRANSAKSI': 'NPWP',
'FG_PENGGANTI': 'NAMA',
'NOMOR_FAKTUR': 'JALAN',
'MASA_PAJAK': 'BLOK',
'TAHUN_PAJAK': 'NOMOR',
'TANGGAL_FAKTUR': 'RT',
'NPWP': 'RW',
'NAMA': 'KECAMATAN',
'ALAMAT_LENGKAP': 'KELURAHAN',
'JUMLAH_DPP': 'KABUPATEN',
'JUMLAH_PPN': 'PROPINSI',
'JUMLAH_PPNBM': 'KODE_POS',
'ID_KETERANGAN_TAMBAHAN': 'NOMOR_TELEPON',
'FG_UANG_MUKA': '',
'UANG_MUKA_DPP': '',
'UANG_MUKA_PPN': '',
'UANG_MUKA_PPNBM': '',
'REFERENSI': '',
'KODE_DOKUMEN_PENDUKUNG':''
}
csvwriter.writerow([data[v] for v in headers])
def baris3(self, headers, csvwriter):
data = {
'FK': 'OF',
'KD_JENIS_TRANSAKSI': 'KODE_OBJEK',
'FG_PENGGANTI': 'NAMA',
'NOMOR_FAKTUR': 'HARGA_SATUAN',
'MASA_PAJAK': 'JUMLAH_BARANG',
'TAHUN_PAJAK': 'HARGA_TOTAL',
'TANGGAL_FAKTUR': 'DISKON',
'NPWP': 'DPP',
'NAMA': 'PPN',
'ALAMAT_LENGKAP': 'TARIF_PPNBM',
'JUMLAH_DPP': 'PPNBM',
'JUMLAH_PPN': '',
'JUMLAH_PPNBM': '',
'ID_KETERANGAN_TAMBAHAN': '',
'FG_UANG_MUKA': '',
'UANG_MUKA_DPP': '',
'UANG_MUKA_PPN': '',
'UANG_MUKA_PPNBM': '',
'REFERENSI': '',
'KODE_DOKUMEN_PENDUKUNG': ''
}
csvwriter.writerow([data[v] for v in headers])
def baris4(self, headers, csvwriter, inv):
partner_id = self.env['res.partner'].browse(inv['partner_id'][0])
if not partner_id.npwp:
raise UserError("Harap masukkan NPWP Customer %s" % inv['partner_id'][1])
if not inv['efaktur_id'][0]:
raise UserError("Harap masukkan Nomor Seri Faktur Pajak Keluaran Invoice Nomor %s" % inv['number'])
# yyyy-mm-dd to dd/mm/yyyy
# d = inv['invoice_date'].split("-")
# invoice_date = "%s/%s/%s" %(d[2],d[1],d[0])
invoice_date = inv['invoice_date'].strftime("%d/%m/%Y")
npwp = partner_id.npwp.replace(".","").replace("-","")
faktur = inv['efaktur_id'][1].replace(".","").replace("-","")
print ("Tax ", inv['amount_tax'])
print ("DPP ", inv['amount_untaxed'])
print ("Tax Round ", round(inv['amount_tax']))
print ("DPP Round ", round(inv['amount_untaxed']))
# fix is_cancel and invoice number column @stephan 2022-07-29, 29/07/2022
masa_pajak = inv['invoice_date'].month
tahun_pajak = inv['invoice_date'].year
if 'cancel' in inv['state']:
data = {
'FK': 'FK',
'KD_JENIS_TRANSAKSI': '01',
'FG_PENGGANTI': '1',
'NOMOR_FAKTUR': faktur,
'MASA_PAJAK': masa_pajak,
'TAHUN_PAJAK': tahun_pajak,
'TANGGAL_FAKTUR': invoice_date,
'NPWP': npwp,
'NAMA': partner_id.nama_wajib_pajak or '',
'ALAMAT_LENGKAP': partner_id.alamat_lengkap_text or '',
'JUMLAH_DPP': round(inv['amount_untaxed']) or 0,
'JUMLAH_PPN': round(inv['amount_tax']) or 0,
'JUMLAH_PPNBM': 0,
'ID_KETERANGAN_TAMBAHAN': '',
'FG_UANG_MUKA': 0,
'UANG_MUKA_DPP': 0,
'UANG_MUKA_PPN': 0,
'UANG_MUKA_PPNBM': 0,
'REFERENSI': inv['name'] or '',
'KODE_DOKUMEN_PENDUKUNG':''
}
elif 'cancel' not in inv['state']:
data = {
'FK': 'FK',
'KD_JENIS_TRANSAKSI': '01',
'FG_PENGGANTI': '0',
'NOMOR_FAKTUR': faktur,
'MASA_PAJAK': masa_pajak,
'TAHUN_PAJAK': tahun_pajak,
'TANGGAL_FAKTUR': invoice_date,
'NPWP': npwp,
'NAMA': partner_id.nama_wajib_pajak or '',
'ALAMAT_LENGKAP': partner_id.alamat_lengkap_text or '',
'JUMLAH_DPP': round(inv['amount_untaxed']) or 0,
'JUMLAH_PPN': round(inv['amount_tax']) or 0,
'JUMLAH_PPNBM': 0,
'ID_KETERANGAN_TAMBAHAN': '',
'FG_UANG_MUKA': 0,
'UANG_MUKA_DPP': 0,
'UANG_MUKA_PPN': 0,
'UANG_MUKA_PPNBM': 0,
'REFERENSI': inv['name'] or '',
'KODE_DOKUMEN_PENDUKUNG': ''
}
_logger.info(data)
csvwriter.writerow([data[v] for v in headers])
def baris5(self, headers, csvwriter, company):
data = {
'FK': '',
'KD_JENIS_TRANSAKSI': '',
'FG_PENGGANTI': '',
'NOMOR_FAKTUR': '',
'MASA_PAJAK': '',
'TAHUN_PAJAK': '',
'TANGGAL_FAKTUR': '',
'NPWP': '',
'NAMA': '',
'ALAMAT_LENGKAP': '',
'JUMLAH_DPP': '',
'JUMLAH_PPN': '',
'JUMLAH_PPNBM': '',
'ID_KETERANGAN_TAMBAHAN': '',
'FG_UANG_MUKA': '',
'UANG_MUKA_DPP': '',
'UANG_MUKA_PPN': '',
'UANG_MUKA_PPNBM': '',
'REFERENSI': '',
'KODE_DOKUMEN_PENDUKUNG':''
}
csvwriter.writerow([data[v] for v in headers])
def baris6(self, headers, csvwriter, line):
#harga_total = round(line['price_subtotal'])
product_id = self.env['product.product'].browse(line['product_id'][0])
# harga_satuan = line['price_unit']
# diskon = line['discount']
# harga_setelah_discount = harga_satuan-(harga_satuan*diskon/100)
# tax_amount = 0
# tax_ids = line['tax_ids']
# for tax_id in tax_ids:
# tax = self.env['account.tax'].search([('id', '=', tax_id)], limit=1)
# tax_percentage = tax.amount
# tax_include = tax.price_include
# if tax_include:
# tax_amount = harga_setelah_discount - (harga_setelah_discount / (1 + tax_percentage / 100))
# else:
# tax_amount = harga_setelah_discount + (harga_setelah_discount*tax_percentage/100)
# dpp_satuan = harga_setelah_discount-tax_amount
jumlah_barang = line['quantity']
harga_total = round(line['price_subtotal'])
dpp = round(line['price_subtotal'])
ppn = round(line['price_total']-line['price_subtotal'])
dpp_satuan = round(dpp/jumlah_barang)
product_name = product_id.display_name
first_index = product_name.find("[")
second_index = product_name.find("]")+1
str_will_replace = product_name[first_index:second_index]
display_name = product_name.replace(str_will_replace,"").strip()
data = {
'FK': 'OF',
'KD_JENIS_TRANSAKSI': product_id.default_code or '',
'FG_PENGGANTI': display_name or '',
'NOMOR_FAKTUR': dpp_satuan,
'MASA_PAJAK': jumlah_barang,
'TAHUN_PAJAK': harga_total,
'TANGGAL_FAKTUR': 0,
'NPWP': dpp,
'NAMA': ppn,
'ALAMAT_LENGKAP': '0',
'JUMLAH_DPP': '0',
'JUMLAH_PPN': '',
'JUMLAH_PPNBM': '',
'ID_KETERANGAN_TAMBAHAN': '',
'FG_UANG_MUKA': '',
'UANG_MUKA_DPP': '',
'UANG_MUKA_PPN': '',
'UANG_MUKA_PPNBM': '',
'REFERENSI': '',
'KODE_DOKUMEN_PENDUKUNG':''
}
csvwriter.writerow([data[v] for v in headers])
|