Period.php 31 KB

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