|
<?php
namespace app\index\controller;
use daswork\Controller;
use daswork\lib\Config;
use app\index\model\MonthPicker;
use app\index\model\Commission as CommissionModel;
use app\index\model\Company;
class Commission extends Controller
{
public $maonth_picker;
public $model;
protected $params;
public function __construct()
{
parent::__construct();
$this->maonth_picker = new MonthPicker();
$this->model = new CommissionModel();
$this->params = escapeString($_POST);
}
public function index()
{
$slect_time = isset($_GET['select_time']) ? $_GET['select_time'] : '';
if ($slect_time != '') {
$this->maonth_picker->cur_month = $slect_time;
}
$this->assign('maonth_picker', $this->maonth_picker);
$c_type = isset($_GET['c_type']) ? $_GET['c_type'] : '';
if ($c_type != '') {
$c_type = intval($c_type);
}
$this->assign('c_type', $c_type);
$company = new Company();
$data_company = $company->listByName();
// var_dump($data_company);
$this->assign('data_company', $data_company);
$where = " where month='". $this->maonth_picker->cur_month . "' ";
if ($c_type !=='') {
$where .= " and type=$c_type ";
}
$where .= " order by id desc";
$data = $this->model->list($where);
array_walk($data, array($this,"handleData"));
// var_dump($data);
// exit;
$this->assign('data', $data);
$this->fetch();
}
public function handleData(&$value)
{
switch ($value['type']) {
case 1:
$value['price_rate'] = round($value['price'] / $value['base_price'], 4);
if ($value['price_rate'] < 1.05) {
$value['c_rate'] = 0.01;
} elseif ($value['price_rate'] < 1.1) {
$value['c_rate'] = 0.015;
} else {
$value['c_rate'] = 0.02;
}
$value['actual_value'] = $value['contract_value'] - $value['agency'];
$value['c_money'] = ($value['actual_value'] - $value['shipping']) * $value['c_rate'];
break;
case 2:
$value['price_rate'] = round($value['price'] / $value['base_price'], 4);
$value['c_rate'] = floor(((($value['price_rate'] - 1) / 0.1) * 0.01 + 0.01) * 100) / 100;
$value['actual_value'] = $value['contract_value'] - $value['agency'];
$value['c_money'] = ($value['actual_value'] - $value['shipping']) * $value['c_rate'];
break;
default:
$value['price_rate'] = 0;
$value['c_rate'] = 0;
$value['base_value'] = $value['amount'] * $value['base_price'];
$value['total_profit'] = $value['contract_value'] - $value['agency'] - $value['base_value'] - $value['shipping'];
break;
}
$value['actual_money'] = $value['receive_money'] + $value['period_balance'];
$value['current_balance'] = $value['actual_money'] - $value['contract_value'];
}
public function info()
{
$id = isset($_GET['id']) ? $_GET['id'] : '';
if ($id) {
$data = $this->model->getOneById($id);
$this->handleData($data);
echo json_encode(['code'=>0, 'data'=>$data]);
} else {
echo json_encode(['code'=>1, 'msg'=>'id 不可为空']);
}
}
public function perbalance()
{
$company = $this->params['company'];
// var_dump($company);
// exit;
$sql = "select sum(receive_money - contract_value) as pb from commission where company='$company';" ;
$sqlinitbalance = "select initial_balance from company where name='$company';";
$result = $this->model->query($sql);
$data = $result->fetchArray(SQLITE3_ASSOC);
$result2 = $this->model->query($sqlinitbalance);
$data2 = $result2->fetchArray(SQLITE3_ASSOC);
$perbalance = $data['pb'];
$initbalance = $data2['initial_balance'];
echo json_encode(['code'=>0, 'data'=>$perbalance + $initbalance]);
}
public function save()
{
$id = $this->params['id'];
$data = [
'id' => $id,
'month' => $this->params['month'],
'company' => $this->params['company'],
'contract_no' => $this->params['contract_no'],
'goods_category' => $this->params['goods_category'],
'goods_type' => $this->params['goods_type'],
'amount' => $this->params['amount'],
'price' => $this->params['price'],
'contract_value' => $this->params['contract_value'],
'receive_money' => $this->params['receive_money'],
'period_balance' => $this->params['period_balance'],
'agency' => $this->params['agency'],
'base_price' => $this->params['base_price'],
'shipping' => $this->params['shipping'],
'project' => $this->params['project'],
'type' => $this->params['type'],
'seller' => $this->params['seller'],
'remark' => $this->params['remark'],
'former_id' => $this->params['former_id']
];
if ($id!=0) {
$res = $this->model->updateById($data);
} else {
unset($data['id']);
$res = $this->model->save($data);
}
if ($res===false) {
echo json_encode(['code' => 1, "msg"=>$this->model->lastErrorMsg()]);
} else {
echo json_encode(['code' => 0, "msg"=>"操作成功"]);
}
}
public function delete()
{
$params = escapeString($_POST);
$id = $params['id'];
$res = $this->model->deleteById($id);
if ($res===false) {
echo json_encode(['code' => 1, "msg"=>$this->model->lastErrorMsg()]);
} else {
echo json_encode(['code' => 0, "msg"=>"操作成功"]);
}
}
}
|