<?php

namespace app\admin\controller\finance;

use app\admin\service\sys_manage\DeptService;
use support\Db;
use support\Request;

class PremisesRevenueController
{

    private $accountType = ['CASH', 'POINTS', 'CARD'];

    public function list(Request $request)
    {
        $page = $request->get('page', 1);
        $pageSize = $request->get('pageSize', 20);
        $premises = $request->get('premises', DeptService::getPremisesList());
        $premisesKeyValue = [];
        foreach($premises as $item){
            $premisesKeyValue[$item['key']] = $item['label'];
        }

        $month = date('Ym');
        $data = [];
        $statistics = ['income'=>0,'expend'=>0];
        foreach ($premises as $premise) {
            $premisesData = [];
            $premisesData['premises'] = $premise['label'];
            $premiseKey = intval($premise['key']);
            $premisesData['expend'] = $premisesData['income'] = [
                'card' => 0,
                'recharge' => 0,
                'goods' => 0,
                'service' => 0,
                'meals' => 0,
                'package' => 0,
                'amount' => 0
            ];
            $income = Db::table("data_income_{$month}")
                ->where('join_data_income_dept_id',$premiseKey)
                ->selectRaw("data_income_classify,SUM(data_income_amount) as data_income_amount")
                ->groupBy('data_income_classify')
                ->get();
            
            foreach($income as $item){
                if($item->data_income_classify == 'CARD'){
                    $premisesData['income']['card']+=$item->data_income_amount;
                }else if($item->data_income_classify == 'RECHARGE'){
                    $premisesData['income']['recharge']+=$item->data_income_amount;
                }else if($item->data_income_classify == 'GOODS'){
                    $premisesData['income']['goods']+=$item->data_income_amount;
                }else if(in_array($item->data_income_classify,['CHNMED','CHNNCD','SERVICE'])){
                    $premisesData['income']['service']+=$item->data_income_amount;
                }else if($item->data_income_classify == 'MEALS'){
                    $premisesData['income']['meals']+=$item->data_income_amount;
                }else if($item->data_income_classify == 'PACKAGE'){
                    $premisesData['income']['package']+=$item->data_income_amount;
                }
                $premisesData['income']['amount'] += $item->data_income_amount;
                $statistics['income'] += $premisesData['income']['amount'];
            }
            $expend = Db::table("data_expend_{$month}")
                ->where('join_data_expend_dept_id',$premiseKey)
                ->selectRaw("data_expend_classify,SUM(data_expend_amount) as data_expend_amount")
                ->groupBy('data_expend_classify')
                ->get();
            
            foreach($expend as $item){
                if($item->data_expend_classify == 'CARD'){
                    $premisesData['expend']['card']+=$item->data_expend_amount;
                }else if($item->data_expend_classify == 'RECHARGE'){
                    $premisesData['expend']['recharge']+=$item->data_expend_amount;
                }else if($item->data_expend_classify == 'GOODS'){
                    $premisesData['expend']['goods']+=$item->data_expend_amount;
                }else if(in_array($item->data_expend_classify,['CHNMED','CHNNCD','SERVICE'])){
                    $premisesData['expend']['service']+=$item->data_expend_amount;
                }else if($item->data_expend_classify == 'MEALS'){
                    $premisesData['expend']['meals']+=$item->data_expend_amount;
                }else if($item->data_expend_classify == 'PACKAGE'){
                    $premisesData['expend']['package']+=$item->data_expend_amount;
                }
                $premisesData['expend']['amount'] += $item->data_expend_amount;
                $statistics['expend'] += $premisesData['expend']['amount'];
            }

            $data[] = $premisesData;
        }

        return json_success('', ['rows'=>$data,'statistics'=>$statistics]);
    }
}