55import frappe
66from frappe import _ , scrub
77from frappe .model .document import Document
8- from frappe .utils import flt , nowdate
8+ from frappe .utils import escape_html , flt , nowdate
99from frappe .utils .background_jobs import enqueue , is_job_enqueued
1010
1111from erpnext .accounts .doctype .accounting_dimension .accounting_dimension import (
@@ -32,6 +32,7 @@ class OpeningInvoiceCreationTool(Document):
3232 create_missing_party : DF .Check
3333 invoice_type : DF .Literal ["Sales" , "Purchase" ]
3434 invoices : DF .Table [OpeningInvoiceCreationToolItem ]
35+ project : DF .Link | None
3536 # end: auto-generated types
3637
3738 def onload (self ):
@@ -85,6 +86,11 @@ def prepare_invoice_summary(doctype, invoices):
8586 )
8687 prepare_invoice_summary (doctype , invoices )
8788
89+ invoices_summary_companies = list (invoices_summary .keys ())
90+
91+ for company in invoices_summary_companies :
92+ invoices_summary [escape_html (company )] = invoices_summary .pop (company )
93+
8894 return invoices_summary , max_count
8995
9096 def validate_company (self ):
@@ -102,18 +108,28 @@ def set_missing_values(self, row):
102108 row .due_date = row .due_date or nowdate ()
103109
104110 def validate_mandatory_invoice_fields (self , row ):
105- if not frappe .db .exists (row .party_type , row .party ):
106- if self .create_missing_party :
107- self .add_party (row .party_type , row .party )
108- else :
111+ if self .create_missing_party :
112+ if not row .party and not row .party_name :
113+ frappe .throw (_ ("Row #{}: Either Party ID or Party Name is required" ).format (row .idx ))
114+
115+ if not row .party and row .party_name :
116+ row .party = self .add_party (row .party_type , row .party_name )
117+
118+ if row .party and not frappe .db .exists (row .party_type , row .party ):
119+ row .party = self .add_party (row .party_type , row .party )
120+
121+ else :
122+ if not row .party :
123+ frappe .throw (_ ("Row #{}: Party ID is required" ).format (row .idx ))
124+ if not frappe .db .exists (row .party_type , row .party ):
109125 frappe .throw (
110126 _ ("Row #{}: {} {} does not exist." ).format (
111127 row .idx , frappe .bold (row .party_type ), frappe .bold (row .party )
112128 )
113129 )
114130
115131 mandatory_error_msg = _ ("Row #{0}: {1} is required to create the Opening {2} Invoices" )
116- for d in ("Party" , " Outstanding Amount" , "Temporary Opening Account" ):
132+ for d in ("Outstanding Amount" , "Temporary Opening Account" ):
117133 if not row .get (scrub (d )):
118134 frappe .throw (mandatory_error_msg .format (row .idx , d , self .invoice_type ))
119135
@@ -159,6 +175,7 @@ def add_party(self, party_type, party):
159175
160176 party_doc .flags .ignore_mandatory = True
161177 party_doc .save (ignore_permissions = True )
178+ return party_doc .name
162179
163180 def get_invoice_dict (self , row = None ):
164181 def get_item_dict ():
@@ -262,7 +279,8 @@ def start_import(invoices):
262279 doc .flags .ignore_mandatory = True
263280 doc .insert (set_name = invoice_number )
264281 doc .submit ()
265- frappe .db .commit ()
282+ if not frappe .in_test :
283+ frappe .db .commit ()
266284 names .append (doc .name )
267285 except Exception :
268286 errors += 1
0 commit comments