|
- <?php
-
- namespace common\models;
-
- use backend\modules\zzcs\models\DictType;
- use yii\db\ActiveRecord;
- use backend\modules\zzcs\models\BaseSupplierPurchase;
- use yii\data\ActiveDataProvider;
- use Yii;
- use common\models\zModel;
-
- /**
- * This is the model class for table "base_supplier".
- *
- * @property integer $ID
- * @property integer $MAIN_CORP_ID
- * @property integer $CREATE_USER_ID
- * @property string $CREATE_TIME
- * @property integer $UPDATE_USER_ID
- * @property string $UPDATE_TIME
- * @property integer $CANCEL_FLAG
- * @property integer $AREA_ID
- * @property integer $SUPPLIER_TYPE
- * @property string $SUPPLIER_NAME
- * @property string $SUPPLIER_CODE
- * @property integer $MANAGE_TYPE
- * @property string $COMPANY_NAME
- * @property string $ID_CARD
- * @property integer $IS_DISABLED
- * @property integer $SETT_TYPE
- * @property integer $SETT_FREQUENCY
- * @property string $ACCOUNT_BANK
- * @property string $ACCOUNT_NUM
- * @property string $ACCOUNT_NAME
- * @property string $SALES_MAN
- * @property integer $STATUS
- */
- class BaseSupplier extends zModel
- {
- const SALE_TYPE_DISTRIB = 177; //分销
- const SALE_TYPE_AGENCY = 277;//代理
- const SALE_TYPE_DIRECT = 312;//直营
- //佣金类别
- const COMMISION_TYPE_CHANNEL = 303; //渠道定义
- const COMMISION_TYPE_PRODUCT = 304; //产品定义
- //返佣类别
- const BACK_COMMISION_TYPE_FIXED = 306; //固定返佣
- //返佣方式
- const BACK_COMMISION_METHOD_SALE = 308; //按销售金额
- const BACK_COMMISION_METHOD_SETTLE = 309; //按结算金额
-
- public $product_type; //产品类型
- public $status; //状态
- public $index_supplier_name; //供应商名称
- public $TYPE = ''; //类型 车队等
-
- /**
- * @inheritdoc
- */
- public static function tableName()
- {
- return 'base_supplier';
- }
-
- /**
- * @inheritdoc
- */
- public function rules()
- {
- return [
- [['CREATE_USER_ID', 'UPDATE_USER_ID', 'CANCEL_FLAG', 'AREA_ID', 'SUPPLIER_TYPE', 'MANAGE_TYPE', 'IS_DISABLED', 'SETT_TYPE', 'SETT_FREQUENCY'], 'integer'],
- [['SUPPLIER_TYPE', 'SETT_TYPE', 'SETT_FREQUENCY'], 'required'],
- [['UPDATE_TIME', 'TYPE'], 'safe'],
- [['CREATE_TIME', 'ID_CARD', 'ACCOUNT_NUM'], 'string', 'max' => 20],
- [['SUPPLIER_NAME', 'SUPPLIER_CODE', 'COMPANY_NAME', 'ACCOUNT_BANK', 'ACCOUNT_NAME'], 'string', 'max' => 100],
- [['SALES_MAN'], 'string', 'max' => 30],
- [['SUPPLIER_NAME'], 'required', 'message' => '请填写供应商名称'],
- [['COMPANY_NAME'], 'required', 'message' => '请填写公司全称'],
- ];
- }
-
- /**
- * @inheritdoc
- */
- public function attributeLabels()
- {
- return [
- 'ID' => 'ID',
- 'CREATE_USER_ID' => 'Create User ID',
- 'CREATE_TIME' => 'Create Time',
- 'UPDATE_USER_ID' => 'Update User ID',
- 'UPDATE_TIME' => 'Update Time',
- 'CANCEL_FLAG' => 'Cancel Flag',
- 'AREA_ID' => 'Area ID',
- 'SUPPLIER_TYPE' => 'Supplier Type',
- 'TYPE' => '类型',
- 'SUPPLIER_NAME' => $this->SUPPLIER_TYPE == 187 ? '供应商名称' : ($this->SUPPLIER_TYPE == 301 ? '渠道商' : '名称'),
- 'SUPPLIER_CODE' => 'Supplier Code',
- 'MANAGE_TYPE' => 'MANAGE_TYPE',
- 'COMPANY_NAME' => '公司全称',
- 'ID_CARD' => 'Id Card',
- 'IS_DISABLED' => 'Is Disabled',
- 'SETT_TYPE' => '结算方式',
- 'SETT_FREQUENCY' => '结算周期',
- 'ACCOUNT_BANK' => 'Account Bank',
- 'ACCOUNT_NUM' => 'Account Num',
- 'ACCOUNT_NAME' => 'Account Name',
- 'SALES_MAN' => '业务员',
- 'SupplierType' => '类型'
- ];
- }
-
- //结算方式
- public function getSettleType()
- {
- return $this->hasOne(DictType::className(), ['ID' => 'SETT_TYPE'])->from(DictType::tableName(). ' as settleType');
- }
-
- //结算周期
- public function getSettleCycle()
- {
- return $this->hasOne(DictType::className(), ['ID' => 'SETT_FREQUENCY'])->from(DictType::tableName(). ' as settleCycle');
- }
-
- public function getBaseSupplierSale()
- {
- return $this -> hasMany(BaseSupplierSale::className(), ['SUPPLIER_ID' => 'ID']);
- }
- /**
- * User: wangxj
- *
- * 获取佣金
- *
- * @param $type integer
- * @param $amount integer
- *
- * @return integer
- */
- public function getCommission($type, $amount)
- {
- $model = BaseSupplierSale::findOne(['cancel_flag' => 0, 'SUPPLIER_ID' => $this->ID, 'PARENT_TYPE' => $type]);
- if ($model !== null && $model->COMMISION_FLAG == 1 && $model->COMMISION_TYPE == BaseSupplierSale::TYPE_CHANNEL) {
- if ($model->BACK_COMMISION_METHOD == BaseSupplierSale::METHOD_PRICE) {
- return $model->BACK_PERCENT * $amount;
- } else {
- return 0;
- }
-
- } else {
- return 0;
- }
- }
-
- /**
- * User: wangxj
- *
- * 返回销售规则
- *
- * @param $type
- * @return null|BaseSupplierSale
- */
- public function getCommissionRule($type)
- {
- $model = BaseSupplierSale::findOne(['cancel_flag' => 0, 'SUPPLIER_ID' => $this->ID, 'PARENT_TYPE' => $type]);
- if ($model !== null) {
- return $model;
- } else {
- return null;
- }
- }
-
- public function load($data, $formName = null)
- {
- $this->index_supplier_name = isset($data['depart_name']) ? $data['depart_name'] : '';
- $this->product_type = isset($data['product_type']) ? $data['product_type'] : 0;
- $this->status = isset($data['status']) ? $data['status'] : 0;
- return parent::load($data, $formName); // TODO: Change the autogenerated stub
- }
-
- public function getIndex()
- {
- $query = BaseSupplierPurchase::find();
- $query->leftJoin(BaseSupplier::tableName(), 'base_supplier.id=base_supplier_purchase.supplier_id');
- $query->andFilterWhere(['base_supplier.cancel_flag' => 0, 'base_supplier_purchase.cancel_flag' => 0, 'base_supplier.MAIN_CORP_ID' => $this->login_user->MAIN_CORP_ID2]);
- $query->orderBy('base_supplier.CREATE_TIME desc');
- if ($this->product_type != 0)
- $query->andFilterWhere(['PRODUCT_TYPE' => $this->product_type]);
- if ($this->status != 0)
- $query->andFilterWhere(['STATUS' => $this->status]);
- $query->andFilterWhere(['like', 'base_supplier.SUPPLIER_NAME', $this->index_supplier_name]);
- $query->groupBy('SUPPLIER_ID');
- $dataProvider = new ActiveDataProvider([
- 'query' => $query
- ]);
- return $dataProvider;
- }
-
- public function getdepart_status()
- {
- return $this->hasOne(DictType::className(), ['ID' => 'STATUS']);
- }
-
- public function beforeSave($insert)
- {
- if ($this->isNewRecord) {
- if ($this->MAIN_CORP_ID === null || $this->MAIN_CORP_ID == '') {
- $this->MAIN_CORP_ID = $this->login_user->MAIN_CORP_ID2;
- }
- $this->CANCEL_FLAG = 0;
- } else {
- $this->UPDATE_TIME = date('Y-m-d H:i:s', time());
- $this->MAIN_CORP_ID = $this->login_user->MAIN_CORP_ID2;
- }
- return parent::beforeSave($insert);
- }
- }
|