You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

217 lines
7.2 KiB

  1. <?php
  2. namespace common\models;
  3. use backend\modules\zzcs\models\DictType;
  4. use yii\db\ActiveRecord;
  5. use backend\modules\zzcs\models\BaseSupplierPurchase;
  6. use yii\data\ActiveDataProvider;
  7. use Yii;
  8. use common\models\zModel;
  9. /**
  10. * This is the model class for table "base_supplier".
  11. *
  12. * @property integer $ID
  13. * @property integer $MAIN_CORP_ID
  14. * @property integer $CREATE_USER_ID
  15. * @property string $CREATE_TIME
  16. * @property integer $UPDATE_USER_ID
  17. * @property string $UPDATE_TIME
  18. * @property integer $CANCEL_FLAG
  19. * @property integer $AREA_ID
  20. * @property integer $SUPPLIER_TYPE
  21. * @property string $SUPPLIER_NAME
  22. * @property string $SUPPLIER_CODE
  23. * @property integer $MANAGE_TYPE
  24. * @property string $COMPANY_NAME
  25. * @property string $ID_CARD
  26. * @property integer $IS_DISABLED
  27. * @property integer $SETT_TYPE
  28. * @property integer $SETT_FREQUENCY
  29. * @property string $ACCOUNT_BANK
  30. * @property string $ACCOUNT_NUM
  31. * @property string $ACCOUNT_NAME
  32. * @property string $SALES_MAN
  33. * @property integer $STATUS
  34. */
  35. class BaseSupplier extends zModel
  36. {
  37. const SALE_TYPE_DISTRIB = 177; //分销
  38. const SALE_TYPE_AGENCY = 277;//代理
  39. const SALE_TYPE_DIRECT = 312;//直营
  40. //佣金类别
  41. const COMMISION_TYPE_CHANNEL = 303; //渠道定义
  42. const COMMISION_TYPE_PRODUCT = 304; //产品定义
  43. //返佣类别
  44. const BACK_COMMISION_TYPE_FIXED = 306; //固定返佣
  45. //返佣方式
  46. const BACK_COMMISION_METHOD_SALE = 308; //按销售金额
  47. const BACK_COMMISION_METHOD_SETTLE = 309; //按结算金额
  48. public $product_type; //产品类型
  49. public $status; //状态
  50. public $index_supplier_name; //供应商名称
  51. public $TYPE = ''; //类型 车队等
  52. /**
  53. * @inheritdoc
  54. */
  55. public static function tableName()
  56. {
  57. return 'base_supplier';
  58. }
  59. /**
  60. * @inheritdoc
  61. */
  62. public function rules()
  63. {
  64. return [
  65. [['CREATE_USER_ID', 'UPDATE_USER_ID', 'CANCEL_FLAG', 'AREA_ID', 'SUPPLIER_TYPE', 'MANAGE_TYPE', 'IS_DISABLED', 'SETT_TYPE', 'SETT_FREQUENCY'], 'integer'],
  66. [['SUPPLIER_TYPE', 'SETT_TYPE', 'SETT_FREQUENCY'], 'required'],
  67. [['UPDATE_TIME', 'TYPE'], 'safe'],
  68. [['CREATE_TIME', 'ID_CARD', 'ACCOUNT_NUM'], 'string', 'max' => 20],
  69. [['SUPPLIER_NAME', 'SUPPLIER_CODE', 'COMPANY_NAME', 'ACCOUNT_BANK', 'ACCOUNT_NAME'], 'string', 'max' => 100],
  70. [['SALES_MAN'], 'string', 'max' => 30],
  71. [['SUPPLIER_NAME'], 'required', 'message' => '请填写供应商名称'],
  72. [['COMPANY_NAME'], 'required', 'message' => '请填写公司全称'],
  73. ];
  74. }
  75. /**
  76. * @inheritdoc
  77. */
  78. public function attributeLabels()
  79. {
  80. return [
  81. 'ID' => 'ID',
  82. 'CREATE_USER_ID' => 'Create User ID',
  83. 'CREATE_TIME' => 'Create Time',
  84. 'UPDATE_USER_ID' => 'Update User ID',
  85. 'UPDATE_TIME' => 'Update Time',
  86. 'CANCEL_FLAG' => 'Cancel Flag',
  87. 'AREA_ID' => 'Area ID',
  88. 'SUPPLIER_TYPE' => 'Supplier Type',
  89. 'TYPE' => '类型',
  90. 'SUPPLIER_NAME' => $this->SUPPLIER_TYPE == 187 ? '供应商名称' : ($this->SUPPLIER_TYPE == 301 ? '渠道商' : '名称'),
  91. 'SUPPLIER_CODE' => 'Supplier Code',
  92. 'MANAGE_TYPE' => 'MANAGE_TYPE',
  93. 'COMPANY_NAME' => '公司全称',
  94. 'ID_CARD' => 'Id Card',
  95. 'IS_DISABLED' => 'Is Disabled',
  96. 'SETT_TYPE' => '结算方式',
  97. 'SETT_FREQUENCY' => '结算周期',
  98. 'ACCOUNT_BANK' => 'Account Bank',
  99. 'ACCOUNT_NUM' => 'Account Num',
  100. 'ACCOUNT_NAME' => 'Account Name',
  101. 'SALES_MAN' => '业务员',
  102. 'SupplierType' => '类型'
  103. ];
  104. }
  105. //结算方式
  106. public function getSettleType()
  107. {
  108. return $this->hasOne(DictType::className(), ['ID' => 'SETT_TYPE'])->from(DictType::tableName(). ' as settleType');
  109. }
  110. //结算周期
  111. public function getSettleCycle()
  112. {
  113. return $this->hasOne(DictType::className(), ['ID' => 'SETT_FREQUENCY'])->from(DictType::tableName(). ' as settleCycle');
  114. }
  115. public function getBaseSupplierSale()
  116. {
  117. return $this -> hasMany(BaseSupplierSale::className(), ['SUPPLIER_ID' => 'ID']);
  118. }
  119. /**
  120. * User: wangxj
  121. *
  122. * 获取佣金
  123. *
  124. * @param $type integer
  125. * @param $amount integer
  126. *
  127. * @return integer
  128. */
  129. public function getCommission($type, $amount)
  130. {
  131. $model = BaseSupplierSale::findOne(['cancel_flag' => 0, 'SUPPLIER_ID' => $this->ID, 'PARENT_TYPE' => $type]);
  132. if ($model !== null && $model->COMMISION_FLAG == 1 && $model->COMMISION_TYPE == BaseSupplierSale::TYPE_CHANNEL) {
  133. if ($model->BACK_COMMISION_METHOD == BaseSupplierSale::METHOD_PRICE) {
  134. return $model->BACK_PERCENT * $amount;
  135. } else {
  136. return 0;
  137. }
  138. } else {
  139. return 0;
  140. }
  141. }
  142. /**
  143. * User: wangxj
  144. *
  145. * 返回销售规则
  146. *
  147. * @param $type
  148. * @return null|BaseSupplierSale
  149. */
  150. public function getCommissionRule($type)
  151. {
  152. $model = BaseSupplierSale::findOne(['cancel_flag' => 0, 'SUPPLIER_ID' => $this->ID, 'PARENT_TYPE' => $type]);
  153. if ($model !== null) {
  154. return $model;
  155. } else {
  156. return null;
  157. }
  158. }
  159. public function load($data, $formName = null)
  160. {
  161. $this->index_supplier_name = isset($data['depart_name']) ? $data['depart_name'] : '';
  162. $this->product_type = isset($data['product_type']) ? $data['product_type'] : 0;
  163. $this->status = isset($data['status']) ? $data['status'] : 0;
  164. return parent::load($data, $formName); // TODO: Change the autogenerated stub
  165. }
  166. public function getIndex()
  167. {
  168. $query = BaseSupplierPurchase::find();
  169. $query->leftJoin(BaseSupplier::tableName(), 'base_supplier.id=base_supplier_purchase.supplier_id');
  170. $query->andFilterWhere(['base_supplier.cancel_flag' => 0, 'base_supplier_purchase.cancel_flag' => 0, 'base_supplier.MAIN_CORP_ID' => $this->login_user->MAIN_CORP_ID2]);
  171. $query->orderBy('base_supplier.CREATE_TIME desc');
  172. if ($this->product_type != 0)
  173. $query->andFilterWhere(['PRODUCT_TYPE' => $this->product_type]);
  174. if ($this->status != 0)
  175. $query->andFilterWhere(['STATUS' => $this->status]);
  176. $query->andFilterWhere(['like', 'base_supplier.SUPPLIER_NAME', $this->index_supplier_name]);
  177. $query->groupBy('SUPPLIER_ID');
  178. $dataProvider = new ActiveDataProvider([
  179. 'query' => $query
  180. ]);
  181. return $dataProvider;
  182. }
  183. public function getdepart_status()
  184. {
  185. return $this->hasOne(DictType::className(), ['ID' => 'STATUS']);
  186. }
  187. public function beforeSave($insert)
  188. {
  189. if ($this->isNewRecord) {
  190. if ($this->MAIN_CORP_ID === null || $this->MAIN_CORP_ID == '') {
  191. $this->MAIN_CORP_ID = $this->login_user->MAIN_CORP_ID2;
  192. }
  193. $this->CANCEL_FLAG = 0;
  194. } else {
  195. $this->UPDATE_TIME = date('Y-m-d H:i:s', time());
  196. $this->MAIN_CORP_ID = $this->login_user->MAIN_CORP_ID2;
  197. }
  198. return parent::beforeSave($insert);
  199. }
  200. }