""" Create custom Print Format for Sales Invoice — Gigafibre/TARGO style. Inspired by Cogeco layout: summary page 1, details page 2, envelope window address. Run inside erpnext-backend-1: /home/frappe/frappe-bench/env/bin/python /home/frappe/frappe-bench/setup_invoice_print_format.py """ import os, sys os.chdir("/home/frappe/frappe-bench/sites") import frappe frappe.init(site="erp.gigafibre.ca", sites_path=".") frappe.connect() print("Connected:", frappe.local.site) # ── Update Print Settings to Letter size ── from frappe.installer import update_site_config frappe.db.set_single_value("Print Settings", "pdf_page_size", "Letter") frappe.db.commit() print(" PDF page size set to Letter") # ── Register the logo file if not exists ── if not frappe.db.exists("File", {"file_url": "/files/targo-logo-green.svg"}): f = frappe.get_doc({ "doctype": "File", "file_name": "targo-logo-green.svg", "file_url": "/files/targo-logo-green.svg", "is_private": 0, }) f.insert(ignore_permissions=True) frappe.db.commit() print(" Registered logo file") PRINT_FORMAT_NAME = "Facture TARGO" html_template = r""" {%- set company_name = "TARGO Communications" -%} {%- set company_addr = "123 rue Principale" -%} {%- set company_city = "Victoriaville QC G6P 1A1" -%} {%- set company_tel = "(819) 758-1555" -%} {%- set company_web = "gigafibre.ca" -%} {%- set tps_no = "TPS: #819304698RT0001" -%} {%- set tvq_no = "TVQ: #1215640113TQ0001" -%} {%- set brand_green = "#019547" -%} {%- set brand_light = "#e8f5ee" -%} {%- set is_credit = doc.is_return == 1 -%} {%- set mois_fr = {"January":"janvier","February":"février","March":"mars","April":"avril","May":"mai","June":"juin","July":"juillet","August":"août","September":"septembre","October":"octobre","November":"novembre","December":"décembre"} -%} {%- macro date_fr(d) -%} {%- if d -%} {%- set dt = frappe.utils.getdate(d) -%} {{ dt.day }} {{ mois_fr.get(dt.strftime("%B"), dt.strftime("%B")) }} {{ dt.year }} {%- else -%}—{%- endif -%} {%- endmacro -%} {%- macro date_short(d) -%} {%- if d -%} {%- set dt = frappe.utils.getdate(d) -%} {{ "%02d/%02d/%04d" | format(dt.day, dt.month, dt.year) }} {%- else -%}—{%- endif -%} {%- endmacro -%} {# Decode HTML entities in item names #} {%- macro clean(s) -%}{{ s | replace("'", "'") | replace("&", "&") | replace("<", "<") | replace(">", ">") | replace(""", '"') if s else "" }}{%- endmacro -%} {# Get customer address from Service Location or address_display #} {%- set cust_addr = doc.address_display or "" -%} {%- if not cust_addr and doc.customer_address -%} {%- set addr_doc = frappe.get_doc("Address", doc.customer_address) -%} {%- set cust_addr = (addr_doc.address_line1 or "") + "\n" + (addr_doc.city or "") + " " + (addr_doc.state or "") + " " + (addr_doc.pincode or "") -%} {%- endif -%}
{% if is_credit %}NOTE DE CRÉDIT{% else %}FACTURE{% endif %}
Services fournis à
{{ doc.customer_name or doc.customer }}
{% if cust_addr %}
{{ cust_addr | striptags | replace("\n","
") }}
{% endif %}
{% if doc.due_date %} {% endif %}
Nº de compte
{{ doc.customer }}
Nº de facture
{{ doc.name }}
Date de facturation
{{ date_fr(doc.posting_date) }}
Date d'échéance
{{ date_fr(doc.due_date) }}
{{ tps_no }}  |  {{ tvq_no }}
{% if is_credit %}
Note de crédit {% if doc.return_against %} — Renversement de la facture {{ doc.return_against }}{% endif %}
{% endif %}
{% if is_credit %}MONTANT CRÉDITÉ{% else %}MONTANT TOTAL DÛ{% endif %} {{ frappe.utils.fmt_money(doc.grand_total | abs, currency=doc.currency) }}
SOMMAIRE DU COMPTE
{% if doc.outstanding_amount != doc.grand_total %} {% endif %} {%- set service_groups = {} -%} {%- for item in doc.items -%} {%- set group = item.item_group or "Services" -%} {%- if group not in service_groups -%} {%- set _ = service_groups.update({group: 0}) -%} {%- endif -%} {%- set _ = service_groups.update({group: service_groups[group] + (item.amount or 0)}) -%} {%- endfor -%} {% for group, amount in service_groups.items() %} {% endfor %} {% for tax in doc.taxes %} {% endfor %}
Solde antérieur {{ frappe.utils.fmt_money((doc.outstanding_amount or 0) - (doc.grand_total or 0), currency=doc.currency) }}
Frais du mois courant
{{ group }} {{ frappe.utils.fmt_money(amount, currency=doc.currency) }}
Sous-total avant taxes {{ frappe.utils.fmt_money(doc.net_total, currency=doc.currency) }}
{{ "TPS" if "TPS" in (tax.description or tax.account_head or "") else "TVQ" }} ({{ tax.rate }}%) {{ frappe.utils.fmt_money(tax.tax_amount, currency=doc.currency) }}
Total du mois courant {{ frappe.utils.fmt_money(doc.grand_total, currency=doc.currency) }}
Contactez-nous
{{ company_tel }}
{{ company_web }}
Service à la clientèle
Lun-Ven 8h-17h
info@gigafibre.ca

QR
Payez en ligne
Scannez le code QR ou visitez
{{ company_web }}/payer
✂ Prière d'expédier cette partie avec votre paiement
Montant versé
________
Nº de compte
{{ doc.customer }}
Date d'échéance
{{ date_short(doc.due_date) }}
Montant à payer
{{ frappe.utils.fmt_money(doc.grand_total | abs, currency=doc.currency) }}
{{ doc.customer_name or doc.customer }}
{% if cust_addr %} {{ cust_addr | striptags | replace("\n","
") }} {% endif %}
Nº de facture: {{ doc.name }}
Nº de compte: {{ doc.customer }}
Date: {{ date_fr(doc.posting_date) }}
DÉTAILS DE LA FACTURE
{% for item in doc.items %} {% endfor %} {% for tax in doc.taxes %} {% endfor %}
Description Qté Prix unit. Montant
{{ clean(item.item_name or item.item_code) }} {{ item.qty | int if item.qty == (item.qty | int) else item.qty }} {{ frappe.utils.fmt_money(item.rate, currency=doc.currency) }} {{ frappe.utils.fmt_money(item.amount, currency=doc.currency) }}
Sous-total avant taxes {{ frappe.utils.fmt_money(doc.net_total, currency=doc.currency) }}
{{ "TPS" if "TPS" in (tax.description or tax.account_head or "") else "TVQ" }} ({{ tax.rate }}%) {{ frappe.utils.fmt_money(tax.tax_amount, currency=doc.currency) }}
TOTAL {{ frappe.utils.fmt_money(doc.grand_total, currency=doc.currency) }}
TARGO
""" # Create or update the Print Format if frappe.db.exists("Print Format", PRINT_FORMAT_NAME): doc = frappe.get_doc("Print Format", PRINT_FORMAT_NAME) doc.html = html_template doc.save(ignore_permissions=True) print(f" Updated Print Format: {PRINT_FORMAT_NAME}") else: doc = frappe.get_doc({ "doctype": "Print Format", "name": PRINT_FORMAT_NAME, "__newname": PRINT_FORMAT_NAME, "doc_type": "Sales Invoice", "module": "Accounts", "print_format_type": "Jinja", "standard": "No", "custom_format": 1, "html": html_template, "default_print_language": "fr", "disabled": 0, }) doc.insert(ignore_permissions=True) print(f" Created Print Format: {PRINT_FORMAT_NAME}") # Set as default for Sales Invoice frappe.db.set_value("Property Setter", None, "value", PRINT_FORMAT_NAME, { "doctype_or_field": "DocType", "doc_type": "Sales Invoice", "property": "default_print_format", }) frappe.db.commit() print(f" Done! Print Format '{PRINT_FORMAT_NAME}' ready.") print(" Preview: ERPNext → Sales Invoice → Print → Select 'Facture TARGO'")