OrderModel.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. namespace app\common\model;
  3. class OrderModel extends BaseModel
  4. {
  5. protected $table = 'erp_order';
  6. protected function genSchema(array $schema)
  7. {
  8. // TODO: Implement genSchema() method.
  9. }
  10. public function genOrderNo($store_id, $store_abbr) {
  11. $order = $this->where('store_id', $store_id)->order('id','desc')->find();
  12. if(!$order)
  13. return "{$store_abbr}10000001";
  14. $code = (int)str_ireplace($store_abbr, '', $order->no) + 1;
  15. return "{$store_abbr}{$code}";
  16. }
  17. public function products() {
  18. return $this->hasMany(OrderProductModel::class, 'order_id','id')->where('is_delete',0);
  19. }
  20. public function payments() {
  21. return $this->hasMany(OrderPaymentModel::class, 'order_id','id')->where('is_delete',0);
  22. }
  23. public function annuals() {
  24. return $this->hasMany(OrderAnnualFeeModel::class, 'order_id','id')->where('is_delete',0);
  25. }
  26. public function proceeds() {
  27. return $this->hasMany(OrderProceedsModel::class, 'order_id','id')->where('is_delete',0);
  28. }
  29. public function store() {
  30. return $this->hasOne(StoreModel::class, 'id', 'store_id');
  31. }
  32. public function findByPaginate(array $params) {
  33. $where = [
  34. ['is_delete', '=', 0]
  35. ];
  36. if($params['store_id'] > 0)
  37. array_push($where,['store_id', '=', $params['store_id']]);
  38. if($params['type'] > 0)
  39. array_push($where,['type', '=', $params['type']]);
  40. return $this->where($where)
  41. ->with(['products','store'])
  42. ->order('update_time','desc')
  43. ->paginate(['list_rows'=>10, "query" => $params]);
  44. }
  45. public function report(array $params) {
  46. $where = [
  47. ['is_delete', '=', 0]
  48. ];
  49. if($params['store_id'] > 0)
  50. array_push($where,['store_id', '=', $params['store_id']]);
  51. if($params['type'] > 0)
  52. array_push($where,['type', '=', $params['type']]);
  53. return $this->where($where)
  54. ->with(['products','store'])
  55. ->order('create_time','desc')
  56. ->select();
  57. }
  58. public function findById($id)
  59. {
  60. return $this->where('is_delete',0)
  61. ->with([
  62. 'products',
  63. 'payments',
  64. 'payments.payment_channel',
  65. // 'payments.card_config',
  66. 'annuals',
  67. 'proceeds'
  68. ])->find($id);
  69. }
  70. /**
  71. * @param $params
  72. * @param bool $verify
  73. * @return array
  74. * @throws \think\db\exception\DbException
  75. */
  76. public function search($params, $verify = false) {
  77. $where = [
  78. ['is_delete', '=', 0],
  79. ['type', '=', $params['type']],
  80. ['store_id', '=', $params['store_id']],
  81. ];
  82. if (isset($params['order_no']) && strlen($params['order_no']) > 0)
  83. array_push($where, ['no', 'like', '%'.$params['order_no'].'%']);
  84. if (isset($params['start_time']) && $params['start_time'] > 0)
  85. array_push($where, ['create_time', '>=', $params['start_time']]);
  86. if (isset($params['end_time']) && $params['end_time'] > 0)
  87. array_push($where, ['create_time', '<=', $params['end_time']]);
  88. if (isset($params['obj_name']) && strlen($params['obj_name']) > 0)
  89. array_push($where, ['obj_names', 'like', '%'.$params['obj_name'].'%']);
  90. $query = $this->where($where)
  91. ->with(['products', 'payments']);
  92. if (isset($params['advisor_id']))
  93. $query->where("FIND_IN_SET('{$params['advisor_id']}', advisor_ids)");
  94. if (!$verify) {
  95. $query->where("FIND_IN_SET('{$params['admin_id']}', advisor_ids)");
  96. }
  97. return $query->order('update_time', 'desc')
  98. ->page($params['page'] ?? 1)
  99. ->paginate($params['size'] ?? 10)
  100. ->toArray();
  101. }
  102. public function fetchOrderByCustomerId($c_id) {
  103. return $this->where([
  104. ['is_delete', '=', 0],
  105. ['customer_id', '=', $c_id]
  106. ])
  107. ->order('create_time','desc')
  108. ->select()
  109. ->toArray();
  110. }
  111. /**
  112. * @param $customer_id
  113. * @param int $page
  114. * @param int $size
  115. * @return array
  116. * @throws \think\db\exception\DbException
  117. */
  118. public function fetchByCustomerId($customer_id, $page = 1, $size = 10) {
  119. return $this->where([
  120. ['is_delete', '=', 0],
  121. ['customer_id', '=', $customer_id]
  122. ])
  123. ->with(['products', 'payments'])
  124. ->order('update_time', 'desc')
  125. ->page($page)
  126. ->paginate($size)
  127. ->toArray();
  128. }
  129. public function fetchOrderByReturn($admin_id, $store_id, $no = null, $customer_name = null, $page = 1, $size = 10, $verify = false) {
  130. $where = [
  131. ['is_delete', '=', 0],
  132. ['type', '=', 1],
  133. ['store_id', '=', $store_id],
  134. ];
  135. if (isset($no) && strlen($no) > 0)
  136. array_push($where, ['no', 'like', '%'.$no.'%']);
  137. if ($customer_name)
  138. array_push($where, ['customer_name', 'like', '%'.$customer_name.'%']);
  139. $query = $this->where($where)
  140. ->with(['products']);
  141. if (!$verify) {
  142. $query->where("FIND_IN_SET('{$admin_id}', advisor_ids)");
  143. }
  144. return $query->order('update_time', 'desc')
  145. ->page($page)
  146. ->paginate($size);
  147. }
  148. public function findByOrderNo($admin_id, $order_no, $verify = false) {
  149. $where = [
  150. ['is_delete', '=', 0],
  151. ['type', '=', 1],
  152. ];
  153. if (isset($order_no) && strlen($order_no) > 0)
  154. array_push($where, ['no', '=', $order_no]);
  155. // array_push($where, ['no', 'like', '%'.$order_no.'%']);
  156. $query = $this->where($where)
  157. ->with(['products']);
  158. if (!$verify) {
  159. $query->where("FIND_IN_SET('{$admin_id}', advisor_ids)");
  160. }
  161. return $query->order('update_time', 'desc')
  162. ->find();
  163. }
  164. }