CalcBonus.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. <?php
  2. namespace common\models;
  3. use Yii;
  4. /**
  5. * This is the model class for table "{{%CALC_BONUS}}".
  6. *
  7. * @property string $ID
  8. * @property string $USER_ID 会员ID
  9. * @property string $LAST_USER_NAME 结算时会员编号
  10. * @property string $LAST_REAL_NAME 结算时会员姓名
  11. * @property string $LAST_DEC_LV 结算时会员级别
  12. * @property string $LAST_EMP_LV 结算时会员聘级
  13. * @property int $LAST_STATUS 结算时状态
  14. * @property string $LAST_MOBILE 结算时手机号码
  15. * @property int $LAST_PERIOD_AT 结算时加入期数
  16. * @property int $LAST_CREATED_AT 结算时加入时间
  17. * @property string $LAST_SUB_COM_ID 结算时所属分公司
  18. * @property int $LAST_PROVINCE 结算时所属省份
  19. * @property int $LAST_CITY 结算时所属城市
  20. * @property int $LAST_COUNTY 结算时所属地区
  21. * @property string $LAST_SYSTEM_ID 结算时体系
  22. * @property int $LAST_IS_DIRECT_SELLER 结算时是否直销员
  23. * @property string $LAST_REC_USER_NAME 结算时开拓者编号
  24. * @property string $LAST_REC_REAL_NAME 结算时开拓者姓名
  25. * @property string $LAST_CON_USER_NAME 结算时上级编号
  26. * @property string $LAST_CON_REAL_NAME 结算时上级姓名
  27. * @property int $LAST_LOCATION 结算时市场
  28. * @property string $BONUS_QY 区域津贴
  29. * @property string $BONUS_YC 育成津贴
  30. * @property string $BONUS_VIP VIP奖
  31. * @property string $BONUS_FX 复销奖
  32. * @property string $BONUS_LS 零售奖
  33. * @property string $BONUS_FW 服务奖
  34. * @property string $BONUS_BT 店补
  35. * @property string $BONUS_BT_PROD 产品补贴
  36. * @property string $BONUS_BT_TOOL 工具流补贴
  37. * @property string $DEDUCT_ZR 责任业绩扣除
  38. * @property string $BONUS_FL 形象店货补
  39. * @property string $BONUS_CF 车房养老奖
  40. * @property string $BONUS_LX 领袖分红
  41. * @property string $SHOULD_QY 应发区域津贴
  42. * @property string $SHOULD_DEDUCT_ZR 应扣责任业绩
  43. * @property string $PV_1L 一市场新增业绩
  44. * @property string $QY_1L 一市场区域业绩
  45. * @property string $SURPLUS_1L 一市场结余业绩
  46. * @property string $PV_2L 二市场新增业绩
  47. * @property string $QY_2L 二市场区域业绩
  48. * @property string $SURPLUS_2L 二市场结余业绩
  49. * @property string $PV_3L 三市场新增业绩
  50. * @property string $QY_3L 三市场区域业绩
  51. * @property string $SURPLUS_3L 三市场结余业绩
  52. * @property string $PV_4L 四市场新增业绩
  53. * @property string $QY_4L 四市场区域业绩
  54. * @property string $SURPLUS_4L 四市场结余业绩
  55. * @property string $PV_5L 五市场新增业绩
  56. * @property string $QY_5L 五市场区域业绩
  57. * @property string $SURPLUS_5L 五市场结余业绩
  58. * @property string $PV_PCS 当期零售
  59. * @property string $PV_LS_TOUCH 虚拟零售区新增业绩
  60. * @property string $SURPLUS_LS 虚拟零售区结余
  61. * @property string $QY_LS 虚拟零售区区域业绩
  62. * @property string $PV_TOUCH 新增业绩累计
  63. * @property string $BONUS_REAL 实际奖金
  64. * @property string $BONUS_TOTAL 总奖金
  65. * @property string $BONUS_INCOME 总收入
  66. * @property int $PERIOD_NUM 结算期数
  67. * @property int $CALC_YEAR 所在结算年
  68. * @property int $CALC_MONTH 所在结算月
  69. * @property int $CALCULATED_AT 结算时间
  70. * @property int $IS_SENT 是否挂网
  71. * @property int $SENT_AT 挂网时间
  72. * @property string $P_CALC_MONTH 表分区的日期索引
  73. * @property int $CREATED_AT 创建时间
  74. *
  75. * @property string $ORI_BONUS_QY 团队奖原奖金
  76. * @property string $ORI_BONUS_QY_BD 报单团队奖原奖金
  77. * @property string $ORI_BONUS_QY_FX 复消团队奖原奖金
  78. * @property string $ORI_BONUS_YC 荣衔奖奖原奖金
  79. * @property string $ORI_BONUS_VIP VIP奖奖原奖金
  80. * @property string $ORI_BONUS_STANDARD 达标奖原奖金
  81. * @property string $BONUS_BD 服务奖
  82. * @property string $ORI_BONUS_BD 服务奖原奖金
  83. * @property string $BONUS_TG 推广奖
  84. * @property string $ORI_BONUS_TG 推广奖原奖金
  85. * @property string $BONUS_XF 消费奖
  86. * @property string $ORI_BONUS_XF 消费奖原奖金
  87. * @property string $BONUS_YJ 业绩奖
  88. * @property string $ORI_BONUS_YJ 业绩奖原奖金
  89. * @property string $ORI_BONUS_YJ_BD 报单业绩奖原奖金
  90. * @property string $ORI_BONUS_YJ_FX 复消业绩奖原奖金
  91. * @property string $BONUS_GX 共享奖
  92. * @property string $ORI_BONUS_GX 共享奖原奖金
  93. * @property string $BONUS_BONUS_GX 实发共享奖奖金
  94. * @property string $BONUS_GL 管理奖
  95. * @property string $ORI_BONUS_GL 管理奖原奖金
  96. * @property string $BONUS_BONUS_GL 实发管理奖奖金
  97. * @property string $ORI_BONUS_GL_BD 报单管理奖原奖金
  98. * @property string $ORI_BONUS_GL_FX 复消管理奖原奖金
  99. * @property string $RECONSUME_POINTS 复消积分
  100. * @property string $MANAGE_TAX 管理费
  101. * @property string $BONUS_BS 蓝星奖金(即新的管理奖)
  102. * @property string $ORI_BONUS_BS 蓝星原奖金(即新的管理奖,原奖金)
  103. * @property string $BONUS_TOURISM 旅游奖奖金
  104. * @property string $BONUS_VILLA 房奖奖金
  105. * @property string $BONUS_GARAGE 车奖奖金
  106. */
  107. class CalcBonus extends \common\components\ActiveRecord
  108. {
  109. /**
  110. * {@inheritdoc}
  111. */
  112. public static function tableName()
  113. {
  114. return '{{%CALC_BONUS}}';
  115. }
  116. /**
  117. * {@inheritdoc}
  118. */
  119. public function rules()
  120. {
  121. return [
  122. [['USER_ID', 'LAST_USER_NAME', 'LAST_REAL_NAME', 'LAST_MOBILE', 'LAST_SYSTEM_ID','LAST_LOCATION', 'PERIOD_NUM', 'CALC_YEAR', 'CALC_MONTH', 'CALCULATED_AT', 'P_CALC_MONTH', 'CREATED_AT'], 'required'],
  123. [['LAST_STATUS', 'LAST_PERIOD_AT', 'LAST_CREATED_AT', 'LAST_PROVINCE', 'LAST_CITY', 'LAST_COUNTY', 'LAST_IS_DIRECT_SELLER', 'LAST_LOCATION', 'PERIOD_NUM', 'CALC_YEAR', 'CALC_MONTH', 'CALCULATED_AT', 'IS_SENT', 'SENT_AT', 'CREATED_AT'], 'integer'],
  124. [['ORI_BONUS_BS','EXCHANGE_POINTS','BONUS_BS','BONUS_QY', 'BONUS_YC', 'BONUS_VIP', 'BONUS_FX', 'BONUS_LS', 'BONUS_FW', 'BONUS_BT', 'BONUS_BT_PROD', 'BONUS_BT_TOOL', 'DEDUCT_ZR', 'BONUS_FL', 'BONUS_CF', 'BONUS_LX', 'SHOULD_QY', 'SHOULD_DEDUCT_ZR', 'PV_1L', 'QY_1L', 'SURPLUS_1L', 'PV_2L', 'QY_2L', 'SURPLUS_2L', 'PV_3L', 'QY_3L', 'SURPLUS_3L', 'PV_4L', 'QY_4L', 'SURPLUS_4L', 'PV_5L', 'QY_5L', 'SURPLUS_5L', 'PV_PCS', 'PV_LS_TOUCH', 'SURPLUS_LS', 'QY_LS', 'PV_TOUCH', 'BONUS_REAL', 'BONUS_TOTAL', 'BONUS_INCOME', 'ORI_BONUS_QY','ORI_BONUS_QY_BD','ORI_BONUS_QY_FX','ORI_BONUS_YC', 'ORI_BONUS_VIP','ORI_BONUS_STANDARD','BONUS_BD','ORI_BONUS_BD','BONUS_TG','ORI_BONUS_TG','BONUS_XF','ORI_BONUS_XF','BONUS_YJ','ORI_BONUS_YJ','ORI_BONUS_YJ_BD','ORI_BONUS_YJ_FX','BONUS_GX','ORI_BONUS_GX','REAL_BONUS_GX','BONUS_GL','ORI_BONUS_GL','REAL_BONUS_GL','ORI_BONUS_GL_BD','ORI_BONUS_GL_FX','RECONSUME_POINTS','MANAGE_TAX', 'BONUS_TOURISM', 'BONUS_GARAGE', 'BONUS_VILLA'], 'number'],
  125. [['ID', 'USER_ID', 'LAST_DEC_LV', 'LAST_EMP_LV', 'LAST_SUB_COM_ID', 'LAST_SYSTEM_ID'], 'string', 'max' => 32],
  126. [['LAST_USER_NAME', 'LAST_REC_USER_NAME', 'LAST_CON_USER_NAME'], 'string', 'max' => 16],
  127. [['LAST_REAL_NAME', 'LAST_REC_REAL_NAME', 'LAST_CON_REAL_NAME'], 'string', 'max' => 128],
  128. [['LAST_MOBILE'], 'string', 'max' => 11],
  129. [['P_CALC_MONTH'], 'safe'],
  130. [['ID'], 'unique'],
  131. ];
  132. }
  133. /**
  134. * {@inheritdoc}
  135. */
  136. public function attributeLabels()
  137. {
  138. return [
  139. 'ID' => 'ID',
  140. 'USER_ID' => '会员ID',
  141. 'LAST_USER_NAME' => '结算时会员编号',
  142. 'LAST_REAL_NAME' => '结算时会员姓名',
  143. 'LAST_DEC_LV' => '结算时会员级别',
  144. 'LAST_EMP_LV' => '结算时会员聘级',
  145. 'LAST_STATUS' => '结算时状态',
  146. 'LAST_MOBILE' => '结算时手机号码',
  147. 'LAST_PERIOD_AT' => '结算时加入期数',
  148. 'LAST_CREATED_AT' => '结算时加入时间',
  149. 'LAST_SUB_COM_ID' => '结算时所属分公司',
  150. 'LAST_PROVINCE' => '结算时所属省份',
  151. 'LAST_CITY' => '结算时所属城市',
  152. 'LAST_COUNTY' => '结算时所属地区',
  153. 'LAST_SYSTEM_ID' => '结算时体系',
  154. 'LAST_IS_DIRECT_SELLER' => '结算时是否直销员',
  155. 'LAST_REC_USER_NAME' => '结算时开拓者编号',
  156. 'LAST_REC_REAL_NAME' => '结算时开拓者姓名',
  157. 'LAST_CON_USER_NAME' => '结算时上级编号',
  158. 'LAST_CON_REAL_NAME' => '结算时上级姓名',
  159. 'LAST_LOCATION' => '结算时市场',
  160. 'BONUS_QY' => '区域津贴',
  161. 'BONUS_YC' => '荣衔奖',
  162. 'BONUS_VIP' => 'VIP奖',
  163. 'BONUS_FX' => '复销奖',
  164. 'BONUS_LS' => '零售奖',
  165. 'BONUS_FW' => '服务奖',
  166. 'BONUS_BT' => '店补',
  167. 'BONUS_BT_PROD' => '产品补贴',
  168. 'BONUS_BT_TOOL' => '工具流补贴',
  169. 'DEDUCT_ZR' => '责任业绩扣除',
  170. 'BONUS_FL' => '形象店货补',
  171. 'BONUS_CF' => '车房养老奖',
  172. 'BONUS_LX' => '领袖分红',
  173. 'SHOULD_QY' => '应发区域津贴',
  174. 'SHOULD_DEDUCT_ZR' => '应扣责任业绩',
  175. 'PV_1L' => '一市场新增业绩',
  176. 'QY_1L' => '一市场区域业绩',
  177. 'SURPLUS_1L' => '一市场结余业绩',
  178. 'PV_2L' => '二市场新增业绩',
  179. 'QY_2L' => '二市场区域业绩',
  180. 'SURPLUS_2L' => '二市场结余业绩',
  181. 'PV_3L' => '三市场新增业绩',
  182. 'QY_3L' => '三市场区域业绩',
  183. 'SURPLUS_3L' => '三市场结余业绩',
  184. 'PV_4L' => '四市场新增业绩',
  185. 'QY_4L' => '四市场区域业绩',
  186. 'SURPLUS_4L' => '四市场结余业绩',
  187. 'PV_5L' => '五市场新增业绩',
  188. 'QY_5L' => '五市场区域业绩',
  189. 'SURPLUS_5L' => '五市场结余业绩',
  190. 'PV_PCS' => '当期零售',
  191. 'PV_LS_TOUCH' => '虚拟零售区新增业绩',
  192. 'SURPLUS_LS' => '虚拟零售区结余',
  193. 'QY_LS' => '虚拟零售区区域业绩',
  194. 'PV_TOUCH' => '新增业绩累计',
  195. 'BONUS_REAL' => '实际奖金',
  196. 'BONUS_TOTAL' => '总奖金',
  197. 'BONUS_INCOME' => '总收入',
  198. 'PERIOD_NUM' => '结算期数',
  199. 'CALC_YEAR' => '所在结算年',
  200. 'CALC_MONTH' => '所在结算月',
  201. 'CALCULATED_AT' => '结算时间',
  202. 'IS_SENT' => '是否挂网',
  203. 'SENT_AT' => '挂网时间',
  204. 'P_CALC_MONTH' => '表分区的日期索引',
  205. 'CREATED_AT' => '创建时间',
  206. 'ORI_BONUS_QY'=> '团队奖原奖金',
  207. 'ORI_BONUS_QY_BD'=> '报单团队奖原奖金',
  208. 'ORI_BONUS_QY_FX'=> '复消团队奖原奖金',
  209. 'ORI_BONUS_YC'=> '荣衔奖原奖金',
  210. 'ORI_BONUS_VIP'=> 'VIP奖原奖金',
  211. 'ORI_BONUS_STANDARD'=> '达标奖原奖金',
  212. 'BONUS_BD'=> '服务奖',
  213. 'ORI_BONUS_BD'=> '服务奖原奖金',
  214. 'BONUS_TG'=> '推广奖',
  215. 'ORI_BONUS_TG'=> '推广奖原奖金',
  216. 'BONUS_XF'=> '消费奖',
  217. 'ORI_BONUS_XF'=> '消费奖原奖金',
  218. 'BONUS_YJ'=> '业绩奖',
  219. 'ORI_BONUS_YJ'=> '业绩奖原奖金',
  220. 'ORI_BONUS_YJ_BD'=> '报单业绩奖原奖金',
  221. 'ORI_BONUS_YJ_FX'=> '复消业绩奖原奖金',
  222. 'BONUS_GX'=> '共享奖',
  223. 'ORI_BONUS_GX'=> '共享奖原奖金',
  224. 'REAL_BONUS_GX'=> '实发共享奖奖金',
  225. 'BONUS_GL'=> '管理奖',
  226. 'ORI_BONUS_GL'=> '管理奖原奖金',
  227. 'REAL_BONUS_GL'=> '实发管理奖奖金',
  228. 'ORI_BONUS_GL_BD'=> '报单管理奖原奖金',
  229. 'ORI_BONUS_GL_FX'=> '复消管理奖原奖金',
  230. 'RECONSUME_POINTS'=> '复消积分',
  231. 'MANAGE_TAX'=> '管理费',
  232. 'EXCHANGE_POINTS' => '兑换积分',
  233. 'BONUS_BS' => '蓝星奖',
  234. 'ORI_BONUS_BS' => '蓝星原奖金',
  235. 'BONUS_BS_MNT' => '蓝星管理奖',
  236. 'ORI_BONUS_BS_MNT' => '蓝星管理奖原奖金',
  237. 'BONUS_BS_ABBR' => '蓝星业绩奖',
  238. 'ORI_BONUS_BS_ABBR' => '蓝星业绩奖原奖金',
  239. 'BONUS_TOURISM' => '旅游奖奖金',
  240. 'BONUS_GARAGE' => '车奖奖金',
  241. 'BONUS_VILLA' => '房奖奖金',
  242. ];
  243. }
  244. /**
  245. * 近几期的奖金
  246. * @param $userId
  247. * @param int $num
  248. * @return array
  249. * @throws \yii\db\Exception
  250. */
  251. public static function periodBonus($userId, $num = 7){
  252. // 获取近几期的年月
  253. $periodYearMonths = Period::getNearlyPeriodYearMonth($num);
  254. $allBonus = [];
  255. foreach($periodYearMonths as $data){
  256. $allBonus[] = static::findUseSlaves()->yearMonth($data['yearMonth'])->select('USER_ID,BONUS_QY, BONUS_YC, BONUS_FX, BONUS_LS, BONUS_FW, BONUS_BT, BONUS_BT_PROD, BONUS_BT_TOOL, DEDUCT_ZR, BONUS_FL, BONUS_CF, BONUS_LX, SHOULD_QY,BONUS_TOURISM,BONUS_GARAGE,BONUS_VILLA')->where('USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM AND IS_SENT=1', [':USER_ID'=>$userId, ':PERIOD_NUM'=>$data['periodNum']])->asArray()->one();
  257. }
  258. return $allBonus;
  259. }
  260. /**
  261. * 获取期数对应的奖金情况
  262. * @param $userId
  263. * @param $periodNum
  264. * @return array|null|\yii\db\ActiveRecord
  265. * @throws \yii\db\Exception
  266. */
  267. public static function periodNumBonus($userId, $periodNum){
  268. $period = Period::instance();
  269. $yearMonth = $period->getYearMonth($periodNum);
  270. return static::findUseSlaves()->yearMonth($yearMonth)->where('USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM', [':USER_ID'=>$userId, ':PERIOD_NUM'=>$periodNum])->select('USER_ID,BONUS_QY, BONUS_YC, BONUS_TG, BONUS_YJ, BONUS_GX, BONUS_GL, ORI_BONUS_QY, ORI_BONUS_YC, ORI_BONUS_TG, ORI_BONUS_YJ, ORI_BONUS_GX, ORI_BONUS_GL, RECONSUME_POINTS, MANAGE_TAX, BONUS_REAL, BONUS_TOTAL, BONUS_INCOME')->asArray()->one();
  271. }
  272. /**
  273. * 根据期数获取月累计奖金
  274. * @param $userId
  275. * @param $periodNum
  276. * @param string $type
  277. * @return int|mixed
  278. * @throws \yii\db\Exception
  279. */
  280. public static function monthBonus($userId, $periodNum=null, $type = 'BONUS_QY') {
  281. if($periodNum) {
  282. $period = Period::instance();
  283. $yearMonth = $period->getYearMonth($periodNum);
  284. if ($yearMonth) {
  285. $bonus = CalcBonus::findUseSlaves()->yearMonth($yearMonth)->select('SUM(' . $type . ') AS ' . $type . '_SUM')->where('USER_ID=:USER_ID AND CALC_MONTH=:CALC_MONTH AND IS_SENT=1', [':USER_ID' => $userId, ':CALC_MONTH' => $yearMonth])->asArray()->one();
  286. }
  287. }else{
  288. $bonus = CalcBonus::findUseSlaves()->select('SUM(' . $type . ') AS ' . $type . '_SUM')->where('USER_ID=:USER_ID AND IS_SENT=1', [':USER_ID' => $userId])->asArray()->one();
  289. }
  290. return $bonus[$type . '_SUM'] ?? 0;
  291. }
  292. }