Compare commits

..

No commits in common. "main" and "migrate_suppliers" have entirely different histories.

6 changed files with 14 additions and 197 deletions

View file

@ -248,9 +248,3 @@ website_route_rules = [
]
export_python_type_annotations = True
jinja = {
"methods": [
"suppliers_portal.overrides.print.amount_in_words_with_asterisks",
]
}

View file

@ -1,11 +0,0 @@
import frappe
from frappe.utils import flt, money_in_words
@frappe.whitelist()
def amount_in_words_with_asterisks(amount):
amount = flt(amount)
amount_in_words = money_in_words(amount)[4:-6]
if amount % 1 == 0:
return f"{amount_in_words + ' and Zero Cents ':*<70}"
return f"{amount_in_words + 's ':*<84}"

View file

@ -1,84 +0,0 @@
import json
import re
import frappe
from frappe.utils import getdate, update_progress_bar
from suppliers_portal.patches.move_invoices_to_erpnext import get_site_code
def execute():
migrate_site_code_address()
fix_invoices_shipping_addresses()
remove_supplier_links()
def remove_supplier_links():
suppliers_file = frappe.get_site_path("private", "backups", "suppliers.json")
with open(suppliers_file) as f:
suppliers = json.load(f)
for idx, supplier in enumerate(suppliers):
if not frappe.db.exists("Supplier", supplier.get("name")):
continue
update_progress_bar(f"Updating Supplier Addresses: {idx}", idx, (len(suppliers)))
linked_addresses = frappe.get_all(
"Dynamic Link",
{"link_doctype": "Supplier", "link_name": supplier.get("name"), "parenttype": "Address"},
["name", "parent"],
)
address = frappe.new_doc("Address")
address.update(
{
"address_type": "Billing",
"address_title": supplier.get("company_name"),
"address_line1": supplier.get("street") or "No Street Address",
"city": supplier.get("city") or "Unknown",
"state": supplier.get("state"),
"pincode": supplier.get("zip_code"),
"country": "United States",
"is_primary_address": True,
"is_shipping_address": True,
"links": [
{
"link_doctype": "Supplier",
"link_name": supplier.get("name"),
},
],
}
)
address.insert(ignore_permissions=True)
if len(linked_addresses):
for linked_address in linked_addresses:
frappe.rename_doc("Address", linked_address.parent, address.name, merge=True, force=True)
def fix_invoices_shipping_addresses():
invoices = frappe.get_all("Purchase Invoice", {"site_code": ["is", "set"]}, ["name", "site_code"])
for idx, invoice in enumerate(invoices):
update_progress_bar("Updating invoice addresses", idx, (len(invoices)))
frappe.db.set_value(
"Purchase Invoice", invoice.name, "shipping_address", invoice.site_code, update_modified=False
)
if frappe.db.exists("Address", invoice.site_code):
frappe.db.set_value(
"Purchase Invoice", invoice.name, "shipping_address", invoice.site_code, update_modified=False
)
def migrate_site_code_address():
linked_addresses = frappe.get_all(
"Dynamic Link", {"link_doctype": "Site Code", "parenttype": "Address"}, ["parent", "link_name"]
)
for idx, site in enumerate(linked_addresses):
update_progress_bar("Updating Site Codes", idx, (len(linked_addresses)))
if site.parent[-1].isdigit():
merge = True if frappe.db.exists("Address", site.link_name) else False
frappe.rename_doc("Address", site.parent, f"{site.link_name}", merge=merge)
continue
frappe.db.set_value("Site Code", site.link_name, "address", site.parent)
frappe.db.set_value("Address", site.parent, "address_title", site.link_name)
frappe.db.set_value("Address", site.parent, "address_title", site.link_name)
try:
frappe.rename_doc("Address", site.parent, f"{site.link_name}")
except frappe.ValidationError:
frappe.rename_doc("Address", site.parent, f"{site.link_name}", merge=True)

View file

@ -123,22 +123,7 @@ def create_roles():
def import_users():
print("Importing users")
users_file = frappe.get_site_path("private", "backups", "users.json")
with open(users_file) as f:
users = json.load(f)
for idx, user in enumerate(users):
update_progress_bar("Importing users", idx, len(users))
if frappe.db.exists("User", user.get("name")):
frappe.db.set_value("User", user.get("name"), "creation", user.get("creation"))
frappe.db.set_value("User", user.get("name"), "modified", user.get("modified"))
frappe.db.set_value("User", user.get("name"), "owner", user.get("owner"))
frappe.db.set_value("User", user.get("name"), "modified_by", user.get("modified_by"))
continue
user_doc = frappe.new_doc("User")
user["send_welcome_email"] = 0
user_doc.update(user)
user_doc.save()
if user_doc.name != user.get("name"):
frappe.rename_doc("User", user_doc.name, user.get("name"), force=True)
frappe.import_doc(users_file)
def create_items():
@ -221,16 +206,9 @@ def import_suppliers():
suppliers_file = frappe.get_site_path("private", "backups", "suppliers.json")
with open(suppliers_file) as f:
suppliers = json.load(f)
print(len(suppliers))
for idx, supplier in enumerate(suppliers):
update_progress_bar(f"Importing Suppliers: {idx}", idx, (len(suppliers)))
if frappe.db.exists("Supplier", supplier.get("name")):
frappe.db.set_value("Supplier", supplier.get("name"), "creation", supplier.get("creation"))
frappe.db.set_value("Supplier", supplier.get("name"), "modified", supplier.get("modified"))
frappe.db.set_value("Supplier", supplier.get("name"), "owner", supplier.get("owner"))
frappe.db.set_value(
"Supplier", supplier.get("name"), "modified_by", supplier.get("modified_by")
)
continue
supplier_doc = frappe.new_doc("Supplier")
supplier_doc.update(
{
@ -244,7 +222,7 @@ def import_suppliers():
supplier_docname = frappe.rename_doc(
"Supplier", supplier_doc.name, supplier.get("name"), force=True
)
frappe.db.set_value("Supplier", supplier_docname, "supplier_name", supplier.get("company_name"))
frappe.db.set_value('Supplier', supplier_docname, 'supplier_name', supplier.get("company_name"))
if supplier.get("street"):
address = create_address(
@ -266,39 +244,21 @@ def import_invoices():
invoices_file = frappe.get_site_path("private", "backups", "invoices.json")
with open(invoices_file) as f:
invoices = json.load(f)
import_count = 0
for idx, invoice in enumerate(invoices):
update_progress_bar(f"Importing invoices {import_count:5}", idx, (len(invoices)))
update_progress_bar("Importing invoices", idx, (len(invoices)))
# skip if purchase invoice already exists
if frappe.db.exists(
"Purchase Invoice",
{
"supplier": invoice.get("supplier"),
"bill_no": invoice.get("supplier_invoice_number"),
},
):
if frappe.db.exists("Purchase Invoice", {"name": invoice.get("name")}):
continue
if frappe.db.exists(
"Purchase Invoice",
{
"name": invoice.get("name"),
},
):
continue
if getdate(invoice.get("invoice_date")) < getdate("2025-5-1"):
continue
import_count += 1
site_code = get_site_code(invoice)
invoice_doc = frappe.new_doc("Purchase Invoice")
supplier = frappe.db.get_value('Supplier', invoice.get("supplier"), 'name')
invoice_doc.update(
{
"supplier": invoice.get("supplier"),
"bill_no": invoice.get("supplier_invoice_number"),
"supplier_name": frappe.get_value("Supplier", invoice.get("supplier"), "supplier_name"),
"supplier_name": frappe.get_value('Supplier', invoice.get("supplier"), 'supplier_name'),
"site_code": site_code or "",
"set_posting_time": True,
"bill_date": getdate(invoice.get("invoice_date")),
@ -308,6 +268,7 @@ def import_invoices():
"payment_terms_template": invoice.get("invoice_terms"),
"remarks": invoice.get("notes"),
"owner": invoice.get("owner"),
"creation": invoice.get("creation"),
}
)
@ -321,20 +282,12 @@ def import_invoices():
"rate": item.get("rate"),
},
)
# to avoid validating the custom due dates based on payment terms
invoice_doc.ignore_default_payment_terms_template = True
try:
invoice_doc.insert(ignore_permissions=True)
invoice_docname = frappe.rename_doc(
"Purchase Invoice", invoice_doc.name, invoice.get("name"), force=True
)
frappe.db.set_value("Purchase Invoice", invoice_docname, "creation", invoice.get("creation"))
frappe.db.set_value("Purchase Invoice", invoice_docname, "modified", invoice.get("modified"))
frappe.db.set_value("Purchase Invoice", invoice_docname, "owner", invoice.get("owner"))
frappe.db.set_value(
"Purchase Invoice", invoice_docname, "modified_by", invoice.get("modified_by")
)
invoice_doc.name = invoice_docname
invoice_doc.reload()
except Exception as e:
print(f"Error inserting invoice {invoice_doc.bill_no}: {e}")
continue
@ -405,7 +358,6 @@ def import_invoices():
payment_doc.submit()
except Exception as e:
print(f"Error importing payment for invoice {invoice_doc.bill_no}: {e}")
print(f"Imported {import_count} Invoices")
def get_site_code(invoice):
@ -419,30 +371,3 @@ def get_site_code(invoice):
matches = site_pattern.findall(notes)
if len(matches) > 0:
return ", ".join(matches).upper()
def rename_existing_invoices():
invoices_file = frappe.get_site_path("private", "backups", "invoices.json")
with open(invoices_file) as f:
invoices = json.load(f)
rename_count = 0
for idx, invoice in enumerate(invoices):
update_progress_bar(f"Updating invoices {rename_count:5}", idx, (len(invoices)))
existing_invoice = frappe.db.get_value(
"Purchase Invoice",
{
"supplier": invoice.get("supplier"),
"bill_no": invoice.get("supplier_invoice_number"),
},
)
if existing_invoice and existing_invoice != invoice.get("name"):
invoice_docname = frappe.rename_doc(
"Purchase Invoice", existing_invoice, invoice.get("name"), force=True
)
frappe.db.set_value("Purchase Invoice", invoice_docname, "creation", invoice.get("creation"))
frappe.db.set_value("Purchase Invoice", invoice_docname, "modified", invoice.get("modified"))
frappe.db.set_value("Purchase Invoice", invoice_docname, "owner", invoice.get("owner"))
frappe.db.set_value(
"Purchase Invoice", invoice_docname, "modified_by", invoice.get("modified_by")
)
rename_count += 1

View file

@ -4,7 +4,7 @@
"creation": "2025-06-17 01:51:35.709515",
"doctype": "DocType",
"engine": "InnoDB",
"field_order": ["site_code", "column_break_pwyn", "address", "addresses_section", "address_html"],
"field_order": ["site_code", "column_break_pwyn", "addresses_section", "address_html"],
"fields": [
{
"fieldname": "site_code",
@ -15,6 +15,7 @@
"in_standard_filter": 1,
"label": "Site Code",
"no_copy": 1,
"not_nullable": 1,
"reqd": 1,
"unique": 1
},
@ -33,17 +34,11 @@
"fieldname": "addresses_section",
"fieldtype": "Section Break",
"label": "Addresses"
},
{
"fieldname": "address",
"fieldtype": "Link",
"label": "Address",
"options": "Address"
}
],
"index_web_pages_for_search": 1,
"links": [],
"modified": "2025-07-01 16:15:49.066309",
"modified": "2025-06-17 02:29:32.191624",
"modified_by": "Administrator",
"module": "Suppliers Portal",
"name": "Site Code",
@ -63,7 +58,6 @@
"write": 1
}
],
"row_format": "Dynamic",
"sort_field": "creation",
"sort_order": "DESC",
"states": [],

View file

@ -14,7 +14,6 @@ class SiteCode(Document):
if TYPE_CHECKING:
from frappe.types import DF
address: DF.Link | None
site_code: DF.Data
# end: auto-generated types