文件
mashangyoujiu/Application/Common/Business/ShopCoupon.class.php

1319 行
57 KiB
PHP
可执行文件

<?php
namespace Common\Business;
//[1.7.14]
use Common\Model\RetailMerchantConfigModel;
use Common\Model\RetailPaySetModel;
use Common\Model\RetailShoppingModel;
use Common\Model\RetailSkuGoodsModel;
use Common\Model\RetailStoreXcxConfigModel;
use ShopCoupon\Model\ShopCouponModel;
use ShopCoupon\Model\ShopCouponRecordModel;
/**
* 商家券类
* Class ShopCoupon
* [
* 查询用户券是否使用状态 getUserShopCouponState
* 判断用户是否领取商家券以及券详情 userCoupon
* 未领取的商家券详情 notReceivedCouponRule
* 已领取商家券规则详情 shopCouponRule
*券详情字段转换 couponTransformation
* 收银机登录会员可用商家券 getVipShopCouponList
* 收银机下单商家券使用 cashPlaceOrder
* 将优惠后的商品信息替换原来购买的商品信息 sellDataArrayReplace
* 获取参与优惠的商品信息 sellDataArray_new
* 小程序下单商家券使用 xcxPlaceOrder
* 小程序下单选择可用商家券 xcxGetUserPayShopCoupon
* 获取小程序普通下单获取商家券所需的商品信息 getShoppingGoods
* 核销成功用户商家券更新 couponRecordUpdate
* 核销商家券 verificationCoupon
* 查询商家券信息 getCouponInfo
* 商家券条件验证 verificationCouponTermVerify
* 用户商家券列表 userShopCouponList
* 领取商家券成功后添加领取记录 receiveCouponCallback
* 领券签名与商户号 grantCouponSign
* 领券签名与商户号参数验证 grantCouponSignVerify
* 商家券列表 couponList
* ]
* @package Common\Business
*/
class ShopCoupon
{
/**
* 查询用户券是否使用状态
* @param $param
* [
* coupon_id
* ]
* @return mixed
*/
public function getUserShopCouponState($param)
{
try {
$ShopCouponRecordModel = new ShopCouponRecordModel();
$used_type = $ShopCouponRecordModel->getFindField([
'id' => $param['coupon_id']
], "used_type");
return callBack(statusCode()['success'], "查询成功", ['used_type' => $used_type]);
} catch (\Exception $e) {
return callBack(statusCode()['success'], "查询失败");
}
}
/**
* 商家小程序立即使用验证
* @param $param
* [
* coupon_id 用户商家券id
* ]
* @return mixed
*/
public function useShopCoupon($param)
{
try {
$fields = 'r.id,r.coupon_code,r.used_type,r.verification_type,r.add_time,s.app_id,s.type,s.user_type,s.available_type,s.stock_type,
s.available_begin_time,s.available_end_time,s.valid_time_type,s.coupon_use_rule,s.store_type,s.store_id_gather,
s.goods_type,s.goods_id_gather,s.stock_name';
$couponRecord = new ShopCouponRecordModel();
$shop_coupon = $couponRecord->verificationCouponInfo($fields, [
'r.id' => $param['coupon_id']
]);
} catch (\Exception $e) {
return callBack(statusCode()['error'], "查询失败");
}
$verifyResult = $this->verificationCouponTermVerify([
'shop_coupon' => $shop_coupon,
'verification_available_type' => 1, //小程序使用
'verify_goods_type' => 2, //不需要验证金额
]);//条件验证
return $verifyResult;
}
/**
* 判断用户是否领取商家券以及券详情
* @param $param
* [
* id 券主键id
* merchant_id
* xcx_id
* ]
* @return mixed
*/
public function userCoupon($param)
{
$ShopCouponRecordModel = new ShopCouponRecordModel();
$shop_coupon_id = $ShopCouponRecordModel->verificationCouponInfo('r.id', [
'r.coupon_id' => $param['id'],
'r.xcx_id' => $param['xcx_id']
]);
if (empty($shop_coupon_id)) {
$result = $this->notReceivedCouponRule($param);
$result['data']['type'] = 1; //未领取
} else {
//已领取
$result = $this->shopCouponRule(['coupon_id' => $param['id']]);
$result['data']['info'] = $result['data']['shop_coupon'];
$result['data']['type'] = 2; //已领取
}
return $result;
}
/**
* 未领取的商家券详情
* @param $param
* [
* id 商家券id
* merchant_id
* xcx_id
* ]
* @return mixed
*/
public function notReceivedCouponRule($param)
{
try {
$fields = 'stock_name,stock_id,available_type,available_begin_time,available_end_time,valid_time_type,
coupon_use_rule,store_type,store_id_gather,goods_type,goods_id_gather,user_type,type,belong_merchant';
$ShopCouponModel = new ShopCouponModel();
$shop_coupon = $ShopCouponModel->getFind([
'id' => $param['id']
], $fields);
} catch (\Exception $e) {
return callBack(statusCode()['error'], "查询异常");
}
if (empty($shop_coupon)) {
return callBack(statusCode()['error'], "未找到该商家券");
}
$shop_coupon = $this->couponTransformation($shop_coupon);
try {
$paySetModel = new RetailPaySetModel();
$paySet = $paySetModel->getFindMerchantPaySet($param['merchant_id']);
if ($paySet['status'] == 0) {
return callBack(statusCode()['error'], "商户未配置支付");
}
} catch (\Exception $e) {
return callBack(statusCode()['error'], "商户未配置支付");
}
$sign = $this->grantCouponSign([
'merchant_id' => $param['merchant_id'],
'shop_coupon' => $shop_coupon,
'paySet' => $paySet
]);
$send_coupon_param = [
[
'stock_id' => $shop_coupon['stock_id'],
'out_request_no' => $sign['data']['out_request_no']
]
];
$shop_coupon['send_coupon_param'] = $send_coupon_param;
$shop_coupon['send_coupon_merchant'] = $shop_coupon['belong_merchant'];
$shop_coupon['sign'] = $sign['data']['sign'];
$title = 'http';
if ($_SERVER['HTTPS'] == 'on') {
$title .= 's';
}
$web_url = $title . '://' . $_SERVER['HTTP_HOST'] . '/index.php/Vip/CouponAuth/couponAuth';
return callBack(statusCode()['success'], "查询成功", ['info' => $shop_coupon, 'web_url' => $web_url]);
}
/**
* 已领取商家券规则详情
* @param $param
* [
* coupon_id 用户商家券id
* ]
* @return mixed
*/
public function shopCouponRule($param)
{
$fields = 's.stock_name,s.available_type,s.available_begin_time,s.available_end_time,s.valid_time_type,
s.coupon_use_rule,s.store_type,s.store_id_gather,s.goods_type,s.goods_id_gather,r.coupon_code,r.add_time';
$ShopCouponRecordModel = new ShopCouponRecordModel();
$shop_coupon = $ShopCouponRecordModel->verificationCouponInfo($fields, [
'r.coupon_id' => $param['coupon_id']
]);
if (empty($shop_coupon)) {
return callBack(statusCode()['error'], "未找到该商家券");
}
$shop_coupon = $this->couponTransformation($shop_coupon);
$shop_coupon['add_time'] = date('Y.m.d H:i:s', $shop_coupon['add_time']);
$title = 'http';
if ($_SERVER['HTTPS'] == 'on') {
$title .= 's';
}
$web_url = $title . '://' . $_SERVER['HTTP_HOST'] . '/index.php/Vip/CouponAuth/couponAuth';
return callBack(statusCode()['success'], "查询成功", ['shop_coupon' => $shop_coupon, 'web_url' => $web_url]);
}
/**
* 券详情字段转换
* @param $shop_coupon 券信息
* @return mixed
*/
public function couponTransformation($shop_coupon)
{
$available_type = explode(',', $shop_coupon['available_type']);
if (count($available_type) > 1) {
$shop_coupon['available_type_name'] = '线上,门店通用券';
} else {
if (in_array(1, $available_type)) {
$shop_coupon['available_type_name'] .= '线上';
} else if (in_array(2, $available_type)) {
$shop_coupon['available_type_name'] .= '门店';
}
}
$coupon_use_rule = json_decode($shop_coupon['coupon_use_rule'], true);
$shop_coupon['discount_amount'] = $coupon_use_rule['fixed_normal_coupon']['discount_amount']; //优惠金额
$shop_coupon['transaction_minimum'] = $coupon_use_rule['fixed_normal_coupon']['transaction_minimum']; //消费门槛
if (!empty($shop_coupon['valid_time_type'])) {
switch ($shop_coupon['valid_time_type']) {
case 1:
//无规律
$irregular_time = $coupon_use_rule['coupon_available_time']['irregulary_avaliable_time']['irregular_time'];
$available_begin_time = str_replace("T", " ", $irregular_time[0]);
$available_end_time = str_replace("T", " ", $irregular_time[1]);
$available_begin_time = str_replace("-", ".", $available_begin_time);
$available_end_time = str_replace("-", ".", $available_end_time);
$shop_coupon['user_time'] = $available_begin_time . '-' . $available_end_time;
break;
case 2:
//固定时间段
$available_day_time = $coupon_use_rule['coupon_available_time']['available_week']['available_day_time'];
$week_day = $coupon_use_rule['coupon_available_time']['available_week']['week_day'];
$available_day_after_receive = $coupon_use_rule['coupon_available_time']['available_day_after_receive']; //多少天内有效
$begin_time = str_replace('-', '.', $available_day_time['begin_time']);
$end_time = str_replace('-', '.', $available_day_time['end_time']);
$shop_coupon['time_slot'] = $begin_time . '-' . $end_time;
if (!empty($week_day)) {
foreach ($week_day as $week => $weekVal) {
switch ($weekVal) {
case 1:
$week_day[$week] = "周一";
break;
case 2:
$week_day[$week] = "周二";
break;
case 3:
$week_day[$week] = "周三";
break;
case 4:
$week_day[$week] = "周四";
break;
case 5:
$week_day[$week] = "周五";
break;
case 6:
$week_day[$week] = "周六";
break;
case 0:
$week_day[$week] = "周日";
break;
}
}
$shop_coupon['week_day'] = implode(',', $week_day);
}
if (!empty($available_day_after_receive) && $available_day_after_receive > 0) {
$shop_coupon['available_day_after_receive'] = $available_day_after_receive . "天内有效";
}
break;
case 3:
//领取后n天后有效
$wait_days_after_receive = $coupon_use_rule['coupon_available_time']['wait_days_after_receive'];
$shop_coupon['wait_days_after_receive'] = $wait_days_after_receive . "天后生效";
$available_day_after_receive = $coupon_use_rule['coupon_available_time']['available_day_after_receive']; //多少天内有效
if (!empty($available_day_after_receive) && $available_day_after_receive > 0) {
$shop_coupon['available_day_after_receive'] = $available_day_after_receive . "天内有效";
}
break;
}
}
$available_begin_time = date('Y.m.d H:i:s', $shop_coupon['available_begin_time']); //开始时间
$available_end_time = date('Y.m.d H:i:s', $shop_coupon['available_end_time']); //结束时间
$shop_coupon['time'] = $available_begin_time . '-' . $available_end_time;
switch ($shop_coupon['store_type']) {
case 1:
$shop_coupon['store_type_name'] = '所有门店';
break;
case 2:
$StoreClass = new Store();
$storeResult = $StoreClass->storeNameList([
'store_id_gather' => $shop_coupon['store_id_gather']
]);
if ($storeResult['status'] == 1) {
$shop_coupon['store_type_name'] = '可使用门店为' . implode(',', $storeResult['data']['list']);
}
break;
}
switch ($shop_coupon['goods_type']) {
case 1:
$shop_coupon['goods_type_name'] = '所有商品';
break;
case 2:
if (!empty($shop_coupon['goods_id_gather'])) {
$GoodsClass = new Goods();
$goodsResult = $GoodsClass->GoodsNameList([
'goods_id_gather' => $shop_coupon['goods_id_gather']
]);
$shop_coupon['goods_type_name'] = '可使用商品为' . implode(',', $goodsResult['data']['list']);
}
break;
}
return $shop_coupon;
}
/**
* 收银机登录会员可用商家券
* @param $param
* [
* merchant_id
* store_id
* xcx_id
* h_coupon_id_redis reids中正在使用的商家券
* ]
* @return mixed
*/
public function getVipShopCouponList($param)
{
try {
$fields = 'r.id,r.coupon_code,r.used_type,r.verification_type,r.add_time,s.app_id,s.type,s.user_type,s.available_type,s.stock_type,
s.available_begin_time,s.available_end_time,s.valid_time_type,s.coupon_use_rule,s.store_type,s.store_id_gather,
s.goods_type,s.goods_id_gather,s.stock_name';
$where = [
'r.merchant_id' => $param['merchant_id'],
'r.used_type' => 1,
'r.verification_type' => 1,
'r.xcx_id' => $param['xcx_id']
];
if (!empty($param['h_coupon_id_redis'])) {
$where['r.id'] = ['not in', $param['h_coupon_id_redis']];
}
$where['_string'] = "(FIND_IN_SET(2,s.available_type)) AND ((FIND_IN_SET(" . $param['store_id'] . ",s.store_id_gather) and s.store_type=2) OR s.store_type=1)";
$shopCouponRecordModel = new ShopCouponRecordModel();
$user_shop_coupon = $shopCouponRecordModel->PlaceOrderCouponList($fields, $where);
} catch (\Exception $e) {
return callBack(statusCode()['success'], "查询成功", ['list' => []]);
}
$shop_coupon = []; //可用商家券
$SaleGoodsClass = new SaleGoods();
foreach ($user_shop_coupon as $key => &$val) {
$result = $this->verificationCouponTermVerify([
'shop_coupon' => $val,
'verification_available_type' => 2, //收银机
'verification_store_id' => $param['store_id'],
'verify_goods_type' => 2 //当收银机登录会员获取商家券时不需要验证金额
]);
if ($result['status'] == 1) {
$val['coupon_choice_type'] = 3; //门店商家券
$val['coupon_name'] = $val['stock_name'];
$coupon_use_rule = json_decode($val['coupon_use_rule'], true);
$val['reduce_money'] = round($coupon_use_rule['fixed_normal_coupon']['discount_amount'], 2); //优惠金额
$val['need_money'] = round($coupon_use_rule['fixed_normal_coupon']['transaction_minimum'], 2); //消费门槛
$val['begin_time'] = date('Y-m-d H:i:s', $val['available_begin_time']); //开始时间
$val['end_time'] = date('Y-m-d H:i:s', $val['available_end_time']); //结束时间
if($val['goods_type'] == 2){
//指定商品
$list = $SaleGoodsClass->appointGoodsList($val['goods_id_gather']);
$str = '';
foreach ($list as $v) {
$str .= ';' . $v['goods_name'];
if (!empty($v['sku_name'])) {
$str .= '-' . $v['sku_name'];
}
}
$val['appoint_type_name'] = '部分商品:' . trim($str, ';');
}
$shop_coupon[] = $val;
}
}
return callBack(statusCode()['success'], "查询成功", ['list' => $shop_coupon]);
}
/**
* 收银机下单商家券使用
* @param $param
* [
* coupon_id 用户商家券id
* allMoney 订单金额
* store_id 门店id
* sellDataArray 购买的商品信息
* ]
* @return mixed
* [
* status
* message
* shop_coupon
* goods 使用商家券的商品信息
* goods_money 参与优惠的商品总金额
* ]
*/
public function cashPlaceOrder($param)
{
if (empty($param['coupon_id']) || empty($param['allMoney']) || empty($param['store_id']) || empty($param['sellDataArray'])) {
return callBack(statusCode()['error'], "未找到该商家券");
}
$fields = 'r.coupon_code,r.used_type,r.verification_type,r.add_time,s.app_id,s.type,s.user_type,s.available_type,s.stock_type,
s.available_begin_time,s.available_end_time,s.valid_time_type,s.coupon_use_rule,s.store_type,s.store_id_gather,
s.goods_type,s.goods_id_gather';
$ShopCouponRecordModel = new ShopCouponRecordModel();
$shop_coupon = $ShopCouponRecordModel->verificationCouponInfo($fields, [
'r.id' => $param['coupon_id']
]);
if (empty($shop_coupon)) {
return callBack(statusCode()['error'], "未找到该商家券");
}
$result = $this->verificationCouponTermVerify([
'shop_coupon' => $shop_coupon,
'verification_available_type' => 2,//核销场景收银机
'verification_store_id' => $param['store_id'],//核销门店
'order_money' => $param['allMoney'], //订单金额
'sellDataArray' => $param['sellDataArray'] //购买的商品信息
]);
return $result;
}
/**
* 将优惠后的商品信息替换原来购买的商品信息(已弃用)
* @param $param
* [
* sellDataArray 实际购买的商品信息
* sellDataArray_new 参与优惠的商品信息处理优惠后的商品信息
* ]
* @return mixed
*/
public function sellDataArrayReplace($param)
{
$sellDataArray = $param['sellDataArray'];
$sellDataArray_new = $param['sellDataArray_new'];
foreach ($sellDataArray as $key => $val) {
foreach ($sellDataArray_new as $c_key => $c_val) {
if ($key == $c_key) {
$sellDataArray[$key] = $c_val;
}
}
}
return $sellDataArray;
}
/**
* 获取参与优惠的商品信息
* @param $param
* [
* sellDataArray 实际购买的商品信息
* shop_coupon_goods 参与优惠的商品信息
* ]
* @return mixed
*/
public function sellDataArray_new($param)
{
$sellDataArray = $param['sellDataArray'];
$shop_coupon_goods = $param['shop_coupon_goods'];
$sellDataArray_new = [];
foreach ($sellDataArray as $key => $val) {
foreach ($shop_coupon_goods as $c_key => $c_val) {
if ($val['goods_id'] == $c_val) {
$sellDataArray_new[$key] = $val;
}
}
}
$sellDataArray_new = ascSortByFields($sellDataArray_new, 'price');
return $sellDataArray_new;
}
/**
* 小程序下单商家券使用
* @param $param
* [
* coupon_id 用户商家券id
* allMoney 订单金额
* store_id 门店id
* sellDataArray 购买的商品信息
* ]
* @return mixed
* [
* status
* message
* shop_coupon
* goods 使用商家券的商品信息
* goods_money 参与优惠的商品总金额
* ]
*/
public function xcxPlaceOrder($param)
{
if (empty($param['coupon_id']) || empty($param['allMoney']) || empty($param['store_id']) || empty($param['sellDataArray'])) {
return callBack(statusCode()['error'], "未找到该商家券");
}
$fields = 'r.coupon_code,r.used_type,r.verification_type,r.add_time,s.app_id,s.type,s.user_type,s.available_type,s.stock_type,
s.available_begin_time,s.available_end_time,s.valid_time_type,s.coupon_use_rule,s.store_type,s.store_id_gather,
s.goods_type,s.goods_id_gather';
$ShopCouponRecordModel = new ShopCouponRecordModel();
$shop_coupon = $ShopCouponRecordModel->verificationCouponInfo($fields, [
'r.id' => $param['coupon_id']
]);
if (empty($shop_coupon)) {
return callBack(statusCode()['error'], "未找到改商家券");
}
$result = $this->verificationCouponTermVerify([
'shop_coupon' => $shop_coupon,
'verification_available_type' => 1,//核销场景小程序
'verification_store_id' => $param['store_id'],//核销门店
'order_money' => $param['allMoney'], //订单金额
'sellDataArray' => $param['sellDataArray'] //购买的商品信息
]);
return $result;
}
/**
* 小程序普通下单选择可用商家券
* @param $param
* [
* scene_type 1普通下单场景 2预售下单场景
* merchant_id
* store_id 门店id
* shopping_ids 购物车id集合 普通订单使用
* goods_id 预售订单使用
* sku_id 规格id
* xcx_id 小程序用户id
* vip_info 会员信息
* is_integral_cash 是否启用积分抵现1启用 0不启用
* h_coupon_id_redis redis中正在使用的商家券信息
* order_money 订单金额
* ]
* @return mixed
*/
public function xcxGetUserPayShopCoupon($param)
{
switch ($param['scene_type']) {
case 1:
//普通下单
$result = $this->getShoppingGoods($param);
$sellDataArray = $result['data']['sellDataArray']; //购买的商品信息
break;
case 2:
$sku_json_info = '';
if (!empty($param['sku_id'])) {
$sku_json_info = json_encode(['id' => $param['sku_id']]);
}
$sellDataArray[] = array(
'goods_id' => $param['goods_id'],
'sku_json_info' => $sku_json_info,
'all_money' => $param['order_money'],
);
break;
}
try {
$fields = 'r.id,r.coupon_code,r.used_type,r.verification_type,r.add_time,s.app_id,s.type,s.user_type,s.available_type,s.stock_type,
s.available_begin_time,s.available_end_time,s.valid_time_type,s.coupon_use_rule,s.store_type,s.store_id_gather,
s.goods_type,s.goods_id_gather,s.stock_name';
$where = [
'r.merchant_id' => $param['merchant_id'],
'r.used_type' => 1,
'r.verification_type' => 1,
'r.xcx_id' => $param['xcx_id']
];
if (!empty($param['h_coupon_id_redis'])) {
$where['r.id'] = ['not in', $param['h_coupon_id_redis']];
}
$where['_string'] = "(FIND_IN_SET(1,s.available_type))";
$shopCouponRecordModel = new ShopCouponRecordModel();
$user_shop_coupon = $shopCouponRecordModel->PlaceOrderCouponList($fields, $where);
} catch (\Exception $e) {
return callBack(statusCode()['success'], "查询成功", ['shop_coupon' => []]);
}
$shop_coupon = []; //可用商家券
if (!empty($user_shop_coupon)) {
foreach ($user_shop_coupon as $key => &$val) {
$result = $this->verificationCouponTermVerify([
'shop_coupon' => $val,
'verification_available_type' => 1, //小程序
'verification_store_id' => $param['store_id'],
'order_money' => $param['order_money'],
'sellDataArray' => $sellDataArray
]);
if ($result['status'] == 1) {
$val['coupon_choice_type'] = 3; //门店商家券
$val['coupon_name'] = $val['stock_name'];
$coupon_use_rule = json_decode($val['coupon_use_rule'], true);
$val['reduce_money'] = round($coupon_use_rule['fixed_normal_coupon']['discount_amount'], 2); //优惠金额
$val['need_money'] = round($coupon_use_rule['fixed_normal_coupon']['transaction_minimum'], 2); //消费门槛
$val['begin_time'] = date('Y.m.d', $val['available_begin_time']); //开始时间
$val['end_time'] = date('Y.m.d', $val['available_end_time']); //结束时间
$shop_coupon[] = $val;
}
}
}
return callBack(statusCode()['success'], "查询成功", ['shop_coupon' => $shop_coupon]);
}
/**
* 获取小程序普通下单获取商家券所需的商品信息
* @param $param
* [
* merchant_id
* store_id 门店id
* shopping_ids 购物车id集合
* xcx_id 小程序用户id
* vip_info 会员信息
* is_integral_cash 是否启用积分抵现1启用 0不启用
* ]
* @return mixed
*/
public function getShoppingGoods($param)
{
try {
$fields = array(
's.num',
's.sku_id',
's.goods_id',
'g.online_price',
'g.shop_mem_status',
'g.shop_mem_price',
'g.vip_online_state',
'g.pricing_method',
'g.plus_mem_status',
'g.plus_price',
'g.attribute_state',
'g.goods_class_id',
'h.status as promotion_status',
'h.act_price',
);
$ShoppingModel = new RetailShoppingModel();
$goods = $ShoppingModel->getShoppingGoods($param['xcx_id'], $param['store_id'], $fields, $param['shopping_ids'], '1,2'); //获取要购买的商品详情
$vipInfo = $param['vip_info']; //会员信息
$MerchantConfigModel = new RetailMerchantConfigModel(); //商户设置
$Config = $MerchantConfigModel->getConfig($param['merchant_id']); //积分设置
$XcxConfigModel = new RetailStoreXcxConfigModel();
$promotion_status = $XcxConfigModel->getFindField(array('storeid' => $param['store_id']), 'promotion_status'); //门店促销设置
$is_integral_cash = $param['is_integral_cash']; //积分抵现
$SkuGoodsModel = new RetailSkuGoodsModel();
$sku_map = [
'state' => 0
]; //规格查询公共条件
$sellDataArray = []; //商品信息
foreach ($goods as $key => &$value) {
if (!empty($value['sku_id'])) {
$sku_map['id'] = $value['sku_id'];
$sku = $SkuGoodsModel->getFind($sku_map);
$value['shop_mem_price'] = $sku['shop_mem_price'] > 0 ? $sku['shop_mem_price'] : 0;
$value['plus_price'] = $sku['plus_price'] > 0 ? $sku['plus_price'] : 0;
$value['online_price'] = $sku['online_price'] > 0 ? $sku['online_price'] : $value['online_price'];
$value['shop_mem_integral'] = $sku['shop_mem_integral'] > 0 ? $sku['shop_mem_integral'] : 0;
$value['plus_mem_integral'] = $sku['plus_mem_integral'] > 0 ? $sku['plus_mem_integral'] : 0;
} else {
$value['shop_mem_integral'] = $value['shop_mem_integral'] > 0 ? $value['shop_mem_integral'] : 0;
$value['plus_mem_integral'] = $value['plus_mem_integral'] > 0 ? $value['plus_mem_integral'] : 0;
}
if ($promotion_status != 1) {
$value['promotion_status'] = 0;
} else if ($value['promotion_status'] == 1) {
$value['online_price'] = $value['act_price'];
}
if ($value['promotion_status'] == 1) {
$value['shop_mem_status'] = 2;
$value['plus_mem_status'] = 2;
$value['vip_online_state'] = 0; // 参加了促销商品 不打折 不优惠
}
$price = $value['online_price']; //购买时的角色价格
$discount = $vipInfo['discount'] && $vipInfo['user_type'] != 1 && $value['vip_online_state'] == 1 && !$value['promotion_status'] ? $vipInfo['discount'] : 1; //会员打折折扣值
if ($vipInfo && $vipInfo['user_type'] != 1) {
//会员
if ($vipInfo['user_type'] == 2 && $value['plus_mem_status'] == 1) {
//plus会员
if ($Config['integral_arrive_cash'] == 1 && $is_integral_cash != 1 && $value['plus_mem_integral'] > 0 && $Config['marketing_methods'] == 2) {
$price = $value['online_price']; //商城原价
} else {
$price = $value['plus_price'];
}
$discount = 1;
} else if ($value['shop_mem_status'] == 1) {
//常规会员
if ($Config['integral_arrive_cash'] == 1 && $is_integral_cash != 1 && $value['shop_mem_integral'] > 0 && $Config['marketing_methods'] == 2) {
$price = $value['online_price']; //商城原价
} else {
$price = $value['shop_mem_price'];
}
}
}
$money = round($price * $discount * $value['num'], 2);
$money = $money > 0.01 ? $money : 0.01;
$sku_json_info = '';
if (!empty($value['sku_id'])) {
$sku_json_info = json_encode(['id' => $value['sku_id']]);
}
$sellDataArray[] = [
'goods_class_id' => $value['goods_class_id'],
'goods_id' => $value['goods_id'],
'sku_json_info' => $sku_json_info,
'all_money' => $money
];
}
return callBack(statusCode()['success'], "查询成功", ['sellDataArray' => $sellDataArray]);
} catch (\Exception $e) {
return callBack(statusCode()['success'], "查询成功", ['sellDataArray' => []]);
}
}
/**
* 核销成功用户商家券更新
* @param $param
* [
* coupon_code 用户商家券券码
* verification_type 核销状态1未核销 2已核销
* verification_available_type 核销场景 1小程序 2收银机 3公众号
* verification_store_id 核销的门店
* operator_id 核销人id 0为小程序用户购买核销
* use_request_no 核销请求单据号
* use_time 请求核销时间
* wechatpay_use_time 核销时间
*
* ]
* @return mixed
*/
public function couponRecordUpdate($param)
{
$where = [
'coupon_code' => $param['coupon_code']
];
$data = [
'used_type' => 2,
'verification_type' => $param['verification_type'],
'verification_available_type' => $param['verification_available_type'],
'verification_store_id' => $param['verification_store_id'],
'operator_id' => $param['operator_id'] ? $param['operator_id'] : 0,
'use_request_no' => $param['use_request_no'],
'wechatpay_use_time' => time(),
'use_time' => $param['use_time'],
'bill_id' => $param['bill_id'] ? $param['bill_id'] : 0
];
$ShopCouponRecordModel = new ShopCouponRecordModel();
$result = $ShopCouponRecordModel->updateOrder($where, $data);
if ($result) {
return callBack(statusCode()['success'], "商家券核销成功");
} else {
return callBack(statusCode()['success'], "商家券核销成功,记录更新失败");
}
}
/**
* 核销商家券
* @param $param
* [
* openid 用户小程序openid
* service_openid 服务商openid
* ordinary_openid 商户公众号的openid
* coupon_code 用户商家券券码 收银机核销
* coupon_id 用户商家券id 小程序核销
* merchant_id
* verification_available_type 核销场景 1小程序 2收银机 3公众号
* verification_store_id 核销的门店
* operator_id 核销人id 0为小程序用户购买核销
* bill_id 订单id
* ]
* @return mixed
*/
public function verificationCoupon($param)
{
$result = $this->getCouponInfo($param);
if ($result['status'] != 1) {
return $result;
}
$shop_coupon = $result['data']['shop_coupon']; //商家券信息
$ShopCouponClass = new \ShopCoupon\Business\ShopCoupon();
$result = $ShopCouponClass->verificationCoupon([
'merchant_id' => $param['merchant_id'],
'user_type' => $shop_coupon['user_type'],
'type' => $shop_coupon['type'],
'coupon_code' => $shop_coupon['coupon_code'],
'app_id' => $shop_coupon['app_id'],
'verification_available_type' => $param['verification_available_type'],
'verification_store_id' => $param['verification_store_id'],
'operator_id' => $param['operator_id'] ? $param['operator_id'] : 0,
'openid' => $param['openid'],
'service_openid' => $param['service_openid'],
'ordinary_openid' => $param['ordinary_openid'],
]);
if ($result['status'] == 1) {
$param['verification_type'] = 2;
} else {
$param['verification_type'] = 1;
}
$param['use_request_no'] = $result['data']['use_request_no'];
$param['use_time'] = $result['data']['use_time'];
$param['coupon_code'] = $shop_coupon['coupon_code'];
$result = $this->couponRecordUpdate($param); //用户券更新
return $result;
}
/**
*询商家券信息
* @param $param
* [
* 用户商家券券码 coupon_code 线下
* 用户商家券 coupon_id 线上
* merchant_id
* verification_available_type 核销场景 1小程序 2收银机 3公众号
* verification_store_id 核销的门店
* ]
* @return mixed
*/
public function getCouponInfo($param)
{
$fields = 'r.id,r.coupon_code,r.used_type,r.verification_type,r.add_time,s.app_id,s.type,s.user_type,s.available_type,s.stock_type,
s.available_begin_time,s.available_end_time,s.valid_time_type,s.coupon_use_rule,s.store_type,s.store_id_gather,
s.goods_type,s.goods_id_gather';
$where = [
'r.merchant_id' => $param['merchant_id'],
'r.used_type' => 1,
'r.verification_type' => 1
];
switch ($param['verification_available_type']) {
case 1:
//小程序
$where['r.id'] = $param['coupon_id'];
break;
case 2:
//线下
if (!empty($param['coupon_id'])) {
$where['r.id'] = $param['coupon_id'];
} else {
$where['r.coupon_code'] = $param['coupon_code'];
}
break;
}
$ShopCouponRecordModel = new ShopCouponRecordModel();
$shop_coupon = $ShopCouponRecordModel->verificationCouponInfo($fields, $where);
if (empty($shop_coupon)) {
return callBack(statusCode()['error'], "未找到可用商家券");
} else {
return callBack(statusCode()['success'], "查询成功", ['shop_coupon' => $shop_coupon]);
}
}
/**
* 商家券条件验证
* @param $param
* [
* shop_coupon 商家券
* verification_available_type 核销场景 1小程序 2收银机 3公众号
* verification_store_id 核销的门店
* order_money 订单的金额
* sellDataArray 商品信息集合['goods_id'=>1,'sku_json_info'=>'','all_money'=>1]
* verify_goods_type 2 当收银机登录会员或者点击立即使用时获取商家券时不需要验证金额
* ]
* @return mixed
*/
public function verificationCouponTermVerify($param)
{
$time = time();
$shop_coupon = $param['shop_coupon'];
$available_type = explode(',', $shop_coupon['available_type']);
$verification_available_type = $param['verification_available_type'];
if ($param['verification_available_type'] == 3) {
$verification_available_type = 2; //线下
}
if (!in_array($verification_available_type, $available_type)) {
return callBack(statusCode()['error'], "该券不支持该场景使用");
}
if ($shop_coupon['available_begin_time'] > $time || $shop_coupon['available_end_time'] < $time) {
return callBack(statusCode()['error'], "未在有效时间内");
}
if ($shop_coupon['store_type'] == 2) {
$store_id_gather = explode(',', $shop_coupon['store_id_gather']);
if (!in_array($param['verification_store_id'], $store_id_gather)) {
return callBack(statusCode()['error'], "该门店不支持此商家券");
}
}
$coupon_use_rule = json_decode($shop_coupon['coupon_use_rule'], true);
$transaction_minimum = $coupon_use_rule['fixed_normal_coupon']['transaction_minimum']; //消费门槛
if (empty($param['verify_goods_type']) || $param['verify_goods_type'] != 2) {
switch ($shop_coupon['goods_type']) {
case 1:
//所有商品
switch ($shop_coupon['stock_type']) {
case 1:
//满减券
if ($param['order_money'] < $transaction_minimum) {
return callBack(statusCode()['error'], "未达到消费门槛");
}
break;
}
break;
case 2:
//指定商品
$sellDataArray = $param['sellDataArray'];
$goods_money = 0; //商品总金额
$goods = []; //参与优惠的商品
foreach ($sellDataArray as $value) {
if (in_array($value['goods_id'], explode(',',$shop_coupon['goods_id_gather']))) {
$goods_money += $value['all_money'];
$goods[] = $value;
}
}
if ($goods_money == 0) {
return callBack(statusCode()['error'], "未购买指定商品");
}
if ($goods_money < $transaction_minimum) {
return callBack(statusCode()['error'], "未达到消费门槛");
}
break;
}
}
if (!empty($shop_coupon['valid_time_type'])) {
switch ($shop_coupon['valid_time_type']) {
case 1:
//无规律时间段
$irregular_time = $coupon_use_rule['coupon_available_time']['irregulary_avaliable_time']['irregular_time']; //无规律时间段
if (!empty($irregular_time)) {
$begin_time = strtotime($irregular_time[0]);
$end_time = strtotime($irregular_time[1]);
if ($begin_time > $time || $end_time < $time) {
return callBack(statusCode()['error'], "未在有效时间内");
}
}
break;
case 2:
//固定周期
$available_day_time = $coupon_use_rule['coupon_available_time']['available_week']['available_day_time']; //时间段
$week_day = $coupon_use_rule['coupon_available_time']['available_week']['week_day']; //星期
if (!empty($week_day)) {
$week = date("w", time());
if (!in_array($week, $week_day)) {
return callBack(statusCode()['error'], "未在有效时间内");
}
}
$begin_time = $available_day_time['begin_time'];
$end_time = $available_day_time['end_time'];
if (!empty($begin_time) && !empty($end_time)) {
$begin_time = strtotime($begin_time);
$end_time = strtotime($end_time);
if ($begin_time > $time || $end_time < $time) {
return callBack(statusCode()['error'], "未在有效时间内");
}
}
$available_day_after_receive = $coupon_use_rule['coupon_available_time']['available_day_after_receive']; //生效内n天有效
if (!empty($available_day_after_receive) && $available_day_after_receive > 0) {
$receive_time = ($time - $shop_coupon['add_time']) / 86400;
if ($receive_time > $available_day_after_receive) {
return callBack(statusCode()['error'], "未在有效时间内,领取后" . $available_day_after_receive . "天内有效");
}
}
break;
case 3:
//领取后N天开始生效
$wait_days_after_receive = $coupon_use_rule['coupon_available_time']['wait_days_after_receive']; //领取n天后开始生效
$available_day_after_receive = $coupon_use_rule['coupon_available_time']['available_day_after_receive']; //生效内n天有效
if (!empty($wait_days_after_receive) && $wait_days_after_receive > 0) {
$wait_time = ($time - $shop_coupon['add_time']) / 86400;
if ($wait_time < $wait_days_after_receive) {
return callBack(statusCode()['error'], "未到有效时间,领取后" . $wait_days_after_receive . "天开始生效");
}
if (!empty($available_day_after_receive) && $available_day_after_receive > 0) {
$receive_time = ($time - ($shop_coupon['add_time'] + 86400 * $wait_days_after_receive)) / 86400;
if ($receive_time > $available_day_after_receive) {
return callBack(statusCode()['error'], "未在有效时间内,生效后" . $available_day_after_receive . "天内有效");
}
}
}
break;
}
}
return callBack(statusCode()['success'], "验证成功", ['shop_coupon' => $shop_coupon, 'goods' => $goods ? $goods : [], 'goods_money' => $goods_money ? $goods_money : 0, 'discount_amount' => $coupon_use_rule['fixed_normal_coupon']['discount_amount'], 'transaction_minimum' => $coupon_use_rule['fixed_normal_coupon']['transaction_minimum'], 'id' => $shop_coupon['id'], 'reduce_money' => $coupon_use_rule['fixed_normal_coupon']['discount_amount'], 'need_money' => $coupon_use_rule['fixed_normal_coupon']['transaction_minimum']]);//收银机专用参数
}
/**
* 用户商家券列表
* @param $param
* [
* merchant_id
* xcx_id
* type 1可用 2已使用 3已过期
* page 页码
* ]
* @return mixed
*/
public function userShopCouponList($param)
{
$where = [
's.merchant_id' => $param['merchant_id'],
's.xcx_id' => $param['xcx_id'],
];
switch ($param['type']) {
case 1:
//可使用
$where['s.used_type'] = 1;
$where['c.available_end_time'] = ['gt', time()];
break;
case 2:
//已使用
$where['s.used_type'] = 2;
break;
case 3:
//已过期
$where['s.used_type'] = 1;
$where['c.available_end_time'] = ['lt', time()];
break;
}
$fields = 's.id,s.coupon_id,c.stock_name,c.coupon_use_rule,c.goods_type,c.store_type,c.available_begin_time,c.available_end_time,c.goods_id_gather,c.available_type,c.store_id_gather';
$page = $param['page'];
try {
$ShopCouponModel = new ShopCouponRecordModel();
$list = $ShopCouponModel->userCouponList($fields, $where, $page, 10);
} catch (\Exception $e) {
return callBack(statusCode()['success'], "查询成功", ['list' => []]);
}
if ($list) {
foreach ($list as &$val) {
switch ($val['goods_type']) {
case 1:
$val['goods_type_name'] = '所有商品';
break;
case 2:
$val['goods_type_name'] = '部分商品';
break;
}
$available_type = explode(',', $val['available_type']);
if (count($available_type) > 1) {
$val['available_type_name'] = '线上,门店通用';
} else {
if (in_array(1, $available_type)) {
$val['available_type_name'] .= '线上';
} else if (in_array(2, $available_type)) {
$val['available_type_name'] .= '门店';
}
}
$coupon_use_rule = json_decode($val['coupon_use_rule'], true);
$val['discount_amount'] = $coupon_use_rule['fixed_normal_coupon']['discount_amount']; //优惠金额
$val['transaction_minimum'] = $coupon_use_rule['fixed_normal_coupon']['transaction_minimum']; //消费门槛
$available_begin_time = date('Y.m.d', $val['available_begin_time']); //开始时间
$available_end_time = date('Y.m.d', $val['available_end_time']); //结束时间
$val['time'] = $available_begin_time . '-' . $available_end_time;
}
}
return callBack(statusCode()['success'], "查询成功", ['list' => $list]);
}
/**
* 领取商家券成功后添加领取记录
* @param $param
* [
* merchant_id
* store_id
* coupon_id 商家券主键id
* xcx_id 小程序用户id
* stock_id 商家券发券id
* coupon_code 商家券券码
* openid 用户openid
* ]
* @return mixed
*/
public function receiveCouponCallback($param)
{
$ShopCouponRecordModel = new ShopCouponRecordModel();
$shop_coupon = $ShopCouponRecordModel->getFind([
'coupon_code' => $param['coupon_code'],
'merchant_id' => $param['merchant_id'],
'xcx_id' => $param['xcx_id']
]);
if (!empty($shop_coupon)) {
return callBack(statusCode()['error'], "已领取");
}
try {
$content = [
'merchant_id' => $param['merchant_id'],
'coupon_id' => $param['coupon_id'],
'xcx_id' => $param['xcx_id'],
'openid' => $param['openid'],
'stock_id' => $param['stock_id'],
'coupon_code' => $param['coupon_code'],
'add_time' => time(),
'store_id' => $param['store_id']
];
$result = $ShopCouponRecordModel->addOrder($content);
if (!empty($result)) {
$where = [
'id' => $param['coupon_id']
];
$ShopCouponModel = new ShopCouponModel();
$ShopCouponModel->toSetDec($where, "operation_num", 1);
return callBack(statusCode()['success'], "领取成功,已存放至卡包");
} else {
return callBack(statusCode()['error'], "已存至卡包,领取记录添加失败");
}
} catch (\Exception $e) {
return callBack(statusCode()['error'], "已存至卡包,领取记录添加失败");
}
}
/**
* 发券签名与商户号
* @param $param
* [
* merchant_id
* shop_coupon 商家券信息
* paySet 商户配置
* ]
* @return mixed
*/
public function grantCouponSign($param)
{
$paySet = $param['paySet'];
$shop_coupon = $param['shop_coupon'];
$out_request_no = onlyCreateKey(); //随机字符串
switch ($shop_coupon['user_type']) {
case 1:
//普通模式
switch ($shop_coupon['type']) {
case 1:
//小程序
$key = $paySet['config']['xcx_secret_key'];
break;
case 2:
//公众号
$key = $paySet['config']['wx_secret_key'];
break;
}
break;
case 2:
//服务商模式
$key = $paySet['config']['systemConfig']['wx_key'];
break;
default:
return callBack(statusCode()['error'], "暂无法领取该商家券,请联系商家");
break;
}
$SignClass = new Sign();
$sign = $SignClass->wxHmacSign([
'stock_id0' => $shop_coupon['stock_id'],
'send_coupon_merchant' => $shop_coupon['belong_merchant'],
'out_request_no0' => $out_request_no
], $key);
return callBack(statusCode()['success'], "生成成功", ['stock_id' => $shop_coupon['stock_id'], 'send_coupon_merchant' => $shop_coupon['belong_merchant'], 'out_request_no' => $out_request_no, 'sign' => $sign]);
}
/**
* 领取商家券前判断(2020-8-20已作废)
* @param $param
* [
* merchant_id
* id 商家券主键id
* xcx_id 小程序用户id
* ]
* @return mixed
*/
public function grantCouponSignVerify($param)
{
if (empty($param['merchant_id']) || empty($param['id']) || empty($param['xcx_id'])) {
return callBack(statusCode()['error'], "登录超时,请删除小程序重新登录");
}
$where = [
'merchant_id' => $param['merchant_id'],
'id' => $param['id'],
'status' => 1,
'suspend_status' => 1
];
$fields = 'available_end_time,available_type,belong_merchant,stock_id,user_type,type,operation_num';
$ShopCouponModel = new ShopCouponModel();
$shop_coupon = $ShopCouponModel->getFind($where, $fields);
if (empty($shop_coupon)) {
return callBack(statusCode()['error'], "该商家券已下架,已刷新重试");
}
if ($shop_coupon['available_end_time'] < time()) {
return callBack(statusCode()['error'], "该商家券已过期,请刷新重试");
}
if ($shop_coupon['operation_num'] <= 0) {
return callBack(statusCode()['error'], "该商家券已抢完");
}
return callBack(statusCode()['success'], "验证成功", ['shop_coupon' => $shop_coupon]);
}
/**
* 商家券列表
* @param $param
* [
* merchant_id
* xcx_id 小程序用户id
* store_id 展示的门店id
* goods_id 商品详情的商品id
* couponType 2商家券
* page
* limit
* ]
* @return mixed
*/
public function couponList($param)
{
try {
$where = [
'merchant_id' => $param['merchant_id'],
'xcx_id' => $param['xcx_id'],
];
$ShopCouponRecord = new ShopCouponRecordModel();
$user_coupon_id = $ShopCouponRecord->getFindField($where, 'coupon_id', true);
$where = [
'merchant_id' => $param['merchant_id'],
'status' => 1,
'suspend_status' => 1,
'available_end_time' => ['egt', time()],
'operation_num' => ['gt', 0],
];
if (!empty($user_coupon_id)) {
$where['id'] = ['not in', $user_coupon_id];
}
$sql_str = '';
if (!empty($param['store_id'])) {
$sql_str .= "((FIND_IN_SET(" . $param['store_id'] . ",store_id_gather) and store_type=2) OR store_type=1)";
}
if (!empty($param['goods_id'])) {
if (!empty($sql_str)) {
$sql_str .= ' AND ';
}
$sql_str .= "((FIND_IN_SET(" . $param['goods_id'] . ",goods_id_gather) and goods_type=2) OR goods_type=1)";
$limit = 3;
} else {
$limit = $param['limit'] ? $param['limit'] : 10;
}
if (!empty($sql_str)) {
$where['_string'] = $sql_str;
}
$page = $param['page'];
$ShopCouponModel = new ShopCouponModel();
$list = $ShopCouponModel->getSelect($where, 'id,stock_name,coupon_use_rule,goods_type,store_type,available_begin_time,available_end_time,goods_id_gather,stock_id,user_type,type,belong_merchant,available_type,store_id_gather', 'id desc', $page . ',' . $limit);
} catch (\Exception $e) {
return callBack(statusCode()['success'], "查询成功", ['list' => []]);
}
if ($list) {
$paySetModel = new RetailPaySetModel();
$paySet = $paySetModel->getFindMerchantPaySet($param['merchant_id']);
foreach ($list as &$val) {
switch ($val['goods_type']) {
case 1:
$val['goods_type_name'] = '所有商品';
break;
case 2:
$val['goods_type_name'] = '部分商品';
break;
}
$val['available_type_name'] = '';
$available_type = explode(',', $val['available_type']);
if (count($available_type) > 1) {
$val['available_type_name'] = '商家券';
} else {
if (in_array(1, $available_type)) {
$val['available_type_name'] .= '商家券线上';
} else if (in_array(2, $available_type)) {
$val['available_type_name'] .= '商家券线下';
}
}
$coupon_use_rule = json_decode($val['coupon_use_rule'], true);
$val['discount_amount'] = $coupon_use_rule['fixed_normal_coupon']['discount_amount']; //优惠金额
$val['transaction_minimum'] = $coupon_use_rule['fixed_normal_coupon']['transaction_minimum']; //消费门槛
$val['available_begin_time'] = date('Y.m.d', $val['available_begin_time']); //开始时间
$val['available_end_time'] = date('Y.m.d', $val['available_end_time']); //结束时间
//diy辉哥规定的字段设置转换
if ($param['couponType'] == 2) {
$val['begin_time'] = $val['available_begin_time'];
$val['end_time'] = $val['available_end_time'];
$val['coupon_name'] = $val['stock_name'];
$val['need_money'] = $val['transaction_minimum'];
$val['reduce_money'] = $val['discount_amount'];
$val['use'] = false;
}
$sign = $this->grantCouponSign([
'merchant_id' => $param['merchant_id'],
'xcx_id' => $param['xcx_id'],
'shop_coupon' => $val,
'paySet' => $paySet
]);
$send_coupon_param = [
[
'stock_id' => $val['stock_id'],
'out_request_no' => $sign['data']['out_request_no']
]
];
$val['send_coupon_param'] = $send_coupon_param;
$val['send_coupon_merchant'] = $val['belong_merchant'];
$val['sign'] = $sign['data']['sign'];
}
}
return callBack(statusCode()['success'], "查询成功", ['list' => $list]);
}
}