Datainfo.php 43.2 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825
<?php

namespace app\admin\controller\kuerp;

use app\common\controller\Backend;
use think\Db;
use app\admin\model\User;
use app\admin\model\Admin;
use app\common\model\Attachment;
use fast\Date;
/**
 * 数据管理
 *
 * @icon fa fa-circle-o
 */
class Datainfo extends Backend
{

    /**
     * Accounts模型对象
     * @var \app\admin\model\kuerp\Accounts
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $setData = (new Goods())->getSetData();
        $this->assign('set', $setData['setList']);
        $this->assignconfig('set',$setData['setList']);
        $this->assign('nowmenu', $setData['nowmenu']);

        $storesList = (new \app\admin\model\kuerp\Stores())->select();
        $storesLists = [];
        foreach ($storesList as $storesList_k=>$storesList_v){
            $storesLists[] = ['name'=>$storesList_v['name'],'value'=>$storesList_v['id']];
        }
        $this->view->assign("storesList", $storesList);
        $this->assignconfig("storesList", $storesLists);

        $storesList = (new \app\admin\model\kuerp\GoodsType())->select();
        $storesLists = [];
        foreach ($storesList as $storesList_k=>$storesList_v){
            $storesLists[] = ['name'=>$storesList_v['supplier_type_name'],'value'=>$storesList_v['id']];
        }
        $this->assignconfig("goodsList", $storesLists);
    }

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            try {
                \think\Db::execute("SET @@sql_mode='';");
            } catch (\Exception $e) {

            }
            $column = [];
            $starttime = Date::unixtime('day', -6);
            $endtime = Date::unixtime('day', 0, 'end');

            $types = $this->request->post("types");
            $legend = '购货金额';
            $joinlist = Db("kuerp_goods_purchase")->where('purchase_time', 'between time', [$starttime, $endtime])
                ->where(['examine_status'=>'1'])
                ->field('SUM(real_money) AS nums,DATE_FORMAT(FROM_UNIXTIME(purchase_time), "%Y-%m-%d") AS join_date')
                ->group('join_date')
                ->select();
            if ($types==2){
                $joinlist = Db("kuerp_goods_sales")->where('purchase_time', 'between time', [$starttime, $endtime])
                    ->where(['examine_status'=>'1'])
                    ->field('SUM(real_money) AS nums,DATE_FORMAT(FROM_UNIXTIME(purchase_time), "%Y-%m-%d") AS join_date')
                    ->group('join_date')
                    ->select();
                $legend = '销货金额';
            }elseif ($types==3){
                $list = Db::name('kuerp_stores')->where(['status'=>'1'])->field('id,name')->select();
                foreach ($list as $list_k=>$list_v){
                    $list[$list_k]['value'] = Db::name('kuerp_goods_stock')->where(['stores_id'=>$list_v['id']])->sum('stock');
                    if(isset($list[$list_k]['id'])){
                        unset($list[$list_k]['id']);
                    }
                }
                $result = array('title'=>'库存统计','legend'=>'库存数量',"data" => $list);
                return json($result);
            }elseif ($types==4){
                $list = Db::name('kuerp_accounts')->field('account_name as name,balance as value')->select();
                $result = array('title'=>'账户统计','legend'=>'资金',"data" => $list);
                return json($result);
            }
            for ($time = $starttime; $time <= $endtime;) {
                $column[] = date("Y-m-d", $time);
                $time += 86400;
            }
            $list = array_fill_keys($column, 0);
            foreach ($joinlist as $k => $v) {
                $list[$v['join_date']] = $v['nums'];
            }
            foreach ($list as $list_k=>$list_v){
                if(empty($list_k)){
                    unset($list[$list_k]);
                }
            }
            $result = array("legend" => $legend, "column" => array_keys($list), "data" => array_values($list));
            return json($result);
        }

        $time_arr = $this->getTimestamp('nowmonth');
        $code = Db::name('kuerp_goods_purchase')->where(['examine_status'=>'1'])->where(['purchase_time'=>['BETWEEN',[$time_arr['start'],$time_arr['end']]]])->column('purchase_code');
        $purchase_num = Db::name('kuerp_goods_purchase_info')->where(['purchase_code'=>['in',$code]])->sum('basic_nmber');
        $purchase_mon = Db::name('kuerp_goods_purchase')->where(['purchase_code'=>['in',$code]])->sum('real_money');
        $this->view->assign('purchase_num',round($purchase_num,2));
        $this->view->assign('purchase_mon',round($purchase_mon,2));

        $code = Db::name('kuerp_goods_sales')->where(['examine_status'=>'1'])->where(['purchase_time'=>['BETWEEN',[$time_arr['start'],$time_arr['end']]]])->column('purchase_code');
        $sales_num = Db::name('kuerp_goods_sales_info')->where(['purchase_code'=>['in',$code]])->sum('basic_nmber');
        $sales_mon = Db::name('kuerp_goods_sales')->where(['purchase_code'=>['in',$code]])->sum('real_money');
        $this->view->assign('sales_num',round($sales_num,2));
        $this->view->assign('sales_mon',round($sales_mon,2));

        $stock_num = Db::name('kuerp_goods')->where(['status'=>1])->sum('inventory');
        $this->view->assign('stock_num',round($stock_num,2));
        $goods = Db::name('kuerp_goods')->where(['status'=>1])->column('goods_code');
        $stock_mon = 0;


        $sort = 'purchase_time';$order='ASC';
        $checkdate = Db::name('kuerp_checkout')->order(['checkdate'=>'DESC'])->value('checktime');
        $map = [];
        if(!empty($checkdate)){
            $map['purchase_time'] = ['>',$checkdate];
        }
        $lists = Db::name('kuerp_goods_sales_profit')
            ->where($map)
            ->order($sort, $order)
            ->select();

        $where = [];
        $list = Db::name('kuerp_goods_sales_profit')
            ->where($where)
            ->order($sort, $order)
            ->select();

        $price_decimal = \think\Db::name('kuerp_sets')->where((new Goods())->setWhere(['name'=>'price_decimal']))->value('value');
        $all_money_all = 0;$profit_all = 0;$net_profit_add = 0;

        $valuation_set= \think\Db::name('kuerp_sets')->where(['name'=>'valuation'])->value('value');//计算方法
        $stores_set= \think\Db::name('kuerp_sets')->where(['name'=>'stores'])->value('is_checked');//是否分仓
        if($valuation_set!=2){
            $valuation_set = 1;
        }
        if($stores_set!=1){
            $stores_set = 0;
        }

        $goods_info = [];
        foreach ($lists as $list_k=>$list_v){
            $cost_all = 0;$pay_money = 0;
            $info = Db::name('kuerp_goods_sales_info')->where(['purchase_code'=>$list_v['purchase_code']])->field('pay_money,basic_nmber,goods_code,stores_id,purchase_time')->select();
            foreach ($info as $info_k=>$info_v){

                if(!isset($goods_info[$info_v['goods_code']])){
                    $where = [];
                    $stores_ids = \think\Db::name('kuerp_stores')->where(['status'=>'1'])->column('id');
                    if($stores_set==1){
                        $where['stores_id'] = ['in',$stores_ids];
                    }else{
                        $where['stores_id'] = $info_v['stores_id'];
                    }
                    $where['goods_code'] = $info_v['goods_code'];

                    $checkdate = Db::name('kuerp_checkout')->order(['checkdate'=>'DESC'])->value('checktime');
                    if(empty($checkdate)){
                        $enable_time = Db::name('kuerp_sets')->where(['name'=>'enable_time'])->value('value');
                        $start_time = strtotime($enable_time);

                        $end_time = strtotime(date('Y-m-d',$info_v['purchase_time']).' 23:59:59');
                        $wheretime['start_time'] = $start_time;
                        $wheretime['end_time'] = $end_time;
                        if(!empty($wheretime)){
                            $where['purchase_time'] = ['between',[$wheretime['start_time'],time()]];
                        }
                        if($valuation_set==1){
                            $purchase_info = Db::name('kuerp_goods_purchase_info')->where($where)->group('purchase_time')
                                ->field('purchase_time,sum(basic_nmber) as basic_nmber,sum(all_money) as all_money')
                                ->order(['purchase_time'=>'ASC'])->select();
                        }else{
                            $purchase_info = Db::name('kuerp_goods_purchase_info')->where($where)
                                ->field('purchase_time,basic_nmber,all_money')
                                ->order(['purchase_time'=>'ASC','purchase_code'=>'ASC'])->select();
                        }
                    }else{
                        $checkout_id = Db::name('kuerp_checkout')->order(['checkdate'=>'DESC'])->value('id');
                        $purchase_more = Db::name('kuerp_checkout_info')->where([
                            'checkout_id'=>$checkout_id,
                            'goods_code'=>$info_v['goods_code'],
                        ])->find();
                        $purchase_infos = [];
                        if(!empty($purchase_more)){
                            $purchase_infos = Db::name('kuerp_goods_purchase')->where(['id'=>$purchase_more['purchase_id']])->find();
                        }

                        $start_time = $checkdate;
                        $end_time = strtotime(date('Y-m-d',$info_v['purchase_time']).' 23:59:59');
                        $wheretime['start_time'] = $start_time;
                        $wheretime['end_time'] = $end_time;
                        if(!empty($wheretime)){
                            $where['purchase_time'] = ['between',[$wheretime['start_time'],time()]];
                        }
                        if($valuation_set==1){
                            $purchase_info = Db::name('kuerp_goods_purchase_info')->where($where)->group('purchase_time')
                                ->field('purchase_time,sum(basic_nmber) as basic_nmber,sum(all_money) as all_money')
                                ->order(['purchase_time'=>'ASC'])->select();
                            if(!empty($purchase_more)){
                                foreach ($purchase_info as $purchase_info_k=>$purchase_info_v){
                                    if($purchase_info_v['purchase_time']==$purchase_more['purchase_time']){
                                        $purchase_info[$purchase_info_k]['basic_nmber'] = (float)$purchase_more['av_basic_nmber'];
                                        $purchase_info[$purchase_info_k]['all_money'] = (float)$purchase_more['av_all_money'];
                                    }
                                }
                            }
                        }else{
                            if(empty($purchase_more)){
                                $purchase_info = Db::name('kuerp_goods_purchase_info')->where($where)
                                    ->field('purchase_time,basic_nmber,all_money')
                                    ->order(['purchase_time'=>'ASC','purchase_code'=>'ASC'])->select();
                            }else{
                                $where['purchase_code'] = ['>=',$purchase_infos['purchase_code']];
                                $purchase_info = Db::name('kuerp_goods_purchase_info')->where($where)
                                    ->field('id,purchase_time,basic_nmber,all_money')
                                    ->order(['purchase_time'=>'ASC','purchase_code'=>'ASC'])->select();
                                foreach ($purchase_info as $purchase_info_k=>$purchase_info_v){
                                    if($purchase_info_v['id']==$purchase_more['purchase_id']){
                                        $purchase_info[$purchase_info_k]['basic_nmber'] = (float)$purchase_more['basic_nmber'];
                                        $purchase_info[$purchase_info_k]['all_money'] = (float)$purchase_more['all_money'];
                                    }
                                }
                            }
                        }
                    }


                    $where = [];
                    $where['goods_code'] = $info_v['goods_code'];
                    $where['stores_id'] = ['in',$stores_ids];
                    if(!empty($wheretime)){
                        $where['createtime'] = ['between',[$wheretime['start_time'],time()]];
                    }
                    //期初
                    $period = Db::name('kuerp_goods_period')->where($where)->field('period as basic_nmber,price as all_money,createtime as purchase_time')->find();
                    if(!empty($period)){
                        $period['purchase_time'] = strtotime(date('Y-m-d',$period['purchase_time']));
                        if($valuation_set==1){
                            $status = true;
                            foreach ($purchase_info as $purchase_info_k=>$purchase_info_v){
                                if($purchase_info_v['purchase_time']==$period['purchase_time']){
                                    $status = false;
                                    $purchase_info[$purchase_info_k]['basic_nmber'] += $period['basic_nmber'];
                                    $purchase_info[$purchase_info_k]['all_money'] += $period['all_money'];
                                }
                            }
                            if($status){
                                $purchase_info[] = $period;
                                $purchase_info = (new Goods())->arraySequence($purchase_info,'purchase_time','SORT_ASC');
                            }
                        }else{
                            $purchase_infos = [];
                            $purchase_infos[] = $period;
                            foreach ($purchase_info as $k=>$v){
                                $purchase_infos[] = $v;
                            }
                            $purchase_info = $purchase_infos;
                        }
                    }

                    $goods = Db::name('kuerp_goods')->where(['goods_code'=>$info_v['goods_code']])->field('id,unit_id')->find();
                    if($goods){
                        $where = [];
                        $where['goods_id'] = $goods['id'];
                        $where['stores_id'] = ['in',$stores_ids];
                        if(!empty($wheretime)){
                            $where['purchase_time'] = ['between',[$wheretime['start_time'],time()]];
                        }
                        //成本调整
                        $adjustment_money = 0;
                        $adjustment = \think\Db::name('kuerp_adjustment_info')->where($where)->select();
                        foreach ($adjustment as $adjustment_k=>$adjustment_v){
                            $advance_rate = 1;//单位换算进率
                            if($adjustment_v['goods_unit']!=$goods['unit_id']){
                                $unit = \think\Db::name('kuerp_unit_conversion')->where(['main_unit'=>$goods['unit_id'],'vice_unit'=>$adjustment_v['goods_unit']])->value('advance_rate');
                                if(!empty($unit) && $unit>0){
                                    $advance_rate = $unit;
                                }
                            }
                            $money = $adjustment_v['money']/$advance_rate;//换算后的调整成本
                            $adjustment[$adjustment_k]['purchase_time'] = strtotime(date('Y-m-d',$adjustment_v['purchase_time']));
                            $adjustment[$adjustment_k]['all_money'] = $adjustment_v['money'];
                        }
                        foreach ($adjustment as $adjustment_k=>$adjustment_v){
                            foreach ($purchase_info as $purchase_info_k=>$purchase_info_v){
                                if($purchase_info_v['purchase_time']==$adjustment_v['purchase_time']){
                                    $purchase_info[$purchase_info_k]['all_money'] += $adjustment_v['all_money'];
                                }
                            }
                        }
                        //成本调整结束
                    }
                }else{
                    $purchase_info = $goods_info[$info_v['goods_code']];
                }

                $cost = (new Goods())->getCost_Price($info_v['goods_code'],'',$info_v['basic_nmber'],$purchase_info);

                if(isset($cost['purchase_info'])){
                    $goods_info[$info_v['goods_code']] = $cost['purchase_info'];
                    $purchase_info = $goods_info[$info_v['goods_code']];
                    $cost = $cost['cost'];
                }
                $cost_all += $cost*$info_v['basic_nmber'];
                $pay_money += $info_v['pay_money'];
            }
            $lists[$list_k]['pay_money'] = $pay_money;
            if($list_v['purchase_type']==2){
                $cost_all = -$cost_all;
            }
            $lists[$list_k]['sales_cost'] = round($cost_all,$price_decimal);
        }
        $sales_cost = 0;
        foreach ($list as $list_k=>$list_v){
            $status = true;
            foreach ($lists as $lists_k=>$lists_v){
                if($list_v['id']==$lists_v['id']){
                    $status = false;
                    $sales_cost += $lists[$lists_k]['sales_cost'];
                }
            }
            if($status){
                $sales_cost += $list_v['sales_cost'];
            }
        }
        $purchase_real_money = Db::name('kuerp_goods_purchase')->where(['examine_status'=>'1'])->sum('real_money');
        $purchase_preferential_money = Db::name('kuerp_goods_purchase')->where(['examine_status'=>'1'])->sum('preferential_money');
        $purchase_money = round($purchase_real_money+$purchase_preferential_money-$sales_cost,$price_decimal);
        $this->view->assign('purchase_money',$purchase_money);
        $this->view->assign('stock_mon',$sales_cost);

        $customers_mon = Db::name('kuerp_customers')->sum('need_receive_account');
        $suppliers_mon = Db::name('kuerp_suppliers')->sum('need_give_account');
        $this->view->assign('customers_mon',round($customers_mon,2));
        $this->view->assign('suppliers_mon',round($suppliers_mon,2));

        $accounts_type1 = Db::name('kuerp_accounts')->where(['account_type_id'=>'1'])->sum('balance');
        $accounts_type2 = Db::name('kuerp_accounts')->where(['account_type_id'=>'2'])->sum('balance');
        $this->view->assign('accounts_type1',round($accounts_type1,$price_decimal));
        $this->view->assign('accounts_type2',round($accounts_type2,$price_decimal));

        return $this->view->fetch();
    }

    public function datainfo(){
        if ($this->request->isAjax()) {
            //获取设置
            $set= \think\Db::name('kuerp_sets')->where((new Goods())->setWhere())->field('type,name,value,is_checked,set')->select();
            $setList = [];
            foreach ($set as $set_k=>$set_v){
                if(!empty($set_v['set'])){
                    $set_v['set'] = unserialize($set_v['set']);
                }
                $setList[$set_v['name']] = $set_v;
            }
            //数量价格格式化
            $number_decimal = $setList['number_decimal']['value'];
            $price_decimal  = $setList['price_decimal']['value'];

            $today = $this->getTimestamp();
            $start_time = $today['start'];
            $end_time = $today['end'];

            $sales_money = Db::name('kuerp_goods_sales')
                ->where(['examine_status'=>'1'])
                ->sum('money_tax_price');
            $sales_money = (int)$sales_money;
            $unit_ad = 10000;
            if($sales_money<$unit_ad){
                $unit = '元';$unit_status = 1;
            }else{
                $unit = '万元';$unit_status = 2;
            }
            $sales_money_str = array_map('intval', str_split($sales_money));
            $sales_money_str = array_reverse($sales_money_str);
            $a = 1;$num_data = [];
            foreach ($sales_money_str as $k=>$v){
                $num_data[] = $v;
                if($a==3){
                    $num_data[] = ',';$a=0;
                }
                $a++;
            }
            if($num_data[sizeof($num_data)-1]==','){
                unset($num_data[sizeof($num_data)-1]);
            }
            $num_data = array_reverse($num_data);

            //饼图
            $goods_type = Db::name('kuerp_goods_type')->where(['status'=>'1'])->field('id,supplier_type_name as name')->select();
            foreach ($goods_type as $goods_type_k=>$goods_type_v){
                $goods_id = Db::name('kuerp_goods')->where(['goods_type_id'=>$goods_type_v['id'],'status'=>'1'])->column('goods_code');
                $this_sales_money = Db::name('kuerp_goods_sales_info')->alias('si')
                    ->join('kuerp_goods_sales s','s.purchase_code=si.purchase_code')
                    ->where(['s.examine_status'=>'1','si.goods_code'=>['in',$goods_id]])
                    ->sum('si.money_tax_price');
                $this_sales_money = round($this_sales_money,$price_decimal);
                if($unit_status==2){
                    $this_sales_money = round($this_sales_money/$unit_ad,$price_decimal);
                }
                $goods_type[$goods_type_k]['sale_money'] = $this_sales_money;

                $this_sales_money = Db::name('kuerp_goods_sales_exchange_info')->alias('si')
                    ->join('kuerp_goods_sales_exchange s','s.purchase_code=si.purchase_code')
                    ->where(['s.examine_status'=>'1','si.goods_code'=>['in',$goods_id]])
                    ->sum('si.money_tax_price');
                $this_sales_money = round($this_sales_money,$price_decimal);
                if($unit_status==2){
                    $this_sales_money = round($this_sales_money/$unit_ad,$price_decimal);
                }
                $goods_type[$goods_type_k]['exchange'] = $this_sales_money;
            }
            $left2_chart = [];
            foreach ($goods_type as $goods_type_k=>$goods_type_v){
                if($goods_type_v['sale_money']>0){
                    $left2_chart[] = ['value'=>$goods_type_v['sale_money'],'name'=>$goods_type_v['name']];
                }
            }
            //退货
            $right2_chart = [];$max = 0;$all_money = 0;$right2_chart['yAxis'] = [];$right2_chart['series'] = [];$right2_chart['series']['max'] = [];
            $right2_chart['series']['data'] = [];
            foreach ($goods_type as $goods_type_k=>$goods_type_v){
                if($goods_type_v['exchange']>0){
                    $right2_chart['yAxis'][] = $goods_type_v['name'];
                    if($max<$goods_type_v['exchange']){
                        $max = $goods_type_v['exchange'];
                    }
                    $all_money += $goods_type_v['exchange'];
                }
            }
            foreach ($goods_type as $goods_type_k=>$goods_type_v){
                if($goods_type_v['exchange']>0){
                    $right2_chart['series']['data'][] = $goods_type_v['exchange'];
                    $right2_chart['series']['max'][] = $max;
                    if($all_money!=0){
                        if(isset($right2_chart['series']['per'][$goods_type_v['name']])){
                            $right2_chart['series']['per'][$goods_type_v['name']] += $goods_type_v['exchange'];
                        }else{
                            $right2_chart['series']['per'][$goods_type_v['name']] = $goods_type_v['exchange'];
                        }
                    }else{
                        $right2_chart['series']['per'][$goods_type_v['name']] = $goods_type_v['exchange'];
                    }
                }
            }
            if(isset($right2_chart['series']['per'])){
                foreach ($right2_chart['series']['per'] as $k=>$v){
                    if($all_money!=0){
                        $right2_chart['series']['per'][$k] = round($v/$all_money*100,$price_decimal);
                    }else{
                        $right2_chart['series']['per'][$k] = 0;
                    }
                }
            }

            $mouth = (int)date('m');
            $time_arr_data = [];
            for($a=1;$a<=$mouth;$a++){
                if($a==12){
                    $time_arr_data[] = ['start_time'=>strtotime(date('Y').'-'.$a),'end_time'=>strtotime((date('Y')+1).'-01-01')-1,'time'=>$a.'月'];
                }else{
                    $time_arr_data[] = ['start_time'=>strtotime(date('Y').'-'.$a),'end_time'=>strtotime(date('Y').'-'.($a+1))-1,'time'=>$a.'月'];
                }
            }

            //新增客户
            $time_arr = $time_arr_data;
            $left3_chart = [];$xAxis = [];$series = [];$series['customers'] = [];$series['customers_num'] = [];
            foreach ($time_arr as $time_arr_k=>$time_arr_v){
                $xAxis[] = $time_arr_v['time'];
                $customers_num = Db::name('kuerp_customers')->where(['createtime'=>['<',$time_arr_v['end_time']]])->count();
                $customers = Db::name('kuerp_customers')->where(['createtime'=>['between',[$time_arr_v['start_time'],$time_arr_v['end_time']]]])->count();
                $series['customers'][] = $customers;
                $series['customers_num'][] = $customers_num;
            }
            $left3_chart['xAxis'] = $xAxis;
            $left3_chart['series'] = $series;

            //累计营业额
            $time_arr = $time_arr_data;
            $center1_chart = [];$xAxis = [];$series = [];$series['count'] = [];$series['num'] = [];
            foreach ($time_arr as $time_arr_k=>$time_arr_v){
                $xAxis[] = $time_arr_v['time'];
                $where = [];
                $where['purchase_time'] = ['between',[$time_arr_v['start_time'],$time_arr_v['end_time']]];
                $where['examine_status'] = '1';
                $this_sales_money = Db::name('kuerp_goods_sales')
                    ->where($where)
                    ->sum('money_tax_price');
                $this_sales_money = round($this_sales_money,$price_decimal);
                if($unit_status==2){
                    $this_sales_money = round($this_sales_money/$unit_ad,$price_decimal);
                }
                $this_sales_count = Db::name('kuerp_goods_sales')->where($where)->count('id');
                $series['count'][] = $this_sales_count;
                $series['num'][] = $this_sales_money;
            }
            $center1_chart['xAxis'] = $xAxis;
            $center1_chart['series'] = $series;

            //采购/销售
            $time_arr = $time_arr_data;
            $center2_chart = [];$yAxis = [];$series = [];$series['purchase'] = [];$series['sales'] = [];
            foreach ($time_arr as $time_arr_k=>$time_arr_v){
                $yAxis[] = $time_arr_v['time'];

                $where = [];
                $where['purchase_time'] = ['between',[$time_arr_v['start_time'],$time_arr_v['end_time']]];
                $where['examine_status'] = '1';

                $this_sales_money = Db::name('kuerp_goods_purchase')
                    ->where($where)
                    ->field('sum(preferential_money) as preferential_money,sum(real_money) as real_money')->find();
                $this_sales_money = round($this_sales_money['preferential_money']+$this_sales_money['real_money'],$price_decimal);
                if($unit_status==2){
                    $this_sales_money = round($this_sales_money/$unit_ad,$price_decimal);
                }
                $series['purchase'][] = -$this_sales_money;
                $this_sales_money = Db::name('kuerp_goods_sales')
                    ->where($where)
                    ->sum('money_tax_price');
                $this_sales_money = round($this_sales_money,$price_decimal);
                if($unit_status==2){
                    $this_sales_money = round($this_sales_money/$unit_ad,$price_decimal);
                }
                $series['sales'][] = $this_sales_money;
            }
            $center2_chart['yAxis'] = $yAxis;
            $center2_chart['series'] = $series;

            //销售排行
            $sale_list = Db::name('kuerp_goods_sales_info')->alias('si')
                ->join('kuerp_goods_sales s','s.purchase_code=si.purchase_code')
                ->where(['s.examine_status'=>'1'])
                ->group('si.goods_code')
                ->field('si.goods_code,sum(si.money_tax_price) as money_tax_price')
                ->order(['money_tax_price'=>'DESC'])->select();
            foreach ($sale_list as $sale_list_k=>$sale_list_v){
                $sale_list[$sale_list_k]['goods_name'] = Db::name('kuerp_goods')->where(['goods_code'=>$sale_list_v['goods_code']])->value('goods_name');
                $sale_list[$sale_list_k]['money_tax_price'] = round($sale_list_v['money_tax_price'],$price_decimal);
                if($unit_status==2){
                    $sale_list[$sale_list_k]['money_tax_price'] = round($sale_list_v['money_tax_price']/$unit_ad,$price_decimal);
                }
            }

            $result = array('unit'=>$unit,"sales_money" => $num_data,"left2_chart" => $left2_chart,"left3_chart" => $left3_chart,'center1_chart'=>$center1_chart
            ,'center2_chart'=>$center2_chart,'right2_chart'=>$right2_chart,'sale_list'=>$sale_list);
            return json($result);
        }
        $weekarray=array("日","一","二","三","四","五","六"); //先定义一个数组
        $week = "星期".$weekarray[date("w")];
        $date = date('Y年m月d日');
        $this->view->assign("date", $date);
        $this->view->assign("week", $week);
        $this->view->assign("time", date('H:i:s'));
        $this->view->assign("title", '可视化大屏');
        return $this->view->fetch();
    }

    public function stockworing(){
        if ($this->request->isAjax()) {
            $data = input();
            $where = [];
            $where['stock_warning'] = '1';
            $map = [];
            if(isset($data['filter'])){
                $filter = (array)json_decode($data['filter'], true);
                $filter = $filter ? $filter : [];
                if(!empty($filter['goods_code'])){
                    $where['goods_code'] = ["like", '%'.$filter['goods_code'].'%'];
                }
                if(!empty($filter['purchase_time'])){
                    $str = explode(' - ',$filter['purchase_time']);
                    $where['purchase_time'] = ["BETWEEN", [strtotime($str[0]),strtotime($str[1])]];
                }
                if(!empty($filter['goods_name'])){
                    $where['goods_name'] = ["like", '%'.$filter['goods_name'].'%'];
                }
                if(!empty($filter['type_id'])){
                    $where['goods_type_id'] = ["in", $filter['type_id']];
                }
                if(!empty($filter['stores_id'])){
                    $map['sw.stores_id'] = ["in", $filter['stores_id']];
                }
            }

            $list = Db::name('kuerp_goods')
                ->where($where)
                ->select();

            $datas = [];
            foreach ($list as $list_k=>$list_v){
                $unit = Db::name('kuerp_unit')->where(['id'=>$list_v['unit_id']])->field('id,name')->find();
                $list[$list_k]['unit'] = $unit;
                if($unit){
                    $unit_name = $unit['name'];
                    $rowadvance_rate = '1';
                    if($list_v['more_unit']==1){
                        $more_unit = Db::name('kuerp_unit_conversion')->alias('uc')
                            ->join('kuerp_unit u','u.id=uc.vice_unit')
                            ->where(['uc.main_unit'=>$unit['id']])
                            ->order('uc.advance_rate','ASC')
                            ->field('uc.vice_unit,u.name,u.id,uc.advance_rate')
                            ->select();
                        foreach ($more_unit as $unit_k=>$unit_v){
                            $unit_name .= ','.$unit_v['name'];
                            $rowadvance_rate .= ':'.$unit_v['advance_rate'];
                        }
                        $list[$list_k]['unitconversion'] = $unit_name.'('.$rowadvance_rate.')';
                    }else{
                        $list[$list_k]['unitconversion'] = '';
                    }
                }
                if(empty($unit)){
                    $list[$list_k]['unit'] = ['name'=>''];
                }
                $list[$list_k]['goodstype'] = Db::name('kuerp_goods_type')->where(['id'=>$list_v['goods_type_id']])->field('supplier_type_name')->find();
                $list[$list_k]['stores'] = Db::name('kuerp_stores')->where(['id'=>$list_v['manager_warehouse_id']])->field('name')->find();
                $list[$list_k]['stock'] = $list_v['inventory'];
                if($list_v['branch']==1){
                    $lists = Db::name('kuerp_goods_stock_warning')->alias('sw')
                        ->join('kuerp_stores ks','ks.id=sw.stores_id')->where($map)->where(['sw.goods_code'=>$list_v['goods_code']])
                        ->field('sw.stores_id,sw.min_stock,sw.max_stock,ks.name')->select();
                    foreach ($lists as $lists_k=>$lists_v){
                        $stock = Db::name('kuerp_goods_stock')->where(['goods_code'=>$list_v['goods_code'],'stores_id'=>$lists_v['stores_id']])->value('stock');
                        $datas[] = [
                            'goodstype' => $list[$list_k]['goodstype'],
                            'goods_code' => $list_v['goods_code'],
                            'goods_name' => $list_v['goods_name'],
                            'goods_barcode' => $list_v['goods_barcode'],
                            'goods_image' => $list_v['goods_image'],
                            'more_unit' => $list[$list_k]['more_unit'],
                            'unit' => $list[$list_k]['unit'],
                            'unitconversion' => $list[$list_k]['unitconversion'],
                            'stores' => ['name'=>$lists_v['name']],
                            'min_stock' => $lists_v['min_stock'],
                            'max_stock' => $lists_v['max_stock'],
                            'stock' => (float)$stock,
                        ];
                    }
                    unset($list[$list_k]);
                }else{
                    $list[$list_k]['stores'] = ['name'=>'全部仓库'];
                }
            }
            foreach ($datas as $datas_k=>$datas_v){
                $list[] = $datas_v;
            }
            foreach ($list as $list_k=>$list_v){
                if($list_v['stock']>=$list_v['min_stock'] && $list_v['stock']<=$list_v['max_stock']){
                    unset($list[$list_k]);
                }
            }
            $result = array("total" => sizeof($list), "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    function getTimestamp($name = 'today', $param = '',$mouth='')
    {
        $nowtimr = strtotime(date('Y-m-d H') . ':' . (date('i') - date('i') % 10));
        switch ($name) {
            case 'now':
                $timeamp['start'] = $nowtimr - 600;
                $timeamp['end'] = $nowtimr;
                $timeamp['time'] = date('H:i', $timeamp['end']);
                break;
            case 'ten':
                $timeamp['start'] = $nowtimr - 1200;
                $timeamp['end'] = $nowtimr - 600;
                $timeamp['time'] = date('H:i', $timeamp['end']);
                break;
            case 'twenty':
                $timeamp['start'] = $nowtimr - 1800;
                $timeamp['end'] = $nowtimr - 1200;
                $timeamp['time'] = date('H:i', $timeamp['end']);
                break;
            case 'thirty':
                $timeamp['start'] = $nowtimr - 2400;
                $timeamp['end'] = $nowtimr - 1800;
                $timeamp['time'] = date('H:i', $timeamp['end']);
                break;
            case 'forty':
                $timeamp['start'] = $nowtimr - 3000;
                $timeamp['end'] = $nowtimr - 2400;
                $timeamp['time'] = date('H:i', $timeamp['end']);
                break;
            case 'fifty':
                $timeamp['start'] = $nowtimr - 3600;
                $timeamp['end'] = $nowtimr - 3000;
                $timeamp['time'] = date('H:i', $timeamp['end']);
                break;
            case 'today'://今天
                $timeamp['time'] = date('m-d');
                $timeamp['start'] = strtotime(date('Y-m-d'));
                $timeamp['end'] = strtotime(date('Y-m-d',strtotime('+1 day')))>time()?time():strtotime(date('Y-m-d',strtotime('+1 day')));
                break;
            case 'yesterday'://昨天
                $timeamp['time'] = date('m-d',strtotime('-1 day'));
                $timeamp['start'] = strtotime(date('Y-m-d',strtotime('-1 day')));
                $timeamp['end'] = strtotime(date('Y-m-d'));
                break;
            case 'beforyesterday'://前天
                $timeamp['time'] = date('m-d',strtotime('-2 day'));
                $timeamp['start'] = strtotime(date('Y-m-d',strtotime('-2 day')));
                $timeamp['end'] = strtotime(date('Y-m-d',strtotime('-1 day')));
                break;
            case 'threeday'://前3天
                $timeamp['time'] = date('m-d',strtotime('-3 day'));
                $timeamp['start'] = strtotime(date('Y-m-d',strtotime('-3 day')));
                $timeamp['end'] = strtotime(date('Y-m-d',strtotime('-2 day')));
                break;
            case 'fourday'://前4天
                $timeamp['time'] = date('m-d',strtotime('-4 day'));
                $timeamp['start'] = strtotime(date('Y-m-d',strtotime('-4 day')));
                $timeamp['end'] = strtotime(date('Y-m-d',strtotime('-3 day')));
                break;
            case 'fifday'://前5天
                $timeamp['time'] = date('m-d',strtotime('-5 day'));
                $timeamp['start'] = strtotime(date('Y-m-d',strtotime('-5 day')));
                $timeamp['end'] = strtotime(date('Y-m-d',strtotime('-4 day')));
                break;
            case 'sixday'://前6天
                $timeamp['time'] = date('m-d',strtotime('-6 day'));
                $timeamp['start'] = strtotime(date('Y-m-d',strtotime('-6 day')));
                $timeamp['end'] = strtotime(date('Y-m-d',strtotime('-5 day')));
                break;
            case 'beforweek'://本周
                $timeamp['start'] = strtotime(date("Y-m-d H:i:s", mktime(0, 0, 0, date("m"), date("d") - date("w") + 1, date("Y"))));
                $timeamp['end'] = strtotime(date("Y-m-d H:i:s", mktime(23, 59, 59, date("m"), date("d") - date("w") + 7, date("Y"))));
                break;
            case 'nowmonth'://本月
                $timeamp['start'] = strtotime(date('Y-m-01'));
                $timeamp['end'] = strtotime(date('Y-m-d',strtotime('+1 day')))>time()?time():strtotime(date('Y-m-d',strtotime('+1 day')));
                break;
            case 'permonth'://上月
                $timeamp['start'] = strtotime(date('Y-m-01', strtotime('-1 month')));
                $timeamp['end'] = strtotime(date('Y-m-01'));
                break;
            case 'oldmonth'://前几月
                $timeamp['start'] = strtotime(date('Y-m-01', strtotime('-'.$mouth.' month')));
                if($mouth==1){
                    $timeamp['end'] = strtotime(date('Y-m-01'));
                }else{
                    $timeamp['end'] = strtotime(date('Y-m-01', strtotime('-'.($mouth-1).' month')));
                }
                break;
            case 'preweek'://上周 注意我们是从周一开始算
                $timeamp['start'] = strtotime(date('Y-m-d', strtotime('-2 week Monday')));
                $timeamp['end'] = strtotime(date('Y-m-d', strtotime('-1 week Monday +1 day')));
                break;
            case 'nowweek'://本周
                $timeamp['start'] = strtotime(date('Y-m-d', strtotime('-1 week Monday')));
                $timeamp['end'] = strtotime(date('Y-m-d', strtotime('+1 day')));
                break;
            case 'preday'://30
                $timeamp['start'] = strtotime(date('Y-m-d'), strtotime($param . ' day'));
                $timeamp['end'] = strtotime(date('Y-m-d'));
                break;
            case 'nextday'://30
                $timeamp['start'] = strtotime(date('Y-m-d'));
                $timeamp['end'] = strtotime(date('Y-m-d'), strtotime($param . ' day'));
                break;
            case 'preyear'://去年
                $timeamp['start'] = strtotime(date('Y-01-01', strtotime('-1 year')));
                $timeamp['end'] = strtotime(date('Y-12-31', strtotime('-1 year')));
                break;
            case 'nowyear'://今年
                $timeamp['start'] = strtotime(date('Y-01-01'));
                $timeamp['end'] = strtotime(date('Y-m-d',strtotime('+1 day')))>time()?time():strtotime(date('Y-m-d',strtotime('+1 day')));
                break;
            case 'quarter'://季度
                $quarter = empty($param) ? ceil((date('n'))/3) : $param;//获取当前季度
                $timeamp['start'] = mktime(0, 0, 0,$quarter*3-2,1,date('Y'));
                $timeamp['end'] = mktime(0, 0, 0,$quarter*3+1,1,date('Y'))>time()?time():mktime(0, 0, 0,$quarter*3+1,1,date('Y'));
                break;
            case 'lastquarter'://季度
                $quarter = empty($param) ? ceil((date('n'))/3) : $param;//获取当前季度
                $quarter -= 1;
                if($quarter>0){
                    $timeamp['start'] = mktime(0, 0, 0,$quarter*3-2,1,date('Y'));
                    $timeamp['end'] = mktime(0, 0, 0,$quarter*3+1,1,date('Y'))>time()?time():mktime(0, 0, 0,$quarter*3+1,1,date('Y'));
                }else{
                    $quarter = 4;
                    $timeamp['start'] = mktime(0, 0, 0,$quarter*3-2,1,(date('Y')-1));
                    $timeamp['end'] = mktime(0, 0, 0,$quarter*3+1,1,(date('Y')-1))>time()?time():mktime(0, 0, 0,$quarter*3+1,1,(date('Y')-1));
                }
                break;
            case 'lastyeayquarter'://去年同季度
                $quarter = empty($param) ? ceil((date('n'))/3) : $param;//获取当前季度
                $timeamp['start'] = mktime(0, 0, 0,$quarter*3-2,1,(date('Y')-1));
                $timeamp['end'] = mktime(0, 0, 0,$quarter*3+1,1,(date('Y')-1))>time()?time():mktime(0, 0, 0,$quarter*3+1,1,(date('Y')-1));
                break;
            default:
                $timeamp['start'] = strtotime(date('Y-m-d'));
                $timeamp['end'] = strtotime(date('Y-m-d', strtotime('+1 day')));
                break;
        }
        if($timeamp['end']>time()){
            $timeamp['end'] = time();
        }
        return $timeamp;
    }
}