- modules/fotolab updated imagej to current version & some cod fixes to make it work
[care2x.git] / Care2007 / modules / weberp / SupplierInvoice.php
blob8e3f660dbb7ef2efc0398587e6bef956f948b830
1 <?php
3 /* $Revision: 1.28 $ */
5 /*The supplier transaction uses the SuppTrans class to hold the information about the invoice
6 the SuppTrans class contains an array of GRNs objects - containing details of GRNs for invoicing
7 Also an array of GLCodes objects - only used if the AP - GL link is effective
8 Also an array of shipment charges for charges to shipments to be apportioned accross the cost of stock items */
10 $PageSecurity = 5;
12 include('includes/DefineSuppTransClass.php');
13 /* Session started in header.inc for password checking and authorisation level check */
14 include('includes/session.inc');
16 $title = _('Enter Supplier Invoice');
18 include('includes/header.inc');
19 include('includes/SQL_CommonFunctions.inc');
21 echo "<A HREF='" . $rootpath . '/SelectSupplier.php?' . SID . "'>" . _('Back to Suppliers') . '</A><BR>';
23 if (isset($_GET['SupplierID'])){
25 /*It must be a new invoice entry - clear any existing invoice details from the SuppTrans object and initiate a newy*/
26 if (isset( $_SESSION['SuppTrans'])){
27 unset ( $_SESSION['SuppTrans']->GRNs);
28 unset ( $_SESSION['SuppTrans']->GLCodes);
29 unset ( $_SESSION['SuppTrans']);
32 if (isset( $_SESSION['SuppTransTmp'])){
33 unset ( $_SESSION['SuppTransTmp']->GRNs);
34 unset ( $_SESSION['SuppTransTmp']->GLCodes);
35 unset ( $_SESSION['SuppTransTmp']);
37 $_SESSION['SuppTrans'] = new SuppTrans;
39 /*Now retrieve supplier information - name, currency, default ex rate, terms, tax rate etc */
41 $sql = "SELECT suppliers.suppname,
42 paymentterms.terms,
43 paymentterms.daysbeforedue,
44 paymentterms.dayinfollowingmonth,
45 suppliers.currcode,
46 currencies.rate AS exrate,
47 suppliers.taxgroupid,
48 taxgroups.taxgroupdescription
49 FROM suppliers,
50 taxgroups,
51 currencies,
52 paymentterms,
53 taxauthorities
54 WHERE suppliers.taxgroupid=taxgroups.taxgroupid
55 AND suppliers.currcode=currencies.currabrev
56 AND suppliers.paymentterms=paymentterms.termsindicator
57 AND suppliers.supplierid = '" . $_GET['SupplierID'] . "'";
59 $ErrMsg = _('The supplier record selected') . ': ' . $_GET['SupplierID'] . ' ' ._('cannot be retrieved because');
60 $DbgMsg = _('The SQL used to retrieve the supplier details and failed was');
62 $result = DB_query($sql, $db, $ErrMsg, $DbgMsg);
65 $myrow = DB_fetch_array($result);
67 $_SESSION['SuppTrans']->SupplierName = $myrow['suppname'];
68 $_SESSION['SuppTrans']->TermsDescription = $myrow['terms'];
69 $_SESSION['SuppTrans']->CurrCode = $myrow['currcode'];
70 $_SESSION['SuppTrans']->ExRate = $myrow['exrate'];
71 $_SESSION['SuppTrans']->TaxGroup = $myrow['taxgroupid'];
72 $_SESSION['SuppTrans']->TaxGroupDescription = $myrow['taxgroupdescription'];
74 if ($myrow['daysbeforedue'] == 0){
75 $_SESSION['SuppTrans']->Terms = '1' . $myrow['dayinfollowingmonth'];
76 } else {
77 $_SESSION['SuppTrans']->Terms = '0' . $myrow['daysbeforedue'];
79 $_SESSION['SuppTrans']->SupplierID = $_GET['SupplierID'];
81 $LocalTaxProvinceResult = DB_query("SELECT taxprovinceid
82 FROM locations
83 WHERE loccode = '" . $_SESSION['UserStockLocation'] . "'", $db);
85 if(DB_num_rows($LocalTaxProvinceResult)==0){
86 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 provincce'),'error');
87 include('includes/footer.inc');
88 exit;
91 $LocalTaxProvinceRow = DB_fetch_row($LocalTaxProvinceResult);
92 $_SESSION['SuppTrans']->LocalTaxProvince = $LocalTaxProvinceRow[0];
94 $_SESSION['SuppTrans']->GetTaxes();
97 $_SESSION['SuppTrans']->GLLink_Creditors = $_SESSION['CompanyRecord']['gllink_creditors'];
98 $_SESSION['SuppTrans']->GRNAct = $_SESSION['CompanyRecord']['grnact'];
99 $_SESSION['SuppTrans']->CreditorsAct = $_SESSION['CompanyRecord']['creditorsact'];
101 $_SESSION['SuppTrans']->InvoiceOrCredit = 'Invoice';
103 } elseif (!isset( $_SESSION['SuppTrans'])){
105 prnMsg( _('To enter a supplier invoice the supplier must first be selected from the supplier selection screen'),'warn');
106 echo "<BR><A HREF='$rootpath/SelectSupplier.php?" . SID ."'>" . _('Select A Supplier to Enter an Invoice For') . '</A>';
107 include('includes/footer.inc');
108 exit;
110 /*It all stops here if there ain't no supplier selected */
113 /* Set the session variables to the posted data from the form if the page has called itself */
114 if (isset($_POST['ExRate'])){
115 $_SESSION['SuppTrans']->ExRate = $_POST['ExRate'];
116 $_SESSION['SuppTrans']->Comments = $_POST['Comments'];
117 $_SESSION['SuppTrans']->TranDate = $_POST['TranDate'];
119 if (substr( $_SESSION['SuppTrans']->Terms,0,1)=='1') { /*Its a day in the following month when due */
120 $_SESSION['SuppTrans']->DueDate = Date($_SESSION['DefaultDateFormat'], Mktime(0,0,0,Date('m')+1, substr( $_SESSION['SuppTrans']->Terms,1),Date('y')));
121 } else { /*Use the Days Before Due to add to the invoice date */
122 $_SESSION['SuppTrans']->DueDate = Date($_SESSION['DefaultDateFormat'], Mktime(0,0,0,Date('m'),Date('d') + (int) substr( $_SESSION['SuppTrans']->Terms,1),Date('y')));
125 $_SESSION['SuppTrans']->SuppReference = $_POST['SuppReference'];
127 if ( $_SESSION['SuppTrans']->GLLink_Creditors == 1){
129 /*The link to GL from creditors is active so the total should be built up from GLPostings and GRN entries
130 if the link is not active then OvAmount must be entered manually. */
132 $_SESSION['SuppTrans']->OvAmount = 0; /* for starters */
133 if (count($_SESSION['SuppTrans']->GRNs) > 0){
134 foreach ( $_SESSION['SuppTrans']->GRNs as $GRN){
135 $_SESSION['SuppTrans']->OvAmount = $_SESSION['SuppTrans']->OvAmount + ($GRN->This_QuantityInv * $GRN->ChgPrice);
138 if (count($_SESSION['SuppTrans']->GLCodes) > 0){
139 foreach ( $_SESSION['SuppTrans']->GLCodes as $GLLine){
140 $_SESSION['SuppTrans']->OvAmount = $_SESSION['SuppTrans']->OvAmount + $GLLine->Amount;
143 if (count($_SESSION['SuppTrans']->Shipts) > 0){
144 foreach ( $_SESSION['SuppTrans']->Shipts as $ShiptLine){
145 $_SESSION['SuppTrans']->OvAmount = $_SESSION['SuppTrans']->OvAmount + $ShiptLine->Amount;
148 $_SESSION['SuppTrans']->OvAmount = round($_SESSION['SuppTrans']->OvAmount,2);
149 }else {
150 /*OvAmount must be entered manually */
151 $_SESSION['SuppTrans']->OvAmount = round($_POST['OvAmount'],2);
156 if (!isset($_POST['PostInvoice'])){
158 if ($_POST['GRNS'] == _('Enter Against Goods Recd')){
159 /*This ensures that any changes in the page are stored in the session before calling the grn page */
160 echo "<META HTTP-EQUIV='Refresh' CONTENT='0; URL=" . $rootpath . "/SuppInvGRNs.php?" . SID . "'>";
161 echo '<P>' . _('You should automatically be forwarded to the entry of invoices against goods received page') .
162 '. ' . _('If this does not happen') .' (' . _('if the browser does not support META Refresh') . ') ' .
163 "<A HREF='" . $rootpath . "/SuppInvGRNs.php?" . SID . "'>" . _('click here') . '</a> ' . _('to continue') . '.<BR>';
164 exit;
166 if ($_POST['Shipts'] == _('Enter Against Shipment')){
167 /*This ensures that any changes in the page are stored in the session before calling the shipments page */
168 echo "<META HTTP-EQUIV='Refresh' CONTENT='0; URL=" . $rootpath . "/SuppShiptChgs.php?" . SID . "'>";
169 echo '<P>' . _('You should automatically be forwarded to the entry of invoices against shipments page') .
170 '. ' . _('If this does not happen') . ' (' . _('if the browser does not support META Refresh'). ') ' .
171 "<A HREF='" . $rootpath . "/SuppShiptChgs.php?" . SID . "'>" . _('click here') . '</a> ' . _('to continue') . '.<BR>';
172 exit;
174 if ($_POST['GL'] == _('Enter General Ledger Analysis')){
175 /*This ensures that any changes in the page are stored in the session before calling the shipments page */
176 echo "<META HTTP-EQUIV='Refresh' CONTENT='0; URL=" . $rootpath . "/SuppTransGLAnalysis.php?" . SID . "'>";
177 echo '<P>' . _('You should automatically be forwarded to the entry of invoices against the general ledger page') .
178 '. ' . _('If this does not happen') . ' (' . _('if the browser does not support META Refresh'). ') ' .
179 "<A HREF='" . $rootpath . "/SuppTransGLAnalysis.php?" . SID . "'>" . _('click here') . '</a> ' . _('to continue') . '.<BR>';
180 exit;
183 /* everything below here only do if a Supplier is selected
184 fisrt add a header to show who we are making an invoice for */
186 echo "<CENTER><TABLE BORDER=2 COLSPAN=4><TR><TD CLASS='tableheader'>" . _('Supplier') .
187 "</TD><TD CLASS='tableheader'>" . _('Currency') .
188 "</TD><TD CLASS='tableheader'>" . _('Terms').
189 "</TD><TD CLASS='tableheader'>" . _('Tax Authority') . '</TD></TR>';
191 echo '<TR><TD><FONT COLOR=blue><B>' . $_SESSION['SuppTrans']->SupplierID . ' - ' .
192 $_SESSION['SuppTrans']->SupplierName . '</B></FONT></TD>
193 <TD ALIGN=CENTER><FONT COLOR=blue><B>' . $_SESSION['SuppTrans']->CurrCode . '</B></FONT></TD>
194 <TD><FONT COLOR=blue><B>' . $_SESSION['SuppTrans']->TermsDescription . '</B></FONT></TD>
195 <TD><FONT COLOR=blue><B>' . $_SESSION['SuppTrans']->TaxGroupDescription . '</B></FONT></TD>
196 </TR>
197 </TABLE>';
199 echo "<FORM ACTION='" . $_SERVER['PHP_SELF'] . "?" . SID . "' METHOD=POST>";
201 echo '<TABLE>';
203 echo '<TR><TD>' . _('Supplier Invoice Reference') . ":</TD>
204 <TD><FONT SIZE=2><INPUT TYPE=TEXT SIZE=20 MAXLENGTH=20 NAME=SuppReference VALUE='" .
205 $_SESSION['SuppTrans']->SuppReference . "'></TD>";
207 if (!isset($_SESSION['SuppTrans']->TranDate)){
208 $_SESSION['SuppTrans']->TranDate= Date($_SESSION['DefaultDateFormat'], Mktime(0,0,0,Date('m'),Date('d')-1,Date('y')));
210 echo '<TD>' . _('Invoice Date') . ' (' . _('in format') . ' ' . $_SESSION['DefaultDateFormat'] . ") :</TD>
211 <TD><INPUT TYPE=TEXT SIZE=11 MAXLENGTH=10 NAME='TranDate' VALUE=" . $_SESSION['SuppTrans']->TranDate . '></TD>';
213 echo '<TD>' . _('Exchange Rate') . ":</TD>
214 <TD><INPUT TYPE=TEXT SIZE=11 MAXLENGTH=10 NAME='ExRate' VALUE=" . $_SESSION['SuppTrans']->ExRate . '></TD></TR>';
215 echo '</TABLE>';
217 echo "<BR><CENTER><INPUT TYPE=SUBMIT NAME='GRNS' VALUE='" . _('Enter Against Goods Recd') . "'> ";
219 echo "<INPUT TYPE=SUBMIT NAME='Shipts' VALUE='" . _('Enter Against Shipment') . "'> ";
221 if ( $_SESSION['SuppTrans']->GLLink_Creditors == 1){
222 echo "<INPUT TYPE=SUBMIT NAME='GL' VALUE='" . _('Enter General Ledger Analysis') . "'></CENTER>";
223 } else {
224 echo '</CENTER>';
228 if (count( $_SESSION['SuppTrans']->GRNs)>0){ /*if there are any GRNs selected for invoicing then */
229 /*Show all the selected GRNs so far from the SESSION['SuppInv']->GRNs array */
231 echo '<TABLE CELLPADDING=2>';
232 $tableheader = "<TR BGCOLOR=#800000><TD CLASS='tableheader'>" . _('Seq') . " #</TD>
233 <TD CLASS='tableheader'>" . _('Item Code') . "</TD>
234 <TD CLASS='tableheader'>" . _('Description') . "</TD>
235 <TD CLASS='tableheader'>" . _('Quantity Charged') . "</TD>
236 <TD CLASS='tableheader'>" . _('Price in') . ' ' . $_SESSION['SuppTrans']->CurrCode . "</TD>
237 <TD CLASS='tableheader'>" . _('Line Total') . ' ' . $_SESSION['SuppTrans']->CurrCode . '</TD></TR>';
238 echo $tableheader;
240 $TotalGRNValue = 0;
242 foreach ($_SESSION['SuppTrans']->GRNs as $EnteredGRN){
244 echo '<TR><TD>' . $EnteredGRN->GRNNo . '</TD><TD>' . $EnteredGRN->ItemCode .
245 '</TD><TD>' . $EnteredGRN->ItemDescription . '</TD><TD ALIGN=RIGHT>' .
246 number_format($EnteredGRN->This_QuantityInv,2) . '</TD><TD ALIGN=RIGHT>' .
247 number_format($EnteredGRN->ChgPrice,2) . '</TD><TD ALIGN=RIGHT>' .
248 number_format($EnteredGRN->ChgPrice * $EnteredGRN->This_QuantityInv,2) . '</TD>
249 </TR>';
251 $TotalGRNValue = $TotalGRNValue + ($EnteredGRN->ChgPrice * $EnteredGRN->This_QuantityInv);
253 $i++;
254 if ($i > 15){
255 $i = 0;
256 echo $tableheader;
260 echo '<TR><TD COLSPAN=5 ALIGN=RIGHT><FONT COLOR=blue>' . _('Total Value of Goods Charged') . ':</FONT></TD>
261 <TD ALIGN=RIGHT><FONT COLOR=blue><U>' . number_format($TotalGRNValue,2) . '</U></FONT></TD></TR>';
262 echo '</TABLE>';
265 if (count( $_SESSION['SuppTrans']->Shipts) > 0){ /*if there are any Shipment charges on the invoice*/
267 echo '<TABLE CELLPADDING=2>';
268 $TableHeader = "<TR><TD CLASS='tableheader'>" . _('Shipment') . "</TD>
269 <TD CLASS='tableheader'>" . _('Amount') . '</TD></TR>';
270 echo $TableHeader;
272 $TotalShiptValue = 0;
274 foreach ($_SESSION['SuppTrans']->Shipts as $EnteredShiptRef){
276 echo '<TR><TD>' . $EnteredShiptRef->ShiptRef . '</TD><TD ALIGN=RIGHT>' .
277 number_format($EnteredShiptRef->Amount,2) . '</TD></TR>';
279 $TotalShiptValue = $TotalShiptValue + $EnteredShiptRef->Amount;
281 $i++;
282 if ($i > 15){
283 $i = 0;
284 echo $TableHeader;
288 echo '<TR><TD COLSPAN=2 ALIGN=RIGHT><FONT SIZE=4 COLOR=blue>' . _('Total') . ':</FONT></TD>
289 <TD ALIGN=RIGHT><FONT SIZE=4 COLOR=BLUE><U>' . number_format($TotalShiptValue,2) . '</U></FONT></TD></TR></TABLE>';
293 if ( $_SESSION['SuppTrans']->GLLink_Creditors == 1){
295 if (count($_SESSION['SuppTrans']->GLCodes) > 0){
296 echo '<TABLE CELLPADDING=2>';
297 $TableHeader = "<TR><TD CLASS='tableheader'>" . _('Account') .
298 "</TD><TD CLASS='tableheader'>" . _('Name') .
299 "</TD><TD CLASS='tableheader'>" . _('Amount') . '<BR>' . _('in') . ' ' . $_SESSION['SuppTrans']->CurrCode . "</TD>
300 <TD CLASS='tableheader'>" . _('Shipment') ."</TD>
301 <TD CLASS='tableheader'>" . _('Job') . "</TD>
302 <TD class='tableheader'>" . _('Narrative') . '</TD></TR>';
303 echo $TableHeader;
305 $TotalGLValue = 0;
307 foreach ($_SESSION['SuppTrans']->GLCodes as $EnteredGLCode){
309 echo '<TR><TD>' . $EnteredGLCode->GLCode . '</TD><TD>' . $EnteredGLCode->GLActName .
310 '</TD><TD ALIGN=RIGHT>' . number_format($EnteredGLCode->Amount,2) .
311 '</TD><TD>' . $EnteredGLCode->ShiptRef . '</TD><TD>' .$EnteredGLCode->JobRef .
312 '</TD><TD>' . $EnteredGLCode->Narrative . '</TD></TR>';
314 $TotalGLValue = $TotalGLValue + $EnteredGLCode->Amount;
316 $i++;
317 if ($i > 15){
318 $i = 0;
319 echo $TableHeader;
323 echo '<TR><TD COLSPAN=2 ALIGN=RIGHT><FONT SIZE=4 COLOR=blue>' . _('Total') . ':</FONT></TD>
324 <TD ALIGN=RIGHT><FONT SIZE=4 COLOR=blue><U>' . number_format($TotalGLValue,2) . '</U></FONT></TD>
325 </TR></TABLE>';
328 if (!isset($TotalGRNValue)){
329 $TotalGRNValue = 0;
331 if (!isset($TotalGLValue)){
332 $TotalGLValue = 0;
334 if (!isset($TotalShiptValue)){
335 $TotalShiptValue = 0;
338 $_SESSION['SuppTrans']->OvAmount = $TotalGRNValue + $TotalGLValue + $TotalShiptValue;
340 echo '<TABLE><TR><TD>' . _('Amount in supplier currency') . ':</TD><TD COLSPAN=2 ALIGN=RIGHT>' .
341 number_format( $_SESSION['SuppTrans']->OvAmount,2) . '</TD></TR>';
342 } else {
343 echo '<TABLE><TR><TD>' . _('Amount in supplier currency') .
344 ':</TD><TD COLSPAN=2 ALIGN=RIGHT><INPUT TYPE=TEXT SIZE=12 MAXLENGTH=10 NAME=OvAmount VALUE=' .
345 number_format( $_SESSION['SuppTrans']->OvAmount,2) . '></TD></TR>';
348 echo "<TR><TD COLSPAN=2><INPUT TYPE=Submit NAME='ToggleTaxMethod' VALUE='" . _('Change Tax Calculation Method') .
349 "'></TD><TD><SELECT NAME='OverRideTax'>";
351 if ($_POST['OverRideTax']=='Man'){
352 echo "<OPTION VALUE='Auto'>" . _('Automatic') . "<OPTION SELECTED VALUE='Man'>" . _('Manual');
353 } else {
354 echo "<OPTION SELECTED VALUE='Auto'>" . _('Automatic') . "<OPTION VALUE='Man'>" . _('Manual');
357 echo '</SELECT></TD></TR>';
358 $TaxTotal =0; //initialise tax total
360 foreach ($_SESSION['SuppTrans']->Taxes as $Tax) {
362 echo '<TR><TD>' . $Tax->TaxAuthDescription . '</TD><TD>';
364 /*Set the tax rate to what was entered */
365 if (isset($_POST['TaxRate' . $Tax->TaxCalculationOrder])){
366 $_SESSION['SuppTrans']->Taxes[$Tax->TaxCalculationOrder]->TaxRate = $_POST['TaxRate' . $Tax->TaxCalculationOrder]/100;
369 /*If a tax rate is entered that is not the same as it was previously then recalculate automatically the tax amounts */
371 if ($_POST['OverRideTax']=='Auto' OR !isset($_POST['OverRideTax'])){
373 echo ' <INPUT TYPE=TEXT NAME=TaxRate' . $Tax->TaxCalculationOrder . ' MAXLENGTH=4 SIZE=4 VALUE=' . $_SESSION['SuppTrans']->Taxes[$Tax->TaxCalculationOrder]->TaxRate * 100 . '>%';
375 /*Now recaluclate the tax depending on the method */
376 if ($Tax->TaxOnTax ==1){
378 $_SESSION['SuppTrans']->Taxes[$Tax->TaxCalculationOrder]->TaxOvAmount = $_SESSION['SuppTrans']->Taxes[$Tax->TaxCalculationOrder]->TaxRate * ($_SESSION['SuppTrans']->OvAmount + $TaxTotal);
380 } else { /*Calculate tax without the tax on tax */
382 $_SESSION['SuppTrans']->Taxes[$Tax->TaxCalculationOrder]->TaxOvAmount = $_SESSION['SuppTrans']->Taxes[$Tax->TaxCalculationOrder]->TaxRate * $_SESSION['SuppTrans']->OvAmount;
387 echo '<INPUT TYPE=HIDDEN NAME="TaxAmount' . $Tax->TaxCalculationOrder . '" VALUE=' . round($_SESSION['SuppTrans']->Taxes[$Tax->TaxCalculationOrder]->TaxOvAmount,2) . '>';
389 echo '</TD><TD ALIGN=RIGHT>' . number_format($_SESSION['SuppTrans']->Taxes[$Tax->TaxCalculationOrder]->TaxOvAmount,2);
391 } else { /*Tax being entered manually accept the taxamount entered as is*/
393 $_SESSION['SuppTrans']->Taxes[$Tax->TaxCalculationOrder]->TaxOvAmount = $_POST['TaxAmount' . $Tax->TaxCalculationOrder];
395 echo ' <INPUT TYPE=HIDDEN NAME=TaxRate' . $Tax->TaxCalculationOrder . ' VALUE=' . $_SESSION['SuppTrans']->Taxes[$Tax->TaxCalculationOrder]->TaxRate * 100 . '>';
397 echo '</TD><TD><INPUT TYPE=TEXT SIZE=12 MAXLENGTH=12 NAME="TaxAmount' . $Tax->TaxCalculationOrder . '" VALUE=' . round($_SESSION['SuppTrans']->Taxes[$Tax->TaxCalculationOrder]->TaxOvAmount,2) . '>';
400 $TaxTotal += $_SESSION['SuppTrans']->Taxes[$Tax->TaxCalculationOrder]->TaxOvAmount;
401 echo '</TD></TR>';
404 $_SESSION['SuppTrans']->OvAmount = round($_SESSION['SuppTrans']->OvAmount,2);
406 $DisplayTotal = number_format(( $_SESSION['SuppTrans']->OvAmount + $TaxTotal), 2);
408 echo '<TR><TD>' . _('Invoice Total') . ':</TD><TD COLSPAN=2 ALIGN=RIGHT><B>' . $DisplayTotal . '</B></TD></TR></TABLE>';
410 echo '<TABLE><TR><TD>' . _('Comments') . '</TD><TD><TEXTAREA NAME=Comments COLS=40 ROWS=2>' .
411 $_SESSION['SuppTrans']->Comments . '</TEXTAREA></TD></TR></TABLE>';
413 echo "<P><INPUT TYPE=SUBMIT NAME='PostInvoice' VALUE='" . _('Enter Invoice') . "'>";
415 } else { //do the postings -and dont show the button to process
417 /*First do input reasonableness checks
418 then do the updates and inserts to process the invoice entered */
420 foreach ($_SESSION['SuppTrans']->Taxes as $Tax) {
423 /*Set the tax rate to what was entered */
424 if (isset($_POST['TaxRate' . $Tax->TaxCalculationOrder])){
425 $_SESSION['SuppTrans']->Taxes[$Tax->TaxCalculationOrder]->TaxRate = $_POST['TaxRate' . $Tax->TaxCalculationOrder]/100;
429 if ($_POST['OverRideTax']=='Auto' OR !isset($_POST['OverRideTax'])){
431 /*Now recaluclate the tax depending on the method */
432 if ($Tax->TaxOnTax ==1){
434 $_SESSION['SuppTrans']->Taxes[$Tax->TaxCalculationOrder]->TaxOvAmount = $_SESSION['SuppTrans']->Taxes[$Tax->TaxCalculationOrder]->TaxRate * ($_SESSION['SuppTrans']->OvAmount + $TaxTotal);
436 } else { /*Calculate tax without the tax on tax */
438 $_SESSION['SuppTrans']->Taxes[$Tax->TaxCalculationOrder]->TaxOvAmount = $_SESSION['SuppTrans']->Taxes[$Tax->TaxCalculationOrder]->TaxRate * $_SESSION['SuppTrans']->OvAmount;
443 } else { /*Tax being entered manually accept the taxamount entered as is*/
445 $_SESSION['SuppTrans']->Taxes[$Tax->TaxCalculationOrder]->TaxOvAmount = $_POST['TaxAmount' . $Tax->TaxCalculationOrder];
451 /*Need to recalc the taxtotal */
453 $TaxTotal=0;
454 foreach ($_SESSION['SuppTrans']->Taxes as $Tax){
455 $TaxTotal += $Tax->TaxOvAmount;
458 $InputError = False;
459 if ( $TaxTotal + $_SESSION['SuppTrans']->OvAmount <= 0){
460 $InputError = True;
461 prnMsg(_('The invoice as entered cannot be processed because the total amount of the invoice is less than or equal to 0') . '. ' . _('Invoices are expected to have a charge'),'error');
463 } elseif (strlen( $_SESSION['SuppTrans']->SuppReference)<1){
464 $InputError = True;
465 prnMsg(_('The invoice as entered cannot be processed because the there is no suppliers invoice number or reference entered') . '. ' . _('The supplier invoice number must be entered'),'error');
467 } elseif (!is_date( $_SESSION['SuppTrans']->TranDate)){
468 $InputError = True;
469 prnMsg( _('The invoice as entered cannot be processed because the invoice date entered is not in the format') . ' ' . $_SESSION['DefaultDateFormat'],'error');
471 } elseif (DateDiff(Date($_SESSION['DefaultDateFormat']), $_SESSION['SuppTrans']->TranDate, "d") < 0){
472 $InputError = True;
473 prnMsg(_('The invoice as entered cannot be processed because the invoice date is after today') . '. ' . _('Purchase invoices are expected to have a date prior to or today'),'error');
475 }elseif ( $_SESSION['SuppTrans']->ExRate <= 0){
476 $InputError = True;
477 prnMsg( _('The invoice as entered cannot be processed because the exchange rate for the invoice 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'),'error');
479 }elseif ( $_SESSION['SuppTrans']->OvAmount < round($TotalShiptValue + $TotalGLValue + $TotalGRNValue,2)){
480 prnMsg( _('The invoice total as entered is less than the sum of the shipment charges, the general ledger entires (if any) and the charges for goods received') . '. ' . _('There must be a mistake somewhere, the invoice as entered will not be processed'),'error');
481 $InputError = True;
483 } else {
484 $sql = "SELECT count(*)
485 FROM supptrans
486 WHERE supplierno='" . $_SESSION['SuppTrans']->SupplierID . "'
487 AND supptrans.suppreference='" . $_POST['SuppReference'] . "'";
489 $ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('The sql to check for the previous entry of the same invoice failed');
490 $DbgMsg = _('The following SQL to start an SQL transaction was used');
492 $result=DB_query($sql, $db, $ErrMsg, $DbgMsg, True);
494 $myrow=DB_fetch_row($result);
495 if ($myrow[0] == 1){ /*Transaction reference already entered */
496 prnMsg( _('The invoice number') . ' : ' . $_POST['SuppReference'] . ' ' . _('has already been entered') . '. ' . _('It cannot be entered again'),'error');
497 $InputError = True;
501 if ($InputError == False){
503 /* SQL to process the postings for purchase invoice */
505 /*Start an SQL transaction */
507 $SQL = 'BEGIN';
509 $ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('The database does not support transactions');
510 $DbgMsg = _('The following SQL to start an SQL transaction was used');
512 $Result = DB_query($SQL, $db, $ErrMsg, $DbgMsg, True);
514 /*Get the next transaction number for internal purposes and the period to post GL transactions in based on the invoice date*/
515 $InvoiceNo = GetNextTransNo(20, $db);
516 $PeriodNo = GetPeriod( $_SESSION['SuppTrans']->TranDate, $db);
517 $SQLInvoiceDate = FormatDateForSQL( $_SESSION['SuppTrans']->TranDate);
519 if ( $_SESSION['SuppTrans']->GLLink_Creditors == 1){
520 /*Loop through the GL Entries and create a debit posting for each of the accounts entered */
521 $LocalTotal = 0;
523 /*the postings here are a little tricky, the logic goes like this:
524 if its a shipment entry then the cost must go against the GRN suspense account defined in the company record
526 if its a general ledger amount it goes straight to the account specified
528 if its a GRN amount invoiced then there are two possibilities:
530 1 The PO line is on a shipment.
531 The whole charge goes to the GRN suspense account pending the closure of the
532 shipment where the variance is calculated on the shipment as a whole and the clearing entry to the GRN suspense
533 is created. Also, shipment records are created for the charges in local currency.
535 2. The order line item is not on a shipment
536 The cost as originally credited to GRN suspense on arrival of goods is debited to GRN suspense.
537 Depending on the setting of WeightedAverageCosting:
538 If the order line item is a stock item and WeightedAverageCosting set to OFF then use standard costing .....
539 Any difference
540 between the std cost and the currency cost charged as converted at the ex rate of of the invoice is written off
541 to the purchase price variance account applicable to the stock item being invoiced.
542 Otherwise
543 Recalculate the new weighted average cost of the stock and update the cost - post the difference to the appropriate stock code
545 Or if its not a stock item
546 but a nominal item then the GL account in the orignal order is used for the price variance account.
549 foreach ($_SESSION['SuppTrans']->GLCodes as $EnteredGLCode){
551 /*GL Items are straight forward - just do the debit postings to the GL accounts specified -
552 the credit is to creditors control act done later for the total invoice value + tax*/
554 $SQL = 'INSERT INTO gltrans (type,
555 typeno,
556 trandate,
557 periodno,
558 account,
559 narrative,
560 amount,
561 jobref)
562 VALUES (20, ' .
563 $InvoiceNo . ",
564 '" . $SQLInvoiceDate . "',
565 " . $PeriodNo . ',
566 ' . $EnteredGLCode->GLCode . ",
567 '" . DB_escape_string($_SESSION['SuppTrans']->SupplierID) . ' ' . DB_escape_string($EnteredGLCode->Narrative) . "',
568 " . round($EnteredGLCode->Amount/ $_SESSION['SuppTrans']->ExRate,2) . ",
569 '" . DB_escape_string($EnteredGLCode->JobRef) . "')";
571 $ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('The general ledger transaction could not be added because');
572 $DbgMsg = _('The following SQL to insert the GL transaction was used');
574 $Result = DB_query($SQL, $db, $ErrMsg, $DbgMsg, True);
576 $LocalTotal += round($EnteredGLCode->Amount/ $_SESSION['SuppTrans']->ExRate,2);
579 foreach ($_SESSION['SuppTrans']->Shipts as $ShiptChg){
581 /*shipment postings are also straight forward - just do the debit postings to the GRN suspense account
582 these entries are reversed from the GRN suspense when the shipment is closed*/
584 $SQL = 'INSERT INTO gltrans (type,
585 typeno,
586 trandate,
587 periodno,
588 account,
589 narrative,
590 amount)
591 VALUES (20, ' .
592 $InvoiceNo . ",
593 '" . $SQLInvoiceDate . "',
594 " . $PeriodNo . ',
595 ' . $_SESSION['SuppTrans']->GRNAct . ",
596 '" . DB_escape_string($_SESSION['SuppTrans']->SupplierID) . ' ' . _('Shipment charge against') . ' ' . $ShiptChg->ShiptRef . "',
597 " . $ShiptChg->Amount/ $_SESSION['SuppTrans']->ExRate . ')';
599 $ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('The general ledger transaction for the shipment') .
600 ' ' . $ShiptChg->ShiptRef . ' ' . _('could not be added because');
602 $DbgMsg = _('The following SQL to insert the GL transaction was used');
604 $Result = DB_query($SQL, $db, $ErrMsg, $DbgMsg, True);
606 $LocalTotal += $ShiptChg->Amount/ $_SESSION['SuppTrans']->ExRate;
610 foreach ($_SESSION['SuppTrans']->GRNs as $EnteredGRN){
612 if (strlen($EnteredGRN->ShiptRef) == 0 OR $EnteredGRN->ShiptRef == 0){
613 /*so its not a shipment item
614 enter the GL entry to reverse the GRN suspense entry created on delivery at standard cost used on delivery */
616 if ($EnteredGRN->StdCostUnit * $EnteredGRN->This_QuantityInv != 0) {
617 $SQL = 'INSERT INTO gltrans (type,
618 typeno,
619 trandate,
620 periodno,
621 account,
622 narrative,
623 amount)
624 VALUES (20, ' . $InvoiceNo . ",
625 '" . $SQLInvoiceDate . "',
626 " . $PeriodNo . ',
627 ' . $_SESSION['SuppTrans']->GRNAct . ",
628 '" . DB_escape_string($_SESSION['SuppTrans']->SupplierID) . ' - ' . _('GRN') . ' ' . $EnteredGRN->GRNNo . ' - ' . DB_escape_string($EnteredGRN->ItemCode) . ' x ' . $EnteredGRN->This_QuantityInv . ' @ ' .
629 _('std cost of') . ' ' . $EnteredGRN->StdCostUnit . "',
630 " . $EnteredGRN->StdCostUnit * $EnteredGRN->This_QuantityInv . ')';
632 $ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('The general ledger transaction could not be added because');
634 $DbgMsg = _('The following SQL to insert the GL transaction was used');
636 $Result = DB_query($SQL, $db, $ErrMsg, $Dbg, True);
641 $PurchPriceVar = round($EnteredGRN->This_QuantityInv * (($EnteredGRN->ChgPrice / $_SESSION['SuppTrans']->ExRate) - $EnteredGRN->StdCostUnit),2);
644 /*Yes.... but where to post this difference to - if its a stock item the variance account must be retreived from the stock category record
645 if its a nominal purchase order item with no stock item then there will be no standard cost and it will all be variance so post it to the
646 account specified in the purchase order detail record */
648 if ($PurchPriceVar !=0){ /* don't bother with this lot if there is no difference ! */
649 if (strlen($EnteredGRN->ItemCode)>0 OR $EnteredGRN->ItemCode != ''){ /*so it is a stock item */
651 /*need to get the stock category record for this stock item - this is function in SQL_CommonFunctions.inc */
652 $StockGLCode = GetStockGLCode($EnteredGRN->ItemCode,$db);
654 /*We have stock item and a purchase price variance need to see whether we are using Standard or WeightedAverageCosting */
656 if ($_SESSION['WeightedAverageCosting']==1){ /*Weighted Average costing */
659 First off figure out the new weighted average cost Need the following data:
661 How many in stock now
662 The quantity being invoiced here - $EnteredGRN->This_QuantityInv
663 The cost of these items - $EnteredGRN->ChgPrice / $_SESSION['SuppTrans']->ExRate
666 $sql ='SELECT SUM(quantity) FROM locstock WHERE stockid="' . $EnteredGRN->ItemCode . '"';
667 $ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('The quantity on hand could not be retrieved from the database');
668 $DbgMsg = _('The following SQL to retrieve the total stock quantity was used');
669 $Result = DB_query($sql, $db, $ErrMsg, $DbgMsg, True);
670 $QtyRow = DB_fetch_row($Result);
671 $TotalQuantityOnHand = $QtyRow[0];
674 /*The cost adjustment is the price variance / the total quantity in stock
675 But that's only provided that the total quantity in stock is > the quantity charged on this invoice
677 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*/
679 $WriteOffToVariances =0;
681 if ($EnteredGRN->This_QuantityInv > $TotalQuantityOnHand){
683 /*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 */
685 $WriteOffToVariances = ($EnteredGRN->This_QuantityInv
686 - $TotalQuantityOnHand)
687 * (($EnteredGRN->ChgPrice / $_SESSION['SuppTrans']->ExRate) - $EnteredGRN->StdCostUnit);
689 $SQL = 'INSERT INTO gltrans (type,
690 typeno,
691 trandate,
692 periodno,
693 account,
694 narrative,
695 amount)
696 VALUES (20, ' .
697 $InvoiceNo . ", '" . $SQLInvoiceDate . "', " . $PeriodNo . ', ' . $StockGLCode['purchpricevaract'] .
698 ", '" . $_SESSION['SuppTrans']->SupplierID . ' - ' . _('GRN') . ' ' . $EnteredGRN->GRNNo .
699 ' - ' . DB_escape_string($EnteredGRN->ItemCode) . ' x ' . ($EnteredGRN->This_QuantityInv-$TotalQuantityOnHand) . ' x ' . _('price var of') . ' ' .
700 number_format(($EnteredGRN->ChgPrice / $_SESSION['SuppTrans']->ExRate) - $EnteredGRN->StdCostUnit,2) .
701 "', " . $WriteOffToVariances . ')';
703 $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');
704 $DbgMsg = _('The following SQL to insert the GL transaction was used');
707 $Result = DB_query($SQL, $db, $ErrMsg, $DbgMsg, True);
709 /*Now post any remaining price variance to stock rather than price variances */
711 $SQL = 'INSERT INTO gltrans (type,
712 typeno,
713 trandate,
714 periodno,
715 account,
716 narrative,
717 amount)
718 VALUES (20, ' .
719 $InvoiceNo . ",
720 '" . $SQLInvoiceDate . "',
721 " . $PeriodNo . ',
722 ' . $StockGLCode['stockact'] . ",
723 '" . DB_escape_string($_SESSION['SuppTrans']->SupplierID) . ' - ' . ('Average Cost Adj') .
724 ' - ' . DB_escape_string($EnteredGRN->ItemCode) . ' x ' . $TotalQuantityOnHand . ' x ' .
725 number_format(($EnteredGRN->ChgPrice / $_SESSION['SuppTrans']->ExRate) - $EnteredGRN->StdCostUnit,2) .
726 "', " . ($PurchPriceVar - $WriteOffToVariances) . ')';
728 $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');
729 $DbgMsg = _('The following SQL to insert the GL transaction was used');
731 $Result = DB_query($SQL, $db, $ErrMsg, $DbgMsg, True);
733 /*Now to update the stock cost with the new weighted average */
735 /*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.
737 A nicety or important?? */
740 $ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('The cost could not be updated because');
741 $DbgMsg = _('The following SQL to update the cost was used');
743 if ($TotalQuantityOnHand>0) {
746 $CostIncrement = ($PurchPriceVar - $WriteOffToVariances) / $TotalQuantityOnHand;
748 $sql = 'UPDATE stockmaster SET lastcost=materialcost+overheadcost+labourcost,
749 materialcost=materialcost+' . $CostIncrement . ' WHERE stockid="' . $EnteredGRN->ItemCode . '"';
750 $Result = DB_query($sql, $db, $ErrMsg, $DbgMsg, True);
751 } else {
752 $sql = 'UPDATE stockmaster SET lastcost=materialcost+overheadcost+labourcost,
753 materialcost=' . ($EnteredGRN->ChgPrice / $_SESSION['SuppTrans']->ExRate) . ' WHERE stockid="' . $EnteredGRN->ItemCode . '"';
754 $Result = DB_query($sql, $db, $ErrMsg, $DbgMsg, True);
756 /* End of Weighted Average Costing Code */
758 } else { //It must be Standard Costing
760 $SQL = 'INSERT INTO gltrans (type,
761 typeno,
762 trandate,
763 periodno,
764 account,
765 narrative,
766 amount)
767 VALUES (20, ' .
768 $InvoiceNo . ", '" . $SQLInvoiceDate . "', " . $PeriodNo . ', ' . $StockGLCode['purchpricevaract'] .
769 ", '" . $_SESSION['SuppTrans']->SupplierID . ' - ' . _('GRN') . ' ' . $EnteredGRN->GRNNo .
770 ' - ' . DB_escape_string($EnteredGRN->ItemCode) . ' x ' . $EnteredGRN->This_QuantityInv . ' x ' . _('price var of') . ' ' .
771 number_format(($EnteredGRN->ChgPrice / $_SESSION['SuppTrans']->ExRate) - $EnteredGRN->StdCostUnit,2) .
772 "', " . $PurchPriceVar . ')';
774 $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');
775 $DbgMsg = _('The following SQL to insert the GL transaction was used');
777 $Result = DB_query($SQL, $db, $ErrMsg, $DbgMsg, True);
779 } else {
781 /* 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
782 order price and the actual invoice price since the std cost was made equal to the order price in local currency at the time
783 the goods were received */
785 $SQL = 'INSERT INTO gltrans (type,
786 typeno,
787 trandate,
788 periodno,
789 account,
790 narrative,
791 amount)
792 VALUES (20,
793 ' . $InvoiceNo . ",
794 '" . $SQLInvoiceDate . "',
795 " . $PeriodNo . ',
796 ' . $EnteredGRN->GLCode . ",
797 '" . $_SESSION['SuppTrans']->SupplierID . ' - ' . _('GRN') . ' ' . $EnteredGRN->GRNNo . ' - ' .
798 $EnteredGRN->ItemDescription . ' x ' . $EnteredGRN->This_QuantityInv . ' x ' . _('price var') .
799 ' ' . number_format(($EnteredGRN->ChgPrice / $_SESSION['SuppTrans']->ExRate) - $EnteredGRN->StdCostUnit,2) . "',
800 " . $PurchPriceVar . ')';
802 $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');
804 $DbgMsg = _('The following SQL to insert the GL transaction was used');
806 $Result = DB_query($SQL, $db, $ErrMsg, $DbgMsg, True);
810 } else {
811 /*then its a purchase order item on a shipment - whole charge amount to GRN suspense pending closure of the shipment when the variance is calculated and the GRN act cleared up for the shipment */
813 $SQL = 'INSERT INTO gltrans (type,
814 typeno,
815 trandate,
816 periodno,
817 account,
818 narrative,
819 amount)
820 VALUES (20, ' .
821 $InvoiceNo . ", '" . $SQLInvoiceDate . "', " . $PeriodNo . ', ' . $_SESSION['SuppTrans']->GRNAct .
822 ", '" . $_SESSION['SuppTrans']->SupplierID . ' - ' . _('GRN') . ' ' . $EnteredGRN->GRNNo . ' - ' .
823 $EnteredGRN->ItemCode . ' x ' . $EnteredGRN->This_QuantityInv . ' @ ' .
824 $_SESSION['SuppTrans']->CurrCode . ' ' . $EnteredGRN->ChgPrice . ' @ ' . _('a rate of') . ' ' .
825 $_SESSION['SuppTrans']->ExRate . "', " .
826 round(($EnteredGRN->ChgPrice * $EnteredGRN->This_QuantityInv) / $_SESSION['SuppTrans']->ExRate,2) . ')';
828 $ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('The general ledger transaction could not be added because');
830 $DbgMsg = _('The following SQL to insert the GL transaction was used');
832 $Result = DB_query($SQL, $db, $ErrMsg, $DbgMsg, True);
834 $LocalTotal += round(($EnteredGRN->ChgPrice * $EnteredGRN->This_QuantityInv) / $_SESSION['SuppTrans']->ExRate,2);
835 } /* end of GRN postings */
837 if ($debug == 1 AND ( abs($_SESSION['SuppTrans']->OvAmount/ $_SESSION['SuppTrans']->ExRate) - $LocalTotal) >0.009999){
839 echo '<P>' . _('The total posted to the debit accounts is') . ' ' .
840 $LocalTotal . ' ' . _('but the sum of OvAmount converted at ExRate') . ' = ' .
841 ( $_SESSION['SuppTrans']->OvAmount / $_SESSION['SuppTrans']->ExRate);
844 foreach ($_SESSION['SuppTrans']->Taxes as $Tax){
845 /* Now the TAX account */
846 if ($Tax->TaxOvAmount <>0){
847 $SQL = 'INSERT INTO gltrans (type,
848 typeno,
849 trandate,
850 periodno,
851 account,
852 narrative,
853 amount)
854 VALUES (20, ' .
855 $InvoiceNo . ",
856 '" . $SQLInvoiceDate . "',
857 " . $PeriodNo . ',
858 ' . $Tax->TaxGLCode . ",
859 '" . $_SESSION['SuppTrans']->SupplierID . ' - ' . _('Inv') . ' ' .
860 $_SESSION['SuppTrans']->SuppReference . ' ' . $Tax->TaxAuthDescription . ' ' . number_format($Tax->TaxRate*100,2) . '% ' . $_SESSION['SuppTrans']->CurrCode .
861 $Tax->TaxOvAmount . ' @ ' . _('exch rate') . ' ' . $_SESSION['SuppTrans']->ExRate .
863 " . round( $Tax->TaxOvAmount/ $_SESSION['SuppTrans']->ExRate,2) . ')';
865 $ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('The general ledger transaction for the tax could not be added because');
867 $DbgMsg = _('The following SQL to insert the GL transaction was used');
869 $Result = DB_query($SQL, $db, $ErrMsg, $DbgMsg, True);
872 } /*end of loop to post the tax */
873 /* Now the control account */
875 $SQL = 'INSERT INTO gltrans (type,
876 typeno,
877 trandate,
878 periodno,
879 account,
880 narrative,
881 amount)
882 VALUES (20, ' .
883 $InvoiceNo . ", '" . $SQLInvoiceDate . "', " . $PeriodNo . ', ' . $_SESSION['SuppTrans']->CreditorsAct .
884 ", '" . $_SESSION['SuppTrans']->SupplierID . ' - ' . _('Inv') . ' ' .
885 $_SESSION['SuppTrans']->SuppReference . ' ' . $_SESSION['SuppTrans']->CurrCode .
886 number_format( $_SESSION['SuppTrans']->OvAmount + $TaxTotal,2) .
887 ' @ ' . _('a rate of') . ' ' . $_SESSION['SuppTrans']->ExRate . "', " .
888 -round(($LocalTotal + ( $TaxTotal / $_SESSION['SuppTrans']->ExRate)),2) . ')';
890 $ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('The general ledger transaction for the control total could not be added because');
892 $DbgMsg = _('The following SQL to insert the GL transaction was used');
894 $Result = DB_query($SQL, $db, $ErrMsg, $DbgMsg, True);
896 } /*Thats the end of the GL postings */
898 /*Now insert the invoice into the SuppTrans table*/
900 $SQL = 'INSERT INTO supptrans (transno,
901 type,
902 supplierno,
903 suppreference,
904 trandate,
905 duedate,
906 ovamount,
907 ovgst,
908 rate,
909 transtext)
910 VALUES ('. $InvoiceNo . ",
911 20 ,
912 '" . $_SESSION['SuppTrans']->SupplierID . "',
913 '" . $_SESSION['SuppTrans']->SuppReference . "',
914 '" . $SQLInvoiceDate . "',
915 '" . FormatDateForSQL($_SESSION['SuppTrans']->DueDate) . "',
916 " . round($_SESSION['SuppTrans']->OvAmount,2) . ',
917 ' . round($TaxTotal,2) . ',
918 ' . $_SESSION['SuppTrans']->ExRate . ",
919 '" . DB_escape_string($_SESSION['SuppTrans']->Comments) . "')";
921 $ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('The supplier invoice transaction could not be added to the database because');
923 $DbgMsg = _('The following SQL to insert the supplier invoice was used');
925 $Result = DB_query($SQL, $db, $ErrMsg, $DbgMsg, True);
927 $SuppTransID = DB_Last_Insert_ID($db,'supptrans','id');
929 /* Insert the tax totals for each tax authority where tax was charged on the invoice */
930 foreach ($_SESSION['SuppTrans']->Taxes AS $TaxTotals) {
932 $SQL = 'INSERT INTO supptranstaxes (supptransid,
933 taxauthid,
934 taxamount)
935 VALUES (' . $SuppTransID . ',
936 ' . $TaxTotals->TaxAuthID . ',
937 ' . $TaxTotals->TaxOvAmount . ')';
939 $ErrMsg =_('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('The supplier transaction taxes records could not be inserted because');
940 $DbgMsg = _('The following SQL to insert the supplier transaction taxes record was used:');
941 $Result = DB_query($SQL,$db,$ErrMsg,$DbgMsg,true);
944 /* Now update the GRN and PurchOrderDetails records for amounts invoiced */
946 foreach ($_SESSION['SuppTrans']->GRNs as $EnteredGRN){
948 $SQL = 'UPDATE purchorderdetails SET qtyinvoiced = qtyinvoiced + ' . $EnteredGRN->This_QuantityInv .',
949 actprice = ' . $EnteredGRN->ChgPrice . '
950 WHERE podetailitem = ' . $EnteredGRN->PODetailItem;
952 $ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('The quantity invoiced of the purchase order line could not be updated because');
954 $DbgMsg = _('The following SQL to update the purchase order details was used');
956 $Result = DB_query($SQL, $db, $ErrMsg, $DbgMsg, True);
958 $SQL = 'UPDATE grns SET quantityinv = quantityinv + ' . $EnteredGRN->This_QuantityInv .
959 ' WHERE grnno = ' . $EnteredGRN->GRNNo;
961 $ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('The quantity invoiced off the goods received record could not be updated because');
963 $DbgMsg = _('The following SQL to update the GRN quantity invoiced was used');
965 $Result = DB_query($SQL, $db, $ErrMsg, $DbgMsg, True);
968 if (strlen($EnteredGRN->ShiptRef)>0 AND $EnteredGRN->ShiptRef != '0'){
970 /* insert the shipment charge records */
972 $SQL = 'INSERT INTO shipmentcharges (shiptref,
973 transtype,
974 transno,
975 stockid,
976 value)
977 VALUES (' . $EnteredGRN->ShiptRef . ',
978 20,
979 ' . $InvoiceNo . ",
980 '" . DB_escape_string($EnteredGRN->ItemCode) . "',
981 " . ($EnteredGRN->This_QuantityInv * $EnteredGRN->ChgPrice) / $_SESSION['SuppTrans']->ExRate . ')';
983 $ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('The shipment charge record for the shipment') .
984 ' ' . $EnteredGRN->ShiptRef . ' ' . _('could not be added because');
986 $DbgMsg = _('The following SQL to insert the Shipment charge record was used');
988 $Result = DB_query($SQL, $db, $ErrMsg, $DbgMsg, True);
992 } /* end of the loop to do the updates for the quantity of order items the supplier has invoiced */
994 /*Add shipment charges records as necessary */
996 foreach ($_SESSION['SuppTrans']->Shipts as $ShiptChg){
998 $SQL = 'INSERT INTO shipmentcharges (shiptref,
999 transtype,
1000 transno,
1001 value)
1002 VALUES (' . $ShiptChg->ShiptRef . ',
1003 20,
1004 ' . $InvoiceNo . ',
1005 ' . $ShiptChg->Amount/ $_SESSION['SuppTrans']->ExRate . ')';
1007 $ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('The shipment charge record for the shipment') .
1008 ' ' . $ShiptChg->ShiptRef . ' ' . _('could not be added because');
1010 $DbgMsg = _('The following SQL to insert the Shipment charge record was used');
1012 $Result = DB_query($SQL, $db, $ErrMsg, $DbgMsg, True);
1017 $SQL="COMMIT";
1019 $ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('The SQL COMMIT failed because');
1021 $DbgMsg = _('The SQL COMMIT failed');
1023 $Result = DB_query($SQL, $db, $ErrMsg, $DbgMsg, True);
1025 unset( $_SESSION['SuppTrans']->GRNs);
1026 unset( $_SESSION['SuppTrans']->Shipts);
1027 unset( $_SESSION['SuppTrans']->GLCodes);
1028 unset( $_SESSION['SuppTrans']);
1030 prnMsg(_('Supplier invoice number') . ' ' . $InvoiceNo . ' ' . _('has been processed'),'success');
1031 echo "<P><A HREF='$rootpath/SelectSupplier.php'>" . _('Enter Another invoice') . '</A>';
1034 } /*end of process invoice */
1036 echo '</FORM>';
1037 include('includes/footer.inc');