This commit is contained in:
liu 2024-06-29 11:43:45 +08:00
commit f8b3d20c49
6 changed files with 235 additions and 214 deletions

View File

@ -29,7 +29,7 @@ class UserProductStorageLists extends BaseAdminDataLists implements ListsSearchI
{
return [
'=' => ['uid', 'oid', 'product_id'],
'between_time' => ['create_time'],
'between_time' => 'create_time',
];
}

View File

@ -1,138 +0,0 @@
<?php
namespace app\api\controller;
use app\common\model\finance\CapitalFlow;
use app\common\model\user\User;
use app\common\model\vip_flow\VipFlow;
class BackController extends BaseApiController
{
public $notNeedLogin = ['backProfit'];
/**
* 返利
* @return void
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function backProfit()
{
//读取前3天的值 按照用户id和类型分下 加到对应的钱
$startTime = strtotime(date('Y-m-d', strtotime('-3 days')));
$endTime = strtotime(date('Y-m-d'));
$result = VipFlow::where('create_time', '>=', $startTime)
->where('create_time', '<', $endTime)
->group('user_id, pay_type')
->field('user_id, pay_type, COUNT(*) as transaction_count, SUM(number) as total_amount')
->select()->toArray();
// 遍历查询结果并分类 现金不进入反的逻辑
//3余额 18采购款 7微信小程序 9微信条码 13 支付宝条码支付
$Balance = [];
$Procurement = [];
$WechatMiniPay = [];
$WechatBarcodePay = [];
$AliBarcodePay = [];
foreach ($result as $row) {
$payType = $row['pay_type'];
$userId = $row['user_id'];
$totalAmount = $row['total_amount'];
switch ($payType) {
case 3:
$user_now_money = User::where(
[
'id' => $userId
]
)->withTrashed()->value('now_money');
$Balance[] = [
'id' => $userId,
'now_money' => bcadd($user_now_money, $totalAmount, 2),
];
break;
case 7:
$WechatMiniPay[] = [
'id' => $userId,
'total_amount' => $totalAmount,
];
break;
case 9:
$WechatBarcodePay[] = [
'id' => $userId,
'total_amount' => $totalAmount,
];
break;
case 13:
$AliBarcodePay[] = [
'id' => $userId,
'total_amount' => $totalAmount,
];
break;
case 18:
$purchase_funds_money = User::where(
[
'id' => $userId
]
)->withTrashed()->value('purchase_funds');
$Procurement[] = [
'id' => $userId,
'purchase_funds' => bcadd($purchase_funds_money, $totalAmount, 2),
];
break;
}
}
//入记录表的话查询后便利入 3余额 18采购款
if ($Balance) {
(new User())->saveAll($Balance);
$this->dealCapital($startTime, $endTime, 3);
}
if ($Procurement) {
(new User())->saveAll($Procurement);
$this->dealCapital($startTime, $endTime, 18);
}
//7微信小程序 9微信条码 13 支付宝条码支付
}
public function dealCapital($startTime, $endTime, $pay_type)
{
$vipFrozen = VipFlow::where('create_time', '>=', $startTime)
->where('create_time', '<', $endTime)
->where('pay_type', $pay_type)->select()->toArray();
if ($pay_type == 18) {
$category_title = 'system_purchase_add';
$title = '系统增加采购款';
$mark = '系统增加采购款';
$filed = 'purchase_funds';
} else {
$category_title = 'system_balance_add';
$title = '系统增加余额';
$mark = '系统反余额冻结';
$filed = 'now_money';
}
$newArr = [];
foreach ($vipFrozen as $k => $value) {
$user_funds = User::where('id', $value['user_id'])->value($filed);
$newArr[$k]['uid'] = $value['user_id'];
$newArr[$k]['category'] = $category_title;
$newArr[$k]['link_type'] = 'order';
$newArr[$k]['link_id'] = $value['order_id'];
$newArr[$k]['amount'] = $value['number'];
$newArr[$k]['before_balance'] = $user_funds;
$newArr[$k]['balance'] = bcadd($user_funds, $value['number'], 2);
$newArr[$k]['create_time'] = date('Y-m-d H:i:s');
$newArr[$k]['type'] = 'in';
$newArr[$k]['title'] = $title . "{$value['number']}";
$newArr[$k]['mark'] = $mark;
}
(new CapitalFlow())->saveAll($newArr);
}
}

View File

@ -0,0 +1,120 @@
<?php
namespace app\common\logic;
use app\common\model\finance\CapitalFlow;
use app\common\model\store_order\StoreOrder;
use app\common\model\user\User;
use app\common\model\vip_flow\VipFlow;
use app\common\service\pay\PayService;
use support\Log;
class BackLogic extends BaseLogic
{
public $payService=new PayService();
/**
* 返利
* @return void
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function backProfit()
{
//读取前3天的值
$startTime = strtotime(date('Y-m-d', strtotime('-3 days')));
$endTime = strtotime(date('Y-m-d'));
$result = VipFlow::where('create_time', '>=', $startTime)
->where('create_time', '<', $endTime)
->where('status', 0)
->where('type', 0)
->select();
//3余额 18采购款 7微信小程序 9微信条码 13 支付宝条码支付
foreach ($result as $row) {
switch ($row['pay_type']) {
case 3:
case 18:
$this->dealCapital($row['pay_type'],$row);
break;
case 7:
case 9:
$this->wechat_refund($row['pay_type'],$row);
break;
case 13:
$this->ali_pay_refund($row['pay_type'],$row);
break;
}
}
//7微信小程序 9微信条码 13 支付宝条码支付
}
public function dealCapital($pay_type, $order)
{
$user = User::where('id', $order['user_id'])->find();
if ($pay_type == 18) {
$category_title = 'system_purchase_add';
$title = '系统增加采购款';
$mark = '系统增加采购款';
$filed = 'purchase_funds';
$user->purchase_funds = bcadd($user['purchase_funds'], $order['number'], 2);
} else {
$category_title = 'system_balance_add';
$title = '系统增加余额';
$mark = '系统反余额冻结';
$filed = 'now_money';
$user->now_money = bcadd($user['now_money'], $order['number'], 2);
}
$user->save();
$newArr['uid'] = $order['user_id'];
$newArr['category'] = $category_title;
$newArr['link_type'] = 'order';
$newArr['link_id'] = $order['order_id'];
$newArr['amount'] = $order['number'];
$newArr['before_balance'] = $user[$filed];
$newArr['balance'] = bcadd($user[$filed], $order['number'], 2);
$newArr['create_time'] = date('Y-m-d H:i:s');
$newArr['type'] = 'in';
$newArr['title'] = $title . "{$order['number']}";
$newArr['mark'] = $mark;
CapitalFlow::create($newArr);
}
public function wechat_refund($pay_type, $row){
$pay_price=StoreOrder::where('id',$row['order_id'])->value('pay_price');
$order = [
'out_trade_no' => (string)$row['order_sn'],
'out_refund_no' => time(),
'reason' => '待返还金返还',
'amount' => [
'refund' => bcmul($row['number'], 100),
'total' => bcmul($pay_price, 100),
'currency' => 'CNY',
],
];
try{
$this->payService->wechat->refund($order);
VipFlow::where('id',$row['id'])->update(['status'=>1]);
}catch (\Exception $e){
Log::error('微信退款失败'.$e->getMessage(),$row->toArray());
}
}
public function ali_pay_refund($pay_type, $row){
$order = [
'out_trade_no' => (string)$row['order_sn'],
'refund_reason' => '待返还金返还',
'refund_amount' => $row['number'],
];
try{
$this->payService->alipay->refund($order);
VipFlow::where('id',$row['id'])->update(['status'=>1]);
}catch (\Exception $e){
Log::error('支付宝退款失败'.$e->getMessage(),$row->toArray());
}
}
}

View File

@ -12,63 +12,62 @@ class CommissionLogic extends BaseLogic
/**
* 走村长分润
*/
public static function setVillage($order,$village_uid=0,$brigade_uid=0, $transaction_id=0)
public static function setVillage($order, $village_uid = 0, $brigade_uid = 0, $transaction_id = 0)
{
$user_1=self::user($order, 0.05, $transaction_id,$village_uid,14);//村长
$user_2=self::user($order, 0.03, $transaction_id,0,12);//会员、厨师
$user_3=self::user($order, 0.01, $transaction_id,$brigade_uid,15);//队长
$user_1 = self::user($order, 0.05, $transaction_id, $village_uid, 14); //村长
$user_2 = self::user($order, 0.03, $transaction_id, 0, 12); //会员、厨师
$user_3 = self::user($order, 0.01, $transaction_id, $brigade_uid, 15); //队长
$platform=self::platform($order, 0.02, $transaction_id);//平台
$store=self::store($order, 0.05, $transaction_id,0);//门店
$user_4=self::user($order, 0.02, $transaction_id,0,16);//其他
$platform = self::platform($order, 0.02, $transaction_id); //平台
$store = self::store($order, 0.05, $transaction_id, 0); //门店
$attrition = self::attrition($order, 0.02,$transaction_id,16); //损耗
$moeny=bcadd(bcadd(bcadd(bcadd($user_1,$user_2,2),$user_3,2),$platform,2),bcadd($store,$user_4,2),2);
$moeny = bcadd(bcadd(bcadd(bcadd($user_1, $user_2, 2), $user_3, 2), $platform, 2), bcadd($store, $attrition, 2), 2);
self::suppliter($order, $moeny, $transaction_id);
}
/**
* 走队长分润
*/
public static function setBrigade($order,$village_uid=0,$brigade_uid=0, $transaction_id=0)
public static function setBrigade($order, $village_uid = 0, $brigade_uid = 0, $transaction_id = 0)
{
$user_1=self::user($order, 0.05, $transaction_id,$brigade_uid,14);//队长
$user_2=self::user($order, 0.03, $transaction_id,0,12);////会员、厨师
$user_3=self::user($order, 0.01, $transaction_id,$village_uid,15);//村长
$user_1 = self::user($order, 0.05, $transaction_id, $brigade_uid, 14); //队长
$user_2 = self::user($order, 0.03, $transaction_id, 0, 12); ////会员、厨师
$user_3 = self::user($order, 0.01, $transaction_id, $village_uid, 15); //村长
$platform=self::platform($order, 0.02, $transaction_id);//平台
$store=self::store($order, 0.05, $transaction_id,0);//门店
$user_4=self::user($order, 0.02, $transaction_id,0,16);//其他
$platform = self::platform($order, 0.02, $transaction_id); //平台
$store = self::store($order, 0.05, $transaction_id, 0); //门店
$attrition = self::attrition($order, 0.02,$transaction_id,16); //损耗
$moeny=bcadd(bcadd(bcadd(bcadd($user_1,$user_2,2),$user_3,2),$platform,2),bcadd($store,$user_4,2),2);
$moeny = bcadd(bcadd(bcadd(bcadd($user_1, $user_2, 2), $user_3, 2), $platform, 2), bcadd($store, $attrition, 2), 2);
self::suppliter($order, $moeny, $transaction_id);
}
/**
* 走厨师分润
*/
public static function setCook($order,$village_uid=0,$brigade_uid=0, $transaction_id=0)
public static function setCook($order, $village_uid = 0, $brigade_uid = 0, $transaction_id = 0)
{
$user_1=self::user($order, 0.07, $transaction_id,0,12);//会员、厨师
$user_2=self::user($order, 0.01, $transaction_id,$village_uid,14);//村长
$user_3=self::user($order, 0.01, $transaction_id,$brigade_uid,15);//队长
$user_1 = self::user($order, 0.07, $transaction_id, 0, 12); //会员、厨师
$user_2 = self::user($order, 0.01, $transaction_id, $village_uid, 14); //村长
$user_3 = self::user($order, 0.01, $transaction_id, $brigade_uid, 15); //队长
$platform=self::platform($order, 0.02, $transaction_id);//平台
$store=self::store($order, 0.05, $transaction_id,0);//门店
$user_4=self::user($order, 0.02, $transaction_id,0,16);//其他
$platform = self::platform($order, 0.02, $transaction_id); //平台
$store = self::store($order, 0.05, $transaction_id, 0); //门店
$attrition = self::attrition($order, 0.02,$transaction_id,16); //损耗
$moeny=bcadd(bcadd(bcadd(bcadd($user_1,$user_2,2),$user_3,2),$platform,2),bcadd($store,$user_4,2),2);
$moeny = bcadd(bcadd(bcadd(bcadd($user_1, $user_2, 2), $user_3, 2), $platform, 2), bcadd($store, $attrition, 2), 2);
self::suppliter($order, $moeny, $transaction_id);
}
/**
* 走线下分润
*/
public static function setStore($order, $transaction_id=0)
public static function setStore($order, $transaction_id = 0)
{
$platform= self::platform($order, 0.05, $transaction_id);//平台
$store= self::store($order, 0.02, $transaction_id,0);//门店
$user= self::user($order, 0.01, $transaction_id,0,16);//其他
$moeny=bcadd(bcadd($platform,$store,2),$user,2);
self::suppliter($order, $moeny, $transaction_id);
$platform = self::platform($order, 0.05, $transaction_id); //平台
$store = self::store($order, 0.02, $transaction_id, 0); //门店
$attrition = self::attrition($order, 0.01,$transaction_id,16); //损耗
$moeny = bcadd(bcadd($platform, $store, 2), $attrition, 2);
self::suppliter($order, $moeny, $transaction_id);
}
@ -78,8 +77,8 @@ class CommissionLogic extends BaseLogic
public static function platform($order, $platformRate, $transaction_id)
{
$financeLogic = new StoreFinanceFlowLogic();
$financeLogic->order=$order;
$financeLogic->user['uid']=$order['uid'];
$financeLogic->order = $order;
$financeLogic->user['uid'] = $order['uid'];
$fees = bcdiv(bcmul($order['pay_price'], $platformRate, 2), 1, 2);
if ($fees > 0) {
$financeLogic->in($transaction_id, $fees, OrderEnum::ORDER_HANDLING_FEES, $order['store_id'], 0, 0, $order['pay_type']); //平台手续费
@ -94,9 +93,9 @@ class CommissionLogic extends BaseLogic
public static function suppliter($order, $platformRate, $transaction_id)
{
$financeLogic = new StoreFinanceFlowLogic();
$financeLogic->order=$order;
$financeLogic->user['uid']=$order['uid'];
$fees = bcsub($order['pay_price'], $platformRate,2);
$financeLogic->order = $order;
$financeLogic->user['uid'] = $order['uid'];
$fees = bcsub($order['pay_price'], $platformRate, 2);
if ($fees > 0) {
$financeLogic->in($transaction_id, $fees, OrderEnum::SUPPLIER_ORDER_OBTAINS, $order['store_id'], 0, 0, $order['pay_type']);
$financeLogic->out($transaction_id, $fees, OrderEnum::SUPPLIER_ORDER_OBTAINS, $order['store_id'], $order['staff_id'], 0, $order['pay_type']);
@ -106,12 +105,12 @@ class CommissionLogic extends BaseLogic
/**
* 门店分润
*/
public static function store($order, $platformRate, $transaction_id,$uid)
public static function store($order, $platformRate, $transaction_id, $uid)
{
$financeLogic = new StoreFinanceFlowLogic();
$financeLogic->user['uid']=$order['uid'];
$financeLogic->other_arr['vip_uid']=$uid;
$financeLogic->order=$order;
$financeLogic->user['uid'] = $order['uid'];
$financeLogic->other_arr['vip_uid'] = $uid;
$financeLogic->order = $order;
$financeLogic->in($transaction_id, $order['pay_price'], OrderEnum::USER_ORDER_PAY, $order['store_id'], $order['staff_id'], 0, $order['pay_type']); //用户订单支付
//缴纳齐全了就加商户没有就加到平台
@ -149,7 +148,7 @@ class CommissionLogic extends BaseLogic
/**
* 分给用户
*/
public static function user($order, $userRate, $transaction_id,$uid=0,$enum = 0)
public static function user($order, $userRate, $transaction_id, $uid = 0, $enum = 0)
{
$financeLogic = new StoreFinanceFlowLogic();
$fees = bcmul($order['pay_price'], $userRate, 2);
@ -160,13 +159,30 @@ class CommissionLogic extends BaseLogic
$capitalFlowDao = new CapitalFlowLogic($GiveUser);
$capitalFlowDao->userIncome('system_balance_add', 'order', $order['id'], $fees);
}
$financeLogic->user['uid']=$order['uid'];
$financeLogic->other_arr['vip_uid']=$uid;
$financeLogic->order=$order;
$financeLogic->user['uid'] = $order['uid'];
$financeLogic->other_arr['vip_uid'] = $uid;
$financeLogic->order = $order;
$financeLogic->in($transaction_id, $fees, $enum, $order['store_id'], 0, 0, $order['pay_type']);
$financeLogic->out($transaction_id, $fees, $enum, $order['store_id'], $order['staff_id'], 0, $order['pay_type']);
$financeLogic->save();
}
return $fees;
}
/**
* 损耗金
*/
public static function attrition($order, $userRate, $transaction_id, $enum)
{
$financeLogic = new StoreFinanceFlowLogic();
$financeLogic->order = $order;
$financeLogic->user['uid'] = $order['uid'];
$fees = bcmul($order['pay_price'], $userRate, 2);
if ($fees > 0) {
SystemStore::where('id', $order['store_id'])->inc('attrition', $fees)->update();
$financeLogic->in($transaction_id, $fees, $enum, $order['store_id'], $order['staff_id'], 0, $order['pay_type']);
$financeLogic->out($transaction_id, $fees, $enum, $order['store_id'], $order['staff_id'], 0, $order['pay_type']);
}
return $fees;
}
}

View File

@ -452,7 +452,7 @@ class PayNotifyLogic extends BaseLogic
$deal_money = bcdiv($extra['amount']['refund'], 100, 2);
$check_user_sing = UserSign::where('order_id', $order['order_id'])->count();
if (in_array($order['pay_type'], [PayEnum::BALANCE_PAY, PayEnum::PURCHASE_FUNDS])) {
if ($order['pay_type'] == PayEnum::BALANCE_PAY) { //用户余额
if ($order['pay_type'] == PayEnum::BALANCE_PAY) { //用户余额支付
$user->now_money = bcadd($user->now_money, $deal_money, 2);
$user->save();
//增加数量
@ -460,7 +460,7 @@ class PayNotifyLogic extends BaseLogic
//退款
$capitalFlowDao->userIncome('system_balance_back', 'system_back', $order['id'], $deal_money);
}
if ($order['pay_type'] == PayEnum::PURCHASE_FUNDS) { //采购款
if ($order['pay_type'] == PayEnum::PURCHASE_FUNDS) { //采购款支付
$user->purchase_funds = bcadd($user->purchase_funds, $deal_money, 2);
$user->save();
//增加数量
@ -480,7 +480,7 @@ class PayNotifyLogic extends BaseLogic
//微信日志 user_order_refund
$capitalFlowDao->userIncome('user_order_refund', 'system_back', $order['id'], $deal_money, '', 1);
//处理财务流水退还
self::store_finance_back($orderSn);
(new StoreFinanceFlowLogic())->store_finance_back($orderSn);
self::addStock($order['id']); //微信
return true;
// self::afterPay($order,$extra['transaction_id']);
@ -521,28 +521,7 @@ class PayNotifyLogic extends BaseLogic
/**
* 财务退还金额相关
* @param $orderSn
* @return void
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public static function store_finance_back($orderSn)
{
$data = StoreFinanceFlow::where('order_sn', $orderSn)
->where(['financial_pm' => 1])
->select()->toArray();
foreach ($data as &$value) {
unset($value['id']);
$value['financial_record_sn'] = (new StoreFinanceFlowLogic)->getSn();
$value['financial_pm'] = 0;
$value['financial_type'] = OrderEnum::PAY_BACK;
$value['create_time'] = time();
}
(new StoreFinanceFlow)->saveAll($data);
}
/**
* 现金退款相关

View File

@ -70,6 +70,7 @@ class StoreFinanceFlowLogic extends BaseLogic
switch ($financialType) {
case OrderEnum::MERCHANT_ORDER_OBTAINS: // 商户
case OrderEnum::ORDER_MARGIN: // 商户保证金
case OrderEnum::OTHER_ORDER_OBTAINS: // 损耗'
$data['type'] = OrderEnum::MERCHANT;
break;
case OrderEnum::PLATFORM_ORDER_OBTAINS: // 平台
@ -144,20 +145,63 @@ class StoreFinanceFlowLogic extends BaseLogic
*/
public function updateStatusStore($order_id, $store_id, $money, $deposit)
{
StoreFinanceFlow::where(['order_id' => $order_id,'financial_type' => 11])->update(['status' => 1]);
StoreFinanceFlow::where(['order_id' => $order_id,'financial_type' => 2])->update(['status' => 1]);
if ($money>0) {
StoreFinanceFlow::where(['order_id' => $order_id, 'financial_type' => 11])->update(['status' => 1]);
StoreFinanceFlow::where(['order_id' => $order_id, 'financial_type' => 2])->update(['status' => 1]);
if ($money > 0) {
SystemStore::where('id', $store_id)->inc('store_money', $money)->update();
}
if ($deposit>0) {
if ($deposit > 0) {
SystemStore::where('id', $store_id)->inc('paid_deposit', $deposit)->update();
}
$find = StoreFinanceFlow::where(['order_id' => $order_id, 'financial_pm' => 1, 'financial_type' => 16,'status'=>0])->find();
StoreFinanceFlow::where(['order_id' => $order_id,'financial_type' => 16])->update(['status' => 1]);
$find = StoreFinanceFlow::where(['order_id' => $order_id, 'financial_pm' => 1, 'financial_type' => 16, 'status' => 0])->find();
StoreFinanceFlow::where(['order_id' => $order_id, 'financial_type' => 16])->update(['status' => 1]);
if ($find) {
if ($find['number'] > 0) {
SystemStore::where('id', $store_id)->inc('attrition', $find['number'])->update();
}
}
}
/**
* 财务退还金额相关
* @param $orderSn
* @return void
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function store_finance_back($orderSn)
{
$data = StoreFinanceFlow::where('order_sn', $orderSn)
->where(['financial_pm' => 1])
->select()->toArray();
foreach ($data as &$value) {
unset($value['id']);
$value['financial_record_sn'] = (new StoreFinanceFlowLogic)->getSn();
$value['financial_pm'] = 0;
$value['financial_type'] = OrderEnum::PAY_BACK;
$value['create_time'] = time();
if ($value['status'] == 1) {
switch ($value['type']) {
case 0:
$user = User::where('id', $value['other_uid'])->findOrEmpty();
$capitalFlowDao = new CapitalFlowLogic($user);
$capitalFlowDao->userExpense('system_now_money_back', 'system_back', $value['order_id'], $value['number']);
break;
case 1:
if ($value['number'] > 0 &&$value['financial_type']==2) {
SystemStore::where('id', $value['store_id'])->dec('store_money',$value['number'])->update();
}
if ($value['number'] > 0 &&$value['financial_type']==11) {
SystemStore::where('id', $value['store_id'])->dec('paid_deposit',$value['number'])->update();
}
if ($value['number'] > 0 &&$value['financial_type']==16) {
SystemStore::where('id', $value['store_id'])->dec('attrition',$value['number'])->update();
}
break;
}
}
}
(new StoreFinanceFlow)->saveAll($data);
}
}