PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/magehelp/application/controllers/invoice_1.php

https://bitbucket.org/jit_bec/shopifine
PHP | 398 lines | 295 code | 83 blank | 20 comment | 32 complexity | f6e3c68298983020e18cc36dfc3a34fe MD5 | raw file
Possible License(s): LGPL-3.0
  1. <?php
  2. require_once ("secure_area.php");
  3. class Invoice extends Secure_area
  4. {
  5. private $user;
  6. private $username;
  7. function __construct()
  8. {
  9. parent::__construct('invoice');
  10. $this->load->model('Invoice_master');
  11. $this->load->model('Invoice_item');
  12. $this->load->model('Delivery_vehicle');
  13. $this->load->model('Delivery_point');
  14. $this->load->model('Shipment_master');
  15. $this->user= $this->Employee->get_logged_in_employee_info();
  16. $this->username = $this->user->last_name." ".$this->user->first_name;
  17. $param = array('user' => 'admin');
  18. $this->load->library('Acl',$param);
  19. // $this->load->library('sale_lib');
  20. }
  21. function unsetSession(){
  22. session_start();
  23. if (isset($_SESSION['invoiceList'])){
  24. unset($_SESSION['invoiceList']);
  25. }
  26. }
  27. function index()
  28. {
  29. $callbackUrl = "http://localhost/opensourcepos/index.php/invoice";
  30. $temporaryCredentialsRequestUrl = "http://localhost/magento/oauth/initiate?oauth_callback=" . urlencode($callbackUrl);
  31. $adminAuthorizationUrl = 'http://localhost/magento/admin/oauth_authorize';
  32. $accessTokenRequestUrl = 'http://localhost/magento/oauth/token';
  33. $apiUrl = 'http://localhost/magento/api/rest';
  34. $consumerKey = '93jffpt61prd21be2r1ioxwok613z38m';
  35. $consumerSecret = 'jqhdamkxjdch6neygjgm8luep9hbcpe6';
  36. session_start();
  37. if (isset($_POST['invoice_number'])){
  38. $_SESSION['invoice_number'] = $_POST['invoice_number'];
  39. }
  40. if (!isset($_GET['oauth_token']) && isset($_SESSION['state']) && $_SESSION['state'] == 1) {
  41. $_SESSION['state'] = 0;
  42. }
  43. try {
  44. $authType = ($_SESSION['state'] == 2) ? OAUTH_AUTH_TYPE_AUTHORIZATION : OAUTH_AUTH_TYPE_URI;
  45. $oauthClient = new OAuth($consumerKey, $consumerSecret, OAUTH_SIG_METHOD_PLAINTEXT, $authType);
  46. $oauthClient->enableDebug();
  47. if (!isset($_GET['oauth_token']) && !$_SESSION['state']) {
  48. $requestToken = $oauthClient->getRequestToken($temporaryCredentialsRequestUrl);
  49. $_SESSION['secret'] = $requestToken['oauth_token_secret'];
  50. $_SESSION['state'] = 1;
  51. redirect($adminAuthorizationUrl . '?oauth_token=' . $requestToken['oauth_token']);
  52. exit;
  53. } else if ($_SESSION['state'] == 1) {
  54. $oauthClient->setToken($_GET['oauth_token'], $_SESSION['secret']);
  55. $accessToken = $oauthClient->getAccessToken($accessTokenRequestUrl);
  56. $_SESSION['state'] = 2;
  57. $_SESSION['token'] = $accessToken['oauth_token'];
  58. $_SESSION['secret'] = $accessToken['oauth_token_secret'];
  59. redirect($callbackUrl);
  60. exit;
  61. } else {
  62. $oauthClient->setToken($_SESSION['token'], $_SESSION['secret']);
  63. //$resourceUrl = "$apiUrl/products?type=rest";
  64. //$resourceUrl = "$apiUrl/orders/100000064/items?type=rest";
  65. $invoiceId = $_SESSION['invoice_number'];
  66. unset($_SESSION['invoice_number']) ;//now unset
  67. if ($invoiceId){
  68. $resourceUrl = "$apiUrl/invoice/".$invoiceId."/items?type=rest";
  69. $oauthClient->fetch($resourceUrl);
  70. $response = $oauthClient->getLastResponse();
  71. }
  72. }
  73. } catch (OAuthException $e) {
  74. print_r($e);
  75. }
  76. $data['invoiceId'] = $invoiceId;
  77. $data['response'] = $response;
  78. $this->_processInvoiceData($invoiceId,$response);
  79. $this->load->view("invoice/invoice_items",$data);
  80. }
  81. function addInvoice(){
  82. session_start();
  83. if (!isset($_SESSION['invoiceList'])){
  84. $_SESSION['invoiceList']= array();
  85. }
  86. //unset($_SESSION['invoiceList']);
  87. $len = sizeof($_SESSION['invoiceList']);
  88. if (isset($_POST['invoiceId'])){
  89. $invoiceId = $_POST['invoiceId'];
  90. $isMatched = $_POST['isMatched'];
  91. $comments = $_POST['comments'];
  92. $jsonItems = $_POST['jsonItems'];
  93. $skuItems = json_decode($jsonItems,true);
  94. $orderId = $_POST['orderId'];
  95. //if ($currentStatus!='readyforshipping' && $currentStatus!='packed'){
  96. $_SESSION['invoiceList'][$len]= array('invoiceId'=> $invoiceId, 'isMatched' => $isMatched,'comments'=>$comments);
  97. $inv_data = array
  98. (
  99. 'status'=>'packed',
  100. 'comments'=>$comments,
  101. 'matched'=>$isMatched,
  102. 'last_updated_by'=>$this->username,
  103. 'packed_by'=>$this->username,
  104. 'magento_order_id'=>$orderId
  105. );
  106. $this->Invoice_master->update($invoiceId,$inv_data);
  107. $this->Invoice_item->updateMultiple($skuItems,$invoiceId);
  108. //}
  109. }
  110. $data['invoiceListSession'] =$_SESSION['invoiceList'];
  111. $this->load->view("invoice/add_invoice",$data);
  112. }
  113. function ship(){
  114. session_start();
  115. if (!isset($_SESSION['invoiceList'])){
  116. $_SESSION['invoiceList']= array();
  117. }
  118. $len = sizeof($_SESSION['invoiceList']);
  119. if (isset($_POST['invoiceId'])){
  120. //$this->load->h
  121. $invoiceId = $_POST['invoiceId'];
  122. $orderId = $_POST['orderId'];
  123. $isMatched = $_POST['isMatched'];
  124. $comments = $_POST['comments'];
  125. $jsonItems = $_POST['jsonItems'];
  126. $skuItems = json_decode($jsonItems,true);
  127. /*check if this is coming because of page refresh or back button: Process only if it does not
  128. exists in seesion.*/
  129. $this->load->helper('common_helper');
  130. $results = check_if_exists_in_array($_SESSION['invoiceList'],'invoiceId',$invoiceId);
  131. if (sizeof($results) == 0){
  132. $_SESSION['invoiceList'][$len]= array('invoiceId'=> $invoiceId, 'isMatched' => $isMatched,'comments'=>$comments);
  133. $inv_data = array
  134. (
  135. 'status'=>'packed',
  136. 'comments'=>$comments,
  137. 'matched'=>$isMatched,
  138. 'last_updated_by'=>$this->username,
  139. 'packed_by'=>$this->username,
  140. 'magento_order_id'=>$orderId
  141. );
  142. $this->db->trans_start();
  143. $this->Invoice_master->update($invoiceId,$inv_data);
  144. $this->Invoice_item->updateMultiple($skuItems,$invoiceId);
  145. $this->db->trans_complete();
  146. if ($this->db->trans_status() === FALSE)
  147. {
  148. //echo $this->db->_error_message();
  149. die($invoiceId .' could not be packed. Please check log ');
  150. }
  151. $invoiceList = $_SESSION['invoiceList'];
  152. }
  153. }
  154. /*check if this is coming because of page refresh or back button: Process only if it does not
  155. exists in seesion.*/
  156. if (sizeof($results) == 0){
  157. foreach ($invoiceList as $invoice){
  158. //$invoiceMgr->updateInvoiceMaster($invoice['invoiceId'],'readyforshipping',$invoice['comments'],$invoice['isMatched']);
  159. $inv_data_ready = array
  160. (
  161. 'status'=>'readyforshipping',
  162. 'comments'=>$invoice['comments'],
  163. 'matched'=>$invoice['isMatched'],
  164. 'last_updated_by'=>$this->username,
  165. 'shipped_by'=>$this->username
  166. );
  167. $this->Invoice_master->update($invoice['invoiceId'],$inv_data_ready);
  168. }
  169. }
  170. $deliveryPoints = $this->Delivery_point->getAll();
  171. $deliveryVehicles = $this->Delivery_vehicle->getAll();
  172. $options="";
  173. $optionsVehicle = "";
  174. foreach($deliveryPoints as $deliveryPoint) {
  175. $id=$deliveryPoint["id"];
  176. $thing=$deliveryPoint["name"];
  177. $options.="<OPTION VALUE=\"$thing\">".$thing;
  178. }
  179. foreach($deliveryVehicles as $deliveryVehicle) {
  180. $id=$deliveryVehicle["id"];
  181. $reg=$deliveryVehicle["reg_number"];
  182. $optionsVehicle.="<OPTION VALUE=\"$reg\">".$reg;
  183. }
  184. $data['options']=$options;
  185. $data['optionsVehicle']=$optionsVehicle;
  186. $data['invoiceListSession'] =$_SESSION['invoiceList'];
  187. $this->load->view("invoice/shipment",$data);
  188. }
  189. function confirmation(){
  190. session_start();
  191. $success = false;
  192. $deliveryPoint = $_POST['deliveryPointDD'];
  193. $deliveryVehicle= $_POST['deliveryVehicleDD'];
  194. //$comments = $_POST['comments'];
  195. $invoiceIDsJSON = $_POST['invoiceIDsJSON'];
  196. $shippingArray = json_decode($invoiceIDsJSON,true);
  197. $this->db->trans_start();
  198. $trackingNumber = $this->_createTrackingNumber($deliveryVehicle, $deliveryPoint);
  199. $shipping_data = array
  200. (
  201. 'tracking_number'=>$trackingNumber,
  202. 'delivery_vehicle'=>$deliveryVehicle,
  203. 'delivery_point'=>$deliveryPoint
  204. );
  205. $this->Shipment_master->insert($shipping_data);
  206. foreach ($shippingArray as $shipping){
  207. $invoice_data_shipping = array(
  208. 'status' => 'shipped',
  209. 'shipping_tracking_number' => $trackingNumber
  210. );
  211. $this->Invoice_master->update($shipping['invoice_id'], $invoice_data_shipping);
  212. }
  213. $this->db->trans_complete();
  214. if ($this->db->trans_status() === FALSE)
  215. {
  216. //echo $this->db->_error_message();
  217. die( 'Shipping Failed.Please check log ');
  218. }
  219. else {
  220. $success = true;
  221. }
  222. $to = "abhijit.mazumder@gmail.com";
  223. if ($success){
  224. $subject = "Shipping Confirmattion Mail For $trackingNumber";
  225. $message = "Shipping Confirmed For ";
  226. foreach ($shippingArray as $shipping){
  227. $message = $message."Invoice Number ".$shipping['invoice_id'];
  228. }
  229. $message = $message."Delivery Vehicle Number ".$deliveryVehicle."Delivery Point ".$deliveryPoint;
  230. }
  231. else {
  232. $subject = "Shipping Faliure Mail For $trackingNumber";
  233. $message = "Shipping Failed For ";
  234. foreach ($shippingArray as $shipping){
  235. $message = $message."Invoice Number ".$shipping['invoice_id'];
  236. }
  237. }
  238. $from = "shopifine@localshopifine.com";
  239. $headers = "From:" . $from;
  240. mail($to,$subject,$message,$headers);
  241. $data['shippingArray'] = $shippingArray;
  242. $data['trackingNumber'] = $trackingNumber;
  243. $this->load->view('invoice/confirmation',$data);
  244. //TODO: if everything fine unset the invoice List in session
  245. }
  246. function _processInvoiceData($invoiceId,$response){
  247. if ($invoiceId){
  248. $invoice_items = json_decode($response,true);
  249. //$conn = $database->connection
  250. $skus;
  251. $status ="invoiced";
  252. $this->db->trans_start();
  253. //first make an entry in invoice master
  254. if (!$this->Invoice_master->exists($invoiceId)) { //insert
  255. $inv_data = array
  256. (
  257. 'magento_invoice_increment_id'=>$invoiceId,
  258. 'status'=>$status,
  259. 'created_at'=>date('Y-m-d H:i:s'),
  260. 'last_updated_by'=>$this->username
  261. );
  262. $this->Invoice_master->insert($inv_data);
  263. }
  264. foreach($invoice_items as $item){
  265. $sku = $item['sku'];
  266. if (!$skus[$sku]['inv_no']){
  267. $skus[$sku]['sku'] = $sku;
  268. $skus[$sku]['name'] = $item['name'];
  269. //var_dump($skus[$sku]['name']);
  270. $skus[$sku]['entity_id'] = $item['entity_id'];
  271. $skus[$sku]['inv_no']=1;
  272. }
  273. else{
  274. $skus[$sku]['inv_no'] = $skus[$sku]['inv_no'] + 1;
  275. }
  276. }
  277. foreach($skus as $sku){
  278. $magento_entity_id = $sku['entity_id'];
  279. $sku_no = $sku['sku'];
  280. $name = $sku['name'];
  281. $invoiced_number= $sku['inv_no'];
  282. if (!$this->Invoice_item->exists($invoiceId,$sku_no)){
  283. $inv_item_data = array
  284. (
  285. 'magento_entity_id'=>$magento_entity_id,
  286. 'sku'=>$sku_no,
  287. 'name'=>$name,
  288. 'invoice_id'=>$invoiceId,
  289. 'invoiced_number'=>$invoiced_number,
  290. 'created_at'=>date('Y-m-d H:i:s'),
  291. 'packed_by'=>$this->username
  292. );
  293. $this->Invoice_item->insert($inv_item_data);
  294. }
  295. }
  296. $this->db->trans_complete();
  297. if ($this->db->trans_status() === FALSE)
  298. {
  299. //echo $this->db->_error_message();
  300. die( 'Transaction Failed while inserting invoice records. Please check log');
  301. }
  302. }
  303. }
  304. function _createTrackingNumber($reg_number,$delivery_point){
  305. $offset = 100000;
  306. $lastId = $this->Shipment_master->last_insert_id();
  307. $trackingNumber = $offset + $lastId + 1;
  308. $vehicleId = $this->Delivery_vehicle->getId($reg_number);
  309. $trackingNumber = $trackingNumber.'00' + $vehicleId ;
  310. $pointId = $this->Delivery_point->getId($delivery_point);
  311. $trackingNumber = $trackingNumber.'00' + $pointId;
  312. return $trackingNumber;
  313. }
  314. }
  315. ?>