Period.php 30 KB

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