Period.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  1. <?php
  2. namespace common\models;
  3. use common\helpers\bonus\Calc\CalcConsole;
  4. use common\helpers\Date;
  5. use common\helpers\Tool;
  6. use Yii;
  7. use yii\db\Exception;
  8. /**
  9. * This is the model class for table "{{%PERIOD}}".
  10. *
  11. * @property string $ID
  12. * @property int $PERIOD_NUM 期数
  13. * @property int $CALC_MONTH 所在结算月
  14. * @property int $CALC_YEAR 所在结算年
  15. * @property int $START_TIME 期数开始时间戳
  16. * @property int $END_TIME 期数结束时间戳
  17. * @property int $IS_MONTH 是否结算月节点
  18. * @property int $IS_YEAR 是否结算年节点
  19. * @property int $IS_CLOSED 是否已封期
  20. * @property int $IS_PERFED 是否已生成业绩单
  21. * @property int $IS_CALCULATED 是否已结算
  22. * @property int $IS_SENT 是否已发放
  23. * @property int $IS_PERFING 是否正在生成业绩单
  24. * @property int $IS_CALCING 是否正在计算状态
  25. * @property int $IS_SENDING 是否正在挂网状态
  26. * @property int $CALC_PERCENT 结算进度
  27. * @property int $SENT_PERCENT 发放进度
  28. * @property string $PERF_ADMIN_ID 生成业绩单管理员
  29. * @property string $CLOSE_ADMIN_ID 手动封期管理员ID
  30. * @property string $CALC_ADMIN_ID 结算管理员ID
  31. * @property string $SENT_ADMIN_ID 发放管理员ID
  32. * @property int $CLOSED_AT 发放管理员ID
  33. * @property int $PERF_STARTED_AT 生成业绩单开始时间
  34. * @property int $PERFED_AT 生成业绩单结束时间
  35. * @property int $CALCULATE_STARTED_AT 结算开始时间
  36. * @property int $CALCULATED_AT 结算完成时间
  37. * @property int $SEND_STARTED_AT 发放开始时间
  38. * @property int $SENT_AT 发放完成时间
  39. * @property int $CREATED_AT 创建时间
  40. */
  41. class Period extends \common\components\ActiveRecord
  42. {
  43. const CALCULATE_NONE = 0;
  44. const CALCULATE_FINISH = 1;
  45. const CALCULATE_FAIL = 2;
  46. const SEND_NONE = 0;
  47. const SEND_FINISH = 1;
  48. const SEND_FAIL = 2;
  49. const PERF_NONE = 0;
  50. const PERF_FINISH = 1;
  51. const PERF_FAIL = 2;
  52. const SYSTEM_START_PERIOD_NUM = 100;
  53. const AUTO_EXEC_CALC = 1;
  54. const MANUAL_EXEC_CALC = 0;
  55. const IS_PROCESSING = 1;
  56. const NOT_PROCESSING = 0;
  57. const IS_PREPARING = 1;
  58. const NOT_PREPARING = 0;
  59. public $nowPeriodArr = null;
  60. public $periodNum = null;
  61. public $periodArr = null;
  62. public function init()
  63. {
  64. parent::init();
  65. }
  66. /**
  67. * @inheritdoc
  68. */
  69. public static function tableName()
  70. {
  71. return '{{%PERIOD}}';
  72. }
  73. /**
  74. * @inheritdoc
  75. */
  76. public function rules()
  77. {
  78. return [
  79. [['PERIOD_NUM', 'CALC_MONTH', 'CALC_YEAR', 'START_TIME', 'END_TIME', 'CREATED_AT'], 'required'],
  80. [['PERIOD_NUM', 'CALC_MONTH', 'CALC_YEAR', 'START_TIME', 'END_TIME', 'IS_MONTH', 'IS_YEAR', 'IS_CLOSED', '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', 'SEND_STARTED_AT', 'SENT_AT', 'CREATED_AT'], 'integer'],
  81. [['ID', 'PERF_ADMIN_ID', 'CLOSE_ADMIN_ID', 'CALC_ADMIN_ID', 'SENT_ADMIN_ID'], 'string', 'max' => 32],
  82. [['PERIOD_NUM'], 'unique'],
  83. [['ID'], 'unique'],
  84. ];
  85. }
  86. /**
  87. * @inheritdoc
  88. */
  89. public function attributeLabels()
  90. {
  91. return [
  92. 'ID' => 'ID',
  93. 'PERIOD_NUM' => '期数',
  94. 'CALC_MONTH' => '所在结算月',
  95. 'CALC_YEAR' => '所在结算年',
  96. 'START_TIME' => '期数开始时间戳',
  97. 'END_TIME' => '期数结束时间戳',
  98. 'IS_MONTH' => '是否结算月节点',
  99. 'IS_YEAR' => '是否结算年节点',
  100. 'IS_CLOSED' => '是否已封期',
  101. 'IS_PERFED' => '是否已生成业绩单',
  102. 'IS_CALCULATED' => '是否已结算',
  103. 'IS_SENT' => '是否已发放',
  104. 'IS_PERFING' => '是否正在生成业绩单',
  105. 'IS_CALCING' => '是否正在计算状态',
  106. 'IS_SENDING' => '是否正在挂网状态',
  107. 'CALC_PERCENT' => '结算进度',
  108. 'SENT_PERCENT' => '发放进度',
  109. 'PERF_ADMIN_ID' => '生成业绩单管理员',
  110. 'CLOSE_ADMIN_ID' => '手动封期管理员ID',
  111. 'CALC_ADMIN_ID' => '结算管理员ID',
  112. 'SENT_ADMIN_ID' => '发放管理员ID',
  113. 'CLOSED_AT' => '发放管理员ID',
  114. 'PERF_STARTED_AT' => '生成业绩单开始时间',
  115. 'PERFED_AT' => '生成业绩单结束时间',
  116. 'CALCULATE_STARTED_AT' => '结算开始时间',
  117. 'CALCULATED_AT' => '结算完成时间',
  118. 'SEND_STARTED_AT' => '发放开始时间',
  119. 'SENT_AT' => '发放完成时间',
  120. 'CREATED_AT' => '创建时间',
  121. ];
  122. }
  123. /**
  124. * 期数赋值给属性
  125. * @param int $periodNum
  126. * @return array|null|\yii\db\ActiveRecord
  127. */
  128. public function setPeriodNum($periodNum = null){
  129. if($periodNum === 0 || $periodNum === null || $periodNum === ''){
  130. //if($this->nowPeriodArr === null){
  131. $this->nowPeriodArr = static::find()->where('IS_CLOSED=0')->orderBy('PERIOD_NUM ASC')->asArray()->one();
  132. //}
  133. $this->periodArr = $this->nowPeriodArr;
  134. $periodNum = $this->nowPeriodArr['PERIOD_NUM'];
  135. } else {
  136. if($periodNum !== $this->periodNum){
  137. $this->periodArr = static::findOneAsArray(['PERIOD_NUM'=>$periodNum]);
  138. }
  139. }
  140. $this->periodNum = $periodNum;
  141. return $this->periodArr;
  142. }
  143. // 通过期数,获取此期数据
  144. public static function getInfoByPeriodNum($periodNum) {
  145. return static::findOneAsArray(['PERIOD_NUM'=>$periodNum]);
  146. }
  147. /**
  148. * 是否存在所传期数
  149. * @param int $periodNum
  150. * @return bool
  151. */
  152. public static function isExistsPeriodNum(int $periodNum){
  153. return static::find()->where(['PERIOD_NUM'=>$periodNum])->exists();
  154. }
  155. /**
  156. * 获取当前未封期的最小期数
  157. * @return int
  158. */
  159. public function getNowPeriodNum(){
  160. $this->setPeriodNum();
  161. if($this->nowPeriodArr){
  162. return $this->nowPeriodArr['PERIOD_NUM'];
  163. } else {
  164. return self::SYSTEM_START_PERIOD_NUM;
  165. }
  166. }
  167. /**
  168. * 获取当前期的开始时间
  169. * @return int
  170. */
  171. public function getNowPeriodStart(){
  172. $this->setPeriodNum();
  173. if($this->nowPeriodArr){
  174. return $this->nowPeriodArr['START_TIME'];
  175. } else {
  176. return Date::nowTime();
  177. }
  178. }
  179. /**
  180. * 获取当前期的结束时间
  181. * @return int
  182. */
  183. public function getNowPeriodEnd(){
  184. $this->setPeriodNum();
  185. if($this->nowPeriodArr){
  186. return $this->nowPeriodArr['END_TIME'];
  187. } else {
  188. return Date::nowTime();
  189. }
  190. }
  191. /**
  192. * 获取当前所在的结算月
  193. * @return int
  194. */
  195. public function getNowMonth(){
  196. $this->setPeriodNum();
  197. if($this->nowPeriodArr){
  198. return $this->nowPeriodArr['CALC_MONTH'];
  199. } else {
  200. return 0;
  201. }
  202. }
  203. /**
  204. * 获取当前期数所在结算年
  205. * @return int
  206. */
  207. public function getNowYear(){
  208. $this->setPeriodNum();
  209. if($this->nowPeriodArr){
  210. return $this->nowPeriodArr['CALC_YEAR'];
  211. } else {
  212. return 0;
  213. }
  214. }
  215. /**
  216. * 当前期数所在年月
  217. * @return int|string
  218. */
  219. public function getNowYearMonth(){
  220. $this->setPeriodNum();
  221. if($this->nowPeriodArr){
  222. return $this->nowPeriodArr['CALC_YEAR'].Tool::numFix($this->nowPeriodArr['CALC_MONTH']);
  223. } else {
  224. return 0;
  225. }
  226. }
  227. /**
  228. * 获取期数所在的结算月
  229. * @param int $periodNum
  230. * @return mixed
  231. * @throws Exception
  232. */
  233. public function getMonth($periodNum = null){
  234. $period = $this->setPeriodNum($periodNum);
  235. if($period){
  236. return $period['CALC_MONTH'];
  237. } else {
  238. throw new Exception('期数不存在');
  239. }
  240. }
  241. /**
  242. * 获取期数所在的结算年
  243. * @param $periodNum
  244. * @return mixed
  245. * @throws Exception
  246. */
  247. public function getYear($periodNum = null){
  248. $period = $this->setPeriodNum($periodNum);
  249. if($period){
  250. return $period['CALC_YEAR'];
  251. } else {
  252. throw new Exception('期数不存在');
  253. }
  254. }
  255. /**
  256. * 期数所在年月
  257. * @param $periodNum
  258. * @return string
  259. * @throws Exception
  260. */
  261. public function getYearMonth($periodNum = null){
  262. $period = $this->setPeriodNum($periodNum);
  263. if($period){
  264. return $period['CALC_YEAR'].Tool::numFix($period['CALC_MONTH'], 2);
  265. } else {
  266. throw new Exception('期数不存在'.$periodNum);
  267. }
  268. }
  269. /**
  270. * 所传年、月所有期数的数量
  271. * @param $year
  272. * @param $month
  273. * @return int
  274. */
  275. public function getYearMonthAllPeriodNumCount($year, $month){
  276. return Period::find()->where('CALC_YEAR=:CALC_YEAR AND CALC_MONTH=:CALC_MONTH', [':CALC_YEAR'=>$year, ':CALC_MONTH'=>$month])->count(1);
  277. }
  278. /**
  279. * 是否是结算月节点
  280. * @param $periodNum
  281. * @return mixed
  282. * @throws Exception
  283. */
  284. public function isCalcMonth($periodNum = null){
  285. $period = $this->setPeriodNum($periodNum);
  286. if($period){
  287. return $period['IS_MONTH'];
  288. } else {
  289. throw new Exception('期数不存在');
  290. }
  291. }
  292. /**
  293. * 是否是结算年节点
  294. * @param $periodNum
  295. * @return mixed
  296. * @throws Exception
  297. */
  298. public function isCalcYear($periodNum = null){
  299. $period = $this->setPeriodNum($periodNum);
  300. if($period){
  301. return $period['IS_YEAR'];
  302. } else {
  303. throw new Exception('期数不存在');
  304. }
  305. }
  306. /**
  307. * 获取挂网的最大期数
  308. * @return int
  309. */
  310. public function getMaxIsSentPeriodNum() {
  311. $maxIsSentData = static::find()->where('IS_SENT=1')->orderBy('PERIOD_NUM DESC')->asArray()->one();
  312. $maxIsSentPeriodNum = self::SYSTEM_START_PERIOD_NUM;
  313. if( $maxIsSentData && $maxIsSentData["PERIOD_NUM"] ) {
  314. $maxIsSentPeriodNum = $maxIsSentData["PERIOD_NUM"];
  315. }
  316. return $maxIsSentPeriodNum;
  317. }
  318. /**
  319. * 返回所传结算月包含的所有期数
  320. * @param $year
  321. * @param $month
  322. * @return array|\yii\db\ActiveRecord[]
  323. */
  324. public static function getPeriodNumsFromMonth($year, $month){
  325. 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();
  326. }
  327. /**
  328. * 获取所传结算月的最小期数和最大期数
  329. * @param $year
  330. * @param $month
  331. * @return array
  332. */
  333. public static function getPeriodNumRangeFromMonth($year, $month){
  334. $allPeriod = self::getPeriodNumsFromMonth($year, $month);
  335. $tempPeriod = [];
  336. foreach($allPeriod as $period){
  337. $tempPeriod[] = $period['PERIOD_NUM'];
  338. }
  339. if(!$tempPeriod){
  340. return null;
  341. }
  342. $minPeriod = min($tempPeriod);
  343. $maxPeriod = max($tempPeriod);
  344. return ['min'=>$minPeriod, 'max'=>$maxPeriod];
  345. }
  346. /**
  347. * 返回结算月的节点期数
  348. * @param $year
  349. * @param $month
  350. * @return mixed|null
  351. */
  352. public static function getPeriodNumPointFromMonth($year, $month) {
  353. $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();
  354. if($data) return $data['PERIOD_NUM'];
  355. else return null;
  356. }
  357. /**
  358. * 返回结算月的节点期
  359. * @param $year
  360. * @param $month
  361. * @return mixed|null
  362. */
  363. public static function getPeriodPointFromMonth($year, $month) {
  364. $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();
  365. if($data) return $data;
  366. else return null;
  367. }
  368. /**
  369. * 是否已经封期
  370. * @param $periodNum
  371. * @return bool
  372. * @throws Exception
  373. */
  374. public function isClosed($periodNum = null){
  375. $period = $this->setPeriodNum($periodNum);
  376. if($period){
  377. return boolval($period['IS_CLOSED']);
  378. } else {
  379. throw new Exception('期数不存在');
  380. }
  381. }
  382. /**
  383. * 是否已经生成业绩单
  384. * @param $periodNum
  385. * @return bool
  386. * @throws Exception
  387. */
  388. public function isPerfed($periodNum = null){
  389. $period = $this->setPeriodNum($periodNum);
  390. if($period){
  391. return boolval($period['IS_PERFED']);
  392. } else {
  393. throw new Exception('期数不存在');
  394. }
  395. }
  396. /**
  397. * 是否已结算
  398. * @param null $periodNum
  399. * @return bool
  400. * @throws Exception
  401. */
  402. public function isCalculated($periodNum = null){
  403. $period = $this->setPeriodNum($periodNum);
  404. if($period){
  405. return boolval($period['IS_CALCULATED'] == self::CALCULATE_FINISH);
  406. } else {
  407. throw new Exception('期数不存在');
  408. }
  409. }
  410. /**
  411. * 是否可被封期
  412. * @param $periodNum
  413. * @return bool
  414. */
  415. public function isCanClose($periodNum = null){
  416. $period = $this->setPeriodNum($periodNum);
  417. if($period['IS_CLOSED']){
  418. return false;
  419. }
  420. $previousPeriod = static::findOne(['PERIOD_NUM'=>$this->periodNum-1]);
  421. if($previousPeriod){
  422. if(!$previousPeriod['IS_CLOSED']){
  423. return false;
  424. }
  425. // 只要上期封期了,下期就可以封期
  426. // if(!$previousPeriod['IS_CALCULATED']){
  427. // return false;
  428. // }
  429. // if(!$previousPeriod['IS_SENT']){
  430. // return false;
  431. // }
  432. }
  433. return true;
  434. }
  435. /**
  436. * 是否可以生成业绩单
  437. * @param $periodNum
  438. * @return bool
  439. */
  440. public function isCanPerf($periodNum = null){
  441. $period = $this->setPeriodNum($periodNum);
  442. // 查看该期是否已经封期
  443. if(!$period['IS_CLOSED']){
  444. return false;
  445. }
  446. // 已经挂网,不能生成业绩单
  447. if($period['IS_SENT'] == self::SEND_FINISH){
  448. return false;
  449. }
  450. //上1期奖金未发放,限制不能生成业绩单
  451. if(!$this->isLastSent($this->periodNum)){
  452. return false;
  453. }
  454. return true;
  455. }
  456. /**
  457. * 是否可以结算
  458. * @param $periodNum
  459. * @return bool
  460. */
  461. public function isCanCalc($periodNum = null){
  462. $period = $this->setPeriodNum($periodNum);
  463. // 查看该期是否已经封期
  464. if(!$period['IS_CLOSED']){
  465. return false;
  466. }
  467. // 还没有生成业绩单,不能封期
  468. if(!$period['IS_PERFED'] || $period['IS_PERFED'] == self::PERF_FAIL){
  469. return false;
  470. }
  471. if($period['IS_SENT'] == self::SEND_FINISH){
  472. return false;
  473. }
  474. // $previousPeriod = static::findOneAsArray(['PERIOD_NUM'=>$this->periodNum-1]);
  475. // if($previousPeriod){
  476. // // 查看上一期是否已经挂网,上一期挂网了,本期才能结算
  477. // if(!$previousPeriod['IS_SENT']){
  478. // return false;
  479. // }
  480. // }
  481. //上1期奖金未发放,限制不能结算
  482. if(!$this->isLastSent($this->periodNum)){
  483. return false;
  484. }
  485. return true;
  486. }
  487. /**
  488. * 是否可以挂网
  489. * @param $periodNum
  490. * @return bool
  491. */
  492. public function isCanSend($periodNum = null){
  493. $period = $this->setPeriodNum($periodNum);
  494. if(!$period['IS_CLOSED']){
  495. return false;
  496. }
  497. if(!$period['IS_CALCULATED'] || $period['IS_CALCULATED'] == self::CALCULATE_FAIL){
  498. return false;
  499. }
  500. if($period['IS_SENT'] == self::SEND_FINISH){
  501. return false;
  502. }
  503. //上1期奖金未发放,限制不能挂网
  504. if(!$this->isLastSent($this->periodNum)){
  505. return false;
  506. }
  507. return true;
  508. }
  509. /**
  510. * 是否已经挂网
  511. * @param $periodNum
  512. * @return bool
  513. */
  514. public function isSent($periodNum = null){
  515. $period = $this->setPeriodNum($periodNum);
  516. if($period['IS_SENT'] == self::SEND_FINISH) return true;
  517. return false;
  518. }
  519. /**
  520. * 上期是否挂网
  521. * @param null $periodNum
  522. * @return bool
  523. */
  524. public function isLastSent($periodNum){
  525. $period = $this->setPeriodNum($periodNum-1);
  526. if ($period) {
  527. if($period['IS_SENT'] == self::SEND_FINISH) {
  528. return true;
  529. } else {
  530. return false;
  531. }
  532. }
  533. return false;
  534. }
  535. /**
  536. * 获取上月的年月
  537. * @param $periodNum
  538. * @return array
  539. */
  540. public function getLastMonth($periodNum = null){
  541. $period = $this->setPeriodNum($periodNum);
  542. if($period){
  543. $year = $period['CALC_YEAR'];
  544. $month = $period['CALC_MONTH'];
  545. $lastYear = Date::lastMonth($year.'-'.$month, 'Y');
  546. $lastMonth = Date::lastMonth($year.'-'.$month, 'm');
  547. return [
  548. 'year' => $lastYear,
  549. 'month' => intval($lastMonth),
  550. 'yearMonth' => $lastYear.Tool::numFix($lastMonth, 2),
  551. ];
  552. } else {
  553. return [
  554. 'year' => 0,
  555. 'month' => 0,
  556. 'yearMonth' => 0,
  557. ];
  558. }
  559. }
  560. /**
  561. * 上几个月的年月
  562. * @param int $num
  563. * @param null $periodNum
  564. * @return array
  565. */
  566. public function getLastNumMonth($num=1,$periodNum = null){
  567. $period = $this->setPeriodNum($periodNum);
  568. if($period){
  569. $year = $period['CALC_YEAR'];
  570. $month = $period['CALC_MONTH'];
  571. $lastYear = Date::lastNumMonth($num,$year.'-'.$month, 'Y');
  572. $lastMonth = Date::lastNumMonth($num,$year.'-'.$month, 'm');
  573. return [
  574. 'year' => $lastYear,
  575. 'month' => intval($lastMonth),
  576. 'yearMonth' => $lastYear.Tool::numFix($lastMonth, 2),
  577. ];
  578. } else {
  579. return [
  580. 'year' => 0,
  581. 'month' => 0,
  582. 'yearMonth' => 0,
  583. ];
  584. }
  585. }
  586. /**
  587. * 下月的年月
  588. * @param null $periodNum
  589. * @return array
  590. */
  591. public function getNextMonth($periodNum = null){
  592. $period = $this->setPeriodNum($periodNum);
  593. if($period){
  594. $year = $period['CALC_YEAR'];
  595. $month = $period['CALC_MONTH'];
  596. $nextYear = Date::nextMonth($year.'-'.$month, 'Y');
  597. $nextMonth = Date::nextMonth($year.'-'.$month, 'm');
  598. return [
  599. 'year' => $nextYear,
  600. 'month' => intval($nextMonth),
  601. 'yearMonth' => $nextYear.Tool::numFix($nextMonth, 2),
  602. ];
  603. } else {
  604. return [
  605. 'year' => 0,
  606. 'month' => 0,
  607. 'yearMonth' => 0,
  608. ];
  609. }
  610. }
  611. /**
  612. * 通过时间戳获取期
  613. * @param $time
  614. * @return array|null|\yii\db\ActiveRecord
  615. */
  616. public static function getPeriodFromTime($time){
  617. return static::find()->where('START_TIME<=:TIME AND END_TIME>:TIME', [':TIME'=>$time])->asArray()->one();
  618. }
  619. /**
  620. * 通过时间戳范围获取期间内的期
  621. * @param $minTime
  622. * @param $maxTime
  623. * @return array|\yii\db\ActiveRecord[]
  624. */
  625. public static function getPeriodFromTimeRange($minTime, $maxTime){
  626. return static::find()->where('END_TIME>:MIN_TIME AND START_TIME<:MAX_TIME', [':MIN_TIME'=>$minTime, ':MAX_TIME'=>$maxTime])->orderBy('PERIOD_NUM ASC')->asArray()->all();
  627. }
  628. /**
  629. * 获取近几期的期数
  630. * @param $num
  631. * @return array
  632. */
  633. public static function getNearlyPeriodNum($num){
  634. // 获取当前期
  635. $period = self::instance();
  636. $periodNum = $period->getNowPeriodNum();
  637. // 获取前$num期的期数
  638. $periodNums = [
  639. $periodNum
  640. ];
  641. for($i=0;$i<$num;$i++){
  642. if($periodNum - $i > 0){
  643. // 查看该期是否存在
  644. if(self::isExistsPeriodNum($periodNum - $i)){
  645. $periodNums[] = $periodNum - $i;
  646. }
  647. }
  648. }
  649. return array_reverse($periodNums);
  650. }
  651. /**
  652. * 获取近几期的挂网期数
  653. * @param $num
  654. * @return array
  655. */
  656. public static function getNearlySendPeriodNum($num){
  657. // 获取当前期
  658. $period = self::instance();
  659. $periodNum = $period->sentMaxPeriodNum();
  660. // 获取前$num期的期数
  661. $periodNums = [
  662. $periodNum
  663. ];
  664. for($i=0;$i<$num;$i++){
  665. if($periodNum - $i > 0){
  666. // 查看该期是否存在
  667. if(self::isExistsPeriodNum($periodNum - $i)){
  668. $periodNums[] = $periodNum - $i;
  669. }
  670. }
  671. }
  672. return array_reverse($periodNums);
  673. }
  674. /**
  675. * 获取近几期的年月
  676. * @param $num
  677. * @return array
  678. * @throws Exception
  679. */
  680. public static function getNearlyPeriodYearMonth($num){
  681. // 获取当前期
  682. $period = self::instance();
  683. $periodNum = $period->getNowPeriodNum();
  684. $nowYearMonth = $period->getNowYearMonth();
  685. // 获取前$num期的期数
  686. $periodYearMonths = [];
  687. for($i=1;$i<=$num;$i++){
  688. if($periodNum - $i > 0){
  689. // 查看该期是否存在
  690. if(self::isExistsPeriodNum($periodNum - $i)){
  691. $yearMonth = $period->getYearMonth($periodNum - $i);
  692. $periodYearMonths[] = [
  693. 'yearMonth' => $yearMonth,
  694. 'periodNum' => $periodNum - $i,
  695. ];
  696. }
  697. }
  698. }
  699. return array_reverse($periodYearMonths);
  700. }
  701. /**
  702. * 获取本月所有已经挂网的期数
  703. * @param $yearMonth
  704. * @return array
  705. */
  706. public static function monthSentAllPeriodNum($yearMonth){
  707. $year = intval(substr($yearMonth, 0, 4));
  708. $month = intval(substr($yearMonth, 4, 2));
  709. $allPeriod = Period::find()->where('CALC_YEAR=:CALC_YEAR AND CALC_MONTH=:CALC_MONTH AND IS_SENT=1', [':CALC_YEAR'=>$year, ':CALC_MONTH'=>$month])->select('PERIOD_NUM')->asArray()->all();
  710. $result = [];
  711. if($allPeriod){
  712. foreach($allPeriod as $period){
  713. $result[] = $period['PERIOD_NUM'];
  714. }
  715. }
  716. return $result;
  717. }
  718. /**
  719. * 所传月份所有已结算的期数
  720. * @param $yearMonth
  721. * @return array
  722. */
  723. public static function monthCalcAllPeriodNum($yearMonth){
  724. $year = intval(substr($yearMonth, 0, 4));
  725. $month = intval(substr($yearMonth, 4, 2));
  726. $allPeriod = Period::find()->where('CALC_YEAR=:CALC_YEAR AND CALC_MONTH=:CALC_MONTH AND IS_CALCING=1', [':CALC_YEAR'=>$year, ':CALC_MONTH'=>$month])->select('PERIOD_NUM')->asArray()->all();
  727. $result = [];
  728. if($allPeriod){
  729. foreach($allPeriod as $period){
  730. $result[] = $period['PERIOD_NUM'];
  731. }
  732. }
  733. return $result;
  734. }
  735. /**
  736. * 获取已结算的最大期数
  737. * @return mixed
  738. */
  739. public static function calculatedMaxPeriodNum(){
  740. return static::find()->where('IS_CALCULATED=1')->max('PERIOD_NUM');
  741. }
  742. /**
  743. * 已挂网的最大期数
  744. * @return mixed
  745. */
  746. public static function sentMaxPeriodNum(){
  747. return static::find()->where('IS_SENT=1')->max('PERIOD_NUM');
  748. }
  749. /**
  750. * 期数结束时间
  751. * @param $periodNum
  752. * @return int|mixed
  753. */
  754. public static function getEndTime($periodNum) {
  755. $endTime = static::find()->where('PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $periodNum])->select('END_TIME')->asArray()->one();
  756. return $endTime['END_TIME'] ?? 0;
  757. }
  758. /**
  759. * 到年底的剩余月份
  760. * @param null $periodNum
  761. * @return int
  762. * @throws Exception
  763. */
  764. public static function getMonthLeft($periodNum = null) {
  765. $period = self::instance();
  766. if ($periodNum) {
  767. $nowMonth = $period->getMonth($periodNum);
  768. } else {
  769. $nowMonth = $period->getNowMonth();
  770. }
  771. return 12 - (int)$nowMonth + 1;
  772. }
  773. /**
  774. * 起始期数和结束期数几个月
  775. * @param $startPeriodNum
  776. * @param $endPeriodNum
  777. * @return int|string
  778. */
  779. public static function getMonthNum($startPeriodNum,$endPeriodNum){
  780. return self::find()->select('CALC_MONTH')->groupBy('CALC_MONTH')->where('PERIOD_NUM>=:START_PERIOD AND PERIOD_NUM<=:END_PERIOD',[':START_PERIOD'=>$startPeriodNum,':END_PERIOD'=>$endPeriodNum])->count();
  781. }
  782. /**
  783. * 从期数获取当前月的所有期
  784. *
  785. */
  786. public static function getCurrentMonthPeriodByPeriodNum($periodNum){
  787. $currentPeriod = self::getInfoByPeriodNum($periodNum);
  788. $currentYear = $currentPeriod['CALC_YEAR'];
  789. $currentMonth = $currentPeriod['CALC_MONTH'];
  790. $periodsArray = self::findAllAsArray(['CALC_YEAR'=>$currentYear,'CALC_MONTH'=>$currentMonth]);
  791. $periods = [];
  792. foreach ($periodsArray as $p){
  793. $periods[] = $p['PERIOD_NUM'];
  794. }
  795. return $periods;
  796. }
  797. public static function getCurrentQuarterPeriodByPeriodNum($periodNum){
  798. $currentPeriod = self::getInfoByPeriodNum($periodNum);
  799. $currentYear = $currentPeriod['CALC_YEAR'];
  800. $currentMonth = $currentPeriod['CALC_MONTH'];
  801. $periodsArray = self::find()->select("*")
  802. ->where(['CALC_YEAR'=>$currentYear])
  803. ->andWhere(['in', 'CALC_MONTH', [$currentMonth-2, $currentMonth-1, $currentMonth]])
  804. ->asArray()->all();
  805. $periods = [];
  806. foreach ($periodsArray as $p){
  807. $periods[] = $p['PERIOD_NUM'];
  808. }
  809. return $periods;
  810. }
  811. public static function updatePeriodIsAutoExec($periodNum, $isAutoExec = 0): int
  812. {
  813. return Period::updateAll(['AUTO_EXEC' => $isAutoExec], ['PERIOD_NUM' => $periodNum]);
  814. }
  815. public static function updatePeriodIsProcessing($periodNum, $isProcessing = 1): int
  816. {
  817. return Period::updateAll(['IS_PROCESSING' => $isProcessing], ['PERIOD_NUM' => $periodNum]);
  818. }
  819. public static function updatePeriodIsPreparing($periodNum, $isPreparing = 1): int
  820. {
  821. return Period::updateAll(['IS_PREPARING' => $isPreparing], ['PERIOD_NUM' => $periodNum]);
  822. }
  823. /**
  824. * 是否计算操作中
  825. * @param $periodNum
  826. * @return bool
  827. */
  828. public static function isProcessing($periodNum = null): bool
  829. {
  830. $db = CalcConsole::CALC_DB_NAME;
  831. $allPeriod = \Yii::$app->$db->createCommand("SELECT * FROM AR_PERIOD order by PERIOD_NUM desc")->queryAll();
  832. $calcPeriod = [];
  833. foreach ($allPeriod as $v) {
  834. if ($v['IS_PREPARE'] > 0) {
  835. $calcPeriod = $v;
  836. break;
  837. }
  838. }
  839. $period = static::findOneAsArray(['PERIOD_NUM' => $periodNum]);
  840. // 若计算服务中CALC_ID不为空,则不允许计算
  841. if ($calcPeriod) {
  842. if ($calcPeriod['CALC_ID']) return true;
  843. }
  844. //挂网则拒绝操作,返回true则拒绝操作
  845. if ($period['IS_SENT'] == self::SEND_FINISH) return true;
  846. if ($period['IS_PROCESSING'] == self::IS_PROCESSING) return true;
  847. return false;
  848. }
  849. /**
  850. * 是否预计算操作中
  851. * @param $periodNum
  852. * @return bool
  853. */
  854. public static function isPreparing($periodNum = null): bool
  855. {
  856. $period = static::findOneAsArray(['PERIOD_NUM' => $periodNum]);
  857. //挂网则拒绝操作,返回true则拒绝操作
  858. if ($period['IS_SENT'] == self::SEND_FINISH) return true;
  859. if ($period['IS_PREPARING'] == self::IS_PREPARING) return true;
  860. $nowTs = time();
  861. if ($nowTs-6 > $period['END_TIME']) return true;
  862. return false;
  863. }
  864. public static function checkPerf($periodNum){
  865. $result = static::find()
  866. ->where(
  867. 'PERIOD_NUM=:PERIOD_NUM ', [':PERIOD_NUM' => $periodNum]
  868. )
  869. ->asArray()
  870. ->one();
  871. if (isset($result['IS_PERFED']) && $result['IS_PERFED'] == 1) {
  872. return true;
  873. }
  874. return false;
  875. }
  876. public static function checkClose($periodNum){
  877. $period = static::findOneAsArray(['PERIOD_NUM' => $periodNum]);
  878. if ($period['IS_CLOSED'] == 1) {
  879. return true;
  880. }
  881. return false;
  882. }
  883. /**
  884. * 获取期数列表
  885. * @param null $periodNum
  886. * @param int $limit
  887. * @return array|\yii\db\ActiveRecord[]
  888. */
  889. public static function getPeriodList($periodNum = null, $limit = 10) {
  890. if ($periodNum) {
  891. $periodList = static::find()->where('PERIOD_NUM >= :PERIOD_NUM', [':PERIOD_NUM' => $periodNum])->select('PERIOD_NUM,IS_SENT,IS_CLOSED')->limit($limit)->asArray()->all();
  892. } else {
  893. $periodList = static::find()->select('PERIOD_NUM,IS_SENT,IS_CLOSED')->limit($limit)->column();
  894. }
  895. return $periodList;
  896. }
  897. }