ProcessStatisticController.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Exports\Export;
  4. use App\Owner;
  5. use App\ProcessMethod;
  6. use App\ProcessStatistic;
  7. use Illuminate\Database\Eloquent\Builder;
  8. use Illuminate\Http\Request;
  9. use Illuminate\Support\Facades\Gate;
  10. use Maatwebsite\Excel\Facades\Excel;
  11. class ProcessStatisticController extends Controller
  12. {
  13. public function conditionQuery(Request $request,$processStatistics){
  14. if ($request->input('started_at')){
  15. $processStatistics=$processStatistics->where('started_at','>=',$request->input('started_at'));
  16. }
  17. if ($request->input('owner_id')){
  18. $owner_id=$request->input('owner_id');
  19. $processStatistics=$processStatistics->whereHas('process',function (Builder $query)use($owner_id){
  20. $query->where('owner_id',$owner_id);
  21. });
  22. }
  23. if ($request->input('ended_at')){
  24. $processStatistics=$processStatistics->where('ended_at','<=',$request->input('ended_at'));
  25. }
  26. if ($request->input('code')){
  27. $code=$request->input('code');
  28. $processStatistics=$processStatistics->whereHas('process',function (Builder $query)use($code){
  29. $query->where('code','like',$code."%");
  30. });
  31. }
  32. if ($request->input('status')){
  33. $status=$request->input('status');
  34. $processStatistics=$processStatistics->whereHas('process',function (Builder $query)use($status){
  35. $query->where('status',$status);
  36. });
  37. }
  38. $processStatistics=$processStatistics->paginate($request->input('paginate')?$request->input('paginate'):50);
  39. return $processStatistics;
  40. }
  41. public function index(Request $request){
  42. $processStatistics=ProcessStatistic::with('process')->orderBy('process_id','DESC');
  43. if ($request->input('checkSign')){
  44. $excel=$this->isExport($request,$processStatistics);
  45. return $excel;
  46. }
  47. $processStatistics=$this->conditionQuery($request,$processStatistics);
  48. $owners=Owner::get();
  49. $processMethods=ProcessMethod::get();
  50. return view('process.statistic',['processStatistics'=>$processStatistics,'owners'=>$owners,'processMethods'=>$processMethods]);
  51. }
  52. //获取导出数据
  53. public function isExport(Request $request,$processStatistics){
  54. if(!Gate::allows('二次加工管理-查询')){ return redirect(url('/')); }
  55. if ($request->input('checkSign')=="-1"){
  56. $processStatistics=$this->conditionQuery($request,$processStatistics);
  57. $excel=$this->export($processStatistics);
  58. return $excel;
  59. }
  60. $id = explode( ',',$request->input('checkSign'));
  61. $processStatistics=$processStatistics->whereIn('process_id',$id)->get();
  62. $excel=$this->export($processStatistics);
  63. return $excel;
  64. }
  65. //执行
  66. public function export($processStatistics){
  67. if(!Gate::allows('二次加工管理-查询')){ return '没有权限'; }
  68. $row=[[
  69. 'process_id'=>'ID',
  70. 'code'=>'任务号',
  71. 'owner_name'=>'货主',
  72. 'started_at'=>'开始日期',
  73. 'ended_at'=>'完成日期',
  74. 'unit_price'=>'单价',
  75. 'amount'=>'预期数量',
  76. 'completed_amount'=>'完成数量',
  77. 'revenue'=>'收入合计',
  78. 'duration_days'=>'完成时间(天)',
  79. 'duration_man_hours'=>'总工时',
  80. 'process_method_name'=>'加工类型',
  81. 'top_capacity'=>'最高日产能',
  82. 'bottom_capacity'=>'最低日产能',
  83. 'average_capacity'=>'日均产能',
  84. 'total_cost'=>'合计成本',
  85. 'gross_profit'=>'毛利润',
  86. 'gross_profit_rate'=>'毛利率',
  87. 'status'=>'状态',
  88. ]];
  89. $list=[];
  90. $i=0;
  91. foreach ($processStatistics as $processStatistic){
  92. $w=[
  93. 'process_id'=>$processStatistic->process_id,
  94. 'code'=>$processStatistic->process?$processStatistic->process->code:'',
  95. 'owner_name'=>$processStatistic->process?$processStatistic->process->owner_name:'',
  96. 'started_at'=>$processStatistic->started_at,
  97. 'ended_at'=>$processStatistic->ended_at,
  98. 'unit_price'=>$processStatistic->process?$processStatistic->process->unit_price:'',
  99. 'amount'=>$processStatistic->process?$processStatistic->process->amount:'',
  100. 'completed_amount'=>$processStatistic->process?$processStatistic->process->completed_amount:'',
  101. 'revenue'=>$processStatistic->revenue,
  102. 'duration_days'=>$processStatistic->duration_days,
  103. 'duration_man_hours'=>$processStatistic->duration_man_hours,
  104. 'process_method_name'=>$processStatistic->process?$processStatistic->process->process_method_name:'',
  105. 'top_capacity'=>$processStatistic->top_capacity,
  106. 'bottom_capacity'=>$processStatistic->bottom_capacity,
  107. 'average_capacity'=>$processStatistic->average_capacity,
  108. 'total_cost'=>$processStatistic->total_cost,
  109. 'gross_profit'=>$processStatistic->gross_profit,
  110. 'gross_profit_rate'=>round(($processStatistic->gross_profit_rate)*100,2).'%',
  111. 'status'=>$processStatistic->process?$processStatistic->process->status:'',
  112. ];
  113. $list[$i]=$w;
  114. $i++;
  115. }
  116. return Excel::download(new Export($row,$list),date('YmdHis', time()).'-二次加工单.xls');
  117. }
  118. }