from typing import Any, Dict from uuid6 import uuid7 def none_to_empty(value: Any) -> str: return "" if value is None else value def create_invoice_header( cf, hif, payment_method_id, payment_method_description ) -> Dict[str, Any]: return { "id": str(uuid7()), "factuges_id": str(hif.get("factuges_id")), "company_id": hif.get("company_id"), "is_proforma": hif.get("is_proforma"), "status": hif.get("status"), "series": hif.get("series"), "reference": hif.get("reference"), "description": hif.get("description"), "invoice_date": hif.get("invoice_date"), "operation_date": hif.get("operation_date"), "notes": none_to_empty(hif.get("notes")), "language_code": cf.get("language_code"), "subtotal_amount_value": str(none_to_empty(hif.get("subtotal_amount_value"))), "global_discount_percentage_value": str(none_to_empty(hif.get("discount_percentage_val"))), "discount_amount_value": str(none_to_empty(hif.get("discount_amount_value"))), "taxable_amount_value": str(hif.get("taxable_amount_value")), "taxes_amount_value": str(hif.get("taxes_amount_value")), "total_amount_value": str(hif.get("total_amount_value")), "payment_method_id": str(payment_method_id), "payment_method_description": payment_method_description, "customer": { "is_company": cf["is_company"], "name": cf["name"], "tin": cf["tin"], "street": cf["street"], "city": cf["city"], "province": cf["province"], "postal_code": cf["postal_code"], "country": cf["country"], "language_code": cf["language_code"], "phone_primary": none_to_empty(cf["phone_primary"]), "phone_secondary": none_to_empty(cf["phone_secondary"]), "mobile_primary": none_to_empty(cf["mobile_primary"]), "mobile_secondary": none_to_empty(cf["mobile_secondary"]), "email_primary": none_to_empty(cf["email_primary"]), "email_secondary": none_to_empty(cf["email_secondary"]), "website": str(none_to_empty(cf["website"])), }, "items": [], } def insert_item_and_taxes(fields) -> Dict[str, Any]: # logger.info(fields) return { "item_id": str(uuid7()), "position": str(fields.get("position")), "description": str(none_to_empty(fields.get("description"))), "quantity_value": str(none_to_empty(fields.get("quantity_value"))), "unit_amount_value": str(none_to_empty(fields.get("unit_amount_value"))), "subtotal_amount_value": str(fields.get("subtotal_amount_value")), "item_discount_percentage_value": str(none_to_empty(fields.get("item_discount_percentage_value"))), "item_discount_amount_value": str(none_to_empty(fields.get("item_discount_amount_value"))), "global_discount_percentage_value": str(none_to_empty(fields.get("global_discount_percentage_value"))), "global_discount_amount_value": str(none_to_empty(fields.get("global_discount_amount_value"))), "total_discount_amount_value": str(fields.get("total_discount_amount_value")), "taxable_amount_value": str(fields.get("taxable_amount_value")), "total_amount_value": str(fields.get("total_amount_value")), "iva_code": str(fields.get("iva_code")), "iva_percentage_value": str(str(fields.get("iva_percentage_value"))), "iva_amount_value": str(fields.get("iva_amount_value")), "rec_code": str(none_to_empty(fields.get("rec_code"))), "rec_percentage_value": str(none_to_empty(fields.get("rec_percentage_value"))), "rec_amount_value": str(fields.get("rec_amount_value")), "taxes_amount_value": str(fields.get("taxes_amount_value")), } # logger.info("Inserting item tax %s code=%s base=%s tax=%s", # item_id, fields.get('tax_code'), fields.get('total_amount_value'), fields.get('tax_amount')) # cur.execute( # SQL.INSERT_INVOICE_ITEM_TAX, (str(uuid7()), item_id, fields.get('tax_code'), # fields.get('total_amount_value'), fields.get('tax_amount')) # )