Coupon.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. namespace app\admin\controller;
  3. /**
  4. * 券
  5. */
  6. class Coupon extends Admin
  7. {
  8. protected $Coupon = null;
  9. public function init()
  10. {
  11. parent::init();
  12. $this->Coupon = model('Coupon');
  13. }
  14. public function index()
  15. {
  16. $this->assign('meta_title', '电子券管理');
  17. return $this->fetch();
  18. }
  19. public function load()
  20. {
  21. $page = input('get.page');
  22. $limit = input('get.limit');
  23. $where = [];
  24. $search = input('get.search');
  25. if (!empty($search)) {
  26. $where['cname|id'] = ['like', '%' . $search . '%'];
  27. }
  28. $list = $this->Coupon
  29. ->where($where)->order('id desc')->paginate($limit, false, ['page' => $page]);
  30. $data = [];
  31. foreach ($list as $key => $value) {
  32. $data[$key]['id'] = $value['id'];
  33. $data[$key]['no'] = $value['no'];
  34. $data[$key]['cname'] = $value['cname'];
  35. $type = $value['type'];
  36. $data[$key]['type_cname'] = $type['cname'];
  37. $supplier = $value['supplier'];
  38. $data[$key]['supplier'] = $supplier['cname'] . '[' . $supplier['id'] . ']';
  39. $data[$key]['supply_price'] = $value['supply_price'];
  40. $data[$key]['sale_price'] = $value['sale_price'];
  41. $data[$key]['total_num'] = $value['total_num'];
  42. $data[$key]['sold_num'] = $value['sold_num'];
  43. $data[$key]['unsold_num'] = $value['unsold_num'];
  44. $data[$key]['sale_time'] = $value['sale_time'];
  45. $data[$key]['create_time'] = $value['create_time'];
  46. $data[$key]['state'] = $value['state'];
  47. }
  48. $this->output(0, '获取成功', $data, $list->total());
  49. }
  50. public function add()
  51. {
  52. if ($this->request->isPost()) {
  53. $no = input('post.no');
  54. if (empty($no)) {
  55. $this->output(1, '编码不能为空');
  56. }
  57. $no_check = $this->Coupon->where('no', $no)->find();
  58. if (!empty($no_check)) {
  59. $this->output(1, '该编码已存在');
  60. }
  61. $cname = input('post.cname');
  62. if (empty($cname)) {
  63. $this->output(1, '名称不能为空');
  64. }
  65. $supply_price = input('post.supply_price');
  66. $supply_price = floatval($supply_price);
  67. if ($supply_price <= 0) {
  68. $this->output(1, '请输入供应价');
  69. }
  70. $sale_price = input('post.sale_price');
  71. $sale_price = floatval($sale_price);
  72. if ($sale_price <= 0) {
  73. $this->output(1, '请输入销售价');
  74. }
  75. $sale_time = input('post.sale_time');
  76. list($sale_start_time, $sale_end_time) = explode('~', $sale_time);
  77. if (strtotime($sale_end_time) < $this->request->time()) {
  78. $this->output(1, '有效结束时间必须大于当期时间');
  79. }
  80. $this->Coupon->no = $no;
  81. $this->Coupon->cname = $cname;
  82. $this->Coupon->type_id = input('post.type_id');
  83. $this->Coupon->supplier_id = input('post.supplier_id');
  84. $this->Coupon->supply_price = $supply_price;
  85. $this->Coupon->sale_price = $sale_price;
  86. $this->Coupon->sale_time = $sale_time;
  87. $result = $this->Coupon->save();
  88. if (!$result) {
  89. $this->output(1, '保存失败');
  90. }
  91. $this->output(0, '保存成功');
  92. } else {
  93. $CouponType = model('coupon.Type');
  94. $types = $CouponType->select();
  95. $this->assign('types', $types);
  96. $Supplier = model('Supplier');
  97. $suppliers = $Supplier->select();
  98. $this->assign('suppliers', $suppliers);
  99. $this->assign('meta_title', '添加券');
  100. return $this->fetch();
  101. }
  102. }
  103. public function edit()
  104. {
  105. if ($this->request->isPost()) {
  106. $id = input('post.id');
  107. $coupon = $this->Coupon->where(['id' => $id])->find();
  108. if (!$coupon) {
  109. $this->error('参数错误');
  110. }
  111. $no = input('post.no');
  112. if (empty($no)) {
  113. $this->output(1, '编码不能为空');
  114. }
  115. $no_check = $this->Coupon->where('no', $no)->where('id', 'neq', $id)->find();
  116. if (!empty($no_check)) {
  117. $this->output(1, '该编码已存在');
  118. }
  119. $cname = input('post.cname');
  120. if (empty($cname)) {
  121. $this->output(1, '名称不能为空');
  122. }
  123. $supply_price = input('post.supply_price');
  124. $supply_price = floatval($supply_price);
  125. if ($supply_price <= 0) {
  126. $this->output(1, '请输入供应价');
  127. }
  128. $sale_price = input('post.sale_price');
  129. $sale_price = floatval($sale_price);
  130. if ($sale_price <= 0) {
  131. $this->output(1, '请输入销售价');
  132. }
  133. $sale_time = input('post.sale_time');
  134. list($sale_start_time, $sale_end_time) = explode('~', $sale_time);
  135. if (strtotime($sale_end_time) < $this->request->time()) {
  136. $this->output(1, '有效结束时间必须大于当期时间');
  137. }
  138. $coupon->no = $no;
  139. $coupon->cname = $cname;
  140. $coupon->type_id = input('post.type_id');
  141. $coupon->supplier_id = input('post.supplier_id');
  142. $coupon->supply_price = $supply_price;
  143. $coupon->sale_price = $sale_price;
  144. $coupon->sale_time = $sale_time;
  145. $result = $coupon->save();
  146. if (!$result) {
  147. $this->output(1, '保存失败');
  148. }
  149. $this->output(0, '保存成功');
  150. } else {
  151. $id = input('get.id');
  152. $coupon = $this->Coupon->where(['id' => $id])->find();
  153. if (!$coupon) {
  154. $this->error('参数错误');
  155. }
  156. $this->assign('coupon', $coupon);
  157. $CouponType = model('coupon.Type');
  158. $types = $CouponType->select();
  159. $this->assign('types', $types);
  160. $Supplier = model('Supplier');
  161. $suppliers = $Supplier->select();
  162. $this->assign('suppliers', $suppliers);
  163. $this->assign('meta_title', '添加券');
  164. return $this->fetch();
  165. }
  166. }
  167. public function delete()
  168. {
  169. if ($this->request->isPost()) {
  170. $id = input('post.id');
  171. $coupon = $this->Coupon->where(['id' => $id])->find();
  172. if (!$coupon) {
  173. $this->output(1, '参数错误');
  174. }
  175. $result = $coupon->delete();
  176. if (!$result) {
  177. $this->output(1, '删除卡片失败');
  178. }
  179. $this->output(0, '删除成功');
  180. }
  181. }
  182. public function state()
  183. {
  184. if ($this->request->isPost()) {
  185. $id = input('post.id');
  186. $coupon = $this->Coupon->where(['id' => $id])->find();
  187. if (!$coupon) {
  188. $this->output(1, '参数错误');
  189. }
  190. $state = input('post.state');
  191. $coupon->state = $state == 'true' ? 1 : 0;
  192. $result = $coupon->save();
  193. if (!$result) {
  194. $this->output(1, '保存失败');
  195. }
  196. $this->output(0, '保存成功');
  197. }
  198. }
  199. }