WaybillsController.php 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\UploadFile;
  4. use App\WaybillAuditLog;
  5. use App\WaybillOnTop;
  6. use App\WaybillPriceModel;
  7. use App\Carrier;
  8. use App\CarType;
  9. use App\City;
  10. use App\Exports\Export;
  11. use App\Owner;
  12. use App\Unit;
  13. use App\Waybill;
  14. use App\WaybillPayoff;
  15. use App\WaybillFinancialExcepted;
  16. use App\WaybillFinancialSnapshot;
  17. use Carbon\Carbon;
  18. use Illuminate\Http\Request;
  19. use Illuminate\Support\Facades\Auth;
  20. use Illuminate\Support\Facades\DB;
  21. use Illuminate\Support\Facades\Gate;
  22. use Illuminate\Support\Facades\Validator;
  23. use Intervention\Image\Facades\Image;
  24. use Maatwebsite\Excel\Facades\Excel;
  25. use Ramsey\Uuid\Uuid;
  26. class WaybillsController extends Controller
  27. {
  28. //超15天精确查询抽离 column前提:数据库字段名必须与request内字段名一致
  29. public function preciseQuery(string $column,Request $request,$waybills){
  30. $today=Carbon::now()->subDays(15);
  31. $waybillsTem=clone $waybills;
  32. $waybillsTem=$waybillsTem->where($column,'like','%'.$request->input($column).'%')->where('waybills.created_at','>',$today->format('Y-m-d'));
  33. if($waybillsTem->count()==0
  34. ||$waybillsTem->get()[0][$column]==$request->input($column)){
  35. $waybills=$waybills->where($column,$request->input($column));
  36. }else{
  37. $waybills=$waybillsTem;
  38. }
  39. return $waybills;
  40. }
  41. public function conditionQuery(Request $request,$waybills){
  42. if ($request->input('exportType')&&$request->input('exportType')==1){
  43. $checkData=explode(',',$request->input('checkData'));
  44. $waybills=$waybills->whereIn("id",$checkData)->get();
  45. $excel=$this->deliveringExport($waybills);
  46. return $excel;
  47. }
  48. if ($request->input('waybill_number')){
  49. $waybills=$this->preciseQuery("waybill_number",$request,$waybills);
  50. }
  51. if ($request->input('carrier_bill')){
  52. $waybills=$this->preciseQuery("carrier_bill",$request,$waybills);
  53. }
  54. if ($request->input('carrier_id')){
  55. $waybills=$waybills->where('carrier_id','=',$request->input('carrier_id'));
  56. }
  57. if ($request->input('owners')){
  58. $owners=array_values(json_decode($request->input('owners'),true));
  59. if (count($owners)>0)$waybills=$waybills->whereIn('owner_id',$owners);
  60. }
  61. if ($request->input('owner_id')){
  62. $waybills=$waybills->where('owner_id','=',$request->input('owner_id'));
  63. }
  64. if ($request->input('wms_bill_number')){
  65. $waybills=$this->preciseQuery("wms_bill_number",$request,$waybills);
  66. }
  67. if ($request->input('origination')){
  68. $waybills=$this->preciseQuery("origination",$request,$waybills);
  69. }
  70. if ($request->input('destination')){
  71. $waybills=$this->preciseQuery("destination",$request,$waybills);
  72. }
  73. if ($request->input('created_at_start')){
  74. $created_at_start=$request->input('created_at_start')." 00:00:00";
  75. $waybills=$waybills->where('waybills.created_at','>=',$created_at_start);
  76. }
  77. if ($request->input('created_at_end')){
  78. $created_at_end=$request->input('created_at_end')." 23:59:59";
  79. $waybills=$waybills->where('waybills.created_at','<=',$created_at_end);
  80. }
  81. if ($request->input('status')){
  82. $waybills=$waybills->where('status',$request->input('status'));
  83. }
  84. if ($request->input('exportType')&&$request->input('exportType')==2){
  85. $waybills=$waybills->get();
  86. $excel=$this->deliveringExport($waybills);
  87. return $excel;
  88. }
  89. if ($request->uriType=='ZF')$waybills=$waybills->where('type','直发车');
  90. if ($request->uriType=='ZX')$waybills=$waybills->where('type','专线');
  91. return $waybills;
  92. }
  93. public function index(Request $request)
  94. {
  95. if(!Gate::allows('运输管理-查询')){ return redirect(url('/')); }
  96. $data=$request->input();
  97. $carries = Carrier::get();
  98. $owners = Owner::filterAuthorities()->get();
  99. $ownerIds = $owners->map(function ($owner){
  100. return $owner['id'];
  101. })->all();
  102. $waybills = $this->getWaybills();
  103. $waybills=$waybills->whereIn('owner_id',$ownerIds);
  104. if ($data != null ) {
  105. $waybills=$this->conditionQuery($request,$waybills);
  106. if (!$waybills&&$request->input('waybill_number')){
  107. $waybills=Waybill::with(['owner', 'waybillAuditLogs' => function ($query) {
  108. return $query->with('user');
  109. }])->orderBy('id','DESC')->where('type','专线')
  110. ->where('waybill_number',$request->input('waybill_number'));
  111. }
  112. $waybills=$waybills->paginate($request->input('paginate')?$request->input('paginate'):50);
  113. } else {
  114. $waybills = $waybills->paginate(50);
  115. }
  116. return view('waybill.index', ['waybills' => $waybills, 'carriers' => $carries, 'owners' => $owners,'filterData'=>$data,'uriType'=>$request->uriType??'']);
  117. }
  118. public function create()
  119. {
  120. if(!Gate::allows('运输管理-录入')){ return redirect(url('/')); }
  121. $owners=Owner::get();
  122. return view('waybill.create',['owners'=>$owners]);
  123. }
  124. public function createZF()
  125. {
  126. if(!Gate::allows('运输管理-录入')){ return redirect(url('/')); }
  127. $owners=Owner::get();
  128. return view('waybill.create',['owners'=>$owners,'type'=>'直发车']);
  129. }
  130. public function createZX()
  131. {
  132. if(!Gate::allows('运输管理-录入')){ return redirect(url('/')); }
  133. $owners=Owner::get();
  134. return view('waybill.create',['owners'=>$owners,'type'=>'专线']);
  135. }
  136. public function store(Request $request)
  137. {
  138. if(!Gate::allows('运输管理-录入')){ return redirect(url('/')); }
  139. $id=false;
  140. $this->validatorWaybill($request,$id)->validate();
  141. $data=$request->input();
  142. $waybill=new Waybill([
  143. 'type'=>$data['type'],
  144. 'status'=>'未审核',
  145. 'waybill_number'=>Uuid::uuid1(),
  146. 'owner_id'=>$data['owner_id'],
  147. 'wms_bill_number'=>$data['wms_bill_number'],
  148. 'origination'=>$data['origination'],
  149. 'destination'=>$data['destination'],
  150. 'recipient'=>$data['recipient'],
  151. 'recipient_mobile'=>$data['recipient_mobile'],
  152. 'charge'=>$data['charge'],
  153. 'ordering_remark'=>$data['ordering_remark']
  154. ]);
  155. if (isset($data['collect_fee'])&&$data['collect_fee']>0)$waybill->collect_fee=$data['collect_fee'];
  156. $waybill->save();
  157. $number_id=$waybill->id;
  158. if ($data['type']=='直发车'){
  159. $waybill_number='BSZF'.date ("ymd").str_pad($number_id>99999?$number_id%99999:$number_id,4,"0",STR_PAD_LEFT);
  160. $waybill->waybill_number=$waybill_number;
  161. $waybill->update();
  162. }else{
  163. $waybill_number='BSZX'.date ("ymd").str_pad($number_id>99999?$number_id%99999:$number_id,4,"0",STR_PAD_LEFT);
  164. $waybill->waybill_number=$waybill_number;
  165. $waybill->update();
  166. }
  167. $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  168. return redirect('waybill/index')->with('successTip','新运单“'.$waybill_number.'”录入成功');
  169. }
  170. public function edit($id)
  171. {
  172. if(!Gate::allows('运输管理-编辑')){ return redirect(url('/')); }
  173. $waybill=Waybill::find($id);
  174. if ($waybill->deliver_at){
  175. $waybill->deliver_at_date=Carbon::parse($waybill->deliver_at)->format('Y-m-d');
  176. $waybill->deliver_at_time=Carbon::parse($waybill->deliver_at)->format('H:i:s');
  177. }
  178. $carriers=Carrier::get();
  179. $cities=City::get();
  180. $units=Unit::get();
  181. $carTypes=CarType::get();
  182. return view('waybill/edit',['waybill'=>$waybill,'carriers'=>$carriers,'cities'=>$cities,'units'=>$units,'carTypes'=>$carTypes]);
  183. }
  184. public function update(Request $request, $id)
  185. {
  186. if(!Gate::allows('运输管理-调度')){ return redirect(url('/')); }
  187. $waybill=Waybill::find($id);
  188. if (!$request->warehouse_weight && $request->warehouse_weight_unit_id){
  189. unset($request->warehouse_weight_unit_id);
  190. }
  191. if (!$request->warehouse_weight_other && $request->warehouse_weight_unit_id_other){
  192. unset($request->warehouse_weight_unit_id_other);
  193. }
  194. if (!$request->carrier_weight && $request->carrier_weight_unit_id){
  195. unset($request->carrier_weight_unit_id);
  196. }
  197. if (!$request->carrier_weight_other && $request->carrier_weight_unit_id_other){
  198. unset($request->carrier_weight_unit_id_other);
  199. }
  200. $this->validatorWaybillDispatch($request,$id)->validate();
  201. //替换换行符
  202. if ($request->dispatch_remark){
  203. $request->dispatch_remark = str_replace(PHP_EOL,' ',$request->dispatch_remark);
  204. }
  205. if (!$request->destination)$request->destination = $waybill->destination;
  206. if ($request->destination_city_id && $waybill->destination_city_id != $request->destination_city_id){
  207. $city=City::find($request->destination_city_id);
  208. if ($city && (mb_strpos($request->destination,$city->name)===false || mb_strpos($request->destination,$city->province_name)===false)){
  209. if (mb_strpos($request->destination,$city->name)===false && mb_strpos($request->destination,$city->province_name)===false){
  210. $request->destination=$city->province_name.$city->name.$request->destination;
  211. goto sign;
  212. }
  213. if (mb_strpos($request->destination,$city->province_name)===false){
  214. $request->destination=$city->province_name.$request->destination;
  215. }
  216. if (mb_strpos($request->destination,$city->name)===false){
  217. $province_name=$city->province_name;
  218. $start_index=mb_strpos($request->destination,$city->province_name.'省');
  219. if ($start_index===false)$start_index=mb_strpos($request->destination,$city->province_name);
  220. else $province_name=$province_name.'省';
  221. $strBefore=mb_substr($request->destination,$start_index,mb_strlen($province_name));
  222. $strAfter=mb_substr($request->destination,$start_index+mb_strlen($province_name));
  223. $request->destination=$strBefore.$city->name.$strAfter;
  224. }
  225. }
  226. }
  227. sign:
  228. $total_receivable=0;
  229. $waybill->fill($request->input());
  230. if ($waybill->save()){
  231. if ($waybill->type=="直发车"){
  232. if ($waybill->charge)$total_receivable=($waybill->charge);
  233. elseif ($waybill->collect_fee)$total_receivable=($waybill->collect_fee);
  234. $total_expense=($waybill->fee)+($waybill->other_fee)-($waybill->collect_fee);
  235. }else {
  236. $waybillPriceModel_id=$request->input('waybillPriceModel');
  237. if ($waybillPriceModel_id){
  238. $carrier_weight=$request->input('carrier_weight');
  239. $waybillPriceModel=WaybillPriceModel::find($waybillPriceModel_id);
  240. $carrier=Carrier::find($waybill->carrier_id);
  241. if ($carrier_weight<$waybillPriceModel->initial_weight){
  242. $fee=(($waybillPriceModel->unit_price)*($waybillPriceModel->initial_weight))+$carrier->delivery_fee;
  243. }else{
  244. $fee=(($waybillPriceModel->unit_price)*$carrier_weight)+$carrier->delivery_fee;
  245. }
  246. if ($waybillPriceModel->base_fee&&$fee<$waybillPriceModel->base_fee){
  247. $fee=$waybillPriceModel->base_fee;
  248. }
  249. $waybill->fee=$fee;
  250. $waybill->waybill_price_model_id=$waybillPriceModel_id;
  251. }
  252. $waybill->save();
  253. if ($waybill->charge)$total_receivable=($waybill->charge);
  254. elseif($waybill->collect_fee) {
  255. $total_receivable=($waybill->collect_fee);
  256. }
  257. $total_expense=($waybill->pick_up_fee)+($waybill->other_fee)+($waybill->fee);
  258. }
  259. if ($total_receivable>0){
  260. $waybillPayoff=WaybillPayoff::where('waybill_id','=',$id)->first();
  261. if ($waybillPayoff){
  262. $waybillPayoff->waybill_id=$id;
  263. $waybillPayoff->total_expense=$total_expense;
  264. $waybillPayoff->total_receivable=$total_receivable;
  265. $waybillPayoff->gross_margin=$total_receivable-$total_expense;
  266. $waybillPayoff->gross_profit_rate=(($total_receivable-$total_expense)/$total_receivable);
  267. $waybillPayoff->save();
  268. }else{
  269. WaybillPayoff::create([
  270. 'waybill_id'=>$id,
  271. 'total_expense'=>$total_expense,
  272. 'total_receivable'=>$total_receivable,
  273. 'gross_margin'=>$total_receivable-$total_expense,
  274. 'gross_profit_rate'=>(($total_receivable-$total_expense)/$total_receivable),
  275. ]);
  276. };
  277. }
  278. $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  279. return redirect('waybill/index')->with('successTip','运单“'.$waybill->waybill_number.'”调度成功');
  280. }
  281. }
  282. public function checkWaybillPriceModel($carrier_id,$destination_city_id,$carrier_weight,$carrier_weight_unit_id){
  283. //确保承运商计数与计数单位为一个数组且长度2
  284. if(!$carrier_id)return false;
  285. if(!$destination_city_id)return false;
  286. if(!$carrier_weight)return false;
  287. if(!$carrier_weight_unit_id)return false;
  288. //多个计数标准,计算价格,取最贵
  289. if ($carrier_weight[0]&&$carrier_weight[1]&&$carrier_weight_unit_id[0]&&$carrier_weight_unit_id[1]){
  290. //城市价格区间不为空
  291. $waybillPriceModelOne=WaybillPriceModel::where('carrier_id',$carrier_id)->where('city_id',$destination_city_id)
  292. ->where('range_min','<',$carrier_weight[0])->where('range_max','>=',$carrier_weight[0])
  293. ->where('unit_id',$carrier_weight_unit_id[0])->first();
  294. $waybillPriceModelTwo=WaybillPriceModel::where('carrier_id',$carrier_id)->where('city_id',$destination_city_id)
  295. ->where('range_min','<',$carrier_weight[1])->where('range_max','>=',$carrier_weight[1])
  296. ->where('unit_id',$carrier_weight_unit_id[1])->first();
  297. if ($waybillPriceModelOne&&$waybillPriceModelTwo){
  298. if ($waybillPriceModelOne->unit_price*$carrier_weight[0]>=$waybillPriceModelTwo->unit_price*$carrier_weight[1]){
  299. return $waybillPriceModelOne->id;
  300. }else{
  301. return $waybillPriceModelTwo->id;
  302. }
  303. }
  304. if ($waybillPriceModelOne)return $waybillPriceModelOne->id;
  305. if ($waybillPriceModelTwo)return $waybillPriceModelTwo->id;
  306. //价格区间为空
  307. $waybillPriceModelRangeOne=WaybillPriceModel::whereRaw('carrier_id = ? AND city_id = ? AND unit_id = ? AND range_max IS NULL',[$carrier_id,$destination_city_id,$carrier_weight_unit_id[0]])->first();
  308. $waybillPriceModelRangeTwo=WaybillPriceModel::whereRaw('carrier_id = ? AND city_id = ? AND unit_id = ? AND range_max IS NULL',[$carrier_id,$destination_city_id,$carrier_weight_unit_id[1]])->first();
  309. if ($waybillPriceModelRangeOne&&$waybillPriceModelRangeTwo){
  310. if ($waybillPriceModelRangeOne->unit_price*$carrier_weight[0]>=$waybillPriceModelRangeTwo->unit_price*$carrier_weight[1]){
  311. return $waybillPriceModelRangeOne->id;
  312. }else{
  313. return $waybillPriceModelRangeTwo->id;
  314. }
  315. }
  316. if ($waybillPriceModelRangeOne)return $waybillPriceModelRangeOne->id;
  317. if ($waybillPriceModelRangeTwo)return $waybillPriceModelRangeTwo->id;
  318. //城市为空
  319. $city=City::where('id',$destination_city_id)->select('province_id')->first();
  320. $waybillPriceModelProvinceOne=WaybillPriceModel::whereRaw('carrier_id = ? AND province_id = ? AND unit_id = ? AND range_max >= ? AND range_min < ? AND city_id IS NULL',
  321. [$carrier_id,$city->province_id,$carrier_weight_unit_id[0],$carrier_weight[0],$carrier_weight[0]])->first();
  322. $waybillPriceModelProvinceTwo=WaybillPriceModel::whereRaw('carrier_id = ? AND province_id = ? AND unit_id = ? AND range_max >= ? AND range_min < ? AND city_id IS NULL',
  323. [$carrier_id,$city->province_id,$carrier_weight_unit_id[1],$carrier_weight[1],$carrier_weight[1]])->first();
  324. if ($waybillPriceModelProvinceOne&&$waybillPriceModelProvinceTwo){
  325. if ($waybillPriceModelProvinceOne->unit_price*$carrier_weight[0]>=$waybillPriceModelProvinceTwo->unit_price*$carrier_weight[1]){
  326. return $waybillPriceModelProvinceOne->id;
  327. }else{
  328. return $waybillPriceModelProvinceTwo->id;
  329. }
  330. }
  331. if ($waybillPriceModelProvinceOne)return $waybillPriceModelProvinceOne->id;
  332. if ($waybillPriceModelProvinceTwo)return $waybillPriceModelProvinceTwo->id;
  333. //城市价格区间都为空
  334. $waybillPriceModelProvinceRangeOne=WaybillPriceModel::whereRaw('carrier_id = ? AND province_id = ? AND unit_id = ? AND range_max IS NULL AND city_id IS NULL',
  335. [$carrier_id,$city->province_id,$carrier_weight_unit_id[0]])->first();
  336. $waybillPriceModelProvinceRangeTwo=WaybillPriceModel::whereRaw('carrier_id = ? AND province_id = ? AND unit_id = ? AND range_max IS NULL AND city_id IS NULL',
  337. [$carrier_id,$city->province_id,$carrier_weight_unit_id[1]])->first();
  338. if ($waybillPriceModelProvinceRangeOne&&$waybillPriceModelProvinceRangeTwo){
  339. if ($waybillPriceModelProvinceRangeOne->unit_price*$carrier_weight[0]>=$waybillPriceModelProvinceRangeTwo->unit_price*$carrier_weight[1]){
  340. return $waybillPriceModelProvinceRangeOne->id;
  341. }else{
  342. return $waybillPriceModelProvinceRangeOne->id;
  343. }
  344. }
  345. if ($waybillPriceModelProvinceRangeOne)return $waybillPriceModelProvinceRangeOne->id;
  346. if ($waybillPriceModelProvinceRangeOne)return $waybillPriceModelProvinceRangeOne->id;
  347. };
  348. for ($i=0;$i<count($carrier_weight);$i++){
  349. if ($carrier_weight[$i]&&$carrier_weight_unit_id[$i]){
  350. //城市价格区间不为空
  351. $waybillPriceModel=WaybillPriceModel::where('carrier_id',$carrier_id)->where('city_id',$destination_city_id)
  352. ->where('range_min','<',$carrier_weight[$i])->where('range_max','>=',$carrier_weight[$i])
  353. ->where('unit_id',$carrier_weight_unit_id[$i])->first();
  354. if($waybillPriceModel)return $waybillPriceModel->id;
  355. //价格区间为空
  356. $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[$i]])->first();
  357. if ($waybillPriceModelRange){ return $waybillPriceModelRange->id;}
  358. //城市为空
  359. $city=City::where('id',$destination_city_id)->select('province_id')->first();
  360. $waybillPriceModelProvince=WaybillPriceModel::whereRaw('carrier_id = ? AND province_id = ? AND unit_id = ? AND range_max >= ? AND range_min < ? AND city_id IS NULL',
  361. [$carrier_id,$city->province_id,$carrier_weight_unit_id[$i],$carrier_weight[$i],$carrier_weight[$i]])->first();
  362. if ($waybillPriceModelProvince){return $waybillPriceModelProvince->id;}
  363. //城市价格区间都为空
  364. $waybillPriceModelProvinceRange=WaybillPriceModel::whereRaw('carrier_id = ? AND province_id = ? AND unit_id = ? AND range_max IS NULL AND city_id IS NULL',
  365. [$carrier_id,$city->province_id,$carrier_weight_unit_id[$i]])->first();
  366. if ($waybillPriceModelProvinceRange){return $waybillPriceModelProvinceRange->id;}
  367. }
  368. }
  369. return false;
  370. }
  371. /*三层条件:无优先级,找到第一个直接返回
  372. * 无论是否为KG||T,计数单位一为KG,计数单位一为T,计数单位二为KG,计数单位二为T
  373. * 计数一与计数二同时存在取最贵价格:
  374. * 计数一存在,二不存在:
  375. * 计数二存在,一不存在:
  376. * 城市价格区间不为空,城市价格区间都为空,城市为空,价格区间为空
  377. * */
  378. public function isWaybillPriceModel(Request $request){
  379. $carrier_id=$request->input('carrier_id');
  380. $destination_city_id=$request->input('destination_city_id');
  381. $carrier_weight=$request->input('carrier_weight');
  382. $carrier_weight_unit_id=$request->input('carrier_weight_unit_id');
  383. $validatorData=["carrier_id"=>$carrier_id,"destination_city_id"=>$destination_city_id,
  384. 'carrier_weight'=>$carrier_weight[0],"carrier_weight_unit_id"=>$carrier_weight_unit_id[0],
  385. "carrier_weight_other"=>$carrier_weight[1],"carrier_weight_unit_id_other"=>$carrier_weight_unit_id[1]];
  386. $errors=Validator::make($validatorData,[
  387. 'carrier_id'=>'required|integer',
  388. 'destination_city_id'=>'required|integer',
  389. 'carrier_weight'=>'nullable|min:0|numeric|max:999999',
  390. 'carrier_weight_unit_id'=>'required_with:carrier_weight',
  391. 'carrier_weight_other'=>'nullable|min:0|numeric|max:999999',
  392. 'carrier_weight_unit_id_other'=>'required_with:carrier_weight_other',
  393. ],[
  394. 'required'=>':attribute 为必填项',
  395. 'max'=>':attribute 字符过多或输入值过大',
  396. 'min'=>':attribute 不得为负',
  397. 'numeric'=>':attribute 应为数字',
  398. 'unique'=>':attribute 已存在',
  399. 'required_with'=>':attribute 未填',
  400. 'integer'=>':attribute 必须为数字',
  401. ],[
  402. 'carrier_weight'=>'承运商计数(抛)',
  403. 'carrier_id'=>'承运商',
  404. 'destination_city_id'=>'目的市',
  405. 'carrier_weight_unit_id'=>'承运商计数单位',
  406. 'carrier_weight_other'=>'承运商计数二',
  407. 'carrier_weight_unit_id_other'=>'承运商计数单位二',
  408. ])->errors();
  409. if (count($errors)>0)return ['error'=>$errors];
  410. $result=$this->checkWaybillPriceModel($carrier_id,$destination_city_id,$carrier_weight,$carrier_weight_unit_id);
  411. if (!$result){
  412. //单位为kg,T时
  413. $unitKG=Unit::where('name','kg')->first();
  414. $unitT=Unit::where('name','T')->first();
  415. if ($carrier_weight_unit_id[0]==$unitKG->id){
  416. $carrier_weight_unit_id[0]=$unitT->id;
  417. $carrier_weight[0]=$carrier_weight[0]/1000;
  418. $result=$this->checkWaybillPriceModel($carrier_id,$destination_city_id,$carrier_weight,$carrier_weight_unit_id);
  419. if ($result)return ['success'=>$result];
  420. }
  421. if ($carrier_weight_unit_id[1]==$unitKG->id){
  422. $carrier_weight_unit_id[1]=$unitT->id;
  423. $carrier_weight[1]=$carrier_weight[1]/1000;
  424. $result=$this->checkWaybillPriceModel($carrier_id,$destination_city_id,$carrier_weight,$carrier_weight_unit_id);
  425. if ($result)return ['success'=>$result];
  426. }
  427. if ($carrier_weight_unit_id[0]==$unitT->id){
  428. $carrier_weight_unit_id[0]=$unitKG->id;
  429. $carrier_weight[0]=$carrier_weight[0]*1000;
  430. $result=$this->checkWaybillPriceModel($carrier_id,$destination_city_id,$carrier_weight,$carrier_weight_unit_id);
  431. if ($result)return ['success'=>$result];
  432. }
  433. if ($carrier_weight_unit_id[1]==$unitT->id){
  434. $carrier_weight_unit_id[1]=$unitKG->id;
  435. $carrier_weight[1]=$carrier_weight[1]*1000;
  436. $result=$this->checkWaybillPriceModel($carrier_id,$destination_city_id,$carrier_weight,$carrier_weight_unit_id);
  437. if ($result)return ['success'=>$result];
  438. }
  439. }
  440. return ['success'=>$result];
  441. }
  442. public function waybillUpdate(Request $request, $id){
  443. if(!Gate::allows('运输管理-编辑')){ return redirect(url('/')); }
  444. $this->validatorWaybill($request,$id)->validate();
  445. $data=$request->input();
  446. $waybill=Waybill::find($id);
  447. $waybill->fill($data);
  448. if ($waybill->save()){
  449. $this->log(__METHOD__,__FUNCTION__,json_encode($waybill),Auth::user()['id']);
  450. return redirect('waybill/index')->with('successTip','运单“'.$waybill->waybill_number.'”修改成功');
  451. }
  452. }
  453. public function waybillAudit(Request $request){
  454. if(!Gate::allows('运输管理-运单审核')){ return redirect(url('/')); }
  455. $id=$request->input('id');
  456. $waybill=Waybill::find($id);
  457. $isAudit=WaybillAuditLog::whereRaw('waybill_id = ? and audit_stage = ?',[$id,"运单阶段"])->first();
  458. if (empty($isAudit)){
  459. $waybillAuditLog=new WaybillAuditLog([
  460. 'waybill_id'=>$id,
  461. 'audit_stage'=>'运单阶段',
  462. 'user_id'=>Auth::id(),
  463. ]);
  464. $waybillAuditLog->save();
  465. $waybillAuditLog['user']=Auth::user();
  466. $waybill->status='已审核';
  467. $result=$waybill->save();
  468. $this->log(__METHOD__,__FUNCTION__,json_encode($waybill),Auth::user()['id']);
  469. return ['success'=>$result,'status'=>$waybill->status,'waybillAuditLog'=>$waybillAuditLog];
  470. }
  471. return ['exception'=>'请勿重复审核!'];
  472. }
  473. public function waybillEdit($id){
  474. if(!Gate::allows('运输管理-编辑')){ return redirect(url('/')); }
  475. $waybill=Waybill::find($id);
  476. $owners=Owner::get();
  477. return view('waybill.waybillEdit',['waybill'=>$waybill,'owners'=>$owners]);
  478. }
  479. public function waybillRetreatAudit(Request $request){
  480. if(!Gate::allows('运输管理-调度')){ return redirect(url('/')); }
  481. $id=$request->input('id');
  482. $waybill=Waybill::find($id);
  483. WaybillAuditLog::whereRaw('waybill_id = ? and audit_stage = ?',[$id,"运单阶段"])->delete();
  484. $waybill->status='待重审';
  485. $result=$waybill->save();
  486. $this->log(__METHOD__,__FUNCTION__,json_encode($waybill),Auth::user()['id']);
  487. return ['success'=>$result,'status'=>$waybill->status];
  488. }
  489. public function waybillEndAudit(Request $request){
  490. if(!Gate::allows('运输管理-调度审核')){ return redirect(url('/')); }
  491. $id=$request->input('id');
  492. $waybill=Waybill::find($id);
  493. if (!$waybill->charge&&!$waybill->collect_fee)return ['exception'=>'收费或到付费用未填!'];
  494. if ($waybill->charge==0&&$waybill->collect_fee==0)return ['exception'=>'收费与到付费用都为0!'];
  495. if ($waybill->type=='专线'){
  496. if (!$waybill->carrier_weight||$waybill->carrier_weight==0)return ['exception'=>'承运商计重未填或为0!'];
  497. if (!$waybill->carrier_weight_unit_id)return ['exception'=>'承运商计重单位未选!'];
  498. }
  499. $isAudit=WaybillAuditLog::whereRaw('waybill_id = ? and audit_stage = ?',[$id,"调度阶段"])->first();
  500. if (empty($isAudit)){
  501. $waybillAuditLog=new WaybillAuditLog([
  502. 'waybill_id'=>$id,
  503. 'audit_stage'=>'调度阶段',
  504. 'user_id'=>Auth::id(),
  505. ]);
  506. $waybillAuditLog->save();
  507. $waybillAuditLog['user']=Auth::user();
  508. if ($waybill->waybill_price_model_id||$waybill->type=='直发车'){
  509. $waybill->status='已完结';
  510. $result=$waybill->save();
  511. $waybillPayoff=WaybillPayoff::where('waybill_id','=',$id)->first();
  512. $waybillPayoff->load(["waybill"]);
  513. $waybillPayoff->waybill->load(["owner","carrier","origination_city","destination_city","carType","waybillAuditLogs"]);
  514. $waybillPayoff->waybill->waybillAuditLogs->load(["user"]);
  515. $waybillPayoffJson=json_encode($waybillPayoff,JSON_UNESCAPED_UNICODE);
  516. WaybillFinancialSnapshot::create([
  517. 'waybill_id'=>$id,
  518. 'json_content'=>$waybillPayoffJson,
  519. ]);
  520. }else{
  521. $waybill->status='无模型';
  522. $result=$waybill->save();
  523. $waybillPayoff=WaybillPayoff::where('waybill_id','=',$id)->first();
  524. if ($waybillPayoff){
  525. $waybillPayoff->load(["waybill"]);
  526. $waybillPayoff->waybill->load(["owner","carrier","origination_city","destination_city","carType","waybillAuditLogs"]);
  527. $waybillPayoff->waybill->waybillAuditLogs->load(["user"]);
  528. $waybillPayoffJson=json_encode($waybillPayoff,JSON_UNESCAPED_UNICODE);
  529. WaybillFinancialExcepted::create([
  530. 'waybill_id'=>$id,
  531. 'json_content'=>$waybillPayoffJson,
  532. ]);
  533. }
  534. }
  535. $this->log(__METHOD__,__FUNCTION__,$waybillPayoffJson,Auth::user()['id']);
  536. return ['success'=>$result,'status'=>$waybill->status,'waybillAuditLog'=>$waybillAuditLog];
  537. }
  538. return ['exception'=>'请勿重复审核!'];
  539. }
  540. public function upload(Request $request){
  541. if(!Gate::allows('运输管理-图片上传')){ return '没有权限'; }
  542. $file=$request->file('file');
  543. $waybill_number=$request->input('waybill_number');
  544. $waybill=Waybill::where('waybill_number',$waybill_number)->first();
  545. if (!$waybill){
  546. return ['success'=>false,'error'=>"未找到该运单!"];
  547. }
  548. if ($waybill->upload_file_url){
  549. return ['success'=>false,'error'=>"该运单已存在照片!"];
  550. }
  551. if (!$file){
  552. return ['success'=>false,'error'=>"照片不得为空!"];
  553. }
  554. if (!$file->isValid()){
  555. return ['success'=>false,'error'=>"找不到照片!"];
  556. }
  557. $tmpFile = $file->getRealPath();
  558. if (! is_uploaded_file($tmpFile)) {
  559. return ['success'=>false,'error'=>"文件错误!"];
  560. }
  561. $fileExtension=$file->getClientOriginalExtension();
  562. // 5.存储, 生成一个随机文件名
  563. $fileName = date('ymd').'-'.Uuid::uuid1();//thumbnail common bulky
  564. $thumbnailName=storage_path('app/public/files/'.$fileName.'-thumbnail.'.$fileExtension);
  565. $commonName=storage_path('app/public/files/'.$fileName.'-common.'.$fileExtension);
  566. $bulkyName=storage_path('app/public/files/'.$fileName.'-bulky.'.$fileExtension);
  567. $result=move_uploaded_file ($tmpFile ,$bulkyName);
  568. if ($result){
  569. $img=Image::make($bulkyName);
  570. if ($img->height() > $img->width())
  571. $img->heighten(250)->save($commonName);
  572. else $img->widen(250)->save($commonName);
  573. $img->heighten(28)->save($thumbnailName);
  574. $uploadFile=new UploadFile([
  575. "table_name"=>"waybills",
  576. "table_id"=>$waybill->id,
  577. "url"=>'/files/'.$fileName,
  578. "type"=>$fileExtension,
  579. ]);
  580. if ($uploadFile->save())
  581. $this->log(__METHOD__,'图片上传',json_encode($request),Auth::user()['id']);
  582. $uploadFile->url=asset('/storage'.$uploadFile->url);
  583. return ['success'=>true,'data'=>$uploadFile];
  584. }
  585. return ['success'=>false,'error'=>"图片保存失败!"];
  586. }
  587. //删除照片
  588. public function deleteImg(Request $request){
  589. if(!Gate::allows('运输管理-图片删除')){ return '没有权限'; }
  590. $ids=$request->input('ids');
  591. $uploadFiles=UploadFile::where('table_name','waybills')->whereIn('table_id',$ids)->get();
  592. foreach ($uploadFiles as $uploadFile){
  593. $bulky=storage_path('app/public/'.$uploadFile->url.'-bulky.'.$uploadFile->type);
  594. $common=storage_path('app/public/'.$uploadFile->url.'-common.'.$uploadFile->type);
  595. $thumbnail=storage_path('app/public/'.$uploadFile->url.'-thumbnail.'.$uploadFile->type);
  596. if (file_exists($bulky) && file_exists($common) && file_exists($thumbnail)){
  597. unlink($bulky);unlink($common);unlink($thumbnail);
  598. }
  599. }
  600. UploadFile::where('table_name','waybills')->whereIn('table_id',$ids)->delete();
  601. $this->log(__METHOD__,'图片删除',json_encode($request),Auth::user()['id']);
  602. return ['success'=>true];
  603. }
  604. public function waybillExport($id,Request $request){
  605. if(!Gate::allows('运输管理-查询')){ return '没有权限'; }
  606. ini_set('max_execution_time',2500);
  607. ini_set('memory_limit','1526M');
  608. $waybills=Waybill::with(['owner', 'waybillAuditLogs' => function ($query) {
  609. $query->with('user');
  610. }]);
  611. if ($id==-1){
  612. $waybills=$this->conditionQuery($request,$waybills);
  613. $waybills=$waybills->get();
  614. }else {
  615. $id = explode( ',',$id);
  616. $waybills=$waybills->whereIn('id',$id)->get();
  617. };
  618. $row=[[
  619. 'type'=>'运单类型',
  620. 'owner'=>'货主',
  621. 'source_bill'=>'上游单号',
  622. 'wms_bill_number'=>'wms订单号',
  623. 'waybill_number'=>'运单号',
  624. 'charge'=>'运输收费',
  625. 'other_charge'=>'其他收费',
  626. 'other_charge_remark'=>'其他收费备注',
  627. 'origination'=>'始发地',
  628. 'destination'=>'目的地',
  629. 'carrier'=>'承运商',
  630. 'carrier_bill'=>'承运商单号',
  631. 'warehouse_weight'=>'仓库计数(抛)',
  632. 'carrier_weight'=>'承运商计数(抛)',
  633. 'warehouse_weight_other'=>'仓库计重',
  634. 'carrier_weight_other'=>'承运商计重',
  635. 'carType'=>'车型',
  636. 'car_owner_info'=>'车辆信息',
  637. 'amount'=>'计件',
  638. 'mileage'=>'里程数',
  639. 'fee'=>'运费(元)',
  640. 'pick_up_fee'=>'提货费(元)',
  641. 'other_fee'=>'其他费用(元)',
  642. 'deliver_at'=>'发货时间',
  643. 'dispatch_remark'=>'调度备注',
  644. 'created_at'=>'创建时间'
  645. ]];
  646. $feeVisible=true;
  647. if(!Gate::allows('运输管理-可见费用项')){
  648. $feeVisible=false;
  649. unset($row[0]['fee'],$row[0]['pick_up_fee'],$row[0]['other_fee'],$row[0]['collect_fee']);
  650. }
  651. $list=[];
  652. for ($i=0; $i<count($waybills);$i++){
  653. $waybill=$waybills[$i];
  654. $w=[
  655. 'type'=>isset($waybill->type)?$waybill->type:'',
  656. 'waybill_number'=>isset($waybill->waybill_number)?$waybill->waybill_number:'',
  657. 'owner'=>isset($waybill->owner->name)?$waybill->owner->name:'',
  658. 'source_bill'=>isset($waybill->source_bill)?$waybill->source_bill:'',
  659. 'wms_bill_number'=>isset($waybill->wms_bill_number)?$waybill->wms_bill_number:'',
  660. 'charge'=>isset($waybill->charge)?$waybill->charge:'',
  661. 'other_charge'=>isset($waybill->other_charge)?$waybill->other_charge:'',
  662. 'other_charge_remark'=>isset($waybill->other_charge_remark)?$waybill->other_charge_remark:'',
  663. 'origination'=>isset($waybill->origination)?$waybill->origination:'',
  664. 'destination'=>isset($waybill->destination)?$waybill->destination:'',
  665. 'recipient'=>isset($waybill->recipient)?$waybill->recipient:'',
  666. 'recipient_mobile'=>isset($waybill->recipient_mobile)?$waybill->recipient_mobile:'',
  667. //'charge'=>isset($waybill->charge)?$waybill->charge:'',
  668. //'ordering_remark'=>isset($waybill->ordering_remark)?$waybill->ordering_remark:'',
  669. 'carrier'=>isset($waybill->carrier_name)?$waybill->carrier_name:'',
  670. 'carrier_bill'=>isset($waybill->carrier_bill)?$waybill->carrier_bill:'',
  671. //'origination_city'=>isset($waybill->origination_city_name)?$waybill->origination_city_name:'',
  672. //'destination_city'=>isset($waybill->destination_city_name)?$waybill->destination_city_name:'',
  673. 'warehouse_weight'=>isset($waybill->warehouse_weight)?$waybill->warehouse_weight.' '.(isset($waybill->warehouse_weight_unit_name)?$waybill->warehouse_weight_unit_name:''):'',
  674. 'warehouse_weight_other'=>isset($waybill->warehouse_weight_other)?$waybill->warehouse_weight_other.' '.(isset($waybill->warehouse_weight_unit_other_name)?$waybill->warehouse_weight_unit_other_name:''):'',
  675. 'carrier_weight'=>isset($waybill->carrier_weight)?$waybill->carrier_weight.' '.(isset($waybill->carrier_weight_unit_name)?$waybill->carrier_weight_unit_name:''):'',
  676. 'carrier_weight_other'=>isset($waybill->carrier_weight_other)?$waybill->carrier_weight_other.' '.(isset($waybill->carrier_weight_unit_other_name)?$waybill->carrier_weight_unit_other_name:''):'',
  677. 'carType'=>isset($waybill->carType->name)?$waybill->carType->name.($waybill->carType->length.'米'):'',
  678. 'car_owner_info'=>isset($waybill->car_owner_info)?$waybill->car_owner_info:'',
  679. 'amount'=>isset($waybill->amount)?$waybill->amount.' '.(isset($waybill->amount_unit_name)?$waybill->amount_unit_name:''):'',
  680. 'mileage'=>isset($waybill->mileage)?$waybill->mileage:'',
  681. 'fee'=>isset($waybill->fee)?$waybill->fee:'',
  682. 'pick_up_fee'=>isset($waybill->pick_up_fee)?$waybill->pick_up_fee:'',
  683. 'other_fee'=>isset($waybill->other_fee)?$waybill->other_fee:'',
  684. //'collect_fee'=>isset($waybill->collect_fee)?$waybill->collect_fee:'',
  685. 'deliver_at'=>isset($waybill->deliver_at)?$waybill->deliver_at:'',
  686. 'dispatch_remark'=>isset($waybill->dispatch_remark)?$waybill->dispatch_remark:'',
  687. //'waybillAuditor'=>isset($waybillAuditor)?$waybillAuditor:'',
  688. //'dispatchAuditor'=>isset($dispatchAuditor)?$dispatchAuditor:'',
  689. 'created_at'=>isset($waybill->created_at)?$waybill->created_at:''
  690. ];
  691. if(!$feeVisible){
  692. unset($w['fee'],$w['pick_up_fee'],$w['other_fee'],$w['collect_fee']);
  693. }
  694. $list[$i]=$w;
  695. }
  696. return Excel::download(new Export($row,$list), date('Y:m:d ') . '运单列表.xlsx');
  697. }
  698. public function deliveringExport($waybills){
  699. ini_set('max_execution_time',2500);
  700. ini_set('memory_limit','1526M');
  701. $row=[[
  702. 'created_at'=>"日期",
  703. 'carrier_name'=>"承运商",
  704. 'waybill_number'=>"宝时运单号",
  705. 'origination'=>"提货仓",
  706. 'owner_name'=>"货主",
  707. 'warehouse_weight_other'=>"预估重量",
  708. 'warehouse_weight'=>"预估体积",
  709. 'status'=>"状态",
  710. 'carrier_bill'=>"专线运单号",
  711. 'inquire_tel'=>"查件电话",
  712. 'amount'=>"件数",
  713. 'carrier_weight_other'=>"重量",
  714. 'carrier_weight'=>"体积",
  715. ]];
  716. $list=[];
  717. foreach ($waybills as $waybill){
  718. $w=[
  719. 'created_at'=>$waybill->created_at,
  720. 'carrier_name'=>$waybill->carrier_name,
  721. 'waybill_number'=>$waybill->waybill_number,
  722. 'origination'=>$waybill->origination,
  723. 'owner_name'=>$waybill->owner_name,
  724. 'warehouse_weight_other'=>$waybill->warehouse_weight_other,
  725. 'warehouse_weight'=>$waybill->warehouse_weight,
  726. 'status'=>$waybill->status=="已完结"?"已完成":$waybill->carrier_bill?"已提交":"待提交",
  727. 'carrier_bill'=>$waybill->carrier_bill,
  728. 'inquire_tel'=>$waybill->inquire_tel,
  729. 'amount'=>$waybill->amount,
  730. 'carrier_weight_other'=>$waybill->carrier_weight_other,
  731. 'carrier_weight'=>$waybill->carrier_weight,
  732. ];
  733. array_push($list,$w);
  734. }
  735. return Excel::download(new Export($row,$list), date('Y:m:d ') . '发运列表.xlsx');
  736. }
  737. //发运
  738. public function delivering(Request $request){
  739. if (!Auth::user())return view('exception.login');
  740. if (Auth::user()->isSuperAdmin())$waybills=Waybill::orderBy('id','DESC');
  741. else{
  742. $carriersUsers=DB::table('carrier_user')->where('user_id',Auth::id())->get();
  743. $carrierIds=array_column($carriersUsers->toArray(),'carrier_id');
  744. $waybills=Waybill::orderBy('id','DESC')->whereIn("carrier_id",$carrierIds);
  745. }
  746. if ($request->input('exportType')){
  747. return $this->conditionQuery($request,$waybills);
  748. }
  749. $waybills=$this->conditionQuery($request,$waybills);
  750. return view('waybill.delivering',compact('waybills'));
  751. }
  752. //承运商提交
  753. public function storeCarrierBill(Request $request){
  754. $errors=Validator::make($request->input(),[
  755. 'id'=>'required|integer',
  756. 'carrier_bill'=>'required',
  757. 'inquire_tel'=>'nullable',
  758. 'amount'=>'nullable|integer',
  759. 'carrier_weight'=>'required_without:carrier_weight_other|nullable|numeric',
  760. 'carrier_weight_other'=>'required_without:carrier_weight|nullable|numeric',
  761. ],[
  762. 'required'=>':attribute 为必填项',
  763. 'integer'=>':attribute 应为整数',
  764. 'numeric'=>':attribute 应为数字',
  765. 'required_with'=>':attribute 重量与体积至少存在一项',
  766. ],[
  767. 'carrier_bill'=>'专线运单号',
  768. 'inquire_tel'=>'查件电话',
  769. 'amount'=>'件数',
  770. 'carrier_weight'=>'体积',
  771. 'carrier_weight_other'=>'重量',
  772. ])->errors();
  773. if (count($errors)>0)return ["errors"=>$errors];
  774. $waybill=Waybill::find($request->input('id'));
  775. // //承运商体积
  776. // if($request->input('carrier_weight')){
  777. // $waybill->amount_unit_id=2;
  778. // }
  779. // //承运商重量
  780. // if ($request->input('carrier_weight_other')){
  781. // $waybill->amount_unit_id=1;
  782. // }
  783. if (!$waybill)return ["error"=>"未找到该运单!"];
  784. $waybill->fill($request->input());
  785. $waybill->update();
  786. return $waybill;
  787. }
  788. protected function validatorWaybill(Request $request,$id){
  789. if ($id){$wms_bill_number=$id;};
  790. $validator=Validator::make($request->input(),[
  791. 'owner_id'=>'required',
  792. 'wms_bill_number'=>['nullable','max:50',isset($wms_bill_number)?"unique:waybills,wms_bill_number,$wms_bill_number":'unique:waybills,wms_bill_number'],
  793. 'origination'=>'required|max:255',
  794. 'destination'=>'required|max:255',
  795. 'recipient'=>'required|max:50',
  796. 'recipient_mobile'=>['required','regex:/^(\d{7,11})|(1[3|4|5|7|8][0-9]\d{4,8})$/'],
  797. 'charge'=>'nullable|min:0|max:999999|numeric',
  798. 'collect_fee'=>'nullable|min:0|numeric',
  799. ],[
  800. 'required'=>':attribute 为必填项',
  801. 'alpha_num'=>':attribute 应为字母或数字',
  802. 'max'=>':attribute 字符过多或输入值过大',
  803. 'regex'=>':attribute 输入有误',
  804. 'integer'=>':attribute 应为整数',
  805. 'min'=>':attribute 不得为负',
  806. 'numeric'=>':attribute 应为数字',
  807. 'unique'=>':attribute 已存在',
  808. ],[
  809. 'owner_id'=>'货主',
  810. 'wms_bill_number'=>'WMS单号',
  811. 'origination'=>'始发地',
  812. 'destination'=>'目的地',
  813. 'recipient'=>'收件人',
  814. 'recipient_mobile'=>'收件人电话',
  815. 'charge'=>'收费',
  816. 'collect_fee'=>'到付金额',
  817. ]);
  818. return $validator;
  819. }
  820. protected function validatorWaybillDispatch(Request $request,$id){
  821. $rule=[
  822. 'carrier_id'=>'required|integer',
  823. 'carrier_bill'=>"sometimes|nullable|max:50|unique:waybills,carrier_bill,$id",
  824. 'fee'=>'sometimes|nullable|min:0|numeric|max:999999',
  825. 'other_fee'=>'sometimes|nullable|min:0|numeric|max:999999',
  826. 'charge'=>'sometimes|nullable|min:0|numeric|max:999999',
  827. 'mileage'=>'nullable|numeric|min:0',
  828. 'amount'=>'nullable|numeric|min:0',
  829. 'amount_unit_id'=>'required',
  830. 'origination_city_id'=>'sometimes|required|integer',
  831. 'destination_city_id'=>'sometimes|required|integer',
  832. 'warehouse_weight_other'=>'sometimes|nullable|min:0|numeric|max:999999',
  833. 'warehouse_weight_unit_id_other'=>'sometimes|required_with:warehouse_weight_other|nullable|integer',
  834. 'pick_up_fee'=>'sometimes|nullable|min:0|numeric|max:999999',
  835. 'warehouse_weight'=>'sometimes|nullable|min:0|numeric|max:999999',
  836. 'warehouse_weight_unit_id'=>'sometimes|required_with:warehouse_weight|nullable|integer',
  837. 'carrier_weight'=>'sometimes|nullable|min:0|numeric|max:999999',
  838. 'carrier_weight_unit_id'=>'sometimes|required_with:carrier_weight',
  839. 'carrier_weight_other'=>'sometimes|nullable|min:0|numeric|max:999999',
  840. 'carrier_weight_unit_id_other'=>'sometimes|required_with:carrier_weight_other',
  841. ];
  842. if ($request->type == '专线'){
  843. $rule['origination_city_id']='required|integer';
  844. $rule['destination_city_id']='required|integer';
  845. }
  846. $validator=Validator::make($request->input(),$rule,[
  847. 'required'=>':attribute 为必填项',
  848. 'alpha_num'=>':attribute 应为字母或数字',
  849. 'max'=>':attribute 字符过多或输入值过大',
  850. 'min'=>':attribute 不得为负',
  851. 'numeric'=>':attribute 应为数字',
  852. 'unique'=>':attribute 已存在',
  853. 'required_with'=>':attribute 未填',
  854. 'integer'=>':attribute 必须为数字',
  855. ],[
  856. 'carrier_id'=>'承运商',
  857. 'carrier_bill'=>'承运商单号',
  858. 'fee'=>'运费',
  859. 'other_fee'=>'其他费用',
  860. 'charge'=>'收费',
  861. 'mileage'=>'里程数',
  862. 'amount'=>'计数',
  863. 'amount_unit_id'=>'计数单位',
  864. 'warehouse_weight'=>'仓库计数(抛)',
  865. 'carrier_weight'=>'承运商计数(抛)',
  866. 'pick_up_fee'=>'提货费',
  867. 'destination_city_id'=>'目的市',
  868. 'carrier_weight_unit_id'=>'承运商计数单位',
  869. 'warehouse_weight_unit_id'=>'仓库计数单位',
  870. 'warehouse_weight_other'=>'仓库计数二',
  871. 'carrier_weight_other'=>'承运商计数二',
  872. 'warehouse_weight_unit_id_other'=>'仓库技数单位二',
  873. 'carrier_weight_unit_id_other'=>'承运商计数单位二',
  874. ]);
  875. return $validator;
  876. }
  877. public function addCounty(Request $request){
  878. $rule =[
  879. 'destination_city' => ['required', 'string','unique:cities,name'],
  880. ];
  881. $messages=[
  882. 'destination_city.required'=>'市/县不能为空',
  883. 'destination_city.string'=>'市/县必须是字符串',
  884. 'destination_city.unique'=>'市/县已存在',
  885. ];
  886. $validator = Validator::make($request->all(),$rule,$messages);
  887. if ($validator->fails()) {
  888. return $validator->errors();
  889. }
  890. $city=new City([
  891. 'name'=>$request->input('destination_city'),
  892. 'type'=>3,
  893. ]);
  894. $city->save();
  895. return $city;
  896. }
  897. // 运单删除 软删除
  898. public function destroy(int $id){
  899. if(!GAte::allows('运输管理-删除')){return['success'=>0,'status'=>'没有权限'];}
  900. if(is_null($id)){return ['success'=>'0','status'=>'传入id为空'];}
  901. $result = Waybill::where('id',$id)->delete();
  902. return ['success'=>$result,'status'=>$result];
  903. }
  904. // 回收站
  905. public function recycle(Request $request){
  906. if(!Gate::allows('运输管理-删除')){return redirect('/');}
  907. $user = Auth::user();
  908. $paginate = $request->input('paginate')??50;
  909. $waybills = Waybill::with(['owner','waybillAuditLogs' => function ($query) {
  910. return $query->with('user');
  911. }])->orderBy('deleted_at', 'DESC')->withTrashed()->whereNotNull('deleted_at')->paginate(50);
  912. $total = $waybills->count();
  913. $paginateParams = [];
  914. $paginateParams['paginate'] = $paginate;
  915. return view('waybill.recycle',compact('waybills','total','paginateParams'));
  916. }
  917. // 软删除恢复
  918. public function apiRestoreSelected(Request $request){
  919. DB::enableQueryLog();
  920. if(!Gate::allows('运输管理-删除')){return ['success'=>'false','fail_info'=>'没有权限'];}
  921. $ids = $request->input('ids')??'';
  922. if($ids == ''){return ['success'=>'false','fail_info'=>'没有可恢复对象'];}
  923. $waybills = Waybill::withTrashed()->whereIn('id',$ids)->get();
  924. $result = '';
  925. $waybills->each(function (Waybill $waybill){
  926. $waybill->restore();
  927. });
  928. $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  929. return ['success'=>'true','waybills'=>$waybills];
  930. }
  931. // 修改运费
  932. public function changeFee(Request $request){
  933. if(!Gate::allows('运输管理-运费')){return ['success'=>'false','fail_info'=>'没有权限'];}
  934. $wayBillId = $request->input('id');
  935. $waybillFee = $request->input('fee');
  936. if(is_null($wayBillId) or is_null($waybillFee)){
  937. return ['success'=>'false','fail_info'=>'参数异常'];
  938. }
  939. $result = Waybill::where('id',$wayBillId)->update(['fee'=>$waybillFee]);
  940. $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  941. return ['success'=>$result,'status'=>$result];
  942. }
  943. // 置顶
  944. public function waybillOnTop(Request $request){
  945. $id = $request->input('id');
  946. $detail = $request->input('detail');
  947. if(!Gate::allows('运输管理-置顶')){return ['success'=>'false','fail_info'=>'没有权限'];}
  948. if(is_null($id)){
  949. return ['success'=>'false','fail_info'=>'传参错误'];
  950. }
  951. $wayontop = WaybillOnTop::withTrashed()->where('waybill_id',$id);
  952. $result = '';
  953. if(count($wayontop->get()) == 0){
  954. $wayontop = WaybillOnTop::create(['waybill_id'=>$id,'remark'=>$detail]);
  955. $result = $wayontop->save();
  956. }else{
  957. $wayontop = WaybillOnTop::withTrashed()->where('waybill_id',$id);
  958. $result = WaybillOnTop::withTrashed()->where('waybill_id',$id)->restore();
  959. }
  960. return ['success'=>$result,'status'=>$result];
  961. }
  962. // 取消置顶
  963. public function cancelOnTop(Request $request){
  964. $id = $request->input('id');
  965. if(!Gate::allows('运输管理-置顶')){return ['success'=>'false','fail_info'=>'没有权限'];}
  966. if(is_null($id)){
  967. return ['success'=>'false','fail_info'=>'传参错误'];
  968. }
  969. $result = WaybillOnTop::where('waybill_id',$id)->forceDelete();
  970. return ['success'=>$result,'status'=>$result];
  971. }
  972. // 获取所有运单信息
  973. public function getWaybills(){
  974. $waybills = Waybill::with(['owner','wmsCommodities','waybillAuditLogs' => function ($query) {
  975. return $query->with('user');
  976. }])->selectRaw('waybills.* ,waybill_on_tops.id top_id ,waybill_on_tops.remark,waybill_on_tops.updated_at top_update')
  977. ->leftJoin('waybill_on_tops','waybill_on_tops.waybill_id','=','waybills.id')
  978. ->whereNull('waybill_on_tops.deleted_at')
  979. ->orderBy('waybill_on_tops.updated_at','desc')
  980. ->orderBy('waybills.id','desc');
  981. return $waybills;
  982. }
  983. }