Sorry, this will be a long one.
I have the following problem when computing the commitment dates of the sale order lines of a sale order.
1. I open a sale order (let's say id=765) in the sale order view 2. The commitment dates of the sale order lines have to be computed regarding the previous 'open' sale orders and the open manufacturing orders to see at which date the products of the actual chosen sale order can be delivered.
I have tried several ways to achieve this, but every try failed.
So there is an open (viewed) sale order with one or several sale order lines.
Trial #1:
On calculation of a sale.order.line I tried to get the previous sale.orders by searching like that:
prev_saleorders = self.env['sale.order'].domain([('state', '=', 'progress'), ('date_order', '<', self.date_order)])
As result I get the correct recordset. Let's say sale.order(764, 763, 762)
BUT... when I go on with my calculation (some other modules are called like total_weight_reckoner on the chosen sale.order) the record set of my actual sale order doesn't consist of sale.order(765,) anymore but seems to have been merged with my search result. total_weight_reckoner throws a singleton errormessage because I now have sale.order(765, 764, 763, 762) instead of just sale.order(765,)
I can't find anything in the web or in my books that tells me, if a search on a model extends my recordset in self.
Trial #2:
I think I have a deep lack of understanding self, environment and context but I tried to find a solution around this and tried to create a new databank cursor with the following code:
@api.model
def getPreviousSaleOrders(self):
domain = [('state', '=', 'progress'), ('date_order', '<', self.date_order)]
new_cr = self.pool.cursor()
self = self.with_env(self.env())
output = self.env['sale.order'].search(domain)
self._cr.rollback()
self._cr.close()
return output
Without any real knowledge of the interna of the environment I tried this code, I found on the web.
Result: I got the correct recordset back and my original sale.order(765,) remained intact.
BUT... after that I got a "closed database cursor"-errormessage when doing further calculations. This time it was the line "self.env['stock.picking'].search([('origin','=',self.name)])".
I think I must have destroyed my "self" again.
Trial #3:
After that I tried to add a computed field to my sale.order-Model
previous_sale_orders = fields.Many2many('sale.order',string="Previous Saleorders",compute='_get_previous_sale_orders')
with:
@api.multi
def _get_previous_sale_orders(self):
for so in self:
domain = [('state', '=', 'progress'), ('date_order', '<', so.date_order)]
so.previous_sale_orders=self.env['sale.order'].search(domain)
Result: I get that singleton-error again like in #1
Can anyone please guide me to the right direction or can tell me what my error in reasoning is?
Actually I just have to get a list of the sale orders which dates are earlier than the actual chosen sale order.
Thanks in advance. I am in despair.
Alle the best,
Thorsten
EDIT: Oops... btw. Odoo 8