diff options
| author | Azka Nathan <darizkyfaz@gmail.com> | 2025-02-25 13:51:45 +0700 |
|---|---|---|
| committer | Azka Nathan <darizkyfaz@gmail.com> | 2025-02-25 13:51:45 +0700 |
| commit | a24d7b2a21ff24e31eef77e2032a861b7eb13e31 (patch) | |
| tree | a3f69b77ef67739275f99fa774fab6d002742a64 | |
| parent | c9f221da247bb35c0c1005e5d8888d7ab1a4bf37 (diff) | |
fix bug
| -rwxr-xr-x | indoteknik_custom/models/purchase_order.py | 71 | ||||
| -rw-r--r-- | indoteknik_custom/models/purchase_order_sales_match.py | 5 |
2 files changed, 51 insertions, 25 deletions
diff --git a/indoteknik_custom/models/purchase_order.py b/indoteknik_custom/models/purchase_order.py index f2401dec..54d771ba 100755 --- a/indoteknik_custom/models/purchase_order.py +++ b/indoteknik_custom/models/purchase_order.py @@ -943,42 +943,63 @@ class PurchaseOrder(models.Model): def compute_total_margin_from_apo(self): sum_so_margin = sum_sales_price = sum_margin = 0 - for line in self.order_line: - sales_order_line = self.env['purchase.order.sales.match'].search([ + for line in self.order_sales_match_line: + po_line = self.env['purchase.order.line'].search([ ('product_id', '=', line.product_id.id), - ('purchase_order_id', '=', line.order_id.id) - ]) - - for so_line in sales_order_line: - sale_order_line = so_line.sale_line_id - sum_so_margin += sale_order_line.item_margin - sales_price = sale_order_line.price_reduce_taxexcl * sale_order_line.product_uom_qty + ('order_id', '=', line.purchase_order_id.id) + ], limit=1) + 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') + + if sale_order_line and po_line: + so_margin = (line.qty_po / line.qty_so) * sale_order_line.item_margin + sum_so_margin += so_margin + + sales_price = sale_order_line.price_reduce_taxexcl * line.qty_po if sale_order_line.order_id.shipping_cost_covered == 'indoteknik': - sales_price -= sale_order_line.delivery_amt_line + sales_price -= (sale_order_line.delivery_amt_line / sale_order_line.product_uom_qty) * line.qty_po if sale_order_line.order_id.fee_third_party > 0: - sales_price -= sale_order_line.fee_third_party_line + sales_price -= (sale_order_line.fee_third_party_line / sale_order_line.product_uom_qty) * line.qty_po sum_sales_price += sales_price - purchase_price = line.price_subtotal - if line.order_id.total_delivery_amt > 0 and line.order_id.total_cost_service > 0: - if line.taxes_id.id == 22: - ending_price = line.ending_price / 1.11 - purchase_price = ending_price - else: - purchase_price = line.ending_price - # purchase_price = line.price_subtotal - if line.order_id.delivery_amount > 0: - purchase_price += line.delivery_amt_line - if line.order_id.delivery_amt > 0: - purchase_price += line.order_id.delivery_amt - real_item_margin = sales_price - purchase_price - sum_margin += real_item_margin + + purchase_price = po_line.price_subtotal + if po_line.ending_price > 0: + if po_line.taxes_id.id == 22: + ending_price = po_line.ending_price / 1.11 + purchase_price = ending_price + else: + purchase_price = po_line.ending_price + if line.purchase_order_id.delivery_amount > 0: + purchase_price += (po_line.delivery_amt_line / po_line.product_qty) * line.qty_po + if line.purchase_order_id.delivery_amt > 0: + purchase_price += line.purchase_order_id.delivery_amt + 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 + 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 diff --git a/indoteknik_custom/models/purchase_order_sales_match.py b/indoteknik_custom/models/purchase_order_sales_match.py index 4ebe959b..ed013dd5 100644 --- a/indoteknik_custom/models/purchase_order_sales_match.py +++ b/indoteknik_custom/models/purchase_order_sales_match.py @@ -26,6 +26,11 @@ class PurchaseOrderSalesMatch(models.Model): margin_deduct = fields.Float(string='After Deduct', compute='_compute_delivery_amt') purchase_price_so = fields.Float(string='Purchase Price Sale Order', related='sale_line_id.purchase_price') purchase_price_po = fields.Float('Purchase Price PO', compute='_compute_purchase_price_po') + purchase_line_id = fields.Many2one('purchase.order.line', string='Purchase Line', compute='_compute_purchase_line_id') + + def _compute_purchase_line_id(self): + for line in self: + line.purchase_line_id = self.env['purchase.order.line'].search([('order_id', '=', line.purchase_order_id.id), ('product_id', '=', line.product_id.id)]) def _compute_purchase_price_po(self): for line in self: |
