from odoo import fields, models, api, _ from odoo.exceptions import AccessError, UserError, ValidationError from dateutil.relativedelta import relativedelta from datetime import datetime, timedelta import logging from pytz import timezone, utc import io import base64 try: from odoo.tools.misc import xlsxwriter except ImportError: import xlsxwriter _logger = logging.getLogger(__name__) class PurchaseOrder(models.Model): _inherit = 'purchase.order' automatic_purchase_id = fields.Many2one( 'automatic.purchase', string='Automatic Purchase Reference', ondelete='set null', index=True, copy=False ) sale_order_id = fields.Many2one('sale.order', string='Sales Order', copy=False) move_entry_id = fields.Many2one('account.move', string='Journal Entries', copy=False) amount_discount = fields.Monetary( string='Total Discount', compute='_compute_amount_discount', store=True ) biaya_lain_lain = fields.Float( 'Biaya Lain Lain', default=0.0, tracking=True, copy=False ) source = fields.Selection([ ('requisition', 'Requisition'), ('reordering', 'Reordering'), ('purchasing_job', 'Purchasing Job'), ('manual', 'Manual') ], string='Source', default='manual', copy=False) count_journals = fields.Integer('Count Payment', compute='_compute_count_journals') def _compute_count_journals(self): for order in self: journals = self.env['account.move'].search([ ('purchase_order_id', '=', order.id), ('move_type', '!=', 'in_invoice') ]) order.count_journals = len(journals) def action_view_related_journals(self): self.ensure_one() journals = self.env['account.move'].search([ ('purchase_order_id', '=', self.id), ('move_type', '!=', 'in_invoice') ]) return { 'name': 'Journals', 'type': 'ir.actions.act_window', 'res_model': 'account.move', 'view_mode': 'tree,form', 'target': 'current', 'domain': [('id', 'in', journals.ids)], } @api.depends('order_line.price_total', 'biaya_lain_lain') def _amount_all(self): super(PurchaseOrder, self)._amount_all() for order in self: amount_total = order.amount_untaxed + order.amount_tax - order.biaya_lain_lain order.amount_total = order.currency_id.round(amount_total) @api.depends('order_line.discount_amount') def _compute_amount_discount(self): for order in self: order.amount_discount = sum(line.discount_amount for line in order.order_line)