WaybillsController.php 54 KB

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