Contract.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. <?php
  2. namespace app\admin\controller\qingdong\statistic;
  3. use addons\qingdong\model\Form;
  4. use addons\qingdong\model\Receivables;
  5. use app\admin\model\AuthGroup;
  6. use app\common\controller\Backend;
  7. use addons\qingdong\model\Contract as ContractModel;
  8. use addons\qingdong\model\Staff;
  9. use fast\Tree;
  10. use think\Config;
  11. /**
  12. * 合同分析
  13. */
  14. class Contract extends Backend
  15. {
  16. public function _initialize()
  17. {
  18. parent::_initialize();
  19. $childrenGroupIds = $this->auth->getChildrenGroupIds(true);
  20. $groupList = collection(AuthGroup::where('id', 'in', $childrenGroupIds)->select())->toArray();
  21. Tree::instance()->init($groupList);
  22. $groupList = [];
  23. if ($this->auth->isSuperAdmin()) {
  24. $groupList = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0));
  25. } else {
  26. $groups = $this->auth->getGroups();
  27. $groupIds = [];
  28. foreach ($groups as $m => $n) {
  29. if (in_array($n['id'], $groupIds) || in_array($n['pid'], $groupIds)) {
  30. continue;
  31. }
  32. $groupList = array_merge($groupList, Tree::instance()->getTreeList(Tree::instance()->getTreeArray($n['pid'])));
  33. foreach ($groupList as $index => $item) {
  34. $groupIds[] = $item['id'];
  35. }
  36. }
  37. }
  38. $this->assign('groupdata',$groupList);
  39. }
  40. /**
  41. * 新增合同
  42. */
  43. public function newadd()
  44. {
  45. $type=input('type','bar');//bar 柱状图 pie 饼状图
  46. $row = input('row/a');
  47. $year = $row['year'] ?? date('Y');
  48. $betweentime = [date('Y-m-d 00:00:00',strtotime($year . '-01-01')), date('Y-m-d 23:59:59',strtotime($year . '-12-31') + 86400 - 1)];
  49. $between = [strtotime($year . '-01-01'), strtotime($year . '-12-31') + 86400 - 1];
  50. $lastbetweentime = [date('Y-m-d 00:00:00',strtotime(($year-1) . '-01-01')), date('Y-m-d 23:59:59',strtotime(($year-1) . '-12-31') + 86400 - 1)];
  51. $lastbetween = [strtotime(($year-1) . '-01-01'), strtotime(($year-1) . '-12-31') + 86400 - 1];
  52. $group_id = $row['group_id'] ?? 0;
  53. $ids=[];
  54. if ($group_id) {//角色组
  55. $ids = Staff::getGroupStaffIds($group_id);
  56. }
  57. if (isset($row['staff_id']) && $row['staff_id']) {
  58. $ids = $staff_id = $row['staff_id'];
  59. } else {
  60. $staff_id = '';
  61. }
  62. $where = ['order_date' => [
  63. 'between',
  64. $betweentime
  65. ], 'check_status' => 2];
  66. if ($group_id || $staff_id) {
  67. $where['owner_staff_id'] = ['in', $ids];
  68. }
  69. //新增合同数
  70. $contracts = ContractModel::where($where)->field("FROM_UNIXTIME(UNIX_TIMESTAMP(order_date),'%Y-%m') as ctime,count(*) as c")->group('ctime')->select();
  71. $select = [];
  72. foreach ($contracts as $v) {
  73. $select[$v['ctime']] = $v['c'];
  74. }
  75. $data=[];
  76. $time = $between[0];
  77. $toMonthTime = $between[1];
  78. for ($time; $time <= $toMonthTime; $time = strtotime('+1 month', $time)) {
  79. $data['date'][] = intval(date('m', $time)).'月份';
  80. $data['number'][] = $select[date('Y-m', $time)] ?? 0;
  81. }
  82. $where['order_date']=[
  83. 'between',
  84. $lastbetweentime
  85. ];
  86. //新增合同数
  87. $lastcontracts = ContractModel::where($where)->field("FROM_UNIXTIME(UNIX_TIMESTAMP(order_date),'%Y-%m') as ctime,count(*) as c")->group('ctime')->select();
  88. $lastselect = [];
  89. foreach ($lastcontracts as $v) {
  90. $lastselect[$v['ctime']] = $v['c'];
  91. }
  92. $time = $lastbetween[0];
  93. $toMonthTime = $lastbetween[1];
  94. for ($time; $time <= $toMonthTime; $time = strtotime('+1 month', $time)) {
  95. $data['last_number'][] = $select[date('Y-m', $time)] ?? 0;
  96. }
  97. $monthRatio=[];
  98. foreach ($data['number'] as $val) {
  99. $last_number = $last_number ?? 0;
  100. if(empty($val) && empty($last_number)){
  101. $ratio=0;
  102. }elseif(empty($val) && $last_number){
  103. $ratio=-100;
  104. }elseif(empty($last_number) && $val){
  105. $ratio=100;
  106. }else{
  107. if($last_number > $val){
  108. $ratio=-(sprintf("%.2f", ($last_number-$val)/$last_number * 100));
  109. }else{
  110. $ratio=(sprintf("%.2f", ($val-$last_number)/$last_number * 100));
  111. }
  112. }
  113. $last_number=$val;
  114. $monthRatio[]=$ratio;
  115. }
  116. $yearRatio=[];
  117. foreach ($data['number'] as $k=>$val) {
  118. $last_number = $data['last_number'][$k] ?? 0;
  119. if(empty($val) && empty($last_number)){
  120. $ratio=0;
  121. }elseif(empty($val) && $last_number){
  122. $ratio=-100;
  123. }elseif(empty($last_number) && $val){
  124. $ratio=100;
  125. }else{
  126. if($last_number > $val){
  127. $ratio=-(sprintf("%.2f", ($last_number-$val)/$last_number * 100));
  128. }else{
  129. $ratio=(sprintf("%.2f", ($val-$last_number)/$last_number * 100));
  130. }
  131. }
  132. $yearRatio[]=$ratio;
  133. }
  134. $data['year_ratio']=$yearRatio;
  135. $data['month_ratio']=$monthRatio;
  136. $data['title']='合同数量统计分析(按创建时间且审核通过统计)';
  137. $this->view->assign([
  138. 'data' =>$data,
  139. 'year' => $year,
  140. 'staff_id' => $staff_id,
  141. 'group_id' => $group_id,
  142. 'type' => $type,
  143. ]);
  144. return $this->view->fetch();
  145. }
  146. /**
  147. * 合同金额统计
  148. */
  149. public function money()
  150. {
  151. $type=input('type','bar');//bar 柱状图 pie 饼状图
  152. $row = input('row/a');
  153. $year = $row['year'] ?? date('Y');
  154. $betweentime = [date('Y-m-d 00:00:00',strtotime($year . '-01-01')), date('Y-m-d 23:59:59',strtotime($year . '-12-31') + 86400 - 1)];
  155. $between = [strtotime($year . '-01-01'), strtotime($year . '-12-31') + 86400 - 1];
  156. $lastbetweentime = [date('Y-m-d 00:00:00',strtotime(($year-1) . '-01-01')), date('Y-m-d 23:59:59',strtotime(($year-1) . '-12-31') + 86400 - 1)];
  157. $lastbetween = [strtotime(($year-1) . '-01-01'), strtotime(($year-1) . '-12-31') + 86400 - 1];
  158. $group_id = $row['group_id'] ?? 0;
  159. $ids=[];
  160. if ($group_id) {//角色组
  161. $ids = Staff::getGroupStaffIds($group_id);
  162. }
  163. if (isset($row['staff_id']) && $row['staff_id']) {
  164. $ids = $staff_id = $row['staff_id'];
  165. } else {
  166. $staff_id = '';
  167. }
  168. $where = ['order_date' => [
  169. 'between',
  170. $betweentime
  171. ], 'check_status' => 2];
  172. if ($group_id || $staff_id) {
  173. $where['owner_staff_id'] = ['in', $ids];
  174. }
  175. //新增合同数
  176. $contracts = ContractModel::where($where)->field("FROM_UNIXTIME(UNIX_TIMESTAMP(order_date),'%Y-%m') as ctime,sum(money) as money")->group('ctime')->select();
  177. $select = [];
  178. foreach ($contracts as $v) {
  179. $select[$v['ctime']] = $v['money'];
  180. }
  181. $data=[];
  182. $time = $between[0];
  183. $toMonthTime = $between[1];
  184. for ($time; $time <= $toMonthTime; $time = strtotime('+1 month', $time)) {
  185. $data['date'][] = intval(date('m', $time)).'月份';
  186. $data['number'][] = $select[date('Y-m', $time)] ?? 0;
  187. }
  188. $where['order_date']=[
  189. 'between',
  190. $lastbetweentime
  191. ];
  192. //新增合同数
  193. $lastcontracts = ContractModel::where($where)->field("FROM_UNIXTIME(UNIX_TIMESTAMP(order_date),'%Y-%m') as ctime,sum(money) as money")->group('ctime')->select();
  194. $lastselect = [];
  195. foreach ($lastcontracts as $v) {
  196. $lastselect[$v['ctime']] = $v['money'];
  197. }
  198. $time = $lastbetween[0];
  199. $toMonthTime = $lastbetween[1];
  200. for ($time; $time <= $toMonthTime; $time = strtotime('+1 month', $time)) {
  201. $data['last_number'][] = $lastselect[date('Y-m', $time)] ?? 0;
  202. }
  203. $monthRatio=[];
  204. foreach ($data['number'] as $val) {
  205. $last_number = $last_number ?? 0;
  206. if(empty($val) && empty($last_number)){
  207. $ratio=0;
  208. }elseif(empty($val) && $last_number){
  209. $ratio=-100;
  210. }elseif(empty($last_number) && $val){
  211. $ratio=100;
  212. }else{
  213. if($last_number > $val){
  214. $ratio=-(sprintf("%.2f", ($last_number-$val)/$last_number * 100));
  215. }else{
  216. $ratio=(sprintf("%.2f", ($val-$last_number)/$last_number * 100));
  217. }
  218. }
  219. $last_number=$val;
  220. $monthRatio[]=$ratio;
  221. }
  222. $yearRatio=[];
  223. foreach ($data['number'] as $k=>$val) {
  224. $last_number = $data['last_number'][$k] ?? 0;
  225. if(empty($val) && empty($last_number)){
  226. $ratio=0;
  227. }elseif(empty($val) && $last_number){
  228. $ratio=-100;
  229. }elseif(empty($last_number) && $val){
  230. $ratio=100;
  231. }else{
  232. if($last_number > $val){
  233. $ratio=-(sprintf("%.2f", ($last_number-$val)/$last_number * 100));
  234. }else{
  235. $ratio=(sprintf("%.2f", ($val-$last_number)/$last_number * 100));
  236. }
  237. }
  238. $yearRatio[]=$ratio;
  239. }
  240. $data['year_ratio']=$yearRatio;
  241. $data['month_ratio']=$monthRatio;
  242. $data['title']='合同金额统计分析(按创建时间且审核通过统计)';
  243. $this->view->assign([
  244. 'data' =>$data,
  245. 'year' => $year,
  246. 'staff_id' => $staff_id,
  247. 'group_id' => $group_id,
  248. 'type' => $type,
  249. ]);
  250. return $this->view->fetch();
  251. }
  252. /**
  253. * 回款金额统计
  254. */
  255. public function receivables()
  256. {
  257. $type=input('type','bar');//bar 柱状图 pie 饼状图
  258. $row = input('row/a');
  259. $year = $row['year'] ?? date('Y');
  260. $betweentime = [date('Y-m-d 00:00:00',strtotime($year . '-01-01')), date('Y-m-d 23:59:59',strtotime($year . '-12-31') + 86400 - 1)];
  261. $between = [strtotime($year . '-01-01'), strtotime($year . '-12-31') + 86400 - 1];
  262. $lastbetweentime = [date('Y-m-d 00:00:00',strtotime(($year-1) . '-01-01')), date('Y-m-d 23:59:59',strtotime(($year-1) . '-12-31') + 86400 - 1)];
  263. $lastbetween = [strtotime(($year-1) . '-01-01'), strtotime(($year-1) . '-12-31') + 86400 - 1];
  264. $group_id = $row['group_id'] ?? 0;
  265. $ids=[];
  266. if ($group_id) {//角色组
  267. $ids = Staff::getGroupStaffIds($group_id);
  268. }
  269. if (isset($row['staff_id']) && $row['staff_id']) {
  270. $ids = $staff_id = $row['staff_id'];
  271. } else {
  272. $staff_id = '';
  273. }
  274. $where = ['return_time' => [
  275. 'between',
  276. $betweentime
  277. ], 'check_status' => 2];
  278. if ($group_id || $staff_id) {
  279. $where['owner_staff_id'] = ['in', $ids];
  280. }
  281. //
  282. $receivables = Receivables::where($where)->field("FROM_UNIXTIME(UNIX_TIMESTAMP(return_time),'%Y-%m') as ctime,sum(money) as money")->group('ctime')->select();
  283. $select = [];
  284. foreach ($receivables as $v) {
  285. $select[$v['ctime']] = $v['money'];
  286. }
  287. $data=[];
  288. $time = $between[0];
  289. $toMonthTime = $between[1];
  290. for ($time; $time <= $toMonthTime; $time = strtotime('+1 month', $time)) {
  291. $data['date'][] = intval(date('m', $time)).'月份';
  292. $data['number'][] = $select[date('Y-m', $time)] ?? 0;
  293. }
  294. $where['return_time']=[
  295. 'between',
  296. $lastbetweentime
  297. ];
  298. //
  299. $lastreceivables = Receivables::where($where)->field("FROM_UNIXTIME(UNIX_TIMESTAMP(return_time),'%Y-%m') as ctime,sum(money) as money")->group('ctime')->select();
  300. $lastselect = [];
  301. foreach ($lastreceivables as $v) {
  302. $lastselect[$v['ctime']] = $v['money'];
  303. }
  304. $time = $lastbetween[0];
  305. $toMonthTime = $lastbetween[1];
  306. for ($time; $time <= $toMonthTime; $time = strtotime('+1 month', $time)) {
  307. $data['last_number'][] = $select[date('Y-m', $time)] ?? 0;
  308. }
  309. $monthRatio=[];
  310. foreach ($data['number'] as $val) {
  311. $last_number = $last_number ?? 0;
  312. if(empty($val) && empty($last_number)){
  313. $ratio=0;
  314. }elseif(empty($val) && $last_number){
  315. $ratio=-100;
  316. }elseif(empty($last_number) && $val){
  317. $ratio=100;
  318. }else{
  319. if($last_number > $val){
  320. $ratio=-(sprintf("%.2f", ($last_number-$val)/$last_number * 100));
  321. }else{
  322. $ratio=(sprintf("%.2f", ($val-$last_number)/$last_number * 100));
  323. }
  324. }
  325. $last_number=$val;
  326. $monthRatio[]=$ratio;
  327. }
  328. $yearRatio=[];
  329. foreach ($data['number'] as $k=>$val) {
  330. $last_number = $data['last_number'][$k] ?? 0;
  331. if(empty($val) && empty($last_number)){
  332. $ratio=0;
  333. }elseif(empty($val) && $last_number){
  334. $ratio=-100;
  335. }elseif(empty($last_number) && $val){
  336. $ratio=100;
  337. }else{
  338. if($last_number > $val){
  339. $ratio=-(sprintf("%.2f", ($last_number-$val)/$last_number * 100));
  340. }else{
  341. $ratio=(sprintf("%.2f", ($val-$last_number)/$last_number * 100));
  342. }
  343. }
  344. $yearRatio[]=$ratio;
  345. }
  346. $data['year_ratio']=$yearRatio;
  347. $data['month_ratio']=$monthRatio;
  348. $data['title']='回款金额统计分析(按创建时间且审核通过统计)';
  349. $this->view->assign([
  350. 'data' =>$data,
  351. 'year' => $year,
  352. 'staff_id' => $staff_id,
  353. 'group_id' => $group_id,
  354. 'type' => $type,
  355. ]);
  356. return $this->view->fetch();
  357. }
  358. }