You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
296 lines
12 KiB
296 lines
12 KiB
10 months ago
|
<?php
|
||
|
|
||
|
// +----------------------------------------------------------------------
|
||
|
// | 天诚科技 [ 刘海东 17600099397赋能开发者,助力企业发展 ]
|
||
|
// +----------------------------------------------------------------------
|
||
|
// | Copyright (c) 2016~2020 https://www.tczxkj.com All rights reserved.
|
||
|
// +----------------------------------------------------------------------
|
||
|
// | Licensed 该系统并不是自由软件,未经许可不能去掉相关版权
|
||
|
// +----------------------------------------------------------------------
|
||
|
// | Author:甘肃天诚志信电子商务有限公司 刘海东 联系电话维系17600099397
|
||
|
// +----------------------------------------------------------------------
|
||
|
|
||
|
namespace app\admin\model\institution;
|
||
|
|
||
|
|
||
|
use traits\ModelTrait;
|
||
|
use basic\ModelBasic;
|
||
|
use service\PhpSpreadsheetService;
|
||
|
use app\admin\model\merchant\Merchant;
|
||
|
use app\admin\model\special\AgentUser;
|
||
|
use app\admin\model\special\Lecturer;
|
||
|
use app\admin\model\order\StoreOrder;
|
||
|
use app\admin\model\order\DataDownloadOrder;
|
||
|
use app\admin\model\ump\EventSignUp;
|
||
|
|
||
|
/**
|
||
|
* Class InstitutionFlowingWater
|
||
|
* @package app\admin\model\institution
|
||
|
*/
|
||
|
class InstitutionFlowingWater extends ModelBasic
|
||
|
{
|
||
|
use ModelTrait;
|
||
|
|
||
|
/**条件处理
|
||
|
* @param $where
|
||
|
* @return mixed
|
||
|
*/
|
||
|
public static function setWhereList($where)
|
||
|
{
|
||
|
$time['data'] = '';
|
||
|
if ($where['start_time'] != '' && $where['end_time'] != '') {
|
||
|
$time['data'] = $where['start_time'] . ' - ' . $where['end_time'];
|
||
|
}
|
||
|
$model = self::getModelTime($time, self::alias('f')->join('Merchant m', 'm.id=f.mer_id')->order('f.add_time desc'), 'f.add_time');
|
||
|
if (isset($where['mer_id']) && $where['mer_id']) $model = $model->where('f.mer_id', $where['mer_id']);
|
||
|
if (isset($where['nickname']) && $where['nickname'] != '') {
|
||
|
$model = $model->where('m.mer_name|m.real_name', 'like', "%$where[nickname]%");
|
||
|
}
|
||
|
$model = $model->where('f.price', '<>', 0);
|
||
|
return $model->field(['f.*', 'FROM_UNIXTIME(f.add_time,"%Y-%m-%d %H:%i:%s") as add_time', 'm.mer_name']);
|
||
|
}
|
||
|
|
||
|
/**机构流水
|
||
|
* @param $where
|
||
|
* @return array
|
||
|
*/
|
||
|
public static function getBillList($where)
|
||
|
{
|
||
|
$data = ($data = self::setWhereList($where)->page((int)$where['page'], (int)$where['limit'])->select()) && count($data) ? $data->toArray() : [];
|
||
|
foreach ($data as &$v) {
|
||
|
$v['type'] = self::get_order_type($v['type']);
|
||
|
}
|
||
|
$count = self::setWhereList($where)->count();
|
||
|
return compact('data', 'count');
|
||
|
}
|
||
|
|
||
|
/**流水导出
|
||
|
* @param $where
|
||
|
*/
|
||
|
public static function SaveExport($where)
|
||
|
{
|
||
|
$data = ($data = self::setWhereList($where)->select()) && count($data) ? $data->toArray() : [];
|
||
|
$export = [];
|
||
|
foreach ($data as $value) {
|
||
|
$type = self::get_order_type($value['type']);
|
||
|
$export[] = [
|
||
|
$value['mer_id'],
|
||
|
$value['mer_name'],
|
||
|
$type,
|
||
|
$value['total_price'],
|
||
|
$value['pay_price'],
|
||
|
$value['refund_price'],
|
||
|
$value['status'] == 0 ? '-' . $value['price'] : $value['price'],
|
||
|
$value['add_time'],
|
||
|
];
|
||
|
}
|
||
|
$filename = '分成记录' . time() . '.xlsx';
|
||
|
$head = ['机构ID', '机构昵称', '订单类型', '订单总价', '实际金额', '退款金额', '分成/退还', '创建时间'];
|
||
|
PhpSpreadsheetService::outdata($filename, $export, $head);
|
||
|
}
|
||
|
|
||
|
/**订单类型
|
||
|
* @param $type
|
||
|
* @return string
|
||
|
*/
|
||
|
public static function get_order_type($type)
|
||
|
{
|
||
|
switch ($type) {
|
||
|
case 0:
|
||
|
$title = '课程订单';
|
||
|
break;
|
||
|
case 1:
|
||
|
$title = '会员订单';
|
||
|
break;
|
||
|
case 2:
|
||
|
$title = '商品订单';
|
||
|
break;
|
||
|
case 3:
|
||
|
$title = '资料订单';
|
||
|
break;
|
||
|
case 4:
|
||
|
$title = '报名订单';
|
||
|
break;
|
||
|
case 5:
|
||
|
$title = '考试订单';
|
||
|
break;
|
||
|
}
|
||
|
return $title;
|
||
|
}
|
||
|
|
||
|
/**订单退款
|
||
|
* @param $oid
|
||
|
*/
|
||
|
public static function orderRefund($oid, $mer_id, $type = 0)
|
||
|
{
|
||
|
if (!$oid) return true;
|
||
|
$mer_divide = Merchant::where('id', $mer_id)->field('now_money,mer_special_divide,mer_store_divide,mer_event_divide,mer_data_divide')->find();//机构分成
|
||
|
$ins_id = Lecturer::where('mer_id', $mer_id)->value('agent');
|
||
|
$water = self::where(['mer_id' => $mer_id, 'oid' => $oid, 'status' => 1])->find();
|
||
|
if (!$water) return true;
|
||
|
switch ($type) {
|
||
|
case 0://课程订单
|
||
|
$divide = bcdiv($mer_divide['mer_special_divide'], 100, 2);//百分比
|
||
|
if ($divide <= 0) return true;
|
||
|
$order = StoreOrder::where(['id' => $oid])->find();
|
||
|
$price = bcmul($order['refund_price'], $divide, 2);
|
||
|
break;
|
||
|
case 2://商品订单
|
||
|
$divide = bcdiv($mer_divide['mer_store_divide'], 100, 2);//百分比
|
||
|
if ($divide <= 0) return true;
|
||
|
$order = StoreOrder::where(['id' => $oid])->find();
|
||
|
$price = bcmul($order['total_price'], $divide, 2);
|
||
|
$price = bcadd($price, $order['total_postage'], 2);
|
||
|
break;
|
||
|
case 3://资料订单
|
||
|
$divide = bcdiv($mer_divide['mer_data_divide'], 100, 2);//百分比
|
||
|
if ($divide <= 0) return true;
|
||
|
$order = DataDownloadOrder::where(['id' => $oid])->find();
|
||
|
$price = bcmul($order['refund_price'], $divide, 2);
|
||
|
break;
|
||
|
case 4://报名订单
|
||
|
$divide = bcdiv($mer_divide['mer_event_divide'], 100, 2);//百分比
|
||
|
if ($divide <= 0) return true;
|
||
|
$order = EventSignUp::where(['id' => $oid])->find();
|
||
|
$price = bcmul($order['refund_price'], $divide, 2);
|
||
|
|
||
|
break;
|
||
|
}
|
||
|
if ($price <= 0) return true;
|
||
|
Merchant::beginTrans();
|
||
|
$res = Merchant::decMerchantNowMoney($order['mer_id'], $price);
|
||
|
$data['total_price'] = $water['total_price'];
|
||
|
$data['pay_price'] = $water['pay_price'];
|
||
|
$data['refund_price'] = $order['refund_price'];
|
||
|
$data['price'] = $price;
|
||
|
$data['oid'] = $oid;
|
||
|
$data['mer_id'] = $order['mer_id'];
|
||
|
$data['type'] = $type;
|
||
|
$data['add_time'] = time();
|
||
|
$res1 = self::set($data);
|
||
|
if($order['agent_id']){
|
||
|
self::orderRefundAgent($oid,$order,$type);
|
||
|
}
|
||
|
if($ins_id){
|
||
|
self::orderRefundInstitution($oid,$order,$type,$ins_id);
|
||
|
}
|
||
|
if ($res && $res1) {
|
||
|
MerchantBill::expend('订单退款', $oid, $order['mer_id'], 'now_money', 'user_refund', $price, bcsub($mer_divide['now_money'], $price, 2), '订单退款' . floatval($data['pay_price']) . '元');
|
||
|
Merchant::commitTrans();
|
||
|
return true;
|
||
|
} else {
|
||
|
Merchant::rollbackTrans();
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// 退机构佣金
|
||
|
public static function orderRefundAgent($oid,$order,$type){
|
||
|
$mer_divide = AgentUser::where('id', $order['agent_id'])->field('now_money,mer_special_divide,mer_store_divide,mer_event_divide,mer_data_divide')->find();//机构分成
|
||
|
$water = self::where(['mer_id' => $order['agent_id'], 'oid' => $oid, 'status' => 1])->find();
|
||
|
|
||
|
switch ($type) {
|
||
|
case 0://课程订单
|
||
|
$divide = bcdiv($mer_divide['mer_special_divide'], 100, 2);//百分比
|
||
|
if ($divide <= 0) return true;
|
||
|
$order = StoreOrder::where(['id' => $oid])->find();
|
||
|
$price = bcmul($order['refund_price'], $divide, 2);
|
||
|
break;
|
||
|
case 2://商品订单
|
||
|
$divide = bcdiv($mer_divide['mer_store_divide'], 100, 2);//百分比
|
||
|
if ($divide <= 0) return true;
|
||
|
$order = StoreOrder::where(['id' => $oid])->find();
|
||
|
$price = bcmul($order['total_price'], $divide, 2);
|
||
|
break;
|
||
|
case 3://资料订单
|
||
|
$divide = bcdiv($mer_divide['mer_data_divide'], 100, 2);//百分比
|
||
|
if ($divide <= 0) return true;
|
||
|
$order = DataDownloadOrder::where(['id' => $oid])->find();
|
||
|
$price = bcmul($order['refund_price'], $divide, 2);
|
||
|
break;
|
||
|
case 4://报名订单
|
||
|
$divide = bcdiv($mer_divide['mer_event_divide'], 100, 2);//百分比
|
||
|
if ($divide <= 0) return true;
|
||
|
$order = EventSignUp::where(['id' => $oid])->find();
|
||
|
$price = bcmul($order['refund_price'], $divide, 2);
|
||
|
|
||
|
break;
|
||
|
}
|
||
|
if ($price <= 0) return true;
|
||
|
AgentUser::beginTrans();
|
||
|
$res = AgentUser::decAgentUserNowMoney($order['agent_id'], $price);
|
||
|
$data['total_price'] = $water['total_price'];
|
||
|
$data['pay_price'] = $water['pay_price'];
|
||
|
$data['refund_price'] = $order['refund_price'];
|
||
|
$data['price'] = $price;
|
||
|
$data['oid'] = $oid;
|
||
|
$data['mer_id'] = $order['agent_id'];
|
||
|
$data['agent_id'] = $order['agent_id'];
|
||
|
$data['type'] = $type;
|
||
|
$data['add_time'] = time();
|
||
|
$res1 = self::set($data);
|
||
|
if ($res && $res1) {
|
||
|
InstitutionBill::expend('订单退款', $oid, $order['agent_id'], 'now_money', 'user_refund', $price, bcsub($mer_divide['now_money'], $price, 2), '订单退款' . floatval($data['pay_price']) . '元');
|
||
|
AgentUser::commitTrans();
|
||
|
return true;
|
||
|
} else {
|
||
|
AgentUser::rollbackTrans();
|
||
|
return false;
|
||
|
}
|
||
|
return true;
|
||
|
}
|
||
|
// 退机构佣金
|
||
|
public static function orderRefundInstitution($oid,$order,$type,$ins_id){
|
||
|
$mer_divide = Merchant::where('id', $ins_id)->field('now_money,mer_special_divide,mer_store_divide,mer_event_divide,mer_data_divide')->find();//机构分成
|
||
|
$water = self::where(['mer_id' => $ins_id, 'oid' => $oid, 'status' => 1])->find();
|
||
|
switch ($type) {
|
||
|
case 0://课程订单
|
||
|
$divide = bcdiv($mer_divide['mer_special_divide'], 100, 2);//百分比
|
||
|
if ($divide <= 0) return true;
|
||
|
$order = StoreOrder::where(['id' => $oid])->find();
|
||
|
$price = bcmul($order['refund_price'], $divide, 2);
|
||
|
break;
|
||
|
case 2://商品订单
|
||
|
$divide = bcdiv($mer_divide['mer_store_divide'], 100, 2);//百分比
|
||
|
if ($divide <= 0) return true;
|
||
|
$order = StoreOrder::where(['id' => $oid])->find();
|
||
|
$price = bcmul($order['total_price'], $divide, 2);
|
||
|
break;
|
||
|
case 3://资料订单
|
||
|
$divide = bcdiv($mer_divide['mer_data_divide'], 100, 2);//百分比
|
||
|
if ($divide <= 0) return true;
|
||
|
$order = DataDownloadOrder::where(['id' => $oid])->find();
|
||
|
$price = bcmul($order['refund_price'], $divide, 2);
|
||
|
break;
|
||
|
case 4://报名订单
|
||
|
$divide = bcdiv($mer_divide['mer_event_divide'], 100, 2);//百分比
|
||
|
if ($divide <= 0) return true;
|
||
|
$order = EventSignUp::where(['id' => $oid])->find();
|
||
|
$price = bcmul($order['refund_price'], $divide, 2);
|
||
|
|
||
|
break;
|
||
|
}
|
||
|
if ($price <= 0) return true;
|
||
|
Merchant::beginTrans();
|
||
|
$res = Merchant::decMerchantNowMoney($ins_id, $price);
|
||
|
$data['total_price'] = $water['total_price'];
|
||
|
$data['pay_price'] = $water['pay_price'];
|
||
|
$data['refund_price'] = $order['refund_price'];
|
||
|
$data['price'] = $price;
|
||
|
$data['oid'] = $oid;
|
||
|
$data['mer_id'] = $ins_id;
|
||
|
$data['type'] = $type;
|
||
|
$data['add_time'] = time();
|
||
|
$res1 = self::set($data);
|
||
|
if ($res && $res1) {
|
||
|
InstitutionBill::expend('订单退款', $oid, $ins_id, 'now_money', 'user_refund', $price, bcsub($mer_divide['now_money'], $price, 2), '订单退款' . floatval($data['pay_price']) . '元');
|
||
|
Merchant::commitTrans();
|
||
|
return true;
|
||
|
} else {
|
||
|
Merchant::rollbackTrans();
|
||
|
return false;
|
||
|
}
|
||
|
return true;
|
||
|
}
|
||
|
}
|