[IMP] Cancelling of procurements related to purchase orders should check state/UoM of the purchase order line + optim action_cancel
[IMP] Add tests [IMP] Clean up
This commit is contained in:
parent
c68ecbe093
commit
c12f4efc53
|
@ -1052,16 +1052,17 @@ class purchase_order_line(osv.osv):
|
||||||
|
|
||||||
def action_cancel(self, cr, uid, ids, context=None):
|
def action_cancel(self, cr, uid, ids, context=None):
|
||||||
self.write(cr, uid, ids, {'state': 'cancel'}, context=context)
|
self.write(cr, uid, ids, {'state': 'cancel'}, context=context)
|
||||||
for po_line in self.browse(cr, uid, ids, context=context):
|
# We will group by PO first, so we do the check only once for each PO
|
||||||
if all([l.state == 'cancel' for l in po_line.order_id.order_line]):
|
purchase_orders = list(set([x.order_id for x in self.browse(cr, uid, ids, context=context)]))
|
||||||
self.pool.get('purchase.order').action_cancel(cr, uid, [po_line.order_id.id], context=context)
|
for purchase in purchase_orders:
|
||||||
|
if all([l.state == 'cancel' for l in purchase.order_line]):
|
||||||
|
self.pool.get('purchase.order').action_cancel(cr, uid, [purchase.id], context=context)
|
||||||
|
|
||||||
def _check_product_uom_group(self, cr, uid, context=None):
|
def _check_product_uom_group(self, cr, uid, context=None):
|
||||||
group_uom = self.pool.get('ir.model.data').get_object(cr, uid, 'product', 'group_uom')
|
group_uom = self.pool.get('ir.model.data').get_object(cr, uid, 'product', 'group_uom')
|
||||||
res = [user for user in group_uom.users if user.id == uid]
|
res = [user for user in group_uom.users if user.id == uid]
|
||||||
return len(res) and True or False
|
return len(res) and True or False
|
||||||
|
|
||||||
|
|
||||||
def onchange_product_id(self, cr, uid, ids, pricelist_id, product_id, qty, uom_id,
|
def onchange_product_id(self, cr, uid, ids, pricelist_id, product_id, qty, uom_id,
|
||||||
partner_id, date_order=False, fiscal_position_id=False, date_planned=False,
|
partner_id, date_order=False, fiscal_position_id=False, date_planned=False,
|
||||||
name=False, price_unit=False, state='draft', context=None):
|
name=False, price_unit=False, state='draft', context=None):
|
||||||
|
@ -1179,9 +1180,15 @@ class procurement_order(osv.osv):
|
||||||
|
|
||||||
def propagate_cancel(self, cr, uid, procurement, context=None):
|
def propagate_cancel(self, cr, uid, procurement, context=None):
|
||||||
if procurement.rule_id.action == 'buy' and procurement.purchase_line_id:
|
if procurement.rule_id.action == 'buy' and procurement.purchase_line_id:
|
||||||
|
uom_obj = self.pool.get("product.uom")
|
||||||
purchase_line_obj = self.pool.get('purchase.order.line')
|
purchase_line_obj = self.pool.get('purchase.order.line')
|
||||||
if procurement.purchase_line_id.product_qty > procurement.product_qty and procurement.purchase_line_id.order_id.state == 'draft':
|
uom = procurement.purchase_line_id.product_uom
|
||||||
purchase_line_obj.write(cr, uid, [procurement.purchase_line_id.id], {'product_qty': procurement.purchase_line_id.product_qty - procurement.product_qty}, context=context)
|
product_qty = uom_obj._compute_qty_obj(cr, uid, procurement.product_uom, procurement.product_qty, uom, context=context)
|
||||||
|
if procurement.purchase_line_id.state not in ('draft', 'cancel'):
|
||||||
|
raise osv.except_osv(_('Error!'),
|
||||||
|
_('Can not cancel this procurement as the related purchase order has been confirmed already. Please cancel the purchase order first. '))
|
||||||
|
if float_compare(procurement.purchase_line_id.product_qty, product_qty, 0, precision_rounding=uom.rounding) > 0:
|
||||||
|
purchase_line_obj.write(cr, uid, [procurement.purchase_line_id.id], {'product_qty': procurement.purchase_line_id.product_qty - product_qty}, context=context)
|
||||||
else:
|
else:
|
||||||
purchase_line_obj.action_cancel(cr, uid, [procurement.purchase_line_id.id], context=context)
|
purchase_line_obj.action_cancel(cr, uid, [procurement.purchase_line_id.id], context=context)
|
||||||
return super(procurement_order, self).propagate_cancel(cr, uid, procurement, context=context)
|
return super(procurement_order, self).propagate_cancel(cr, uid, procurement, context=context)
|
||||||
|
|
|
@ -20,14 +20,14 @@
|
||||||
min_qty: 2.0
|
min_qty: 2.0
|
||||||
qty: 10.0
|
qty: 10.0
|
||||||
-
|
-
|
||||||
Set routes on product to be MTO adn Buy
|
Set routes on product to be MTO and Buy
|
||||||
-
|
-
|
||||||
!python {model: product.product}: |
|
!python {model: product.product}: |
|
||||||
route_warehouse0_buy = self.pool.get('stock.warehouse').browse(cr, uid, ref('stock.warehouse0')).buy_pull_id.route_id.id
|
route_warehouse0_buy = self.pool.get('stock.warehouse').browse(cr, uid, ref('stock.warehouse0')).buy_pull_id.route_id.id
|
||||||
route_warehouse0_mto = self.pool.get('stock.warehouse').browse(cr, uid, ref('stock.warehouse0')).mto_pull_id.route_id.id
|
route_warehouse0_mto = self.pool.get('stock.warehouse').browse(cr, uid, ref('stock.warehouse0')).mto_pull_id.route_id.id
|
||||||
self.write(cr, uid, ref('product_mto'), { 'route_ids': [(6, 0, [route_warehouse0_mto,route_warehouse0_buy])]}, context=context )
|
self.write(cr, uid, ref('product_mto'), { 'route_ids': [(6, 0, [route_warehouse0_mto,route_warehouse0_buy])]}, context=context )
|
||||||
-
|
-
|
||||||
Create a sales order with a line of 5 "My Product".
|
Create a sales order with a line of 5 Units "My Product".
|
||||||
-
|
-
|
||||||
!record {model: sale.order, id: sale_order_product_mto}:
|
!record {model: sale.order, id: sale_order_product_mto}:
|
||||||
partner_id: base.res_partner_3
|
partner_id: base.res_partner_3
|
||||||
|
@ -40,6 +40,21 @@
|
||||||
Confirm the sale order
|
Confirm the sale order
|
||||||
-
|
-
|
||||||
!workflow {model: sale.order, action: order_confirm, ref: sale_order_product_mto}
|
!workflow {model: sale.order, action: order_confirm, ref: sale_order_product_mto}
|
||||||
|
-
|
||||||
|
Create another sales order with 2 Dozen of the same product
|
||||||
|
-
|
||||||
|
!record {model: sale.order, id: sale_order_product_mto2}:
|
||||||
|
partner_id: base.res_partner_4
|
||||||
|
note: Create Sales order
|
||||||
|
warehouse_id: wh_pps
|
||||||
|
order_line:
|
||||||
|
- product_id: product_mto
|
||||||
|
product_uom_qty: 2.00
|
||||||
|
product_uom: product.product_uom_dozen
|
||||||
|
-
|
||||||
|
Confirm the sale order
|
||||||
|
-
|
||||||
|
!workflow {model: sale.order, action: order_confirm, ref: sale_order_product_mto2}
|
||||||
-
|
-
|
||||||
I run scheduler.
|
I run scheduler.
|
||||||
-
|
-
|
||||||
|
@ -62,11 +77,21 @@
|
||||||
for procu in self.browse(cr,uid,procu_ids,context=context):
|
for procu in self.browse(cr,uid,procu_ids,context=context):
|
||||||
assert procu.state == u'running', 'Procurement with id: %d should be running but is with state : %s!' %(procu.id, procu.state)
|
assert procu.state == u'running', 'Procurement with id: %d should be running but is with state : %s!' %(procu.id, procu.state)
|
||||||
|
|
||||||
# Check that one or more Purchase order
|
# Check that one purchase order has been created
|
||||||
purchase_ids = [proc.purchase_line_id.order_id for proc in self.browse(cr, uid, procu_ids) if proc.purchase_line_id]
|
purchase_ids = [proc.purchase_line_id.order_id for proc in self.browse(cr, uid, procu_ids) if proc.purchase_line_id]
|
||||||
assert len(purchase_ids) > 0, 'No purchase order found !'
|
assert len(purchase_ids) == 1, 'No purchase order found !'
|
||||||
|
|
||||||
# Cancel the main procurement
|
# Check the total quantity on the purchase order which should be in Unit(s)
|
||||||
|
purchase_line = purchase_ids[0].order_line[0]
|
||||||
|
assert purchase_line.product_qty == 29.0, 'As we ordered 2 Dozen(s) and 5 Unit(s), total should be 29 Unit(s) and not %s' % (purchase_line.product_qty,)
|
||||||
|
|
||||||
|
# Let us cancel the procurement related to the 2nd sales order first and check that the 2 Dozen(s) are subtracted correctly
|
||||||
|
so2 = self.pool.get('sale.order').browse(cr, uid, ref('sale_order_product_mto2'))
|
||||||
|
self.cancel(cr, uid, [so2.order_line[0].procurement_ids[0].id], context=context)
|
||||||
|
assert so2.order_line[0].procurement_ids[0].state == u'cancel', 'Main procurement should be cancelled !'
|
||||||
|
assert purchase_line.product_qty == 5.0, 'As we ordered 2 Dozen(s) and 5 Unit(s) and cancelled the two Dozen, total should be 5 Unit(s) and not %s' % (purchase_line.product_qty,)
|
||||||
|
|
||||||
|
# Cancel the main procurement from the sale order in units
|
||||||
main_procu_id = self.search(cr, uid, [('origin', '=', so.name)])
|
main_procu_id = self.search(cr, uid, [('origin', '=', so.name)])
|
||||||
assert len(main_procu_id) == 1, 'Main procurement not identified !'
|
assert len(main_procu_id) == 1, 'Main procurement not identified !'
|
||||||
self.cancel(cr, uid, main_procu_id, context=context)
|
self.cancel(cr, uid, main_procu_id, context=context)
|
||||||
|
@ -76,6 +101,6 @@
|
||||||
for procu in self.browse(cr, uid, procu_ids, context=context):
|
for procu in self.browse(cr, uid, procu_ids, context=context):
|
||||||
assert procu.state == u'cancel', 'Procurement %d should be cancelled but is with a state : %s!' %(procu.id, procu.state)
|
assert procu.state == u'cancel', 'Procurement %d should be cancelled but is with a state : %s!' %(procu.id, procu.state)
|
||||||
|
|
||||||
# Check that the purchase order is well cancelled
|
# Check that the purchase order is cancelled
|
||||||
for po in self.pool.get('purchase.order').browse(cr, uid, [po.id for po in purchase_ids], context=context):
|
for po in self.pool.get('purchase.order').browse(cr, uid, [po.id for po in purchase_ids], context=context):
|
||||||
assert po.state == u'cancel', 'Purchase order %d should be cancelled but is in state : %s!' %(po.id, po.state)
|
assert po.state == u'cancel', 'Purchase order %d should be cancelled but is in state : %s!' %(po.id, po.state)
|
||||||
|
|
Loading…
Reference in New Issue