SendCFAndLXForm.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. <?php
  2. namespace common\models\forms;
  3. use common\helpers\Date;
  4. use common\components\Model;
  5. use common\helpers\Form;
  6. use common\helpers\Tool;
  7. use common\helpers\user\Balance;
  8. use common\helpers\user\Info;
  9. use common\libs\logging\operate\AdminOperate;
  10. use common\models\CFLXAudit;
  11. use common\models\DealType;
  12. use common\models\UserBonus;
  13. use common\models\UserTeamwork;
  14. use yii\base\Exception;
  15. /**
  16. * Login form
  17. *
  18. * @private CFLXAudit $_model
  19. */
  20. class SendCFAndLXForm extends Model {
  21. public $id;
  22. //public $year;
  23. public $selectedIds;
  24. public $statusValue;
  25. public $sendType;
  26. public $remark;
  27. public $auditStatus;
  28. private $_model;
  29. const SEND_TYPES = ['all' => '车房养老奖和领袖分红奖', 'cf' => '车房养老奖', 'lx' => '领袖分红奖'];
  30. public function init() {
  31. parent::init();
  32. $this->adminOperateLogger = new AdminOperate([
  33. 'fetchClass' => UserBonus::class,
  34. ]);
  35. }
  36. /**
  37. * @inheritdoc
  38. */
  39. public function rules() {
  40. return [
  41. [['id', 'remark', 'auditStatus', 'selectedIds', 'sendType', 'statusValue'], 'trim'],
  42. [['id', 'auditStatus', 'remark', 'sendType', 'statusValue'], 'required'],
  43. [['sendType'], 'isType'],
  44. [['statusValue'], 'isStatusValue'],
  45. [['selectedIds'], 'isSelected'],
  46. [['auditStatus'], 'isAuditStatus'],
  47. ];
  48. }
  49. public function attributeLabels() {
  50. return [
  51. 'id' => '审核表ID',
  52. 'sendType' => '发放类型',
  53. 'statusValue' => '包含会员状态',
  54. //'year' => '发放年度',
  55. 'remark' => '备注',
  56. ];
  57. }
  58. /**
  59. * 指定校验场景
  60. * @return array
  61. */
  62. public function scenarios() {
  63. $parentScenarios = parent::scenarios();
  64. $customScenarios = [
  65. 'apply' => ['selectedIds', 'sendType', 'statusValue', 'remark'],
  66. 'audit' => ['selectedIds', 'auditStatus', 'remark'],
  67. ];
  68. return array_merge($parentScenarios, $customScenarios);
  69. }
  70. /**
  71. * 年份是否正确
  72. * @param $attribute
  73. */
  74. public function isYear($attribute) {
  75. if (CFLXAudit::find()->where('YEAR=:YEAR AND AUDIT_STATUS=:AUDIT_STATUS', [':YEAR' => $this->year, ':AUDIT_STATUS' => \Yii::$app->params['auditStatus']['true']['value']])->exists()) {
  76. $this->addError($attribute, '本年度已经发放');
  77. }
  78. }
  79. /**
  80. * 发放类型
  81. * @param $attribute
  82. */
  83. public function isType($attribute) {
  84. if (!in_array($this->sendType, array_keys(self::SEND_TYPES))) {
  85. $this->addError($attribute, '请选择正确的发放类型');
  86. }
  87. }
  88. /**
  89. * 状态类型校验
  90. * @param $attribute
  91. */
  92. public function isStatusValue($attribute) {
  93. foreach ($this->statusValue as $status) {
  94. if (!array_key_exists($status, \Yii::$app->params['userStatus'])) {
  95. $this->addError($attribute, '请选择正确的会员状态');
  96. break;
  97. }
  98. }
  99. }
  100. /**
  101. * 校验审核状态
  102. * @param $attributes
  103. */
  104. public function isAuditStatus($attributes){
  105. if (!array_key_exists($this->auditStatus, \Yii::$app->params['auditStatus'])) {
  106. $this->addError($attributes, '无效的审核状态');
  107. }
  108. }
  109. /**
  110. * 批量数据
  111. * @param $attributes
  112. */
  113. public function isSelected($attributes) {
  114. if (!$this->selectedIds) {
  115. $this->addError($attributes, 'A piece of data must be selected'); // 必须选择一条数据
  116. }
  117. if (!is_array($this->selectedIds)) {
  118. $this->selectedIds = [$this->selectedIds];
  119. }
  120. if($this->scenario=='apply') {
  121. $this->selectedIds = array_unique($this->selectedIds);
  122. //根据会员状态及审核状态筛选
  123. foreach ($this->selectedIds as $key => $value) {
  124. if ($status = Info::getStatusByUserId($value)) {
  125. if (!in_array($status, $this->statusValue)) {
  126. unset($this->selectedIds[$key]);
  127. }
  128. }
  129. if (CFLXAudit::find()->where('USER_ID=:USER_ID AND AUDIT_STATUS =:AUDIT_STATUS', [':USER_ID' => $value, ':AUDIT_STATUS' => \Yii::$app->params['auditStatus']['un']['value']])->exists()) {
  130. unset($this->selectedIds[$key]);
  131. }
  132. }
  133. $this->selectedIds = array_values($this->selectedIds);
  134. }
  135. }
  136. /**
  137. * 申请
  138. * @return CFLXAudit|null
  139. * @throws \yii\db\Exception
  140. */
  141. public function add() {
  142. if (!$this->validate()) {
  143. return null;
  144. }
  145. $db = \Yii::$app->db;
  146. $transaction = $db->beginTransaction();
  147. try {
  148. $model = $this->_model;
  149. $model->YEAR = Date::nowYear();
  150. $model->AUDIT_STATUS = \Yii::$app->params['auditStatus']['un']['value'];
  151. $model->CREATE_ADMIN = \Yii::$app->user->id;
  152. $model->CREATE_REMARK = $this->remark;
  153. $model->CREATED_AT = Date::nowTime();
  154. if (!$model->save()) {
  155. throw new Exception(Form::formatErrorsForApi($model->getErrors()));
  156. }
  157. $transaction->commit();
  158. } catch (Exception $e) {
  159. $transaction->rollBack();
  160. $this->addError('add', $e->getMessage());
  161. return null;
  162. }
  163. return $model;
  164. }
  165. /**
  166. * 批量发放车房领袖
  167. * @return array|null
  168. * @throws \yii\db\Exception
  169. */
  170. public function apply() {
  171. if (!$this->validate()) {
  172. return null;
  173. }
  174. $logs = [];
  175. $db = \Yii::$app->db;
  176. $transaction = $db->beginTransaction();
  177. try {
  178. foreach ($this->selectedIds as $select) {
  179. $model = new CFLXAudit();
  180. if ($this->sendType == 'lx') {
  181. if (!$oneData = UserBonus::findOneAsArray('LX>0 AND USER_ID=:USER_ID', [':USER_ID' => $select])) continue;
  182. $model->LX = $oneData['LX'];
  183. } elseif ($this->sendType == 'cf') {
  184. if (!$oneData = UserBonus::findOneAsArray('CF>0 AND USER_ID=:USER_ID', [':USER_ID' => $select])) continue;
  185. $model->CF = $oneData['CF'];
  186. } else {
  187. if (!$oneData = UserBonus::findOneAsArray('(CF>0 OR LX>0) AND USER_ID=:USER_ID', [':USER_ID' => $select])) continue;
  188. $model->LX = $oneData['LX'];
  189. $model->CF = $oneData['CF'];
  190. }
  191. $model->USER_ID = $select;
  192. $model->YEAR = Date::nowYear();
  193. $model->AUDIT_STATUS = \Yii::$app->params['auditStatus']['un']['value'];
  194. $model->CREATE_ADMIN = \Yii::$app->user->id;
  195. $model->CREATE_REMARK = $this->remark;
  196. $model->CREATED_AT = Date::nowTime();
  197. if (!$model->save()) {
  198. throw new Exception(Form::formatErrorsForApi($model->getErrors()));
  199. }
  200. /*if($model->LX>0){
  201. Balance::changeUserBonus($select, 'lx', -$model->LX, ['REMARK' => '年度领袖分红奖申请']);
  202. }
  203. if($model->CF>0){
  204. Balance::changeUserBonus($select, 'cf', -$model->CF, ['REMARK' => '年度车房养老奖申请']);
  205. }*/
  206. $logs[] = $model->ID;
  207. }
  208. $transaction->commit();
  209. } catch (Exception $e) {
  210. $transaction->rollBack();
  211. $this->addError('apply', $e->getMessage());
  212. return null;
  213. }
  214. $this->adminOperateLogger->fetchClass = CFLXAudit::class;
  215. $this->adminOperateLogger->setIsBatch(true)->afterUpdate($logs, 'ID');
  216. $this->adminOperateLogger->clean()->save([
  217. 'optType' => '年度奖申请发放',
  218. 'remark' => $this->remark,
  219. ]);
  220. return ['logs' => $logs, 'typeName' => self::SEND_TYPES[$this->sendType]];
  221. }
  222. /**
  223. * 审核
  224. * @return array|null
  225. * @throws \yii\db\Exception
  226. */
  227. public function audit() {
  228. if (!$this->validate()) {
  229. return null;
  230. }
  231. $logs = [];
  232. $uids=[];
  233. if($this->auditStatus=='true'){
  234. foreach ($this->selectedIds as $select) {
  235. $oneBalanceAudit = CFLXAudit::findOneAsArray('AUDIT_STATUS=0 AND ID=:ID',[':ID'=>$select],'USER_ID');
  236. $uids[]=$oneBalanceAudit['USER_ID'];
  237. }
  238. }
  239. if($this->auditStatus=='true'){
  240. $this->adminOperateLogger->fetchClass = UserBonus::class;
  241. $this->adminOperateLogger->setIsBatch(true)->beforeUpdate($uids, 'USER_ID');
  242. }else{
  243. $this->adminOperateLogger->fetchClass = CFLXAudit::class;
  244. $this->adminOperateLogger->setIsBatch(true)->beforeUpdate($this->selectedIds, 'ID');
  245. }
  246. $db = \Yii::$app->db;
  247. $transaction = $db->beginTransaction();
  248. try {
  249. foreach ($this->selectedIds as $key => $select) {
  250. $model=CFLXAudit::findOne(['ID'=>$select]);
  251. $model->AUDIT_STATUS = \Yii::$app->params['auditStatus'][$this->auditStatus]['value'];
  252. $model->CREATE_REMARK = $this->remark?$this->remark:$model->CREATE_REMARK;
  253. $model->AUDIT_ADMIN = \Yii::$app->user->id;
  254. $model->AUDITED_AT = Date::nowTime();
  255. if ($this->auditStatus == 'true') {
  256. if ($model->CF > 0) {
  257. Balance::changeUserBonus($model->USER_ID, 'cf', -$model->CF, ['REMARK' => '车房养老转出至会员账户', 'DEAL_TYPE_ID' => DealType::CF_TRANSFER_OUT]);
  258. Balance::changeUserBonus($model->USER_ID, 'bonus', $model->CF, ['SORT' => $key *10, 'REMARK' => '车房养老转入至会员账户', 'DEAL_TYPE_ID' => DealType::CF_TRANSFER_IN]);
  259. $this->_teamworkBonus($model->USER_ID, $model->CF, 'cf', $key);
  260. }
  261. if ($model->LX > 0) {
  262. Balance::changeUserBonus($model->USER_ID, 'lx', -$model->LX, ['REMARK' => '领袖分红转出至会员账户', 'DEAL_TYPE_ID' => DealType::LX_TRANSFER_OUT]);
  263. Balance::changeUserBonus($model->USER_ID, 'bonus', $model->LX, ['SORT' => $key *10 + 5, 'REMARK' => '领袖分红转入至会员账户', 'DEAL_TYPE_ID' => DealType::LX_TRANSFER_IN]);
  264. $this->_teamworkBonus($model->USER_ID, $model->LX, 'lx', $key);
  265. }
  266. }
  267. if (!$model->save()) {
  268. throw new Exception(Form::formatErrorsForApi($model->getErrors()));
  269. }
  270. $logs[] = $model->USER_ID;
  271. }
  272. $transaction->commit();
  273. } catch (Exception $e) {
  274. $transaction->rollBack();
  275. $this->addError('audit', $e->getMessage());
  276. return null;
  277. }
  278. if($this->auditStatus=='true'){
  279. $this->adminOperateLogger->fetchClass = UserBonus::class;
  280. $this->adminOperateLogger->setIsBatch(true)->afterUpdate($uids, 'USER_ID')->clean()->save([
  281. 'optType' => '审核年度奖申请',
  282. 'remark' => $this->remark,
  283. ]);
  284. }else{
  285. $this->adminOperateLogger->fetchClass = CFLXAudit::class;
  286. $this->adminOperateLogger->setIsBatch(true)->afterUpdate($this->selectedIds, 'ID')->clean()->save([
  287. 'optType' => '审核年度奖申请',
  288. 'remark' => $this->remark,
  289. ]);
  290. }
  291. return ['logs' => $logs];
  292. }
  293. /**
  294. * 点位合作时转账
  295. * @param $userId
  296. * @param $amount
  297. * @param null $type
  298. * @return bool
  299. * @throws Exception
  300. * @throws \yii\db\Exception
  301. */
  302. private function _teamworkBonus($userId, $amount, $type = null, $key) {
  303. if (!$teamwork = UserTeamwork::findAllAsArray('USER_ID!=:MAIN_UID AND MAIN_UID=:MAIN_UID AND IS_DEL=0', [':MAIN_UID' => $userId], 'USER_ID,DIVIDE_PERCENT')) return false;
  304. $fromUserName = Info::getUserNameByUserId($userId);
  305. foreach ($teamwork as $value) {
  306. $bonus = Tool::formatPrice($amount * $value['DIVIDE_PERCENT'] * 0.01);
  307. if ($bonus <= 0) continue;
  308. $toUserName = Info::getUserNameByUserId($value['USER_ID']);
  309. if ($type == 'cf') {
  310. Balance::changeUserBonus($userId, 'bonus', -abs($bonus), ['SORT' => $key*10+1, 'DEAL_TYPE_ID' => DealType::TEAMWORK_TRANSFER_OUT, 'REMARK' => 'To:' . $toUserName . ' Per:' . $value['DIVIDE_PERCENT'] . '%']);
  311. Balance::changeUserBonus($value['USER_ID'], 'bonus', abs($bonus), ['SORT' => $key*10+1, 'DEAL_TYPE_ID' => DealType::TEAMWORK_TRANSFER_IN, 'REMARK' => 'From:' . $fromUserName . ' Per' . $value['DIVIDE_PERCENT'] . '%']);
  312. } elseif ($type == 'lx') {
  313. Balance::changeUserBonus($userId, 'bonus', -abs($bonus), ['SORT' => $key*10+2, 'DEAL_TYPE_ID' => DealType::TEAMWORK_TRANSFER_OUT, 'REMARK' => 'To:' . $toUserName . ' Per' . $value['DIVIDE_PERCENT'] . '%']);
  314. Balance::changeUserBonus($value['USER_ID'], 'bonus', abs($bonus), ['SORT' => $key*10+2, 'DEAL_TYPE_ID' => DealType::TEAMWORK_TRANSFER_IN, 'REMARK' => 'From:' . $fromUserName . ' Per' . $value['DIVIDE_PERCENT'] . '%']);
  315. }
  316. }
  317. }
  318. /**
  319. * 删除前
  320. * @param $selected
  321. */
  322. public function beforeDelete($selected) {
  323. $this->adminOperateLogger->fetchClass = CFLXAudit::class;
  324. $this->adminOperateLogger->setIsBatch(true)->beforeDelete($selected, 'ID');
  325. }
  326. /**
  327. * 删除
  328. * @param $selected
  329. * @throws Exception
  330. */
  331. public function delete($selected) {
  332. $this->adminOperateLogger->clean()->save([
  333. 'optType' => '删除年度奖申请',
  334. ]);
  335. }
  336. }