Commit 6ab2aecd by Damien Moulard

update suppliers info at product info check

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