123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299 |
- <?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;
-
- protected $special_a;
- protected $special_b;
- protected $special_c;
-
- 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");
- $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);
-
-
- $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;
-
-
-
- $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;
- }
-
-
-
-
- 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;
-
-
-
- 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
- ];
- }
- }
|