WaybillFinancialSnapshotsController.php 11 KB

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