Period.php 31 KB

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