You've already forked qlg.tsgz.moe
addons
app_download_files
extend
hyhproject
admin
app
common
behavior
common
conf
exception
model
Addons.php
Ads.php
Aliyunoss.php
Areas.php
Auth.php
AuthFamily.php
Banks.php
Base.php
Brands.php
Carts.php
CashConfigs.php
CashDraws.php
ChargeItems.php
CompanyBank.php
Ectwallet.php
Express.php
Favorites.php
Goods.php
GoodsAppraises.php
GoodsCats.php
GoodsConsult.php
GoodsVirtuals.php
HomeMenus.php
Hooks.php
Informs.php
Invoices.php
LogMoneys.php
LogPayParams.php
LogPays.php
LogSms.php
MessageQueues.php
Messages.php
OrderComplains.php
OrderRefunds.php
Orders.php
Payments.php
Position.php
Settlements.php
ShopCats.php
ShopExtras.php
Shopping.php
Shops.php
SysConfigs.php
SysSummary.php
Systems.php
Table.php
Tags.php
UserAddress.php
UserLevel.php
UserReward.php
UserScores.php
UserTrees.php
UserVouchers.php
Users.php
taglib
validate
home
home2
mobile2
wechat2
.htaccess
command.php
mobile
oss
static
thinkphp
upload
vendor
wxtmp
.gitignore
.htaccess
.user.ini
404.html
H5436787D.wgt
admin.php
app-release.apk
app_download.html
cash.lock
demo.php
get_startup.php
get_version.php
get_version_new.php
index.html
index.php
reg.lock
robots.txt
291 lines
15 KiB
PHP
Executable File
291 lines
15 KiB
PHP
Executable File
<?php
|
|
namespace wstmart\common\model;
|
|
use think\Db;
|
|
/**
|
|
* ============================================================================
|
|
* 券数据
|
|
*/
|
|
class UserVouchers extends Base{
|
|
public function startGiveVouchers(){
|
|
if('127.0.0.1' != $ip = request()->ip(0) ) return $ip;
|
|
set_time_limit(0);
|
|
$sm = Model('common/SysSummary');
|
|
$sysData= $sm->getInfo('toPayFast,toPaySlow');
|
|
//获取代快付值
|
|
$toPayFast = $sysData['toPayFast'];
|
|
$HistoryToPayFast = $sysData['toPayFast'];
|
|
//今日代快付释放值
|
|
$payFast = round($toPayFast * (dataConf('fastPayDayScale')*0.01),5);
|
|
//获取代慢付值
|
|
$toPaySlow = $sysData['toPaySlow'];
|
|
$HistoryToPaySlow = $sysData['toPaySlow'];
|
|
$m = Model('common/Table');
|
|
$m->setTable('user_vouchers_summary');
|
|
$HistoryVouchersSummarySum = $m->getSum(['isDisabled'=>0],'expectedProductNum+expectedCouponsNum');
|
|
//今日付慢付释放值
|
|
$paySlow = round($toPaySlow * (dataConf('slowPayDayScale')*0.01),5);
|
|
//应释放总额
|
|
$toPaySum = $payFast + $paySlow;
|
|
Db::startTrans();
|
|
try{
|
|
$m = Model('common/Table');
|
|
$m->setTable('user_vouchers_summary');
|
|
if(1 == date('j')){
|
|
//每月损耗
|
|
$monthScale = round((100-dataConf('couponsNextMonthInitSacle'))*0.01,5);
|
|
$giveList = $m->getList([],'id, userId, expectedProductNum, expectedCouponsNum');
|
|
foreach ($giveList as &$g) {
|
|
if($g['expectedProductNum'] > 0){
|
|
$decNum = $g['expectedProductNum'] * $monthScale;
|
|
$this->insertVouchersNotice($g['userId'],0,$decNum,0,$desc='预获产品券月耗损',0,1);
|
|
}
|
|
if($g['expectedCouponsNum'] > 0){
|
|
$decNum = $g['expectedCouponsNum'] * $monthScale;
|
|
$this->insertVouchersNotice($g['userId'],0,0,$decNum,$desc='预获优惠券月耗损',0,1);
|
|
}
|
|
}
|
|
}
|
|
$where['isDisabled'] = 0;
|
|
//总预获产品券
|
|
$allPreProduct = $m->getSum($where,'expectedProductNum');
|
|
//总预获优惠券
|
|
$allPreCoupons = $m->getSum($where,'expectedCouponsNum');
|
|
//最大释放总额 预代付值 * 比例
|
|
$maxPayNum = round(($allPreProduct + $allPreCoupons) * (dataConf('couponsMaxGiveSacle')*0.01),2);
|
|
if($toPaySum > $maxPayNum){
|
|
//加入代慢付值
|
|
$addPaySlow = round($toPaySum - $maxPayNum,5);
|
|
$sm->addSysSummary(0,$addPaySlow,1,'应释放总额大于最大释放总额');
|
|
}
|
|
//扣减系统释放值
|
|
if ($paySlow < 0) $paySlow = 0;
|
|
if ($payFast < 0) $payFast = 0;
|
|
$sm->addSysSummary($payFast,$paySlow,2,'扣减系统释放值');
|
|
//取最小值作为总释放额
|
|
$payNum = min($maxPayNum,$toPaySum);
|
|
if($payNum==0) return;
|
|
//每人应释放数
|
|
$personNum = $payNum / ($allPreProduct + $allPreCoupons);
|
|
//将所有未释放的的isGive设为0
|
|
//获取今日0点时间戳
|
|
$today = strtotime(date("Y-m-d"),time());
|
|
$m->updateInfo('giveDate < '.$today,['isGive'=>0]);
|
|
$list = $m->getList(['isDisabled'=>0,'isGive'=>0],'
|
|
id,
|
|
userId,
|
|
expectedProductNum,
|
|
expectedCouponsNum,
|
|
alreadyProductNum,
|
|
alreadyCouponsNum,
|
|
giveDay
|
|
');
|
|
$time = time();
|
|
foreach ($list as &$v) {
|
|
unset($data);
|
|
$minNumer = 15;
|
|
//都小于15不统计,直接加入代慢付
|
|
// 原:当购户“预获优惠券”、“预获产品券”的值同时≤15元时,所获“已获优惠券”、“已获产品券”转入代快付值
|
|
// 改为:当购户“预获优惠券”、“预获产品券”的值同时≤15元时,所获“已获优惠券”、“已获产品券”转入代慢付值
|
|
if($v['expectedProductNum'] <= $minNumer && $v['expectedCouponsNum'] <= $minNumer){
|
|
$toSlowNum = round($personNum * ($v['expectedProductNum'] + $v['expectedCouponsNum']),2);
|
|
$sm->addSysSummary(0,$toSlowNum,1,'用户ID['.$v['userId'].']双预获券都小于15/日转换退回');
|
|
continue;
|
|
}else if($v['expectedProductNum'] <= $minNumer){//预获产品券小于15;
|
|
$personAlreadyProductNum = 0;
|
|
$personAlreadyCouponsNum = round($personNum * ($v['expectedProductNum'] + $v['expectedCouponsNum']),2);
|
|
}else if($v['expectedCouponsNum'] <= $minNumer){//预获优惠券小于15
|
|
$personAlreadyCouponsNum = 0;
|
|
$personAlreadyProductNum = round($personNum * ($v['expectedProductNum'] + $v['expectedCouponsNum']),2);
|
|
}else{//都大于15
|
|
$couponsScale = dataConf('hasCoupousScale');//优惠券比例
|
|
$productScale = 100 - $couponsScale;//产品券比例
|
|
$personAlreadyProductNum = $personNum * round(($v['expectedProductNum'] + $v['expectedCouponsNum']) * ($productScale*0.01),2);
|
|
$personAlreadyCouponsNum = $personNum * round(($v['expectedProductNum'] + $v['expectedCouponsNum']) * ($couponsScale*0.01),2);
|
|
}
|
|
//实际有预获产品券小于应给值
|
|
if($v['expectedProductNum'] < $personAlreadyProductNum){
|
|
$personAlreadyCouponsNum += $personAlreadyProductNum;
|
|
$personAlreadyProductNum = 0;
|
|
}
|
|
//实际有预获优惠券小于应给值
|
|
if($v['expectedCouponsNum'] < $personAlreadyCouponsNum){
|
|
$personAlreadyProductNum += $personAlreadyCouponsNum;
|
|
$personAlreadyCouponsNum = 0;
|
|
}
|
|
//都小于就算了
|
|
if($v['expectedProductNum'] < $personAlreadyProductNum || $v['expectedCouponsNum'] < $personAlreadyCouponsNum){
|
|
$personAlreadyProductNum = 0;
|
|
$personAlreadyCouponsNum = 0;
|
|
}
|
|
|
|
|
|
if($personAlreadyProductNum >= 0.01){
|
|
|
|
$this->insertVouchersNotice($v['userId'],0,$personAlreadyProductNum,0,$desc='分润扣减',0,0);//预获产品券减
|
|
//加入变动记录,最后一位//1产品券2优惠券3旺旺券4现金券
|
|
$this->insertAlreadyVouchersNotice($v['userId'],0,$personAlreadyProductNum,0,'预获产品券转换所得',1,1);
|
|
// Model('common/LogMoneys')->addMoneyLog(0,$v['userId'],0,2,'预获产品券转换所得',1,$personAlreadyProductNum,'dayPay',1);
|
|
$data['alreadyProductNum'] = $v['alreadyProductNum'] + $personAlreadyProductNum;//已获产品券加
|
|
}
|
|
if($personAlreadyCouponsNum >= 0.01){
|
|
$this->insertVouchersNotice($v['userId'],0,0,$personAlreadyCouponsNum,$desc='分润扣减',0,0);//预获优惠券减
|
|
//加入变动记录,最后一位//1产品券2优惠券3旺旺券4现金券
|
|
$this->insertAlreadyVouchersNotice($v['userId'],0,0,$personAlreadyCouponsNum,'预获优惠券转换所得',1,1);
|
|
// Model('common/LogMoneys')->addMoneyLog(0,$v['userId'],0,2,'预获优惠券转换所得',1,$personAlreadyCouponsNum,'dayPay',2);
|
|
$data['alreadyCouponsNum'] = $v['alreadyCouponsNum'] + $personAlreadyCouponsNum;//已获优惠券加
|
|
}
|
|
$data['isGive'] = 1;
|
|
$data['giveDate'] = $time;
|
|
$data['giveDay'] = $v['giveDay'] + 1;
|
|
Db::name('user_vouchers_summary')->where(['id'=>$v['id']])->update($data);
|
|
}
|
|
Db::commit();
|
|
Db::startTrans();
|
|
// 每次所得值(费、税前)同时扣减“助购预获”值和“预获产品券”值,直至减为负数为止,负数部分按每天0.1%耗损。
|
|
// A、当“助购预获”值为负数时,每天0.1%耗损在“预获产品券”值扣减。
|
|
// B、当“预获产品券”值为负数时,每天0.1%耗损在“预获优惠券”值扣减。
|
|
// C、当“助购预获”值和“预获产品券”值同为负数时,每天0.1%耗损在“预获优惠券”值同时扣减。
|
|
$data = Db::name('user_vouchers_summary')->whereOr(["helpSaleNum"=>["lt", 0]])->whereOr(["expectedProductNum"=>["lt", 0]])->select();
|
|
foreach ($data as $_each){
|
|
if($_each["expectedProductNum"]<0){
|
|
$count = (float)$_each["expectedProductNum"];
|
|
if($_each["helpSaleNum"] < 0)$count+=(float)$_each["helpSaleNum"];
|
|
model('common/UserVouchers')->insertVouchersNotice($_each["userId"], 0, 0, $count*((float)dataConf("deductionRateWhennegPreCoupons")/100),
|
|
"预获产品券为负,每天耗损预获优惠券", 0);
|
|
}else{
|
|
$count = (float)$_each["helpSaleNum"];
|
|
model('common/UserVouchers')->insertVouchersNotice($_each["userId"], 0, $count*((float)dataConf("deductionRateWhennegPreCoupons")/100),
|
|
0, "助购券为负,每天耗损预获产品券", 0);
|
|
}
|
|
}
|
|
Db::commit();
|
|
$sm = Model('common/SysSummary');
|
|
$sysData= $sm->getInfo('toPayFast,toPaySlow');
|
|
$toPayFast = $sysData['toPayFast'];
|
|
$toPaySlow = $sysData['toPaySlow'];
|
|
$m = Model('common/Table');
|
|
$m->setTable('user_vouchers_summary');
|
|
$vouchersSummarySum = $m->getSum(['isDisabled'=>0],'expectedProductNum+expectedCouponsNum');
|
|
$deltaFast = $HistoryToPayFast - $toPayFast;
|
|
$deltaSlow = $HistoryToPaySlow - $toPaySlow;
|
|
$deltaConv = $HistoryVouchersSummarySum - $vouchersSummarySum;
|
|
$unConv = $deltaFast + $deltaSlow - $deltaConv;
|
|
if ($unConv > 0) $sm->addSysSummary(0, $unConv, 1, "加入系统未转换值");
|
|
return WSTReturn("ok", 1);
|
|
}catch (\Exception $e) {
|
|
dump($e);
|
|
Db::rollback();errLog($e);
|
|
return WSTReturn($e->getMessage(),-1);
|
|
}
|
|
}
|
|
/**
|
|
* 插入已获会员券值记录
|
|
* @param [type] $userId [会员ID]
|
|
* @param [type] $orderId [订单ID]
|
|
* @param integer $expectedProductNum [预获产品券]
|
|
* @param integer $expectedCouponsNum [预获优惠券]
|
|
* @param string $desc [备注]
|
|
* @param string $isAdd [1增加0扣除]
|
|
* @return [type] [description]
|
|
*/
|
|
public function insertAlreadyVouchersNotice($userId,$orderId,$alreadyProductNum=0,$alreadyCouponsNum=0,$desc='',$isAdd=1,$isShow=1){
|
|
//插入详细表
|
|
$m = Model('common/Table');
|
|
$m->setTable('log_moneys');
|
|
if($alreadyProductNum>0){
|
|
$m->insertInfo(['targetType'=>0,'targetId'=>$userId, 'dataId'=>$orderId,'dataSrc'=>1,'money'=>$alreadyProductNum,'moneyName'=>1,'remark'=>$desc,'moneyType'=>$isAdd,'payType'=>'qlgpay','createTime'=>date('Y/m/d H:i:s')]);
|
|
}
|
|
if($alreadyCouponsNum>0){
|
|
$m->insertInfo(['targetType'=>0,'targetId'=>$userId, 'dataId'=>$orderId,'dataSrc'=>1,'money'=>$alreadyCouponsNum,'moneyName'=>2,'remark'=>$desc,'moneyType'=>$isAdd,'payType'=>'qlgpay','createTime'=>date('Y/m/d H:i:s')]);
|
|
}
|
|
//插入或更新详细表
|
|
$m->setTable('users');
|
|
$summaryInfo = $m->getInfo(['userId'=>$userId],'userId,productNum,couponsNum');
|
|
if(1 == $isAdd){
|
|
$data['productNum'] = $summaryInfo['productNum'] + $alreadyProductNum;
|
|
$data['couponsNum'] = $summaryInfo['couponsNum'] + $alreadyCouponsNum;
|
|
}else{
|
|
$data['productNum'] = $summaryInfo['productNum'] - $alreadyProductNum;
|
|
$data['couponsNum'] = $summaryInfo['couponsNum'] - $alreadyCouponsNum;
|
|
}
|
|
$m->updateInfo(['userId'=>$userId],$data);
|
|
}
|
|
|
|
/**
|
|
* 插入已获会员券值记录
|
|
* @param [type] $userId [会员ID]
|
|
* @param [type] $orderId [订单ID]
|
|
* @param integer $expectedProductNum [预获产品券]
|
|
* @param integer $expectedCouponsNum [预获优惠券]
|
|
* @param string $desc [备注]
|
|
* @param string $isAdd [1增加0扣除]
|
|
* @return [type] [description]
|
|
*/
|
|
public function insertHelpSaleNotice($userId,$orderId,$helpSaleNum=0,$desc='',$isAdd=1,$isShow=1){
|
|
//插入详细表
|
|
$m = Model('common/Table');
|
|
$m->setTable('user_help_get_notice');
|
|
$m->insertInfo(['userId'=>$userId,'orderId'=>$orderId,'helpGet'=>$helpSaleNum,'remark'=>$desc,'isAdd'=>$isAdd,'isShow'=>$isShow,'createTime'=>time()]);
|
|
|
|
|
|
|
|
$m = Model('common/Table');
|
|
$m->setTable('user_vouchers_summary');
|
|
if($summaryInfo = $m->getInfo(['userId'=>$userId],'id,helpSaleNum')){
|
|
if(1 == $isAdd){
|
|
$data['helpSaleNum'] = $summaryInfo['helpSaleNum'] + $helpSaleNum;
|
|
}else{
|
|
$data['helpSaleNum'] = $summaryInfo['helpSaleNum'] - $helpSaleNum;
|
|
}
|
|
$m->updateInfo(['id'=>$summaryInfo['id']],$data);
|
|
}else{
|
|
if(0 == $isAdd){
|
|
$helpSaleNum *= -1;
|
|
}
|
|
$data = compact('userId','helpSaleNum');
|
|
$m->insertInfo($data);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 插入会员券值记录
|
|
* @param [type] $userId [会员ID]
|
|
* @param [type] $orderId [订单ID]
|
|
* @param integer $expectedProductNum [预获产品券]
|
|
* @param integer $expectedCouponsNum [预获优惠券]
|
|
* @param string $desc [备注]
|
|
* @param string $isAdd [1增加0扣除]
|
|
* @return [type] [description]
|
|
*/
|
|
public function insertVouchersNotice($userId,$orderId,$expectedProductNum=0,$expectedCouponsNum=0,$desc='',$isAdd=1,$isShow=1){
|
|
//插入详细表
|
|
$m = Model('common/Table');
|
|
$m->setTable('user_vouchers_notice');
|
|
$m->insertInfo(['userId'=>$userId,'orderId'=>$orderId,'expectedProductNum'=>$expectedProductNum,'expectedCouponsNum'=>$expectedCouponsNum,'remark'=>$desc,'moneyType'=>$isAdd,'isShow'=>$isShow,'createTime'=>time()]);
|
|
//插入或更新详细表
|
|
$m->setTable('user_vouchers_summary');
|
|
if($summaryInfo = $m->getInfo(['userId'=>$userId],'id,expectedProductNum,expectedCouponsNum')){
|
|
if(1 == $isAdd){
|
|
$data['expectedProductNum'] = $summaryInfo['expectedProductNum'] + $expectedProductNum;
|
|
$data['expectedCouponsNum'] = $summaryInfo['expectedCouponsNum'] + $expectedCouponsNum;
|
|
}else{
|
|
$data['expectedProductNum'] = $summaryInfo['expectedProductNum'] - $expectedProductNum;
|
|
$data['expectedCouponsNum'] = $summaryInfo['expectedCouponsNum'] - $expectedCouponsNum;
|
|
}
|
|
$m->updateInfo(['id'=>$summaryInfo['id']],$data);
|
|
}else{
|
|
if(0 == $isAdd){
|
|
$expectedProductNum *= -1;
|
|
$expectedCouponsNum *= -1;
|
|
}
|
|
$data = compact('userId','expectedProductNum','expectedCouponsNum');
|
|
$data['alreadyProductNum'] = 0;
|
|
$data['alreadyCouponsNum'] = 0;
|
|
$data['isGive'] = 1;
|
|
$data['giveDate'] = time();
|
|
$data['giveDay'] = 0;
|
|
$m->insertInfo($data);
|
|
}
|
|
}
|
|
}
|