Stats.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\report\controller;
  4. use app\report\model\GoodBasic;
  5. use app\model\AccountItem;
  6. use app\report\model\ConsultBids;
  7. use app\report\model\ConsultInfo;
  8. use app\report\model\ConsultOrder;
  9. use app\report\model\GoodStock;use app\report\model\OrderOut;
  10. use app\report\model\OrderOutChild;
  11. use app\report\model\Purchease;
  12. use app\report\model\PurcheaseOrder;
  13. use app\report\model\Sale;
  14. use think\App;
  15. class Stats extends Base
  16. {
  17. private $saleDerpart=[52=>'客服部@百辰荣达',53=>'项目部@普润心堂',56=>'网络部@泓源广诚',57=>'平台部@万宇恒通',59=>'财务部@万宇恒通']; // 客服部@百辰荣达,
  18. //项目部@普润心堂,网络部@泓源广诚,平台部@万宇恒通,财务部@万宇恒通
  19. private $cgdDerpart=[58=>"万宇供应链资源部"];//万宇供应链资源部
  20. public function __construct(App $app) {parent::__construct($app);}
  21. //咨询单
  22. public function zxorder()
  23. {
  24. $param =$this->request->param(["start"=>date("Y-m-01 00:00:00"),"end"=>date("Y-m-t 23:59:59")]);
  25. $time = [$param["start"],$param['end']];
  26. $zxModel =new ConsultOrder();
  27. $temp=[];
  28. foreach ($this->saleDerpart as $departid=>$depart){
  29. $uid =$this->GetDepartId($departid);
  30. $temp[$departid]["name"] =$depart;
  31. $temp[$departid]["num"] =$zxModel->alias("a")->leftJoin("consult_info b","wsm_consult_order.zxNo=b.zxNo")
  32. ->whereBetween('b.addtime',$time)
  33. ->whereIn('createrid',$uid)
  34. ->where(["a.is_del"=>0,"b.is_del"=>0])
  35. ->count();
  36. }
  37. $this->success("获取成功",array_values($temp));
  38. }
  39. /**
  40. * 销售单.
  41. *
  42. * @return \think\Response
  43. */
  44. public function saleOrder()
  45. {
  46. $param =$this->request->param(['start'=>date('Y-m-01 00:00:00'),'end'=>date('Y-m-t 23:59:59')]);
  47. $time = [$param['start'],$param['end']];
  48. $Model =new Sale();
  49. $temp=[];
  50. foreach ($this->saleDerpart as $departid=>$depart){
  51. $uid =$this->GetDepartId($departid);
  52. $temp[$departid]['name'] =$depart;
  53. $temp[$departid]['result'] =$Model->group("order_type")
  54. ->whereBetween('addtime',$time)
  55. ->whereIn('apply_id',$uid)
  56. ->where(['is_del'=>0])
  57. ->column("count(1) num,order_type","order_type");
  58. }
  59. $this->success('获取成功',array_values($temp));
  60. }
  61. /**
  62. * 报备单
  63. *
  64. * @param \think\Request $request
  65. * @return \think\Response
  66. */
  67. public function bkOrder()
  68. {
  69. $param =$this->request->param(['start'=>date('Y-m-01 00:00:00'),'end'=>date('Y-m-t 23:59:59')]);
  70. $time = [$param['start'],$param['end']];
  71. $Model =new Purchease();
  72. $temp=[];
  73. foreach ($this->saleDerpart as $departid=>$depart){
  74. $uid =$this->GetDepartId($departid);
  75. $temp[$departid]['name'] =$depart;
  76. $temp[$departid]['num'] =$Model
  77. ->whereBetween('addtime',$time)
  78. ->whereIn('apply_id',$uid)
  79. ->where(['is_del'=>0,"status"=>[1,2]])
  80. ->count();
  81. }
  82. $this->success('获取成功',array_values($temp));
  83. }
  84. /**
  85. * 采购部门采购情况
  86. */
  87. public function cgdOrder()
  88. {
  89. $param =$this->request->param(['start'=>date('Y-m-01 00:00:00'),'end'=>date('Y-m-t 23:59:59')]);
  90. $time = [$param['start'],$param['end']];
  91. $Model =new PurcheaseOrder();
  92. $temp=[];
  93. foreach ($this->cgdDerpart as $departid=>$depart){
  94. $uid =$this->GetDepartId($departid);
  95. $temp[$departid]['name'] =$depart;
  96. $data =$Model->alias("a")->leftJoin("order_num b","a.cgdNo=b.cgdNo")
  97. ->leftJoin("sale c","c.orderCode=b.orderCode and c.is_del=0")
  98. ->leftJoin("platform d","d.id=c.platform_id and d.is_del=0")
  99. ->field("a.companyNo,a.companyName,a.order_type,d.use_type,count(1) num")
  100. ->group("a.companyNo,a.companyName,order_type,use_type")
  101. ->whereBetween('a.addtime',$time)
  102. // ->whereIn('a.cgder_id',$uid)
  103. ->where('a.is_del',0)
  104. ->select();
  105. $result=[];
  106. foreach ($data as $key=>$val){
  107. if(!isset($result[$val['companyNo']])){
  108. $result[$val['companyNo']]= [ 'cgder'=>$val['companyName'],
  109. 'total_cgd' => 0,//订单总量
  110. 'tag_1' => 0,//库存品
  111. 'tag_2' => 0,//非库存品
  112. 'tag_zx' => 0,//咨询采反总数
  113. 'tag_c' => 0,//to C
  114. 'tag_b' => 0,//to B
  115. ];
  116. }
  117. $result[$val['companyNo']]['total_cgd']+=$val['num'];
  118. switch ($val['order_type']) {
  119. case 1:
  120. //库存品
  121. // if ($val['order_source'] == 0) {
  122. $result[$val['companyNo']]['tag_1']+=$val['num'];
  123. // }
  124. break;
  125. case 2:
  126. //非库存品
  127. $result[$val['companyNo']]['tag_2']+=$val['num'];
  128. break;
  129. case 3:
  130. //咨询采反
  131. $result[$val['companyNo']]['tag_zx']+=$val['num'];
  132. break;
  133. }
  134. switch ($val['use_type']) {
  135. case 1:
  136. //to B
  137. $result[$val['companyNo']]['tag_b']+=$val['num'];
  138. break;
  139. case 2:
  140. //to C
  141. $result[$val['companyNo']]['tag_c']+=$val['num'];
  142. break;
  143. }
  144. }
  145. $temp[$departid]['result'] =$result;
  146. }
  147. $this->success('获取成功',array_values($temp));
  148. }
  149. /**
  150. * 显示编辑资源表单页.
  151. *
  152. * @param int $id
  153. * @return \think\Response
  154. */
  155. public function bidsOrder()
  156. {
  157. $param =$this->request->param(['start'=>date('Y-m-01 00:00:00'),'end'=>date('Y-m-t 23:59:59')]);
  158. $time = [$param['start'],$param['end']];
  159. $model = new ConsultBids();
  160. $order =new ConsultInfo();
  161. $temp=[];
  162. foreach ($this->cgdDerpart as $departid=>$depart){
  163. // $uid =$this->GetDepartId($departid);
  164. $temp[$departid]['name'] =$depart;
  165. $list = $model->whereBetween("addtime",$time)->where('is_del',0)->group("createrid,creater")->field
  166. ("creater,createrid,count(1) num")->select()->toArray();
  167. $list[]= ["creater"=>"竞价单总数","creater_id"=>"","num"=>$order->whereBetween('addtime',$time)->where('is_del',0)->count()];
  168. $temp[$departid]['result'] =$list;
  169. }
  170. $this->success('获取成功',array_values($temp));
  171. }
  172. /**
  173. * 咨询单空返数据
  174. *
  175. * @param \think\Request $request
  176. * @param int $id
  177. * @return \think\Response
  178. */
  179. public function bidsFail()
  180. {$param =$this->request->param(['start'=>date('Y-m-01 00:00:00'),'end'=>date('Y-m-t 23:59:59')]);
  181. $time = [$param['start'],$param['end']];
  182. $model = new ConsultOrder();
  183. $temp=[];
  184. // $uid = (new AccountItem())->GetUidByDepartId(array_keys($this->saleDerpart));
  185. $list = $model->alias('a')
  186. ->leftJoin('consult_info b','wsm_consult_order.zxNo=b.zxNo')
  187. ->leftJoin('consult_bids c','b.infoNo=c.infoNo')
  188. ->whereBetween('a.addtime',$time)
  189. ->whereNull("c.id")
  190. ->where(['a.is_del'=>0,'b.is_del'=>0])
  191. // ->where('b.status','<>',5)
  192. ->group('b.status')
  193. ->column('b.status,count(1) num','b.status');
  194. $temp['result'] =$list;
  195. $this->success('获取成功',$temp);
  196. }
  197. /**
  198. * 发货工单
  199. */
  200. public function outChildOrder()
  201. {
  202. $param =$this->request->param(['start'=>date('Y-m-01 00:00:00'),'end'=>date('Y-m-t 23:59:59')]);
  203. $time = [$param['start'],$param['end']];
  204. $model = new OrderOutChild();
  205. foreach ($this->saleDerpart as $departid=>$sale){
  206. $uid = $this->GetDepartId($departid);
  207. $temp[$departid]['name'] =$sale;
  208. $build= $model->alias('a')
  209. // ->whereBetween('a.addtime',$time)
  210. ->whereIn('a.apply_id',$uid)
  211. ->where('a.is_del',0)
  212. ->whereIn('a.status',[0,1])
  213. ->field('order_type,DATEDIFF(now(), a.addtime ) as expire,"" stage')
  214. ->withAttr('stage',function ($v,$row){
  215. if($row['expire']>=0 && $row['expire']<=1) return 1;
  216. elseif($row['expire']<=2) return 2;
  217. elseif($row['expire']<=7) return 3;
  218. elseif($row['expire']<=14) return 4;
  219. elseif($row['expire']<=30) return 5;
  220. else return 6;
  221. })->select();
  222. $tlist=[];
  223. $order_type_name = ["1"=>"库存品","2"=>"非库存品","3"=>"咨询采返",'4'=>'报备'];
  224. foreach ($build as $item){
  225. if(!isset($tlist[$item['order_type']])){
  226. $tlist[$item['order_type']]=[$order_type_name[$item['order_type']]?:"",0,0,0,0,0,0];
  227. }
  228. $tlist[$item['order_type']][$item['stage']]+=1;
  229. }
  230. $temp[$departid]['result'] =$tlist?:[];
  231. }
  232. $this->success('获取成功',array_values($temp));
  233. }
  234. /** 发货单数据统计
  235. * @throws \think\db\exception\DataNotFoundException
  236. * @throws \think\db\exception\DbException
  237. * @throws \think\db\exception\ModelNotFoundException
  238. */
  239. public function outOrder()
  240. {
  241. $param =$this->request->param(['start'=>date('Y-m-01 00:00:00'),'end'=>date('Y-m-t 23:59:59')]);
  242. $time = [$param['start'],$param['end']];
  243. $model = new OrderOut();
  244. foreach ($this->saleDerpart as $departid=>$sale){
  245. $uid = $this->GetDepartId($departid);
  246. $temp[$departid]['name'] =$sale;
  247. $build= $model->alias('a')
  248. // ->whereBetween('a.addtime',$time)
  249. ->whereIn('a.apply_id',$uid)
  250. ->where('a.is_del',0)
  251. ->where('a.status',"=",0)
  252. ->field('order_type,DATEDIFF(ifnull( a.sendtime, NOW()), a.addtime ) as expire,"" stage')
  253. ->withAttr('stage',function ($v,$row){
  254. if($row['expire']>=0 && $row['expire']<=1) return 1;
  255. elseif($row['expire']<=2) return 2;
  256. elseif($row['expire']<=7) return 3;
  257. elseif($row['expire']<=14) return 4;
  258. elseif($row['expire']<=30) return 5;
  259. else return 6;
  260. })->select();
  261. $tlist=[];
  262. $order_type_name = ['1'=>'库存品','2'=>'非库存品','3'=>'咨询采返','4'=>'报备'];
  263. foreach ($build as $item){
  264. if(!isset($tlist[$item['order_type']])){
  265. $tlist[$item['order_type']]=[$order_type_name[$item['order_type']]?:'',0,0,0,0,0,0];
  266. }
  267. $tlist[$item['order_type']][$item['stage']]+=1;
  268. }
  269. $temp[$departid]['result'] =$tlist;
  270. }
  271. $this->success('获取成功',array_values($temp));
  272. }
  273. //未发货数据
  274. public function wsendOrder(){
  275. $param =$this->request->param(['start_y'=>date('Y')]);
  276. $temp=[];
  277. $model =new Sale();
  278. $userArr=[];
  279. foreach ($this->saleDerpart as $departid=>$depart){
  280. $uid = $this->GetDepartId($departid);
  281. $userArr=array_merge($userArr,$uid);
  282. $temp[$departid]["name"] = $depart;
  283. $start = 1;
  284. $end = $param['start_y']<date('Y')?12: date("m");
  285. while ($start<=$end){
  286. $list = $model->alias('a')
  287. ->whereIn('status',[0,1])
  288. ->whereIn('send_status',[1,2])
  289. ->where('is_del',0)
  290. ->whereIn('apply_id',$uid)
  291. ->whereMonth('addtime',$param['start_y'].'-'.str_pad($start.'' , 2,'0',0))
  292. ->where('good_num-th_num > ifnull((select sum(receipt_quantity) from wsm_order_addr where orderCode=a.orderCode and is_del=0),0)')
  293. ->group('send_status')
  294. ->column('count(1) num ,send_status','send_status');
  295. // echo $param['start'].'-'.str_pad($start.'' , 2,'0',0);
  296. $temp[$departid]['result'][$start]=$list;
  297. $start++;
  298. }
  299. }
  300. $noArr=[];
  301. $noArr['name'] = "无地址订单";
  302. $start = 1;
  303. $end = $param['start_y']<date('Y')?12: date('m');
  304. while ($start<=$end){
  305. // $month = $start;a
  306. $list = $model->whereIn('status',[0,1])
  307. ->whereIn('send_status',[1,2])
  308. ->whereIn('apply_id',$userArr)
  309. ->where('is_del',0)
  310. ->whereMonth('addtime',$param['start_y'].'-'.str_pad($start.'' , 2,'0',0))
  311. ->field('sale_price,orderCode,good_num,total_price,0 addr_num, 0 before_th')
  312. ->withAttr('addr_num',function($v,$data){
  313. $num = Sale::name('order_addr')->where(['orderCode'=>$data['orderCode'],'is_del'=>0])->sum
  314. ('receipt_quantity');
  315. return bcmul($data['sale_price'],strval($num),2);
  316. })
  317. ->withAttr('before_th' , function ($v , $data) {
  318. $num= Sale::name('sale_return')->where(['orderCode'=>$data['orderCode'] , 'is_del'=>0,
  319. 'status'=>4])->sum('num');
  320. return bcmul($data['sale_price'],strval($num),2);
  321. }
  322. )
  323. ->select();
  324. if($list->isEmpty())$noArr['result'][$start]=['total_price'=>0];
  325. else{
  326. $total =strval(array_sum(array_column($list->toArray() , 'total_price')));
  327. $before_th =strval(array_sum(array_column($list->toArray() , 'before_th')));
  328. $addr_num =strval(array_sum(array_column($list->toArray() , 'addr_num')));
  329. $noArr['result'][$start]['total_price']=bcsub(bcsub($total,$before_th,2),$addr_num,2);
  330. }
  331. $start++;
  332. }
  333. $this->success('获取成功',["result"=>$temp,"noAddr"=>$noArr]);
  334. }
  335. //发货单维度商品每日库存
  336. public function goodStock(){
  337. $param =$this->request->param(['spuCode'=>'','good_name'=>'','companyNo'=>'','page'=>1,
  338. 'size'=>10]);
  339. $where=[["is_del","=",0]];
  340. $param['spuCode']==''?: $where[]=["spuCode","like","%{$param['spuCode']}%"];
  341. $param['good_name']==''?: $where[]=["good_name","like","%{$param['good_name']}%"];
  342. $param['companyNo']==''?: $where[]=["companyNo","like","%{$param['companyNo']}%"];
  343. $model = new GoodBasic();
  344. $stock = new GoodStock();
  345. $list=$model->where($where)->visible(["spuCode","good_name",'spec_info','cat_info','usable_stock',
  346. 'total_stock','wait_in_stock','wait_out_stock',"child"])->withAttr("child",function($v,$data) use($stock ){
  347. return $stock->withJoin(["wareinfo"],"left")
  348. ->where("spuCode",$data['spuCode'])
  349. ->column("wareinfo.name,contactor_name,supplierName,companyName,usable_stock+wait_out_stock total_stock,usable_stock,wait_out_stock,wait_in_stock");
  350. })->paginate(["list_rows"=>$param['size'], "page"=>$param["page"]]);
  351. $this->success('获取成功',['result'=>$list->items(),"count"=>$list->total()]);
  352. }
  353. //发货单维度商品每日库存
  354. public function outChildStock(){
  355. $param =$this->request->param(['start'=>date('Y-m-01 00:00:00'),'end'=>date('Y-m-t 23:59:59'),'page'=>1,
  356. 'size'=>10]);
  357. $time = [$param['start'],$param['end']];
  358. $model = new OrderOutChild();
  359. $list = $model
  360. ->where(["status"=>2,"order_type"=>1,"is_del"=>0])
  361. ->whereBetween("sendtime",$time)
  362. ->field("spuCode,good_name,companyName,sum(num) num")
  363. ->group('good_name,companyName,spuCode')
  364. ->paginate(['list_rows'=>$param['size'], 'page'=>$param['page']]);
  365. foreach ($list->items() as &$value){
  366. $value['stock_num'] = (new GoodStock())->where('spuCode',$value['spuCode'])->sum('usable_stock');
  367. $value['good_price'] = (new PurcheaseOrder())->where(['spuCode'=>$value['spuCode'],'order_source'=>0])->value('good_price',0);
  368. $value['total_fee'] = bcmul(strval($value['good_price']),strval($value['num']),2);
  369. }
  370. $this->success('获取成功',['result'=>$list->items(),'count'=>$list->total()]);
  371. }
  372. }