WaybillsController.php 53 KB

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