~unifield-team/unifield-wm/us-826

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
-
  Create a Sale Order
-
  !record {model: sale.order, id: so_H}:
    company_id: base.main_company
    date_order: '2011-04-13'
    invoice_quantity: order
    order_policy: manual
    partner_id: partner_A
    partner_invoice_id: address_A
    partner_order_id: address_A
    partner_shipping_id: address_A
    picking_policy: direct
    pricelist_id: product.list0
    shop_id: sale.shop
    priority: normal
    categ: medical
-
  Create a Sale Order Line
-
  !record {model: sale.order.line, id: sol_H1}:
    product_uom: product.product_uom_unit
    product_id: product_A
    order_id: so_H
    price_unit: 1
    product_uom_qty: 10
    date_planned: '2011-04-13'
    type: make_to_order
-
  !record {model: sale.order.line, id: sol_H2}:
    product_uom: product.product_uom_unit
    product_id: product_B
    order_id: so_H
    price_unit: 1
    product_uom_qty: 5
    date_planned: '2011-04-13'
    type: make_to_order
-
  I validate the sale order
-
  !python {model: sale.order}: |
    import netsvc
    wf_service = netsvc.LocalService("workflow")
    wf_service.trg_validate(uid, 'sale.order', ref("so_H"), 'order_confirm', cr)
-
  Run the scheduler
-
  !python {model: procurement.order}: |
    self.run_scheduler(cr, uid)
-
  Validate the purchase order
-
  !python {model: purchase.order}: |
    proc_obj = self.pool.get('procurement.order')
    sol_obj = self.pool.get('sale.order.line')
    # get the corresponding procurement order
    data = sol_obj.read(cr, uid, [ref("sol_H1")], ['procurement_id'], context=context)[0]
    proc_id = data['procurement_id'][0]
    data = proc_obj.read(cr, uid, [proc_id], ['purchase_id', 'state', 'note'], context=context)[0]
    po_id = data['purchase_id'][0]
    # change Invoicing control to From Picking
    self.write(cr, uid, [po_id], {'invoice_method':'picking'}, context=context)
    import netsvc
    wf_service = netsvc.LocalService("workflow")
    wf_service.trg_validate(uid, 'purchase.order', po_id, 'purchase_confirm', cr)
    wf_service.trg_validate(uid, 'purchase.order', po_id, 'purchase_approve', cr)
-
  Process the incoming shipment - reduce both qty - a back order is created
-
  !python {model: stock.picking}: |
    proc_obj = self.pool.get('procurement.order')
    sol_obj = self.pool.get('sale.order.line')
    po_obj = self.pool.get('purchase.order')
    # get the corresponding procurement order
    data = sol_obj.read(cr, uid, [ref("sol_H1")], ['procurement_id'], context=context)[0]
    proc_id = data['procurement_id'][0]
    data = proc_obj.read(cr, uid, [proc_id], ['purchase_id', 'state', 'note'], context=context)[0]
    po_id = data['purchase_id'][0]
    # find the IN
    in_ids = self.search(cr, uid, [('purchase_id', '=', po_id)], context=context)
    pick = self.browse(cr, uid, in_ids[0], context=context)
    # get the weird openERP picking type
    picking_type = self.pool.get('stock.partial.picking').get_picking_type(cr, uid, pick, context=context)
    # Convert to OUT to Simple OUT
    out_ids = self.search(cr, uid, [('sale_id', '=', ref("so_H"))], context=context)
    for out_id in out_ids:
      self.convert_to_standard(cr, uid, [out_id], context=context)
    # process wizard
    dic = self.action_process(cr, uid, in_ids, context=context)
    wiz_model = dic['res_model']
    wiz_id = dic['res_id']
    wiz_c = dic['context']
    # reduce qty and then split moves
    wiz_obj = self.pool.get(wiz_model)
    for obj in wiz_obj.browse(cr, uid, [wiz_id], context=wiz_c):
      for out in getattr(obj, 'product_moves_%s'%picking_type): # -> because is now IN with average product cost method !!!
        # change the product of original move
        dic_change = out.change_product(context=wiz_c)
        change_model = dic_change['res_model']
        change_ids = [dic_change['res_id']]
        change_c = dic_change['context']
        change_obj = self.pool.get(change_model)
        # product A/B -> C
        change_obj.write(cr, uid, change_ids, {'new_product_id': ref("product_C"), 'change_reason': 'yml test'}, context=change_c)
        change_obj.change_product(cr, uid, change_ids, context=change_c)
        # call the split wizard
        dic_split = out.split(context=wiz_c)
        split_model = dic_split['res_model']
        split_ids = [dic_split['res_id']]
        split_c = dic_split['context']
        split_obj = self.pool.get(split_model)
        # depending on the qty, we put different quantity in the new move
        if out.quantity_ordered == 10:
          # reduce qty
          out.write({'quantity': 7, 'quantity_ordered': 7}, context=wiz_c)
          split_obj.write(cr, uid, split_ids, {'quantity': 5}, context=split_c)
        if out.quantity_ordered == 5:
          # reduce qty
          out.write({'quantity': 4, 'quantity_ordered': 4}, context=wiz_c)
          split_obj.write(cr, uid, split_ids, {'quantity': 3}, context=split_c)
        # we perform the split
        split_obj.split(cr, uid, split_ids, context=split_c)
        
    for obj in wiz_obj.browse(cr, uid, [wiz_id], context=wiz_c): # we have to loop again from wiz to load the updated values for out moves
      for out in getattr(obj, 'product_moves_%s'%picking_type): # -> because is now IN with average product cost method !!!
        # change the product of splitted move
        dic_change = out.change_product(context=wiz_c)
        change_model = dic_change['res_model']
        change_ids = [dic_change['res_id']]
        change_c = dic_change['context']
        change_obj = self.pool.get(change_model)
        if out.quantity_ordered == 5 or out.quantity_ordered == 3:
          # product C -> D for original A of qty 5 and original B of qty 3
          change_obj.write(cr, uid, change_ids, {'new_product_id': ref("product_D"), 'change_reason': 'yml test'}, context=change_c)
          change_obj.change_product(cr, uid, change_ids, context=change_c)
    wiz_obj.copy_all(cr,uid,[dic['res_id']], context=dic['context'])
    wiz_obj.do_incoming_shipment(cr, uid, [dic['res_id']], context=dic['context'])
    # openERP swap IN -> backorder
    data = self.read(cr, uid, in_ids, ['backorder_id'], context=context)[0]
    back_ids = in_ids
    in_ids = [data['backorder_id'][0]]
    # assert a backorder has been created
    assert in_ids, 'backorder does not exist'
    data = self.read(cr, uid, in_ids, ['state'], context=context)[0]
    assert data['state'] == 'done', 'the incoming shipment is not Done - done - %s'%data['state']
    # assert the backorder
    data = self.read(cr, uid, back_ids, ['state'], context=context)[0]
    assert data['state'] == 'assigned', 'the backorder is not assigned - assigned - %s'%data['state']
    # check the moves in backorder
    a_qty = [3.0]
    b_qty = [1.0]
    for obj in self.browse(cr, uid, back_ids, context=context):
      for move in obj.move_lines:
        if move.product_id.id == ref("product_A"):
          a_qty.remove(move.product_qty)
        if move.product_id.id == ref("product_B"):
          b_qty.remove(move.product_qty)
    # all qty must be empty
    assert not a_qty, 'Quantity for product A is not empty - %s'%a_qty
    assert not b_qty, 'Quantity for product B is not empty - %s'%b_qty
    # check the moves in IN - products have changed !
    c_qty = [2.0, 1.0]
    d_qty = [5.0, 3.0]
    for obj in self.browse(cr, uid, in_ids, context=context):
      for move in obj.move_lines:
        if move.product_id.id == ref("product_C"):
          c_qty.remove(move.product_qty)
        if move.product_id.id == ref("product_D"):
          d_qty.remove(move.product_qty)
    # all qty must be empty
    assert not c_qty, 'Quantity for product C is not empty - %s'%c_qty
    assert not d_qty, 'Quantity for product D is not empty - %s'%d_qty
    # check the moves in OUT
    a_qty = [3.0]
    b_qty = [1.0]
    c_qty = [2.0, 1.0]
    d_qty = [5.0, 3.0]
    out_ids = self.search(cr, uid, [('sale_id', '=', ref("so_H"))], context=context)
    # only one OUT for the sale order
    assert len(out_ids) == 1, 'number of out objects is wrong - 1 - %s'%len(out_ids)
    for obj in self.browse(cr, uid, out_ids, context=context):
      for move in obj.move_lines:
        if move.product_id.id == ref("product_A"):
          a_qty.remove(move.product_qty)
        if move.product_id.id == ref("product_B"):
          b_qty.remove(move.product_qty)
        if move.product_id.id == ref("product_C"):
          c_qty.remove(move.product_qty)
        if move.product_id.id == ref("product_D"):
          d_qty.remove(move.product_qty)
    # all qty must be empty
    assert not a_qty, 'Quantity for product A is not empty - %s'%a_qty
    assert not b_qty, 'Quantity for product B is not empty - %s'%b_qty
    assert not c_qty, 'Quantity for product C is not empty - %s'%c_qty
    assert not d_qty, 'Quantity for product D is not empty - %s'%d_qty
    
    # cancel the backorder *NO UPDATE* - then check the out
    dic = self.enter_reason(cr, uid, back_ids, context=context)
    wiz_model = dic['res_model']
    wiz_id = dic['res_id']
    wiz_c = dic['context']
    # enter a reason
    wiz_obj = self.pool.get(wiz_model)
    for obj in wiz_obj.browse(cr, uid, [wiz_id], context=wiz_c):
      wiz_obj.write(cr, uid, [obj.id], {'change_reason': 'test yml'}, context=context)
      
    wiz_obj.do_cancel(cr, uid, [dic['res_id']], context=dic['context'])
    data = self.read(cr, uid, back_ids, ['state', 'purchase_id'], context=context)[0]
    assert data['state'] == 'cancel', 'the incoming shipment is not Canceled - cancel - %s'%data['state']
    # check the corresponding purchase state
    data = po_obj.read(cr, uid, [data['purchase_id'][0]], ['state'], context=context)[0]
    assert data['state'] == 'done', 'the corresponding po is not in shipping exception state - except_picking - %s'%data['state']
    # check the moves in OUT - back order contained 3 A and 1 B, should not have been decreased in OUT
    a_qty = [3.0]
    b_qty = [1.0]
    c_qty = [2.0, 1.0]
    d_qty = [5.0, 3.0]
    out_ids = self.search(cr, uid, [('sale_id', '=', ref("so_H"))], context=context)
    # only one OUT for the sale order
    assert len(out_ids) == 1, 'number of out objects is wrong - 1 - %s'%len(out_ids)
    for obj in self.browse(cr, uid, out_ids, context=context):
      for move in obj.move_lines:
        if move.product_id.id == ref("product_A"):
          a_qty.remove(move.product_qty)
        if move.product_id.id == ref("product_B"):
          b_qty.remove(move.product_qty)
        if move.product_id.id == ref("product_C"):
          c_qty.remove(move.product_qty)
        if move.product_id.id == ref("product_D"):
          d_qty.remove(move.product_qty)
    # all qty must be empty
    assert not a_qty, 'Quantity for product A is not empty - %s'%a_qty
    assert not b_qty, 'Quantity for product B is not empty - %s'%b_qty
    assert not c_qty, 'Quantity for product C is not empty - %s'%c_qty
    assert not d_qty, 'Quantity for product D is not empty - %s'%d_qty