PageRenderTime 92ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/StockCheck.php

http://kwamoja.codeplex.com
PHP | 366 lines | 302 code | 58 blank | 6 comment | 42 complexity | c3abb48d8eb40e78eb6c5909ad093eaf MD5 | raw file
Possible License(s): LGPL-3.0, AGPL-1.0, GPL-2.0, LGPL-2.1
  1. <?php
  2. /* $Id$*/
  3. include('includes/session.inc');
  4. if (isset($_POST['PrintPDF'])
  5. and isset($_POST['FromCriteria'])
  6. and mb_strlen($_POST['FromCriteria'])>=1
  7. and isset($_POST['ToCriteria'])
  8. and mb_strlen($_POST['ToCriteria'])>=1){
  9. include('includes/PDFStarter.php');
  10. $pdf->addInfo('Title',_('Stock Count Sheets'));
  11. $pdf->addInfo('Subject',_('Stock Count Sheets'));
  12. $FontSize=10;
  13. $PageNumber=1;
  14. $line_height=30;
  15. /*First off do the stock check file stuff */
  16. if ($_POST['MakeStkChkData']=='New'){
  17. $sql = "TRUNCATE TABLE stockcheckfreeze";
  18. $result = DB_query($sql,$db);
  19. $sql = "INSERT INTO stockcheckfreeze (stockid,
  20. loccode,
  21. qoh,
  22. stockcheckdate)
  23. SELECT locstock.stockid,
  24. locstock.loccode,
  25. locstock.quantity,
  26. '" . Date('Y-m-d') . "'
  27. FROM locstock,
  28. stockmaster
  29. WHERE locstock.stockid=stockmaster.stockid AND locstock.loccode='" . $_POST['Location'] . "' AND
  30. stockmaster.categoryid>='" . $_POST['FromCriteria'] . "' AND
  31. stockmaster.categoryid<='" . $_POST['ToCriteria'] . "' AND
  32. stockmaster.mbflag!='A' AND
  33. stockmaster.mbflag!='K' AND
  34. stockmaster.mbflag!='D'";
  35. $result = DB_query($sql, $db,'','',false,false);
  36. if (DB_error_no($db) !=0) {
  37. $Title = _('Stock Count Sheets - Problem Report');
  38. include('includes/header.inc');
  39. prnMsg(_('The inventory quantities could not be added to the freeze file because') . ' ' . DB_error_msg($db),'error');
  40. echo '<br /><a href="' . $RootPath . '/index.php">' . _('Back to the menu') . '</a>';
  41. if ($debug==1){
  42. echo '<br />' . $sql;
  43. }
  44. include('includes/footer.inc');
  45. exit;
  46. }
  47. }
  48. if ($_POST['MakeStkChkData']=='AddUpdate'){
  49. $sql = "DELETE stockcheckfreeze
  50. FROM stockcheckfreeze
  51. INNER JOIN stockmaster ON stockcheckfreeze.stockid=stockmaster.stockid
  52. WHERE stockmaster.categoryid >='" . $_POST['FromCriteria'] . "'
  53. AND stockmaster.categoryid<='" . $_POST['ToCriteria'] . "'
  54. AND stockcheckfreeze.loccode='" . $_POST['Location'] . "'";
  55. $result = DB_query($sql,$db,'','',false,false);
  56. if (DB_error_no($db) !=0) {
  57. $Title = _('Stock Freeze') . ' - ' . _('Problem Report') . '.... ';
  58. include('includes/header.inc');
  59. prnMsg(_('The old quantities could not be deleted from the freeze file because') . ' ' . DB_error_msg($db),'error');
  60. echo '<br /><a href="' . $RootPath . '/index.php">' . _('Back to the menu') . '</a>';
  61. if ($debug==1){
  62. echo '<br />' . $sql;
  63. }
  64. include('includes/footer.inc');
  65. exit;
  66. }
  67. $sql = "INSERT INTO stockcheckfreeze (stockid,
  68. loccode,
  69. qoh,
  70. stockcheckdate)
  71. SELECT locstock.stockid,
  72. loccode ,
  73. locstock.quantity,
  74. '" . Date('Y-m-d') . "'
  75. FROM locstock INNER JOIN stockmaster
  76. ON locstock.stockid=stockmaster.stockid
  77. WHERE locstock.loccode='" . $_POST['Location'] . "'
  78. AND stockmaster.categoryid>='" . $_POST['FromCriteria'] . "'
  79. AND stockmaster.categoryid<='" . $_POST['ToCriteria'] . "'
  80. AND stockmaster.mbflag!='A'
  81. AND stockmaster.mbflag!='K'
  82. AND stockmaster.mbflag!='G'
  83. AND stockmaster.mbflag!='D'";
  84. $result = DB_query($sql, $db,'','',false,false);
  85. if (DB_error_no($db) !=0) {
  86. $Title = _('Stock Freeze - Problem Report');
  87. include('includes/header.inc');
  88. prnMsg(_('The inventory quantities could not be added to the freeze file because') . ' ' . DB_error_msg($db),'error');
  89. echo '<br /><a href="' . $RootPath . '/index.php">' . _('Back to the menu') . '</a>';
  90. if ($debug==1){
  91. echo '<br />' . $sql;
  92. }
  93. include('includes/footer.inc');
  94. exit;
  95. } else {
  96. $Title = _('Stock Check Freeze Update');
  97. include('includes/header.inc');
  98. echo '<p><a href="' . htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') . '">' . _('Print Check Sheets') . '</a>';
  99. prnMsg( _('Added to the stock check file successfully'),'success');
  100. include('includes/footer.inc');
  101. exit;
  102. }
  103. }
  104. $SQL = "SELECT stockmaster.categoryid,
  105. stockcheckfreeze.stockid,
  106. stockmaster.description,
  107. stockmaster.decimalplaces,
  108. stockcategory.categorydescription,
  109. stockcheckfreeze.qoh
  110. FROM stockcheckfreeze INNER JOIN stockmaster
  111. ON stockcheckfreeze.stockid=stockmaster.stockid
  112. INNER JOIN stockcategory
  113. ON stockmaster.categoryid=stockcategory.categoryid
  114. WHERE stockmaster.categoryid >= '" . $_POST['FromCriteria'] . "'
  115. AND stockmaster.categoryid <= '" . $_POST['ToCriteria'] . "'
  116. AND (stockmaster.mbflag='B' OR mbflag='M')
  117. AND stockcheckfreeze.loccode = '" . $_POST['Location'] . "'";
  118. if (isset($_POST['NonZerosOnly']) and $_POST['NonZerosOnly']==true){
  119. $SQL .= " AND stockcheckfreeze.qoh<>0";
  120. }
  121. $SQL .= " ORDER BY stockmaster.categoryid, stockmaster.stockid";
  122. $InventoryResult = DB_query($SQL,$db,'','',false,false);
  123. if (DB_error_no($db) !=0) {
  124. $Title = _('Stock Sheets') . ' - ' . _('Problem Report') . '.... ';
  125. include('includes/header.inc');
  126. prnMsg( _('The inventory quantities could not be retrieved by the SQL because') . ' ' . DB_error_msg($db),'error');
  127. echo '<br /><a href="' . $RootPath . '/index.php">' . _('Back to the menu') . '</a>';
  128. if ($debug==1){
  129. echo '<br />' . $SQL;
  130. }
  131. include ('includes/footer.inc');
  132. exit;
  133. }
  134. if (DB_num_rows($InventoryResult) ==0) {
  135. $Title = _('Stock Count Sheets - Problem Report');
  136. include('includes/header.inc');
  137. prnMsg(_('Before stock count sheets can be printed, a copy of the stock quantities needs to be taken - the stock check freeze. Make a stock check data file first'),'error');
  138. echo '<br /><a href="' . $RootPath . '/index.php">' . _('Back to the menu') . '</a>';
  139. include('includes/footer.inc');
  140. exit;
  141. }
  142. include ('includes/PDFStockCheckPageHeader.inc');
  143. $Category = '';
  144. while ($InventoryCheckRow = DB_fetch_array($InventoryResult,$db)){
  145. if ($Category!=$InventoryCheckRow['categoryid']){
  146. $FontSize=12;
  147. if ($Category!=''){ /*Then it's NOT the first time round */
  148. /*draw a line under the CATEGORY TOTAL*/
  149. $pdf->line($Left_Margin, $YPos-2,$Page_Width-$Right_Margin, $YPos-2);
  150. $YPos -=(2*$line_height);
  151. }
  152. $LeftOvers = $pdf->addTextWrap($Left_Margin,$YPos,260-$Left_Margin,$FontSize,$InventoryCheckRow['categoryid'] . ' - ' . $InventoryCheckRow['categorydescription'], 'left');
  153. $Category = $InventoryCheckRow['categoryid'];
  154. }
  155. $FontSize=10;
  156. $YPos -=$line_height;
  157. if (isset($_POST['ShowInfo']) and $_POST['ShowInfo']==true){
  158. $SQL = "SELECT SUM(salesorderdetails.quantity - salesorderdetails.qtyinvoiced) AS qtydemand
  159. FROM salesorderdetails INNER JOIN salesorders
  160. ON salesorderdetails.orderno=salesorders.orderno
  161. WHERE salesorders.fromstkloc ='" . $_POST['Location'] . "'
  162. AND salesorderdetails.stkcode = '" . $InventoryCheckRow['stockid'] . "'
  163. AND salesorderdetails.completed = 0
  164. AND salesorders.quotation=0";
  165. $DemandResult = DB_query($SQL,$db,'','',false, false);
  166. if (DB_error_no($db) !=0) {
  167. $Title = _('Stock Check Sheets - Problem Report');
  168. include('includes/header.inc');
  169. prnMsg( _('The sales order demand quantities could not be retrieved by the SQL because') . ' ' . DB_error_msg($db), 'error');
  170. echo '<br /><a href="' . $RootPath . '/index.php">' . _('Back to the menu') . '</a>';
  171. if ($debug==1){
  172. echo '<br />' . $SQL;
  173. }
  174. include('includes/footer.inc');
  175. exit;
  176. }
  177. $DemandRow = DB_fetch_array($DemandResult);
  178. $DemandQty = $DemandRow['qtydemand'];
  179. //Also need to add in the demand for components of assembly items
  180. $sql = "SELECT SUM((salesorderdetails.quantity-salesorderdetails.qtyinvoiced)*bom.quantity) AS dem
  181. FROM salesorderdetails INNER JOIN salesorders
  182. ON salesorders.orderno = salesorderdetails.orderno
  183. INNER JOIN bom
  184. ON salesorderdetails.stkcode=bom.parent
  185. INNER JOIN stockmaster
  186. ON stockmaster.stockid=bom.parent
  187. WHERE salesorders.fromstkloc='" . $_POST['Location'] . "'
  188. AND salesorderdetails.quantity-salesorderdetails.qtyinvoiced > 0
  189. AND bom.component='" . $InventoryCheckRow['stockid'] . "'
  190. AND stockmaster.mbflag='A'
  191. AND salesorders.quotation=0";
  192. $DemandResult = DB_query($sql,$db,'','',false,false);
  193. if (DB_error_no($db) !=0) {
  194. prnMsg(_('The demand for this product from') . ' ' . $myrow['loccode'] . ' ' . _('cannot be retrieved because') . ' - ' . DB_error_msg($db),'error');
  195. if ($debug==1){
  196. echo '<br />' . _('The SQL that failed was') . ' ' . $sql;
  197. }
  198. exit;
  199. }
  200. if (DB_num_rows($DemandResult)==1){
  201. $DemandRow = DB_fetch_row($DemandResult);
  202. $DemandQty += $DemandRow[0];
  203. }
  204. $LeftOvers = $pdf->addTextWrap(350,$YPos,60,$FontSize,locale_number_format($InventoryCheckRow['qoh'], $InventoryCheckRow['decimalplaces']), 'right');
  205. $LeftOvers = $pdf->addTextWrap(410,$YPos,60,$FontSize,locale_number_format($DemandQty,$InventoryCheckRow['decimalplaces']), 'right');
  206. $LeftOvers = $pdf->addTextWrap(470,$YPos,60,$FontSize,locale_number_format($InventoryCheckRow['qoh']-$DemandQty,$InventoryCheckRow['decimalplaces']), 'right');
  207. }
  208. $LeftOvers = $pdf->addTextWrap($Left_Margin,$YPos,150,$FontSize,$InventoryCheckRow['stockid'], 'left');
  209. $LeftOvers = $pdf->addTextWrap(150,$YPos,200,$FontSize,$InventoryCheckRow['description'], 'left');
  210. $pdf->line($Left_Margin, $YPos-2,$Page_Width-$Right_Margin, $YPos-2);
  211. if ($YPos < $Bottom_Margin + $line_height){
  212. $PageNumber++;
  213. include('includes/PDFStockCheckPageHeader.inc');
  214. }
  215. } /*end STOCK SHEETS while loop */
  216. $pdf->OutputD($_SESSION['DatabaseName'] . '_Stock_Count_Sheets_' . Date('Y-m-d') .'.pdf');
  217. } else { /*The option to print PDF was not hit */
  218. $Title=_('Stock Check Sheets');
  219. include('includes/header.inc');
  220. if (!isset($_POST['FromCriteria']) and !isset($_POST['ToCriteria'])) {
  221. /*if $FromCriteria is not set then show a form to allow input */
  222. echo '<p class="page_title_text noPrint" ><img src="'.$RootPath.'/css/'.$Theme.'/images/printer.png" title="'
  223. . _('print') . '" alt="" />' . ' ' . $Title.'</p><br />';
  224. echo '<form action="' . htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') . '" method="post" class="noPrint">';
  225. echo '<div>';
  226. echo '<input type="hidden" name="FormID" value="' . $_SESSION['FormID'] . '" />';
  227. echo '<table class="selection">';
  228. echo '<tr><td>' . _('From Inventory Category Code') . ':</td>
  229. <td><select name="FromCriteria">';
  230. $sql="SELECT categoryid, categorydescription FROM stockcategory ORDER BY categorydescription";
  231. $CatResult= DB_query($sql,$db);
  232. while ($myrow = DB_fetch_array($CatResult)){
  233. echo '<option value="' . $myrow['categoryid'] . '">' . $myrow['categorydescription'] . ' - ' . $myrow['categoryid'] . '</option>';
  234. }
  235. echo '</select></td></tr>';
  236. echo '<tr>
  237. <td>' . _('To Inventory Category Code') . ':</td>
  238. <td><select name="ToCriteria">';
  239. /*Set the index for the categories result set back to 0 */
  240. DB_data_seek($CatResult,0);
  241. while ($myrow = DB_fetch_array($CatResult)){
  242. echo '<option value="' . $myrow['categoryid'] . '">' . $myrow['categorydescription'] . ' - ' . $myrow['categoryid'] . '</option>';
  243. }
  244. echo '</select></td></tr>';
  245. echo '<tr>
  246. <td>' . _('For Inventory in Location') . ':</td>
  247. <td><select name="Location">';
  248. $sql = "SELECT loccode, locationname FROM locations ORDER BY locationname";
  249. $LocnResult=DB_query($sql,$db);
  250. while ($myrow=DB_fetch_array($LocnResult)){
  251. echo '<option value="' . $myrow['loccode'] . '">' . $myrow['locationname'] . '</option>';
  252. }
  253. echo '</select>
  254. </td>
  255. </tr>';
  256. echo '<tr>
  257. <td>' . _('Action for Stock Check Freeze') . ':</td>
  258. <td><select name="MakeStkChkData">';
  259. if (!isset($_POST['MakeStkChkData'])){
  260. $_POST['MakeStkChkData'] = 'PrintOnly';
  261. }
  262. if ($_POST['MakeStkChkData'] =='New'){
  263. echo '<option selected="selected" value="New">' . _('Make new stock check data file') . '</option>';
  264. } else {
  265. echo '<option value="New">' . _('Make new stock check data file') . '</option>';
  266. }
  267. if ($_POST['MakeStkChkData'] =='AddUpdate'){
  268. echo '<option selected="selected" value="AddUpdate">' . _('Add/update existing stock check file') . '</option>';
  269. } else {
  270. echo '<option value="AddUpdate">' . _('Add/update existing stock check file') . '</option>';
  271. }
  272. if ($_POST['MakeStkChkData'] =='PrintOnly'){
  273. echo '<option selected="selected" value="PrintOnly">' . _('Print Stock Check Sheets Only') . '</option>';
  274. } else {
  275. echo '<option value="PrintOnly">' . _('Print Stock Check Sheets Only') . '</option>';
  276. }
  277. echo '</select></td></tr>';
  278. echo '<tr>
  279. <td>' . _('Show system quantity on sheets') . ':</td>
  280. <td>';
  281. if (isset($_POST['ShowInfo']) and $_POST['ShowInfo'] == false){
  282. echo '<input type="checkbox" name="ShowInfo" value="false" />';
  283. } else {
  284. echo '<input type="checkbox" name="ShowInfo" value="true" />';
  285. }
  286. echo '</td>
  287. </tr>';
  288. echo '<tr>
  289. <td>' . _('Only print items with non zero quantities') . ':</td>
  290. <td>';
  291. if (isset($_POST['NonZerosOnly']) and $_POST['NonZerosOnly'] == false){
  292. echo '<input type="checkbox" name="NonZerosOnly" value="false" />';
  293. } else {
  294. echo '<input type="checkbox" name="NonZerosOnly" value="true" />';
  295. }
  296. echo '</td>
  297. </tr>
  298. </table>
  299. <br />
  300. <div class="centre">
  301. <input type="submit" name="PrintPDF" value="' . _('Print and Process') . '" />
  302. </div>
  303. </div>
  304. </form>';
  305. }
  306. include('includes/footer.inc');
  307. } /*end of else not PrintPDF */
  308. ?>