Period.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. <?php
  2. namespace common\models;
  3. use common\helpers\Date;
  4. use common\helpers\Tool;
  5. use Yii;
  6. use yii\db\Exception;
  7. /**
  8. * This is the model class for table "{{%PERIOD}}".
  9. *
  10. * @property string $ID
  11. * @property int $PERIOD_NUM 期数
  12. * @property int $CALC_MONTH 所在结算月
  13. * @property int $CALC_YEAR 所在结算年
  14. * @property int $START_TIME 期数开始时间戳
  15. * @property int $END_TIME 期数结束时间戳
  16. * @property int $IS_MONTH 是否结算月节点
  17. * @property int $IS_YEAR 是否结算年节点
  18. * @property int $IS_CLOSED 是否已封期
  19. * @property int $IS_PERFED 是否已生成业绩单
  20. * @property int $IS_CALCULATED 是否已结算
  21. * @property int $IS_SENT 是否已发放
  22. * @property int $IS_PERFING 是否正在生成业绩单
  23. * @property int $IS_CALCING 是否正在计算状态
  24. * @property int $IS_SENDING 是否正在挂网状态
  25. * @property int $CALC_PERCENT 结算进度
  26. * @property int $SENT_PERCENT 发放进度
  27. * @property string $PERF_ADMIN_ID 生成业绩单管理员
  28. * @property string $CLOSE_ADMIN_ID 手动封期管理员ID
  29. * @property string $CALC_ADMIN_ID 结算管理员ID
  30. * @property string $SENT_ADMIN_ID 发放管理员ID
  31. * @property int $CLOSED_AT 发放管理员ID
  32. * @property int $PERF_STARTED_AT 生成业绩单开始时间
  33. * @property int $PERFED_AT 生成业绩单结束时间
  34. * @property int $CALCULATE_STARTED_AT 结算开始时间
  35. * @property int $CALCULATED_AT 结算完成时间
  36. * @property int $SEND_STARTED_AT 发放开始时间
  37. * @property int $SENT_AT 发放完成时间
  38. * @property int $CREATED_AT 创建时间
  39. */
  40. class Period extends \common\components\ActiveRecord
  41. {
  42. const CALCULATE_NONE = 0;
  43. const CALCULATE_FINISH = 1;
  44. const CALCULATE_FAIL = 2;
  45. const SEND_NONE = 0;
  46. const SEND_FINISH = 1;
  47. const SEND_FAIL = 2;
  48. const PERF_NONE = 0;
  49. const PERF_FINISH = 1;
  50. const PERF_FAIL = 2;
  51. const SYSTEM_START_PERIOD_NUM = 100;
  52. public $nowPeriodArr = null;
  53. public $periodNum = null;
  54. public $periodArr = null;
  55. public function init()
  56. {
  57. parent::init();
  58. }
  59. /**
  60. * @inheritdoc
  61. */
  62. public static function tableName()
  63. {
  64. return '{{%PERIOD}}';
  65. }
  66. /**
  67. * @inheritdoc
  68. */
  69. public function rules()
  70. {
  71. return [
  72. [['PERIOD_NUM', 'CALC_MONTH', 'CALC_YEAR', 'START_TIME', 'END_TIME', 'CREATED_AT'], 'required'],
  73. [['PERIOD_NUM', 'CALC_MONTH', 'CALC_YEAR', 'START_TIME', 'END_TIME', 'IS_MONTH', 'IS_YEAR', 'IS_PERFED', 'IS_CALCULATED', 'IS_SENT', 'IS_PERFING', 'IS_CALCING', 'IS_SENDING', 'CALC_PERCENT', 'SENT_PERCENT', 'CLOSED_AT', 'PERF_STARTED_AT', 'PERFED_AT', 'CALCULATE_STARTED_AT', 'CALCULATED_AT','CREATED_AT'], 'integer'],
  74. [['ID', 'PERF_ADMIN_ID', 'CLOSE_ADMIN_ID', 'CALC_ADMIN_ID', 'SENT_ADMIN_ID'], 'string', 'max' => 32],
  75. [['PERIOD_NUM'], 'unique'],
  76. [['ID'], 'unique'],
  77. ];
  78. }
  79. /**
  80. * @inheritdoc
  81. */
  82. public function attributeLabels()
  83. {
  84. return [
  85. 'ID' => 'ID',
  86. 'PERIOD_NUM' => '期数',
  87. 'CALC_MONTH' => '所在结算月',
  88. 'CALC_YEAR' => '所在结算年',
  89. 'START_TIME' => '期数开始时间戳',
  90. 'END_TIME' => '期数结束时间戳',
  91. 'IS_MONTH' => '是否结算月节点',
  92. 'IS_YEAR' => '是否结算年节点',
  93. 'IS_CLOSED' => '是否已封期',
  94. 'IS_PERFED' => '是否已生成业绩单',
  95. 'IS_CALCULATED' => '是否已结算',
  96. 'IS_SENT' => '是否已发放',
  97. 'IS_PERFING' => '是否正在生成业绩单',
  98. 'IS_CALCING' => '是否正在计算状态',
  99. 'IS_SENDING' => '是否正在挂网状态',
  100. 'CALC_PERCENT' => '结算进度',
  101. 'SENT_PERCENT' => '发放进度',
  102. 'PERF_ADMIN_ID' => '生成业绩单管理员',
  103. 'CLOSE_ADMIN_ID' => '手动封期管理员ID',
  104. 'CALC_ADMIN_ID' => '结算管理员ID',
  105. 'SENT_ADMIN_ID' => '发放管理员ID',
  106. 'CLOSED_AT' => '发放管理员ID',
  107. 'PERF_STARTED_AT' => '生成业绩单开始时间',
  108. 'PERFED_AT' => '生成业绩单结束时间',
  109. 'CALCULATE_STARTED_AT' => '结算开始时间',
  110. 'CALCULATED_AT' => '结算完成时间',
  111. 'SEND_STARTED_AT' => '发放开始时间',
  112. 'SENT_AT' => '发放完成时间',
  113. 'CREATED_AT' => '创建时间',
  114. ];
  115. }
  116. /**
  117. * 期数赋值给属性
  118. * @param int $periodNum
  119. * @return array|null|\yii\db\ActiveRecord
  120. */
  121. public function setPeriodNum($periodNum = null){
  122. if (empty($periodNum)) {
  123. $this->nowPeriodArr = static::find()
  124. ->where('IS_PREPARE=1')
  125. ->orderBy('PERIOD_NUM ASC')
  126. ->asArray()
  127. ->one();
  128. if (empty($this->nowPeriodArr)) {
  129. return false;
  130. }
  131. $this->periodArr = $this->nowPeriodArr;
  132. $periodNum = $this->nowPeriodArr['PERIOD_NUM'];
  133. } else {
  134. $this->periodArr = static::findOneAsArray(
  135. 'PERIOD_NUM=:PERIOD_NUM AND IS_PREPARE=1',
  136. [':PERIOD_NUM'=>$periodNum]
  137. );
  138. if (empty($this->periodArr)) {
  139. return false;
  140. }
  141. }
  142. $this->periodNum = $periodNum;
  143. return $this->periodArr;
  144. }
  145. // 通过期数,获取此期数据
  146. public static function getInfoByPeriodNum($periodNum) {
  147. return static::findOneAsArray(['PERIOD_NUM'=>$periodNum]);
  148. }
  149. /**
  150. * 是否存在所传期数
  151. * @param int $periodNum
  152. * @return bool
  153. */
  154. public static function isExistsPeriodNum(int $periodNum){
  155. return static::find()->where(['PERIOD_NUM'=>$periodNum])->exists();
  156. }
  157. /**
  158. * 获取当前未封期的最小期数
  159. * @return int
  160. */
  161. public function getNowPeriodNum(){
  162. $this->setPeriodNum();
  163. if($this->nowPeriodArr){
  164. return $this->nowPeriodArr['PERIOD_NUM'];
  165. } else {
  166. return self::SYSTEM_START_PERIOD_NUM;
  167. }
  168. }
  169. /**
  170. * 获取当前期的开始时间
  171. * @return int
  172. */
  173. public function getNowPeriodStart(){
  174. $this->setPeriodNum();
  175. if($this->nowPeriodArr){
  176. return $this->nowPeriodArr['START_TIME'];
  177. } else {
  178. return Date::nowTime();
  179. }
  180. }
  181. /**
  182. * 获取当前期的结束时间
  183. * @return int
  184. */
  185. public function getNowPeriodEnd(){
  186. $this->setPeriodNum();
  187. if($this->nowPeriodArr){
  188. return $this->nowPeriodArr['END_TIME'];
  189. } else {
  190. return Date::nowTime();
  191. }
  192. }
  193. /**
  194. * 获取当前所在的结算月
  195. * @return int
  196. */
  197. public function getNowMonth(){
  198. $this->setPeriodNum();
  199. if($this->nowPeriodArr){
  200. return $this->nowPeriodArr['CALC_MONTH'];
  201. } else {
  202. return 0;
  203. }
  204. }
  205. /**
  206. * 获取当前期数所在结算年
  207. * @return int
  208. */
  209. public function getNowYear(){
  210. $this->setPeriodNum();
  211. if($this->nowPeriodArr){
  212. return $this->nowPeriodArr['CALC_YEAR'];
  213. } else {
  214. return 0;
  215. }
  216. }
  217. /**
  218. * 当前期数所在年月
  219. * @return int|string
  220. */
  221. public function getNowYearMonth(){
  222. $this->setPeriodNum();
  223. if($this->nowPeriodArr){
  224. return $this->nowPeriodArr['CALC_YEAR'].Tool::numFix($this->nowPeriodArr['CALC_MONTH']);
  225. } else {
  226. return 0;
  227. }
  228. }
  229. /**
  230. * 获取期数所在的结算月
  231. * @param int $periodNum
  232. * @return mixed
  233. * @throws Exception
  234. */
  235. public function getMonth($periodNum = null){
  236. $period = $this->setPeriodNum($periodNum);
  237. if($period){
  238. return $period['CALC_MONTH'];
  239. } else {
  240. throw new Exception('期数不存在');
  241. }
  242. }
  243. /**
  244. * 获取期数所在的结算年
  245. * @param $periodNum
  246. * @return mixed
  247. * @throws Exception
  248. */
  249. public function getYear($periodNum = null){
  250. $period = $this->setPeriodNum($periodNum);
  251. if($period){
  252. return $period['CALC_YEAR'];
  253. } else {
  254. throw new Exception('期数不存在');
  255. }
  256. }
  257. /**
  258. * 期数所在年月
  259. * @param $periodNum
  260. * @return string
  261. * @throws Exception
  262. */
  263. public function getYearMonth($periodNum = null){
  264. $period = $this->setPeriodNum($periodNum);
  265. if($period){
  266. return $period['CALC_YEAR'].Tool::numFix($period['CALC_MONTH'], 2);
  267. } else {
  268. throw new Exception('期数不存在'.$periodNum);
  269. }
  270. }
  271. /**
  272. * 所传年、月所有期数的数量
  273. * @param $year
  274. * @param $month
  275. * @return int
  276. */
  277. public function getYearMonthAllPeriodNumCount($year, $month){
  278. return Period::find()->where('CALC_YEAR=:CALC_YEAR AND CALC_MONTH=:CALC_MONTH', [':CALC_YEAR'=>$year, ':CALC_MONTH'=>$month])->count(1);
  279. }
  280. /**
  281. * 是否是结算月节点
  282. * @param $periodNum
  283. * @return mixed
  284. * @throws Exception
  285. */
  286. public function isCalcMonth($periodNum = null){
  287. $period = $this->setPeriodNum($periodNum);
  288. if($period){
  289. return $period['IS_MONTH'];
  290. } else {
  291. throw new Exception('期数不存在');
  292. }
  293. }
  294. /**
  295. * 是否是结算年节点
  296. * @param $periodNum
  297. * @return mixed
  298. * @throws Exception
  299. */
  300. public function isCalcYear($periodNum = null){
  301. $period = $this->setPeriodNum($periodNum);
  302. if($period){
  303. return $period['IS_YEAR'];
  304. } else {
  305. throw new Exception('期数不存在');
  306. }
  307. }
  308. /**
  309. * 获取挂网的最大期数
  310. * @return int
  311. */
  312. public function getMaxIsSentPeriodNum() {
  313. $maxIsSentData = static::find()->where('IS_SENT=1')->orderBy('PERIOD_NUM DESC')->asArray()->one();
  314. $maxIsSentPeriodNum = self::SYSTEM_START_PERIOD_NUM;
  315. if( $maxIsSentData && $maxIsSentData["PERIOD_NUM"] ) {
  316. $maxIsSentPeriodNum = $maxIsSentData["PERIOD_NUM"];
  317. }
  318. return $maxIsSentPeriodNum;
  319. }
  320. /**
  321. * 返回所传结算月包含的所有期数
  322. * @param $year
  323. * @param $month
  324. * @return array|\yii\db\ActiveRecord[]
  325. */
  326. public static function getPeriodNumsFromMonth($year, $month){
  327. return static::find()->select('PERIOD_NUM')->where('CALC_YEAR=:CALC_YEAR AND CALC_MONTH=:CALC_MONTH', [':CALC_YEAR'=>$year, ':CALC_MONTH'=>$month])->orderBy('PERIOD_NUM ASC')->asArray()->all();
  328. }
  329. /**
  330. * 获取所传结算月的最小期数和最大期数
  331. * @param $year
  332. * @param $month
  333. * @return array
  334. */
  335. public static function getPeriodNumRangeFromMonth($year, $month){
  336. $allPeriod = self::getPeriodNumsFromMonth($year, $month);
  337. $tempPeriod = [];
  338. foreach($allPeriod as $period){
  339. $tempPeriod[] = $period['PERIOD_NUM'];
  340. }
  341. if(!$tempPeriod){
  342. return null;
  343. }
  344. $minPeriod = min($tempPeriod);
  345. $maxPeriod = max($tempPeriod);
  346. return ['min'=>$minPeriod, 'max'=>$maxPeriod];
  347. }
  348. /**
  349. * 返回结算月的节点期数
  350. * @param $year
  351. * @param $month
  352. * @return mixed|null
  353. */
  354. public static function getPeriodNumPointFromMonth($year, $month) {
  355. $data = static::find()->where('CALC_YEAR=:CALC_YEAR AND CALC_MONTH=:CALC_MONTH AND IS_MONTH=1', [':CALC_YEAR'=>$year, ':CALC_MONTH' => $month])->asArray()->one();
  356. if($data) return $data['PERIOD_NUM'];
  357. else return null;
  358. }
  359. /**
  360. * 返回结算月的节点期
  361. * @param $year
  362. * @param $month
  363. * @return mixed|null
  364. */
  365. public static function getPeriodPointFromMonth($year, $month) {
  366. $data = static::find()->where('CALC_YEAR=:CALC_YEAR AND CALC_MONTH=:CALC_MONTH AND IS_MONTH=1', [':CALC_YEAR'=>$year, ':CALC_MONTH' => $month])->asArray()->one();
  367. if($data) return $data;
  368. else return null;
  369. }
  370. /**
  371. * 是否已经封期
  372. * @param $periodNum
  373. * @return bool
  374. * @throws Exception
  375. */
  376. public function isClosed($periodNum = null){
  377. $period = $this->setPeriodNum($periodNum);
  378. if($period){
  379. return boolval($period['IS_CLOSED']);
  380. } else {
  381. throw new Exception('期数不存在');
  382. }
  383. }
  384. /**
  385. * 是否已经生成业绩单
  386. * @param $periodNum
  387. * @return bool
  388. * @throws Exception
  389. */
  390. public function isPerfed($periodNum = null){
  391. $period = $this->setPeriodNum($periodNum);
  392. if($period){
  393. return boolval($period['IS_PERFED']);
  394. } else {
  395. throw new Exception('期数不存在');
  396. }
  397. }
  398. /**
  399. * 是否已结算
  400. * @param null $periodNum
  401. * @return bool
  402. * @throws Exception
  403. */
  404. public function isCalculated($periodNum = null){
  405. $period = $this->setPeriodNum($periodNum);
  406. if($period){
  407. return boolval($period['IS_CALCULATED'] == self::CALCULATE_FINISH);
  408. } else {
  409. throw new Exception('期数不存在');
  410. }
  411. }
  412. /**
  413. * 是否可被封期
  414. * @param $periodNum
  415. * @return bool
  416. */
  417. public function isCanClose($periodNum = null){
  418. $period = $this->setPeriodNum($periodNum);
  419. if($period['IS_CLOSED']){
  420. return false;
  421. }
  422. $previousPeriod = static::findOne(['PERIOD_NUM'=>$this->periodNum-1]);
  423. if($previousPeriod){
  424. if(!$previousPeriod['IS_CLOSED']){
  425. return false;
  426. }
  427. // 只要上期封期了,下期就可以封期
  428. // if(!$previousPeriod['IS_CALCULATED']){
  429. // return false;
  430. // }
  431. // if(!$previousPeriod['IS_SENT']){
  432. // return false;
  433. // }
  434. }
  435. return true;
  436. }
  437. /**
  438. * 是否可以生成业绩单
  439. * @param $periodNum
  440. * @return bool
  441. */
  442. public function isCanPerf($periodNum = null){
  443. $period = $this->setPeriodNum($periodNum);
  444. // 查看该期是否已经封期
  445. if(!$period['IS_CLOSED']){
  446. return false;
  447. }
  448. // 已经挂网,不能生成业绩单
  449. if($period['IS_SENT'] == self::SEND_FINISH){
  450. return false;
  451. }
  452. //上1期奖金未发放,限制不能生成业绩单
  453. if(!$this->isLastSent($this->periodNum)){
  454. return false;
  455. }
  456. return true;
  457. }
  458. /**
  459. * 是否可以结算
  460. * @param $periodNum
  461. * @return bool
  462. */
  463. public function isCanCalc($periodNum = null){
  464. $period = $this->setPeriodNum($periodNum);
  465. // 查看该期是否已经封期
  466. if(!$period['IS_CLOSED']){
  467. return false;
  468. }
  469. // 还没有生成业绩单,不能封期
  470. if(!$period['IS_PERFED'] || $period['IS_PERFED'] == self::PERF_FAIL){
  471. return false;
  472. }
  473. if($period['IS_SENT'] == self::SEND_FINISH){
  474. return false;
  475. }
  476. // $previousPeriod = static::findOneAsArray(['PERIOD_NUM'=>$this->periodNum-1]);
  477. // if($previousPeriod){
  478. // // 查看上一期是否已经挂网,上一期挂网了,本期才能结算
  479. // if(!$previousPeriod['IS_SENT']){
  480. // return false;
  481. // }
  482. // }
  483. //上1期奖金未发放,限制不能结算
  484. if(!$this->isLastSent($this->periodNum)){
  485. return false;
  486. }
  487. return true;
  488. }
  489. /**
  490. * 是否可以挂网
  491. * @param $periodNum
  492. * @return bool
  493. */
  494. public function isCanSend($periodNum = null){
  495. $period = $this->setPeriodNum($periodNum);
  496. if(!$period['IS_CLOSED']){
  497. return false;
  498. }
  499. if(!$period['IS_CALCULATED'] || $period['IS_CALCULATED'] == self::CALCULATE_FAIL){
  500. return false;
  501. }
  502. if($period['IS_SENT'] == self::SEND_FINISH){
  503. return false;
  504. }
  505. //上1期奖金未发放,限制不能挂网
  506. if(!$this->isLastSent($this->periodNum)){
  507. return false;
  508. }
  509. return true;
  510. }
  511. /**
  512. * 是否已经挂网
  513. * @param $periodNum
  514. * @return bool
  515. */
  516. public function isSent($periodNum = null){
  517. $period = $this->setPeriodNum($periodNum);
  518. if($period['IS_SENT'] == self::SEND_FINISH) return true;
  519. return false;
  520. }
  521. /**
  522. * 上期是否挂网
  523. * @param null $periodNum
  524. * @return bool
  525. */
  526. public function isLastSent($periodNum){
  527. $period = $this->setPeriodNum($periodNum-1);
  528. if ($period) {
  529. if($period['IS_SENT'] == self::SEND_FINISH) {
  530. return true;
  531. } else {
  532. return false;
  533. }
  534. }
  535. return false;
  536. }
  537. // 奖金计算,更新业绩表,进度百分比
  538. public static function updatePercent($percent, $periodNum) {
  539. $result = Period::updateAll(
  540. ['PERF_PERCENT' => $percent],
  541. 'PERIOD_NUM=:PERIOD_NUM',
  542. [':PERIOD_NUM' => $periodNum]
  543. );
  544. return $result;
  545. }
  546. /**
  547. * 更新计算进度
  548. */
  549. public static function updateCalcProcess($perpare, $periodNum) {
  550. $result = Period::updateAll(
  551. ['IS_PREPARE' => $perpare],
  552. 'PERIOD_NUM=:PERIOD_NUM',
  553. [':PERIOD_NUM' => $periodNum]
  554. );
  555. return $result;
  556. }
  557. }