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

class ReportLogbookBill(models.Model):
    _name = 'report.logbook.bill'
    _description = "Logbook Bill"
    _inherit = ['mail.thread']
    _rec_name = 'name'

    name = fields.Char(string='Name', default='Logbook Bill')
    date = fields.Datetime(string='Date Created')
    date_approve = fields.Datetime(string='Date Approve', tracking=3)
    date_pengajuan = fields.Datetime(string='Date Pengajuan', tracking=3)
    approve_by_finance = fields.Boolean(string='Approve By Finance', tracking=3)
    pengajuan_by = fields.Many2one(comodel_name='res.users', string='Pengajuan By', tracking=3)
    approve_by = fields.Many2one(comodel_name='res.users', string='Approve By', tracking=3)
    created_by = fields.Many2one(comodel_name='res.users', string='Created By', tracking=3)
    report_logbook_bill_line = fields.One2many(
        comodel_name='report.logbook.bill.line',
        inverse_name='report_logbook_bill_id',
        string='Logbook Bill Line'
    )
    state = fields.Selection(
        [('belum_terima', 'Belum Terima'),
         ('terima_sebagian', 'Terima Sebagian'),
         ('terima_semua', 'Sudah di terima semua'),
        ],
        default='terima_semua',
        string='Status',
        tracking=True,
    )

    state_pengajuan = fields.Selection(
        [('pengajuan', 'Pengajuan'),
         ('diajukan', 'Sudah Diajukan'),
        ],
        default='pengajuan',
        string='Status Pengajuan',
        tracking=True,
    )

    count_line = fields.Char(string='Count Line', compute='_compute_count_line')

    @api.depends('report_logbook_bill_line')
    def _compute_count_line(self):
        for rec in self:
            rec.count_line = len(rec.report_logbook_bill_line)

    @api.model
    def create(self, vals):
        vals['name'] = self.env['ir.sequence'].next_by_code('report.logbook.bill') or '0'
        result = super(ReportLogbookBill, self).create(vals)
        return result

    def approve(self):
        current_time = datetime.utcnow()
        if self.env.user.is_accounting:
            self.approve_by_finance = True
            self.date_approve = current_time
            self.approve_by = self.env.user.id
            if any(line.not_exist for line in self.report_logbook_bill_line):
                if all(line.not_exist for line in self.report_logbook_bill_line):
                    self.state = 'belum_terima'
                else:
                    self.state = 'terima_sebagian'
            else:
                self.state = 'terima_semua'
            self.relation_po_to_logbook()
        else:
            if self.env.user.is_logistic_approver:
                self.state_pengajuan = 'diajukan'
                self.date_pengajuan = current_time
                self.pengajuan_by = self.env.user.id
                

    def relation_po_to_logbook(self):
        for line in self.report_logbook_bill_line:
            line.purchase_id.logbook_bill_id = self.id
        
class ReportLogbookBillLine(models.Model):
    _name = 'report.logbook.bill.line'

    name = fields.Char(string='Name')
    logbook_bill_id = fields.Many2one('report.logbook.bill', string='Logbook Bill')
    purchase_id = fields.Many2one('purchase.order', string='Purchase Order')
    invoice = fields.Boolean(string='Invoice')
    faktur_pajak = fields.Boolean(string='FP')
    surat_jalan = fields.Boolean(string='SJ')
    purchase_id = fields.Many2one('purchase.order', string='Purchase Order')
    partner_id = fields.Many2one('res.partner', string='Customer')
    proforma_invoice = fields.Boolean(string='Proforma Inv')
    report_logbook_bill_id = fields.Many2one('report.logbook.bill', string='Logbook Bill')
    not_exist = fields.Boolean(string='Not Exist')
    date_approve = fields.Datetime(string='Date Approve', tracking=3)
    grand_total = fields.Float(string='Grand Total')
    note = fields.Char(string='Note Logistik')
    note_finance = fields.Char(string='Note Finance')