<?php
/**
 * @link      http://www.u-bo.com
 * @copyright 南京友博网络科技有限公司 
 * @license   http://www.u-bo.com/license/
 */

namespace home\modules\receivable\controllers;

use Yii;
use common\models\Common;
use common\models\PermissionScope;
use home\modules\receivable\models\Billing;
use yii\data\ActiveDataProvider;
use yii\helpers\ArrayHelper;
use home\modules\contract\models\Contract;

/**
 * 开票记录控制器
 * 
 * @author Strief http://blog.u-bo.com
 * @since 0.1
 */
class BillingController extends \home\controllers\Controller
{
    public function actions()
    {
        $actions = parent::actions();
        unset($actions['index']);
        $actions['create']['layout'] = '//modal';
        $actions['view']['layout'] = '//modal';
        $actions['update']['layout'] = '//modal';
        return $actions;
    }

    public function actionIndex()
    {
        $query = Billing::find();
        $query->orderBy([
            'record_id' => SORT_DESC,
        ]);

        //搜索
        $request = Yii::$app->request;
        $keywords = trim($request->get('keywords'));
        $field = $request->get('search_field');
        $category_id = $request->get('category_id');
        if (!empty($keywords) && !empty($field)) {
            if ($field != 'contract_name') {
                $query->andwhere(['like', $field, $keywords]);    
            } else {
                $contracts = Contract::find()->where(['like', 'name', $keywords])->select('contract_no')->asArray()->all();
                $contractsArray = ArrayHelper::getColumn($contracts, 'contract_no');
                $query->where(['in', 'contract_no', $contractsArray]);
            }
        }
        $listContractNos = [];
        $listContractNos = Contract::getMyContract();
        if ($listContractNos !== FALSE && is_array($listContractNos)) {
            $query->andWhere(['in', 'contract_no', $listContractNos]);
        }
        if (!empty($category_id)) {
            $query->andWhere("FIND_IN_SET(category_id,:ids)", [':ids' => $category_id]);
        }

        $provider = new ActiveDataProvider([
            'query' => $query,
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);

        return $this->render('index',
                [
                'get' => $request->get(),
                'model' => new Billing(),
                'models' => $provider->models,
                'pages' => $provider->pagination,
        ]);
    }

    /**
     * 详情
     */
    public function actionDetail()
    {
        $request = Yii::$app->request;
        $id = (int) $request->get('id');
        $type = $request->get('type');
        $model = Billing::findOne(['record_id' => $id]);
        if (empty($model)) {
            Yii::$app->getSession()->setFlash('error', '该开票记录不存在');
            return $this->flash();
        }
        $render = '';
        $render = empty($type)?'detail' : 'contract-detail' ;
        if (!empty($type)) {
            $this->layout = "//modal";
        }
        return $this->render($render, [
            'model' => $model,
        ]);
    }

    /**
     * 获取合同对应的项目
     */
    public function actionAjax(){
        $contract_no = Yii::$app->request->post('contract_no');
        $contract = Contract::findOne(['contract_no' => $contract_no]);
        if (isset($contract->project)) {
            $json['project_no'] = $contract->project['name'];
        } else {
            $json['project_no'] = '无';
        }
        return \Yii::createObject([
                'class' => 'yii\web\Response',
                'format' => \yii\web\Response::FORMAT_JSON,
                'data' => $json,
            ]);
    }
}
