Order.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace app\admin\controller\customer;
  3. use app\admin\controller\Admin;
  4. /**
  5. * 订单
  6. */
  7. class Order extends Admin
  8. {
  9. protected $CustomerOrder = null;
  10. public function init()
  11. {
  12. parent::init();
  13. $this->CustomerOrder = model('customer.Order');
  14. }
  15. public function index()
  16. {
  17. $this->assign('meta_title', '销售记录');
  18. return $this->fetch();
  19. }
  20. public function load()
  21. {
  22. $page = input('get.page');
  23. $limit = input('get.limit');
  24. $where = [];
  25. $search = input('get.search');
  26. if (!empty($search)) {
  27. $where['c.cname|c.id'] = ['like', '%' . $search . '%'];
  28. }
  29. $list = $this->CustomerOrder->field('co.*')
  30. ->alias('co')
  31. ->join('Customer c', 'c.id = co.customer_id')
  32. ->where($where)->order('co.id desc')->paginate($limit, false, ['page' => $page]);
  33. $data = [];
  34. foreach ($list as $key => $value) {
  35. $data[$key]['id'] = $value['id'];
  36. $customer = $value['customer'];
  37. $data[$key]['customer'] = $customer['cname'] . '[' . $customer['id'] . ']';
  38. $data[$key]['denomination'] = $value['denomination'];
  39. $data[$key]['pay_amount'] = $value['pay_amount'];
  40. $data[$key]['num'] = $value['num'];
  41. $data[$key]['discount'] = $value['discount'];
  42. $data[$key]['expire_time'] = $value['expire_time'];
  43. $card_number = $value['card_number'];
  44. $card_number_str = '';
  45. foreach ($card_number as $k => $val) {
  46. $card_number_str .= $val['start'] . '~' . $val['end'] . '<br>';
  47. }
  48. $data[$key]['card_number'] = $card_number_str;
  49. $goods_type = $value['goods_type'];
  50. $goods_type_str = '';
  51. foreach ($goods_type as $k => $val) {
  52. $goods_type_str .= '【' . $val['cname'] . '】';
  53. }
  54. $data[$key]['goods_type'] = empty($goods_type_str) ? '全品类' : $goods_type_str;
  55. $store_type = $value['store_type'];
  56. $store_type_str = '';
  57. foreach ($store_type as $k => $val) {
  58. $store_type_str .= '【' . $val['cname'] . '】';
  59. }
  60. $data[$key]['store_type'] = empty($store_type_str) ? '全品类' : $store_type_str;
  61. $data[$key]['create_time'] = $value['create_time'];
  62. }
  63. $this->output(0, '获取成功', $data, $list->total());
  64. }
  65. public function add()
  66. {
  67. if ($this->request->isPost()) {
  68. $customer_id = input('post.customer_id');
  69. if (empty($customer_id)) {
  70. $this->output(1, '参数错误');
  71. }
  72. $num = input('post.num');
  73. $num = intval($num);
  74. if ($num <= 0) {
  75. $this->output(1, '数量必须大于o');
  76. }
  77. $expire_time = input('post.expire_time');
  78. if (empty($expire_time)) {
  79. $this->output(1, '有效期不能为空');
  80. }
  81. list($expire_start_time, $expire_end_time) = explode('~', $expire_time);
  82. if (strtotime($expire_end_time) < $this->request->time()) {
  83. $this->output(1, '有效结束时间必须大于当期时间');
  84. }
  85. $denomination = input('post.denomination');
  86. $denomination = floatval($denomination);
  87. if ($denomination <= 0) {
  88. $this->output(1, '请输入面值');
  89. }
  90. $pay_amount = input('post.pay_amount');
  91. $pay_amount = floatval($pay_amount);
  92. if ($pay_amount <= 0) {
  93. $this->output(1, '请输入实付金额');
  94. }
  95. $goods_type = input('post.goods_type/a', []);
  96. $store_type = input('post.store_type/a', []);
  97. $card_number_arr = input('post.card_number/a', []);
  98. $total_num = array_sum(array_column($card_number_arr, 'num'));
  99. if ($total_num != $num) {
  100. $this->output(1, '卡号数量和销售数量不相等');
  101. }
  102. $this->CustomerOrder->startTrans();
  103. $CardNumber = model('card.Number');
  104. foreach ($card_number_arr as $key => $value) {
  105. for ($i = $value['start']; $i <= $value['end']; $i++) {
  106. $card_number = $CardNumber->where(['id' => $i, 'customer_id' => 0])->find();
  107. if (!$card_number) {
  108. $this->CustomerOrder->rollback();
  109. $this->output(1, '序号[' . $i . ']已经售出');
  110. }
  111. $card_number->customer_id = $customer_id;
  112. $card_number->goods_type = $goods_type;
  113. $card_number->store_type = $store_type;
  114. $card_number->denomination = $denomination;
  115. $card_number->balance = $denomination;
  116. $card_number->expire_start_time = strtotime($expire_start_time);
  117. $card_number->expire_end_time = strtotime($expire_end_time);
  118. $card_number->sale_time = $this->request->time();
  119. $result = $card_number->save();
  120. if (!$result) {
  121. $this->CustomerOrder->rollback();
  122. $this->output(1, '卡号失败');
  123. }
  124. }
  125. }
  126. $this->CustomerOrder->customer_id = $customer_id;
  127. $this->CustomerOrder->num = $total_num;
  128. $this->CustomerOrder->expire_time = $expire_time;
  129. $this->CustomerOrder->denomination = $denomination;
  130. $this->CustomerOrder->pay_amount = $pay_amount;
  131. $this->CustomerOrder->discount = round($pay_amount / $denomination, 2) * 100;
  132. $this->CustomerOrder->goods_type = $goods_type;
  133. $this->CustomerOrder->store_type = $store_type;
  134. $this->CustomerOrder->card_number = $card_number_arr;
  135. $result = $this->CustomerOrder->save();
  136. if (!$result) {
  137. $this->CustomerOrder->rollback();
  138. $this->output(1, '保存失败');
  139. }
  140. $this->CustomerOrder->commit();
  141. $this->output(0, '保存成功');
  142. } else {
  143. //用户信息
  144. $Customer = model('Customer');
  145. $customers = $Customer->select();
  146. $this->assign('customers', $customers);
  147. //店铺分类
  148. $checkHost = config('mall_url');
  149. $url = $checkHost . 'api/store/get_store_class';
  150. $store_class = common_curl($url, []);
  151. $store_class = $store_class['result']['store_class'];
  152. $this->assign('store_class',$store_class);
  153. $this->assign('meta_title', '添加订单');
  154. return $this->fetch();
  155. }
  156. }
  157. }