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.

283 lines
9.5 KiB
PHTML

<?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=>'营业利润'
];
//创建
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'],
"name"=>'wm_platm',
"type"=>1
])->field("MAX(count_month),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 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"]);
}
}
}