WaybillsController.php 55 KB

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