346 行
17 KiB
PHP
可执行文件
346 行
17 KiB
PHP
可执行文件
<?php
|
||
|
||
namespace NotifyApi\Controller;
|
||
|
||
use Common\Model\RetailFranchiseeBillModel;
|
||
use Common\Model\RetailFranchiseeModel;
|
||
use Common\Model\RetailPayOrderModel;
|
||
use Common\Model\RetailRechargeOrderModel;
|
||
use Common\Model\StoreModel;
|
||
use Think\Controller;
|
||
|
||
//[1.6.8]
|
||
|
||
/**
|
||
* 加盟店资金处理任务
|
||
* Class FranchiseeRateDealController
|
||
* @package NotifyApi\Controller
|
||
*/
|
||
class FranchiseeRateDealController extends Controller
|
||
{
|
||
public function deal()
|
||
{
|
||
// 开始统计时间
|
||
$start = mSecTime();
|
||
// 加盟商查询
|
||
$FranchiseeModel = new RetailFranchiseeModel();
|
||
$Franchisee = $FranchiseeModel->getBillStoreList();//获取加盟店列表
|
||
$FranchiseeBillModel = new RetailFranchiseeBillModel();
|
||
$PayOrderModel = new RetailPayOrderModel();
|
||
$RechargeOrderModel = new RetailRechargeOrderModel();
|
||
// 加盟商运营统计
|
||
foreach ($Franchisee as $val) {
|
||
$map = array(
|
||
'store_id' => $val['store_id'],
|
||
);
|
||
// 上次统计信息
|
||
$FranchiseeBills = $FranchiseeBillModel->getOrderFind($map, 'franchisee_time,periods,add_time', $order = 'id DESC');
|
||
if ($FranchiseeBills) {
|
||
$franchisee_time = explode('-', $FranchiseeBills['franchisee_time']);
|
||
$start_time = strtotime(str_replace('.', '-', $franchisee_time[1])) + 1;//距离上次结束时间后加1秒
|
||
$diff_time = (time() - $start_time) / 86400;//不足一天则不生成账单
|
||
if ($val['type'] == 1) {
|
||
//日结
|
||
$account_time = $start_time + 86400;
|
||
if ($diff_time < 1) {
|
||
echo 'The statistical time is not reached 1' . PHP_EOL;
|
||
continue;
|
||
} else {
|
||
$periods_nub = floor($diff_time);// 循环周期
|
||
}
|
||
} else if ($val['type'] == 2) {
|
||
//周结
|
||
$account_time = $start_time + 86400 * 7;
|
||
if ($diff_time < 7) {
|
||
echo 'The statistical time is not reached 2' . PHP_EOL;
|
||
continue;
|
||
} else {
|
||
$periods_nub = floor($diff_time / 7);// 循环周期
|
||
}
|
||
} else if ($val['type'] == 3) {
|
||
//月结
|
||
$account_time = strtotime("+1 months", $start_time);
|
||
$arr = $this->getMonthNum(date('Y-m-d', $start_time), date('Y-m-d', time()));
|
||
$periods_nub = $arr[1];// 循环周期
|
||
if ($periods_nub < 1) {
|
||
echo 'The statistical time is not reached 3' . PHP_EOL;
|
||
continue;
|
||
}
|
||
}
|
||
$periods = $FranchiseeBills['periods'] + 1;// 期数
|
||
|
||
} else {
|
||
// 初次统计时间,之前没有生成过账单
|
||
$start_time = $val['add_time'];//以加盟店的添加时间(00:00:00)为开始时间
|
||
$diff_time = (time() - $start_time) / 86400;//当前时间距离上次天数
|
||
if ($val['type'] == 1) {
|
||
//日结
|
||
$account_time = $start_time + 86400;
|
||
if ($diff_time < 1) {
|
||
echo 'The statistical time is not reached 4' . PHP_EOL;//不足一天则不生成账单
|
||
continue;
|
||
} else {
|
||
$periods_nub = floor($diff_time);// 循环周期
|
||
}
|
||
} else if ($val['type'] == 2) {
|
||
//周结
|
||
$account_time = $start_time + 86400 * 7;
|
||
if ($diff_time < 7) {
|
||
echo 'The statistical time is not reached 5' . PHP_EOL;
|
||
continue;
|
||
} else {
|
||
$periods_nub = floor($diff_time / 7);// 循环周期
|
||
}
|
||
} else if ($val['type'] == 3) {
|
||
//月结
|
||
$account_time = strtotime("+1 months", $start_time);
|
||
$arr = $this->getMonthNum(date('Y-m-d', $start_time), date('Y-m-d', time()));
|
||
$periods_nub = $arr[1];// 循环周期
|
||
if ($periods_nub < 1) {
|
||
echo 'The statistical time is not reached 6' . PHP_EOL;
|
||
continue;
|
||
}
|
||
}
|
||
$periods = 1;// 期数
|
||
}
|
||
$account_time = round($account_time - 1);//周期统计的结束时间
|
||
// 循环周期生成账单
|
||
for ($i = 0; $i < $periods_nub; $i++) {
|
||
//scene_type 1付款 2退款
|
||
//order_type 1购买商品 2会员充值 4购买礼品卡 5购买付费会员 6小助手快捷收银 7购买次卡
|
||
//pay_type 1微信 2支付宝 3现金 4小程序微信 5其他支付方式 6会员余额
|
||
|
||
/*----------------收入----------------*/
|
||
|
||
//总营业收入(所有的支付方式的营业订单+快捷收单+充值)
|
||
$aMap = array(
|
||
'store_id' => $val['store_id'],
|
||
'trade_date' => array('between', array($start_time, $account_time)),
|
||
'order_state' => 'SUCCESS',
|
||
'scene_type' => 1,
|
||
'order_type' => array('in', '1,2,6'),
|
||
'pay_type' => array('in', '1,2,3,4,5,6'),
|
||
);
|
||
$all_income = $PayOrderModel->getSum($aMap, 'trade_amount');
|
||
|
||
//现金+其他支付的营业/快捷订单收入
|
||
$biMap = array(
|
||
'store_id' => $val['store_id'],
|
||
'trade_date' => array('between', array($start_time, $account_time)),
|
||
'order_state' => 'SUCCESS',
|
||
'scene_type' => 1,
|
||
'order_type' => array('in', '1,6'),
|
||
'pay_type' => array('in', '3,5'),
|
||
);
|
||
$business_income = $PayOrderModel->getSum($biMap, 'trade_amount');
|
||
|
||
//现金+其他支付的充值
|
||
$brMap = array(
|
||
'store_id' => $val['store_id'],
|
||
'trade_date' => array('between', array($start_time, $account_time)),
|
||
'order_state' => 'SUCCESS',
|
||
'scene_type' => 1,
|
||
'order_type' => 2,
|
||
'pay_type' => 3,//目前只能现金充值
|
||
);
|
||
$business_recharge = $PayOrderModel->getSum($brMap, 'trade_amount');
|
||
|
||
//微信+支付宝的营业/快捷订单收入
|
||
$oiMap = array(
|
||
'store_id' => $val['store_id'],
|
||
'trade_date' => array('between', array($start_time, $account_time)),
|
||
'order_state' => 'SUCCESS',
|
||
'scene_type' => 1,
|
||
'order_type' => array('in', '1,6'),
|
||
'pay_type' => array('in', '1,2,4'),
|
||
);
|
||
$online_income = $PayOrderModel->getSum($oiMap, 'trade_amount');
|
||
|
||
//余额支付的营业/快捷订单收入
|
||
$baiMap = array(
|
||
'store_id' => $val['store_id'],
|
||
'trade_date' => array('between', array($start_time, $account_time)),
|
||
'order_state' => 'SUCCESS',
|
||
'scene_type' => 1,
|
||
'order_type' => array('in', '1,6'),
|
||
'pay_type' => 6,
|
||
);
|
||
$balance_income = $PayOrderModel->getSum($baiMap, 'trade_amount');
|
||
|
||
//微信+支付宝的充值
|
||
$orMap = array(
|
||
'store_id' => $val['store_id'],
|
||
'trade_date' => array('between', array($start_time, $account_time)),
|
||
'order_state' => 'SUCCESS',
|
||
'scene_type' => 1,
|
||
'order_type' => 2,
|
||
'pay_type' => array('in', '1,2,4'),
|
||
);
|
||
$online_recharge = $PayOrderModel->getSum($orMap, 'trade_amount');
|
||
|
||
|
||
/*----------------退款----------------*/
|
||
|
||
//现金+其他支付的营业订单/快捷退款
|
||
$ormMap = array(
|
||
'store_id' => $val['store_id'],
|
||
'trade_date' => array('between', array($start_time, $account_time)),
|
||
'order_state' => 'REFUND',
|
||
'scene_type' => 2,
|
||
'order_type' => array('in', '1,6'),
|
||
'pay_type' => array('in', '3,5'),
|
||
);
|
||
$offline_refund_money = $PayOrderModel->getSum($ormMap, 'trade_amount');
|
||
|
||
//现金+其他支付的充值退款
|
||
$orrMap = array(
|
||
'store_id' => $val['store_id'],
|
||
'trade_date' => array('between', array($start_time, $account_time)),
|
||
'order_state' => 'REFUND',
|
||
'scene_type' => 2,
|
||
'order_type' => 2,
|
||
'pay_type' => 3,
|
||
);
|
||
$offline_refund_recharge = $PayOrderModel->getSum($orrMap, 'trade_amount');
|
||
|
||
//微信+支付宝的营业订单/快捷退款
|
||
$ormMap = array(
|
||
'store_id' => $val['store_id'],
|
||
'trade_date' => array('between', array($start_time, $account_time)),
|
||
'order_state' => 'REFUND',
|
||
'scene_type' => 2,
|
||
'order_type' => array('in', '1,6'),
|
||
'pay_type' => array('in', '1,2,4'),
|
||
);
|
||
$online_refund_money = $PayOrderModel->getSum($ormMap, 'trade_amount');
|
||
|
||
//余额支付的营业/快捷退款
|
||
$brmMap = array(
|
||
'store_id' => $val['store_id'],
|
||
'trade_date' => array('between', array($start_time, $account_time)),
|
||
'order_state' => 'REFUND',
|
||
'scene_type' => 2,
|
||
'order_type' => array('in', '1,6'),
|
||
'pay_type' => 6,
|
||
);
|
||
$balance_refund_money = $PayOrderModel->getSum($brmMap, 'trade_amount');
|
||
|
||
//微信+支付宝的充值退款
|
||
$orrMap = array(
|
||
'store_id' => $val['store_id'],
|
||
'trade_date' => array('between', array($start_time, $account_time)),
|
||
'order_state' => 'REFUND',
|
||
'scene_type' => 2,
|
||
'order_type' => 2,
|
||
'pay_type' => array('in', '1,2,4'),
|
||
);
|
||
$online_refund_recharge = $PayOrderModel->getSum($orrMap, 'trade_amount');
|
||
|
||
//门店充值赠送的金额(仅用来记账,不做计算)
|
||
$grMap = array(
|
||
'store_id' => $val['store_id'],
|
||
'trade_date' => array('between', array($start_time, $account_time)),
|
||
'status' => 1,
|
||
'user_type' => array('in', '2,3'),//2店长 3收银员
|
||
'scene_type' => array('in', '1,3,6'),//1收银机 3门店后台 4小程序 5公众号 6小助手
|
||
);
|
||
$give_recharge = $RechargeOrderModel->getSum($grMap, 'give_money');
|
||
|
||
//其他金额:目前仅有分销佣金
|
||
$Api = new \Addons\Api\HandleStoreSettled();
|
||
$other_money = $Api->handlePrice($val['store_id'], $start_time, $account_time);
|
||
|
||
/*-------------------开始计算钱-------------------*/
|
||
$all_online_income = bcadd($online_income, $balance_income, 2);//线上总营收/快捷
|
||
$all_online_refund = bcadd($online_refund_money, $balance_refund_money, 2);//线上总营收/快捷退款
|
||
|
||
//营业/快捷收入手续费(不含现金与其他支付) = rate * [线上总营收/快捷]
|
||
$service_charge = bcmul($val['rate'] / 100, $all_online_income, 2);
|
||
|
||
//营业/快捷退款手续费(不含现金与其他支付) = rate * [线上总营收/快捷退款]
|
||
$refund_service_charge = bcmul($val['rate'] / 100, $all_online_refund, 2);
|
||
|
||
//实际可结算营收金额 = ([线上总营收/快捷] - [营业/快捷收入手续费]) - ([线上总营收/快捷退款] - [营业/快捷退款手续费])
|
||
$actual_income = bcsub(bcsub($all_online_income, $service_charge, 2), bcsub($all_online_refund, $refund_service_charge, 2), 2);
|
||
|
||
//实际可提现/平账金额 = [实际可结算营收金额] - ([现金+其他支付的充值] - [现金+其他支付的充值退款]) - 其他金额
|
||
$money = bcsub(bcsub($actual_income, bcsub($business_recharge, $offline_refund_recharge, 2), 2), $other_money, 2);
|
||
|
||
// 数据保存
|
||
$addItem = array(
|
||
'merchant_id' => $val['merchant_id'],
|
||
'store_id' => $val['store_id'],
|
||
'franchisee_time' => date('Y.m.d H:i:s', $start_time) . '-' . date('Y.m.d H:i:s', $account_time),//'结算日期',
|
||
'periods' => $periods, //'结算周期',
|
||
'add_time' => time(), //'创建时间',
|
||
'rate' => $val['rate'], //'费率',
|
||
'type' => $val['type'], //'计算周期:1日结 2周结 3月结',
|
||
'version' => 2, //版本类型 1:1.0版本账单 2:2.0版本账单
|
||
'all_income' => $all_income, //'总收入',
|
||
'business_income' => $business_income, //'现金+其他支付的营业订单/快捷收入',
|
||
'business_recharge' => $business_recharge, //'现金+其他支付的充值',
|
||
'online_income' => $online_income, //'微信+支付宝的营业订单/快捷收入',
|
||
'balance_income' => $balance_income, //'余额支付的营业/快捷收入',
|
||
'online_recharge' => $online_recharge, //'微信+支付宝的充值',
|
||
'give_recharge' => $give_recharge, //'所有充值赠送的金额(仅记账使用,不做计算)',
|
||
'offline_refund_money' => $offline_refund_money,//'现金+其他支付的营业订单/快捷退款',
|
||
'offline_refund_recharge' => $offline_refund_recharge,//'现金+其他支付的充值退款',
|
||
'online_refund_money' => $online_refund_money,//'微信+支付宝的营业订单/快捷退款',
|
||
'balance_refund_money' => $balance_refund_money,//'余额支付的营业/快捷退款',
|
||
'online_refund_recharge' => $online_refund_recharge,//'微信+支付宝的充值退款',
|
||
'other_money' => $other_money, //'其他金额(结算需要减少的金额:分销佣金,其他待定)',
|
||
'actual_income' => $actual_income, //'实际可结算营收金额',
|
||
'money' => $money, //'实际可提现/平账金额',
|
||
'status' => $money > 0 ? 1 : ($money < 0 ? 9 : 10),//'审核状态:1待提现 2提现待审核 3提现驳回 4提现通过待贷款 5已打款 6平账待审核 7平账驳回 8已平账 9待平账 10无需操作',
|
||
'service_charge' => $service_charge, //'营业/快捷收入手续费(不含现金与其他支付)',
|
||
'refund_service_charge' => $refund_service_charge,//'营业/快捷退款手续费(不含现金与其他支付)',
|
||
'data_type' => $money > 0 ? 1 : 2, //'结算方式描述:1提现 2平账 ',
|
||
);
|
||
$res = $FranchiseeBillModel->addOrder($addItem);
|
||
|
||
//时间增加,进行下一期账单生成
|
||
if ($val['type'] == 1) {
|
||
$start_time += 86400;
|
||
$account_time += 86400;
|
||
$periods += 1;
|
||
} elseif ($val['type'] == 2) {
|
||
$start_time += 86400 * 7;
|
||
$account_time += 86400 * 7;
|
||
$periods += 1;
|
||
} elseif ($val['type'] == 3) {
|
||
$start_time = strtotime("+1 months", $start_time);
|
||
$account_time = strtotime("+1 months", $account_time);
|
||
$periods += 1;
|
||
}
|
||
}
|
||
}
|
||
// 结束统计时间
|
||
$end = mSecTime();
|
||
echo 'Total Time: ' . (($end - $start) / 1000);
|
||
|
||
}
|
||
|
||
/**
|
||
*计算两个日期的月份差
|
||
*/
|
||
public function getMonthNum($date1, $date2)
|
||
{
|
||
if (strtotime($date1) > strtotime($date2)) {
|
||
$ymd = $date2;
|
||
$date2 = $date1;
|
||
$date1 = $ymd;
|
||
}
|
||
list($y1, $m1, $d1) = explode('-', $date1);
|
||
list($y2, $m2, $d2) = explode('-', $date2);
|
||
$math = ($y2 - $y1) * 12 + $m2 - $m1;
|
||
$y = round($math / 12);
|
||
$m = intval($math % 12);
|
||
$d = (mktime(0, 0, 0, $m2, $d2, $y2) - mktime(0, 0, 0, $m2, $d1, $y2)) / 86400;
|
||
if ($d < 0) {
|
||
$m -= 1;
|
||
$d += date('j', mktime(0, 0, 0, $m2, 0, $y2));
|
||
}
|
||
$m < 0 && $y -= 1;
|
||
return array($y, $m, $d);
|
||
}
|
||
} |