Commit bc0bdfc8 by François C.

Merge branch 'log_reception_rapport_errors' into 'dev_cooperatic'

save reception errors report logs improvements

See merge request !48
parents 86219535 31e0ecb2
Pipeline #1197 passed with stage
in 1 minute 25 seconds
...@@ -212,7 +212,7 @@ function validatePrices() { ...@@ -212,7 +212,7 @@ function validatePrices() {
var updates = { var updates = {
'group_amount_total' : order['amount_total'], 'group_amount_total' : order['amount_total'],
'update_type' : 'br_vaid', 'update_type' : 'br_valid',
'updated_products' : [], 'updated_products' : [],
'user_comments': "", 'user_comments': "",
'orders' : [order] 'orders' : [order]
......
...@@ -259,54 +259,59 @@ def save_error_report(request): ...@@ -259,54 +259,59 @@ def save_error_report(request):
save it to an excel file, save it to an excel file,
attach file to order in odoo attach file to order in odoo
""" """
if request.is_ajax():
if request.method == 'POST': if request.method == 'POST':
data = None
try:
myJson = request.body myJson = request.body
data = json.loads(myJson.decode()) data = json.loads(myJson.decode())
except Exception as e1:
coop_logger.error("Save reception report : Unable to load data %s (%s)", str(e1), str(myJson))
temp_files_name = [] if data and ('orders' in data):
orders_name = "" orders_name_elts = []
orders_partner = "" orders_partner = ""
group_ids = [] group_ids = []
try:
for i, order in enumerate(data['orders']) : for i, order in enumerate(data['orders']) :
# list of temp files: 1 report per order & group # list of temp files: 1 report per order & group
data['orders'][i]['temp_file_name'] = "temp/" + order['name'] + "_rapport-reception_temp.xlsx" data['orders'][i]['temp_file_name'] = "temp/" + order['name'] + "_rapport-reception_temp.xlsx"
group_ids.append(order['id']) group_ids.append(order['id'])
# Concatenate orders name orders_name_elts.append(order['name'])
if orders_name != "" :
orders_name = orders_name + '-'
orders_name = orders_name + order['name']
# Concatenate orders partner # Concatenate orders partner
if order['partner'] != orders_partner : if order['partner'] != orders_partner:
if orders_partner != "" : if orders_partner != "":
orders_partner = orders_partner + ', ' orders_partner = orders_partner + ', '
orders_partner = orders_partner + order['partner'] + ' du ' + order['date_order'] orders_partner = orders_partner + order['partner'] + ' du ' + order['date_order']
# If group of orders # If group of orders
if len(data['orders']) > 1 : if len(data['orders']) > 1:
orders_name = '-'.join(orders_name_elts)
temp_group_file_name = "temp/" + orders_name + "_rapport-reception_temp.xlsx" temp_group_file_name = "temp/" + orders_name + "_rapport-reception_temp.xlsx"
# Add group in data['orders'] # Add group in data['orders']
group_order = { group_order = {
'name' : orders_name, 'name': orders_name,
'partner' : orders_partner, 'partner': orders_partner,
'date_order' : data['orders'][0]['date_order'], 'date_order': data['orders'][0]['date_order'],
'amount_total' : data['group_amount_total'], 'amount_total': data['group_amount_total'],
'updated_products' : data['updated_products'], 'updated_products': data['updated_products'],
'temp_file_name' : temp_group_file_name, 'temp_file_name': temp_group_file_name,
'group_ids' : group_ids 'group_ids': group_ids
} }
data['orders'].append(group_order) data['orders'].append(group_order)
except Exception as e2:
coop_logger.error("Save reception report : Error while create group_order %s", str(e2))
# Save qties & comments after step 1 # Save qties & comments after step 1
if data['update_type'] == 'qty_valid': if data['update_type'] == 'qty_valid':
for order in data['orders']: for order in data['orders']:
try:
wb = Workbook() wb = Workbook()
ws = wb.active ws = wb.active
ws.title = "Commande " + order['name'] ws.title = "Commande " + order['name']
...@@ -321,8 +326,8 @@ def save_error_report(request): ...@@ -321,8 +326,8 @@ def save_error_report(request):
'supplier_shortage']) 'supplier_shortage'])
# If in group add group name # If in group add group name
if len(data['orders']) > 1 : if len(data['orders']) > 1:
ws.append( ['group', orders_name] ) ws.append(['group', orders_name])
try: try:
if 'updated_products' in order: if 'updated_products' in order:
for product in order['updated_products']: for product in order['updated_products']:
...@@ -338,17 +343,17 @@ def save_error_report(request): ...@@ -338,17 +343,17 @@ def save_error_report(request):
else: else:
supplier_shortage = '' supplier_shortage = ''
ws.append( ['produit', ws.append(['produit',
product['product_id'][1], product['product_id'][1],
supplier_code, supplier_code,
str(product['barcode']), str(product['barcode']),
product['old_qty'], product['old_qty'],
product['product_qty'], product['product_qty'],
product['price_unit'], product['price_unit'],
supplier_shortage] ) supplier_shortage])
except Exception as exp: except Exception as exp:
print("Error while updating products") coop_logger.error("Error while updating products in report : %s", str(exp))
print(exp)
if ('user_comments' in data) and data['user_comments'] != "": if ('user_comments' in data) and data['user_comments'] != "":
ws.append( ['commentaire', data['user_comments']] ) ws.append( ['commentaire', data['user_comments']] )
else: else:
...@@ -356,7 +361,8 @@ def save_error_report(request): ...@@ -356,7 +361,8 @@ def save_error_report(request):
# Save file # Save file
wb.save(filename=order['temp_file_name']) wb.save(filename=order['temp_file_name'])
except Exception as e3:
coop_logger.error("Save reception report : Error while create order Workbook %s", str(e3))
# Create report with data from steps 1 & 2 # Create report with data from steps 1 & 2
...@@ -385,13 +391,14 @@ def save_error_report(request): ...@@ -385,13 +391,14 @@ def save_error_report(request):
'supplier_shortage' : row[7] 'supplier_shortage' : row[7]
} }
elif row[0] == 'group': elif row[0] == 'group':
group_name = row[1] # group's orders name group_name = row[1] # group's orders name : Unused
elif row[0] == 'commentaire': elif row[0] == 'commentaire':
data_comment_s1 = row[1] # user comments data_comment_s1 = row[1] # user comments
# Clear step 1 temp file # Clear step 1 temp file
os.remove(order['temp_file_name']) os.remove(order['temp_file_name'])
except: except Exception as e4:
coop_logger.error("Save reception report : reloading first step xlsx file %s", str(e4))
data_comment_s1 = "Données de la première étape absentes !" data_comment_s1 = "Données de la première étape absentes !"
# Add data from step 2 # Add data from step 2
...@@ -450,13 +457,13 @@ def save_error_report(request): ...@@ -450,13 +457,13 @@ def save_error_report(request):
error_total_abs += abs(item['error_line']) error_total_abs += abs(item['error_line'])
data_full.append(item) data_full.append(item)
except Exception as exp: except Exception as e5:
coop_logger.error("Save reception report : Error while updating products %s", str(e5))
# no updated products, do nothing # no updated products, do nothing
print("Error while updating products")
print(exp)
# Add remaining products, the ones edited only in step 1 # Add remaining products, the ones edited only in step 1
for product in data_qties.values(): for product in data_qties.values():
try:
item = { item = {
'product_id': product['nom_contenu'], 'product_id': product['nom_contenu'],
'product_supplier_code': product['supplier_code'], 'product_supplier_code': product['supplier_code'],
...@@ -474,7 +481,9 @@ def save_error_report(request): ...@@ -474,7 +481,9 @@ def save_error_report(request):
error_total_abs += abs(item['error_line']) error_total_abs += abs(item['error_line'])
data_full.append(item) data_full.append(item)
except Exception as e6:
coop_logger.error("Save reception report : Error while creating item from product %s (%s)", str(e6), str(e6))
try:
# Sort by error amount # Sort by error amount
def sortByError(e): def sortByError(e):
return abs(e['error_line']) return abs(e['error_line'])
...@@ -567,23 +576,28 @@ def save_error_report(request): ...@@ -567,23 +576,28 @@ def save_error_report(request):
length = 20 length = 20
ws.column_dimensions[column_cells[3].column_letter].width = length ws.column_dimensions[column_cells[3].column_letter].width = length
except Exception as e7:
coop_logger.error("PO save report : error while creating final Workbook %s", str(e7))
# Save file # Save file
fileName = "temp/" + order['name'] + "_rapport-reception.xlsx" fileName = "temp/" + order['name'] + "_rapport-reception.xlsx"
try: try:
wb.save(filename=fileName) wb.save(filename=fileName)
except Exception as exp:
print("Error while saving file %s"%fileName)
print(str(exp))
#Attach file to order #Attach file to order
if 'group_ids' in order : # group report if 'group_ids' in order: # group report
# Attach group report to each order # Attach group report to each order
for group_item_id in order['group_ids'] : for group_item_id in order['group_ids']:
m = CagetteReception(group_item_id) m = CagetteReception(group_item_id)
m.attach_file(fileName, False) m.attach_file(fileName, False)
os.remove(fileName) os.remove(fileName)
else: else:
m = CagetteReception(order['id']) m = CagetteReception(order['id'])
m.attach_file(fileName) m.attach_file(fileName)
except Exception as e8:
coop_logger.error("PO save report Error while saving file %s (%s)", fileName, str(e8))
else:
coop_logger.error("Cant find 'orders' in data (%s)", str(data))
else:
coop_logger.info("Was waiting for a POST method (%s)", str(request.method))
return JsonResponse("ok", safe=False) return JsonResponse("ok", safe=False)
def reception_FAQ(request): def reception_FAQ(request):
......
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