@@ -757,50 +757,78 @@ def _l10n_it_buyer_seller_info(self):
757757 )
758758 return buyer_seller_info
759759
760- def _l10n_it_edi_extension_prepare_partner_values (self , tree , section_xpath = None ):
761- if section_xpath :
762- partner_info = {
763- "name_xpath" : f"{ section_xpath } //Denominazione" ,
764- "first_name_xpath" : f"{ section_xpath } //Nome" ,
765- "last_name_xpath" : f"{ section_xpath } //Cognome" ,
766- "country_code_xpath" : f"{ section_xpath } //IdPaese" ,
767- "vat_xpath" : f"{ section_xpath } //IdCodice" ,
768- "codice_fiscale_xpath" : f"{ section_xpath } //CodiceFiscale" ,
769- "eori_code_xpath" : f"{ section_xpath } //CodEORI" ,
770- }
771- else :
760+ def _l10n_it_edi_extension_get_partner_info_by_role (self , tree , role ):
761+ if role in ("buyer" , "seller" ):
772762 buyer_seller_info = self ._l10n_it_buyer_seller_info ()
773- is_incoming = self .is_purchase_document (include_receipts = True )
774- partner_role = "seller" if is_incoming else "buyer"
775- partner_info = buyer_seller_info [partner_role ]
763+ partner_info = buyer_seller_info [role ]
764+ else :
765+ role_to_xpath = {
766+ "intermediary" : "//TerzoIntermediarioOSoggettoEmittente" ,
767+ "tax_representative" : "//RappresentanteFiscale" ,
768+ }
769+ section_xpath = role_to_xpath .get (role )
770+ if section_xpath is None :
771+ raise UserError (
772+ self .env ._ (
773+ "Role %(role)s is not supported for partner creation" , role = role
774+ )
775+ )
776776
777- name = get_text (tree , partner_info ["name_xpath" ])
778- country_code = get_text (tree , partner_info ["country_code_xpath" ])
779- country = self .env ["res.country" ].search ([("code" , "=" , country_code )], limit = 1 )
780- vals = {
781- "vat" : get_text (tree , partner_info ["vat_xpath" ]),
782- "l10n_it_codice_fiscale" : get_text (
783- tree , partner_info ["codice_fiscale_xpath" ]
784- ),
785- "is_company" : bool (len (name )),
786- "l10n_edi_it_eori_code" : get_text (tree , partner_info ["eori_code_xpath" ]),
787- "country_id" : country .id ,
788- }
777+ if tree .xpath (section_xpath ):
778+ partner_info = {
779+ "name_xpath" : f"{ section_xpath } //Denominazione" ,
780+ "first_name_xpath" : f"{ section_xpath } //Nome" ,
781+ "last_name_xpath" : f"{ section_xpath } //Cognome" ,
782+ "country_code_xpath" : f"{ section_xpath } //IdPaese" ,
783+ "vat_xpath" : f"{ section_xpath } //IdCodice" ,
784+ "codice_fiscale_xpath" : f"{ section_xpath } //CodiceFiscale" ,
785+ "eori_code_xpath" : f"{ section_xpath } //CodEORI" ,
786+ }
787+ else :
788+ partner_info = dict ()
789+ return partner_info
789790
790- if name :
791- vals ["name" ] = name
792- else :
791+ def _l10n_it_edi_extension_prepare_partner_values (self , tree , role ):
792+ if partner_info := self ._l10n_it_edi_extension_get_partner_info_by_role (
793+ tree , role
794+ ):
795+ vals = {
796+ "vat" : get_text (tree , partner_info ["vat_xpath" ]),
797+ "l10n_it_codice_fiscale" : get_text (
798+ tree , partner_info ["codice_fiscale_xpath" ]
799+ ),
800+ "l10n_edi_it_eori_code" : get_text (
801+ tree , partner_info ["eori_code_xpath" ]
802+ ),
803+ }
804+
805+ country_code = get_text (tree , partner_info ["country_code_xpath" ])
806+ if country := self .env ["res.country" ].search (
807+ [("code" , "=" , country_code )], limit = 1
808+ ):
809+ vals ["country_id" ] = country .id
810+
811+ if name := get_text (tree , partner_info ["name_xpath" ]):
812+ vals ["name" ] = name
813+ vals ["is_company" ] = True
793814 if first_name := get_text (tree , partner_info ["first_name_xpath" ]):
794815 vals ["firstname" ] = first_name
795816 if last_name := get_text (tree , partner_info ["last_name_xpath" ]):
796817 vals ["lastname" ] = last_name
818+ else :
819+ vals = dict ()
797820 return vals
798821
799- def _l10n_it_edi_extension_create_partner (self , invoice_data , section_xpath = None ):
822+ def _l10n_it_edi_extension_create_partner (self , invoice_data , role ):
800823 partner_values = self ._l10n_it_edi_extension_prepare_partner_values (
801- invoice_data , section_xpath
824+ invoice_data ,
825+ role ,
802826 )
803- return self .env ["res.partner" ].create (partner_values )
827+ if partner_values :
828+ partner = self .env ["res.partner" ].create (partner_values )
829+ else :
830+ partner = self .env ["res.partner" ].browse ()
831+ return partner
804832
805833 def _l10n_it_edi_import_invoice (self , invoice , data , is_new ):
806834 invoice = super ()._l10n_it_edi_import_invoice (invoice , data , is_new )
@@ -811,18 +839,22 @@ def _l10n_it_edi_import_invoice(self, invoice, data, is_new):
811839 and not invoice .partner_id
812840 and self .env .company .l10n_edi_it_create_partner
813841 ):
814- partner = self ._l10n_it_edi_extension_create_partner (body_tree )
842+ is_incoming = self .is_purchase_document (include_receipts = True )
843+ partner = self ._l10n_it_edi_extension_create_partner (
844+ body_tree ,
845+ "seller" if is_incoming else "buyer" ,
846+ )
815847 invoice .partner_id = partner
816848
817- if body_tree . xpath ( "//RappresentanteFiscale" ):
818- tax_representative = self . _l10n_it_edi_extension_create_partner (
819- body_tree , section_xpath = "//RappresentanteFiscale"
820- )
849+ if tax_representative := self . _l10n_it_edi_extension_create_partner (
850+ body_tree ,
851+ "tax_representative" ,
852+ ):
821853 invoice .l10n_it_edi_tax_representative_id = tax_representative .id
822854
823- if body_tree . xpath ( "//TerzoIntermediarioOSoggettoEmittente" ):
824- intermediary = self . _l10n_it_edi_extension_create_partner (
825- body_tree , section_xpath = "//TerzoIntermediarioOSoggettoEmittente"
826- )
855+ if intermediary := self . _l10n_it_edi_extension_create_partner (
856+ body_tree ,
857+ "intermediary" ,
858+ ):
827859 invoice .l10n_it_edi_intermediary_id = intermediary .id
828860 return invoice
0 commit comments