Period.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. <?php
  2. namespace common\models;
  3. use common\helpers\Date;
  4. use common\helpers\Tool;
  5. use Yii;
  6. use yii\db\Exception;
  7. /**
  8. * This is the model class for table "{{%PERIOD}}".
  9. *
  10. * @property string $ID
  11. * @property int $PERIOD_NUM 期数
  12. * @property int $CALC_MONTH 所在结算月
  13. * @property int $CALC_YEAR 所在结算年
  14. * @property int $START_TIME 期数开始时间戳
  15. * @property int $END_TIME 期数结束时间戳
  16. * @property int $IS_MONTH 是否结算月节点
  17. * @property int $IS_YEAR 是否结算年节点
  18. * @property int $IS_CLOSED 是否已封期
  19. * @property int $IS_PERFED 是否已生成业绩单
  20. * @property int $IS_CALCULATED 是否已结算
  21. * @property int $IS_SENT 是否已发放
  22. * @property int $IS_PERFING 是否正在生成业绩单
  23. * @property int $IS_CALCING 是否正在计算状态
  24. * @property int $IS_SENDING 是否正在挂网状态
  25. * @property int $CALC_PERCENT 结算进度
  26. * @property int $SENT_PERCENT 发放进度
  27. * @property string $PERF_ADMIN_ID 生成业绩单管理员
  28. * @property string $CLOSE_ADMIN_ID 手动封期管理员ID
  29. * @property string $CALC_ADMIN_ID 结算管理员ID
  30. * @property string $SENT_ADMIN_ID 发放管理员ID
  31. * @property int $CLOSED_AT 发放管理员ID
  32. * @property int $PERF_STARTED_AT 生成业绩单开始时间
  33. * @property int $PERFED_AT 生成业绩单结束时间
  34. * @property int $CALCULATE_STARTED_AT 结算开始时间
  35. * @property int $CALCULATED_AT 结算完成时间
  36. * @property int $SEND_STARTED_AT 发放开始时间
  37. * @property int $SENT_AT 发放完成时间
  38. * @property int $CREATED_AT 创建时间
  39. */
  40. class Period extends \common\components\ActiveRecord
  41. {
  42. const CALCULATE_NONE = 0;
  43. const CALCULATE_FINISH = 1;
  44. const CALCULATE_FAIL = 2;
  45. const SEND_NONE = 0;
  46. const SEND_FINISH = 1;
  47. const SEND_FAIL = 2;
  48. const PERF_NONE = 0;
  49. const PERF_FINISH = 1;
  50. const PERF_FAIL = 2;
  51. const SYSTEM_START_PERIOD_NUM = 100;
  52. public $nowPeriodArr = null;
  53. public $periodNum = null;
  54. public $periodArr = null;
  55. public function init()
  56. {
  57. parent::init();
  58. }
  59. /**
  60. * @inheritdoc
  61. */
  62. public static function tableName()
  63. {
  64. return '{{%PERIOD}}';
  65. }
  66. /**
  67. * @inheritdoc
  68. */
  69. public function rules()
  70. {
  71. return [
  72. [['PERIOD_NUM', 'CALC_MONTH', 'CALC_YEAR', 'START_TIME', 'END_TIME', 'CREATED_AT'], 'required'],
  73. [['PERIOD_NUM', 'CALC_MONTH', 'CALC_YEAR', 'START_TIME', 'END_TIME', 'IS_MONTH', 'IS_YEAR', 'IS_PERFED', 'IS_CALCULATED', 'IS_SENT', 'IS_PERFING', 'IS_CALCING', 'CALC_PERCENT', 'CLOSED_AT', 'PERF_STARTED_AT', 'PERFED_AT', 'CALCULATE_STARTED_AT', 'CALCULATED_AT','CREATED_AT'], 'integer'],
  74. [['ID'], 'string', 'max' => 32],
  75. [['PERIOD_NUM'], 'unique'],
  76. [['ID'], 'unique'],
  77. ];
  78. }
  79. /**
  80. * @inheritdoc
  81. */
  82. public function attributeLabels()
  83. {
  84. return [
  85. 'ID' => 'ID',
  86. 'PERIOD_NUM' => '期数',
  87. 'CALC_MONTH' => '所在结算月',
  88. 'CALC_YEAR' => '所在结算年',
  89. 'START_TIME' => '期数开始时间戳',
  90. 'END_TIME' => '期数结束时间戳',
  91. 'IS_MONTH' => '是否结算月节点',
  92. 'IS_YEAR' => '是否结算年节点',
  93. 'IS_PERFED' => '是否已生成业绩单',
  94. 'IS_CALCULATED' => '是否已结算',
  95. 'IS_PERFING' => '是否正在生成业绩单',
  96. 'IS_CALCING' => '是否正在计算状态',
  97. 'CALC_PERCENT' => '结算进度',
  98. 'CLOSED_AT' => '发放管理员ID',
  99. 'PERF_STARTED_AT' => '生成业绩单开始时间',
  100. 'PERFED_AT' => '生成业绩单结束时间',
  101. 'CALCULATE_STARTED_AT' => '结算开始时间',
  102. 'CALCULATED_AT' => '结算完成时间',
  103. 'CREATED_AT' => '创建时间',
  104. ];
  105. }
  106. /**
  107. * 期数赋值给属性
  108. * @param int $periodNum
  109. * @return array|null|\yii\db\ActiveRecord
  110. */
  111. public function setPeriodNum($periodNum = null){
  112. if (empty($periodNum)) {
  113. $this->nowPeriodArr = static::find()
  114. ->where('IS_PREPARE=1')
  115. ->orderBy('PERIOD_NUM ASC')
  116. ->asArray()
  117. ->one();
  118. if (empty($this->nowPeriodArr)) {
  119. return false;
  120. }
  121. $this->periodArr = $this->nowPeriodArr;
  122. $periodNum = $this->nowPeriodArr['PERIOD_NUM'];
  123. } else {
  124. $this->periodArr = static::findOneAsArray(
  125. 'PERIOD_NUM=:PERIOD_NUM AND IS_PREPARE=1',
  126. [':PERIOD_NUM'=>$periodNum]
  127. );
  128. if (empty($this->periodArr)) {
  129. return false;
  130. }
  131. }
  132. $this->periodNum = $periodNum;
  133. return $this->periodArr;
  134. }
  135. // 通过期数,获取此期数据
  136. public static function getInfoByPeriodNum($periodNum) {
  137. return static::findOneAsArray(['PERIOD_NUM'=>$periodNum]);
  138. }
  139. /**
  140. * 是否存在所传期数
  141. * @param int $periodNum
  142. * @return bool
  143. */
  144. public static function isExistsPeriodNum(int $periodNum){
  145. return static::find()->where(['PERIOD_NUM'=>$periodNum])->exists();
  146. }
  147. /**
  148. * 获取当前期的开始时间
  149. * @return int
  150. */
  151. public function getNowPeriodStart(){
  152. $this->setPeriodNum();
  153. if($this->nowPeriodArr){
  154. return $this->nowPeriodArr['START_TIME'];
  155. } else {
  156. return Date::nowTime();
  157. }
  158. }
  159. /**
  160. * 获取当前期的结束时间
  161. * @return int
  162. */
  163. public function getNowPeriodEnd(){
  164. $this->setPeriodNum();
  165. if($this->nowPeriodArr){
  166. return $this->nowPeriodArr['END_TIME'];
  167. } else {
  168. return Date::nowTime();
  169. }
  170. }
  171. /**
  172. * 获取当前所在的结算月
  173. * @return int
  174. */
  175. public function getNowMonth(){
  176. $this->setPeriodNum();
  177. if($this->nowPeriodArr){
  178. return $this->nowPeriodArr['CALC_MONTH'];
  179. } else {
  180. return 0;
  181. }
  182. }
  183. /**
  184. * 获取当前期数所在结算年
  185. * @return int
  186. */
  187. public function getNowYear(){
  188. $this->setPeriodNum();
  189. if($this->nowPeriodArr){
  190. return $this->nowPeriodArr['CALC_YEAR'];
  191. } else {
  192. return 0;
  193. }
  194. }
  195. /**
  196. * 当前期数所在年月
  197. * @return int|string
  198. */
  199. public function getNowYearMonth(){
  200. $this->setPeriodNum();
  201. if($this->nowPeriodArr){
  202. return $this->nowPeriodArr['CALC_YEAR'].Tool::numFix($this->nowPeriodArr['CALC_MONTH']);
  203. } else {
  204. return 0;
  205. }
  206. }
  207. /**
  208. * 获取期数所在的结算月
  209. * @param int $periodNum
  210. * @return mixed
  211. * @throws Exception
  212. */
  213. public function getMonth($periodNum = null){
  214. $period = $this->setPeriodNum($periodNum);
  215. if($period){
  216. return $period['CALC_MONTH'];
  217. } else {
  218. throw new Exception('期数不存在');
  219. }
  220. }
  221. /**
  222. * 获取期数所在的结算年
  223. * @param $periodNum
  224. * @return mixed
  225. * @throws Exception
  226. */
  227. public function getYear($periodNum = null){
  228. $period = $this->setPeriodNum($periodNum);
  229. if($period){
  230. return $period['CALC_YEAR'];
  231. } else {
  232. throw new Exception('期数不存在');
  233. }
  234. }
  235. /**
  236. * 期数所在年月
  237. * @param $periodNum
  238. * @return string
  239. * @throws Exception
  240. */
  241. public function getYearMonth($periodNum = null){
  242. $period = $this->setPeriodNum($periodNum);
  243. if($period){
  244. return $period['CALC_YEAR'].Tool::numFix($period['CALC_MONTH'], 2);
  245. } else {
  246. throw new Exception('期数不存在'.$periodNum);
  247. }
  248. }
  249. /**
  250. * 所传年、月所有期数的数量
  251. * @param $year
  252. * @param $month
  253. * @return int
  254. */
  255. public function getYearMonthAllPeriodNumCount($year, $month){
  256. return Period::find()->where('CALC_YEAR=:CALC_YEAR AND CALC_MONTH=:CALC_MONTH', [':CALC_YEAR'=>$year, ':CALC_MONTH'=>$month])->count(1);
  257. }
  258. /**
  259. * 是否是结算月节点
  260. * @param $periodNum
  261. * @return mixed
  262. * @throws Exception
  263. */
  264. public function isCalcMonth($periodNum = null){
  265. $period = $this->setPeriodNum($periodNum);
  266. if($period){
  267. return $period['IS_MONTH'];
  268. } else {
  269. throw new Exception('期数不存在');
  270. }
  271. }
  272. /**
  273. * 是否是结算年节点
  274. * @param $periodNum
  275. * @return mixed
  276. * @throws Exception
  277. */
  278. public function isCalcYear($periodNum = null){
  279. $period = $this->setPeriodNum($periodNum);
  280. if($period){
  281. return $period['IS_YEAR'];
  282. } else {
  283. throw new Exception('期数不存在');
  284. }
  285. }
  286. /**
  287. * 返回所传结算月包含的所有期数
  288. * @param $year
  289. * @param $month
  290. * @return array|\yii\db\ActiveRecord[]
  291. */
  292. public static function getPeriodNumsFromMonth($year, $month){
  293. 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();
  294. }
  295. /**
  296. * 获取所传结算月的最小期数和最大期数
  297. * @param $year
  298. * @param $month
  299. * @return array
  300. */
  301. public static function getPeriodNumRangeFromMonth($year, $month){
  302. $allPeriod = self::getPeriodNumsFromMonth($year, $month);
  303. $tempPeriod = [];
  304. foreach($allPeriod as $period){
  305. $tempPeriod[] = $period['PERIOD_NUM'];
  306. }
  307. if(!$tempPeriod){
  308. return null;
  309. }
  310. $minPeriod = min($tempPeriod);
  311. $maxPeriod = max($tempPeriod);
  312. return ['min'=>$minPeriod, 'max'=>$maxPeriod];
  313. }
  314. /**
  315. * 返回结算月的节点期数
  316. * @param $year
  317. * @param $month
  318. * @return mixed|null
  319. */
  320. public static function getPeriodNumPointFromMonth($year, $month) {
  321. $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();
  322. if($data) return $data['PERIOD_NUM'];
  323. else return null;
  324. }
  325. /**
  326. * 返回结算月的节点期
  327. * @param $year
  328. * @param $month
  329. * @return mixed|null
  330. */
  331. public static function getPeriodPointFromMonth($year, $month) {
  332. $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();
  333. if($data) return $data;
  334. else return null;
  335. }
  336. // 奖金计算,更新业绩表,进度百分比
  337. public static function updatePercent($percent, $periodNum) {
  338. $result = Period::updateAll(
  339. ['PERF_PERCENT' => $percent],
  340. 'PERIOD_NUM=:PERIOD_NUM',
  341. [':PERIOD_NUM' => $periodNum]
  342. );
  343. return $result;
  344. }
  345. /**
  346. * 更新计算进度
  347. */
  348. public static function updateCalcProcess($perpare, $periodNum) {
  349. $result = Period::updateAll(
  350. ['IS_PREPARE' => $perpare],
  351. 'PERIOD_NUM=:PERIOD_NUM',
  352. [':PERIOD_NUM' => $periodNum]
  353. );
  354. return $result;
  355. }
  356. }