PageRenderTime 51ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/chronique/StockCheck.php

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