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
|
-
Create an Incoming Shipment
-
!record {model: stock.picking , id: in_B}:
company_id: base.main_company
invoice_state: none
name: InA
type: in
-
create the moves
-
!record {model: stock.move, id: move_B1}:
name: MoveA
picking_id: in_B
product_id: product_A
date_expected: 2011-07-11
date: 2011-07-10
product_qty: 10.0
type: in
product_uom: product.product_uom_unit
reason_type_id: reason_types_moves.reason_type_external_supply
location_dest_id: stock.stock_location_stock
location_id: stock.stock_location_suppliers
-
create the moves
-
!record {model: stock.move, id: move_B2}:
name: MoveB
picking_id: in_B
product_id: product_B
date_expected: 2011-07-11
date: 2011-07-10
product_qty: 5.0
type: in
product_uom: product.product_uom_unit
reason_type_id: reason_types_moves.reason_type_external_supply
location_dest_id: stock.stock_location_stock
location_id: stock.stock_location_suppliers
-
Process the incoming shipment - reduce both qty - a back order is created
-
!python {model: stock.picking}: |
po_obj = self.pool.get('purchase.order')
# find the IN
in_ids = [ref("in_B")]
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)
# process wizard
self.draft_force_assign(cr, uid, in_ids)
self.force_assign(cr, uid, in_ids)
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)
wiz_obj.copy_all(cr,uid,[dic['res_id']], context=dic['context'])
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
# cancel the backorder with out update - out does not exist
dic = self.enter_reason(cr, uid, back_ids, context=dict(context, cancel_type='update_out'))
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']
# the corresponding purchase order state does not need to be checked as no purchase order exists
# cancel with update tested without purchase order
|