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
Just putting store=True did the trick.
Thanks so much to Niyas Raphy, you really did me a solid.