1<?php
2
3include('includes/session.php');
4include('includes/barcodepack/class.code128.php');
5
6$PtsPerMM = 2.83464567; //pdf points per mm (72 dpi / 25.4 mm per inch)
7
8if ((isset($_POST['ShowLabels']) OR isset($_POST['SelectAll']))
9	AND isset($_POST['StockCategory'])
10	AND mb_strlen($_POST['StockCategory'])>=1){
11
12	$Title = _('Print Labels');
13	include('includes/header.php');
14
15	$SQL = "SELECT prices.stockid,
16					stockmaster.description,
17					stockmaster.barcode,
18					prices.price,
19					currencies.decimalplaces
20			FROM stockmaster INNER JOIN	stockcategory
21   			     ON stockmaster.categoryid=stockcategory.categoryid
22			INNER JOIN prices
23				ON stockmaster.stockid=prices.stockid
24			INNER JOIN currencies
25				ON prices.currabrev=currencies.currabrev
26			WHERE stockmaster.categoryid = '" . $_POST['StockCategory'] . "'
27			AND prices.typeabbrev='" . $_POST['SalesType'] . "'
28			AND prices.currabrev='" . $_POST['Currency'] . "'
29			AND prices.startdate<='" . FormatDateForSQL($_POST['EffectiveDate']) . "'
30			AND (prices.enddate='0000-00-00' OR prices.enddate>'" . FormatDateForSQL($_POST['EffectiveDate']) . "')
31			AND prices.debtorno=''
32			ORDER BY prices.currabrev,
33				stockmaster.categoryid,
34				stockmaster.stockid,
35				prices.startdate";
36
37	$LabelsResult = DB_query($SQL,'','',false,false);
38
39	if (DB_error_no() !=0) {
40		prnMsg( _('The Price Labels could not be retrieved by the SQL because'). ' - ' . DB_error_msg(), 'error');
41		echo '<br /><a href="' .$RootPath .'/index.php">' .   _('Back to the menu'). '</a>';
42		if ($debug==1){
43			prnMsg(_('For debugging purposes the SQL used was:') . $SQL,'error');
44		}
45		include('includes/footer.php');
46		exit;
47	}
48	if (DB_num_rows($LabelsResult)==0){
49		prnMsg(_('There were no price labels to print out for the category specified'),'warn');
50		echo '<br /><a href="'.htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') . '">' .  _('Back') . '</a>';
51		include('includes/footer.php');
52		exit;
53	}
54
55
56	echo '<form action="' . htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') . '" method="post">';
57	echo '<input type="hidden" name="FormID" value="' . $_SESSION['FormID'] . '" />';
58	echo '<table class="selection">
59			<tr>
60				<th>' . _('Item Code') . '</th>
61				<th>' . _('Item Description') . '</th>
62				<th>' . _('Price') . '</th>
63				<th>' . _('Print') . ' ?</th>
64			</tr>
65			<tr>
66				<th colspan="4"><input type="submit" name="SelectAll" value="' . _('Select All Labels') . '" /><input type="checkbox" name="CheckAll" ';
67	if (isset($_POST['CheckAll'])){
68		echo 'checked="checked" ';
69	}
70	echo 'onchange="ReloadForm(SelectAll)" /></td>
71		</tr>';
72
73	$i=0;
74	while ($LabelRow = DB_fetch_array($LabelsResult)){
75		echo '<tr>
76				<td>' . $LabelRow['stockid'] . '</td>
77				<td>' . $LabelRow['description'] . '</td>
78				<td class="number">' . locale_number_format($LabelRow['price'],$LabelRow['decimalplaces']) . '</td>
79				<td>';
80		if (isset($_POST['SelectAll']) AND isset($_POST['CheckAll'])) {
81			echo '<input type="checkbox" checked="checked" name="PrintLabel' . $i .'" />';
82		} else {
83			echo '<input type="checkbox" name="PrintLabel' . $i .'" />';
84		}
85		echo '</td>
86			</tr>';
87		echo '<input type="hidden" name="StockID' . $i . '" value="' . $LabelRow['stockid'] . '" />
88			<input type="hidden" name="Description' . $i . '" value="' . $LabelRow['description'] . '" />
89			<input type="hidden" name="Barcode' . $i . '" value="' . $LabelRow['barcode'] . '" />
90			<input type="hidden" name="Price' . $i . '" value="' . locale_number_format($LabelRow['price'],$LabelRow['decimalplaces']) . '" />';
91		$i++;
92	}
93	$i--;
94	echo '</table>
95		<input type="hidden" name="NoOfLabels" value="' . $i . '" />
96		<input type="hidden" name="LabelID" value="' . $_POST['LabelID'] . '" />
97		<input type="hidden" name="StockCategory" value="' . $_POST['StockCategory'] . '" />
98		<input type="hidden" name="SalesType" value="' . $_POST['SalesType'] . '" />
99		<input type="hidden" name="Currency" value="' . $_POST['Currency'] . '" />
100		<input type="hidden" name="EffectiveDate" value="' . $_POST['EffectiveDate'] . '" />
101		<input type="hidden" name="LabelsPerItem" value="' . $_POST['LabelsPerItem'] . '" />
102		<br />
103		<div class="centre">
104
105			<input type="submit" name="PrintLabels" value="'. _('Print Labels'). '" />
106		</div>
107		<br />
108			<div class="centre">
109				<a href="'. $RootPath . '/Labels.php">' . _('Label Template Maintenance'). '</a>
110			</div>
111		</form>';
112	include('includes/footer.php');
113	exit;
114}
115
116$NoOfLabels = 0;
117if (isset($_POST['PrintLabels']) AND isset($_POST['NoOfLabels']) AND $_POST['NoOfLabels']>0){
118
119	for ($i=0;$i < $_POST['NoOfLabels'];$i++){
120		if (isset($_POST['PrintLabel'.$i])){
121			$NoOfLabels++;
122		}
123	}
124	if ($NoOfLabels ==0){
125		prnMsg(_('There are no labels selected to print'),'info');
126	}
127}
128if (isset($_POST['PrintLabels']) AND $NoOfLabels>0) {
129
130	$result = DB_query("SELECT 	description,
131								pagewidth*" . $PtsPerMM . " as page_width,
132								pageheight*" . $PtsPerMM . " as page_height,
133								width*" . $PtsPerMM . " as label_width,
134								height*" . $PtsPerMM . " as label_height,
135								rowheight*" . $PtsPerMM . " as label_rowheight,
136								columnwidth*" . $PtsPerMM . " as label_columnwidth,
137								topmargin*" . $PtsPerMM . " as label_topmargin,
138								leftmargin*" . $PtsPerMM . " as label_leftmargin
139						FROM labels
140						WHERE labelid='" . $_POST['LabelID'] . "'");
141	$LabelDimensions = DB_fetch_array($result);
142
143	$result = DB_query("SELECT fieldvalue,
144								vpos,
145								hpos,
146								fontsize,
147								barcode
148						FROM labelfields
149						WHERE labelid = '" . $_POST['LabelID'] . "'");
150	$LabelFields = array();
151	$i=0;
152	while ($LabelFieldRow = DB_fetch_array($result)){
153		if ($LabelFieldRow['fieldvalue'] == 'itemcode'){
154			$LabelFields[$i]['FieldValue'] = 'stockid';
155		} elseif ($LabelFieldRow['fieldvalue'] == 'itemdescription'){
156			$LabelFields[$i]['FieldValue'] = 'description';
157		} else {
158			$LabelFields[$i]['FieldValue'] = $LabelFieldRow['fieldvalue'];
159		}
160		$LabelFields[$i]['VPos'] = $LabelFieldRow['vpos']*$PtsPerMM;
161		$LabelFields[$i]['HPos'] = $LabelFieldRow['hpos']*$PtsPerMM;
162		$LabelFields[$i]['FontSize'] = $LabelFieldRow['fontsize'];
163		$LabelFields[$i]['Barcode'] = $LabelFieldRow['barcode'];
164		$i++;
165	}
166
167	$PaperSize = 'Custom'; // so PDF starter wont default the DocumentPaper
168	$DocumentPaper = array($LabelDimensions['page_width'],$LabelDimensions['page_height']);
169	include('includes/PDFStarter.php');
170	$Top_Margin = $LabelDimensions['label_topmargin'];
171	$Left_Margin = $LabelDimensions['label_leftmargin'];
172	$Page_Height = $LabelDimensions['page_height'];
173	$Page_Width = $LabelDimensions['page_width'];
174	$Right_Margin =0;
175	$Bottom_Margin =0;
176
177	$pdf->addInfo('Title', $LabelDimensions['description'] . ' ' . _('Price Labels') );
178	$pdf->addInfo('Subject', $LabelDimensions['description'] . ' ' . _('Price Labels') );
179	$pdf->setPrintHeader(false);
180	$pdf->setPrintFooter(false);
181
182
183	$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
184	$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
185	$pdf->setPrintHeader(false);
186	$pdf->setPrintFooter(false);
187
188	$PageNumber=1;
189	//go down first then accross
190	$YPos = $Page_Height - $Top_Margin; //top of current label
191	$XPos = $Left_Margin; // left of current label
192
193	$TotalLabels = $NoOfLabels * $_POST['LabelsPerItem'];
194	$LabelsPrinted = 0;
195	for ($i=0;$i < $_POST['NoOfLabels'];$i++){
196		if (isset($_POST['PrintLabel'.$i])){
197			$NoOfLabels--;
198			for ($LabelNumber=0; $LabelNumber < $_POST['LabelsPerItem'];$LabelNumber++){
199				foreach ($LabelFields as $Field){
200					//print_r($Field);
201					if ($Field['FieldValue']== 'price'){
202						$Value = $_POST['Price' . $i] . ' '. $_POST['Currency'];
203					} elseif ($Field['FieldValue']== 'stockid'){
204						$Value = $_POST['StockID' . $i];
205					} elseif ($Field['FieldValue']== 'description'){
206						$Value = $_POST['Description' . $i];
207					} elseif ($Field['FieldValue']== 'barcode'){
208						$Value = $_POST['Barcode' . $i];
209					}
210					if ($Field['FieldValue'] == 'price'){ //need to format for the number of decimal places
211						$LeftOvers = $pdf->addTextWrap($XPos+$Field['HPos'],$YPos-$LabelDimensions['label_height']+$Field['VPos'],$LabelDimensions['label_width']-$Field['HPos'],$Field['FontSize'],$Value);
212					}elseif ($Field['FieldValue'] == 'logo'){
213						$pdf->addJpegFromFile($_SESSION['LogoFile'],$XPos+$Field['HPos'],$YPos-$LabelDimensions['label_height']+$Field['VPos'],'', $Field['FontSize']);
214
215					}elseif($Field['Barcode']==1) {
216
217						$BarcodeImage = new code128(str_replace('_','',$Value));
218
219						ob_start();
220						imagepng(imagepng($BarcodeImage->draw()));
221						$Image_String = ob_get_contents();
222						ob_end_clean();
223
224						$pdf->addJpegFromFile('@' . $Image_String,$XPos+$Field['HPos'],$YPos-$LabelDimensions['label_height']+$Field['VPos'],'', $Field['FontSize']);
225
226					} else {
227						$LeftOvers = $pdf->addTextWrap($XPos+$Field['HPos'],$YPos-$LabelDimensions['label_height']+$Field['VPos'],$LabelDimensions['label_width']-$Field['HPos'],$Field['FontSize'],$Value);
228					}
229				} // end loop through label fields
230				$LabelsPrinted++;
231				if ($LabelsPrinted < $TotalLabels){ // if there is another label to print
232					//setup $YPos and $XPos for the next label
233					if (($YPos - $LabelDimensions['label_rowheight']) < $LabelDimensions['label_height']){
234						/* not enough space below the above label to print a new label
235						 * so the above was the last label in the column
236						 * need to start either a new column or new page
237						 */
238						if (($Page_Width - $XPos - $LabelDimensions['label_columnwidth']) < $LabelDimensions['label_width']) {
239							/* Not enough space to start a new column so we are into a new page
240							 */
241							$pdf->newPage();
242							$PageNumber++;
243							$YPos = $Page_Height - $Top_Margin; //top of next label
244							$XPos = $Left_Margin; // left of next label
245						} else {
246							/* There is enough space for another column */
247							$YPos = $Page_Height - $Top_Margin; //back to the top of next label column
248							$XPos += $LabelDimensions['label_columnwidth']; // left of next label
249						}
250					} else {
251						/* There is space below to print a label
252						 */
253						$YPos -= $LabelDimensions['label_rowheight']; //Top of next label
254					}
255				}//end if there is another label to print
256			}
257		} //this label is set to print
258	} //loop through labels selected to print
259
260
261	$FileName=$_SESSION['DatabaseName']. '_' . _('Price_Labels') . '_' . date('Y-m-d').'.pdf';
262	ob_clean();
263	$pdf->OutputI($FileName);
264	$pdf->__destruct();
265
266} else { /*The option to print PDF was not hit */
267
268	$Title= _('Price Labels');
269	include('includes/header.php');
270
271	echo '<p class="page_title_text"><img src="' . $RootPath . '/css/' . $Theme . '/images/customer.png" title="' . _('Price Labels') . '" alt="" />
272         ' . ' ' . _('Print Price Labels') . '</p>';
273
274	if (!function_exists('gd_info')) {
275		prnMsg(_('The GD module for PHP is required to print barcode labels. Your PHP installation is not capable currently. You will most likely experience problems with this script until the GD module is enabled.'),'error');
276	}
277
278
279	if (!isset($_POST['StockCategory'])) {
280
281	/*if $StockCategory is not set then show a form to allow input	*/
282
283		echo '<form action="' . htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') . '" method="post">
284				<input type="hidden" name="FormID" value="' . $_SESSION['FormID'] . '" />
285				<table class="selection">
286				<tr>
287					<td>' . _('Label to print') . ':</td>
288					<td><select required="required" autofocus="autofocus" name="LabelID">';
289
290		$LabelResult = DB_query("SELECT labelid, description FROM labels");
291		while ($LabelRow = DB_fetch_array($LabelResult)){
292			echo '<option value="' . $LabelRow['labelid'] . '">' . $LabelRow['description'] . '</option>';
293		}
294		echo '</select></td>
295			</tr>
296			<tr>
297				<td>' .  _('For Stock Category') .':</td>
298				<td><select name="StockCategory">';
299
300		$CatResult= DB_query("SELECT categoryid, categorydescription FROM stockcategory ORDER BY categorydescription");
301		while ($myrow = DB_fetch_array($CatResult)){
302			echo '<option value="' . $myrow['categoryid'] . '">' . $myrow['categorydescription'] . '</option>';
303		}
304		echo '</select></td></tr>';
305
306		echo '<tr><td>' . _('For Sales Type/Price List').':</td>
307                  <td><select name="SalesType">';
308		$sql = "SELECT sales_type, typeabbrev FROM salestypes";
309		$SalesTypesResult=DB_query($sql);
310
311		while ($myrow=DB_fetch_array($SalesTypesResult)){
312			if ($_SESSION['DefaultPriceList']==$myrow['typeabbrev']){
313				echo '<option selected="selected" value="' . $myrow['typeabbrev'] . '">' . $myrow['sales_type'] . '</option>';
314			} else {
315				echo '<option value="' . $myrow['typeabbrev'] . '">' . $myrow['sales_type'] . '</option>';
316			}
317		}
318		echo '</select></td></tr>';
319
320		echo '<tr><td>' . _('For Currency').':</td>
321                  <td><select name="Currency">';
322		$sql = "SELECT currabrev, country, currency FROM currencies";
323		$CurrenciesResult=DB_query($sql);
324
325		while ($myrow=DB_fetch_array($CurrenciesResult)){
326			if ($_SESSION['CompanyRecord']['currencydefault']==$myrow['currabrev']){
327				echo '<option selected="selected" value="' . $myrow['currabrev'] . '">' . $myrow['country'] . ' - ' .$myrow['currency'] . '</option>';
328			} else {
329				echo '<option value="' . $myrow['currabrev'] . '">' . $myrow['country'] . ' - ' .$myrow['currency'] . '</option>';
330			}
331		}
332		echo '</select></td>
333		</tr>
334		<tr>
335			<td>' . _('Effective As At') . ':</td>
336			<td><input type="text" maxlength="10" size="11" class="date" name="EffectiveDate" value="' . Date($_SESSION['DefaultDateFormat']) . '" />';
337        echo '</td></tr>';
338
339		echo'<tr><td>' . _('Number of labels per item') . ':</td>
340			<td><input type="text" class="number" name="LabelsPerItem" size="3" value="1" /></tr>';
341
342		echo '</table>
343				<br />
344				<div class="centre">
345					<input type="submit" name="ShowLabels" value="'. _('Show Labels'). '" />
346				</div>
347				<br />
348				<div class="centre">
349					<a href="'. $RootPath . '/Labels.php">' . _('Label Template Maintenance'). '</a>
350				</div>
351				</form>';
352
353	}
354	include('includes/footer.php');
355
356} /*end of else not PrintPDF */
357
358?>