skilld-labs / go-odoo

Golang wrapper for Odoo API
Apache License 2.0
86 stars 62 forks source link

Use relational type (to create an an invoice) #47

Closed hoamer closed 7 months ago

hoamer commented 1 year ago

Odoo version: 15

Hello together :)

I am trying to figure out how to use the Relational type. My goal is to create an invoice. For this I create an "account.move". (https://github.com/skilld-labs/go-odoo/blob/master/account_move.go) In the most basic setup, I have an partner (PartnerId) and lineIds, the latter one is of type "Relation".

My questions -- 1 -- From the documentation

func NewRelation() *Relation {}
func (r *Relation) Get() []int64 {}

I guess I have to create a new relation and use "AddRecord", where I hand over the "account.move.line" to the expected interface. Is this correct?

-- 2 -- From the linked Odoo-documentation (https://www.odoo.com/documentation/13.0/reference/orm.html#odoo.models.Model.write) it seems I have to refer in the Relation-object to the "account.move.line" <-> "account.move". But how do I do this, before I created "account.move"?

-- 3 -- In the case I am totally wrong with my assumptions, could someone point me to the steps how to proceed, or give me an example?

I wish you all a great weekend!

Best regards Sebastian

ahuret commented 1 year ago

Hello @hoamer :wave: Sorry for the long delay, I just came back from vacations ! :sun_with_face: I hope you found a solution :)

So, to answer your questions:

Relation type refer to https://www.odoo.com/documentation/13.0/developer/reference/addons/orm.html#odoo.models.Model.write this section of documentation. As you can see there are 7 methods to interaction with relations:

// from documentation
(0, 0, values)
adds a new record created from the provided value dict.

(1, id, values)
updates an existing record of id id with the values in values. Can not be used in [create()](https://www.odoo.com/documentation/13.0/developer/reference/addons/orm.html#odoo.models.Model.create).

(2, id, 0)
removes the record of id id from the set, then deletes it (from the database). Can not be used in [create()](https://www.odoo.com/documentation/13.0/developer/reference/addons/orm.html#odoo.models.Model.create).

(3, id, 0)
removes the record of id id from the set, but does not delete it. Can not be used in [create()](https://www.odoo.com/documentation/13.0/developer/reference/addons/orm.html#odoo.models.Model.create).

(4, id, 0)
adds an existing record of id id to the set.

(5, 0, 0)
removes all records from the set, equivalent to using the command 3 on every record explicitly. Can not be used in [create()](https://www.odoo.com/documentation/13.0/developer/reference/addons/orm.html#odoo.models.Model.create).

(6, 0, ids)
replaces all existing records in the set by the ids list, equivalent to using the command 5 followed by a command 4 for each id in ids.

So it all depends on what you wanna do.

0 refers to AddNewRecord , meaning that you can directly set a AccountMoveLine inside, odoo will create it and assign it to the AccountMove object. 1 refers to UpdateRecord , it allow to modify an existing AccountMoveLine, by setting the corresponding id with new values. 2 and 3 refers to DeleteRecord and RemoveRecord , I don't think it's interesting for you use case. 4 refers to AddRecord and allow to add one existing AccountMoveLine the AccountMove object by setting its id. 5 refers to RemoveAllRecords and remove all associated AccountMoveLine records. 6 refers to ReplaceAllRecords and replace all associated AccountMoveLine records by the provided records.

It makes me think we could totally update a bit the model generator to add new methods to manipulate the relations. Something like

func (am *AccountMove) AddNewAccountMoveLine(aml *AccountMoveLine)
func (am *AccountMove) AddExistingAccountMoveLine(id int64)
func (am *AccountMove) RemoveAllAccountMoveLines()
func (am *AccountMove) UpdateExistingAccountMoveLine(id int64, updatedAml *AccountMoveLine)

for example ! Would be a nice contribution by the way, if you're interested :)

I hope I answered your question, feel free to ask more !

ahuret commented 7 months ago

closing it for now. feel free to re-open it if you have issues with it !