PageRenderTime 22ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/chronique/WOSerialNos.php

http://chronique.googlecode.com/
PHP | 342 lines | 321 code | 11 blank | 10 comment | 17 complexity | e0fe4a00d8c19f1649c63f714bea4b55 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception
  1. <?php
  2. /* $Id: WOSerialNos.php 4757 2011-11-27 09:16:35Z daintree $*/
  3. /*This page shows the serial numbers created for a works order
  4. * - creating automatically from the last serial number counter in the stockmaster or by manual entry
  5. * - If the item is lot controlled then the lot reference and the quantity in the lot can be entered
  6. * - this updates the quantity on the work order.
  7. * The work order quantity can only be modified by creating lots or serial numbers for controlled item work orders
  8. *
  9. * Need to allow adding serial numbers/batches and deleting batches/serial numbers
  10. */
  11. include('includes/session.inc');
  12. if ($_REQUEST['Serialised']==1){
  13. $title = _('Work Order Serial Numbers in Progress');
  14. } else {
  15. $title = _('Work Order Batches in Progress');
  16. }
  17. include('includes/header.inc');
  18. if (!isset($_REQUEST['WO']) OR $_REQUEST['WO']==''){
  19. prnMsg(_('This page must to be called from the work order entry screen'),'error');
  20. include('includes/footer.inc');
  21. exit;
  22. }
  23. if (isset($_GET['StockID'])){ //the page was called for the first time - get variables into $_POST array
  24. $StockID = $_GET['StockID'];
  25. $Description = $_GET['Description'];
  26. $WO = $_GET['WO'];
  27. $Serialised = $_GET['Serialised'];
  28. $NextSerialNo = $_GET['NextSerialNo'];
  29. } else {
  30. $StockID = $_POST['StockID'];
  31. $Description = $_POST['Description'];
  32. $WO = $_POST['WO'];
  33. $Serialised = $_POST['Serialised'];
  34. $NextSerialNo = $_POST['NextSerialNo'];
  35. }
  36. echo '<p class="page_title_text">
  37. <img src="'.$rootpath.'/css/'.$theme.'/images/transactions.png" title="" alt="" />' . ' ' . _('For Work Order Number') . ' ' . $WO . ' ' . _('and output item') . ' ' . $StockID . ' - ' . $Description . '
  38. </p>';
  39. $DbgMsg = _('The SQL that failed was');
  40. if (isset($_POST['AddControlledItems'])){
  41. if (isset($_POST['NumberToAdd'])){ // Must be adding serial numbers automatically
  42. if (!is_numeric(filter_number_format($_POST['NumberToAdd']))){
  43. prnMsg(_('The quantity of controlled items to add was not numeric - a number is expected'),'error');
  44. } else {
  45. DB_Txn_Begin($db);
  46. /*Process the additional controlled items into woserialnos and update the quantity on the work order order in woitems*/
  47. $InputError = false;
  48. $sql = "INSERT INTO woserialnos (stockid,
  49. wo,
  50. qualitytext,
  51. serialno)
  52. VALUES ('" . $StockID . "',
  53. '" . $WO . "',
  54. '',
  55. '' ";
  56. for ($i=0;$i< filter_number_format($_POST['NumberToAdd']);$i++){
  57. $NextItemNumber = $NextSerialNo + $i;
  58. $result = DB_query("SELECT serialno FROM woserialnos
  59. WHERE wo='" . $WO . "'
  60. AND stockid='" . $StockID ."'
  61. AND serialno='" . $NextItemNumber . "'",$db);
  62. if (DB_num_rows($result)!=0){
  63. $InputError=true;
  64. prnMsg($NextItemNumber . ' ' . _('is already entered on this work order'),'error');
  65. }
  66. $result = DB_query("SELECT serialno FROM stockserialitems
  67. WHERE serialno='" . $NextItemNumber . "'
  68. AND stockid='" . $StockID ."'",$db);
  69. if (DB_num_rows($result)!=0){
  70. $InputError=true;
  71. prnMsg($NextItemNumber . ' ' . _('has already been used for this item'),'error');
  72. }
  73. if (!$InputError){
  74. if ($i>0){
  75. $sql .= ',';
  76. }
  77. $sql .= $ValueLine . $NextItemNumber . "')";
  78. }
  79. }
  80. $NextSerialNo = $NextItemNumber + 1;
  81. $ErrMsg = _('Unable to add the serial numbers requested');
  82. $result = DB_query($sql,$db,$ErrMsg,$DbgMsg,true);
  83. // update the nextserialno in the stockmaster for the item
  84. $result = DB_query("UPDATE stockmaster
  85. SET nextserialno='" . $NextSerialNo . "'
  86. WHERE stockid='" . $StockID . "'",$db);
  87. $result = DB_query("UPDATE woitems SET qtyreqd=qtyreqd+" . filter_number_format($_POST['NumberToAdd']) . "
  88. WHERE stockid='" . $StockID . "'
  89. AND wo='" . $WO . "'",$db,$ErrMsg,$DbgMsg,true);
  90. DB_Txn_Commit($db);
  91. }
  92. } // end Adding a number of serial numbers automatically
  93. else { //adding just an individual entry
  94. $InputError = false;
  95. if (mb_strlen($_POST['Reference'])==0){
  96. prnMsg(_('The batch or serial number reference has not been entered - a reference is required'),'error');
  97. $InputError=true;
  98. }
  99. if (!is_numeric(filter_number_format($_POST['Quantity']))){
  100. prnMsg(_('The quantity for the batch must be numeric'),'error');
  101. $InputError=true;
  102. }
  103. $result = DB_query("SELECT serialno FROM woserialnos
  104. WHERE wo='" . $WO . "'
  105. AND stockid='" . $StockID ."'
  106. AND serialno='" . $_POST['Reference'] . "'",$db);
  107. if (DB_num_rows($result)!=0){
  108. $InputError=true;
  109. prnMsg(_('The serial number or batch reference must be unique to the item - the reference entered is already entered on this work order'),'error');
  110. }
  111. $result = DB_query("SELECT serialno FROM stockserialitems
  112. WHERE serialno='" . $_POST['Reference'] . "'
  113. AND stockid='" . $StockID ."'",$db);
  114. if (DB_num_rows($result)!=0){
  115. $InputError=true;
  116. prnMsg(_('The serial number or batch reference must be unique to the item. The serial number/batch entered already exists'),'error');
  117. }
  118. if (!$InputError){
  119. DB_Txn_Begin($db);
  120. $ErrMsg = _('Could not add a new serial number/batch');
  121. $result = DB_query("UPDATE woitems
  122. SET qtyreqd=qtyreqd+" . filter_number_format($_POST['Quantity']) . "
  123. WHERE stockid='" . $StockID . "'
  124. AND wo='" . $WO . "'",$db,$ErrMsg,$DbgMsg,true);
  125. $sql = "INSERT INTO woserialnos (stockid,
  126. wo,
  127. qualitytext,
  128. quantity,
  129. serialno)
  130. VALUES ('" . $StockID . "',
  131. '" . $WO . "',
  132. '',
  133. '" . filter_number_format($_POST['Quantity']) . "',
  134. '" . $_POST['Reference'] . "')";
  135. $ErrMsg = _('Unable to add the batch or serial number requested');
  136. $result = DB_query($sql,$db,$ErrMsg,$DbgMsg,true);
  137. DB_Txn_Commit($db);
  138. }
  139. }
  140. }
  141. if (isset($_GET['Delete'])){ //user hit delete link
  142. /*when serial numbers /lots received they are removed from the woserialnos table so no need to check if already received - they will only show here if they are in progress */
  143. $result = DB_query("DELETE FROM woserialnos
  144. WHERE wo='" . $WO . "'
  145. AND stockid='" . $StockID . "'
  146. AND serialno='" . $_GET['Reference'] ."'",
  147. $db);
  148. $result = DB_query("UPDATE woitems SET qtyreqd=qtyreqd-" . filter_number_format($_GET['Quantity']) . "
  149. WHERE wo='" . $WO . "'
  150. AND stockid = '" . $StockID . "'",$db);
  151. prnMsg(_('The batch/serial number') . ' ' . $_GET['Reference'] . ' ' . _('has been deleted from this work order'),'info');
  152. }
  153. if (isset($_POST['UpdateItems'])){
  154. //update the serial numbers and quantities and notes for each serial number or batch
  155. $InputError=false;
  156. $WOQuantityTotal=0;
  157. for ($i=0;$i<$_POST['CountOfItems'];$i++){
  158. if (mb_strlen($_POST['Reference' . $i])==0){
  159. prnMsg($_POST['OldReference' .$i] . ': ' , _('The new batch or serial number reference has not been entered - a reference is required'),'error');
  160. $InputError=true;
  161. }
  162. if (!is_numeric($_POST['Quantity' . $i])){
  163. prnMsg(_('The quantity for the batch must be numeric'),'error');
  164. $InputError=true;
  165. }
  166. if ($_POST['Reference' .$i] != $_POST['OldReference' .$i]){
  167. $result = DB_query("SELECT serialno FROM woserialnos
  168. WHERE wo='" . $WO . "'
  169. AND stockid='" . $StockID ."'
  170. AND serialno='" . $_POST['Reference' . $i] . "'",$db);
  171. if (DB_num_rows($result)!=0){
  172. $InputError=true;
  173. prnMsg($_POST['Reference' .$i] . ': ' . _('The reference entered is already entered on this work order'),'error');
  174. }
  175. $result = DB_query("SELECT serialno FROM stockserialitems
  176. WHERE serialno='" . $_POST['Reference' .$i] . "'
  177. AND stockid='" . $StockID ."'",$db);
  178. if (DB_num_rows($result)!=0){
  179. $InputError=true;
  180. prnMsg($_POST['Reference' .$i] . ': ' . _('The serial number/batch entered already exists'),'error');
  181. }
  182. }
  183. if (!$InputError){
  184. $sql[] = "UPDATE woserialnos SET serialno='" . $_POST['Reference'.$i] . "',
  185. quantity='" . filter_number_format($_POST['Quantity'.$i]) ."',
  186. qualitytext='" . $_POST['Notes'.$i] . "'
  187. WHERE wo='" . $WO . "'
  188. AND stockid='" . $StockID . "'
  189. AND serialno='" . $_POST['OldReference'.$i] . "'";
  190. $WOQuantityTotal += filter_number_format($_POST['Quantity'.$i]);
  191. } else {
  192. $WOQuantityTotal += $_POST['OldQuantity'.$i];
  193. }
  194. }//end loop around all serial numbers/batches
  195. $ErrMsg = _('Could not update serial/batches on the work order');
  196. if (sizeof($sql)>0){
  197. $result = DB_Txn_Begin($db);
  198. foreach ($sql as $SQLStatement){
  199. $result = DB_query($SQLStatement,$db,$ErrMsg,$DbgMsg,true);
  200. }
  201. $result = DB_query("UPDATE woitems SET qtyreqd = '" . $WOQuantityTotal . "'
  202. WHERE wo = '" .$WO . "'
  203. AND stockid='" . $StockID . "'", $db, $ErrMsg,$DbgMsg,true);
  204. $result = DB_Txn_Commit($db);
  205. }
  206. }
  207. echo '<form method="post" action="' . htmlspecialchars($_SERVER['PHP_SELF']) . '" name="form">';
  208. echo '<input type="hidden" name="FormID" value="' . $_SESSION['FormID'] . '" />';
  209. echo '<input type="hidden" name="StockID" value="' . $StockID . '" />';
  210. echo '<input type="hidden" name="Description" value="' . $Description . '" />';
  211. echo '<input type="hidden" name="WO" value="' . $WO . '" />';
  212. echo '<input type="hidden" name="Serialised" value="' . $Serialised . '" />';
  213. echo '<input type="hidden" name="NextSerialNo" value="' . $NextSerialNo . '" />';
  214. echo '<table class="selection">';
  215. if ($Serialised==1 AND $NextSerialNo>0){
  216. echo '<tr><td>' . _('Add A Number of New Serial Numbers');
  217. echo ':</td>
  218. <td><input type="text" name="NumberToAdd" size="10" class="number" maxlength="10" value="1" /></td>
  219. <td>' . _('Starting at') . ':</td><td align="right">' . $NextSerialNo . '</td>';
  220. } else {
  221. //Need to allow entry of batch or serial number and its a batch a quantity too for individual entry
  222. if ($Serialised==1){
  223. echo '<tr>
  224. <th></th>
  225. <th>' . _('Serial No') . '</th></tr>
  226. <tr><td>';
  227. echo _('Add a single serial number');
  228. } else {
  229. echo '<tr>
  230. <th></th>
  231. <th>' . _('Batch/Lot Ref') . '</th><th>' . _('Quantity') . '</th></tr>
  232. <tr><td>';
  233. echo _('Add a single batch/lot number');
  234. }
  235. echo '<td><input type="text" name="Reference" maxlength="30" size="30" /></td>';
  236. if ($Serialised==0){ //also need to add the quantity
  237. echo '<td><input type="text" name="Quantity" size="10" class="number" maxlength="10" value="1" /></td>';
  238. } else { //it will be 1 for a serial item
  239. echo '<input type="hidden" name="Quantity" value="1" />';
  240. }
  241. }
  242. echo '<td><input type="submit" name="AddControlledItems" value="' . _('Add') . '" /></td>
  243. </tr>
  244. </table>
  245. <br />';
  246. $sql = "SELECT serialno,
  247. quantity,
  248. qualitytext
  249. FROM woserialnos
  250. WHERE wo='" . $WO . "'
  251. AND stockid='" . $StockID . "'";
  252. $ErrMsg = _('Could note get the work order serial/batch items');
  253. $WOSerialNoResult = DB_query($sql,$db,$ErrMsg);
  254. if (DB_num_rows($WOSerialNoResult)==0){
  255. prnMsg(_('There are no serial items or batches yet defined for this work order item. Create new items first'),'info');
  256. } else {
  257. echo '<br />
  258. <table class="selection">';
  259. if ($Serialised==1){
  260. $Header = '<tr>
  261. <th>' . _('Serial No') . '</th>
  262. <th>' . _('Notes') . '</th>
  263. </tr>';
  264. } else {
  265. $Header = '<tr>
  266. <th>' . _('Batch Ref') . '</th>
  267. <th>' . _('Quantity') . '</th>
  268. <th>' . _('Notes') . '</th>
  269. </tr>';
  270. }
  271. echo $Header;
  272. $i = 0;
  273. $j = 0;
  274. while ($WOSNRow = DB_fetch_array($WOSerialNoResult)){
  275. if ($j==5){
  276. echo $Header;
  277. $j=0;
  278. }
  279. echo '<tr>
  280. <td><input type="text" name="Reference' . $i .'" value="' . $WOSNRow['serialno'] . '"/></td>';
  281. echo '<input type="hidden" name="OldReference' . $i . '" value="' . $WOSNRow['serialno'] . '"/>';
  282. if ($Serialised==0){
  283. echo '<td><input type="text" name="Quantity' . $i .'" value="' . locale_number_format($WOSNRow['quantity'],'Variable') . '" /></td>';
  284. echo '<input type="hidden" name="OldQuantity' . $i . '" value="' . locale_number_format($WOSNRow['quantity'],'Variable') . '" />';
  285. } else {
  286. echo '<input type="hidden" name="Quantity' . $i . '" value="1" />';
  287. }
  288. echo '<td><textarea name="Notes' . $i .'" cols="60" rows="3">' . $WOSNRow['qualitytext'] .'</textarea></td>';
  289. echo '<td><a href="' . htmlspecialchars($_SERVER['PHP_SELF']) . '?Delete=1&Reference=' . $WOSNRow['serialno'] . '&Quantity=' . locale_number_format($WOSNRow['quantity'],'Variable') . '&WO=' . $WO . '&StockID=' . $StockID . '&Description=' . $Description . '&Serialised=' . $Serialised . '&NextSerialNo=' . $NextSerialNo . '">' . _('Delete') . '</a></td></tr>';
  290. $i++;
  291. $j++;
  292. }
  293. echo '<input type="hidden" name="CountOfItems" value="' . $i . '" />';
  294. if ($Serialised==0){
  295. echo '<tr><td style="text-align: center" colspan="3">';
  296. } else {
  297. echo '<tr><td style="text-align: center" colspan="2">';
  298. }
  299. echo '<input type="submit" name="UpdateItems" value="' . _('Update') . '" /></td></tr>';
  300. echo '</table>';
  301. } //end of if there are woserialno items defined
  302. echo '<p/><a href="' . $rootpath . '/WorkOrderEntry.php?WO=' . $WO . '">' . _('Back To Work Order') . ' ' . $WO .'</a>';
  303. echo '</form>';
  304. include('includes/footer.inc');
  305. ?>