-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathis_analyse_cbn.py
1129 lines (1010 loc) · 51.4 KB
/
is_analyse_cbn.py
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
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
from openerp import models,fields,api
import time
import datetime
from collections import OrderedDict
import tempfile
class product_product(models.Model):
_inherit = "product.product"
@api.model
def analyse_cbn(self,filter=False):
cr, uid, context = self.env.args
validation = filter['validation']
if validation=='ko':
#** Lecture des critères enregistrés *******************************
code_pg_debut = self.env['is.mem.var'].get(uid,'code_pg_debut')
gest = self.env['is.mem.var'].get(uid,'gest')
cat = self.env['is.mem.var'].get(uid,'cat')
moule = self.env['is.mem.var'].get(uid,'moule')
projet = self.env['is.mem.var'].get(uid,'projet')
client = self.env['is.mem.var'].get(uid,'client')
fournisseur = self.env['is.mem.var'].get(uid,'fournisseur')
type_commande = self.env['is.mem.var'].get(uid,'type_commande')
type_rapport = self.env['is.mem.var'].get(uid,'type_rapport')
calage = self.env['is.mem.var'].get(uid,'calage')
nb_semaines = self.env['is.mem.var'].get(uid,'nb_semaines')
valorisation=''
else:
#** Lecture des filtres ********************************************
code_pg_debut = filter['code_pg_debut']
gest = filter['gest']
cat = filter['cat']
moule = filter['moule']
projet = filter['projet']
client = filter['client']
fournisseur = filter['fournisseur']
type_commande = filter['type_commande']
type_rapport = filter['type_rapport']
calage = filter['calage']
nb_semaines = filter['nb_semaines']
valorisation = filter['valorisation']
#*******************************************************************
#** Enregistrement des critères enregistrés ************************
self.env['is.mem.var'].set(uid, 'code_pg_debut', code_pg_debut)
self.env['is.mem.var'].set(uid, 'gest', gest)
self.env['is.mem.var'].set(uid, 'cat', cat)
self.env['is.mem.var'].set(uid, 'moule', moule)
self.env['is.mem.var'].set(uid, 'projet', projet)
self.env['is.mem.var'].set(uid, 'client', client)
self.env['is.mem.var'].set(uid, 'fournisseur', fournisseur)
self.env['is.mem.var'].set(uid, 'type_commande', type_commande)
self.env['is.mem.var'].set(uid, 'type_rapport', type_rapport)
self.env['is.mem.var'].set(uid, 'calage', calage)
self.env['is.mem.var'].set(uid, 'nb_semaines', nb_semaines)
#*******************************************************************
#** Valeur par défaut **************************************************
code_pg_debut = code_pg_debut or ''
gest = gest or ''
cat = cat or ''
moule = moule or ''
projet = projet or ''
client = client or ''
fournisseur = fournisseur or ''
type_commande = type_commande or ''
type_rapport = type_rapport or 'Fabrication'
calage = calage or 'Date de fin'
nb_semaines = nb_semaines or 18
nb_semaines = int(nb_semaines)
height = filter['height']
#***********************************************************************
#** Listes de choix des filtres ****************************************
select_nb_semaines=[4,8,12,16,18,20,25,30,40,60]
select_type_commande=['','ferme_uniquement','ferme','ouverte','cadencée']
select_type_rapport=['Fabrication','Achat']
select_calage=['Date de fin','Date de début']
#***********************************************************************
# ** Titre du rapport **************************************************
titre="Suggestions de fabrication"
if type_rapport=="Achat":
titre="Suggestions d'achat";
if valorisation:
titre="Valorisation stock fabrication"
if type_rapport=="Achat":
titre="Valorisation stock achat"
# **********************************************************************
# ** Recherche des catégories ******************************************
SQL="""
select id, name
from is_category
where id>0
"""
cr.execute(SQL)
result = cr.fetchall()
cat2id={}
for row in result:
cat2id[row[1]]=row[0]
# **********************************************************************
# ** Recherche des couts ***********************************************
SQL="""
select name, cout_act_total
from is_cout
"""
cr.execute(SQL)
result = cr.fetchall()
Couts={}
for row in result:
Couts[row[0]]=row[1]
# **********************************************************************
# ** Filtre pour les requêtes ******************************************
filtre="";
if code_pg_debut:
filtre=filtre+" and pt.is_code ilike '"+code_pg_debut+"%' "
if type_rapport=="Achat":
filtre=filtre+" and pt.purchase_ok=true "
else:
filtre=filtre+" and pt.purchase_ok<>true "
if cat:
filtre=filtre+" and ic.name='"+cat+"' "
if moule:
filtre=filtre+" and (im.name='"+moule+"' or id.name='"+moule+"' )"
if projet:
filtre=filtre+" and (imp1.name ilike '%"+projet+"%' or imp2.name ilike '%"+projet+"%') "
if client:
filtre=filtre+" and rp.is_code='"+client+"' "
# **********************************************************************
# ** Recherche du type de commandes d'achat ****************************
TypeCde={}
if type_rapport=="Achat":
SQL="""
SELECT
cof.type_commande,
cof.partner_id,
rp.is_code,
rp.is_adr_code,
cofp.product_id
FROM is_cde_ouverte_fournisseur cof inner join is_cde_ouverte_fournisseur_product cofp on cofp.order_id=cof.id
inner join res_partner rp on cof.partner_id=rp.id
"""
cr.execute(SQL)
result = cr.fetchall()
for row in result:
cle=('0000'+row[2])[-4:]+'-'+row[3]+'/'+str(row[4])
TypeCde[cle]=row[0]
SQL="""
SELECT
rp.is_code,
rp.is_adr_code,
product_id
FROM is_cde_ferme_cadencee cfc inner join res_partner rp on cfc.partner_id=rp.id
"""
cr.execute(SQL)
result = cr.fetchall()
for row in result:
cle=('0000'+row[0])[-4:]+'-'+row[1]+'/'+str(row[2])
TypeCde[cle]=u'cadencée'
# **********************************************************************
# ** Recherche de la liste des gestionnaires ***************************
SQL="""
SELECT distinct ig.name gest
FROM product_product pp inner join product_template pt on pp.product_tmpl_id=pt.id
left outer join is_mold im on pt.is_mold_id=im.id
left outer join is_dossierf id on pt.is_dossierf_id=id.id
inner join is_gestionnaire ig on pt.is_gestionnaire_id=ig.id
left outer join is_category ic on pt.is_category_id=ic.id
left outer join is_mold_project imp1 on im.project=imp1.id
left outer join is_mold_project imp2 on id.project=imp2.id
left outer join res_partner rp on pt.is_client_id=rp.id
WHERE pp.id>0 """+filtre+"""
"""
if fournisseur:
code=fournisseur.split('-')[0]
SQL=SQL+"""
and (
(
select rp2.is_code
from product_supplierinfo ps inner join res_partner rp2 on ps.name=rp2.id
where ps.product_tmpl_id=pt.id order by ps.sequence,ps.id limit 1
)='"""+code+"""'
)
"""
SQL=SQL+""" ORDER BY ig.name """
cr.execute(SQL)
result = cr.fetchall()
SelectGestionnaires={}
for row in result:
SelectGestionnaires[row[0]]=1
SelectGestionnaires=OrderedDict(sorted(SelectGestionnaires.items(), key=lambda t: t[0]))
select_gest=[]
select_gest.append('')
for x in SelectGestionnaires:
select_gest.append(x)
# **********************************************************************
# ** Recherche de la liste des fournisseurs ****************************
SQL="""
SELECT
( select concat(rp2.is_code,'-',rp2.is_adr_code)
from product_supplierinfo ps inner join res_partner rp2 on ps.name=rp2.id
where ps.product_tmpl_id=pt.id order by ps.sequence,ps.id limit 1
) code_fournisseur
FROM product_product pp inner join product_template pt on pp.product_tmpl_id=pt.id
left outer join is_mold im on pt.is_mold_id=im.id
left outer join is_dossierf id on pt.is_dossierf_id=id.id
inner join is_gestionnaire ig on pt.is_gestionnaire_id=ig.id
left outer join is_category ic on pt.is_category_id=ic.id
left outer join is_mold_project imp1 on im.project=imp1.id
left outer join is_mold_project imp2 on id.project=imp2.id
left outer join res_partner rp on pt.is_client_id=rp.id
WHERE pp.id>0 """+filtre+"""
"""
cr.execute(SQL)
result = cr.fetchall()
select_fournisseur=[]
select_fournisseur.append('')
for row in result:
cle=row[0]
if cle and cle not in select_fournisseur:
select_fournisseur.append(cle)
select_fournisseur.sort()
# **********************************************************************
if validation=='ko':
html='Indiquez vos critères de filtre et validez'
else:
# ** Filtre sur le fournisseur (partner_id) ****************************
partner_id=False
if fournisseur:
tab=fournisseur.split('-')
SQL="select id from res_partner where is_code='"+tab[0]+"' and is_adr_code='"+tab[1]+"' "
cr.execute(SQL)
result = cr.fetchall()
for row in result:
partner_id=row[0]
if gest:
filtre=filtre+" and ig.name='"+gest+"' "
if partner_id:
filtre=filtre+"""
and (
( select rp2.id from product_supplierinfo ps inner join res_partner rp2 on ps.name=rp2.id
where ps.product_tmpl_id=pt.id order by ps.sequence limit 1
)="""+str(partner_id)+"""
)
"""
filtre=filtre+" AND ic.name!='80' "
# **********************************************************************
# ** Tableau des semaines **********************************************
date=datetime.datetime.now()
jour=date.weekday()
date = date - datetime.timedelta(days=jour)
TabSemaines=[]
for i in range(0,int(nb_semaines)):
d=date.strftime('%Y%m%d')
TabSemaines.append(d)
date = date + datetime.timedelta(days=7)
# **********************************************************************
# ** Recherche stock A *************************************************
SQL="""
select sq.product_id, sum(sq.qty) as qt
from stock_quant sq inner join stock_location sl on sq.location_id=sl.id
where sl.usage='internal' and sl.active='t' and sl.control_quality='f'
group by sq.product_id
order by sq.product_id
"""
cr.execute(SQL)
result = cr.fetchall()
StocksA={}
for row in result:
StocksA[row[0]]=row[1]
# **********************************************************************
# ** Recherche stock Q *************************************************
SQL="""
select sq.product_id, sum(sq.qty) as qt
from stock_quant sq inner join stock_location sl on sq.location_id=sl.id
where sl.usage='internal' and sl.active='t' and sl.control_quality='t'
group by sq.product_id
order by sq.product_id
"""
cr.execute(SQL)
result = cr.fetchall()
StocksQ={}
for row in result:
StocksQ[row[0]]=row[1]
# **********************************************************************
# ** Recherche stock sécu **********************************************
SQL="""
select pp.id, pt.is_stock_secu as qt
from product_product pp inner join product_template pt on pp.product_tmpl_id=pt.id
"""
cr.execute(SQL)
result = cr.fetchall()
StocksSecu={}
for row in result:
StocksSecu[row[0]]=row[1]
# **********************************************************************
# ** Recherche des fournisseurs par défaut *****************************
SQL="""
select
a.id as product_id,
c.is_code as is_code,
c.name as name,
b.delay as delay,
c.is_adr_code
from product_product a, product_supplierinfo b, res_partner c
where a.product_tmpl_id=b.product_tmpl_id and b.name=c.id
order by b.sequence, b.id
"""
cr.execute(SQL)
result = cr.fetchall()
Fournisseurs={}
Delai_Fournisseurs={}
for row in result:
name=('0000'+row[1])[-4:]+'-'+row[4]
if name=='':
name=row[2]
Fournisseurs[row[0]]=name
Delai_Fournisseurs[row[0]]=row[3]
# **********************************************************************
# ** Recherche des prévisions du CBN ***********************************
SQL="""
SELECT
mp.id as numod,
mp.start_date as date_debut,
mp.start_date_cq as date_fin,
mp.quantity as qt,
mp.type as typeod,
mp.product_id,
pt.is_code as code,
pp.name_template as designation,
pt.is_stock_secu,
pt.produce_delay,
pt.lot_mini,
pt.multiple,
pt.is_mold_dossierf as moule,
mp.name as name,
ig.name as gest
FROM mrp_prevision mp inner join product_product pp on mp.product_id=pp.id
inner join product_template pt on pp.product_tmpl_id=pt.id
left outer join is_mold im on pt.is_mold_id=im.id
left outer join is_dossierf id on pt.is_dossierf_id=id.id
left outer join is_gestionnaire ig on pt.is_gestionnaire_id=ig.id
left outer join is_category ic on pt.is_category_id=ic.id
left outer join is_mold_project imp1 on im.project=imp1.id
left outer join is_mold_project imp2 on id.project=imp2.id
left outer join res_partner rp on pt.is_client_id=rp.id
WHERE mp.id>0 """+filtre+"""
ORDER BY mp.name
"""
cr.execute(SQL)
result = cr.fetchall()
TabIni={};
TabIni=self.RemplitTab(TabIni, result, TabSemaines, type_rapport,
StocksA, StocksQ, StocksSecu, Fournisseurs, Delai_Fournisseurs,
calage, valorisation, Couts, fournisseur, TypeCde, type_commande)
# **********************************************************************
# ** Recherche des commandes client ************************************
SQL="""
SELECT
so.id as numod,
sol.is_date_expedition as date_debut,
sol.is_date_expedition as date_fin,
sol.product_uom_qty as qt,
sol.is_type_commande as typeod,
sol.product_id,
pt.is_code as code,
pp.name_template as designation,
pt.is_stock_secu,
pt.produce_delay,
pt.lot_mini,
pt.multiple,
pt.is_mold_dossierf as moule,
so.name as name
FROM sale_order_line sol inner join sale_order so on sol.order_id=so.id
inner join product_product pp on sol.product_id=pp.id
inner join product_template pt on pp.product_tmpl_id=pt.id
left outer join is_mold im on pt.is_mold_id=im.id
left outer join is_dossierf id on pt.is_dossierf_id=id.id
left outer join is_gestionnaire ig on pt.is_gestionnaire_id=ig.id
left outer join is_category ic on pt.is_category_id=ic.id
left outer join is_mold_project imp1 on im.project=imp1.id
left outer join is_mold_project imp2 on id.project=imp2.id
left outer join res_partner rp on pt.is_client_id=rp.id
WHERE sol.id>0 """+filtre+""" and sol.state<>'done' and sol.state<>'cancel'
ORDER BY sol.name
"""
cr.execute(SQL)
result = cr.fetchall()
TabIni=self.RemplitTab(TabIni, result, TabSemaines, type_rapport,
StocksA, StocksQ, StocksSecu, Fournisseurs, Delai_Fournisseurs,
calage, valorisation, Couts, fournisseur, TypeCde, type_commande)
# **********************************************************************
# ** Recherche des OF **************************************************
SQL="""
SELECT
mp.id as numod,
mp.date_planned as date_debut,
mp.date_planned as date_fin,
sm.product_qty as qt,
'FL' as typeod,
sm.product_id,
pt.is_code as code,
pp.name_template as designation,
pt.is_stock_secu,
pt.produce_delay,
pt.lot_mini,
pt.multiple,
pt.is_mold_dossierf as moule,
mp.name as name
FROM stock_move sm inner join product_product pp on sm.product_id=pp.id
inner join product_template pt on pp.product_tmpl_id=pt.id
inner join mrp_production mp on mp.id=sm.production_id
left outer join is_mold im on pt.is_mold_id=im.id
left outer join is_dossierf id on pt.is_dossierf_id=id.id
left outer join is_gestionnaire ig on pt.is_gestionnaire_id=ig.id
left outer join is_category ic on pt.is_category_id=ic.id and ic.name!='74'
left outer join is_mold_project imp1 on im.project=imp1.id
left outer join is_mold_project imp2 on id.project=imp2.id
left outer join res_partner rp on pt.is_client_id=rp.id
WHERE sm.id>0 """+filtre+""" and production_id>0 and sm.state<>'done' and sm.state<>'cancel'
ORDER BY sm.name
"""
cr.execute(SQL)
result = cr.fetchall()
TabIni=self.RemplitTab(TabIni, result, TabSemaines, type_rapport,
StocksA, StocksQ, StocksSecu, Fournisseurs, Delai_Fournisseurs,
calage, valorisation, Couts, fournisseur, TypeCde, type_commande)
# **********************************************************************
# ** Recherche des composants des OF ***********************************
SQL="""
SELECT
sm.id as numod,
sm.date_expected as date_debut,
sm.date_expected as date_fin,
sm.product_qty as qt,
'FM' as typeod,
sm.product_id,
pt.is_code as code,
pp.name_template as designation,
pt.is_stock_secu,
pt.produce_delay,
pt.lot_mini,
pt.multiple,
pt.is_mold_dossierf as moule,
sm.name as name
FROM stock_move sm inner join product_product pp on sm.product_id=pp.id
inner join product_template pt on pp.product_tmpl_id=pt.id
left outer join is_mold im on pt.is_mold_id=im.id
left outer join is_dossierf id on pt.is_dossierf_id=id.id
left outer join is_gestionnaire ig on pt.is_gestionnaire_id=ig.id
left outer join is_category ic on pt.is_category_id=ic.id
left outer join is_mold_project imp1 on im.project=imp1.id
left outer join is_mold_project imp2 on id.project=imp2.id
left outer join res_partner rp on pt.is_client_id=rp.id
WHERE sm.id>0 """+filtre+""" and raw_material_production_id>0 and sm.state<>'done' and sm.state<>'cancel'
ORDER BY sm.name
"""
cr.execute(SQL)
result = cr.fetchall()
TabIni=self.RemplitTab(TabIni, result, TabSemaines, type_rapport,
StocksA, StocksQ, StocksSecu, Fournisseurs, Delai_Fournisseurs,
calage, valorisation, Couts, fournisseur, TypeCde, type_commande)
# **********************************************************************
# ** Recherche des commandes fournisseurs *****************************
SQL="""
SELECT
po.id as numod,
pol.date_planned as date_debut,
pol.date_planned as date_fin,
sm.product_qty as qt,
'SF' as typeod,
pol.product_id,
pt.is_code as code,
pp.name_template as designation,
pt.is_stock_secu,
pt.produce_delay,
pt.lot_mini,
pt.multiple,
pt.is_mold_dossierf as moule,
po.name as name
FROM purchase_order_line pol left outer join purchase_order po on pol.order_id=po.id
inner join product_product pp on pol.product_id=pp.id
inner join product_template pt on pp.product_tmpl_id=pt.id
inner join stock_move sm on pol.id=sm.purchase_line_id
left outer join is_mold im on pt.is_mold_id=im.id
left outer join is_dossierf id on pt.is_dossierf_id=id.id
left outer join is_gestionnaire ig on pt.is_gestionnaire_id=ig.id
left outer join is_category ic on pt.is_category_id=ic.id and ic.name not in ('70','72','73','74')
left outer join is_mold_project imp1 on im.project=imp1.id
left outer join is_mold_project imp2 on id.project=imp2.id
left outer join res_partner rp on pt.is_client_id=rp.id
WHERE pol.id>0 """+filtre+""" and pol.state<>'draft' and pol.state<>'done' and pol.state<>'cancel'
and sm.state in ('draft','waiting','confirmed','assigned')
ORDER BY pol.name
"""
cr.execute(SQL)
result = cr.fetchall()
TabIni=self.RemplitTab(TabIni, result, TabSemaines, type_rapport,
StocksA, StocksQ, StocksSecu, Fournisseurs, Delai_Fournisseurs,
calage, valorisation, Couts, fournisseur, TypeCde, type_commande)
# **********************************************************************
# ** Recherche pour avoir tous les articles dans le résultat **********
if valorisation:
SQL="""
SELECT
pt.id as numod,
'' as date_debut,
'' as date_fin,
0 as qt,
'stock' as typeod,
pp.id as product_id,
pt.is_code as code,
pp.name_template as designation,
pt.is_stock_secu,
pt.produce_delay,
pt.lot_mini,
pt.multiple,
pt.is_mold_dossierf as moule,
pp.name_template as name
FROM product_product pp inner join product_template pt on pp.product_tmpl_id=pt.id
left outer join is_mold im on pt.is_mold_id=im.id
left outer join is_dossierf id on pt.is_dossierf_id=id.id
left outer join is_gestionnaire ig on pt.is_gestionnaire_id=ig.id
left outer join is_category ic on pt.is_category_id=ic.id and ic.name not in ('70','72','73','74')
left outer join is_mold_project imp1 on im.project=imp1.id
left outer join is_mold_project imp2 on id.project=imp2.id
left outer join res_partner rp on pt.is_client_id=rp.id
WHERE pt.id>0 """+filtre+"""
ORDER BY pt.is_code
"""
cr.execute(SQL)
result = cr.fetchall()
TabIni=self.RemplitTab(TabIni, result, TabSemaines, type_rapport,
StocksA, StocksQ, StocksSecu, Fournisseurs, Delai_Fournisseurs,
calage, valorisation, Couts, fournisseur, TypeCde, type_commande)
# **********************************************************************
#** RemplitTabSho ******************************************************
TabSho={};
lig=0;
Tab=TabIni
for key, val in Tab.iteritems():
Type=Tab[key]['TYPE']
if Type!='99-Stock':
color=self.color_cel(Type)
if 0 not in TabSho:
TabSho[0]={}
if 1 not in TabSho:
TabSho[1]={}
TabSho[0][lig]=Tab[key]["Code"]
TabSho[1][lig]=Type
Type=Type[3:]
for i in range(0, nb_semaines):
k=TabSemaines[i]
if k in Tab[key]:
Qt=round(Tab[key][k],2)
else:
Qt=0
Lien="#"
k=TabSemaines[i]+'OT'
if k in Tab[key]:
OT=Tab[key][k]
OT=OT[0:len(OT)-1]
else:
OT=''
k=TabSemaines[i]+'INFO'
if k in Tab[key]:
INFO=Tab[key][k]
else:
INFO=''
docid=''
if Type=='FS' or Type=='SA' or Type=='CF' or Type=='CP' or Type=='FL' or Type=='SF':
Lien="Modif_FS_Liste.php?zzTypeOD="+Type.lower()+"&zzNumOD="+str(OT)
docid=str(OT)
k=i+2
if k not in TabSho:
TabSho[k]={}
if Qt==0 and color!='Black':
val=''
else:
val="{0:10.0f}".format(Qt)
#TabSho[k][lig]="<a style=\"color:"+color+";\" class=\"info\" target='Modif_Odoo' href=\""+Lien+"\">"+"{0:10.0f}".format(Qt)+"<span>"+INFO+"</span></a>"
TabSho[k][lig]="<a style=\"color:"+color+";\" class=\"info\" type='"+Type+"' docid='"+str(docid)+"'>"+val+"<span>"+INFO+"</span></a>"
#** Calcul du stock theorique **************************
if Tab[key]['TYPE']=='90-Stock':
if i==0:
Stock=Tab[key][0]+Qt
else:
q=TabSho[1+i][lig]
Stock=TabSho[1+i][lig]+Qt
TabSho[2+i][lig]=Stock
#*******************************************************
#** Calcul du stock valorisé ***************************
if Tab[key]['TYPE']=='92-Stock Valorisé':
if i==0:
Stock=Tab[key][0]+Qt;
else:
Stock=TabSho[1+i][lig]+Qt;
TabSho[2+i][lig]=round(Stock,2)
#*******************************************************
lig+=1
Tab=TabSho
# ******************************************************************
NomCol = ["Sécu / Délai / Lot / Multi / Stock A/Q", "Type"]
Style = ["NormalLN", "NormalCN"]
Format = ["TEXT" , "TEXT" ]
Total = [0 , 0 ]
Lien = ["" , "" ]
Size = [220 , 40 ]
# ** Tableau des semaines ******************************************
date=datetime.datetime.now()
jour=date.weekday()
date = date - datetime.timedelta(days=jour)
TabSemaines=[]
for i in range(0,int(nb_semaines)):
s='S'+str(date.isocalendar()[1])+'<br />'+date.strftime('%d.%m')
TabSemaines.append(s)
date = date + datetime.timedelta(days=7)
# ******************************************************************
for i in range(0,int(nb_semaines)):
NomCol.append(TabSemaines[i])
Style.append("NormalRN")
Format.append("TEXT")
Total.append(0)
Lien.append("")
Size.append(48)
width=220+40+nb_semaines*(48+2)+22
#** Création des listes et de la clé de tri ***********************
lst={}
lst['key']=[]
for col, v1 in Tab.iteritems():
lst[col]=[]
for lig, v2 in v1.iteritems():
if col==1:
key=Tab[0][lig]+Tab[1][lig]
lst['key'].append(key)
lst[col].append(v2)
#*******************************************************************
#** Tri des listes *************************************************
z=[]
z.append(lst['key'])
for col in range(0,len(Tab)):
z.append(lst[col])
NewTab=zip(*z)
NewTab.sort()
#*******************************************************************
#** Reconsctuction du Tab ******************************************
Tab={}
lig=0
for row in NewTab:
col=0
for cel in row:
if col>0:
if lig==0:
Tab[col-1]={}
Tab[col-1][lig]=cel
col+=1
lig+=1
#*******************************************************************
#** Génération du fichier CSV **************************************
attachment_id=''
if valorisation:
csv={};
for lig in range(0,len(Tab[0])):
#** Recherche du CodePG et de la désignation ***************
Key=Tab[0][lig]
Key=Key.split('</b>')
Key=Key[0]
Key=Key.split('<b>')
Key=Key[1]
CodePG=Key
Key=Tab[0][lig]
Key=Key.split('<br />')
Key=Key[1]
Designation=Key
#***********************************************************
if CodePG not in csv:
csv[CodePG]={}
csv[CodePG][0]=CodePG
csv[CodePG][1]=Designation
Type=Tab[1][lig]
if Type=='90-Stock':
for col in range(2,len(Tab)):
csv[CodePG][col*1000+1]=Tab[col][lig]
if Type=='92-Stock Valorisé':
for col in range(2,len(Tab)):
csv[CodePG][col*1000+2]=Tab[col][lig]
#** Ecriture fichier CSV **************************************
user = self.env['res.users'].browse(uid)
name = 'analyse-cbn-'+user.login+'.csv'
path='/tmp/'+name
f = open(path,'wb')
txt=[];
txt.append('CodePG')
txt.append('Désignation')
date=datetime.datetime.now()
jour=date.weekday()
date = date - datetime.timedelta(days=jour)
for i in range(0,int(nb_semaines)):
v='Stock S'+str(date.isocalendar()[1])+' '+date.strftime('%d.%m')
txt.append(v)
v='Valorisé S'+str(date.isocalendar()[1])+' '+date.strftime('%d.%m')
txt.append(v)
date = date + datetime.timedelta(days=7)
f.write(u'\t'.join(txt)+'\r\n')
for k, v in csv.iteritems():
v=self.ksort(v)
txt=[]
for k2, v2 in v:
txt.append(str(v2).replace('.',','))
f.write(u'\t'.join(txt)+'\r\n')
f.close()
#***************************************************************
# ** Creation ou modification de la pièce jointe *******************
attachment_obj = self.env['ir.attachment']
attachments = attachment_obj.search([('res_id','=',user.id),('name','=',name)])
csv = open(path,'rb').read()
vals = {
'name': name,
'datas_fname': name,
'type': 'binary',
'res_id': user.id,
'datas': csv.encode('base64'),
}
attachment_id=False
if attachments:
for attachment in attachments:
attachment.write(vals)
attachment_id=attachment.id
else:
attachment = attachment_obj.create(vals)
attachment_id=attachment.id
#*******************************************************************
#*******************************************************************
#** Code HTML ******************************************************
if len(Tab)==0:
html='Aucune donnée !'
else:
html=''
head="<thead><tr class=\"TitreTabC\">\n"
for col in range(0,len(Tab)):
align='left'
if col>1:
align='right'
head+="<th style=\"width:"+str(Size[col])+"px;text-align:"+align+"\">"+NomCol[col]+"</th>\n"
head+="</tr></thead>\n"
html+="<div style=\"width:"+str(width+20)+"px;\" id=\"table_head\">\n"
html+="<table style=\"border-width:0px;border-spacing:0px;padding:0px;width:"+str(width)+"px;\">\n"
html+=head
html+="</table>\n"
html+="</div>\n"
alt=1
#height=10000
#height:"+str(height)+"px
html+="<div style=\"width:"+str(width+20)+"px;\" id=\"table_body\">\n";
html+="<table style=\"border-width:0px;border-spacing:0px;padding:0px;width:"+str(width)+"px;\">\n";
html+=head;
html+="<tbody class=\"tbody\">\n"
for lig in range(0,len(Tab[0])):
if lig>0:
if Tab[0][lig]!=Tab[0][lig-1]:
alt=-alt
if alt==1:
bgcolor="ffdec0"
else:
bgcolor="fae9da"
onclick = "onclick=\"clicktr('id"+str(lig)+"','"+bgcolor+"','2')\""
html+="<tr style=\"background-color:#"+bgcolor+";\" id=\"id"+str(lig)+\
"\"onmouseover=\"clicktr('id"+str(lig)+"','ffff00','1')\"onmouseout=\"clicktr('id"+str(lig)+\
"','"+bgcolor+"','1')\" "+onclick+">\n"
for col in range(0,len(Tab)):
cel=Tab[col][lig]
if(col==1):
cel=cel[3:]
if lig>0:
if col==0:
if Tab[0][lig]==Tab[0][lig-1]:
cel=" "
#** Recherche du nombre de lignes ayant le même code ***********
rowspan=1
if col==0:
if lig==0 or (lig>0 and Tab[0][lig]!=Tab[0][lig-1]):
while (lig+rowspan) in Tab[0] and Tab[0][lig]==Tab[0][lig+rowspan]:
rowspan+=1
#***************************************************************
color="black"
if col>0:
color=self.color_cel(Tab[1][lig],cel)
if col>1:
if type(cel)==float:
if cel==0 and color!='Black':
cel=''
else:
cel="{:10.0f}".format(cel)
cel="<b>"+str(cel)+"</b>"
align='left'
if col>1:
align='right'
if lig==0 or col>0 or (lig>0 and Tab[0][lig]!=Tab[0][lig-1]):
html+="<td style=\"width:"+str(Size[col])+"px;color:"+color+";text-align:"+align+";\" rowspan=\""+str(rowspan)+"\" class=\""+Style[col]+"\">"+str(cel)+"</td>\n"
html+="</tr>\n"
html+="</tbody>\n"
html+="</table>\n"
html+="</div>"
if valorisation:
html+="<a class=\"info\" type='stock-valorise' attachment_id='"+str(attachment_id)+"'>Stock valorisé</a>\n"
#***********************************************************************
vals={
'titre' : titre,
'code_pg_debut' : code_pg_debut,
'moule' : moule,
'cat' : cat,
'projet' : projet,
'client' : client,
'valorisation' : valorisation,
'select_gest' : select_gest,
'gest' : gest,
'select_fournisseur' : select_fournisseur,
'fournisseur' : fournisseur,
'select_nb_semaines' : select_nb_semaines,
'nb_semaines' : nb_semaines,
'select_type_commande': select_type_commande,
'type_commande' : type_commande,
'select_type_rapport' : select_type_rapport,
'type_rapport' : type_rapport,
'select_calage' : select_calage,
'calage' : calage,
'html' : html,
}
return vals
@api.model
def ksort(self,d):
return [(k,d[k]) for k in sorted(d.keys())]
@api.model
def RemplitTab(self,Tab, result, TabSemaines, type_rapport, StocksA,
StocksQ, StocksSecu, Fournisseurs, Delai_Fournisseurs, calage,
valorisation, Couts, fournisseur, TypeCde, type_commande):
for row in result:
numod = row[0]
date_debut = row[1]
date_fin = row[2]
qt = row[3]
typeod = row[4].strip()
product_id = row[5]
code_pg = row[6]
designation = row[7]
is_stock_secu = row[8]
produce_delay = row[9]
lot_mini = row[10]
multiple = row[11]
moule = row[12] or ''
name = row[13]
test=True
if type_rapport=='Achat':
is_code=''
if product_id in Fournisseurs:
is_code = Fournisseurs[product_id]
cle = is_code+'/'+str(product_id);
if cle in TypeCde:
t=TypeCde[cle]
Code=code_pg+' ('+t+')';
if type_commande!='' and type_commande!=t:
test=False
StockA=0;
StockQ=0;
if product_id in StocksA:
StockA=StocksA[product_id]
if product_id in StocksQ:
StockQ=StocksQ[product_id]
if typeod=='stock' and StockA==0:
test=False
if test:
Tri=moule
if type_rapport=='Achat':
if product_id in Fournisseurs:
Fournisseur=Fournisseurs[product_id]
else:
Fournisseur='0000'
Tri=Fournisseur
if typeod=='':
typeod='ferme'
Cle=code_pg+typeod
Code=Tri+" / <b>"+code_pg+"</b>"
if type_rapport=='Achat' and moule!='':
Code=Code+' / '+moule
if type_rapport=='Achat':
k = is_code+'/'+str(product_id);
if k in TypeCde:
t=TypeCde[k]
Code=Code+' ('+t+')';
if type_rapport=='Achat':
Delai=0
if product_id in Delai_Fournisseurs:
Delai=Delai_Fournisseurs[product_id]
else:
Delai=produce_delay
Code=Code+\
'<br />'+\
designation+'<br />'+\
"{0:10.0f}".format(is_stock_secu)+' / '+\
"{0:10.0f}".format(Delai)+' / '+\
"{0:10.0f}".format(lot_mini)+' / '+\
"{0:10.0f}".format(multiple)+' / '+\
"{0:10.0f}".format(StockA)+' / '+\