diff options
| author | stephanchrst <stephanchrst@gmail.com> | 2024-08-02 10:50:53 +0700 |
|---|---|---|
| committer | stephanchrst <stephanchrst@gmail.com> | 2024-08-02 10:50:53 +0700 |
| commit | 9dc7af549efd30d2afa9568480607592d42ca04a (patch) | |
| tree | 6a679457b0b9af144998c1aed8fc1df8abfe7b08 | |
| parent | 11261e1b5ebd8a0d139abdbf23e985a8fb470eac (diff) | |
test case
| -rwxr-xr-x | indoteknik_custom/models/sale_order.py | 39 | ||||
| -rw-r--r-- | indoteknik_custom/models/sale_order_line.py | 106 |
2 files changed, 120 insertions, 25 deletions
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 '' |
