PageRenderTime 90ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/magehelp/application/controllers/impex.php

https://bitbucket.org/jit_bec/shopifine
PHP | 402 lines | 250 code | 71 blank | 81 comment | 24 complexity | 6d5a3d8608be7e1d3c19d40c452d959e MD5 | raw file
Possible License(s): LGPL-3.0
  1. <?php
  2. /*
  3. * To change this template, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. /**
  7. * Description of users
  8. *
  9. * @author abhijit
  10. */
  11. require_once ("secure_area.php");
  12. class Impex extends Secure_area {
  13. function __construct()
  14. {
  15. parent::__construct('impex','adminmenu');
  16. $this->load->model('Product_stock');
  17. }
  18. function index(){
  19. }
  20. public function importAttributeSets (){
  21. $status = $this->Attribute_set->importAttributeSets();
  22. log_message('debug', 'status of Attribute set import '.$status);
  23. }
  24. public function importCategories(){
  25. $status = $this->Category->importCategories();
  26. log_message('debug', 'status of category import '.$status);
  27. }
  28. public function importInvoices(){
  29. $status ="invoiced";
  30. $this->db->trans_start();
  31. $magentoInvoices = $this->Invoice_master->getUnprocessedInvoicesFromMagento();
  32. //first make an entry in invoice master
  33. //if (!$this->Invoice_master->exists($invoiceId)) { //insert
  34. foreach ($magentoInvoices as $inv){
  35. $inv_entity_id = $inv['entity_id'];
  36. $inv_increment_id = $inv['invoice_increment_id'];
  37. $order_id = $inv['order_id'];
  38. $order_increment_id = $inv['order_increment_id'];
  39. $inv_data = array
  40. (
  41. 'magento_invoice_increment_id'=>$inv_increment_id,
  42. 'magento_invoice_entity_id'=>$inv_entity_id,
  43. 'magento_order_increment_id'=>$order_increment_id,
  44. 'magento_order_id'=>$order_id,
  45. 'status'=>$status,
  46. 'created_at'=>date('Y-m-d H:i:s')
  47. );
  48. $this->Invoice_master->insert($inv_data);
  49. }
  50. $items = $this->Invoice_item->getUnprocessedInvoiceItemsFromMagento();
  51. foreach ($items as $item){
  52. $magento_entity_id = $item['entity_id'];
  53. $inv_entity_id = $item['parent_id'];
  54. $inv_increment_id = $item['increment_id'];
  55. $product_id = $item['product_id'];
  56. $type = $item['type_id'];
  57. $sku= $item['sku'];
  58. $name = $item['name'];
  59. $qty = $item['qty'];
  60. $inv_item_data = array
  61. (
  62. 'magento_entity_id'=>$magento_entity_id,
  63. 'magento_invoice_entity_id'=>$inv_entity_id,
  64. 'invoice_id' =>$inv_increment_id,
  65. 'sku'=>$sku,
  66. 'name'=>$name,
  67. 'type'=>$type,
  68. 'magento_product_id'=>$product_id,
  69. 'invoiced_number'=>$qty,
  70. 'created_at'=>date('Y-m-d H:i:s'),
  71. );
  72. $this->Invoice_item->insert($inv_item_data);
  73. }
  74. $this->db->trans_complete();
  75. if ($this->db->trans_status() === FALSE)
  76. {
  77. //echo $this->db->_error_message();
  78. //die( 'Transaction Failed while inserting invoice records. Please check log');
  79. }
  80. }
  81. function exportProductsMagmi (){
  82. $error = false;
  83. $result = $this->Product->getMagentoExportView();
  84. $resource = $this->Resource->getByResourceName('exportdir-impexconfig');
  85. $filename = $resource['relative_path_link'] . $this->Appconfig->get('create_product_file');
  86. // $filename = $result['relative_path_link'];
  87. $this->load->helper('file');
  88. //var_dump(($result));
  89. //delete_files($filename);
  90. //delete_files($filename);
  91. if ( ! write_file($filename,$result ,'w+'))
  92. {
  93. $error = true;
  94. }
  95. $data['mode'] = $this->Appconfig->get('create_product_mode');
  96. $data['profile'] = $this->Appconfig->get('create_product_profile');
  97. $data['filename'] = $filename;
  98. $data['error'] = $error;
  99. $this->load->view('impex/process_magento',$data);
  100. }
  101. function updateStocksInMagmi (){
  102. $error = "false";
  103. //$result = $this->Product->getMagentoExportView();
  104. $resource = $this->Resource->getByResourceName('exportdir-impexconfig');
  105. $filename = $resource['relative_path_link'] . $this->Appconfig->get('update_stock_csv');
  106. // $filename = $result['relative_path_link'];
  107. $this->load->helper('file');
  108. //var_dump(($result));
  109. //delete_files($filename);
  110. //delete_files($filename);
  111. // if ( ! write_file($filename,$result ,'w+'))
  112. // {
  113. // $error = true;
  114. // }
  115. if ($this->Product_stock->totalNoOfRowsInHistory (array('sync_status'=>0,'update_status' =>'inprocess','initiated_by'=>$this->User->get_logged_in_employee_info()->person_id))>0){
  116. $error = "true";
  117. }
  118. $data['mode'] = $this->Appconfig->get('update_stock_mode');
  119. $data['profile'] = $this->Appconfig->get('update_stock_profile');
  120. $data['server_url'] = $this->Appconfig->get('server_url');
  121. $data['filename'] = $filename;
  122. $data['error'] = $error;
  123. $this->load->view('impex/update_stock',$data);
  124. }
  125. function createExportStockFile (){
  126. $idArray = $_REQUEST['selected'];
  127. if (!empty($idArray)){
  128. $in_where['field_name']='id';
  129. $in_where['value_array']=$idArray;
  130. }
  131. $resource = $this->Resource->getByResourceName('exportdir-impexconfig');
  132. $products = $this->Product_stock->getAll(true,null,1,null,array('barcode as sku',"CONCAT('+',balance_to_update) as qty"),null,$in_where);
  133. //var_dump($products);
  134. $filename = $resource['relative_path_link'] . $this->Appconfig->get('update_stock_csv');
  135. $this->load->helper('file');
  136. //var_dump(($result));
  137. //delete_files($filename);
  138. //delete_files($filename);
  139. if ( ! write_file($filename,$products ,'w+'))
  140. {
  141. $error = true;
  142. $data['status'] = 'error';
  143. $data['message'] = 'The File' .$filename.' Could Not Be Created';
  144. }
  145. else {
  146. $data['status'] = 'success';
  147. $data['message'] = 'The File' .$filename.' Successfully Created';
  148. }
  149. echo json_encode($data);
  150. }
  151. function syncNeeeded(){
  152. $idArray = explode(',',$_REQUEST['selected']);
  153. //$idArray = explode(',','13,15,17,19');
  154. //var_dump($idArray);
  155. if (!empty($idArray)){
  156. $in_where['field_name']='id';
  157. $in_where['value_array']=$idArray;
  158. $products = $this->Product_stock->getAll(false,null,1,null,array('barcode','product_id',"balance_to_update"),null,$in_where);
  159. foreach ($products as $index=>$array){
  160. //array_push($product,array('initiated_by'=>$this->User->get_logged_in_employee_info()->person_id));
  161. //$product['initiated_by']=$this->User->get_logged_in_employee_info()->person_id;
  162. $products[$index]['initiated_by']=$this->User->get_logged_in_employee_info()->person_id;
  163. }
  164. //var_dump($products);
  165. $this->Product_stock->insertStockUpdateHistoryBatch($products);
  166. }
  167. }
  168. // function createExportTest (){
  169. // //$idArray = $_REQUEST['selected'];
  170. // $products = array();
  171. // array_push($products, "1");
  172. // array_push($products, "2");
  173. // array_push($products, "3");
  174. // if (!empty($idArray)){
  175. // $in_where['field_name']='id';
  176. // $in_where['value_array']=$idArray;
  177. // }
  178. // $resource = $this->Resource->getByResourceName('exportdir-impexconfig');
  179. //// /
  180. // foreach($products as $p){
  181. // $result .=$p.","."success,\n";
  182. // }
  183. // $filename = $resource['relative_path_link'] . 'test';
  184. // $this->load->helper('file');
  185. //
  186. // //var_dump(($result));
  187. // //delete_files($filename);
  188. // //delete_files($filename);
  189. // if ( ! write_file($filename,$result ,'w+'))
  190. // {
  191. // $error = true;
  192. // }
  193. // //echo 'test';
  194. // }
  195. function doSync(){
  196. $file = fopen('/var/www/magehelp/sku_status.csv', 'r+');
  197. while (($line = fgetcsv($file)) !== FALSE) {
  198. //$line is an array of the csv elements
  199. if ($line[1]=='success'){
  200. $this->db->trans_start();
  201. $result = $this->Product_stock->getHistoryData(array('barcode'=>$line[0],'update_status'=>'inprocess','sync_status'=>0),array('balance_to_update'));
  202. $balance = $result->balance_to_update;
  203. log_message("debug","baance ".$balance);
  204. $this->Product_stock->updateStockUpdateHistory(array('barcode'=>$line[0],'update_status'=>'inprocess'),array('update_status'=>'success','sync_status'=>1));
  205. $this->Product_stock->updateGeneral(array('barcode'=>$line[0]),null,array('balance_to_update'=>'balance_to_update -' .$balance));
  206. $this->db->trans_complete();
  207. }
  208. else if ($line[1]=='failure'){
  209. $this->Product_stock->updateStockUpdateHistory(array('barcode'=>$line[0],'update_status'=>'inprocess'),array('update_status'=>'failure','sync_status'=>1));
  210. }
  211. }
  212. ftruncate($file, 0);
  213. fclose($file);
  214. $data['status'] = 'success';
  215. echo json_encode($data);
  216. }
  217. function populateProductsStocksToExport(){
  218. $page = $_REQUEST['page'];
  219. $limit = $_REQUEST['rows'];
  220. $sidx = $_REQUEST['sidx'];
  221. $sord = $_REQUEST['sord'];
  222. $where['balance_to_update >'] = 0;
  223. $productsdata = array();
  224. $count = $this->Product_stock->totalNoOfRows($where);
  225. if( $count > 0 && $limit > 0) {
  226. $total_pages = ceil($count/$limit);
  227. } else {
  228. $total_pages = 0;
  229. }
  230. if ($page > $total_pages) $page=$total_pages;
  231. $start = $limit*$page - $limit;
  232. // if for some reasons start position is negative set it to 0
  233. // typical case is that the user type 0 for the requested page
  234. if($start <0) $start = 0;
  235. $clauses = array('orderBy'=>$sidx,'orderDir'=>$sord,'startLimit'=>$start,'limit'=>$limit);
  236. $data['total'] = $total_pages;
  237. $data['page'] = $page;
  238. $data['records'] = $count;
  239. $products = $this->Product_stock->getAll(false,$clauses,1,$where);
  240. //$dp['system_name']
  241. foreach ($products as $dp){
  242. array_push($productsdata, array('id'=> $dp['id'],'dprow' => array($dp['barcode'],$dp['product_id'],$dp['balance_to_update'])));
  243. }
  244. $data['productstockdata'] = $productsdata;
  245. echo json_encode($data);
  246. }
  247. function populateProductsPrice(){
  248. $page = $_REQUEST['page'];
  249. $limit = $_REQUEST['rows'];
  250. $sidx = $_REQUEST['sidx'];
  251. $sord = $_REQUEST['sord'];
  252. // $where['balance_to_update >'] = 0;
  253. $productsdata = array();
  254. $count = $this->Product_price->totalNoOfRows($where);
  255. if( $count > 0 && $limit > 0) {
  256. $total_pages = ceil($count/$limit);
  257. } else {
  258. $total_pages = 0;
  259. }
  260. if ($page > $total_pages) $page=$total_pages;
  261. $start = $limit*$page - $limit;
  262. // if for some reasons start position is negative set it to 0
  263. // typical case is that the user type 0 for the requested page
  264. if($start <0) $start = 0;
  265. $clauses = array('orderBy'=>'product_id','orderDir'=>$sord,'startLimit'=>$start,'limit'=>$limit);
  266. $data['total'] = $total_pages;
  267. $data['page'] = $page;
  268. $data['records'] = $count;
  269. $columns = array('product_id','barcode','product_name','inv_total_cost',
  270. 'total_balance','inv_cost_price','vat','sales_tax','margin_type','margin_value',"CASE margin_type WHEN 'none' THEN ROUND(`inv_cost_price`*(1+(`vat`/100)),2) WHEN 'amount' THEN `inv_cost_price` + ROUND(`inv_cost_price`*(`vat`/100),2) + `margin_value`
  271. WHEN 'percentage' THEN ROUND((`inv_cost_price` + `inv_cost_price`*(`vat`/100))*(1+(`margin_value`/100)),2 )
  272. ELSE 0 END 'calculated'");
  273. //'`inv_cost_price`*`vat` as calculated');
  274. $products = $this->Product_price->getAll(false,$where,$columns,$clauses);
  275. //var_dump($products);
  276. //$dp['system_name']
  277. foreach ($products as $dp){
  278. array_push($productsdata, array('product_id'=> $dp['product_id'],'dprow' => array($dp['barcode'],$dp['product_name'],$dp['inv_total_cost']
  279. ,$dp['total_balance'],$dp['inv_cost_price'],$dp['vat']
  280. ,/*$dp['sales_tax'],*/$dp['margin_type'],$dp['margin_value'],$dp['calculated']
  281. )));
  282. }
  283. $data['productstockdata'] = $productsdata;
  284. echo json_encode($data);
  285. }
  286. function dbprefix (){
  287. var_dump ($this->db->dbprefix);
  288. }
  289. function updatePriceInMagmi (){
  290. $data['mode'] = $this->Appconfig->get('update_price_mode');
  291. $data['profile'] = $this->Appconfig->get('update_price_profile');
  292. $data['server_url'] = $this->Appconfig->get('server_url');
  293. $data['filename'] = $filename;
  294. $this->load->view('impex/update_price',$data);
  295. }
  296. function createExportPriceFile (){
  297. $idArray = $_REQUEST['selected'];
  298. if (!empty($idArray)){
  299. $in_where['field_name']='product_id';
  300. $in_where['value_array']=$idArray;
  301. }
  302. $resource = $this->Resource->getByResourceName('exportdir-impexconfig');
  303. // $columns = array('product_id','barcode','product_name','inv_total_cost',
  304. // 'total_balance','inv_cost_price','vat','sales_tax','margin_type','margin_value',"CASE margin_type WHEN 'none' THEN ROUND(`inv_cost_price`*`vat`,2) WHEN 'amount' THEN `inv_cost_price` + ROUND(`inv_cost_price`*(`vat`/100),2) + `margin_value`
  305. //WHEN 'percentage' THEN ROUND((`inv_cost_price` + `inv_cost_price`*(`vat`/100))*(1+(`margin_value`/100)),2 )
  306. //ELSE 0 END 'calculated'");
  307. $columns = array('barcode as sku','inv_cost_price as cost',"CASE margin_type WHEN 'none' THEN ROUND(`inv_cost_price`*(1+(`vat`/100)),2) WHEN 'amount' THEN `inv_cost_price` + ROUND(`inv_cost_price`*(`vat`/100),2) + `margin_value`
  308. WHEN 'percentage' THEN ROUND((`inv_cost_price` + `inv_cost_price`*(`vat`/100))*(1+(`margin_value`/100)),2 )
  309. ELSE 0 END 'price'");
  310. //'`inv_cost_price`*`vat` as calculated');
  311. $products = $this->Product_price->getAll(true,null,$columns,null,null,$in_where);
  312. //var_dump($products);
  313. $filename = $resource['relative_path_link'] . $this->Appconfig->get('update_price_csv');
  314. $this->load->helper('file');
  315. //var_dump(($result));
  316. //delete_files($filename);
  317. //delete_files($filename);
  318. if ( ! write_file($filename,$products ,'w+'))
  319. {
  320. $error = true;
  321. $data['status'] = 'error';
  322. $data['message'] = 'The File' .$filename.' Could Not Be Created';
  323. }
  324. else {
  325. $data['status'] = 'success';
  326. $data['message'] = 'The File' .$filename.' Successfully Created';
  327. }
  328. echo json_encode($data);
  329. }
  330. }
  331. ?>