diff options
| author | stephanchrst <stephanchrst@gmail.com> | 2024-09-12 14:17:36 +0700 |
|---|---|---|
| committer | stephanchrst <stephanchrst@gmail.com> | 2024-09-12 14:17:36 +0700 |
| commit | a37bc839612b5162b4446182ac23c1dfd1c3253e (patch) | |
| tree | 6dc7020025cf6eeaea6bc86ce591fd269f34d0de | |
| parent | a8e539c92236453ce7aad06d23cf117f4b7239fc (diff) | |
calculate margin after purchase delivery deduction
| -rw-r--r-- | indoteknik_custom/models/automatic_purchase.py | 6 | ||||
| -rwxr-xr-x | indoteknik_custom/models/purchase_order.py | 11 | ||||
| -rw-r--r-- | indoteknik_custom/models/purchase_order_sales_match.py | 12 | ||||
| -rwxr-xr-x | indoteknik_custom/models/sale_order.py | 26 | ||||
| -rwxr-xr-x | indoteknik_custom/views/purchase_order.xml | 5 | ||||
| -rwxr-xr-x | indoteknik_custom/views/sale_order.xml | 2 |
6 files changed, 59 insertions, 3 deletions
diff --git a/indoteknik_custom/models/automatic_purchase.py b/indoteknik_custom/models/automatic_purchase.py index 1d1322fa..4531cff9 100644 --- a/indoteknik_custom/models/automatic_purchase.py +++ b/indoteknik_custom/models/automatic_purchase.py @@ -293,6 +293,9 @@ class AutomaticPurchase(models.Model): sale_ids_set.add(sale_id_with_salesperson) sale_ids_name.add(sale_order.sale_id.name) + margin_item = sale_order.sale_line_id.item_margin / sale_order.qty_so if sale_order.qty_so else 0 + margin_item = margin_item * sale_order.qty_po + matches_so_line = { 'purchase_order_id': purchase_order.id, 'sale_id': sale_order.sale_id.id, @@ -305,7 +308,8 @@ class AutomaticPurchase(models.Model): 'product_id': sale_order.product_id.id, 'qty_so': sale_order.qty_so, 'qty_po': sale_order.qty_po, - 'margin_so': sale_order.sale_line_id.item_percent_margin + 'margin_so': sale_order.sale_line_id.item_percent_margin, + 'margin_item': margin_item } po_matches_so_line = self.env['purchase.order.sales.match'].create([matches_so_line]) diff --git a/indoteknik_custom/models/purchase_order.py b/indoteknik_custom/models/purchase_order.py index edcbbb19..afa67cf2 100755 --- a/indoteknik_custom/models/purchase_order.py +++ b/indoteknik_custom/models/purchase_order.py @@ -69,6 +69,17 @@ class PurchaseOrder(models.Model): date_done_picking = fields.Datetime(string='Date Done Picking', compute='get_date_done') bills_dp_id = fields.Many2one('account.move', string='Bills DP') grand_total = fields.Monetary(string='Grand Total', help='Amount total + amount delivery', compute='_compute_grand_total') + total_margin_match = fields.Float(string='Total Margin Match', compute='_compute_total_margin_match') + + def _compute_total_margin_match(self): + for purchase in self: + match = self.env['purchase.order.sales.match'] + result = match.read_group( + [('purchase_order_id.id', '=', purchase.id)], + ['margin_item'], + [] + ) + purchase.total_margin_match = result[0].get('margin_item', 0.0) def _compute_grand_total(self): for order in self: diff --git a/indoteknik_custom/models/purchase_order_sales_match.py b/indoteknik_custom/models/purchase_order_sales_match.py index 78581409..d1d929d3 100644 --- a/indoteknik_custom/models/purchase_order_sales_match.py +++ b/indoteknik_custom/models/purchase_order_sales_match.py @@ -20,7 +20,17 @@ class PurchaseOrderSalesMatch(models.Model): product_id = fields.Many2one('product.product', string='Product') qty_so = fields.Float(string='Qty SO') qty_po = fields.Float(string='Qty PO') - margin_so = fields.Float(string='Margin SO') + margin_so = fields.Float(string='Margin SO') + margin_item = fields.Float(string='Margin') + delivery_amt = fields.Float(string='Delivery Amount', compute='_compute_delivery_amt') + margin_deduct = fields.Float(string='After Deduct', compute='_compute_delivery_amt') + + def _compute_delivery_amt(self): + for line in self: + percent_margin = line.margin_item / line.purchase_order_id.total_margin_match \ + if line.purchase_order_id.total_margin_match else 0 + line.delivery_amt = line.purchase_order_id.delivery_amt * percent_margin + line.margin_deduct = line.margin_item - line.delivery_amt @api.onchange('sale_id') def onchange_sale_id(self): diff --git a/indoteknik_custom/models/sale_order.py b/indoteknik_custom/models/sale_order.py index 9506aedb..9a6dbd9e 100755 --- a/indoteknik_custom/models/sale_order.py +++ b/indoteknik_custom/models/sale_order.py @@ -112,6 +112,32 @@ class SaleOrder(models.Model): note_website = fields.Char(string="Note Website") use_button = fields.Boolean(string='Using Calculate Selling Price', copy=False) voucher_shipping_id = fields.Many2one(comodel_name='voucher', string='Voucher Shipping', copy=False) + margin_after_delivery_purchase = fields.Float(string='Margin After Delivery Purchase', compute='_compute_margin_after_delivery_purchase') + percent_margin_after_delivery_purchase = fields.Float(string='% Margin After Delivery Purchase', compute='_compute_margin_after_delivery_purchase') + purchase_delivery_amt = fields.Float(string='Purchase Delivery Amount', compute='_compute_purchase_delivery_amount') + + def _compute_purchase_delivery_amount(self): + for order in self: + match = self.env['purchase.order.sales.match'] + result2 = match.search([ + ('sale_id.id', '=', order.id) + ]) + delivery_amt = 0 + for res in result2: + delivery_amt = res.delivery_amt + order.purchase_delivery_amt = delivery_amt + + def _compute_margin_after_delivery_purchase(self): + for order in self: + order.margin_after_delivery_purchase = order.total_margin - order.purchase_delivery_amt + if order.amount_untaxed == 0: + order.total_percent_margin = 0 + continue + if order.shipping_cost_covered == 'indoteknik': + delivery_amt = order.delivery_amt + else: + delivery_amt = 0 + order.percent_margin_after_delivery_purchase = round((order.margin_after_delivery_purchase / (order.amount_untaxed-delivery_amt-order.fee_third_party)) * 100, 2) def _compute_date_kirim(self): for rec in self: diff --git a/indoteknik_custom/views/purchase_order.xml b/indoteknik_custom/views/purchase_order.xml index 6bc97bbf..346b31a5 100755 --- a/indoteknik_custom/views/purchase_order.xml +++ b/indoteknik_custom/views/purchase_order.xml @@ -34,12 +34,12 @@ <field name="sale_order"/> <field name="is_create_uangmuka"/> <field name="approval_status"/> - <field name="amount_total_without_service"/> </field> <field name="approval_status" position="after"> <field name="revisi_po" invisible="1"/> </field> <field name="incoterm_id" position="after"> + <field name="amount_total_without_service"/> <field name="delivery_amt"/> </field> <field name="currency_id" position="after"> @@ -237,6 +237,9 @@ <field name="product_id"/> <field name="qty_so"/> <field name="qty_po"/> + <field name="margin_item" optional="hide"/> + <field name="delivery_amt" optional="hide"/> + <field name="margin_deduct" optional="hide"/> <field name="margin_so"/> </tree> </field> diff --git a/indoteknik_custom/views/sale_order.xml b/indoteknik_custom/views/sale_order.xml index 58b67348..e772dcae 100755 --- a/indoteknik_custom/views/sale_order.xml +++ b/indoteknik_custom/views/sale_order.xml @@ -69,6 +69,8 @@ <field name="tag_ids" position="after"> <field name="eta_date" readonly="1"/> <field name="flash_sale"/> + <field name="margin_after_delivery_purchase"/> + <field name="percent_margin_after_delivery_purchase"/> </field> <field name="analytic_account_id" position="after"> <field name="customer_type" required="1"/> |
