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):
return result
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']
if forExport is True:
f += ['discount', 'price_subtotal', 'price_tax', 'taxes_id']
......@@ -66,13 +68,16 @@ class Order(models.Model):
# Adding barcode and other data for every purchased product
f = ['barcode', 'product_tmpl_id', 'shelf_id']
if forExport is False: # i.e for reception
f += ['taxes_id', 'standard_price', 'coeff9_inter']
# coeff = self.get_coop_main_coeff() : non, car ne prend en compte que la marge principale
f += ['taxes_id', 'standard_price']
# add the 9 product coeffs
for i in range(1,10):
f.append('coeff' + str(i) + '_id')
c = [['id', 'in', pids]]
res_bc = self.o_api.search_read('product.product', c, f)
tmpl_ids = []
if res_bc:
taxes = {}
taxes = {} # Needed to add tax coeff for each product
res_tax = self.get_taxes_data_for_lines(res_bc)
if res_tax:
for tax in res_tax:
......@@ -92,26 +97,26 @@ class Order(models.Model):
if len(shelf_ids) > 0:
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 p in res:
if p['product_id'][0] == l['id']:
# coop_logger.info(str(l))
coop_logger.info(str(l))
p['shelf_sortorder'] = 'X'
p['barcode'] = l['barcode']
p['product_tmpl_id'] = l['product_tmpl_id'][0]
if ('standard_price' in l):
p['p_price'] = l['standard_price']
p_coeff = None
try:
# from standard_price to public price (Excluding taxes)
coeff = round(l['coeff9_inter'] / l['standard_price'], 2)
# coeff9_inter is price at the last coeff. level
tax_coeff = (1 + (float(taxes[str(l['taxes_id'][0])]))/100)
# Set total coeff (margin and taxes)
p_coeff = coeff * tax_coeff
except Exception as e:
coop_logger.warning('order get_lines : %s', str(e))
p['p_coeff'] = p_coeff
# Let's add product coeff order and id (needed to compute sale price in further operations : new_shelf_price for ex.)
for i in range(1,10):
if l['coeff' + str(i) + '_id'] is not False:
coeff_id = l['coeff' + str(i) + '_id'][0]
p['coeff' + str(i) + '_id'] = coeff_id
if coeff_id not in found_coeffs_ids:
found_coeffs_ids.append(coeff_id)
p['tax_coeff'] = (1 + (float(taxes[str(l['taxes_id'][0])]))/100)
if l['shelf_id'] is not False:
for s in shelfs_sortorder:
......@@ -121,6 +126,7 @@ class Order(models.Model):
p['shelf_sortorder'] = 'X'
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
f = ['indicative_package','product_tmpl_id','product_code']
......@@ -138,8 +144,9 @@ class Order(models.Model):
except Exception as e:
# if product is not active, it is not included in res_bc result
p['active'] = False
return res
lines_data['lines'] = res
lines_data['used_coeffs'] = used_coeffs
return lines_data
def get_taxes_data_for_lines(self, lines):
taxes_id = []
......@@ -161,7 +168,8 @@ class Order(models.Model):
c = [['id', '=', self.id]]
order = self.o_api.search_read('purchase.order', c, f)
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['order'] = order[0]
res['lines'] = lines
......@@ -219,7 +227,8 @@ class Order(models.Model):
import re
fixed_prefix = getattr(settings, 'FIXED_BARCODE_PREFIX', '0490')
labels_data = {'total': 0, 'details': []}
lines = self.get_lines()
lines_data = self.get_lines()
lines = lines_data['lines']
bc_pattern = re.compile('^' + fixed_prefix)
for l in lines:
if ('barcode' in l) and not (bc_pattern.match(str(l['barcode'])) is None):
......@@ -346,7 +355,8 @@ class Orders(models.Model):
try:
fixed_prefix = getattr(settings, 'FIXED_BARCODE_PREFIX', '0490')
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 (l['product_tmpl_id'] in labels_data):
labels_data[l['product_tmpl_id']] = 0
......
......@@ -74,9 +74,9 @@ class CagetteReception(models.Model):
def implies_scale_file_generation(self):
answer = False
lines = Order(self.id).get_lines()
lines_data = Order(self.id).get_lines()
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):
answer = True
# print ('answer=' + str(answer))
......@@ -131,7 +131,8 @@ class CagetteReception(models.Model):
"""
import json
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 = {}
for p in order_lines:
received_products[p['product_id'][0]] = p['product_qty']
......@@ -176,7 +177,8 @@ class CagetteReception(models.Model):
def update_products_price(self):
processed = 0
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:
# 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'
......
......@@ -15,7 +15,8 @@ Sémantiquement, ici :
* If 1 element: single order
*/
var orders = {},
group_ids = [];
group_ids = [],
product_coeffs = [];
var reception_status = null,
list_to_process = [],
......@@ -267,6 +268,13 @@ function resetPopUpButtons() {
/* 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
* @param {Array} po_ids if set, fetch data for these po only
......@@ -285,6 +293,7 @@ function fetch_data(po_ids = null) {
success: function(data) {
// for each order
for (order_data of data.orders) {
store_received_product_coeffs(order_data.used_coeffs);
// for each product in order
for (i in order_data.po) {
// If in step 2, find old qty in previous step data
......@@ -1345,19 +1354,31 @@ function editProductInfo (productToEdit, value = null, batch = false) {
if (reception_status == "qty_valid" && productToEdit.price_unit != newValue) {
if (index == -1) { // First update
productToEdit.old_price_unit = productToEdit.price_unit;
productToEdit.new_shelf_price = null;
if (! isNaN(productToEdit.p_coeff)) {
try {
new_shelf_price = parseFloat(newValue * productToEdit.p_coeff);
old_shelf_price = parseFloat(productToEdit.p_price * productToEdit.p_coeff);
if (Math.abs(new_shelf_price - old_shelf_price) > 0.001)
productToEdit.new_shelf_price = new_shelf_price.toFixed(2);
} catch (e) {
err = {msg: e.name + ' : ' + e.message, ctx: 'computing new_shelf_price'};
console.error(err);
report_JS_error(err, 'reception');
productToEdit.new_shelf_price = parseFloat(newValue);
try {
// Let's compute product final price, using coeffs.
for (let k = 1; k <10; k++) {
if (typeof productToEdit['coeff' + k + '_id'] !== "undefined") {
product_coeffs.forEach(
(coeff) => {
if (coeff.id == productToEdit['coeff' + k + '_id']) {
if (coeff.operation_type == "fixed") {
productToEdit.new_shelf_price += coeff.value;
} else if (coeff.operation_type == "multiplier") {
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;
......
......@@ -73,8 +73,9 @@ def get_list_orders(request):
}
if get_order_lines is True:
order_lines = CagetteReception.get_order_lines_by_po(int(order["id"]), nullQty = True)
ligne["po"] = order_lines
order_lines_data = CagetteReception.get_order_lines_by_po(int(order["id"]), nullQty = True)
ligne["po"] = order_lines_data['lines']
ligne["used_coeffs"] = order_lines_data['used_coeffs']
orders.append(ligne)
......@@ -124,17 +125,17 @@ def produits(request, id):
def get_order_lines(request, id_po):
"""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):
"""Send content of multiple orders"""
data = json.loads(request.body.decode())
orders = []
for id_po in data['po_ids']:
order_lines = CagetteReception.get_order_lines_by_po(int(id_po), nullQty = True)
orders.append({'id_po': id_po, 'po': order_lines})
order_lines_data = CagetteReception.get_order_lines_by_po(int(id_po), nullQty = True)
orders.append({'id_po': id_po, 'po': order_lines_data['lines'], 'used_coeffs': order_lines_data['used_coeffs']})
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