Period.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991
  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. public function getTeamsPeriodNum(){
  168. $this->nowPeriodArr = static::find()->where('IS_CLOSED=1 AND IS_SENT=0')->orderBy('PERIOD_NUM ASC')->asArray()->one();
  169. $this->setPeriodNum($this->nowPeriodArr['PERIOD_NUM']);
  170. if($this->nowPeriodArr){
  171. return $this->nowPeriodArr['PERIOD_NUM'];
  172. } else {
  173. return self::SYSTEM_START_PERIOD_NUM;
  174. }
  175. }
  176. /**
  177. * 获取当前期的开始时间
  178. * @return int
  179. */
  180. public function getNowPeriodStart(){
  181. $this->setPeriodNum();
  182. if($this->nowPeriodArr){
  183. return $this->nowPeriodArr['START_TIME'];
  184. } else {
  185. return Date::nowTime();
  186. }
  187. }
  188. /**
  189. * 获取当前期的结束时间
  190. * @return int
  191. */
  192. public function getNowPeriodEnd(){
  193. $this->setPeriodNum();
  194. if($this->nowPeriodArr){
  195. return $this->nowPeriodArr['END_TIME'];
  196. } else {
  197. return Date::nowTime();
  198. }
  199. }
  200. /**
  201. * 获取当前所在的结算月
  202. * @return int
  203. */
  204. public function getNowMonth(){
  205. $this->setPeriodNum();
  206. if($this->nowPeriodArr){
  207. return $this->nowPeriodArr['CALC_MONTH'];
  208. } else {
  209. return 0;
  210. }
  211. }
  212. /**
  213. * 获取当前期数所在结算年
  214. * @return int
  215. */
  216. public function getNowYear(){
  217. $this->setPeriodNum();
  218. if($this->nowPeriodArr){
  219. return $this->nowPeriodArr['CALC_YEAR'];
  220. } else {
  221. return 0;
  222. }
  223. }
  224. /**
  225. * 当前期数所在年月
  226. * @return int|string
  227. */
  228. public function getNowYearMonth(){
  229. $this->setPeriodNum();
  230. if($this->nowPeriodArr){
  231. return $this->nowPeriodArr['CALC_YEAR'].Tool::numFix($this->nowPeriodArr['CALC_MONTH']);
  232. } else {
  233. return 0;
  234. }
  235. }
  236. public function getTeamsYearMonth($periodNum){
  237. $this->setPeriodNum($periodNum);
  238. if($this->nowPeriodArr){
  239. return $this->nowPeriodArr['CALC_YEAR'].Tool::numFix($this->nowPeriodArr['CALC_MONTH']);
  240. } else {
  241. return 0;
  242. }
  243. }
  244. /**
  245. * 获取期数所在的结算月
  246. * @param int $periodNum
  247. * @return mixed
  248. * @throws Exception
  249. */
  250. public function getMonth($periodNum = null){
  251. $period = $this->setPeriodNum($periodNum);
  252. if($period){
  253. return $period['CALC_MONTH'];
  254. } else {
  255. throw new Exception('期数不存在');
  256. }
  257. }
  258. /**
  259. * 获取期数所在的结算年
  260. * @param $periodNum
  261. * @return mixed
  262. * @throws Exception
  263. */
  264. public function getYear($periodNum = null){
  265. $period = $this->setPeriodNum($periodNum);
  266. if($period){
  267. return $period['CALC_YEAR'];
  268. } else {
  269. throw new Exception('期数不存在');
  270. }
  271. }
  272. /**
  273. * 期数所在年月
  274. * @param $periodNum
  275. * @return string
  276. * @throws Exception
  277. */
  278. public function getYearMonth($periodNum = null){
  279. $period = $this->setPeriodNum($periodNum);
  280. if($period){
  281. return $period['CALC_YEAR'].Tool::numFix($period['CALC_MONTH'], 2);
  282. } else {
  283. throw new Exception('期数不存在'.$periodNum);
  284. }
  285. }
  286. /**
  287. * 所传年、月所有期数的数量
  288. * @param $year
  289. * @param $month
  290. * @return int
  291. */
  292. public function getYearMonthAllPeriodNumCount($year, $month){
  293. return Period::find()->where('CALC_YEAR=:CALC_YEAR AND CALC_MONTH=:CALC_MONTH', [':CALC_YEAR'=>$year, ':CALC_MONTH'=>$month])->count(1);
  294. }
  295. /**
  296. * 是否是结算月节点
  297. * @param $periodNum
  298. * @return mixed
  299. * @throws Exception
  300. */
  301. public function isCalcMonth($periodNum = null){
  302. $period = $this->setPeriodNum($periodNum);
  303. if($period){
  304. return $period['IS_MONTH'];
  305. } else {
  306. throw new Exception('期数不存在');
  307. }
  308. }
  309. /**
  310. * 是否是结算年节点
  311. * @param $periodNum
  312. * @return mixed
  313. * @throws Exception
  314. */
  315. public function isCalcYear($periodNum = null){
  316. $period = $this->setPeriodNum($periodNum);
  317. if($period){
  318. return $period['IS_YEAR'];
  319. } else {
  320. throw new Exception('期数不存在');
  321. }
  322. }
  323. /**
  324. * 获取挂网的最大期数
  325. * @return int
  326. */
  327. public function getMaxIsSentPeriodNum() {
  328. $maxIsSentData = static::find()->where('IS_SENT=1')->orderBy('PERIOD_NUM DESC')->asArray()->one();
  329. $maxIsSentPeriodNum = self::SYSTEM_START_PERIOD_NUM;
  330. if( $maxIsSentData && $maxIsSentData["PERIOD_NUM"] ) {
  331. $maxIsSentPeriodNum = $maxIsSentData["PERIOD_NUM"];
  332. }
  333. return $maxIsSentPeriodNum;
  334. }
  335. /**
  336. * 返回所传结算月包含的所有期数
  337. * @param $year
  338. * @param $month
  339. * @return array|\yii\db\ActiveRecord[]
  340. */
  341. public static function getPeriodNumsFromMonth($year, $month){
  342. 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();
  343. }
  344. /**
  345. * 获取所传结算月的最小期数和最大期数
  346. * @param $year
  347. * @param $month
  348. * @return array
  349. */
  350. public static function getPeriodNumRangeFromMonth($year, $month){
  351. $allPeriod = self::getPeriodNumsFromMonth($year, $month);
  352. $tempPeriod = [];
  353. foreach($allPeriod as $period){
  354. $tempPeriod[] = $period['PERIOD_NUM'];
  355. }
  356. if(!$tempPeriod){
  357. return null;
  358. }
  359. $minPeriod = min($tempPeriod);
  360. $maxPeriod = max($tempPeriod);
  361. return ['min'=>$minPeriod, 'max'=>$maxPeriod];
  362. }
  363. /**
  364. * 返回结算月的节点期数
  365. * @param $year
  366. * @param $month
  367. * @return mixed|null
  368. */
  369. public static function getPeriodNumPointFromMonth($year, $month) {
  370. $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();
  371. if($data) return $data['PERIOD_NUM'];
  372. else return null;
  373. }
  374. /**
  375. * 返回结算月的节点期
  376. * @param $year
  377. * @param $month
  378. * @return mixed|null
  379. */
  380. public static function getPeriodPointFromMonth($year, $month) {
  381. $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();
  382. if($data) return $data;
  383. else return null;
  384. }
  385. /**
  386. * 是否已经封期
  387. * @param $periodNum
  388. * @return bool
  389. * @throws Exception
  390. */
  391. public function isClosed($periodNum = null){
  392. $period = $this->setPeriodNum($periodNum);
  393. if($period){
  394. return boolval($period['IS_CLOSED']);
  395. } else {
  396. throw new Exception('期数不存在');
  397. }
  398. }
  399. /**
  400. * 是否已经生成业绩单
  401. * @param $periodNum
  402. * @return bool
  403. * @throws Exception
  404. */
  405. public function isPerfed($periodNum = null){
  406. $period = $this->setPeriodNum($periodNum);
  407. if($period){
  408. return boolval($period['IS_PERFED']);
  409. } else {
  410. throw new Exception('期数不存在');
  411. }
  412. }
  413. /**
  414. * 是否已结算
  415. * @param null $periodNum
  416. * @return bool
  417. * @throws Exception
  418. */
  419. public function isCalculated($periodNum = null){
  420. $period = $this->setPeriodNum($periodNum);
  421. if($period){
  422. return boolval($period['IS_CALCULATED'] == self::CALCULATE_FINISH);
  423. } else {
  424. throw new Exception('期数不存在');
  425. }
  426. }
  427. /**
  428. * 是否可被封期
  429. * @param $periodNum
  430. * @return bool
  431. */
  432. public function isCanClose($periodNum = null){
  433. $period = $this->setPeriodNum($periodNum);
  434. if($period['IS_CLOSED']){
  435. return false;
  436. }
  437. $previousPeriod = static::findOne(['PERIOD_NUM'=>$this->periodNum-1]);
  438. if($previousPeriod){
  439. if(!$previousPeriod['IS_CLOSED']){
  440. return false;
  441. }
  442. // 只要上期封期了,下期就可以封期
  443. // if(!$previousPeriod['IS_CALCULATED']){
  444. // return false;
  445. // }
  446. // if(!$previousPeriod['IS_SENT']){
  447. // return false;
  448. // }
  449. }
  450. return true;
  451. }
  452. /**
  453. * 是否可以生成业绩单
  454. * @param $periodNum
  455. * @return bool
  456. */
  457. public function isCanPerf($periodNum = null){
  458. $period = $this->setPeriodNum($periodNum);
  459. // 查看该期是否已经封期
  460. if(!$period['IS_CLOSED']){
  461. return false;
  462. }
  463. // 已经挂网,不能生成业绩单
  464. if($period['IS_SENT'] == self::SEND_FINISH){
  465. return false;
  466. }
  467. //上1期奖金未发放,限制不能生成业绩单
  468. if(!$this->isLastSent($this->periodNum)){
  469. return false;
  470. }
  471. return true;
  472. }
  473. /**
  474. * 是否可以结算
  475. * @param $periodNum
  476. * @return bool
  477. */
  478. public function isCanCalc($periodNum = null){
  479. $period = $this->setPeriodNum($periodNum);
  480. // 查看该期是否已经封期
  481. if(!$period['IS_CLOSED']){
  482. return false;
  483. }
  484. // 还没有生成业绩单,不能封期
  485. if(!$period['IS_PERFED'] || $period['IS_PERFED'] == self::PERF_FAIL){
  486. return false;
  487. }
  488. if($period['IS_SENT'] == self::SEND_FINISH){
  489. return false;
  490. }
  491. // $previousPeriod = static::findOneAsArray(['PERIOD_NUM'=>$this->periodNum-1]);
  492. // if($previousPeriod){
  493. // // 查看上一期是否已经挂网,上一期挂网了,本期才能结算
  494. // if(!$previousPeriod['IS_SENT']){
  495. // return false;
  496. // }
  497. // }
  498. //上1期奖金未发放,限制不能结算
  499. if(!$this->isLastSent($this->periodNum)){
  500. return false;
  501. }
  502. return true;
  503. }
  504. /**
  505. * 是否可以挂网
  506. * @param $periodNum
  507. * @return bool
  508. */
  509. public function isCanSend($periodNum = null){
  510. $period = $this->setPeriodNum($periodNum);
  511. if(!$period['IS_CLOSED']){
  512. return false;
  513. }
  514. if(!$period['IS_CALCULATED'] || $period['IS_CALCULATED'] == self::CALCULATE_FAIL){
  515. return false;
  516. }
  517. if($period['IS_SENT'] == self::SEND_FINISH){
  518. return false;
  519. }
  520. //上1期奖金未发放,限制不能挂网
  521. if(!$this->isLastSent($this->periodNum)){
  522. return false;
  523. }
  524. return true;
  525. }
  526. /**
  527. * 是否已经挂网
  528. * @param $periodNum
  529. * @return bool
  530. */
  531. public function isSent($periodNum = null){
  532. $period = $this->setPeriodNum($periodNum);
  533. if($period['IS_SENT'] == self::SEND_FINISH) return true;
  534. return false;
  535. }
  536. /**
  537. * 上期是否挂网
  538. * @param null $periodNum
  539. * @return bool
  540. */
  541. public function isLastSent($periodNum){
  542. $period = $this->setPeriodNum($periodNum-1);
  543. if ($period) {
  544. if($period['IS_SENT'] == self::SEND_FINISH) {
  545. return true;
  546. } else {
  547. return false;
  548. }
  549. }
  550. return false;
  551. }
  552. /**
  553. * 获取上月的年月
  554. * @param $periodNum
  555. * @return array
  556. */
  557. public function getLastMonth($periodNum = null){
  558. $period = $this->setPeriodNum($periodNum);
  559. if($period){
  560. $year = $period['CALC_YEAR'];
  561. $month = $period['CALC_MONTH'];
  562. $lastYear = Date::lastMonth($year.'-'.$month, 'Y');
  563. $lastMonth = Date::lastMonth($year.'-'.$month, 'm');
  564. return [
  565. 'year' => $lastYear,
  566. 'month' => intval($lastMonth),
  567. 'yearMonth' => $lastYear.Tool::numFix($lastMonth, 2),
  568. ];
  569. } else {
  570. return [
  571. 'year' => 0,
  572. 'month' => 0,
  573. 'yearMonth' => 0,
  574. ];
  575. }
  576. }
  577. /**
  578. * 上几个月的年月
  579. * @param int $num
  580. * @param null $periodNum
  581. * @return array
  582. */
  583. public function getLastNumMonth($num=1,$periodNum = null){
  584. $period = $this->setPeriodNum($periodNum);
  585. if($period){
  586. $year = $period['CALC_YEAR'];
  587. $month = $period['CALC_MONTH'];
  588. $lastYear = Date::lastNumMonth($num,$year.'-'.$month, 'Y');
  589. $lastMonth = Date::lastNumMonth($num,$year.'-'.$month, 'm');
  590. return [
  591. 'year' => $lastYear,
  592. 'month' => intval($lastMonth),
  593. 'yearMonth' => $lastYear.Tool::numFix($lastMonth, 2),
  594. ];
  595. } else {
  596. return [
  597. 'year' => 0,
  598. 'month' => 0,
  599. 'yearMonth' => 0,
  600. ];
  601. }
  602. }
  603. /**
  604. * 下月的年月
  605. * @param null $periodNum
  606. * @return array
  607. */
  608. public function getNextMonth($periodNum = null){
  609. $period = $this->setPeriodNum($periodNum);
  610. if($period){
  611. $year = $period['CALC_YEAR'];
  612. $month = $period['CALC_MONTH'];
  613. $nextYear = Date::nextMonth($year.'-'.$month, 'Y');
  614. $nextMonth = Date::nextMonth($year.'-'.$month, 'm');
  615. return [
  616. 'year' => $nextYear,
  617. 'month' => intval($nextMonth),
  618. 'yearMonth' => $nextYear.Tool::numFix($nextMonth, 2),
  619. ];
  620. } else {
  621. return [
  622. 'year' => 0,
  623. 'month' => 0,
  624. 'yearMonth' => 0,
  625. ];
  626. }
  627. }
  628. /**
  629. * 通过时间戳获取期
  630. * @param $time
  631. * @return array|null|\yii\db\ActiveRecord
  632. */
  633. public static function getPeriodFromTime($time){
  634. return static::find()->where('START_TIME<=:TIME AND END_TIME>:TIME', [':TIME'=>$time])->asArray()->one();
  635. }
  636. /**
  637. * 通过时间戳范围获取期间内的期
  638. * @param $minTime
  639. * @param $maxTime
  640. * @return array|\yii\db\ActiveRecord[]
  641. */
  642. public static function getPeriodFromTimeRange($minTime, $maxTime){
  643. 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();
  644. }
  645. /**
  646. * 获取近几期的期数
  647. * @param $num
  648. * @return array
  649. */
  650. public static function getNearlyPeriodNum($num){
  651. // 获取当前期
  652. $period = self::instance();
  653. $periodNum = $period->getNowPeriodNum();
  654. // 获取前$num期的期数
  655. $periodNums = [
  656. $periodNum
  657. ];
  658. for($i=0;$i<$num;$i++){
  659. if($periodNum - $i > 0){
  660. // 查看该期是否存在
  661. if(self::isExistsPeriodNum($periodNum - $i)){
  662. $periodNums[] = $periodNum - $i;
  663. }
  664. }
  665. }
  666. return array_reverse($periodNums);
  667. }
  668. /**
  669. * 获取近几期的挂网期数
  670. * @param $num
  671. * @return array
  672. */
  673. public static function getNearlySendPeriodNum($num){
  674. // 获取当前期
  675. $period = self::instance();
  676. $periodNum = $period->sentMaxPeriodNum();
  677. // 获取前$num期的期数
  678. $periodNums = [
  679. $periodNum
  680. ];
  681. for($i=0;$i<$num;$i++){
  682. if($periodNum - $i > 0){
  683. // 查看该期是否存在
  684. if(self::isExistsPeriodNum($periodNum - $i)){
  685. $periodNums[] = $periodNum - $i;
  686. }
  687. }
  688. }
  689. return array_reverse($periodNums);
  690. }
  691. /**
  692. * 获取近几期的年月
  693. * @param $num
  694. * @return array
  695. * @throws Exception
  696. */
  697. public static function getNearlyPeriodYearMonth($num){
  698. // 获取当前期
  699. $period = self::instance();
  700. $periodNum = $period->getNowPeriodNum();
  701. $nowYearMonth = $period->getNowYearMonth();
  702. // 获取前$num期的期数
  703. $periodYearMonths = [];
  704. for($i=1;$i<=$num;$i++){
  705. if($periodNum - $i > 0){
  706. // 查看该期是否存在
  707. if(self::isExistsPeriodNum($periodNum - $i)){
  708. $yearMonth = $period->getYearMonth($periodNum - $i);
  709. $periodYearMonths[] = [
  710. 'yearMonth' => $yearMonth,
  711. 'periodNum' => $periodNum - $i,
  712. ];
  713. }
  714. }
  715. }
  716. return array_reverse($periodYearMonths);
  717. }
  718. /**
  719. * 获取本月所有已经挂网的期数
  720. * @param $yearMonth
  721. * @return array
  722. */
  723. public static function monthSentAllPeriodNum($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_SENT=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. * @param $yearMonth
  738. * @return array
  739. */
  740. public static function monthCalcAllPeriodNum($yearMonth){
  741. $year = intval(substr($yearMonth, 0, 4));
  742. $month = intval(substr($yearMonth, 4, 2));
  743. $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();
  744. $result = [];
  745. if($allPeriod){
  746. foreach($allPeriod as $period){
  747. $result[] = $period['PERIOD_NUM'];
  748. }
  749. }
  750. return $result;
  751. }
  752. /**
  753. * 获取已结算的最大期数
  754. * @return mixed
  755. */
  756. public static function calculatedMaxPeriodNum(){
  757. return static::find()->where('IS_CALCULATED=1')->max('PERIOD_NUM');
  758. }
  759. /**
  760. * 已挂网的最大期数
  761. * @return mixed
  762. */
  763. public static function sentMaxPeriodNum(){
  764. return static::find()->where('IS_SENT=1')->max('PERIOD_NUM');
  765. }
  766. /**
  767. * 期数结束时间
  768. * @param $periodNum
  769. * @return int|mixed
  770. */
  771. public static function getEndTime($periodNum) {
  772. $endTime = static::find()->where('PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $periodNum])->select('END_TIME')->asArray()->one();
  773. return $endTime['END_TIME'] ?? 0;
  774. }
  775. /**
  776. * 到年底的剩余月份
  777. * @param null $periodNum
  778. * @return int
  779. * @throws Exception
  780. */
  781. public static function getMonthLeft($periodNum = null) {
  782. $period = self::instance();
  783. if ($periodNum) {
  784. $nowMonth = $period->getMonth($periodNum);
  785. } else {
  786. $nowMonth = $period->getNowMonth();
  787. }
  788. return 12 - (int)$nowMonth + 1;
  789. }
  790. /**
  791. * 起始期数和结束期数几个月
  792. * @param $startPeriodNum
  793. * @param $endPeriodNum
  794. * @return int|string
  795. */
  796. public static function getMonthNum($startPeriodNum,$endPeriodNum){
  797. 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();
  798. }
  799. /**
  800. * 从期数获取当前月的所有期
  801. *
  802. */
  803. public static function getCurrentMonthPeriodByPeriodNum($periodNum){
  804. $currentPeriod = self::getInfoByPeriodNum($periodNum);
  805. $currentYear = $currentPeriod['CALC_YEAR'];
  806. $currentMonth = $currentPeriod['CALC_MONTH'];
  807. $periodsArray = self::findAllAsArray(['CALC_YEAR'=>$currentYear,'CALC_MONTH'=>$currentMonth]);
  808. $periods = [];
  809. foreach ($periodsArray as $p){
  810. $periods[] = $p['PERIOD_NUM'];
  811. }
  812. return $periods;
  813. }
  814. public static function getCurrentQuarterPeriodByPeriodNum($periodNum){
  815. $currentPeriod = self::getInfoByPeriodNum($periodNum);
  816. $currentYear = $currentPeriod['CALC_YEAR'];
  817. $currentMonth = $currentPeriod['CALC_MONTH'];
  818. $periodsArray = self::find()->select("*")
  819. ->where(['CALC_YEAR'=>$currentYear])
  820. ->andWhere(['in', 'CALC_MONTH', [$currentMonth-2, $currentMonth-1, $currentMonth]])
  821. ->asArray()->all();
  822. $periods = [];
  823. foreach ($periodsArray as $p){
  824. $periods[] = $p['PERIOD_NUM'];
  825. }
  826. return $periods;
  827. }
  828. public static function updatePeriodIsAutoExec($periodNum, $isAutoExec = 0): int
  829. {
  830. return Period::updateAll(['AUTO_EXEC' => $isAutoExec], ['PERIOD_NUM' => $periodNum]);
  831. }
  832. public static function updatePeriodIsProcessing($periodNum, $isProcessing = 1): int
  833. {
  834. return Period::updateAll(['IS_PROCESSING' => $isProcessing], ['PERIOD_NUM' => $periodNum]);
  835. }
  836. public static function updatePeriodIsPreparing($periodNum, $isPreparing = 1): int
  837. {
  838. return Period::updateAll(['IS_PREPARING' => $isPreparing], ['PERIOD_NUM' => $periodNum]);
  839. }
  840. /**
  841. * 是否计算操作中
  842. * @param $periodNum
  843. * @return bool
  844. */
  845. public static function isProcessing($periodNum = null): bool
  846. {
  847. $db = CalcConsole::CALC_DB_NAME;
  848. $allPeriod = \Yii::$app->$db->createCommand("SELECT * FROM AR_PERIOD order by PERIOD_NUM desc")->queryAll();
  849. $calcPeriod = [];
  850. foreach ($allPeriod as $v) {
  851. if ($v['IS_PREPARE'] > 0) {
  852. $calcPeriod = $v;
  853. break;
  854. }
  855. }
  856. $period = static::findOneAsArray(['PERIOD_NUM' => $periodNum]);
  857. // 若计算服务中CALC_ID不为空,则不允许计算
  858. if ($calcPeriod) {
  859. if ($calcPeriod['CALC_ID']) return true;
  860. }
  861. //挂网则拒绝操作,返回true则拒绝操作
  862. if ($period['IS_SENT'] == self::SEND_FINISH) return true;
  863. if ($period['IS_PROCESSING'] == self::IS_PROCESSING) return true;
  864. return false;
  865. }
  866. /**
  867. * 是否预计算操作中
  868. * @param $periodNum
  869. * @return bool
  870. */
  871. public static function isPreparing($periodNum = null): bool
  872. {
  873. $period = static::findOneAsArray(['PERIOD_NUM' => $periodNum]);
  874. //挂网则拒绝操作,返回true则拒绝操作
  875. if ($period['IS_SENT'] == self::SEND_FINISH) return true;
  876. if ($period['IS_PREPARING'] == self::IS_PREPARING) return true;
  877. $nowTs = time();
  878. if ($nowTs-6 > $period['END_TIME']) return true;
  879. return false;
  880. }
  881. public static function checkPerf($periodNum){
  882. $result = static::find()
  883. ->where(
  884. 'PERIOD_NUM=:PERIOD_NUM ', [':PERIOD_NUM' => $periodNum]
  885. )
  886. ->asArray()
  887. ->one();
  888. if (isset($result['IS_PERFED']) && $result['IS_PERFED'] == 1) {
  889. return true;
  890. }
  891. return false;
  892. }
  893. public static function checkClose($periodNum){
  894. $period = static::findOneAsArray(['PERIOD_NUM' => $periodNum]);
  895. if ($period['IS_CLOSED'] == 1) {
  896. return true;
  897. }
  898. return false;
  899. }
  900. /**
  901. * 获取期数列表
  902. * @param null $periodNum
  903. * @param int $limit
  904. * @return array|\yii\db\ActiveRecord[]
  905. */
  906. public static function getPeriodList($periodNum = null, $limit = 10) {
  907. if ($periodNum) {
  908. $periodList = static::find()->where('PERIOD_NUM >= :PERIOD_NUM', [':PERIOD_NUM' => $periodNum])->select('PERIOD_NUM,IS_SENT,IS_CLOSED')->limit($limit)->asArray()->all();
  909. } else {
  910. $periodList = static::find()->select('PERIOD_NUM,IS_SENT,IS_CLOSED')->limit($limit)->column();
  911. }
  912. return $periodList;
  913. }
  914. }