WaybillFinancialSnapshotsController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Exports\WaybillExport;
  4. use App\WaybillFinancialSnapshot;
  5. use Carbon\Carbon;
  6. use Illuminate\Http\Request;
  7. use Illuminate\Support\Facades\Auth;
  8. use Illuminate\Support\Facades\Gate;
  9. use Maatwebsite\Excel\Facades\Excel;
  10. class WaybillFinancialSnapshotsController extends Controller
  11. {
  12. public function conditionQuery(Request $request,$waybillFinancialSnapshots){
  13. if ($request->input('created_at_start')){
  14. $waybillFinancialSnapshots=$waybillFinancialSnapshots->where('created_at','>',$request->input('created_at_start'));
  15. }
  16. if ($request->input('created_at_end')){
  17. $waybillFinancialSnapshots=$waybillFinancialSnapshots->where('created_at','<',$request->input('created_at_end'));
  18. }
  19. $waybillFinancialSnapshots=$waybillFinancialSnapshots->paginate(50);
  20. return $waybillFinancialSnapshots;
  21. }
  22. public function index(Request $request)
  23. {
  24. if(!Gate::allows('财务报表-查询')){ return redirect(url('/')); }
  25. $waybillFinancialSnapshots=WaybillFinancialSnapshot::orderBy('id', 'DESC');
  26. $waybillFinancialSnapshots=$this->conditionQuery($request,$waybillFinancialSnapshots);
  27. return view('waybill.waybillFinancialSnapshot.index',['waybillFinancialSnapshots'=>$waybillFinancialSnapshots,'filterData'=>$request->input(),'type'=>'']);
  28. }
  29. public function indexZF(Request $request)
  30. {
  31. if(!Gate::allows('财务报表-查询')){ return redirect(url('/')); }
  32. $waybillFinancialSnapshots=WaybillFinancialSnapshot::orderBy('id', 'DESC')->where('json_content','like','%直发车%');
  33. $waybillFinancialSnapshots=$this->conditionQuery($request,$waybillFinancialSnapshots);
  34. return view('waybill.waybillFinancialSnapshot.index',['waybillFinancialSnapshots'=>$waybillFinancialSnapshots,'filterData'=>$request->input(),'type'=>'ZF']);
  35. }
  36. public function indexZX(Request $request)
  37. {
  38. if(!Gate::allows('财务报表-查询')){ return redirect(url('/')); }
  39. $waybillFinancialSnapshots=WaybillFinancialSnapshot::orderBy('id', 'DESC')->where('json_content','like','%专线%');
  40. $waybillFinancialSnapshots=$this->conditionQuery($request,$waybillFinancialSnapshots);
  41. return view('waybill.waybillFinancialSnapshot.index',['waybillFinancialSnapshots'=>$waybillFinancialSnapshots,'filterData'=>$request->input(),'type'=>'ZX']);
  42. }
  43. public function export($id,Request $request){
  44. if(!Gate::allows('财务报表-查询')){ return '没有权限'; }
  45. if ($id==-1){
  46. $id=[];
  47. ini_set('max_execution_time',2500);
  48. ini_set('memory_limit','1526M');
  49. $waybillFinancialSnapshots=WaybillFinancialSnapshot::select('id');
  50. if ($request->input('created_at_start')){
  51. $waybillFinancialSnapshots=$waybillFinancialSnapshots->where('created_at','>=',$request->input('created_at_start'));
  52. }
  53. if ($request->input('created_at_end')){
  54. $waybillFinancialSnapshots=$waybillFinancialSnapshots->where('created_at','<=',$request->input('created_at_end'));
  55. }
  56. $waybillFinancialSnapshots=$waybillFinancialSnapshots->get();
  57. foreach ($waybillFinancialSnapshots as $waybillFinancialSnapshot){
  58. array_push($id,$waybillFinancialSnapshot->id);
  59. }
  60. }else $id = explode( ',',$id);
  61. if (!$id)return ;
  62. $row=[[
  63. 'type'=>'运单类型',
  64. 'waybill_number'=>'运单号',
  65. 'owner'=>'货主',
  66. 'wms_bill_number'=>'WMS单号',
  67. 'origination'=>'始发地',
  68. 'destination'=>'目的地',
  69. 'recipient'=>'收件人',
  70. 'recipient_mobile'=>'收件人电话',
  71. 'charge'=>'收费(元)',
  72. 'ordering_remark'=>'下单备注',
  73. 'carrier'=>'承运商',
  74. 'carrier_bill'=>'承运商单号',
  75. 'origination_city'=>'始发市',
  76. 'destination_city'=>'目的市',
  77. 'warehouse_weight'=>'仓库计数(抛)',
  78. 'warehouse_weight_other'=>'仓库计数二',
  79. 'carrier_weight'=>'承运商计数(抛)',
  80. 'carrier_weight_other'=>'承运商计数二',
  81. 'carType'=>'车型',
  82. 'car_owner_info'=>'车型',
  83. 'fee'=>'运费(元)',
  84. 'pick_up_fee'=>'提货费(元)',
  85. 'other_fee'=>'其他费用(元)',
  86. 'collect_fee'=>'到付金额(元)',
  87. 'dispatch_remark'=>'调度备注',
  88. 'waybillAuditor'=>'运单审核人',
  89. 'dispatchAuditor'=>'调度审核人',
  90. 'created_at'=>'运单创建时间',
  91. 'total_receivable'=>'应收款(元)',
  92. 'total_expense'=>'应付款(元)',
  93. 'gross_margin'=>'毛利(元)',
  94. 'gross_profit_rate'=>'毛利率(元)',
  95. 'snapshotCreated_at'=>'报表生成时间',
  96. ]];
  97. $list=[];
  98. for ($i=0; $i<count($id);$i++){
  99. $waybillFinancialSnapshot=WaybillFinancialSnapshot::where('waybill_id','=',$id[$i])->first();
  100. $waybillFinancialSnapshot=json_decode($waybillFinancialSnapshot->json_content);
  101. $waybillAuditor='';
  102. $dispatchAuditor='';
  103. foreach ($waybillFinancialSnapshot->waybill->waybill_audit_logs as $waybill_audit_logs){
  104. if ($waybill_audit_logs->audit_stage=="运单阶段"){
  105. $waybillAuditor=$waybill_audit_logs->user->name;
  106. }
  107. if ($waybill_audit_logs->audit_stage=="调度阶段"){
  108. $dispatchAuditor=$waybill_audit_logs->user->name;
  109. }
  110. };
  111. $w=[
  112. 'type'=>isset($waybillFinancialSnapshot->waybill->type)?$waybillFinancialSnapshot->waybill->type:'',
  113. 'waybill_number'=>isset($waybillFinancialSnapshot->waybill->waybill_number)?$waybillFinancialSnapshot->waybill->waybill_number:'',
  114. 'owner'=>isset($waybillFinancialSnapshot->waybill->owner->name)?$waybillFinancialSnapshot->waybill->owner->name:'',
  115. 'wms_bill_number'=>isset($waybillFinancialSnapshot->waybill->wms_bill_number)?$waybillFinancialSnapshot->waybill->wms_bill_number:'',
  116. 'origination'=>isset($waybillFinancialSnapshot->waybill->origination)?$waybillFinancialSnapshot->waybill->origination:'',
  117. 'destination'=>isset($waybillFinancialSnapshot->waybill->destination)?$waybillFinancialSnapshot->waybill->destination:'',
  118. 'recipient'=>isset($waybillFinancialSnapshot->waybill->recipient)?$waybillFinancialSnapshot->waybill->recipient:'',
  119. 'recipient_mobile'=>isset($waybillFinancialSnapshot->waybill->recipient_mobile)?$waybillFinancialSnapshot->waybill->recipient_mobile:'',
  120. 'charge'=>isset($waybillFinancialSnapshot->waybill->charge)?$waybillFinancialSnapshot->waybill->charge:'',
  121. 'ordering_remark'=>isset($waybillFinancialSnapshot->waybill->ordering_remark)?$waybillFinancialSnapshot->waybill->ordering_remark:'',
  122. 'carrier'=>isset($waybillFinancialSnapshot->waybill->carrier->name)?$waybillFinancialSnapshot->waybill->carrier->name:'',
  123. 'carrier_bill'=>isset($waybillFinancialSnapshot->waybill->carrier_bill)?$waybillFinancialSnapshot->waybill->carrier_bill:'',
  124. 'origination_city'=>isset($waybillFinancialSnapshot->waybill->origination_city->name)?$waybillFinancialSnapshot->waybill->origination_city->name:'',
  125. 'destination_city'=>isset($waybillFinancialSnapshot->waybill->destination_city->name)?$waybillFinancialSnapshot->waybill->destination_city->name:'',
  126. 'warehouse_weight'=>isset($waybillFinancialSnapshot->waybill->warehouse_weight)?
  127. $waybillFinancialSnapshot->waybill->warehouse_weight.' '.(isset($waybillFinancialSnapshot->waybill->warehouse_weight_unit_name)?$waybillFinancialSnapshot->waybill->warehouse_weight_unit_name:''):'',
  128. 'warehouse_weight_other'=>isset($waybillFinancialSnapshot->waybill->warehouse_weight_other)?
  129. $waybillFinancialSnapshot->waybill->warehouse_weight_other.' '.(isset($waybillFinancialSnapshot->waybill->warehouse_weight_unit_other_name)?$waybillFinancialSnapshot->waybill->warehouse_weight_unit_other_name:''):'',
  130. 'carrier_weight'=>isset($waybillFinancialSnapshot->waybill->carrier_weight)?
  131. $waybillFinancialSnapshot->waybill->carrier_weight.' '.(isset($waybillFinancialSnapshot->waybill->warehouse_weight_unit_name)?$waybillFinancialSnapshot->waybill->warehouse_weight_unit_name:''):'',
  132. 'carrier_weight_other'=>isset($waybillFinancialSnapshot->waybill->carrier_weight_other)?
  133. $waybillFinancialSnapshot->waybill->carrier_weight_other.' '.(isset($waybillFinancialSnapshot->waybill->carrier_weight_unit_other_name)?$waybillFinancialSnapshot->waybill->carrier_weight_unit_other_name:''):'',
  134. 'carType'=>isset($waybillFinancialSnapshot->waybill->carType->name)?$waybillFinancialSnapshot->waybill->carType->name.($waybillFinancialSnapshot->waybill->carType->length.'米'):'',
  135. 'car_owner_info'=>isset($waybillFinancialSnapshot->waybill->car_owner_info)?$waybillFinancialSnapshot->waybill->car_owner_info:'',
  136. 'fee'=>isset($waybillFinancialSnapshot->waybill->fee)?$waybillFinancialSnapshot->waybill->fee:'',
  137. 'pick_up_fee'=>isset($waybillFinancialSnapshot->waybill->pick_up_fee)?$waybillFinancialSnapshot->waybill->pick_up_fee:'',
  138. 'other_fee'=>isset($waybillFinancialSnapshot->waybill->other_fee)?$waybillFinancialSnapshot->waybill->other_fee:'',
  139. 'collect_fee'=>isset($waybillFinancialSnapshot->waybill->collect_fee)?$waybillFinancialSnapshot->waybill->collect_fee:'',
  140. 'dispatch_remark'=>isset($waybillFinancialSnapshot->waybill->dispatch_remark)?$waybillFinancialSnapshot->waybill->dispatch_remark:'',
  141. 'waybillAuditor'=>isset($waybillAuditor)?$waybillAuditor:'',
  142. 'dispatchAuditor'=>isset($dispatchAuditor)?$dispatchAuditor:'',
  143. 'created_at'=>isset($waybillFinancialSnapshot->waybill->created_at)?$waybillFinancialSnapshot->waybill->created_at:'',
  144. 'total_receivable'=>isset($waybillFinancialSnapshot->total_receivable)?$waybillFinancialSnapshot->total_receivable:'',
  145. 'total_expense'=>isset($waybillFinancialSnapshot->total_expense)?$waybillFinancialSnapshot->total_expense:'',
  146. 'gross_margin'=>isset($waybillFinancialSnapshot->gross_margin)?$waybillFinancialSnapshot->gross_margin:'',
  147. 'gross_profit_rate'=>isset($waybillFinancialSnapshot->gross_profit_rate)?$waybillFinancialSnapshot->gross_profit_rate:'',
  148. 'snapshotCreated_at'=>isset($waybillFinancialSnapshot->created_at)?$waybillFinancialSnapshot->created_at:'',
  149. ];
  150. $list[$i]=$w;
  151. }
  152. $this->log(__METHOD__,__FUNCTION__,json_encode($waybillFinancialSnapshot),Auth::user()['id']);
  153. return Excel::download(new WaybillExport($row,$list), date('Y:m:d ') . '财务报表.xlsx');
  154. }
  155. }