文件
mashangyoujiu/Application/PayApi/Controller/SellDataController.class.php

523 行
23 KiB
PHP
可执行文件

<?php
namespace PayApi\Controller;
use Common\Model\BillOrderViewModel;
use Common\Model\RetailGoodsClassModel;
use Common\Model\RetailOtherPayModel;
use Common\Model\RetailBillOrderModel;
use Common\Model\RetailPayOrderModel;
use Common\Model\RetailRechargeOrderModel;
use Common\Model\RetailSellModel;
use Think\Controller;
//[1.9.20]
/**
* 销售数据获取接口
* Class SellDataController
* @package PayApi\Controller
*/
class SellDataController extends Controller
{
/**
* 获取支付总额数据
* @param $start_time 开始时间
* @param $now_time 结束时间
* @param $store_id 门店id
* @param $cashier_id 收银员id
* @return mixed
*/
public function getPayData_v1($start_time, $now_time, $store_id, $cashier_id)
{
if (!$start_time || !$now_time || !$store_id || !$cashier_id) {
return false;
}
//查询购买商品的订单
$BillModel = new RetailBillOrderModel();
$map = [
'cashier_id' => $cashier_id,
'pay_state' => 1,
'type' => 2,
'add_time' => ['between', [$start_time, $now_time]],
[
['order_type' => 1],
['order_type' => 2, 'refund_status' => 2],
'_logic' => 'OR',
]
];
$fields = [
'id',
'order_type',
'pay_type',
'all_money',
'refund_money',
];
// $t_1 = mSecTime();
$billList = $BillModel->getSelect($map, $fields);
$result['sql_bill_order'] = M()->_sql();
// $t_2 = mSecTime();
// $result['sql_time_bill_order'] = ($t_2 - $t_1) / 1000;
$t_1 = mSecTime();
$cash_amount = 0; //现金支付金额
$weChat_amount = 0; //微信支付金额
$ali_amount = 0; //支付宝支付金额
$vip_amount = 0; //会员卡支付金额
$MoralEducationCoin = 0; //德育币支付金额
$other_amount = 0; //其他支付金额
$otherTotalList = []; //其他支付详情列表
$billID = [];
//处理购买商品的订单
foreach ($billList as $value) {
$money = $value['order_type'] == 1 ? $value['all_money'] : (-$value['all_money']);
switch ($value['pay_type']) {
case '1':
//现金
$cash_amount += $money;
break;
case '3':
//微信
$weChat_amount += $money;
break;
case '4':
//支付宝
$ali_amount += $money;
break;
case '5':
//会员余额
$vip_amount += $money;
break;
case '7':
case '8':
//7其他 8组合
$billID[] = $value['id'];
break;
case '9':
//德育币
$MoralEducationCoin += $money;
break;
}
}
// $t_2 = mSecTime();
// $result['sql_time_bill_order_for'] = ($t_2 - $t_1) / 1000;
//查询购买商品组合、其他的订单和快速收款、出售次卡的支付流水记录
$PayOrderModel = new RetailPayOrderModel();
if (!empty($billID)) {
$new_map = [
[
[
'cashier_id' => $cashier_id,
[
['order_type' => ['in', [6, 7]]],
['order_type' => 9, 'bill_id' => ['not in', $billID]]
],
'add_time' => ['between', [$start_time, $now_time]],
],
'bill_id' => ['in', $billID],
'_logic' => 'OR',
],
[
['order_state' => 'SUCCESS', ['scene_type' => 1]],
['order_state' => 'REFUND', ['scene_type' => 2]],
'_logic' => 'OR',
],
];
} else {
$new_map = [
'cashier_id' => $cashier_id,
'order_type' => ['in', [6, 7, 9]],
'trade_date' => ['between', [$start_time, $now_time]],
[
['order_state' => 'SUCCESS', ['scene_type' => 1]],
['order_state' => 'REFUND', ['scene_type' => 2]],
'_logic' => 'OR',
],
];
}
$fields = [
'id',
'scene_type',
'pay_type',
'trade_amount',
'refund_money',
'other_pay_id',
];
// $t_1 = mSecTime();
$payList = $PayOrderModel->getSelect($new_map, $fields);
if (!empty($payList)) {
$OtherPayModel = new RetailOtherPayModel();
foreach ($payList as $val) {
$money = $val['scene_type'] == 1 ? $val['trade_amount'] : (-$val['trade_amount']);
switch ($val['pay_type']) {
case '1':
$weChat_amount += $money;
break;
case '2':
$ali_amount += $money;
break;
case '3':
$cash_amount += $money;
// var_dump('pay_order:' . $val['id'] . ' ' . $money);
break;
case '5':
$other_amount += $money;
if (!$otherTotalList[$val['other_pay_id']]) {
$otherTotalList[$val['other_pay_id']] = [
'id' => $val['other_pay_id'],
'name' => $val['other_pay_id'] == 0 ? '其他方式' : ($OtherPayModel->getFindField(['id' => $val['other_pay_id']], 'name')),
'money' => $money,
];
} else {
$otherTotalList[$val['other_pay_id']]['money'] = round($otherTotalList[$val['other_pay_id']]['money'] + $money, 2);
}
break;
case '6':
$vip_amount += $money;
break;
case '8':
//8组合
$MoralEducationCoin += $money;
break;
}
}
sort($otherTotalList);
// $t_2 = mSecTime();
// $result['sql_time_pay_order'] = ($t_2 - $t_1) / 1000;
}
// $t_2 = mSecTime();
// $result['sql_time_other_pay_sum'] = ($t_2 - $t_1) / 1000;
$RechargeOrderModel = new RetailRechargeOrderModel();
//获取在班时间充值金额
$rMap = [
'store_id' => $store_id,
'status' => 1,
'user_type' => 3,
'cashier_id' => $cashier_id,
'scene_type' => 1,
'type' => 1,
'add_time' => ['between', [$start_time, $now_time]],
'state' => 1,
'pay_type' => 1, //1现金 2微信 3支付宝
];
//根据条件获取退款金额
$map = [
'store_id' => $store_id,
'status' => 1,
'user_type' => 3,
'cashier_id' => $cashier_id,
'type' => 9,
'add_time' => ['between', [$start_time, $now_time]],
'state' => 1,
'pay_type' => 1, //1现金 2微信 3支付宝
];
// $t_1 = mSecTime();
$recharge_cash = $RechargeOrderModel->getSum($rMap, 'money');
$refund_recharge_cash = $RechargeOrderModel->getSum($map, 'money');
// $t_2 = mSecTime();
// $result['sql_time_recharge_cash'] = ($t_2 - $t_1) / 1000;
$rMap['pay_type'] = 2; //微信充值
$map['pay_type'] = 2; //微信充值
// $t_1 = mSecTime();
$recharge_wx = $RechargeOrderModel->getSum($rMap, 'money');
$refund_recharge_wx = $RechargeOrderModel->getSum($map, 'money');
// $t_2 = mSecTime();
// $result['sql_time_recharge_wx'] = ($t_2 - $t_1) / 1000;
$rMap['pay_type'] = 3; //支付宝充值
$map['pay_type'] = 3; //支付宝充值
// $t_1 = mSecTime();
$recharge_zfb = $RechargeOrderModel->getSum($rMap, 'money');
$refund_recharge_zfb = $RechargeOrderModel->getSum($map, 'money');
// $t_2 = mSecTime();
// $result['sql_time_recharge_al'] = ($t_2 - $t_1) / 1000;
$recharge_cash_amount = round(($recharge_cash - $refund_recharge_cash), 2); //会员充值现金
$recharge_weChat_amount = round(($recharge_wx - $refund_recharge_wx), 2); //会员充值微信
$recharge_ali_amount = round(($recharge_zfb - $refund_recharge_zfb), 2); //会员充值支付宝
$rMap['pay_type'] = ['in', '1,2,3'];
// $t_1 = mSecTime();
$recharge_give_amount = $RechargeOrderModel->getSum($rMap, 'give_money');
unset($map['pay_type']);
$recharge_refund_give_amount = $RechargeOrderModel->getSum($map, 'give_money');
$recharge_give_amount = round($recharge_give_amount - $recharge_refund_give_amount, 2);
// $t_2 = mSecTime();
// $result['sql_time_recharge_all'] = ($t_2 - $t_1) / 1000;
$result['all_amount'] = round($cash_amount + $weChat_amount + $ali_amount + $vip_amount + $other_amount + $MoralEducationCoin, 2); //营业总额(不含充值)
$result['cash_amount'] = round($cash_amount, 2); //现金支付金额
$result['weChat_amount'] = round($weChat_amount, 2); //微信支付金额
$result['ali_amount'] = round($ali_amount, 2); //支付宝支付金额
$result['vip_amount'] = round($vip_amount, 2); //会员卡支付金额
$result['MoralEducationCoin'] = round($MoralEducationCoin, 2);//德育币支付金额
$result['other_amount'] = round($other_amount, 2); //其他支付总金额
$result['other_amount_array'] = $otherTotalList; //其他支付金额详情
$result['recharge_all_amount'] = round($recharge_cash_amount + $recharge_weChat_amount + $recharge_ali_amount, 2);//总充值金额
$result['recharge_cash_amount'] = $recharge_cash_amount; //现金充值金额
$result['recharge_weChat_amount'] = $recharge_weChat_amount; //微信充值金额
$result['recharge_ali_amount'] = $recharge_ali_amount; //支付宝充值金额
$result['recharge_give_amount'] = $recharge_give_amount; //总赠送金额
return $result;
}
/**
* 获取出售商品数据
* @param $start_time 开始时间
* @param $now_time 结束时间
* @param $store_id 门店id
* @param $cashier_id 收银员id
* @return array
*/
public function getSellData_v1($start_time, $now_time, $store_id, $cashier_id)
{
if (!$start_time || !$now_time || !$store_id || !$cashier_id) {
return false;
}
$SellModel = new RetailSellModel();
$sMap = array(
'pay_type' => ['neq', 0],
'cashier_id' => $cashier_id,
'pay_state' => 1,
'type' => 2,
'sell_type' => 1,
'add_time' => ['between', [$start_time, $now_time]],
);
$field_sell = [
'id',
'goods_id',
'goods_class_id',
'goods_type',
'goods_name',
'all_money',
'refund_money',
'num',
'refund_num',
'pricing_method',
'original_price',
'vip_less_money',
'vip_dis_money',
'integral_dis_money',
'coupon_money',
'cashier_dis_money',
'small_changes_money',
'cashier_manual_discount_money',
];
$t_1 = mSecTime();
$sellList = $SellModel->getSelect($sMap, $field_sell); //获取所有sell数据
$result['sql_getSell'] = M()->_sql();
$t_2 = mSecTime();
$result['sql_time_getGroupSelect'] = ($t_2 - $t_1) / 1000;
$tt_1 = mSecTime();
$goodsList = [];
//计算代金券金额
$dMap = [
'pay_type' => ['neq', 0],
'cashier_id' => $cashier_id,
'pay_state' => 1,
'type' => 2,
'order_type' => 1,
'add_time' => ['between', [$start_time, $now_time]],
];
$BillOrderViewModel = new BillOrderViewModel();
$pay_coupon_money = $BillOrderViewModel->getSum($dMap, 'pay_coupon_fee');
$dMap['order_type'] = 2;
$refund_pay_coupon_money = $BillOrderViewModel->getSum($dMap, 'pay_coupon_fee');
$sumSellPrice = array(
'vip_less_money' => 0, //会员价优惠
'vip_dis_money' => 0, //会员等级打折
'integral_money' => 0, //积分抵扣
'coupon_money' => 0, //优惠券减少
'cashier_dis_money' => 0, //收银员整单打折
'small_changes_money' => 0, //收银员抹零
'before_money' => 0, //应收总金额
'all_money' => 0, //实收总金额
'cashier_manual_money' => 0, //收银员单品打折
'pay_coupon_money' => round($pay_coupon_money - $refund_pay_coupon_money, 2),//代金券金额
);
if (!empty($sellList)) {
$tempObjArr = array();
/*按分类分组*/
foreach ($sellList as &$value) {
$key = $value['goods_class_id'];
if ($key !== 0 && !$key) {
$key = -1;
}
if ($value['refund_num'] == 0) {
$sumSellPrice['vip_less_money'] += $value['vip_less_money'];
$sumSellPrice['vip_dis_money'] += $value['vip_dis_money'];
$sumSellPrice['integral_money'] += $value['integral_dis_money'];
$sumSellPrice['coupon_money'] += $value['coupon_money'];
$sumSellPrice['cashier_dis_money'] += $value['cashier_dis_money'];
$sumSellPrice['cashier_manual_money'] += $value['cashier_manual_discount_money'];
$sumSellPrice['small_changes_money'] += $value['small_changes_money'];
} else if ($value['num'] > $value['refund_num']) {
$rate = $value['refund_num'] / $value['num'];
$sumSellPrice['vip_less_money'] = round($value['vip_less_money'] - floor($value['vip_less_money'] * 100 * $rate) / 100, 2);
$sumSellPrice['vip_dis_money'] = round($value['vip_dis_money'] - floor($value['vip_dis_money'] * 100 * $rate) / 100, 2);
$sumSellPrice['integral_money'] = round($value['integral_dis_money'] - floor($value['integral_dis_money'] * 100 * $rate) / 100, 2);
$sumSellPrice['coupon_money'] = round($value['coupon_money'] - floor($value['coupon_money'] * 100 * $rate) / 100, 2);
$sumSellPrice['cashier_dis_money'] = round($value['cashier_dis_money'] - floor($value['cashier_dis_money'] * 100 * $rate) / 100, 2);
$sumSellPrice['cashier_manual_money'] += round($value['cashier_manual_discount_money'] - floor($value['cashier_manual_discount_money'] * 100 * $rate) / 100, 2);
$sumSellPrice['small_changes_money'] = round($value['small_changes_money'] - floor($value['small_changes_money'] * 100 * $rate) / 100, 2);
}
$value['num'] = round($value['num'] - $value['refund_num'], 3);
$value['before_money'] = round($value['num'] * $value['original_price'], 2); //原应收总价
$value['all_money'] = round($value['all_money'] - $value['refund_money'], 2);
$sumSellPrice['before_money'] += $value['before_money'];
$sumSellPrice['all_money'] += $value['all_money'];
if ($value['num'] > 0) {
if (!empty($tempObjArr[$key])) {
if (in_array($value['goods_id'], $tempObjArr[$key]['goods_id'])) {
$index1 = array_search($value['goods_id'], $tempObjArr[$key]['goods_id']); //商品所在分类下的商品下标
$tempObjArr[$key]['goods'][$index1]['num'] += $value['num'];
$tempObjArr[$key]['goods'][$index1]['before_money'] += $value['before_money']; //原应收总价
$tempObjArr[$key]['goods'][$index1]['all_money'] += $value['all_money'];
} else {
$tempObjArr[$key]['goods_id'][] = $value['goods_id'];
$tempObjArr[$key]['goods'][] = $value;
}
} else {
$tempObjArr[$key]['goods_id'][] = $value['goods_id'];
$tempObjArr[$key]['goods'][] = $value;
}
}
}
$GoodsClassModel = new RetailGoodsClassModel();
foreach ($tempObjArr as $k => &$val) {
if ($k == -1) {
$val['class_name'] = '未知分类';
} else if (!$k) {
$val['class_name'] = '无码商品';
} else {
$t_1 = mSecTime();
$val['class_name'] = $GoodsClassModel->getFindField(array($GoodsClassModel::ID => $k), $GoodsClassModel::CLASS_NAME);
$t_2 = mSecTime();
$val['sql_time_class_name'] = ($t_2 - $t_1) / 1000;
}
// unset($val['goods_id']);
$goodsList[] = $val;
}
}
$tt_2 = mSecTime();
$val['sql_time_foreach_sell'] = ($tt_2 - $tt_1) / 1000;
$result['goodsList'] = $goodsList;
$sumSellPrice['vip_less_money'] = round($sumSellPrice['vip_less_money'], 2);
$sumSellPrice['vip_dis_money'] = round($sumSellPrice['vip_dis_money'], 2);
$sumSellPrice['integral_money'] = round($sumSellPrice['integral_money'], 2);
$sumSellPrice['coupon_money'] = round($sumSellPrice['coupon_money'], 2);
$sumSellPrice['cashier_dis_money'] = round($sumSellPrice['cashier_dis_money'], 2);
$sumSellPrice['cashier_manual_money'] = round($sumSellPrice['cashier_manual_money'], 2);
$sumSellPrice['small_changes_money'] = round($sumSellPrice['small_changes_money'], 2);
$sumSellPrice['before_money'] = round($sumSellPrice['before_money'], 2);
$sumSellPrice['all_money'] = round($sumSellPrice['all_money'], 2);
$result['sumSellPrice'] = $sumSellPrice;
return $result;
}
/**
* 组合支付支付数据统计
* @param $where 订单条件
* @param $type 查询类型 1 付款 2退款
* @return mixed
*/
public function combinationPayment($where, $type)
{
$BillModel = new RetailBillOrderModel();
$PayOrderModel = new RetailPayOrderModel();
if ($type == 1) {
$where[$BillModel::ORDER_TYPE] = 1;
} else if ($type == 2) {
$where[$BillModel::ORDER_TYPE] = 2;
}
$where[$BillModel::PAY_TYPE] = 8;
$combination_bill_ids = $BillModel->getFindField($where, $BillModel::ID, true);
$combination_xj = 0;
$combination_wxpay = 0;
$combination_zfb = 0;
$combination_membermoney = 0;
$combination_xcxwx = 0;
$combination_other_pay = 0;
$combination_xj_num = 0;
$combination_wxpay_num = 0;
$combination_zfb_num = 0;
$combination_membermoney_num = 0;
$combination_xcxwx_num = 0;
$combination_other_pay_num = 0;
$combination_mec_pay = 0;
$combination_mec_num = 0;
if ($combination_bill_ids) {
$map[$PayOrderModel::BILL_ID] = array('in', $combination_bill_ids);
if ($type == 1) {
$map[$PayOrderModel::ORDER_STATE] = 'SUCCESS';
} else if ($type == 2) {
$map[$PayOrderModel::ORDER_STATE] = 'REFUND';
}
$filed = $PayOrderModel::BILL_ID . "," . $PayOrderModel::PAY_TYPE . "," . $PayOrderModel::TRADE_AMOUNT;
$payOrderInfo = $PayOrderModel->getSelect($map, $filed);
if ($payOrderInfo) {
foreach ($payOrderInfo as $key => &$val) {
switch ($val[$PayOrderModel::PAY_TYPE]) {
case '1':
$combination_wxpay += $val[$PayOrderModel::TRADE_AMOUNT];
$combination_wxpay_num += 1;
break;
case '2':
$combination_zfb += $val[$PayOrderModel::TRADE_AMOUNT];
$combination_zfb_num += 1;
break;
case '3':
$combination_xj += $val[$PayOrderModel::TRADE_AMOUNT];
$combination_xj_num += 1;
break;
case '4':
$combination_xcxwx += $val[$PayOrderModel::TRADE_AMOUNT];
$combination_xcxwx_num += 1;
break;
case '5':
$combination_other_pay += $val[$PayOrderModel::TRADE_AMOUNT];
$combination_other_pay_num += 1;
break;
case '6':
$combination_membermoney += $val[$PayOrderModel::TRADE_AMOUNT];
$combination_membermoney_num += 1;
break;
case '8':
$combination_mec_pay += $val[$PayOrderModel::TRADE_AMOUNT];
$combination_mec_num += 1;
break;
}
}
}
}
$data['combination_xj'] = $combination_xj; //现金
$data['combination_wxpay'] = $combination_wxpay; //微信
$data['combination_zfb'] = $combination_zfb; //支付宝
$data['combination_membermoney'] = $combination_membermoney; //会员支付
$data['combination_xcxwx'] = $combination_xcxwx; //小程序微信
$data['combination_other_pay'] = $combination_other_pay; //其他支付
$data['combination_wxpay_num'] = $combination_wxpay_num; //微信笔数
$data['combination_zfb_num'] = $combination_zfb_num; //支付宝笔数
$data['combination_xj_num'] = $combination_xj_num; //现金笔数
$data['combination_xcxwx_num'] = $combination_xcxwx_num; //小程序微信笔数
$data['combination_other_pay_num'] = $combination_other_pay_num; //其他笔数
$data['combination_membermoney_num'] = $combination_membermoney_num; //会员支付笔数
$data['combination_mec_pay'] = $combination_mec_pay; //德育币支付金额
$data['combination_mec_num'] = $combination_mec_num; //德育币支付笔数
return $data;
}
}