summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorstephanchrst <stephanchrst@gmail.com>2022-09-28 16:25:01 +0700
committerstephanchrst <stephanchrst@gmail.com>2022-09-28 16:25:01 +0700
commit597d6458234a14bd20e7fba8a0ceb07168423d2a (patch)
treed3af413c17daa60486fa52ed63d582e3ac66e5da
parent009be5919cb5835f98f5882f69b3a7652e8ec658 (diff)
Update purchase_order.py, purchase_order_line.py, and 3 more files...
-rwxr-xr-xindoteknik_custom/models/purchase_order.py22
-rwxr-xr-xindoteknik_custom/models/purchase_order_line.py86
-rwxr-xr-xindoteknik_custom/models/sale_order.py9
-rwxr-xr-xindoteknik_custom/views/purchase_order.xml12
-rwxr-xr-xindoteknik_custom/views/sale_order.xml2
5 files changed, 91 insertions, 40 deletions
diff --git a/indoteknik_custom/models/purchase_order.py b/indoteknik_custom/models/purchase_order.py
index acc737e8..60121919 100755
--- a/indoteknik_custom/models/purchase_order.py
+++ b/indoteknik_custom/models/purchase_order.py
@@ -14,6 +14,18 @@ class PurchaseOrder(models.Model):
], 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')
+ total_margin = fields.Float(
+ 'Margin', compute='compute_total_margin',
+ help="Total Margin in Sales Order Header")
+ total_percent_margin = fields.Float(
+ 'Margin%', compute='compute_total_margin',
+ help="Total % Margin in Sales Order Header")
+ total_so_margin = fields.Float(
+ 'SO Margin', compute='compute_total_margin',
+ help="Total Margin in Sales Order Header")
+ total_so_percent_margin = fields.Float(
+ 'SO Margin%', compute='compute_total_margin',
+ help="Total % Margin in Sales Order Header")
def get_procurement_status(self):
for purchase_order in self:
@@ -136,3 +148,13 @@ class PurchaseOrder(models.Model):
res = super(PurchaseOrder, self).button_cancel()
self.approval_status = False
return res
+
+ def compute_total_margin(self):
+ if not self.order_line:
+ self.total_margin = 0
+ self.total_percent_margin = 0
+ self.total_so_margin = 0
+ self.total_so_percent_margin = 0
+ return
+ for line in self.order_line:
+ line.compute_item_margin()
diff --git a/indoteknik_custom/models/purchase_order_line.py b/indoteknik_custom/models/purchase_order_line.py
index 4229fcea..fcfab1da 100755
--- a/indoteknik_custom/models/purchase_order_line.py
+++ b/indoteknik_custom/models/purchase_order_line.py
@@ -5,6 +5,18 @@ from odoo.tools import DEFAULT_SERVER_DATETIME_FORMAT
class PurchaseOrderLine(models.Model):
_inherit = 'purchase.order.line'
+ item_margin = fields.Float(
+ 'Margin', compute='compute_item_margin',
+ help="Total Margin in Sales Order Header")
+ item_percent_margin = fields.Float(
+ 'Margin%', compute='compute_item_margin',
+ help="Total % Margin in Sales Order Header")
+ so_item_margin = fields.Float(
+ 'SO Margin', compute='compute_item_margin',
+ help="Total Margin in Sales Order Header")
+ so_item_percent_margin = fields.Float(
+ 'SO Margin%', compute='compute_item_margin',
+ help="Total % Margin in Sales Order Header")
# Override method from addons/purchase/models/purchase.py
@api.onchange('product_qty', 'product_uom')
@@ -27,37 +39,43 @@ class PurchaseOrderLine(models.Model):
self.price_unit = price_unit
return res
- # def compute_item_margin(self):
- # for line in self:
- # if not line.product_id or line.price_unit <= 0 or line.product_uom_qty <= 0 or line.product_id.type == 'service' or not line.order_id.sale_order_id.id:
- # line.item_margin = 0
- # line.item_percent_margin = 0
- # continue
- # sale_order_line = self.env['sale.order.line'].search(
- # [('product_id', '=', line.product_id.id),
- # ('order_id', '=', line.order_id.sale_order_id.id)], limit=1, order='price_reduce_taxexcl')
- #
- # est_purchase_price = sale_order_line.purchase_price
- # real_purchase_price = line.price_unit
- # real_tax = 0
- # real_tax_amount = 0
- # count_real_tax = 0
- # for tax in line.taxes_id:
- # count_real_tax += 1
- # real_tax = tax
- # real_tax_amount += tax.amount
- # if sale_order_line.purchase_tax_id.amount != real_tax_amount or count_real_tax > 1:
- # raise UserError("Beda tax amount dengan Sales")
- #
- # # untaxed sales price
- # sales_price = sale_order_line.price_reduce_taxexcl * sale_order_line.product_uom_qty
- # # must add expedition price in sales_price, expedition payed by customer?
- # # sales_price -= round((line.order_id.sale_order_id.delivery_amount / line.order_id.sale_order_id.count_line_product), 2)
- # # untaxed purchase price
- # purchase_price = line.price_subtotal
- # # must add expedition price in purchase_price as expenses
- # purchase_price += round((line.order_id.delivery_amount / line.order_id.count_line_product), 2)
- # margin_per_item = sales_price - purchase_price
- # line.item_margin = margin_per_item
- # if sales_price > 0:
- # line.item_percent_margin = round((margin_per_item / sales_price), 2) * 100 \ No newline at end of file
+ def compute_item_margin(self):
+ sum_so_margin = sum_sales_price = sum_margin = 0
+ for line in self:
+ if not line.product_id or line.product_id.type == 'service':
+ line.so_item_margin = 0
+ line.so_item_percent_margin = 0
+ line.item_margin = 0
+ line.item_percent_margin = 0
+ continue
+ sale_order_line = self.env['sale.order.line'].search(
+ [('product_id', '=', line.product_id.id),
+ ('order_id', '=', line.order_id.sale_order_id.id)], limit=1, order='price_reduce_taxexcl')
+ # calculate margin so and real margin
+ # margin so compare est purchase price vs sales price
+ # real margin compare purchase price vs sales price
+ line.so_item_margin = sale_order_line.item_margin
+ line.so_item_percent_margin = sale_order_line.item_percent_margin
+ sum_so_margin += sale_order_line.item_margin
+
+ sales_price = sale_order_line.price_reduce_taxexcl * sale_order_line.product_uom_qty
+ # minus with delivery if covered by indoteknik
+ if sale_order_line.order_id.shipping_cost_covered == 'indoteknik':
+ sales_price -= round((sale_order_line.order_id.delivery_amt / sale_order_line.order_id.count_line_product), 2)
+ sum_sales_price += sales_price
+
+ purchase_price = line.price_subtotal
+ if line.order_id.delivery_amount > 0:
+ purchase_price += round((line.order_id.delivery_amount / line.order_id.count_line_product), 2)
+ # add with delivery amount in sales price or purchase price if any
+ real_item_margin = sales_price - purchase_price
+ real_item_percent_margin = round((real_item_margin/sales_price), 2) * 100
+ line.item_margin = real_item_margin
+ line.item_percent_margin = real_item_percent_margin
+ sum_margin += real_item_margin
+
+ if sum_so_margin > 0 and sum_sales_price > 0 and sum_margin > 0:
+ self.order_id.total_so_margin = sum_so_margin
+ self.order_id.total_so_percent_margin = round((sum_so_margin / sum_sales_price), 2) * 100
+ self.order_id.total_margin = sum_margin
+ self.order_id.total_percent_margin = round((sum_margin / sum_sales_price), 2) * 100
diff --git a/indoteknik_custom/models/sale_order.py b/indoteknik_custom/models/sale_order.py
index d1200de2..1442b432 100755
--- a/indoteknik_custom/models/sale_order.py
+++ b/indoteknik_custom/models/sale_order.py
@@ -25,7 +25,7 @@ class SaleOrder(models.Model):
shipping_cost_covered = fields.Selection([
('indoteknik', 'Indoteknik'),
('customer', 'Customer')
- ], string='Shipping Paid by', help='Siapa yang menanggung biaya ekspedisi?', copy=False)
+ ], string='Shipping Covered by', help='Siapa yang menanggung biaya ekspedisi?', copy=False)
shipping_paid_by = fields.Selection([
('indoteknik', 'Indoteknik'),
('customer', 'Customer')
@@ -91,9 +91,9 @@ class SaleOrder(models.Model):
self.env.user.id != 6 and self.env.user.id != 7 and self.env.user.id != 377): # vita
approval1 += 1
if approval2 > 0:
- raise UserError("Harus diapprove oleh Pimpinan (Akbar / Tyas)")
+ raise UserError("Harus diapprove oleh Pimpinan")
elif approval1 > 0:
- raise UserError("Harus diapprove oleh Manager (?)")
+ raise UserError("Harus diapprove oleh Manager")
order.approval_status = 'approved'
return res
@@ -125,7 +125,6 @@ class SaleOrder(models.Model):
@api.onchange('sales_tax_id')
def onchange_sales_tax_id(self):
for line in self.order_line:
- # line.tax_id = self.sales_tax_id
line.product_id_change()
@@ -183,6 +182,6 @@ class SaleOrderLine(models.Model):
def product_id_change(self):
for line in self:
super(SaleOrderLine, self).product_id_change()
- if line.product_id:
+ if line.product_id and line.product_id.type == 'product':
line.tax_id = line.order_id.sales_tax_id
diff --git a/indoteknik_custom/views/purchase_order.xml b/indoteknik_custom/views/purchase_order.xml
index 37b9c0cb..e1f6560c 100755
--- a/indoteknik_custom/views/purchase_order.xml
+++ b/indoteknik_custom/views/purchase_order.xml
@@ -24,6 +24,18 @@
<field name="sale_order_id" attrs="{'readonly': [('state', 'not in', ['draft'])]}"/>
<field name="approval_status"/>
</field>
+ <xpath expr="//form/sheet/notebook/page/field[@name='order_line']/tree/field[@name='price_subtotal']" position="after">
+ <field name="item_margin"/>
+ <field name="so_item_margin"/>
+ <field name="item_percent_margin"/>
+ <field name="so_item_percent_margin"/>
+ </xpath>
+ <field name="amount_total" position="after">
+ <field name="total_margin"/>
+ <field name="total_so_margin"/>
+ <field name="total_percent_margin"/>
+ <field name="total_so_percent_margin"/>
+ </field>
</field>
</record>
</data>
diff --git a/indoteknik_custom/views/sale_order.xml b/indoteknik_custom/views/sale_order.xml
index 17902aca..297d0045 100755
--- a/indoteknik_custom/views/sale_order.xml
+++ b/indoteknik_custom/views/sale_order.xml
@@ -16,10 +16,10 @@
<field name="shipping_cost_covered"/>
<field name="shipping_paid_by"/>
<field name="delivery_amt"/>
- <field name="sales_tax_id" domain="[('type_tax_use','=','sale')]"/>
</field>
<field name="partner_shipping_id" position="after">
<field name="approval_status" />
+ <field name="sales_tax_id" domain="[('type_tax_use','=','sale')]"/>
</field>
<xpath expr="//form/sheet/notebook/page/field[@name='order_line']/tree/field[@name='price_total']" position="after">
<field name="vendor_id" attrs="{'readonly': [('parent.state', 'not in', ['draft', 'sent', 'sale'])]}"/>