From 7a4a650a89b464b4bd3acda721d807b9f313fe68 Mon Sep 17 00:00:00 2001 From: Leonardo Pistone Date: Fri, 20 Mar 2015 13:59:13 +0100 Subject: [PATCH 1/4] fix crash: excepted singleton --- stock_reserve/model/product.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stock_reserve/model/product.py b/stock_reserve/model/product.py index fd9ea385b..5256f3d16 100644 --- a/stock_reserve/model/product.py +++ b/stock_reserve/model/product.py @@ -60,7 +60,7 @@ class ProductProduct(models.Model): compute='_reservation_count', string='# Sales') - @api.multi + @api.one def _reservation_count(self): StockReservation = self.env['stock.reservation'] product_id = self._ids[0] From 8739885586af6b1b36781c27249d1a60e405b2f4 Mon Sep 17 00:00:00 2001 From: Leonardo Pistone Date: Mon, 30 Mar 2015 12:23:58 +0200 Subject: [PATCH 2/4] fix reservation_count computation 1. Make it float, because quantity is float. 2. Respect unit of measure (instead of adding apples and pears). 3. Do that for both product and template. 4. Test the above. --- stock_reserve/model/product.py | 8 ++++---- stock_reserve/test/stock_reserve.yml | 7 +++++++ 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/stock_reserve/model/product.py b/stock_reserve/model/product.py index 5256f3d16..46e394e57 100644 --- a/stock_reserve/model/product.py +++ b/stock_reserve/model/product.py @@ -25,7 +25,7 @@ from openerp import models, fields, api class ProductTemplate(models.Model): _inherit = 'product.template' - reservation_count = fields.Integer( + reservation_count = fields.Float( compute='_reservation_count', string='# Sales') @@ -36,7 +36,7 @@ class ProductTemplate(models.Model): domain = [('product_id', 'in', product_ids), ('state', 'in', ['draft', 'assigned'])] reservations = StockReservation.search(domain) - self.reservation_count = sum(reserv.product_uom_qty + self.reservation_count = sum(reserv.product_qty for reserv in reservations) @api.multi @@ -56,7 +56,7 @@ class ProductTemplate(models.Model): class ProductProduct(models.Model): _inherit = 'product.product' - reservation_count = fields.Integer( + reservation_count = fields.Float( compute='_reservation_count', string='# Sales') @@ -67,7 +67,7 @@ class ProductProduct(models.Model): domain = [('product_id', '=', product_id), ('state', 'in', ['draft', 'assigned'])] reservations = StockReservation.search(domain) - self.reservation_count = sum(reserv.product_uom_qty + self.reservation_count = sum(reserv.product_qty for reserv in reservations) @api.multi diff --git a/stock_reserve/test/stock_reserve.yml b/stock_reserve/test/stock_reserve.yml index 66b6f412e..0a4e58adb 100644 --- a/stock_reserve/test/stock_reserve.yml +++ b/stock_reserve/test/stock_reserve.yml @@ -62,6 +62,13 @@ - !python {model: stock.reservation}: | self.reserve(cr, uid, [ref('reserv_sorbet2')], context=context) +- + I check the reserved amount of the product and the template +- + !python {model: product.product, id: product_sorbet}: | + from nose.tools import * + assert_almost_equal(6.5, self.reservation_count) + assert_almost_equal(6.5, self.product_tmpl_id.reservation_count) - Then the reservation should be assigned and have reserved a quant - From 3411e32f576432d578c887014e16fbb146c098c7 Mon Sep 17 00:00:00 2001 From: Leonardo Pistone Date: Mon, 30 Mar 2015 12:31:05 +0200 Subject: [PATCH 3/4] refactor: delegate to variants, use self.id --- stock_reserve/model/product.py | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/stock_reserve/model/product.py b/stock_reserve/model/product.py index 46e394e57..1f6c42333 100644 --- a/stock_reserve/model/product.py +++ b/stock_reserve/model/product.py @@ -29,15 +29,10 @@ class ProductTemplate(models.Model): compute='_reservation_count', string='# Sales') - @api.multi + @api.one def _reservation_count(self): - StockReservation = self.env['stock.reservation'] - product_ids = self._get_products() - domain = [('product_id', 'in', product_ids), - ('state', 'in', ['draft', 'assigned'])] - reservations = StockReservation.search(domain) - self.reservation_count = sum(reserv.product_qty - for reserv in reservations) + self.reservation_count = sum(variant.reservation_count + for variant in self.product_variant_ids) @api.multi def action_view_reservations(self): @@ -62,11 +57,9 @@ class ProductProduct(models.Model): @api.one def _reservation_count(self): - StockReservation = self.env['stock.reservation'] - product_id = self._ids[0] - domain = [('product_id', '=', product_id), + domain = [('product_id', '=', self.id), ('state', 'in', ['draft', 'assigned'])] - reservations = StockReservation.search(domain) + reservations = self.env['stock.reservation'].search(domain) self.reservation_count = sum(reserv.product_qty for reserv in reservations) From 67d88d07015a31857cab18b3aac2dd34f1777d6d Mon Sep 17 00:00:00 2001 From: Leonardo Pistone Date: Mon, 30 Mar 2015 12:31:46 +0200 Subject: [PATCH 4/4] refactor tests, improve failures --- stock_reserve/test/stock_reserve.yml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/stock_reserve/test/stock_reserve.yml b/stock_reserve/test/stock_reserve.yml index 0a4e58adb..369b1563f 100644 --- a/stock_reserve/test/stock_reserve.yml +++ b/stock_reserve/test/stock_reserve.yml @@ -60,8 +60,8 @@ - I confirm the reservation - - !python {model: stock.reservation}: | - self.reserve(cr, uid, [ref('reserv_sorbet2')], context=context) + !python {model: stock.reservation, id: reserv_sorbet2}: | + self.reserve() - I check the reserved amount of the product and the template - @@ -80,9 +80,9 @@ - I check Virtual stock of Sorbet after update reservation - - !python {model: product.product}: | - product = self.browse(cr, uid, ref('stock_reserve.product_sorbet'), context=context) - assert product.virtual_available == 3.5, "Stock is not updated." + !python {model: product.product, id: product_sorbet}: | + from nose.tools import * + assert_almost_equal(3.5, self.virtual_available) - I run the scheduler -