From 9dc7af549efd30d2afa9568480607592d42ca04a Mon Sep 17 00:00:00 2001 From: stephanchrst Date: Fri, 2 Aug 2024 10:50:53 +0700 Subject: test case --- indoteknik_custom/models/sale_order.py | 39 ++++++++++ indoteknik_custom/models/sale_order_line.py | 106 +++++++++++++++++++++------- 2 files changed, 120 insertions(+), 25 deletions(-) (limited to 'indoteknik_custom/models') diff --git a/indoteknik_custom/models/sale_order.py b/indoteknik_custom/models/sale_order.py index 44e4a886..d6f04607 100755 --- a/indoteknik_custom/models/sale_order.py +++ b/indoteknik_custom/models/sale_order.py @@ -834,5 +834,44 @@ class SaleOrder(models.Model): } } + def calculate_selling_price(self): + for order_line in self.order_line: + rec_purchase_price, rec_taxes, 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), + ('product_id.id', '=', order_line.product_id.id), + ('order_id.state', 'in', state), + ('id', '!=', order_line.id) + ], limit=1, order='create_date desc') + # if rec_vendor_id == self.vendor_id and rec_purchase_price == last_so.purchase_price: + # selling_price = last_so.price_unit + # tax_id = last_so.tax_id + if rec_vendor_id == order_line.vendor_id.id and rec_purchase_price != last_so.purchase_price: + if rec_taxes.price_include: + selling_price = (rec_purchase_price / 1.11) / (1 - (last_so.line_item_margin / 100)) + else: + selling_price = rec_purchase_price / (1 - (last_so.line_item_margin / 100)) + tax_id = last_so.tax_id + discount = 0 + elif 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), + ('product_id.id', '=', order_line.product_id.id), + ('order_id.state', 'in', state), + ('vendor_id', '=', rec_vendor_id), + ('id', '!=', order_line.id) + ], limit=1, order='create_date desc') + selling_price = last_so.price_unit + tax_id = last_so.tax_id + discount = last_so.discount + else: + selling_price = last_so.price_unit + tax_id = last_so.tax_id + discount = last_so.discount + order_line.price_unit = selling_price + order_line.tax_id = tax_id + order_line.discount = discount + print(1) diff --git a/indoteknik_custom/models/sale_order_line.py b/indoteknik_custom/models/sale_order_line.py index 1f90b821..af3b4adc 100644 --- a/indoteknik_custom/models/sale_order_line.py +++ b/indoteknik_custom/models/sale_order_line.py @@ -40,8 +40,6 @@ class SaleOrderLine(models.Model): for match_so in matches_so: match_so.note_procurement = line.note_procurement - - @api.onchange('product_uom', 'product_uom_qty') def product_uom_change(self): @@ -63,9 +61,9 @@ class SaleOrderLine(models.Model): line.qty_reserved = reserved_qty def _compute_reserved_from(self): - for line in self: - report_stock_forecasted = self.env['report.stock.report_product_product_replenishment'] - report_stock_forecasted._get_report_data(False, [line.product_id.id]) + for line in self: + report_stock_forecasted = self.env['report.stock.report_product_product_replenishment'] + report_stock_forecasted._get_report_data(False, [line.product_id.id]) def _compute_vendor_subtotal(self): for line in self: @@ -105,59 +103,117 @@ class SaleOrderLine(models.Model): @api.onchange('vendor_id') def onchange_vendor_id(self): + # TODO : need to change this logic @stephan if not self.product_id or self.product_id.type == 'service': return elif self.product_id.categ_id.id == 34: # finish good / manufacturing only cost = self.product_id.standard_price self.purchase_price = cost elif self.product_id.x_manufacture.override_vendor_id: - purchase_price = self.env['purchase.pricelist'].search( - [('vendor_id', '=', self.product_id.x_manufacture.override_vendor_id.id), - ('product_id', '=', self.product_id.id)], - limit=1, order='count_trx_po desc, count_trx_po_vendor desc') - price, taxes = self._get_valid_purchase_price(purchase_price) + # purchase_price = self.env['purchase.pricelist'].search( + # [('vendor_id', '=', self.product_id.x_manufacture.override_vendor_id.id), + # ('product_id', '=', self.product_id.id)], + # limit=1, order='count_trx_po desc, count_trx_po_vendor desc') + price, taxes, vendor_id = self._get_purchase_price_by_vendor(self.product_id, self.vendor_id) self.purchase_price = price self.purchase_tax_id = taxes + # else: + # purchase_price = self.env['purchase.pricelist'].search( + # [('vendor_id', '=', self.vendor_id.id), ('product_id', '=', self.product_id.id)], + # limit=1, order='count_trx_po desc, count_trx_po_vendor desc') + # price, taxes = self._get_valid_purchase_price(purchase_price) + # self.purchase_price = price + # self.purchase_tax_id = taxes + + def _calculate_selling_price(self): + rec_purchase_price, rec_taxes, rec_vendor_id = self._get_purchase_price(self.product_id) + state = ['sale', 'done'] + last_so = self.env['sale.order.line'].search([ + ('order_id.partner_id.id', '=', self.order_id.partner_id.id), + ('product_id.id', '=', self.product_id.id), + ('order_id.state', 'in', state) + ], limit=1, order='create_date desc') + # if rec_vendor_id == self.vendor_id and rec_purchase_price == last_so.purchase_price: + # selling_price = last_so.price_unit + # tax_id = last_so.tax_id + if rec_vendor_id == self.vendor_id and rec_purchase_price != last_so.purchase_price: + if rec_taxes.price_include: + selling_price = (rec_purchase_price/1.11) / (1-(last_so.line_item_margin / 100)) + else: + selling_price = rec_purchase_price / (1-(last_so.line_item_margin / 100)) + tax_id = last_so.tax_id + elif rec_vendor_id != last_so.vendor_id: + last_so = self.env['sale.order.line'].search([ + ('order_id.partner_id.id', '=', self.order_id.partner_id.id), + ('product_id.id', '=', self.product_id.id), + ('state', 'in', state), + ('vendor_id', '=', rec_vendor_id) + ], limit=1, order='order_id.date_order desc') + selling_price = last_so.price_unit + tax_id = last_so.tax_id else: - purchase_price = self.env['purchase.pricelist'].search( - [('vendor_id', '=', self.vendor_id.id), ('product_id', '=', self.product_id.id)], - limit=1, order='count_trx_po desc, count_trx_po_vendor desc') - price, taxes = self._get_valid_purchase_price(purchase_price) - self.purchase_price = price - self.purchase_tax_id = taxes + selling_price = last_so.price_unit + tax_id = last_so.tax_id + self.price_unit = selling_price + self.tax_id = tax_id + print(1) + + def _get_purchase_price(self, product_id): + purchase_price = self.env['purchase.pricelist'].search( + [('product_id', '=', product_id.id), + ('is_winner', '=', True)], + limit=1) + + return self._get_valid_purchase_price(purchase_price) + + def _get_purchase_price_by_vendor(self, product_id, vendor_id): + purchase_price = self.env['purchase.pricelist'].search( + [('product_id', '=', product_id.id), + ('vendor_id', '=', vendor_id.id), + # ('is_winner', '=', True) + ], + limit=1) + + return self._get_valid_purchase_price(purchase_price) def _get_valid_purchase_price(self, purchase_price): price = 0 taxes = '' + vendor_id = '' human_last_update = purchase_price.human_last_update or datetime.min system_last_update = purchase_price.system_last_update or datetime.min if purchase_price.taxes_product_id.type_tax_use == 'purchase': price = purchase_price.product_price taxes = purchase_price.taxes_product_id.id + vendor_id = purchase_price.vendor_id.id if system_last_update > human_last_update: if purchase_price.taxes_system_id.type_tax_use == 'purchase': price = purchase_price.system_price taxes = purchase_price.taxes_system_id.id + vendor_id = purchase_price.vendor_id.id - return price, taxes + return price, taxes, vendor_id @api.onchange('product_id') def product_id_change(self): + # TODO need to change purchase price logic @stephan super(SaleOrderLine, self).product_id_change() for line in self: if line.product_id and line.product_id.type == 'product': - query = [('product_id', '=', line.product_id.id)] - if line.product_id.x_manufacture.override_vendor_id: - query = [('product_id', '=', line.product_id.id), - ('vendor_id', '=', line.product_id.x_manufacture.override_vendor_id.id)] - purchase_price = self.env['purchase.pricelist'].search( - query, limit=1, order='count_trx_po desc, count_trx_po_vendor desc') - line.vendor_id = purchase_price.vendor_id + # query = [('product_id', '=', line.product_id.id)] + # if line.product_id.x_manufacture.override_vendor_id: + # query = [('product_id', '=', line.product_id.id), + # ('vendor_id', '=', line.product_id.x_manufacture.override_vendor_id.id)] + # purchase_price = self.env['purchase.pricelist'].search( + # query, limit=1, order='count_trx_po desc, count_trx_po_vendor desc') + price, taxes, vendor_id = self._get_purchase_price(line.product_id) + line.vendor_id = vendor_id line.tax_id = line.order_id.sales_tax_id - price, taxes = line._get_valid_purchase_price(purchase_price) + # price, taxes = line._get_valid_purchase_price(purchase_price) line.purchase_price = price + line.purchase_tax_id = taxes attribute_values = line.product_id.product_template_attribute_value_ids.mapped('name') attribute_values_str = ', '.join(attribute_values) if attribute_values else '' -- cgit v1.2.3 From 56bb6d2f83a2c8dec98160bed397a26007df7450 Mon Sep 17 00:00:00 2001 From: stephanchrst Date: Fri, 2 Aug 2024 10:51:18 +0700 Subject: fixed test case --- indoteknik_custom/models/sale_order.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'indoteknik_custom/models') diff --git a/indoteknik_custom/models/sale_order.py b/indoteknik_custom/models/sale_order.py index d6f04607..2a930a73 100755 --- a/indoteknik_custom/models/sale_order.py +++ b/indoteknik_custom/models/sale_order.py @@ -836,7 +836,7 @@ class SaleOrder(models.Model): def calculate_selling_price(self): for order_line in self.order_line: - rec_purchase_price, rec_taxes, rec_vendor_id = order_line._get_purchase_price(order_line.product_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), @@ -848,10 +848,11 @@ class SaleOrder(models.Model): # selling_price = last_so.price_unit # tax_id = last_so.tax_id if 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.line_item_margin / 100)) + selling_price = (rec_purchase_price / 1.11) / (1 - (last_so.item_percent_margin / 100)) else: - selling_price = rec_purchase_price / (1 - (last_so.line_item_margin / 100)) + selling_price = rec_purchase_price / (1 - (last_so.item_percent_margin / 100)) tax_id = last_so.tax_id discount = 0 elif rec_vendor_id != last_so.vendor_id.id: @@ -873,5 +874,3 @@ class SaleOrder(models.Model): order_line.tax_id = tax_id order_line.discount = discount print(1) - - -- cgit v1.2.3 From 7229d06c4d4557fd1e92e3afbda789620f5a2d0c Mon Sep 17 00:00:00 2001 From: stephanchrst Date: Fri, 2 Aug 2024 15:32:50 +0700 Subject: fix for test case --- indoteknik_custom/models/sale_order.py | 41 +++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 15 deletions(-) (limited to 'indoteknik_custom/models') diff --git a/indoteknik_custom/models/sale_order.py b/indoteknik_custom/models/sale_order.py index 2a930a73..a4c3d079 100755 --- a/indoteknik_custom/models/sale_order.py +++ b/indoteknik_custom/models/sale_order.py @@ -844,18 +844,8 @@ class SaleOrder(models.Model): ('order_id.state', 'in', state), ('id', '!=', order_line.id) ], limit=1, order='create_date desc') - # if rec_vendor_id == self.vendor_id and rec_purchase_price == last_so.purchase_price: - # selling_price = last_so.price_unit - # tax_id = last_so.tax_id - if 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)) - else: - selling_price = rec_purchase_price / (1 - (last_so.item_percent_margin / 100)) - tax_id = last_so.tax_id - discount = 0 - elif rec_vendor_id != last_so.vendor_id.id: + + 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), ('product_id.id', '=', order_line.product_id.id), @@ -863,9 +853,31 @@ class SaleOrder(models.Model): ('vendor_id', '=', rec_vendor_id), ('id', '!=', order_line.id) ], limit=1, order='create_date desc') - selling_price = last_so.price_unit + 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)) + else: + selling_price = rec_purchase_price / (1 - (last_so.item_percent_margin / 100)) + tax_id = last_so.tax_id + for tax in tax_id: + if tax.price_include: + selling_price = selling_price + (selling_price*11/100) + else: + selling_price = selling_price + discount = 0 + else: + selling_price = last_so.price_unit + tax_id = last_so.tax_id + discount = last_so.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)) + else: + selling_price = rec_purchase_price / (1 - (last_so.item_percent_margin / 100)) tax_id = last_so.tax_id - discount = last_so.discount + discount = 0 else: selling_price = last_so.price_unit tax_id = last_so.tax_id @@ -873,4 +885,3 @@ class SaleOrder(models.Model): order_line.price_unit = selling_price order_line.tax_id = tax_id order_line.discount = discount - print(1) -- cgit v1.2.3 From 3cf53a4a6bc09a41bfd39f243a951e21b060c8ca Mon Sep 17 00:00:00 2001 From: stephanchrst Date: Sat, 3 Aug 2024 09:04:51 +0700 Subject: fix empty data --- indoteknik_custom/models/sale_order.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'indoteknik_custom/models') diff --git a/indoteknik_custom/models/sale_order.py b/indoteknik_custom/models/sale_order.py index a4c3d079..c5689a79 100755 --- a/indoteknik_custom/models/sale_order.py +++ b/indoteknik_custom/models/sale_order.py @@ -866,10 +866,14 @@ class SaleOrder(models.Model): else: selling_price = selling_price discount = 0 - else: + 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 + 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: @@ -878,10 +882,14 @@ class SaleOrder(models.Model): selling_price = rec_purchase_price / (1 - (last_so.item_percent_margin / 100)) tax_id = last_so.tax_id discount = 0 - else: + 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 + discount = order_line.discount order_line.price_unit = selling_price order_line.tax_id = tax_id order_line.discount = discount -- cgit v1.2.3 From fa8a40973dee9f4f4cd2d7908e9a1ed4fb75cfaa Mon Sep 17 00:00:00 2001 From: stephanchrst Date: Fri, 9 Aug 2024 16:53:40 +0700 Subject: add todo --- indoteknik_custom/models/sale_order.py | 3 +++ 1 file changed, 3 insertions(+) (limited to 'indoteknik_custom/models') diff --git a/indoteknik_custom/models/sale_order.py b/indoteknik_custom/models/sale_order.py index c5689a79..5e5d76b4 100755 --- a/indoteknik_custom/models/sale_order.py +++ b/indoteknik_custom/models/sale_order.py @@ -835,6 +835,9 @@ class SaleOrder(models.Model): } def calculate_selling_price(self): + # TODO ongkos kirim, biaya pihak ketiga calculate @stephan + # TODO voucher @stephan + # TODO vendor hilanging child di field SO Line @stephan for order_line in self.order_line: rec_purchase_price, rec_taxes_id, rec_vendor_id = order_line._get_purchase_price(order_line.product_id) state = ['sale', 'done'] -- cgit v1.2.3 From 2788396b4b1ccbb83c287f9bba0d52e7799f7b38 Mon Sep 17 00:00:00 2001 From: stephanchrst Date: Tue, 13 Aug 2024 14:19:02 +0700 Subject: add todo in calculate selling price --- indoteknik_custom/models/sale_order.py | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'indoteknik_custom/models') diff --git a/indoteknik_custom/models/sale_order.py b/indoteknik_custom/models/sale_order.py index 5e5d76b4..0be40fd0 100755 --- a/indoteknik_custom/models/sale_order.py +++ b/indoteknik_custom/models/sale_order.py @@ -838,6 +838,10 @@ class SaleOrder(models.Model): # TODO ongkos kirim, biaya pihak ketiga calculate @stephan # TODO voucher @stephan # TODO vendor hilanging child di field SO Line @stephan + # TODO 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 for order_line in self.order_line: rec_purchase_price, rec_taxes_id, rec_vendor_id = order_line._get_purchase_price(order_line.product_id) state = ['sale', 'done'] @@ -884,6 +888,11 @@ class SaleOrder(models.Model): else: selling_price = rec_purchase_price / (1 - (last_so.item_percent_margin / 100)) tax_id = last_so.tax_id + for tax in tax_id: + if tax.price_include: + selling_price = selling_price + (selling_price * 11 / 100) + else: + selling_price = selling_price discount = 0 elif last_so: selling_price = last_so.price_unit -- cgit v1.2.3 From 1845bfdd4e3bd693d691e505b3dc398c028b7e1b Mon Sep 17 00:00:00 2001 From: stephanchrst Date: Wed, 14 Aug 2024 10:40:12 +0700 Subject: another checkpoint --- indoteknik_custom/models/sale_order.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'indoteknik_custom/models') diff --git a/indoteknik_custom/models/sale_order.py b/indoteknik_custom/models/sale_order.py index 0be40fd0..e71b8ce4 100755 --- a/indoteknik_custom/models/sale_order.py +++ b/indoteknik_custom/models/sale_order.py @@ -837,8 +837,8 @@ class SaleOrder(models.Model): def calculate_selling_price(self): # TODO ongkos kirim, biaya pihak ketiga calculate @stephan # TODO voucher @stephan - # TODO vendor hilanging child di field SO Line @stephan - # TODO button pindahin @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 @@ -890,7 +890,7 @@ class SaleOrder(models.Model): tax_id = last_so.tax_id for tax in tax_id: if tax.price_include: - selling_price = selling_price + (selling_price * 11 / 100) + selling_price = selling_price + (selling_price*11/100) else: selling_price = selling_price discount = 0 -- cgit v1.2.3 From 0831a8dc838f7e6bcf184b3ba9e42fc45c3e4f20 Mon Sep 17 00:00:00 2001 From: stephanchrst Date: Wed, 14 Aug 2024 11:07:44 +0700 Subject: another checkpoint again --- indoteknik_custom/models/sale_order_line.py | 81 ++++++++++++++++------------- 1 file changed, 46 insertions(+), 35 deletions(-) (limited to 'indoteknik_custom/models') diff --git a/indoteknik_custom/models/sale_order_line.py b/indoteknik_custom/models/sale_order_line.py index af3b4adc..52069d62 100644 --- a/indoteknik_custom/models/sale_order_line.py +++ b/indoteknik_custom/models/sale_order_line.py @@ -1,6 +1,6 @@ from odoo import fields, models, api, _ from odoo.exceptions import UserError -from datetime import datetime +from datetime import datetime, timedelta class SaleOrderLine(models.Model): @@ -125,38 +125,37 @@ class SaleOrderLine(models.Model): # self.purchase_price = price # self.purchase_tax_id = taxes - def _calculate_selling_price(self): - rec_purchase_price, rec_taxes, rec_vendor_id = self._get_purchase_price(self.product_id) - state = ['sale', 'done'] - last_so = self.env['sale.order.line'].search([ - ('order_id.partner_id.id', '=', self.order_id.partner_id.id), - ('product_id.id', '=', self.product_id.id), - ('order_id.state', 'in', state) - ], limit=1, order='create_date desc') - # if rec_vendor_id == self.vendor_id and rec_purchase_price == last_so.purchase_price: - # selling_price = last_so.price_unit - # tax_id = last_so.tax_id - if rec_vendor_id == self.vendor_id and rec_purchase_price != last_so.purchase_price: - if rec_taxes.price_include: - selling_price = (rec_purchase_price/1.11) / (1-(last_so.line_item_margin / 100)) - else: - selling_price = rec_purchase_price / (1-(last_so.line_item_margin / 100)) - tax_id = last_so.tax_id - elif rec_vendor_id != last_so.vendor_id: - last_so = self.env['sale.order.line'].search([ - ('order_id.partner_id.id', '=', self.order_id.partner_id.id), - ('product_id.id', '=', self.product_id.id), - ('state', 'in', state), - ('vendor_id', '=', rec_vendor_id) - ], limit=1, order='order_id.date_order desc') - selling_price = last_so.price_unit - tax_id = last_so.tax_id - else: - selling_price = last_so.price_unit - tax_id = last_so.tax_id - self.price_unit = selling_price - self.tax_id = tax_id - print(1) + # def _calculate_selling_price(self): + # rec_purchase_price, rec_taxes, rec_vendor_id = self._get_purchase_price(self.product_id) + # state = ['sale', 'done'] + # last_so = self.env['sale.order.line'].search([ + # ('order_id.partner_id.id', '=', self.order_id.partner_id.id), + # ('product_id.id', '=', self.product_id.id), + # ('order_id.state', 'in', state) + # ], limit=1, order='create_date desc') + # # if rec_vendor_id == self.vendor_id and rec_purchase_price == last_so.purchase_price: + # # selling_price = last_so.price_unit + # # tax_id = last_so.tax_id + # if rec_vendor_id == self.vendor_id and rec_purchase_price != last_so.purchase_price: + # if rec_taxes.price_include: + # selling_price = (rec_purchase_price/1.11) / (1-(last_so.line_item_margin / 100)) + # else: + # selling_price = rec_purchase_price / (1-(last_so.line_item_margin / 100)) + # tax_id = last_so.tax_id + # elif rec_vendor_id != last_so.vendor_id: + # last_so = self.env['sale.order.line'].search([ + # ('order_id.partner_id.id', '=', self.order_id.partner_id.id), + # ('product_id.id', '=', self.product_id.id), + # ('state', 'in', state), + # ('vendor_id', '=', rec_vendor_id) + # ], limit=1, order='order_id.date_order desc') + # selling_price = last_so.price_unit + # tax_id = last_so.tax_id + # else: + # selling_price = last_so.price_unit + # tax_id = last_so.tax_id + # self.price_unit = selling_price + # self.tax_id = tax_id def _get_purchase_price(self, product_id): purchase_price = self.env['purchase.pricelist'].search( @@ -177,28 +176,40 @@ class SaleOrderLine(models.Model): return self._get_valid_purchase_price(purchase_price) def _get_valid_purchase_price(self, purchase_price): + current_time = datetime.now() + delta_time = current_time - timedelta(days=365) + # delta_time = delta_time.strftime('%Y-%m-%d %H:%M:%S') + price = 0 taxes = '' vendor_id = '' human_last_update = purchase_price.human_last_update or datetime.min system_last_update = purchase_price.system_last_update or datetime.min - if purchase_price.taxes_product_id.type_tax_use == 'purchase': + if purchase_price.taxes_product_id.type_tax_use == 'purchase': price = purchase_price.product_price taxes = purchase_price.taxes_product_id.id vendor_id = purchase_price.vendor_id.id + if delta_time > human_last_update: + price = 0 + taxes = '' + vendor_id = '' if system_last_update > human_last_update: if purchase_price.taxes_system_id.type_tax_use == 'purchase': price = purchase_price.system_price taxes = purchase_price.taxes_system_id.id vendor_id = purchase_price.vendor_id.id + if delta_time > system_last_update: + price = 0 + taxes = '' + vendor_id = '' return price, taxes, vendor_id @api.onchange('product_id') def product_id_change(self): - # TODO need to change purchase price logic @stephan + # need to change purchase price logic @stephan super(SaleOrderLine, self).product_id_change() for line in self: if line.product_id and line.product_id.type == 'product': -- cgit v1.2.3 From 513d2b473f4fbf7245c35289e2a3215c5da556a6 Mon Sep 17 00:00:00 2001 From: stephanchrst Date: Wed, 14 Aug 2024 11:47:00 +0700 Subject: finished and ready to use for calculate selling price --- indoteknik_custom/models/sale_order.py | 49 ++++++++++++++++++++++------- indoteknik_custom/models/sale_order_line.py | 23 ++++++++++++++ 2 files changed, 60 insertions(+), 12 deletions(-) (limited to 'indoteknik_custom/models') 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' \ -- cgit v1.2.3