summaryrefslogtreecommitdiff
path: root/vit_efaktur/wizard/pm.py
blob: 608069e8aa4e14d8d23d8c171e39ded7790b37fc (plain)
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
from odoo import api, fields, models, _
import time
import csv
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_pm_wizard(models.TransientModel):
    _name = 'vit.efaktur_pm'
    
    export_file = fields.Binary(string="Export File",  )
    export_filename = fields.Char(string="Export File",  )

    # @api.multi
    def confirm_button(self):
        """
        export pm yang is_efaktur_exported = False
        update setelah export
        :return: 
        """
        cr = self.env.cr

        headers = [
            'FM',
            'KD_JENIS_TRANSAKSI',
            'FG_PENGGANTI',
            'NOMOR_FAKTUR',
            'MASA_PAJAK',
            'TAHUN_PAJAK',
            'TANGGAL_FAKTUR',
            'NPWP',
            'NAMA',
            'ALAMAT_LENGKAP',
            'JUMLAH_DPP',
            'JUMLAH_PPN',
            'JUMLAH_PPNBM',
            'IS_CREDITABLE'
        ]


        mpath = get_module_path('vit_efaktur')

        # csvfile = open(mpath + '/static/fpm.csv', 'wb')
        csvfile = StringIO()
        csvwriter = csv.writer(csvfile, delimiter=',')
        csvwriter.writerow([h.upper() for h in headers])

        onv_obj = self.env['account.move']
        invoices = onv_obj.search([('is_efaktur_exported','=',False),
                                   ('state','=','open'),
                                   ('efaktur_masukan','!=', ''),
                                   ('move_type','=','in_invoice')])


        i=0
        for invoice in invoices:
            self.baris2(headers, csvwriter, invoice)
            invoice.is_efaktur_exported=True
            invoice.date_efaktur_exported=time.strftime("%Y-%m-%d %H:%M:%S")
            i+=1

        cr.commit()
        # csvfile.close()

        # raise UserError("Export %s record(s) Done!" % i)
        
        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_pm',
            'view_mode': 'form',
            'res_id': self.id,
            'views': [(False, 'form')],
            'target': 'new',
        }

    def baris2(self, headers, csvwriter, inv):
        if not inv.partner_id.npwp:
            raise UserError("Harap masukkan NPWP Supplier %s" % inv.partner_id.name)

        if not inv.efaktur_masukan:
            raise UserError("Harap masukkan Nomor Seri Faktur Pajak Masukan 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 = inv.partner_id.npwp.replace(".","").replace("-","")
        faktur = inv.efaktur_masukan.replace(".","").replace("-","")

        data = {
            'FM':'FM',
            'KD_JENIS_TRANSAKSI':'01',
            'FG_PENGGANTI':'0',
            'NOMOR_FAKTUR':faktur,
            'MASA_PAJAK': inv.masa_pajak or '',
            'TAHUN_PAJAK': inv.tahun_pajak or '',
            'TANGGAL_FAKTUR': invoice_date,
            'NPWP': npwp,
            'NAMA': inv.partner_id.name or '',
            'ALAMAT_LENGKAP': inv.partner_id.alamat_lengkap or '',
            'JUMLAH_DPP': int(inv.amount_untaxed) or 0,
            'JUMLAH_PPN': int(inv.amount_tax) or 0,
            'JUMLAH_PPNBM': 0,
            'IS_CREDITABLE':1
        }
        csvwriter.writerow([data[v] for v in headers])