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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
|
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2011 TeMPO Consulting, MSF
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from osv import osv, fields
from tools.translate import _
class sale_order_followup_test(osv.osv_memory):
_name = 'sale.order.followup.test'
_columns = {'name': fields.char(size=64, string='Name')}
def create_test(self, cr, uid, ids, context={}):
self.pool.get('product.category').create(cr, uid, {'name': 'test category'}, context=context)
return True
sale_order_followup_test()
class sale_order_followup(osv.osv_memory):
_name = 'sale.order.followup'
_description = 'Sales Order Followup'
def get_selection(self, cr, uid, o, field):
"""
"""
sel = self.pool.get(o._name).fields_get(cr, uid, [field])
res = dict(sel[field]['selection']).get(getattr(o,field),getattr(o,field))
name = '%s,%s' % (o._name, field)
tr_ids = self.pool.get('ir.translation').search(cr, uid, [('type', '=', 'selection'), ('name', '=', name),('src', '=', res)])
if tr_ids:
return self.pool.get('ir.translation').read(cr, uid, tr_ids, ['value'])[0]['value']
else:
return res
def _get_order_state(self, cr, uid, ids, field_name, args, context={}):
if not context:
context = {}
res = {}
for follow in self.browse(cr, uid, ids, context=context):
res[follow.id] = None
if follow.order_id:
res[follow.id] = self.get_selection(cr, uid, follow.order_id, 'state')
return res
_columns = {
'order_id': fields.many2one('sale.order', string='Internal reference', readonly=True),
'cust_ref': fields.related('order_id', 'client_order_ref', string='Customer reference', readonly=True, type='char'),
'creation_date': fields.related('order_id', 'create_date', string='Creation date', readonly=True, type='date'),
'state': fields.function(_get_order_state, method=True, type='char', string='Order state', readonly=True),
'requested_date': fields.related('order_id', 'delivery_requested_date', string='Requested date', readonly=True, type='date'),
'confirmed_date': fields.related('order_id', 'delivery_confirmed_date', string='Confirmed date', readonly=True, type='date'),
'line_ids': fields.one2many('sale.order.line.followup', 'followup_id', string='Lines', readonly=True),
'choose_type': fields.selection([('documents', 'Documents view'), ('progress', 'Progress view')], string='Type of view'),
}
_defaults = {
'choose_type': lambda *a: 'progress',
}
def go_to_view(self, cr, uid, ids, context={}):
'''
Launches the correct view according to the user's choice
'''
for followup in self.browse(cr, uid, ids, context=context):
if followup.choose_type == 'documents':
view_id = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'sales_followup', 'sale_order_followup_document_view')[1]
else:
view_id = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'sales_followup', 'sale_order_followup_progress_view')[1]
return {'type': 'ir.actions.act_window',
'res_model': 'sale.order.followup',
'res_id': followup.id,
'view_id': [view_id],
'view_type': 'form',
'view_mode': 'form',
'target': 'dummy'}
def switch_documents(self, cr, uid, ids, context={}):
'''
Switch to documents view
'''
self.write(cr, uid, ids, {'choose_type': 'documents'})
return self.go_to_view(cr, uid, ids, context=context)
def switch_progress(self, cr, uid, ids, context={}):
'''
Switch to progress view
'''
self.write(cr, uid, ids, {'choose_type': 'progress'})
return self.go_to_view(cr, uid, ids, context=context)
def update_followup(self, cr, uid, ids, context={}):
'''
Updates data in followup view
'''
new_context = context.copy()
# Get information of the old followup before deletion
for followup in self.browse(cr, uid, ids, context=new_context):
new_context['active_ids'] = [followup.order_id.id]
new_context['view_type'] = followup.choose_type
# Get the id of the new followup object
result = self.start_order_followup(cr, uid, ids, context=new_context).get('res_id')
if not result:
raise osv.except_osv(_('Error'), _('No followup found ! Cannot update !'))
else:
# Remove the old followup object and all his lines (on delete cascade)
self.unlink(cr, uid, ids, context=new_context)
# Returns the same view as before
if new_context.get('view_type') == 'documents':
return self.switch_documents(cr, uid, [result], context=new_context)
else:
return self.switch_progress(cr, uid, [result], context=new_context)
def start_order_followup(self, cr, uid, ids, context={}):
'''
Creates and display a followup object
'''
order_obj = self.pool.get('sale.order')
line_obj = self.pool.get('sale.order.line.followup')
# openERP BUG ?
ids = context.get('active_ids',[])
if not ids:
raise osv.except_osv(_('Error'), _('No order found !'))
if len(ids) != 1:
raise osv.except_osv(_('Error'), _('You should select one order to follow !'))
followup_id = False
for o in order_obj.browse(cr, uid, ids, context=context):
followup_id = self.create(cr, uid, {'order_id': o.id})
for line in o.order_line:
purchase_ids = self.get_purchase_ids(cr, uid, line.id, context=context)
purchase_line_ids = self.get_purchase_line_ids(cr, uid, line.id, purchase_ids, context=context)
incoming_ids = self.get_incoming_ids(cr, uid, line.id, purchase_ids, context=context)
outgoing_ids = self.get_outgoing_ids(cr, uid, line.id, context=context)
tender_ids = self.get_tender_ids(cr, uid, line.id, context=context)
# quotation_ids = self.get_quotation_ids(cr, uid, line.id, context=context)
line_obj.create(cr, uid, {'followup_id': followup_id,
'line_id': line.id,
'tender_ids': [(6,0,tender_ids)],
# 'quotation_ids': [(6,0,quotation_ids)],
'purchase_ids': [(6,0,purchase_ids)],
'purchase_line_ids': [(6,0,purchase_line_ids)],
'incoming_ids': [(6,0,incoming_ids)],
'outgoing_ids': [(6,0,outgoing_ids)],})
view_id = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'sales_followup', 'sale_order_line_follow_choose_view')[1]
return {'type': 'ir.actions.act_window',
'res_model': 'sale.order.followup',
'res_id': followup_id,
'view_id': [view_id],
'view_type': 'form',
'view_mode': 'form',}
def get_purchase_ids(self, cr, uid, line_id, context={}):
'''
Returns a list of purchase orders related to the sale order line
'''
line_obj = self.pool.get('sale.order.line')
if isinstance(line_id, (int, long)):
line_id = [line_id]
purchase_ids = []
for line in line_obj.browse(cr, uid, line_id, context=context):
if line.type == 'make_to_order' and line.procurement_id:
if line.procurement_id.purchase_id and not line.procurement_id.purchase_id.rfq_ok:
purchase_ids.append(line.procurement_id.purchase_id.id)
elif line.procurement_id.tender_id and line.procurement_id.tender_id.rfq_ids:
for rfq in line.procurement_id.tender_id.rfq_ids:
if not rfq.rfq_ok:
purchase_ids.append(rfq.id)
return purchase_ids
def get_purchase_line_ids(self, cr, uid, line_id, purchase_ids, context={}):
'''
Returns a list of purchase order lines related to the sale order line
'''
po_line_obj = self.pool.get('purchase.order.line')
line_obj = self.pool.get('sale.order.line')
po_line_ids = []
if isinstance(purchase_ids, (int, long)):
purchase_ids = [purchase_ids]
if isinstance(line_id, (int, long)):
line_id = [line_id]
for line in line_obj.browse(cr, uid, line_id, context=context):
po_line_ids = po_line_obj.search(cr, uid, [('order_id', 'in', purchase_ids), ('product_id', '=', line.product_id.id)], context=context)
return po_line_ids
def get_quotation_ids(self, cr, uid, line_id, context={}):
'''
Returns a list of request for quotation related to the sale order line
'''
line_obj = self.pool.get('sale.order.line')
if isinstance(line_id, (int, long)):
line_id = [line_id]
quotation_ids = []
for line in line_obj.browse(cr, uid, line_id, context=context):
if line.type == 'make_to_order' and line.procurement_id:
if line.procurement_id.purchase_id and line.procurement_id.purchase_id.rfq_ok:
quotation_ids.append(line.procurement_id.purchase_id.id)
elif line.procurement_id.tender_id and line.procurement_id.tender_id.rfq_ids:
for rfq in line.procurement_id.tender_id.rfq_ids:
if rfq.rfq_ok:
quotation_ids.append(rfq.id)
return quotation_ids
def get_incoming_ids(self, cr, uid, line_id, purchase_ids, context={}):
'''
Returns a list of incoming shipments related to the sale order line
'''
line_obj = self.pool.get('sale.order.line')
purchase_obj = self.pool.get('purchase.order')
if isinstance(line_id, (int, long)):
line_id = [line_id]
if isinstance(purchase_ids, (int, long)):
purchase_ids= [purchase_ids]
incoming_ids = []
for line in line_obj.browse(cr, uid, line_id, context=context):
for po in purchase_obj.browse(cr, uid, purchase_ids, context=context):
for po_line in po.order_line:
if po_line.product_id.id == line.product_id.id:
for move in po_line.move_ids:
incoming_ids.append(move.id)
return incoming_ids
def get_outgoing_ids(self, cr, uid, line_id, context={}):
'''
Returns a list of outgoing deliveries related to the sale order line
'''
line_obj = self.pool.get('sale.order.line')
move_obj = self.pool.get('stock.move')
if isinstance(line_id, (int, long)):
line_id = [line_id]
outgoing_ids = []
# Get all stock.picking associated to the sale order line
for line in line_obj.browse(cr, uid, line_id, context=context):
for move in line.move_ids:
if move.id not in outgoing_ids:
outgoing_ids.append(move.id)
# if move.picking_id and move.picking_id.id not in outgoing_ids:
# outgoing_ids.append(move.picking_id.id)
return outgoing_ids
def get_tender_ids(self, cr, uid, line_id, context={}):
'''
Returns a list of call for tender related to the sale order line
'''
line_obj = self.pool.get('sale.order.line')
if isinstance(line_id, (int, long)):
line_id = [line_id]
tender_ids = []
for line in line_obj.browse(cr, uid, line_id, context=context):
for tender in line.tender_line_ids:
tender_ids.append(tender.id)
return tender_ids
sale_order_followup()
class sale_order_line_followup(osv.osv_memory):
_name = 'sale.order.line.followup'
_description = 'Sales Order Lines Followup'
def _get_status(self, cr, uid, ids, field_name, arg, context={}):
'''
Get all status about the line
'''
move_obj = self.pool.get('stock.move')
res = {}
for line in self.browse(cr, uid, ids, context=context):
res[line.id] = {'sourced_ok': 'No',
# 'quotation_status': 'No quotation',
'tender_status': 'N/A',
'purchase_status': 'N/A',
'incoming_status': 'N/A',
'outgoing_status': 'No deliveries',
'product_available': 'Waiting',
'outgoing_nb': 0,
'available_qty': 0.00}
# Set the available qty in stock
res[line.id]['available_qty'] = self.pool.get('product.product').browse(cr, uid, line.line_id.product_id.id, context=context).qty_available
# Define if the line is sourced or not according to the state on the SO line
if line.line_id.state == 'draft':
res[line.id]['sourced_ok'] = 'No'
if line.line_id.state in ('confirmed', 'done'):
res[line.id]['sourced_ok'] = 'Done'
if line.line_id.state == 'cancel':
res[line.id]['sourced_ok'] = 'Cancelled'
if line.line_id.state == 'exception':
res[line.id]['sourced_ok'] = 'Exception'
####################################################
# Get information about the state of call for tender
####################################################
tender_status = {'n_a': 'N/A',
'no_tender': 'No tender',
'partial': 'Partial',
'draft': 'Waiting',
'comparison': 'In Progress',
'done': 'Done',
'cancel': 'Cancelled'}
if line.line_id.type == 'make_to_stock' or line.line_id.po_cft == 'po':
res[line.id]['tender_status'] = tender_status.get('n_a', 'Error on state !')
elif line.line_id.po_cft == 'cft' and not line.tender_ids:
res[line.id]['tender_status'] = tender_status.get('no_tender', 'Error on state !')
else:
# Check if all generated tenders are in the same state
tender_state = False
for tender in line.tender_ids:
if not tender_state:
tender_state = tender.state
if tender_state != tender.state:
tender_state = 'partial'
res[line.id]['tender_status'] = tender_status.get(tender_state, 'Error on state !')
####################################################
# Get information about the state of purchase orders
####################################################
purchase_status = {'n_a': 'N/A',
'no_order': 'No order',
'partial': 'Partial',
'draft': 'Draft',
'confirmed': 'Confirmed',
'wait': 'Confirmed',
'approved': 'Approved',
'done': 'Done',
'cancel': 'Cancelled',
'except_picking': 'Exception',
'except_invoice': 'Exception',}
if line.line_id.type == 'make_to_stock':
res[line.id]['purchase_status'] = purchase_status.get('n_a', 'Error on state !')
elif not line.purchase_ids:
res[line.id]['purchase_status'] = purchase_status.get('no_order', 'Error on state !')
else:
# Check if all generated PO are in the same state
purchase_state = False
for order in line.purchase_ids:
if not purchase_state:
purchase_state = order.state
if purchase_state != order.state:
purchase_state = 'partial'
res[line.id]['purchase_status'] = purchase_status.get(purchase_state, 'Error on state !')
###########################################################
# Get information about the state of all incoming shipments
###########################################################
incoming_status = {'n_a': 'N/A',
'no_incoming': 'No shipment',
'partial': 'Partial',
'draft': 'Waiting',
'confirmed': 'Waiting',
'assigned': 'Available',
'done': 'Done',
'cancel': 'Cancelled'}
if line.line_id.type == 'make_to_stock':
res[line.id]['incoming_status'] = incoming_status.get('n_a', 'Error on state !')
elif not line.incoming_ids:
res[line.id]['incoming_status'] = incoming_status.get('no_incoming', 'Error on state !')
else:
shipment_state = False
for shipment in line.incoming_ids:
if not shipment_state:
shipment_state = shipment.state
if shipment_state != shipment.state:
shipment_state = 'partial'
res[line.id]['incoming_status'] = incoming_status.get(shipment_state, 'Error on state !')
#######################################################################
# Get information about the step and the state of all outgoing delivery
#######################################################################
out_status = {'no_out': 'No deliveries',
'partial': 'Partial',
'draft': 'Waiting',
'confirmed': 'Waiting',
'assigned': 'Available',
'done': 'Done',
'picked': 'Picked',
'packed': 'Packed',
'shipped': 'Shipped',
'cancel': 'Cancelled',}
if not line.outgoing_ids:
res[line.id]['outgoing_status'] = out_status.get('no_out', 'Error on state !')
res[line.id]['outgoing_nb'] = '0'
else:
# Get the first stock.picking
first_out = False
moves_first_out = []
moves_first_out_ids = []
for out in line.outgoing_ids:
if out.picking_id and not out.picking_id.previous_step_id:
first_out = out.picking_id
moves_first_out.append(out)
moves_first_out_ids.append(out.id)
# Check the flow type of the first picking (full or quick)
if first_out.subtype == 'standard':
res[line.id]['outgoing_nb'] = len(moves_first_out)
out_state = False
for out in moves_first_out:
if not out_state:
out_state = out.state
if out.state != out_state:
out_state = 'partial'
res[line.id]['outgoing_status'] = out_status.get(out_state, 'Error on state !')
res[line.id]['product_available'] = out_status.get(out_state, 'Error on state !')
else:
# Full mode
# Begin from the first out moves
ppl_ids = []
out_step = {'general': {'moves': [], 'state': False},
'picking': {'moves': [], 'state': False},
'packing': {'moves': [], 'state': False},
'dispatch': {'moves': [], 'state': False},
'distrib': {'moves': [], 'state': False},
'customer': {'moves': [], 'state': False},}
# Sort outgoing moves by type (picking, packing, dispatch, distrib, sending)
for out in line.outgoing_ids:
if not out.backmove_id:
out_step['general']['moves'].append(out)
elif out.backmove_id.id in moves_first_out_ids and out.picking_id and out.picking_id.subtype == 'picking':
out_step['picking']['moves'].append(out)
elif out.backmove_id.id in moves_first_out_ids and out.picking_id and out.picking_id.subtype == 'ppl':
out_step['packing']['moves'].append(out)
ppl_ids.append(out.picking_id.id)
elif out.backmove_id.id in moves_first_out_ids and out.picking_id and out.picking_id.subtype == 'packing':
if out.location_dest_id.usage == 'customer':
out_step['customer']['moves'].append(out)
elif out.location_dest_id.id == out.picking_id.warehouse_id.lot_distribution_id.id:
out_step['distrib']['moves'].append(out)
elif out.location_dest_id.id == out.picking_id.warehouse_id.lot_dispatch_id.id:
out_step['dispatch']['moves'].append(out)
elif out.location_id.id == out.picking_id.warehouse_id.lot_dispatch_id.id:
out_step['dispatch']['moves'].append(out)
elif out.location_id.id == out.picking_id.warehouse_id.lot_distribution_id.id:
out_step['distrib']['moves'].append(out)
nb_out = len(out_step['picking']['moves'])
nb_return_pack = 0
nb_return_pack2 = 0
for pack in out_step['packing']['moves']:
if pack.state == 'done' and pack.product_qty == 0.00:
nb_return_pack += 1
nb_return_dist = 0
for cust in out_step['customer']['moves']:
if cust.state == 'done' and cust.product_qty == 0.00:
nb_return_dist += 1
# Set the state for the step 'customer'
ret_iter = 0
for cust in out_step['customer']['moves']:
if cust.state == 'done' and ret_iter != nb_return_dist:
ret_iter += 1
continue
if not out_step['customer']['state']:
out_step['customer']['state'] = cust.state
if out_step['customer']['state'] != cust.state:
out_step['customer']['state'] = 'partial'
# Set the state for the step 'distrib'
ret_iter = 0
for dist in out_step['distrib']['moves']:
if dist.state == 'cancel' or dist.product_qty == 0.00:
continue
if dist.state == 'done' and ret_iter != nb_return_dist:
ret_iter += 1
continue
if not out_step['distrib']['state']:
out_step['distrib']['state'] = dist.state
if out_step['distrib']['state'] != dist.state:
out_step['distrib']['state'] = 'partial'
# Set the state for the step 'dispatch'
for disp in out_step['dispatch']['moves']:
if disp.location_id.id == disp.picking_id.warehouse_id.lot_dispatch_id.id:
nb_return_pack2 += 1
nb_out -= 1
if not out_step['dispatch']['state']:
out_step['dispatch']['state'] = disp.state
if out_step['dispatch']['state'] != disp.state:
out_step['dispatch']['state'] = 'partial'
# Set the state for the step 'packing'
ret_iter = 0
for pack in out_step['packing']['moves']:
if pack.product_qty == 0.00 or pack.location_dest_id.id != pack.picking_id.warehouse_id.lot_dispatch_id.id:
continue
if pack.state == 'done' and ret_iter != (nb_return_pack + nb_return_pack2):
ret_iter += 1
continue
if not out_step['packing']['state']:
out_step['packing']['state'] = pack.state
if out_step['packing']['state'] != pack.state:
out_step['packing']['state'] = 'partial'
# Set the state for the step 'picking'
ret_iter = 0
ret_iter2 = 0
for pick in out_step['picking']['moves']:
if pick.state == 'cancel':
nb_out -= 1
continue
if pick.state == 'done' and ret_iter != nb_return_pack:
nb_out -= 1
ret_iter += 1
continue
elif pick.state == 'done' and ret_iter2 != nb_return_pack2:
ret_iter2 += 1
# nb_out -= 1
continue
if not out_step['picking']['state']:
out_step['picking']['state'] = pick.state
if out_step['picking']['state'] != pick.state:
out_step['picking']['state'] = 'partial'
# Increase the nb of out if there are products in general picking ticket
total_line = 0.00
for general in out_step['general']['moves']:
if general.product_qty != 0.00 and general.state != 'cancel':
total_line += general.product_qty
nb_out += 1
out_step['general']['state'] = general.state
# If all products should be processed from the main picking ticket or if the main picking ticket is done
if total_line == line.line_id.product_uom_qty:
res[line.id]['product_available'] = out_status.get(out_step['general']['state'], 'Error on state !')
res[line.id]['outgoing_status'] = out_status.get(out_step['general']['state'], 'Error on state !')
elif out_step['customer']['state'] == 'done':
res[line.id]['product_available'] = out_status.get('done', 'Error on state !')
res[line.id]['outgoing_status'] = out_status.get('done', 'Error on state !')
elif total_line < line.line_id.product_uom_qty and out_step['general']['state']:
res[line.id]['product_available'] = out_status.get('partial', 'Error on state !')
res[line.id]['outgoing_status'] = out_status.get('partial', 'Error on state !')
else:
# If not all products are sent to the supplier
if out_step['customer']['state'] and out_step['customer']['state'] == 'partial':
res[line.id]['outgoing_status'] = out_status.get('partial', 'Error on state !')
res[line.id]['product_available'] = out_status.get('done', 'Error on state !')
# If all products are waiting to send to customer
elif out_step['customer']['state'] and out_step['customer']['state'] == 'assigned':
res[line.id]['outgoing_status'] = out_status.get('shipped', 'Error on state !')
res[line.id]['product_available'] = out_status.get('done', 'Error on state !')
# If all products are not in distribution
if out_step['distrib']['state'] and out_step['distrib']['state'] == 'partial':
res[line.id]['outgoing_status'] = out_status.get('partial', 'Error on state !')
elif out_step['distrib']['state'] and out_step['distrib']['state'] == 'assigned':
res[line.id]['outgoing_status'] = out_status.get('packed', 'Error on state !')
res[line.id]['product_available'] = out_status.get('done', 'Error on state !')
# If all products are not in dispatch zone
if out_step['dispatch']['state'] == 'partial':
res[line.id]['outgoing_status'] = out_status.get('partial', 'Error on state !')
# If all products are not picked
if out_step['picking']['state'] == 'partial' or out_step['packing']['state'] == 'partial':
res[line.id]['outgoing_status'] = out_status.get('partial', 'Error on state !')
res[line.id]['product_available'] = out_status.get(out_step['picking']['state'], 'Error on state !')
elif out_step['picking']['state'] == 'assigned':
res[line.id]['outgoing_status'] = out_status.get('assigned', 'Error on state !')
res[line.id]['product_available'] = out_status.get('assigned', 'Error on state !')
elif out_step['picking']['state'] == 'done' and out_step['packing']['state'] == 'assigned':
res[line.id]['outgoing_status'] = out_status.get('picked', 'Error on state !')
res[line.id]['product_available'] = out_status.get('done', 'Error on state !')
# Set the number of the outgoing deliveries
res[line.id]['outgoing_nb'] = '%s' %nb_out
return res
_columns = {
'followup_id': fields.many2one('sale.order.followup', string='Sale Order Followup', required=True, on_delete='cascade'),
'line_id': fields.many2one('sale.order.line', string='Order line', required=True, readonly=True),
'procure_method': fields.related('line_id', 'type', type='selection', selection=[('make_to_stock','From stock'), ('make_to_order','On order')], readonly=True, string='Proc. Method'),
'po_cft': fields.related('line_id', 'po_cft', type='selection', selection=[('po','PO'), ('cft','CFT')], readonly=True, string='PO/CFT'),
'line_number': fields.related('line_id', 'line_number', string='Order line', readonly=True, type='integer'),
'product_id': fields.related('line_id', 'product_id', string='Product reference', readondy=True,
type='many2one', relation='product.product'),
'qty_ordered': fields.related('line_id', 'product_uom_qty', string='Ordered qty', readonly=True),
'uom_id': fields.related('line_id', 'product_uom', type='many2one', relation='product.uom', string='UoM', readonly=True),
'sourced_ok': fields.function(_get_status, method=True, string='Sourced', type='char',
readonly=True, multi='status'),
'tender_ids': fields.many2many('tender.line', 'call_tender_follow_rel',
'follow_line_id', 'tender_id', string='Tender'),
'tender_status': fields.function(_get_status, method=True, string='Tender', type='char',
readonly=True, multi='status'),
# 'quotation_ids': fields.many2many('purchase.order', 'quotation_follow_rel', 'follow_line_id',
# 'quotation_id', string='Requests for Quotation', readonly=True),
# 'quotation_status': fields.function(_get_status, method=True, string='Request for Quotation',
# type='char', readonly=True, multi='status'),
'purchase_ids': fields.many2many('purchase.order', 'purchase_follow_rel', 'follow_line_id',
'purchase_id', string='Purchase Orders', readonly=True),
'purchase_line_ids': fields.many2many('purchase.order.line', 'purchase_line_follow_rel', 'follow_line_id',
'purchase_line_id', string='Purchase Orders', readonly=True),
'purchase_status': fields.function(_get_status, method=True, string='Purchase Order',
type='char', readonly=True, multi='status'),
'incoming_ids': fields.many2many('stock.move', 'incoming_follow_rel', 'follow_line_id',
'incoming_id', string='Incoming Shipment', readonly=True),
'incoming_status': fields.function(_get_status, method=True, string='Incoming Shipment',
type='char', readonly=True, multi='status'),
'product_available': fields.function(_get_status, method=True, string='Product available',
type='char', readonly=True, multi='status'),
'available_qty': fields.function(_get_status, method=True, string='Product available',
type='float', readonly=True, multi='status'),
'outgoing_ids': fields.many2many('stock.move', 'outgoing_follow_rel', 'outgoing_id',
'follow_line_id', string='Outgoing Deliveries', readonly=True),
'outgoing_status': fields.function(_get_status, method=True, string='Outgoing delivery',
type='char', readonly=True, multi='status'),
'outgoing_nb': fields.function(_get_status, method=True, string='Outgoing delivery',
type='char', readonly=True, multi='status'),
}
sale_order_line_followup()
class sale_order_followup_from_menu(osv.osv_memory):
_name = 'sale.order.followup.from.menu'
_description = 'Sale order followup menu entry'
_columns = {
'order_id': fields.many2one('sale.order', string='Internal reference', required=True),
'cust_order_id': fields.many2one('sale.order', string='Customer reference', required=True),
}
def go_to_followup(self, cr, uid, ids, context={}):
new_context = context.copy()
new_ids = []
for menu in self.browse(cr, uid, ids, context=context):
new_ids.append(menu.order_id and menu.order_id.id or menu.cust_order_id.id)
new_context['active_ids'] = new_ids
return self.pool.get('sale.order.followup').start_order_followup(cr, uid, ids, context=new_context)
def change_order_id(self, cr, uid, ids, order_id, cust_order_id, type='order_id'):
res = {}
if type == 'cust_order_id' and cust_order_id:
res.update({'order_id': False})
elif order_id:
res.update({'cust_order_id': False})
return {'value': res}
sale_order_followup_from_menu()
class tender_line(osv.osv):
_name = 'tender.line'
_inherit = 'tender.line'
def go_to_tender_info(self, cr, uid, ids, context={}):
'''
Return the form of the object
'''
view_id = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'tender_flow', 'tender_form')[1]
tender_id = self.pool.get('tender.line').browse(cr, uid, ids[0], context=context).tender_id.id
return {'type': 'ir.actions.act_window',
'res_model': 'tender',
'view_type': 'form',
'view_mode': 'form',
'view_id': [view_id],
'res_id': tender_id,}
tender_line()
class purchase_order(osv.osv):
_name = 'purchase.order'
_inherit = 'purchase.order'
def go_to_po_info(self, cr, uid, ids, context={}):
'''
Return the form of the object
'''
view_id = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'purchase', 'purchase_order_form')[1]
po_id = self.pool.get('purchase.order.line').browse(cr, uid, ids, context=context).order_id.id
return {'type': 'ir.actions.act_window',
'res_model': 'purchase.order',
'view_type': 'form',
'view_mode': 'form',
'view_id': [view_id],
'res_id': po_id,}
purchase_order()
class request_for_quotation(osv.osv):
_name = 'purchase.order'
_inherit = 'purchase.order'
def go_to_rfq_info(self, cr, uid, ids, context={}):
'''
Return the form of the object
'''
view_id = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'purchase', 'purchase_order_form')[1]
return {'type': 'ir.actions.act_window',
'res_model': 'purchase.order',
'view_type': 'form',
'view_mode': 'form',
'view_id': [view_id],
'res_id': ids[0],}
request_for_quotation()
class stock_move(osv.osv):
_name = 'stock.move'
_inherit = 'stock.move'
def _get_view_id(self, cr, uid, ids, context={}):
'''
Returns the good view id
'''
if isinstance(ids, (int,long)):
ids = [ids]
obj_data = self.pool.get('ir.model.data')
pick = self.pool.get('stock.move').browse(cr, uid, ids, context=context)[0].picking_id
view_list = {'out': ('stock', 'view_picking_out_form'),
'in': ('stock', 'view_picking_in_form'),
'internal': ('stock', 'view_picking_form'),
'picking': ('msf_outgoing', 'view_picking_ticket_form'),
'ppl': ('msf_outgoing', 'view_ppl_form'),
'packing': ('msf_outgoing', 'view_packing_form')
}
if pick.type == 'out':
module, view = view_list.get(pick.subtype,('msf_outgoing', 'view_picking_ticket_form'))
try:
return obj_data.get_object_reference(cr, uid, module, view)[1], pick.id
except ValueError, e:
pass
module, view = view_list.get(pick.type,('stock', 'view_picking_form'))
return self.pool.get('ir.model.data').get_object_reference(cr, uid, module, view)[1], pick.id
def go_to_incoming_info(self, cr, uid, ids, context={}):
'''
Return the form of the object
'''
view_id = self._get_view_id(cr, uid, ids, context=context)
return {'type': 'ir.actions.act_window',
'res_model': 'stock.picking',
'view_type': 'form',
'view_mode': 'form',
'view_id': [view_id[0]],
'res_id': view_id[1],}
def go_to_outgoing_info(self, cr, uid, ids, context={}):
'''
Return the form of the object
'''
view_id = self._get_view_id(cr, uid, ids, context=context)
return {'type': 'ir.actions.act_window',
'res_model': 'stock.picking',
'view_type': 'form',
'view_mode': 'form',
'view_id': [view_id[0]],
'res_id': view_id[1],}
stock_move()
class purchase_order_line(osv.osv):
_name = 'purchase.order.line'
_inherit = 'purchase.order.line'
STATE_SELECTION = [
('draft', 'Draft'),
('wait', 'Waiting'),
('confirmed', 'Waiting Approval'),
('approved', 'Approved'),
('except_picking', 'Shipping Exception'),
('except_invoice', 'Invoice Exception'),
('done', 'Done'),
('cancel', 'Cancelled'),
('rfq_sent', 'RfQ Sent'),
('rfq_updated', 'RfQ Updated'),
]
ORDER_TYPE = [('regular', 'Regular'), ('donation_exp', 'Donation before expiry'),
('donation_st', 'Standard donation'), ('loan', 'Loan'),
('in_kind', 'In Kind Donation'), ('purchase_list', 'Purchase List'),
('direct', 'Direct Purchase Order')]
_columns = {
'order_type': fields.related('order_id', 'order_type', type='selection', selection=ORDER_TYPE, readonly=True),
'po_state': fields.related('order_id', 'state', type='selection', selection=STATE_SELECTION, readonly=True),
}
def go_to_po_info(self, cr, uid, ids, context={}):
'''
Return the form of the object
'''
view_id = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'purchase', 'purchase_order_form')[1]
if isinstance(ids, (int,long)):
ids = [ids]
po_id = self.pool.get('purchase.order.line').browse(cr, uid, ids, context=context)[0].order_id.id
return {'type': 'ir.actions.act_window',
'res_model': 'purchase.order',
'view_type': 'form',
'view_mode': 'form',
'view_id': [view_id],
'res_id': po_id,}
purchase_order_line()
class sale_order(osv.osv):
_name = 'sale.order'
_inherit = 'sale.order'
def name_search(self, cr, uid, name='', args=None, operator='ilike', context={}, limit=80):
'''
Search all SO by internal or customer reference
'''
if context.get('from_followup'):
ids = []
if name and len(name) > 1:
ids.extend(self.search(cr, uid, [('client_order_ref', operator, name)], context=context))
return self.name_get(cr, uid, ids, context=context)
else:
return super(sale_order, self).name_search(cr, uid, name, args, operator, context, limit)
def name_get(self, cr, uid, ids, context={}):
'''
If the method is called from followup wizard, set the customer ref in brackets
'''
if context.get('from_followup'):
res = []
for r in self.browse(cr, uid, ids, context=context):
if r.client_order_ref:
res.append((r.id, '%s' % r.client_order_ref))
else:
res.append((r.id, '%s' % r.name))
return res
else:
return super(sale_order, self).name_get(cr, uid, ids, context=context)
sale_order()
|