Gaurav | 2670ad7 | 2019-02-19 10:17:17 +0530 | [diff] [blame] | 1 | from __future__ import unicode_literals |
| 2 | |
Sagar Vora | ba76f87 | 2021-03-29 20:18:45 +0530 | [diff] [blame] | 3 | import io |
| 4 | import json |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 5 | |
Sagar Vora | ba76f87 | 2021-03-29 20:18:45 +0530 | [diff] [blame] | 6 | import frappe |
Gaurav | f1e28e0 | 2019-02-13 16:46:24 +0530 | [diff] [blame] | 7 | from frappe import _ |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 8 | from frappe.utils import cstr, flt |
Saqib | 6cf9254 | 2021-08-24 15:26:44 +0530 | [diff] [blame] | 9 | from frappe.utils.file_manager import remove_file |
Rohit Waghchaure | 58f489f | 2019-04-01 17:50:31 +0530 | [diff] [blame] | 10 | from six import string_types |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 11 | |
| 12 | from erpnext.controllers.taxes_and_totals import get_itemised_tax |
Gaurav | 3f04613 | 2019-02-19 16:28:22 +0530 | [diff] [blame] | 13 | from erpnext.regional.italy import state_codes |
Gaurav | f1e28e0 | 2019-02-13 16:46:24 +0530 | [diff] [blame] | 14 | |
| 15 | |
| 16 | def update_itemised_tax_data(doc): |
| 17 | if not doc.taxes: return |
| 18 | |
hello@openetech.com | 7021402 | 2019-10-16 23:38:51 +0530 | [diff] [blame] | 19 | if doc.doctype == "Purchase Invoice": return |
| 20 | |
Gaurav | f1e28e0 | 2019-02-13 16:46:24 +0530 | [diff] [blame] | 21 | itemised_tax = get_itemised_tax(doc.taxes) |
| 22 | |
| 23 | for row in doc.items: |
| 24 | tax_rate = 0.0 |
| 25 | if itemised_tax.get(row.item_code): |
| 26 | tax_rate = sum([tax.get('tax_rate', 0) for d, tax in itemised_tax.get(row.item_code).items()]) |
| 27 | |
| 28 | row.tax_rate = flt(tax_rate, row.precision("tax_rate")) |
| 29 | row.tax_amount = flt((row.net_amount * tax_rate) / 100, row.precision("net_amount")) |
| 30 | row.total_amount = flt((row.net_amount + row.tax_amount), row.precision("total_amount")) |
| 31 | |
| 32 | @frappe.whitelist() |
| 33 | def export_invoices(filters=None): |
Sagar Vora | ba76f87 | 2021-03-29 20:18:45 +0530 | [diff] [blame] | 34 | frappe.has_permission('Sales Invoice', throw=True) |
Gaurav | f1e28e0 | 2019-02-13 16:46:24 +0530 | [diff] [blame] | 35 | |
Sagar Vora | ba76f87 | 2021-03-29 20:18:45 +0530 | [diff] [blame] | 36 | invoices = frappe.get_all( |
| 37 | "Sales Invoice", |
| 38 | filters=get_conditions(filters), |
| 39 | fields=["name", "company_tax_id"] |
| 40 | ) |
Gaurav | f1e28e0 | 2019-02-13 16:46:24 +0530 | [diff] [blame] | 41 | |
Sagar Vora | ba76f87 | 2021-03-29 20:18:45 +0530 | [diff] [blame] | 42 | attachments = get_e_invoice_attachments(invoices) |
Gaurav | f1e28e0 | 2019-02-13 16:46:24 +0530 | [diff] [blame] | 43 | |
Sagar Vora | ba76f87 | 2021-03-29 20:18:45 +0530 | [diff] [blame] | 44 | zip_filename = "{0}-einvoices.zip".format( |
| 45 | frappe.utils.get_datetime().strftime("%Y%m%d_%H%M%S")) |
Gaurav | f1e28e0 | 2019-02-13 16:46:24 +0530 | [diff] [blame] | 46 | |
Sagar Vora | ba76f87 | 2021-03-29 20:18:45 +0530 | [diff] [blame] | 47 | download_zip(attachments, zip_filename) |
Gaurav | f1e28e0 | 2019-02-13 16:46:24 +0530 | [diff] [blame] | 48 | |
| 49 | |
Gaurav | f1e28e0 | 2019-02-13 16:46:24 +0530 | [diff] [blame] | 50 | def prepare_invoice(invoice, progressive_number): |
| 51 | #set company information |
| 52 | company = frappe.get_doc("Company", invoice.company) |
| 53 | |
| 54 | invoice.progressive_number = progressive_number |
| 55 | invoice.unamended_name = get_unamended_name(invoice) |
| 56 | invoice.company_data = company |
| 57 | company_address = frappe.get_doc("Address", invoice.company_address) |
| 58 | invoice.company_address_data = company_address |
| 59 | |
| 60 | #Set invoice type |
Saqib Ansari | 032246d | 2021-04-09 12:08:24 +0530 | [diff] [blame] | 61 | if not invoice.type_of_document: |
| 62 | if invoice.is_return and invoice.return_against: |
| 63 | invoice.type_of_document = "TD04" #Credit Note (Nota di Credito) |
| 64 | invoice.return_against_unamended = get_unamended_name(frappe.get_doc("Sales Invoice", invoice.return_against)) |
| 65 | else: |
| 66 | invoice.type_of_document = "TD01" #Sales Invoice (Fattura) |
Gaurav | f1e28e0 | 2019-02-13 16:46:24 +0530 | [diff] [blame] | 67 | |
| 68 | #set customer information |
| 69 | invoice.customer_data = frappe.get_doc("Customer", invoice.customer) |
| 70 | customer_address = frappe.get_doc("Address", invoice.customer_address) |
| 71 | invoice.customer_address_data = customer_address |
| 72 | |
| 73 | if invoice.shipping_address_name: |
| 74 | invoice.shipping_address_data = frappe.get_doc("Address", invoice.shipping_address_name) |
| 75 | |
| 76 | if invoice.customer_data.is_public_administration: |
| 77 | invoice.transmission_format_code = "FPA12" |
| 78 | else: |
| 79 | invoice.transmission_format_code = "FPR12" |
| 80 | |
Gaurav | 2670ad7 | 2019-02-19 10:17:17 +0530 | [diff] [blame] | 81 | invoice.e_invoice_items = [item for item in invoice.items] |
| 82 | tax_data = get_invoice_summary(invoice.e_invoice_items, invoice.taxes) |
Gaurav | f1e28e0 | 2019-02-13 16:46:24 +0530 | [diff] [blame] | 83 | invoice.tax_data = tax_data |
| 84 | |
| 85 | #Check if stamp duty (Bollo) of 2 EUR exists. |
Rohit Waghchaure | 68a1456 | 2019-05-22 13:17:46 +0530 | [diff] [blame] | 86 | stamp_duty_charge_row = next((tax for tax in invoice.taxes if tax.charge_type == "Actual" and tax.tax_amount == 2.0 ), None) |
Gaurav | f1e28e0 | 2019-02-13 16:46:24 +0530 | [diff] [blame] | 87 | if stamp_duty_charge_row: |
| 88 | invoice.stamp_duty = stamp_duty_charge_row.tax_amount |
| 89 | |
Gaurav | 2670ad7 | 2019-02-19 10:17:17 +0530 | [diff] [blame] | 90 | for item in invoice.e_invoice_items: |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 91 | if item.tax_rate == 0.0 and item.tax_amount == 0.0 and tax_data.get("0.0"): |
Gaurav | f1e28e0 | 2019-02-13 16:46:24 +0530 | [diff] [blame] | 92 | item.tax_exemption_reason = tax_data["0.0"]["tax_exemption_reason"] |
| 93 | |
Rohit Waghchaure | 1b7059b | 2019-03-12 17:44:29 +0530 | [diff] [blame] | 94 | customer_po_data = {} |
| 95 | for d in invoice.e_invoice_items: |
| 96 | if (d.customer_po_no and d.customer_po_date |
| 97 | and d.customer_po_no not in customer_po_data): |
| 98 | customer_po_data[d.customer_po_no] = d.customer_po_date |
| 99 | |
| 100 | invoice.customer_po_data = customer_po_data |
| 101 | |
Gaurav | f1e28e0 | 2019-02-13 16:46:24 +0530 | [diff] [blame] | 102 | return invoice |
| 103 | |
| 104 | def get_conditions(filters): |
| 105 | filters = json.loads(filters) |
| 106 | |
Sagar Vora | ba76f87 | 2021-03-29 20:18:45 +0530 | [diff] [blame] | 107 | conditions = {"docstatus": 1, "company_tax_id": ("!=", "")} |
Gaurav | f1e28e0 | 2019-02-13 16:46:24 +0530 | [diff] [blame] | 108 | |
| 109 | if filters.get("company"): conditions["company"] = filters["company"] |
| 110 | if filters.get("customer"): conditions["customer"] = filters["customer"] |
| 111 | |
| 112 | if filters.get("from_date"): conditions["posting_date"] = (">=", filters["from_date"]) |
| 113 | if filters.get("to_date"): conditions["posting_date"] = ("<=", filters["to_date"]) |
| 114 | |
| 115 | if filters.get("from_date") and filters.get("to_date"): |
| 116 | conditions["posting_date"] = ("between", [filters.get("from_date"), filters.get("to_date")]) |
| 117 | |
| 118 | return conditions |
| 119 | |
Sagar Vora | ba76f87 | 2021-03-29 20:18:45 +0530 | [diff] [blame] | 120 | |
Gaurav | f1e28e0 | 2019-02-13 16:46:24 +0530 | [diff] [blame] | 121 | def download_zip(files, output_filename): |
Sagar Vora | ba76f87 | 2021-03-29 20:18:45 +0530 | [diff] [blame] | 122 | import zipfile |
Gaurav | f1e28e0 | 2019-02-13 16:46:24 +0530 | [diff] [blame] | 123 | |
Sagar Vora | ba76f87 | 2021-03-29 20:18:45 +0530 | [diff] [blame] | 124 | zip_stream = io.BytesIO() |
| 125 | with zipfile.ZipFile(zip_stream, 'w', zipfile.ZIP_DEFLATED) as zip_file: |
| 126 | for file in files: |
| 127 | file_path = frappe.utils.get_files_path( |
| 128 | file.file_name, is_private=file.is_private) |
Gaurav | f1e28e0 | 2019-02-13 16:46:24 +0530 | [diff] [blame] | 129 | |
Sagar Vora | ba76f87 | 2021-03-29 20:18:45 +0530 | [diff] [blame] | 130 | zip_file.write(file_path, arcname=file.file_name) |
Gaurav | f1e28e0 | 2019-02-13 16:46:24 +0530 | [diff] [blame] | 131 | |
| 132 | frappe.local.response.filename = output_filename |
Sagar Vora | ba76f87 | 2021-03-29 20:18:45 +0530 | [diff] [blame] | 133 | frappe.local.response.filecontent = zip_stream.getvalue() |
Gaurav | f1e28e0 | 2019-02-13 16:46:24 +0530 | [diff] [blame] | 134 | frappe.local.response.type = "download" |
Sagar Vora | ba76f87 | 2021-03-29 20:18:45 +0530 | [diff] [blame] | 135 | zip_stream.close() |
Gaurav | f1e28e0 | 2019-02-13 16:46:24 +0530 | [diff] [blame] | 136 | |
| 137 | def get_invoice_summary(items, taxes): |
| 138 | summary_data = frappe._dict() |
| 139 | for tax in taxes: |
| 140 | #Include only VAT charges. |
| 141 | if tax.charge_type == "Actual": |
| 142 | continue |
| 143 | |
Gaurav | 2670ad7 | 2019-02-19 10:17:17 +0530 | [diff] [blame] | 144 | #Charges to appear as items in the e-invoice. |
| 145 | if tax.charge_type in ["On Previous Row Total", "On Previous Row Amount"]: |
| 146 | reference_row = next((row for row in taxes if row.idx == int(tax.row_id or 0)), None) |
| 147 | if reference_row: |
| 148 | items.append( |
| 149 | frappe._dict( |
| 150 | idx=len(items)+1, |
| 151 | item_code=reference_row.description, |
| 152 | item_name=reference_row.description, |
rohitwaghchaure | 9673d0d | 2019-03-24 12:19:58 +0530 | [diff] [blame] | 153 | description=reference_row.description, |
Gaurav | 2670ad7 | 2019-02-19 10:17:17 +0530 | [diff] [blame] | 154 | rate=reference_row.tax_amount, |
| 155 | qty=1.0, |
| 156 | amount=reference_row.tax_amount, |
| 157 | stock_uom=frappe.db.get_single_value("Stock Settings", "stock_uom") or _("Nos"), |
| 158 | tax_rate=tax.rate, |
| 159 | tax_amount=(reference_row.tax_amount * tax.rate) / 100, |
| 160 | net_amount=reference_row.tax_amount, |
Rohit Waghchaure | c10064a | 2019-09-23 17:39:55 +0530 | [diff] [blame] | 161 | taxable_amount=reference_row.tax_amount, |
Rohit Waghchaure | 58f489f | 2019-04-01 17:50:31 +0530 | [diff] [blame] | 162 | item_tax_rate={tax.account_head: tax.rate}, |
Gaurav | 2670ad7 | 2019-02-19 10:17:17 +0530 | [diff] [blame] | 163 | charges=True |
| 164 | ) |
| 165 | ) |
| 166 | |
Gaurav | f1e28e0 | 2019-02-13 16:46:24 +0530 | [diff] [blame] | 167 | #Check item tax rates if tax rate is zero. |
| 168 | if tax.rate == 0: |
| 169 | for item in items: |
Rohit Waghchaure | 58f489f | 2019-04-01 17:50:31 +0530 | [diff] [blame] | 170 | item_tax_rate = item.item_tax_rate |
| 171 | if isinstance(item.item_tax_rate, string_types): |
| 172 | item_tax_rate = json.loads(item.item_tax_rate) |
| 173 | |
| 174 | if item_tax_rate and tax.account_head in item_tax_rate: |
Gaurav | 2670ad7 | 2019-02-19 10:17:17 +0530 | [diff] [blame] | 175 | key = cstr(item_tax_rate[tax.account_head]) |
Rohit Waghchaure | 58f489f | 2019-04-01 17:50:31 +0530 | [diff] [blame] | 176 | if key not in summary_data: |
| 177 | summary_data.setdefault(key, {"tax_amount": 0.0, "taxable_amount": 0.0, |
| 178 | "tax_exemption_reason": "", "tax_exemption_law": ""}) |
| 179 | |
Gaurav | f1e28e0 | 2019-02-13 16:46:24 +0530 | [diff] [blame] | 180 | summary_data[key]["tax_amount"] += item.tax_amount |
| 181 | summary_data[key]["taxable_amount"] += item.net_amount |
| 182 | if key == "0.0": |
| 183 | summary_data[key]["tax_exemption_reason"] = tax.tax_exemption_reason |
| 184 | summary_data[key]["tax_exemption_law"] = tax.tax_exemption_law |
| 185 | |
Rohit Waghchaure | c10064a | 2019-09-23 17:39:55 +0530 | [diff] [blame] | 186 | if summary_data.get("0.0") and tax.charge_type in ["On Previous Row Total", |
| 187 | "On Previous Row Amount"]: |
| 188 | summary_data[key]["taxable_amount"] = tax.total |
| 189 | |
Gaurav | f1e28e0 | 2019-02-13 16:46:24 +0530 | [diff] [blame] | 190 | if summary_data == {}: #Implies that Zero VAT has not been set on any item. |
| 191 | summary_data.setdefault("0.0", {"tax_amount": 0.0, "taxable_amount": tax.total, |
| 192 | "tax_exemption_reason": tax.tax_exemption_reason, "tax_exemption_law": tax.tax_exemption_law}) |
| 193 | |
| 194 | else: |
| 195 | item_wise_tax_detail = json.loads(tax.item_wise_tax_detail) |
| 196 | for rate_item in [tax_item for tax_item in item_wise_tax_detail.items() if tax_item[1][0] == tax.rate]: |
Gaurav | 2670ad7 | 2019-02-19 10:17:17 +0530 | [diff] [blame] | 197 | key = cstr(tax.rate) |
Gaurav | f1e28e0 | 2019-02-13 16:46:24 +0530 | [diff] [blame] | 198 | if not summary_data.get(key): summary_data.setdefault(key, {"tax_amount": 0.0, "taxable_amount": 0.0}) |
| 199 | summary_data[key]["tax_amount"] += rate_item[1][1] |
| 200 | summary_data[key]["taxable_amount"] += sum([item.net_amount for item in items if item.item_code == rate_item[0]]) |
| 201 | |
Gaurav | 2670ad7 | 2019-02-19 10:17:17 +0530 | [diff] [blame] | 202 | for item in items: |
| 203 | key = cstr(tax.rate) |
| 204 | if item.get("charges"): |
| 205 | if not summary_data.get(key): summary_data.setdefault(key, {"taxable_amount": 0.0}) |
| 206 | summary_data[key]["taxable_amount"] += item.taxable_amount |
| 207 | |
Gaurav | f1e28e0 | 2019-02-13 16:46:24 +0530 | [diff] [blame] | 208 | return summary_data |
| 209 | |
| 210 | #Preflight for successful e-invoice export. |
| 211 | def sales_invoice_validate(doc): |
| 212 | #Validate company |
rohitwaghchaure | ef3f864 | 2019-02-20 15:47:06 +0530 | [diff] [blame] | 213 | if doc.doctype != 'Sales Invoice': |
| 214 | return |
| 215 | |
Gaurav | f1e28e0 | 2019-02-13 16:46:24 +0530 | [diff] [blame] | 216 | if not doc.company_address: |
| 217 | frappe.throw(_("Please set an Address on the Company '%s'" % doc.company), title=_("E-Invoicing Information Missing")) |
| 218 | else: |
Rohit Waghchaure | 74cfe57 | 2019-02-26 20:08:26 +0530 | [diff] [blame] | 219 | validate_address(doc.company_address) |
Gaurav | f1e28e0 | 2019-02-13 16:46:24 +0530 | [diff] [blame] | 220 | |
Rohit Waghchaure | 0f98cb8 | 2019-02-26 15:01:30 +0530 | [diff] [blame] | 221 | company_fiscal_regime = frappe.get_cached_value("Company", doc.company, 'fiscal_regime') |
| 222 | if not company_fiscal_regime: |
| 223 | frappe.throw(_("Fiscal Regime is mandatory, kindly set the fiscal regime in the company {0}") |
| 224 | .format(doc.company)) |
| 225 | else: |
| 226 | doc.company_fiscal_regime = company_fiscal_regime |
| 227 | |
Gaurav | bd80fd1 | 2019-03-28 12:18:03 +0530 | [diff] [blame] | 228 | doc.company_tax_id = frappe.get_cached_value("Company", doc.company, 'tax_id') |
| 229 | doc.company_fiscal_code = frappe.get_cached_value("Company", doc.company, 'fiscal_code') |
Gaurav | f1e28e0 | 2019-02-13 16:46:24 +0530 | [diff] [blame] | 230 | if not doc.company_tax_id and not doc.company_fiscal_code: |
| 231 | frappe.throw(_("Please set either the Tax ID or Fiscal Code on Company '%s'" % doc.company), title=_("E-Invoicing Information Missing")) |
| 232 | |
| 233 | #Validate customer details |
Gaurav | 010acf7 | 2019-03-28 10:42:23 +0530 | [diff] [blame] | 234 | customer = frappe.get_doc("Customer", doc.customer) |
| 235 | |
Rohit Waghchaure | 68a1456 | 2019-05-22 13:17:46 +0530 | [diff] [blame] | 236 | if customer.customer_type == "Individual": |
Gaurav | 010acf7 | 2019-03-28 10:42:23 +0530 | [diff] [blame] | 237 | doc.customer_fiscal_code = customer.fiscal_code |
Gaurav | f1e28e0 | 2019-02-13 16:46:24 +0530 | [diff] [blame] | 238 | if not doc.customer_fiscal_code: |
| 239 | frappe.throw(_("Please set Fiscal Code for the customer '%s'" % doc.customer), title=_("E-Invoicing Information Missing")) |
| 240 | else: |
Gaurav | 010acf7 | 2019-03-28 10:42:23 +0530 | [diff] [blame] | 241 | if customer.is_public_administration: |
| 242 | doc.customer_fiscal_code = customer.fiscal_code |
Gaurav | f1e28e0 | 2019-02-13 16:46:24 +0530 | [diff] [blame] | 243 | if not doc.customer_fiscal_code: |
| 244 | frappe.throw(_("Please set Fiscal Code for the public administration '%s'" % doc.customer), title=_("E-Invoicing Information Missing")) |
| 245 | else: |
Gaurav | 010acf7 | 2019-03-28 10:42:23 +0530 | [diff] [blame] | 246 | doc.tax_id = customer.tax_id |
Gaurav | f1e28e0 | 2019-02-13 16:46:24 +0530 | [diff] [blame] | 247 | if not doc.tax_id: |
| 248 | frappe.throw(_("Please set Tax ID for the customer '%s'" % doc.customer), title=_("E-Invoicing Information Missing")) |
| 249 | |
| 250 | if not doc.customer_address: |
| 251 | frappe.throw(_("Please set the Customer Address"), title=_("E-Invoicing Information Missing")) |
| 252 | else: |
Rohit Waghchaure | 74cfe57 | 2019-02-26 20:08:26 +0530 | [diff] [blame] | 253 | validate_address(doc.customer_address) |
Gaurav | f1e28e0 | 2019-02-13 16:46:24 +0530 | [diff] [blame] | 254 | |
| 255 | if not len(doc.taxes): |
| 256 | frappe.throw(_("Please set at least one row in the Taxes and Charges Table"), title=_("E-Invoicing Information Missing")) |
| 257 | else: |
| 258 | for row in doc.taxes: |
| 259 | if row.rate == 0 and row.tax_amount == 0 and not row.tax_exemption_reason: |
Suraj Shetty | 48e9bc3 | 2020-01-29 15:06:18 +0530 | [diff] [blame] | 260 | frappe.throw(_("Row {0}: Please set at Tax Exemption Reason in Sales Taxes and Charges").format(row.idx), |
Gaurav | f1e28e0 | 2019-02-13 16:46:24 +0530 | [diff] [blame] | 261 | title=_("E-Invoicing Information Missing")) |
| 262 | |
Rohit Waghchaure | 0f98cb8 | 2019-02-26 15:01:30 +0530 | [diff] [blame] | 263 | for schedule in doc.payment_schedule: |
| 264 | if schedule.mode_of_payment and not schedule.mode_of_payment_code: |
| 265 | schedule.mode_of_payment_code = frappe.get_cached_value('Mode of Payment', |
| 266 | schedule.mode_of_payment, 'mode_of_payment_code') |
Gaurav | f1e28e0 | 2019-02-13 16:46:24 +0530 | [diff] [blame] | 267 | |
| 268 | #Ensure payment details are valid for e-invoice. |
rohitwaghchaure | c18e925 | 2019-02-20 17:13:15 +0530 | [diff] [blame] | 269 | def sales_invoice_on_submit(doc, method): |
Gaurav | f1e28e0 | 2019-02-13 16:46:24 +0530 | [diff] [blame] | 270 | #Validate payment details |
rohitwaghchaure | c18e925 | 2019-02-20 17:13:15 +0530 | [diff] [blame] | 271 | if get_company_country(doc.company) not in ['Italy', |
| 272 | 'Italia', 'Italian Republic', 'Repubblica Italiana']: |
| 273 | return |
| 274 | |
Gaurav | f1e28e0 | 2019-02-13 16:46:24 +0530 | [diff] [blame] | 275 | if not len(doc.payment_schedule): |
| 276 | frappe.throw(_("Please set the Payment Schedule"), title=_("E-Invoicing Information Missing")) |
| 277 | else: |
| 278 | for schedule in doc.payment_schedule: |
| 279 | if not schedule.mode_of_payment: |
Suraj Shetty | 48e9bc3 | 2020-01-29 15:06:18 +0530 | [diff] [blame] | 280 | frappe.throw(_("Row {0}: Please set the Mode of Payment in Payment Schedule").format(schedule.idx), |
Gaurav | f1e28e0 | 2019-02-13 16:46:24 +0530 | [diff] [blame] | 281 | title=_("E-Invoicing Information Missing")) |
Gaurav | 2670ad7 | 2019-02-19 10:17:17 +0530 | [diff] [blame] | 282 | elif not frappe.db.get_value("Mode of Payment", schedule.mode_of_payment, "mode_of_payment_code"): |
Suraj Shetty | 48e9bc3 | 2020-01-29 15:06:18 +0530 | [diff] [blame] | 283 | frappe.throw(_("Row {0}: Please set the correct code on Mode of Payment {1}").format(schedule.idx, schedule.mode_of_payment), |
Gaurav | 2670ad7 | 2019-02-19 10:17:17 +0530 | [diff] [blame] | 284 | title=_("E-Invoicing Information Missing")) |
Gaurav | f1e28e0 | 2019-02-13 16:46:24 +0530 | [diff] [blame] | 285 | |
| 286 | prepare_and_attach_invoice(doc) |
| 287 | |
Gaurav | b30a9b1 | 2019-03-01 12:33:19 +0530 | [diff] [blame] | 288 | def prepare_and_attach_invoice(doc, replace=False): |
| 289 | progressive_name, progressive_number = get_progressive_name_and_number(doc, replace) |
Gaurav | f1e28e0 | 2019-02-13 16:46:24 +0530 | [diff] [blame] | 290 | |
| 291 | invoice = prepare_invoice(doc, progressive_number) |
rohitwaghchaure | cdcff6c | 2019-09-09 10:15:01 +0530 | [diff] [blame] | 292 | item_meta = frappe.get_meta("Sales Invoice Item") |
| 293 | |
| 294 | invoice_xml = frappe.render_template('erpnext/regional/italy/e-invoice.xml', |
| 295 | context={"doc": invoice, "item_meta": item_meta}, is_path=True) |
| 296 | |
Rohit Waghchaure | 0f98cb8 | 2019-02-26 15:01:30 +0530 | [diff] [blame] | 297 | invoice_xml = invoice_xml.replace("&", "&") |
Gaurav | f1e28e0 | 2019-02-13 16:46:24 +0530 | [diff] [blame] | 298 | |
| 299 | xml_filename = progressive_name + ".xml" |
Saurabh | acf8379 | 2019-02-24 08:57:16 +0530 | [diff] [blame] | 300 | |
| 301 | _file = frappe.get_doc({ |
| 302 | "doctype": "File", |
| 303 | "file_name": xml_filename, |
| 304 | "attached_to_doctype": doc.doctype, |
| 305 | "attached_to_name": doc.name, |
| 306 | "is_private": True, |
| 307 | "content": invoice_xml |
| 308 | }) |
| 309 | _file.save() |
Aditya Hase | 234d357 | 2019-05-01 11:48:10 +0530 | [diff] [blame] | 310 | return _file |
Gaurav | b30a9b1 | 2019-03-01 12:33:19 +0530 | [diff] [blame] | 311 | |
| 312 | @frappe.whitelist() |
| 313 | def generate_single_invoice(docname): |
| 314 | doc = frappe.get_doc("Sales Invoice", docname) |
Sagar Vora | ba76f87 | 2021-03-29 20:18:45 +0530 | [diff] [blame] | 315 | frappe.has_permission("Sales Invoice", doc=doc, throw=True) |
Rohit Waghchaure | 1b7059b | 2019-03-12 17:44:29 +0530 | [diff] [blame] | 316 | |
Gaurav | b30a9b1 | 2019-03-01 12:33:19 +0530 | [diff] [blame] | 317 | e_invoice = prepare_and_attach_invoice(doc, True) |
Sagar Vora | ba76f87 | 2021-03-29 20:18:45 +0530 | [diff] [blame] | 318 | return e_invoice.file_url |
Gaurav | b30a9b1 | 2019-03-01 12:33:19 +0530 | [diff] [blame] | 319 | |
Sagar Vora | ba76f87 | 2021-03-29 20:18:45 +0530 | [diff] [blame] | 320 | # Delete e-invoice attachment on cancel. |
rohitwaghchaure | c18e925 | 2019-02-20 17:13:15 +0530 | [diff] [blame] | 321 | def sales_invoice_on_cancel(doc, method): |
| 322 | if get_company_country(doc.company) not in ['Italy', |
| 323 | 'Italia', 'Italian Republic', 'Repubblica Italiana']: |
| 324 | return |
| 325 | |
Gaurav | f1e28e0 | 2019-02-13 16:46:24 +0530 | [diff] [blame] | 326 | for attachment in get_e_invoice_attachments(doc): |
| 327 | remove_file(attachment.name, attached_to_doctype=doc.doctype, attached_to_name=doc.name) |
| 328 | |
rohitwaghchaure | c18e925 | 2019-02-20 17:13:15 +0530 | [diff] [blame] | 329 | def get_company_country(company): |
| 330 | return frappe.get_cached_value('Company', company, 'country') |
| 331 | |
Sagar Vora | ba76f87 | 2021-03-29 20:18:45 +0530 | [diff] [blame] | 332 | def get_e_invoice_attachments(invoices): |
| 333 | if not isinstance(invoices, list): |
| 334 | if not invoices.company_tax_id: |
| 335 | return |
| 336 | |
| 337 | invoices = [invoices] |
| 338 | |
| 339 | tax_id_map = { |
| 340 | invoice.name: ( |
| 341 | invoice.company_tax_id |
| 342 | if invoice.company_tax_id.startswith("IT") |
| 343 | else "IT" + invoice.company_tax_id |
| 344 | ) for invoice in invoices |
| 345 | } |
| 346 | |
| 347 | attachments = frappe.get_all( |
| 348 | "File", |
| 349 | fields=("name", "file_name", "attached_to_name", "is_private"), |
| 350 | filters= { |
| 351 | "attached_to_name": ('in', tax_id_map), |
| 352 | "attached_to_doctype": 'Sales Invoice' |
| 353 | } |
| 354 | ) |
Mangesh-Khairnar | 359a73e | 2019-07-04 11:37:20 +0530 | [diff] [blame] | 355 | |
Gaurav | f1e28e0 | 2019-02-13 16:46:24 +0530 | [diff] [blame] | 356 | out = [] |
Gaurav | f1e28e0 | 2019-02-13 16:46:24 +0530 | [diff] [blame] | 357 | for attachment in attachments: |
Sagar Vora | ba76f87 | 2021-03-29 20:18:45 +0530 | [diff] [blame] | 358 | if ( |
| 359 | attachment.file_name |
| 360 | and attachment.file_name.endswith(".xml") |
| 361 | and attachment.file_name.startswith( |
| 362 | tax_id_map.get(attachment.attached_to_name)) |
| 363 | ): |
Gaurav | f1e28e0 | 2019-02-13 16:46:24 +0530 | [diff] [blame] | 364 | out.append(attachment) |
| 365 | |
| 366 | return out |
| 367 | |
Rohit Waghchaure | 74cfe57 | 2019-02-26 20:08:26 +0530 | [diff] [blame] | 368 | def validate_address(address_name): |
| 369 | fields = ["pincode", "city", "country_code"] |
| 370 | data = frappe.get_cached_value("Address", address_name, fields, as_dict=1) or {} |
Gaurav | f1e28e0 | 2019-02-13 16:46:24 +0530 | [diff] [blame] | 371 | |
Rohit Waghchaure | 74cfe57 | 2019-02-26 20:08:26 +0530 | [diff] [blame] | 372 | for field in fields: |
| 373 | if not data.get(field): |
Suraj Shetty | 48e9bc3 | 2020-01-29 15:06:18 +0530 | [diff] [blame] | 374 | frappe.throw(_("Please set {0} for address {1}").format(field.replace('-',''), address_name), |
Rohit Waghchaure | 74cfe57 | 2019-02-26 20:08:26 +0530 | [diff] [blame] | 375 | title=_("E-Invoicing Information Missing")) |
Gaurav | f1e28e0 | 2019-02-13 16:46:24 +0530 | [diff] [blame] | 376 | |
| 377 | def get_unamended_name(doc): |
| 378 | attributes = ["naming_series", "amended_from"] |
| 379 | for attribute in attributes: |
| 380 | if not hasattr(doc, attribute): |
| 381 | return doc.name |
| 382 | |
| 383 | if doc.amended_from: |
| 384 | return "-".join(doc.name.split("-")[:-1]) |
| 385 | else: |
| 386 | return doc.name |
| 387 | |
Gaurav | b30a9b1 | 2019-03-01 12:33:19 +0530 | [diff] [blame] | 388 | def get_progressive_name_and_number(doc, replace=False): |
| 389 | if replace: |
| 390 | for attachment in get_e_invoice_attachments(doc): |
| 391 | remove_file(attachment.name, attached_to_doctype=doc.doctype, attached_to_name=doc.name) |
| 392 | filename = attachment.file_name.split(".xml")[0] |
| 393 | return filename, filename.split("_")[1] |
| 394 | |
Gaurav | f1e28e0 | 2019-02-13 16:46:24 +0530 | [diff] [blame] | 395 | company_tax_id = doc.company_tax_id if doc.company_tax_id.startswith("IT") else "IT" + doc.company_tax_id |
| 396 | progressive_name = frappe.model.naming.make_autoname(company_tax_id + "_.#####") |
| 397 | progressive_number = progressive_name.split("_")[1] |
| 398 | |
Gaurav | 3f04613 | 2019-02-19 16:28:22 +0530 | [diff] [blame] | 399 | return progressive_name, progressive_number |
| 400 | |
Gaurav Naik | 3bf0acb | 2019-02-20 12:08:53 +0530 | [diff] [blame] | 401 | def set_state_code(doc, method): |
Rohit Waghchaure | 74cfe57 | 2019-02-26 20:08:26 +0530 | [diff] [blame] | 402 | if doc.get('country_code'): |
| 403 | doc.country_code = doc.country_code.upper() |
| 404 | |
deepeshgarg007 | 1915e15 | 2019-02-21 17:55:57 +0530 | [diff] [blame] | 405 | if not doc.get('state'): |
| 406 | return |
| 407 | |
Gaurav | 3f04613 | 2019-02-19 16:28:22 +0530 | [diff] [blame] | 408 | if not (hasattr(doc, "state_code") and doc.country in ["Italy", "Italia", "Italian Republic", "Repubblica Italiana"]): |
| 409 | return |
| 410 | |
| 411 | state_codes_lower = {key.lower():value for key,value in state_codes.items()} |
Rohit Waghchaure | 4ef924d | 2019-03-01 16:24:54 +0530 | [diff] [blame] | 412 | |
| 413 | state = doc.get('state','').lower() |
| 414 | if state_codes_lower.get(state): |
| 415 | doc.state_code = state_codes_lower.get(state) |