PageRenderTime 35ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/chronique/Z_ImportStocks.php

http://chronique.googlecode.com/
PHP | 306 lines | 274 code | 16 blank | 16 comment | 18 complexity | 9a8190fd40d964a01b7b3973336679f8 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: Z_ImportStocks.php 4628 2011-07-08 10:43:09Z daintree $*/
  3. include('includes/session.inc');
  4. $title = _('Import Items');
  5. include('includes/header.inc');
  6. // If this script is called with a file object, then the file contents are imported
  7. // If this script is called with the gettemplate flag, then a template file is served
  8. // Otherwise, a file upload form is displayed
  9. $headers = array(
  10. 'StockID', // 0 'STOCKID',
  11. 'Description', // 1 'DESCRIPTION',
  12. 'LongDescription', // 2 'LONGDESCRIPTION',
  13. 'CategoryID', // 3 'CATEGORYID',
  14. 'Units', // 4 'UNITS',
  15. 'MBFlag', // 5 'MBFLAG',
  16. 'EOQ', // 6 'EOQ',
  17. 'Discontinued', // 7 'DISCONTINUED',
  18. 'Controlled', // 8 'CONTROLLED',
  19. 'Serialised', // 9 'SERIALISED',
  20. 'Perishable', // 10 'PERISHABLE',
  21. 'Volume', // 11 'VOLUME',
  22. 'KGS', // 12 'KGS',
  23. 'BarCode', // 13 'BARCODE',
  24. 'DiscountCategory', // 14 'DISCOUNTCATEGORY',
  25. 'TaxCat', // 15 'TAXCAT',
  26. 'DecimalPlaces', // 16 'DECIMALPLACES',
  27. 'ItemPDF' // 17 'ITEMPDF'
  28. );
  29. if ($_FILES['userfile']['name']) { //start file processing
  30. //initialize
  31. $allowType='text/csv';
  32. $fieldTarget = 18;
  33. $InputError = 0;
  34. //check file info
  35. $fileName = $_FILES['userfile']['name'];
  36. $tmpName = $_FILES['userfile']['tmp_name'];
  37. $fileSize = $_FILES['userfile']['size'];
  38. $fileType = $_FILES['userfile']['type'];
  39. if ($fileType != $allowType) {
  40. prnMsg (_('File has type '. $fileType. ', but only '. $allowType. ' is allowed.'),'error');
  41. include('includes/footer.inc');
  42. exit;
  43. }
  44. //get file handle
  45. $handle = fopen($tmpName, 'r');
  46. //get the header row
  47. $headRow = fgetcsv($handle, 10000, ",");
  48. //check for correct number of fields
  49. if ( count($headRow) != count($headers) ) {
  50. prnMsg (_('File contains '. count($headRow). ' columns, expected '. count($headers). '. Try downloading a new template.'),'error');
  51. fclose($handle);
  52. include('includes/footer.inc');
  53. exit;
  54. }
  55. //test header row field name and sequence
  56. $head = 0;
  57. foreach ($headRow as $headField) {
  58. if ( mb_strtoupper($headField) != mb_strtoupper($headers[$head]) ) {
  59. prnMsg (_('File contains incorrect headers ('. mb_strtoupper($headField). ' != '. mb_strtoupper($header[$head]). '. Try downloading a new template.'),'error');
  60. fclose($handle);
  61. include('includes/footer.inc');
  62. exit;
  63. }
  64. $head++;
  65. }
  66. //start database transaction
  67. DB_Txn_Begin($db);
  68. //loop through file rows
  69. $row = 1;
  70. while ( ($myrow = fgetcsv($handle, 10000, ",")) !== FALSE ) {
  71. //check for correct number of fields
  72. $fieldCount = count($myrow);
  73. if ($fieldCount != $fieldTarget){
  74. prnMsg (_($fieldTarget. ' fields required, '. $fieldCount. ' fields received'),'error');
  75. fclose($handle);
  76. include('includes/footer.inc');
  77. exit;
  78. }
  79. // cleanup the data (csv files often import with empty strings and such)
  80. $StockID = mb_strtoupper($myrow[0]);
  81. foreach ($myrow as &$value) {
  82. $value = trim($value);
  83. }
  84. //first off check if the item already exists
  85. $sql = "SELECT COUNT(stockid) FROM stockmaster WHERE stockid='".$StockID."'";
  86. $result = DB_query($sql,$db);
  87. $testrow = DB_fetch_row($result);
  88. if ($testrow[0] != 0) {
  89. $InputError = 1;
  90. prnMsg (_('Stock item "'. $StockID. '" already exists'),'error');
  91. }
  92. //next validate inputs are sensible
  93. if (!$myrow[1] or mb_strlen($myrow[1]) > 50 OR mb_strlen($myrow[1])==0) {
  94. $InputError = 1;
  95. prnMsg (_('The stock item description must be entered and be fifty characters or less long') . '. ' . _('It cannot be a zero length string either') . ' - ' . _('a description is required'). ' ("'. implode('","',$myrow). $stockid. '") ','error');
  96. }
  97. if (mb_strlen($myrow[2])==0) {
  98. $InputError = 1;
  99. prnMsg (_('The stock item description cannot be a zero length string') . ' - ' . _('a long description is required'),'error');
  100. }
  101. if (mb_strlen($StockID) ==0) {
  102. $InputError = 1;
  103. prnMsg (_('The Stock Item code cannot be empty'),'error');
  104. }
  105. if (ContainsIllegalCharacters($StockID) OR mb_strstr($StockID,' ')) {
  106. $InputError = 1;
  107. prnMsg(_('The stock item code cannot contain any of the following characters') . " ' & + \" \\ " . _('or a space'). " (". $StockID. ")",'error');
  108. $StockID='';
  109. }
  110. if (mb_strlen($myrow[4]) >20) {
  111. $InputError = 1;
  112. prnMsg(_('The unit of measure must be 20 characters or less long'),'error');
  113. }
  114. if (mb_strlen($myrow[13]) >20) {
  115. $InputError = 1;
  116. prnMsg(_('The barcode must be 20 characters or less long'),'error');
  117. }
  118. if ($myrow[10]!=0 AND $myrow[10]!=1) {
  119. $InputError = 1;
  120. prnMsg (_('Values in the Perishable field must be either 0 (No) or 1 (Yes)') ,'error');
  121. }
  122. if (!is_numeric($myrow[11])) {
  123. $InputError = 1;
  124. prnMsg (_('The volume of the packaged item in cubic metres must be numeric') ,'error');
  125. }
  126. if ($myrow[11] <0) {
  127. $InputError = 1;
  128. prnMsg(_('The volume of the packaged item must be a positive number'),'error');
  129. }
  130. if (!is_numeric($myrow[12])) {
  131. $InputError = 1;
  132. prnMsg(_('The weight of the packaged item in KGs must be numeric'),'error');
  133. }
  134. if ($myrow[12]<0) {
  135. $InputError = 1;
  136. prnMsg(_('The weight of the packaged item must be a positive number'),'error');
  137. }
  138. if (!is_numeric($myrow[6])) {
  139. $InputError = 1;
  140. prnMsg(_('The economic order quantity must be numeric'),'error');
  141. }
  142. if ($$myrow[6] <0) {
  143. $InputError = 1;
  144. prnMsg (_('The economic order quantity must be a positive number'),'error');
  145. }
  146. if ($myrow[8]==0 AND $myrow[9]==1){
  147. $InputError = 1;
  148. prnMsg(_('The item can only be serialised if there is lot control enabled already') . '. ' . _('Batch control') . ' - ' . _('with any number of items in a lot/bundle/roll is enabled when controlled is enabled') . '. ' . _('Serialised control requires that only one item is in the batch') . '. ' . _('For serialised control') . ', ' . _('both controlled and serialised must be enabled'),'error');
  149. }
  150. $mbflag = $myrow[5];
  151. if ($mbflag!='M' and $mbflag!='K' and $mbflag!='A' and $mbflag!='B' and $mbflag!='D' and $mbflag!='G') {
  152. $InputError = 1;
  153. prnMsg(_('Items must be of MBFlag type Manufactured(M), Assembly(A), Kit-Set(K), Purchased(B), Dummy(D) or Phantom(G)'),'error');
  154. }
  155. if (($mbflag=='A' OR $mbflag=='K' OR $mbflag=='D' OR $mbflag=='G') AND $myrow[8]==1){
  156. $InputError = 1;
  157. prnMsg(_('Assembly/Kitset/Phantom/Service items cannot also be controlled items') . '. ' . _('Assemblies, Dummies and Kitsets are not physical items and batch/serial control is therefore not appropriate'),'error');
  158. }
  159. if ($myrow[3]==''){
  160. $InputError = 1;
  161. prnMsg(_('There are no inventory categories defined. All inventory items must belong to a valid inventory category,'),'error');
  162. }
  163. if ($myrow[17]==''){
  164. $InputError = 1;
  165. prnMsg(_('ItemPDF must contain either a filename, or the keyword `none`'),'error');
  166. }
  167. if ($InputError !=1){
  168. if ($myrow[9]==1){ /*Not appropriate to have several dp on serial items */
  169. $myrow[16]=0;
  170. }
  171. //attempt to insert the stock item
  172. $sql = "
  173. INSERT INTO stockmaster (
  174. stockid,
  175. description,
  176. longdescription,
  177. categoryid,
  178. units,
  179. mbflag,
  180. eoq,
  181. discontinued,
  182. controlled,
  183. serialised,
  184. perishable,
  185. volume,
  186. kgs,
  187. barcode,
  188. discountcategory,
  189. taxcatid,
  190. decimalplaces,
  191. appendfile)
  192. VALUES (
  193. '$StockID',
  194. '" . $myrow[1] . "',
  195. '" . $myrow[2] . "',
  196. '" . $myrow[3] . "',
  197. '" . $myrow[4] . "',
  198. '" . $myrow[5] . "',
  199. " . $myrow[6] . ",
  200. " . $myrow[7] . ",
  201. " . $myrow[8] . ",
  202. " . $myrow[9] . ",
  203. " . $myrow[10] . ",
  204. " . $myrow[11] . ",
  205. " . $myrow[12] . ",
  206. '" . $myrow[13] . "',
  207. '" . $myrow[14] . "',
  208. " . $myrow[15] . ",
  209. " . $myrow[16] . ",
  210. '" . $myrow[17] . "'
  211. );
  212. ";
  213. $ErrMsg = _('The item could not be added because');
  214. $DbgMsg = _('The SQL that was used to add the item failed was');
  215. $result = DB_query($sql,$db, $ErrMsg, $DbgMsg);
  216. if (DB_error_no($db) ==0) { //the insert of the new code worked so bang in the stock location records too
  217. $sql = "INSERT INTO locstock (loccode,
  218. stockid)
  219. SELECT locations.loccode,
  220. '" . $StockID . "'
  221. FROM locations";
  222. $ErrMsg = _('The locations for the item') . ' ' . $StockID . ' ' . _('could not be added because');
  223. $DbgMsg = _('NB Locations records can be added by opening the utility page') . ' <i>Z_MakeStockLocns.php</i> ' . _('The SQL that was used to add the location records that failed was');
  224. $InsResult = DB_query($sql,$db,$ErrMsg,$DbgMsg);
  225. if (DB_error_no($db) ==0) {
  226. prnMsg( _('New Item') .' ' . $StockID . ' '. _('has been added to the transaction'),'info');
  227. } else { //location insert failed so set some useful error info
  228. $InputError = 1;
  229. prnMsg(_($InsResult),'error');
  230. }
  231. } else { //item insert failed so set some useful error info
  232. $InputError = 1;
  233. prnMsg(_($InsResult),'error');
  234. }
  235. }
  236. if ($InputError == 1) { //this row failed so exit loop
  237. break;
  238. }
  239. $row++;
  240. }
  241. if ($InputError == 1) { //exited loop with errors so rollback
  242. prnMsg(_('Failed on row '. $row. '. Batch import has been rolled back.'),'error');
  243. DB_Txn_Rollback($db);
  244. } else { //all good so commit data transaction
  245. DB_Txn_Commit($db);
  246. prnMsg( _('Batch Import of') .' ' . $fileName . ' '. _('has been completed. All transactions committed to the database.'),'success');
  247. }
  248. fclose($handle);
  249. } elseif ( isset($_POST['gettemplate']) || isset($_GET['gettemplate']) ) { //download an import template
  250. echo '<br /><br /><br />"'. implode('","',$headers). '"<br /><br /><br />';
  251. } else { //show file upload form
  252. echo '
  253. <br />
  254. <a href="Z_ImportStocks.php?gettemplate=1">Get Import Template</a>
  255. <br />
  256. <br />
  257. ';
  258. echo "<form ENCtype='multipart/form-data' action='Z_ImportStocks.php' method=post>";
  259. echo '<input type="hidden" name="FormID" value="' . $_SESSION['FormID'] . '">';
  260. echo "<input type='hidden' name='MAX_FILE_SIZE' value='1000000'>" .
  261. _('Upload file') . ": <input name='userfile' type='file'>
  262. <input type='submit' VALUE='" . _('Send File') . "'>
  263. </form>
  264. ";
  265. }
  266. include('includes/footer.inc');
  267. ?>