1
votes

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?

1
Which type of fields are isbn, printer_wip_account, transporter, transp_transit, contract_worksheet and total_alles? - forvas
isbn = Many2one to produdct.product, printer_wip_account is a res.partner journal account, transporter is a res.partner, transp_transit, the transporter's account journnal, contract_worksheet is a One2many to another model (from which the calculations are going to be read), total_alles is one of those computed fields which belongs to contract_worksheet model - NeoVe
Idk if I should change the way on which I'm attacking this problem though - NeoVe
What should be done is read fields from One2many contract_worksheet, write the result into debit or credit fields, on a specific journal, depending on a state, but I'm complicating things maybe... - NeoVe
I think that a related field on a One2many cannot be called like a Many2one related one, ie: many2one.related, I think it should be one2many.many2onefromotherclass.related , maybe that's one of the issues - NeoVe

1 Answers

1
votes

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.