Skip to content
Snippets Groups Projects

[IMP] energy_selfconsumption: importation of selfconsumption inscriptions, supply points and distribution tables by CSV

1 file
+ 29
32
Compare changes
  • Side-by-side
  • Inline
@@ -25,22 +25,19 @@ class SelfconsumptionImportWizard(models.TransientModel):
@@ -25,22 +25,19 @@ class SelfconsumptionImportWizard(models.TransientModel):
raise ValidationError("Only csv format files are accepted.")
raise ValidationError("Only csv format files are accepted.")
def import_file_button(self):
def import_file_button(self):
 
error_string_list = ''
file_data = base64.b64decode(self.import_file)
file_data = base64.b64decode(self.import_file)
parsing_data = self.with_context(active_id=self.ids[0])._parse_file(file_data)
parsing_data = self.with_context(active_id=self.ids[0])._parse_file(file_data)
active_id = self.env.context.get('active_id')
active_id = self.env.context.get('active_id')
project = self.env['energy_selfconsumption.selfconsumption'].browse(active_id)
project = self.env['energy_selfconsumption.selfconsumption'].browse(active_id)
parsing_errors = []
for index, line in enumerate(parsing_data[1:]):
for index, single_statement_data in enumerate(parsing_data[1:]):
error = self.import_line(line, project)
found = self.import_line(single_statement_data, project)
if error:
if not found:
error_string_list = "".join(
parsing_errors.append((index, single_statement_data))
[error_string_list, _('<li>Line {line}: {error}</li>\n').format(index, error)])
if parsing_errors:
if error_string_list:
error_list = ''
project.message_post(subject=_('Import Errors'),
for error in parsing_errors:
body=_('Import errors found: <ul>{list}</ul>'.format(list=error_string_list)))
error_list = "".join(
[error_list, _('<li>Line: {index} DNI: {vat} </li>\n').format(index=error[0], vat=error[1][0])])
project.message_post(subject=_('Import Result'),
body=_('Partners not found for: <ul>{list}</ul>'.format(list=error_list)))
return True
return True
def _parse_file(self, data_file):
def _parse_file(self, data_file):
@@ -71,27 +68,27 @@ class SelfconsumptionImportWizard(models.TransientModel):
@@ -71,27 +68,27 @@ class SelfconsumptionImportWizard(models.TransientModel):
], limit=1)
], limit=1)
if not partner:
if not partner:
if line[2]:
return _('Partner with VAT:<b>{vat}</b> was not found.').format(vat=line[0])
partner = self.env['res.partner'].create({
'vat': line[0],
if not project.inscription_ids.filtered_domain([('partner_id', '=', partner.id)]):
'firstname': line[2],
try:
'lastname': line[3],
self.env['energy_project.inscription'].create({
'company_type': 'person'
'project_id': project.id,
 
'partner_id': partner.id,
 
'effective_date': fields.date.today()
})
})
else:
except:
return False
return _('Could not create inscription.').format(vat=line[0])
if partner.id in project.inscription_ids.mapped('partner_id.id'):
return False
supply_point = self.env['energy_selfconsumption.supply_point'].search(
try:
[('code', '=', line[2]), ('company_id', '=', self.env.company)])
self.env['energy_project.inscription'].create({
'project_id': project.id,
if supply_point and supply_point.partner_id != partner:
'partner_id': partner.id,
return _(
'effective_date': fields.date.today()
'The supply point partner {supply_partner} and the partner {vat} in the subscription are different.').format(
}
supply_partner=supply_point.partner_id.vat, vat=partner.vat)
)
except:
return False
return False
return True
def create_supply_point(self, code, street, street2, city, state, zip, country, owner_vat):
def create_supply_point(self, code, street, street2, city, state, zip, country, owner_vat):
owner = self.env['res.partner'].search([
owner = self.env['res.partner'].search([
Loading