Commit 31e0ecb2 by François C.

Better save reception errors report logs

parent 86219535
Pipeline #1195 passed with stage
in 1 minute 21 seconds
......@@ -212,7 +212,7 @@ function validatePrices() {
var updates = {
'group_amount_total' : order['amount_total'],
'update_type' : 'br_vaid',
'update_type' : 'br_valid',
'updated_products' : [],
'user_comments': "",
'orders' : [order]
......
......@@ -259,54 +259,59 @@ def save_error_report(request):
save it to an excel file,
attach file to order in odoo
"""
if request.is_ajax():
if request.method == 'POST':
data = None
try:
myJson = request.body
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 = []
orders_name = ""
if data and ('orders' in data):
orders_name_elts = []
orders_partner = ""
group_ids = []
try:
for i, order in enumerate(data['orders']) :
# list of temp files: 1 report per order & group
data['orders'][i]['temp_file_name'] = "temp/" + order['name'] + "_rapport-reception_temp.xlsx"
group_ids.append(order['id'])
# Concatenate orders name
if orders_name != "" :
orders_name = orders_name + '-'
orders_name = orders_name + order['name']
orders_name_elts.append(order['name'])
# Concatenate orders partner
if order['partner'] != orders_partner :
if orders_partner != "" :
if order['partner'] != orders_partner:
if orders_partner != "":
orders_partner = orders_partner + ', '
orders_partner = orders_partner + order['partner'] + ' du ' + order['date_order']
# 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"
# Add group in data['orders']
group_order = {
'name' : orders_name,
'partner' : orders_partner,
'date_order' : data['orders'][0]['date_order'],
'amount_total' : data['group_amount_total'],
'updated_products' : data['updated_products'],
'temp_file_name' : temp_group_file_name,
'group_ids' : group_ids
'name': orders_name,
'partner': orders_partner,
'date_order': data['orders'][0]['date_order'],
'amount_total': data['group_amount_total'],
'updated_products': data['updated_products'],
'temp_file_name': temp_group_file_name,
'group_ids': group_ids
}
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
if data['update_type'] == 'qty_valid':
for order in data['orders']:
try:
wb = Workbook()
ws = wb.active
ws.title = "Commande " + order['name']
......@@ -321,8 +326,8 @@ def save_error_report(request):
'supplier_shortage'])
# If in group add group name
if len(data['orders']) > 1 :
ws.append( ['group', orders_name] )
if len(data['orders']) > 1:
ws.append(['group', orders_name])
try:
if 'updated_products' in order:
for product in order['updated_products']:
......@@ -338,17 +343,17 @@ def save_error_report(request):
else:
supplier_shortage = ''
ws.append( ['produit',
ws.append(['produit',
product['product_id'][1],
supplier_code,
str(product['barcode']),
product['old_qty'],
product['product_qty'],
product['price_unit'],
supplier_shortage] )
supplier_shortage])
except Exception as exp:
print("Error while updating products")
print(exp)
coop_logger.error("Error while updating products in report : %s", str(exp))
if ('user_comments' in data) and data['user_comments'] != "":
ws.append( ['commentaire', data['user_comments']] )
else:
......@@ -356,7 +361,8 @@ def save_error_report(request):
# Save file
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
......@@ -385,13 +391,14 @@ def save_error_report(request):
'supplier_shortage' : row[7]
}
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':
data_comment_s1 = row[1] # user comments
# Clear step 1 temp file
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 !"
# Add data from step 2
......@@ -450,13 +457,13 @@ def save_error_report(request):
error_total_abs += abs(item['error_line'])
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
print("Error while updating products")
print(exp)
# Add remaining products, the ones edited only in step 1
for product in data_qties.values():
try:
item = {
'product_id': product['nom_contenu'],
'product_supplier_code': product['supplier_code'],
......@@ -474,7 +481,9 @@ def save_error_report(request):
error_total_abs += abs(item['error_line'])
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
def sortByError(e):
return abs(e['error_line'])
......@@ -567,23 +576,28 @@ def save_error_report(request):
length = 20
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
fileName = "temp/" + order['name'] + "_rapport-reception.xlsx"
try:
wb.save(filename=fileName)
except Exception as exp:
print("Error while saving file %s"%fileName)
print(str(exp))
#Attach file to order
if 'group_ids' in order : # group report
if 'group_ids' in order: # group report
# 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.attach_file(fileName, False)
os.remove(fileName)
else:
m = CagetteReception(order['id'])
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)
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