summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorstephanchrst <stephanchrst@gmail.com>2024-08-14 11:47:00 +0700
committerstephanchrst <stephanchrst@gmail.com>2024-08-14 11:47:00 +0700
commit513d2b473f4fbf7245c35289e2a3215c5da556a6 (patch)
tree13f88944ade0bfc42bc508771198d4f26f129dbc
parent0831a8dc838f7e6bcf184b3ba9e42fc45c3e4f20 (diff)
finished and ready to use for calculate selling price
-rwxr-xr-xindoteknik_custom/models/sale_order.py49
-rw-r--r--indoteknik_custom/models/sale_order_line.py23
2 files changed, 60 insertions, 12 deletions
diff --git a/indoteknik_custom/models/sale_order.py b/indoteknik_custom/models/sale_order.py
index e71b8ce4..5badacba 100755
--- a/indoteknik_custom/models/sale_order.py
+++ b/indoteknik_custom/models/sale_order.py
@@ -108,6 +108,7 @@ class SaleOrder(models.Model):
date_driver_arrival = fields.Datetime(string='Arrival Date', compute='_compute_date_kirim', copy=False)
date_driver_departure = fields.Datetime(string='Departure Date', compute='_compute_date_kirim', copy=False)
note_website = fields.Char(string="Note Website")
+ use_button = fields.Boolean(string='Using Calculate Selling Price', copy=False)
def _compute_date_kirim(self):
for rec in self:
@@ -835,37 +836,57 @@ class SaleOrder(models.Model):
}
def calculate_selling_price(self):
- # TODO ongkos kirim, biaya pihak ketiga calculate @stephan
+ # ongkos kirim, biaya pihak ketiga calculate @stephan
# TODO voucher @stephan
# vendor hilangin child di field SO Line @stephan
# button pindahin @stephan
- # TODO last so 1 tahun ke belakang @stephan
- # TODO pastikan harga beli 1 tahun ke belakang jg
- # TODO counter di klik berapa banyak
+ # last so 1 tahun ke belakang @stephan
+ # pastikan harga beli 1 tahun ke belakang jg
+ # harga yg didapat dari semua kumpulan parent parner dan child nya
+ # counter di klik berapa banyak @stephan
for order_line in self.order_line:
+ if not order_line.product_id:
+ continue
+ current_time = datetime.now()
+ delta_time = current_time - timedelta(days=365)
+ delta_time = delta_time.strftime('%Y-%m-%d %H:%M:%S')
+
+ # Initialize partners list with parent_id or partner_id
+ partners = []
+ parent_id = self.partner_id.parent_id or self.partner_id
+
+ # Add all child_ids and the parent itself to partners as IDs
+ partners.extend(parent_id.child_ids.ids)
+ partners.append(parent_id.id)
+
rec_purchase_price, rec_taxes_id, rec_vendor_id = order_line._get_purchase_price(order_line.product_id)
state = ['sale', 'done']
last_so = self.env['sale.order.line'].search([
- ('order_id.partner_id.id', '=', order_line.order_id.partner_id.id),
+ # ('order_id.partner_id.id', '=', order_line.order_id.partner_id.id),
+ ('order_id.partner_id', 'in', partners),
('product_id.id', '=', order_line.product_id.id),
('order_id.state', 'in', state),
- ('id', '!=', order_line.id)
+ ('id', '!=', order_line.id),
+ ('order_id.date_order', '>=', delta_time)
], limit=1, order='create_date desc')
if rec_vendor_id != last_so.vendor_id.id:
last_so = self.env['sale.order.line'].search([
- ('order_id.partner_id.id', '=', order_line.order_id.partner_id.id),
+ # ('order_id.partner_id.id', '=', order_line.order_id.partner_id.id),
+ ('order_id.partner_id', 'in', partners),
('product_id.id', '=', order_line.product_id.id),
('order_id.state', 'in', state),
('vendor_id', '=', rec_vendor_id),
- ('id', '!=', order_line.id)
+ ('id', '!=', order_line.id),
+ ('order_id.date_order', '>=', delta_time)
], limit=1, order='create_date desc')
+
if rec_purchase_price != last_so.purchase_price:
rec_taxes = self.env['account.tax'].search([('id', '=', rec_taxes_id)], limit=1)
if rec_taxes.price_include:
- selling_price = (rec_purchase_price / 1.11) / (1 - (last_so.item_percent_margin / 100))
+ selling_price = (rec_purchase_price / 1.11) / (1 - (last_so.item_percent_margin_without_deduction / 100))
else:
- selling_price = rec_purchase_price / (1 - (last_so.item_percent_margin / 100))
+ selling_price = rec_purchase_price / (1 - (last_so.item_percent_margin_without_deduction / 100))
tax_id = last_so.tax_id
for tax in tax_id:
if tax.price_include:
@@ -881,12 +902,13 @@ class SaleOrder(models.Model):
selling_price = order_line.price_unit
tax_id = order_line.tax_id
discount = order_line.discount
+
elif rec_vendor_id == order_line.vendor_id.id and rec_purchase_price != last_so.purchase_price:
rec_taxes = self.env['account.tax'].search([('id', '=', rec_taxes_id)], limit=1)
if rec_taxes.price_include:
- selling_price = (rec_purchase_price / 1.11) / (1 - (last_so.item_percent_margin / 100))
+ selling_price = (rec_purchase_price / 1.11) / (1 - (last_so.item_percent_margin_without_deduction / 100))
else:
- selling_price = rec_purchase_price / (1 - (last_so.item_percent_margin / 100))
+ selling_price = rec_purchase_price / (1 - (last_so.item_percent_margin_without_deduction / 100))
tax_id = last_so.tax_id
for tax in tax_id:
if tax.price_include:
@@ -894,10 +916,12 @@ class SaleOrder(models.Model):
else:
selling_price = selling_price
discount = 0
+
elif last_so:
selling_price = last_so.price_unit
tax_id = last_so.tax_id
discount = last_so.discount
+
else:
selling_price = order_line.price_unit
tax_id = order_line.tax_id
@@ -905,3 +929,4 @@ class SaleOrder(models.Model):
order_line.price_unit = selling_price
order_line.tax_id = tax_id
order_line.discount = discount
+ order_line.order_id.use_button = True
diff --git a/indoteknik_custom/models/sale_order_line.py b/indoteknik_custom/models/sale_order_line.py
index 52069d62..a64a744c 100644
--- a/indoteknik_custom/models/sale_order_line.py
+++ b/indoteknik_custom/models/sale_order_line.py
@@ -30,6 +30,7 @@ class SaleOrderLine(models.Model):
amount_voucher_disc = fields.Float(string='Voucher Discount')
qty_reserved = fields.Float(string='Qty Reserved', compute='_compute_qty_reserved')
reserved_from = fields.Char(string='Reserved From', copy=False)
+ item_percent_margin_without_deduction = fields.Float('%Margin', compute='_compute_item_margin_without_deduction')
@api.constrains('note_procurement')
def note_procurement_to_apo(self):
@@ -72,6 +73,28 @@ class SaleOrderLine(models.Model):
else:
line.vendor_subtotal = 0
+ def _compute_item_margin_without_deduction(self):
+ for line in self:
+ if not line.product_id or line.product_id.type == 'service' \
+ or line.price_unit <= 0 or line.product_uom_qty <= 0 \
+ or not line.vendor_id:
+ line.item_percent_margin_without_deduction = 0
+ continue
+ # calculate margin without tax
+ sales_price = line.price_reduce_taxexcl * line.product_uom_qty
+
+ purchase_price = line.purchase_price
+ if line.purchase_tax_id.price_include:
+ purchase_price = line.purchase_price / 1.11
+
+ purchase_price = purchase_price * line.product_uom_qty
+ margin_per_item = sales_price - purchase_price
+
+ if sales_price > 0:
+ line.item_percent_margin_without_deduction = round((margin_per_item / sales_price), 2) * 100
+ else:
+ line.item_percent_margin_without_deduction = 0
+
def compute_item_margin(self):
for line in self:
if not line.product_id or line.product_id.type == 'service' \