0

I have a problem where I have a lot of data records, and when I try to download more than 1 data record an error always appears which basically looks like this

File "c:\\users\\it uw\\odoo14\\mspcustomaddons\\l10n_id_efaktur\\models\\account_move.py ", line 136, in download_efaktur
self.\_generate_efaktur(',')
File "c:\\users\\it uw\\odoo14\\mspcustomaddons\\l10n_id_efaktur\\models\\account_move.py", line 296, in \_generate_efaktur
output_head = self.\_generate_efaktur_invoice(delimiter)
File "c:\\users\\it uw\\odoo14\\mspcustomaddons\\l10n_id_efaktur\\models\\account_move.py", line 144, in \_generate_efaktur_invoice
company_id = self. company_id
File "C:\\Users\\IT UW\\odoo14\\flectra\\flectra\\fields.py", line 947, in __get__
record. ensure_one()
File "C:\\Users\\IT UW\\odoo14\\flectra\\flectra\\models.py", line 4437, in ensure_one
raise ValueError("Expected singleton: %s" % self)
ValueError: \<class 'ValueError'\>: "Expected singleton: account. invoice(35590, 35589, 35588, 35587)" while evaluating
'action = records.download_effect()'

And here is the code

@api.multi
def download_efaktur(self):
    """Collect the data and execute function _generate_efaktur."""
    for record in self:
        if record.state == 'draft':
            raise ValidationError(_('Could not download E-faktur in draft state'))
        if record.partner_id.l10n_id_pkp and not record.l10n_id_tax_number:
            raise ValidationError(_('Connect %(move_number)s with E-faktur to download this report',
                                  move_number=record.name))
        
    self._generate_efaktur(',')
    return self.download_csv()
        
@api.multi
def _generate_efaktur(self, delimiter):
    if self.filtered(lambda x: not x.l10n_id_kode_transaksi):
        raise UserError(_('Some documents don\'t have a transaction code'))
    if self.filtered(lambda x: x.type != 'out_invoice'):
        raise UserError(_('Some documents are not Customer Invoices'))
    
    output_head = self._generate_efaktur_invoice(delimiter)
    my_utf8 = output_head.encode("utf-8")
    out = base64.b64encode(my_utf8)
    
    attachment = self.env['ir.attachment'].create({
        'datas': out,
        'name': 'efaktur_%s.csv' % (fields.Datetime.to_string(fields.Datetime.now())
                                                   .replace(" ", "_")),
        'type': 'binary',
        })
        
    for record in self:
        record.message_post(attachment_ids=[attachment.id])
    self.l10n_id_attachment_id = attachment.id
    return {
            'type': 'ir.actions.client',
            'tag': 'reload',
        }

I have tried to change the looping logic in the download_efaktur() method and also the generate_efaktur() method but the error still appears.

greybeard
  • 2,249
  • 8
  • 30
  • 66
it uw
  • 1

1 Answers1

0

The error seems to be in this line

File "c:\users\it uw\odoo14\mspcustomaddons\l10n_id_efaktur\models\account_move.py", line 144, in _generate_efaktur_invoice company_id = self.company_id

You haven't showed the code of the method _generate_efaktur_invoice

But when you make the call to:

output_head = self._generate_efaktur_invoice(delimiter)

Seems that self it's a recordset of more than one record so the code self.company_id will raise an error because it will call to self.ensure_one() as part of the ORM operations to retrieve the value of the company_id field that expect to be from only one record

aekis.dev
  • 2,626
  • 1
  • 12
  • 19
  • @api.multi def _generate_efaktur_invoice(self, delimiter): """Generate E-Faktur for customer invoice.""" # Invoice of Customer for invoice in self: company_id = self.company_id dp_product_id = self.env['ir.config_parameter'].sudo().get_param('sale.default_deposit_product_id') output_head = '%s%s%s' % ( _csv_row(FK_HEAD_LIST, delimiter), _csv_row(LT_HEAD_LIST, delimiter), _csv_row(OF_HEAD_LIST, delimiter), ) is in that code? – it uw Aug 14 '23 at 04:55
  • yes, check it there – aekis.dev Aug 14 '23 at 19:12