/application/controllers/admin/shippings.php

https://gitlab.com/bandana/Astro-Veda · PHP · 408 lines · 296 code · 105 blank · 7 comment · 29 complexity · 928ea29a8cef908f469b6bf93e8fac8f MD5 · raw file

  1. <?php
  2. class Shippings extends BaseController {
  3. public function index() {
  4. $shippings = Shipping::find('all', array(
  5. /*'conditions' => array(
  6. 'deleted = ?',
  7. 0
  8. ),*/
  9. 'order' => 'id desc'
  10. ));
  11. return $this->load_view('admin/shipping', array('shippings' => $shippings));
  12. }
  13. public function add_quotation($shipping_id) {
  14. try {
  15. $shipping = Shipping::find_by_id($shipping_id);
  16. if(!$shipping) {
  17. throw new Exception("Invalid Shipping order!");
  18. }
  19. if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
  20. return $this->load_view('admin/quotation', array('shipping' => $shipping));
  21. }
  22. $params = array(
  23. 'shipping' => $shipping,
  24. 'total_cost' => $this->input->post('total_cost'),
  25. 'date' => $this->input->post('date'),
  26. 'object_cost' => $this->input->post('object_cost'),
  27. 'shipping_cost' => $this->input->post('shipping_cost'),
  28. 'company_name' => $this->input->post('company_name'),
  29. 'quotation_number' => $this->input->post('quotation_number'),
  30. 'days' => $this->input->post('days'),
  31. 'gemstone_id' => $shipping->gemstone_id,
  32. );
  33. $quotation = new Quotation;
  34. $quotation = $quotation->create($params);
  35. $quotation->save();
  36. $gcm_users = $shipping->user->gcm_users;
  37. $data = array(
  38. 'address' => array(
  39. 'country' => $shipping->country,
  40. 'state' => $shipping->state,
  41. 'city' => $shipping->city,
  42. 'street_address' => $shipping->street,
  43. 'apt_no' => $shipping->apt_no,
  44. 'postal_code' => $shipping->postal_code,
  45. 'phone_number' => $shipping->phone_number,
  46. ),
  47. 'type' => $shipping->type,
  48. 'total_cost' => $quotation->total_cost,
  49. 'date' => date("Y-m-d H:i:s", strtotime($quotation->date)),
  50. 'object_cost' => $quotation->object_cost,
  51. 'shipping_cost' => $quotation->shipping_cost,
  52. 'to_whom' => $shipping->full_name,
  53. 'shipping_company_name' => $quotation->company_name,
  54. 'quotation_number' => $quotation->quotation_number,
  55. 'days' => $quotation->days,
  56. 'object_id' => $shipping->gemstone_id,
  57. );
  58. if($shipping->type == 1)
  59. $data['object'] = 'Natal Chart';
  60. elseif($shipping->type == 2)
  61. $data['object'] = 'Gemstone';
  62. $message = json_encode(array(
  63. 'type' => 6,
  64. 'data' => $data
  65. ));
  66. $this->gcm->setMessage($message);
  67. foreach ($gcm_users as $gcm_user) {
  68. $this->gcm->addRecepient($gcm_user->gcm_regd_id);
  69. }
  70. // set additional data
  71. $this->gcm->setData(array(
  72. 'stat' => 'OK'
  73. ));
  74. $this->gcm->setTtl(false);
  75. $this->gcm->setGroup(false);
  76. $this->gcm->send();
  77. if($shipping->type == 1) {
  78. $params = array(
  79. 'user' => $shipping->user,
  80. 'object_type' => 1,
  81. 'notification_type' => 6,
  82. 'information_type' => 0,
  83. 'object_id' => $shipping->user->natal_chart->id,
  84. 'details' => 'Quotation add to Shipping',
  85. );
  86. $push = new PushNotificationLog;
  87. $push->create($params);
  88. }
  89. elseif($shipping->type == 2) {
  90. $params = array(
  91. 'user' => $shipping->user,
  92. 'object_type' => 2,
  93. 'notification_type' => 6,
  94. 'information_type' => 0,
  95. 'object_id' => $shipping->gemstone_id,
  96. 'details' => 'Quotation add to Shipping',
  97. );
  98. $push = new PushNotificationLog;
  99. $push->create($params);
  100. }
  101. $this->session->set_flashdata(
  102. 'alert_success',
  103. "Quotation added to the shipping order successfully."
  104. );
  105. redirect('/admin/shippings');
  106. }
  107. catch(Exception $e) {
  108. $this->session->set_flashdata('alert_error', $e->getMessage());
  109. redirect('/admin/shippings');
  110. }
  111. }
  112. public function send_quotation($quotation_id) {
  113. try {
  114. $quotation = Quotation::find_by_id($quotation_id);
  115. if(!$quotation)
  116. throw new Exception("Invalid Quotation!");
  117. if($quotation->approved == 1)
  118. throw new Exception("Invalid Quotation!");
  119. $gcm_users = $quotation->shipping->user->gcm_users;
  120. $data = array(
  121. 'address' => array(
  122. 'country' => $quotation->shipping->country,
  123. 'state' => $quotation->shipping->state,
  124. 'city' => $quotation->shipping->city,
  125. 'street_address' => $quotation->shipping->street,
  126. 'apt_no' => $quotation->shipping->apt_no,
  127. 'postal_code' => $quotation->shipping->postal_code,
  128. 'phone_number' => $quotation->shipping->phone_number,
  129. ),
  130. 'type' => $quotation->shipping->type,
  131. 'total_cost' => $quotation->total_cost,
  132. 'date' => date("Y-m-d H:i:s", strtotime($quotation->date)),
  133. 'object_cost' => $quotation->object_cost,
  134. 'shipping_cost' => $quotation->shipping_cost,
  135. 'to_whom' => $quotation->shipping->full_name,
  136. 'shipping_company_name' => $quotation->company_name,
  137. 'quotation_number' => $quotation->quotation_number,
  138. 'days' => $quotation->days,
  139. 'object_id' => $quotation->shipping->gemstone_id,
  140. );
  141. if($quotation->shipping->type == 1)
  142. $data['object'] = 'Natal Chart';
  143. elseif($quotation->shipping->type == 2)
  144. $data['object'] = 'Gemstone';
  145. $message = json_encode(array(
  146. 'type' => 6,
  147. 'data' => $data
  148. ));
  149. $this->gcm->setMessage($message);
  150. foreach ($gcm_users as $gcm_user) {
  151. $this->gcm->addRecepient($gcm_user->gcm_regd_id);
  152. }
  153. // set additional data
  154. $this->gcm->setData(array(
  155. 'stat' => 'OK'
  156. ));
  157. $this->gcm->setTtl(false);
  158. $this->gcm->setGroup(false);
  159. $this->gcm->send();
  160. if($quotation->shipping->type == 1) {
  161. $params = array(
  162. 'user' => $quotation->shipping->user,
  163. 'object_type' => 1,
  164. 'notification_type' => 6,
  165. 'information_type' => 0,
  166. 'object_id' => $quotation->shipping->user->natal_chart->id,
  167. 'details' => 'Quotation sent to the User',
  168. );
  169. $push = new PushNotificationLog;
  170. $push->create($params);
  171. }
  172. elseif($quotation->shipping->type == 2) {
  173. $params = array(
  174. 'user' => $quotation->shipping->user,
  175. 'object_type' => 2,
  176. 'notification_type' => 6,
  177. 'information_type' => 0,
  178. 'object_id' => $quotation->shipping->gemstone_id,
  179. 'details' => 'Quotation sent to the User',
  180. );
  181. $push = new PushNotificationLog;
  182. $push->create($params);
  183. }
  184. $this->session->set_flashdata(
  185. 'alert_success',
  186. "Quotation sent to the user successfully."
  187. );
  188. redirect('/admin/shippings');
  189. }
  190. catch(Exception $e) {
  191. $this->session->set_flashdata('alert_error', $e->getMessage());
  192. redirect('/admin/shippings');
  193. }
  194. }
  195. public function delete($shipping_id) {
  196. try {
  197. $shipping = Shipping::find_by_id($shipping_id);
  198. if(!$shipping)
  199. throw new Exception("Shipping Order not found");
  200. if($shipping->deleted)
  201. throw new Exception("Shipping Order already deleted");
  202. $shipping->delete();
  203. $this->session->set_flashdata('alert_success', "Shipping order deleted successfully");
  204. redirect('admin/shippings');
  205. }
  206. catch(Exception $e) {
  207. $this->session->set_flashdata('alert_error', $e->getMessage());
  208. redirect('admin/shippings');
  209. }
  210. }
  211. public function complete($shipping_id) {
  212. try {
  213. $shipping = Shipping::find_by_id_and_completed($shipping_id, 0);
  214. if(!$shipping)
  215. throw new Exception("Shipping Order not found");
  216. $shipping->completed = 1;
  217. $shipping->save();
  218. $gcm_users = $shipping->user->gcm_users;
  219. if($shipping->type == 1) {
  220. $natal_chart = NatalChart::find_by_user_id($shipping->user_id);
  221. $natal_chart->status = 4;
  222. $natal_chart->save();
  223. $message = json_encode(array(
  224. 'type' => 2,
  225. 'data' => array(
  226. 'information_type' => 3,
  227. 'description' => "Your Natal Chart is shipped now. It will take ".$shipping->quotation->days." days for the delivery.",
  228. ),
  229. ));
  230. }
  231. elseif($shipping->type == 2) {
  232. $user_gemstone = UserGemstone::find_by_id($shipping->gemstone_id);
  233. $user_gemstone->status = 3;
  234. $user_gemstone->save();
  235. $data = array(
  236. 'information_type' => 3,
  237. 'gemstone_id' => $user_gemstone->id,
  238. 'gems_description' => $user_gemstone->details,
  239. 'gem_stone_type' => $user_gemstone->gemstone_id,
  240. );
  241. $message = json_encode(array(
  242. 'type' => 4,
  243. 'data' => $data
  244. ));
  245. }
  246. $this->gcm->setMessage($message);
  247. foreach ($gcm_users as $gcm_user) {
  248. $this->gcm->addRecepient($gcm_user->gcm_regd_id);
  249. }
  250. // set additional data
  251. $this->gcm->setData(array(
  252. 'stat' => 'OK'
  253. ));
  254. $this->gcm->setTtl(false);
  255. $this->gcm->setGroup(false);
  256. $this->gcm->send();
  257. if($shipping->type == 1) {
  258. $params = array(
  259. 'user' => $shipping->user,
  260. 'object_type' => 1,
  261. 'notification_type' => 2,
  262. 'information_type' => 3,
  263. 'object_id' => $shipping->user->natal_chart->id,
  264. 'details' => 'Shipping completed',
  265. );
  266. $push = new PushNotificationLog;
  267. $push->create($params);
  268. }
  269. elseif($shipping->type == 2) {
  270. $params = array(
  271. 'user' => $shipping->user,
  272. 'object_type' => 2,
  273. 'notification_type' => 4,
  274. 'information_type' => 3,
  275. 'object_id' => $shipping->gemstone_id,
  276. 'details' => 'Shipping completed',
  277. );
  278. $push = new PushNotificationLog;
  279. $push->create($params);
  280. }
  281. $this->session->set_flashdata('alert_success', "Shipping order completion confirmed successfully");
  282. redirect('admin/shippings');
  283. }
  284. catch(Exception $e) {
  285. $this->session->set_flashdata('alert_error', $e->getMessage());
  286. redirect('admin/shippings');
  287. }
  288. }
  289. public function view_quotation($shipping_id) {
  290. try {
  291. $shipping = Shipping::find_by_id($shipping_id);
  292. if(!$shipping)
  293. throw new Exception("Shipping Order not found");
  294. if(!$shipping->quotation)
  295. throw new Exception("Quotation not found");
  296. return $this->load_view('admin/view_quotation', array('quotation' => $shipping->quotation));
  297. }
  298. catch(Exception $e) {
  299. $this->session->set_flashdata('alert_error', $e->getMessage());
  300. redirect('admin/shippings');
  301. }
  302. }
  303. }