Commit 5cdce3aa by François C.

Fix final price computation (shelf_new_price), using all possible coeffs

parent 269b53c3
Pipeline #2584 passed with stage
in 1 minute 28 seconds
...@@ -48,7 +48,9 @@ class Order(models.Model): ...@@ -48,7 +48,9 @@ class Order(models.Model):
return result return result
def get_lines(self, forExport=False, withNullQty=False): def get_lines(self, forExport=False, withNullQty=False):
lines_data = {'lines': None, 'used_coeffs': None}
f = ['id', 'product_id', 'package_qty', 'product_qty_package', 'product_qty', 'product_uom', 'price_unit', 'partner_id'] f = ['id', 'product_id', 'package_qty', 'product_qty_package', 'product_qty', 'product_uom', 'price_unit', 'partner_id']
if forExport is True: if forExport is True:
f += ['discount', 'price_subtotal', 'price_tax', 'taxes_id'] f += ['discount', 'price_subtotal', 'price_tax', 'taxes_id']
...@@ -66,13 +68,16 @@ class Order(models.Model): ...@@ -66,13 +68,16 @@ class Order(models.Model):
# Adding barcode and other data for every purchased product # Adding barcode and other data for every purchased product
f = ['barcode', 'product_tmpl_id', 'shelf_id'] f = ['barcode', 'product_tmpl_id', 'shelf_id']
if forExport is False: # i.e for reception if forExport is False: # i.e for reception
f += ['taxes_id', 'standard_price', 'coeff9_inter'] f += ['taxes_id', 'standard_price']
# coeff = self.get_coop_main_coeff() : non, car ne prend en compte que la marge principale # add the 9 product coeffs
for i in range(1,10):
f.append('coeff' + str(i) + '_id')
c = [['id', 'in', pids]] c = [['id', 'in', pids]]
res_bc = self.o_api.search_read('product.product', c, f) res_bc = self.o_api.search_read('product.product', c, f)
tmpl_ids = [] tmpl_ids = []
if res_bc: if res_bc:
taxes = {} taxes = {} # Needed to add tax coeff for each product
res_tax = self.get_taxes_data_for_lines(res_bc) res_tax = self.get_taxes_data_for_lines(res_bc)
if res_tax: if res_tax:
for tax in res_tax: for tax in res_tax:
...@@ -92,26 +97,26 @@ class Order(models.Model): ...@@ -92,26 +97,26 @@ class Order(models.Model):
if len(shelf_ids) > 0: if len(shelf_ids) > 0:
shelfs_sortorder = Shelfs.get_shelfs_sortorder(shelf_ids) shelfs_sortorder = Shelfs.get_shelfs_sortorder(shelf_ids)
found_coeffs_ids = [] # Extract from all products, to make a unique query after loop
for l in res_bc: for l in res_bc:
for p in res: for p in res:
if p['product_id'][0] == l['id']: if p['product_id'][0] == l['id']:
# coop_logger.info(str(l)) coop_logger.info(str(l))
p['shelf_sortorder'] = 'X' p['shelf_sortorder'] = 'X'
p['barcode'] = l['barcode'] p['barcode'] = l['barcode']
p['product_tmpl_id'] = l['product_tmpl_id'][0] p['product_tmpl_id'] = l['product_tmpl_id'][0]
if ('standard_price' in l): if ('standard_price' in l):
p['p_price'] = l['standard_price'] p['p_price'] = l['standard_price']
p_coeff = None # Let's add product coeff order and id (needed to compute sale price in further operations : new_shelf_price for ex.)
try: for i in range(1,10):
# from standard_price to public price (Excluding taxes) if l['coeff' + str(i) + '_id'] is not False:
coeff = round(l['coeff9_inter'] / l['standard_price'], 2) coeff_id = l['coeff' + str(i) + '_id'][0]
# coeff9_inter is price at the last coeff. level p['coeff' + str(i) + '_id'] = coeff_id
tax_coeff = (1 + (float(taxes[str(l['taxes_id'][0])]))/100)
# Set total coeff (margin and taxes) if coeff_id not in found_coeffs_ids:
p_coeff = coeff * tax_coeff found_coeffs_ids.append(coeff_id)
except Exception as e:
coop_logger.warning('order get_lines : %s', str(e)) p['tax_coeff'] = (1 + (float(taxes[str(l['taxes_id'][0])]))/100)
p['p_coeff'] = p_coeff
if l['shelf_id'] is not False: if l['shelf_id'] is not False:
for s in shelfs_sortorder: for s in shelfs_sortorder:
...@@ -121,6 +126,7 @@ class Order(models.Model): ...@@ -121,6 +126,7 @@ class Order(models.Model):
p['shelf_sortorder'] = 'X' p['shelf_sortorder'] = 'X'
tmpl_ids.append(l['product_tmpl_id'][0]) tmpl_ids.append(l['product_tmpl_id'][0])
used_coeffs = self.o_api.search_read('product.coefficient', [['id', 'in', found_coeffs_ids]], ['operation_type', 'value'])
# Adding indicative_package for every product # Adding indicative_package for every product
f = ['indicative_package','product_tmpl_id','product_code'] f = ['indicative_package','product_tmpl_id','product_code']
...@@ -138,8 +144,9 @@ class Order(models.Model): ...@@ -138,8 +144,9 @@ class Order(models.Model):
except Exception as e: except Exception as e:
# if product is not active, it is not included in res_bc result # if product is not active, it is not included in res_bc result
p['active'] = False p['active'] = False
lines_data['lines'] = res
return res lines_data['used_coeffs'] = used_coeffs
return lines_data
def get_taxes_data_for_lines(self, lines): def get_taxes_data_for_lines(self, lines):
taxes_id = [] taxes_id = []
...@@ -161,7 +168,8 @@ class Order(models.Model): ...@@ -161,7 +168,8 @@ class Order(models.Model):
c = [['id', '=', self.id]] c = [['id', '=', self.id]]
order = self.o_api.search_read('purchase.order', c, f) order = self.o_api.search_read('purchase.order', c, f)
if order: if order:
lines = self.get_lines(forExport=True) lines_data = self.get_lines(forExport=True)
lines = lines_data['lines']
res['taxes'] = self.get_taxes_data_for_lines(lines) res['taxes'] = self.get_taxes_data_for_lines(lines)
res['order'] = order[0] res['order'] = order[0]
res['lines'] = lines res['lines'] = lines
...@@ -219,7 +227,8 @@ class Order(models.Model): ...@@ -219,7 +227,8 @@ class Order(models.Model):
import re import re
fixed_prefix = getattr(settings, 'FIXED_BARCODE_PREFIX', '0490') fixed_prefix = getattr(settings, 'FIXED_BARCODE_PREFIX', '0490')
labels_data = {'total': 0, 'details': []} labels_data = {'total': 0, 'details': []}
lines = self.get_lines() lines_data = self.get_lines()
lines = lines_data['lines']
bc_pattern = re.compile('^' + fixed_prefix) bc_pattern = re.compile('^' + fixed_prefix)
for l in lines: for l in lines:
if ('barcode' in l) and not (bc_pattern.match(str(l['barcode'])) is None): if ('barcode' in l) and not (bc_pattern.match(str(l['barcode'])) is None):
...@@ -346,7 +355,8 @@ class Orders(models.Model): ...@@ -346,7 +355,8 @@ class Orders(models.Model):
try: try:
fixed_prefix = getattr(settings, 'FIXED_BARCODE_PREFIX', '0490') fixed_prefix = getattr(settings, 'FIXED_BARCODE_PREFIX', '0490')
bc_pattern = re.compile('^' + fixed_prefix) bc_pattern = re.compile('^' + fixed_prefix)
for l in Orders.get_lines(oids): lines_data = Orders.get_lines(oids)
for l in lines_data['lines']:
if not (bc_pattern.match(str(l['barcode'])) is None): if not (bc_pattern.match(str(l['barcode'])) is None):
if not (l['product_tmpl_id'] in labels_data): if not (l['product_tmpl_id'] in labels_data):
labels_data[l['product_tmpl_id']] = 0 labels_data[l['product_tmpl_id']] = 0
......
...@@ -74,9 +74,9 @@ class CagetteReception(models.Model): ...@@ -74,9 +74,9 @@ class CagetteReception(models.Model):
def implies_scale_file_generation(self): def implies_scale_file_generation(self):
answer = False answer = False
lines = Order(self.id).get_lines() lines_data = Order(self.id).get_lines()
bc_pattern = re.compile('^0493|0499') bc_pattern = re.compile('^0493|0499')
for l in lines: for l in lines_data['lines']:
if not (bc_pattern.match(str(l['barcode'])) is None): if not (bc_pattern.match(str(l['barcode'])) is None):
answer = True answer = True
# print ('answer=' + str(answer)) # print ('answer=' + str(answer))
...@@ -131,7 +131,8 @@ class CagetteReception(models.Model): ...@@ -131,7 +131,8 @@ class CagetteReception(models.Model):
""" """
import json import json
processed_lines = 0 processed_lines = 0
order_lines = CagetteReception.get_order_lines_by_po(self.id, nullQty=True) order_lines_data = CagetteReception.get_order_lines_by_po(self.id, nullQty=True)
order_lines = order_lines_data['lines']
received_products = {} received_products = {}
for p in order_lines: for p in order_lines:
received_products[p['product_id'][0]] = p['product_qty'] received_products[p['product_id'][0]] = p['product_qty']
...@@ -176,7 +177,8 @@ class CagetteReception(models.Model): ...@@ -176,7 +177,8 @@ class CagetteReception(models.Model):
def update_products_price(self): def update_products_price(self):
processed = 0 processed = 0
errors = [] errors = []
order_lines = CagetteReception.get_order_lines_by_po(self.id) order_lines_data = CagetteReception.get_order_lines_by_po(self.id)
order_lines = order_lines_data['lines']
if order_lines and len(order_lines) > 0: if order_lines and len(order_lines) > 0:
# Exceptions are due to the fact API returns None whereas the action is really done !... # Exceptions are due to the fact API returns None whereas the action is really done !...
marshal_none_error = 'cannot marshal None unless allow_none is enabled' marshal_none_error = 'cannot marshal None unless allow_none is enabled'
......
...@@ -15,7 +15,8 @@ Sémantiquement, ici : ...@@ -15,7 +15,8 @@ Sémantiquement, ici :
* If 1 element: single order * If 1 element: single order
*/ */
var orders = {}, var orders = {},
group_ids = []; group_ids = [],
product_coeffs = [];
var reception_status = null, var reception_status = null,
list_to_process = [], list_to_process = [],
...@@ -267,6 +268,13 @@ function resetPopUpButtons() { ...@@ -267,6 +268,13 @@ function resetPopUpButtons() {
/* FETCH SERVER DATA */ /* FETCH SERVER DATA */
function store_received_product_coeffs(coeffs) {
for (let i=0;i<coeffs.length;i++) {
if(product_coeffs.indexOf(coeffs[i]) == -1)
product_coeffs.push(coeffs[i])
}
}
/** /**
* Get order(s) data from server * Get order(s) data from server
* @param {Array} po_ids if set, fetch data for these po only * @param {Array} po_ids if set, fetch data for these po only
...@@ -285,6 +293,7 @@ function fetch_data(po_ids = null) { ...@@ -285,6 +293,7 @@ function fetch_data(po_ids = null) {
success: function(data) { success: function(data) {
// for each order // for each order
for (order_data of data.orders) { for (order_data of data.orders) {
store_received_product_coeffs(order_data.used_coeffs);
// for each product in order // for each product in order
for (i in order_data.po) { for (i in order_data.po) {
// If in step 2, find old qty in previous step data // If in step 2, find old qty in previous step data
...@@ -1345,19 +1354,31 @@ function editProductInfo (productToEdit, value = null, batch = false) { ...@@ -1345,19 +1354,31 @@ function editProductInfo (productToEdit, value = null, batch = false) {
if (reception_status == "qty_valid" && productToEdit.price_unit != newValue) { if (reception_status == "qty_valid" && productToEdit.price_unit != newValue) {
if (index == -1) { // First update if (index == -1) { // First update
productToEdit.old_price_unit = productToEdit.price_unit; productToEdit.old_price_unit = productToEdit.price_unit;
productToEdit.new_shelf_price = null; productToEdit.new_shelf_price = parseFloat(newValue);
try {
if (! isNaN(productToEdit.p_coeff)) { // Let's compute product final price, using coeffs.
try { for (let k = 1; k <10; k++) {
new_shelf_price = parseFloat(newValue * productToEdit.p_coeff); if (typeof productToEdit['coeff' + k + '_id'] !== "undefined") {
old_shelf_price = parseFloat(productToEdit.p_price * productToEdit.p_coeff); product_coeffs.forEach(
if (Math.abs(new_shelf_price - old_shelf_price) > 0.001) (coeff) => {
productToEdit.new_shelf_price = new_shelf_price.toFixed(2); if (coeff.id == productToEdit['coeff' + k + '_id']) {
} catch (e) { if (coeff.operation_type == "fixed") {
err = {msg: e.name + ' : ' + e.message, ctx: 'computing new_shelf_price'}; productToEdit.new_shelf_price += coeff.value;
console.error(err); } else if (coeff.operation_type == "multiplier") {
report_JS_error(err, 'reception'); productToEdit.new_shelf_price *= (1 + coeff.value);
}
}
}
)
}
} }
productToEdit.new_shelf_price *= productToEdit.tax_coeff;
productToEdit.new_shelf_price = productToEdit.new_shelf_price.toFixed(2);
} catch (e) {
productToEdit.new_shelf_price = null;
err = {msg: e.name + ' : ' + e.message, ctx: 'computing new_shelf_price'};
console.error(err);
report_JS_error(err, 'reception');
} }
firstUpdate = true; firstUpdate = true;
......
...@@ -73,8 +73,9 @@ def get_list_orders(request): ...@@ -73,8 +73,9 @@ def get_list_orders(request):
} }
if get_order_lines is True: if get_order_lines is True:
order_lines = CagetteReception.get_order_lines_by_po(int(order["id"]), nullQty = True) order_lines_data = CagetteReception.get_order_lines_by_po(int(order["id"]), nullQty = True)
ligne["po"] = order_lines ligne["po"] = order_lines_data['lines']
ligne["used_coeffs"] = order_lines_data['used_coeffs']
orders.append(ligne) orders.append(ligne)
...@@ -124,17 +125,17 @@ def produits(request, id): ...@@ -124,17 +125,17 @@ def produits(request, id):
def get_order_lines(request, id_po): def get_order_lines(request, id_po):
"""Send content of an order""" """Send content of an order"""
order_lines = CagetteReception.get_order_lines_by_po(int(id_po)) order_lines_data = CagetteReception.get_order_lines_by_po(int(id_po))
return JsonResponse({'id_po': id_po, 'po': order_lines}) return JsonResponse({'id_po': id_po, 'po': order_lines_data['lines'], 'used_coeffs': order_lines_data['used_coeffs']})
def get_orders_lines(request): def get_orders_lines(request):
"""Send content of multiple orders""" """Send content of multiple orders"""
data = json.loads(request.body.decode()) data = json.loads(request.body.decode())
orders = [] orders = []
for id_po in data['po_ids']: for id_po in data['po_ids']:
order_lines = CagetteReception.get_order_lines_by_po(int(id_po), nullQty = True) order_lines_data = CagetteReception.get_order_lines_by_po(int(id_po), nullQty = True)
orders.append({'id_po': id_po, 'po': order_lines}) orders.append({'id_po': id_po, 'po': order_lines_data['lines'], 'used_coeffs': order_lines_data['used_coeffs']})
return JsonResponse({'orders': orders}) return JsonResponse({'orders': orders})
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment