From: <tim...@us...> - 2010-10-25 18:53:53
|
Revision: 4106 http://web-erp.svn.sourceforge.net/web-erp/?rev=4106&view=rev Author: tim_schofield Date: 2010-10-25 18:53:44 +0000 (Mon, 25 Oct 2010) Log Message: ----------- Marcos Garcia Trejo: New labelprinting functionality Modified Paths: -------------- trunk/doc/Change.log.html trunk/includes/PDFStarter.php trunk/index.php Added Paths: ----------- trunk/Labels.php trunk/PDFPrintLabel.php trunk/companies/weberpdemo/reportwriter/labels.xml trunk/css/labelsDim.png trunk/css/paramsLabel.png trunk/includes/DefineLabelClass.php Added: trunk/Labels.php =================================================================== --- trunk/Labels.php (rev 0) +++ trunk/Labels.php 2010-10-25 18:53:44 UTC (rev 4106) @@ -0,0 +1,552 @@ +<?php +/** + * <b>Labels XML file Managment</b> + * This programa maintains the XML file containing the label defintions + * used to print the products prices tickets + * @author Marcos R Garcia <addsmgt at gmail dot com> + * @version 1.1 + * Last changes: 2010-08-31 + * +**/ + +/* $Revision= 1.1; $ */ +$Version_adds= 1.1; + +$PageSecurity=15; +/** Error reporting */ + +include('includes/session.inc'); +$title=_('Label Templates Maintainance'); +include('includes/header.inc'); + +$debug=false; +include('includes/DefineLabelClass.php'); + +$allLabels = //!< The variable $allLabels is the global variable that contains the list + getXMLFile(LABELS_FILE); //!< of all the label objects defined until now. In case of a fresh + //!< installation or an empty XML labels file it holds a NULL value. + +if ($debug) { + echo "<br>" ; + echo "<pre>"; + print_r($_POST); + echo "</pre>"; +//exit(); +} + +/** Check if some action has been requested +*/ +$showList=true; // By default will show the tamplates list + +/** + * Save the changes in template? + */ +if (isset($_POST['Update'])) { + // Get the data from the user input & validate it (not new) + $label=getData($_POST, false, $ok); + // If all OK try to update the requested label + if (!$ok OR !updateLabel($label)) { + // show the data label from the input data as update data (id read only)} + showLabel($label, _('Correct data'), $theme, false); + $showList=false; + } + +/** + * Save the data for a new template? + */ +} elseif (isset($_POST['Save'])) { + // Get the data from the user input & validate it for new id + $label=getData($_POST, true, $ok); + if (!$ok OR !createLabel($label)) { // + showLabel($label, _('Correct data'), $theme, false); + $showList=false; + } + +/** + * Get the data from an old one to create a new template? + */ +} elseif (isset($_POST['Copy'])) { + $label=$allLabels->getLabel($_POST['labelID']); + $label->id = _('New ID'); // Well, where did I get it? of course from the user, but .. + showLabel($label, _('Edit data new label'), $theme, false); + $showList=false; + +/** + * Change some data from an old template? + */ +} elseif (isset($_POST['Edit'])) { + $label=$allLabels->getLabel($_POST['labelID']); + showLabel($label, _('Edit data label'), $theme, true); + $showList=false; + +/** + * Eliminate an unnecesary template? + */ +} elseif (isset($_POST['Delete'])) { + $allLabels=deleteLabel($allLabels, $_POST['labelID']); + +/** + * Create a new template? + */ +} elseif (isset($_POST['New'])) { + showLabel(null, _('New label'), $theme); + $showList=false; + +/** + * Do nothing? only show the list (if it exist)) + */ +} elseif (isset($_POST['Cancel'])) { + ; // showLabelList + +/** + * No action requested, show all or get the first one + */ +} else { + if ($allLabels==null OR count($allLabels->label)<1) { + showLabel(null, _('There is no labels, create a new one'), $theme); + $showList=false; + } +} + +/** + * The default is to show the list of labels templates yet defined, + * the exception occurs when previously has been selected a particular + * template, for edit, copy or to create a new one + */ +if ($showList) { + echo '<p class="page_title_text"><img src="'.$rootpath.'/css/'.$theme.'/images/maintenance.png" title="' . _('Search') . '" alt="">' . ' ' . $title.'</p>'; + showLabelsList($allLabels->label); +} +include('includes/footer.inc'); +exit(); + +/*! \brief getData - Gets the input data from the user's submit + * + * This code constructs a new objet Label from the form's data + * provided by the user. It validates the data too. + * @param $data The array of strings gived by the user + * @param $new This flag indicates that the labels is new, so check no repeated id + * @param $ok The variable where the routine gives the validation result. + * @return The Label object constructed qith the data, included errors. + */ +function getData($data, $new, &$ok) { + $ok = validData($data, $new); + return newLabel($data); +} + +function validData($data, $new) { + global $allLabels, $DimensionTags, $DataTags; + +// Check the heading data + $errors=array(); + if ($new) { + if (empty($data['id'])) + $errors[]=_('Id required'); + elseif ($allLabels!=null AND $allLabels->findLabel($data['id'])!==false) + $errors[]=_('This id exist in previous list'); + } + if (empty($data['description'])) + $errors=_('the description is required'); + +// Check the dimensions data + foreach ($DimensionTags as $iTag=>$tag) { + if ($tag['type']=='s') continue; // select type does not require validation + $dd = trim($data[$iTag]); + $desc=$tag['desc']; + switch ($tag['type']) { + case 'n': + if (!is_numeric($dd)) + $errors[]= _('The value of').' '.$desc.' '._('would be numeric'); + elseif ((float)$data[$iTag]<=0) + $errors[]= _('The value of').' '.$desc.' '._('requires a positive value').$dd; + break; + case 'i': + if (!is_numeric($dd) OR (int)$data[$iTag]<=0) + $errors[]= _('The value of').' '.$tag['desc'].' '._('would be a positive integer'); + break; + } + } + // Checking consistency between data + // Rh > He + $tag = $DimensionTags[$iTag='Rh']; + if ((float)$data['Rh'] < (float)$data['He'] ) { + $desc=$tag['desc']; + $errors[]= _('The value of').' '.$desc.' '._('requires to be greater than the height of the labels'); + } + // Sh >= rows*Rh+Tm + $tag = $DimensionTags[$iTag='Sh']; + if ((float)$data['Sh'] <= (float)$data['Tm'] + ( (int)$data['Rows']*((float)$data['Rh']) ) ) { + $desc=$tag['desc']; + $errors[]= _('The value of').' '.$desc.' '._('requires to be greater than the height of all the rows, including the top margin'); + } + // Cw > Wi + $tag = $DimensionTags[$iTag='Cw']; + if ((float)$data['Cw'] < (float)$data['Wi'] ) { + $desc=$tag['desc']; + $errors[]= _('The value of').' '.$desc.' '._('requires to be greater than the width of the labels'); + } + // Sw >= Cols*Cw+Lm + $tag = $DimensionTags[$iTag='Sw']; + if ((float)$data['Sw'] <= (float)$data['Lm'] + ( (int)$data['Cols']*((float)$data['Cw']) ) ) { + $desc=$tag['desc']; + $errors[]= _('The value of').' '.$desc.' '._('requires to be greater than the width of all the cols, including the left margin'); + } + + $rowCount=0; + $jRow=0; + foreach ($data['row'] as $iRow=>$row) { + $jRow++; + if (empty($row)) continue; // The empty row indicates no data + $rowCount++; // we have data + + if (!is_numeric($row) ) $row=0; + else $row = (float)$row; + if ($row<=0) + $errors[]= _('The vert. pos. value would be positive') ." ($jRow)"; + elseif ((float)$row>(float)$data['He']) + $errors[]= _('The value of the vert. pos. would be less than')." ". $data['He'] ."($jRow)"; + + // now the rest of the line data is validated + foreach ($DataTags as $iTag=>$tag) { + if ($tag['type']=='s' // select type does not require validation + OR $iTag == 'row') continue; // the row is just validated + $dd = trim($data[$iTag][$iRow]); + $desc=$tag['desc']; + if ($tag['type']=='n') { + if (!is_numeric($dd)) + $errors[]= _('The value of').' '.$desc.' '._('would be numeric')." ($jRow)"; + elseif (empty($dd) OR (float)$dd<=0) + $errors[]= _('The value of').' '.$desc.' '._('requires a positive value')." ($jRow)"; + } + switch ($iTag) { + case 'font': + if ((float)$dd+$row >= (float)$data['He']) { + $errors[]= _('The value of').' '.$desc.' '._('in this position exceeds the label height'). + " ($jRow)"; + } + break; + case 'pos': + $posD = (float)$dd; + break; + case 'max': + if ((float)$dd+$posD >= (float)$data['Wi']) { + $errors[]= _('The position and lenght of the string leads the text to get out of the label'). + " ($jRow)"; + } + break; + } + } + } + // Display the errors detected + if (count($errors)>0) { + foreach($errors as $err) + prnMsg($err); + return false; + } + return true; // all data are valid! +} + +/*! \brief Shows the label requested + * + * It shows the data label from the input $data as update data (id read only) + * if the third parameter is true or a fresh data label (new label). It is + * possible that the combination $data valid and $readonly false occurs when + * invalid data needs to be recaptured because an error in a new label capture. + */ +function showLabel($label, $msg, $theme, $readonly=false) { + global $rootpath; + if ($label==null) + $label = newLabel(); + if ($readonly) { + $name = "Update"; + $value = _('Update'); + } else { + $name = "Save"; + $value = _('Save'); + } + + $vCancel = _('Cancel'); + + $tableGD = setTableGD($label, $readonly); + $tableLines = setTableLines($label->data->line); + + echo '<p class="page_title_text"><img src="'.$rootpath.'/css/'.$theme.'/images/maintenance.png" title="' . _('Search') . '" alt="">' . ' ' . $msg.'</p>'; + + echo '<br> + <form action="'.$_SERVER['PHP_SELF'].'" method="POST"> + <input type="hidden" name="FormID" value="' . $_SESSION['FormID'] . '" /> + <div class="centre"> + <table border="2" cellspacing="4" class="selection"> + <tbody> + <tr> + <td align="center"><img src="'.$rootpath.'/css/paramsLabel.png" align="top" border="0"></td> + <td>'.$tableGD.'</td> + </tr> + <tr> + <td align="center"><IMG src="'.$rootpath.'/css/labelsDim.png" align="top" border="0"></td> + <td>'.$tableLines.'</td> + </tr> + </tbody> + </table> + <br /><input type="submit" name="'.$name.'" value="'.$value.'"> + <input type="submit" name="Cancel" value="'.$vCancel.'"> + </form>'; +} + +function setTableGD($label, $readonly) { + global $GlobalTags, $DimensionTags; + $html=' + <table border="0" cellspacing="1" class="selection">'; + $html .= setDataFields($GlobalTags, 0, $label, $specialTag='id', $readonly); + $html .= setDataFields($DimensionTags, 1, $label->dimensions); + $html .= ' + </table>'; + return $html; +} + +function setDataFields($tags, $withTagId, $data, $specialTag=false, $readonly=false) { + $iCol=0; + $html = ''; + foreach ($tags as $iTag=>$tag) { + $vDat = (is_object($data) AND isset($data->$iTag))?$data->$iTag:''; + if ($tag['type']=='s') { + $input ='<select name="'. $iTag . '">'; + foreach ($tag['values'] as $i=>$val) { + $xSel = ($vDat==$i)?' selected' : ''; + $input .= ' + <option value="'. $i .'"'. $xSel .'>'.$val.'</option>'; + } + $input .= ' + </select>'; + } else { + $ro=''; + if ($readonly AND $specialTag==$iTag) + $ro="readonly "; + $input = '<INPUT type="text" name="'. $iTag .'" value="'. $vDat .'" + size="'. $tag['sz'] .'" maxlength="'. $tag['maxsz'] .'"'. $ro .'>'; + } + if (!$iCol++) // when zero begins a line + $html .= ' + <tr>'; + else + $iCol=0; + $wTag= $withTagId ? (' ('.$iTag.')') :''; + $html .= ' + <td align="right">' . $tag['desc'] . $wTag . ':</td> + <td>' . $input . '</td>'; + if (!$iCol) + $html .= ' + </tr>'; + } + if ($iCol) // if the count ends in an odd count, add the end of line + $html .= ' + </tr>'; + + return $html; +} + +function setTableLines($lineArray) { + global $DataTags; + $html=' + <table border="0" cellspacing="1" class="selection">'; + $html .= setTableHeader($DataTags); + + $iCount=MAX_LINES_PER_LABEL; + foreach ($lineArray as $i=>$data) { + + $iCount--; + $html .= setLineFields($DataTags, $data); + } + while ($iCount-->0) + $html .= setLineFields($DataTags, null); + $html .= ' + </table>'; + return $html; +} + +function setTableHeader($tags) { + $html= ' + <tr>'; + foreach ($tags as $tit) + $html .= ' + <th>' . $tit['desc'] . '</th>'; + return $html . ' + </tr>'; +} + +function setLineFields($tags, $data) { + $html = ' + <tr>'; + foreach ($tags as $iTag=>$tag) { + $vDat = ($data!=null AND isset($data->$iTag))?$data->$iTag:''; + if ($tag['type']=='s') { + $input ='<select name="'. $iTag . '[]">'; + foreach ($tag['values'] as $kI=>$kVal) { + $xSel = ($vDat==$kI) ? ' selected':''; + $input .= ' + <option value="'. $kI .'"'. $xSel .'>'.$kVal.'</option>'; + } + $input .= ' + </select>'; + } else { + $input = '<INPUT type="text" name="'. $iTag .'[]" value="'. $vDat .'" + size="'. $tag['sz'] .'" maxlength="'. $tag['maxsz'] .'">'; + } + $html .= ' + <td align="center">' . $input . '</td>'; + } + $html .= ' + </tr>'; + return $html; +} + +/*! \brief Shows the current label list +* +* When the user begin or finishes an maintance action, the program +* shows the current list, in particular, the ID and the description of the label. +* +* @param $list The label object list to be displayed +* @return Nothing +*/ +function showLabelsList($list) { + $txt= //*< + array(_('Label id'),_('Description'), _('Label array'), + _('New'), _('Edit'), _('Copy'), _('Delete'), + _('Do you really want to erase the label') + ); + // The header of the list + echo '<script type="text/javascript"> + function submitForm(formID, action, value) { + document.getElementById(\'action\').name=action; + document.getElementById(\'labelID\').value=value; + document.getElementById(formID).submit(); + } + function areYouSure(form, action, value) { + var r=confirm("'.$txt[7].'"); + if (r) { + submitForm(form, action, value); + } + } + </script> + <form action="'.$_SERVER["PHP_SELF"].'" method="POST" id="form1" target="_self"> + <input type="hidden" name="FormID" value="' . $_SESSION['FormID'] . '" /> + <input type="hidden" name="action" id="action" value=""> + <input type="hidden" name="labelID" id="labelID" value=""> + </form> + <div class="centre"> + <table class=selection> + <thead> + <tr> + <th>'.$txt[0].'</th> + <th>'.$txt[1].'</th> + <th>'.$txt[2].'</th> + <th><input type="submit" name="new" onclick="submitForm(\'form1\',\'New\',\'\');" value="'.$txt[3].'"> + </th> + </tr> + </thead> + <tbody>'; + foreach ($list as $label) { + $dim = (string)$label->dimensions->Rows. " x ". (string)$label->dimensions->Cols; + echo ' + <tr><td>'. $label->id . '</td> + <td>'. $label->description . '</td> + <td><div class="centre">'. $dim . '</div></td> + <td><input type="submit" onclick="submitForm('. "'form1','Edit','". $label->id . "');" .'" value="' . + $txt[4]. '"> + <input type="submit" onclick="submitForm('. "'form1','Copy','". $label->id . "');" .'" value="' . + $txt[5]. '"> + <input type="submit" onclick="areYouSure('. "'form1','Delete','". $label->id . "');" .'" value="' . + $txt[6]. '"> + </td> + </tr>'; + } + echo ' + </tbody> + </table> + </div>'; +} + +/*! \brief Generates a new label +* +* After the user gives the label data, this routine tries to insert +* the new label in the current list. +* +* @param $label The object label that will replace an old one +* @return True when the update was ok +*/ +function createLabel($label) { + global $allLabels; + $new = emptyList(); + $done=false; + if ($allLabels!=null) { + foreach ($allLabels as $oldLabel) { + if (!$done AND (string)$oldLabel->id >= (string)$label->id) { + $new->addLabel($label); + $done=true; + } + $new->addLabel($oldLabel); // inser data in the list, replacing the old one + } + } + if (!$done) + $new->addLabel($label); + $allLabels = $new; + rewrite($allLabels); // rewrite it to the XML file + return true; +} + +/*! \brief Update the label data +* +* After the user modifies the label data this routine tries to update +* the corresponding structure in the label list with the object given. +* If the label with the id of the new one is found, the object could +* be accepted in the list and written to the XML file. +* +* @param $label The object label that will replace an old one +* @return True when the update was ok +*/ +function updateLabel($label) { + global $allLabels; + $new = emptyList(); + foreach ($allLabels as $oldLabel) { + if ((string)$oldLabel->id == (string)$label->id) + $new->addLabel($label); + else + $new->addLabel($oldLabel); // inser data in the list, replacing the old one + } + $allLabels = $new; + rewrite($allLabels); // rewrite it to the XML file + return true; +} + +/*! \brief Label elimination from the list. + * + * This routine eliminates one label from the global list $allLabels. It + * just find the index that correspond to the id given as input data. + * Because this function is called from the selection of a button with + * correct data, it would be unsual that some error exist, but it is + * included the code for the validation of some this strange case. + * + * @param $labelID is the identifier of the label to delete. + * @see $allLabels + * @return true in case of success + */ +function deleteLabel($list, $labelID) { + $new = emptyList(); + foreach ($list as $label) { + if ((string)$label->id!=$labelID) + $new->addLabel($label); + } + rewrite($new); + return $new; +} + +function rewrite($list) { + // First rename the previous XML file + if (is_file(LABELS_FILE) AND !rename(LABELS_FILE, LABELS_FILE.".bak")) + abortMsg(_('Could not rename the previous file.')); + $result=$list->asXML(LABELS_FILE); + if (!$result) + abortMsg(_('Could not create the new XML file.').': '.LABELS_FILE); +} + +?> \ No newline at end of file Added: trunk/PDFPrintLabel.php =================================================================== --- trunk/PDFPrintLabel.php (rev 0) +++ trunk/PDFPrintLabel.php 2010-10-25 18:53:44 UTC (rev 4106) @@ -0,0 +1,498 @@ +<?php +/* $Revision: 1.2 $ */ + +$PageSecurity = 10; + +$Version_adds= "1.2"; + +include('includes/session.inc'); +require_once("includes/DefineLabelClass.php"); + +$msgErr=null; +$decimalplaces=2; +$today = date('Y-m-d'); +$pdf= null; + + $allLabels = //!< The variable $allLabels is the global variable that contains the list + getXMLFile(LABELS_FILE); //!< of all the label objects defined until now. In case of a fresh + //!< installation or an empty XML labels file it holds a NULL value. + +// If there is no label templates, the user could select to set up a new one +if ($allLabels==null) { + echo '<br/><br/>'; + abortMsg( _("There isn't any label template to select for printing. Click") . + ' <a href="Labels.php"><b>' . _('HERE'). '</b></a> '. _('to set up a new one') ); +} + +/** + * The option print was selected, first it is checked if there are enough data + * to print the labels. + */ +if (isset($_POST['PrintPDF']) OR isset($_POST['PDFTest']) ) { + if (!isset($_POST['QtyByItem']) OR (int)$_POST['QtyByItem']<1) + $msgErr = _('You must specify the number of labels per item required'); + else { + if (count($_POST['StockID'])<1) + $msgErr = _('You must select the items to be printed'); + else { + // + $label = $allLabels->getLabel($_POST['labelID']); + list($dimensions, $lines) = resizeLabel($label); + $formatPage = getPageDimensions($dimensions); + list($Page_Width, + $Page_Height, + $Top_Margin, + $Bottom_Margin, + $Left_Margin, + $Right_Margin) = $formatPage; + + // Do it! + $PaperSize = 'User_special'; // Don't use any of the predefined sizes + $DocumentPaper='LETTER'; + $DocumentOrientation='P'; // Correccion para la version trunk :( + include('includes/PDFStarter.php'); + if ($Version>="3.12") + $pdf->setPageFormat($formatPage); + $ok = printLabels( + $dimensions, + $lines, + intval($_POST['QtyByItem']), + $_POST['Currency'], + $_POST['SalesType'], + $_POST['StockID']); + + if ($ok) + exit(); // the print was success + else // Has been ocurred an error + $msgErr = _('There were an error. Consult your IT staff'); + } + } +} + +/** + * There is not activated option print, then show the window for capture the printing + * options. + */ + +$title = _('Print Price Labels'); +include('includes/header.inc'); + +if ($msgErr!=null) + prnMsg($msgErr,'warn'); + +showLabelOptions(); + +include('includes/footer.inc'); +exit(); + +function showLabelOptions() { + global $allLabels, $decimalplaces, $rootpath, $theme; + $txt = array( + _('Label Sticker Printing'), + _('Select label type'), + _('Number of labels per item'), + _('Price list'), _('Currency'), + _('Category'), _('Update values') + ); + if (!isset($_POST['labelID'])) + $_POST['labelID']=(string)$allLabels->label[0]->id; + $optionLabels = selLabels($_POST['labelID']); + if (!isset($_POST['QtyByItem'])) + $_POST['QtyByItem']=1; + if (!isset($_POST['SalesType'])) + $_POST['SalesType']=$_SESSION['DefaultPriceList']; + $optionSales = selSalesType($_POST['SalesType']); + + if (!isset($_POST['Currency'])) + $_POST['Currency']=$_SESSION['CompanyRecord']['currencydefault']; + $decimalplaces=getDecimalPlaces($_POST['Currency']); + + $optionCurrency = selCurrency($_POST['Currency']); + if (!isset($_POST['Category'])) + $_POST['Category']=''; + $optionsCategory = selCategory($_POST['Category']); + + $tableItems = tableItems($_POST['Category'], $okItems); + + $sendButton = '<br /><div class=centre><input type="submit" name="PrintPDF" value="'. _('Print labels') .'"> + <input type="submit" name="PDFTest" value="'. _('Print labels with borders') .'"></div>'; + $iTxt=0; + + echo '<script type="text/javascript"> + function setAll(all) { + var x=document.getElementById("form1"); + for (var i=0;i<x.length;i++) { + if (x.elements[i].id==\'item\'); + x.elements[i].checked=all.checked; + } + } + </script>'; + + echo '<p class="page_title_text"><img src="'.$rootpath.'/css/'.$theme.'/images/maintenance.png" title="' . _('Search') . '" alt="">' . ' ' .$txt[$iTxt++].'</p>'; + echo '<form name ="form1" action="'.$_SERVER['PHP_SELF'].'" method="POST" id="form1">'; + echo '<input type="hidden" name="FormID" value="' . $_SESSION['FormID'] . '" />'; + echo '<table class=selection>'; + echo '<tbody>'; + echo '<tr> + <td class="number">'.$txt[$iTxt++].':</td> + <td><select name="labelID">'. + $optionLabels + .'</select></td> + </tr>'; + echo '<tr> + <td class="number">'.$txt[$iTxt++].':</td> + <td><input type="text" class=number name="QtyByItem" value="'.$_POST['QtyByItem'].'" size="2" + maxlength="4"></td> + </tr>'; + echo '<tr> + <td class="number">'.$txt[$iTxt++].':</td> + <td><select name="SalesType" onChange="ReloadForm(form1.refresh)"> + '.$optionSales.' + </select></td> + </tr>'; + echo '<td class="number">'.$txt[$iTxt++].':</td> + <td><select name="Currency" onChange="ReloadForm(form1.refresh)"> + '.$optionCurrency.' + </select></td> + </tr>'; + echo '<tr> + <td class="number">'.$txt[$iTxt++].':</td> + <td><select name="Category" onChange="ReloadForm(form1.refresh)"> + '.$optionsCategory.' + </select> </td> + </tr>'; + echo '<tr> + <th colspan="2"> + <input type="submit" name="refresh" value="Refresh options"> + </th>'; + echo '<tr> + <td colspan="2"> + '.$tableItems.' + </td> + </tr>'; + + echo '</tbody> + </table> + '.$sendButton.' + </form>'; +} + +function selLabels($type) { + global $allLabels; + $list=array(); + + foreach ($allLabels->label as $label) + $list[(string)$label->id] = (string)$label->description; + + return selectOptions($list, $type); +} + +function selSalesType($type) { + return selectTable("SELECT typeabbrev, sales_type FROM salestypes ORDER BY sales_type", $type); +} + +function selCurrency($curr) { + return selectTable("SELECT currabrev, currency FROM currencies", $curr); +} + +function selCategory(&$categ) { + return selectTable("SELECT categoryid, categorydescription FROM stockcategory ORDER BY categorydescription", $categ); +} + +function selectTable($sql, &$currentKey) { + global $db; + $result = DB_query($sql, $db); + while ($myrow=DB_fetch_row($result)) { + if (empty($currentKey)) + $currentKey=$myrow[0]; + $list[$myrow[0]] = $myrow[1]; + } + DB_free_result($result); + return selectOptions($list, $currentKey); +} + +function selectOptions($list, $currentKey) { + $html=''; + foreach ($list as $key=>$value) { + $xs = ($currentKey==$key) ? " selected":""; + $html .= ' + <option value="'. $key .'"'. $xs .'>'. $value. '</option>'; + } + return $html; +} + +function tableItems($category, &$ok) { + global $db, $decimalplaces; + + if (empty($category)) { + $ok=false; + return noneButton( _('Select a Category') ); + } + $result = getStockItems($category, $_POST['Currency'], $_POST['SalesType']); + if (!DB_num_rows($result)) { + $ok=false; + return noneButton( _('This category has no items to show') ); + } + + $txt=array( + _('Code'), _('Description'), _('Price').'<br>('.$_POST['Currency'].')', + _('All') + ); + $ix=0; + // The table's header + $html= '<table border="0" width="100%"> + <thead> + <tr> + <th>'.$txt[$ix++].'</th> + <th>'.$txt[$ix++].'</th> + <th>'.$txt[$ix++].'</th> + <th colspan="2" align="center">'.$txt[$ix++].' + <input type="checkbox" checked onclick="setAll(this);"> + </th> + </tr> + </thead> + + <tbody>'; + $ok=true; + $odd=true; + while ($myrow=DB_fetch_array($result)) { + $price = number_format($myrow['price'],$decimalplaces); + $oddEven=$odd?"Odd":"Even"; + $odd = !$odd; + $html .= <<<ZZZ + <tr class="{$oddEven}TableRows"> + <td>{$myrow['stockid']}</td> + <td>{$myrow['description']}</td> + <td class="number">{$price}</td> + <td><div class="centre"> + <INPUT type="checkbox" checked name="StockID[{$myrow['stockid']}]" id="item"> + </div> + </td> + <td> </td> + </tr> +ZZZ; + } + return $html . ' + </tbody> + </table> + </div>'; +} + +function noneButton($msg) { + return ' + <div class="centre"> + <INPUT type="button" disabled name="None" value="'. $msg . '"> + </div>'; +} + +/** + * This access to item data includes its price. + * The routine works in two contexts: when only the category is given + * it looks for all the items + */ +function getStockItems($category, $currAbrev, $typeSales, $stockID=false) { + global $db, $today; + if ($stockID!==false) { + $wS= "sm.stockid='$stockID' LIMIT 1"; + } else { + $wS= "sm.categoryid='$category' ORDER BY sm.stockid"; + } + + $sql="SELECT sm.stockid, sm.description, sm.longdescription, sm.barcode, pr.price ". + "FROM stockmaster AS sm LEFT JOIN prices AS pr ON sm.stockid=pr.stockid ". + "AND pr.currabrev = '$currAbrev' " . + "AND pr.typeabbrev= '$typeSales' " . + "AND ('$today' BETWEEN pr.startdate AND pr.enddate) " . + "AND pr.debtorno='' " . + "WHERE $wS"; + + return DB_query($sql, $db); +} + +function getStockData($stockID, $currency, $salesType) { + $result = getStockItems(null, $currency, $salesType, $stockID); + return DB_fetch_array($result); +} + +/** + * Change de scale of data given by points + * Returns two array for the dimension and lines of data + */ +function resizeLabel($label) { + global $DimensionTags, $DataTags; + + //<* Values required from the beggining + $scales=array('pt'=>1, 'in'=>72, 'mm'=>(float)72/25.4, 'cm'=>(float)72/2.54); + + $obj = $label->dimensions; + $unit = (string)$obj->Unit; + if ( array_key_exists($unit , $scales) ) + $factor = $scales[$unit]; + else + abortMsg( _('Unit not defined in scale operation! Correct the template') ); + + $dims = array(); + foreach ($DimensionTags as $iTag=>$tag) { + if ($tag['type']=='n') // it is a data numeric + $dims[$iTag] = round(((float)$obj->$iTag)*$factor, 3); + elseif ($tag['type']=='i') + $dims[$iTag] = (int)$obj->$iTag; + } + + $obj = $label->data; + $line = array(); + $i=0; + foreach ($obj->line as $labelLine) { + $line[$i] = array(); + foreach ($DataTags as $iTag=>$tag) { + if ($tag['type']=='n') // it is a data numeric + $line[$i][$iTag]= round(((float)$labelLine->$iTag)*$factor, 3); + else + $line[$i][$iTag]=(string)$labelLine->$iTag; // field to use in printing data + } + $i++; + } + return array($dims, $line); +} + +/** + * Returns the following data: + * $Page_Width, + * $Page_Height, + * $Top_Margin, + * $Bottom_Margin, + * $Left_Margin, + * $Right_Margin + */ +function getPageDimensions($dimensions) { + $bm =(float)$dimensions['Sh'] - ( (float)$dimensions['Tm'] + + (int)$dimensions['Rows']*(float)$dimensions['He']); + $rm =(float)$dimensions['Sw'] - ( (float)$dimensions['Lm'] + + (int)$dimensions['Cols']*(float)$dimensions['Wi']); + return array( + (float)$dimensions['Sw'], + (float)$dimensions['Sh'], + (float)$dimensions['Tm'], 0, +// ($bm>0?$bm:0), + (float)$dimensions['Lm'], 0 +// ($rm>0?$rm:0) + ); +} + +function printLabels($dimensions, $lines, $qtyByItem, $currency, $salesType, $stockIDList) { + global $pdf, $decimalplaces, $Version; + $row = $col = 0; + + $decimalplaces=getDecimalPlaces($currency); + + foreach ($stockIDList as $stockID=>$on) { // At least there is one item + $itemData = getStockData($stockID, $currency, $salesType); + $num=$qtyByItem; + while ($num-- > 0) { // Print $num labels per item + printStockid($itemData, $dimensions, $lines, $currency, $row, $col); + if (++$col>=$dimensions['Cols']) { + $col=0; + if (++$row>=$dimensions['Rows']) { + $row=0; + $pdf->newpage(); + } + } + } + } + /* if ($row OR $col) // it seems to be unnecesary. + $pdf->newpage(); */ + + // now, emit the PDF file (if not errors!) + if ($Version>="3.12") { + $pdf->OutputD($_SESSION['DatabaseName'] . '_Labels_' . date('Y-m-d') . '.pdf');//UldisN + $pdf->__destruct(); //UldisN + } else { + $pdfcode = $pdf->output(); + $len = strlen($pdfcode); + + if ($len<=20){ + return false; + } else{ + header('Content-type: application/pdf'); + header('Content-Length: ' . $len); + header('Content-Disposition: inline; filename=Labels.pdf'); + header('Expires: 0'); + header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); + header('Pragma: public'); + + $pdf->Stream(); + } + } + return true; // All fine!! +} + +/*! \brief The heart of the program (perhaps the liver) + * + * It shows the data label from the input $data as update data (id read only) + * if the third parameter is true or a fresh data label (new label). It is + * possible that the combination $data valid and $readonly false occurs when + * invalid data needs to be recaptured because an error in a new label capture. + */ +function printStockid($itemData, $labelDim, $dataParams, $currency, $row, $col) { + global $pdf, $decimalplaces; +//echo $row.':'.$col.'<br>'; + // Calculate the bottom left corner position + $iX = $labelDim['Lm'] + $col * $labelDim['Cw']; + $iY = $labelDim['Sh'] - ($labelDim['Tm'] + ($row+1) * $labelDim['Rh']); + + if (isset($_POST['PDFTest'])) { + $pdf->line($iX, $iY+$labelDim['He'], $iX+$labelDim['Wi'], $iY+$labelDim['He']); // top + $pdf->line($iX, $iY, $iX+$labelDim['Wi'], $iY); // bottom + $pdf->line($iX, $iY, $iX, $iY+$labelDim['He']); // left + $pdf->line($iX+$labelDim['Wi'], $iY, $iX+$labelDim['Wi'], $iY+$labelDim['He']); + } + // Now, for every data, write down the correspondig text + $descrip= $ldescrip=''; + foreach ($dataParams as $line) { + unset($resid); // unlink the previous residue + unset($txt); + $adj='left'; + switch ($line['dat']) { + case 'code': + $txt = $itemData['stockid']; + break; + case 'name1': + $txt = $itemData['description']; + $resid = &$descrip; + break; + case 'name2': + $txt = $descrip; + unset($descrip); + break; + case 'lname1': + $txt = $itemData['longdescription']; + $resid = &$ldescrip; + break; + case 'lname2': + $txt = $ldescrip; + unset($ldescrip); + break; + case 'price': + $txt = number_format($itemData['price'], $decimalplaces). ' '. $currency; +// $adj='left'; + break; + case 'bcode': break; + } + $ix = $iX + $line['pos']; + $iy = $iY + $line['row']; + if (isset($txt)) { + $resid = $pdf->addTextWrap($ix,$iy,$line['max'],$line['font'],$txt, $adj); + } + } +} + +function getDecimalPlaces($currency) { + global $db; + $sql="SELECT decimalplaces FROM currencies WHERE currabrev='$currency'"; + $result = DB_query($sql, $db); + if (!DB_num_rows($result)) + abortMsg(_('Couldnt get the currency data')); + $myrow=DB_fetch_row($result); + return $myrow[0]; +} +?> Added: trunk/companies/weberpdemo/reportwriter/labels.xml =================================================================== --- trunk/companies/weberpdemo/reportwriter/labels.xml (rev 0) +++ trunk/companies/weberpdemo/reportwriter/labels.xml 2010-10-25 18:53:44 UTC (rev 4106) @@ -0,0 +1,2 @@ +<?xml version="1.0"?> +<labels><label><id>111-price</id><description>111-CodePrice</description><dimensions><Unit>mm</Unit><Rows>12</Rows><Cols>10</Cols><Sh>162</Sh><Sw>196</Sw><He>12</He><Wi>18</Wi><Tm>3</Tm><Lm>3</Lm><Rh>12.5</Rh><Cw>18.5</Cw></dimensions><data><line><row>7</row><pos>1</pos><max>15</max><font>2</font><dat>code</dat></line><line><row>2</row><pos>1</pos><max>15</max><font>2</font><dat>price</dat></line></data></label><label><id>130-price</id><description>130-CodePrice</description><dimensions><Unit>mm</Unit><Rows>8</Rows><Cols>6</Cols><Sh>170</Sh><Sw>205</Sw><He>18</He><Wi>31</Wi><Tm>3</Tm><Lm>2</Lm><Rh>20.5</Rh><Cw>33.5</Cw></dimensions><data><line><row>13</row><pos>2</pos><max>27</max><font>4</font><dat>code</dat></line><line><row>5</row><pos>2</pos><max>27</max><font>4</font><dat>price</dat></line></data></label></labels> Added: trunk/css/labelsDim.png =================================================================== --- trunk/css/labelsDim.png (rev 0) +++ trunk/css/labelsDim.png 2010-10-25 18:53:44 UTC (rev 4106) @@ -0,0 +1,108 @@ +\x89PNG + + |