跳至内容
菜单
此问题已终结
3 回复
4966 查看

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.

最佳答案

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

形象
丢弃
编写者 最佳答案

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

形象
丢弃
相关帖文 回复 查看 活动
2
6月 23
4382
1
4月 22
3130
2
6月 17
22761
1
10月 15
8742
2
3月 15
10291