|
<?php
namespace app\controller;
use think\App;
use think\facade\View;
use app\model\Order as OrderModel;
use app\model\Commission as CommissionModel;
use app\model\OrderItem as OrderItemModel;
use app\model\OrderDetails as OrderDetailsModel;
use app\model\Department as DepartmentModel;
use think\facade\Db;
use app\model\Company as CompanyModel;
use app\model\Seller as SellerModel;
use think\facade\Env;
class Commission extends Base
{
protected $model;
protected $orderModel;
protected $itemModel;
protected $detailsModel;
// ; 提成比例 单位千分
// 销售
// 一般
protected $general_ratio;
// 特批提成的三种比例 10, 15, 20
protected $special_a;
protected $special_b;
protected $special_c;
// 特批2提成比例另计
protected $special_d;
protected $special_e;
// 后勤
// 一般
protected $general_logistics;
// 特批
protected $special_logistics;
// 部门
// 一般
protected $general_deparment;
// 特批
protected $special_deparment;
public function __construct(App $app)
{
parent::__construct($app);
$this->model = new CommissionModel();
$this->orderModel = new OrderModel();
$this->itemModel = new OrderItemModel();
$this->detailsModel = new OrderDetailsModel();
$this->general_ratio= Env::get('commission.general_ratio', 250);
$this->special_a= Env::get('commission.special_a', 10);
$this->special_b= Env::get('commission.special_b', 15);
$this->special_c= Env::get('commission.special_c', 20);
$this->special_d= Env::get('commission.special_d', 10);
$this->special_e= Env::get('commission.special_e', 10);
$this->general_logistics= Env::get('commission.general_logistics', 50);
$this->special_logistics= Env::get('commission.special_logistics', 2);
$this->general_deparment= Env::get('commission.general_deparment', 14);
$this->special_deparment= Env::get('commission.special_deparment', 1.4);
}
public function year()
{
$init_year = Env::get('INIT_YEAR');
$current_year = date("Y");
return View::fetch('',[
'init_year' => $init_year,
'current_year' => $current_year,
]);
}
public function index($year=0, $month=0)
{
$year = $year ? : date("Y");
$month = $month ? : date("n"); // 不带前导0的月份
$map = [
['year', '=', $year],
['month', '=', $month]
];
$list = [];
$data = $this->model->where($map)->field('seller_name,seller_id,sum(general_money) as general_money, sum(special_money) as special_money,department_id,remark')
->group('seller_id')->select();
$list = $data->toArray();
foreach ($list as &$value) {
$value['profit'] = intval(bcdiv($value['general_money'] * 1000, $this->general_ratio));
$value['general_ratio'] = bcdiv($this->general_ratio, 1000, 4) * 100 ."%";
$value['total_special_money'] = $value['special_money'];
$value['special_ratio'] = '100%';
}
unset($value); // 避免影响下面的$value
// 后倾
$logistics['seller_name'] = '后勤';
$total_general_money = $this->model->where($map)->sum('general_money');
$total_special_money = $this->model->where($map)->sum('special_money');
$logistics['profit'] = intval(bcdiv($total_general_money * 1000, $this->general_ratio));
$logistics['general_ratio'] = bcdiv($this->general_logistics, 1000, 4) * 100 ."%";
$logistics['general_money'] = intval(bcdiv(bcmul($this->general_logistics, $total_general_money), $this->general_ratio));
$logistics['total_special_money'] = $total_special_money;
$logistics['special_ratio'] = bcdiv($this->special_logistics, 1000, 5) * 100 .'%';
$logistics['special_money'] = intval(bcmul($this->special_logistics, $total_special_money));
$logistics['remark'] = "";
$list[] = $logistics;
// var_dump($list);
// 部门主管提成
$departements = DepartmentModel::select();
foreach ($departements as $value) {
$arr = [];
$arr['seller_name'] = $value->director . '月度' ;
$arr['general_money'] = 0;
$arr['special_money'] = 0;
foreach ($data as $key => $val) {
if ($val->department_id == $value->id) {
$arr['general_money'] += $val->general_money;
$arr['special_money'] += $val->special_money;
}
}
$arr['profit'] = intval(bcdiv($arr['general_money'] * 1000, $this->general_ratio)) ;
$arr['general_ratio'] = bcdiv($this->general_deparment, 1000, 4) * 100 .'%';
$arr['general_money'] = intval(bcdiv(bcmul($this->general_deparment, $arr['general_money']), $this->general_ratio));
$arr['total_special_money'] = $arr['special_money'];
$arr['special_ratio'] = bcdiv($this->special_deparment, 1000, 5) * 100 .'%';
$arr['special_money'] = intval(bcmul($this->special_deparment, $arr['special_money']));
$arr['remark'] = "";
$list[] = $arr;
}
// var_dump($total_general_money);
// var_dump($total_special_money);
// var_dump($list);
// exit;
return View::fetch('',[
'year' => $year,
'month' => $month,
'list' => $list,
'total_general_money' => $total_general_money,
'total_special_money' => $total_special_money,
]);
}
public function lists($year=0, $month=0, $key="")
{
if (!$year || !$month) {
throw new \think\exception\HttpException(404, '页面参数不正确');
}
$map = [
['year', '=', $year],
['month', '=', $month]
];
if ($key) {
$map[] = ['company_name','LIKE', '%'.$key.'%'];
}
$list = $this->model->where($map)->order('id')->select();
$total_general_money = $this->model->where($map)->sum('general_money');
$total_special_money = $this->model->where($map)->sum('special_money');
return View::fetch('',[
'year' => $year,
'month' => $month,
'key' => $key,
'list' => $list,
'total_general_money' =>$total_general_money,
'total_special_money' =>$total_special_money,
]);
}
public function info($id=0)
{
}
public function save()
{
if ($this->request->isPost()) {
$param = $this->request->param();
try {
if ($param['id']) {
$commission = $this->model->find(intval($param['id']));
} else {
$commission = new CommissionModel();
$commission->create_time = time();
}
$commission->order_id = intval($param['order_id']);
$commission->no = $param['no'];
$commission->contract_no = $param['contract_no'];
$commission->company_id = intval($param['company_id']);
$commission->company_name = $param['company_name'];
$commission->seller_id = intval($param['seller_id']);
$commission->seller_name = $param['seller_name'];
$commission->department_id = intval($param['department_id']);
$commission->department_name = $param['department_name'];
$commission->general_money = int_money($param['general_money']);
$commission->special_money = int_money($param['special_money']);
$commission->time = $param['time'];
$commission->remark = $param['remark'];
$commission->year = date('Y',strtotime($param['time']));
$commission->month = date('n',strtotime($param['time']));
$commission->save();
} catch (\Exception $e) {
return json(['code'=>0, 'msg'=>$e->getMessage()]);
}
return json(['code'=>2, 'msg'=>'操作成功']);
}
}
public function delete($id)
{
if ($this->request->isPost()) {
if ($this->model->destroy($id)) {
return json(['code'=>2]);
} else {
return json(['code'=>0]);
}
}
}
public function infoByOrderId($id)
{
$orderInfo = $this->detailsModel->find($id);
$orderItemLists = $this->itemModel->where('order_id', $id)->select();
$commission = new CommissionModel();
$commission->order_id = $orderInfo->id;
$commission->no = $orderInfo->no;
$commission->contract_no = $orderInfo->contract_no;
$commission->company_id = $orderInfo->company_id;
$commission->company_name = $orderInfo->company_name;
$commission->seller_id = $orderInfo->seller_id;
$commission->seller_name = $orderInfo->seller_name;
$commission->department_id = $orderInfo->department_id;
$commission->department_name = $orderInfo->department_name;
// var_dump($orderInfo);
// var_dump($orderItemLists->toArray());
// exit;
foreach ($orderItemLists as $orderItem) {
$order = $this->doCommission($orderItem);
$commission->general_money += $order['general_money'];
$commission->special_money += $order['special_mone'];
}
$commission->general_money = number_money($commission->general_money);
$commission->special_money = number_money($commission->special_money);
return json(['code'=>2, 'data' => $commission]);
}
public function doCommission($orderItem=null)
{
$general_money = 0;
$special_money = 0;
switch ($orderItem->type) {
case 1:
$price_rate = intval(bcdiv($orderItem->actual_price, $orderItem->goods_price, 3)*1000);
if ($price_rate < 1050) {
$ratio = $this->special_a;
} elseif ($price_rate < 1100) {
$ratio = $this->special_b;
} else {
$ratio = $this->special_c;
}
$general_money = 0;
$special_money = intval(bcdiv(($orderItem->actual_money - $orderItem->fare - $orderItem->agency) * $ratio,1000));
break;
case 2:
$price_rate = intval(bcdiv($orderItem->actual_price, $orderItem->goods_price, 3)*1000);
$ratio = intval(bcdiv($price_rate - 1000, 100) * 10 +10);
$general_money = 0;
$special_money = intval(bcdiv(($orderItem->actual_money - $orderItem->fare - $orderItem->agency) * $ratio,1000));
break;
default:
$price_rate = 0;
$ratio = $this->general_ratio;
$base_money = $orderItem->actual_amount * $orderItem->goods_price;
$general_money = intval(bcdiv(($orderItem->actual_money - $base_money - $orderItem->fare - $orderItem->agency) * $ratio, 1000));
$special_money = 0;
break;
}
return [
'general_money' => $general_money,
'special_mone' => $special_money
];
}
}
|