diff --git a/__init__.py b/__init__.py
index 88b5218..4e2285b 100644
--- a/__init__.py
+++ b/__init__.py
@@ -29,6 +29,7 @@ def register():
inventory.Inventory,
inventory.CreateInventoriesStart,
stock.WarehouseKardexStockStart,
+ product.ChangeUdmProductStart,
module='stock_co', type_='model')
Pool.register(
shipment.ShipmentDetailed,
@@ -45,6 +46,7 @@ def register():
shipment.ShipmentInForceDraft,
shipment.ShipmentInReturnForceDraft,
shipment.ShipmentInternalLoadStock,
+ product.ChangeUdmProduct,
module='stock_co', type_='wizard')
Pool.register(
stock.MoveByProduct,
diff --git a/product.py b/product.py
index ead365e..e1d7efd 100644
--- a/product.py
+++ b/product.py
@@ -128,6 +128,75 @@ class AverageCost(ModelSQL, ModelView):
cls._order.insert(0, ('effective_date', 'DESC'))
+class ChangeUdmProductStart(ModelView):
+ 'Change Udm Product Start'
+
+ __name__ = 'stock_co.change_udm_product.start'
+
+ udm = fields.Many2One('product.uom', "UOM", required=True,
+ help="If change the uom in product, this update the uom in sales, purchases \b \
+ invoices and moves stock")
+
+class ChangeUdmProduct(Wizard):
+ 'Change Udm Product'
+
+ __name__ = 'stock_co.change_udm_product'
+
+ start = StateView('stock_co.change_udm_product.start',
+ 'stock_co.change_udm_product_view_form', [
+ Button('Cancel', 'end', 'tryton-cancel'),
+ Button('Accept', 'accept', 'tryton-ok', default=True),
+ ])
+ accept = StateTransition()
+
+ def transition_accept(self):
+ pool = Pool()
+ cursor = Transaction().connection.cursor()
+ Template = pool.get('product.template')
+ Sale = pool.get('sale.line')
+ Purchase = pool.get('purchase.line')
+ Invoice = pool.get('account.invoice.line')
+ Move = pool.get('stock.move')
+
+ sale = Sale.__table__()
+ purchase = Purchase.__table__()
+ invoice = Invoice.__table__()
+ move = Move.__table__()
+ template = Template.__table__()
+
+ product_template = Template(Transaction().context.get('active_id'))
+ product_id = product_template.products[0].id
+ uom_update = self.start.udm.id
+ sale_uom = product_template.sale_uom.id
+ purchase_uom = product_template.purchase_uom.id
+ default_uom = product_template.default_uom.id
+
+ cursor.execute(*sale.update(
+ [sale.unit], [uom_update],
+ where=(sale.unit == sale_uom) & (sale.product == product_id)))
+ cursor.execute(*purchase.update(
+ [purchase.unit], [uom_update],
+ where=(purchase.unit == purchase_uom) & (purchase.product == product_id)))
+ cursor.execute(*invoice.update(
+ [invoice.unit], [uom_update],
+ where=(invoice.unit == purchase_uom)
+ & (invoice.invoice_type == 'in')
+ & (invoice.product == product_id)))
+ cursor.execute(*invoice.update(
+ [invoice.unit], [uom_update],
+ where=(invoice.unit == sale_uom)
+ & (invoice.invoice_type == 'out')
+ & (invoice.product == product_id)))
+ cursor.execute(*move.update(
+ [move.uom], [uom_update],
+ where=(move.uom == default_uom) & (move.product == product_id)))
+ cursor.execute(*template.update(
+ [template.default_uom, template.purchase_uom, template.sale_uom],
+ [uom_update, uom_update, uom_update],
+ where=(template.id == product_template.id)))
+
+ return 'end'
+
# class UpdateAverageCosts(Wizard):
# 'Update Average Costs'
# __name__ = 'stock_co.update_average_costs'
diff --git a/product.xml b/product.xml
index bd5d97a..859d180 100644
--- a/product.xml
+++ b/product.xml
@@ -21,6 +21,22 @@ this repository contains the full copyright notices and license terms. -->