diff --git a/msp/msp/doctype/auto_invoice_generator/auto_invoice_generator.json b/msp/msp/doctype/auto_invoice_generator/auto_invoice_generator.json index b64baf5..a50816e 100644 --- a/msp/msp/doctype/auto_invoice_generator/auto_invoice_generator.json +++ b/msp/msp/doctype/auto_invoice_generator/auto_invoice_generator.json @@ -12,6 +12,9 @@ "invoices_from_delivery_notes_section", "customer", "get_invoice", + "it_contract_invoices_section", + "billing_month", + "contract_invoices", "statistics", "date", "invoice_count", @@ -74,12 +77,28 @@ "fieldname": "log", "fieldtype": "Small Text", "label": "Log" + }, + { + "fieldname": "billing_month", + "fieldtype": "Data", + "label": "Billing Month" + }, + { + "fieldname": "contract_invoices", + "fieldtype": "Button", + "label": "Contract Invoices", + "options": "get_invoices" + }, + { + "fieldname": "it_contract_invoices_section", + "fieldtype": "Section Break", + "label": "IT Contract Invoices" } ], "index_web_pages_for_search": 1, "links": [], - "migration_hash": "81a435d8fdde574c14c22be58f9e32ca", - "modified": "2022-09-22 14:23:33.413375", + "migration_hash": "d64bd35ec0b0d442bcfc080be217f2f6", + "modified": "2023-05-31 11:12:52.773165", "modified_by": "Administrator", "module": "MSP", "name": "Auto Invoice Generator", diff --git a/msp/msp/doctype/auto_invoice_generator/auto_invoice_generator.py b/msp/msp/doctype/auto_invoice_generator/auto_invoice_generator.py index 3d88791..e502755 100644 --- a/msp/msp/doctype/auto_invoice_generator/auto_invoice_generator.py +++ b/msp/msp/doctype/auto_invoice_generator/auto_invoice_generator.py @@ -127,11 +127,12 @@ class AutoInvoiceGenerator(Document): print(len(customer_list)) cust_count = 0 invoice_count = 0 + log_list = [] for cust in customer_list: cust_doc = frappe.get_doc("Customer",cust) invoice_in_draft = frappe.get_all("Sales Invoice", filters = {"status" : "Draft", "customer": cust}) if len(invoice_in_draft) > 0: - self.log = "Für Kunde"+ " "+ cust + " wurden keine Rechnungen erstellt, da noch nicht berechnete Rechnungen in Draft vorhanden" + log_list.append("Für Kunde"+ " "+ cust + " wurden keine Rechnungen erstellt, da noch nicht berechnete Rechnungen in Draft vorhanden") continue else: cust_count += 1 @@ -237,7 +238,13 @@ class AutoInvoiceGenerator(Document): frappe.msgprint("Für " + str(cust_count)+ " Kunden wurden " + str(invoice_count) + " Rechnungen erstellt.") self.date = datetime.today().strftime('%Y-%m-%d') self.invoice_count = invoice_count - self.customer_count = cust_count + self.customer_count = cust_count + log_str = "" + for i in log_list: + log_str += i + "\n" + + self.log = log_str + self.save() def create_invoice_doc_item(self, item): @@ -292,4 +299,108 @@ class AutoInvoiceGenerator(Document): invoice_doc.append("taxes", new_tax) invoice_doc.save() - + @frappe.whitelist() + def get_invoices(self): + contract_list = frappe.get_all("IT Contract", filters ={"status":"active", "billing_active":1}) + print(contract_list) + if len(contract_list)>0: + count = 0 + for contr in contract_list: + contract= frappe.get_doc("IT Contract",contr.name) + #if contract.billing_active == 1: + if contract.invoicing_in_advance == 1: + billing_month = self.set_billing_month_plus(self.billing_month) + print(billing_month) + else: + billing_month = self.billing_month + customer = contract.customer + items = contract.items + if contract.introduction_text: + introduction = str(contract.introduction_text) +"Leistungszeitraum " + billing_month + else: + introduction = "Leistungszeitraum " + billing_month + title = contract.it_contract_type+ " " + billing_month + " " + contract.customer_name + print(title) + inv_title_list = frappe.get_all("Sales Invoice", filters = {"title":title}) + if len(inv_title_list) == 0: + count += 1 + invoice_items = [self.create_contract_invoice_doc_item(el) for el in items] + invoice_doc = self.create_contract_invoice(customer,invoice_items,title, introduction) + if count == 0: + frappe.msgprint("Für den angegebenen Zeitraum wurden bereits alle Rechnungen erstellt") + + + else: + frappe.msgprint("Keine Kontrakte abzurechnen") + + def create_contract_invoice_doc_item(self, item): + #Funktion kreiert Invoice Item aus den gegebenen IT Contract Items + invoice_doc_item = frappe.get_doc({ + "doctype": "Sales Invoice Item", + "item_code": item.item_code, + "description": item.description, + "qty": item.qty, + "uom" : "Stk", + "rate": item.rate, + + }) + return invoice_doc_item + + def create_contract_invoice(self,cust,invoice_doc_items,title, introduction): + invoice_doc = frappe.get_doc({ + "doctype": "Sales Invoice", + "title": title, + "customer": cust, + "company": frappe.get_doc("Global Defaults").default_company, + "items": invoice_doc_items, + "introduction_text":introduction, + + }) + if len(invoice_doc_items)>0: + + settings_doc = frappe.get_single("Auto Invoice Generator Settings") + customer_doc = frappe.get_doc("Customer", cust ) + + if customer_doc.payment_terms: + invoice_doc.payment_terms_template = customer_doc.payment_terms + else: + invoice_doc.payment_terms_template = settings_doc.payment_terms_template + invoice_doc.tc_name = settings_doc.tc_name + tac_doc = frappe.get_doc("Terms and Conditions", settings_doc.tc_name) + invoice_doc.terms = tac_doc.terms + + invoice_doc.taxes_and_charges = party_st(invoice_doc.customer, "Customer", invoice_doc.posting_date, invoice_doc.company) + taxes = frappe.get_doc("Sales Taxes and Charges Template", settings_doc.taxes_and_charges).taxes + + for tax in taxes: + new_tax = frappe.get_doc({ + "doctype": "Sales Taxes and Charges", + "charge_type": tax.charge_type, + "account_head": tax.account_head, + "rate": tax.rate, + "description": tax.description + }) + invoice_doc.append("taxes", new_tax) + invoice_doc.save() + + def set_billing_month_plus(self,date): + + month, year = date.split('.') + month = int(month) + year = int(year) + + # Den Monat um eins erhöhen + month += 1 + + # Wenn der Monat größer als 12 ist, das Jahr um eins erhöhen und den Monat auf 1 setzen + if month > 12: + month = 1 + year += 1 + + + new_billing_month = "{:02d}.{}".format(month, year) + return new_billing_month + + + + diff --git a/msp/msp/doctype/it_contract/it_contract.json b/msp/msp/doctype/it_contract/it_contract.json index 5250920..3beebf0 100644 --- a/msp/msp/doctype/it_contract/it_contract.json +++ b/msp/msp/doctype/it_contract/it_contract.json @@ -29,6 +29,7 @@ "column_break_19", "accounting_period", "billing_active", + "invoicing_in_advance", "history_section", "delivery_note_list" ], @@ -178,10 +179,17 @@ "fieldname": "introduction_text", "fieldtype": "Text Editor", "label": "Introduction Text" + }, + { + "default": "0", + "fieldname": "invoicing_in_advance", + "fieldtype": "Check", + "label": "Invoicing in advance" } ], "links": [], - "modified": "2023-02-22 20:09:42.111402", + "migration_hash": "c5e40dc088dd7fd9d7d8cc905833c3f8", + "modified": "2023-05-31 11:06:24.441274", "modified_by": "Administrator", "module": "MSP", "name": "IT Contract", diff --git a/msp/msp/doctype/it_contract_item/it_contract_item.json b/msp/msp/doctype/it_contract_item/it_contract_item.json index 324a2b8..5997f13 100644 --- a/msp/msp/doctype/it_contract_item/it_contract_item.json +++ b/msp/msp/doctype/it_contract_item/it_contract_item.json @@ -30,6 +30,7 @@ }, { "fetch_from": "item_code.description", + "fetch_if_empty": 1, "fieldname": "description", "fieldtype": "Text Editor", "label": "Description" @@ -56,7 +57,8 @@ "index_web_pages_for_search": 1, "istable": 1, "links": [], - "modified": "2023-02-22 19:51:22.759330", + "migration_hash": "9e4c62717954cad74301d644a4b105a2", + "modified": "2023-05-31 11:45:32.701190", "modified_by": "Administrator", "module": "MSP", "name": "IT Contract Item",