OrderModel.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. 'annuals',
  66. 'proceeds'
  67. ])->find($id);
  68. }
  69. /**
  70. * @param $params
  71. * @param bool $verify
  72. * @return array
  73. * @throws \think\db\exception\DbException
  74. */
  75. public function search($params, $verify = false) {
  76. $where = [
  77. ['is_delete', '=', 0],
  78. ['type', '=', $params['type']],
  79. ['store_id', '=', $params['store_id']],
  80. ];
  81. if (isset($params['order_no']) && strlen($params['order_no']) > 0)
  82. array_push($where, ['no', 'like', '%'.$params['order_no'].'%']);
  83. if (isset($params['start_time']) && $params['start_time'] > 0)
  84. array_push($where, ['create_time', '>=', $params['start_time']]);
  85. if (isset($params['end_time']) && $params['end_time'] > 0)
  86. array_push($where, ['create_time', '<=', $params['end_time']]);
  87. if (isset($params['obj_name']) && strlen($params['obj_name']) > 0)
  88. array_push($where, ['obj_names', 'like', '%'.$params['obj_name'].'%']);
  89. $query = $this->where($where)
  90. ->with(['products', 'payments']);
  91. if (isset($params['advisor_id']))
  92. $query->where("FIND_IN_SET('{$params['advisor_id']}', advisor_ids)");
  93. if (!$verify) {
  94. $query->where("FIND_IN_SET('{$params['admin_id']}', advisor_ids)");
  95. }
  96. return $query->order('update_time', 'desc')
  97. ->page($params['page'] ?? 1)
  98. ->paginate($params['size'] ?? 10)
  99. ->toArray();
  100. }
  101. public function fetchOrderByCustomerId($c_id) {
  102. return $this->where([
  103. ['is_delete', '=', 0],
  104. ['customer_id', '=', $c_id]
  105. ])
  106. ->order('create_time','desc')
  107. ->select()
  108. ->toArray();
  109. }
  110. /**
  111. * @param $customer_id
  112. * @param int $page
  113. * @param int $size
  114. * @return array
  115. * @throws \think\db\exception\DbException
  116. */
  117. public function fetchByCustomerId($customer_id, $page = 1, $size = 10) {
  118. return $this->where([
  119. ['is_delete', '=', 0],
  120. ['customer_id', '=', $customer_id]
  121. ])
  122. ->with(['products', 'payments'])
  123. ->order('update_time', 'desc')
  124. ->page($page)
  125. ->paginate($size)
  126. ->toArray();
  127. }
  128. public function fetchOrderByReturn($admin_id, $store_id, $no = null, $customer_name = null, $page = 1, $size = 10, $verify = false) {
  129. $where = [
  130. ['is_delete', '=', 0],
  131. ['type', '=', 1],
  132. ['store_id', '=', $store_id],
  133. ];
  134. if (isset($no) && strlen($no) > 0)
  135. array_push($where, ['no', 'like', '%'.$no.'%']);
  136. if ($customer_name)
  137. array_push($where, ['customer_name', 'like', '%'.$customer_name.'%']);
  138. $query = $this->where($where)
  139. ->with(['products']);
  140. if (!$verify) {
  141. $query->where("FIND_IN_SET('{$admin_id}', advisor_ids)");
  142. }
  143. return $query->order('update_time', 'desc')
  144. ->page($page)
  145. ->paginate($size);
  146. }
  147. public function findByOrderNo($admin_id, $order_no, $verify = false) {
  148. $where = [
  149. ['is_delete', '=', 0],
  150. ['type', '=', 1],
  151. ];
  152. if (isset($order_no) && strlen($order_no) > 0)
  153. array_push($where, ['no', '=', $order_no]);
  154. // array_push($where, ['no', 'like', '%'.$order_no.'%']);
  155. $query = $this->where($where)
  156. ->with(['products']);
  157. if (!$verify) {
  158. $query->where("FIND_IN_SET('{$admin_id}', advisor_ids)");
  159. }
  160. return $query->order('update_time', 'desc')
  161. ->find();
  162. }
  163. }