/
Stocks.php
1455 lines (1313 loc) · 58.3 KB
/
Stocks.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
/* $Id: Stocks.php 7112 2015-01-31 18:10:59Z rchacon $ */
include('includes/session.inc');
$Title = _('Item Maintenance');
/* webERP manual links before header.inc */
$ViewTopic= 'Inventory';
$BookMark = 'InventoryAddingItems';
include('includes/header.inc');
include('includes/SQL_CommonFunctions.inc');
/*If this form is called with the StockID then it is assumed that the stock item is to be modified */
if (isset($_GET['StockID'])){
$StockID =trim(mb_strtoupper($_GET['StockID']));
} elseif (isset($_POST['StockID'])){
$StockID =trim(mb_strtoupper($_POST['StockID']));
} else {
$StockID = '';
}
$ItemDescriptionLanguagesArray = explode(',',$_SESSION['ItemDescriptionLanguages']);//WARNING: if the last character is a ",", there are n+1 languages.
if (isset($_POST['NextItem'])){
$Result = DB_query("SELECT stockid FROM stockmaster WHERE stockid>'" . $StockID . "' ORDER BY stockid ASC LIMIT 1");
$NextItemRow = DB_fetch_row($Result);
$StockID = $NextItemRow[0];
foreach ($ItemDescriptionLanguagesArray as $LanguageId) {
unset($_POST['Description_' . str_replace('.','_',$LanguageId)]);
}
}
if (isset($_POST['PreviousItem'])){
$Result = DB_query("SELECT stockid FROM stockmaster WHERE stockid<'" . $StockID . "' ORDER BY stockid DESC LIMIT 1");
$PreviousItemRow = DB_fetch_row($Result);
$StockID = $PreviousItemRow[0];
foreach ($ItemDescriptionLanguagesArray as $LanguageId) {
unset($_POST['Description_' . str_replace('.','_',$LanguageId)]);
}
}
if (isset($StockID) AND !isset($_POST['UpdateCategories'])) {
$sql = "SELECT COUNT(stockid)
FROM stockmaster
WHERE stockid='".$StockID."'
GROUP BY stockid";
$result = DB_query($sql);
$myrow = DB_fetch_row($result);
if ($myrow[0]==0) {
$New=1;
} else {
$New=0;
}
}
if (isset($_POST['New'])) {
$New=$_POST['New'];
}
echo '<a href="' . $RootPath . '/SelectProduct.php">' . _('Back to Items') . '</a>
<br />
<p class="page_title_text">
<img src="'.$RootPath.'/css/'.$Theme.'/images/inventory.png" title="' . _('Stock') . '" alt="" />' . ' ' . $Title . '
</p>';
if (isset($_FILES['ItemPicture']) AND $_FILES['ItemPicture']['name'] !='') {
$result = $_FILES['ItemPicture']['error'];
$UploadTheFile = 'Yes'; //Assume all is well to start off with
$filename = $_SESSION['part_pics_dir'] . '/' . $StockID . '.jpg';
//But check for the worst
if (mb_strtoupper(mb_substr(trim($_FILES['ItemPicture']['name']),mb_strlen($_FILES['ItemPicture']['name'])-3))!='JPG'){
prnMsg(_('Only jpg files are supported - a file extension of .jpg is expected'),'warn');
$UploadTheFile ='No';
} elseif ( $_FILES['ItemPicture']['size'] > ($_SESSION['MaxImageSize']*1024)) { //File Size Check
prnMsg(_('The file size is over the maximum allowed. The maximum size allowed in KB is') . ' ' . $_SESSION['MaxImageSize'],'warn');
$UploadTheFile ='No';
} elseif ( $_FILES['ItemPicture']['type'] == 'text/plain' ) { //File Type Check
prnMsg( _('Only graphics files can be uploaded'),'warn');
$UploadTheFile ='No';
} elseif ( $_FILES['ItemPicture']['error'] == 6 ) { //upload temp directory check
prnMsg( _('No tmp directory set. You must have a tmp directory set in your PHP for upload of files. '),'warn');
$UploadTheFile ='No';
} elseif (file_exists($filename)){
prnMsg(_('Attempting to overwrite an existing item image'),'warn');
$result = unlink($filename);
if (!$result){
prnMsg(_('The existing image could not be removed'),'error');
$UploadTheFile ='No';
}
}
if ($UploadTheFile=='Yes'){
$result = move_uploaded_file($_FILES['ItemPicture']['tmp_name'], $filename);
$message = ($result)?_('File url') . '<a href="' . $filename .'">' . $filename . '</a>' : _('Something is wrong with uploading a file');
}
}
if (isset($Errors)) {
unset($Errors);
}
$Errors = array();
$InputError = 0;
if (isset($_POST['submit'])) {
//initialise no input errors assumed initially before we test
/* actions to take once the user has clicked the submit button
ie the page has called itself with some user input */
//first off validate inputs sensible
$i=1;
if (!isset($_POST['Description']) OR mb_strlen($_POST['Description']) > 50 OR mb_strlen($_POST['Description'])==0) {
$InputError = 1;
prnMsg (_('The stock item description must be entered and be fifty characters or less long') . '. ' . _('It cannot be a zero length string either') . ' - ' . _('a description is required'),'error');
$Errors[$i] = 'Description';
$i++;
}
if (mb_strlen($_POST['LongDescription'])==0) {
$InputError = 1;
prnMsg (_('The stock item description cannot be a zero length string') . ' - ' . _('a long description is required'),'error');
$Errors[$i] = 'LongDescription';
$i++;
}
if (mb_strlen($StockID) ==0) {
$InputError = 1;
prnMsg (_('The Stock Item code cannot be empty'),'error');
$Errors[$i] = 'StockID';
$i++;
}
if (ContainsIllegalCharacters($StockID) OR mb_strpos($StockID,' ')) {
$InputError = 1;
prnMsg(_('The stock item code cannot contain any of the following characters') . " - ' & + \" \\ ." . _('or a space'),'error');
$Errors[$i] = 'StockID';
$i++;
$StockID='';
}
if (mb_strlen($_POST['Units']) >20) {
$InputError = 1;
prnMsg(_('The unit of measure must be 20 characters or less long'),'error');
$Errors[$i] = 'Units';
$i++;
}
if (mb_strlen($_POST['BarCode']) >20) {
$InputError = 1;
prnMsg(_('The barcode must be 20 characters or less long'),'error');
$Errors[$i] = 'BarCode';
$i++;
}
if (!is_numeric(filter_number_format($_POST['Volume']))) {
$InputError = 1;
prnMsg (_('The volume of the packaged item in cubic metres must be numeric') ,'error');
$Errors[$i] = 'Volume';
$i++;
}
if (filter_number_format($_POST['Volume']) <0) {
$InputError = 1;
prnMsg(_('The volume of the packaged item must be a positive number'),'error');
$Errors[$i] = 'Volume';
$i++;
}
if (!is_numeric(filter_number_format($_POST['GrossWeight']))) {
$InputError = 1;
prnMsg(_('The weight of the packaged item in Gross Weight must be numeric'),'error');
$Errors[$i] = 'GrossWeight';
$i++;
}
if (filter_number_format($_POST['GrossWeight'])<0) {
$InputError = 1;
prnMsg(_('The weight of the packaged item must be a positive number'),'error');
$Errors[$i] = 'GrossWeight';
$i++;
}
if (!is_numeric(filter_number_format($_POST['NetWeight']))) {
$InputError = 1;
prnMsg(_('The net weight of the item in Net Weight must be numeric'),'error');
$Errors[$i] = 'NetWeight';
$i++;
}
if (filter_number_format($_POST['NetWeight'])<0) {
$InputError = 1;
prnMsg(_('The net weight of the item must be a positive number'),'error');
$Errors[$i] = 'NetWeight';
$i++;
}
if (!is_numeric(filter_number_format($_POST['EOQ']))) {
$InputError = 1;
prnMsg(_('The economic order quantity must be numeric'),'error');
$Errors[$i] = 'EOQ';
$i++;
}
if (filter_number_format($_POST['EOQ']) <0) {
$InputError = 1;
prnMsg (_('The economic order quantity must be a positive number'),'error');
$Errors[$i] = 'EOQ';
$i++;
}
if ($_POST['Controlled']==0 AND $_POST['Serialised']==1){
$InputError = 1;
prnMsg(_('The item can only be serialised if there is lot control enabled already') . '. ' . _('Batch control') . ' - ' . _('with any number of items in a lot/bundle/roll is enabled when controlled is enabled') . '. ' . _('Serialised control requires that only one item is in the batch') . '. ' . _('For serialised control') . ', ' . _('both controlled and serialised must be enabled'),'error');
$Errors[$i] = 'Serialised';
$i++;
}
if ($_POST['NextSerialNo']!=0 AND $_POST['Serialised']==0){
$InputError = 1;
prnMsg(_('The item can only have automatically generated serial numbers if it is a serialised item'),'error');
$Errors[$i] = 'NextSerialNo';
$i++;
}
if ($_POST['NextSerialNo']!=0 AND $_POST['MBFlag']!='M'){
$InputError = 1;
prnMsg(_('The item can only have automatically generated serial numbers if it is a manufactured item'),'error');
$Errors[$i] = 'NextSerialNo';
$i++;
}
if (($_POST['MBFlag']=='A'
OR $_POST['MBFlag']=='K'
OR $_POST['MBFlag']=='D'
OR $_POST['MBFlag']=='G')
AND $_POST['Controlled']==1){
$InputError = 1;
prnMsg(_('Assembly/Kitset/Phantom/Service/Labour items cannot also be controlled items') . '. ' . _('Assemblies/Dummies/Phantom and Kitsets are not physical items and batch/serial control is therefore not appropriate'),'error');
$Errors[$i] = 'Controlled';
$i++;
}
if (trim($_POST['CategoryID'])==''){
$InputError = 1;
prnMsg(_('There are no inventory categories defined. All inventory items must belong to a valid inventory category,'),'error');
$Errors[$i] = 'CategoryID';
$i++;
}
if (!is_numeric(filter_number_format($_POST['Pansize']))) {
$InputError = 1;
prnMsg(_('Pansize quantity must be numeric'),'error');
$Errors[$i] = 'Pansize';
$i++;
}
if (!is_numeric(filter_number_format($_POST['ShrinkFactor']))) {
$InputError = 1;
prnMsg(_('Shrinkage factor quantity must be numeric'),'error');
$Errors[$i] = 'ShrinkFactor';
$i++;
}
if ($InputError !=1){
if ($_POST['Serialised']==1){ /*Not appropriate to have several dp on serial items */
$_POST['DecimalPlaces']=0;
}
if ($New==0) { /*so its an existing one */
/*first check on the changes being made we must disallow:
- changes from manufactured or purchased to Service, Assembly or Kitset if there is stock - changes from manufactured, kitset or assembly where a BOM exists
*/
$sql = "SELECT mbflag,
controlled,
serialised,
materialcost+labourcost+overheadcost AS itemcost,
stockcategory.stockact,
stockcategory.wipact,
description,
longdescription
FROM stockmaster
INNER JOIN stockcategory
ON stockmaster.categoryid=stockcategory.categoryid
WHERE stockid = '".$StockID."'";
$MBFlagResult = DB_query($sql);
$myrow = DB_fetch_row($MBFlagResult);
$OldMBFlag = $myrow[0];
$OldControlled = $myrow[1];
$OldSerialised = $myrow[2];
$UnitCost = $myrow[3];
$OldStockAccount = $myrow[4];
$OldWIPAccount = $myrow[5];
$OldDescription = $myrow[6];
$OldLongDescription = $myrow[7];
$sql = "SELECT SUM(locstock.quantity)
FROM locstock
WHERE stockid='".$StockID."'
GROUP BY stockid";
$result = DB_query($sql);
$StockQtyRow = DB_fetch_row($result);
/*Now check the GL account of the new category to see if it is different to the old stock gl account */
$result = DB_query("SELECT stockact,
wipact
FROM stockcategory
WHERE categoryid='" . $_POST['CategoryID'] . "'");
$NewStockActRow = DB_fetch_array($result);
$NewStockAct = $NewStockActRow['stockact'];
$NewWIPAct = $NewStockActRow['wipact'];
if ($OldMBFlag != $_POST['MBFlag']){
if (($OldMBFlag == 'M' OR $OldMBFlag=='B') AND ($_POST['MBFlag']=='A' OR $_POST['MBFlag']=='K' OR $_POST['MBFlag']=='D' OR $_POST['MBFlag']=='G')){ /*then need to check that there is no stock holding first */
/* stock holding OK for phantom (ghost) items */
if ($StockQtyRow[0]!=0 AND $OldMBFlag!='G'){
$InputError=1;
prnMsg( _('The make or buy flag cannot be changed from') . ' ' . $OldMBFlag . ' ' . _('to') . ' ' . $_POST['MBFlag'] . ' ' . _('where there is a quantity of stock on hand at any location') . '. ' . _('Currently there are') . ' ' . $StockQtyRow[0] . ' ' . _('on hand') , 'errror');
}
/* don't allow controlled/serialized */
if ($_POST['Controlled']==1){
$InputError=1;
prnMsg( _('The make or buy flag cannot be changed from') . ' ' . $OldMBFlag . ' ' . _('to') . ' ' . $_POST['MBFlag'] . ' ' . _('where the item is to be lot controlled') . '. ' . _('Kitset, phantom, dummy and assembly items cannot be lot controlled'), 'error');
}
}
/*now check that if the item is being changed to a kitset, there are no items on sales orders or purchase orders*/
if ($_POST['MBFlag']=='K') {
$sql = "SELECT quantity-qtyinvoiced
FROM salesorderdetails
WHERE stkcode = '".$StockID."'
AND completed=0";
$result = DB_query($sql);
$ChkSalesOrds = DB_fetch_row($result);
if ($ChkSalesOrds[0]!=0){
$InputError = 1;
prnMsg( _('The make or buy flag cannot be changed to a kitset where there is a quantity outstanding to be delivered on sales orders') . '. ' . _('Currently there are') .' ' . $ChkSalesOrds[0] . ' '. _('outstanding'), 'error');
}
}
/*now check that if it is to be a kitset or assembly or dummy there is no quantity on purchase orders outstanding*/
if ($_POST['MBFlag']=='K' OR $_POST['MBFlag']=='A' OR $_POST['MBFlag']=='D') {
$sql = "SELECT quantityord-quantityrecd
FROM purchorderdetails INNER JOIN purchorders
ON purchorders.orderno=purchorderdetails.orderno
WHERE itemcode = '".$StockID."'
AND purchorderdetails.completed=0
AND purchorders.status<>'Cancelled'
AND purchorders.status<>'Completed'
AND purchorders.status<>'Rejected'";
$result = DB_query($sql);
$ChkPurchOrds = DB_fetch_row($result);
if ($ChkPurchOrds[0]!=0){
$InputError = 1;
prnMsg( _('The make or buy flag cannot be changed to'). ' ' . $_POST['MBFlag'] . ' '. _('where there is a quantity outstanding to be received on purchase orders') . '. ' . _('Currently there are'). ' ' . $ChkPurchOrds[0] . ' '. _('yet to be received'). 'error');
}
}
/*now check that if it was a Manufactured, Kitset, Phantom or Assembly and is being changed to a purchased or dummy - that no BOM exists */
if (($OldMBFlag=='M' OR $OldMBFlag =='K' OR $OldMBFlag=='A' OR $OldMBFlag=='G') AND ($_POST['MBFlag']=='B' OR $_POST['MBFlag']=='D')) {
$sql = "SELECT COUNT(*)
FROM bom
WHERE parent = '".$StockID."'
GROUP BY parent";
$result = DB_query($sql);
$ChkBOM = DB_fetch_row($result);
if ($ChkBOM[0]!=0){
$InputError = 1;
prnMsg( _('The make or buy flag cannot be changed from manufactured, kitset or assembly to'). ' ' . $_POST['MBFlag'] . ' '. _('where there is a bill of material set up for the item') . '. ' . _('Bills of material are not appropriate for purchased or dummy items'), 'error');
}
}
/*now check that if it was Manufac, Phantom or Purchased and is being changed to assembly or kitset, it is not a component on an existing BOM */
if (($OldMBFlag=='M' OR $OldMBFlag =='B' OR $OldMBFlag=='D' OR $OldMBFlag=='G') AND ($_POST['MBFlag']=='A' OR $_POST['MBFlag']=='K')) {
$sql = "SELECT COUNT(*)
FROM bom
WHERE component = '".$StockID."'
GROUP BY component";
$result = DB_query($sql);
$ChkBOM = DB_fetch_row($result);
if ($ChkBOM[0]!=0){
$InputError = 1;
prnMsg( _('The make or buy flag cannot be changed from manufactured, purchased or dummy to a kitset or assembly where the item is a component in a bill of material') . '. ' . _('Assembly and kitset items are not appropriate as components in a bill of materials'), 'error');
}
}
}
/* Do some checks for changes in the Serial & Controlled setups */
if ($OldControlled != $_POST['Controlled'] AND $StockQtyRow[0]!=0){
$InputError=1;
prnMsg( _('You can not change a Non-Controlled Item to Controlled (or back from Controlled to non-controlled when there is currently stock on hand for the item') , 'error');
}
if ($OldSerialised != $_POST['Serialised'] AND $StockQtyRow[0]!=0){
$InputError=1;
prnMsg( _('You can not change a Serialised Item to Non-Serialised (or vice-versa) when there is a quantity on hand for the item') , 'error');
}
/* Do some check for property input */
for ($i=0;$i<$_POST['PropertyCounter'];$i++){
if ($_POST['PropNumeric' .$i]==1){
if ( filter_number_format($_POST['PropValue' . $i]) < $_POST['PropMin' . $i] OR filter_number_format($_POST['PropValue' . $i]) > $_POST['PropMax' . $i]){
$InputError = 1;
prnMsg(_('The property value should between') . ' '.$_POST['PropMin' . $i] . ' ' . _('and') . $_POST['PropMax' . $i],'error');
}
}
}
if ($InputError == 0){
DB_Txn_Begin();
$sql = "UPDATE stockmaster
SET longdescription='" . $_POST['LongDescription'] . "',
description='" . $_POST['Description'] . "',
discontinued='" . $_POST['Discontinued'] . "',
controlled='" . $_POST['Controlled'] . "',
serialised='" . $_POST['Serialised']."',
perishable='" . $_POST['Perishable']."',
categoryid='" . $_POST['CategoryID'] . "',
units='" . $_POST['Units'] . "',
mbflag='" . $_POST['MBFlag'] . "',
eoq='" . filter_number_format($_POST['EOQ']) . "',
volume='" . filter_number_format($_POST['Volume']) . "',
grossweight='" . filter_number_format($_POST['GrossWeight']) . "',
netweight='" . filter_number_format($_POST['NetWeight']) . "',
barcode='" . $_POST['BarCode'] . "',
discountcategory='" . $_POST['DiscountCategory'] . "',
taxcatid='" . $_POST['TaxCat'] . "',
decimalplaces='" . $_POST['DecimalPlaces'] . "',
shrinkfactor='" . filter_number_format($_POST['ShrinkFactor']) . "',
pansize='" . filter_number_format($_POST['Pansize']) . "',
nextserialno='" . $_POST['NextSerialNo'] . "'
WHERE stockid='".$StockID."'";
$ErrMsg = _('The stock item could not be updated because');
$DbgMsg = _('The SQL that was used to update the stock item and failed was');
$result = DB_query($sql,$ErrMsg,$DbgMsg,true);
$ErrMsg = _('Could not update the language description because');
$DbgMsg = _('The SQL that was used to update the language description and failed was');
if (count($ItemDescriptionLanguagesArray)>0){
foreach ($ItemDescriptionLanguagesArray as $LanguageId) {
if ($LanguageId != ''){
$result = DB_query("DELETE FROM stockdescriptiontranslations WHERE stockid='" . $StockID . "' AND language_id='" . $LanguageId . "'", $ErrMsg, $DbgMsg, true);
$result = DB_query("INSERT INTO stockdescriptiontranslations (stockid,
language_id,
descriptiontranslation,
longdescriptiontranslation)
VALUES('" . $StockID . "','" .
$LanguageId . "', '" .
$_POST['Description_' . str_replace('.','_',$LanguageId)] . "', '" .
$_POST['LongDescription_' . str_replace('.','_',$LanguageId)].
"')",$ErrMsg,$DbgMsg,true);
}
}
/*
foreach ($ItemDescriptionLanguagesArray as $LanguageId) {
$DescriptionTranslation = $_POST['Description_' . str_replace('.', '_', $LanguageId)];
//WARNING: It DOES NOT update if database row DOES NOT exist.
$sql = "UPDATE stockdescriptiontranslations " .
"SET descriptiontranslation='" . $DescriptionTranslation . "' " .
"WHERE stockid='" . $StockID . "' AND (language_id='" . $LanguageId. "')";
$result = DB_query($sql, $ErrMsg, $DbgMsg, true);
}
*/
}
/* Activate the needs revision flag for translations for modified descriptions */
if ($OldDescription != $_POST['Description'] OR $OldLongDescription != $_POST['LongDescription']){
$sql = "UPDATE stockdescriptiontranslations
SET needsrevision = '0'
WHERE stockid='".$StockID."'";
$ErrMsg = _('The stock description translations could not be updated because');
$DbgMsg = _('The SQL that was used to set the flag for translation revision failed was');
$result = DB_query($sql,$ErrMsg,$DbgMsg,true);
}
//delete any properties for the item no longer relevant with the change of category
$result = DB_query("DELETE FROM stockitemproperties WHERE stockid ='" . $StockID . "'",$ErrMsg, $DbgMsg, true);
//now insert any item properties
for ($i=0;$i<$_POST['PropertyCounter'];$i++){
if ($_POST['PropType' . $i] ==2){
if ($_POST['PropValue' . $i]=='on'){
$_POST['PropValue' . $i]=1;
} else {
$_POST['PropValue' . $i]=0;
}
}
if ($_POST['PropNumeric' .$i]==1){
$_POST['PropValue' . $i]=filter_number_format($_POST['PropValue' . $i]);
} else {
$_POST['PropValue' . $i]=$_POST['PropValue' . $i];
}
$result = DB_query("INSERT INTO stockitemproperties (stockid,
stkcatpropid,
value)
VALUES ('" . $StockID . "',
'" . $_POST['PropID' . $i] . "',
'" . $_POST['PropValue' . $i] . "')",
$ErrMsg,$DbgMsg,true);
} //end of loop around properties defined for the category
if ($OldStockAccount != $NewStockAct AND $_SESSION['CompanyRecord']['gllink_stock']==1) {
/*Then we need to make a journal to transfer the cost to the new stock account */
$JournalNo = GetNextTransNo(0,$db); //enter as a journal
$SQL = "INSERT INTO gltrans (type,
typeno,
trandate,
periodno,
account,
narrative,
amount)
VALUES ( 0,
'" . $JournalNo . "',
'" . Date('Y-m-d') . "',
'" . GetPeriod(Date($_SESSION['DefaultDateFormat']),$db,true) . "',
'" . $NewStockAct . "',
'" . $StockID . ' ' . _('Change stock category') . "',
'" . ($UnitCost* $StockQtyRow[0]) . "')";
$ErrMsg = _('The stock cost journal could not be inserted because');
$DbgMsg = _('The SQL that was used to create the stock cost journal and failed was');
$result = DB_query($SQL, $ErrMsg, $DbgMsg,true);
$SQL = "INSERT INTO gltrans (type,
typeno,
trandate,
periodno,
account,
narrative,
amount)
VALUES ( 0,
'" . $JournalNo . "',
'" . Date('Y-m-d') . "',
'" . GetPeriod(Date($_SESSION['DefaultDateFormat']),$db,true) . "',
'" . $OldStockAccount . "',
'" . $StockID . ' ' . _('Change stock category') . "',
'" . (-$UnitCost* $StockQtyRow[0]) . "')";
$result = DB_query($SQL, $ErrMsg, $DbgMsg,true);
} /* end if the stock category changed and forced a change in stock cost account */
if ($OldWIPAccount != $NewWIPAct AND $_SESSION['CompanyRecord']['gllink_stock']==1) {
/*Then we need to make a journal to transfer the cost of WIP to the new WIP account */
/*First get the total cost of WIP for this category */
$WOCostsResult = DB_query("SELECT workorders.costissued,
SUM(woitems.qtyreqd * woitems.stdcost) AS costrecd
FROM woitems INNER JOIN workorders
ON woitems.wo = workorders.wo
INNER JOIN stockmaster
ON woitems.stockid=stockmaster.stockid
WHERE stockmaster.stockid='". $StockID . "'
AND workorders.closed=0
GROUP BY workorders.costissued",
_('Error retrieving value of finished goods received and cost issued against work orders for this item'));
$WIPValue = 0;
while ($WIPRow=DB_fetch_array($WOCostsResult)){
$WIPValue += ($WIPRow['costissued']-$WIPRow['costrecd']);
}
if ($WIPValue !=0){
$JournalNo = GetNextTransNo(0,$db); //enter as a journal
$SQL = "INSERT INTO gltrans (type,
typeno,
trandate,
periodno,
account,
narrative,
amount)
VALUES ( 0,
'" . $JournalNo . "',
'" . Date('Y-m-d') . "',
'" . GetPeriod(Date($_SESSION['DefaultDateFormat']),$db,true) . "',
'" . $NewWIPAct . "',
'" . $StockID . ' ' . _('Change stock category') . "',
'" . $WIPValue . "')";
$ErrMsg = _('The WIP cost journal could not be inserted because');
$DbgMsg = _('The SQL that was used to create the WIP cost journal and failed was');
$result = DB_query($SQL, $ErrMsg, $DbgMsg,true);
$SQL = "INSERT INTO gltrans (type,
typeno,
trandate,
periodno,
account,
narrative,
amount)
VALUES ( 0,
'" . $JournalNo . "',
'" . Date('Y-m-d') . "',
'" . GetPeriod(Date($_SESSION['DefaultDateFormat']),$db,true) . "',
'" . $OldWIPAccount . "',
'" . $StockID . ' ' . _('Change stock category') . "',
'" . (-$WIPValue) . "')";
$result = DB_query($SQL, $ErrMsg, $DbgMsg,true);
}
} /* end if the stock category changed and forced a change in WIP account */
DB_Txn_Commit();
prnMsg( _('Stock Item') . ' ' . $StockID . ' ' . _('has been updated'), 'success');
echo '<br />';
}
} else { //it is a NEW part
//but lets be really sure here
$result = DB_query("SELECT stockid
FROM stockmaster
WHERE stockid='" . $StockID ."'");
if (DB_num_rows($result)==1){
prnMsg(_('The stock code entered is actually already in the database - duplicate stock codes are prohibited by the system. Try choosing an alternative stock code'),'error');
$InputError = 1;
$Errors[$i] = 'StockID';
$i++;
} else {
DB_Txn_Begin();
$sql = "INSERT INTO stockmaster (stockid,
description,
longdescription,
categoryid,
units,
mbflag,
eoq,
discontinued,
controlled,
serialised,
perishable,
volume,
grossweight,
netweight,
barcode,
discountcategory,
taxcatid,
decimalplaces,
shrinkfactor,
pansize)
VALUES ('".$StockID."',
'" . $_POST['Description'] . "',
'" . $_POST['LongDescription'] . "',
'" . $_POST['CategoryID'] . "',
'" . $_POST['Units'] . "',
'" . $_POST['MBFlag'] . "',
'" . filter_number_format($_POST['EOQ']) . "',
'" . $_POST['Discontinued'] . "',
'" . $_POST['Controlled'] . "',
'" . $_POST['Serialised']. "',
'" . $_POST['Perishable']. "',
'" . filter_number_format($_POST['Volume']) . "',
'" . filter_number_format($_POST['GrossWeight']) . "',
'" . filter_number_format($_POST['NetWeight']) . "',
'" . $_POST['BarCode'] . "',
'" . $_POST['DiscountCategory'] . "',
'" . $_POST['TaxCat'] . "',
'" . $_POST['DecimalPlaces']. "',
'" . filter_number_format($_POST['ShrinkFactor']) . "',
'" . filter_number_format($_POST['Pansize']) . "')";
$ErrMsg = _('The item could not be added because');
$DbgMsg = _('The SQL that was used to add the item failed was');
$result = DB_query($sql, $ErrMsg, $DbgMsg,'',true);
if (DB_error_no() ==0) {
//now insert the language descriptions
$ErrMsg = _('Could not update the language description because');
$DbgMsg = _('The SQL that was used to update the language description and failed was');
if (count($ItemDescriptionLanguages)>0){
foreach ($ItemDescriptionLanguagesArray as $LanguageId) {
if ($LanguageId != '' AND $_POST['Description_' . str_replace('.','_',$LanguageId)] != ''){
$result = DB_query("INSERT INTO stockdescriptiontranslations (stockid,
language_id,
descriptiontranslation,
longdescriptiontranslation)
VALUES('" . $StockID . "','" .
$LanguageId . "', '" .
$_POST['Description_' . str_replace('.','_',$LanguageId)] . "', '" .
$_POST['longDescription_' . str_replace('.','_',$LanguageId)].
"')",$ErrMsg,$DbgMsg,true);
}
}
}
//now insert any item properties
for ($i=0;$i<$_POST['PropertyCounter'];$i++){
if ($_POST['PropType' . $i] ==2){
if ($_POST['PropValue' . $i]=='on'){
$_POST['PropValue' . $i]=1;
} else {
$_POST['PropValue' . $i]=0;
}
}
if ($_POST['PropNumeric' .$i]==1){
$_POST['PropValue' . $i]=filter_number_format($_POST['PropValue' . $i]);
} else {
$_POST['PropValue' . $i]=$_POST['PropValue' . $i];
}
$result = DB_query("INSERT INTO stockitemproperties (stockid,
stkcatpropid,
value)
VALUES ('" . $StockID . "',
'" . $_POST['PropID' . $i] . "',
'" . $_POST['PropValue' . $i] . "')",
$ErrMsg,$DbgMsg,true);
} //end of loop around properties defined for the category
//Add data to locstock
$sql = "INSERT INTO locstock (loccode,
stockid)
SELECT locations.loccode,
'" . $StockID . "'
FROM locations";
$ErrMsg = _('The locations for the item') . ' ' . $StockID . ' ' . _('could not be added because');
$DbgMsg = _('NB Locations records can be added by opening the utility page') . ' <i>Z_MakeStockLocns.php</i> ' . _('The SQL that was used to add the location records that failed was');
$InsResult = DB_query($sql,$ErrMsg,$DbgMsg,true);
DB_Txn_Commit();
if (DB_error_no() ==0) {
prnMsg( _('New Item') .' ' . '<a href="SelectProduct.php?StockID=' . $StockID . '">' . $StockID . '</a> '. _('has been added to the database') .
'<br />' . _('NB: The item cost and pricing must also be setup') .
'<br />' . '<a target="_blank" href="StockCostUpdate.php?StockID=' . $StockID . '">' . _('Enter Item Cost') . '</a>
<br />' . '<a target="_blank" href="Prices.php?Item=' . $StockID . '">' . _('Enter Item Prices') . '</a> ','success');
echo '<br />';
unset($_POST['Description']);
unset($_POST['LongDescription']);
unset($_POST['EOQ']);
// Leave Category ID set for ease of batch entry
// unset($_POST['CategoryID']);
unset($_POST['Units']);
unset($_POST['MBFlag']);
unset($_POST['Discontinued']);
unset($_POST['Controlled']);
unset($_POST['Serialised']);
unset($_POST['Perishable']);
unset($_POST['Volume']);
unset($_POST['GrossWeight']);
unset($_POST['NetWeight']);
unset($_POST['BarCode']);
unset($_POST['ReorderLevel']);
unset($_POST['DiscountCategory']);
unset($_POST['DecimalPlaces']);
unset($_POST['ShrinkFactor']);
unset($_POST['Pansize']);
unset($StockID);
foreach ($ItemDescriptionLanguagesArray as $LanguageId) {
unset($_POST['Description_' . str_replace('.','_',$LanguageId)]);
}
$New=1;
}//ALL WORKED SO RESET THE FORM VARIABLES
}//THE INSERT OF THE NEW CODE WORKED SO BANG IN THE STOCK LOCATION RECORDS TOO
}//END CHECK FOR ALREADY EXISTING ITEM OF THE SAME CODE
}
} else {
echo '<br />' . "\n";
prnMsg( _('Validation failed, no updates or deletes took place'), 'error');
}
} elseif (isset($_POST['delete']) AND mb_strlen($_POST['delete']) >1 ) {
//the button to delete a selected record was clicked instead of the submit button
$CancelDelete = 0;
// PREVENT DELETES IF DEPENDENT RECORDS IN 'StockMoves'
$sql= "SELECT COUNT(*) FROM stockmoves WHERE stockid='".$StockID."' GROUP BY stockid";
$result = DB_query($sql);
$myrow = DB_fetch_row($result);
if ($myrow[0]>0) {
$CancelDelete = 1;
prnMsg( _('Cannot delete this stock item because there are stock movements that refer to this item'),'warn');
echo '<br />' . _('There are') . ' ' . $myrow[0] . ' ' . _('stock movements that refer to this item');
} else {
$sql= "SELECT COUNT(*) FROM bom WHERE component='".$StockID."' GROUP BY component";
$result = DB_query($sql);
$myrow = DB_fetch_row($result);
if ($myrow[0]>0) {
$CancelDelete = 1;
prnMsg( _('Cannot delete this item record because there are bills of material that require this part as a component'),'warn');
echo '<br />' . _('There are') . ' ' . $myrow[0] . ' ' . _('bills of material that require this part as a component');
} else {
$sql= "SELECT COUNT(*) FROM salesorderdetails WHERE stkcode='".$StockID."' GROUP BY stkcode";
$result = DB_query($sql);
$myrow = DB_fetch_row($result);
if ($myrow[0]>0) {
$CancelDelete = 1;
prnMsg( _('Cannot delete this item record because there are existing sales orders for this part'),'warn');
echo '<br />' . _('There are') . ' ' . $myrow[0] . ' ' . _('sales order items against this part');
} else {
$sql= "SELECT COUNT(*) FROM salesanalysis WHERE stockid='".$StockID."' GROUP BY stockid";
$result = DB_query($sql);
$myrow = DB_fetch_row($result);
if ($myrow[0]>0) {
$CancelDelete = 1;
prnMsg(_('Cannot delete this item because sales analysis records exist for it'),'warn');
echo '<br />' . _('There are') . ' ' . $myrow[0] . ' ' . _('sales analysis records against this part');
} else {
$sql= "SELECT COUNT(*) FROM purchorderdetails WHERE itemcode='".$StockID."' GROUP BY itemcode";
$result = DB_query($sql);
$myrow = DB_fetch_row($result);
if ($myrow[0]>0) {
$CancelDelete = 1;
prnMsg(_('Cannot delete this item because there are existing purchase order items for it'),'warn');
echo '<br />' . _('There are') . ' ' . $myrow[0] . ' ' . _('purchase order item record relating to this part');
} else {
$sql = "SELECT SUM(quantity) AS qoh FROM locstock WHERE stockid='".$StockID."' GROUP BY stockid";
$result = DB_query($sql);
$myrow = DB_fetch_row($result);
if ($myrow[0]!=0) {
$CancelDelete = 1;
prnMsg( _('Cannot delete this item because there is currently some stock on hand'),'warn');
echo '<br />' . _('There are') . ' ' . $myrow[0] . ' ' . _('on hand for this part');
} else {
$sql = "SELECT COUNT(*) FROM offers WHERE stockid='".$StockID."' GROUP BY stockid";
$result = DB_query($sql);
$myrow = DB_fetch_row($result);
if ($myrow[0]!=0) {
$CancelDelete = 1;
prnMsg( _('Cannot delete this item because there are offers for this item'),'warn');
echo '<br />' . _('There are') . ' ' . $myrow[0] . ' ' . _('offers from suppliers for this part');
} else {
$sql = "SELECT COUNT(*) FROM tenderitems WHERE stockid='".$StockID."' GROUP BY stockid";
$result = DB_query($sql);
$myrow = DB_fetch_row($result);
if ($myrow[0]!=0) {
$CancelDelete = 1;
prnMsg( _('Cannot delete this item because there are tenders for this item'),'warn');
echo '<br />' . _('There are') . ' ' . $myrow[0] . ' ' . _('tenders from suppliers for this part');
}
}
}
}
}
}
}
}
if ($CancelDelete==0) {
$result = DB_Txn_Begin();
/*Deletes LocStock records*/
$sql ="DELETE FROM locstock WHERE stockid='".$StockID."'";
$result=DB_query($sql,_('Could not delete the location stock records because'),'',true);
/*Deletes Price records*/
$sql ="DELETE FROM prices WHERE stockid='".$StockID."'";
$result=DB_query($sql,_('Could not delete the prices for this stock record because'),'',true);
/*and cascade deletes in PurchData */
$sql ="DELETE FROM purchdata WHERE stockid='".$StockID."'";
$result=DB_query($sql,_('Could not delete the purchasing data because'),'',true);
/*and cascade delete the bill of material if any */
$sql = "DELETE FROM bom WHERE parent='".$StockID."'";
$result=DB_query($sql,_('Could not delete the bill of material because'),'',true);
//and cascade delete the item properties
$sql="DELETE FROM stockitemproperties WHERE stockid='".$StockID."'";
$result=DB_query($sql, _('Could not delete the item properties'),'',true);
//and cascade delete the item descriptions in other languages
$sql = "DELETE FROM stockdescriptiontranslations WHERE stockid='" . $StockID . "'";
$result=DB_query($sql,_('Could not delete the item language descriptions'),'',true);
$sql="DELETE FROM stockmaster WHERE stockid='".$StockID."'";
$result=DB_query($sql, _('Could not delete the item record'),'',true);
$result = DB_Txn_Commit();
prnMsg(_('Deleted the stock master record for') . ' ' . $StockID . '....' .
'<br />. . ' . _('and all the location stock records set up for the part') .
'<br />. . .' . _('and any bill of material that may have been set up for the part') .
'<br /> . . . .' . _('and any purchasing data that may have been set up for the part') .
'<br /> . . . . .' . _('and any prices that may have been set up for the part'),'success');
echo '<br />';
unset($_POST['LongDescription']);
unset($_POST['Description']);
unset($_POST['EOQ']);
unset($_POST['CategoryID']);
unset($_POST['Units']);
unset($_POST['MBFlag']);
unset($_POST['Discontinued']);
unset($_POST['Controlled']);
unset($_POST['Serialised']);
unset($_POST['Perishable']);
unset($_POST['Volume']);
unset($_POST['GrossWeight']);
unset($_POST['NetWeight']);
unset($_POST['BarCode']);
unset($_POST['ReorderLevel']);
unset($_POST['DiscountCategory']);
unset($_POST['TaxCat']);
unset($_POST['DecimalPlaces']);
unset($_SESSION['SelectedStockItem']);
foreach ($ItemDescriptionLanguagesArray as $LanguageId) {
unset($_POST['Description_' . str_replace('.','_',$LanguageId)]);
}
unset($StockID);
$New=1;
} //end if Delete Part
}
echo '<form name="ItemForm" enctype="multipart/form-data" method="post" action="' . htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') . '">';
echo '<div>';
if (isset($StockID)){
echo '<table width="100%">
<tr>
<td width="5%"><input style="background:url(css/previous.png);width:26px;height:43px;" type="submit" name="PreviousItem" value="" /></td>
<td width="90%"></td>
<td width="5%"><input style="background:url(css/next.png);width:26px;height:43px;" type="submit" name="NextItem" value="" /></td>
</tr>';
}
echo '<input type="hidden" name="FormID" value="' . $_SESSION['FormID'] . '" />
<input type="hidden" name="New" value="'.$New.'" />
<table class="selection">';
if (!isset($StockID) OR $StockID=='' or isset($_POST['UpdateCategories'])) {
/*If the page was called without $StockID passed to page then assume a new stock item is to be entered show a form with a part Code field other wise the form showing the fields with the existing entries against the part will show for editing with only a hidden StockID field. New is set to flag that the page may have called itself and still be entering a new part, in which case the page needs to know not to go looking up details for an existing part*/
if (!isset($StockID)) {
$StockID='';
}
if ($New==1) {
echo '<tr>
<td>' . _('Item Code'). ':</td>
<td><input type="text" ' . (in_array('StockID',$Errors) ? 'class="inputerror"' : '' ) .'" data-type="no-illegal-chars" autofocus="autofocus" required="required" value="'.$StockID.'" name="StockID" size="20" maxlength="20" title ="'._('Input the stock code, the following characters are prohibited:') . ' \' " + . & \\ > <" placeholder="'._('alpha-numeric only').'" /></td>
</tr>';
} else {
echo '<tr>
<td>' . _('Item Code'). ':</td>
<td>' . $StockID . '<input type="hidden" name ="StockID" value="' . $StockID . '" /></td>
</tr>';
}
} elseif (!isset($_POST['UpdateCategories']) AND $InputError!=1) { // Must be modifying an existing item and no changes made yet
$sql = "SELECT stockid,
description,
longdescription,
categoryid,
units,
mbflag,
discontinued,
controlled,
serialised,
perishable,
eoq,
volume,
grossweight,
netweight,
barcode,
discountcategory,
taxcatid,
decimalplaces,
nextserialno,
pansize,
shrinkfactor
FROM stockmaster
WHERE stockid = '".$StockID."'";
$result = DB_query($sql);
$myrow = DB_fetch_array($result);
$_POST['LongDescription'] = $myrow['longdescription'];
$_POST['Description'] = $myrow['description'];
$_POST['EOQ'] = $myrow['eoq'];
$_POST['CategoryID'] = $myrow['categoryid'];
$_POST['Units'] = $myrow['units'];
$_POST['MBFlag'] = $myrow['mbflag'];
$_POST['Discontinued'] = $myrow['discontinued'];
$_POST['Controlled'] = $myrow['controlled'];
$_POST['Serialised'] = $myrow['serialised'];
$_POST['Perishable'] = $myrow['perishable'];
$_POST['Volume'] = $myrow['volume'];
$_POST['GrossWeight'] = $myrow['grossweight'];
$_POST['NetWeight'] = $myrow['netweight'];
$_POST['BarCode'] = $myrow['barcode'];
$_POST['DiscountCategory'] = $myrow['discountcategory'];
$_POST['TaxCat'] = $myrow['taxcatid'];
$_POST['DecimalPlaces'] = $myrow['decimalplaces'];
$_POST['NextSerialNo'] = $myrow['nextserialno'];
$_POST['Pansize'] = $myrow['pansize'];
$_POST['ShrinkFactor'] = $myrow['shrinkfactor'];
$sql = "SELECT descriptiontranslation, longdescriptiontranslation, language_id FROM stockdescriptiontranslations WHERE stockid='" . $StockID . "' AND (";
foreach ($ItemDescriptionLanguagesArray as $LanguageId) {
$sql .= "language_id='" . $LanguageId ."' OR ";
}
$sql = mb_substr($sql,0,mb_strlen($sql)-3) . ')';
$result = DB_query($sql);
while ($myrow = DB_fetch_array($result)){
$_POST['Description_' . str_replace('.','_',$myrow['language_id'])] = $myrow['descriptiontranslation'];
$_POST['LongDescription_' . str_replace('.','_',$myrow['language_id'])] = $myrow['longdescriptiontranslation'];
}
echo '<tr><td>' . _('Item Code') . ':</td>
<td>' . $StockID . '<input type="hidden" name="StockID" value="' . $StockID . '" /></td>
</tr>';
} else { // some changes were made to the data so don't re-set form variables to DB ie the code above
echo '<tr>
<td>' . _('Item Code') . ':</td>
<td>' . $StockID . '<input type="hidden" name="StockID" value="' . $StockID . '" /></td>
</tr>';
}
if (isset($_POST['Description'])) {
$Description = $_POST['Description'];
} else {
$Description ='';
}
echo '<tr>