-
Notifications
You must be signed in to change notification settings - Fork 19
/
nomenclature.php
1590 lines (1290 loc) · 71 KB
/
nomenclature.php
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
<?php
require 'config.php';
dol_include_once('/product/class/product.class.php');
dol_include_once('/fourn/class/fournisseur.product.class.php');
dol_include_once('/core/class/html.formother.class.php');
dol_include_once('/core/lib/product.lib.php');
dol_include_once('/nomenclature/class/nomenclature.class.php');
dol_include_once('/commande/class/commande.class.php');
dol_include_once('/comm/propal/class/propal.class.php');
dol_include_once('/product/class/html.formproduct.class.php');
dol_include_once('/nomenclature/lib/nomenclature.lib.php');
require_once DOL_DOCUMENT_ROOT . '/core/class/html.form.class.php';
require_once DOL_DOCUMENT_ROOT . '/compta/facture/class/facture.class.php';
if(!empty($conf->workstationatm->enabled)) {
dol_include_once('/workstationatm/class/workstation.class.php');
}
$hookmanager->initHooks(array('nomenclaturecard'));
$newToken = function_exists('newToken') ? newToken() : $_SESSION['newtoken'];
$langs->load("stocks");
$langs->load("products");
$langs->load("nomenclature@nomenclature");
$product = new Product($db);
$fk_product = GETPOST('fk_product', 'int');
$product_ref = GETPOST('ref', 'alpha');
if ($fk_product || $product_ref) $product->fetch($fk_product, $product_ref);
$qty_ref = (float)GETPOST('qty_ref', 'int'); // il s'agit de la qty de la ligne de document, si vide alors il faudra utiliser qty_reference de la nomenclature
$action= GETPOST('action', 'alpha');
$PDOdb=new TPDOdb;
$fk_object= (int)GETPOST('fk_object', 'int');
$fk_nomenclature= (int)GETPOST('fk_nomenclature', 'int');
$object_type = GETPOST('object_type', 'none');
$fk_origin = GETPOST('fk_origin', 'int');
$disableAnchorRedirection = GETPOST('disableAnchorRedirection', 'none');
if(empty($object_type)) {
$object_type='product';
$fk_object = $product->id;
}
if(! empty($object_type)) {
$class = ucfirst($object_type);
$object = new $class($db);
if(! empty($fk_origin)) $object->fetch($fk_origin);
}
/*
* Actions
*/
$parameters = array(
'object_type' => $object_type,
'fk_object' => $fk_object,
'fk_nomenclature' => $fk_nomenclature,
);
$reshook = $hookmanager->executeHooks('doActions', $parameters, $product, $action); // Note that $action and $object may have been modified by some hooks
if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
if (empty($reshook))
{
if($action==='delete_nomenclature') {
$n=new TNomenclature;
$n->load($PDOdb, (int)GETPOST('fk_nomenclature', 'int'));
$n->delete($PDOdb);
setEventMessage('NomenclatureDeleted');
}
else if($action==='clone_nomenclature') {
cloneNomenclatureFromProduct($PDOdb, GETPOST('fk_product_clone', 'int'), $fk_object, $object_type);
}
else if($action==='add_nomenclature') {
$n=new TNomenclature;
$n->set_values($_REQUEST);
$res = $n->save($PDOdb);
if($res>0){
setEventMessage($langs->trans('BomAdded'));
}
else{
setEventMessage($langs->trans('BomAddError'), 'errors');
}
}
else if($action==='add_fk_nomenclature') {
//TODO ajouter les enfants de la nomenclature passé en post à la nomenclature courrante
}
else if($action === 'delete_nomenclature_detail') {
$n=new TNomenclature;
$n->load($PDOdb, $fk_nomenclature);
if($n->getId()>0) {
$n->TNomenclatureDet[GETPOST('k', 'none')]->to_delete = true;
$n->save($PDOdb);
}
}
else if($action === 'delete_ws') {
$n=new TNomenclature;
// $PDOdb->debug = true;
$n->load($PDOdb, $fk_nomenclature);
if($n->getId()>0) {
$k = (int)GETPOST('k', 'int');
//var_dump( $fk_nomenclature,$k,$n->TNomenclatureWorkstation);
$n->TNomenclatureWorkstation[$k]->to_delete = true;
$n->save($PDOdb);
}
}
else if($action==='save_nomenclature') {
if (GETPOST('clone_nomenclature', 'none'))
{
$n=new TNomenclature;
$n->load($PDOdb, $fk_nomenclature);
$n->delete($PDOdb);
$res = cloneNomenclatureFromProduct($PDOdb, (int)GETPOST('fk_clone_from_product', 'int'), $fk_object, $object_type);
}
else
{
$anchorTag = '';
$n=new TNomenclature;
if($fk_nomenclature>0) $n->load($PDOdb, $fk_nomenclature, false, $product->id , $qty_ref, $object_type, $fk_origin);
else $n->loadByObjectId($PDOdb, $fk_object, $object_type,true, $product->id, $qty_ref, $fk_origin); // si pas de fk_nomenclature, alors on provient d'un document, donc $qty_ref tjr passé en param
//Cas où on sauvegarde depuis une ligne et qu'il faut dupliquer la nomenclature
if(!$n->iExist && GETPOST('type_object', 'none')!='product') {
$n->reinit();
}
//vérification titre déjà existant
if(getDolGlobalInt('NOMENCLATURE_UNIQUE_TITLE') && TNomenclature::getRightToSaveTitle(GETPOST('title', 'alphanohtml')) <= 0 && !empty(GETPOST('title', 'alphanohtml'))) {
//si déjà existant on donne au $_POST le titre précédent pour ne pas que le nouveau soit enregistré
$_POST['title'] = $n->title ;
setEventMessage('NomenclatureTitleWarning', 'warnings');
}
$n->set_values($_POST);
$n->is_default = (int)GETPOST('is_default', 'int');
if($n->is_default>0) TNomenclature::resetDefaultNomenclature($PDOdb, $n->fk_object, $n->object_type);
//Cas ou l'on déplace une ligne
if(!empty($_POST['TNomenclature'])) {
// Réorganisation des clefs du tableau au cas où l'odre a été changé par déplacement des lignes
$tab = array();
foreach($_POST['TNomenclature'] as $val) $tab[] = $val;
foreach($tab as $k=>$TDetValues) {
if(empty($n->TNomenclatureDet[$k])) $n->TNomenclatureDet[$k] = new TNomenclatureDet;
$n->TNomenclatureDet[$k]->set_values($TDetValues);
if(isset($_POST['TNomenclature_'.$k.'_workstations'])) {
$n->TNomenclatureDet[$k]->workstations = implode(',', $_POST['TNomenclature_'.$k.'_workstations']);
}
}
}
if(!empty($_POST['TNomenclatureWorkstation'])) {
foreach($_POST['TNomenclatureWorkstation'] as $k=>$TDetValues) {
if(!empty($n->TNomenclatureWorkstation[$k])) $n->TNomenclatureWorkstation[$k]->set_values($TDetValues);
}
}
//Cas ou l'on ajoute un produit dans la nomenclature
$fk_new_product = GETPOST('fk_new_product_'.$n->getId(), 'none');
$fk_new_product_qty = GETPOST('fk_new_product_qty_'.$n->getId(), 'none');
if(GETPOST('add_nomenclature', 'none') && $fk_new_product>0) {
$last_det = end($n->TNomenclatureDet);
if(($last_det === false || $last_det === true) && !empty($last_det) && empty($last_det->rowid)) $last_det->rowid = 0;
$url = dol_buildpath('nomenclature/nomenclature.php', 2).'?fk_product='.$n->fk_object.'&fk_nomenclature='.$n->getId().'#line_'.(intval($last_det->rowid));
$res = $n->addProduct($PDOdb, $fk_new_product, $fk_new_product_qty);
if(empty($res)) {
$p_err= new Product($db);
$p_err->fetch($fk_new_product);
setEventMessage($langs->trans('ThisProductCreateAnInfinitLoop').' '.$p_err->getNomUrl(0),'errors');
header("location: ".$url, true);
exit;
} elseif ($n->object_type === 'product') {
$last_det = end($n->TNomenclatureDet);
$url = dol_buildpath('nomenclature/nomenclature.php', 2).'?fk_product='.$n->fk_object.'&fk_nomenclature='.$n->getId().'#line_'.(intval($last_det->rowid));
if(empty($disableAnchorRedirection)){
header("location: ".$url, true);
exit;
}
}
}
//Cas où l'on ajoute un nouveau poste à charge
$fk_new_workstation = GETPOST('fk_new_workstation', 'none');
if(GETPOST('add_workstation', 'none') && $fk_new_workstation>0 ) {
$k = $n->addChild($PDOdb, 'TNomenclatureWorkstation');
$det = &$n->TNomenclatureWorkstation[$k];
/** @var TNomenclatureWorkstation $det */
$det->fk_workstation = $fk_new_workstation;
$det->rang = $k+1;
$anchorTag = '#nomenclature-ws-item-k-'.$k; // pas le choix de passer par k car n'est pas encore enregistré
}
// prevent multiple event from ajax call
if ( getDolGlobalInt('NOMENCLATURE_CLOSE_ON_APPLY_NOMENCLATURE_PRICE') &&
GETPOST('apply_nomenclature_price', 'alphanohtml')){
print '<script type="text/javascript">
let dialog = document.getElementById("dialog-nomenclature");
if (dialog) {
$("#dialog-nomenclature").dialog("close");
let url = new URL(window.location.href);
url.searchParams.set("cache", new Date().getTime());
window.location.href = url.toString();
} else {
console.error("Dialog element not found");
}
</script>';
setEventMessage($langs->trans('NomenclatureSaved'));
}
if(empty($_SESSION['dol_events']['mesgs']) && !getDolGlobalInt('NOMENCLATURE_CLOSE_ON_APPLY_NOMENCLATURE_PRICE')) {
setEventMessage($langs->trans('NomenclatureSaved'));
}
//Mise à jour des prix de la nomenclature
$n->setPrice($PDOdb,$qty_ref,$n->fk_object,$n->object_type, $fk_origin);
$n->save($PDOdb);
// Fait l'update du PA et PU de la ligne si nécessaire
_updateObjectLine($n, $object_type, $fk_object, (int)GETPOST('fk_origin', 'int'), GETPOST('apply_nomenclature_price', 'none'));
if(empty($disableAnchorRedirection)){
header("Location: ".$_SERVER["PHP_SELF"].'?fk_product='.intval($fk_product)."&fk_nomenclature=".$n->id.$anchorTag);
exit;
}
}
}
else if ($action == 'confirm_create_stock' && getDolGlobalInt('NOMENCLATURE_ALLOW_MVT_STOCK_FROM_NOMEN'))
{
$fk_nomenclature_used = GETPOST('fk_nomenclature_used', 'int');
$fk_warehouse_to_make = GETPOST('fk_warehouse_to_make', 'int');
$fk_warehouse_needed = GETPOST('fk_warehouse_needed', 'int');
$qty = GETPOST('nomenclature_qty_to_create', 'int');
$use_subbom = GETPOST('use_subbom', 'int');
$n = new TNomenclature;
$n->load($PDOdb, $fk_nomenclature_used);
$res = $n->addMvtStock($qty, $fk_warehouse_to_make, $fk_warehouse_needed, $use_subbom);
if ($res < 0)
{
setEventMessages('', $n->errors, 'errors');
header('Location: '.dol_buildpath('/nomenclature/nomenclature.php', 1).'?fk_product='.$product->id.'&action=create_stock&fk_nomenclature_used='.$n->getId().'&qty_reference='.$qty.'&fk_warehouse_to_make='.$fk_warehouse_to_make.'&fk_warehouse_needed='.$fk_warehouse_needed);
exit;
}
else
{
setEventMessage($langs->trans('NomenclatureMvtOk'));
header('Location: '.dol_buildpath('/nomenclature/nomenclature.php', 1).'?fk_product='.$product->id);
exit;
}
}
}
if($object_type != 'product') {
$langs->load('nomenclature@nomenclature');
$origin_object_id = (int)GETPOST('fk_origin', 'int');
$n=new TNomenclature;
$n->loadByObjectId($PDOdb,$fk_object, $object_type, false, $product->id, $qty_ref, $origin_object_id);
$readonly = $object->statut > 0;
_fiche_nomenclature($PDOdb, $n, $product, $object, $fk_object, $object_type, $qty_ref, $readonly);
print '<script type="text/javascript" src="'.dol_buildpath('nomenclature/js/searchproductcategory.js.php',1).'"></script>';
}
else{
_show_product_nomenclature($PDOdb, $product, $object);
}
$db->close();
/**
* @param TPDOdb $PDOdb
* @param Product $product
* @param CommonObject $object
*/
function _show_product_nomenclature(&$PDOdb, &$product, &$object) {
global $user, $langs, $db, $conf;
llxHeader('',$langs->trans('Nomenclature'));
$newToken = function_exists('newToken') ? newToken() : $_SESSION['newtoken'];
$form = new Form($db);
$formconfirm = getFormConfirmNomenclature($form, $product, GETPOST('fk_nomenclature_used', 'none'), GETPOST('action', 'alpha'), GETPOST('qty_reference', 'none'));
if (!empty($formconfirm)) echo $formconfirm;
$head=product_prepare_head($product, $user);
$titre=$langs->trans('Nomenclature');
$picto=($product->type==1?'service':'product');
dol_fiche_head($head, 'nomenclature', $titre, 0, $picto);
if ((float) DOL_VERSION >= 4.0) dol_banner_tab($product, 'ref', '', (!empty($user->societe_id)?0:1), 'ref');
else headerProduct($product);
?><script type="text/javascript">
function uncheckOther(obj)
{
$("input[name=is_default]").not($(obj)).prop("checked", false);
}
function deleteNomenc(fk_nomenclature) {
if(window.confirm('Vous-êtes sûr ?')) {
document.location.href="?action=delete_nomenclature&fk_product=<?php echo $product->id; ?>&fk_nomenclature="+fk_nomenclature+"&token=<?php echo $newToken; ?>"
}
}
$(document).ready(function() {
$("input[name=clone_nomenclature]").click(function() {
document.location.href="?action=clone_nomenclature&fk_product=<?php echo $product->id; ?>&fk_product_clone="+$("#fk_clone_from_product").val();
});
// Pas le choix à cause de l'accordéon le hash par en vrille du coup un petit set timeout et c'est bon
setTimeout(function () {
if(window.location.hash){
var hash = window.location.hash;
$('html, body').animate({
scrollTop: $(hash).offset().top -80
}, 300, 'swing');
}
}, 500);
});
</script><?php
$TNomenclature = TNomenclature::get($PDOdb, $product->id);
if (GETPOST('optioncss', 'none') !== 'print')
{
?>
<div class="tabsAction">
<div class="inline-block divButAction">
<a href="?action=add_nomenclature&fk_product=<?php echo $product->id ?>&fk_object=<?php echo $product->id ?>&token=<?php echo $newToken ?>" class="butAction"><?php echo $langs->trans('AddNomenclature'); ?></a>
</div>
<?php
//$form=new Form($db);
//print $form->select_produits('', 'fk_clone_from_product', '', 0);
$htmlname = 'fk_clone_from_product';
$urloption='htmlname='.$htmlname.'&outjson=1&price_level=0&type=&mode=1&status=1&finished=2';
print ajax_autocompleter('', $htmlname, dol_buildpath('/nomenclature/ajax/products.php', 1), $urloption, getDolGlobalString('PRODUIT_USE_SEARCH_TO_SELECT',''), 0, array());
print $langs->trans("RefOrLabel").' : ';
print '<input type="text" size="20" name="search_'.$htmlname.'" id="search_'.$htmlname.'" value="" '.getDolGlobalString('NOMENCLATURE_DISABLE_AUTOFOCUS','autofocus').' />';
?>
<div class="inline-block divButAction">
<input id="nomenclature_bt_clone_nomenclature" type="button" name="clone_nomenclature" class="butAction" value="<?php echo $langs->trans('CloneNomenclatureFromProduct'); ?>" />
</div>
</div>
<?php
}
if (GETPOST('optioncss', 'none') !== 'print') print '<div class="accordion" >';
else print '<div class="no-accordion">';
$accordeonActiveIndex = 'false';
$idion = 0;
foreach($TNomenclature as $iN => &$n) {
// open if edited
$fk_nomenclature=(int)GETPOST('fk_nomenclature', 'int');
// default open
if(!empty($n->is_default) && empty($fk_nomenclature)){
$accordeonActiveIndex = $idion;
}
if(!empty($fk_nomenclature) && $fk_nomenclature == $n->id){ $accordeonActiveIndex = $idion; }
$idion++;
// On passe par là depuis l'onglet "Ouvrage" d'un produit, du coup il faut passer la qty_reference de la nomenclature
_fiche_nomenclature($PDOdb, $n, $product, $object, $product->id, 'product', $n->qty_reference);
}
if(count($TNomenclature) === 1){ $accordeonActiveIndex = 0 ;}
print '</div>';
print '<script>$( function() { $( ".accordion" ).accordion({header: ".accordion-title", collapsible: true, active:'.$accordeonActiveIndex.'}); } );</script>';
print '<script type="text/javascript" src="'.dol_buildpath('nomenclature/js/searchproductcategory.js.php',1).'"></script>';
$liste = new TListviewTBS('listeUse');
$sql="SELECT n.fk_object as 'Id', n.fk_object, nd.qty
FROM ".MAIN_DB_PREFIX."nomenclaturedet nd
LEFT JOIN ".MAIN_DB_PREFIX."nomenclature n ON (n.rowid=nd.fk_nomenclature)
WHERE nd.fk_product=".$product->id." AND n.object_type='product'";
echo $liste->render($PDOdb, $sql, array(
'limit'=>array(
'nbLine'=>'30'
)
,'type'=>array(
'qty'=>'number'
)
,'link'=>array(
'Id'=>'<a href="'.dol_buildpath('/nomenclature/nomenclature.php?fk_product=@val@',1).'&token=' . $newToken . '">'.img_picto($langs->trans('Nomenclature'),'object_list').' '.$langs->trans('Nomenclature').'</a>'
)
,'liste'=>array(
'titre'=>$langs->trans('ListUseNomenclaure')
,'image'=>img_picto('','title.png', '', 0)
,'picto_precedent'=>img_picto('','back.png', '', 0)
,'picto_suivant'=>img_picto('','next.png', '', 0)
,'messa geNothing'=>$langs->trans('NoUseInNomenclature')
,'picto_search'=>img_picto('','search.png', '', 0)
)
,'title'=>array(
'fk_object'=>$langs->trans('product')
,'qty'=>$langs->trans('Qty')
,'Id'=>$langs->trans('Nomenclature')
)
,'eval'=>array(
'fk_object' => 'get_format_libelle_produit(@fk_object@)'
)
)
);
dol_fiche_end();
llxFooter();
}
/**
* @param int $fk_product
* @return string
*/
function get_format_libelle_produit($fk_product = null) {
global $db;
if (!empty($fk_product)) {
$product = new Product($db);
$product->fetch($fk_product);
$product->ref.=' '.$product->label;
return $product->getNomUrl(1);
} else {
return 'Produit non défini.';
}
}
/**
* @param TPDOdb $PDOdb
* @param TNomenclature $n
* @param Product $product
* @param CommonObject $object
* @param int $fk_object
* @param string $object_type
* @param int $qty_ref
* @param bool $readonly
*/
function _fiche_nomenclature(&$PDOdb, &$n,&$product, &$object, $fk_object=0, $object_type='product', $qty_ref=1, $readonly=false) {
global $langs, $conf, $db, $user, $hookmanager;
$coef_qty_price = $n->setPrice($PDOdb,$qty_ref,$fk_object,$object_type,(int)GETPOST('fk_origin', 'int'));
$newToken = function_exists('newToken') ? newToken() : $_SESSION['newtoken'];
print '<h3 class="accordion-title">';
print $langs->trans('Nomenclature').' n°'.$n->getId();
print ' '.$n->title;
print ' - '.$langs->trans('nomenclatureQtyReference').' '. $n->qty_reference;
$price_buy = $n->getBuyPrice(); // prix d'achat total
print ' - '.$langs->trans('TotalAmountCostWithCharge').' '. price($price_buy);
$price_to_sell = $n->getSellPrice($qty_ref); // prix de vente conseillé total
print ' - '.$langs->trans('PriceConseil').' '. price($price_to_sell*$qty_ref);
if (GETPOST('json', 'none') == 1 && $n->non_secable) print ' ('.$langs->trans('nomenclatureNonSecableForQty', $n->qty_reference).')';
print '</h3>';
print '<div id="nomenclature'.$n->id.'" class="tabBar accordion-body">';
$json = GETPOST('json', 'int');
$form=new Form($db);
if($n->getId() == 0 && count($n->TNomenclatureDet)+count($n->TNomenclatureWorkstation)>0) {
echo '<div class="error">'.$langs->trans('NonLocalNomenclature').'</div>';
}
$pAction = $n->getId() ? $_SERVER['PHP_SELF'].'?fk_product='.$n->fk_object : 'auto';
$formCore=new TFormCore($pAction, 'form_nom_'.$n->getId(), 'post', false);
if ($readonly) {
$formCore->Set_typeaff('view'); // $formCore methods will return read-only elements instead of form inputs
} else {
echo $formCore->hidden('action', 'save_nomenclature');
echo $formCore->hidden('json', $json);
echo $formCore->hidden('fk_nomenclature', $n->getId());
echo $formCore->hidden('fk_product', $product->id);
echo $formCore->hidden('fk_object', $fk_object);
echo $formCore->hidden('object_type', $object_type);
echo $formCore->hidden('fk_origin', GETPOST('fk_origin', 'int'));
echo $formCore->hidden('qty_ref', $qty_ref);
if ($json) echo $formCore->hidden('non_secable', $n->non_secable);
}
$TCoef = TNomenclatureCoefObject::loadCoefObject($PDOdb, $object, $object->element);
$TCoefFinal = TNomenclatureCoef::loadCoef($PDOdb, 'pricefinal');
?>
<script type="text/javascript">
$(document).ready(function() {
$(".det-table>tbody").sortable({
handle:".handler"
,placeholder: "ui-state-highlight"
,stop:function(event,ui) {
var sorted = $(this).sortable( "toArray", { attribute: "rowid" } );
$.ajax({
url:"<?php echo dol_buildpath('/nomenclature/script/interface.php',1) ?>"
,data:{
put:"rang"
,type:'det'
,TRank:sorted
}
});
}
});
$(".workstation-table>tbody").sortable({
handle:".handler"
,placeholder: "ui-state-highlight"
,stop:function(event,ui) {
var sorted = $(this).sortable( "toArray", { attribute: "rowid" } );
$.ajax({
url:"<?php echo dol_buildpath('/nomenclature/script/interface.php',1) ?>"
,data:{
put:"rang"
,type:'ws'
,TRank:sorted
}
,success: function(data) {
//console.log('Sort');
$('.workstation-table tr[rowid]').each(function(i, elem) {
$(elem).find('input, textarea').each(function(j, child) {
var name = $(child).prop('name').replace(/^(TNomenclatureWorkstation\[)([0-9]+)(\].*)/, '$1'+i+'$3');
$(child).prop('id', name).prop('name', name);
//console.log(name);
});
});
}
});
}
});
<?php if(getDolGlobalInt('NOMENCLATURE_ALLOW_USE_MANUAL_COEF')) { ?>
// Récupération des coef
TCoef = {<?php foreach($TCoef as $obj_coef) echo '"'.$obj_coef->code_type.'":'.$obj_coef->tx.','; ?>};
$('.select_coef').change(function() {
$(this).parent('td').find('input[type="text"]').val(TCoef[$(this).val()]);
});
<?php }?>
// Récupération du coeffinal
$('.select_coef_final').change(function() {
var code_type = $(this).val();
$.ajax({
url: "<?php echo dol_buildpath('/nomenclature/script/interface.php', 1) ?>"
, data: {
put: 'set-marge-final'
, code_type: code_type
, nomenclature_id: '<?php echo $n->rowid; ?>'
}
, dataType: "json"
});
});
<?php if(getDolGlobalInt('NOMENCLATURE_USE_LOSS_PERCENT')) { ?>
$("input[name*=qty_base], input[name*=loss_percent]").change(function() {
if(typeof $('input[name*=qty_base]').attr('name') !== 'undefined') {
var line = $(this).closest('tr');
var val_qty_base = $(line).find("input[name*=qty_base]").val();
var val_loss_percent = $(line).find("input[name*=loss_percent]").val();
var newQty = val_qty_base * ( 1 + val_loss_percent / 100 );
newQty = Math.round(newQty*100)/100;
<?php if(getDolGlobalInt('MAIN_MAX_DECIMALS_UNIT')) { ?>
var maxdecimal = <?php echo getDolGlobalInt('MAIN_MAX_DECIMALS_UNIT') ?>; //on arrondit la nouvelle quantité en fonction du nombre de décimal autorisé par la conf
newQty = newQty.toFixed(maxdecimal);
<?php } ?>
$(this).closest('tr').find('td.ligne_col_qty').find("input[name*=qty]").val(newQty);
}
});
<?php } ?>
});
</script>
<table class="liste" width="100%" id="nomenclature-<?php echo $n->getId(); ?>"><?php
if($object_type == 'product') {
?><tr class="liste_titre">
<td class="liste_titre"><?php echo $langs->trans('Nomenclature').' '.$langs->trans('numberShort').$n->getId(); ?></td>
<td class="liste_titre"><?php echo $formCore->texte($langs->trans('Title'), 'title', $n->title, 50,255); ?></td>
<td class="liste_titre"><?php echo $formCore->texte($langs->trans('nomenclatureQtyReference'), 'qty_reference', $n->qty_reference, 5,10); ?></td>
<td class="liste_titre"><?php echo $formCore->checkbox('', 'non_secable', array(1 => $langs->trans('nomenclatureNonSecable').' '.img_help(1, $langs->trans('nomenclatureNonSecableHelp'))), $n->non_secable); ?></td>
<td align="right" class="liste_titre"><?php echo $formCore->checkbox('', 'is_default', array(1 => $langs->trans('nomenclatureIsDefault')), $n->is_default, 'onclick="javascript:uncheckOther(this);"') ?></td>
<td align="right" class="liste_titre"><?php if (!$readonly) { ?><a href="javascript:deleteNomenc(<?php echo $n->getId(); ?>)"><?php echo img_delete($langs->trans('DeleteThisNomenclature')) ?></a><?php } ?></td>
</tr><?php
}
?>
<tr>
<td colspan="6">
<?php
$TNomenclatureDet = &$n->TNomenclatureDet;
if(count($TNomenclatureDet)>0) {
?>
<table width="100%" class="liste det-table">
<thead>
<tr class="liste_titre">
<th class="liste_titre col_product" width="55%"><?php echo $langs->trans('Product'); ?></th>
<?php if(getDolGlobalInt('PRODUCT_USE_UNITS')) { ?> <th class="liste_titre col_fk_unit" width="5%"><?php echo $langs->trans('Unit'); ?></th> <?php } ?>
<?php
if(getDolGlobalInt('FOURN_PRODUCT_AVAILABILITY'))
{
print '<th class="liste_titre" width="5%">'.$langs->trans('Availability').'</th>';
}
if(!empty($conf->stock->enabled) && !getDolGlobalInt('NOMENCLATURE_HIDE_STOCK_COLUMNS')) {
?>
<th class="liste_titre col_physicalStock" width="5%"><?php echo $langs->trans('PhysicalStock'); ?></th>
<th class="liste_titre col_virtualStock" width="5%"><?php echo $langs->trans('VirtualStock'); ?></th>
<?php } ?>
<?php if(getDolGlobalInt('NOMENCLATURE_USE_LOSS_PERCENT')) { ?>
<th class="liste_titre col_qty_base" width="5%"><?php echo $langs->trans('qty_base'); ?></th>
<th class="liste_titre col_loss_percent" width="5%"><?php echo $langs->trans('LossPercent'); ?></th>
<?php } ?>
<th class="liste_titre col_qty" width="5%"><?php echo $langs->trans('Qty'); ?></th>
<?php if(getDolGlobalInt('NOMENCLATURE_USE_CUSTOM_BUYPRICE')) { ?> <th class="liste_titre col_buy_price" width="5%"><?php echo $langs->trans('BuyingPriceCustom'); ?></th> <?php } ?>
<?php if($user->hasRight('nomenclature', 'showPrice')) { ?>
<th class="liste_titre col_amountCostUnit" align="right" width="5%"><?php echo $langs->trans('AmountCostUnit'); ?></th>
<th class="liste_titre col_amountCost" align="right" width="5%"><?php echo $langs->trans('AmountCost'); ?></th>
<th class="liste_titre col_type" width="5%"><?php echo $langs->trans('CoefCharge'); ?></th>
<th class="liste_titre col_amountCostWithChargeUnit" align="right" width="5%"><?php echo $langs->trans('AmountCostWithChargeUnit'); ?></th>
<th class="liste_titre col_amountCostWithCharge" align="right" width="5%"><?php echo $langs->trans('AmountCostWithCharge'); ?></th>
<?php if(getDolGlobalInt('NOMENCLATURE_USE_COEF_ON_COUT_REVIENT')) { ?>
<th class="liste_titre col_coef2" width="5%"><?php echo $langs->trans('CoefMarge'); ?></th>
<th class="liste_titre col_coef2" width="5%"><?php echo $langs->trans('PV'); ?></th>
<?php } ?>
<th class="liste_titre col_amountCostWithChargeCustom" align="right" width="5%"><?php echo $langs->trans('AmountCostWithChargeCustom'); ?></th><?php
}
?>
<th class="liste_titre" width="1%"> </th>
<th class="liste_titre" width="1%"> </th>
</tr>
</thead>
<tbody>
<?php
//Chaque tableau de coef a pour key le rowid du coef
$total_charge =$coldisplay= 0;
$class='';$total_produit = $total_mo = 0;
foreach($TNomenclatureDet as $k=>&$det) {
$class = ($class == 'impair') ? 'pair' : 'impair';
?>
<tr class="<?php echo $class ?>" rowid="<?php echo $det->getId(); ?>" id="line_<?php echo $det->getId(); ?>">
<td><?php
$p_nomdet = new Product($db);
if ($det->fk_product>0 && $p_nomdet->fetch($det->fk_product)>0)
{
echo $p_nomdet->getNomUrl(1).' '.$p_nomdet->label;
if($p_nomdet->load_stock() < 0) $p_nomdet->load_virtual_stock(); // TODO AA pourquoi ? load_stock le fait et s'il échoue... :/
} elseif ($readonly) {
echo $det->title;
} else {
echo '<input type="text" value="'.$det->title.'" name="TNomenclature['.$k.'][title]" />';
}
$sub_n = _draw_child_arbo($PDOdb, $p_nomdet->id, $det->qty);
if ($readonly) echo '<div class="note_private">';
echo $formCore->zonetexte('', 'TNomenclature['.$k.'][note_private]', $det->note_private, 80, 1,' style="width:95%;"');
if ($readonly) echo '</div>';
if($det->fk_product > 0) print '<input type="hidden" name="TNomenclature['.$k.'][fk_product]" value="'.$det->fk_product.'"/>';
if(getDolGlobalInt('NOMENCLATURE_ALLOW_TO_LINK_PRODUCT_TO_WORKSTATION')) {
if(empty($TWorkstationToSelect)) {
$TWorkstationToSelect=array();
foreach($n->TNomenclatureWorkstation as &$wsn) {
$TWorkstationToSelect[$wsn->workstation->id] = $wsn->workstation->name;
}
}
echo $form->multiselectarray('TNomenclature_'.$k.'_workstations', $TWorkstationToSelect,(empty($det->workstations) ? array() : explode(',', $det->workstations)),0,0,'minwidth300' );
}
?></td>
<?php if(getDolGlobalInt('PRODUCT_USE_UNITS')) { ?>
<td class="ligne_col_fk_unit nowrap" ><?php
// To display warning message if product haven't the same unit as bom
$det->productCurrentUnit = $object->getValueFrom('c_units', $p_nomdet->fk_unit, 'label');
$det->warningUnitNotTheSameAsProduct = ($det->fk_unit != $p_nomdet->fk_unit);
if(getDolGlobalInt('NOMENCLATURE_ALLOW_SELECT_FOR_PRODUCT_UNIT') || !empty($det->warningUnitNotTheSameAsProduct)){
echo $form->selectUnits($det->fk_unit, 'TNomenclature['.$k.'][fk_unit]', 1);
}
else {
// On copie l'unité de la ligne dans l'objet produit pour utiliser la fonction getLabelOfUnit()
$original_fk_unit = $p_nomdet->fk_unit;
$p_nomdet->fk_unit = $det->fk_unit;
print ucfirst($langs->trans($p_nomdet->getLabelOfUnit()));
// On remet l'unité de base du produit au cas où
$p_nomdet->fk_unit = $original_fk_unit;
}
if($det->warningUnitNotTheSameAsProduct){
$unitTitle = $langs->trans('WarningUnitOfBomIsNotTheSameAsProduct', $langs->trans($det->productCurrentUnit));
echo '<span class="badge badge-danger classfortooltip" title="'.$unitTitle.'" ><span class="fa fa-warning"></span></span>';
}
?></td>
<?php }
if(getDolGlobalInt('FOURN_PRODUCT_AVAILABILITY'))
{
// On récupère le dernier tarif fournisseur pour ce produit
if( $p_nomdet->id>0) {
$q = 'SELECT fk_availability
FROM '.MAIN_DB_PREFIX.'product_fournisseur_price
WHERE fk_product = '.(int) $p_nomdet->id.' AND fk_availability > 0 ORDER BY rowid DESC LIMIT 1';
$resql = $db->query($q);
$res = $db->fetch_object($resql);
$fk_availability = $res->fk_availability;
}
else {
$fk_availability=0;
}
echo '<td>';
if($fk_availability > 0) {
$form->load_cache_availability();
$availability=$form->cache_availability[$fk_availability]['label'];
echo $availability;
$availability='';
}
echo '</td>';
}
if(!empty($conf->stock->enabled) && !getDolGlobalInt('NOMENCLATURE_HIDE_STOCK_COLUMNS')) {
?>
<td>
<?php echo $det->fk_product>0 ? price($p_nomdet->stock_reel,'',0,1,1,2) : '-'; ?>
</td>
<td class="ligne_col_virtualStock">
<?php
if(!empty($conf->of->enabled) && $p_nomdet->id>0){
// On récupère les quantités dans les OF
$q = 'SELECT ofl.qty, ofl.qty_needed, ofl.qty, ofl.type
FROM '.MAIN_DB_PREFIX.'assetOf `of`
INNER JOIN '.MAIN_DB_PREFIX.'assetOf_line ofl ON(ofl.fk_assetOf = `of`.rowid)
WHERE fk_product = '.$p_nomdet->id.' AND `of`.status NOT IN("DRAFT","CLOSE")';
$resql = $db->query($q);
if($resql){
// On régule le stock théorique en fonction de ces quantités
while($res = $db->fetch_object($resql)) {
if($res->type === 'TO_MAKE') $p_nomdet->stock_theorique += $res->qty; // Pour les TO_MAKE la bonne qté est dans le champ qty
elseif($res->type === 'NEEDED') $p_nomdet->stock_theorique -= empty($res->qty_needed) ? $res->qty : $res->qty_needed;
}
}
else{
print $db->lasterror();
}
}
echo !empty($det->fk_product) ? price($p_nomdet->stock_theorique,'',0,1,1,2) : '-';
?>
</td>
<?php }
if(getDolGlobalInt('NOMENCLATURE_USE_LOSS_PERCENT')) {
echo '<td class="ligne_col_qty_base" nowrap>'.$formCore->texte('', 'TNomenclature['.$k.'][qty_base]', $det->qty_base, 2,100, '').'</td>';
echo '<td nowrap>'.$formCore->texte('', 'TNomenclature['.$k.'][loss_percent]', $det->loss_percent, 2,100).'%</td>';
}
?>
<td class="ligne_col_qty"><?php
echo $formCore->texte('', 'TNomenclature['.$k.'][qty]', $det->qty, 7,100);
if($coef_qty_price != 1) echo '<br /> x '.price($coef_qty_price,'','',2,2) ;
?></td>
<?php
if(getDolGlobalInt('NOMENCLATURE_USE_CUSTOM_BUYPRICE')) {
$c = new TNomenclature();
//Conf active and Product has a nomenclature
if (getDolGlobalInt('NOMENCLATURE_TAKE_PRICE_FROM_CHILD_FIRST') && $c->loadByObjectId($PDOdb, $n->TNomenclatureDet[$k]->fk_product, 'product')){
echo '<td nowrap>';
echo $formCore->texte('', 'TNomenclature['.$k.'][buying_price]',empty($det->buying_price) ? '' : $det->buying_price, 7,100);
echo '</td>';
}
else{
?><td nowrap>
<select id="TNomenclature[<?php echo $k; ?>][fk_fournprice]" name="TNomenclature[<?php echo $k; ?>][fk_fournprice]" class="flat"></select><?php
echo $formCore->texte('', 'TNomenclature['.$k.'][buying_price]', empty($det->buying_price) ? '' : $det->buying_price, 7,100);
echo '</td>';
$det->printSelectProductFournisseurPrice($k, $n->rowid, $n->object_type);
}
}
if($user->hasRight('nomenclature', 'showPrice')) {
$price = $det->calculate_price; //Si on arrondit cette valeur l'affichage de la colonne prix d'achat unitaire est fausse
$price_charge = $det->charged_price;
print '<td class="col_amountCostUnit" >';
if(!empty($det->qty)){
if (
getDolGlobalInt('NOMENCLATURE_APPLY_FULL_COST_NON_SECABLE')
&& is_object($sub_n)
&& $sub_n->non_secable
)
{
echo price(round($price / $sub_n->qty_reference, 2));
}
else echo price(round($price / $det->qty, 2));
}
print '</td>';
echo '<td align="right" valign="middle">';
if(getDolGlobalInt('NOMENCLATURE_ACTIVATE_DETAILS_COSTS')) {
echo price( $price ).img_help(1,$langs->trans('PricePA'));
echo '<span class="pricePMP"><br />'.price(price2num($det->calculate_price_pmp,'MT')).img_help(1,$langs->trans('PricePMP')).'</span>';
if(!empty($conf->of->enabled)) echo '<span class="priceOF"><br />'.price(price2num($det->calculate_price_of,'MT')).img_help(1,$langs->trans('PriceOF')).'</span>';
}
else{
echo price($price);
}
echo '</td>';
}
?>
<td nowrap><?php echo $formCore->combo('', 'TNomenclature['.$k.'][code_type]', TNomenclatureDet::getTType($PDOdb), $det->code_type, 1, '', '', 'select_coef'); ?>
<?php if(getDolGlobalInt('NOMENCLATURE_ALLOW_USE_MANUAL_COEF')) {
echo $formCore->texte('', 'TNomenclature['.$k.'][tx_custom]', empty($det->tx_custom) ? $TCoef[$det->code_type]->tx : $det->tx_custom, 3,100);
} ?>
</td>
<?php
print '<td class="col_amountCostWithChargeUnit" >';
echo $det->qty>0?price(round($price_charge/$det->qty, 2)) : '';
print '</td>';
echo '<td class="col_amountCostWithCharge" align="right" valign="middle">';
if(getDolGlobalInt('NOMENCLATURE_ACTIVATE_DETAILS_COSTS')) {
echo price($price_charge);
echo '<span class="pricePMP"><br />'.price(price2num($det->charged_price_pmp,'MT')).'</span>';
if(!empty($conf->of->enabled)) echo '<span class="priceOF"><br />'.price(price2num($det->charged_price_of,'MT')).'</span>';
}
else{
echo price($price_charge);
}
echo '</td>';
if(getDolGlobalInt('NOMENCLATURE_USE_COEF_ON_COUT_REVIENT')) { ?>
<td nowrap><?php echo $formCore->combo('', 'TNomenclature['.$k.'][code_type2]', TNomenclatureDet::getTType($PDOdb), $det->code_type2, 1, '', '', 'select_coef'); ?>
<?php if(getDolGlobalInt('NOMENCLATURE_ALLOW_USE_MANUAL_COEF')) {
echo $formCore->texte('', 'TNomenclature['.$k.'][tx_custom2]', empty($det->tx_custom2) && !empty($TCoef[$det->code_type2]->tx)? $TCoef[$det->code_type2]->tx : $det->tx_custom2, 3,100);
} ?>
</td>
<?php
$pv = price2num($det->pv,'MT');
echo '<td align="right" valign="middle">';
if(getDolGlobalInt('NOMENCLATURE_ACTIVATE_DETAILS_COSTS')) {
echo price( $pv).img_help(1,$langs->trans('PricePA'));
echo '<span class="pricePMP"><br />'.price(price2num($det->pv_pmp,'MT')).img_help(1,$langs->trans('PricePMP')).'</span>';
if(!empty($conf->of->enabled)) echo '<span class="priceOF"><br />'.price(price2num($det->pv_of,'MT')).img_help(1,$langs->trans('PriceOF')).'</span>';
}
else{
echo price($pv);
}
echo '</td>';
echo '</td>';
}
echo '<td align="right" valign="middle">';
if ($readonly) {
echo price($det->price);
} else {
echo '<input style="text-align:right;" name="TNomenclature[' . $k . '][price]" value="' . price($det->price) . '" size="5" />';
}
echo '</td>';
?>
<td><?php
if($n->getId()>0)
{
$param = '?action=delete_nomenclature_detail&k='.$k.'&fk_nomenclature='.$n->getId().'&fk_product='.$product->id.'&fk_object='.$fk_object;
$param.= '&object_type='.$object_type.'&qty_ref='.$qty_ref.'&fk_origin='.GETPOST('fk_origin', 'int').'&json='.$json . '&token='. $newToken ;
// Si la nomenclature a été enregistré puis que les lignes ont été delete, alors l'icone de suppression ne doit pas s'afficher car ce sont les lignes chargé depuis le load_original()
if (! empty($n->iExist) && !$readonly) echo '<a href="'.dol_buildpath('/nomenclature/nomenclature.php',1).$param.'" class="tojs">'.img_delete().'</a>';
}
?></td>
<td align="center" class="linecolmove tdlineupdown">
<?php
$coldisplay++;
if(empty($det->id)) $det->id = 0;
if(!$readonly) { ?>
<a class="lineupdown handler" href="<?php echo $_SERVER["PHP_SELF"].'?fk_product='.$product->id.'&action=up&rowid='.$det->id; ?>&token=<?php echo $newToken; ?>">
<?php echo img_picto('Move','grip'); ?>
</a>
<?php } ?>
</td>
</tr>
<?php
}
?></tbody><tfoot><?php
if($user->hasRight('nomenclature', 'showPrice')) {
$colspan = 3;
if(getDolGlobalInt('FOURN_PRODUCT_AVAILABILITY') && getDolGlobalInt('FOURN_PRODUCT_AVAILABILITY') > 0) $colspan ++;
if(empty($conf->stock->enabled)) $colspan -= 2;
if(getDolGlobalInt('PRODUCT_USE_UNITS')) $colspan ++;