forked from webERP-team/webERP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SupplierInvoice.php
1927 lines (1596 loc) · 88.8 KB
/
SupplierInvoice.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 supplier transaction uses the SuppTrans class to hold the information about the invoice
the SuppTrans class contains an array of GRNs objects - containing details of GRNs for invoicing
Also an array of GLCodes objects - only used if the AP - GL link is effective
Also an array of shipment charges for charges to shipments to be apportioned accross the cost of stock items */
include('includes/DefineSuppTransClass.php');
include('includes/DefinePOClass.php'); //needed for auto receiving code
/* Session started in header.php for password checking and authorisation level check */
include('includes/session.php');
$Title = _('Enter Supplier Invoice');
/* webERP manual links before header.php */
$ViewTopic= 'AccountsPayable';
$BookMark = 'SupplierInvoice';
include('includes/header.php');
include('includes/SQL_CommonFunctions.inc');
if (empty($_GET['identifier'])) {
$identifier=date('U');
} else {
$identifier=$_GET['identifier'];
}
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];
if (!isset($_SESSION['SuppTrans']->SupplierID)) {
$_SESSION['SuppTrans']->SupplierID = $_GET['SupplierID'];
}
} else {
$SupplierName=$_SESSION['SuppTrans']->SupplierName;
}
echo '<p class="page_title_text"><img alt="" src="'.$RootPath . '/css/' . $Theme .
'/images/transactions.png" title="' . _('Supplier Invoice') . '" />' . ' ' .
_('Enter Supplier Invoice') . ': ' . $SupplierName . ' ' . $_SESSION['SuppTrans']->SupplierID . '</p>';
if (isset($_GET['SupplierID']) AND $_GET['SupplierID']!=''){
/*It must be a new invoice entry - clear any existing invoice details from the SuppTrans object and initiate a newy*/
if (isset( $_SESSION['SuppTrans'])){
unset ( $_SESSION['SuppTrans']->GRNs);
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,
suppliers.taxgroupid,
taxgroups.taxgroupdescription
FROM suppliers,
taxgroups,
currencies,
paymentterms,
taxauthorities
WHERE suppliers.taxgroupid=taxgroups.taxgroupid
AND suppliers.currcode=currencies.currabrev
AND suppliers.paymentterms=paymentterms.termsindicator
AND 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']->CurrDecimalPlaces = $myrow['decimalplaces'];
$_SESSION['SuppTrans']->TaxGroup = $myrow['taxgroupid'];
$_SESSION['SuppTrans']->TaxGroupDescription = $myrow['taxgroupdescription'];
$_SESSION['SuppTrans']->SupplierID = $myrow['supplierid'];
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 = 'Invoice';
} elseif (!isset( $_SESSION['SuppTrans'])){
prnMsg( _('To enter a supplier invoice 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 Invoice For') . '</a>';
include('includes/footer.php');
exit;
/*It all stops here if there ain't no supplier selected */
}
/* The code below automatically receives the outstanding balances on the purchase order ReceivePO and adds all the GRNs from that purchase order onto the invoice
* This is geared towards smaller businesses that have purchase orders that are automatically approved by users, and they want to enter the invoice directly based
* on the details entered in the purchase order screen.
*/
if (isset($_GET['ReceivePO']) AND $_GET['ReceivePO']!=''){
/*Need to check that the user has permission to receive goods */
if (! in_array($_SESSION['PageSecurityArray']['GoodsReceived.php'], $_SESSION['AllowedPageSecurityTokens'])){
prnMsg(_('Your permissions do not allow receiving of goods. Automatic receiving of purchase orders is restricted to those only users who are authorised to receive goods/services'),'error');
} else {
/* The user has permission to receive goods then lets go */
$_GET['ModifyOrderNumber'] = intval($_GET['ReceivePO']);
include('includes/PO_ReadInOrder.inc');
if ($_SESSION['PO'.$identifier]->Status == 'Authorised'){
$Result = DB_Txn_Begin();
/*Now Get the next GRN - function in SQL_CommonFunctions*/
$GRN = GetNextTransNo(25);
if (!isset($_GET['DeliveryDate'])){
$DeliveryDate = date($_SESSION['DefaultDateFormat']);
} else {
$DeliveryDate = $_GET['DeliveryDate'];
}
$_POST['ExRate'] = $_SESSION['SuppTrans']->ExRate;
$_POST['TranDate'] = $DeliveryDate;
$PeriodNo = GetPeriod($DeliveryDate);
$OrderHasControlledItems = false; //assume the best
foreach ($_SESSION['PO'.$identifier]->LineItems as $OrderLine) {
//Set the quantity to receive with this auto delivery assuming all is well
$_SESSION['PO'.$identifier]->LineItems[$OrderLine->LineNo]->ReceiveQty = $OrderLine->Quantity - $OrderLine->QtyReceived;
if ($OrderLine->Controlled ==1) { // it's a controlled item - we can't deal with auto receiving controlled items!!!
prnMsg(_('Auto receiving of controlled stock items that require serial number or batch number entry is not currently catered for. Only orders with normal non-serial numbered items can be received automatically'),'error');
$OrderHasControlledItems = true;
}
}
if ($OrderHasControlledItems == false){
foreach ($_SESSION['PO'.$identifier]->LineItems as $OrderLine) {
$LocalCurrencyPrice = ($OrderLine->Price / $_SESSION['SuppTrans']->ExRate);
if ($OrderLine->StockID!='') { //Its a stock item line
/*Need to get the current standard cost as it is now so we can process GL jorunals later*/
$SQL = "SELECT materialcost + labourcost + overheadcost as stdcost
FROM stockmaster
WHERE stockid='" . $OrderLine->StockID . "'";
$ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('The standard cost of the item being received cannot be retrieved because');
$DbgMsg = _('The following SQL to retrieve the standard cost was used');
$Result = DB_query($SQL,$ErrMsg,$DbgMsg,true);
$myrow = DB_fetch_row($Result);
$CurrentStandardCost = $myrow[0];
if ($OrderLine->QtyReceived==0){ //its the first receipt against this line
$_SESSION['PO'.$identifier]->LineItems[$OrderLine->LineNo]->StandardCost = $CurrentStandardCost;
}
/*Set the purchase order line stdcostunit = weighted average / standard cost used for all receipts of this line
This assures that the quantity received against the purchase order line multiplied by the weighted average of standard
costs received = the total of standard cost posted to GRN suspense*/
$_SESSION['PO'.$identifier]->LineItems[$OrderLine->LineNo]->StandardCost = (($CurrentStandardCost * $OrderLine->ReceiveQty) + ($_SESSION['PO'.$identifier]->LineItems[$OrderLine->LineNo]->StandardCost *$OrderLine->QtyReceived)) / ($OrderLine->ReceiveQty + $OrderLine->QtyReceived);
} elseif ($OrderLine->QtyReceived==0 AND $OrderLine->StockID=='') {
/*Its a nominal item being received */
/*Need to record the value of the order per unit in the standard cost field to ensure GRN account entries clear */
$_SESSION['PO'.$identifier]->LineItems[$OrderLine->LineNo]->StandardCost = $LocalCurrencyPrice;
}
if ($OrderLine->StockID=='') { /*Its a NOMINAL item line */
$CurrentStandardCost = $_SESSION['PO'.$identifier]->LineItems[$OrderLine->LineNo]->StandardCost;
}
/*Now the SQL to do the update to the PurchOrderDetails */
$SQL = "UPDATE purchorderdetails SET quantityrecd = quantityrecd + '" . $OrderLine->ReceiveQty . "',
stdcostunit='" . $_SESSION['PO'.$identifier]->LineItems[$OrderLine->LineNo]->StandardCost . "',
completed='" . $_SESSION['PO'.$identifier]->LineItems[$OrderLine->LineNo]->Completed . "'
WHERE podetailitem = '" . $OrderLine->PODetailRec . "'";
$ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('The purchase order detail record could not be updated with the quantity received because');
$DbgMsg = _('The following SQL to update the purchase order detail record was used');
$Result = DB_query($SQL, $ErrMsg, $DbgMsg, true);
if ($OrderLine->StockID !=''){ /*Its a stock item so use the standard cost for the journals */
$UnitCost = $CurrentStandardCost;
} else { /*otherwise its a nominal PO item so use the purchase cost converted to local currency */
$UnitCost = $OrderLine->Price / $_SESSION['SuppTrans']->ExRate;
}
/*Need to insert a GRN item */
$SQL = "INSERT INTO grns (grnbatch,
podetailitem,
itemcode,
itemdescription,
deliverydate,
qtyrecd,
supplierid,
stdcostunit)
VALUES ('" . $GRN . "',
'" . $OrderLine->PODetailRec . "',
'" . $OrderLine->StockID . "',
'" . DB_escape_string($OrderLine->ItemDescription) . "',
'" . FormatDateForSQL($DeliveryDate) . "',
'" . $OrderLine->ReceiveQty . "',
'" . $_SESSION['PO'.$identifier]->SupplierID . "',
'" . $CurrentStandardCost . "')";
$ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('A GRN record could not be inserted') . '. ' . _('This receipt of goods has not been processed because');
$DbgMsg = _('The following SQL to insert the GRN record was used');
$Result = DB_query($SQL, $ErrMsg, $DbgMsg, true);
if ($OrderLine->StockID!=''){ /* if the order line is in fact a stock item */
/* Update location stock records - NB a PO cannot be entered for a dummy/assembly/kit parts */
/* Need to get the current location quantity will need it later for the stock movement */
$SQL="SELECT locstock.quantity
FROM locstock
WHERE locstock.stockid='" . $OrderLine->StockID . "'
AND loccode= '" . $_SESSION['PO'.$identifier]->Location . "'";
$Result = DB_query($SQL);
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;
}
$SQL = "UPDATE locstock
SET quantity = locstock.quantity + '" . $OrderLine->ReceiveQty . "'
WHERE locstock.stockid = '" . $OrderLine->StockID . "'
AND loccode = '" . $_SESSION['PO'.$identifier]->Location . "'";
$ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('The location stock record could not be updated because');
$DbgMsg = _('The following SQL to update the location stock record was used');
$Result = DB_query($SQL, $ErrMsg, $DbgMsg, true);
/* Insert stock movements - with unit cost */
$SQL = "INSERT INTO stockmoves (stockid,
type,
transno,
loccode,
trandate,
userid,
price,
prd,
reference,
qty,
standardcost,
newqoh)
VALUES (
'" . $OrderLine->StockID . "',
25,
'" . $GRN . "',
'" . $_SESSION['PO'.$identifier]->Location . "',
'" . FormatDateForSQL($DeliveryDate) . "',
'" . $_SESSION['UserID'] . "',
'" . $LocalCurrencyPrice . "',
'" . $PeriodNo . "',
'" . $_SESSION['PO'.$identifier]->SupplierID . " (" . DB_escape_string($_SESSION['PO'.$identifier]->SupplierName) . ") - " .$_SESSION['PO'.$identifier]->OrderNo . "',
'" . $OrderLine->ReceiveQty . "',
'" . $_SESSION['PO'.$identifier]->LineItems[$OrderLine->LineNo]->StandardCost . "',
'" . ($QtyOnHandPrior + $OrderLine->ReceiveQty) . "'
)";
$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, $ErrMsg, $DbgMsg, true);
} /*end of its a stock item - updates to locations and insert movements*/
/* Check to see if the line item was flagged as the purchase of an asset */
if ($OrderLine->AssetID !='' AND $OrderLine->AssetID !='0'){ //then it is an asset
/*first validate the AssetID and if it doesn't exist treat it like a normal nominal item */
$CheckAssetExistsResult = DB_query("SELECT assetid,
datepurchased,
costact
FROM fixedassets
INNER JOIN fixedassetcategories
ON fixedassets.assetcategoryid=fixedassetcategories.categoryid
WHERE assetid='" . $OrderLine->AssetID . "'");
if (DB_num_rows($CheckAssetExistsResult)==1){ //then work with the assetid provided
/*Need to add a fixedassettrans for the cost of the asset being received */
$SQL = "INSERT INTO fixedassettrans (assetid,
transtype,
transno,
transdate,
periodno,
inputdate,
fixedassettranstype,
amount)
VALUES ('" . $OrderLine->AssetID . "',
25,
'" . $GRN . "',
'" . FormatDateForSQL($DeliveryDate) . "',
'" . $PeriodNo . "',
'" . Date('Y-m-d') . "',
'" . _('cost') . "',
'" . $CurrentStandardCost * $OrderLine->ReceiveQty . "')";
$ErrMsg = _('CRITICAL ERROR! NOTE DOWN THIS ERROR AND SEEK ASSISTANCE The fixed asset transaction could not be inserted because');
$DbgMsg = _('The following SQL to insert the fixed asset transaction record was used');
$Result = DB_query($SQL,$ErrMsg, $DbgMsg, true);
/*Now get the correct cost GL account from the asset category */
$AssetRow = DB_fetch_array($CheckAssetExistsResult);
/*Over-ride any GL account specified in the order with the asset category cost account */
$_SESSION['PO'.$identifier]->LineItems[$OrderLine->LineNo]->GLCode = $AssetRow['costact'];
/*Now if there are no previous additions to this asset update the date purchased */
if ($AssetRow['datepurchased']=='0000-00-00'){
/* it is a new addition as the date is set to 0000-00-00 when the asset record is created
* before any cost is added to the asset
*/
$SQL = "UPDATE fixedassets
SET datepurchased='" . FormatDateForSQL($DeliveryDate) . "',
cost = cost + " . ($CurrentStandardCost * $OrderLine->ReceiveQty) . "
WHERE assetid = '" . $OrderLine->AssetID . "'";
} else {
$SQL = "UPDATE fixedassets SET cost = cost + " . ($CurrentStandardCost * $OrderLine->ReceiveQty) . "
WHERE assetid = '" . $OrderLine->AssetID . "'";
}
$ErrMsg = _('CRITICAL ERROR! NOTE DOWN THIS ERROR AND SEEK ASSISTANCE. The fixed asset cost and date purchased was not able to be updated because:');
$DbgMsg = _('The following SQL was used to attempt the update of the cost and the date the asset was purchased');
$Result = DB_query($SQL,$ErrMsg, $DbgMsg, true);
} //assetid provided doesn't exist so ignore it and treat as a normal nominal item
} //assetid is set so the nominal item is an asset
/* If GLLink_Stock then insert GLTrans to debit the GL Code and credit GRN Suspense account at standard cost*/
if ($_SESSION['PO'.$identifier]->GLLink==1 AND $OrderLine->GLCode !=0){
/*GLCode is set to 0 when the GLLink is not activated this covers a situation where the GLLink is now active but it wasn't when this PO was entered */
/*first the debit using the GLCode in the PO detail record entry*/
$SQL = "INSERT INTO gltrans (type,
typeno,
trandate,
periodno,
account,
narrative,
amount)
VALUES (
25,
'" . $GRN . "',
'" . FormatDateForSQL($DeliveryDate) . "',
'" . $PeriodNo . "',
'" . $OrderLine->GLCode . "',
'PO: " . $_SESSION['PO'.$identifier]->OrderNo . " " . $_SESSION['PO'.$identifier]->SupplierID . " - " . $OrderLine->StockID
. " - " . DB_escape_string($OrderLine->ItemDescription) . " x " . $OrderLine->ReceiveQty . " @ " .
locale_number_format($CurrentStandardCost,$_SESSION['CompanyRecord']['decimalplaces']) . "',
'" . $CurrentStandardCost * $OrderLine->ReceiveQty . "'
)";
$ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('The purchase GL posting could not be inserted because');
$DbgMsg = _('The following SQL to insert the purchase GLTrans record was used');
$Result = DB_query($SQL,$ErrMsg, $DbgMsg, true);
/* If the CurrentStandardCost != UnitCost (the standard at the time the first delivery was booked in, and its a stock item, then the difference needs to be booked in against the purchase price variance account */
/*now the GRN suspense entry*/
$SQL = "INSERT INTO gltrans (type,
typeno,
trandate,
periodno,
account,
narrative,
amount)
VALUES (25,
'" . $GRN . "',
'" . FormatDateForSQL($DeliveryDate) . "',
'" . $PeriodNo . "',
'" . $_SESSION['CompanyRecord']['grnact'] . "',
'" . _('PO'.$identifier) . ': ' . $_SESSION['PO'.$identifier]->OrderNo . ' ' . $_SESSION['PO'.$identifier]->SupplierID . ' - ' . $OrderLine->StockID . ' - ' . DB_escape_string($OrderLine->ItemDescription) . ' x ' . $OrderLine->ReceiveQty . ' @ ' . locale_number_format($UnitCost,$_SESSION['CompanyRecord']['decimalplaces']) . "',
'" . -$UnitCost * $OrderLine->ReceiveQty . "'
)";
$ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('The GRN suspense side of the GL posting could not be inserted because');
$DbgMsg = _('The following SQL to insert the GRN Suspense GLTrans record was used');
$Result = DB_query($SQL, $ErrMsg, $DbgMsg,true);
} /* end of if GL and stock integrated and standard cost !=0 */
} /*end of OrderLine loop */
$StatusComment=date($_SESSION['DefaultDateFormat']) .' - ' . _('Order Completed on entry of GRN') . '<br />' . $_SESSION['PO'.$identifier]->StatusComments;
$sql="UPDATE purchorders
SET status='Completed',
stat_comment='" . $StatusComment . "'
WHERE orderno='" . $_SESSION['PO'.$identifier]->OrderNo . "'";
$result=DB_query($sql);
if ($_SESSION['PO'.$identifier]->GLLink==1) {
EnsureGLEntriesBalance(25, $GRN);
}
$Result = DB_Txn_Commit();
//Now add all these deliveries to this purchase invoice
$SQL = "SELECT grnbatch,
grnno,
purchorderdetails.orderno,
purchorderdetails.unitprice,
grns.itemcode,
grns.deliverydate,
grns.itemdescription,
grns.qtyrecd,
grns.quantityinv,
grns.stdcostunit,
grns.supplierref,
purchorderdetails.glcode,
purchorderdetails.shiptref,
purchorderdetails.jobref,
purchorderdetails.podetailitem,
purchorderdetails.assetid,
stockmaster.decimalplaces
FROM grns INNER JOIN purchorderdetails
ON grns.podetailitem=purchorderdetails.podetailitem
LEFT JOIN stockmaster ON grns.itemcode=stockmaster.stockid
WHERE grns.supplierid ='" . $_SESSION['SuppTrans']->SupplierID . "'
AND purchorderdetails.orderno = '" . intval($_GET['ReceivePO']) . "'
AND grns.qtyrecd - grns.quantityinv > 0
ORDER BY grns.grnno";
$GRNResults = DB_query($SQL);
while ($myrow=DB_fetch_array($GRNResults)){
if ($myrow['decimalplaces']==''){
$myrow['decimalplaces']=2;
}
$_SESSION['SuppTrans']->Add_GRN_To_Trans($myrow['grnno'],
$myrow['podetailitem'],
$myrow['itemcode'],
$myrow['itemdescription'],
$myrow['qtyrecd'],
$myrow['quantityinv'],
$myrow['qtyrecd'] - $myrow['quantityinv'],
$myrow['unitprice'],
$myrow['unitprice'],
true,
$myrow['stdcostunit'],
$myrow['shiptref'],
$myrow['jobref'],
$myrow['glcode'],
$myrow['orderno'],
$myrow['assetid'],
0,
$myrow['decimalplaces'],
$myrow['grnbatch'],
$myrow['supplierref']);
}
} //end if the order has no controlled items on it
} //only allow auto receiving of all lines if the PO is authorised
} //only allow auto receiving if the user has permission to receive goods
} // Page called with link to receive all the items on a PO
/* 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 += ($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']->Shipts) > 0){
foreach ( $_SESSION['SuppTrans']->Shipts as $ShiptLine){
$_SESSION['SuppTrans']->OvAmount += $ShiptLine->Amount;
}
}
if (count($_SESSION['SuppTrans']->Contracts) > 0){
foreach ( $_SESSION['SuppTrans']->Contracts as $Contract){
$_SESSION['SuppTrans']->OvAmount += $Contract->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['PostInvoice'])){
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 . '/SuppInvGRNs.php">';
echo '<div class="centre">' . _('You should automatically be forwarded to the entry of invoices against goods received page') .
'. ' . _('If this does not happen') .' (' . _('if the browser does not support META Refresh') . ') ' .
'<a href="' . $RootPath . '/SuppInvGRNs.php">' . _('click here') . '</a> ' . _('to continue') . '</div>
<br />';
exit;
}
if (isset($_POST['Shipts']) AND $_POST['Shipts'] == _('Shipments')){
/*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 '<div class="centre">' . _('You should automatically be forwarded to the entry of invoices 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') . '.</div><br />';
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 '<div class="centre">' . _('You should automatically be forwarded to the entry of invoices 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') . '.</div><br />';
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 invoices 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 invoice amounts 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 invoice for */
echo '<br /><table class="selection">
<tr>
<th>' . _('Supplier') . '</th>
<th>' . _('Currency') . '</th>
<th>' . _('Terms') . '</th>
<th>' . _('Tax Authority') . '</th>
</tr>';
echo '<tr>
<td><b>' . $_SESSION['SuppTrans']->SupplierID . ' - ' .
$_SESSION['SuppTrans']->SupplierName . '</b></td>
<th><b>' . $_SESSION['SuppTrans']->CurrCode . '</b></th>
<td><b>' . $_SESSION['SuppTrans']->TermsDescription . '</b></td>
<td><b>' . $_SESSION['SuppTrans']->TaxGroupDescription . '</b></td>
</tr>
</table>';
echo '<br /><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>' . _('Supplier Invoice Reference') . ':</td>
<td><input type="text" required="required" pattern=".{1,20}" title="'._('The input should not be blank and should be less than 20 characters').'" placeholder="'._('Within 20 characters needed').'" 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>' . _('Invoice Date') . ' (' . _('in format') . ' ' . $_SESSION['DefaultDateFormat'] . ') :</td>
<td><input type="text" class="date" size="11" maxlength="10" name="TranDate" value="' . $_SESSION['SuppTrans']->TranDate . '" /></td>
<td>' . _('Exchange Rate') . ':</td>
<td><input class="number" maxlength="12" name="ExRate" size="14" type="text" value="' . locale_number_format($_SESSION['SuppTrans']->ExRate,10) . '" /></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>';
$CanSubmit = false;//To avoid a empty submit
$TotalGRNValue = 0;
if (count( $_SESSION['SuppTrans']->GRNs)>0){ /*if there are any GRNs selected for invoicing then */
/*Show all the selected GRNs so far from the SESSION['SuppInv']->GRNs array */
$CanSubmit = true;
echo '<br />
<table class="selection">
<tr>
<th colspan="6">' . _('Purchase Order Charges') . '</th>
</tr>';
$tableheader = '<tr style="tableheader">
<th>' . _('Seq') . ' #</th>
<th>' . _('GRN Batch') . '</th>
<th>' . _('Supplier Ref') . '</th>
<th>' . _('Item Code') . '</th>
<th>' . _('Description') . '</th>
<th>' . _('Quantity Charged') . '</th>
<th>' . _('Price in') . ' ' . $_SESSION['SuppTrans']->CurrCode . '</th>
<th>' . _('Line Total') . ' ' . $_SESSION['SuppTrans']->CurrCode . '</th>
</tr>';
echo $tableheader;
foreach ($_SESSION['SuppTrans']->GRNs as $EnteredGRN){
echo '<tr>
<td>' . $EnteredGRN->GRNNo . '</td>
<td>' . $EnteredGRN->GRNBatchNo . '</td>
<td>' . $EnteredGRN->SupplierRef . '</td>
<td>' . $EnteredGRN->ItemCode . '</td>
<td>' . $EnteredGRN->ItemDescription . '</td>
<td class="number">' . locale_number_format($EnteredGRN->This_QuantityInv,$EnteredGRN->DecimalPlaces) . '</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>
</tr>';
$TotalGRNValue += ($EnteredGRN->ChgPrice * $EnteredGRN->This_QuantityInv);
}
echo '<tr>
<td colspan="5" class="number" style="color:blue">' . _('Total Value of Goods Charged') . ':</td>
<td class="number" style="color:blue">' . locale_number_format($TotalGRNValue,$_SESSION['SuppTrans']->CurrDecimalPlaces) . '</td>
</tr>
</table>';
}
$TotalShiptValue = 0;
if (count( $_SESSION['SuppTrans']->Shipts) > 0){ /*if there are any Shipment charges on the invoice*/
$CanSubmit = true;
echo '<br />
<table class="selection">
<tr>
<th colspan="2">' . _('Shipment Charges') . '</th>
</tr>';
$TableHeader = '<tr>
<th>' . _('Shipment') . '</th>
<th>' . _('Amount') . '</th>
</tr>';
echo $TableHeader;
$i=0; //row counter
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;
$i++;
if ($i > 15){
$i = 0;
echo $TableHeader;
}
}
echo '<tr>
<td class="number" style="color:blue">' . _('Total shipment charges') . ':</td>
<td class="number" style="color:blue">' . locale_number_format($TotalShiptValue,$_SESSION['SuppTrans']->CurrDecimalPlaces) . '</td>
</tr>
</table>';
}
$TotalAssetValue = 0;
if (count( $_SESSION['SuppTrans']->Assets) > 0){ /*if there are any fixed assets on the invoice*/
$CanSubmit = true;
echo '<br />
<table class="selection">
<tr>
<th colspan="3">' . _('Fixed Asset Additions') . '</th>
</tr>';
$TableHeader = '<tr>
<th>' . _('Asset ID') . '</th>
<th>' . _('Description') . '</th>
<th>' . _('Amount') . ' ' . $_SESSION['SuppTrans']->CurrCode . '</th>
</tr>';
echo $TableHeader;
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:blue">' . _('Total asset additions') . ':</td>
<td class="number" style="color:blue">' . locale_number_format($TotalAssetValue,$_SESSION['SuppTrans']->CurrDecimalPlaces) . '</td>
</tr>
</table>';
} //end loop around assets added to invocie
$TotalContractsValue = 0;
if (count( $_SESSION['SuppTrans']->Contracts) > 0){ /*if there are any contract charges on the invoice*/
$CanSubmit = true;
echo '<br />
<table class="selection">
<tr>
<th colspan="3">' . _('Contract Charges') . '</th>
</tr>';
$TableHeader = '<tr>
<th>' . _('Contract') . '</th>
<th>' . _('Narrative') . '</th>
<th>' . _('Amount') . ' ' . $_SESSION['SuppTrans']->CurrCode . '</th>
</tr>';
echo $TableHeader;
$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 colspan="2" class="number" style="color:blue">' . _('Total contract charges') . ':</td>
<td class="number" style="color:blue">' . locale_number_format($TotalContractsValue,$_SESSION['SuppTrans']->CurrDecimalPlaces) . '</td>
</tr>
</table>';
}
$TotalGLValue = 0;
if ( $_SESSION['SuppTrans']->GLLink_Creditors == 1){
if (count($_SESSION['SuppTrans']->GLCodes) > 0){
$CanSubmit = true;
echo '<br />
<table class="selection">
<tr>
<th colspan="5">' . _('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;
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;
}
echo '<tr>
<td colspan="4" class="number" style="color:blue">' . _('Total GL Analysis') . ':</td>
<td class="number" style="color:blue">' . locale_number_format($TotalGLValue,$_SESSION['SuppTrans']->CurrDecimalPlaces) . '</td>
</tr>
</table>';
}
$_SESSION['SuppTrans']->OvAmount = ($TotalGRNValue + $TotalGLValue + $TotalAssetValue + $TotalShiptValue + $TotalContractsValue);
echo '<br />
<table class="selection">
<tr>
<td>' . _('Amount in supplier currency') . ':</td>
<td colspan="2" class="number">' . locale_number_format( $_SESSION['SuppTrans']->OvAmount,$_SESSION['SuppTrans']->CurrDecimalPlaces) . '</td>
</tr>';
} else {
echo '<br />
<table class="selection">
<tr>
<td>' . _('Amount in supplier currency') . ':</td>
<td colspan="2" class="number"><input type="text" class="number" title="'._('The input must be numeric').'" size="12" 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">' . _('Manually') . '</option>';
} else {
echo '<option selected="selected" value="Auto">' . _('Automatic') . '</option>
<option value="Man">' . _('Manually') . '</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,$_SESSION['SuppTrans']->CurrDecimalPlaces) . '" />%';
/*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="' . locale_number_format(round($_SESSION['SuppTrans']->Taxes[$Tax->TaxCalculationOrder]->TaxOvAmount,$_SESSION['SuppTrans']->CurrDecimalPlaces),$_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*/
// if (!isset($_POST['TaxAmount' . $Tax->TaxCalculationOrder])) {
// $_POST['TaxAmount' . $Tax->TaxCalculationOrder]=0;
// }
$_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>';
}
$_SESSION['SuppTrans']->OvAmount = round($_SESSION['SuppTrans']->OvAmount,$_SESSION['SuppTrans']->CurrDecimalPlaces);
$DisplayTotal = locale_number_format(( $_SESSION['SuppTrans']->OvAmount + $TaxTotal), $_SESSION['SuppTrans']->CurrDecimalPlaces);
echo '<tr>
<td>' . _('Invoice Total') . ':</td>
<td colspan="2" class="number"><b>' . $DisplayTotal . '</b></td>
</tr>
</table>';
echo '<br />
<table class="selection">
<tr>
<td>' . _('Comments') . '</td>
<td><textarea name="Comments" cols="40" rows="2">' . $_SESSION['SuppTrans']->Comments . '</textarea></td>
</tr>
</table>';
if ($CanSubmit) {
echo '<br />
<div class="centre">
<input type="submit" name="PostInvoice" value="' . _('Enter Invoice') . '" />
</div>';
}
echo '</div>
</form>';
} else { // $_POST['PostInvoice'] is set so do the postings -and dont show the button to process
/*First do input reasonableness checks
then do the updates and inserts to process the invoice 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 */
/*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;