PageRenderTime 44ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/opensourcepos/application/controllers/invoice.php

https://bitbucket.org/jit_bec/shopifine
PHP | 452 lines | 337 code | 94 blank | 21 comment | 34 complexity | 07dfa62821f43a81ee2cfa03c76ede2d 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->load->model('acl/User','Userview');
  16. $this->user= $this->Userview->get_logged_in_employee_info();
  17. $this->username = $this->user->last_name." ".$this->user->first_name;
  18. $param = array('user' => $this->user->username);
  19. $this->load->library('acl-library/Acl',$param,'Acl');
  20. }
  21. function index()
  22. {
  23. $users = $this->Userview->getAllUsersByRole(null,array('field_name'=>'role_name',array('Everyone','Guest')));
  24. foreach($users as $user) {
  25. $user_id=$user["person_id"];
  26. $username=$user["username"];
  27. //$value = $denom["denom_json"];
  28. if (!empty($user_id)){
  29. $userOptions.="<OPTION VALUE=\"$user_id\">".$username;
  30. }
  31. }
  32. $data['userOptions'] = $userOptions;
  33. $this->load->view("invoice/invoice_list",$data);
  34. }
  35. function packInvoice(){
  36. $id = $_REQUEST['id'];
  37. $data['invoice_id']= $id;
  38. $inv_data = array
  39. (
  40. 'owner_id'=>$this->user->person_id,
  41. 'last_updated_by'=>$this->username,
  42. );
  43. $where_clause_array= array('magento_invoice_entity_id'=>$id);
  44. $this->Invoice_master->update($where_clause_array,$inv_data);
  45. $this->load->view("invoice/pack_invoice",$data);
  46. }
  47. function populateInvoiceItems(){
  48. $invoiceid = $_REQUEST['invoiceId'];
  49. $page = $_REQUEST['page'];
  50. $limit = $_REQUEST['rows'];
  51. $sidx = $_REQUEST['sidx'];
  52. $sord = $_REQUEST['sord'];
  53. $whereClause = array();
  54. if (!empty($invoiceid)){
  55. $whereClause['magento_invoice_entity_id'] = $invoiceid;
  56. }
  57. $whereClause['type'] = 'simple';
  58. //standard response parameters
  59. $mfrsdata = array();
  60. $count = $this->Invoice_item->totalNoOfRows($whereClause);
  61. if( $count > 0 && $limit > 0) {
  62. $total_pages = ceil($count/$limit);
  63. } else {
  64. $total_pages = 0;
  65. }
  66. if ($page > $total_pages) $page=$total_pages;
  67. $start = $limit*$page - $limit;
  68. // if for some reasons start position is negative set it to 0
  69. // typical case is that the user type 0 for the requested page
  70. if($start <0) $start = 0;
  71. $clauses = array('orderBy'=>$sidx,'orderDir'=>$sord,'startLimit'=>$start,'limit'=>$limit);
  72. $data['total'] = $total_pages;
  73. $data['page'] = $page;
  74. $data['records'] = $count;
  75. $mfrs = $this->Invoice_item->getAll(false,$whereClause,$clauses);
  76. foreach ($mfrs as $dp){
  77. array_push($mfrsdata, array('id'=> $dp['magento_entity_id'],'dprow' => array($dp['sku'],$dp['name'],$dp['invoiced_number'],$dp['packed_number'])));
  78. }
  79. $data['invoicedata'] = $mfrsdata;
  80. echo json_encode($data);
  81. }
  82. function completePacking(){
  83. //
  84. $invoiceId = $_POST['invoiceId'];
  85. $isMatched = $_POST['isMatched'];
  86. $comments = $_POST['comments'];
  87. $jsonItems = $_POST['packedJson'];
  88. $items = json_decode($jsonItems,true);
  89. $inv_data = array
  90. (
  91. 'status'=>'packed',
  92. 'comments'=>$comments,
  93. 'matched'=>$isMatched,
  94. 'last_updated_by'=>$this->username,
  95. 'packed_by'=>$this->username,
  96. );
  97. $where_clause_array= array('magento_invoice_entity_id'=>$invoiceId);
  98. $this->Invoice_master->update($where_clause_array,$inv_data);
  99. $this->Invoice_item->updateMultipleByEntityId($items);
  100. //$data['invoiceListSession'] =$_SESSION['invoiceList'];
  101. $this->load->view("invoice/invoice_list");
  102. }
  103. function ship(){
  104. $inv_data_ready = array
  105. (
  106. 'status'=>'readyforshipping',
  107. 'last_updated_by'=>$this->username,
  108. 'shipped_by'=>$this->username
  109. );
  110. $where_clause_array = array('owner_id'=>$this->user->person_id);
  111. $where_clause_array['status'] ='packed';
  112. $this->Invoice_master->update($where_clause_array,$inv_data_ready);
  113. $deliveryPoints = $this->Delivery_point->getAll();
  114. $deliveryVehicles = $this->Delivery_vehicle->getAll();
  115. $options="";
  116. $optionsVehicle = "";
  117. foreach($deliveryPoints as $deliveryPoint) {
  118. $id=$deliveryPoint["id"];
  119. $thing=$deliveryPoint["name"];
  120. $options.="<OPTION VALUE=\"$thing\">".$thing;
  121. }
  122. foreach($deliveryVehicles as $deliveryVehicle) {
  123. $id=$deliveryVehicle["id"];
  124. $reg=$deliveryVehicle["reg_number"];
  125. $optionsVehicle.="<OPTION VALUE=\"$reg\">".$reg;
  126. }
  127. $data['options']=$options;
  128. $data['optionsVehicle']=$optionsVehicle;
  129. $this->load->view("invoice/shipment",$data);
  130. }
  131. function confirmation(){
  132. $deliveryPoint = $_POST['deliveryPointDD'];
  133. $deliveryVehicle= $_POST['deliveryVehicleDD'];
  134. $invoiceIDsJSON = $_POST['selectedInv'];
  135. $shippingArray = json_decode($invoiceIDsJSON,true);
  136. $this->db->trans_start();
  137. $trackingNumber = $this->_createTrackingNumber($deliveryVehicle, $deliveryPoint);
  138. $shipping_data = array
  139. (
  140. 'tracking_number'=>$trackingNumber,
  141. 'delivery_vehicle'=>$deliveryVehicle,
  142. 'delivery_point'=>$deliveryPoint
  143. );
  144. $this->Shipment_master->insert($shipping_data);
  145. foreach ($shippingArray as $shipping){
  146. $invoice_data_shipping = array(
  147. 'status' => 'shipped',
  148. 'shipping_tracking_number' => $trackingNumber
  149. );
  150. $where_clause = array('magento_invoice_entity_id' =>$shipping);
  151. $this->Invoice_master->update($where_clause, $invoice_data_shipping);
  152. }
  153. $this->db->trans_complete();
  154. if ($this->db->trans_status() === FALSE)
  155. {
  156. //echo $this->db->_error_message();
  157. die( 'Shipping Failed.Please check log ');
  158. }
  159. else {
  160. $success = true;
  161. }
  162. $to = "abhijit.mazumder@gmail.com";
  163. if ($success){
  164. $subject = "Shipping Confirmattion Mail For $trackingNumber";
  165. $message = "Shipping Confirmed For ";
  166. foreach ($shippingArray as $shipping){
  167. $message = $message."Invoice Number ".$shipping['invoice_id'];
  168. }
  169. $message = $message."Delivery Vehicle Number ".$deliveryVehicle."Delivery Point ".$deliveryPoint;
  170. }
  171. else {
  172. $subject = "Shipping Faliure Mail For $trackingNumber";
  173. $message = "Shipping Failed For ";
  174. foreach ($shippingArray as $shipping){
  175. $message = $message."Invoice Number ".$shipping['invoice_id'];
  176. }
  177. }
  178. $from = "shopifine@localshopifine.com";
  179. $headers = "From:" . $from;
  180. mail($to,$subject,$message,$headers);
  181. //$data['shippingArray'] = $shippingArray;
  182. $data['trackingNumber'] = $trackingNumber;
  183. $this->load->view('invoice/confirmation',$data);
  184. //TODO: if everything fine unset the invoice List in session
  185. }
  186. function _createTrackingNumber($reg_number,$delivery_point){
  187. $offset = 100000;
  188. $lastId = $this->Shipment_master->last_insert_id();
  189. $trackingNumber = $offset + $lastId + 1;
  190. $vehicleId = $this->Delivery_vehicle->getId($reg_number);
  191. $trackingNumber = $trackingNumber.'00' + $vehicleId ;
  192. $pointId = $this->Delivery_point->getId($delivery_point);
  193. $trackingNumber = $trackingNumber.'00' + $pointId;
  194. return $trackingNumber;
  195. }
  196. function populateInvoices(){
  197. $searchOn = strip($_REQUEST['_search']);
  198. $page = $_REQUEST['page'];
  199. $limit = $_REQUEST['rows'];
  200. $sidx = $_REQUEST['sidx'];
  201. $sord = $_REQUEST['sord'];
  202. $where = array('status'=>'invoiced');
  203. $userdetais = $this->Userview->getUserInfo($this->user->person_id);
  204. if ($userdetais[0]->role_name!='Administrator'){
  205. $where['owner_id'] = $this->user->person_id;
  206. }
  207. //standard response parameters
  208. $mfrsdata = array();
  209. $count = $this->Invoice_master->totalNoOfRows($where,true);
  210. if( $count > 0 && $limit > 0) {
  211. $total_pages = ceil($count/$limit);
  212. } else {
  213. $total_pages = 0;
  214. }
  215. if ($page > $total_pages) $page=$total_pages;
  216. $start = $limit*$page - $limit;
  217. // if for some reasons start position is negative set it to 0
  218. // typical case is that the user type 0 for the requested page
  219. if($start <0) $start = 0;
  220. $clauses = array('orderBy'=>$sidx,'orderDir'=>$sord,'startLimit'=>$start,'limit'=>$limit);
  221. $data['total'] = $total_pages;
  222. $data['page'] = $page;
  223. $data['records'] = $count;
  224. $searchOn = strip($_REQUEST['_search']);
  225. $mfrs = $this->Invoice_master->getAll(false,$where,true);
  226. foreach ($mfrs as $dp){
  227. $userdetais = $this->Userview->getUserInfo($dp['owner_id']);
  228. if (!empty($userdetais)){
  229. $ownername = $userdetais[0]->username;
  230. }
  231. else {
  232. $ownername = 'Not Owned Yet';
  233. }
  234. array_push($mfrsdata, array('id'=> $dp['magento_invoice_entity_id'],'dprow' => array($dp['magento_invoice_increment_id'],$dp['status'],$dp['magento_order_increment_id'],$dp['comments'],$ownername,'pack')));
  235. }
  236. $data['invoicedata'] = $mfrsdata;
  237. echo json_encode($data);
  238. }
  239. function populatePackedInvoicesByUser(){
  240. $page = $_REQUEST['page'];
  241. $limit = $_REQUEST['rows'];
  242. $sidx = $_REQUEST['sidx'];
  243. $sord = $_REQUEST['sord'];
  244. $where = array('status'=>'packed');
  245. $where['owner_id'] = $this->user->person_id;
  246. //standard response parameters
  247. $mfrsdata = array();
  248. $count = $this->Invoice_master->totalNoOfRows($where);
  249. if( $count > 0 && $limit > 0) {
  250. $total_pages = ceil($count/$limit);
  251. } else {
  252. $total_pages = 0;
  253. }
  254. if ($page > $total_pages) $page=$total_pages;
  255. $start = $limit*$page - $limit;
  256. // if for some reasons start position is negative set it to 0
  257. // typical case is that the user type 0 for the requested page
  258. if($start <0) $start = 0;
  259. $clauses = array('orderBy'=>$sidx,'orderDir'=>$sord,'startLimit'=>$start,'limit'=>$limit);
  260. $data['total'] = $total_pages;
  261. $data['page'] = $page;
  262. $data['records'] = $count;
  263. $mfrs = $this->Invoice_master->getAll(false,$where);
  264. foreach ($mfrs as $dp){
  265. array_push($mfrsdata, array('id'=> $dp['magento_invoice_entity_id'],'dprow' => array($dp['magento_invoice_increment_id'],$dp['status'],$dp['magento_order_increment_id'],$dp['comments'])));
  266. }
  267. $data['invoicedata'] = $mfrsdata;
  268. echo json_encode($data);
  269. }
  270. function populateShipmentReadyInvoices(){
  271. $page = $_REQUEST['page'];
  272. $limit = $_REQUEST['rows'];
  273. $sidx = $_REQUEST['sidx'];
  274. $sord = $_REQUEST['sord'];
  275. $where = array('status'=>'readyforshipping');
  276. $where['owner_id'] = $this->user->person_id;
  277. //standard response parameters
  278. $mfrsdata = array();
  279. $count = $this->Invoice_master->totalNoOfRows($where);
  280. if( $count > 0 && $limit > 0) {
  281. $total_pages = ceil($count/$limit);
  282. } else {
  283. $total_pages = 0;
  284. }
  285. if ($page > $total_pages) $page=$total_pages;
  286. $start = $limit*$page - $limit;
  287. // if for some reasons start position is negative set it to 0
  288. // typical case is that the user type 0 for the requested page
  289. if($start <0) $start = 0;
  290. $clauses = array('orderBy'=>$sidx,'orderDir'=>$sord,'startLimit'=>$start,'limit'=>$limit);
  291. $data['total'] = $total_pages;
  292. $data['page'] = $page;
  293. $data['records'] = $count;
  294. $mfrs = $this->Invoice_master->getAll(false,$where);
  295. foreach ($mfrs as $dp){
  296. array_push($mfrsdata, array('id'=> $dp['magento_invoice_entity_id'],'dprow' => array($dp['magento_invoice_increment_id'],$dp['status'],$dp['magento_order_increment_id'],$dp['comments'])));
  297. }
  298. $data['invoicedata'] = $mfrsdata;
  299. echo json_encode($data);
  300. }
  301. function populateShippedInvoices(){
  302. $page = $_REQUEST['page'];
  303. $limit = $_REQUEST['rows'];
  304. $sidx = $_REQUEST['sidx'];
  305. $sord = $_REQUEST['sord'];
  306. $where = array('status'=>'shipped');
  307. $where['owner_id'] = $this->user->person_id;
  308. $where['shipping_tracking_number']= $_REQUEST['trackingNumber'];
  309. //standard response parameters
  310. $mfrsdata = array();
  311. $count = $this->Invoice_master->totalNoOfRows($where);
  312. if( $count > 0 && $limit > 0) {
  313. $total_pages = ceil($count/$limit);
  314. } else {
  315. $total_pages = 0;
  316. }
  317. if ($page > $total_pages) $page=$total_pages;
  318. $start = $limit*$page - $limit;
  319. // if for some reasons start position is negative set it to 0
  320. // typical case is that the user type 0 for the requested page
  321. if($start <0) $start = 0;
  322. $clauses = array('orderBy'=>$sidx,'orderDir'=>$sord,'startLimit'=>$start,'limit'=>$limit);
  323. $data['total'] = $total_pages;
  324. $data['page'] = $page;
  325. $data['records'] = $count;
  326. $mfrs = $this->Invoice_master->getAll(false,$where);
  327. foreach ($mfrs as $dp){
  328. array_push($mfrsdata, array('id'=> $dp['magento_invoice_entity_id'],'dprow' => array($dp['magento_invoice_increment_id'],$dp['status'],$dp['magento_order_increment_id'],$dp['comments'])));
  329. }
  330. $data['invoicedata'] = $mfrsdata;
  331. echo json_encode($data);
  332. }
  333. function assign(){
  334. $inv_id_array = json_decode($_REQUEST['selInv']);
  335. foreach($inv_id_array as $id){
  336. $inv_data = array
  337. (
  338. 'owner_id'=>$_REQUEST['userId'],
  339. 'last_updated_by'=>$this->username,
  340. );
  341. $where_clause_array= array('magento_invoice_entity_id'=>$id);
  342. $this->Invoice_master->update($where_clause_array,$inv_data);
  343. }
  344. }
  345. function acquire(){
  346. $inv_id_array = json_decode($_REQUEST['selInv']);
  347. foreach($inv_id_array as $id){
  348. $inv_data = array
  349. (
  350. 'owner_id'=>$this->user->person_id,
  351. 'last_updated_by'=>$this->username,
  352. );
  353. $where_clause_array= array('magento_invoice_entity_id'=>$id);
  354. $this->Invoice_master->update($where_clause_array,$inv_data);
  355. }
  356. }
  357. }
  358. ?>