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')