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