Skip to Content
Menu
Musisz się zarejestrować, aby móc wchodzić w interakcje z tą społecznością.
To pytanie dostało ostrzeżenie
3 Odpowiedzi
4936 Widoki

Hello everyone

I recently tried rewriting the field journal_id in account_invoices. The thing is, I wanted to add compute method so it'll determine the journal based on different criteria. After that, when I go to Accounts module, the journal_id is probably causing the search to not work at all and show all invoices at all time. Also when I click on Customer Invoices per say, it will always show all invoices, non-grouped.

When rewriting  the default journal_id i copied everything, even the needed methods. Odoo 10.

Here is the class that is doing all this.

class InvoiceWarehouse(models.Model):
    _inherit = 'account.invoice'

# _default journal method for journal_id, needs to be placed above journal_id
    @api.model
    def _default_journal(self):
        if self._context.get('default_journal_id', False):
            return self.env['account.journal'].browse(self._context.get('default_journal_id'))
        inv_type = self._context.get('type', 'out_invoice')
        inv_types = inv_type if isinstance(inv_type, list) else [inv_type]
        company_id = self._context.get('company_id', self.env.user.company_id.id)
        domain = [
            ('type', 'in', [TYPE2JOURNAL[ty] for ty in inv_types if ty in TYPE2JOURNAL]),
            ('company_id', '=', company_id),
        ]
        return self.env['account.journal'].search(domain, limit=1)



# ___________________________________________FIELDS___________________________________________________________

    warehouse_id = fields.Many2one('stock.warehouse', string="Warehouse", compute='_get_warehouse')
    #original journal_id
    journal_id = fields.Many2one('account.journal', string='Journal', required=True, readonly=True, states={'draft': [('readonly', False)]}, default=_default_journal, compute='_get_order_journal', domain="[('type', 'in', {'out_invoice': ['sale'], 'out_refund': ['sale'], 'in_refund': ['purchase'], 'in_invoice': ['purchase']}.get(type, [])), ('company_id', '=', company_id)]") # ____________________________________________________________________________________________________________ def _get_warehouse(self): for res in self: warehouse_id = self.env['sale.order'].search([('name', '=', res.origin)], limit=1).warehouse_id.id if warehouse_id: res.warehouse_id = warehouse_id def _get_order_journal(self): for res in self: journal_id = self.env['sale.order'].search([('name', '=', res.origin)], limit=1).warehouse_id.journal_id print(self.env['sale.order'].search([('name', '=', res.origin)], limit=1).warehouse_id) print(journal_id) if journal_id: res.journal_id = journal_id

Thanks in advance


Awatar
Odrzuć

Just putting store=True did the trick.

Thanks so much to Niyas Raphy, you really did me a solid.

Najlepsza odpowiedź

Hi,

Compute fields will not work in search  method, if you want the computed field in search,  then give store attribute as True for the field and try.


See the sample,


journal_id = fields.Many2one('account.journal', string='Journal', compute="xyz", store=True)


Thanks

Awatar
Odrzuć
Autor Najlepsza odpowiedź

Hi , so store=True did work, but then the computation method was not called because i did not put api_depends. After all that it worked as a charm.

Here's the code now , Thank you so much


    journal_id = fields.Many2one('account.journal', string='Journal',
                                 required=True, readonly=True, store=True,states={'draft': [('readonly', False)]},
                                 default=_default_journal, compute='_get_order_journal',
                                 domain="[('type', 'in', {'out_invoice': ['sale'], 'out_refund': ['sale'], 'in_refund': ['purchase'], 'in_invoice': ['purchase']}.get(type, [])), ('company_id', '=', company_id)]")
   
    @api.depends('origin')
    def _get_order_journal(self):
        for res in self:
            journal_id = self.env['sale.order'].search([('name', '=', res.origin)], limit=1).warehouse_id.journal_id
            print(self.env['sale.order'].search([('name', '=', res.origin)], limit=1).warehouse_id)
            print(journal_id)
            if journal_id:
                res.journal_id = journal_id

Awatar
Odrzuć
Powiązane posty Odpowiedzi Widoki Czynność
2
cze 23
4293
1
kwi 22
3072
2
cze 17
22675
1
paź 15
8680
2
mar 15
10233