1<?php
2
3
4include('includes/session.php');
5
6$ViewTopic = 'ARReports';
7$BookMark = 'PrintInvoicesCredits';
8
9if (isset($_GET['FromTransNo'])) {
10	$FromTransNo = filter_number_format($_GET['FromTransNo']);
11} elseif (isset($_POST['FromTransNo'])) {
12	$FromTransNo = filter_number_format($_POST['FromTransNo']);
13} else {
14	$FromTransNo = '';
15}
16
17if (isset($_GET['InvOrCredit'])) {
18	$InvOrCredit = $_GET['InvOrCredit'];
19} elseif (isset($_POST['InvOrCredit'])) {
20	$InvOrCredit = $_POST['InvOrCredit'];
21}
22
23if (isset($_GET['PrintPDF'])) {
24	$PrintPDF = $_GET['PrintPDF'];
25} elseif (isset($_POST['PrintPDF'])) {
26	$PrintPDF = $_POST['PrintPDF'];
27}
28
29if (!isset($_POST['ToTransNo'])
30	OR trim($_POST['ToTransNo'])==''
31	OR filter_number_format($_POST['ToTransNo']) < $FromTransNo) {
32
33	$_POST['ToTransNo'] = $FromTransNo;
34}
35
36$FirstTrans = $FromTransNo; /* Need to start a new page only on subsequent transactions */
37
38if (isset($PrintPDF)
39		and $PrintPDF!=''
40		and isset($FromTransNo)
41		and isset($InvOrCredit)
42		and $FromTransNo!='') {
43
44	$PaperSize = 'A4_Landscape';
45	include ('includes/PDFStarter.php');
46
47	if ($InvOrCredit=='Invoice') {
48		$pdf->addInfo('Title',_('Sales Invoice') . ' ' . $FromTransNo . ' ' . _('to') . ' ' . $_POST['ToTransNo']);
49		$pdf->addInfo('Subject',_('Invoices from') . ' ' . $FromTransNo . ' ' . _('to') . ' ' . $_POST['ToTransNo']);
50	} else {
51		$pdf->addInfo('Title',_('Sales Credit Note') );
52		$pdf->addInfo('Subject',_('Credit Notes from') . ' ' . $FromTransNo . ' ' . _('to') . ' ' . $_POST['ToTransNo']);
53	}
54
55	$FirstPage = true;
56	$line_height=16;
57
58	//Keep a record of the user's language
59	$UserLanguage = $_SESSION['Language'];
60
61	while ($FromTransNo <= filter_number_format($_POST['ToTransNo'])){
62
63	/* retrieve the invoice details from the database to print
64	notice that salesorder record must be present to print the invoice purging of sales orders will
65	nobble the invoice reprints */
66
67	// check if the user has set a default bank account for invoices, if not leave it blank
68		$sql = "SELECT bankaccounts.invoice,
69					bankaccounts.bankaccountnumber,
70					bankaccounts.bankaccountcode
71				FROM bankaccounts
72				WHERE bankaccounts.invoice = '1'";
73		$result=DB_query($sql,'','',false,false);
74		if(DB_error_no()!=1) {
75			if(DB_num_rows($result)==1) {
76				$myrow = DB_fetch_array($result);
77				$DefaultBankAccountNumber = _('Account') .': ' .$myrow['bankaccountnumber'];
78				$DefaultBankAccountCode = _('Bank Code:') .' ' .$myrow['bankaccountcode'];
79			} else {
80				$DefaultBankAccountNumber = '';
81				$DefaultBankAccountCode = '';
82			}
83		} else {
84			$DefaultBankAccountNumber = '';
85			$DefaultBankAccountCode = '';
86		}
87// gather the invoice data
88
89		if ($InvOrCredit=='Invoice') {
90			$sql = "SELECT debtortrans.trandate,
91							debtortrans.ovamount,
92							debtortrans.ovdiscount,
93							debtortrans.ovfreight,
94							debtortrans.ovgst,
95							debtortrans.rate,
96							debtortrans.invtext,
97							debtortrans.consignment,
98							debtortrans.packages,
99							debtorsmaster.name,
100							debtorsmaster.address1,
101							debtorsmaster.address2,
102							debtorsmaster.address3,
103							debtorsmaster.address4,
104							debtorsmaster.address5,
105							debtorsmaster.address6,
106							debtorsmaster.currcode,
107							debtorsmaster.invaddrbranch,
108							debtorsmaster.taxref,
109							debtorsmaster.language_id,
110							paymentterms.terms,
111							paymentterms.dayinfollowingmonth,
112							paymentterms.daysbeforedue,
113							salesorders.deliverto,
114							salesorders.deladd1,
115							salesorders.deladd2,
116							salesorders.deladd3,
117							salesorders.deladd4,
118							salesorders.deladd5,
119							salesorders.deladd6,
120							salesorders.customerref,
121							salesorders.orderno,
122							salesorders.orddate,
123							locations.locationname,
124							shippers.shippername,
125							custbranch.brname,
126							custbranch.braddress1,
127							custbranch.braddress2,
128							custbranch.braddress3,
129							custbranch.braddress4,
130							custbranch.braddress5,
131							custbranch.braddress6,
132							custbranch.brpostaddr1,
133							custbranch.brpostaddr2,
134							custbranch.brpostaddr3,
135							custbranch.brpostaddr4,
136							custbranch.brpostaddr5,
137							custbranch.brpostaddr6,
138							custbranch.salesman,
139							salesman.salesmanname,
140							debtortrans.debtorno,
141							debtortrans.branchcode,
142							currencies.decimalplaces
143						FROM debtortrans INNER JOIN debtorsmaster
144						ON debtortrans.debtorno=debtorsmaster.debtorno
145						INNER JOIN custbranch
146						ON debtortrans.debtorno=custbranch.debtorno
147						AND debtortrans.branchcode=custbranch.branchcode
148						INNER JOIN salesorders
149						ON debtortrans.order_ = salesorders.orderno
150						INNER JOIN shippers
151						ON debtortrans.shipvia=shippers.shipper_id
152						INNER JOIN salesman
153						ON custbranch.salesman=salesman.salesmancode
154						INNER JOIN locations
155						ON salesorders.fromstkloc=locations.loccode
156						INNER JOIN locationusers
157						ON locationusers.loccode=locations.loccode AND locationusers.userid='" .  $_SESSION['UserID'] . "' AND locationusers.canview=1
158						INNER JOIN paymentterms
159						ON debtorsmaster.paymentterms=paymentterms.termsindicator
160						INNER JOIN currencies
161						ON debtorsmaster.currcode=currencies.currabrev
162						WHERE debtortrans.type=10
163						AND debtortrans.transno='" . $FromTransNo . "'";
164
165			if(isset($_POST['PrintEDI']) AND $_POST['PrintEDI']=='No') {
166				$sql = $sql . ' AND debtorsmaster.ediinvoices=0';
167			}
168		} else {/* then its a credit note */
169			$sql = "SELECT debtortrans.trandate,
170							debtortrans.ovamount,
171							debtortrans.ovdiscount,
172							debtortrans.ovfreight,
173							debtortrans.ovgst,
174							debtortrans.rate,
175							debtortrans.invtext,
176							debtorsmaster.invaddrbranch,
177							debtorsmaster.name,
178							debtorsmaster.address1,
179							debtorsmaster.address2,
180							debtorsmaster.address3,
181							debtorsmaster.address4,
182							debtorsmaster.address5,
183							debtorsmaster.address6,
184							debtorsmaster.currcode,
185							debtorsmaster.taxref,
186							debtorsmaster.language_id,
187							custbranch.brname,
188							custbranch.braddress1,
189							custbranch.braddress2,
190							custbranch.braddress3,
191							custbranch.braddress4,
192							custbranch.braddress5,
193							custbranch.braddress6,
194							custbranch.brpostaddr1,
195							custbranch.brpostaddr2,
196							custbranch.brpostaddr3,
197							custbranch.brpostaddr4,
198							custbranch.brpostaddr5,
199							custbranch.brpostaddr6,
200							custbranch.salesman,
201							salesman.salesmanname,
202							debtortrans.debtorno,
203							debtortrans.branchcode,
204							currencies.decimalplaces
205						FROM debtortrans INNER JOIN debtorsmaster
206						ON debtortrans.debtorno=debtorsmaster.debtorno
207						INNER JOIN custbranch
208						ON debtortrans.debtorno=custbranch.debtorno
209						AND debtortrans.branchcode=custbranch.branchcode
210						INNER JOIN salesman
211						ON custbranch.salesman=salesman.salesmancode
212						INNER JOIN currencies
213						ON debtorsmaster.currcode=currencies.currabrev
214						WHERE debtortrans.type=11
215						AND debtortrans.transno='" . $FromTransNo . "'";
216
217
218			if(isset($_POST['PrintEDI']) AND $_POST['PrintEDI']=='No') {
219				$sql = $sql . ' AND debtorsmaster.ediinvoices=0';
220			}
221		} // end else
222
223		$result=DB_query($sql,'','',false, false);
224
225		if (DB_error_no()!=0) {
226			$Title = _('Transaction Print Error Report');
227			include ('includes/header.php');
228			prnMsg( _('There was a problem retrieving the invoice or credit note details for note number') . ' ' . $InvoiceToPrint . ' ' . _('from the database') . '. ' . _('To print an invoice, the sales order record, the customer transaction record and the branch record for the customer must not have been purged') . '. ' . _('To print a credit note only requires the customer, transaction, salesman and branch records be available'),'error');
229			if ($debug==1) {
230				prnMsg (_('The SQL used to get this information that failed was') . '<br />' . $sql,'error');
231			}
232			include ('includes/footer.php');
233			exit;
234		}
235		if (DB_num_rows($result)==1) {
236			$myrow = DB_fetch_array($result);
237
238			if ( $_SESSION['SalesmanLogin'] != '' AND $_SESSION['SalesmanLogin'] != $myrow['salesman'] ){
239				$Title=_('Select Invoices/Credit Notes To Print');
240				include('includes/header.php');
241					prnMsg(_('Your account is set up to see only a specific salespersons orders. You are not authorised to view transaction for this order'),'error');
242				include('includes/footer.php');
243					exit;
244			}
245			if ( $CustomerLogin == 1 AND $myrow['debtorno'] != $_SESSION['CustomerID'] ){
246				$Title=_('Select Invoices/Credit Notes To Print');
247				include('includes/header.php');
248					echo '<p class="bad">' . _('This transaction is addressed to another customer and cannot be displayed for privacy reasons') . '. ' . _('Please select only transactions relevant to your company').'</p>';
249				include('includes/footer.php');
250					exit;
251				}
252
253			$ExchRate = $myrow['rate'];
254
255			//Change the language to the customer's language
256			$_SESSION['Language'] = $myrow['language_id'];
257			include('includes/LanguageSetup.php');
258
259
260			if ($InvOrCredit=='Invoice') {
261
262				$sql = "SELECT stockmoves.stockid,
263								stockmaster.description,
264								-stockmoves.qty as quantity,
265								stockmoves.discountpercent,
266								((1 - stockmoves.discountpercent) * stockmoves.price * " . $ExchRate . "* -stockmoves.qty) AS fxnet,
267								(stockmoves.price * " . $ExchRate . ") AS fxprice,
268								stockmoves.narrative,
269								stockmaster.controlled,
270								stockmaster.serialised,
271								stockmaster.units,
272								stockmoves.stkmoveno,
273								stockmaster.decimalplaces
274							FROM stockmoves INNER JOIN stockmaster
275							ON stockmoves.stockid = stockmaster.stockid
276							WHERE stockmoves.type=10
277							AND stockmoves.transno='" . $FromTransNo . "'
278							AND stockmoves.show_on_inv_crds=1";
279			} else {
280		/* only credit notes to be retrieved */
281				$sql = "SELECT stockmoves.stockid,
282								stockmaster.description,
283								stockmoves.qty as quantity,
284								stockmoves.discountpercent,
285								((1 - stockmoves.discountpercent) * stockmoves.price * " . $ExchRate . " * stockmoves.qty) AS fxnet,
286								(stockmoves.price * " . $ExchRate . ") AS fxprice,
287								stockmoves.narrative,
288								stockmaster.controlled,
289								stockmaster.serialised,
290								stockmaster.units,
291								stockmoves.stkmoveno,
292								stockmaster.decimalplaces
293							FROM stockmoves INNER JOIN stockmaster
294							ON stockmoves.stockid = stockmaster.stockid
295							WHERE stockmoves.type=11
296							AND stockmoves.transno='" . $FromTransNo . "'
297							AND stockmoves.show_on_inv_crds=1";
298			} // end else
299
300			$result=DB_query($sql);
301			if(DB_error_no()!=0) {
302
303				$Title = _('Transaction Print Error Report');
304				include ('includes/header.php');
305				echo '<br />' . _('There was a problem retrieving the invoice or credit note stock movement details for invoice number') . ' ' . $FromTransNo . ' ' . _('from the database');
306				if ($debug==1) {
307					echo '<br />' . _('The SQL used to get this information that failed was') . '<br />' . $sql;
308				}
309				include('includes/footer.php');
310				exit;
311			}
312
313			if ($InvOrCredit=='Invoice') {
314				/* Calculate Due Date info. This reference is used in the PDFTransPageHeader.inc file. */
315				$DisplayDueDate = CalcDueDate(ConvertSQLDate($myrow['trandate']), $myrow['dayinfollowingmonth'], $myrow['daysbeforedue']);
316			}
317
318			if (DB_num_rows($result)>0) {
319
320				$FontSize = 10;
321				$PageNumber = 1;
322
323				include('includes/PDFTransPageHeader.inc');
324				$FirstPage = False;
325
326
327				while ($myrow2=DB_fetch_array($result)) {
328					if ($myrow2['discountpercent']==0) {
329						$DisplayDiscount ='';
330					} else {
331						$DisplayDiscount = locale_number_format($myrow2['discountpercent']*100,2) . '%';
332						$DiscountPrice=$myrow2['fxprice']*(1-$myrow2['discountpercent']);
333					}
334					$DisplayNet=locale_number_format($myrow2['fxnet'],$myrow['decimalplaces']);
335					$DisplayPrice=locale_number_format($myrow2['fxprice'],$myrow['decimalplaces']);
336					$DisplayQty=locale_number_format($myrow2['quantity'],$myrow2['decimalplaces']);
337
338					$LeftOvers = $pdf->addTextWrap($Left_Margin+3,$YPos,95,$FontSize,$myrow2['stockid']);
339					//Get translation if it exists
340					$TranslationResult = DB_query("SELECT descriptiontranslation
341													FROM stockdescriptiontranslations
342													WHERE stockid='" . $myrow2['stockid'] . "'
343													AND language_id='" . $myrow['language_id'] ."'");
344
345					if (DB_num_rows($TranslationResult)==1){ //there is a translation
346						$TranslationRow = DB_fetch_array($TranslationResult);
347						$LeftOvers = $pdf->addTextWrap($Left_Margin+100,$YPos,251,$FontSize,$TranslationRow['descriptiontranslation']);
348					} else {
349						$LeftOvers = $pdf->addTextWrap($Left_Margin+100,$YPos,251,$FontSize,$myrow2['description']);
350					}
351
352					$lines=1;
353					while($LeftOvers!='') {
354						$LeftOvers = $pdf->addTextWrap($Left_Margin+100,$YPos,251,$FontSize,$LeftOvers);
355						$lines++;
356					}
357
358					$LeftOvers = $pdf->addTextWrap($Left_Margin+353,$YPos,96,$FontSize,$DisplayPrice,'right');
359					$LeftOvers = $pdf->addTextWrap($Left_Margin+453,$YPos,95,$FontSize,$DisplayQty,'right');
360					$LeftOvers = $pdf->addTextWrap($Left_Margin+553,$YPos,35,$FontSize,$myrow2['units'],'centre');
361					$LeftOvers = $pdf->addTextWrap($Left_Margin+590,$YPos,50,$FontSize,$DisplayDiscount,'right');
362					$LeftOvers = $pdf->addTextWrap($Left_Margin+642,$YPos,120,$FontSize,$DisplayNet,'right');
363
364					if($myrow2['controlled']==1) {
365
366						$GetControlMovts = DB_query("
367								SELECT
368									moveqty,
369									serialno
370								FROM stockserialmoves
371								WHERE stockmoveno='" . $myrow2['stkmoveno'] . "'");
372
373						if($myrow2['serialised']==1) {
374							while($ControlledMovtRow = DB_fetch_array($GetControlMovts)) {
375								$YPos -= ($line_height);
376								$LeftOvers = $pdf->addTextWrap($Left_Margin+100,$YPos,100,$FontSize,$ControlledMovtRow['serialno'],'left');
377								if($YPos-$line_height <= $Bottom_Margin) {
378									/* head up a new invoice/credit note page */
379									/*draw the vertical column lines right to the bottom */
380									PrintLinesToBottom ();
381									include ('includes/PDFTransPageHeader.inc');
382								} //end if need a new page headed up
383							}
384						} else {
385							while($ControlledMovtRow = DB_fetch_array($GetControlMovts)) {
386								$YPos -= ($line_height);
387								$LeftOvers = $pdf->addTextWrap($Left_Margin+100,$YPos,100,$FontSize,(-$ControlledMovtRow['moveqty']) . ' x ' . $ControlledMovtRow['serialno'], 'left');
388								if($YPos-$line_height <= $Bottom_Margin) {
389									/* head up a new invoice/credit note page */
390									/*draw the vertical column lines right to the bottom */
391									PrintLinesToBottom ();
392									include ('includes/PDFTransPageHeader.inc');
393								} //end if need a new page headed up
394							}
395						}
396					}
397					$YPos -= ($line_height);
398
399					$lines=explode("\r\n",htmlspecialchars_decode($myrow2['narrative']));
400					for ($i=0;$i<sizeOf($lines);$i++) {
401						while (mb_strlen($lines[$i])>1) {
402							if ($YPos-$line_height <= $Bottom_Margin) {
403								/* head up a new invoice/credit note page */
404								/* draw the vertical column lines right to the bottom */
405								PrintLinesToBottom ();
406	   		        				include ('includes/PDFTransPageHeader.inc');
407			   				} //end if need a new page headed up
408
409			   				/* increment a line down for the next line item */
410			   				if (mb_strlen($lines[$i])>1){
411								$lines[$i] = $pdf->addTextWrap($Left_Margin+100,$YPos,245,$FontSize,stripslashes($lines[$i]));
412							}
413							$YPos -= ($line_height);
414						}
415					} //end for loop around lines of narrative to display
416
417
418					if ($YPos <= $Bottom_Margin) {
419						/* head up a new invoice/credit note page */
420						/*draw the vertical column lines right to the bottom */
421						PrintLinesToBottom ();
422						include ('includes/PDFTransPageHeader.inc');
423					} //end if need a new page headed up
424
425				} //end while there are line items to print out
426			} /*end if there are stock movements to show on the invoice or credit note*/
427
428			$YPos -= $line_height;
429
430			/* check to see enough space left to print the 4 lines for the totals/footer */
431			if (($YPos-$Bottom_Margin)<(2*$line_height)) {
432				PrintLinesToBottom ();
433				include ('includes/PDFTransPageHeader.inc');
434			}
435			/* Print a column vertical line  with enough space for the footer */
436			/* draw the vertical column lines to 4 lines shy of the bottom
437			to leave space for invoice footer info ie totals etc */
438			$pdf->line($Left_Margin+97, $TopOfColHeadings+12,$Left_Margin+97,$Bottom_Margin+(4*$line_height));
439
440			/* Print a column vertical line */
441			$pdf->line($Left_Margin+350, $TopOfColHeadings+12,$Left_Margin+350,$Bottom_Margin+(4*$line_height));
442
443			/* Print a column vertical line */
444			$pdf->line($Left_Margin+450, $TopOfColHeadings+12,$Left_Margin+450,$Bottom_Margin+(4*$line_height));
445
446			/* Print a column vertical line */
447			$pdf->line($Left_Margin+550, $TopOfColHeadings+12,$Left_Margin+550,$Bottom_Margin+(4*$line_height));
448
449			/* Print a column vertical line */
450			$pdf->line($Left_Margin+587, $TopOfColHeadings+12,$Left_Margin+587,$Bottom_Margin+(4*$line_height));
451
452			$pdf->line($Left_Margin+640, $TopOfColHeadings+12,$Left_Margin+640,$Bottom_Margin+(4*$line_height));
453
454			/* Rule off at bottom of the vertical lines */
455			$pdf->line($Left_Margin, $Bottom_Margin+(4*$line_height),$Page_Width-$Right_Margin,$Bottom_Margin+(4*$line_height));
456
457			/* Now print out the footer and totals */
458
459			if ($InvOrCredit=='Invoice') {
460
461				$DisplaySubTot = locale_number_format($myrow['ovamount'],$myrow['decimalplaces']);
462				$DisplayFreight = locale_number_format($myrow['ovfreight'],$myrow['decimalplaces']);
463				$DisplayTax = locale_number_format($myrow['ovgst'],$myrow['decimalplaces']);
464				$DisplayTotal = locale_number_format($myrow['ovfreight']+$myrow['ovgst']+$myrow['ovamount'],$myrow['decimalplaces']);
465
466			} else {
467
468				$DisplaySubTot = locale_number_format(-$myrow['ovamount'],$myrow['decimalplaces']);
469				$DisplayFreight = locale_number_format(-$myrow['ovfreight'],$myrow['decimalplaces']);
470				$DisplayTax = locale_number_format(-$myrow['ovgst'],$myrow['decimalplaces']);
471				$DisplayTotal = locale_number_format(-$myrow['ovfreight']-$myrow['ovgst']-$myrow['ovamount'],$myrow['decimalplaces']);
472			}
473
474			$YPos = $Bottom_Margin+(3*$line_height);
475
476			/* Print out the invoice text entered */
477			$FontSize =8;
478			$LeftOvers = $pdf->addTextWrap($Left_Margin+5,$YPos-12,270,$FontSize,$myrow['invtext']);
479			if (mb_strlen($LeftOvers)>0) {
480				$LeftOvers = $pdf->addTextWrap($Left_Margin+5,$YPos-24,270,$FontSize,$LeftOvers);
481				if (mb_strlen($LeftOvers)>0) {
482					$LeftOvers = $pdf->addTextWrap($Left_Margin+5,$YPos-36,270,$FontSize,$LeftOvers);
483					/*If there is some of the InvText leftover after 3 lines 200 wide then it is not printed :( */
484				}
485			}
486			$FontSize = 10;
487
488			$pdf->addText($Page_Width-$Right_Margin-220, $YPos+15,$FontSize, _('Sub Total'));
489			$LeftOvers = $pdf->addTextWrap($Left_Margin+642,$YPos+5,120,$FontSize,$DisplaySubTot, 'right');
490
491			$pdf->addText($Page_Width-$Right_Margin-220, $YPos+2,$FontSize, _('Freight'));
492			$LeftOvers = $pdf->addTextWrap($Left_Margin+642,$YPos-8,120,$FontSize,$DisplayFreight, 'right');
493
494			$pdf->addText($Page_Width-$Right_Margin-220, $YPos-10,$FontSize, _('Tax'));
495			$LeftOvers = $pdf->addTextWrap($Left_Margin+642,$YPos-($line_height)-5,120, $FontSize,$DisplayTax, 'right');
496
497			/*rule off for total */
498			$pdf->line($Page_Width-$Right_Margin-222, $YPos-(2*$line_height),$Page_Width-$Right_Margin,$YPos-(2*$line_height));
499
500			/*vertical to separate totals from comments and ROMALPA */
501			$pdf->line($Page_Width-$Right_Margin-222, $YPos+$line_height,$Page_Width-$Right_Margin-222,$Bottom_Margin);
502
503			$YPos+=10;
504			if ($InvOrCredit=='Invoice') {
505				/* Print out the payment terms */
506				$pdf->addTextWrap($Left_Margin+5,$YPos-5,280,$FontSize,_('Payment Terms') . ': ' . $myrow['terms']);
507
508				$pdf->addText($Page_Width-$Right_Margin-220, $YPos - ($line_height*2)-10,$FontSize, _('TOTAL INVOICE'));
509				$FontSize=9;
510				$YPos-=4;
511
512				$LeftOvers = $pdf->addTextWrap($Left_Margin+280,$YPos,260,$FontSize,$_SESSION['RomalpaClause']);
513				while (mb_strlen($LeftOvers)>0 AND $YPos > $Bottom_Margin) {
514					$YPos-=12;
515					$LeftOvers = $pdf->addTextWrap($Left_Margin+280,$YPos,260,$FontSize,$LeftOvers);
516				}
517
518				/* Add Images for Visa / Mastercard / Paypal */
519				if (file_exists('companies/' . $_SESSION['DatabaseName'] . '/payment.jpg')) {
520					$pdf->addJpegFromFile('companies/' . $_SESSION['DatabaseName'] . '/payment.jpg',$Page_Width/2 -280,$YPos-20,0,40);
521				}
522
523				// Print Bank acount details if available and default for invoices is selected
524				$pdf->addText($Page_Width-$Right_Margin-490, $YPos - ($line_height*3)+32,$FontSize, $DefaultBankAccountCode . ' ' . $DefaultBankAccountNumber);
525				$FontSize=10;
526			} else {
527				$pdf->addText($Page_Width-$Right_Margin-220, $YPos-($line_height*2)-10,$FontSize, _('TOTAL CREDIT'));
528 			}
529			$LeftOvers = $pdf->addTextWrap($Left_Margin+642,35,120, $FontSize,$DisplayTotal, 'right');
530		} /* end of check to see that there was an invoice record to print */
531
532		$FromTransNo++;
533	} /* end loop to print invoices */
534
535	/* Put the transaction number back as would have been incremented by one after last pass */
536	$FromTransNo--;
537
538	if (isset($_GET['Email'])){ //email the invoice to address supplied
539
540
541		include ('includes/htmlMimeMail.php');
542		$FileName = $_SESSION['reports_dir'] . '/' . $_SESSION['DatabaseName'] . '_' . $InvOrCredit . '_' . $FromTransNo . '.pdf';
543		$pdf->Output($FileName,'F');
544		$mail = new htmlMimeMail();
545
546		$Attachment = $mail->getFile($FileName);
547		$mail->setText(_('Please find attached') . ' ' . $InvOrCredit . ' ' . $FromTransNo );
548		$mail->SetSubject($InvOrCredit . ' ' . $FromTransNo);
549		$mail->addAttachment($Attachment, $FileName, 'application/pdf');
550		if($_SESSION['SmtpSetting'] == 0){
551			$mail->setFrom($_SESSION['CompanyRecord']['coyname'] . ' <' . $_SESSION['CompanyRecord']['email'] . '>');
552			$result = $mail->send(array($_GET['Email']));
553		}else{
554			$result = SendmailBySmtp($mail,array($_GET['Email']));
555		}
556
557		unlink($FileName); //delete the temporary file
558
559		$Title = _('Emailing') . ' ' .$InvOrCredit . ' ' . _('Number') . ' ' . $FromTransNo;
560		include('includes/header.php');
561		echo '<p>' . $InvOrCredit . ' '  . _('number') . ' ' . $FromTransNo . ' ' . _('has been emailed to') . ' ' . $_GET['Email'];
562		include('includes/footer.php');
563		exit;
564
565	} else { //its not an email just print the invoice to PDF
566		$pdf->OutputD($_SESSION['DatabaseName'] . '_' . $InvOrCredit . '_' . $FromTransNo . '.pdf');
567
568	}
569	$pdf->__destruct();
570	//Change the language back to the user's language
571	$_SESSION['Language'] = $UserLanguage;
572	include('includes/LanguageSetup.php');
573
574
575} else { /*The option to print PDF was not hit */
576
577	$Title=_('Select Invoices/Credit Notes To Print');
578	include('includes/header.php');
579
580	if (!isset($FromTransNo) OR $FromTransNo=='') {
581
582		/* if FromTransNo is not set then show a form to allow input of either a single invoice number or a range of invoices to be printed. Also get the last invoice number created to show the user where the current range is up to */
583		echo '<form action="' . htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') .  '" method="post">';
584        echo '<div>';
585		echo '<input type="hidden" name="FormID" value="' . $_SESSION['FormID'] . '" />';
586
587		echo '<p class="page_title_text"><img src="'.$RootPath.'/css/'.$Theme.'/images/printer.png" title="' . _('Print') . '" alt="" />' . ' ' . _('Print Invoices or Credit Notes (Landscape Mode)') . '</p>';
588
589		echo '<table class="selection">
590				<tr>
591					<td>' . _('Print Invoices or Credit Notes') . '</td>
592					<td><select name="InvOrCredit">';
593
594		if ($InvOrCredit=='Invoice' OR !isset($InvOrCredit)) {
595
596			echo '<option selected="selected" value="Invoice">' . _('Invoices') . '</option>';
597			echo '<option value="Credit">' . _('Credit Notes') . '</option>';
598		} else {
599			echo '<option selected="selected" value="Credit">' . _('Credit Notes') . '</option>';
600			echo '<option value="Invoice">' . _('Invoices') . '</option>';
601		}
602
603		echo '</select></td>
604				</tr>
605				<tr>
606					<td>', _('Print EDI Transactions'), '</td>
607					<td><select name="PrintEDI">';
608
609		if ($InvOrCredit=='Invoice' OR !isset($InvOrCredit)) {
610
611			echo '<option selected="selected" value="No">' . _('Do not Print PDF EDI Transactions') . '</option>';
612			echo '<option value="Yes">' . _('Print PDF EDI Transactions Too') . '</option>';
613
614		} else {
615
616			echo '<option value="No">' . _('Do not Print PDF EDI Transactions') . '</option>';
617			echo '<option selected="selected" value="Yes">' . _('Print PDF EDI Transactions Too') . '</option>';
618		}
619
620		echo '</select></td>
621				</tr>';
622		echo '<tr>
623				<td>' . _('Start invoice/credit note number to print') . '</td>
624				<td><input class="number" type="text" maxlength="6" size="7" name="FromTransNo" required="required" /></td>
625			</tr>';
626		echo '<tr>
627				<td>' . _('End invoice/credit note number to print') . '</td>
628				<td><input class="number" type="text" maxlength="6" size="7" name="ToTransNo" /></td>
629			</tr>
630			</table>';
631		echo '<div class="centre">
632				<br />
633				<input type="submit" name="Print" value="' . _('Print Preview') . '" />
634				<br />
635				<input type="submit" name="PrintPDF" value="' . _('Print PDF') . '" />
636				</div><br>';
637
638		$sql = "SELECT typeno FROM systypes WHERE typeid=10";
639
640		$result = DB_query($sql);
641		$myrow = DB_fetch_row($result);
642
643		echo '<div class="page_help_text"><b>' . _('The last invoice created was number') . ' ' . $myrow[0] . '</b><br />' . _('If only a single invoice is required') . ', ' . _('enter the invoice number to print in the Start transaction number to print field and leave the End transaction number to print field blank') . '. ' . _('Only use the end invoice to print field if you wish to print a sequential range of invoices') . '';
644
645		$sql = "SELECT typeno FROM systypes WHERE typeid=11";
646
647		$result = DB_query($sql);
648		$myrow = DB_fetch_row($result);
649
650		echo '<br /><b>' . _('The last credit note created was number') . ' ' . $myrow[0] . '</b><br />' . _('A sequential range can be printed using the same method as for invoices above') . '. ' . _('A single credit note can be printed by only entering a start transaction number') . '</div>';
651
652		echo '</div>
653			</form>';
654	} else { // A FromTransNo number IS set
655
656		while($FromTransNo <= filter_number_format($_POST['ToTransNo'])) {
657
658			/*retrieve the invoice details from the database to print
659			notice that salesorder record must be present to print the invoice purging of sales orders will
660			nobble the invoice reprints */
661
662			if ($InvOrCredit=='Invoice') {
663
664				$sql = "SELECT debtortrans.trandate,
665								debtortrans.ovamount,
666								debtortrans.ovdiscount,
667								debtortrans.ovfreight,
668								debtortrans.ovgst,
669								debtortrans.rate,
670								debtortrans.invtext,
671								debtortrans.consignment,
672								debtorsmaster.name,
673								debtorsmaster.address1,
674								debtorsmaster.address2,
675								debtorsmaster.address3,
676								debtorsmaster.address4,
677								debtorsmaster.address5,
678								debtorsmaster.address6,
679								debtorsmaster.currcode,
680								salesorders.deliverto,
681								salesorders.deladd1,
682								salesorders.deladd2,
683								salesorders.deladd3,
684								salesorders.deladd4,
685								salesorders.deladd5,
686								salesorders.deladd6,
687								salesorders.customerref,
688								salesorders.orderno,
689								salesorders.orddate,
690								shippers.shippername,
691								custbranch.brname,
692								custbranch.braddress1,
693								custbranch.braddress2,
694								custbranch.braddress3,
695								custbranch.braddress4,
696								custbranch.braddress5,
697								custbranch.braddress6,
698								custbranch.salesman,
699								salesman.salesmanname,
700								debtortrans.debtorno,
701								currencies.decimalplaces,
702								paymentterms.dayinfollowingmonth,
703								paymentterms.daysbeforedue,
704								paymentterms.terms
705							FROM debtortrans INNER JOIN debtorsmaster
706							ON debtortrans.debtorno=debtorsmaster.debtorno
707							INNER JOIN custbranch
708							ON debtortrans.debtorno=custbranch.debtorno
709							AND debtortrans.branchcode=custbranch.branchcode
710							INNER JOIN salesorders
711							ON debtortrans.order_ = salesorders.orderno
712							INNER JOIN shippers
713							ON debtortrans.shipvia=shippers.shipper_id
714							INNER JOIN salesman
715							ON custbranch.salesman=salesman.salesmancode
716							INNER JOIN locations
717							ON salesorders.fromstkloc=locations.loccode
718							INNER JOIN locationusers
719							ON locationusers.loccode=locations.loccode AND locationusers.userid='" .  $_SESSION['UserID'] . "' AND locationusers.canview=1
720							INNER JOIN paymentterms
721							ON debtorsmaster.paymentterms=paymentterms.termsindicator
722							INNER JOIN currencies
723							ON debtorsmaster.currcode=currencies.currabrev
724							WHERE debtortrans.type=10
725							AND debtortrans.transno='" . $FromTransNo . "'";
726			} else { /* then its a credit note */
727
728				$sql = "SELECT debtortrans.trandate,
729								debtortrans.ovamount,
730								debtortrans.ovdiscount,
731								debtortrans.ovfreight,
732								debtortrans.ovgst,
733								debtortrans.rate,
734								debtortrans.invtext,
735								debtorsmaster.name,
736								debtorsmaster.address1,
737								debtorsmaster.address2,
738								debtorsmaster.address3,
739								debtorsmaster.address4,
740								debtorsmaster.address5,
741								debtorsmaster.address6,
742								debtorsmaster.currcode,
743								custbranch.brname,
744								custbranch.braddress1,
745								custbranch.braddress2,
746								custbranch.braddress3,
747								custbranch.braddress4,
748								custbranch.braddress5,
749								custbranch.braddress6,
750								custbranch.salesman,
751								salesman.salesmanname,
752								debtortrans.debtorno,
753								currencies.decimalplaces
754							FROM debtortrans INNER JOIN debtorsmaster
755							ON debtortrans.debtorno=debtorsmaster.debtorno
756							INNER JOIN custbranch
757							ON debtortrans.debtorno=custbranch.debtorno
758							AND debtortrans.branchcode=custbranch.branchcode
759							INNER JOIN salesman
760							ON custbranch.salesman=salesman.salesmancode
761							INNER JOIN currencies
762							ON debtorsmaster.currcode=currencies.currabrev
763							WHERE debtortrans.type=11
764							AND debtortrans.transno='" . $FromTransNo . "'";
765			}
766
767			$result=DB_query($sql);
768			if(DB_num_rows($result)==0 OR DB_error_no()!=0) {
769				echo '<p>' . _('There was a problem retrieving the invoice or credit note details for note number') . ' ' . $FromTransNo . ' ' . _('from the database') . '. ' . _('To print an invoice, the sales order record, the customer transaction record and the branch record for the customer must not have been purged') . '. ' . _('To print a credit note only requires the customer, transaction, salesman and branch records be available');
770				if ($debug==1) {
771					prnMsg( _('The SQL used to get this information that failed was') . '<br />' . $sql, 'warn');
772				}
773				break;
774				include('includes/footer.php');
775				exit;
776			} elseif (DB_num_rows($result)==1) {
777	/* Then there's an invoice (or credit note) to print. So print out the invoice header and GST Number from the company record */
778
779				$myrow = DB_fetch_array($result);
780
781				if ($_SESSION['SalesmanLogin']!='' AND $_SESSION['SalesmanLogin']!=$myrow['salesman']){
782					prnMsg(_('Your account is set up to see only a specific salespersons orders. You are not authorised to view transaction for this order'),'error');
783					include('includes/footer.php');
784					exit;
785				}
786				if( $CustomerLogin == 1 AND $myrow['debtorno'] != $_SESSION['CustomerID']) {
787					echo '<p class="bad">' . _('This transaction is addressed to another customer and cannot be displayed for privacy reasons') . '. ' . _('Please select only transactions relevant to your company');
788					include('includes/footer.php');
789					exit;
790				}
791
792				$ExchRate = $myrow['rate'];
793				$PageNumber = 1;
794
795				echo '<table class="table1">
796						<tr>
797							<td valign="top" style="width:10%"><img src="' . $_SESSION['LogoFile'] . '" alt="" /></td>
798							<td style="background-color:#bbbbbb">';
799
800				if ($InvOrCredit=='Invoice') {
801					echo '<h2>' . _('TAX INVOICE') . ' ';
802				} else {
803					echo '<h2 style="color:red">' . _('TAX CREDIT NOTE') . ' ';
804				}
805				echo _('Number') . ' ' . $FromTransNo . '</h2>
806						<br />' . _('Tax Authority Ref') . '. ' . $_SESSION['CompanyRecord']['gstno'];
807
808				if ( $InvOrCredit == 'Invoice' ) {
809					/* Print payment terms and due date */
810					$DisplayDueDate = CalcDueDate(ConvertSQLDate($myrow['trandate']), $myrow['dayinfollowingmonth'], $myrow['daysbeforedue']);
811					echo '<br />' . _('Payment Terms') . ': '. $myrow['terms'] . '<br />' . _('Due Date') . ': ' . $DisplayDueDate;
812				}
813
814				echo '</td>
815						</tr>
816						</table>';
817
818	/* Main table with customer name and charge to info. */
819				echo '<table class="table1">
820						<tr>
821							<td><h2>' . $_SESSION['CompanyRecord']['coyname'] . '</h2>
822							<br />';
823                echo $_SESSION['CompanyRecord']['regoffice1'] . '<br />';
824                echo $_SESSION['CompanyRecord']['regoffice2'] . '<br />';
825                echo $_SESSION['CompanyRecord']['regoffice3'] . '<br />';
826                echo $_SESSION['CompanyRecord']['regoffice4'] . '<br />';
827                echo $_SESSION['CompanyRecord']['regoffice5'] . '<br />';
828                echo $_SESSION['CompanyRecord']['regoffice6'] . '<br />';
829                echo _('Telephone') . ': ' . $_SESSION['CompanyRecord']['telephone'] . '<br />';
830                echo _('Facsimile') . ': ' . $_SESSION['CompanyRecord']['fax'] . '<br />';
831                echo _('Email') . ': ' . $_SESSION['CompanyRecord']['email'] . '<br />';
832
833				echo '</td>
834						<td style="width:50%" class="number">';
835
836	/* Put the customer charged to details in a sub table within a cell of the main table*/
837
838				echo '<table class="table1">
839						<tr>
840							<th style="background-color:#bbbbbb">' . _('Charge To') . ':</th>
841						</tr>
842						<tr>
843							<td>';
844				echo $myrow['name'] .
845						'<br />' . $myrow['address1'] .
846						'<br />' . $myrow['address2'] .
847						'<br />' . $myrow['address3'] .
848						'<br />' . $myrow['address4'] .
849						'<br />' . $myrow['address5'] .
850						'<br />' . $myrow['address6'];
851
852				echo '</td>
853						</tr>
854						</table>';
855	/*end of the small table showing charge to account details */
856				echo _('Page') . ': ' . $PageNumber;
857				echo '</td>
858						</tr>
859						</table>';
860	/*end of the main table showing the company name and charge to details */
861
862                if ($InvOrCredit=='Invoice') {
863	/* Table with Charge Branch and Delivered To info. */
864					echo '<table class="table1">
865							<tr>
866								<th style="background-color:#bbbbbb">' . _('Charge Branch') . ':</th>
867								<th style="background-color:#bbbbbb">' . _('Delivered To') . ':</th>
868                    </tr>';
869					echo '<tr>
870							<td>' . $myrow['brname'] .
871                               '<br />' . $myrow['braddress1'] .
872                               '<br />' . $myrow['braddress2'] .
873                               '<br />' . $myrow['braddress3'] .
874                               '<br />' . $myrow['braddress4'] .
875                               '<br />' . $myrow['braddress5'] .
876								'<br />' . $myrow['braddress6'] .
877							'</td>';
878
879					echo '<td>' . $myrow['deliverto'] .
880                            '<br />' . $myrow['deladd1'] .
881                            '<br />' . $myrow['deladd2'] .
882                            '<br />' . $myrow['deladd3'] .
883                            '<br />' . $myrow['deladd4'] .
884                            '<br />' . $myrow['deladd5'] .
885							'<br />' . $myrow['deladd6'] .
886						'</td>';
887					echo '</tr>
888						</table><hr />';
889	/* End Charge Branch and Delivered To table */
890	/* Table with order details */
891					echo '<table class="table1">
892						<tr>
893							<td style="background-color:#bbbbbb"><b>' . _('Your Order Ref') . '</b></td>
894							<td style="background-color:#bbbbbb"><b>' . _('Our Order No') . '</b></td>
895							<td style="background-color:#bbbbbb"><b>' . _('Order Date') . '</b></td>
896							<td style="background-color:#bbbbbb"><b>' . _('Invoice Date') . '</b></td>
897							<td style="background-color:#bbbbbb"><b>' . _('Sales Person') . '</b></td>
898							<td style="background-color:#bbbbbb"><b>' . _('Shipper') . '</b></td>
899							<td style="background-color:#bbbbbb"><b>' . _('Consignment Ref') . '</b></td>
900						</tr>';
901				   	echo '<tr>
902							<td>' . $myrow['customerref'] . '</td>
903							<td>' . $myrow['orderno'] . '</td>
904							<td>' . ConvertSQLDate($myrow['orddate']) . '</td>
905							<td>' . ConvertSQLDate($myrow['trandate']) . '</td>
906							<td>' . $myrow['salesmanname'] . '</td>
907							<td>' . $myrow['shippername'] . '</td>
908							<td>' . $myrow['consignment'] . '</td>
909						</tr>
910					</table>';
911	/* End order details table */
912				   $sql ="SELECT stockmoves.stockid,
913						   		stockmaster.description,
914								-stockmoves.qty as quantity,
915								stockmoves.discountpercent,
916								((1 - stockmoves.discountpercent) * stockmoves.price * " . $ExchRate . "* -stockmoves.qty) AS fxnet,
917								(stockmoves.price * " . $ExchRate . ") AS fxprice,
918								stockmoves.narrative,
919								stockmaster.units,
920								stockmaster.decimalplaces
921							FROM stockmoves
922							INNER JOIN stockmaster
923								ON stockmoves.stockid = stockmaster.stockid
924							WHERE stockmoves.type=10
925							AND stockmoves.transno='" . $FromTransNo . "'
926							AND stockmoves.show_on_inv_crds=1";
927
928				} else { /* then its a credit note */
929	/* Table for Branch info */
930					echo '<table width="50%">
931						<tr>
932							<th style="background-color:#bbbbbb">' . _('Branch') . ':</th>
933						</tr>';
934					echo '<tr>
935							<td style="background-color:#EEEEEE">' . $myrow['brname'] .
936							'<br />' . $myrow['braddress1'] .
937							'<br />' . $myrow['braddress2'] .
938							'<br />' . $myrow['braddress3'] .
939							'<br />' . $myrow['braddress4'] .
940							'<br />' . $myrow['braddress5'] .
941							'<br />' . $myrow['braddress6'] .
942							'</td>
943						</tr></table>';
944	/* End Branch info table */
945	/* Table for Sales Person info. */
946					echo '<hr />
947							<table class="table1">
948							<tr>
949								<th style="background-color:#bbbbbb">' . _('Date') . '</th>
950								<th style="background-color:#bbbbbb">' . _('Sales Person') . '</th>
951					</tr>';
952				   echo '<tr>
953				   		<td>' . ConvertSQLDate($myrow['trandate']) . '</td>
954						<td>' . $myrow['salesmanname'] . '</td>
955						</tr>
956						</table>';
957	/* End Sales Person table */
958				   $sql ="SELECT stockmoves.stockid,
959						   		stockmaster.description,
960								stockmoves.qty as quantity,
961								stockmoves.discountpercent, ((1 - stockmoves.discountpercent) * stockmoves.price * " . $ExchRate . " * stockmoves.qty) AS fxnet,
962								(stockmoves.price * " . $ExchRate . ") AS fxprice,
963								stockmaster.units,
964								stockmoves.narrative,
965								stockmaster.decimalplaces
966							FROM stockmoves
967							INNER JOIN stockmaster
968								ON stockmoves.stockid = stockmaster.stockid
969							WHERE stockmoves.type=11
970							AND stockmoves.transno='" . $FromTransNo . "'
971							AND stockmoves.show_on_inv_crds=1";
972				}
973
974				echo '<hr />';
975				echo '<div class="centre"><h4>' . _('All amounts stated in') . ' ' . $myrow['currcode'] . '</h4></div>';
976
977				$result=DB_query($sql);
978				if (DB_error_no()!=0) {
979					echo '<br />' . _('There was a problem retrieving the invoice or credit note stock movement details for invoice number') . ' ' . $FromTransNo . ' ' . _('from the database');
980					if ($debug==1){
981						 echo '<br />' . _('The SQL used to get this information that failed was') . '<br />' .$sql;
982					}
983					exit;
984				}
985
986				if (DB_num_rows($result)>0){
987	/* Table for stock details */
988					echo '<table class="table1">
989						<tr>
990							<th>' . _('Item Code') . '</th>
991							<th>' . _('Item Description') . '</th>
992							<th>' . _('Quantity') . '</th>
993							<th>' . _('Unit') . '</th>
994							<th>' . _('Price') . '</th>
995							<th>' . _('Discount') . '</th>
996							<th>' . _('Net') . '</th>
997						</tr>';
998
999					$LineCounter =17;
1000
1001					while ($myrow2=DB_fetch_array($result)){
1002
1003					      $DisplayPrice = locale_number_format($myrow2['fxprice'],$myrow['decimalplaces']);
1004					      $DisplayQty = locale_number_format($myrow2['quantity'],$myrow2['decimalplaces']);
1005					      $DisplayNet = locale_number_format($myrow2['fxnet'],$myrow['decimalplaces']);
1006
1007					      if ($myrow2['discountpercent']==0){
1008						   $DisplayDiscount ='';
1009					      } else {
1010						   $DisplayDiscount = locale_number_format($myrow2['discountpercent']*100,2) . '%';
1011					      }
1012
1013						printf ('<tr class="striped_row">
1014									<td>%s</td>
1015									<td>%s</td>
1016									<td class="number">%s</td>
1017									<td class="number">%s</td>
1018									<td class="number">%s</td>
1019									<td class="number">%s</td>
1020									<td class="number">%s</td>
1021									</tr>',
1022									$myrow2['stockid'],
1023									$myrow2['description'],
1024									$DisplayQty,
1025									$myrow2['units'],
1026									$DisplayPrice,
1027									$DisplayDiscount,
1028									$DisplayNet);
1029
1030					      if (mb_strlen($myrow2['narrative'])>1){
1031                                $narrative = str_replace(array("\r\n", "\n", "\r", "\\r\\n"), '<br />', $myrow2['narrative']);
1032							echo '<tr class="striped_row">
1033								<td></td>
1034								<td colspan="6">' . $narrative . '</td>
1035								</tr>';
1036							$LineCounter++;
1037					      }
1038
1039					      $LineCounter++;
1040
1041						if($LineCounter == ($_SESSION['PageLength'] - 2)) {
1042
1043							/* head up a new invoice/credit note page */
1044
1045							$PageNumber++;
1046	/* End the stock table before the new page */
1047							echo '</table>
1048								<table class="table1">
1049								<tr>
1050									<td valign="top"><img src="' . $_SESSION['LogoFile'] . '" alt="" /></td>
1051									<td style="background-color:#bbbbbb">';
1052
1053								if ($InvOrCredit=='Invoice') {
1054									echo '<h2>' . _('TAX INVOICE') . ' ';
1055								} else {
1056									echo '<h2 style="color:red">' . _('TAX CREDIT NOTE') . ' ';
1057								}
1058								echo _('Number') . ' ' . $FromTransNo . '</h2><br />' . _('GST Number') . ' - ' . $_SESSION['CompanyRecord']['gstno'] . '</td>
1059								</tr>
1060								</table>';
1061
1062	/*Print the company name and address */
1063								echo '<table class="table1">
1064										<tr>
1065											<td><h2>' . $_SESSION['CompanyRecord']['coyname'] . '</h2><br />';
1066												echo $_SESSION['CompanyRecord']['regoffice1'] . '<br />';
1067												echo $_SESSION['CompanyRecord']['regoffice2'] . '<br />';
1068												echo $_SESSION['CompanyRecord']['regoffice3'] . '<br />';
1069												echo $_SESSION['CompanyRecord']['regoffice4'] . '<br />';
1070												echo $_SESSION['CompanyRecord']['regoffice5'] . '<br />';
1071												echo $_SESSION['CompanyRecord']['regoffice6'] . '<br />';
1072												echo _('Telephone') . ': ' . $_SESSION['CompanyRecord']['telephone'] . '<br />';
1073												echo _('Facsimile') . ': ' . $_SESSION['CompanyRecord']['fax'] . '<br />';
1074												echo _('Email') . ': ' . $_SESSION['CompanyRecord']['email'] . '<br />';
1075												echo '</td><td class="number">' . _('Page') . ': ' . $PageNumber . '</td>
1076														</tr>
1077													</table>';
1078												echo '<table class="table1">
1079														<tr>
1080															<th>' . _('Item Code') . '</th>
1081															<th>' . _('Item Description') . '</th>
1082															<th>' . _('Quantity') . '</th>
1083															<th>' . _('Unit') . '</th>
1084															<th>' . _('Price') . '</th>
1085															<th>' . _('Discount') . '</th>
1086															<th>' . _('Net') . '</th>
1087														</tr>';
1088
1089												$LineCounter = 10;
1090
1091						} //end if need a new page headed up
1092					} //end while there are line items to print out
1093					echo '</table>';
1094				} /*end if there are stock movements to show on the invoice or credit note*/
1095
1096				/* check to see enough space left to print the totals/footer */
1097				$LinesRequiredForText = floor(mb_strlen($myrow['invtext'])/140);
1098
1099				if($LineCounter >= ($_SESSION['PageLength'] - 8 - $LinesRequiredForText)) {
1100
1101					/* head up a new invoice/credit note page */
1102					$PageNumber++;
1103					echo '<table class="table1">
1104						<tr>
1105							<td valign="top"><img src="' . $_SESSION['LogoFile'] . '" alt="" /></td>
1106							<td style="background-color:#bbbbbb">';
1107
1108				if ($InvOrCredit=='Invoice') {
1109						echo '<h2>' . _('TAX INVOICE') . ' ';
1110					} else {
1111						echo '<h2 style="color:red">' . _('TAX CREDIT NOTE') . ' ';
1112					}
1113					echo _('Number') . ' ' . $FromTransNo . '</h2>
1114							<br />' . _('GST Number') . ' - ' . $_SESSION['CompanyRecord']['gstno'] . '</td>
1115							</tr>
1116							</table>';
1117
1118	/*Print the company name and address */
1119					echo '<table class="table1">
1120							<tr>
1121								<td><h2>' . $_SESSION['CompanyRecord']['coyname'] . '</h2><br />';
1122					echo $_SESSION['CompanyRecord']['regoffice1'] . '<br />';
1123					echo $_SESSION['CompanyRecord']['regoffice2'] . '<br />';
1124					echo $_SESSION['CompanyRecord']['regoffice3'] . '<br />';
1125					echo $_SESSION['CompanyRecord']['regoffice4'] . '<br />';
1126					echo $_SESSION['CompanyRecord']['regoffice5'] . '<br />';
1127					echo $_SESSION['CompanyRecord']['regoffice6'] . '<br />';
1128					echo _('Telephone') . ': ' . $_SESSION['CompanyRecord']['telephone'] . '<br />';
1129					echo _('Facsimile') . ': ' . $_SESSION['CompanyRecord']['fax'] . '<br />';
1130					echo _('Email') . ': ' . $_SESSION['CompanyRecord']['email'] . '<br />';
1131					echo '</td><td class="number">' . _('Page') . ': ' . $PageNumber . '</td>
1132							</tr>
1133							</table>';
1134					echo '<table class="table1">
1135							<tr>
1136								<th>' . _('Item Code') . '</th>
1137								<th>' . _('Item Description') . '</th>
1138								<th>' . _('Quantity') . '</th>
1139								<th>' . _('Unit') . '</th>
1140								<th>' . _('Price') . '</th>
1141								<th>' . _('Discount') . '</th>
1142								<th>' . _('Net') . '</th>
1143							</tr>
1144						</table>';
1145
1146					$LineCounter = 10;
1147				}
1148
1149	/*Print out the invoice text entered */
1150				echo '<br /><br />' . $myrow['invtext'];
1151
1152	/*Space out the footer to the bottom of the page */
1153				$LineCounter=$LineCounter+2+$LinesRequiredForText;
1154				while($LineCounter < ($_SESSION['PageLength'] - 6)) {
1155					echo '<br />';
1156					$LineCounter++;
1157				}
1158
1159	/* Footer table with totals */
1160
1161				if ($InvOrCredit=='Invoice') {
1162				   $DisplaySubTot = locale_number_format($myrow['ovamount'],$myrow['decimalplaces']);
1163				   $DisplayFreight = locale_number_format($myrow['ovfreight'],$myrow['decimalplaces']);
1164				   $DisplayTax = locale_number_format($myrow['ovgst'],$myrow['decimalplaces']);
1165				   $DisplayTotal = locale_number_format($myrow['ovfreight']+$myrow['ovgst']+$myrow['ovamount'],$myrow['decimalplaces']);
1166				} else {
1167				   $DisplaySubTot = locale_number_format(-$myrow['ovamount'],$myrow['decimalplaces']);
1168				   $DisplayFreight = locale_number_format(-$myrow['ovfreight'],$myrow['decimalplaces']);
1169				   $DisplayTax = locale_number_format(-$myrow['ovgst'],$myrow['decimalplaces']);
1170				   $DisplayTotal = locale_number_format(-$myrow['ovfreight']-$myrow['ovgst']-$myrow['ovamount'],$myrow['decimalplaces']);
1171				}
1172
1173				echo '<table class="table1"><tr>
1174						<td class="number">' . _('Sub Total') . '</td>
1175						<td class="number" style="width:15%">' . $DisplaySubTot . '</td></tr>';
1176				echo '<tr><td class="number">' . _('Freight') . '</td>
1177						<td class="number">' . $DisplayFreight . '</td></tr>';
1178				echo '<tr><td class="number">' . _('Tax') . '</td>
1179						<td class="number">' . $DisplayTax . '</td></tr>';
1180				if ($InvOrCredit=='Invoice'){
1181					echo '<tr><td class="number"><b>' . _('TOTAL INVOICE') . '</b></td>
1182							<td class="number"><b>' . $DisplayTotal . '</b></td></tr>';
1183				} else {
1184					echo '<tr><td class="number" style="color:red"><b>' . _('TOTAL CREDIT') . '</b></td>
1185							<td class="number" style="color:red"><b>' . $DisplayTotal . '</b></td></tr>';
1186				}
1187				echo '</table>';
1188	/* End footer totals table */
1189			} /* end of check to see that there was an invoice record to print */
1190			$FromTransNo++;
1191		} /* end loop to print invoices */
1192	} /*end of if FromTransNo exists */
1193	include('includes/footer.php');
1194} /*end of else not PrintPDF */
1195
1196
1197function PrintLinesToBottom () {
1198
1199	global $Bottom_Margin;
1200	global $Left_Margin;
1201	global $line_height;
1202	global $PageNumber;
1203	global $pdf;
1204	global $TopOfColHeadings;
1205
1206	/* draw the vertical column lines right to the bottom */
1207	$pdf->line($Left_Margin+97, $TopOfColHeadings+12,$Left_Margin+97,$Bottom_Margin);
1208
1209	/* Print a column vertical line */
1210	$pdf->line($Left_Margin+350, $TopOfColHeadings+12,$Left_Margin+350,$Bottom_Margin);
1211
1212	/* Print a column vertical line */
1213	$pdf->line($Left_Margin+450, $TopOfColHeadings+12,$Left_Margin+450,$Bottom_Margin);
1214
1215	/* Print a column vertical line */
1216	$pdf->line($Left_Margin+550, $TopOfColHeadings+12,$Left_Margin+550,$Bottom_Margin);
1217
1218	/* Print a column vertical line */
1219	$pdf->line($Left_Margin+587, $TopOfColHeadings+12,$Left_Margin+587,$Bottom_Margin);
1220
1221	$pdf->line($Left_Margin+640, $TopOfColHeadings+12,$Left_Margin+640,$Bottom_Margin);
1222
1223	$PageNumber++;
1224
1225}
1226
1227?>
1228