You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

416 lines
14 KiB
PHP

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace Admin\Controller;
use Think\Controller;
/**
* cp/公会对账单
* ThinkController
*/
class StatementWarningController extends AdminController
{
private $statementWarningKey;
private $statementWarningModel;
private $statementWarningInfoModel;
private $statementWarningType=[
1=>'业务收入',
2=>'业务成本',
3=>'毛利',
4=>'营业利润'
];
private $companyBelong=[
0=>'下游内团',
1=>'下游外团',
9=>'上游'
];
private $isWm=[
1=>'是',
2=>'否'
];
//创建
public function _initialize(){
//初始化
$this->statementWarningModel=M("statement_warning","tab_");
$this->statementWarningInfoModel=M("statement_warning_info","tab_");
$this->statementWarningKey=A("StatementWarningSet")->getStatementWarningKey();
parent::_initialize();
}
public function lists()
{
if(!array_key_exists("year",$_REQUEST)){
$this->redirect(ACTION_NAME, array('year' => date('Y',time())));
}
$year = $_REQUEST['year'];
$yearData = $this->statementWarningModel->where("count_year = '{$year}'")->select();
$yearData = $this->setWaringDataShow( $this->setWaringDataCount( $this->resetWaringData($yearData) ) );
$isCan = D("CmdTasks")->isCanAddTask("StatementWarningSet");
$this->assign("is_can",$isCan);
$this->assign("data",$yearData);
$this->getLastUpdate();
$this->getYearList();
$this->display();
}
private function getLastUpdate()
{
$lastUpdate = $this->statementWarningModel->where([
'count_year'=>$_REQUEST['year'],
'count_month'=>date('m',time()),
"name"=>'wm_platm',
"type"=>1
])->field("create_time")->find();
if($lastUpdate){
$lastUpdate = date('Y-m-d H:i:s',$lastUpdate['create_time']);
}else{
$lastUpdate = '未更新';
}
$this->assign('last_update', $lastUpdate);
}
private function resetWaringData($data)
{
$sendData = [];
foreach ($this->statementWarningType as $key => $va) {
$sendData[$key] = [
'name'=>$va,
'list'=>[],
'count'=>[0,0,0,0,0,0,0,0,0,0,0,0,0]
];
if($key == 3){
$sendData[$key]['list']['margin_ratio'] = [0,0,0,0,0,0,0,0,0,0,0,0,0];
}
if($key == 4){
$sendData[$key]['list']['profit_ratio'] = [0,0,0,0,0,0,0,0,0,0,0,0,0];
}
}
foreach ($data as $k => $v) {
$name = $v['name'];
$typelist = &$sendData[$v['type']]['list'];
if( !isset($typelist[$name]) ){
$typelist[$name] = [0,0,0,0,0,0,0,0,0,0,0,0,0];
}
$typelist[$name][$v['count_month']-1] = $v['money'];
}
return $sendData;
}
private function setWaringDataCount($data)
{
//业务成本/收入
for ($type=1; $type < 3; $type++) {
foreach ($data[$type]['list'] as $k => &$item) {
$item[12] = array_sum($item);
for ($i=0; $i < 13; $i++) {
$data[$type]['count'][$i] += $item[$i];
}
}
}
//毛利
$data[3]['list']['费用'][12] = array_sum($data[3]['list']['费用']);
$data[3]['list']['其他收入'][12] = array_sum($data[3]['list']['其他收入']);
for ($i=0; $i < 13; $i++) {
$data[3]['count'][$i] = $data[1]['count'][$i]-$data[2]['count'][$i];
$data[4]['count'][$i] = ($data[3]['count'][$i]-$data[3]['list']['费用'][$i]+$data[3]['list']['其他收入'][$i]);
if($data[1]['count'][$i] != 0){
$data[3]['list']['margin_ratio'][$i] = round( $data[3]['count'][$i]/$data[1]['count'][$i],4);
$data[4]['list']['profit_ratio'][$i] = round($data[4]['count'][$i]/$data[1]['count'][$i],4);
}
}
return $data;
}
private function setWaringDataShow($data)
{
foreach ($data as $type => $item) {
for ($i=0; $i < 13; $i++) {
$data[$type]['count'][$i] = round($data[$type]['count'][$i]/10000,2);
}
foreach ($item['list'] as $name => $value) {
for ($i=0; $i < 13; $i++) {
if( in_array($name,['margin_ratio','profit_ratio']) ){
$value[$i] = ( ($value[$i]*100)."%");
}else{
$value[$i] = round($value[$i]/10000,2);
}
}
unset($data[$type]['list'][$name]);
if(isset($this->statementWarningKey[$name])){
$data[$type]['list'][$name]['name'] = $this->statementWarningKey[$name];
$data[$type]['list'][$name]['list'] = $value;
$data[$type]['list'][$name]['is_edit'] = 0;
}else{
$data[$type]['list'][$name]['name'] = $name;
$data[$type]['list'][$name]['list'] = $value;
$data[$type]['list'][$name]['is_edit'] = 1;
}
}
}
return $data;
}
//获取历史年份
private function getYearList()
{
$star = 2021;
$end = date("Y",time());
$list = [];
for ($i=$star; $i <= $end; $i++) {
$temp = array(
"value"=>$i,
"name"=>"{$i}"
);
$list[] = $temp ;
}
$this->assign('YearList', $list);
}
public function edit()
{
$act = $_REQUEST['act'];
$isEdit = 1;
if($act == 'add'){
$isEdit = 0;
$this->assign('is_edit', 0);
}else{
$this->assign('is_edit', 1);
}
$this->getYearList();
if(IS_POST) {
$parm = I('post.');
if(count($parm['value']) < 12){
$this->ajaxReturn(["msg"=>"不想添加的月份请设置值为0不能为空","code"=>0]);
}
if($parm['is_edit'] == 0){
$this->doAdd($parm);
}else{
$this->doEdit($parm);
}
$this->ajaxReturn(["msg"=>"添加成功","code"=>1,"url"=>U("lists")]);
}else{
if($isEdit == 1){
$this->resetWarningData();
$this->assign('title', '编辑预警类目');
}else{
$this->assign('title', '新增预警类目');
}
$this->display();
}
}
private function doAdd($p)
{
$adddata = [];
$baseArray = [
'count_year'=>$p['year'],
'type'=>$p['type'],
'name'=>trim($p['name']),
'create_time'=>time()
];
for ($i=0; $i < 12; $i++) {
if($p['value'][$i] != 0){
$countMoth = ($i-0+1);
if(strlen($countMoth) < 2) $countMoth="0".$countMoth;
$baseArray['count_month'] = $countMoth;
$baseArray['money'] = $p['value'][$i]*10000;
$adddata[] = $baseArray;
}
}
if(empty($adddata)){
$this->ajaxReturn(["msg"=>"添加失败,值不能全为0","code"=>0]);
}
$res = $this->statementWarningModel->addAll($adddata);
if(!$res){
$this->ajaxReturn(["msg"=>"添加失败","code"=>0]);
}
}
private function resetWarningData()
{
$where = [
'name'=>$_REQUEST['name'],
'count_year'=>$_REQUEST['count_year']
];
$data = $this->statementWarningModel->where($where)->select();
$sendData = [
'count_year'=>$data[0]['count_year'],
'type'=>$data[0]['type'],
'name'=>$data[0]['name'],
'value'=>[0,0,0,0,0,0,0,0,0,0,0,0],
'ids'=>[0,0,0,0,0,0,0,0,0,0,0,0]
];
foreach ($data as $key => $value) {
$sendData['value'][$value['count_month']-1] = $value['money']/10000;
$sendData['ids'][$value['count_month']-1] = $value['id'];
}
$this->assign('data', $sendData);
}
private function doEdit($p)
{
$adddata = [];
$baseArray = [
'count_year'=>$p['year'],
'type'=>$p['type'],
'name'=>trim($p['name']),
'create_time'=>time()
];
for ($i=0; $i < 12; $i++) {
$id = $p['ids'][$i];
$money = $p['value'][$i];
if($id > 0){
$save['id'] = $id;
$save['money'] = $p['value'][$i]*10000;
$save['name'] = trim($p['name']);
$this->statementWarningModel->save($save);
continue;
}
if($money != 0){
$countMoth = ($i-0+1);
if(strlen($countMoth) < 2) $countMoth="0".$countMoth;
$baseArray['count_month'] = $countMoth;
$baseArray['money'] = $p['value'][$i]*10000;
$adddata[] = $baseArray;
}
}
if(!empty($adddata)){
$this->statementWarningModel->addAll($adddata);
}
}
public function del()
{
$parm = I('get.');
$where = [
'count_year'=>$parm['count_year'],
'name'=>trim($parm['name']),
];
$this->statementWarningModel->where($where)->delete();
$this->success("删除成功!");
}
public function updateStatement()
{
if(!isset($_REQUEST['time'])) $this->error("参数错误");
$time = $_REQUEST['time'];
$params = "php ".SUBSITE_INDEX." StatementWarningSet/setFreeMonth/count_date/{$time}";
$r = D("CmdTasks")->addTask("StatementWarningSet",$params);
if($r){
$this->ajaxReturn(["success"=>"ok"]);
}else{
$this->ajaxReturn(["error"=>"error"]);
}
}
/*********** 详情 ***************/
public function infoLists()
{
if(!array_key_exists("time",$_REQUEST)){
$this->redirect(ACTION_NAME, array('time' => date('Y-m',time())));
}
$time = explode('-',$_REQUEST['time']);
$year= $time[0];
if(strlen($time[1]) < 2) $time[1]="0".$time[1];
$month= $time[1];
$data = $this->statementWarningInfoModel->where("count_year = '{$year}' AND count_month = '{$month}'")->order('FIELD(company_belong,9,0,1),is_wm asc,statement_money desc')->select();
$count = $this->statementWarningInfoModel->where("count_year = '{$year}' AND count_month = '{$month}'")->field("company_belong,sum(statement_money) statement_money,sum(wm_amount) wm_amount,sum(other_amount) other_amount")->group('company_belong')->select();
$this->setWaringInfShow($data,$count);
$this->getMonthLastUpdate($year,$month);
$this->display();
}
private function setWaringInfShow($data,$count)
{
$allcount = [
'statement_money'=>0,
'wm_amount'=>0,
'other_amount'=>0
];
$sendData = [];
foreach ($data as $key => $value) {
$value['statement_money'] = round($value['statement_money']/10000,2);
$value['wm_amount'] = round($value['wm_amount']/10000,2);
$value['other_amount'] = round($value['other_amount']/10000,2);
$value['is_wmstr'] = $this->isWm[$value['is_wm']];
$sendData[$value['company_belong']]['list'][] = $value;
}
foreach ($count as $key => $value) {
$value['statement_money'] = round($value['statement_money']/10000,2);
$value['wm_amount'] = round($value['wm_amount']/10000,2);
$value['other_amount'] = round($value['other_amount']/10000,2);
$sendData[$value['company_belong']]['name'] = $this->companyBelong[$value['company_belong']];
$sendData[$value['company_belong']]['row'] = count( $sendData[$value['company_belong']]['list'])-0+1;
$sendData[$value['company_belong']]['count'] = $value;
$allcount['statement_money'] += $value['statement_money'];
$allcount['wm_amount'] += $value['wm_amount'];
$allcount['other_amount'] += $value['other_amount'];
}
$this->assign('data',$sendData);
$this->assign('allcount',$allcount);
}
private function getMonthLastUpdate($year,$month)
{
$lastUpdate = $this->statementWarningModel->where([
'count_year'=>$year,
'count_month'=>$month,
])->field("MAX(create_time) create_time")->find();
if($lastUpdate){
$lastUpdate = date('Y-m-d H:i:s',$lastUpdate['create_time']);
}else{
$lastUpdate = '未更新';
}
$this->assign('last_update', $lastUpdate);
}
public function infoEdit()
{
$p = I('post.');
$id = $p['id'];
$info = $this->statementWarningInfoModel->where("id = '{$id}'")->find();
$diffMoney = $info['wm_amount'];
if(isset($p['is_wm']) && $p['is_wm'] != $info['is_wm']){
if($p['is_wm'] == 1){
$info['wm_amount'] = $info['statement_money'];
$info['other_amount'] = 0;
}else{
$info['wm_amount'] = 0;
$info['other_amount'] = $info['statement_money'];
}
$info['is_wm'] = $p['is_wm'];
}
if(isset($p['wm_amount'])){
$info['wm_amount'] = $p['wm_amount']*10000;
$info['other_amount'] = $info['statement_money']-$p['wm_amount']*10000;
}
$this->statementWarningInfoModel->save($info);
$diffMoney = ($diffMoney - $info['wm_amount']);
if($diffMoney != 0)
{
//获取母单
$poolname = '';
if($info['company_belong'] == 1){
$poolname = 'pu_statement';
}
if($info['company_belong'] == 0){
$poolname = 'pc_statement';
}
if($info['company_belong'] == 9){
$poolname = 'up_statement';
}
$this->statementWarningModel->where([
'count_year'=>$info['count_year'],
'count_month'=>$info['count_month'],
'name'=>$poolname,
])->setDec('money',$diffMoney);
}
$this->ajaxReturn(['success'=>'ok']);
}
}