forked from webERP-team/webERP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SelectCreditItems.php
2006 lines (1696 loc) · 82.3 KB
/
SelectCreditItems.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
/*The credit selection screen uses the Cart class used for the making up orders
some of the variable names refer to order - please think credit when you read order */
include('includes/DefineCartClass.php');
include('includes/DefineSerialItems.php');
/* Session started in session.php for password checking and authorisation level check */
include('includes/session.php');
$Title = _('Create Credit Note');
$ViewTopic= 'ARTransactions';
$BookMark = 'CreateCreditNote';
include('includes/header.php');
include('includes/SQL_CommonFunctions.inc');
include('includes/GetSalesTransGLCodes.inc');
include('includes/GetPrice.inc');
if (empty($_GET['identifier'])) {
/*unique session identifier to ensure that there is no conflict with other order entry sessions on the same machine */
$identifier=date('U');
} else {
$identifier=$_GET['identifier'];
}
if (isset($_POST['ProcessCredit']) AND !isset($_SESSION['CreditItems'.$identifier])){
prnMsg(_('This credit note has already been processed. Refreshing the page will not enter the credit note again') . '<br />' . _('Please use the navigation links provided rather than using the browser back button and then having to refresh'),'info');
echo '<br /><a href="' . $RootPath . '/index.php">' . _('Back to the menu') . '</a>';
include('includes/footer.php');
exit;
}
if (isset($_GET['NewCredit'])){
/*New credit note entry - clear any existing credit note details from the Items object and initiate a newy*/
if (isset($_SESSION['CreditItems'.$identifier])){
unset ($_SESSION['CreditItems'.$identifier]->LineItems);
unset ($_SESSION['CreditItems'.$identifier]);
}
}
if (!isset($_SESSION['CreditItems'.$identifier])){
/* It must be a new credit note being created $_SESSION['CreditItems'.$identifier] would be set up from a previous call*/
$_SESSION['CreditItems'.$identifier] = new cart;
$_SESSION['RequireCustomerSelection'] = 1;
}
if (isset($_POST['ChangeCustomer'])){
$_SESSION['RequireCustomerSelection']=1;
}
if (isset($_POST['Quick'])){
unset($_POST['PartSearch']);
}
if (isset($_POST['CancelCredit'])) {
unset($_SESSION['CreditItems'.$identifier]->LineItems);
unset($_SESSION['CreditItems'.$identifier]);
$_SESSION['CreditItems'.$identifier] = new cart;
$_SESSION['RequireCustomerSelection'] = 1;
}
if (isset($_POST['SearchCust']) AND $_SESSION['RequireCustomerSelection']==1){
if ($_POST['Keywords'] AND $_POST['CustCode']) {
prnMsg( _('Customer name keywords have been used in preference to the customer code extract entered'), 'info' );
}
if ($_POST['Keywords']=='' AND $_POST['CustCode']=='') {
prnMsg( _('At least one Customer Name keyword OR an extract of a Customer Code must be entered for the search'), 'info' );
} else {
if (mb_strlen($_POST['Keywords'])>0) {
//insert wildcard characters in spaces
$SearchString = '%' . str_replace(' ', '%', $_POST['Keywords']) . '%';
$SQL = "SELECT debtorsmaster.name,
custbranch.debtorno,
custbranch.brname,
custbranch.contactname,
custbranch.phoneno,
custbranch.faxno,
custbranch.branchcode
FROM custbranch
INNER JOIN debtorsmaster
ON custbranch.debtorno=debtorsmaster.debtorno
WHERE custbranch.brname " . LIKE . " '" . $SearchString . "'
AND custbranch.disabletrans='0'";
} elseif (mb_strlen($_POST['CustCode'])>0){
$SQL = "SELECT debtorsmaster.name,
custbranch.debtorno,
custbranch.brname,
custbranch.contactname,
custbranch.phoneno,
custbranch.faxno,
custbranch.branchcode
FROM custbranch
INNER JOIN debtorsmaster
ON custbranch.debtorno=debtorsmaster.debtorno
WHERE custbranch.debtorno " . LIKE . "'%" . $_POST['CustCode'] . "%'
AND custbranch.disabletrans='0'";
}
$ErrMsg = _('Customer branch records requested cannot be retrieved because');
$DbgMsg = _('SQL used to retrieve the customer details was');
$result_CustSelect = DB_query($SQL,$ErrMsg,$DbgMsg);
if (DB_num_rows($result_CustSelect)==1){
$myrow=DB_fetch_array($result_CustSelect);
$SelectedCustomer = trim($myrow['debtorno']);
$SelectedBranch = trim($myrow['branchcode']);
$_POST['JustSelectedACustomer'] = true;
} elseif (DB_num_rows($result_CustSelect)==0){
prnMsg(_('Sorry') . ' ... ' . _('there are no customer branch records contain the selected text') . ' - ' . _('please alter your search criteria and try again'),'info');
}
} /*one of keywords or custcode was more than a zero length string */
} /*end of if search button for customers was hit*/
if (isset($_POST['JustSelectedACustomer']) AND !isset($SelectedCustomer)){
/*Need to figure out the number of the form variable that the user clicked on */
for ($i=1; $i < count($_POST); $i++){ //loop through the returned customers
if(isset($_POST['SubmitCustomerSelection'.$i])){
break;
}
}
if ($i==count($_POST)){
prnMsg(_('Unable to identify the selected customer'),'error');
} else {
$SelectedCustomer = trim($_POST['SelectedCustomer'.$i]);
$SelectedBranch = trim($_POST['SelectedBranch'.$i]);
}
}
if (isset($SelectedCustomer) AND isset($_POST['JustSelectedACustomer'])) {
/*will only be true if page called from customer selection form
Now retrieve customer information - name, salestype, currency, terms etc
*/
$_SESSION['CreditItems'.$identifier]->DebtorNo = $SelectedCustomer;
$_SESSION['CreditItems'.$identifier]->Branch = $SelectedBranch;
$_SESSION['RequireCustomerSelection'] = 0;
/* default the branch information from the customer branches table CustBranch -particularly where the stock
will be booked back into. */
$sql = "SELECT debtorsmaster.name,
debtorsmaster.salestype,
debtorsmaster.currcode,
currencies.rate,
currencies.decimalplaces,
custbranch.brname,
custbranch.braddress1,
custbranch.braddress2,
custbranch.braddress3,
custbranch.braddress4,
custbranch.braddress5,
custbranch.braddress6,
custbranch.phoneno,
custbranch.email,
custbranch.salesman,
custbranch.defaultlocation,
custbranch.taxgroupid,
locations.taxprovinceid
FROM custbranch
INNER JOIN locations ON locations.loccode=custbranch.defaultlocation
INNER JOIN debtorsmaster ON custbranch.debtorno=debtorsmaster.debtorno
INNER JOIN currencies ON debtorsmaster.currcode=currencies.currabrev
WHERE custbranch.branchcode='" . $_SESSION['CreditItems'.$identifier]->Branch . "'
AND custbranch.debtorno = '" . $_SESSION['CreditItems'.$identifier]->DebtorNo . "'";
$ErrMsg = _('The customer branch record of the customer selected') . ': ' . $SelectedCustomer . ' ' . _('cannot be retrieved because');
$DbgMsg = _('SQL used to retrieve the branch details was');
$result =DB_query($sql,$ErrMsg,$DbgMsg);
$myrow = DB_fetch_array($result);
/* the sales type determines the price list to be used by default the customer of the user is
defaulted from the entry of the userid and password. */
$_SESSION['CreditItems'.$identifier]->CustomerName = $myrow['name'];
$_SESSION['CreditItems'.$identifier]->DefaultSalesType = $myrow['salestype'];
$_SESSION['CreditItems'.$identifier]->DefaultCurrency = $myrow['currcode'];
$_SESSION['CreditItems'.$identifier]->CurrDecimalPlaces = $myrow['decimalplaces'];
$_SESSION['CurrencyRate'] = $myrow['rate'];
$_SESSION['CreditItems'.$identifier]->DeliverTo = $myrow['brname'];
$_SESSION['CreditItems'.$identifier]->BrAdd1 = $myrow['braddress1'];
$_SESSION['CreditItems'.$identifier]->BrAdd2 = $myrow['braddress2'];
$_SESSION['CreditItems'.$identifier]->BrAdd3 = $myrow['braddress3'];
$_SESSION['CreditItems'.$identifier]->BrAdd4 = $myrow['braddress4'];
$_SESSION['CreditItems'.$identifier]->BrAdd5 = $myrow['braddress5'];
$_SESSION['CreditItems'.$identifier]->BrAdd6 = $myrow['braddress6'];
$_SESSION['CreditItems'.$identifier]->PhoneNo = $myrow['phoneno'];
$_SESSION['CreditItems'.$identifier]->Email = $myrow['email'];
$_SESSION['CreditItems'.$identifier]->SalesPerson = $myrow['salesman'];
$_SESSION['CreditItems'.$identifier]->Location = $myrow['defaultlocation'];
$_SESSION['CreditItems'.$identifier]->TaxGroup = $myrow['taxgroupid'];
$_SESSION['CreditItems'.$identifier]->DispatchTaxProvince = $myrow['taxprovinceid'];
$_SESSION['CreditItems'.$identifier]->GetFreightTaxes();
}
/* if the change customer button hit or the customer has not already been selected */
if ($_SESSION['RequireCustomerSelection'] ==1
OR !isset($_SESSION['CreditItems'.$identifier]->DebtorNo)
OR $_SESSION['CreditItems'.$identifier]->DebtorNo=='' ) {
echo '<form action="' . htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') . '?identifier='.$identifier . '" method="post">';
echo '<div>';
echo '<input type="hidden" name="FormID" value="' . $_SESSION['FormID'] . '" />';
echo '<p class="page_title_text"><img src="' . $RootPath . '/css/' . $Theme . '/images/magnifier.png" title="' .
_('Search') . '" alt="" />' . ' ' . _('Select Customer For Credit Note') . '</p>';
echo '<table cellpadding="3" class="selection">';
echo '<tr><th colspan="5"><h3> ' . _('Customer Selection') . '</h3></th></tr>';
echo '<tr>
<td>' . _('Enter text in the customer name') . ':</td>
<td><input type="text" name="Keywords" size="20" maxlength="25" /></td>
<td><b>' . _('OR') . '</b></td>
<td>' . _('Enter text extract in the customer code') . ':</td>
<td><input type="text" name="CustCode" size="15" maxlength="18" /></td>
</tr>';
echo '</table>
<br />
<div class="centre">
<input type="submit" name="SearchCust" value="' . _('Search Now') . '" />
</div>';
if (isset($result_CustSelect)) {
echo '<br /><table cellpadding="2">';
$TableHeader = '<tr>
<th>' . _('Customer') . '</th>
<th>' . _('Branch') . '</th>
<th>' . _('Contact') . '</th>
<th>' . _('Phone') . '</th>
<th>' . _('Fax') . '</th>
</tr>';
echo $TableHeader;
$j = 1;
$LastCustomer='';
while ($myrow=DB_fetch_array($result_CustSelect)) {
if ($LastCustomer != $myrow['name']) {
echo '<td>' . $myrow['name'] . '</td>';
} else {
echo '<td></td>';
}
echo '<tr class="striped_row">
<td><input tabindex="'.($j+5).'" type="submit" name="SubmitCustomerSelection' . $j .'" value="' . htmlspecialchars($myrow['brname'], ENT_QUOTES,'UTF-8'). '" />
<input type="hidden" name="SelectedCustomer' . $j .'" value="'.$myrow['debtorno'].'" />
<input type="hidden" name="SelectedBranch' . $j .'" value="'. $myrow['branchcode'].'" /></td>
<td>' . $myrow['contactname'] . '</td>
<td>' . $myrow['phoneno'] . '</td>
<td>' . $myrow['faxno'] . '</td>
</tr>';
$LastCustomer=$myrow['name'];
$j++;
//end of page full new headings if
} //end of while loop
echo '</table><input type="hidden" name="JustSelectedACustomer" value="Yes" />';
}//end if results to show
echo '</div>
</form>';
//end if RequireCustomerSelection
} else {
/* everything below here only do if a customer is selected
first add a header to show who we are making a credit note for */
echo '<p class="page_title_text"><img src="' . $RootPath . '/css/' . $Theme . '/images/magnifier.png" title="' .
_('Search') . '" alt="" />' . ' ' . $_SESSION['CreditItems'.$identifier]->CustomerName . ' - ' . $_SESSION['CreditItems'.$identifier]->DeliverTo . '</p>';
if (isset($_POST['SalesPerson'])){
$_SESSION['CreditItems' . $identifier]->SalesPerson = $_POST['SalesPerson'];
}
/* do the search for parts that might be being looked up to add to the credit note */
if (isset($_POST['Search'])){
if ($_POST['Keywords']!='' AND $_POST['StockCode']!='') {
prnMsg( _('Stock description keywords have been used in preference to the Stock code extract entered') . '.', 'info' );
}
if ($_POST['Keywords']!='') {
//insert wildcard characters in spaces
$SearchString = '%' . str_replace(' ', '%', $_POST['Keywords']) . '%';
if ($_POST['StockCat']=='All'){
$SQL = "SELECT stockmaster.stockid,
stockmaster.description,
stockmaster.units
FROM stockmaster INNER JOIN stockcategory
ON stockmaster.categoryid=stockcategory.categoryid
WHERE (stockcategory.stocktype='F' OR stockcategory.stocktype='D')
AND stockmaster.description " . LIKE . " '" . $SearchString . "'
GROUP BY stockmaster.stockid,
stockmaster.description,
stockmaster.units
ORDER BY stockmaster.stockid";
} else {
$SQL = "SELECT stockmaster.stockid,
stockmaster.description,
stockmaster.units
FROM stockmaster INNER JOIN stockcategory
ON stockmaster.categoryid=stockcategory.categoryid
WHERE (stockcategory.stocktype='F' OR stockcategory.stocktype='D')
AND stockmaster.description " . LIKE . " '" . $SearchString . "'
AND stockmaster.categoryid='" . $_POST['StockCat'] . "'
GROUP BY stockmaster.stockid,
stockmaster.description,
stockmaster.units
ORDER BY stockmaster.stockid";
}
} elseif ($_POST['StockCode']!=''){
$SearchString = '%' . $_POST['StockCode'] . '%';
if ($_POST['StockCat']=='All'){
$SQL = "SELECT stockmaster.stockid,
stockmaster.description,
stockmaster.units
FROM stockmaster INNER JOIN stockcategory
ON stockmaster.categoryid=stockcategory.categoryid
WHERE (stockcategory.stocktype='F' OR stockcategory.stocktype='D')
AND stockmaster.stockid " . LIKE . " '" . $SearchString . "'
GROUP BY stockmaster.stockid,
stockmaster.description,
stockmaster.units
ORDER BY stockmaster.stockid";
} else {
$SQL = "SELECT stockmaster.stockid,
stockmaster.description,
stockmaster.units
FROM stockmaster INNER JOIN stockcategory
ON stockmaster.categoryid=stockcategory.categoryid
WHERE (stockcategory.stocktype='F' OR stockcategory.stocktype='D')
AND stockmaster.stockid " . LIKE . " '" . $SearchString . "'
AND stockmaster.categoryid='" . $_POST['StockCat'] . "'
GROUP BY stockmaster.stockid,
stockmaster.description,
stockmaster.units
ORDER BY stockmaster.stockid";
}
} else {
if ($_POST['StockCat']=='All'){
$SQL = "SELECT stockmaster.stockid,
stockmaster.description,
stockmaster.units
FROM stockmaster INNER JOIN stockcategory
ON stockmaster.categoryid=stockcategory.categoryid
WHERE (stockcategory.stocktype='F' OR stockcategory.stocktype='D')
GROUP BY stockmaster.stockid,
stockmaster.description,
stockmaster.units
ORDER BY stockmaster.stockid";
} else {
$SQL = "SELECT stockmaster.stockid,
stockmaster.description,
stockmaster.units
FROM stockmaster INNER JOIN stockcategory
ON stockmaster.categoryid=stockcategory.categoryid
WHERE (stockcategory.stocktype='F' OR stockcategory.stocktype='D')
AND stockmaster.categoryid='" . $_POST['StockCat'] . "'
GROUP BY stockmaster.stockid,
stockmaster.description,
stockmaster.units
ORDER BY stockmaster.stockid";
}
}
$ErrMsg = _('There is a problem selecting the part records to display because');
$SearchResult = DB_query($SQL,$ErrMsg);
if (DB_num_rows($SearchResult)==0){
prnMsg(_('There are no products available that match the criteria specified'),'info');
if ($debug==1){
prnMsg(_('The SQL statement used was') . ':<br />' . $SQL,'info');
}
}
if (DB_num_rows($SearchResult)==1){
$myrow=DB_fetch_array($SearchResult);
$_POST['NewItem'] = $myrow['stockid'];
DB_data_seek($SearchResult,0);
}
} //end of if search for parts to add to the credit note
/*Always do the stuff below if not looking for a customerid
Set up the form for the credit note display and entry*/
echo '<form id="MainForm" action="' . htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') . '?identifier='.$identifier . '" method="post">
<div>
<input type="hidden" name="FormID" value="' . $_SESSION['FormID'] . '" />';
/*Process Quick Entry */
if (isset($_POST['QuickEntry'])){
/* get the item details from the database and hold them in the cart object make the quantity 1 by default then add it to the cart */
$i=1;
do {
do {
$QuickEntryCode = 'part_' . $i;
$QuickEntryQty = 'qty_' . $i;
$i++;
} while (!is_numeric(filter_number_format($_POST[$QuickEntryQty]))
AND filter_number_format($_POST[$QuickEntryQty]) <=0
AND mb_strlen($_POST[$QuickEntryCode])!=0
AND $i<=$QuickEntires);
$_POST['NewItem'] = trim($_POST[$QuickEntryCode]);
$NewItemQty = filter_number_format($_POST[$QuickEntryQty]);
if (mb_strlen($_POST['NewItem'])==0){
break; /* break out of the loop if nothing in the quick entry fields*/
}
$AlreadyOnThisCredit =0;
foreach ($_SESSION['CreditItems'.$identifier]->LineItems AS $OrderItem) {
/* do a loop round the items on the credit note to see that the item
is not already on this credit note */
if ($_SESSION['SO_AllowSameItemMultipleTimes']==0 AND strcasecmp($OrderItem->StockID, $_POST['NewItem']) == 0) {
$AlreadyOnThisCredit = 1;
prnMsg($_POST['NewItem'] . ' ' . _('is already on this credit - the system will not allow the same item on the credit note more than once. However you can change the quantity credited of the existing line if necessary'),'warn');
}
} /* end of the foreach loop to look for preexisting items of the same code */
if ($AlreadyOnThisCredit!=1){
$sql = "SELECT stockmaster.description,
stockmaster.longdescription,
stockmaster.stockid,
stockmaster.units,
stockmaster.volume,
stockmaster.grossweight,
(materialcost+labourcost+overheadcost) AS standardcost,
stockmaster.mbflag,
stockmaster.decimalplaces,
stockmaster.controlled,
stockmaster.serialised,
stockmaster.discountcategory,
stockmaster.taxcatid
FROM stockmaster
WHERE stockmaster.stockid = '". $_POST['NewItem'] . "'";
$ErrMsg = _('There is a problem selecting the part because');
$result1 = DB_query($sql,$ErrMsg);
if ($myrow = DB_fetch_array($result1)){
$LineNumber = $_SESSION['CreditItems'.$identifier]->LineCounter;
if ($_SESSION['CreditItems'.$identifier]->add_to_cart ($myrow['stockid'],
$NewItemQty,
$myrow['description'],
$myrow['longdescription'],
GetPrice ($_POST['NewItem'],
$_SESSION['CreditItems'.$identifier]->DebtorNo,
$_SESSION['CreditItems'.$identifier]->Branch),
0,
$myrow['units'],
$myrow['volume'],
$myrow['grossweight'],
0,
$myrow['mbflag'],
Date($_SESSION['DefaultDateFormat']),
0,
$myrow['discountcategory'],
$myrow['controlled'],
$myrow['serialised'],
$myrow['decimalplaces'],
'',
'No',
-1,
$myrow['taxcatid'],
'',
'',
'',
$myrow['standardcost']) ==1){
$_SESSION['CreditItems'.$identifier]->GetTaxes($LineNumber);
if ($myrow['controlled']==1){
/*Qty must be built up from serial item entries */
$_SESSION['CreditItems'.$identifier]->LineItems[$LineNumber]->Quantity = 0;
}
}
} else {
prnMsg( $_POST['NewItem'] . ' ' . _('does not exist in the database and cannot therefore be added to the credit note'),'warn');
}
} /* end of if not already on the credit note */
} while ($i<=$_SESSION['QuickEntries']); /*loop to the next quick entry record */
unset($_POST['NewItem']);
} /* end of if quick entry */
/* setup system defaults for looking up prices and the number of ordered items
if an item has been selected for adding to the basket add it to the session arrays */
if ($_SESSION['CreditItems'.$identifier]->ItemsOrdered > 0 OR isset($_POST['NewItem'])){
if (isset($_GET['Delete'])){
$_SESSION['CreditItems'.$identifier]->remove_from_cart($_GET['Delete']);
}
if (isset($_POST['ChargeFreightCost'])){
$_SESSION['CreditItems'.$identifier]->FreightCost = filter_number_format($_POST['ChargeFreightCost']);
}
if (isset($_POST['Location'])
AND $_POST['Location'] != $_SESSION['CreditItems'.$identifier]->Location){
$_SESSION['CreditItems'.$identifier]->Location = $_POST['Location'];
$NewDispatchTaxProvResult = DB_query("SELECT taxprovinceid FROM locations WHERE loccode='" . $_POST['Location'] . "'");
$myrow = DB_fetch_array($NewDispatchTaxProvResult);
$_SESSION['CreditItems'.$identifier]->DispatchTaxProvince = $myrow['taxprovinceid'];
foreach ($_SESSION['CreditItems'.$identifier]->LineItems as $LineItem) {
$_SESSION['CreditItems'.$identifier]->GetTaxes($LineItem->LineNumber);
}
}
foreach ($_SESSION['CreditItems'.$identifier]->LineItems as $LineItem) {
if (isset($_POST['Quantity_' . $LineItem->LineNumber])){
$Quantity = filter_number_format($_POST['Quantity_' . $LineItem->LineNumber]);
$Narrative = $_POST['Narrative_' . $LineItem->LineNumber];
if (isset($_POST['Price_' . $LineItem->LineNumber])){
if (isset($_POST['Gross']) AND $_POST['Gross']==true){
$TaxTotalPercent =0;
foreach ($LineItem->Taxes AS $Tax) {
if ($Tax->TaxOnTax ==1){
$TaxTotalPercent += (1 + $TaxTotalPercent) * $Tax->TaxRate;
} else {
$TaxTotalPercent += $Tax->TaxRate;
}
}
$Price = round(filter_number_format($_POST['Price_' . $LineItem->LineNumber])/($TaxTotalPercent + 1),$_SESSION['CreditItems'.$identifier]->CurrDecimalPlaces);
} else {
$Price = filter_number_format($_POST['Price_' . $LineItem->LineNumber]);
}
$DiscountPercentage = filter_number_format($_POST['Discount_' . $LineItem->LineNumber]);
foreach ($LineItem->Taxes as $TaxKey=>$TaxLine) {
if (is_numeric(filter_number_format($_POST[$LineItem->LineNumber . $TaxLine->TaxCalculationOrder . '_TaxRate']))){
$_SESSION['CreditItems'.$identifier]->LineItems[$LineItem->LineNumber]->Taxes[$TaxKey]->TaxRate = filter_number_format($_POST[$LineItem->LineNumber . $TaxKey . '_TaxRate'])/100;
}
}
}
if ($Quantity<0 OR $Price <0 OR $DiscountPercentage >100 OR $DiscountPercentage <0){
prnMsg(_('The item could not be updated because you are attempting to set the quantity credited to less than 0 or the price less than 0 or the discount more than 100% or less than 0%'),'warn');
} elseif (isset($_POST['Quantity_' . $LineItem->LineNumber])) {
$_SESSION['CreditItems'.$identifier]->update_cart_item($LineItem->LineNumber,
$Quantity,
$Price,
$DiscountPercentage/100,
$Narrative,
'No',
$LineItem->ItemDue,
$LineItem->POLine,
0,
$identifier);
}
}
}
foreach ($_SESSION['CreditItems'.$identifier]->FreightTaxes as $FreightTaxKey=>$FreightTaxLine) {
if (is_numeric(filter_number_format($_POST['FreightTaxRate' . $FreightTaxLine->TaxCalculationOrder]))){
$_SESSION['CreditItems'.$identifier]->FreightTaxes[$FreightTaxKey]->TaxRate = filter_number_format($_POST['FreightTaxRate' . $FreightTaxKey])/100;
}
}
if (isset($_POST['NewItem'])){
/* get the item details from the database and hold them in the cart object make the quantity 1 by default then add it to the cart */
$AlreadyOnThisCredit =0;
foreach ($_SESSION['CreditItems'.$identifier]->LineItems AS $OrderItem) {
/* do a loop round the items on the credit note to see that the item
is not already on this credit note */
if ($_SESSION['SO_AllowSameItemMultipleTimes']==0 AND strcasecmp($OrderItem->StockID, $_POST['NewItem']) == 0) {
$AlreadyOnThisCredit = 1;
prnMsg(_('The item selected is already on this credit the system will not allow the same item on the credit note more than once. However you can change the quantity credited of the existing line if necessary.'),'warn');
}
} /* end of the foreach loop to look for preexisting items of the same code */
if ($AlreadyOnThisCredit!=1){
$sql = "SELECT stockmaster.description,
stockmaster.longdescription,
stockmaster.stockid,
stockmaster.units,
stockmaster.volume,
stockmaster.grossweight,
stockmaster.mbflag,
stockmaster.discountcategory,
stockmaster.controlled,
stockmaster.decimalplaces,
stockmaster.serialised,
(materialcost+labourcost+overheadcost) AS standardcost,
stockmaster.taxcatid
FROM stockmaster
WHERE stockmaster.stockid = '". $_POST['NewItem'] . "'";
$ErrMsg = _('The item details could not be retrieved because');
$DbgMsg = _('The SQL used to retrieve the item details but failed was');
$result1 = DB_query($sql,$ErrMsg,$DbgMsg);
$myrow = DB_fetch_array($result1);
$LineNumber = $_SESSION['CreditItems'.$identifier]->LineCounter;
/*validate the data returned before adding to the items to credit */
if ($_SESSION['CreditItems'.$identifier]->add_to_cart ($myrow['stockid'],
1,
$myrow['description'],
$myrow['longdescription'],
GetPrice($_POST['NewItem'],
$_SESSION['CreditItems'.$identifier]->DebtorNo,
$_SESSION['CreditItems'.$identifier]->Branch),
0,
$myrow['units'],
$myrow['volume'],
$myrow['grossweight'],
0,
$myrow['mbflag'],
Date($_SESSION['DefaultDateFormat']),
0,
$myrow['discountcategory'],
$myrow['controlled'],
$myrow['serialised'],
$myrow['decimalplaces'],
'',
'No',
-1,
$myrow['taxcatid'],
'',
'',
'',
$myrow['standardcost']) ==1){
$_SESSION['CreditItems'.$identifier]->GetTaxes($LineNumber);
if ($myrow['controlled']==1){
/*Qty must be built up from serial item entries */
$_SESSION['CreditItems'.$identifier]->LineItems[$LineNumber]->Quantity = 0;
}
}
} /* end of if not already on the credit note */
} /* end of if its a new item */
/* This is where the credit note as selected should be displayed reflecting any deletions or insertions*/
echo '<table cellpadding="2" class="selection">
<tr>
<th>' . _('Item Code') . '</th>
<th>' . _('Item Description') . '</th>
<th>' . _('Quantity') . '</th>
<th>' . _('Unit') . '</th>
<th>' . _('Price') . '</th>
<th>' . _('Gross') . '</th>
<th>' . _('Discount') . '</th>
<th>' . _('Total') . '<br />' . _('Excl Tax') . '</th>
<th>' . _('Tax Authority') . '</th>
<th>' . _('Tax') . '<br />' . _('Rate') . '</th>
<th>' . _('Tax') . '<br />' . _('Amount') . '</th>
<th>' . _('Total') . '<br />' . _('Incl Tax') . '</th>
</tr>';
$_SESSION['CreditItems'.$identifier]->total = 0;
$_SESSION['CreditItems'.$identifier]->totalVolume = 0;
$_SESSION['CreditItems'.$identifier]->totalWeight = 0;
$TaxTotal = 0;
$TaxTotals = array();
$TaxGLCodes = array();
foreach ($_SESSION['CreditItems'.$identifier]->LineItems as $LineItem) {
$LineTotal = round($LineItem->Quantity * $LineItem->Price * (1 - $LineItem->DiscountPercent),$_SESSION['CreditItems'.$identifier]->CurrDecimalPlaces);
$DisplayLineTotal = locale_number_format($LineTotal,$_SESSION['CreditItems'.$identifier]->CurrDecimalPlaces);
echo '<tr class="striped_row">
<td>' . $LineItem->StockID . '</td>
<td title="'. $LineItem->LongDescription . '">' . $LineItem->ItemDescription . '</td>';
if ($LineItem->Controlled==0){
echo '<td><input type="text" class="number" name="Quantity_' . $LineItem->LineNumber . '" maxlength="8" size="6" value="' . locale_number_format(round($LineItem->Quantity,$LineItem->DecimalPlaces),$LineItem->DecimalPlaces) . '" /></td>';
} else {
echo '<td class="number"><a href="' . $RootPath . '/CreditItemsControlled.php?LineNo=' . $LineItem->LineNumber . '&identifier=' . $identifier . '">' . locale_number_format($LineItem->Quantity,$LineItem->DecimalPlaces) . '</a>
<input type="hidden" name="Quantity_' . $LineItem->LineNumber . '" value="' . locale_number_format(round($LineItem->Quantity,$LineItem->DecimalPlaces),$LineItem->DecimalPlaces) . '" /></td>';
}
echo '<td>' . $LineItem->Units . '</td>
<td><input type="text" class="number" name="Price_' . $LineItem->LineNumber . '" size="10" maxlength="12" value="' . locale_number_format($LineItem->Price,$_SESSION['CreditItems'.$identifier]->CurrDecimalPlaces) . '" /></td>
<td><input type="CheckBox" name="Gross" value="false" /></td>
<td><input type="text" class="number" name="Discount_' . $LineItem->LineNumber . '" size="3" maxlength="3" value="' . locale_number_format(($LineItem->DiscountPercent * 100),'Variable') . '" />%</td>
<td class="number">' . $DisplayLineTotal . '</td>';
/*Need to list the taxes applicable to this line */
echo '<td>';
foreach ($_SESSION['CreditItems'.$identifier]->LineItems[$LineItem->LineNumber]->Taxes AS $Tax) {
echo '<br />';
echo $Tax->TaxAuthDescription;
}
echo '</td>';
echo '<td>';
$i=0; // initialise the number of taxes iterated through
$TaxLineTotal =0; //initialise tax total for the line
foreach ($LineItem->Taxes AS $TaxKey=>$Tax) {
if ($i>0){
echo '<br />';
}
echo '<input type="text" class="number" name="' . $LineItem->LineNumber . $TaxKey . '_TaxRate" maxlength="4" size="4" value="' . locale_number_format($Tax->TaxRate*100,'Variable') . '" />';
$i++;
if ($Tax->TaxOnTax ==1){
$TaxTotals[$Tax->TaxAuthID] += ($Tax->TaxRate * ($LineTotal + $TaxLineTotal));
$TaxLineTotal += ($Tax->TaxRate * ($LineTotal + $TaxLineTotal));
} else {
$TaxTotals[$Tax->TaxAuthID] += ($Tax->TaxRate * $LineTotal);
$TaxLineTotal += ($Tax->TaxRate * $LineTotal);
}
$TaxGLCodes[$Tax->TaxAuthID] = $Tax->TaxGLCode;
}
echo '</td>';
$TaxTotal += $TaxLineTotal;
$DisplayTaxAmount = locale_number_format($TaxLineTotal ,$_SESSION['CreditItems'.$identifier]->CurrDecimalPlaces);
$DisplayGrossLineTotal = locale_number_format($LineTotal + $TaxLineTotal, $_SESSION['CreditItems'.$identifier]->CurrDecimalPlaces);
echo '<td class="number">' . $DisplayTaxAmount . '</td>
<td class="number">' . $DisplayGrossLineTotal . '</td>
<td><a href="' . htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') . '?identifier=' . $identifier . '&Delete=' . $LineItem->LineNumber . '" onclick="return confirm(\'' . _('Are you sure you wish to delete this line item from the credit note?') . '\');">' . _('Delete') . '</a></td>
</tr>';
echo '<tr class="striped_row">
<td colspan="11"><textarea name="Narrative_' . $LineItem->LineNumber . '" cols="100%" rows="1">' . $LineItem->Narrative . '</textarea><br /></td>
</tr>';
$_SESSION['CreditItems'.$identifier]->total += $LineTotal;
$_SESSION['CreditItems'.$identifier]->totalVolume += ($LineItem->Quantity * $LineItem->Volume);
$_SESSION['CreditItems'.$identifier]->totalWeight += ($LineItem->Quantity * $LineItem->Weight);
}
if (!isset($_POST['ChargeFreightCost'])
AND !isset($_SESSION['CreditItems'.$identifier]->FreightCost)){
$_POST['ChargeFreightCost']=0;
}
echo '<tr>
<td colspan="5"></td>';
echo '<td colspan="2" class="number">' . _('Credit Freight') . '</td>
<td><input type="text" class="number" size="6" maxlength="6" name="ChargeFreightCost" value="' . locale_number_format($_SESSION['CreditItems'.$identifier]->FreightCost,$_SESSION['CreditItems'.$identifier]->CurrDecimalPlaces) . '" /></td>';
$FreightTaxTotal =0; //initialise tax total
echo '<td>';
$i=0; // initialise the number of taxes iterated through
foreach ($_SESSION['CreditItems'.$identifier]->FreightTaxes as $FreightTaxLine) {
if ($i>0){
echo '<br />';
}
echo $FreightTaxLine->TaxAuthDescription;
$i++;
}
echo '</td><td>';
$i=0;
foreach ($_SESSION['CreditItems'.$identifier]->FreightTaxes as $FreightTaxLine) {
if ($i>0){
echo '<br />';
}
echo '<input type="text" class="number" name=FreightTaxRate' . $FreightTaxLine->TaxCalculationOrder . ' maxlength="4" size="4" value="' . locale_number_format(($FreightTaxLine->TaxRate * 100),'Variable') . '" />';
if ($FreightTaxLine->TaxOnTax ==1){
$TaxTotals[$FreightTaxLine->TaxAuthID] += ($FreightTaxLine->TaxRate * ($_SESSION['CreditItems'.$identifier]->FreightCost + $FreightTaxTotal));
$FreightTaxTotal += ($FreightTaxLine->TaxRate * ($_SESSION['CreditItems'.$identifier]->FreightCost + $FreightTaxTotal));
} else {
$TaxTotals[$FreightTaxLine->TaxAuthID] += ($FreightTaxLine->TaxRate * $_SESSION['CreditItems'.$identifier]->FreightCost);
$FreightTaxTotal += ($FreightTaxLine->TaxRate * $_SESSION['CreditItems'.$identifier]->FreightCost);
}
$i++;
$TaxGLCodes[$FreightTaxLine->TaxAuthID] = $FreightTaxLine->TaxGLCode;
}
echo '</td>';
echo '<td class="number">' . locale_number_format($FreightTaxTotal,$_SESSION['CreditItems'.$identifier]->CurrDecimalPlaces) . '</td>
<td class="number">' . locale_number_format($FreightTaxTotal+ $_SESSION['CreditItems'.$identifier]->FreightCost,$_SESSION['CreditItems'.$identifier]->CurrDecimalPlaces) . '</td>
</tr>';
$TaxTotal += $FreightTaxTotal;
$DisplayTotal = locale_number_format($_SESSION['CreditItems'.$identifier]->total + $_SESSION['CreditItems'.$identifier]->FreightCost,$_SESSION['CreditItems'.$identifier]->CurrDecimalPlaces);
echo '<tr>
<td colspan="7" class="number">' . _('Credit Totals') . '</td>
<td class="number"><b>' . $DisplayTotal . '</b></td>
<td colspan="2"></td>
<td class="number"><b>' . locale_number_format($TaxTotal,$_SESSION['CreditItems'.$identifier]->CurrDecimalPlaces) . '</td>
<td class="number"><b>' . locale_number_format($TaxTotal+($_SESSION['CreditItems'.$identifier]->total + $_SESSION['CreditItems'.$identifier]->FreightCost),$_SESSION['CreditItems'.$identifier]->CurrDecimalPlaces) . '</b></td>
</tr>
</table>';
/*Now show options for the credit note */
echo '<br />
<table class="selection">
<tr>
<td>' . _('Credit Note Type') . ' :</td>
<td><select name="CreditType" onchange="ReloadForm(MainForm.Update)" >';
if (!isset($_POST['CreditType']) OR $_POST['CreditType']=='Return'){
echo '<option selected="selected" value="Return">' . _('Goods returned to store') . '</option>
<option value="WriteOff">' . _('Goods written off') . '</option>
<option value="ReverseOverCharge">' . _('Reverse an Overcharge') . '</option>';
} elseif ($_POST['CreditType']=='WriteOff') {
echo '<option selected="selected" value="WriteOff">' . _('Goods written off') . '</option>
<option value="Return">' . _('Goods returned to store') . '</option>
<option value="ReverseOverCharge">' . _('Reverse an Overcharge') . '</option>';
} elseif($_POST['CreditType']=='ReverseOverCharge'){
echo '<option selected="selected" value="ReverseOverCharge">' . _('Reverse Overcharge Only') . '</option>
<option value="Return">' . _('Goods Returned To Store') . '</option>
<option value="WriteOff">' . _('Good written off') . '</option>';
}
echo '</select></td></tr>';
if (!isset($_POST['CreditType']) OR $_POST['CreditType']=='Return'){
/*if the credit note is a return of goods then need to know which location to receive them into */
echo '<tr>
<td>' . _('Goods Returned to Location') . ' :</td>
<td><select name="Location">';
$SQL="SELECT locations.loccode, locationname FROM locations INNER JOIN locationusers ON locationusers.loccode=locations.loccode AND locationusers.userid='" . $_SESSION['UserID'] . "' AND locationusers.canupd=1";
$Result = DB_query($SQL);
if (!isset($_POST['Location'])){
$_POST['Location'] = $_SESSION['CreditItems'.$identifier]->Location;
}
while ($myrow = DB_fetch_array($Result)) {
if ($_POST['Location']==$myrow['loccode']){
echo '<option selected="selected" value="' . $myrow['loccode'] . '">' . $myrow['locationname'] . '</option>';
} else {
echo '<option value="' . $myrow['loccode'] . '">' . $myrow['locationname'] . '</option>';
}
}
echo '</select></td></tr>';
} elseif ($_POST['CreditType']=='WriteOff') { /* the goods are to be written off to somewhere */
echo '<tr><td>' . _('Write off the cost of the goods to') . '</td>
<td><select name=WriteOffGLCode>';
$SQL="SELECT accountcode,
accountname
FROM chartmaster INNER JOIN accountgroups
ON chartmaster.group_=accountgroups.groupname
WHERE accountgroups.pandl=1
ORDER BY accountcode";
$Result = DB_query($SQL);
while ($myrow = DB_fetch_array($Result)) {
if ($_POST['WriteOffGLCode']==$myrow['accountcode']){
echo '<option selected="selected" value="' . $myrow['accountcode'] . '">' . $myrow['accountcode'] . ' - ' . $myrow['accountname'] . '</option>';
} else {
echo '<option value="' . $myrow['accountcode'] . '">' . $myrow['accountcode'] . ' - ' . $myrow['accountname'] . '</option>';
}
}
echo '</select></td></tr>';
}
echo '<tr>
<td>' . _('Sales person'). ':</td>
<td><select name="SalesPerson">';
$SalesPeopleResult = DB_query("SELECT salesmancode, salesmanname FROM salesman WHERE current=1");
if (!isset($_POST['SalesPerson']) AND $_SESSION['SalesmanLogin']!=NULL ){
$_SESSION['CreditItems'.$identifier]->SalesPerson = $_SESSION['SalesmanLogin'];
}
while ($SalesPersonRow = DB_fetch_array($SalesPeopleResult)){
if ($SalesPersonRow['salesmancode']==$_SESSION['CreditItems'.$identifier]->SalesPerson){
echo '<option selected="selected" value="' . $SalesPersonRow['salesmancode'] . '">' . $SalesPersonRow['salesmanname'] . '</option>';
} else {
echo '<option value="' . $SalesPersonRow['salesmancode'] . '">' . $SalesPersonRow['salesmanname'] . '</option>';
}
}
echo '</select></td>
</tr>';
if (!isset($_POST['CreditText'])) {
$_POST['CreditText']='';
}
echo '<tr><td>' . _('Credit Note Text') . ' :</td>
<td><textarea name="CreditText" COLS="31" rows="5">' . $_POST['CreditText'] . '</textarea></td>
</tr>
</table><br />';
$OKToProcess = true;
/*Check for the worst */
if (isset($_POST['CreditType']) and $_POST['CreditType']=='WriteOff' AND !isset($_POST['WriteOffGLCode'])){
prnMsg (_('The GL code to write off the credit value to must be specified. Please select the appropriate GL code for the selection box'),'info');
$OKToProcess = false;
}
echo '<div class="centre">
<input type="submit" name="Update" value="' . _('Update') . '" />
<input type="submit" name="CancelCredit" value="' . _('Cancel') . '" onclick="return confirm(\'' . _('Are you sure you wish to cancel the whole of this credit note?') . '\');" />';
if (!isset($_POST['ProcessCredit']) AND $OKToProcess == true){
echo '<input type="submit" name="ProcessCredit" value="' . _('Process Credit Note') . '" />
<br />';
}
echo '</div>';
} # end of if lines
/* Now show the stock item selection search stuff below */
if (isset($_POST['PartSearch']) AND $_POST['PartSearch']!='' AND !isset($_POST['ProcessCredit'])){
echo '<input type="hidden" name="PartSearch" value="' . _('Yes Please') . '" />';
$SQL="SELECT categoryid,
categorydescription
FROM stockcategory
WHERE stocktype='F'
ORDER BY categorydescription";
$result1 = DB_query($SQL);
echo '<br />
<table class="selection">
<tr>
<td>' . _('Select a stock category') . ': <select name="StockCat">';
echo '<option selected="selected" value="All">' . _('All') . '</option>';
while ($myrow1 = DB_fetch_array($result1)) {
if (isset($_POST['StockCat']) and $_POST['StockCat']==$myrow1['categoryid']){
echo '<option selected="selected" value="' . $myrow1['categoryid'] . '">' . $myrow1['categorydescription'] . '</option>';
} else {
echo '<option value="' . $myrow1['categoryid'] . '">' . $myrow1['categorydescription'] . '</option>';
}
}
echo '</select></td>';
if (!isset($_POST['Keywords'])) {
$_POST['Keywords'] = '';
}
if (!isset($_POST['StockCode'])) {
$_POST['StockCode'] = '';
}
echo '<td>' . _('Enter text extracts in the description') . ': </td>';
echo '<td><input type="text" name="Keywords" size="20" maxlength="25" value="' . $_POST['Keywords'] . '" /></td></tr>';
echo '<tr><td></td>';
echo '<td><b>' ._('OR') . '</b> ' . _('Enter extract of the Stock Code') . ': </td>';
echo '<td><input type="text" name="StockCode" size="15" maxlength="18" value="' . $_POST['StockCode'] . '" /></td>';
echo '</tr>';
echo '</table>
<br />
<div class="centre">';
echo '<input type="submit" name="Search" value="' . _('Search Now') .'" />
<input type="submit" name="ChangeCustomer" value="' . _('Change Customer') . '" />
<input type="submit" name="Quick" value="' . _('Quick Entry') . '" />
</div>';
if (isset($SearchResult)) {
echo '<table cellpadding="2" class="selection">';