WaybillFinancialSnapshotsController.php 10 KB

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