Commit faded170 by Damien Moulard

set an end date to supplier-product association instead of removing it

parent 7cea0713
Pipeline #1264 passed with stage
in 1 minute 22 seconds
......@@ -259,7 +259,7 @@ class Order(models.Model):
for line in order_lines:
product_line_name = line["name"]
if line["product_code"] is not False:
if "product_code" in line and line["product_code"] is not False:
product_code = str(line["product_code"])
product_line_name = f"[{product_code}] {product_line_name}"
......
......@@ -497,6 +497,7 @@ function save_supplier_product_association(product, supplier, cell) {
product.suppliersinfo.push({
supplier_id: supplier.id,
package_qty: package_qty,
product_code: false,
price: price
});
......@@ -539,7 +540,7 @@ function save_supplier_product_association(product, supplier, cell) {
* @param {object} product
* @param {object} supplier
*/
function remove_supplier_product_association(product, supplier) {
function end_supplier_product_association(product, supplier) {
openModal();
const data = {
......@@ -550,7 +551,7 @@ function remove_supplier_product_association(product, supplier) {
// Send request to create association
$.ajax({
type: "POST",
url: "/orders/remove_supplier_product_association",
url: "/orders/end_supplier_product_association",
dataType: "json",
traditional: true,
contentType: "application/json; charset=utf-8",
......@@ -572,7 +573,7 @@ function remove_supplier_product_association(product, supplier) {
let msg = "erreur serveur lors de la suppression de l'association product/supplier".
msg += ` (product_tmpl_id: ${product.id}; supplier_id: ${supplier.id})`;
err = {msg: msg, ctx: 'remove_supplier_product_association'};
err = {msg: msg, ctx: 'end_supplier_product_association'};
if (typeof data.responseJSON != 'undefined' && typeof data.responseJSON.error != 'undefined') {
err.msg += ' : ' + data.responseJSON.error;
}
......@@ -1541,20 +1542,20 @@ function display_products(params) {
const supplier_id = id_split[3];
if (val == -1) {
let modal_remove_supplier_product_association = $('#templates #modal_remove_supplier_product_association');
let modal_end_supplier_product_association = $('#templates #modal_end_supplier_product_association');
const product = products.find(p => p.id == prod_id);
modal_remove_supplier_product_association.find(".product_name").text(product.name);
modal_end_supplier_product_association.find(".product_name").text(product.name);
const supplier = selected_suppliers.find(s => s.id == supplier_id);
modal_remove_supplier_product_association.find(".supplier_name").text(supplier.display_name);
modal_end_supplier_product_association.find(".supplier_name").text(supplier.display_name);
openModal(
modal_remove_supplier_product_association.html(),
modal_end_supplier_product_association.html(),
() => {
if (is_time_to('validate_remove_supplier_product_association')) {
remove_supplier_product_association(product, supplier);
if (is_time_to('validate_end_supplier_product_association')) {
end_supplier_product_association(product, supplier);
}
},
'Valider',
......
......@@ -13,7 +13,7 @@ urlpatterns = [
url(r'^get_suppliers$', views.get_suppliers),
url(r'^get_supplier_products$', views.get_supplier_products),
url(r'^associate_supplier_to_product$', views.associate_supplier_to_product),
url(r'^remove_supplier_product_association$', views.remove_supplier_product_association),
url(r'^end_supplier_product_association$', views.end_supplier_product_association),
url(r'^create_orders$', views.create_orders),
url(r'^get_orders_attachment$', views.get_orders_attachment),
]
......@@ -54,26 +54,28 @@ def get_supplier_products(request):
def associate_supplier_to_product(request):
""" This product is now supplied by this supplier """
res = {}
try:
data = json.loads(request.body.decode())
res = CagetteProduct.associate_supplier_to_product(data)
except Exception as e:
res["error"] = str(e)
data = json.loads(request.body.decode())
res = CagetteProduct.associate_supplier_to_product(data)
if 'error' in res:
return JsonResponse(res, status=500)
else:
return JsonResponse({'res': res})
return JsonResponse({'res': res})
def remove_supplier_product_association(request):
def end_supplier_product_association(request):
""" This product is now unavailable from this supplier """
res = {}
try:
data = json.loads(request.body.decode())
res = CagetteProduct.remove_supplier_product_association(data)
except Exception as e:
res["error"] = str(e)
return JsonResponse(res, status=500)
return JsonResponse({'res': res})
data = json.loads(request.body.decode())
res = CagetteProduct.end_supplier_product_association(data)
if 'error' in res:
return JsonResponse(res, status=500)
else:
return JsonResponse({'res': res})
def create_orders(request):
""" Create products orders """
......
......@@ -141,6 +141,8 @@ class CagetteProduct(models.Model):
c = [['product_tmpl_id', '=', product_tmpl_id]]
res_products = api.search_read('product.product', c, f)
product = res_products[0]
today = datetime.date.today().strftime("%Y-%m-%d")
f = {
'product_tmpl_id' : product_tmpl_id,
......@@ -150,29 +152,40 @@ class CagetteProduct(models.Model):
'price': price,
'base_price': price,
'package_qty': package_qty,
'date_start': today,
'sequence': 1000 # lowest priority for the new suppliers
}
try:
res = api.create('product.supplierinfo', f)
res['create'] = api.create('product.supplierinfo', f)
except Exception as e:
res['error'] = str(e)
return res
@staticmethod
def remove_supplier_product_association(data):
def end_supplier_product_association(data):
api = OdooAPI()
res = {}
product_tmpl_id = data["product_tmpl_id"]
partner_id = data["supplier_id"]
f = ["id"]
c = [['product_tmpl_id', '=', product_tmpl_id], ['name', '=', partner_id]]
c = [['product_tmpl_id', '=', product_tmpl_id], ['name', '=', partner_id], ['date_end', '=', False]]
res_supplierinfo = api.search_read('product.supplierinfo', c, f)
psi_id = res_supplierinfo[0]['id']
res = api.execute('product.supplierinfo', 'unlink', [psi_id])
today = datetime.date.today().strftime("%Y-%m-%d")
f = {
'date_end': today
}
try:
res["update"] = api.update('product.supplierinfo', psi_id, f)
except Exception as e:
res['error'] = str(e)
return res
......@@ -514,10 +527,12 @@ class CagetteProducts(models.Model):
# Filter valid data
ptids = []
valid_psi = []
for p in psi:
if (p["product_tmpl_id"] is not False
and (p["date_start"] is False or p["date_start"] is not False and p["date_start"] <= today)
and (p["date_end"] is False or p["date_end"] is not False and p["date_end"] >= today)):
and (p["date_end"] is False or p["date_end"] is not False and p["date_end"] > today)):
valid_psi.append(p)
ptids.append(p["product_tmpl_id"][0])
else:
ptids = [ int(x) for x in pids ]
......@@ -560,7 +575,7 @@ class CagetteProducts(models.Model):
if supplier_ids is not None and len(supplier_ids) > 0:
# Add all the product suppliersinfo (products from multiple suppliers into the suppliers list provided)
filtered_products_t[i]['suppliersinfo'] = []
for psi_item in psi:
for psi_item in valid_psi:
if psi_item["product_tmpl_id"] is not False and psi_item ["product_tmpl_id"][0] == fp["id"]:
filtered_products_t[i]['suppliersinfo'].append({
'supplier_id': int(psi_item["name"][0]),
......
......@@ -246,7 +246,7 @@
<hr/>
</div>
<div id="modal_remove_supplier_product_association">
<div id="modal_end_supplier_product_association">
<h3>Attention !</h3>
<p>
Vous vous apprêtez à rendre le produit <span class="product_name"></span>
......
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