WaybillsController.php 52 KB

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