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