WaybillsController.php 34 KB

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