PageRenderTime 53ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/magehelp/application/controllers/products.php

https://bitbucket.org/jit_bec/shopifine
PHP | 836 lines | 565 code | 104 blank | 167 comment | 68 complexity | 71c555de1740b825b9bc1768fa5ba40b 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('Product_stock');
  18. $this->load->model('Product_price');
  19. $this->load->helper('grid_helper');
  20. $this->load->library('barcode-library/Barcode_lib','','barcode');
  21. }
  22. function index()
  23. {
  24. $data['mfrOptions'] = populateManfacturers();
  25. $data['attributeSetOptions'] = populateAttributeSets();
  26. $data['pkgOptions'] = populatePackages();
  27. $data['supplierOptions'] = populateSuppliers();
  28. $this->load->view('products/products_grid',$data);
  29. }
  30. function populateMfrs(){
  31. echo populateManfacturers();
  32. }
  33. function populatePackages(){
  34. echo populatePackages();
  35. }
  36. function populateMeasurementDropDowns(){
  37. $package_id = $_POST[pkgId];
  38. if (!empty($package_id)){
  39. $htmlUom = $this->getUOMDetails($package_id);
  40. echo $htmlUom;
  41. }
  42. }
  43. function populateMeasurementDropDownsByName(){
  44. $package_name = $_POST[pkgName];
  45. $package_id = $this->Packaging->getIdByPkgName($package_name);
  46. if (!empty($package_id)){
  47. $htmlUom = $this->getUOMDetails($package_id);
  48. echo $htmlUom;
  49. }
  50. }
  51. private function getUOMDetails($package_id){
  52. $uomJson = $this->Packaging->getJsonById($package_id);
  53. $uomArray = json_decode($uomJson,true);
  54. $uomList = array();
  55. $uomSizeMap = array();
  56. $htmlUom = null;
  57. foreach ($uomArray as $uomDetails){
  58. $thisUom = $uomDetails['uom'];
  59. //$uomId =
  60. $denom = $uomDetails['denom'];
  61. if (!in_array($thisUom, $uomList)){
  62. array_push($uomList, $thisUom);
  63. $htmlUom.="<OPTION VALUE=\"$thisUom\">".$thisUom;
  64. $uomSizeMap[$thisUom] = array(array('denom' => $denom));
  65. }
  66. else {
  67. array_push($uomSizeMap[$thisUom], array('denom' => $denom));
  68. }
  69. }
  70. session_start();
  71. $_SESSION['denomJson'] = json_encode($uomSizeMap);
  72. return $htmlUom;
  73. }
  74. function populateModel(){
  75. $mfr = $_POST[mfr];
  76. echo populateModels($mfr);
  77. }
  78. function populateBarcodesForStock(){
  79. $barcodeData = $this->Product_stock->getAll(false,null,1);
  80. $barcodeArray = array();
  81. foreach ($barcodeData as $barcoderow){
  82. array_push($barcodeArray,$barcoderow['barcode']);
  83. }
  84. return $barcodeArray;
  85. }
  86. //hack for jqgrid dataUrl
  87. function doNothing (){
  88. }
  89. function populateDenomDropdown (){
  90. session_start();
  91. $uom = $_POST[uom];
  92. $htmlSize = null;
  93. if (isset($_SESSION['denomJson'])) {
  94. $allUomSizeMap = json_decode($_SESSION['denomJson'],true);
  95. //unset($_SESSION['denomJson']); //done. Now unset. very important
  96. $uomSizeMap = $allUomSizeMap[$uom];
  97. if (!empty($uomSizeMap)) {
  98. asort($uomSizeMap);
  99. foreach ($uomSizeMap as $validSize){
  100. $size = $validSize['denom'];
  101. $htmlSize.= "<OPTION VALUE=\"$size\">".$size;
  102. }
  103. }
  104. echo $htmlSize;
  105. }
  106. }
  107. function createBarcodeAndProduct(){
  108. $barCodedata = $_POST['barcodeData'];
  109. $barcode = null;
  110. $mfrCode =100;
  111. $modelCode = 100;
  112. $categoryCode = 100; //not used
  113. // $supplierCode = 100;
  114. $uomCode = 10;
  115. $sizeCode = 10;
  116. $packageCode = 10;
  117. $modelName = null;
  118. $mfrName = null;
  119. $modelId = null;
  120. $mfrId = null;
  121. $systemName = null;
  122. $scannedBarcode = $barCodedata['scannedBarcode'];
  123. $name = $barCodedata['name'];
  124. $desc = $barCodedata['desc'];
  125. $mfrOp = $barCodedata['mfrOp'];
  126. $mfrIp = $barCodedata['mfrIp'];
  127. $modelOp = $barCodedata['modelOp'];
  128. $modelIp = $barCodedata['modelIp'];
  129. $newModelIp = $barCodedata['newModelIp'];
  130. $categoryArray = json_decode($barCodedata['category']);
  131. //$categoryIp = $barCodedata['categoryIp'];
  132. $supplierOp = $barCodedata['supplierOp'];
  133. $uomOp = $barCodedata['uomOp'];
  134. $sizeOp = $barCodedata['sizeOp'];
  135. $packageOp = $barCodedata['packageOp'];
  136. $override = $barCodedata['override'];
  137. // $costPrice = $barCodedata['costPrice'];
  138. // $price = $barCodedata['price'];
  139. $attributeSet = $barCodedata['attributeSet'];
  140. $this->db->trans_start();
  141. if (!empty($uomOp)){
  142. $uomId = $this->Unitofmeasure->getByName($uomOp,array('id'),false)->id;
  143. }
  144. if (!empty($scannedBarcode)){
  145. if ($this->Product->ifExistsByBarcode($scannedBarcode)){
  146. $response['status'] = 'error';
  147. $response['message'] = 'The Product With Barcode ' . $scannedBarcode. ' Already Exists In The System' ;
  148. echo json_encode($response);
  149. return;
  150. }
  151. }
  152. else if (empty($scannedBarcode) && $override !=1){
  153. if (!empty($mfrOp)){
  154. $where['manufacturer_id'] = $mfrOp;
  155. }
  156. if (!empty($modelOp)){
  157. $where['model_id'] = $modelOp;
  158. }
  159. if (!empty($uomOp)){
  160. $where['uom'] = $uomOp;
  161. }
  162. if (!empty($sizeOp)){
  163. $where['measurement_denomination'] = $sizeOp;
  164. }
  165. if (!empty($packageOp)){
  166. $where['package_id'] = $packageOp;
  167. }
  168. $approx_match=$this->Product->totalNoOfRows($where,null,array('product_name'=>$name));
  169. if ($approx_match>0){
  170. $response['status'] = 'exists';
  171. $response['message'] = 'We found ' . $approx_match. ' Products Which Approximately Matches With Your Product' ;
  172. echo json_encode($response);
  173. log_message('debug','total statement ='.$this->db->last_query());
  174. return;
  175. }
  176. //echo $this->Product->totalNoOfRows($where,null,array('product_name'=>$name));
  177. // if (){
  178. //
  179. // }
  180. }
  181. // if (empty($reorderLevel)){
  182. // $reorderLevel = 0;
  183. // }
  184. if (!empty($mfrOp)){
  185. $mfrId = $mfrOp;
  186. $mfrCode += $mfrOp;
  187. if (!empty($modelOp)){
  188. $modelId = $modelOp;
  189. $modelCode += $modelOp;
  190. $modelName = $this->Mfr_model->getName($modelOp);
  191. }
  192. else {
  193. if (!empty($modelIp)){
  194. $modelData = array('manufacturer_id'=>$mfrId,'model_name' => $modelIp);
  195. $modelInsertId = $this->Mfr_model->insert($modelData);
  196. $modelId = $modelInsertId;
  197. $modelCode += $modelInsertId;
  198. $modelName = $modelIp;
  199. }
  200. }
  201. $mfrName = $this->Manufacturer->getName($mfrOp);
  202. }
  203. if (empty($mfrOp) && !empty($mfrIp)){
  204. $mfrData = array ('manufacturer_name'=>trim($mfrIp));
  205. $mfrName = $mfrIp;
  206. $mfrInsertId = $this->Manufacturer->insert($mfrData);
  207. $mfrCode += $mfrInsertId;
  208. $mfrId = $mfrInsertId;
  209. if ($mfrInsertId != 0 && $newModelIp!=""){
  210. $modelData = array('manufacturer_id'=>$mfrInsertId,'model_name' => trim($newModelIp));
  211. $modelInsertId = $this->Mfr_model->insert($modelData);
  212. $modelId = $modelInsertId;
  213. $modelCode += $modelInsertId;
  214. $modelName = $newModelIp;
  215. }
  216. }
  217. $categorymapping = array();
  218. if (!empty($categoryArray)){
  219. foreach($categoryArray as $category){
  220. $categoryCode += $category;
  221. $categoryDetails = $this->Category->getById($category);
  222. array_push($categorymapping,array('id'=>$category,'name'=> $categoryDetails['category_name']));
  223. }
  224. }
  225. //print_r(json_en$categorymapping);
  226. log_message('debug', json_encode($categorymapping));
  227. //
  228. // if (!empty($supplierOp)){
  229. // $supplierCode += $supplierOp;
  230. // }
  231. if (!empty($uomId)){
  232. $uomCode += $uomId;
  233. }
  234. if (!empty($sizeOp)){
  235. $sizeCode += $sizeOp;
  236. }
  237. if (!empty($packageOp)){
  238. $packageCode +=$packageOp;
  239. }
  240. $productid = $this->Product->lastIdPresent() + 1;
  241. if (empty($scannedBarcode)){
  242. $barcode = "1".$mfrCode. $modelCode.$packageCode .$uomCode.$sizeCode.$productid;
  243. }
  244. else {
  245. $barcode = $scannedBarcode;
  246. }
  247. //$ifexist = false;
  248. // if (empty($barcode)){
  249. // if (!empty($mfrId) && !empty($modelId)){
  250. // $where_clause_if_exist = array(
  251. //
  252. // 'manufacturer_id'=>$mfrId,
  253. // // 'category_id'=>$category,
  254. // 'model_id'=>$modelId,
  255. // 'package_id'=>$packageOp,
  256. // 'uom'=>$uomOp,
  257. // 'measurement_denomination'=>$sizeOp);
  258. // $barcodeExist = $this->Product->getBarcode($where_clause_if_exist);
  259. // if (!empty($barcodeExist)){
  260. // $response['status'] = 'error';
  261. // $response['message'] = 'The Product Your Are Trying To Add
  262. // Looks Like Already Exists.Please Check Barcode '.$barcodeExist;
  263. // echo json_encode($response);
  264. // return;
  265. // }
  266. // }
  267. // $where_clause_if_exist = array(
  268. //
  269. // 'product_name'=>$name);
  270. // $barcodeExist = $this->Product->getBarcode($where_clause_if_exist);
  271. // if (!empty($barcodeExist)){
  272. // $response['status'] = 'error';
  273. // $response['message'] = 'The Product Your Are Trying To Add
  274. // Looks Like Already Exists.Please Check Barcode '.$barcodeExist;
  275. // echo json_encode($response);
  276. // return;
  277. // }
  278. // }
  279. // else {
  280. // $ifexist = $this->Product->ifExistsByBarcode($barcode);
  281. // if ($ifexist){
  282. // $response['status'] = 'error';
  283. // $response['message'] = $barcode.' Already Exists';
  284. // echo json_encode($response);
  285. // return;
  286. // }
  287. $defaultDesc = $name.'-'.$mfrName.'-'.$modelName.' '.$sizeOp.' '.$uomOp.' ';
  288. $productData = array('barcode'=>$barcode,
  289. 'product_name'=>$name,
  290. 'description'=> $defaultDesc,// by default
  291. 'meta_description'=>$defaultDesc, // by default
  292. 'manufacturer'=>$mfrName,
  293. 'manufacturer_id'=>$mfrId,
  294. 'model'=>$modelName,
  295. 'model_id'=>$modelId,
  296. 'package_id'=>$packageOp,
  297. 'uom'=>$uomOp,
  298. 'attribute_set'=>$attributeSet,
  299. 'measurement_denomination'=>$sizeOp);
  300. $insertedProductId = $this->Product->insert($productData);
  301. $this->Supplier->createProductSupplierMapping($insertedProductId,$supplierOp);
  302. $product_stock_data = array ('product_id'=>$insertedProductId,'barcode'=>$barcode);
  303. $this->Product_stock->insert($product_stock_data);
  304. // $product_price_data = array ('product_id'=>$insertedProductId,'barcode'=>$barcode);
  305. // if (!empty($costPrice)){
  306. // $product_price_data['cost_price'] = $costPrice;
  307. // }
  308. // if (!empty($price)){
  309. // $product_price_data['price'] = $price;
  310. // }
  311. // $this->Product_price->insert($product_price_data);
  312. foreach ($categorymapping as $mapping){
  313. $this->Category->saveProductCategoryMapping($mapping['id'],$insertedProductId,$mapping['name'],$barcode);
  314. }
  315. $this->db->trans_complete();
  316. if ($this->db->trans_status() === FALSE)
  317. {
  318. log_message('Product Insertion Failed');
  319. }
  320. $response['status'] = 'success';
  321. $response['message'] = 'Item '.$name. ' Has Been Successfully Added. The Barcode Is '.$barcode;
  322. echo json_encode($response);
  323. }
  324. function editProduct (){
  325. $editdata = $_REQUEST['editData'];
  326. $id = $editdata['id'];
  327. $productData = array(
  328. 'product_name'=>$editdata['name'],
  329. 'description'=>$editdata['desc'],
  330. 'meta_description'=>$editdata['meta_desc'],
  331. 'reorder_level'=>$editdata['reorderLevel'],
  332. 'vat'=>$editdata['vat'],
  333. 'attribute_set'=>$editdata['attributeSet'],
  334. 'margin_type'=>$editdata['margin_type'],
  335. 'margin_value'=>$editdata['margin_value'],
  336. );
  337. $this->Product->update($id,$productData);
  338. $response['status'] = 'success';
  339. $response['message'] = 'Item '.$name. ' Has Been Successfully Modified';
  340. echo json_encode($response);
  341. }
  342. // function populateProductsInGrid (){
  343. // session_start();
  344. // $searchOn = strip($_REQUEST['_search']);
  345. // $page = $_REQUEST['page'];
  346. // $limit = $_REQUEST['rows'];
  347. // $sidx = $_REQUEST['sidx'];
  348. // $sord = $_REQUEST['sord'];
  349. // $loadAll = false;
  350. // if (!empty($_REQUEST['loadall'])){
  351. // $loadAll = $_REQUEST['loadall'];
  352. // $_SESSION['load'] = true;
  353. // }
  354. //
  355. //
  356. // $productsdata = array();
  357. // $count = $this->Product->totalNoOfRows();
  358. // if( $count > 0 && $limit > 0) {
  359. // $total_pages = ceil($count/$limit);
  360. // } else {
  361. // $total_pages = 0;
  362. // }
  363. // if ($page > $total_pages) $page=$total_pages;
  364. //
  365. // $start = $limit*$page - $limit;
  366. //
  367. // // if for some reasons start position is negative set it to 0
  368. // // typical case is that the user type 0 for the requested page
  369. // if($start <0) $start = 0;
  370. // $clauses = array('orderBy'=>$sidx,'orderDir'=>$sord,'startLimit'=>$start,'limit'=>$limit);
  371. //
  372. // $data['total'] = $total_pages;
  373. // $data['page'] = $page;
  374. // $data['records'] = $count;
  375. // if($searchOn=='true') {
  376. // $sessionLoadAll = $_SESSION['load'];
  377. // $filters = json_decode($_REQUEST['filters'],true);
  378. // $groupOp = $filters['groupOp'];
  379. // $rules = $filters['rules'];
  380. // $where_condition = array();
  381. // foreach ($rules as $rule){
  382. // $field = $rule['field'];
  383. // $op= $rule['op'];
  384. // $input = $rule['data'];
  385. // //$
  386. // $where_condition[$field] = $input;
  387. // }
  388. // if ($sessionLoadAll){
  389. // $products = $this->Product->getAllFlattenedByLike($where_condition,false,$clauses);
  390. //
  391. // }
  392. // else {
  393. // $products = $this->Product->getAllFlattenedByLike($where_condition,false,$clauses,1);
  394. // }
  395. //
  396. //
  397. // }
  398. // else {
  399. // if (empty($_REQUEST['loadall'])) {
  400. // $_SESSION['load'] = false;
  401. // }
  402. // if ($loadAll){
  403. // $products = $this->Product->getAllFlattened(false,$clauses);
  404. // }
  405. // else {
  406. // $products = $this->Product->getAllFlattened(false,$clauses,1);
  407. // }
  408. //
  409. //
  410. // }
  411. // //$dp['system_name']
  412. // foreach ($products as $dp){
  413. // 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'],$dp['attribute_set'],$dp['reorder_level'],$dp['vat'],$dp['meta_description'],'Print Barcode'),$dp['measurement_denomination'] ." ".$dp['uom']));
  414. // }
  415. // $data['productdata'] = $productsdata;
  416. // //$export = $_REQUEST['oper'];
  417. // echo json_encode($data);
  418. // }
  419. function populateProductsInGrid (){
  420. $productsdata = array();
  421. $where = array();
  422. $in_where = array();
  423. $loadall = $_REQUEST['loadall'];
  424. if (!$loadall){
  425. $where['isactive'] = 1;
  426. }
  427. //setOwnerStatusCommon($where,$in_where);
  428. if (!empty($_REQUEST['mfr_id'])){
  429. $where['manufacturer_id'] = $_REQUEST['mfr_id'];
  430. }
  431. if (!empty($_REQUEST['model_id'])){
  432. $where['model_id'] = $_REQUEST['model_id'];
  433. }
  434. if (!empty($_REQUEST['uom'])){
  435. $where['uom'] = $_REQUEST['uom'];
  436. }
  437. if (!empty($_REQUEST['size'])){
  438. $where['measurement_denomination'] = $_REQUEST['size'];
  439. }
  440. if (!empty($_REQUEST['package_id'])){
  441. $where['package_id'] = $_REQUEST['package_id'];
  442. }
  443. $or_where = array();
  444. if (!empty($_REQUEST['product_name'])){
  445. $or_where['product_name'] = $_REQUEST['product_name'];
  446. }
  447. //standard response parameters
  448. $griddata= populateGridCommon('Product',$where,null,$in_where,$or_where);
  449. $dbrows = $griddata['db_data'];
  450. $data = $griddata['grid_metadata'];
  451. //
  452. foreach ($dbrows as $dp){
  453. 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'],$dp['attribute_set'],$dp['reorder_level'],$dp['vat'],$dp['margin_type'],$dp['margin_value'],$dp['meta_description'],'Print Barcode'),$dp['measurement_denomination'] ." ".$dp['uom']));
  454. }
  455. $data['productdata'] = $productsdata;
  456. //$export = $_REQUEST['oper'];
  457. echo json_encode($data);
  458. }
  459. function populateProductsSuppliersGrid (){
  460. $searchOn = strip($_REQUEST['_search']);
  461. $page = $_REQUEST['page'];
  462. $limit = $_REQUEST['rows'];
  463. $sidx = $_REQUEST['sidx'];
  464. $sord = $_REQUEST['sord'];
  465. $productcategorydata = array();
  466. $count = $this->Supplier->totalNoOfRowsInProductSupplierMapping();
  467. if( $count > 0 && $limit > 0) {
  468. $total_pages = ceil($count/$limit);
  469. } else {
  470. $total_pages = 0;
  471. }
  472. if ($page > $total_pages) $page=$total_pages;
  473. $start = $limit*$page - $limit;
  474. // if for some reasons start position is negative set it to 0
  475. // typical case is that the user type 0 for the requested page
  476. if($start <0) $start = 0;
  477. $clauses = array('orderBy'=>$sidx,'orderDir'=>$sord,'startLimit'=>$start,'limit'=>$limit);
  478. $data['total'] = $total_pages;
  479. $data['page'] = $page;
  480. $data['records'] = strval($count);
  481. if($searchOn=='true') {
  482. $filters = json_decode($_REQUEST['filters'],true);
  483. $groupOp = $filters['groupOp'];
  484. $rules = $filters['rules'];
  485. $like_condition = array();
  486. foreach ($rules as $rule){
  487. $field = $rule['field'];
  488. $op= $rule['op'];
  489. $input = $rule['data'];
  490. $like_condition[$field] = $input;
  491. }
  492. $productsuppliers = $this->Supplier->getAllProductsSupplierMapping(false,null,array('id','product_name','barcode','product_id','supplier_id','supplier_name'),$clauses,$like_condition);
  493. }
  494. else {
  495. $productsuppliers = $this->Supplier->getAllProductsSupplierMapping(false,null,array('id','product_name','barcode','product_id','supplier_id','supplier_name'),$clauses);
  496. }
  497. // $id=1;
  498. foreach ($productsuppliers as $dp){
  499. array_push($productcategorydata, array('id'=> $dp['id'],'dprow' => array($dp['product_name'],$dp['barcode'],$dp['product_id'],$dp['supplier_id'],$dp['supplier_name'])));
  500. }
  501. $data['productsupplierdata'] = $productcategorydata;
  502. echo json_encode($data);
  503. }
  504. function deactivate(){
  505. $ids = $_POST[ids];
  506. $this->Product->deactivate($ids);
  507. }
  508. function activate(){
  509. $ids = $_POST[ids];
  510. $this->Product->activate($ids);
  511. }
  512. function exportProductsInGrid (){
  513. $export = $_REQUEST['oper'];
  514. if ($export =='csv')
  515. $searchOn = strip($_REQUEST['_search']);
  516. $productsdata = array();
  517. if($searchOn=='true') {
  518. $filters = json_decode($_REQUEST['filters'],true);
  519. $groupOp = $filters['groupOp'];
  520. $rules = $filters['rules'];
  521. $where_condition = array();
  522. foreach ($rules as $rule){
  523. $field = $rule['field'];
  524. $op= $rule['op'];
  525. $input = $rule['data'];
  526. //$
  527. $where_condition[$field] = $input;
  528. }
  529. $products = $this->Product->getAllFlattenedByLike($where_condition,true);
  530. }
  531. else {
  532. $products = $this->Product->getAllFlattened(true);
  533. }
  534. header("Content-type: application/octet-stream");
  535. header("Content-description: File Transfer");
  536. header("Content-disposition: attachment; filename=\"thefilename.csv\"");
  537. header("Pragma: public");
  538. header("Cache-control: max-age=0");
  539. header("Expires: 0");
  540. echo $products;
  541. }
  542. function loadInventory (){
  543. $data['barcodes'] = json_encode($this->populateBarcodesForStock());
  544. $this->load->view('products/product_stock_details',$data);
  545. }
  546. function populateProductStocksInGrid (){
  547. //session_start();
  548. $searchOn = strip($_REQUEST['_search']);
  549. $page = $_REQUEST['page'];
  550. $limit = $_REQUEST['rows'];
  551. $sidx = $_REQUEST['sidx'];
  552. $sord = $_REQUEST['sord'];
  553. $productsdata = array();
  554. $count = $this->Product_stock->totalNoOfRows();
  555. if( $count > 0 && $limit > 0) {
  556. $total_pages = ceil($count/$limit);
  557. } else {
  558. $total_pages = 0;
  559. }
  560. if ($page > $total_pages) $page=$total_pages;
  561. $start = $limit*$page - $limit;
  562. // if for some reasons start position is negative set it to 0
  563. // typical case is that the user type 0 for the requested page
  564. if($start <0) $start = 0;
  565. $clauses = array('orderBy'=>$sidx,'orderDir'=>$sord,'startLimit'=>$start,'limit'=>$limit);
  566. $data['total'] = $total_pages;
  567. $data['page'] = $page;
  568. $data['records'] = $count;
  569. if($searchOn=='true') {
  570. $filters = json_decode($_REQUEST['filters'],true);
  571. $groupOp = $filters['groupOp'];
  572. $rules = $filters['rules'];
  573. $where_condition = array();
  574. foreach ($rules as $rule){
  575. $field = $rule['field'];
  576. $op= $rule['op'];
  577. $input = $rule['data'];
  578. $where_condition[$field] = $input;
  579. }
  580. $products = $this->Product_stock->getAllByLike($where_condition,false,$clauses,1);
  581. }
  582. else {
  583. $products = $this->Product_stock->getAll(false,$clauses,1);
  584. }
  585. //$dp['system_name']
  586. foreach ($products as $dp){
  587. array_push($productsdata, array('id'=> $dp['id'],'dprow' => array($dp['product_id'],$dp['barcode'],$dp['system_name'],$dp['stock'],0,$dp['isactive'])));
  588. }
  589. $data['productdata'] = $productsdata;
  590. echo json_encode($data);
  591. }
  592. function updateStocks (){
  593. $id = strip($_REQUEST['id']);
  594. $oper = strip($_REQUEST['oper']);
  595. $stockAdd = strip($_REQUEST['stock_add']);
  596. $lastaction = 'added';
  597. $stockLevel = $this->Product_stock->getStockLevel('id',$id);
  598. $stockLevel += $stockAdd;
  599. $stock_data = array ('stock' => $stockLevel,'stock_added'=>$stockAdd,'last_action'=>$lastaction);
  600. $this->Product_stock->update($id,$stock_data);
  601. }
  602. function updateStocksByBarcode (){
  603. $barcode = strip($_REQUEST['barcode']);
  604. //$oper = strip($_REQUEST['oper']);
  605. $stockAdd = strip($_REQUEST['stock_add']);
  606. $lastaction = 'added';
  607. $stockLevel = $this->Product_stock->getStockLevel('barcode',$barcode);
  608. $stockLevel += $stockAdd;
  609. $stock_data = array ('stock' => $stockLevel,'p'=>$stockAdd,'last_action'=>$lastaction);
  610. $this->Product_stock->updateByBarcode($barcode,$stock_data);
  611. }
  612. function printBarcode(){
  613. $id =$_REQUEST['id'];
  614. $barcodeArray = $this->Product->getValues(array("id"=>$id),array("barcode"));
  615. $barcode = $barcodeArray[0]["barcode"];
  616. $filename = IMAGEPATH.'temp/barcodefinal.png';
  617. $this->load->helper('file');
  618. delete_files($filename);
  619. if ( ! write_file($filename, ''))
  620. {
  621. echo 'Unable to write the file';
  622. }
  623. $this->barcode->generateBarcode($barcode,$filename);
  624. $data['filename'] = $filename;
  625. $this->load->view("utilities/barcode",$data);
  626. }
  627. function addcategoryToProduct () {
  628. log_message('debug','productop '. $_REQUEST['productOp']);
  629. log_message('debug','productIp '.$_REQUEST['productIp']);
  630. log_message('debug', 'category '.$_REQUEST['category']);
  631. $categoryArray = $_REQUEST['category'];
  632. if (!empty($categoryArray)){
  633. foreach($categoryArray as $category){
  634. $this->Category->saveProductCategoryMapping($category,$_REQUEST['productOp']);
  635. }
  636. }
  637. }
  638. function populateProductCategoryMapping () {
  639. $searchOn = strip($_REQUEST['_search']);
  640. $page = $_REQUEST['page'];
  641. $limit = $_REQUEST['rows'];
  642. $sidx = $_REQUEST['sidx'];
  643. $sord = $_REQUEST['sord'];
  644. $productcategorydata = array();
  645. $count = $this->Category->totalNoOfRowsInProductCategoryMapping();
  646. if( $count > 0 && $limit > 0) {
  647. $total_pages = ceil($count/$limit);
  648. } else {
  649. $total_pages = 0;
  650. }
  651. if ($page > $total_pages) $page=$total_pages;
  652. $start = $limit*$page - $limit;
  653. // if for some reasons start position is negative set it to 0
  654. // typical case is that the user type 0 for the requested page
  655. if($start <0) $start = 0;
  656. $clauses = array('orderBy'=>$sidx,'orderDir'=>$sord,'startLimit'=>$start,'limit'=>$limit);
  657. $data['total'] = $total_pages;
  658. $data['page'] = $page;
  659. $data['records'] = strval($count);
  660. if($searchOn=='true') {
  661. $filters = json_decode($_REQUEST['filters'],true);
  662. $groupOp = $filters['groupOp'];
  663. $rules = $filters['rules'];
  664. $like_condition = array();
  665. foreach ($rules as $rule){
  666. $field = $rule['field'];
  667. $op= $rule['op'];
  668. $input = $rule['data'];
  669. $like_condition[$field] = $input;
  670. }
  671. $productcategories = $this->Category->getAllProductCategoryMapping(false,null,$clauses,$like_condition);
  672. }
  673. else {
  674. $productcategories = $this->Category->getAllProductCategoryMapping(false,null,$clauses);
  675. }
  676. foreach ($productcategories as $dp){
  677. array_push($productcategorydata, array('id'=> $dp['id'],'dprow' => array($this->Product->getByProductId($dp['product_id'])->product_name,$dp['barcode'],$dp['product_id'],$dp['category_id'],$dp['category_name'])));
  678. }
  679. $data['productcategorydata'] = $productcategorydata;
  680. echo json_encode($data);
  681. }
  682. function loadProductCategoryMapping () {
  683. $productArray = $this->Product->getValues();
  684. foreach ($productArray as $product){
  685. //$data = array('label' => $model['model_name'], 'value' => $model['id']);
  686. //array_push($autoData,$data);
  687. $id = $product['id'];
  688. $name = $product['product_name'].'->'.$product['barcode'];
  689. $productOptions.="<OPTION VALUE=\"$id\">".$name;
  690. }
  691. $data['productOptions'] = $productOptions;
  692. $this->load->view("products/product_category_grid",$data);
  693. }
  694. function loadProductSupplierMapping () {
  695. $productArray = $this->Product->getValues();
  696. foreach ($productArray as $product){
  697. //$data = array('label' => $model['model_name'], 'value' => $model['id']);
  698. //array_push($autoData,$data);
  699. $id = $product['id'];
  700. $name = $product['product_name'].'->'.$product['barcode'];
  701. $productOptions.="<OPTION VALUE=\"$id\">".$name;
  702. }
  703. $data['productOptions'] = $productOptions;
  704. $data['supplierOptions'] = populateSuppliers();
  705. $this->load->view("products/product_supplier_grid",$data);
  706. }
  707. function deleteProductCategoryMapping(){
  708. $idAraay = $_REQUEST['id'];
  709. //$idAraay = explode(",", $ids);
  710. //log_message('debug', explode(",", $idAraay));
  711. foreach ($idAraay as $id){
  712. $this->Category->deleteProductCategoryMapping($id);
  713. }
  714. }
  715. function deleteSupplierProductMapping(){
  716. $idAraay = $_REQUEST['id'];
  717. //$idAraay = explode(",", $ids);
  718. //log_message('debug', explode(",", $idAraay));
  719. foreach ($idAraay as $id){
  720. $this->Supplier->deleteSupplierMapping($id);
  721. }
  722. }
  723. function addSupplierToProduct () {
  724. log_message('debug','productop '. $_REQUEST['productOp']);
  725. log_message('debug','productIp '.$_REQUEST['productIp']);
  726. $supplierArray = $_REQUEST['supplier'];
  727. if (!empty($supplierArray)){
  728. $this->Supplier->createProductSupplierMapping($_REQUEST['productOp'],$supplierArray);
  729. }
  730. }
  731. }
  732. ?>