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