Advertisement
Guest User

Untitled

a guest
Dec 11th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.88 KB | None | 0 0
  1. <?php
  2. require_once(realpath(dirname(__FILE__) . "/../tools/rest.php"));
  3. require_once(realpath(dirname(__FILE__) . "/../tools/mail_handler.php"));
  4.  
  5. class ProductOrder extends REST{
  6.  
  7. private $mysqli = NULL;
  8. private $db = NULL;
  9. private $product_order_detail = NULL;
  10. private $fcm = NULL;
  11. private $mail_handler = NULL;
  12.  
  13. public function __construct($db) {
  14. parent::__construct();
  15. $this->db = $db;
  16. $this->mysqli = $db->mysqli;
  17. $this->product_order_detail = new ProductOrderDetail($this->db);
  18. $this->fcm = new Fcm($this->db);
  19. $this->mail_handler = new MailHandler($this->db);
  20. }
  21.  
  22. public function findAll(){
  23. if($this->get_request_method() != "GET") $this->response('',406);
  24. $query="SELECT * FROM product_order po ORDER BY po.id DESC";
  25. $this->show_response($this->db->get_list($query));
  26. }
  27.  
  28. public function findOne(){
  29. if($this->get_request_method() != "GET") $this->response('',406);
  30. if(!isset($this->_request['id'])) $this->responseInvalidParam();
  31. $id = (int)$this->_request['id'];
  32. $query="SELECT distinct * FROM product_order po WHERE po.id=$id";
  33. $this->show_response($this->db->get_one($query));
  34. }
  35.  
  36. public function findOnePlain($id){
  37. $query="SELECT * FROM product_order po WHERE po.id=$id";
  38. return $this->db->get_one($query);
  39. }
  40.  
  41. public function findAllByPage(){
  42. if($this->get_request_method() != "GET") $this->response('',406);
  43. if(!isset($this->_request['limit']) || !isset($this->_request['page']))$this->responseInvalidParam();
  44. $limit = (int)$this->_request['limit'];
  45. $offset = ((int)$this->_request['page']) - 1;
  46. $q = (isset($this->_request['q'])) ? ($this->_request['q']) : "";
  47. if($q != ""){
  48. $query= "SELECT DISTINCT * FROM product_order po "
  49. ."WHERE buyer REGEXP '$q' OR code REGEXP '$q' OR address REGEXP '$q' OR email REGEXP '$q' OR phone REGEXP '$q' OR comment REGEXP '$q' OR shipping REGEXP '$q' "
  50. ."ORDER BY po.id DESC LIMIT $limit OFFSET $offset";
  51. } else {
  52. $query="SELECT DISTINCT * FROM product_order po ORDER BY po.id DESC LIMIT $limit OFFSET $offset";
  53. }
  54. $this->show_response($this->db->get_list($query));
  55. }
  56.  
  57. public function allCount(){
  58. if($this->get_request_method() != "GET") $this->response('',406);
  59. $query="SELECT COUNT(DISTINCT po.id) FROM product_order po";
  60. $this->show_response_plain($this->db->get_count($query));
  61. }
  62.  
  63. public function insertOne(){
  64. if($this->get_request_method() != "POST") $this->response('', 406);
  65. $data = json_decode(file_get_contents("php://input"), true);
  66. if(!isset($data)) $this->responseInvalidParam();
  67. $resp = $this->insertOnePlain($data);
  68. $this->show_response($resp);
  69. }
  70.  
  71. public function insertOnePlain($data){
  72. $column_names = array('code', 'buyer', 'address', 'email', 'shipping', 'date_ship', 'phone', 'comment', 'status', 'total_fees', 'tax', 'serial', 'created_at', 'last_update');
  73. $table_name = 'product_order';
  74. $pk = 'id';
  75. $data['code'] = $this->getRandomCode();
  76. $resp = $this->db->post_one($data, $pk, $column_names, $table_name);
  77. return $resp;
  78. }
  79.  
  80. public function updateOne(){
  81. if($this->get_request_method() != "POST") $this->response('',406);
  82. $data = json_decode(file_get_contents("php://input"),true);
  83. if(!isset($data['id'])) $this->responseInvalidParam();
  84. $id = (int)$data['id'];
  85. $column_names = array('buyer', 'address', 'email', 'shipping', 'date_ship', 'phone', 'comment', 'status', 'total_fees', 'tax', 'serial', 'created_at', 'last_update');
  86. $table_name = 'product_order';
  87. $pk = 'id';
  88. $this->show_response($this->db->post_update($id, $data, $pk, $column_names, $table_name));
  89. }
  90.  
  91. public function deleteOne(){
  92. if($this->get_request_method() != "GET") $this->response('',406);
  93. if(!isset($this->_request['id'])) $this->responseInvalidParam();
  94. $id = (int)$this->_request['id'];
  95. $table_name = 'product_order';
  96. $pk = 'id';
  97. $this->show_response($this->db->delete_one($id, $pk, $table_name));
  98. }
  99.  
  100. public function deleteOnePlain($id){
  101. $table_name = 'product_order';
  102. $pk = 'id';
  103. return $this->db->delete_one($id, $pk, $table_name);
  104. }
  105.  
  106. public function countByStatusPlain($status){
  107. $query = "SELECT COUNT(DISTINCT po.id) FROM product_order po WHERE po.status='$status' ";
  108. return $this->db->get_count($query);
  109. }
  110.  
  111. public function processOrder(){
  112. if($this->get_request_method() != "POST") $this->response('',406);
  113. $data = json_decode(file_get_contents("php://input"),true);
  114. if(!isset($data['id']) || !isset($data['product_order']) || !isset($data['product_order_detail'])) {
  115. $this->responseInvalidParam();
  116. }
  117. $id = (int)$data['id'];
  118. $order = $data['product_order'];
  119. $order_detail = $data['product_order_detail'];
  120.  
  121. $resp_od = $this->product_order_detail->checkAvailableProductOrderDetail($order_detail);
  122. if($resp_od['status'] == 'success'){
  123. // process product stock
  124. foreach($resp_od['data'] as $od){
  125. $val = (int)$od['stock'] - (int)$od['amount'];
  126. $product_id = $od['product_id'];
  127. if($val > 0){
  128. $query = "UPDATE product SET stock=$val WHERE id=$product_id";
  129. } else {
  130. $query = "UPDATE product SET stock=$val, status='OUT OF STOCK' WHERE id=$product_id";
  131. }
  132. $this->mysqli->query($query) or die($this->mysqli->error.__LINE__);
  133. }
  134. // update order status
  135. $new_status = 'PROCESSED';
  136. $order_id = $order['id'];
  137. $query_2 = "UPDATE product_order SET status='$new_status' WHERE id=$order_id";
  138. $this->mysqli->query($query_2) or die($this->mysqli->error.__LINE__);
  139.  
  140. // send notification
  141. $order['status'] = $new_status;
  142. $this->sendNotifProductOrder($order);
  143.  
  144. // send email
  145. $this->mail_handler->curlEmailOrderProcess($order_id);
  146. }
  147. $this->show_response($resp_od);
  148. }
  149.  
  150. private function sendNotifProductOrder($order){
  151. if($order['serial'] != null){
  152. $regid = $this->fcm->findBySerial($order['serial']);
  153. $registration_ids = array($regid['regid']);
  154. $data = array(
  155. 'title' => 'Order Status Changed',
  156. 'content' => 'Your order ' . $order['code'] .' status has been change to ' . $order['status'],
  157. 'type' => 'PROCESS_ORDER',
  158. 'code' => $order['code'],
  159. 'status' => $order['status']
  160. );
  161. $this->fcm->sendPushNotification($registration_ids, $data);
  162. }
  163. }
  164.  
  165. // function to generate unique id
  166. private function getRandomCode() {
  167. $size = 10; // must > 6
  168. $alpha_key = '';
  169. $alpha_key2 = '';
  170. $keys = range('A', 'Z');
  171. for ($i = 0; $i < 2; $i++) {
  172. $alpha_key .= $keys[array_rand($keys)];
  173. $alpha_key2 .= $keys[array_rand($keys)];
  174. }
  175. $length = $size - 5;
  176. $key = '';
  177. $keys = range(0, 9);
  178. for ($i = 0; $i < $length; $i++) {
  179. $key .= $keys[array_rand($keys)];
  180. }
  181. $final_key = $alpha_key . $key . $alpha_key2;
  182.  
  183. // make sure code is unique in database
  184. $query = "SELECT COUNT(DISTINCT po.id) FROM product_order po WHERE po.code='$final_key' ";
  185. $num_rows = $this->db->get_count($query);
  186.  
  187. if($num_rows > 0) {
  188. return $this->getRandomCode();
  189. } else {
  190. return $final_key;
  191. }
  192. }
  193. }
  194. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement