|
- <?php
-
- namespace backend\modules\motorcade\models;
-
- use backend\modules\api\models\BaseUser;
- use backend\modules\zzcs\models\DictType;
- use Yii;
- use yii\data\ActiveDataProvider;
- use common\models\zModel;
-
- /**
- * This is the model class for table "bus_department".
- *
- * @property integer $ID
- * @property integer $cancel_flag
- * @property integer $create_user_id
- * @property string $create_time
- * @property string $depart_name
- * @property integer $main_cooperation_id
- * @property integer $depart_type
- * @property integer $settle_type
- * @property integer $settle_cycle
- * @property integer $sales_man_id
- * @property int $status
- */
- class BusDepartment extends zModel
- {
- /**
- * @inheritdoc
- */
- public static function tableName()
- {
- return 'bus_department';
- }
-
- /**
- * @inheritdoc
- */
- public function rules()
- {
- return [
- [['depart_type', 'settle_type', 'settle_cycle'], 'required'],
- [['depart_type', 'settle_type', 'settle_cycle'], 'required', 'on' => ['add_customer']],
- [['cancel_flag', 'create_user_id', 'main_cooperation_id', 'depart_type', 'settle_type', 'settle_cycle'], 'integer'],
- [['create_time'], 'safe'],
- [['depart_name'], 'string', 'max' => 255],
- [['depart_name'], 'unique', 'targetAttribute' => ['depart_name', 'cancel_flag', 'main_cooperation_id'], 'message' => '用车单位已经存在!'],
- [['depart_name'], 'required', 'message' => '请填写客户名称', 'on' => ['add_customer']],
- [['company_name'], 'required', 'message' => '请填写公司全称', 'on' => ['add_customer']],
- [['sales_man_id'], 'required', 'message' => '请选择业务员', 'on' => ['add_customer']],
- ];
- }
-
- /**
- * 自定义规则:检查当前运营主体下有没有有效的(cancel_flag=0)的同名用车单位
- * User: Steven
- * @param $attribute
- * @return bool
- */
- /*public function uniqueBySpecial($attribute)
- {
- $res = BusDepartment::findOne([
- 'depart_name' => $this->depart_name,
- 'cancel_flag' => 0,
- 'main_cooperation_id' => Yii::$app->user->identity->MAIN_CORP_ID2
- ]);
- if ($res != null) {
- $this->addError($attribute, '用车单位已经存在!');
- return false;
- } else {
- return true;
- }
- }*/
-
- /**
- * @inheritdoc
- */
- public function attributeLabels()
- {
- return [
- 'ID' => 'ID',
- 'cancel_flag' => 'Cancel Flag',
- 'create_user_id' => 'Create User ID',
- 'create_time' => 'Create Time',
- 'depart_name' => '客户名称',
- 'company_name' => '公司全称',
- 'main_cooperation_id' => '运营主体ID',
- 'depart_type' => '类型',
- 'settle_type' => '结算方式', //(授信 275,预付 288,单结 292)
- 'settle_cycle' => '结算周期', //(293:日结 294:周结 295 月结)
- 'sales_man_id' => '业务员',
- 'departStatus.TYPE_NAME' => '状态',
- 'departType.TYPE_NAME' => '类型'
- ];
- }
-
- public function beforeSave($insert)
- {
- if ($this->isNewRecord) {
- $this->create_time = date('Y-m-d H:i:s', time());
- $this->create_user_id = Yii::$app->user->id;
- $this->main_cooperation_id = Yii::$app->user->identity->MAIN_CORP_ID2;
- $this->cancel_flag = 0;
- }
- return parent::beforeSave($insert);
- }
-
-
- //结算方式
- public function getSettleType()
- {
- return $this->hasOne(DictType::className(), ['ID' => 'settle_type'])->from(DictType::tableName() . ' as settleType');
- }
-
- //结算周期
- public function getSettleCycle()
- {
- return $this->hasOne(DictType::className(), ['ID' => 'settle_cycle'])->from(DictType::tableName() . ' as settleCycle');
- }
-
- public function getDepartType()
- {
- return $this->hasOne(DictType::className(), ['ID' => 'depart_type']);
- }
-
- public function getDepartStatus()
- {
- return $this->hasOne(DictType::className(), ['ID' => 'status']);
- }
-
- public function getSalesName()
- {
- return $this->hasOne(BaseUser::className(), ['ID' => 'sales_man_id']);
- }
-
- /*public function load($data, $formName = null)
- {
- $this->depart_name = isset($data['depart_name']) ? $data['depart_name'] : '';
- $this->depart_type = isset($data['depart_type']) ? $data['depart_type'] : 0;
- $this->sales_man_id = isset($data['sales_man_id']) ? $data['sales_man_id'] : 0;
- $this->status = isset($data['status']) ? $data['status'] : 0;
- return parent::load($data, $formName); // TODO: Change the autogenerated stub
- }*/
-
- public function getIndex()
- {
- $data = Yii::$app->request->get();
- $this->depart_name = isset($data['depart_name']) ? $data['depart_name'] : '';
- $this->depart_type = isset($data['depart_type']) ? $data['depart_type'] : 0;
- $this->sales_man_id = isset($data['sales_man_id']) ? $data['sales_man_id'] : 0;
- $this->status = isset($data['status']) ? $data['status'] : 0;
- $query = BusDepartment::find();
- $query->andFilterWhere(['cancel_flag' => 0, 'main_cooperation_id' => Yii::$app->user->identity->MAIN_CORP_ID2]);
- $query->andFilterWhere(['like', 'depart_name', $this->depart_name]);
- $query->orderBy('create_time desc');
- if ($this->depart_type != 0)
- $query->andFilterWhere(['depart_type' => $this->depart_type]);
- if ($this->sales_man_id != 0)
- $query->andFilterWhere(['sales_man_id' => $this->sales_man_id]);
- if ($this->status != 0)
- $query->andFilterWhere(['status' => $this->status]);
- $dataProvider = new ActiveDataProvider([
- 'query' => $query
- ]);
- return $dataProvider;
- }
-
- public static function getSales()
- {
- $sql = "select sales_man_id,base_user.TRUE_NAME from bus_department inner join base_user on bus_department.sales_man_id = base_user.ID where bus_department.main_cooperation_id = " . Yii::$app->user->identity->MAIN_CORP_ID2 . " group by sales_man_id";
- $data = Yii::$app->db->createCommand($sql)->queryAll();
- return $data;
- }
-
-
- //全部用车单位、客户
- public static function getList($type = '')
- {
- if ($type == 'array')
- return BusDepartment::find()->where(['cancel_flag' => 0, 'main_cooperation_id' => Yii::$app->user->identity->MAIN_CORP_ID2])->asArray()->all();
- else
- return BusDepartment::find()->where(['cancel_flag' => 0, 'main_cooperation_id' => Yii::$app->user->identity->MAIN_CORP_ID2])->all();
- }
-
- }
|