Esempio n. 1
0
 public function create()
 {
     $data = array('title' => $_POST['title'], 'require_amount' => $_POST['require_amount'] * 100, 'minus_amount' => $_POST['minus_amount'] * 100, 'rule_type' => empty($_POST['rule_type']) ? 1 : $_POST['rule_type'], 'status' => empty($_POST['status']) ? C('status.common.unverified') : $_POST['status'], 'created_time' => $this->input->server('REQUEST_TIME'), 'updated_time' => $this->input->server('REQUEST_TIME'));
     // 比较减免3000额度
     if ($compare_info = compare_minus_amount($data['minus_amount'])) {
         $this->_return_json($compare_info);
     }
     $id = $this->MCoupon_rules->create($data);
     $response = array('status' => C('tips.code.op_failed'), 'msg' => '规则创建失败');
     if ($id) {
         $response = array('status' => C('tips.code.op_success'), 'msg' => '规则创建成功');
     }
     $this->_return_json($response);
 }
 /**
  * @author: liaoxianwen@ymt360.com
  * @description
  */
 public function create()
 {
     $coupon_info = $this->MCoupons->get_one('*', array('coupon_nums >' => 0, 'id' => $_POST['coupon_id']));
     if ($coupon_info) {
         $rule_info = $this->MCoupon_rules->get_one('*', array('id' => $coupon_info['coupon_rule_id']));
         // 比较减免3000额度
         if ($compare_info = compare_minus_amount($rule_info['minus_amount'])) {
             $this->_return_json($compare_info);
         }
         // 根据优惠券活动的site_id location_id line_ids 来确定用户数量
         $customer_where = array('province_id' => $coupon_info['location_id'], 'status !=' => C('status.common.del'));
         if ($coupon_info['line_ids'] != 0) {
             $line_ids = explode(',', $coupon_info['line_ids']);
             $customer_where['in'] = array('line_id' => $line_ids);
         }
         if (isset($_POST['customer_ids']) && is_array($_POST['customer_ids']) && $_POST['customer_ids']) {
             $customer_where['in']['id'] = $_POST['customer_ids'];
         }
         $customers = $this->MCustomer->get_lists('*', $customer_where);
         if ($customers) {
             $coupon_codes = coupon_code_create(count($customers));
             $coupon_nums = empty($_POST['coupon_nums']) ? 1 : $_POST['coupon_nums'];
             foreach ($customers as $key => $customer) {
                 //用户优惠券的创建
                 $data[] = array('coupon_id' => $_POST['coupon_id'], 'customer_id' => $customer['id'], 'coupon_rule_id' => $coupon_info['coupon_rule_id'], 'coupon_code' => $coupon_codes[$key], 'require_amount' => $rule_info['require_amount'], 'minus_amount' => $rule_info['minus_amount'], 'coupon_nums' => $coupon_nums, 'status' => C('status.common.success'), 'valid_time' => $coupon_info['valid_time'], 'invalid_time' => $coupon_info['invalid_time'], 'created_time' => $this->input->server('REQUEST_TIME'), 'updated_time' => $this->input->server('REQUEST_TIME'));
             }
             $affect_rows = $this->MCustomer_coupons->create_batch($data);
             if ($affect_rows) {
                 // 更新coupons 的coupon_nums coupon_used_nums
                 $coupon_updata = array('coupon_nums' => $coupon_info['coupon_nums'] - 1, 'coupon_used_nums' => $coupon_info['coupon_used_nums'] + 1);
                 $this->MCoupons->update_info($coupon_updata, array('id' => $coupon_info['id']));
                 $total_coupon = $affect_rows * $coupon_nums;
                 $response = array('status' => C('tips.code.op_success'), 'msg' => "券码分发成功,共发放{$affect_rows}个用户,发放了{$total_coupon}");
             }
         } else {
             $response = array('status' => C('tips.code.op_failed'), 'msg' => '券活动信息有误');
         }
     } else {
         $response = array('status' => C('tips.code.op_failed'), 'msg' => '券活动信息有误');
     }
     $this->_return_json($response);
 }