-
Notifications
You must be signed in to change notification settings - Fork 3
/
Credit_Invoice.php
1551 lines (1326 loc) · 62 KB
/
Credit_Invoice.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
/* $Revision: 1.35 $ */
/* $Id$*/
/*Functions to get the GL codes to post the transaction to */
include('includes/GetSalesTransGLCodes.inc');
/*defines the structure of the data required to hold the transaction as a session variable */
include('includes/DefineCartClass.php');
include('includes/DefineSerialItems.php');
/* Session started in header.inc for password checking and authorisation level check */
include('includes/session.inc');
$title = _('Credit An Invoice');
include('includes/header.inc');
include('includes/SQL_CommonFunctions.inc');
if (!isset($_GET['InvoiceNumber']) AND !$_SESSION['ProcessingCredit']) {
/* This page can only be called with an invoice number for crediting*/
prnMsg(_('This page can only be opened if an invoice has been selected for crediting') . '. ' . _('Please select an invoice first') . ' - ' . _('from the customer inquiry screen click the link to credit an invoice'),'info');
include('includes/footer.inc');
exit;
} elseif (isset($_GET['InvoiceNumber'])) {
$_GET['InvoiceNumber']=(int)$_GET['InvoiceNumber'];
unset($_SESSION['CreditItems']->LineItems);
unset($_SESSION['CreditItems']);
$_SESSION['ProcessingCredit'] = $_GET['InvoiceNumber'];
$_SESSION['CreditItems'] = new cart;
/*read in all the guff from the selected invoice into the Items cart */
$InvoiceHeaderSQL = "SELECT DISTINCT
debtortrans.id AS transid,
debtortrans.debtorno,
debtorsmaster.name,
debtortrans.branchcode,
debtortrans.reference,
debtortrans.invtext,
debtortrans.order_,
debtortrans.trandate,
debtortrans.tpe,
debtortrans.shipvia,
debtortrans.ovfreight,
debtortrans.rate AS currency_rate,
debtorsmaster.currcode,
custbranch.defaultlocation,
custbranch.taxgroupid,
stockmoves.loccode,
locations.taxprovinceid
FROM debtortrans
INNER JOIN debtorsmaster
ON debtortrans.debtorno = debtorsmaster.debtorno
INNER JOIN custbranch
ON debtortrans.branchcode = custbranch.branchcode
AND debtortrans.debtorno = custbranch.debtorno
INNER JOIN currencies
ON debtorsmaster.currcode = currencies.currabrev
INNER JOIN stockmoves
ON stockmoves.transno=debtortrans.transno
INNER JOIN locations
ON stockmoves.loccode = locations.loccode
WHERE debtortrans.transno = '" . $_GET['InvoiceNumber'] . "'
AND debtortrans.type=10
AND stockmoves.type=10";
$ErrMsg = _('A credit cannot be produced for the selected invoice') . '. ' . _('The invoice details cannot be retrieved because');
$DbgMsg = _('The SQL used to retrieve the invoice details was');
$GetInvHdrResult = DB_query($InvoiceHeaderSQL,$db,$ErrMsg,$DbgMsg);
if (DB_num_rows($GetInvHdrResult)==1) {
$myrow = DB_fetch_array($GetInvHdrResult);
/*CustomerID variable registered by header.inc */
$_SESSION['CreditItems']->DebtorNo = $myrow['debtorno'];
$_SESSION['CreditItems']->TransID = $myrow['transid'];
$_SESSION['CreditItems']->Branch = $myrow['branchcode'];
$_SESSION['CreditItems']->CustomerName = $myrow['name'];
$_SESSION['CreditItems']->CustRef = $myrow['reference'];
$_SESSION['CreditItems']->Comments = $myrow['invtext'];
$_SESSION['CreditItems']->DefaultSalesType =$myrow['tpe'];
$_SESSION['CreditItems']->DefaultCurrency = $myrow['currcode'];
$_SESSION['CreditItems']->Location = $myrow['loccode'];
$_SESSION['Old_FreightCost'] = $myrow['ovfreight'];
$_SESSION['CurrencyRate'] = $myrow['currency_rate'];
$_SESSION['CreditItems']->OrderNo = $myrow['order_'];
$_SESSION['CreditItems']->ShipVia = $myrow['shipvia'];
$_SESSION['CreditItems']->TaxGroup = $myrow['taxgroupid'];
$_SESSION['CreditItems']->FreightCost = $myrow['ovfreight'];
$_SESSION['CreditItems']->DispatchTaxProvince = $myrow['taxprovinceid'];
$_SESSION['CreditItems']->GetFreightTaxes();
DB_free_result($GetInvHdrResult);
/*now populate the line items array with the stock movement records for the invoice*/
$LineItemsSQL = "SELECT stockmoves.stkmoveno,
stockmoves.stockid,
stockmaster.description,
stockmaster.volume,
stockmaster.kgs,
stockmaster.mbflag,
stockmaster.controlled,
stockmaster.serialised,
stockmaster.decimalplaces,
stockmaster.taxcatid,
stockmaster.units,
stockmaster.discountcategory,
(stockmoves.price * " . $_SESSION['CurrencyRate'] . ") AS price, -
stockmoves.qty as quantity,
stockmoves.discountpercent,
stockmoves.trandate,
stockmaster.eoq,
stockmaster.materialcost
+ stockmaster.labourcost
+ stockmaster.overheadcost AS standardcost,
stockmoves.narrative
FROM stockmoves, stockmaster
WHERE stockmoves.stockid = stockmaster.stockid
AND stockmoves.transno ='" . $_GET['InvoiceNumber'] . "'
AND stockmoves.type=10
AND stockmoves.show_on_inv_crds=1";
$ErrMsg = _('This invoice can not be credited using this program') . '. ' . _('A manual credit note will need to be prepared') . '. ' . _('The line items of the order cannot be retrieved because');
$Dbgmsg = _('The SQL used to get the transaction header was');
$LineItemsResult = DB_query($LineItemsSQL,$db,$ErrMsg, $DbgMsg);
if (DB_num_rows($LineItemsResult)>0) {
while ($myrow=DB_fetch_array($LineItemsResult)) {
$LineNumber = $_SESSION['CreditItems']->LineCounter;
$_SESSION['CreditItems']->add_to_cart($myrow['stockid'],
$myrow['quantity'],
$myrow['description'],
$myrow['price'],
$myrow['discountpercent'],
$myrow['units'],
$myrow['volume'],
$myrow['kgs'],
0,
$myrow['mbflag'],
$myrow['trandate'],
0,
$myrow['discountcategory'],#
0,
$myrow['controlled'],
$myrow['serialised'],
$myrow['decimalplaces'],
2,
$myrow['narrative'],
'No',
-1,
$myrow['taxcatid'],
'',
'',
$myrow['standardcost'],
$myrow['eoq'],
'',
1,
1);
$_SESSION['CreditItems']->GetExistingTaxes($LineNumber, $myrow['stkmoveno']);
if ($myrow['controlled']==1){/* Populate the SerialItems array too*/
$SQL = "SELECT serialno,
moveqty
FROM stockserialmoves
WHERE stockmoveno='" . $myrow['stkmoveno'] . "'
AND stockid = '" . $myrow['stockid'] . "'";
$ErrMsg = _('This invoice can not be credited using this program') . '. ' . _('A manual credit note will need to be prepared') . '. ' . _('The line item') . ' ' . $myrow['stockid'] . ' ' . _('is controlled but the serial numbers or batch numbers could not be retrieved because');
$DbgMsg = _('The SQL used to get the controlled item details was');
$SerialItemsResult = DB_query($SQL,$db,$ErrMsg, $DbgMsg);
while ($SerialItemsRow = DB_fetch_array($SerialItemsResult)){
$_SESSION['CreditItems']->LineItems[$LineNumber]->SerialItems[$SerialItemsRow['serialno']] = new SerialItem($SerialItemsRow['serialno'], -$SerialItemsRow['moveqty']);
$_SESSION['CreditItems']->LineItems[$LineNumber]->QtyDispatched -= $SerialItemsRow['moveqty'];
}
} /* end if the item is a controlled item */
} /* loop thro line items from stock movement records */
} else { /* there are no stock movement records created for that invoice */
echo '<div class="centre"><a href="' . $rootpath . '/index.php">' . _('Back to the menu') . '</a></div>';
prnMsg( _('There are no line items that were retrieved for this invoice') . '. ' . _('The automatic credit program can not create a credit note from this invoice'),'warn');
include('includes/footer.inc');
exit;
} //end of checks on returned data set
DB_free_result($LineItemsResult);
} else {
prnMsg( _('This invoice can not be credited using the automatic facility') . '<br />' . _('CRITICAL ERROR') . ': ' . _('Please report that a duplicate DebtorTrans header record was found for invoice') . ' ' . $_SESSION['ProcessingCredit'],'warn');
include('includes/footer.inc');
exit;
} //valid invoice record returned from the entered invoice number
}
if (isset($_POST['Location'])){
$_SESSION['CreditItems']->Location = $_POST['Location'];
$NewDispatchTaxProvResult = DB_query("SELECT taxprovinceid FROM locations WHERE loccode='" . $_POST['Location'] . "'",$db);
$myrow = DB_fetch_array($NewDispatchTaxProvResult);
$_SESSION['CreditItems']->DispatchTaxProvince = $myrow['taxprovinceid'];
foreach ($_SESSION['CreditItems']->LineItems as $LineItem) {
$_SESSION['CreditItems']->GetTaxes($LineItem->LineNumber);
}
}
if (isset($_POST['ChargeFreightCost'])){
$_SESSION['CreditItems']->FreightCost = filter_currency_input($_POST['ChargeFreightCost']);
}
foreach ($_SESSION['CreditItems']->FreightTaxes as $FreightTaxLine) {
if (isset($_POST['FreightTaxRate' . $FreightTaxLine->TaxCalculationOrder])){
$_SESSION['CreditItems']->FreightTaxes[$FreightTaxLine->TaxCalculationOrder]->TaxRate = filter_number_input($_POST['FreightTaxRate' . $FreightTaxLine->TaxCalculationOrder])/100;
}
}
if ($_SESSION['CreditItems']->ItemsOrdered > 0 OR isset($_POST['NewItem'])){
if(isset($_GET['Delete'])){
$_SESSION['CreditItems']->remove_from_cart($_GET['Delete']);
}
foreach ($_SESSION['CreditItems']->LineItems as $LineItem) {
if (isset($_POST['Quantity_' . $LineItem->LineNumber])){
$Narrative = $_POST['Narrative_' . $LineItem->LineNumber];
$Quantity = $_POST['Quantity_' . $LineItem->LineNumber];
$Price = filter_number_input($_POST['Price_' . $LineItem->LineNumber]);
$DiscountPercentage = filter_number_input($_POST['Discount_' . $LineItem->LineNumber]);
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%'),'error');
} else {
$_SESSION['CreditItems']->LineItems[$LineItem->LineNumber]->QtyDispatched=$Quantity;
$_SESSION['CreditItems']->LineItems[$LineItem->LineNumber]->Price=$Price;
$_SESSION['CreditItems']->LineItems[$LineItem->LineNumber]->DiscountPercent=($DiscountPercentage/100);
$_SESSION['CreditItems']->LineItems[$LineItem->LineNumber]->Narrative=$Narrative;
}
foreach ($LineItem->Taxes as $TaxLine) {
if (isset($_POST[$LineItem->LineNumber . $TaxLine->TaxCalculationOrder . '_TaxRate'])){
$_SESSION['CreditItems']->LineItems[$LineItem->LineNumber]->Taxes[$TaxLine->TaxCalculationOrder]->TaxRate = filter_number_input($_POST[$LineItem->LineNumber . $TaxLine->TaxCalculationOrder . '_TaxRate'])/100;
}
}
}
}
}
/* Always display credit quantities
NB QtyDispatched in the LineItems array is used for the quantity to credit */
echo '<p class="page_title_text"><img src="'.$rootpath.'/css/'.$theme.'/images/credit.gif" title="' . _('Search') . '" alt="" />' . $title.'</p>';
if (!isset($_POST['ProcessCredit'])) {
echo '<form action="' . htmlspecialchars($_SERVER['PHP_SELF'], ENT_QUOTES, 'UTF-8') .'" method="post">';
echo '<input type="hidden" name="FormID" value="' . $_SESSION['FormID'] . '" />';
echo '<table cellpadding="2" class="selection"><tr>';
echo '<tr><th colspan="13" class="header">' . _('Credit Invoice') . ' ' . $_SESSION['ProcessingCredit'] . '<b>'.' - ' . $_SESSION['CreditItems']->CustomerName . '</b> ' . _('Credit Note amounts stated in') . ' ' . $_SESSION['CreditItems']->DefaultCurrency . '</th>';
echo '</th></tr>';
echo '<th>' . _('Item Code') . '</th>
<th>' . _('Item Description') . '</th>
<th>' . _('Invoiced') . '</th>
<th>' . _('Units') . '</th>
<th>' . _('Credit') . '<br />' . _('Quantity') . '</th>
<th>' . _('Price') . '</th>
<th>' . _('Discount') . ' %' . '</th>
<th>' . _('Total') . '<br />' . _('Excl Tax') . '</th>
<th>' . _('Tax Authority') . '</th>
<th>' . _('Tax') . ' %' . '</th>
<th>' . _('Tax') . '<br />' . _('Amount') . '</th>
<th>' . _('Total') . '<br />' . _('Incl Tax') . '</th></tr>';
$_SESSION['CreditItems']->total = 0;
$_SESSION['CreditItems']->totalVolume = 0;
$_SESSION['CreditItems']->totalWeight = 0;
}
$TaxTotals = array();
$TaxGLCodes = array();
$TaxTotal =0;
/*show the line items on the invoice with the quantity to credit and price being available for modification */
$k=0; //row colour counter
$j=0; //row counter
foreach ($_SESSION['CreditItems']->LineItems as $LnItm) {
$LineTotal =($LnItm->QtyDispatched * $LnItm->Price * (1 - $LnItm->DiscountPercent));
if (!isset($_POST['ProcessCredit'])) {
$_SESSION['CreditItems']->total += $LineTotal;
$_SESSION['CreditItems']->totalVolume += $LnItm->QtyDispatched * $LnItm->Volume;
$_SESSION['CreditItems']->totalWeight += $LnItm->QtyDispatched * $LnItm->Weight;
if ($k==1){
$RowStarter = 'class="EvenTableRows"';
$k=0;
} else {
$RowStarter = 'class="OddTableRows"';
$k=1;
}
$j++;
echo '<tr '.$RowStarter.'><td>' . $LnItm->StockID . '</td>
<td>' . $LnItm->ItemDescription . '</td>
<td class="number">' . locale_number_format($LnItm->Quantity,$LnItm->DecimalPlaces) . '</td>
<td>' . $LnItm->Units . '</td>';
if ($LnItm->Controlled==1){
echo '<td><input type="hidden" name="Quantity_' . $LnItm->LineNumber .'" value="' . $LnItm->QtyDispatched . '" /><a href="'.$rootpath.'/CreditItemsControlled.php?LineNo=' . $LnItm->LineNumber . '&CreditInvoice=Yes>' . $LnItm->QtyDispatched . '</a></td>';
} else {
echo '<td><input tabindex="'.$j.'" type="text" class="number" name="Quantity_' . $LnItm->LineNumber .'" maxlength="6" size="6" value="' . locale_number_format($LnItm->QtyDispatched, $LnItm->DecimalPlaces) . '" /></td>';
}
$DisplayLineTotal = locale_money_format($LineTotal,$_SESSION['CreditItems']->DefaultCurrency);
$j++;
echo '<td><input tabindex="'.$j.'" type="text" class="number" name="Price_' . $LnItm->LineNumber . '" maxlength="12" size="11" value="' . locale_number_format($LnItm->Price,4) . '" /></td>
<td><input tabindex="'.$j.'" type="text" class="number" name="Discount_' . $LnItm->LineNumber . '" maxlength="5" size="5" value="' . locale_number_format($LnItm->DiscountPercent * 100,2) . '" /></td>
<td class="number">'.$DisplayLineTotal.'</td>';
/*Need to list the taxes applicable to this line */
echo '<td>';
$i=0;
if (is_array($_SESSION['CreditItems']->LineItems[$LnItm->LineNumber]->Taxes) ){
foreach ($_SESSION['CreditItems']->LineItems[$LnItm->LineNumber]->Taxes AS $Tax) {
if ($i>0){
echo '<br />';
}
echo $Tax->TaxAuthDescription;
$i++;
}
}
echo '</td>';
echo '<td class="number">';
}
$sql="SELECT taxid FROM taxauthorities";
$result=DB_query($sql, $db);
while ($myrow=DB_fetch_array($result)) {
$TaxTotals[$myrow['taxid']]=0;
}
$i=0; // initialise the number of taxes iterated through
$TaxLineTotal =0; //initialise tax total for the line
if (is_array($LnItm->Taxes) ){
foreach ($LnItm->Taxes as $Tax) {
if ($i>0){
echo '<br />';
}
if (!isset($_POST['ProcessCredit'])) {
echo '<input type="text" class="number" name="' . $LnItm->LineNumber . $Tax->TaxCalculationOrder . '_TaxRate" maxlength="5" size="5" value="' . locale_number_format($Tax->TaxRate*100,2) . '" />';
}
$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;
}
}
$TaxTotal += $TaxLineTotal;
$DisplayTaxAmount = locale_money_format($TaxLineTotal ,$_SESSION['CreditItems']->DefaultCurrency);
$DisplayGrossLineTotal = locale_money_format($LineTotal+ $TaxLineTotal,$_SESSION['CreditItems']->DefaultCurrency);
if (!isset($_POST['ProcessCredit'])) {
echo '</td>';
echo '<td class="number">' . $DisplayTaxAmount . '</td>
<td class="number">' . $DisplayGrossLineTotal . '</td>
<td><a href="'. htmlspecialchars($_SERVER['PHP_SELF'], ENT_QUOTES, 'UTF-8') . '?Delete=' . $LnItm->LineNumber . '">' . _('Delete') . '</a></td></tr>';
echo '<tr'.$RowStarter . '><td colspan="12"><textarea tabindex="'.$j.'" name="Narrative_' . $LnItm->LineNumber . '" cols=100% rows=1>' . $LnItm->Narrative . '</textarea><br /><hr></td></tr>';
$j++;
}
} /*end foreach loop displaying the invoice lines to credit */
if (!isset($_POST['ChargeFreightCost']) AND !isset($_SESSION['CreditItems']->FreightCost)){
$_POST['ChargeFreightCost']=0;
}
if (!isset($_POST['ProcessCredit'])) {
echo '<tr>
<td colspan="3" class="number">' . _('Freight cost charged on invoice') . '</td>
<td class="number">' . locale_money_format($_SESSION['Old_FreightCost'],$_SESSION['CreditItems']->DefaultCurrency) . '</td>
<td colspan="2" class="number">' . _('Credit Freight Cost') . '</td>
<td><input tabindex="'.$j.'" type="text" class="number" size="6" maxlength="6" name="ChargeFreightCost" value="' . locale_money_format($_SESSION['CreditItems']->FreightCost, $_SESSION['CreditItems']->DefaultCurrency) . '" /></td>';
echo '<td></td><td>';
$i=0; // initialise the number of taxes iterated through
foreach ($_SESSION['CreditItems']->FreightTaxes as $FreightTaxLine) {
if ($i>0){
echo '<br />';
}
echo $FreightTaxLine->TaxAuthDescription;
$i++;
}
echo '</td><td>';
}
$FreightTaxTotal =0; //initialise tax total
$i=0;
foreach ($_SESSION['CreditItems']->FreightTaxes as $FreightTaxLine) {
if ($i>0){
echo '<br />';
}
if (!isset($_POST['ProcessCredit'])) {
echo '<input type="text" class="number" name="FreightTaxRate' . $FreightTaxLine->TaxCalculationOrder . '" maxlength="5" size="5" value="' . locale_number_format($FreightTaxLine->TaxRate * 100, 2) . '" />';
}
if ($FreightTaxLine->TaxOnTax ==1){
$TaxTotals[$FreightTaxLine->TaxAuthID] += ($FreightTaxLine->TaxRate * ($_SESSION['CreditItems']->FreightCost + $FreightTaxTotal));
$FreightTaxTotal += ($FreightTaxLine->TaxRate * ($_SESSION['CreditItems']->FreightCost + $FreightTaxTotal));
} else {
$TaxTotals[$FreightTaxLine->TaxAuthID] += ($FreightTaxLine->TaxRate * $_SESSION['CreditItems']->FreightCost);
$FreightTaxTotal += ($FreightTaxLine->TaxRate * $_SESSION['CreditItems']->FreightCost);
}
$i++;
$TaxGLCodes[$FreightTaxLine->TaxAuthID] = $FreightTaxLine->TaxGLCode;
}
if (!isset($_POST['ProcessCredit'])) {
echo '</td>';
echo '<td class="number">' . locale_money_format($FreightTaxTotal,$_SESSION['CreditItems']->DefaultCurrency) . '</td>
<td class="number">' . locale_money_format($FreightTaxTotal+ $_SESSION['CreditItems']->FreightCost,$_SESSION['CreditItems']->DefaultCurrency) . '</td>
</tr>';
}
$TaxTotal += $FreightTaxTotal;
$DisplayTotal = locale_money_format($_SESSION['CreditItems']->total + $_SESSION['CreditItems']->FreightCost,$_SESSION['CreditItems']->DefaultCurrency);
if (!isset($_POST['ProcessCredit'])) {
echo '<tr>
<td colspan="7" class="number">' . _('Credit Totals') . '</td>
<td class="number"><hr><b>' . $DisplayTotal . '</b><hr></td>
<td colspan="2"></td>
<td class="number"><hr><b>' . locale_money_format($TaxTotal,$_SESSION['CreditItems']->DefaultCurrency) . '<hr></td>
<td class="number"><hr><b>' . locale_money_format($TaxTotal+($_SESSION['CreditItems']->total + $_SESSION['CreditItems']->FreightCost),$_SESSION['CreditItems']->DefaultCurrency) . '</b><hr></td>
</tr></table>';
}
$DefaultDispatchDate = Date($_SESSION['DefaultDateFormat']);
$OKToProcess = true;
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;
}
if (isset($_POST['ProcessCredit']) AND $OKToProcess == true) {
/* SQL to process the postings for sales credit notes... First Get the area where the credit note is to from the branches table */
$SQL = "SELECT area
FROM custbranch
WHERE custbranch.debtorno ='". $_SESSION['CreditItems']->DebtorNo . "'
AND custbranch.branchcode = '" . $_SESSION['CreditItems']->Branch . "'";
$Result = DB_query($SQL,$db);
$myrow = DB_fetch_row($Result);
$Area = $myrow[0];
DB_free_result($Result);
/*company record is read in on login and has information on GL Links and debtors GL account*/
if ($_SESSION['CompanyRecord']==0){
/*The company data and preferences could not be retrieved for some reason */
prnMsg(_('The company information and preferences could not be retrieved') . ' - ' . _('see your system administrator'),'error');
include('includes/footer.inc');
exit;
}
/*Now Get the next credit note number - function in SQL_CommonFunctions*/
$CreditNo = GetNextTransNo(11, $db);
$PeriodNo = GetPeriod($DefaultDispatchDate, $db);
/*Start an SQL transaction */
$SQL = 'BEGIN';
$Result = DB_query($SQL,$db);
$DefaultDispatchDate= FormatDateForSQL($DefaultDispatchDate);
/*Calculate the allocation and see if it is possible to allocate to the invoice being credited */
$SQL = "SELECT (ovamount+ovgst+ovfreight-ovdiscount-alloc) as baltoallocate
FROM debtortrans
WHERE transno=" . $_SESSION['ProcessingCredit'] . " AND type=10";
$Result = DB_query($SQL,$db);
$myrow = DB_fetch_row($Result);
/*Do some rounding */
$_SESSION['CreditItems']->total = round($_SESSION['CreditItems']->total,2);
$TaxTotal = round($TaxTotal,2);
$Allocate_amount=0;
$Settled =0;
$SettledInvoice=0;
if ($myrow[0]>0){ /*the invoice is not already fully allocated */
if ($myrow[0] > ($_SESSION['CreditItems']->total + $_SESSION['CreditItems']->FreightCost + $TaxTotal)){
$Allocate_amount = $_SESSION['CreditItems']->total + $_SESSION['CreditItems']->FreightCost + $TaxTotal;
$Settled = 1;
} else if ($myrow[0] > ($_SESSION['CreditItems']->total + $_SESSION['CreditItems']->FreightCost + $TaxTotal)) {
/*the balance left to allocate is less than the credit note value */
$Allocate_amount = $myrow[0];
$SettledInvoice = 1;
$Settled =0;
} else {
$Allocate_amount = $myrow[0];
$SettledInvoice = 1;
$Settled =1;
}
/*Now need to update the invoice DebtorTrans record for the amount to be allocated and if the invoice is now settled*/
$SQL = "UPDATE debtortrans
SET alloc = alloc + " . $Allocate_amount . ",
settled='" . $SettledInvoice . "'
WHERE transno = '" . $_SESSION['ProcessingCredit'] . "'
AND type=10";
$ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('The alteration to the invoice record to reflect the allocation of the credit note to the invoice could not be done because');
$DbgMsg = _('The following SQL to update the invoice allocation was used');
$Result = DB_query($SQL,$db,$ErrMsg,$DbgMsg,true);
}
/*Now insert the Credit Note into the DebtorTrans table with the allocations as calculated above*/
$SQL = "INSERT INTO debtortrans (transno,
type,
debtorno,
branchcode,
trandate,
inputdate,
prd,
reference,
tpe,
order_,
ovamount,
ovgst,
ovfreight,
rate,
invtext,
alloc,
settled)
VALUES (". $CreditNo . ",
11,
'" . $_SESSION['CreditItems']->DebtorNo . "',
'" . $_SESSION['CreditItems']->Branch . "',
'" . $DefaultDispatchDate . "',
'" . date('Y-m-d H-i-s') . "',
'" . $PeriodNo . "',
'Inv-" . $_SESSION['ProcessingCredit'] . "',
'" . $_SESSION['CreditItems']->DefaultSalesType . "',
'" . $_SESSION['CreditItems']->OrderNo . "',
-" . filter_currency_input($_SESSION['CreditItems']->total) . ",
-" . filter_currency_input($TaxTotal) . ",
-" . filter_currency_input($_SESSION['CreditItems']->FreightCost) . ",
'" . $_SESSION['CurrencyRate'] . "',
'" . $_POST['CreditText'] . "',
-" . filter_currency_input($Allocate_amount) . ",
'" . $Settled . "')";
$ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('The customer credit note transaction could not be added to the database because');
$DbgMsg = _('The following SQL to insert the customer credit note was used');
$Result = DB_query($SQL,$db,$ErrMsg, $DbgMsg, true);
$CreditTransID = DB_Last_Insert_ID($db,'debtortrans','id');
/* Insert the tax totals for each tax authority where tax was charged on the invoice */
foreach ($TaxTotals AS $TaxAuthID => $TaxAmount) {
$SQL = "INSERT INTO debtortranstaxes (
debtortransid,
taxauthid,
taxamount)
VALUES ('" . $CreditTransID . "',
'" . $TaxAuthID . "',
'-" . filter_currency_input($TaxAmount/$_SESSION['CurrencyRate']) . "')";
$ErrMsg =_('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('The debtor transaction taxes records could not be inserted because');
$DbgMsg = _('The following SQL to insert the debtor transaction taxes record was used');
$Result = DB_query($SQL,$db,$ErrMsg,$DbgMsg,true);
}
/*Now insert the allocation record if > 0 */
if ($Allocate_amount!=0){
$SQL = "INSERT INTO custallocns (amt,
transid_allocfrom,
transid_allocto,
datealloc)
VALUES ('" . $Allocate_amount . "',
'" . $CreditTransID . "',
'" . $_SESSION['CreditItems']->TransID . "',
'" . Date('Y-m-d') . "')";
$ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('The allocation record for the credit note could not be added to the database because');
$DbgMsg = _('The following SQL to insert the allocation record for the credit note was used');
$Result = DB_query($SQL,$db,$ErrMsg,$DbgMsg,true);
}
/* Update sales order details quantity invoiced less this credit quantity. */
foreach ($_SESSION['CreditItems']->LineItems as $CreditLine) {
if ($CreditLine->QtyDispatched >0){
$LocalCurrencyPrice= round(($CreditLine->Price / $_SESSION['CurrencyRate']),2);
/*Determine the type of stock item being credited */
$SQL = "SELECT mbflag FROM stockmaster WHERE stockid = '" . $CreditLine->StockID . "'";
$Result = DB_query($SQL,
$db,
_('Could not determine if the item') . ' ' . $CreditLine->StockID . ' ' . _('is purchased or manufactured'),
_('The SQL used that failed was'),true);
$MBFlagRow = DB_fetch_row($Result);
$MBFlag = $MBFlagRow[0];
if ($MBFlag=='M' OR $MBFlag=='B'){
/*Need to get the current location quantity will need it later for the stock movements */
$SQL="SELECT locstock.quantity
FROM locstock
WHERE locstock.stockid='" . $CreditLine->StockID . "'
AND loccode= '" . $_SESSION['CreditItems']->Location . "'";
$Result = DB_query($SQL, $db);
if (DB_num_rows($Result)==1){
$LocQtyRow = DB_fetch_row($Result);
$QtyOnHandPrior = $LocQtyRow[0];
} else {
/*There must actually be some error this should never happen */
$QtyOnHandPrior = 0;
}
} else {
$QtyOnHandPrior =0; //because its a dummy/assembly/kitset part
}
if ($_POST['CreditType']=='Return'){
/* some want this some do not */
$SQL = "UPDATE salesorderdetails
SET qtyinvoiced = qtyinvoiced - " . filter_number_input($CreditLine->QtyDispatched) . ",
completed=0
WHERE orderno = '" . $_SESSION['CreditItems']->OrderNo . "'
AND stkcode = '" . $CreditLine->StockID . "'
AND orderlineno='" . $CreditLine->LineNumber."'";
$ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('The sales order detail record could not be updated for the reduced quantity invoiced because');
$DbgMsg = _('The following SQL to update the sales order detail record was used');
$Result = DB_query($SQL,$db,$ErrMsg,$DbgMsg,true);
/* Update location stock records if not a dummy stock item */
if ($MBFlag=='B' OR $MBFlag=='M') {
$SQL = "UPDATE locstock
SET locstock.quantity = locstock.quantity + " . filter_number_input($CreditLine->QtyDispatched) . "
WHERE locstock.stockid = '" . $CreditLine->StockID . "'
AND loccode = '" . $_SESSION['CreditItems']->Location . "'";
$ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('Location stock record could not be updated because');
$DbgMsg = _('The following SQL to update the location stock record was used');
$Result = DB_query($SQL, $db, $ErrMsg,$DbgMsg,true);
} else if ($MBFlag=='A'){ /* its an assembly */
/*Need to get the BOM for this part and make stock moves for the components
and of course update the Location stock balances */
$StandardCost =0; /*To start with - accumulate the cost of the comoponents for use in journals later on */
$sql = "SELECT bom.component,
bom.quantity,
stockmaster.materialcost + stockmaster.labourcost + stockmaster.overheadcost AS standard
FROM bom,
stockmaster
WHERE bom.component=stockmaster.stockid
AND bom.parent='" . $CreditLine->StockID . "'
AND bom.effectiveto > '" . Date('Y-m-d') . "'
AND bom.effectiveafter < '" . Date('Y-m-d') . "'";
$ErrMsg = _('Could not retrieve assembly components from the database for') . ' ' . $CreditLine->StockID . ' ' . _('because');
$DbgMsg = _('The SQL that failed was');
$AssResult = DB_query($sql,$db, $ErrMsg, $DbgMsg, true);
while ($AssParts = DB_fetch_array($AssResult,$db)){
$StandardCost += $AssParts['standard'];
/*Determine the type of stock item being credited */
$SQL = "SELECT mbflag
FROM
stockmaster
WHERE stockid = '" . $AssParts['component'] . "'";
$Result = DB_query($SQL,$db);
$MBFlagRow = DB_fetch_row($Result);
$Component_MBFlag = $MBFlagRow[0];
/* Insert stock movements for the stock coming back in - with unit cost */
if ($Component_MBFlag=='M' OR $Component_MBFlag=='B'){
/*Need to get the current location quantity will need it later for the stock movement */
$SQL="SELECT locstock.quantity
FROM locstock
WHERE locstock.stockid='" . $AssParts['component'] . "'
AND loccode= '" . $_SESSION['CreditItems']->Location . "'";
$Result = DB_query($SQL, $db, _('Could not get the current location stock of the assembly component') . ' ' . $AssParts['component'], _('The SQL that failed was'), true);
if (DB_num_rows($Result)==1){
$LocQtyRow = DB_fetch_row($Result);
$QtyOnHandPrior = $LocQtyRow[0];
} else {
/*There must actually be some error this should never happen */
$QtyOnHandPrior = 0;
}
} else {
$QtyOnHandPrior =0; //because its a dummy/assembly/kitset part
}
if ($Component_MBFlag=='M' OR $Component_MBFlag=='B'){
$SQL = "INSERT INTO stockmoves (
stockid,
type,
transno,
loccode,
trandate,
debtorno,
branchcode,
prd,
reference,
qty,
standardcost,
show_on_inv_crds,
newqoh )
VALUES ('" . $AssParts['component'] . "',
11,
'" . $CreditNo . "',
'" . $_SESSION['CreditItems']->Location . "',
'" . $DefaultDispatchDate . "',
'" . $_SESSION['CreditItems']->DebtorNo . "',
'" . $_SESSION['CreditItems']->Branch . "',
'" . $PeriodNo . "',
'" . _('Ex Inv') . ': ' . $_SESSION['ProcessingCredit'] . ' ' . _('Assembly') . ': ' . $CreditLine->StockID . "',
'" . filter_number_input($AssParts['quantity'] * $CreditLine->QtyDispatched) . "',
'" . $AssParts['standard'] . "',
0,
'" . filter_number_input($QtyOnHandPrior + ($AssParts['quantity'] * $CreditLine->QtyDispatched)) . "'
)";
} else {
$SQL = "INSERT INTO stockmoves (
stockid,
type,
transno,
loccode,
trandate,
debtorno,
branchcode,
prd,
reference,
qty,
standardcost,
show_on_inv_crds)
VALUES ('" . $AssParts['component'] . "',
11,
'" . $CreditNo . "',
'" . $_SESSION['CreditItems']->Location . "',
'" . $DefaultDispatchDate . "',
'" . $_SESSION['CreditItems']->DebtorNo . "',
'" . $_SESSION['CreditItems']->Branch . "',
'" . $PeriodNo . "',
'" . _('Ex Inv') . ': ' . $_SESSION['ProcessingCredit'] . ' ' . _('Assembly') . ': ' . $CreditLine->StockID . "',
'" . filter_number_input($AssParts['quantity'] * $CreditLine->QtyDispatched) . "',
'" . $AssParts['standard'] . "',
0)";
}
$ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('Stock movement records for the assembly components of') .' ' . $CreditLine->StockID . ' ' . _('could not be inserted because');
$DbgMsg = _('The following SQL to insert the assembly components stock movement records was used');
$Result = DB_query($SQL, $db, $ErrMsg, $DbgMsg, true);
if ($Component_MBFlag=="M" OR $Component_MBFlag=="B"){
$SQL = "UPDATE locstock
SET locstock.quantity = locstock.quantity + " . filter_number_input($AssParts['quantity'] * $CreditLine->QtyDispatched) . "
WHERE locstock.stockid = '" . $AssParts['component'] . "'
AND loccode = '" . $_SESSION['CreditItems']->Location . "'";
$ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('Location stock record could not be updated for an assembly component because');
$DbgMsg = _('The following SQL to update the components location stock record was used');
$Result = DB_query($SQL,
$db,
$ErrMsg,
$DbgMsg,
true);
}
} /* end of assembly explosion and updates */
/*Update the cart with the recalculated standard cost from the explosion of the assemblys components*/
$_SESSION['CreditItems']->LineItems[$CreditLine->LineNumber]->StandardCost = $StandardCost;
$CreditLine->StandardCost = $StandardCost;
}
/* Insert stock movements for the stock coming back in - with unit cost */
if ($MBFlag=="M" OR $MBFlag=="B"){
$SQL = "INSERT INTO stockmoves (
stockid,
type,
transno,
loccode,
trandate,
debtorno,
branchcode,
price,
prd,
reference,
qty,
discountpercent,
standardcost,
newqoh,
narrative)
VALUES ('" . $CreditLine->StockID . "',
11,
'" . $CreditNo . "',
'" . $_SESSION['CreditItems']->Location . "',
'" . $DefaultDispatchDate . "',
'" . $_SESSION['CreditItems']->DebtorNo . "',
'" . $_SESSION['CreditItems']->Branch . "',
'" . $LocalCurrencyPrice . "',
'" . $PeriodNo . "',
'" . _('Ex Inv') .' - ' . $_SESSION['ProcessingCredit'] . "',
'" . filter_number_input($CreditLine->QtyDispatched) . "',
'" . $CreditLine->DiscountPercent . "',
'" . $CreditLine->StandardCost . "',
'" . ($QtyOnHandPrior + $CreditLine->QtyDispatched) . "',
'" . $CreditLine->Narrative . "')";
} else {
$SQL = "INSERT INTO stockmoves (
stockid,
type,
transno,
loccode,
trandate,
debtorno,
branchcode,
price,
prd,
reference,
qty,
discountpercent,
standardcost,
narrative)
VALUES ('" . $CreditLine->StockID . "',
11,
'" . $CreditNo . "',
'" . $_SESSION['CreditItems']->Location . "',
'" . $DefaultDispatchDate . "',
'" . $_SESSION['CreditItems']->DebtorNo . "',
'" . $_SESSION['CreditItems']->Branch . "',
'" . $LocalCurrencyPrice . "',
'" . $PeriodNo . "',
'" . _('Ex Inv') . " - " . $_SESSION['ProcessingCredit'] . "',
'" . filter_number_input($CreditLine->QtyDispatched) . "',
'" . $CreditLine->DiscountPercent . "',
'" . $CreditLine->StandardCost . "',
'" . $CreditLine->Narrative . "'
)";
}
$ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('Stock movement records could not be inserted because');
$DbgMsg = _('The following SQL to insert the stock movement records was used');
$Result = DB_query($SQL, $db,$ErrMsg,$DbgMsg,true);
$StkMoveNo = DB_Last_Insert_ID($db,'stockmoves','stkmoveno');
/*Insert the StockSerialMovements and update the StockSerialItems for controlled items*/
//echo "<div align=left><pre>"; var_dump($CreditLine); echo "</pre> </div>";
if ($CreditLine->Controlled ==1){
foreach($CreditLine->SerialItems as $Item){
/*We need to add the StockSerialItem record and The StockSerialMoves as well */
$SQL = "SELECT quantity from stockserialitems
WHERE stockid='" . $CreditLine->StockID . "'
AND loccode='" . $_SESSION['CreditItems']->Location . "'
AND serialno='" . $Item->BundleRef . "'";
$ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('The serial stock item record could not be selected because');
$DbgMsg = _('The following SQL to select the serial stock item record was used');
$Result = DB_query($SQL, $db, $ErrMsg, $DbgMsg, true);
if (DB_num_rows($Result)==0){
$SQL = "INSERT INTO stockserialitems (stockid,
loccode,
serialno,
quantity)
VALUES
('" . $CreditLine->StockID . "',
'" . $_SESSION['CreditItems']->Location . "',
'" . $Item->BundleRef . "',
'". $Item->BundleQty ."')";
$ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('The serial stock item record could not be updated because');
$DbgMsg = _('The following SQL to update the serial stock item record was used');
$Result = DB_query($SQL, $db, $ErrMsg, $DbgMsg, true);
} else {
$SQL = "UPDATE stockserialitems
SET quantity= quantity + " . $Item->BundleQty . "
WHERE stockid='" . $CreditLine->StockID . "'
AND loccode='" . $_SESSION['CreditItems']->Location . "'
AND serialno='" . $Item->BundleRef . "'";
$ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('The serial stock item record could not be updated because');
$DbgMsg = _('The following SQL to update the serial stock item record was used');
$Result = DB_query($SQL, $db, $ErrMsg, $DbgMsg, true);
}
/* now insert the serial stock movement */
$SQL = "INSERT INTO stockserialmoves (stockmoveno,
stockid,
serialno,
moveqty)
VALUES ('" . $StkMoveNo . "',
'" . $CreditLine->StockID . "',
'" . $Item->BundleRef . "',
'" . $Item->BundleQty . "')";
$ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('The serial stock movement record could not be inserted because');
$DbgMsg = _('The following SQL to insert the serial stock movement records was used');
$Result = DB_query($SQL, $db, $ErrMsg, $DbgMsg, true);
}/* foreach controlled item in the serialitems array */
} /*end if the orderline is a controlled item */
} elseif ($_POST['CreditType']=='WriteOff') {
/*Insert a stock movement coming back in to show the credit note and
a reversing stock movement to show the write off
no mods to location stock records*/
$SQL = "INSERT INTO stockmoves (
stockid,
type,
transno,
loccode,
trandate,
debtorno,
branchcode,
price,
prd,
reference,
qty,
discountpercent,
standardcost,
newqoh,
narrative )
VALUES ('" . $CreditLine->StockID . "',
11,
'" . $CreditNo . "',
'" . $_SESSION['CreditItems']->Location . "',
'" . $DefaultDispatchDate . "',
'" . $_SESSION['CreditItems']->DebtorNo . "',
'" . $_SESSION['CreditItems']->Branch . "',
'" . $LocalCurrencyPrice . "',
'" . $PeriodNo . "',
'" . _('Ex Inv') . ' - ' . $_SESSION['ProcessingCredit'] . "',
'" . filter_number_input($CreditLine->QtyDispatched) . "',
'" . $CreditLine->DiscountPercent . "',
'" . $CreditLine->StandardCost . "',
'" . ($QtyOnHandPrior +$CreditLine->QtyDispatched) . "',
'" . $CreditLine->Narrative . "')";
$ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('Stock movement records could not be inserted because');
$DbgMsg = _('The following SQL to insert the stock movement records was used');
$Result = DB_query($SQL, $db,$ErrMsg, $DbgMsg, true);
$SQL = "INSERT INTO stockmoves (
stockid,
type,
transno,
loccode,
trandate,
debtorno,
branchcode,