@@ -41,22 +41,44 @@ def generate_vat_declaration(company, year, month, declared, report_data):
41
41
42
42
# 4.1 - Verificamos que no existan un registro duplicado
43
43
if not frappe .db .exists ('VAT Declaration' , {'name' : f"VAT Declaration { date .today ()} " }):
44
- vat_dec = frappe .get_doc ({
45
- "doctype" : "VAT Declaration" ,
46
- "title" : f"VAT Declaration { date .today ()} " ,
47
- "company" : company ,
48
- "posting_date" : date .today (),
49
- "declaration_year" : year ,
50
- "declaration_month" : MONTHS_MAP .get (str (month )),
51
- "declaration_items" : declaration_invoices ,
52
- "docstatus" : 1
53
- })
54
-
55
- # for validated documents: status_journal = vat_dec.insert(ignore_permissions=True)
56
- # status_declaration = vat_dec.save(ignore_permissions=True)
57
- status_declaration = vat_dec .insert (ignore_permissions = True )
58
-
59
- # 5 - Actualizamos las facturas con su nueva Referencia en campo de tipo data,
44
+ # CREAMOS EL REGISTRO COMO VALIDADO
45
+ try :
46
+ vat_dec = frappe .get_doc ({
47
+ "doctype" : "VAT Declaration" ,
48
+ "title" : f"VAT Declaration { date .today ()} " ,
49
+ "company" : company ,
50
+ "posting_date" : date .today (),
51
+ "declaration_year" : year ,
52
+ "declaration_month" : MONTHS_MAP .get (str (month )),
53
+ "declaration_items" : declaration_invoices ,
54
+ "docstatus" : 1
55
+ })
56
+
57
+ # for validated documents: status_journal = vat_dec.insert(ignore_permissions=True)
58
+ # status_declaration = vat_dec.save(ignore_permissions=True)
59
+ status_declaration = vat_dec .insert (ignore_permissions = True )
60
+
61
+ # SI OCURRE ALGUN ERROR
62
+ except :
63
+ frappe .msgprint (msg = _ (f'More details in the following log \n { frappe .get_traceback ()} ' ),
64
+ title = _ ('Sorry, a problem occurred while trying to generate the VAT declaration' ), indicator = 'red' )
65
+ return
66
+
67
+ # SI LA CREACION ES EXITOSA, ACTUALIZAMOS LAS FACTURAS CON LA REFERENCIA
68
+ else :
69
+ # Por cada factura venta, compra
70
+ for record in records :
71
+ if record .get ('compras_ventas' ) == 'V' : # si es venta
72
+ frappe .db .sql ('''UPDATE `tabSales Invoice`
73
+ SET facelec_s_vat_declaration=%(declaration)s
74
+ WHERE name=%(name_inv)s
75
+ ''' , {'declaration' : status_declaration .name , 'name_inv' : str (record .get ('invoice_name' ))})
76
+
77
+ else : # si es compra
78
+ frappe .db .sql ('''UPDATE `tabPurchase Invoice`
79
+ SET facelec_p_vat_declaration=%(declaration)s
80
+ WHERE name=%(name_inv)s
81
+ ''' , {'declaration' : status_declaration .name , 'name_inv' : str (record .get ('invoice_name' ))})
60
82
61
83
else :
62
84
nme_reg = f'VAT Declaration { date .today ()} '
0 commit comments