summaryrefslogtreecommitdiff
path: root/indoteknik_custom/models/logbook_bill.py
blob: bb95609221ff1467ab6e65e630acb6d60819c4c7 (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
from odoo import models, fields, api, _
from odoo.exceptions import UserError
from pytz import timezone
from datetime import datetime

class LogbookBill(models.TransientModel):
    _name = 'logbook.bill'

    name = fields.Char(string='Name', default='Logbook Bill')
    logbook_bill_line = fields.One2many(
        comodel_name='logbook.bill.line',
        inverse_name='logbook_bill_id',
        string='Logbook Bill Line'
    )


    def grand_total(self, picking):
        total = 0
        for line in picking.move_ids_without_package:
            po = self.env['purchase.order.line'].search([
                ('order_id', '=', picking.purchase_id.id),
                ('product_id', '=', line.product_id.id),
            ], order='id desc', limit=1)
            total += line.quantity_done * po.price_unit
        total_with_tax = total * 1.11 
        return total_with_tax


    def create_logbook_bill(self):
        logbook_line = self.logbook_bill_line

        current_time = datetime.utcnow()
        report_logbook_ids = []
        parameters_header = {
            'date': current_time,
            'created_by': self.env.user.id,
        }

        report_logbook = self.env['report.logbook.bill'].create([parameters_header])
        for line in logbook_line:
            picking = self.env['stock.picking'].search([('name', '=', line.name)], limit=1)
            stock = picking
            parent_id = stock.partner_id.parent_id.id
            parent_id =  parent_id if parent_id else stock.partner_id.id

            data = {
                'purchase_id': stock.purchase_id.id,
                'name': stock.name,
                'grand_total': self.grand_total(picking),
                'partner_id': parent_id,
                'invoice': line.invoice,
                'surat_jalan': line.surat_jalan,
                'proforma_invoice': line.proforma_invoice,
                'faktur_pajak': line.faktur_pajak,
                'date_approve': stock.date_done,
                'report_logbook_bill_id': report_logbook.id,
                'note': line.note,
                'note_finance': line.note_finance
            }
            self.env['report.logbook.bill.line'].create([data])

            report_logbook_ids.append(report_logbook.id)
            line.unlink()
            
        self.unlink()
        return {
            'name': _('Report Logbook Bill'),
            'view_mode': 'tree,form',
            'res_model': 'report.logbook.bill',
            'target': 'current',
            'type': 'ir.actions.act_window',
            'domain': [('id', 'in', report_logbook_ids)],
        }

class LogbookBillLine(models.TransientModel):
    _name = 'logbook.bill.line'

    name = fields.Char(string='Name')
    logbook_bill_id = fields.Many2one('logbook.bill', string='Logbook Bill')
    partner_id = fields.Many2one('res.partner', string='Customer')
    purchase_id = fields.Many2one('purchase.order', string='Purchase Order')
    invoice = fields.Boolean(string='Invoice')
    faktur_pajak = fields.Boolean(string='Faktur Pajak')
    surat_jalan = fields.Boolean(string='Surat Jalan')
    proforma_invoice = fields.Boolean(string='Proforma Invoice')
    date_approve = fields.Datetime(string='Date Approve', tracking=3)
    note = fields.Char(string='Note Logistik')
    note_finance = fields.Char(string='Note Finance')

    @api.onchange('name')
    def onchange_name(self):
        current_time = datetime.now(timezone('Asia/Jakarta')).strftime('%Y-%m-%d %H:%M:%S')

        if self.name:
            if len(self.name) == 13:
                self.name = self.name[:-1]
            picking = self.env['stock.picking'].search([('name', '=', self.name)], limit=1)
            if picking:
                self.partner_id = picking.partner_id
                self.purchase_id = picking.purchase_id.id

                self.date_approve = picking.date_done

            else:
                raise UserError('Nomor DO tidak ditemukan')