PageRenderTime 51ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/opensourcepos/application/controllers/products_2.php

https://bitbucket.org/jit_bec/shopifine
PHP | 645 lines | 466 code | 84 blank | 95 comment | 53 complexity | 4840ecf05bbbfd3e60669a371529fc25 MD5 | raw file
Possible License(s): LGPL-3.0
  1. <?php
  2. require_once ("secure_area.php");
  3. class Products extends Secure_area
  4. {
  5. const ID = "id";
  6. const PRODUCT_NAME = "product_name";
  7. const SYSTEM_NAME = "system_name";
  8. const DESC = "description";
  9. const BARCODE = "barcode";
  10. const MFR = "manufacturer";
  11. const MODEL = "model";
  12. const SUPPLIER = "supplier";
  13. function __construct()
  14. {
  15. parent::__construct('products');
  16. $this->load->model('Unitofmeasure');
  17. $this->load->model('Category');
  18. $this->load->model('Packaging');
  19. $this->load->model('Manufacturer');
  20. $this->load->model('Mfr_model');
  21. $this->load->model('Product');
  22. $this->load->model('Product_stock');
  23. $this->load->model('Product_price');
  24. $this->load->helper('grid_helper');
  25. $this->load->library('barcode-library/Barcode_lib','','barcode');
  26. }
  27. // function index()
  28. // {
  29. // $packageTypes = $this->Packaging->getAllPackageDetails();
  30. // $pkgOptions = null;
  31. //
  32. //
  33. // foreach($packageTypes as $packageType) {
  34. // $pkg_id=$packageType["package_id"];
  35. // $pkg_name=$packageType["package_name"];
  36. // $pkgOptions.="<OPTION VALUE=\"$pkg_id\">".$pkg_name;
  37. //
  38. // }
  39. //// $mfrs = $this->Manufacturer->getAllMfrs();
  40. ////
  41. //// foreach ($mfrs as &$mfr){
  42. //// $mfr = "\"".$mfr['manufacturer_name']."\"";
  43. //// }
  44. //// //$values = array_map('array_pop', $mfrs);
  45. //// $imploded = implode(",",$mfrs);
  46. //// $data['mfrs'] = $imploded;
  47. //
  48. // $mfrs = $this->Manufacturer->getAll();
  49. // $autoData = array();
  50. // foreach ($mfrs as $mfr){
  51. // $data = array('label' => $mfr['manufacturer_name'], 'value' => $mfr['id']);
  52. // array_push($autoData,$data);
  53. // }
  54. // $values = json_encode($autoData);
  55. // //$imploded = implode(",",$mfrs);
  56. // $data['mfrs'] = $values;
  57. //
  58. // $data['pkgOptions'] = $pkgOptions;
  59. // $this->load->view('products/product_details',$data);
  60. // }
  61. function index()
  62. {
  63. $packageTypes = $this->Packaging->getAllPackageDetails();
  64. $pkgOptions = null;
  65. $mfrOptions = null;
  66. foreach($packageTypes as $packageType) {
  67. $pkg_id=$packageType["package_id"];
  68. $pkg_name=$packageType["package_name"];
  69. $pkgOptions.="<OPTION VALUE=\"$pkg_id\">".$pkg_name;
  70. }
  71. //$values = json_encode($autoData);
  72. //$imploded = implode(",",$mfrs);
  73. $mfrs = $this->Manufacturer->getAll();
  74. foreach ($mfrs as $mfr){
  75. $mfr_id=$mfr["id"];
  76. $mfr_name=$mfr["manufacturer_name"];
  77. $mfrOptions.="<OPTION VALUE=\"$mfr_id\">".trim($mfr_name)."</OPTION>";
  78. }
  79. $data['mfrOptions'] = $mfrOptions;
  80. $data['pkgOptions'] = $pkgOptions;
  81. $this->load->view('products/products_grid',$data);
  82. }
  83. function populateMfrs(){
  84. $mfrs = $this->Manufacturer->getAll();
  85. foreach ($mfrs as $mfr){
  86. $mfr_id=$mfr["id"];
  87. $mfr_name=$mfr["manufacturer_name"];
  88. $mfrOptions.="<OPTION VALUE=\"$mfr_id\">".trim($mfr_name)."</OPTION>";
  89. }
  90. echo $mfrOptions;
  91. }
  92. function populatePackages(){
  93. $packageTypes = $this->Packaging->getAllPackageDetails();
  94. $pkgOptions = null;
  95. //$mfrOptions = null;
  96. foreach($packageTypes as $packageType) {
  97. $pkg_id=$packageType["package_id"];
  98. $pkg_name=$packageType["package_name"];
  99. $pkgOptions.="<OPTION VALUE=\"$pkg_id\">".$pkg_name;
  100. }
  101. echo $pkgOptions;
  102. }
  103. function populateMeasurementDropDowns(){
  104. $package_id = $_POST[pkgId];
  105. if (!empty($package_id)){
  106. $htmlUom = $this->getUOMDetails($package_id);
  107. echo $htmlUom;
  108. }
  109. }
  110. function populateMeasurementDropDownsByName(){
  111. $package_name = $_POST[pkgName];
  112. $package_id = $this->Packaging->getIdByPkgName($package_name);
  113. if (!empty($package_id)){
  114. $htmlUom = $this->getUOMDetails($package_id);
  115. echo $htmlUom;
  116. }
  117. }
  118. private function getUOMDetails($package_id){
  119. $uomJson = $this->Packaging->getJsonById($package_id);
  120. $uomArray = json_decode($uomJson,true);
  121. $uomList = array();
  122. $uomSizeMap = array();
  123. $htmlUom = null;
  124. foreach ($uomArray as $uomDetails){
  125. $thisUom = $uomDetails['uom'];
  126. //$uomId =
  127. $denom = $uomDetails['denom'];
  128. if (!in_array($thisUom, $uomList)){
  129. array_push($uomList, $thisUom);
  130. $htmlUom.="<OPTION VALUE=\"$thisUom\">".$thisUom;
  131. $uomSizeMap[$thisUom] = array(array('denom' => $denom));
  132. }
  133. else {
  134. array_push($uomSizeMap[$thisUom], array('denom' => $denom));
  135. }
  136. }
  137. session_start();
  138. $_SESSION['denomJson'] = json_encode($uomSizeMap);
  139. return $htmlUom;
  140. }
  141. function populateModel(){
  142. $mfr = $_POST[mfr];
  143. $models = $this->Mfr_model->getAllByMfrId($mfr);
  144. $htmlData= null;
  145. foreach ($models as $model){
  146. //$data = array('label' => $model['model_name'], 'value' => $model['id']);
  147. //array_push($autoData,$data);
  148. $id = $model['id'];
  149. $name = $model['model_name'];
  150. $htmlData.="<OPTION VALUE=\"$id\">".$name;
  151. }
  152. //$values = json_encode($autoData);
  153. echo $htmlData;
  154. }
  155. function populateBarcodesForStock(){
  156. $barcodeData = $this->Product_stock->getAll(false,null,1);
  157. $barcodeArray = array();
  158. foreach ($barcodeData as $barcoderow){
  159. array_push($barcodeArray,$barcoderow['barcode']);
  160. }
  161. return $barcodeArray;
  162. //echo json_encode($barcodeArray);
  163. }
  164. //hack for jqgrid dataUrl
  165. function doNothing (){
  166. }
  167. function populateDenomDropdown (){
  168. session_start();
  169. $uom = $_POST[uom];
  170. $htmlSize = null;
  171. if (isset($_SESSION['denomJson'])) {
  172. $allUomSizeMap = json_decode($_SESSION['denomJson'],true);
  173. //unset($_SESSION['denomJson']); //done. Now unset. very important
  174. $uomSizeMap = $allUomSizeMap[$uom];
  175. if (!empty($uomSizeMap)) {
  176. asort($uomSizeMap);
  177. foreach ($uomSizeMap as $validSize){
  178. $size = $validSize['denom'];
  179. $htmlSize.= "<OPTION VALUE=\"$size\">".$size;
  180. }
  181. }
  182. echo $htmlSize;
  183. }
  184. }
  185. function createBarcodeAndProduct(){
  186. $barCodedata = $_POST['barcodeData'];
  187. $barcode = null;
  188. $mfrCode =100;
  189. $modelCode = 100;
  190. $categoryCode = 100;
  191. $supplierCode = 100;
  192. $uomCode = 10;
  193. $sizeCode = 10;
  194. $packageCode = 10;
  195. $modelName = null;
  196. $mfrName = null;
  197. $modelId = null;
  198. $mfrId = null;
  199. $systemName = null;
  200. $scannedBarcode = $barCodedata['scannedBarcode'];
  201. $name = $barCodedata['name'];
  202. $desc = $barCodedata['desc'];
  203. $mfrOp = $barCodedata['mfrOp'];
  204. $mfrIp = $barCodedata['mfrIp'];
  205. $modelOp = $barCodedata['modelOp'];
  206. $modelIp = $barCodedata['modelIp'];
  207. $newModelIp = $barCodedata['newModelIp'];
  208. $categoryArray = json_decode($barCodedata['category']);
  209. //$categoryIp = $barCodedata['categoryIp'];
  210. $supplierOp = $barCodedata['supplierOp'];
  211. $uomOp = $barCodedata['uomOp'];
  212. $sizeOp = $barCodedata['sizeOp'];
  213. $packageOp = $barCodedata['packageOp'];
  214. $reorderLevel = $barCodedata['reorderLevel'];
  215. $costPrice = $barCodedata['costPrice'];
  216. $price = $barCodedata['price'];
  217. if (empty($reorderLevel)){
  218. $reorderLevel = 0;
  219. }
  220. if (!empty($mfrOp)){
  221. $mfrId = $mfrOp;
  222. $mfrCode += $mfrOp;
  223. if (!empty($modelOp)){
  224. $modelId = $modelOp;
  225. $modelCode += $modelOp;
  226. $modelName = $this->Mfr_model->getName($modelOp);
  227. }
  228. else {
  229. if (!empty($modelIp))
  230. $modelData = array('manufacturer_id'=>$mfrId,'model_name' => $modelIp);
  231. $modelInsertId = $this->Mfr_model->insert($modelData);
  232. $modelId = $modelInsertId;
  233. $modelCode += $modelInsertId;
  234. $modelName = $modelIp;
  235. }
  236. $mfrName = $this->Manufacturer->getName($mfrOp);
  237. }
  238. if (empty($mfrOp) && !empty($mfrIp)){
  239. $mfrData = array ('manufacturer_name'=>trim($mfrIp));
  240. $mfrName = $mfrIp;
  241. $mfrInsertId = $this->Manufacturer->insert($mfrData);
  242. $mfrCode += $mfrInsertId;
  243. $mfrId = $mfrInsertId;
  244. if ($mfrInsertId != 0 && $newModelIp!=""){
  245. $modelData = array('manufacturer_id'=>$mfrInsertId,'model_name' => trim($newModelIp));
  246. $modelInsertId = $this->Mfr_model->insert($modelData);
  247. $modelId = $modelInsertId;
  248. $modelCode += $modelInsertId;
  249. $modelName = $newModelIp;
  250. }
  251. }
  252. $categorymapping = array();
  253. if (!empty($categoryArray)){
  254. foreach($categoryArray as $category){
  255. $categoryCode += $category;
  256. $categoryDetails = $this->Category->getById($category);
  257. array_push(array('id'=>$category,'name'=> $categoryDetails['category_name']));
  258. }
  259. }
  260. log_message('debug', $categorymapping);
  261. print_r($categorymapping);
  262. if (!empty($supplierOp)){
  263. $supplierCode += $supplierOp;
  264. }
  265. if (!empty($uomOp)){
  266. $uomCode += $uomOp;
  267. }
  268. if (!empty($sizeOp)){
  269. $sizeCode += $sizeOp;
  270. }
  271. if (!empty($packageOp)){
  272. $packageCode +=$packageOp;
  273. }
  274. $productid = $this->Product->lastIdPresent() + 1;
  275. if (empty($scannedBarcode)){
  276. $barcode = "1".$mfrCode. $modelCode . $supplierCode .$packageCode .$uomCode.$sizeCode.$productid;
  277. }
  278. else {
  279. $barcode = $scannedBarcode;
  280. }
  281. $ifexist = false;
  282. if (empty($scannedBarcode)){
  283. $where_clause_if_exist = array(
  284. 'manufacturer_id'=>$mfrId,
  285. 'category_id'=>$category,
  286. 'model_id'=>$modelId,
  287. 'package_id'=>$packageOp,
  288. 'uom'=>$uomOp,
  289. 'measurement_denomination'=>$sizeOp);
  290. $barcodeExist = $this->Product->getBarcode($where_clause_if_exist);
  291. if (!empty($barcodeExist)){
  292. $response['status'] = 'error';
  293. $response['message'] = 'The Product Your Are Trying To Add
  294. Looks Like Already Exists.Please Check Barcode '.$barcodeExist;
  295. echo json_encode($response);
  296. return;
  297. }
  298. }
  299. else {
  300. $ifexist = $this->Product->ifExistsByBarcode($scannedBarcode);
  301. if ($ifexist){
  302. $response['status'] = 'error';
  303. $response['message'] = $scannedBarcode.' Already Exists';
  304. echo json_encode($response);
  305. return;
  306. }
  307. }
  308. // $systemName = $name."-".$mfrName."-".$modelName."-".uom."-".$sizeOp;
  309. // $this->db->trans_start();
  310. //
  311. // $productData = array('barcode'=>$barcode,
  312. // 'product_name'=>$name,
  313. //// 'system_name'=>$systemName,
  314. // 'description'=>$desc,
  315. // 'manufacturer'=>$mfrName,
  316. // 'manufacturer_id'=>$mfrId,
  317. // 'category_id'=>$category,
  318. // 'category_name' => $categoryName,
  319. // 'model'=>$modelName,
  320. // 'model_id'=>$modelId,
  321. // 'package_id'=>$packageOp,
  322. // 'reorder_level'=>$reorderLevel,
  323. // 'uom'=>$uomOp,
  324. // 'measurement_denomination'=>$sizeOp);
  325. // $insertedProductId = $this->Product->insert($productData);
  326. // $product_stock_data = array ('product_id'=>$insertedProductId,'barcode'=>$barcode);
  327. // $this->Product_stock->insert($product_stock_data);
  328. // $product_price_data = array ('product_id'=>$insertedProductId,'barcode'=>$barcode);
  329. // if (!empty($costPrice)){
  330. // $product_price_data['cost_price'] = $costPrice;
  331. // }
  332. // if (!empty($price)){
  333. // $product_price_data['price'] = $price;
  334. // }
  335. // $this->Product_price->insert($product_price_data);
  336. // $this->db->trans_complete();
  337. //
  338. //
  339. // if ($this->db->trans_status() === FALSE)
  340. // {
  341. // log_message('Product Insertion Failed');
  342. // }
  343. //
  344. // $response['status'] = 'success';
  345. // $response['message'] = 'Item '.$name. ' Has Been Successfully Added. The Barcode Is '.$barcode;
  346. // echo json_encode($response);
  347. }
  348. function editProduct (){
  349. $mfrName = null;
  350. $modelName = null;
  351. $id = $_REQUEST[Products::ID];
  352. $mfrId = $_REQUEST[Products::MFR];
  353. $modelId = $_REQUEST[Products::MODEL];
  354. if (!empty($mfrId)) {
  355. $mfrName = $this->Manufacturer->getName($mfrId);
  356. }
  357. if (!empty($modelId)) {
  358. $modelName = $this->Mfr_model->getName($modelId);
  359. }
  360. $productData = array(
  361. Products::PRODUCT_NAME=>$_REQUEST[Products::PRODUCT_NAME],
  362. Products::DESC=>$_REQUEST[Products::DESC],
  363. 'manufacturer'=>$mfrName,
  364. 'manufacturer_id'=>$mfrId,
  365. 'model'=>$modelName,
  366. 'model_id'=>$modelId
  367. );
  368. $this->Product->update($id,$productData);
  369. }
  370. function populateProductsInGrid (){
  371. session_start();
  372. $searchOn = strip($_REQUEST['_search']);
  373. $page = $_REQUEST['page'];
  374. $limit = $_REQUEST['rows'];
  375. $sidx = $_REQUEST['sidx'];
  376. $sord = $_REQUEST['sord'];
  377. $loadAll = false;
  378. if (!empty($_REQUEST['loadall'])){
  379. $loadAll = $_REQUEST['loadall'];
  380. $_SESSION['load'] = true;
  381. }
  382. $productsdata = array();
  383. $count = $this->Product->totalNoOfRows();
  384. if( $count > 0 && $limit > 0) {
  385. $total_pages = ceil($count/$limit);
  386. } else {
  387. $total_pages = 0;
  388. }
  389. if ($page > $total_pages) $page=$total_pages;
  390. $start = $limit*$page - $limit;
  391. // if for some reasons start position is negative set it to 0
  392. // typical case is that the user type 0 for the requested page
  393. if($start <0) $start = 0;
  394. $clauses = array('orderBy'=>$sidx,'orderDir'=>$sord,'startLimit'=>$start,'limit'=>$limit);
  395. $data['total'] = $total_pages;
  396. $data['page'] = $page;
  397. $data['records'] = $count;
  398. if($searchOn=='true') {
  399. $sessionLoadAll = $_SESSION['load'];
  400. $filters = json_decode($_REQUEST['filters'],true);
  401. $groupOp = $filters['groupOp'];
  402. $rules = $filters['rules'];
  403. $where_condition = array();
  404. foreach ($rules as $rule){
  405. $field = $rule['field'];
  406. $op= $rule['op'];
  407. $input = $rule['data'];
  408. //$
  409. $where_condition[$field] = $input;
  410. }
  411. if ($sessionLoadAll){
  412. $products = $this->Product->getAllFlattenedByLike($where_condition,false,$clauses);
  413. }
  414. else {
  415. $products = $this->Product->getAllFlattenedByLike($where_condition,false,$clauses,1);
  416. }
  417. }
  418. else {
  419. if (empty($_REQUEST['loadall'])) {
  420. $_SESSION['load'] = false;
  421. }
  422. if ($loadAll){
  423. $products = $this->Product->getAllFlattened(false,$clauses);
  424. }
  425. else {
  426. $products = $this->Product->getAllFlattened(false,$clauses,1);
  427. }
  428. }
  429. //$dp['system_name']
  430. foreach ($products as $dp){
  431. array_push($productsdata, array('id'=> $dp['id'],'dprow' => array($dp['barcode'],/*$dp['system_name'],*/$dp['product_name'],$dp['description'],$dp['manufacturer_id'],$dp['model_id'],$dp['manufacturer'],$dp['model'],$dp['supplier'],$dp['package_id'],$dp['package_name'],/*$dp['category_name'],*/$dp['uom'],$dp['measurement_denomination'],'Print Barcode')));
  432. }
  433. $data['productdata'] = $productsdata;
  434. //$export = $_REQUEST['oper'];
  435. echo json_encode($data);
  436. }
  437. function deactivate(){
  438. $ids = $_POST[ids];
  439. $this->Product->deactivate($ids);
  440. }
  441. function activate(){
  442. $ids = $_POST[ids];
  443. $this->Product->activate($ids);
  444. }
  445. function exportProductsInGrid (){
  446. $export = $_REQUEST['oper'];
  447. if ($export =='csv')
  448. $searchOn = strip($_REQUEST['_search']);
  449. $productsdata = array();
  450. if($searchOn=='true') {
  451. $filters = json_decode($_REQUEST['filters'],true);
  452. $groupOp = $filters['groupOp'];
  453. $rules = $filters['rules'];
  454. $where_condition = array();
  455. foreach ($rules as $rule){
  456. $field = $rule['field'];
  457. $op= $rule['op'];
  458. $input = $rule['data'];
  459. //$
  460. $where_condition[$field] = $input;
  461. }
  462. $products = $this->Product->getAllFlattenedByLike($where_condition,true);
  463. }
  464. else {
  465. $products = $this->Product->getAllFlattened(true);
  466. }
  467. header("Content-type: application/octet-stream");
  468. header("Content-description: File Transfer");
  469. header("Content-disposition: attachment; filename=\"thefilename.csv\"");
  470. header("Pragma: public");
  471. header("Cache-control: max-age=0");
  472. header("Expires: 0");
  473. echo $products;
  474. }
  475. function loadInventory (){
  476. $data['barcodes'] = json_encode($this->populateBarcodesForStock());
  477. $this->load->view('products/product_stock_details',$data);
  478. }
  479. function populateProductStocksInGrid (){
  480. //session_start();
  481. $searchOn = strip($_REQUEST['_search']);
  482. $page = $_REQUEST['page'];
  483. $limit = $_REQUEST['rows'];
  484. $sidx = $_REQUEST['sidx'];
  485. $sord = $_REQUEST['sord'];
  486. $productsdata = array();
  487. $count = $this->Product_stock->totalNoOfRows();
  488. if( $count > 0 && $limit > 0) {
  489. $total_pages = ceil($count/$limit);
  490. } else {
  491. $total_pages = 0;
  492. }
  493. if ($page > $total_pages) $page=$total_pages;
  494. $start = $limit*$page - $limit;
  495. // if for some reasons start position is negative set it to 0
  496. // typical case is that the user type 0 for the requested page
  497. if($start <0) $start = 0;
  498. $clauses = array('orderBy'=>$sidx,'orderDir'=>$sord,'startLimit'=>$start,'limit'=>$limit);
  499. $data['total'] = $total_pages;
  500. $data['page'] = $page;
  501. $data['records'] = $count;
  502. if($searchOn=='true') {
  503. $filters = json_decode($_REQUEST['filters'],true);
  504. $groupOp = $filters['groupOp'];
  505. $rules = $filters['rules'];
  506. $where_condition = array();
  507. foreach ($rules as $rule){
  508. $field = $rule['field'];
  509. $op= $rule['op'];
  510. $input = $rule['data'];
  511. $where_condition[$field] = $input;
  512. }
  513. $products = $this->Product_stock->getAllByLike($where_condition,false,$clauses,1);
  514. }
  515. else {
  516. $products = $this->Product_stock->getAll(false,$clauses,1);
  517. }
  518. //$dp['system_name']
  519. foreach ($products as $dp){
  520. array_push($productsdata, array('id'=> $dp['id'],'dprow' => array($dp['product_id'],$dp['barcode'],$dp['system_name'],$dp['stock'],0,$dp['isactive'])));
  521. }
  522. $data['productdata'] = $productsdata;
  523. echo json_encode($data);
  524. }
  525. function updateStocks (){
  526. $id = strip($_REQUEST['id']);
  527. $oper = strip($_REQUEST['oper']);
  528. $stockAdd = strip($_REQUEST['stock_add']);
  529. $lastaction = 'added';
  530. $stockLevel = $this->Product_stock->getStockLevel('id',$id);
  531. $stockLevel += $stockAdd;
  532. $stock_data = array ('stock' => $stockLevel,'stock_added'=>$stockAdd,'last_action'=>$lastaction);
  533. $this->Product_stock->update($id,$stock_data);
  534. }
  535. function updateStocksByBarcode (){
  536. $barcode = strip($_REQUEST['barcode']);
  537. //$oper = strip($_REQUEST['oper']);
  538. $stockAdd = strip($_REQUEST['stock_add']);
  539. $lastaction = 'added';
  540. $stockLevel = $this->Product_stock->getStockLevel('barcode',$barcode);
  541. $stockLevel += $stockAdd;
  542. $stock_data = array ('stock' => $stockLevel,'stock_added'=>$stockAdd,'last_action'=>$lastaction);
  543. $this->Product_stock->updateByBarcode($barcode,$stock_data);
  544. }
  545. function printBarcode(){
  546. $id =$_REQUEST['id'];
  547. $barcodeArray = $this->Product->getValues(array("id"=>$id),array("barcode"));
  548. $barcode = $barcodeArray[0]["barcode"];
  549. $filename = IMAGEPATH.'temp/barcodefinal.png';
  550. $this->load->helper('file');
  551. delete_files($filename);
  552. if ( ! write_file($filename, ''))
  553. {
  554. echo 'Unable to write the file';
  555. }
  556. $this->barcode->generateBarcode($barcode,$filename);
  557. $data['filename'] = $filename;
  558. $this->load->view("utilities/barcode",$data);
  559. }
  560. }
  561. ?>