WaybillsController.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\WaybillAuditLog;
  4. use App\WaybillPriceModel;
  5. use App\Carrier;
  6. use App\CarType;
  7. use App\City;
  8. use App\Exports\WaybillExport;
  9. use App\Owner;
  10. use App\Unit;
  11. use App\Waybill;
  12. use App\WaybillPayoff;
  13. use App\WaybillFinancialExcepted;
  14. use App\WaybillFinancialSnapshot;
  15. use Carbon\Carbon;
  16. use Illuminate\Http\Request;
  17. use Illuminate\Support\Facades\Auth;
  18. use Illuminate\Support\Facades\Gate;
  19. use Illuminate\Support\Facades\Validator;
  20. use Maatwebsite\Excel\Facades\Excel;
  21. use Ramsey\Uuid\Uuid;
  22. class WaybillsController extends Controller
  23. {
  24. public function index(Request $request)
  25. {
  26. if(!Gate::allows('运单管理-查询')){ return redirect(url('/')); }
  27. $data=$request->input();
  28. if ($data != null ) {
  29. $today=Carbon::now()->subDays(15);
  30. $waybills=Waybill::with(['owner', 'waybillAuditLogs' => function ($query) {
  31. return $query->with('user');
  32. }])->orderBy('id','DESC');
  33. if ($request->input('waybill_number')){
  34. $waybills =$waybills->where('waybill_number','like','%'.$request->input('waybill_number').'%')->where('created_at','>',$today->format('Y-m-d'));
  35. }
  36. if ($request->input('carrier_bill')){
  37. $waybills=$waybills->where('carrier_bill','like','%'.$request->input('carrier_bill').'%')->where('created_at','>',$today->format('Y-m-d'));
  38. }
  39. if ($request->input('carrier_id')){
  40. $waybills=$waybills->where('carrier_id','=',$request->input('carrier_id'));
  41. }
  42. if ($request->input('owner_id')){
  43. $waybills=$waybills->where('owner_id','=',$request->input('owner_id'));
  44. }
  45. if ($request->input('wms_bill_number')){
  46. $waybills=$waybills->where('wms_bill_number','like','$'.$request->input('wms_bill_number').'%')->where('created_at','>',$today->format('Y-m-d'));
  47. }
  48. if ($request->input('created_at_start')){
  49. $waybills=$waybills->where('created_at','>',$request->input('created_at_start'));
  50. }
  51. if ($request->input('created_at_end')){
  52. $waybills=$waybills->where('created_at','<',$request->input('created_at_end'));
  53. }
  54. $waybills=$waybills->paginate($request->input('paginate')?$request->input('paginate'):50);
  55. if (!$waybills&&$request->input('waybill_number')){
  56. $waybills=Waybill::with(['owner', 'waybillAuditLogs' => function ($query) {
  57. return $query->with('user');
  58. }])->orderBy('id','DESC')->where('type','专线')->where('waybill_number',$request->input('waybill_number'))
  59. ->paginate($request->input('paginate')?$request->input('paginate'):50);
  60. }
  61. $carries = Carrier::get();
  62. $owners = Owner::get();
  63. return view('waybill.index', ['waybills' => $waybills, 'carriers' => $carries, 'owners' => $owners,'filterData'=>$data,'uriType'=>'']);
  64. } else {
  65. $waybills = Waybill::with(['owner', 'waybillAuditLogs' => function ($query) {
  66. return $query->with('user');
  67. }])->paginate(50);
  68. $carries = Carrier::get();
  69. $owners = Owner::get();
  70. return view('waybill.index', ['waybills' => $waybills, 'carriers' => $carries, 'owners' => $owners,'filterData'=>$data,'uriType'=>'']);
  71. }
  72. }
  73. public function indexZF(Request $request){
  74. if(!Gate::allows('运单管理-查询')){ return redirect(url('/')); }
  75. $data=$request->input();
  76. if ($data != null ) {
  77. $today=Carbon::now()->subDays(15);
  78. $waybills=Waybill::with(['owner', 'waybillAuditLogs' => function ($query) {
  79. return $query->with('user');
  80. }])->orderBy('id','DESC')->where('type','直发车');
  81. if ($request->input('waybill_number')){
  82. $waybills =$waybills->where('waybill_number','like','%'.$request->input('waybill_number').'%')->where('created_at','>',$today->format('Y-m-d'));
  83. }
  84. if ($request->input('carrier_bill')){
  85. $waybills=$waybills->where('carrier_bill','like','%'.$request->input('carrier_bill').'%')->where('created_at','>',$today->format('Y-m-d'));
  86. }
  87. if ($request->input('carrier_id')){
  88. $waybills=$waybills->where('carrier_id','=',$request->input('carrier_id'));
  89. }
  90. if ($request->input('owner_id')){
  91. $waybills=$waybills->where('owner_id','=',$request->input('owner_id'));
  92. }
  93. if ($request->input('wms_bill_number')){
  94. $waybills=$waybills->where('wms_bill_number','like','$'.$request->input('wms_bill_number').'%')->where('created_at','>',$today->format('Y-m-d'));
  95. }
  96. if ($request->input('created_at_start')){
  97. $waybills=$waybills->where('created_at','>',$request->input('created_at_start'));
  98. }
  99. if ($request->input('created_at_end')){
  100. $waybills=$waybills->where('created_at','<',$request->input('created_at_end'));
  101. }
  102. $waybills=$waybills->paginate($request->input('paginate')?$request->input('paginate'):50);
  103. if (!$waybills&&$request->input('waybill_number')){
  104. $waybills=Waybill::with(['owner', 'waybillAuditLogs' => function ($query) {
  105. return $query->with('user');
  106. }])->orderBy('id','DESC')->where('type','专线')->where('waybill_number',$request->input('waybill_number'))
  107. ->paginate($request->input('paginate')?$request->input('paginate'):50);
  108. }
  109. $carries = Carrier::get();
  110. $owners = Owner::get();
  111. return view('waybill.index', ['waybills' => $waybills, 'carriers' => $carries, 'owners' => $owners,'filterData'=>$data,'uriType'=>'ZF']);
  112. } else {
  113. $waybills = Waybill::with(['owner', 'waybillAuditLogs' => function ($query) {
  114. return $query->with('user');
  115. }])->where('type','直发车')->paginate(50);
  116. $carries = Carrier::get();
  117. $owners = Owner::get();
  118. return view('waybill.index', ['waybills' => $waybills, 'carriers' => $carries, 'owners' => $owners,'filterData'=>$data,'uriType'=>'ZF']);
  119. }
  120. }
  121. public function indexZX(Request $request){
  122. if(!Gate::allows('运单管理-查询')){ return redirect(url('/')); }
  123. $data=$request->input();
  124. if ($data != null ) {
  125. $today=Carbon::now()->subDays(15);
  126. $waybills=Waybill::with(['owner', 'waybillAuditLogs' => function ($query) {
  127. return $query->with('user');
  128. }])->orderBy('id','DESC')->where('type','专线');
  129. if ($request->input('waybill_number')){
  130. $waybills =$waybills->where('waybill_number','like','%'.$request->input('waybill_number').'%')->where('created_at','>',$today->format('Y-m-d'));
  131. }
  132. if ($request->input('carrier_bill')){
  133. $waybills=$waybills->where('carrier_bill','like','%'.$request->input('carrier_bill').'%')->where('created_at','>',$today->format('Y-m-d'));
  134. }
  135. if ($request->input('carrier_id')){
  136. $waybills=$waybills->where('carrier_id','=',$request->input('carrier_id'));
  137. }
  138. if ($request->input('owner_id')){
  139. $waybills=$waybills->where('owner_id','=',$request->input('owner_id'));
  140. }
  141. if ($request->input('wms_bill_number')){
  142. $waybills=$waybills->where('wms_bill_number','like','$'.$request->input('wms_bill_number').'%')->where('created_at','>',$today->format('Y-m-d'));
  143. }
  144. if ($request->input('created_at_start')){
  145. $waybills=$waybills->where('created_at','>',$request->input('created_at_start'));
  146. }
  147. if ($request->input('created_at_end')){
  148. $waybills=$waybills->where('created_at','<',$request->input('created_at_end'));
  149. }
  150. $waybills=$waybills->paginate($request->input('paginate')?$request->input('paginate'):50);
  151. if (!$waybills&&$request->input('waybill_number')){
  152. $waybills=Waybill::with(['owner', 'waybillAuditLogs' => function ($query) {
  153. return $query->with('user');
  154. }])->orderBy('id','DESC')->where('type','专线')->where('waybill_number',$request->input('waybill_number'))
  155. ->paginate($request->input('paginate')?$request->input('paginate'):50);
  156. }
  157. $carries = Carrier::get();
  158. $owners = Owner::get();
  159. return view('waybill.index', ['waybills' => $waybills, 'carriers' => $carries, 'owners' => $owners,'filterData'=>$data,'uriType'=>'ZX']);
  160. } else {
  161. $waybills = Waybill::with(['owner', 'waybillAuditLogs' => function ($query) {
  162. return $query->with('user');
  163. }])->where('type','专线')->paginate(50);
  164. $carries = Carrier::get();
  165. $owners = Owner::get();
  166. return view('waybill.index', ['waybills' => $waybills, 'carriers' => $carries, 'owners' => $owners,'filterData'=>$data,'uriType'=>'ZX']);
  167. }
  168. }
  169. public function create()
  170. {
  171. if(!Gate::allows('运单管理-录入')){ return redirect(url('/')); }
  172. $owners=Owner::get();
  173. return view('waybill.create',['owners'=>$owners]);
  174. }
  175. public function createZF()
  176. {
  177. if(!Gate::allows('运单管理-录入')){ return redirect(url('/')); }
  178. $owners=Owner::get();
  179. return view('waybill.create',['owners'=>$owners,'type'=>'直发车']);
  180. }
  181. public function createZX()
  182. {
  183. if(!Gate::allows('运单管理-录入')){ return redirect(url('/')); }
  184. $owners=Owner::get();
  185. return view('waybill.create',['owners'=>$owners,'type'=>'专线']);
  186. }
  187. public function store(Request $request)
  188. {
  189. if(!Gate::allows('运单管理-录入')){ return redirect(url('/')); }
  190. $this->validatorWaybill($request)->validate();
  191. $data=$request->input();
  192. $waybill=new Waybill([
  193. 'type'=>$data['type'],
  194. 'state'=>'未审核',
  195. 'waybill_number'=>Uuid::uuid1(),
  196. 'owner_id'=>$data['owner_id'],
  197. 'wms_bill_number'=>$data['wms_bill_number'],
  198. 'origination'=>$data['origination'],
  199. 'destination'=>$data['destination'],
  200. 'recipient'=>$data['recipient'],
  201. 'recipient_mobile'=>$data['recipient_mobile'],
  202. 'charge'=>$data['charge'],
  203. 'collect_fee'=>isset($data['collect_fee'])?$data['collect_fee']:0,
  204. 'ordering_remark'=>$data['ordering_remark']
  205. ]);
  206. $waybill->save();
  207. $number_id=$waybill->id;
  208. if ($data['type']=='直发车'){
  209. $waybill_number='BSZF'.date ("ymd").str_pad($number_id>99999?$number_id%99999:$number_id,4,"0",STR_PAD_LEFT);
  210. $waybill->waybill_number=$waybill_number;
  211. $waybill->update();
  212. }else{
  213. $waybill_number='BSZX'.date ("ymd").str_pad($number_id>99999?$number_id%99999:$number_id,4,"0",STR_PAD_LEFT);
  214. $waybill->waybill_number=$waybill_number;
  215. $waybill->update();
  216. }
  217. $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  218. return redirect('waybill')->with('successTip','新运单“'.$waybill_number.'”录入成功');
  219. }
  220. public function edit($id)
  221. {
  222. if(!Gate::allows('运单管理-编辑')){ return redirect(url('/')); }
  223. $waybill=Waybill::find($id);
  224. $carriers=Carrier::get();
  225. $cities=City::get();
  226. $units=Unit::get();
  227. $carTypes=CarType::get();
  228. return view('waybill/edit',['waybill'=>$waybill,'carriers'=>$carriers,'cities'=>$cities,'units'=>$units,'carTypes'=>$carTypes]);
  229. }
  230. public function update(Request $request, $id)
  231. {
  232. if(!Gate::allows('运单管理-调度')){ return redirect(url('/')); }
  233. $this->validatorWaybillDispatch($request)->validate();
  234. $data=$request->input();
  235. $waybill=Waybill::find($id);
  236. $waybill->fill($data);
  237. if ($waybill->save()){
  238. if ($waybill->type=="直发车"){
  239. $total_receivable=($waybill->charge);
  240. $total_expense=($waybill->fee)+($waybill->other_fee)-($waybill->collect_fee);
  241. }else if ($waybill->type=="专线"){
  242. $waybillPriceModel_id=$request->input('waybillPriceModel');
  243. if ($waybillPriceModel_id){
  244. $carrier_weight=$request->input('carrier_weight');
  245. $waybillPriceModel=WaybillPriceModel::find($waybillPriceModel_id);
  246. $carrier=Carrier::find($waybill->carrier_id);
  247. if ($carrier_weight<$waybillPriceModel->initial_weight){
  248. $fee=(($waybillPriceModel->unit_price)*($waybillPriceModel->initial_weight))+$carrier->delivery_fee;
  249. }else{
  250. $fee=(($waybillPriceModel->unit_price)*$carrier_weight)+$carrier->delivery_fee;
  251. }
  252. $waybill->fee=$fee;
  253. $waybill->waybill_price_model_id=$waybillPriceModel_id;
  254. }
  255. $waybill->save();
  256. $total_receivable=($waybill->charge);
  257. $total_expense=($waybill->pick_up_fee)+($waybill->other_fee)+($waybill->fee);
  258. }
  259. $waybillPayoff=WaybillPayoff::where('waybill_id','=',$id)->first();
  260. if ($waybillPayoff){
  261. $waybillPayoff->waybill_id=$id;
  262. $waybillPayoff->total_expense=$total_expense;
  263. $waybillPayoff->total_receivable=$total_receivable;
  264. $waybillPayoff->gross_margin=$total_receivable-$total_expense;
  265. $waybillPayoff->gross_profit_rate=(($total_receivable-$total_expense)/$total_receivable);
  266. $waybillPayoff->save();
  267. }else{
  268. WaybillPayoff::create([
  269. 'waybill_id'=>$id,
  270. 'total_expense'=>$total_expense,
  271. 'total_receivable'=>$total_receivable,
  272. 'gross_margin'=>$total_receivable-$total_expense,
  273. 'gross_profit_rate'=>(($total_receivable-$total_expense)/$total_receivable),
  274. ]);
  275. };
  276. $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  277. return redirect('waybill')->with('successTip','运单“'.$waybill->waybill_number.'”调度成功');
  278. }
  279. }
  280. public function isWaybillPriceModel(Request $request){
  281. $carrier_id=$request->input('carrier_id');
  282. $destination_city_id=$request->input('destination_city_id');
  283. $carrier_weight=$request->input('carrier_weight');
  284. $carrier_weight_unit_id=$request->input('carrier_weight_unit_id');
  285. if(!$carrier_id)return['success'=>false];
  286. if(!$destination_city_id)return['success'=>false];
  287. if(!$carrier_weight)return['success'=>false];
  288. if(!$carrier_weight_unit_id)return['success'=>false];
  289. $waybillPriceModel=WaybillPriceModel::where('carrier_id',$carrier_id)->where('city_id',$destination_city_id)
  290. ->where('range_min','<',$carrier_weight)->where('range_max','>=',$carrier_weight)
  291. ->where('unit_id',$carrier_weight_unit_id)->first();
  292. if($waybillPriceModel)return['success'=>$waybillPriceModel->id];
  293. $waybillPriceModelRange=WaybillPriceModel::whereRaw('carrier_id = ? and city_id = ? and unit_id = ? and range_max is null',[$carrier_id,$destination_city_id,$carrier_weight_unit_id])->first();
  294. if ($waybillPriceModelRange){ return ['success'=>$waybillPriceModelRange->id];}
  295. $city=City::where('id',$destination_city_id)->select('province_id')->first();
  296. $waybillPriceModelProvince=WaybillPriceModel::whereRaw('carrier_id = ? and province_id = ? and unit_id = ? and range_max >= ? and range_min < ? and city_id is null',
  297. [$carrier_id,$city->province_id,$carrier_weight_unit_id,$carrier_weight,$carrier_weight])->first();
  298. if ($waybillPriceModelProvince){return ['success'=>$waybillPriceModelProvince->id];}
  299. $waybillPriceModelProvinceRange=WaybillPriceModel::whereRaw('carrier_id = ? and province_id = ? and unit_id = ? and range_max is null and city_id is null',
  300. [$carrier_id,$city->province_id,$carrier_weight_unit_id])->first();
  301. if ($waybillPriceModelProvinceRange){return ['success'=>$waybillPriceModelProvinceRange->id];}else{
  302. return ['success'=>false];
  303. }
  304. }
  305. public function waybillUpdate(Request $request, $id){
  306. if(!Gate::allows('运单管理-编辑')){ return redirect(url('/')); }
  307. $this->validatorWaybill($request)->validate();
  308. $data=$request->input();
  309. $waybill=Waybill::find($id);
  310. $waybill->fill($data);
  311. if ($waybill->save()){
  312. $this->log(__METHOD__,__FUNCTION__,json_encode($waybill),Auth::user()['id']);
  313. return redirect('waybill')->with('successTip','运单“'.$waybill->waybill_number.'”修改成功');
  314. }
  315. }
  316. public function waybillAudit(Request $request){
  317. if(!Gate::allows('运单管理-运单审核')){ return redirect(url('/')); }
  318. $id=$request->input('id');
  319. $waybill=Waybill::find($id);
  320. $isAudit=WaybillAuditLog::whereRaw('waybill_id = ? and audit_stage = ?',[$id,"运单阶段"])->first();
  321. if (empty($isAudit)){
  322. $waybillAuditLog=new WaybillAuditLog([
  323. 'waybill_id'=>$id,
  324. 'audit_stage'=>'运单阶段',
  325. 'user_id'=>Auth::id(),
  326. ]);
  327. $waybillAuditLog->save();
  328. $waybillAuditLog['user']=Auth::user();
  329. $waybill->state='已审核';
  330. $result=$waybill->save();
  331. $this->log(__METHOD__,__FUNCTION__,json_encode($waybill),Auth::user()['id']);
  332. return ['success'=>$result,'state'=>$waybill->state,'waybillAuditLog'=>$waybillAuditLog];
  333. }
  334. return ['exception'=>'请勿重复审核!'];
  335. }
  336. public function waybillEdit($id){
  337. if(!Gate::allows('运单管理-编辑')){ return redirect(url('/')); }
  338. $waybill=Waybill::find($id);
  339. $owners=Owner::get();
  340. return view('waybill.waybillEdit',['waybill'=>$waybill,'owners'=>$owners]);
  341. }
  342. public function waybillRetreatAudit(Request $request){
  343. if(!Gate::allows('运单管理-调度')){ return redirect(url('/')); }
  344. $id=$request->input('id');
  345. $waybill=Waybill::find($id);
  346. WaybillAuditLog::whereRaw('waybill_id = ? and audit_stage = ?',[$id,"运单阶段"])->delete();
  347. $waybill->state='待重审';
  348. $result=$waybill->save();
  349. $this->log(__METHOD__,__FUNCTION__,json_encode($waybill),Auth::user()['id']);
  350. return ['success'=>$result,'state'=>$waybill->state];
  351. }
  352. public function waybillEndAudit(Request $request){
  353. if(!Gate::allows('运单管理-调度审核')){ return redirect(url('/')); }
  354. $id=$request->input('id');
  355. $waybill=Waybill::find($id);
  356. $isAudit=WaybillAuditLog::whereRaw('waybill_id = ? and audit_stage = ?',[$id,"调度阶段"])->first();
  357. if (empty($isAudit)){
  358. $waybillAuditLog=new WaybillAuditLog([
  359. 'waybill_id'=>$id,
  360. 'audit_stage'=>'调度阶段',
  361. 'user_id'=>Auth::id(),
  362. ]);
  363. $waybillAuditLog->save();
  364. $waybillAuditLog['user']=Auth::user();
  365. if ($waybill->waybill_price_model_id||$waybill->type=='直发车'){
  366. $waybill->state='完结';
  367. $result=$waybill->save();
  368. $waybillPayoff=WaybillPayoff::where('waybill_id','=',$id)->first();
  369. $waybillPayoff->load(["waybill"]);
  370. $waybillPayoff->waybill->load(["owner","carrier","origination_city","destination_city","warehouse_weight_unit","carrier_weight_unit","carType","waybillAuditLogs"]);
  371. $waybillPayoff->waybill->waybillAuditLogs->load(["user"]);
  372. $waybillPayoffJson=json_encode($waybillPayoff,JSON_UNESCAPED_UNICODE);
  373. WaybillFinancialSnapshot::create([
  374. 'waybill_id'=>$id,
  375. 'json_content'=>$waybillPayoffJson,
  376. ]);
  377. }else{
  378. $waybill->state='未定义计费模型';
  379. $result=$waybill->save();
  380. $waybillPayoff=WaybillPayoff::where('waybill_id','=',$id)->first();
  381. $waybillPayoff->load(["waybill"]);
  382. $waybillPayoff->waybill->load(["owner","carrier","origination_city","destination_city","warehouse_weight_unit","carrier_weight_unit","carType","waybillAuditLogs"]);
  383. $waybillPayoff->waybill->waybillAuditLogs->load(["user"]);
  384. $waybillPayoffJson=json_encode($waybillPayoff,JSON_UNESCAPED_UNICODE);
  385. WaybillFinancialExcepted::create([
  386. 'waybill_id'=>$id,
  387. 'json_content'=>$waybillPayoffJson,
  388. ]);
  389. }
  390. $this->log(__METHOD__,__FUNCTION__,$waybillPayoffJson,Auth::user()['id']);
  391. return ['success'=>$result,'state'=>$waybill->state,'waybillAuditLog'=>$waybillAuditLog];
  392. }
  393. return ['exception'=>'请勿重复审核!'];
  394. }
  395. public function waybillExport($id){
  396. $id = explode( ',',$id);
  397. $row=[[
  398. 'type'=>'运单类型',
  399. 'waybill_number'=>'运单号',
  400. 'owner'=>'货主',
  401. 'wms_bill_number'=>'WMS单号',
  402. 'origination'=>'始发地',
  403. 'destination'=>'目的地',
  404. 'recipient'=>'收件人',
  405. 'recipient_mobile'=>'收件人电话',
  406. 'charge'=>'收费(元)',
  407. 'ordering_remark'=>'下单备注',
  408. 'carrier'=>'承运商',
  409. 'carrier_bill'=>'承运商单号',
  410. 'origination_city'=>'始发市',
  411. 'destination_city'=>'目的市',
  412. 'warehouse_weight'=>'仓库计重(抛)',
  413. 'warehouse_weight_unit'=>'仓库计重单位 ',
  414. 'carrier_weight'=>'承运商计重(抛)',
  415. 'carrier_weight_unit'=>'承运商计重单位',
  416. 'carType'=>'车型',
  417. 'car_owner_info'=>'车辆信息',
  418. 'fee'=>'运费(元)',
  419. 'pick_up_fee'=>'提货费(元)',
  420. 'other_fee'=>'其他费用(元)',
  421. 'collect_fee'=>'到付金额(元)',
  422. 'dispatch_remark'=>'调度备注',
  423. 'waybillAuditor'=>'运单审核人',
  424. 'dispatchAuditor'=>'调度审核人',
  425. 'created_at'=>'创建时间'
  426. ]];
  427. $feeVisible=true;
  428. if(!Gate::allows('运单管理-可见费用项')){
  429. $feeVisible=false;
  430. unset($row[0]['fee'],$row[0]['pick_up_fee'],$row[0]['other_fee'],$row[0]['collect_fee']);
  431. }
  432. $list=[];
  433. for ($i=0; $i<count($id);$i++){
  434. $waybill=Waybill::with(['owner', 'waybillAuditLogs' => function ($query) {
  435. return $query->with('user');
  436. }])->find($id[$i]);
  437. foreach ($waybill->waybillAuditLogs as $waybillAuditLog){
  438. if ($waybillAuditLog->audit_stage=="运单阶段"){
  439. $waybillAuditor=$waybillAuditLog->user->name;
  440. }else{
  441. $waybillAuditor='';
  442. }
  443. if ($waybillAuditLog->audit_stage=="调度阶段"){
  444. $dispatchAuditor=$waybillAuditLog->user->name;
  445. }else{
  446. $dispatchAuditor='';
  447. }
  448. };
  449. $w=[
  450. 'type'=>isset($waybill->type)?$waybill->type:'',
  451. 'waybill_number'=>isset($waybill->waybill_number)?$waybill->waybill_number:'',
  452. 'owner'=>isset($waybill->owner->name)?$waybill->owner->name:'',
  453. 'wms_bill_number'=>isset($waybill->wms_bill_number)?$waybill->wms_bill_number:'',
  454. 'origination'=>isset($waybill->origination)?$waybill->origination:'',
  455. 'destination'=>isset($waybill->destination)?$waybill->destination:'',
  456. 'recipient'=>isset($waybill->recipient)?$waybill->recipient:'',
  457. 'recipient_mobile'=>isset($waybill->recipient_mobile)?$waybill->recipient_mobile:'',
  458. 'charge'=>isset($waybill->charge)?$waybill->charge:'',
  459. 'ordering_remark'=>isset($waybill->ordering_remark)?$waybill->ordering_remark:'',
  460. 'carrier'=>isset($waybill->carrier_name)?$waybill->carrier_name:'',
  461. 'carrier_bill'=>isset($waybill->carrier_bill)?$waybill->carrier_bill:'',
  462. 'origination_city'=>isset($waybill->origination_city_name)?$waybill->origination_city_name:'',
  463. 'destination_city'=>isset($waybill->destination_city_name)?$waybill->destination_city_name:'',
  464. 'warehouse_weight'=>isset($waybill->warehouse_weight)?$waybill->warehouse_weight:'',
  465. 'warehouse_weight_unit'=>isset($waybill->warehouse_weight_unit_name)?$waybill->warehouse_weight_unit_name:'',
  466. 'carrier_weight'=>isset($waybill->carrier_weight)?$waybill->carrier_weight:'',
  467. 'carrier_weight_unit'=>isset($waybill->carrier_weight_unit_name)?$waybill->carrier_weight_unit_name:'',
  468. 'carType'=>isset($waybill->carType->name)?$waybill->carType->name.($waybill->carType->length.'米'):'',
  469. 'car_owner_info'=>isset($waybill->car_owner_info)?$waybill->car_owner_info:'',
  470. 'fee'=>isset($waybill->fee)?$waybill->fee:'',
  471. 'pick_up_fee'=>isset($waybill->pick_up_fee)?$waybill->pick_up_fee:'',
  472. 'other_fee'=>isset($waybill->other_fee)?$waybill->other_fee:'',
  473. 'collect_fee'=>isset($waybill->collect_fee)?$waybill->collect_fee:'',
  474. 'dispatch_remark'=>isset($waybill->dispatch_remark)?$waybill->dispatch_remark:'',
  475. 'waybillAuditor'=>isset($waybillAuditor)?$waybillAuditor:'',
  476. 'dispatchAuditor'=>isset($dispatchAuditor)?$dispatchAuditor:'',
  477. 'created_at'=>isset($waybill->created_at)?$waybill->created_at:''
  478. ];
  479. if(!$feeVisible){
  480. unset($w['fee'],$w['pick_up_fee'],$w['other_fee'],$w['collect_fee']);
  481. }
  482. $list[$i]=$w;
  483. }
  484. $this->log(__METHOD__,__FUNCTION__,json_encode($waybill),Auth::user()['id']);
  485. return Excel::download(new WaybillExport($row,$list), date('Y:m:d ') . '运单列表.xls');
  486. }
  487. protected function validatorWaybill(Request $request){
  488. $validator=Validator::make($request->input(),[
  489. 'owner_id'=>'required',
  490. 'wms_bill_number'=>'nullable|alpha_num|max:50|unique:waybills,wms_bill_number',
  491. 'origination'=>'required|max:255',
  492. 'destination'=>'required|max:255',
  493. 'recipient'=>'required|max:50',
  494. 'recipient_mobile'=>['required','regex:/^(\d{7,11})|(1[3|4|5|7|8][0-9]\d{4,8})$/'],
  495. 'charge'=>'required|min:0|numeric',
  496. 'collect_fee'=>'nullable|min:0|numeric',
  497. ],[
  498. 'required'=>':attribute 为必填项',
  499. 'alpha_num'=>':attribute 应为字母或数字',
  500. 'max'=>':attribute 字符过多',
  501. 'regex'=>':attribute 输入有误',
  502. 'integer'=>':attribute 应为整数',
  503. 'min'=>':attribute 不得为负',
  504. 'numeric'=>':attribute 应为数字',
  505. 'unique'=>':attribute 已存在',
  506. ],[
  507. 'owner_id'=>'货主',
  508. 'wms_bill_number'=>'WMS单号',
  509. 'origination'=>'始发地',
  510. 'destination'=>'目的地',
  511. 'recipient'=>'收件人',
  512. 'recipient_mobile'=>'收件人电话',
  513. 'charge'=>'收费',
  514. 'collect_fee'=>'到付金额',
  515. ]);
  516. return $validator;
  517. }
  518. protected function validatorWaybillDispatch(Request $request){
  519. if ($request->input('type')=='直发车'){
  520. $validator=Validator::make($request->input(),[
  521. 'carrier_bill'=>'required|alpha_num|max:50|unique:waybills,carrier_bill',
  522. 'fee'=>'required|min:0|numeric',
  523. 'other_fee'=>'nullable|min:0|numeric',
  524. ],[
  525. 'required'=>':attribute 为必填项',
  526. 'alpha_num'=>':attribute 应为字母或数字',
  527. 'max'=>':attribute 字符过多',
  528. 'min'=>':attribute 不得为负',
  529. 'numeric'=>':attribute 应为数字',
  530. 'unique'=>':attribute 已存在',
  531. ],[
  532. 'carrier_bill'=>'承运商单号',
  533. 'fee'=>'运费',
  534. 'other_fee'=>'其他费用',
  535. ]);
  536. return $validator;
  537. }else if ($request->input('type')=='专线'){
  538. $validator=Validator::make($request->input(),[
  539. 'carrier_bill'=>'required|alpha_num|max:50|unique:waybills,carrier_bill',
  540. 'warehouse_weight'=>'required|min:0|numeric',
  541. 'carrier_weight'=>'required|min:0|numeric',
  542. 'pick_up_fee'=>'required|min:0|numeric',
  543. 'other_fee'=>'nullable|min:0|numeric',
  544. 'carrier_id'=>'required|integer',
  545. 'destination_city_id'=>'required|integer',
  546. 'carrier_weight_unit_id'=>'required|integer',
  547. ],[
  548. 'required'=>':attribute 为必填项',
  549. 'alpha_num'=>':attribute 应为字母或数字',
  550. 'max'=>':attribute 字符过多',
  551. 'min'=>':attribute 不得为负',
  552. 'numeric'=>':attribute 应为数字',
  553. 'unique'=>':attribute 已存在',
  554. ],[
  555. 'carrier_bill'=>'承运商单号',
  556. 'warehouse_weight'=>'仓库计重(抛)',
  557. 'carrier_weight'=>'承运商计重(抛)',
  558. 'pick_up_fee'=>'提货费',
  559. 'other_fee'=>'其他费用',
  560. 'carrier_id'=>'承运商',
  561. 'destination_city_id'=>'目的市',
  562. 'carrier_weight_unit_id'=>'承运商计重单位',
  563. ]);
  564. return $validator;
  565. }else{
  566. return false;
  567. }
  568. }
  569. }