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

511 行
18 KiB
PHP
可执行文件

<?php
namespace Common\Business;
use Common\Model\RetailBillDiscountModel;
use Common\Model\RetailPayOrderModel;
use PayCoupon\Model\PayCouponModel;
use PayCoupon\Model\PayCouponRecordModel;
//[1.8.68]
/**
* 代金券公共类
* Class PayCoupon
* [
* 统计代金券金额 payCouponMoney
* 统计代金券金额的查询条件 payCouponMoneyTerm
* 核销代金券或退款的微信回调 couponVerification_v1
* 代金券详情 couponInfo
* 用户的已领代金券列表 userPayCouponList
* 小程序代金券列表 couponList
* 代金券展示参数转换 transformation
* ]
* @package Common\Business
*/
class PayCoupon
{
/**
* 使用代金券是否可以退款
* @param $param
* [
* bill_id 订单id
* refund_all_money 要退款的总金额
* all_money 原订单总金额
* ]
* @return mixed
*/
public function payCouponRefundVerify($param)
{
$BillDisModel = new RetailBillDiscountModel();
$dis_count = $BillDisModel->getFind([
'bill_id'=>$param['bill_id']
],'pay_coupon_id_str,pay_coupon_fee');
if(!empty($dis_count)){
$PayCouponRecordModel = new PayCouponRecordModel();
$info = $PayCouponRecordModel->couponInfo([
'r.user_coupon_id' => $dis_count['pay_coupon_id_str'],
],'p.coupon_type');
if($info['coupon_type'] == 2 && $param['refund_all_money'] < floatval($param['all_money'] + $dis_count['pay_coupon_fee'])){
return callBack(statusCode()['error'],'单品代金券只支持全额退款');
}
}
return callBack(statusCode()['success'],'验证成功,可以退款');
}
/**
* 统计代金券金额
* @param $param
* [
* merchant_id
* store_id
* pay_type 1微信 4小程序微信 默认全部
* order_type 单个值或者数组 1购买商品 2会员充值 4购买礼品卡 5购买PLUS会员 6小助手快捷收银 7购买次卡 8积分商城订单 9赊账订单
* cashier_id 收银员id
* identity_type 1普通用户 2会员
* start_time 开始时间
* end_time 结束时间
* scene_type 1付款 2退款 3付款- 退款
* ]
* @return mixed
*/
public function payCouponMoney($param)
{
if (empty($param['merchant_id']) && empty($param['store_id'])) {
return 0;
}
$where = $this->payCouponMoneyTerm($param);
$PayOrderModel = new RetailPayOrderModel();
$list = $PayOrderModel->getSelect($where, 'pay_coupon_fee,refund_coupon_fee');
$money = 0; //代金券金额
$scene_type = $param['scene_type'];
if (!empty($list)) {
foreach ($list as $val) {
switch ($scene_type) {
case 1:
$money += $val['pay_coupon_fee'];
break;
case 2:
$money += $val['refund_coupon_fee'];
break;
case 3:
$money += $val['pay_coupon_fee'];
if ($val['refund_coupon_fee'] > 0) {
$money -= $val['refund_coupon_fee'];
}
break;
}
}
}
return $money;
}
/**
* 统计代金券金额的查询条件
* @param $param
* [
* merchant_id
* store_id
* pay_type 1微信 4小程序微信 默认全部
* order_type 1购买商品 2会员充值 4购买礼品卡 5购买PLUS会员 6小助手快捷收银 7购买次卡 8积分商城订单 9赊账订单
* cashier_id 收银员id
* identity_type 1普通用户 2会员
* start_time 开始时间
* end_time 结束时间
* ]
* @return mixed
*/
public function payCouponMoneyTerm($param)
{
if (!empty($param['merchant_id'])) {
$where['merchant_id'] = $param['merchant_id'];
}
if (!empty($param['store_id'])) {
$where['store_id'] = $param['store_id'];
}
if (!empty($param['cashier_id'])) {
$where['cashier_id'] = $param['cashier_id'];
}
if ($param['identity_type'] == 1) {
$where['vid'] = [['exp', 'IS NULL'], 0, 'OR'];
} else if ($param['identity_type'] == 2) {
$where['vid'] = ['gt', 0];
}
if (!empty($param['pay_type'])) {
$where['pay_type'] = $param['pay_type'];
} else {
$where['pay_type'] = ['in', [1, 4]];
}
if (!empty($param['start_time']) && !empty($param['end_time'])) {
$where['add_time'] = ['between', [$param['start_time'], $param['end_time']]];
} else if (!empty($param['start_time'])) {
$where['add_time'] = ['gt', $param['start_time']];
} else if (!empty($param['end_time'])) {
$where['add_time'] = ['lt', $param['end_time']];
}
if (!empty($param['order_type'])) {
if (is_array($param['order_type'])) {
$where['order_type'] = ['in', $param['order_type']];
} else {
$where['order_type'] = $param['order_type'];
}
}
$where['scene_type'] = 1;
$where['pay_coupon_fee'] = ['gt', 0];
return $where;
}
/**
* 核销代金券或退款的微信回调2021-5-21 新版
* @param $data
* [
* json
* {
* merchant_id
* old_pay_order_id 退款的原支付订单id
* pay_info 支付成功后的微信订单信息
* use_type 1购买商品 2会员充值 4购买礼品卡 5购买PLUS会员 6小助手快捷收银 7购买次卡 8积分商城订单
* order_id 订单id
* store_id 门店id
* use_scene 核销场景 1小程序 2收银机 3公众号
* coupon_way 1核销 2退款
* }
* ]
* @return mixed
* [
* pay_coupon_id_str 代金券id
* pay_coupon_fee 代金券优惠总金额 单位元
* cash_fee 微信的现金金额 指客户实际付款或退款的金额
* ]
*/
public function couponVerification_v1($data)
{
$data = json_decode($data, true);
if (!is_array($data)) {
return callBack(statusCode()['error'], '订单信息格式有误');
}
$coupon_way = $data['coupon_way'] ? $data['coupon_way'] : 1;
$pay_info = $data['pay_info']; //微信订单信息
if (!in_array($coupon_way, [1, 2])) {
return callBack(statusCode()['error'], '代金券类型有误');
}
if ($coupon_way == 1) {
//核销
$coupon_count = $pay_info['coupon_count'] ? $pay_info['coupon_count'] : 1; //代金券使用数量
$coupon_id_name = 'coupon_id_';
} else if ($coupon_way == 2) {
//退款
$coupon_count = $pay_info['coupon_refund_count'] ? $pay_info['coupon_refund_count'] : 1; //代金券退款数量
$coupon_id_name = 'coupon_refund_id_';
}
$pay_coupon_id_arr = []; //代金券id
for ($i = 0; $i < $coupon_count; $i++) {
if (!empty($pay_info[$coupon_id_name . $i])) {
$pay_coupon_id_arr[] = $pay_info[$coupon_id_name . $i];
} else {
break;
}
}
if (!empty($pay_coupon_id_arr)) {
//修改本系统代金券的状态
$PlugClass = new Plug();
$result = $PlugClass->PlugPermissions([
'merchant_id' => $data['merchant_id'],
'identification_arr' => [
'wxpaycoupon'
]
]);
//已购买插件
if (!empty($result['data']['plug']['wxpaycoupon'])) {
$PayCouponRecordModel = new PayCouponRecordModel();
switch ($coupon_way) {
case 1:
//核销
$PayCouponRecordModel->updateOrder([
'user_coupon_id' => ['in', $pay_coupon_id_arr]
], [
'use_type' => $data['use_type'],
'order_id' => $data['order_id'],
'use_store_id' => $data['store_id'],
'use_time' => time(),
'use_scene' => $data['use_scene'],
'use_status' => 2
]);
break;
case 2:
//退款
$old_pay_order_id = $data['old_pay_order_id'];
$PayOrderModel = new RetailPayOrderModel();
$old_pay_order_info = $PayOrderModel->getFind([
'id'=>$old_pay_order_id
],'refund_coupon_fee,pay_coupon_fee');
if(floatval(($pay_info['coupon_refund_fee']/100) + $old_pay_order_info['refund_coupon_fee']) >= $old_pay_order_info['pay_coupon_fee']){
$PayCouponRecordModel->updateOrder([
'user_coupon_id' => ['in', $pay_coupon_id_arr]
], [
'use_status' => 1
]);
}
break;
}
}
switch ($coupon_way) {
case 1:
//核销
$coupon_refund_fee = $pay_info['coupon_fee']; //使用的代金券总金额
$cash_fee = $pay_info['cash_fee'];
break;
case 2:
//退款
$coupon_refund_fee = $pay_info['coupon_refund_fee'];//使用的代金券总金额
$cash_fee = $pay_info['cash_refund_fee'];
break;
}
return callBack(statusCode()['success'], '代金券核记录修改成功', [
'pay_coupon_id_str' => implode(',', $pay_coupon_id_arr),
'pay_coupon_fee' => ($coupon_refund_fee / 100),
'cash_fee' => ($cash_fee / 100)
]);
} else {
return callBack(statusCode()['error'], '未找到使用的代金券');
}
}
/**
* 代金券详情
* @param $param
* [
* id 代金券主键id
* uid 用户id
* ]
* @return mixed
*/
public function couponInfo($param)
{
try {
$PayCouponModel = new PayCouponModel();
$info = $PayCouponModel->getFind([
'id' => $param['id']
]);
$PayCouponRecordModel = new PayCouponRecordModel();
$coupon_state = $PayCouponRecordModel->getFind([
'coupon_id' => $param['id'],
'uid' => $param['uid']
]);
} catch (\Exception $e) {
return callBack(statusCode()['error'], "未找到该代金券");
}
if (empty($info)) {
return callBack(statusCode()['error'], "未找到该代金券");
}
$info = $this->infoTransformation($info);
if (empty($coupon_state)) {
$type = 1; //未领取
} else {
$type = 2; //已领取
}
$title = 'http';
if ($_SERVER['HTTPS'] == 'on') {
$title .= 's';
}
$web_url = $title . '://' . $_SERVER['HTTP_HOST'] . '/index.php/Vip/CouponAuth/couponAuth';
return callBack(statusCode()['success'], "查询成功", ['info' => $info, 'web_url' => $web_url, 'type' => $type]);
}
/**
* 代金券详情转换
* @param $info
* @return mixed
*/
public function infoTransformation($info)
{
$coupon_use_rule = json_decode($info['coupon_use_rule'], true);
$info['coupon_amount'] = $coupon_use_rule['fixed_normal_coupon']['coupon_amount'];
$info['transaction_minimum'] = $coupon_use_rule['fixed_normal_coupon']['transaction_minimum'];
$info['time'] = date('Y.m.d', $info['available_begin_time']) . '-' . date('Y.m.d', $info['available_end_time']);
if (!empty($info['add_time'])) {
$info['add_time'] = date('Y.m.d H:i:s', $info['add_time']);
}
if (!empty($info['valid_time_type'])) {
switch ($info['valid_time_type']) {
case 1:
//固定时间段
$info['user_time'] = $info['fix_available_time']['begin_time'] . '-' . $info['fix_available_time']['end_time'];
break;
case 2:
//领取后第二天
$info['user_time'] = '领取后第二天可用';
break;
}
}
switch ($info['user_type']) {
case 1:
//普通支付模式
switch ($info['type']) {
case 1:
$info['type_name'] = '线上用';
break;
case 2:
$info['type_name'] = '线下用';
break;
}
break;
case 2:
//服务商支付模式
$info['type_name'] = '通用';
break;
}
switch ($info['coupon_type']) {
case 1:
$info['coupon_type_name'] = '全部商品';
break;
case 2:
$GoodsClass = new Goods();
$GoodsResult = $GoodsClass->GoodsNameList([
'goods_id_gather' => $info['goods_id_gather']
]);
$info['coupon_type_name'] = '可使用商品为' . implode(',', $GoodsResult['data']['list']);
break;
}
return $info;
}
/**
* 用户的已领代金券列表
* @param $param
* [
* merchant_id
* type 1可用 2已使用 3已过期
* uid
* page
* limit
* ]
* @return mixed
*/
public function userPayCouponList($param)
{
$where = [
'r.merchant_id' => $param['merchant_id'],
'r.uid' => $param['uid'],
];
switch ($param['type']) {
case 1:
$where['r.use_status'] = 1;
$where['p.available_end_time'] = ['gt', time()];
break;
case 2:
$where['r.use_status'] = 2;
break;
case 3:
$where['r.use_status'] = 1;
$where['p.available_end_time'] = ['lt', time()];
break;
}
$fields = 'r.id,r.coupon_id,p.stock_name,p.coupon_use_rule,p.user_type,p.type,p.available_begin_time,p.available_end_time,p.coupon_type,p.goods_id_gather,p.store_id_gather';
try {
$PayCouponRecordModel = new PayCouponRecordModel();
$list = $PayCouponRecordModel->userRecordList($fields, $where, $param['page'], $param['limit']);
} catch (\Exception $e) {
return callBack(statusCode()['success'], "查询成功", ['list' => []]);
}
if (!empty($list)) {
$list = $this->transformation($list);
}
return callBack(statusCode()['success'], "查询成功", ['list' => $list]);
}
/**
* 小程序代金券列表
* @param $param
* [
* merchant_id
* uid
* page
* limit
*
* ]
* @return mixed
*/
public function couponList($param)
{
try {
$where = [
'uid' => $param['uid'],
'merchant_id' => $param['merchant_id']
];
$PayCouponRecordModel = new PayCouponRecordModel();
$coupon_id_gather = $PayCouponRecordModel->getFindField($where, "coupon_id", true); //已经领取的代金券id集合
$where = [
'merchant_id' => $param['merchant_id'],
'status' => 1,
'activate_status' => 1,
'suspend_status' => 1,
'operation_num' => ['gt', 0],
'available_end_time' => ['gt', time()]
];
if (!empty($coupon_id_gather)) {
$where['id'] = ['not in', $coupon_id_gather];
}
$where['_string'] = "((FIND_IN_SET(" . $param['store_id'] . ",store_id_gather) and coupon_type=2) OR coupon_type=1)";
$fields = 'id,stock_name,coupon_use_rule,user_type,type,available_begin_time,available_end_time,coupon_type,goods_id_gather';
$PayCouponModel = new PayCouponModel();
$list = $PayCouponModel->getSelect($where, $fields, 'id desc', $param['page'] . ',' . $param['limit']);
if (!empty($list)) {
$list = $this->transformation($list);
}
return callBack(statusCode()['success'], "查询成功", ['list' => $list]);
} catch (\Exception $e) {
return callBack(statusCode()['success'], "查询成功", ['list' => []]);
}
}
/**
* 代金券展示参数转换
* @param $list
* @return mixed
*/
public function transformation($list)
{
foreach ($list as &$val) {
$fixed_normal_coupon = json_decode($val['coupon_use_rule'], true)['fixed_normal_coupon'];
$val['coupon_amount'] = $fixed_normal_coupon['coupon_amount'];
$val['transaction_minimum'] = $fixed_normal_coupon['transaction_minimum'];
$val['time'] = date('Y.m.d', $val['available_begin_time']) . '-' . date('Y.m.d', $val['available_end_time']);
switch ($val['user_type']) {
case 1:
//普通支付模式
switch ($val['type']) {
case 1:
$val['type_name'] = '线上用';
break;
case 2:
$val['type_name'] = '线下用';
break;
}
break;
case 2:
//服务商支付模式
$val['type_name'] = '通用';
break;
}
switch ($val['coupon_type']) {
case 1:
$val['coupon_type_name'] = '所有商品可用';
break;
case 2:
$val['coupon_type_name'] = '部分商品可用';
break;
}
}
return $list;
}
}