forked from webERP-team/webERP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SupplierCredit.php
1340 lines (1063 loc) · 58.9 KB
/
SupplierCredit.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
/*This page is very largely the same as the SupplierInvoice.php script
the same result could have been acheived by using if statements in that script and just having the one
SupplierTransaction.php script. However, to aid readability - variable names have been changed -
and reduce clutter (in the form of a heap of if statements) two separate scripts have been used,
both with very similar code.
This does mean that if the logic is to be changed for supplier transactions then it needs to be changed
in both scripts.
This is widely considered poor programming but in my view, much easier to read for the uninitiated
*/
/*The supplier transaction uses the SuppTrans class to hold the information about the credit note
the SuppTrans class contains an array of GRNs objects - containing details of GRNs for invoicing and also
an array of GLCodes objects - only used if the AP - GL link is effective */
include('includes/DefineSuppTransClass.php');
/* Session started in header.php for password checking and authorisation level check */
include('includes/session.php');
$Title = _('Supplier Credit Note');
include('includes/header.php');
include('includes/SQL_CommonFunctions.inc');
if (isset($_GET['New'])) {
unset($_SESSION['SuppTrans']);
}
if (!isset($_SESSION['SuppTrans']->SupplierName)) {
$sql="SELECT suppname FROM suppliers WHERE supplierid='" . $_GET['SupplierID']."'";
$result = DB_query($sql);
$myrow = DB_fetch_row($result);
$SupplierName=$myrow[0];
} else {
$SupplierName=$_SESSION['SuppTrans']->SupplierName;
}
echo '<p class="page_title_text"><img src="'.$RootPath.'/css/'.$Theme.'/images/transactions.png" title="' . _('Supplier Credit Note') . '" alt="" />' . ' '
. _('Enter Supplier Credit Note:') . ' ' . $SupplierName;
echo '</p>';
if (isset($_GET['SupplierID']) and $_GET['SupplierID']!=''){
/*It must be a new credit note entry - clear any existing credit note details from the SuppTrans object and initiate a newy*/
if (isset($_SESSION['SuppTrans'])){
unset ($_SESSION['SuppTrans']->GRNs);
unset ($_SESSION['SuppTrans']->Shipts);
unset ($_SESSION['SuppTrans']->GLCodes);
unset($_SESSION['SuppTrans']->Assets);
unset ($_SESSION['SuppTrans']);
}
if (isset( $_SESSION['SuppTransTmp'])){
unset ( $_SESSION['SuppTransTmp']->GRNs);
unset ( $_SESSION['SuppTransTmp']->GLCodes);
unset ( $_SESSION['SuppTransTmp']);
}
$_SESSION['SuppTrans'] = new SuppTrans;
/*Now retrieve supplier information - name, currency, default ex rate, terms, tax rate etc */
$sql = "SELECT suppliers.suppname,
suppliers.supplierid,
paymentterms.terms,
paymentterms.daysbeforedue,
paymentterms.dayinfollowingmonth,
suppliers.currcode,
currencies.rate AS exrate,
currencies.decimalplaces AS currdecimalplaces,
suppliers.taxgroupid,
taxgroups.taxgroupdescription
FROM suppliers INNER JOIN taxgroups
ON suppliers.taxgroupid=taxgroups.taxgroupid
INNER JOIN currencies
ON suppliers.currcode=currencies.currabrev
INNER JOIN paymentterms
ON suppliers.paymentterms=paymentterms.termsindicator
WHERE suppliers.supplierid = '" . $_GET['SupplierID'] . "'";
$ErrMsg = _('The supplier record selected') . ': ' . $_GET['SupplierID'] . ' ' ._('cannot be retrieved because');
$DbgMsg = _('The SQL used to retrieve the supplier details and failed was');
$result = DB_query($sql, $ErrMsg, $DbgMsg);
$myrow = DB_fetch_array($result);
$_SESSION['SuppTrans']->SupplierName = $myrow['suppname'];
$_SESSION['SuppTrans']->TermsDescription = $myrow['terms'];
$_SESSION['SuppTrans']->CurrCode = $myrow['currcode'];
$_SESSION['SuppTrans']->ExRate = $myrow['exrate'];
$_SESSION['SuppTrans']->TaxGroup = $myrow['taxgroupid'];
$_SESSION['SuppTrans']->TaxGroupDescription = $myrow['taxgroupdescription'];
$_SESSION['SuppTrans']->SupplierID = $myrow['supplierid'];
$_SESSION['SuppTrans']->CurrDecimalPlaces = $myrow['currdecimalplaces'];
if ($myrow['daysbeforedue'] == 0){
$_SESSION['SuppTrans']->Terms = '1' . $myrow['dayinfollowingmonth'];
} else {
$_SESSION['SuppTrans']->Terms = '0' . $myrow['daysbeforedue'];
}
$_SESSION['SuppTrans']->SupplierID = $_GET['SupplierID'];
$LocalTaxProvinceResult = DB_query("SELECT taxprovinceid
FROM locations
WHERE loccode = '" . $_SESSION['UserStockLocation'] . "'");
if(DB_num_rows($LocalTaxProvinceResult)==0){
prnMsg(_('The tax province associated with your user account has not been set up in this database. Tax calculations are based on the tax group of the supplier and the tax province of the user entering the invoice. The system administrator should redefine your account with a valid default stocking location and this location should refer to a valid tax province'),'error');
include('includes/footer.php');
exit;
}
$LocalTaxProvinceRow = DB_fetch_row($LocalTaxProvinceResult);
$_SESSION['SuppTrans']->LocalTaxProvince = $LocalTaxProvinceRow[0];
$_SESSION['SuppTrans']->GetTaxes();
$_SESSION['SuppTrans']->GLLink_Creditors = $_SESSION['CompanyRecord']['gllink_creditors'];
$_SESSION['SuppTrans']->GRNAct = $_SESSION['CompanyRecord']['grnact'];
$_SESSION['SuppTrans']->CreditorsAct = $_SESSION['CompanyRecord']['creditorsact'];
$_SESSION['SuppTrans']->InvoiceOrCredit = 'Credit Note'; //note no gettext going on here
} elseif (!isset($_SESSION['SuppTrans'])){
prnMsg(_('To enter a supplier credit note the supplier must first be selected from the supplier selection screen'),'warn');
echo '<br /><a href="' . $RootPath . '/SelectSupplier.php">' . _('Select A Supplier to Enter an Credit Note For') . '</a>';
include('includes/footer.php');
exit;
/*It all stops here if there aint no supplier selected */
}
/* Set the session variables to the posted data from the form if the page has called itself */
if (isset($_POST['ExRate'])){
$_SESSION['SuppTrans']->ExRate = filter_number_format($_POST['ExRate']);
$_SESSION['SuppTrans']->Comments = $_POST['Comments'];
$_SESSION['SuppTrans']->TranDate = $_POST['TranDate'];
if (mb_substr( $_SESSION['SuppTrans']->Terms,0,1)=='1') { /*Its a day in the following month when due */
$DayInFollowingMonth = (int) mb_substr( $_SESSION['SuppTrans']->Terms,1);
$DaysBeforeDue = 0;
} else { /*Use the Days Before Due to add to the invoice date */
$DayInFollowingMonth = 0;
$DaysBeforeDue = (int) mb_substr( $_SESSION['SuppTrans']->Terms,1);
}
$_SESSION['SuppTrans']->DueDate = CalcDueDate($_SESSION['SuppTrans']->TranDate, $DayInFollowingMonth, $DaysBeforeDue);
$_SESSION['SuppTrans']->SuppReference = $_POST['SuppReference'];
if ( $_SESSION['SuppTrans']->GLLink_Creditors == 1){
/*The link to GL from creditors is active so the total should be built up from GLPostings and GRN entries
if the link is not active then OvAmount must be entered manually. */
$_SESSION['SuppTrans']->OvAmount = 0; /* for starters */
if (count($_SESSION['SuppTrans']->GRNs) > 0){
foreach ( $_SESSION['SuppTrans']->GRNs as $GRN){
$_SESSION['SuppTrans']->OvAmount = $_SESSION['SuppTrans']->OvAmount + ($GRN->This_QuantityInv * $GRN->ChgPrice);
}
}
if (count($_SESSION['SuppTrans']->GLCodes) > 0){
foreach ( $_SESSION['SuppTrans']->GLCodes as $GLLine){
$_SESSION['SuppTrans']->OvAmount += $GLLine->Amount;
}
}
if (count($_SESSION['SuppTrans']->Contracts) > 0){
foreach ( $_SESSION['SuppTrans']->Contracts as $Contract){
$_SESSION['SuppTrans']->OvAmount += $Contract->Amount;
}
}
if (count($_SESSION['SuppTrans']->Shipts) > 0){
foreach ( $_SESSION['SuppTrans']->Shipts as $ShiptLine){
$_SESSION['SuppTrans']->OvAmount += $ShiptLine->Amount;
}
}
if (count($_SESSION['SuppTrans']->Assets) > 0){
foreach ( $_SESSION['SuppTrans']->Assets as $FixedAsset){
$_SESSION['SuppTrans']->OvAmount += $FixedAsset->Amount;
}
}
$_SESSION['SuppTrans']->OvAmount = round($_SESSION['SuppTrans']->OvAmount,$_SESSION['SuppTrans']->CurrDecimalPlaces);
} else {
/*OvAmount must be entered manually */
$_SESSION['SuppTrans']->OvAmount = round(filter_number_format($_POST['OvAmount']),$_SESSION['SuppTrans']->CurrDecimalPlaces);
}
}
if (isset($_POST['GRNS'])
AND $_POST['GRNS'] == _('Purchase Orders')){
/*This ensures that any changes in the page are stored in the session before calling the grn page */
echo '<meta http-equiv="Refresh" content="0; url=' . $RootPath . '/SuppCreditGRNs.php">';
echo '<br />' .
_('You should automatically be forwarded to the entry of credit notes against goods received page') . '. ' . _('If this does not happen') . ' (' . _('if the browser does not support META Refresh') . ') ' . '<a href="' . $RootPath . '/SuppCreditGRNs.php">' . _('click here') . '</a> ' . _('to continue') . '.
<br />';
include('includes/footer.php');
exit;
}
if (isset($_POST['Shipts'])){
/*This ensures that any changes in the page are stored in the session before calling the shipments page */
echo '<meta http-equiv="Refresh" content="0; url=' . $RootPath . '/SuppShiptChgs.php">';
echo '<br />
' . _('You should automatically be forwarded to the entry of credit notes against shipments page') . '. ' . _('If this does not happen') . ' (' . _('if the browser does not support META Refresh') . ') ' . '<a href="' . $RootPath . '/SuppShiptChgs.php">' . _('click here') . '</a> ' . _('to continue') . '.
<br />';
include('includes/footer.php');
exit;
}
if (isset($_POST['GL'])
AND $_POST['GL'] == _('General Ledger')){
/*This ensures that any changes in the page are stored in the session before calling the shipments page */
echo '<meta http-equiv="Refresh" content="0; url=' . $RootPath . '/SuppTransGLAnalysis.php">';
echo '<br />
' . _('You should automatically be forwarded to the entry of credit notes against the general ledger page') . '. ' . _('If this does not happen') . ' (' . _('if the browser does not support META Refresh') . ') ' . '<a href="' . $RootPath . '/SuppTransGLAnalysis.php">' . _('click here') . '</a> ' . _('to continue') . '.
<br />';
include('includes/footer.php');
exit;
}
if (isset($_POST['Contracts'])
AND $_POST['Contracts'] == _('Contracts')){
/*This ensures that any changes in the page are stored in the session before calling the shipments page */
echo '<meta http-equiv="refresh" content="0; url=' . $RootPath . '/SuppContractChgs.php">';
echo '<div class="centre">
' . _('You should automatically be forwarded to the entry of supplier credit notes against contracts page') . '. ' . _('If this does not happen') . ' (' . _('if the browser does not support META Refresh'). ') ' . '<a href="' . $RootPath . '/SuppContractChgs.php">' . _('click here') . '</a> ' . _('to continue') . '.
</div>
<br />';
exit;
}
if (isset($_POST['FixedAssets'])
AND $_POST['FixedAssets'] == _('Fixed Assets')){
/*This ensures that any changes in the page are stored in the session before calling the shipments page */
echo '<meta http-equiv="refresh" content="0; url=' . $RootPath . '/SuppFixedAssetChgs.php">';
echo '<div class="centre">
' . _('You should automatically be forwarded to the entry of invoices against fixed assets page') . '. ' . _('If this does not happen') . ' (' . _('if the browser does not support META Refresh'). ') ' . '<a href="' . $RootPath . '/SuppFixedAssetChgs.php">' . _('click here') . '</a> ' . _('to continue') . '.
</div>
<br />';
exit;
}
/* everything below here only do if a Supplier is selected
fisrt add a header to show who we are making an credit note for */
echo '<table class="selection">
<tr><th>' . _('Supplier') . '</th>
<th>' . _('Currency') . '</th>
<th>' . _('Terms') . '</th>
<th>' . _('Tax Group') . '</th>
</tr>';
echo '<tr>
<th><b>' . $_SESSION['SuppTrans']->SupplierID . ' - ' . $_SESSION['SuppTrans']->SupplierName . '</b></th>
<th><b>' . $_SESSION['SuppTrans']->CurrCode . '</b></th>
<td><b>' . $_SESSION['SuppTrans']->TermsDescription . '</b></td>
<td><b>' . $_SESSION['SuppTrans']->TaxGroupDescription . '</b></td>
</tr>
</table>';
echo '<form action="' . htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') . '" method="post" id="form1">';
echo '<div>';
echo '<input type="hidden" name="FormID" value="' . $_SESSION['FormID'] . '" />';
echo '<br />
<table class="selection">';
echo '<tr>
<td style="color:red">' . _('Supplier Credit Note Reference') . ':</td>
<td><input type="text" required="required" size="20" maxlength="20" name="SuppReference" value="' . $_SESSION['SuppTrans']->SuppReference . '" /></td>';
if (!isset($_SESSION['SuppTrans']->TranDate)){
$_SESSION['SuppTrans']->TranDate= Date($_SESSION['DefaultDateFormat'], Mktime(0,0,0,Date('m'),Date('d')-1,Date('y')));
}
echo '<td style="color:red">' . _('Credit Note Date') . ' (' . _('in format') . ' ' . $_SESSION['DefaultDateFormat'] . ') :</td>
<td><input type="text" class="date" size="11" maxlength="10" name="TranDate" value="' . $_SESSION['SuppTrans']->TranDate . '" /></td>
<td style="color:red">' . _('Exchange Rate') . ':</td>
<td><input type="text" class="number" size="11" maxlength="10" name="ExRate" value="' . locale_number_format($_SESSION['SuppTrans']->ExRate,'Variable') . '" /></td>
</tr>
</table>';
echo '<br />
<div class="centre">
<input type="submit" name="GRNS" value="' . _('Purchase Orders') . '"/>
<input type="submit" name="Shipts" value="' . _('Shipments') . '" />
<input type="submit" name="Contracts" value="' . _('Contracts') . '" /> ';
if ( $_SESSION['SuppTrans']->GLLink_Creditors ==1){
echo '<input type="submit" name="GL" value="' . _('General Ledger') . '" /> ';
}
echo '<input type="submit" name="FixedAssets" value="' . _('Fixed Assets') . '" />
</div>
<br />';
if (count($_SESSION['SuppTrans']->GRNs)>0){ /*if there are some GRNs selected for crediting then */
/*Show all the selected GRNs so far from the SESSION['SuppInv']->GRNs array
Note that the class for carrying GRNs refers to quantity invoiced read credited in this context*/
echo '<table class="selection">
<tr><th colspan="6">' . _('Purchase Order Credits') . '</th></tr>';
$TableHeader = '<tr><th>' . _('GRN') . '</th>
<th>' . _('Item Code') . '</th>
<th>' . _('Description') . '</th>
<th>' . _('Quantity') . '<br />' . _('Credited') . '</th>
<th>' . _('Price Credited') . '<br />' . _('in') . ' ' . $_SESSION['SuppTrans']->CurrCode . '</th>
<th>' . _('Line Total') . '<br />' . _('in') . ' ' . $_SESSION['SuppTrans']->CurrCode . '</th>
</tr>';
echo $TableHeader;
$TotalGRNValue=0;
foreach ($_SESSION['SuppTrans']->GRNs as $EnteredGRN){
echo '<tr><td>' . $EnteredGRN->GRNNo . '</td>
<td>' . $EnteredGRN->ItemCode . '</td>
<td>' . $EnteredGRN->ItemDescription . '</td>
<td class="number">' . locale_number_format($EnteredGRN->This_QuantityInv,2) . '</td>
<td class="number">' . locale_number_format($EnteredGRN->ChgPrice,$_SESSION['SuppTrans']->CurrDecimalPlaces) . '</td>
<td class="number">' . locale_number_format($EnteredGRN->ChgPrice * $EnteredGRN->This_QuantityInv,$_SESSION['SuppTrans']->CurrDecimalPlaces) . '</td>
<td></tr>';
$TotalGRNValue = $TotalGRNValue + ($EnteredGRN->ChgPrice * $EnteredGRN->This_QuantityInv);
}
echo '<tr><td colspan="5" class="number">' . _('Total Value of Goods Credited') . ':</td>
<td class="number"><U>' . locale_number_format($TotalGRNValue,$_SESSION['SuppTrans']->CurrDecimalPlaces) . '</U></td></tr>';
echo '</table>
<br />';
}
if (count($_SESSION['SuppTrans']->Shipts)>0){ /*if there are any Shipment charges on the credit note*/
echo '<table class="selection">
<tr>
<th colspan="2">' . _('Shipment Credits') . '</th>
</tr>';
$TableHeader = '<tr>
<th>' . _('Shipment') . '</th>
<th>' . _('Amount') . '</th>
</tr>';
echo $TableHeader;
$TotalShiptValue=0;
$i=0;
foreach ($_SESSION['SuppTrans']->Shipts as $EnteredShiptRef){
echo '<tr>
<td>' . $EnteredShiptRef->ShiptRef . '</td>
<td class="number">' . locale_number_format($EnteredShiptRef->Amount,$_SESSION['SuppTrans']->CurrDecimalPlaces) . '</td>
</tr>';
$TotalShiptValue += $EnteredShiptRef->Amount;
}
echo '<tr>
<td class="number" style="color:red">' . _('Total Credited Against Shipments') . ':</td>
<td class="number" style="color:red">' . locale_number_format($TotalShiptValue,$_SESSION['SuppTrans']->CurrDecimalPlaces) . '</td>
</tr>
</table><br />';
}
if (count( $_SESSION['SuppTrans']->Assets) > 0){ /*if there are any fixed assets on the invoice*/
echo '<br />
<table class="selection">
<tr>
<th colspan="3">' . _('Fixed Asset Credits') . '</th>
</tr>';
$TableHeader = '<tr><th>' . _('Asset ID') . '</th>
<th>' . _('Description') . '</th>
<th>' . _('Amount') . ' ' . $_SESSION['SuppTrans']->CurrCode . '</th></tr>';
echo $TableHeader;
$TotalAssetValue = 0;
foreach ($_SESSION['SuppTrans']->Assets as $EnteredAsset){
echo '<tr><td>' . $EnteredAsset->AssetID . '</td>
<td>' . $EnteredAsset->Description . '</td>
<td class="number">' . locale_number_format($EnteredAsset->Amount,$_SESSION['SuppTrans']->CurrDecimalPlaces) . '</td></tr>';
$TotalAssetValue += $EnteredAsset->Amount;
$i++;
if ($i > 15){
$i = 0;
echo $TableHeader;
}
}
echo '<tr>
<td colspan="2" class="number" style="color:red">' . _('Total') . ':</td>
<td class="number" style="color:red">' . locale_number_format($TotalAssetValue,$_SESSION['SuppTrans']->CurrDecimalPlaces) . '</td>
</tr>
</table>';
} //end loop around fixed assets
if (count( $_SESSION['SuppTrans']->Contracts) > 0){ /*if there are any contract charges on the invoice*/
echo '<table class="selection">
<tr>
<th colspan="3">' . _('Contract Charges') . '</th>
</tr>';
$TableHeader = '<tr><th>' . _('Contract') . '</th>
<th>' . _('Narrative') . '</th>
<th>' . _('Amount') . '<br />' . _('in') . ' ' . $_SESSION['SuppTrans']->CurrCode . '</th>
</tr>';
echo $TableHeader;
$TotalContractsValue = 0;
$i=0;
foreach ($_SESSION['SuppTrans']->Contracts as $Contract){
echo '<tr><td>' . $Contract->ContractRef . '</td>
<td>' . $Contract->Narrative . '</td>
<td class="number">' . locale_number_format($Contract->Amount,$_SESSION['SuppTrans']->CurrDecimalPlaces) . '</td>
</tr>';
$TotalContractsValue += $Contract->Amount;
$i++;
if ($i == 15){
$i = 0;
echo $TableHeader;
}
}
echo '<tr><td class="number" colspan="2" style="color:red">' . _('Total Credited against Contracts') . ':</td>
<td class="number">' . locale_number_format($TotalContractsValue,$_SESSION['SuppTrans']->CurrDecimalPlaces) . '</td>
</tr></table><br />';
}
if ($_SESSION['SuppTrans']->GLLink_Creditors ==1){
if (count($_SESSION['SuppTrans']->GLCodes)>0){
echo '<table class="selection">
<tr>
<th colspan="3">' . _('General Ledger Analysis') . '</th>
</tr>';
$TableHeader = '<tr>
<th>' . _('Account') . '</th>
<th>' . _('Account Name') . '</th>
<th>' . _('Narrative') . '</th>
<th>' . _('Tag') . '</th>
<th>' . _('Amount') . '<br />' . _('in') . ' ' . $_SESSION['SuppTrans']->CurrCode . '</th>
</tr>';
echo $TableHeader;
$TotalGLValue=0;
foreach ($_SESSION['SuppTrans']->GLCodes as $EnteredGLCode){
echo '<tr>
<td>' . $EnteredGLCode->GLCode . '</td>
<td>' . $EnteredGLCode->GLActName . '</td>
<td>' . $EnteredGLCode->Narrative . '</td>
<td>' . $EnteredGLCode->Tag . ' - ' . $EnteredGLCode->TagName . '</td>
<td class="number">' . locale_number_format($EnteredGLCode->Amount,$_SESSION['SuppTrans']->CurrDecimalPlaces) . '</td>
</tr>';
$TotalGLValue += $EnteredGLCode->Amount;
$i++;
if ($i>15){
$i=0;
echo $TableHeader;
}
}
echo '<tr>
<td colspan="4" class="number" style="color:red">' . _('Total GL Analysis') . ':</td>
<td class="number" style="color:red">' . locale_number_format($TotalGLValue,$_SESSION['SuppTrans']->CurrDecimalPlaces) . '</td>
</tr>
</table>
<br />';
}
if (!isset($TotalGRNValue)) {
$TotalGRNValue=0;
}
if (!isset($TotalGLValue)) {
$TotalGLValue=0;
}
if (!isset($TotalShiptValue)) {
$TotalShiptValue=0;
}
if (!isset($TotalContractsValue)){
$TotalContractsValue = 0;
}
if (!isset($TotalAssetValue)){
$TotalAssetValue = 0;
}
$_SESSION['SuppTrans']->OvAmount = round($TotalGRNValue + $TotalGLValue + $TotalAssetValue + $TotalShiptValue + $TotalContractsValue,$_SESSION['SuppTrans']->CurrDecimalPlaces);
echo '<table class="selection">
<tr>
<td style="color:red">' . _('Credit Amount in Supplier Currency') . ':</td>
<td colspan="2" class="number">' . locale_number_format($_SESSION['SuppTrans']->OvAmount,$_SESSION['SuppTrans']->CurrDecimalPlaces);
echo '<input type="hidden" name="OvAmount" value="' . locale_number_format($_SESSION['SuppTrans']->OvAmount,$_SESSION['SuppTrans']->CurrDecimalPlaces) . '" /></td></tr>';
} else {
echo '<table class="selection">
<tr>
<td style="color:red">' . _('Credit Amount in Supplier Currency') .
':</td>
<td colspan="2" class="number"><input type="text" size="12" class="number" maxlength="10" name="OvAmount" value="' . locale_number_format($_SESSION['SuppTrans']->OvAmount,$_SESSION['SuppTrans']->CurrDecimalPlaces) . '" /></td></tr>';
}
echo '<tr>
<td colspan="2"><input type="submit" name="ToggleTaxMethod" value="' . _('Update Tax Calculation') . '" /></td>
<td><select name="OverRideTax" onchange="ReloadForm(form1.ToggleTaxMethod)">';
if (isset($_POST['OverRideTax']) AND $_POST['OverRideTax']=='Man'){
echo '<option value="Auto">' . _('Automatic') . '</option>
<option selected="selected" value="Man">' . _('Manual Entry') . '</option>';
} else {
echo '<option selected="selected" value="Auto">' . _('Automatic') . '</option>
<option value="Man">' . _('Manual Entry') . '</option>';
}
echo '</select></td>
</tr>';
$TaxTotal =0; //initialise tax total
foreach ($_SESSION['SuppTrans']->Taxes as $Tax) {
echo '<tr>
<td>' . $Tax->TaxAuthDescription . '</td>
<td>';
/*Set the tax rate to what was entered */
if (isset($_POST['TaxRate' . $Tax->TaxCalculationOrder])){
$_SESSION['SuppTrans']->Taxes[$Tax->TaxCalculationOrder]->TaxRate = filter_number_format($_POST['TaxRate' . $Tax->TaxCalculationOrder])/100;
}
/*If a tax rate is entered that is not the same as it was previously then recalculate automatically the tax amounts */
if (!isset($_POST['OverRideTax'])
OR $_POST['OverRideTax']=='Auto'){
echo ' <input type="text" class="number" name="TaxRate' . $Tax->TaxCalculationOrder . '" maxlength="4" size="4" value="' . locale_number_format($_SESSION['SuppTrans']->Taxes[$Tax->TaxCalculationOrder]->TaxRate * 100,2) . '" />%';
/*Now recaluclate the tax depending on the method */
if ($Tax->TaxOnTax ==1){
$_SESSION['SuppTrans']->Taxes[$Tax->TaxCalculationOrder]->TaxOvAmount = $_SESSION['SuppTrans']->Taxes[$Tax->TaxCalculationOrder]->TaxRate * ($_SESSION['SuppTrans']->OvAmount + $TaxTotal);
} else { /*Calculate tax without the tax on tax */
$_SESSION['SuppTrans']->Taxes[$Tax->TaxCalculationOrder]->TaxOvAmount = $_SESSION['SuppTrans']->Taxes[$Tax->TaxCalculationOrder]->TaxRate * $_SESSION['SuppTrans']->OvAmount;
}
echo '<input type="hidden" name="TaxAmount' . $Tax->TaxCalculationOrder . '" value="' . round($_SESSION['SuppTrans']->Taxes[$Tax->TaxCalculationOrder]->TaxOvAmount,$_SESSION['SuppTrans']->CurrDecimalPlaces) . '" />';
echo '</td><td class="number">' . locale_number_format($_SESSION['SuppTrans']->Taxes[$Tax->TaxCalculationOrder]->TaxOvAmount,$_SESSION['SuppTrans']->CurrDecimalPlaces);
} else { /*Tax being entered manually accept the taxamount entered as is*/
$_SESSION['SuppTrans']->Taxes[$Tax->TaxCalculationOrder]->TaxOvAmount = filter_number_format($_POST['TaxAmount' . $Tax->TaxCalculationOrder]);
echo ' <input type="hidden" name="TaxRate' . $Tax->TaxCalculationOrder . '" value="' . locale_number_format($_SESSION['SuppTrans']->Taxes[$Tax->TaxCalculationOrder]->TaxRate * 100,$_SESSION['SuppTrans']->CurrDecimalPlaces) . '" />';
echo '</td>
<td><input type="text" class="number" size="12" maxlength="12" name="TaxAmount' . $Tax->TaxCalculationOrder . '" value="' . locale_number_format(round($_SESSION['SuppTrans']->Taxes[$Tax->TaxCalculationOrder]->TaxOvAmount,$_SESSION['SuppTrans']->CurrDecimalPlaces),$_SESSION['SuppTrans']->CurrDecimalPlaces) . '" />';
}
$TaxTotal += $_SESSION['SuppTrans']->Taxes[$Tax->TaxCalculationOrder]->TaxOvAmount;
echo '</td></tr>';
}
$DisplayTotal = locale_number_format($_SESSION['SuppTrans']->OvAmount + $TaxTotal,$_SESSION['SuppTrans']->CurrDecimalPlaces);
echo '<tr>
<td style="color:red">' . _('Credit Note Total') . '</td>
<td colspan="2" class="number"><b>' . $DisplayTotal. '</b></td>
</tr>
</table>
<br />';
echo '<table class="selection">
<tr>
<td style="color:red">' . _('Comments') . '</td>
<td><textarea name="Comments" cols="40" rows="2">' . $_SESSION['SuppTrans']->Comments . '</textarea></td>
</tr>
</table>';
echo '<br />
<div class="centre">
<input type="submit" name="PostCreditNote" value="' . _('Enter Credit Note') . '" />
</div>';
if (isset($_POST['PostCreditNote'])){
/*First do input reasonableness checks
then do the updates and inserts to process the credit note entered */
$TaxTotal =0;
foreach ($_SESSION['SuppTrans']->Taxes as $Tax) {
/*Set the tax rate to what was entered */
if (isset($_POST['TaxRate' . $Tax->TaxCalculationOrder])){
$_SESSION['SuppTrans']->Taxes[$Tax->TaxCalculationOrder]->TaxRate = filter_number_format($_POST['TaxRate' . $Tax->TaxCalculationOrder])/100;
}
if ($_POST['OverRideTax']=='Auto' OR !isset($_POST['OverRideTax'])){
/*Now recaluclate the tax depending on the method */
if ($Tax->TaxOnTax ==1){
$_SESSION['SuppTrans']->Taxes[$Tax->TaxCalculationOrder]->TaxOvAmount = $_SESSION['SuppTrans']->Taxes[$Tax->TaxCalculationOrder]->TaxRate * ($_SESSION['SuppTrans']->OvAmount + $TaxTotal);
} else { /*Calculate tax without the tax on tax */
$_SESSION['SuppTrans']->Taxes[$Tax->TaxCalculationOrder]->TaxOvAmount = $_SESSION['SuppTrans']->Taxes[$Tax->TaxCalculationOrder]->TaxRate * $_SESSION['SuppTrans']->OvAmount;
}
} else { /*Tax being entered manually accept the taxamount entered as is*/
$_SESSION['SuppTrans']->Taxes[$Tax->TaxCalculationOrder]->TaxOvAmount = filter_number_format($_POST['TaxAmount' . $Tax->TaxCalculationOrder]);
}
$TaxTotal += $_SESSION['SuppTrans']->Taxes[$Tax->TaxCalculationOrder]->TaxOvAmount;
}
$InputError = False;
if ( $TaxTotal + $_SESSION['SuppTrans']->OvAmount <= 0){
$InputError = True;
prnMsg(_('The credit note as entered cannot be processed because the total amount of the credit note is less than or equal to 0') . '. ' . _('Credit notes are expected to be entered as positive amounts to credit'),'warn');
} elseif (mb_strlen($_SESSION['SuppTrans']->SuppReference) < 1){
$InputError = True;
prnMsg(_('The credit note as entered cannot be processed because the there is no suppliers credit note number or reference entered') . '. ' . _('The supplier credit note number must be entered'),'error');
} elseif (!Is_Date($_SESSION['SuppTrans']->TranDate)){
$InputError = True;
prnMsg(_('The credit note as entered cannot be processed because the date entered is not in the format') . ' ' . $_SESSION['DefaultDateFormat'], 'error');
} elseif (DateDiff(Date($_SESSION['DefaultDateFormat']), $_SESSION['SuppTrans']->TranDate, 'd') < 0){
$InputError = True;
prnMsg(_('The credit note as entered cannot be processed because the date is after today') . '. ' . _('Purchase credit notes are expected to have a date prior to or today'),'error');
}elseif ($_SESSION['SuppTrans']->ExRate <= 0){
$InputError = True;
prnMsg(_('The credit note as entered cannot be processed because the exchange rate for the credit note has been entered as a negative or zero number') . '. ' . _('The exchange rate is expected to show how many of the suppliers currency there are in 1 of the local currency'),'warn');
}elseif ($_SESSION['SuppTrans']->OvAmount < round($TotalShiptValue + $TotalGLValue + $TotalAssetValue + $TotalGRNValue,$_SESSION['SuppTrans']->CurrDecimalPlaces)){
prnMsg(_('The credit note total as entered is less than the sum of the shipment charges') . ', ' . _('the general ledger entries (if any) and the charges for goods received') . '. ' . _('There must be a mistake somewhere') . ', ' . _('the credit note as entered will not be processed'),'error');
$InputError = True;
} else {
/* SQL to process the postings for purchase credit note */
/*Start an SQL transaction */
DB_Txn_Begin();
/*Get the next transaction number for internal purposes and the period to post GL transactions in based on the credit note date*/
$CreditNoteNo = GetNextTransNo(21);
$PeriodNo = GetPeriod($_SESSION['SuppTrans']->TranDate);
$SQLCreditNoteDate = FormatDateForSQL($_SESSION['SuppTrans']->TranDate);
if ($_SESSION['SuppTrans']->GLLink_Creditors == 1){
/*Loop through the GL Entries and create a debit posting for each of the accounts entered */
$LocalTotal = 0;
/*the postings here are a little tricky, the logic goes like this:
> if its a shipment entry then the cost must go against the GRN suspense account defined in the company record
> if its a general ledger amount it goes straight to the account specified
> if its a GRN amount credited then there are two possibilities:
1 The PO line is on a shipment.
The whole charge goes to the GRN suspense account pending the closure of the
shipment where the variance is calculated on the shipment as a whole and the clearing entry to the GRN suspense
is created. Also, shipment records are created for the charges in local currency.
2. The order line item is not on a shipment
The whole amount of the credit is written off to the purchase price variance account applicable to the
stock category record of the stock item being credited.
Or if its not a stock item but a nominal item then the GL account in the orignal order is used for the
price variance account.
*/
foreach ($_SESSION['SuppTrans']->GLCodes as $EnteredGLCode){
/*GL Items are straight forward - just do the credit postings to the GL accounts specified -
the debit is to creditors control act done later for the total credit note value + tax*/
$SQL = "INSERT INTO gltrans (type,
typeno,
trandate,
periodno,
account,
narrative,
amount,
tag)
VALUES (21,
'" . $CreditNoteNo . "',
'" . $SQLCreditNoteDate . "',
'" . $PeriodNo . "',
'" . $EnteredGLCode->GLCode . "',
'" . $_SESSION['SuppTrans']->SupplierID . " " . $EnteredGLCode->Narrative . "',
'" . -$EnteredGLCode->Amount/$_SESSION['SuppTrans']->ExRate ."',
'" . $EnteredGLCode->Tag . "' )";
$ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('The general ledger transaction could not be added because');
$DbgMsg = _('The following SQL to insert the GL transaction was used');
$Result = DB_query($SQL, $ErrMsg, $DbgMsg, True);
$LocalTotal += ($EnteredGLCode->Amount/$_SESSION['SuppTrans']->ExRate);
}
foreach ($_SESSION['SuppTrans']->Shipts as $ShiptChg){
/*shipment postings are also straight forward - just do the credit postings to the GRN suspense account
these entries are reversed from the GRN suspense when the shipment is closed - entries only to open shipts*/
$SQL = "INSERT INTO gltrans (type,
typeno,
trandate,
periodno,
account,
narrative,
amount)
VALUES (21,
'" . $CreditNoteNo . "',
'" . $SQLCreditNoteDate . "',
'" . $PeriodNo . "',
'" . $_SESSION['SuppTrans']->GRNAct . "',
'" . $_SESSION['SuppTrans']->SupplierID . ' ' . _('Shipment credit against') . ' ' . $ShiptChg->ShiptRef . "',
'" . -$ShiptChg->Amount/$_SESSION['SuppTrans']->ExRate . "')";
$ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('The general ledger transaction for the shipment') . ' ' . $ShiptChg->ShiptRef . ' ' . _('could not be added because');
$DbgMsg = _('The following SQL to insert the GL transaction was used');
$Result = DB_query($SQL, $ErrMsg, $DbgMsg, True);
$LocalTotal += $ShiptChg->Amount/$_SESSION['SuppTrans']->ExRate;
}
foreach ($_SESSION['SuppTrans']->Assets as $AssetAddition){
/* only the GL entries if the creditors->GL integration is enabled */
$SQL = "INSERT INTO gltrans (type,
typeno,
trandate,
periodno,
account,
narrative,
amount)
VALUES ('21',
'" . $CreditNoteNo . "',
'" . $SQLCreditNoteDate . "',
'" . $PeriodNo . "',
'". $AssetAddition->CostAct . "',
'" . $_SESSION['SuppTrans']->SupplierID . ' ' . _('Asset Credit') . ' ' . $AssetAddition->AssetID . ': ' . $AssetAddition->Description . "',
'" . -$AssetAddition->Amount/ $_SESSION['SuppTrans']->ExRate . "')";
$ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('The general ledger transaction for the asset addition could not be added because');
$DbgMsg = _('The following SQL to insert the GL transaction was used');
$Result = DB_query($SQL, $ErrMsg, $DbgMsg, True);
$LocalTotal += $AssetAddition->Amount/ $_SESSION['SuppTrans']->ExRate;
}
foreach ($_SESSION['SuppTrans']->Contracts as $Contract){
/*contract postings need to get the WIP from the contract item's stock category record
* debit postings to this WIP account
* the WIP account is tidied up when the contract is closed*/
$result = DB_query("SELECT wipact FROM stockcategory
INNER JOIN stockmaster
ON stockcategory.categoryid=stockmaster.categoryid
WHERE stockmaster.stockid='" . $Contract->ContractRef . "'");
$WIPRow = DB_fetch_row($result);
$WIPAccount = $WIPRow[0];
$SQL = "INSERT INTO gltrans (type,
typeno,
trandate,
periodno,
account,
narrative,
amount)
VALUES (21,
'" .$CreditNoteNo . "',
'" . $SQLCreditNoteDate. "',
'" . $PeriodNo . "',
'". $WIPAccount . "',
'" . $_SESSION['SuppTrans']->SupplierID . ' ' . _('Contract charge against') . ' ' . $Contract->ContractRef . "',
'" . (-$Contract->Amount/ $_SESSION['SuppTrans']->ExRate) . "')";
$ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('The general ledger transaction for the contract') . ' ' . $Contract->ContractRef . ' ' . _('could not be added because');
$DbgMsg = _('The following SQL to insert the GL transaction was used');
$Result = DB_query($SQL, $ErrMsg, $DbgMsg, True);
$LocalTotal += ($Contract->Amount/ $_SESSION['SuppTrans']->ExRate);
}
foreach ($_SESSION['SuppTrans']->GRNs as $EnteredGRN){
if (mb_strlen($EnteredGRN->ShiptRef)==0
OR $EnteredGRN->ShiptRef==''
OR $EnteredGRN->ShiptRef==0){ /*so its not a shipment item */
/*so its not a shipment item
enter the GL entry to reverse the GRN suspense entry created on delivery at standard cost used on delivery */
if ($EnteredGRN->StdCostUnit * $EnteredGRN->This_QuantityInv != 0) {
$SQL = "INSERT INTO gltrans (type,
typeno,
trandate,
periodno,
account,
narrative,
amount)
VALUES ('21',
'" . $CreditNoteNo . "',
'" . $SQLCreditNoteDate . "',
'" . $PeriodNo . "',
'" . $_SESSION['SuppTrans']->GRNAct . "',
'" . $_SESSION['SuppTrans']->SupplierID . ' - ' . _('GRN Credit Note') . ' ' . $EnteredGRN->GRNNo . ' - ' . $EnteredGRN->ItemCode . ' x ' . $EnteredGRN->This_QuantityInv . ' @ ' . _('std cost of') . ' ' . $EnteredGRN->StdCostUnit . "',
'" . (-$EnteredGRN->StdCostUnit * $EnteredGRN->This_QuantityInv) . "')";
$ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('The general ledger transaction could not be added because');
$DbgMsg = _('The following SQL to insert the GL transaction was used');
$Result = DB_query($SQL, $ErrMsg, $DbgMsg, True);
}
$PurchPriceVar = $EnteredGRN->This_QuantityInv * (($EnteredGRN->ChgPrice / $_SESSION['SuppTrans']->ExRate) - $EnteredGRN->StdCostUnit);
/*Yes but where to post this difference to - if its a stock item the variance account must be retrieved from the stock category record
if its a nominal purchase order item with no stock item then post it to the account specified in the purchase order detail record */
if ($PurchPriceVar !=0){ /* don't bother with this lot if there is no difference ! */
if (mb_strlen($EnteredGRN->ItemCode)>0 OR $EnteredGRN->ItemCode != ''){ /*so it is a stock item */
/*need to get the stock category record for this stock item - this is function in SQL_CommonFunctions.inc */
$StockGLCode = GetStockGLCode($EnteredGRN->ItemCode);
/*We have stock item and a purchase price variance need to see whether we are using Standard or WeightedAverageCosting */
if ($_SESSION['WeightedAverageCosting']==1){ /*Weighted Average costing */
/*
First off figure out the new weighted average cost Need the following data:
How many in stock now
The quantity being invoiced here - $EnteredGRN->This_QuantityInv
The cost of these items - $EnteredGRN->ChgPrice / $_SESSION['SuppTrans']->ExRate
*/
$sql ="SELECT SUM(quantity) FROM locstock WHERE stockid='" . $EnteredGRN->ItemCode . "'";
$ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('The quantity on hand could not be retrieved from the database');
$DbgMsg = _('The following SQL to retrieve the total stock quantity was used');
$Result = DB_query($sql, $ErrMsg, $DbgMsg, True);
$QtyRow = DB_fetch_row($Result);
$TotalQuantityOnHand = $QtyRow[0];
/*The cost adjustment is the price variance / the total quantity in stock
But thats only provided that the total quantity in stock is greater than the quantity charged on this invoice
If the quantity on hand is less the amount charged on this invoice then some must have been sold and the price variance on these must be written off to price variances*/
$WriteOffToVariances =0;
if ($EnteredGRN->This_QuantityInv > $TotalQuantityOnHand){
/*So we need to write off some of the variance to variances and only the balance of the quantity in stock to go to stock value */
$WriteOffToVariances = ($EnteredGRN->This_QuantityInv
- $TotalQuantityOnHand)
* (($EnteredGRN->ChgPrice / $_SESSION['SuppTrans']->ExRate) - $EnteredGRN->StdCostUnit);
$SQL = "INSERT INTO gltrans (type,
typeno,
trandate,
periodno,
account,
narrative,
amount)
VALUES (21,
'" . $CreditNoteNo . "',
'" . $SQLCreditNoteDate . "',
'" . $PeriodNo . "',
'" . $StockGLCode['purchpricevaract'] . "',
'" . $_SESSION['SuppTrans']->SupplierID . ' - ' . _('GRN Credit Note') . ' ' . $EnteredGRN->GRNNo .' - ' . $EnteredGRN->ItemCode . ' x ' . ($EnteredGRN->This_QuantityInv-$TotalQuantityOnHand) . ' x ' . _('price var of') . ' ' . locale_number_format(($EnteredGRN->ChgPrice / $_SESSION['SuppTrans']->ExRate) - $EnteredGRN->StdCostUnit,$_SESSION['CompanyRecord']['decimalplaces']) ."',
'" . (-$WriteOffToVariances) . "')";
$ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('The general ledger transaction could not be added for the price variance of the stock item because');
$DbgMsg = _('The following SQL to insert the GL transaction was used');
$Result = DB_query($SQL, $ErrMsg, $DbgMsg, True);
}
/*Now post any remaining price variance to stock rather than price variances */
$SQL = "INSERT INTO gltrans (type,
typeno,
trandate,
periodno,
account,
narrative,
amount)
VALUES (21,
'" . $CreditNoteNo . "',
'" . $SQLCreditNoteDate . "',
'" . $PeriodNo . "',
'" . $StockGLCode['stockact'] . "',
'" . $_SESSION['SuppTrans']->SupplierID . ' - ' . _('Average Cost Adj') .
' - ' . $EnteredGRN->ItemCode . ' x ' . $TotalQuantityOnHand . ' x ' .
locale_number_format(($EnteredGRN->ChgPrice / $_SESSION['SuppTrans']->ExRate) - $EnteredGRN->StdCostUnit,$_SESSION['CompanyRecord']['decimalplaces']) . "',
'" . (-($PurchPriceVar - $WriteOffToVariances)) . "')";
$ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('The general ledger transaction could not be added for the price variance of the stock item because');
$DbgMsg = _('The following SQL to insert the GL transaction was used');
$Result = DB_query($SQL, $ErrMsg, $DbgMsg, True);
/*Now to update the stock cost with the new weighted average */
/*Need to consider what to do if the cost has been changed manually between receiving the stock and entering the invoice - this code assumes there has been no cost updates made manually and all the price variance is posted to stock.
A nicety or important?? */
$ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('The cost could not be updated because');
$DbgMsg = _('The following SQL to update the cost was used');
if ($TotalQuantityOnHand>0) {
$CostIncrement = ($PurchPriceVar - $WriteOffToVariances) / $TotalQuantityOnHand;
$sql = "UPDATE stockmaster SET lastcost=materialcost+overheadcost+labourcost,
materialcost=materialcost-" . $CostIncrement . "
WHERE stockid='" . $EnteredGRN->ItemCode . "'";
$Result = DB_query($sql, $ErrMsg, $DbgMsg, True);
} else {
$sql = "UPDATE stockmaster SET lastcost=materialcost+overheadcost+labourcost,
materialcost=" . ($EnteredGRN->ChgPrice / $_SESSION['SuppTrans']->ExRate) . " WHERE stockid='" . $EnteredGRN->ItemCode . "'";
$Result = DB_query($sql, $ErrMsg, $DbgMsg, True);
}
/* End of Weighted Average Costing Code */
} else { //It must be Standard Costing
$SQL = "INSERT INTO gltrans (type,
typeno,
trandate,
periodno,
account,
narrative,
amount)
VALUES (21,
'" . $CreditNoteNo . "',
'" . $SQLCreditNoteDate . "',
'" . $PeriodNo . "',
'" . $StockGLCode['purchpricevaract'] . "',
'" . $_SESSION['SuppTrans']->SupplierID . ' - ' . _('GRN') . ' ' . $EnteredGRN->GRNNo . ' - ' . $EnteredGRN->ItemCode . ' x ' . $EnteredGRN->This_QuantityInv . ' x ' . _('price var of') . ' ' . locale_number_format(($EnteredGRN->ChgPrice / $_SESSION['SuppTrans']->ExRate) - $EnteredGRN->StdCostUnit,$_SESSION['CompanyRecord']['decimalplaces']) . "',
'" . (-$PurchPriceVar) . "')";
$ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('The general ledger transaction could not be added for the price variance of the stock item because');
$DbgMsg = _('The following SQL to insert the GL transaction was used');
$Result = DB_query($SQL, $ErrMsg, $DbgMsg, True);
}
} else {
/* its a nominal purchase order item that is not on a shipment so post the whole lot to the GLCode specified in the order, the purchase price var is actually the diff between the
order price and the actual invoice price since the std cost was made equal to the order price in local currency at the time
the goods were received */
$GLCode = $EnteredGRN->GLCode; //by default
if ($EnteredGRN->AssetID!=0) { //then it is an asset