<?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\Plan;
use home\modules\contract\models\Contract;
use yii\data\ActiveDataProvider;
use home\modules\project\models\Project;
use yii\helpers\ArrayHelper;
use common\components\Tool;

/**
 * 回款计划控制器
 * 
 * @author Strief http://blog.u-bo.com
 * @since 0.1
 */
class PlanController 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 = Plan::find();
        $query->orderBy([
            'plan_id' => SORT_DESC,
        ]);

        //搜索
        $request = Yii::$app->request;
        $keywords = trim($request->get('keywords'));
        $status = $request->get('status');
        $field = $request->get('search_field');
        $prompt = $request->get('prompt');
        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 ($status != '') {
            $query->andwhere(['status' => (int) $status]);
        }
        if ($prompt != '') {
            $query->andWhere(['prompt' => (int) $prompt]);
        }//$query->createCommand()->sql;

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

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

    /**
     * 详情
     */
    public function actionDetail()
    {
        $request = Yii::$app->request;
        $id = (int) $request->get('id');
        //当从合同详情页查看计划详情时，传递的参数，以作区别
        $type = $request->get('type');
        $model = Plan::findOne(['plan_id' => $id]);
        if (empty($model)) {
            Yii::$app->getSession()->setFlash('error', '该回款计划不存在');
            return $this->flash();
        }
        $contract = Contract::findOne(['contract_no' => $model->contract_no]);
        $render = '';
        $render = empty($type) ? 'detail' : 'contract-detail';
        if (!empty($type)) {
            $this->layout = "//modal";
        }
        return $this->render($render, [
                'model' => $model,
                'contract' => $contract,
        ]);
    }

    /**
     * 获取合同对应的项目以及客户
     */
    public function actionAjax()
    {
        $contract_no = Yii::$app->request->post('contract_no');
        $contract = Contract::findOne(['contract_no' => $contract_no]);

        $json['project_no'] = isset($contract->project) ? $contract->project['name'] : '';
        $json['customer_name'] = isset($contract->customer) ? $contract->customer['name'] : '';

        return \Yii::createObject([
                'class' => 'yii\web\Response',
                'format' => \yii\web\Response::FORMAT_JSON,
                'data' => $json,
        ]);
    }
}