123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- <?php
- namespace app\admin\controller;
- /**
- * 券
- */
- class Coupon extends Admin
- {
- protected $Coupon = null;
- public function init()
- {
- parent::init();
- $this->Coupon = model('Coupon');
- }
- public function index()
- {
- $this->assign('meta_title', '电子券管理');
- return $this->fetch();
- }
- public function load()
- {
- $page = input('get.page');
- $limit = input('get.limit');
- $where = [];
- $search = input('get.search');
- if (!empty($search)) {
- $where['cname|id'] = ['like', '%' . $search . '%'];
- }
- $list = $this->Coupon
- ->where($where)->order('id desc')->paginate($limit, false, ['page' => $page]);
- $data = [];
- foreach ($list as $key => $value) {
- $data[$key]['id'] = $value['id'];
- $data[$key]['no'] = $value['no'];
- $data[$key]['cname'] = $value['cname'];
- $type = $value['type'];
- $data[$key]['type_cname'] = $type['cname'];
- $supplier = $value['supplier'];
- $data[$key]['supplier'] = $supplier['cname'] . '[' . $supplier['id'] . ']';
- $data[$key]['supply_price'] = $value['supply_price'];
- $data[$key]['sale_price'] = $value['sale_price'];
- $data[$key]['total_num'] = $value['total_num'];
- $data[$key]['sold_num'] = $value['sold_num'];
- $data[$key]['unsold_num'] = $value['unsold_num'];
- $data[$key]['sale_time'] = $value['sale_time'];
- $data[$key]['create_time'] = $value['create_time'];
- $data[$key]['state'] = $value['state'];
- }
- $this->output(0, '获取成功', $data, $list->total());
- }
- public function add()
- {
- if ($this->request->isPost()) {
- $no = input('post.no');
- if (empty($no)) {
- $this->output(1, '编码不能为空');
- }
- $no_check = $this->Coupon->where('no', $no)->find();
- if (!empty($no_check)) {
- $this->output(1, '该编码已存在');
- }
- $cname = input('post.cname');
- if (empty($cname)) {
- $this->output(1, '名称不能为空');
- }
- $supply_price = input('post.supply_price');
- $supply_price = floatval($supply_price);
- if ($supply_price <= 0) {
- $this->output(1, '请输入供应价');
- }
- $sale_price = input('post.sale_price');
- $sale_price = floatval($sale_price);
- if ($sale_price <= 0) {
- $this->output(1, '请输入销售价');
- }
- $sale_time = input('post.sale_time');
- list($sale_start_time, $sale_end_time) = explode('~', $sale_time);
- if (strtotime($sale_end_time) < $this->request->time()) {
- $this->output(1, '有效结束时间必须大于当期时间');
- }
- $this->Coupon->no = $no;
- $this->Coupon->cname = $cname;
- $this->Coupon->type_id = input('post.type_id');
- $this->Coupon->supplier_id = input('post.supplier_id');
- $this->Coupon->supply_price = $supply_price;
- $this->Coupon->sale_price = $sale_price;
- $this->Coupon->sale_time = $sale_time;
- $result = $this->Coupon->save();
- if (!$result) {
- $this->output(1, '保存失败');
- }
- $this->output(0, '保存成功');
- } else {
- $CouponType = model('coupon.Type');
- $types = $CouponType->select();
- $this->assign('types', $types);
- $Supplier = model('Supplier');
- $suppliers = $Supplier->select();
- $this->assign('suppliers', $suppliers);
- $this->assign('meta_title', '添加券');
- return $this->fetch();
- }
- }
- public function edit()
- {
- if ($this->request->isPost()) {
- $id = input('post.id');
- $coupon = $this->Coupon->where(['id' => $id])->find();
- if (!$coupon) {
- $this->error('参数错误');
- }
- $no = input('post.no');
- if (empty($no)) {
- $this->output(1, '编码不能为空');
- }
- $no_check = $this->Coupon->where('no', $no)->where('id', 'neq', $id)->find();
- if (!empty($no_check)) {
- $this->output(1, '该编码已存在');
- }
- $cname = input('post.cname');
- if (empty($cname)) {
- $this->output(1, '名称不能为空');
- }
- $supply_price = input('post.supply_price');
- $supply_price = floatval($supply_price);
- if ($supply_price <= 0) {
- $this->output(1, '请输入供应价');
- }
- $sale_price = input('post.sale_price');
- $sale_price = floatval($sale_price);
- if ($sale_price <= 0) {
- $this->output(1, '请输入销售价');
- }
- $sale_time = input('post.sale_time');
- list($sale_start_time, $sale_end_time) = explode('~', $sale_time);
- if (strtotime($sale_end_time) < $this->request->time()) {
- $this->output(1, '有效结束时间必须大于当期时间');
- }
- $coupon->no = $no;
- $coupon->cname = $cname;
- $coupon->type_id = input('post.type_id');
- $coupon->supplier_id = input('post.supplier_id');
- $coupon->supply_price = $supply_price;
- $coupon->sale_price = $sale_price;
- $coupon->sale_time = $sale_time;
- $result = $coupon->save();
- if (!$result) {
- $this->output(1, '保存失败');
- }
- $this->output(0, '保存成功');
- } else {
- $id = input('get.id');
- $coupon = $this->Coupon->where(['id' => $id])->find();
- if (!$coupon) {
- $this->error('参数错误');
- }
- $this->assign('coupon', $coupon);
- $CouponType = model('coupon.Type');
- $types = $CouponType->select();
- $this->assign('types', $types);
- $Supplier = model('Supplier');
- $suppliers = $Supplier->select();
- $this->assign('suppliers', $suppliers);
- $this->assign('meta_title', '添加券');
- return $this->fetch();
- }
- }
- public function delete()
- {
- if ($this->request->isPost()) {
- $id = input('post.id');
- $coupon = $this->Coupon->where(['id' => $id])->find();
- if (!$coupon) {
- $this->output(1, '参数错误');
- }
- $result = $coupon->delete();
- if (!$result) {
- $this->output(1, '删除卡片失败');
- }
- $this->output(0, '删除成功');
- }
- }
- public function state()
- {
- if ($this->request->isPost()) {
- $id = input('post.id');
- $coupon = $this->Coupon->where(['id' => $id])->find();
- if (!$coupon) {
- $this->output(1, '参数错误');
- }
- $state = input('post.state');
- $coupon->state = $state == 'true' ? 1 : 0;
- $result = $coupon->save();
- if (!$result) {
- $this->output(1, '保存失败');
- }
- $this->output(0, '保存成功');
- }
- }
- }
|