GoodsSalesProfit.php 22.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
<?php

namespace app\admin\controller\kuerp;

use app\common\controller\Backend;
use think\Db;

/**
 * 销售利润管理
 *
 * @icon fa fa-circle-o
 */
class GoodsSalesProfit extends Backend
{

    /**
     * GoodsSalesProfit模型对象
     * @var \app\admin\model\kuerp\GoodsSalesProfit
     */
    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']);

        $this->model = new \app\admin\model\kuerp\GoodsSalesProfit;
        $this->view->assign("purchaseTypeList", $this->model->getPurchaseTypeList());

        $this->view->assign("customerType", $this->model->getCustomertypeList());
        $this->view->assign("customerList", $this->model->getCustomersList());
        $this->view->assign("goodsList", $this->model->getGoodsList());
        $this->view->assign("goodsType", $this->model->getGoodsTypeList());
    }

    public function import()
    {
        parent::import();
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {
        $price_decimal = \think\Db::name('kuerp_sets')->where((new Goods())->setWhere(['name'=>'price_decimal']))->value('value');
        $number_decimal = \think\Db::name('kuerp_sets')->where((new Goods())->setWhere(['name'=>'number_decimal']))->value('value');
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            $data = input();
            $where = [];
            if(isset($data['filter'])){
                $filter = (array)json_decode($data['filter'], true);
                $filter = $filter ? $filter : [];
                if(!empty($filter['purchase_time'])){
                    $str = explode(' - ',$filter['purchase_time']);
                    $where['purchase_time'] = ["BETWEEN", [strtotime($str[0]),strtotime($str[1])]];
                }
                if(!empty($filter['customerType'])){
                    $cid = Db::name('kuerp_customers')->where((new Goods())->setWhere(['customertype_id'=>$filter['customerType']]))->column('id');
                    $where['customers_id'] = ["in", $cid];
                }
                if(!empty($filter['customerList'])){
                    $where['customers_id'] = ['in',$filter['customerList']];
                }
            }
            $list = Db::name('kuerp_goods_sales')->where($where)->where(['examine_status'=>'1'])->group('customers_id')->field('customers_id')->select();

            $listss = [];
            foreach ($list as $list_k2=>$list_v2){
                $customers = Db::name('kuerp_customers')->where(['id'=>$list_v2['customers_id']])->field('customer_code,customertype_id,customer_name')->find();
                if($customers){
                    $c_type = Db::name('kuerp_customertype')->where(['id'=>$customers['customertype_id']])->value('type_name');
                }

                $info = Db::name('kuerp_goods_sales_profit')->where(['customers_id'=>$list_v2['customers_id']])->select();

                if($info){
                    $checkdate = Db::name('kuerp_checkout')->order(['checkdate'=>'DESC'])->value('checktime');
                    $enable_time = Db::name('kuerp_sets')->where(['name'=>'enable_time'])->value('value');
                    $map = [];
                    if(!empty($checkdate)){
                        $map['purchase_time'] = ['>',$checkdate];
                    }
                    $price_decimal = Db::name('kuerp_sets')->where(['name'=>'price_decimal'])->value('value');
                    $valuation_set= Db::name('kuerp_sets')->where(['name'=>'valuation'])->value('value');//计算方法
                    $stores_set= Db::name('kuerp_sets')->where(['name'=>'stores'])->value('is_checked');//是否分仓
                    if($valuation_set!=2){
                        $valuation_set = 1;
                    }
                    if($stores_set!=1){
                        $stores_set = 0;
                    }
                    $price_taxes= Db::name('kuerp_sets')->where(['name'=>'price_taxes'])->value('is_checked');
                    $taxes_set= Db::name('kuerp_sets')->where(['name'=>'taxes'])->field('value,is_checked')->find();

                    $sort = 'purchase_time';$order='ASC';
                    $lists = Db::name('kuerp_goods_sales_profit')
                        ->where($map)
                        ->order($sort, $order)
                        ->select();
                    $goods_info = [];
                    $stores_ids = Db::name('kuerp_stores')->where(['status'=>'1'])->column('id');

                    $lists = (new Goods())->getPrList($lists,$stores_set,$stores_ids,$enable_time,$valuation_set,$goods_info,$price_taxes,$taxes_set,$price_decimal);

                    foreach ($info as $list_k=>$list_v){
                        $status = true;
                        foreach ($lists as $lists_k=>$lists_v){
                            if($list_v['id']==$lists_v['id']){
                                $status = false;
                                $info[$list_k]['pay_money'] = $lists[$lists_k]['pay_money'];
                                $info[$list_k]['sales_cost'] = $lists[$lists_k]['sales_cost'];
                                $info[$list_k]['profit'] = $lists[$lists_k]['profit'];
                                $info[$list_k]['profit_rate'] = $lists[$lists_k]['profit_rate'];
                                $info[$list_k]['net_profit'] = $lists[$lists_k]['net_profit'];
                                $info[$list_k]['net_profit_rate'] = $lists[$lists_k]['net_profit_rate'];
                            }
                        }
                        if($status){
                            $cost_all = $list_v['sales_cost'];
                            $info[$list_k]['sales_cost'] = round($cost_all,$price_decimal);
                            $info[$list_k]['profit'] = round($list_v['all_money']-$cost_all,$price_decimal);
                            if($list_v['all_money']>0){
                                $info[$list_k]['profit_rate'] = round($info[$list_k]['profit']/$list_v['all_money']*100,2);
                            }else{
                                $info[$list_k]['profit_rate'] = 0;
                            }
                            $info[$list_k]['net_profit'] = round($info[$list_k]['profit']-$list_v['preferential_money']-$list_v['purchase_money'],$price_decimal);
                            if($list_v['all_money']>0){
                                $info[$list_k]['net_profit_rate'] = round($info[$list_k]['net_profit']/$list_v['all_money']*100,2);
                            }else{
                                $info[$list_k]['net_profit_rate'] = 0;
                            }
                        }
                    }
                }

                $count = 0;
                $basic_nmber = 0;
                $all_money = 0;
                $tax_money = 0;
                $money_tax_price = 0;
                $cost_all = 0;
                foreach ($info as $info_k=>$info_v){
                    $basic_nmber += $info_v['number'];
                    $all_money += $info_v['all_money'];
                    $tax_money += $info_v['tax_money'];
                    $money_tax_price += $info_v['money_tax_price'];
                    $cost_all += $info_v['sales_cost'];
                    if($info_v['purchase_type']==1){
                        $count += 1;
                        /*$basic_nmber += $info_v['number'];
                        $all_money += $info_v['all_money'];
                        $tax_money += $info_v['tax_money'];
                        $money_tax_price += $info_v['money_tax_price'];
                        $cost_all += $info_v['sales_cost'];
                    }else{
                        $basic_nmber -= $info_v['number'];
                        $all_money -= $info_v['all_money'];
                        $tax_money -= $info_v['tax_money'];
                        $money_tax_price -= $info_v['money_tax_price'];
                        $cost_all -= $info_v['sales_cost'];*/
                    }
                }
                if($basic_nmber>0){
                    $unit_cost = round($cost_all/$basic_nmber,$price_decimal);
                }else{
                    $unit_cost = 0;
                }
                $profit = $all_money-$cost_all;
                if($profit>0){
                    $profit_rate = round($profit/$all_money*100,2);
                }else{
                    $profit_rate = 0;
                }
                $listss[] = [
                    'c_type_name' => isset($c_type)?$c_type:'',
                    'customer_code' => isset($customers['customer_code'])?$customers['customer_code']:'',
                    'customer_name' => isset($customers['customer_name'])?$customers['customer_name']:'',
                    'count' => $count,
                    'basic_nmber' => round($basic_nmber,$number_decimal),
                    'all_money' => round($all_money,$price_decimal),
                    'tax_money' => round($tax_money,$price_decimal),
                    'money_tax_price' => round($money_tax_price,$price_decimal),
                    'unit_cost' => round($unit_cost,$price_decimal),
                    'cost_all' => round($cost_all,$price_decimal),
                    'profit' => round($profit,$price_decimal),
                    'profit_rate' => round($profit_rate,4),
                ];
            }
            $lists = $listss;

            $result = array("total" => sizeof($lists), "rows" => $lists);

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

    /**
     * 二维数组根据字段进行排序
     * @params array $array 需要排序的数组
     * @params string $field 排序的字段
     * @params string $sort 排序顺序标志 SORT_DESC 降序;SORT_ASC 升序
     */
    function arraySequence($array, $field, $sort)
    {
        $arrSort = array();
        foreach ($array as $uniqid => $row) {
            foreach ($row as $key => $value) {
                $arrSort[$key][$uniqid] = $value;
            }
        }
        array_multisort($arrSort[$field], constant($sort), $array);
        return $array;
    }

    public function index2()
    {
        $price_decimal = \think\Db::name('kuerp_sets')->where((new Goods())->setWhere(['name'=>'price_decimal']))->value('value');
        $number_decimal = \think\Db::name('kuerp_sets')->where((new Goods())->setWhere(['name'=>'number_decimal']))->value('value');

        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            $data = input();
            $where = [];
            if(isset($data['filter'])){
                $filter = (array)json_decode($data['filter'], true);
                $filter = $filter ? $filter : [];
                if(!empty($filter['purchase_time'])){
                    $str = explode(' - ',$filter['purchase_time']);
                    $where['purchase_time'] = ["BETWEEN", [strtotime($str[0]),strtotime($str[1])]];
                }
                if(!empty($filter['goodsType'])){
                    $cid = Db::name('kuerp_goods')->where((new Goods())->setWhere(['goods_type_id'=>$filter['goodsType']]))->column('goods_code');
                    $where['goods_code'] = ["in", $cid];
                }
                if(!empty($filter['goods_code'])){
                    $where['goods_code'] = ['in',$filter['goods_code']];
                }
                if(!empty($filter['id'])){
                    $where['id'] = ['<',$filter['id']];
                }
            }

            $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_info')
                ->where($map)
                ->order($sort, $order)
                ->select();

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

            if(!empty($list)){
                $all_money_all = 0;$profit_all = 0;$net_profit_add = 0;

                $checkdate = Db::name('kuerp_checkout')->order(['checkdate'=>'DESC'])->value('checktime');
                $enable_time = Db::name('kuerp_sets')->where(['name'=>'enable_time'])->value('value');
                $map = [];
                if(!empty($checkdate)){
                    $map['purchase_time'] = ['>',$checkdate];
                }
                $price_decimal = Db::name('kuerp_sets')->where(['name'=>'price_decimal'])->value('value');
                $valuation_set= Db::name('kuerp_sets')->where(['name'=>'valuation'])->value('value');//计算方法
                $stores_set= Db::name('kuerp_sets')->where(['name'=>'stores'])->value('is_checked');//是否分仓
                if($valuation_set!=2){
                    $valuation_set = 1;
                }
                if($stores_set!=1){
                    $stores_set = 0;
                }
                $price_taxes= Db::name('kuerp_sets')->where(['name'=>'price_taxes'])->value('is_checked');
                $taxes_set= Db::name('kuerp_sets')->where(['name'=>'taxes'])->field('value,is_checked')->find();
                $stores_ids = Db::name('kuerp_stores')->where(['status'=>'1'])->column('id');

                $goods_info = [];
                $lists = (new Goods())->getPrList($lists,$stores_set,$stores_ids,$enable_time,$valuation_set,$goods_info,$price_taxes,$taxes_set,$price_decimal);

                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;
                            $list[$list_k]['pay_money'] = $lists[$lists_k]['pay_money'];
                            $list[$list_k]['sales_cost'] = $lists[$lists_k]['sales_cost'];
                            $list[$list_k]['profit'] = $lists[$lists_k]['profit'];
                            $list[$list_k]['profit_rate'] = $lists[$lists_k]['profit_rate'];
                            $list[$list_k]['net_profit'] = $lists[$lists_k]['net_profit'];
                            $list[$list_k]['net_profit_rate'] = $lists[$lists_k]['net_profit_rate'];
                            $list[$list_k]['info_goods'] = $lists_v['info_goods'];
                            $all_money_all += $lists[$lists_k]['all_money'];
                            $profit_all += $lists[$lists_k]['profit'];
                            $net_profit_add += $lists[$lists_k]['net_profit'];
                        }
                    }
                    if($status){
                        $cost_all = (float)(Db::name('kuerp_goods_sales_profit')
                            ->where(['purchase_code'=>$list_v['purchase_code']])->value('sales_cost'));
                        $list[$list_k]['sales_cost'] = round($cost_all,$price_decimal);
                        $list[$list_k]['profit'] = round($list_v['all_money']-$cost_all,$price_decimal);
                        if($list_v['all_money']>0){
                            $list[$list_k]['profit_rate'] = round($list[$list_k]['profit']/$list_v['all_money']*100,2);
                        }else{
                            $list[$list_k]['profit_rate'] = 0;
                        }
                        $list[$list_k]['net_profit'] = round($list[$list_k]['profit']-$list_v['preferential_money']-$list_v['purchase_money'],$price_decimal);
                        if($list_v['all_money']>0){
                            $list[$list_k]['net_profit_rate'] = round($list[$list_k]['net_profit']/$list_v['all_money']*100,2);
                        }else{
                            $list[$list_k]['net_profit_rate'] = 0;
                        }
                        $list[$list_k]['info_goods'] = [];
                    }
                }
                $profit_list = [];$cost_price_list = [];
                foreach ($list as $list_k=>$list_v){
                    $profit_list[$list_v['id']] = $list_v['sales_cost'];
                    $cost_price_list[$list_v['id']] = $list_v['info_goods'];
                }
                unset($list);


                $where = [];
                if(isset($data['filter'])){
                    $filter = (array)json_decode($data['filter'], true);
                    $filter = $filter ? $filter : [];
                    if(!empty($filter['purchase_time'])){
                        $str = explode(' - ',$filter['purchase_time']);
                        $where['purchase_time'] = ["BETWEEN", [strtotime($str[0]),strtotime($str[1])]];
                    }
                    if(!empty($filter['goodsType'])){
                        $cid = Db::name('kuerp_goods')->where((new Goods())->setWhere(['goods_type_id'=>$filter['goodsType']]))->column('goods_code');
                        $where['goods_code'] = ["in", $cid];
                    }
                    if(!empty($filter['goods_code'])){
                        $where['goods_code'] = ['in',$filter['goods_code']];
                    }
                }
                $where = (new Goods())->setWhere($where);
                $lists=[];
                $list = Db::name('kuerp_goods_sales_info')->where($where)->group('goods_code')->field('goods_code')->select();
                foreach ($list as $list_k=>$list_v){
                    $goods = Db::name('kuerp_goods')->where((new Goods())->setWhere(['goods_code'=>$list_v['goods_code']]))->field('goods_code,goods_type_id,goods_name')->find();
                    if($goods){
                        $c_type = Db::name('kuerp_goods_type')->where((new Goods())->setWhere(['id'=>$goods['goods_type_id']]))->value('supplier_type_name');
                    }

                    $info = Db::name('kuerp_goods_sales_info')->where((new Goods())->setWhere(['goods_code'=>$list_v['goods_code']]))->select();
                    $count = 0;
                    $basic_nmber = 0;
                    $all_money = 0;
                    $tax_money = 0;
                    $money_tax_price = 0;
                    $cost_all = 0;
                    foreach ($info as $info_k=>$info_v){
                        if($info_v['purchase_type']==1){
                            $count += 1;
                            $basic_nmber += $info_v['basic_nmber'];
                            $all_money += $info_v['all_money'];
                            $tax_money += $info_v['tax_money'];
                            $money_tax_price += $info_v['money_tax_price'];
                            $cost_all += isset($profit_list[$info_v['id']])?$profit_list[$info_v['id']]:0;
                        }else{
                            $basic_nmber -= $info_v['basic_nmber'];
                            $all_money -= $info_v['all_money'];
                            $tax_money -= $info_v['tax_money'];
                            $money_tax_price -= $info_v['money_tax_price'];
                            $cost_price = isset($cost_price_list[$info_v['id']])?$cost_price_list[$info_v['id']]:[];
                            $cost_price = isset($cost_price[$info_v['goods_code']])?$cost_price[$info_v['goods_code']]:'';
                            if($cost_price===''){
                                $cost_price = Db::name('kuerp_goods')->where(['goods_code'=>$info_v['goods_code']])->value('purchase');
                            }
                            $cost_all -= $cost_price*$info_v['basic_nmber'];
                        }
                    }
                    if($basic_nmber>0){
                        $unit_cost = round($cost_all/$basic_nmber,$price_decimal);
                    }else{
                        $unit_cost = 0;
                    }
                    $profit = $all_money-$cost_all;
                    if($profit>0){
                        $profit_rate = round($profit/$all_money*100,2);
                    }else{
                        $profit_rate = 0;
                    }
                    $lists[] = [
                        'c_type_name' => isset($c_type)?$c_type:'',
                        'customer_code' => isset($goods['goods_code'])?$goods['goods_code']:'',
                        'customer_name' => isset($goods['goods_name'])?$goods['goods_name']:'',
                        'count' => $count,
                        'basic_nmber' => round($basic_nmber,$number_decimal),
                        'all_money' => round($all_money,$price_decimal),
                        'tax_money' => round($tax_money,$price_decimal),
                        'money_tax_price' => round($money_tax_price,$price_decimal),
                        'unit_cost' => round($unit_cost,$price_decimal),
                        'cost_all' => round($cost_all,$price_decimal),
                        'profit' => round($profit,$price_decimal),
                        'profit_rate' => round($profit_rate,$price_decimal),
                    ];
                }
            }else{
                $lists=[];
            }

            $result = array("total" => sizeof($lists), "rows" => $lists);

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