Search code examples
pythonpython-2.7odoo-8odoo

Write quantities to specific journal - Odoo v8


What I need is to automatically write some quantities to a specific journal, read'em and write them on debit or credit fields, depending on the situation.

But, the whole point is, that I need to do this:

1.- Without creating invoices

2.- Depending on the state of class/table

3.- Without adding the function to any field, so as soon as it changes state, the quantites are written into the journal.

Ive tried this method, but I think this doesn't works because it should be on a computed Float or Integer field:

@api.multi
@api.depends('order_picking', 'order_picking.isbn', 'contract_worksheet')
def accounting_scenarios(self):
    for record in self:
        if record.state == awaitingraw:
            record.isbn.printer_wip_account.debit = record.contract_worksheet.total_alles
        elif record.state == work_in_progress:
            record.transporter.transp_transit.debit = record.contract_worksheet.total_alles

So, it basically does nothing.

I need a behaviour like, when a purchase or sale is done, the debit or credit fieds are updated with the invoices generated. BUT in this case, the invoice isn't necessary, I just need to read, and copy some field (computed or not), and then "paste" this on the aforementioned fields of the selected journal.

I hope I've explained myself.

Any ideas?


Solution

  • If you need to perform some action each time a field changes, but you don't need to create a new field which calls a compute method (which uses @api.depends decorator), you should overwrite the ORM write method of the model the field belongs to. So, in your case I would do something like this:

    @api.multi
    def write(self, vals):
        res = super(YourModel, self).write(vals)
        if 'state' in vals:
            for record in self:
                contract_worksheet = record.contract_worksheet
                if record.state == awaitingraw:
                    printer_wip_account = record.isbn.printer_wip_account
                    res &= printer_wip_account.write({
                        'debit': sum(c.total_alles for c in contract_worksheet),
                    })
                elif record.state == work_in_progress:
                    transp_transit = record.transporter.transp_transit
                    res &= transp_transit.write({
                        'debit': sum(c.total_alles for c in contract_worksheet),
                    })
        return res
    

    The above code may not be accurate, because I don't know the type of all the fields you're using there, but, you can't take the value of One2many field directly, you need to loop it to get the values you want. I'm supposing the field total_alles is a computed Float, and each contract_worksheet record has a different value for it, so you have to sum them to get a only float to write in debit value.