Period.php 30 KB

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