Skip to content
Permalink
Browse files

refactoring wizard

  • Loading branch information...
jarobase committed Oct 25, 2018
1 parent d1ba043 commit 66f70a19255a66ee659fac429315276dc7991080
Showing with 42 additions and 105 deletions.
  1. +42 −105 merge_purchase_order/wizard/merge_puchase_order_wizard.py
@@ -10,13 +10,13 @@ class MergePurchaseOrder(models.TransientModel):
merge_type = \
fields.Selection([
('new_cancel',
'Create new order and cancel all selected purchase orders'),
'Create new order and cancel all selected purchase orders'),
('new_delete',
'Create new order and delete all selected purchase orders'),
('merge_cancel',
'Merge order on existing selected order and cancel others'),
('merge_delete',
'Merge order on existing selected order and delete others')],
'Merge order on existing selected order and delete others')],
default='new_cancel')
purchase_order_id = fields.Many2one('purchase.order', 'Purchase Order')

@@ -30,20 +30,20 @@ def onchange_merge_type(self):
self._context.get('active_ids', []))
res['domain'] = {
'purchase_order_id':
[('id', 'in',
[purchase.id for purchase in purchase_orders])]
[('id', 'in',
[purchase.id for purchase in purchase_orders])]
}
return res

@api.multi
def merge_orders(self):
purchase_orders = self.env['purchase.order'].browse(
self._context.get('active_ids', []))
existing_po_line = False
# existing_po_line = False
if len(self._context.get('active_ids', [])) < 2:
raise UserError(
_('Please select atleast two purchase orders to perform '
'the Merge Operation.'))
'the Merge Operation.'))
if any(order.state != 'draft' for order in purchase_orders):
raise UserError(
_('Please select Purchase orders which are in RFQ state '
@@ -52,110 +52,47 @@ def merge_orders(self):
if any(order.partner_id.id != partner for order in purchase_orders):
raise UserError(
_('Please select Purchase orders whose Vendors are same to '
' perform the Merge Operation.'))
if self.merge_type == 'new_cancel':
' perform the Merge Operation.'))
# Update lines depending on the merge_type
if self.merge_type == 'new_cancel' or self.merge_type == 'new_delete':
po = self.env['purchase.order'].with_context({
'trigger_onchange': True,
'onchange_fields_to_trigger': [partner]
}).create({'partner_id': partner})
default = {'order_id': po.id}
for order in purchase_orders:
for line in order.order_line:
if po.order_line:
for poline in po.order_line:
if line.product_id == poline.product_id and\
line.price_unit == poline.price_unit:
existing_po_line = poline
break
if existing_po_line:
existing_po_line.product_qty += line.product_qty
po_taxes = [
tax.id for tax in existing_po_line.taxes_id]
[po_taxes.append((tax.id))
for tax in line.taxes_id]
existing_po_line.taxes_id = \
[(6, 0, po_taxes)]
else:
line.copy(default=default)
for order in purchase_orders:


# 'merge_cancel': or other cases
else:
po = self.purchase_order_id
default = {'order_id': self.purchase_order_id.id}

existing_po_line = False
for order in purchase_orders:
if order == po and self.merge_type != 'new_cancel' and self.merge_type != 'new_delete':
continue
for line in order.order_line:
if po.order_line:
for poline in po.order_line:
if line.product_id == poline.product_id and line.price_unit == poline.price_unit:
existing_po_line = poline
break
if existing_po_line:
existing_po_line.product_qty += line.product_qty
po_taxes = [tax.id for tax in existing_po_line.taxes_id]
# Fixme needs to be in [] ?
[po_taxes.append(tax.id) for tax in line.taxes_id]
existing_po_line.taxes_id = [(6, 0, po_taxes)]
else:
line.copy(default=default)

for order in purchase_orders:
if self.merge_type == 'new_cancel':
order.button_cancel()
elif self.merge_type == 'new_delete':
po = self.env['purchase.order'].with_context({
'trigger_onchange': True,
'onchange_fields_to_trigger': [partner]
}).create({'partner_id': partner})
default = {'order_id': po.id}
for order in purchase_orders:
for line in order.order_line:
if po.order_line:
for po_line in po.order_line:
if line.product_id == po_line.product_id and \
line.price_unit == po_line.price_unit:
existing_po_line = po_line
break
if existing_po_line:
existing_po_line.product_qty += line.product_qty
po_taxes = [
tax.id for tax in existing_po_line.taxes_id]
[po_taxes.append((tax.id))
for tax in line.taxes_id]
existing_po_line.taxes_id = \
[(6, 0, po_taxes)]
else:
line.copy(default=default)
for order in purchase_orders:
if self.merge_type == 'merge_cancel':
order.sudo().button_cancel()

# 'new_delete': or other cases
else:
order.sudo().button_cancel()
order.sudo().unlink()
elif self.merge_type == 'merge_cancel':
default = {'order_id': self.purchase_order_id.id}
po = self.purchase_order_id
for order in purchase_orders:
if order == po:
continue
for line in order.order_line:
if po.order_line:
for po_line in po.order_line:
if line.product_id == po_line.product_id and \
line.price_unit == po_line.price_unit:
existing_po_line = po_line
break
if existing_po_line:
existing_po_line.product_qty += line.product_qty
po_taxes = [
tax.id for tax in existing_po_line.taxes_id]
[po_taxes.append((tax.id))
for tax in line.taxes_id]
existing_po_line.taxes_id = \
[(6, 0, po_taxes)]
else:
line.copy(default=default)
for order in purchase_orders:
if order != po:
order.sudo().button_cancel()
else:
default = {'order_id': self.purchase_order_id.id}
po = self.purchase_order_id
for order in purchase_orders:
if order == po:
continue
for line in order.order_line:
if po.order_line:
for po_line in po.order_line:
if line.product_id == po_line.product_id and \
line.price_unit == po_line.price_unit:
existing_po_line = po_line
break
if existing_po_line:
existing_po_line.product_qty += line.product_qty
po_taxes = [
tax.id for tax in existing_po_line.taxes_id]
[po_taxes.append((tax.id))
for tax in line.taxes_id]
existing_po_line.taxes_id = \
[(6, 0, po_taxes)]
else:
line.copy(default=default)
for order in purchase_orders:
if order != po:
order.sudo().button_cancel()
order.sudo().unlink()

0 comments on commit 66f70a1

Please sign in to comment.
You can’t perform that action at this time.