from odoo import fields, models, api, _ from odoo.exceptions import AccessError, UserError, ValidationError class PurchaseOrder(models.Model): _inherit = 'purchase.order' sale_order_id = fields.Many2one('sale.order', string='Sale Order') procurement_status = fields.Char(string='Procurement Status', compute='get_procurement_status', readonly=True) total_margin = fields.Float( 'Total Margin', compute='compute_total_margin', help="Total Margin in Sales Order Header") total_percent_margin = fields.Float( 'Total Percent Margin', compute='compute_total_margin', help="Total % Margin in Sales Order Header") approval_status = fields.Selection([ ('pengajuan1', 'Approval Manager'), #siapa? darren - 11 ('pengajuan2', 'Approval Pimpinan'), #akbar - 7 ('approved', 'Approved'), ], string='Approval Status', readonly=True, copy=False, index=True, tracking=3) count_line_product = fields.Float('Count Line Product', compute='compute_count_line_product') delivery_amount = fields.Float('Delivery Amount', compute='compute_delivery_amount') def get_procurement_status(self): for purchase_order in self: product_uom_qty = sum_qty_received = 0 for order_line in purchase_order.order_line: product_uom_qty += order_line.product_uom_qty sum_qty_received += order_line.qty_received if product_uom_qty == sum_qty_received: status = 'Terproses' elif product_uom_qty > sum_qty_received > 0: status = 'Sebagian Diproses' else: status = 'Menunggu Diproses' purchase_order.procurement_status = status def sale_order_sync(self): if not self.sale_order_id: return purchase_orders = self.search(['&', ('sale_order_id', '=', self.sale_order_id.id), ('id', '!=', self.id)]) products_exception = [] for purchase_order in purchase_orders: for order_line in purchase_order.order_line: products_exception.append(order_line.product_id.id) self.order_line.unlink() for order_line in self.sale_order_id.order_line: if order_line.product_id.id and order_line.product_id.id not in products_exception: values = { 'order_id': self.id, 'product_id': order_line.product_id.id, 'name': order_line.product_id.display_name, 'product_qty': order_line.product_qty, } self.env['purchase.order.line'].sudo().create(values) def compute_count_line_product(self): for order in self: count = 0 for line in order.order_line: if line.product_id.type == 'product': count += 1 if count == 0: order.count_line_product = 1 else: order.count_line_product = count def compute_delivery_amount(self): for order in self: amount = 0 for line in order.order_line: if line.product_id.type == 'service': amount += line.price_total order.delivery_amount = amount def compute_total_margin(self): for po in self: po.total_margin = 0 po.total_percent_margin = 0 for po in self: total_margin = total_percent_margin = 0 for line in po.order_line: if not line.product_id: po.total_margin = 0 po.total_percent_margin = 0 continue total_margin += line.item_margin po.total_margin = total_margin if po.amount_untaxed > 0: total_percent_margin = round((total_margin / po.amount_untaxed), 2) * 100 po.total_percent_margin = total_percent_margin def button_confirm(self): res = super(PurchaseOrder, self).button_confirm() # for line in self.order_line: # if line.product_id.id == 232383: # raise UserError(_('Tidak bisa Confirm menggunakan Produk Sementara')) for order in self: approval1 = approval2 = 0 for line in order.order_line: if not line.product_id: continue if (line.item_percent_margin < 15 or line.item_percent_margin == 100) and \ (self.env.user.id != 6 and self.env.user.id != 7): # tyas or akbar approval2 += 1 elif (line.item_percent_margin >= 15 and line.item_percent_margin < 25) and \ (self.env.user.id != 6 and self.env.user.id != 7 and self.env.user.id != 11): # tyas or akbar or darren approval1 += 1 if approval2 > 0: raise UserError("Need Tyas / Akbar Approval") elif approval1 > 0: raise UserError("Need Adela Approval") order.approval_status = 'approved' return res def po_approve(self): for order in self: if order.state == 'cancel' or order.state == 'done' or order.state == 'sale': raise UserError("Status harus draft atau sent") approval1 = approval2 = 0 for line in order.order_line: if not line.product_id: continue if (line.item_percent_margin < 15 or line.item_percent_margin == 100) and \ (self.env.user.id != 6 and self.env.user.id != 7): approval2 += 1 elif (line.item_percent_margin >= 15 and line.item_percent_margin < 25) and \ (self.env.user.id != 6 and self.env.user.id != 7 and self.env.user.id != 11): approval1 += 1 if approval2 > 0: order.approval_status = 'pengajuan2' elif approval1 > 0: order.approval_status = 'pengajuan1' else: raise UserError("Bisa langsung Confirm") def button_cancel(self): res = super(PurchaseOrder, self).button_cancel() self.approval_status = False return res