Commit 6ab2aecd by Damien Moulard

update suppliers info at product info check

parent 48cfbd78
......@@ -112,19 +112,6 @@ function add_product() {
return -1;
}
/*
onst product_ids = products.map(p => p.id);
if (product_ids.length > 0) {
clicked_order_pill.find('.pill_order_name').empty().append(`<i class="fas fa-spinner fa-spin"></i>`);
$.ajax({
type: 'POST',
url: '/products/get_product_for_order_helper',
data: JSON.stringify(product_ids),
dataType:"json",
*/
$.ajax({
type: 'POST',
url: '/products/get_product_for_order_helper',
......@@ -195,9 +182,9 @@ function compute_products_coverage_qties() {
*/
function check_products_data() {
return new Promise((resolve, reject) => {
const product_ids = products.map(p => p.id);
const suppliers_id = selected_suppliers.map(s => s.id);
if (product_ids.length > 0) {
if (suppliers_id.length > 0) {
$.notify(
"Vérfication des informations produits...",
{
......@@ -209,18 +196,29 @@ function check_products_data() {
clicked_order_pill.find('.pill_order_name').empty().append(`<i class="fas fa-spinner fa-spin"></i>`);
$.ajax({
type: 'POST',
url: '/products/get_product_for_order_helper',
data: JSON.stringify(product_ids),
type: 'GET',
url: '/orders/get_supplier_products',
data: {
sids: suppliers_id
},
dataType:"json",
traditional: true,
contentType: "application/json; charset=utf-8",
success: function(data) {
for (let product of data.products) {
for (let product of data.res.products) {
const p_index = products.findIndex(p => p.id == product.id);
// Override products data with new data
// Override products data with new data (without suppliersinfo so we don't override qty)
const updated_suppliersinfo = product.suppliersinfo;
delete product.suppliersinfo;
products[p_index] = { ...products[p_index], ...product };
// Update suppliers info
for (let psi_index in products[p_index].suppliersinfo) {
const updated_psi = updated_suppliersinfo.find(psi => psi.supplier_id == products[p_index].suppliersinfo[psi_index].supplier_id);
products[p_index].suppliersinfo[psi_index].package_qty = updated_psi.package_qty;
products[p_index].suppliersinfo[psi_index].price = updated_psi.price;
}
}
$('.notifyjs-wrapper').trigger('notify-hide');
......@@ -279,8 +277,7 @@ function add_supplier() {
selected_suppliers.push(supplier);
let url = "/orders/get_supplier_products";
url += "?sid=" + encodeURIComponent(supplier.id);
url += "?sids=" + encodeURIComponent(supplier.id);
// Fetch supplier products
$.ajax({
......
......@@ -41,8 +41,8 @@ def get_suppliers(request):
def get_supplier_products(request):
""" Get supplier products """
sid = request.GET.get('sid', '')
res = CagetteProducts.get_products_for_order_helper(sid)
suppliers_id = request.GET.getlist('sids', '')
res = CagetteProducts.get_products_for_order_helper(suppliers_id)
if 'error' in res:
return JsonResponse(res, status=500)
......
......@@ -455,11 +455,11 @@ class CagetteProducts(models.Model):
return res
@staticmethod
def get_products_for_order_helper(supplier_id, pids = []):
def get_products_for_order_helper(supplier_ids, pids = []):
"""
One of the two parameters must be set.
Get products by supplier if a supplier_id is set.
If supplier_id is None, get products specified in pids.
One of the two parameters must be not empty.
Get products by supplier if one or more supplier_id is set.
If supplier_ids is empty, get products specified in pids. In this case, suppliers info won't be fetched.
"""
api = OdooAPI()
res = {}
......@@ -468,10 +468,10 @@ class CagetteProducts(models.Model):
try:
today = datetime.date.today().strftime("%Y-%m-%d")
if supplier_id is not None:
if len(supplier_ids) > 0:
# Get products/supplier relation
f = ["product_tmpl_id", 'date_start', 'date_end', 'package_qty', 'price']
c = [['name', '=', int(supplier_id)]]
f = ["product_tmpl_id", 'date_start', 'date_end', 'package_qty', 'price', 'name']
c = [['name', 'in', [ int(x) for x in supplier_ids]]]
psi = api.search_read('product.supplierinfo', c, f)
# Filter valid data
......@@ -515,10 +515,10 @@ class CagetteProducts(models.Model):
# Add supplier data to product data
for i, fp in enumerate(filtered_products_t):
if supplier_id is not None:
if len(supplier_ids) > 0:
psi_item = next(item for item in psi if item["product_tmpl_id"] is not False and item["product_tmpl_id"][0] == fp["id"])
filtered_products_t[i]['suppliersinfo'] = [{
'supplier_id': int(supplier_id),
'supplier_id': int(psi_item["name"][0]),
'package_qty': psi_item["package_qty"],
'price': psi_item["price"]
}]
......@@ -531,7 +531,7 @@ class CagetteProducts(models.Model):
res["products"] = filtered_products_t
except Exception as e:
coop_logger.error('get_products_for_order_helper %s (%s)', str(e), str(supplier_id))
coop_logger.error('get_products_for_order_helper %s (%s)', str(e))
res["error"] = str(e)
return res
......
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