433 行
20 KiB
PHP
可执行文件
433 行
20 KiB
PHP
可执行文件
<?php
|
|
|
|
namespace CashierApi\Controller;
|
|
|
|
use Common\Model\CardBagModel;
|
|
use Common\Model\RetailCardUseCodeModel;
|
|
use Common\Model\RetailMerchantConfigModel;
|
|
use Common\Model\RetailVipCardModel;
|
|
use Common\Model\RetailVipCardRecordModel;
|
|
use Common\Model\RetailVipModel;
|
|
use Common\Model\SubCardModel;
|
|
use Common\Model\SubCardViewModel;
|
|
use Think\Controller;
|
|
|
|
//[1.8.50]
|
|
|
|
class UseCardController extends BaseController
|
|
{
|
|
/**
|
|
* 会员使用次卡(已废弃)
|
|
* @param int $use_code 使用码
|
|
* @return array $res
|
|
*/
|
|
public function vipUseCard()
|
|
{
|
|
// 接受数据
|
|
$use_code = I('post.use_code');
|
|
if (!$use_code || !verifyNumber($use_code)) {
|
|
$this->ajaxReturn(['status' => 0, 'msg' => '请扫描/输入正确使用码']);
|
|
}
|
|
// 实例化模型
|
|
$RetailCardUseCodeModel = new RetailCardUseCodeModel();
|
|
// 判断使用码是否可用
|
|
$find_code = $RetailCardUseCodeModel->getFind(['use_code' => $use_code, 'merchantid' => self::$merchant_id]);
|
|
if (!$find_code) {
|
|
$this->ajaxReturn(['status' => 0, 'msg' => '请扫描正确的使用码']);
|
|
}
|
|
$time = time();
|
|
// 判断是否过期
|
|
if ($time > $find_code['end_time'] || $find_code['status'] == 1) {
|
|
$this->ajaxReturn(['status' => 0, 'msg' => '使用码已过期,请用户刷新使用码']);
|
|
}
|
|
$RetailVipModel = new RetailVipModel();
|
|
// 判断会员
|
|
$vip_info = $RetailVipModel->getFind(['id' => $find_code['vip_id'], 'status' => 1]);
|
|
if (!$vip_info) {
|
|
$this->ajaxReturn(['status' => 0, 'msg' => '会员查询失败']);
|
|
}
|
|
$SubCardViewModel = new SubCardViewModel();
|
|
// 查询用户次卡
|
|
$map = [
|
|
'VipCard.id' => $find_code['vip_card_id'],
|
|
'VipCard.merchantid' => self::$merchant_id,
|
|
'VipCard.vip_id' => $vip_info['id'],
|
|
'VipCard.status' => 0,
|
|
'VipCard.state' => 0,
|
|
];
|
|
$sub_card = $SubCardViewModel->toFind($map);
|
|
if (!$sub_card) {
|
|
$this->ajaxReturn(['status' => 0, 'msg' => '用户次卡查询失败']);
|
|
}
|
|
// 判断有效使用时间
|
|
if ($sub_card['effective_time_type'] == 1) {
|
|
if (!$sub_card['effective_start'] || !$sub_card['effective_end'] || $sub_card['effective_start'] > time() || $sub_card['effective_end'] < time()) {
|
|
$this->ajaxReturn(['status' => 0, 'msg' => '非有效的使用时间']);
|
|
}
|
|
} else if ($sub_card['effective_time_type'] == 2) {
|
|
$sub_card['effective_start'] = $sub_card['addtime'];
|
|
$sub_card['effective_end'] = $sub_card['effective_start'] + ($sub_card['effective_day'] * 86400);
|
|
if ($sub_card['effective_start'] > time() || $sub_card['effective_end'] < time()) {
|
|
$this->ajaxReturn(['status' => 0, 'msg' => '非有效的使用时间']);
|
|
}
|
|
} else if ($sub_card['effective_time_type'] == 3) {
|
|
$sub_card['effective_start'] = $sub_card['addtime'] + 86400;
|
|
$sub_card['effective_end'] = $sub_card['effective_start'] + ($sub_card['effective_day'] * 86400);
|
|
if ($sub_card['effective_start'] > time() || $sub_card['effective_end'] < time()) {
|
|
$this->ajaxReturn(['status' => 0, 'msg' => '非有效的使用时间']);
|
|
}
|
|
}
|
|
|
|
// 判断类型 查询指定门店
|
|
if ($sub_card['card_type'] == 1 && $sub_card['sub_card_id']) {
|
|
$SubCard = new SubCardModel();
|
|
$store_ids = $SubCard->toFindField(['id' => $sub_card['sub_card_id']], 'storeids');
|
|
} else if ($sub_card['card_type'] == 2 && $sub_card['card_bag_id']) {
|
|
$CardBag = new CardBagModel();
|
|
$store_ids = $CardBag->toFindField(['id' => $sub_card['card_bag_id']], 'storeids');
|
|
} else {
|
|
$this->ajaxReturn(['status' => 0, 'msg' => '未知的次卡类型']);
|
|
}
|
|
// 判断门店
|
|
if ($store_ids && ($store_ids != 0 || $store_ids != '0')) {
|
|
$store_ids = explode(',', $store_ids);
|
|
if (!in_array(self::$store_id, $store_ids)) {
|
|
$this->ajaxReturn(['status' => 0, 'msg' => '该次卡无法在当前门店核销']);
|
|
}
|
|
}
|
|
$RetailVipCardRecordModel = new RetailVipCardRecordModel();
|
|
$start_time = strtotime(date("Y-m-d") . ' 00:00:00'); // 今日开始时间
|
|
$end_time = strtotime(date("Y-m-d") . ' 23:59:59'); // 今日结束时间
|
|
$day_date = date('Y-m-d', $time);
|
|
// 统计使用次数
|
|
$map1 = [
|
|
'vip_id' => $vip_info['id'],
|
|
'merchantid' => self::$merchant_id,
|
|
'vip_card_id' => $find_code['vip_card_id'],
|
|
'addtime' => ['between', [$start_time, $end_time]]
|
|
];
|
|
$day_use_num = $RetailVipCardRecordModel->getCount($map1);
|
|
// 判断次数
|
|
if ($sub_card['use_type'] == 1) { // 该次卡每天可用一次
|
|
if ($day_use_num >= 1) {
|
|
$this->ajaxReturn(['status' => 0, 'msg' => '该次卡已达到使用上限']);
|
|
}
|
|
$frequency = round(($sub_card['effective_end'] - $sub_card['effective_start']) / 60 / 60 / 24);
|
|
} else if ($sub_card['use_type'] == 2) { // 自定义使用次数
|
|
if (!$sub_card['frequency'] || !$sub_card['limit_num']) {
|
|
$this->ajaxReturn(['status' => 0, 'msg' => '无效的次卡']);
|
|
}
|
|
if (intval($day_use_num) >= $sub_card['limit_num']) {
|
|
$this->ajaxReturn(['status' => 0, 'msg' => '该次卡已达到使用上限']);
|
|
}
|
|
// 自定义使用判断总次数
|
|
$map2 = [
|
|
'vip_id' => $vip_info['id'],
|
|
'merchantid' => self::$merchant_id,
|
|
'vip_card_id' => $find_code['vip_card_id'],
|
|
];
|
|
$all_num = $RetailVipCardRecordModel->getCount($map2);
|
|
// 判断总次数
|
|
if (intval($all_num) >= $sub_card['frequency']) {
|
|
$this->ajaxReturn(['status' => 0, 'msg' => '该次卡已使用完毕']);
|
|
}
|
|
$frequency = intval($sub_card['frequency']);
|
|
} else if ($sub_card['use_type'] == 3) {
|
|
if (!$sub_card['limit_num']) {
|
|
$this->ajaxReturn(['status' => 0, 'msg' => '无效的次卡']);
|
|
}
|
|
$timeTwo = $time - ($sub_card['limit_num'] * 86400);//当前时间 - 天数时间
|
|
//查询符合条件的最后一条记录,添加时间倒序
|
|
$mapTwo = [
|
|
'vip_id' => $vip_info['id'],
|
|
'merchantid' => self::$merchant_id,
|
|
'vip_card_id' => $find_code['vip_card_id'],
|
|
'addtime' => array('gt', $timeTwo),
|
|
];
|
|
$addtime = $RetailVipCardRecordModel->getOrderFindField($mapTwo, $field = 'addtime', $order = 'addtime desc');
|
|
if ($addtime) {
|
|
$this->ajaxReturn(['status' => 0, 'msg' => '次卡尚未冷却']);
|
|
}
|
|
$frequency = ceil(($sub_card['effective_end'] - $sub_card['effective_start']) / 86400 / ($sub_card['limit_num']));
|
|
}
|
|
$RetailVipCardModel = new RetailVipCardModel();
|
|
// 判断使用时段
|
|
$time_interval = explode(',', $sub_card['time_interval']);
|
|
$day_start = strtotime($day_date . $time_interval[0]);
|
|
$day_end = strtotime($day_date . $time_interval[1]);
|
|
if ($time < $day_start || $time > $day_end) {
|
|
$this->ajaxReturn(['status' => 0, 'msg' => '非使用时段,无法使用']);
|
|
}
|
|
// 增加使用次数
|
|
$set_result = $RetailVipCardModel->toSetInc(['id' => $find_code['vip_card_id']], 'use_num', 1);
|
|
if (!$set_result) {
|
|
$this->ajaxReturn(['status' => 1, 'msg' => '使用失败']);
|
|
}
|
|
// 判断剩余次数是否可以使用
|
|
$use_num = intval($sub_card['use_num'] + 1);
|
|
if ($use_num >= $frequency) {
|
|
$RetailVipCardModel->updateOrder(['id' => $find_code['vip_card_id']], ['state' => 1]);
|
|
}
|
|
// 修改使用码状态
|
|
$save_result = $RetailCardUseCodeModel->updateOrder(['id' => $find_code['id']], ['status' => 1]);
|
|
if (!$save_result) {
|
|
$this->ajaxReturn(['status' => 0, 'msg' => '使用成功,本次使用码未失效,请收银员注意避免重复使用']);
|
|
}
|
|
// 添加使用记录
|
|
$cr = [
|
|
'merchantid' => self::$merchant_id,
|
|
'vip_id' => $vip_info['id'],
|
|
'vip_card_id' => $find_code['vip_card_id'],
|
|
'addtime' => $time,
|
|
'use_store_id' => self::$store_id,
|
|
'cashier_id' => self::$cashier_id,
|
|
];
|
|
$add_result = $RetailVipCardRecordModel->addOrder($cr);
|
|
if ($add_result) {
|
|
$res = ['status' => 1, 'msg' => '使用成功'];
|
|
} else {
|
|
$res = ['status' => 0, 'msg' => '使用成功,使用记录添加失败'];
|
|
}
|
|
$this->ajaxReturn($res);
|
|
}
|
|
|
|
/**
|
|
* 用户次卡列表
|
|
* 2020年12月12日16:50:02
|
|
* @param phone 会员手机号
|
|
* @return array
|
|
*/
|
|
public function userCardList()
|
|
{
|
|
// 接收参数
|
|
$phone = I('post.phone');
|
|
$page = I('post.page') ? I('post.page') : 1;
|
|
$limit = I('post.limit') ? I('post.limit') : 10;
|
|
// 判断
|
|
if (!$page || !$limit || !verifyMobilePhone($phone)) {
|
|
$this->ajaxReturn(['status' => 0, 'msg' => '参数错误']);
|
|
}
|
|
// 获取会员
|
|
$RetailVipModel = new RetailVipModel();
|
|
$vipInfo = $RetailVipModel->getFind(['mobile_phone' => $phone, 'merchantid' => self::$merchant_id], 'id,name,nickname,mobile_phone,headimgurl');
|
|
if (empty($vipInfo['id'])) {
|
|
$this->ajaxReturn(['status' => 0, 'msg' => '请先绑定会员卡']);
|
|
}
|
|
if (!$vipInfo['name']) $vipInfo['name'] = '-';
|
|
if (!$vipInfo['nickname']) $vipInfo['nickname'] = '-';
|
|
|
|
// 查询
|
|
$SubCardApi = new \Api\Controller\SubCardController();
|
|
$res = $SubCardApi->userCardList(self::$merchant_id, $vipInfo['id'], $page, $limit);
|
|
if (empty($res['card_list']) && $page == 1) {
|
|
$this->ajaxReturn(['status' => 0, 'msg' => '用户暂无次卡']);
|
|
}
|
|
$res['vipInfo'] = $vipInfo;
|
|
$this->ajaxReturn($res);
|
|
}
|
|
|
|
/**
|
|
* 次卡核销
|
|
* 2020年12月12日10:50:03
|
|
* @param card_id 次卡id
|
|
* @param vip_id 会员id
|
|
* @param use_num 核销次数
|
|
* @param use_code 核销码
|
|
*/
|
|
public function timeCardVerify()
|
|
{
|
|
$cardId = I('post.card_id');
|
|
if (empty($cardId)) {
|
|
$this->ajaxReturn(['status' => 0, 'msg' => '请选择次卡']);
|
|
}
|
|
$vipId = I('post.vip_id');
|
|
$useNum = I('post.use_num');
|
|
$useCode = I('post.use_code');
|
|
if (empty($useNum)) {
|
|
$this->ajaxReturn(['status' => 0, 'msg' => '请选择核销次数']);
|
|
}
|
|
$SubCardViewModel = new SubCardViewModel();
|
|
$RetailVipCardModel = new RetailVipCardModel();
|
|
$VipCardRecordModel = new RetailVipCardRecordModel();
|
|
$RetailCardUseCodeModel = new RetailCardUseCodeModel();
|
|
|
|
$time = time();
|
|
$MerchantConfigModel = new RetailMerchantConfigModel();
|
|
$use_sub_card_code = $MerchantConfigModel->getFindField(['merchant_id' => self::$merchant_id], 'use_sub_card_code'); //商户配置
|
|
//判断次卡的使用是否需要核销码
|
|
if ($use_sub_card_code == 1 || !$vipId) {
|
|
// 判断使用码是否可用
|
|
$find_code = $RetailCardUseCodeModel->getFind(['use_code' => $useCode, 'merchantid' => self::$merchant_id, 'status' => 0]);
|
|
if (!$find_code) {
|
|
$this->ajaxReturn(['status' => 0, 'msg' => '请扫描/输入正确的核销码']);
|
|
}
|
|
// 判断是否过期
|
|
if ($time > $find_code['end_time'] || $find_code['status'] == 1) {
|
|
$this->ajaxReturn(['status' => 0, 'msg' => '使用码已过期,请用户刷新使用码']);
|
|
}
|
|
// 核销码进入没有会员id
|
|
if (!$vipId) {
|
|
$RetailVipModel = new RetailVipModel();
|
|
// 判断会员
|
|
$vip_info = $RetailVipModel->getFind(['id' => $find_code['vip_id'], 'status' => 1]);
|
|
if (!$vip_info) {
|
|
$this->ajaxReturn(['status' => 0, 'msg' => '会员查询失败']);
|
|
}
|
|
$vipId = $vip_info['id'];
|
|
}
|
|
|
|
}
|
|
// 查询用户次卡
|
|
$map = [
|
|
'VipCard.id' => $cardId,
|
|
'VipCard.merchantid' => self::$merchant_id,
|
|
'VipCard.vip_id' => $vipId,
|
|
'VipCard.status' => 0,
|
|
'VipCard.state' => 0,
|
|
'VipCard.start_time' => ['elt', $time],
|
|
'VipCard.end_time' => ['egt', $time],
|
|
];
|
|
$sub_card = $SubCardViewModel->toFind($map);
|
|
if (!$sub_card) {
|
|
$this->ajaxReturn(['status' => 0, 'msg' => '用户次卡查询失败']);
|
|
}
|
|
|
|
// 判断类型 查询指定门店
|
|
if ($sub_card['card_type'] == 1 && $sub_card['sub_card_id']) {
|
|
$SubCard = new SubCardModel();
|
|
$store_ids = $SubCard->toFindField(['id' => $sub_card['sub_card_id']], 'storeids');
|
|
} else if ($sub_card['card_type'] == 2 && $sub_card['card_bag_id']) {
|
|
$CardBag = new CardBagModel();
|
|
$store_ids = $CardBag->toFindField(['id' => $sub_card['card_bag_id']], 'storeids');
|
|
} else {
|
|
$this->ajaxReturn(['status' => 0, 'msg' => '未知的次卡类型']);
|
|
}
|
|
// 判断门店
|
|
if ($store_ids && ($store_ids != 0 || $store_ids != '0')) {
|
|
$store_ids = explode(',', $store_ids);
|
|
if (!in_array(self::$store_id, $store_ids)) {
|
|
$this->ajaxReturn(['status' => 0, 'msg' => '次卡无法在当前门店核销']);
|
|
}
|
|
}
|
|
$start_time = strtotime(date("Y-m-d") . ' 00:00:00'); // 今日开始时间
|
|
$end_time = strtotime(date("Y-m-d") . ' 23:59:59'); // 今日结束时间
|
|
$day_date = date('Y-m-d', $time);
|
|
// 判断使用时段
|
|
$time_interval = explode(',', $sub_card['time_interval']);
|
|
$day_start = strtotime($day_date . $time_interval[0]);
|
|
$day_end = strtotime($day_date . $time_interval[1]);
|
|
if ($time < $day_start || $time > $day_end) {
|
|
$this->ajaxReturn(['status' => 0, 'msg' => '非使用时段,无法使用']);
|
|
}
|
|
// 统计使用次数
|
|
$useMap = [
|
|
'vip_id' => $vipId,
|
|
'merchantid' => self::$merchant_id,
|
|
'vip_card_id' => $cardId,
|
|
'addtime' => ['between', [$start_time, $end_time]],
|
|
];
|
|
// 判断次数
|
|
$less_num = 0;//该次卡的剩余使用次数
|
|
switch ($sub_card['use_type']) {
|
|
case '1':
|
|
// 每天可用一次
|
|
$day_use_num = $VipCardRecordModel->getCount($useMap);
|
|
if ($day_use_num >= 1) {
|
|
$this->ajaxReturn(['status' => 0, 'msg' => '该次卡已达到使用上限']);
|
|
}
|
|
$less_num = ceil(($sub_card['end_time'] - $sub_card['start_time']) / 86400);
|
|
break;
|
|
case '2':
|
|
//自定义使用次数
|
|
$day_use_num = $VipCardRecordModel->getCount($useMap);
|
|
if (intval($day_use_num) >= $sub_card['limit_num']) {
|
|
$this->ajaxReturn(['status' => 0, 'msg' => '该次卡已达到使用上限']);
|
|
}
|
|
if (bcadd($sub_card['use_num'], $useNum, 0) > $sub_card['frequency']) {
|
|
$this->ajaxReturn(['status' => 0, 'msg' => '超出次卡使用上限']);
|
|
}
|
|
$less_num = bcsub($sub_card['frequency'], bcadd($sub_card['use_num'], $useNum, 0), 0);
|
|
break;
|
|
case '3':
|
|
//几天可使用一次
|
|
if ($useNum > 1) {
|
|
$this->ajaxReturn(['status' => 0, 'msg' => '超出次卡使用上限']);
|
|
}
|
|
unset($useMap['addtime']);
|
|
$pre_use_time = $VipCardRecordModel->getOrderFindField($useMap, 'addtime', 'id desc');
|
|
|
|
$pre_use_start_time = strtotime(date('Y-m-d', $pre_use_time));//15 00:00:00
|
|
$min_pre_use_start_time = $start_time - ($sub_card['limit_num'] * 86400);//18 00:00:00 - 3*86400 = 15 00:00:00
|
|
if (empty($pre_use_time) || $pre_use_start_time <= $min_pre_use_start_time) {
|
|
$less_num = ceil(($sub_card['end_time'] - ($start_time + 86400)) / 86400 / ($sub_card['limit_num']));
|
|
} else {
|
|
$this->ajaxReturn(['status' => 0, 'msg' => '次卡尚未冷却']);
|
|
}
|
|
break;
|
|
}
|
|
|
|
// 增加已使用次数
|
|
$set_result = $RetailVipCardModel->toSetInc(['id' => $cardId], 'use_num', $useNum);
|
|
if (!$set_result) {
|
|
$this->ajaxReturn(['status' => 1, 'msg' => '使用失败']);
|
|
}
|
|
// 判断剩余次数是否可以使用
|
|
if ($less_num <= 0) {
|
|
$RetailVipCardModel->updateOrder(['id' => $cardId], ['state' => 1]);
|
|
}
|
|
// 添加使用记录
|
|
for ($i = 0; $i < $useNum; $i++) {
|
|
$cr = [
|
|
'vip_id' => $vipId,
|
|
'addtime' => $time,
|
|
'merchantid' => self::$merchant_id,
|
|
'cashier_id' => self::$cashier_id,
|
|
'vip_card_id' => $cardId,
|
|
'use_store_id' => self::$store_id,
|
|
];
|
|
$VipCardRecordModel->addOrder($cr);
|
|
}
|
|
if ($use_sub_card_code == 1 || !$vipId) {
|
|
// 作废核销码
|
|
$RetailCardUseCodeModel->updateOrder(['id' => $find_code['id']], ['status' => 1]);
|
|
}
|
|
|
|
$this->ajaxReturn(['status' => 1, 'msg' => '次卡核销成功']);
|
|
}
|
|
|
|
/**
|
|
* 根据核销码获取对应次卡
|
|
*/
|
|
public function getCardInfo()
|
|
{
|
|
// 接受数据
|
|
$use_code = I('post.use_code');
|
|
if (!$use_code || !verifyNumber($use_code)) {
|
|
$this->ajaxReturn(['status' => 0, 'msg' => '请扫描/输入正确使用码']);
|
|
}
|
|
// 实例化模型
|
|
$RetailVipModel = new RetailVipModel();
|
|
$RetailCardUseCodeModel = new RetailCardUseCodeModel();
|
|
// 判断使用码是否可用
|
|
$find_code = $RetailCardUseCodeModel->getFind(['use_code' => $use_code, 'merchantid' => self::$merchant_id, 'status' => 0]);
|
|
if (!$find_code) {
|
|
$this->ajaxReturn(['status' => 0, 'msg' => '请扫描/输入正确的核销码']);
|
|
}
|
|
$time = time();
|
|
// 判断是否过期
|
|
if ($time > $find_code['end_time'] || $find_code['status'] == 1) {
|
|
$this->ajaxReturn(['status' => 0, 'msg' => '使用码已过期,请用户刷新使用码']);
|
|
}
|
|
// 判断会员
|
|
$vip_id = $RetailVipModel->getFindField(['id' => $find_code['vip_id'], 'status' => 1], 'id');
|
|
if (!$vip_id) {
|
|
$this->ajaxReturn(['status' => 0, 'msg' => '会员查询失败']);
|
|
}
|
|
$page = 1;
|
|
$limit = 10;
|
|
$SubCardApi = new \Api\Controller\SubCardController();
|
|
$result = $SubCardApi->userCardList(self::$merchant_id, $vip_id, $page, $limit, $find_code['vip_card_id']);
|
|
$this->ajaxReturn($result);
|
|
}
|
|
}
|