diff options
| author | Azka Nathan <darizkyfaz@gmail.com> | 2024-03-26 13:50:27 +0700 |
|---|---|---|
| committer | Azka Nathan <darizkyfaz@gmail.com> | 2024-03-26 13:50:27 +0700 |
| commit | 935393a6a58b3df18f3361b36a6ea60647cd8be4 (patch) | |
| tree | e4ec9d300aff98a38a8524c209a19f970aa24f23 /indoteknik_custom/models/purchase_order.py | |
| parent | db1780524b1e153338ced116f786b4d712d66aca (diff) | |
| parent | b2377426bec8aa334277aac48b0b25f0dfac420f (diff) | |
Merge branch 'purchasing-job' into production
# Conflicts:
# indoteknik_custom/models/__init__.py
# indoteknik_custom/models/sale_order.py
Diffstat (limited to 'indoteknik_custom/models/purchase_order.py')
| -rwxr-xr-x | indoteknik_custom/models/purchase_order.py | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/indoteknik_custom/models/purchase_order.py b/indoteknik_custom/models/purchase_order.py index dc654196..1cdf5094 100755 --- a/indoteknik_custom/models/purchase_order.py +++ b/indoteknik_custom/models/purchase_order.py @@ -16,6 +16,7 @@ _logger = logging.getLogger(__name__) class PurchaseOrder(models.Model): _inherit = 'purchase.order' + order_sales_match_line = fields.One2many('purchase.order.sales.match', 'purchase_order_id', string='Sales Match Lines', states={'cancel': [('readonly', True)], 'done': [('readonly', True)]}, copy=True) sale_order_id = fields.Many2one('sale.order', string='Sale Order') procurement_status = fields.Char(string='Procurement Status', compute='get_procurement_status', readonly=True) po_status = fields.Selection([ @@ -52,6 +53,7 @@ class PurchaseOrder(models.Model): responsible_ids = fields.Many2many('res.users', string='Responsibles', compute='_compute_responsibles') status_paid_cbd = fields.Boolean(string='Paid Status', tracking=3, help='Field ini diisi secara manual oleh Finance AP dan hanya untuk status PO CBD') revisi_po = fields.Boolean(string='Revisi', tracking=3) + from_apo = fields.Boolean(string='From APO') @api.model def action_multi_cancel(self): @@ -441,6 +443,10 @@ class PurchaseOrder(models.Model): return res def compute_total_margin(self): + if self.from_apo: + self.compute_total_margin_from_apo() + return + sum_so_margin = sum_sales_price = sum_margin = 0 for line in self.order_line: sale_order_line = line.so_line_id @@ -474,6 +480,59 @@ class PurchaseOrder(models.Model): self.total_so_margin = 0 self.total_so_percent_margin = 0 + def compute_total_margin_from_apo(self): + purchase_price_dict = {} + + for lines in self.order_line: + product_id = lines.product_id.id + + if product_id not in purchase_price_dict: + purchase_price_dict[product_id] = lines.price_subtotal + + sum_so_margin = sum_sales_price = sum_margin = 0 + for line in self.order_sales_match_line: + sale_order_line = line.sale_line_id + + if not sale_order_line: + sale_order_line = self.env['sale.order.line'].search([ + ('product_id', '=', line.product_id.id), + ('order_id', '=', line.sale_id.id) + ], limit=1, order='price_reduce_taxexcl') + + sum_so_margin += sale_order_line.item_margin + + sales_price = sale_order_line.price_reduce_taxexcl * sale_order_line.product_uom_qty + + if sale_order_line.order_id.shipping_cost_covered == 'indoteknik': + sales_price -= sale_order_line.delivery_amt_line + + if sale_order_line.order_id.fee_third_party > 0: + sales_price -= sale_order_line.fee_third_party_line + + sum_sales_price += sales_price + + product_id = sale_order_line.product_id.id + + purchase_price = purchase_price_dict.get(product_id, 0) + + if line.purchase_order_id.delivery_amount > 0: + purchase_price += line.delivery_amt_line + + real_item_margin = sales_price - purchase_price + sum_margin += real_item_margin + + + if sum_so_margin != 0 and sum_sales_price != 0 and sum_margin != 0: + self.total_so_margin = sum_so_margin + self.total_so_percent_margin = round((sum_so_margin / sum_sales_price), 2) * 100 + self.total_margin = sum_margin + self.total_percent_margin = round((sum_margin / sum_sales_price), 2) * 100 + else: + self.total_margin = 0 + self.total_percent_margin = 0 + self.total_so_margin = 0 + self.total_so_percent_margin = 0 + def compute_amt_total_without_service(self): for order in self: sum_price_total = 0 |
