CustomerOrder = model('customer.Order'); } 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['c.cname|c.id'] = ['like', '%' . $search . '%']; } $list = $this->CustomerOrder->field('co.*') ->alias('co') ->join('Customer c', 'c.id = co.customer_id') ->where($where)->order('co.id desc')->paginate($limit, false, ['page' => $page]); $data = []; foreach ($list as $key => $value) { $data[$key]['id'] = $value['id']; $customer = $value['customer']; $data[$key]['customer'] = $customer['cname'] . '[' . $customer['id'] . ']'; $data[$key]['denomination'] = $value['denomination']; $data[$key]['pay_amount'] = $value['pay_amount']; $data[$key]['num'] = $value['num']; $data[$key]['discount'] = $value['discount']; $data[$key]['expire_time'] = $value['expire_time']; $card_number = $value['card_number']; $card_number_str = ''; foreach ($card_number as $k => $val) { $card_number_str .= $val['start'] . '~' . $val['end'] . '
'; } $data[$key]['card_number'] = $card_number_str; $goods_type = $value['goods_type']; $goods_type_str = ''; foreach ($goods_type as $k => $val) { $goods_type_str .= '【' . $val['cname'] . '】'; } $data[$key]['goods_type'] = empty($goods_type_str) ? '全品类' : $goods_type_str; $store_type = $value['store_type']; $store_type_str = ''; foreach ($store_type as $k => $val) { $store_type_str .= '【' . $val['cname'] . '】'; } $data[$key]['store_type'] = empty($store_type_str) ? '全品类' : $store_type_str; $data[$key]['create_time'] = $value['create_time']; } $this->output(0, '获取成功', $data, $list->total()); } public function add() { if ($this->request->isPost()) { $customer_id = input('post.customer_id'); if (empty($customer_id)) { $this->output(1, '参数错误'); } $num = input('post.num'); $num = intval($num); if ($num <= 0) { $this->output(1, '数量必须大于o'); } $expire_time = input('post.expire_time'); if (empty($expire_time)) { $this->output(1, '有效期不能为空'); } list($expire_start_time, $expire_end_time) = explode('~', $expire_time); if (strtotime($expire_end_time) < $this->request->time()) { $this->output(1, '有效结束时间必须大于当期时间'); } $denomination = input('post.denomination'); $denomination = floatval($denomination); if ($denomination <= 0) { $this->output(1, '请输入面值'); } $pay_amount = input('post.pay_amount'); $pay_amount = floatval($pay_amount); if ($pay_amount <= 0) { $this->output(1, '请输入实付金额'); } $goods_type = input('post.goods_type/a', []); $store_type = input('post.store_type/a', []); $card_number_arr = input('post.card_number/a', []); $total_num = array_sum(array_column($card_number_arr, 'num')); if ($total_num != $num) { $this->output(1, '卡号数量和销售数量不相等'); } $this->CustomerOrder->startTrans(); $CardNumber = model('card.Number'); foreach ($card_number_arr as $key => $value) { for ($i = $value['start']; $i <= $value['end']; $i++) { $card_number = $CardNumber->where(['id' => $i, 'customer_id' => 0])->find(); if (!$card_number) { $this->CustomerOrder->rollback(); $this->output(1, '序号[' . $i . ']已经售出'); } $card_number->customer_id = $customer_id; $card_number->goods_type = $goods_type; $card_number->store_type = $store_type; $card_number->denomination = $denomination; $card_number->balance = $denomination; $card_number->expire_start_time = strtotime($expire_start_time); $card_number->expire_end_time = strtotime($expire_end_time); $card_number->sale_time = $this->request->time(); $result = $card_number->save(); if (!$result) { $this->CustomerOrder->rollback(); $this->output(1, '卡号失败'); } } } $this->CustomerOrder->customer_id = $customer_id; $this->CustomerOrder->num = $total_num; $this->CustomerOrder->expire_time = $expire_time; $this->CustomerOrder->denomination = $denomination; $this->CustomerOrder->pay_amount = $pay_amount; $this->CustomerOrder->discount = round($pay_amount / $denomination, 2) * 100; $this->CustomerOrder->goods_type = $goods_type; $this->CustomerOrder->store_type = $store_type; $this->CustomerOrder->card_number = $card_number_arr; $result = $this->CustomerOrder->save(); if (!$result) { $this->CustomerOrder->rollback(); $this->output(1, '保存失败'); } $this->CustomerOrder->commit(); $this->output(0, '保存成功'); } else { //用户信息 $Customer = model('Customer'); $customers = $Customer->select(); $this->assign('customers', $customers); //店铺分类 $checkHost = config('mall_url'); $url = $checkHost . 'api/store/get_store_class'; $store_class = common_curl($url, []); $store_class = $store_class['result']['store_class']; $this->assign('store_class',$store_class); $this->assign('meta_title', '添加订单'); return $this->fetch(); } } }