WaybillsController.php 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\UploadFile;
  4. use App\WaybillAuditLog;
  5. use App\WaybillOnTop;
  6. use App\WaybillPriceModel;
  7. use App\Carrier;
  8. use App\CarType;
  9. use App\City;
  10. use App\Exports\Export;
  11. use App\Owner;
  12. use App\Unit;
  13. use App\Waybill;
  14. use App\WaybillPayoff;
  15. use App\WaybillFinancialExcepted;
  16. use App\WaybillFinancialSnapshot;
  17. use Carbon\Carbon;
  18. use Illuminate\Http\Request;
  19. use Illuminate\Support\Facades\Auth;
  20. use Illuminate\Support\Facades\DB;
  21. use Illuminate\Support\Facades\Gate;
  22. use Illuminate\Support\Facades\Validator;
  23. use Intervention\Image\Facades\Image;
  24. use Maatwebsite\Excel\Facades\Excel;
  25. use Ramsey\Uuid\Uuid;
  26. class WaybillsController extends Controller
  27. {
  28. //超15天精确查询抽离 column前提:数据库字段名必须与request内字段名一致
  29. public function preciseQuery(string $column,Request $request,$waybills){
  30. $today=Carbon::now()->subDays(15);
  31. $waybillsTem=clone $waybills;
  32. $waybillsTem=$waybillsTem->where($column,'like','%'.$request->input($column).'%')->where('waybills.created_at','>',$today->format('Y-m-d'));
  33. if($waybillsTem->count()==0
  34. ||$waybillsTem->get()[0][$column]==$request->input($column)){
  35. $waybills=$waybills->where($column,$request->input($column));
  36. }else{
  37. $waybills=$waybillsTem;
  38. }
  39. return $waybills;
  40. }
  41. public function conditionQuery(Request $request,$waybills){
  42. if ($request->input('exportType')&&$request->input('exportType')==1){
  43. $checkData=explode(',',$request->input('checkData'));
  44. $waybills=$waybills->whereIn("id",$checkData)->get();
  45. $excel=$this->deliveringExport($waybills);
  46. return $excel;
  47. }
  48. if ($request->input('waybill_number')){
  49. $waybills=$this->preciseQuery("waybill_number",$request,$waybills);
  50. }
  51. if ($request->input('carrier_bill')){
  52. $waybills=$this->preciseQuery("carrier_bill",$request,$waybills);
  53. }
  54. if ($request->input('carrier_id')){
  55. $waybills=$waybills->where('carrier_id','=',$request->input('carrier_id'));
  56. }
  57. if ($request->input('owners')){
  58. $owners=array_values(json_decode($request->input('owners'),true));
  59. if (count($owners)>0)$waybills=$waybills->whereIn('owner_id',$owners);
  60. }
  61. if ($request->input('owner_id')){
  62. $waybills=$waybills->where('owner_id','=',$request->input('owner_id'));
  63. }
  64. if ($request->input('wms_bill_number')){
  65. $waybills=$this->preciseQuery("wms_bill_number",$request,$waybills);
  66. }
  67. if ($request->input('origination')){
  68. $waybills=$this->preciseQuery("origination",$request,$waybills);
  69. }
  70. if ($request->input('destination')){
  71. $waybills=$this->preciseQuery("destination",$request,$waybills);
  72. }
  73. if ($request->input('created_at_start')){
  74. $created_at_start=$request->input('created_at_start')." 00:00:00";
  75. $waybills=$waybills->where('waybills.created_at','>=',$created_at_start);
  76. }
  77. if ($request->input('created_at_end')){
  78. $created_at_end=$request->input('created_at_end')." 23:59:59";
  79. $waybills=$waybills->where('waybills.created_at','<=',$created_at_end);
  80. }
  81. if ($request->input('status')){
  82. $waybills=$waybills->where('status',$request->input('status'));
  83. }
  84. if ($request->input('exportType')&&$request->input('exportType')==2){
  85. $waybills=$waybills->get();
  86. $excel=$this->deliveringExport($waybills);
  87. return $excel;
  88. }
  89. if ($request->uriType=='ZF')$waybills=$waybills->where('type','直发车');
  90. if ($request->uriType=='ZX')$waybills=$waybills->where('type','专线');
  91. return $waybills;
  92. }
  93. public function index(Request $request)
  94. {
  95. if(!Gate::allows('运输管理-查询')){ return redirect(url('/')); }
  96. $data=$request->input();
  97. $carries = Carrier::get();
  98. $owners = Owner::filterAuthorities()->get();
  99. $ownerIds = $owners->map(function ($owner){
  100. return $owner['id'];
  101. })->all();
  102. $waybills = $this->getWaybills();
  103. $waybills=$waybills->whereIn('owner_id',$ownerIds);
  104. if ($data != null ) {
  105. $waybills=$this->conditionQuery($request,$waybills);
  106. if (!$waybills&&$request->input('waybill_number')){
  107. $waybills=Waybill::with(['owner', 'waybillAuditLogs' => function ($query) {
  108. return $query->with('user');
  109. }])->orderBy('id','DESC')->where('type','专线')
  110. ->where('waybill_number',$request->input('waybill_number'));
  111. }
  112. $waybills=$waybills->paginate($request->input('paginate')?$request->input('paginate'):50);
  113. } else {
  114. $waybills = $waybills->paginate(50);
  115. }
  116. return view('waybill.index', ['waybills' => $waybills, 'carriers' => $carries, 'owners' => $owners,'filterData'=>$data,'uriType'=>$request->uriType??'']);
  117. }
  118. public function create()
  119. {
  120. if(!Gate::allows('运输管理-录入')){ return redirect(url('/')); }
  121. $owners=Owner::get();
  122. return view('waybill.create',['owners'=>$owners]);
  123. }
  124. public function createZF()
  125. {
  126. if(!Gate::allows('运输管理-录入')){ return redirect(url('/')); }
  127. $owners=Owner::get();
  128. return view('waybill.create',['owners'=>$owners,'type'=>'直发车']);
  129. }
  130. public function createZX()
  131. {
  132. if(!Gate::allows('运输管理-录入')){ return redirect(url('/')); }
  133. $owners=Owner::get();
  134. return view('waybill.create',['owners'=>$owners,'type'=>'专线']);
  135. }
  136. public function store(Request $request)
  137. {
  138. if(!Gate::allows('运输管理-录入')){ return redirect(url('/')); }
  139. $id=false;
  140. $this->validatorWaybill($request,$id)->validate();
  141. $data=$request->input();
  142. $waybill=new Waybill([
  143. 'type'=>$data['type'],
  144. 'status'=>'未审核',
  145. 'waybill_number'=>Uuid::uuid1(),
  146. 'owner_id'=>$data['owner_id'],
  147. 'wms_bill_number'=>$data['wms_bill_number'],
  148. 'origination'=>$data['origination'],
  149. 'destination'=>$data['destination'],
  150. 'recipient'=>$data['recipient'],
  151. 'recipient_mobile'=>$data['recipient_mobile'],
  152. 'charge'=>$data['charge'],
  153. 'ordering_remark'=>$data['ordering_remark']
  154. ]);
  155. if (isset($data['collect_fee'])&&$data['collect_fee']>0)$waybill->collect_fee=$data['collect_fee'];
  156. $waybill->save();
  157. $number_id=$waybill->id;
  158. if ($data['type']=='直发车'){
  159. $waybill_number='BSZF'.date ("ymd").str_pad($number_id>99999?$number_id%99999:$number_id,4,"0",STR_PAD_LEFT);
  160. $waybill->waybill_number=$waybill_number;
  161. $waybill->update();
  162. }else{
  163. $waybill_number='BSZX'.date ("ymd").str_pad($number_id>99999?$number_id%99999:$number_id,4,"0",STR_PAD_LEFT);
  164. $waybill->waybill_number=$waybill_number;
  165. $waybill->update();
  166. }
  167. $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  168. return redirect('waybill/index')->with('successTip','新运单“'.$waybill_number.'”录入成功');
  169. }
  170. public function edit($id)
  171. {
  172. if(!Gate::allows('运输管理-编辑')){ return redirect(url('/')); }
  173. $waybill=Waybill::find($id);
  174. $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(Request $request){
  601. if(!Gate::allows('运输管理-查询')){ return '没有权限'; }
  602. ini_set('max_execution_time',2500);
  603. ini_set('memory_limit','1526M');
  604. $exportType=$request->exportType;
  605. $waybills=Waybill::with(['owner', 'waybillAuditLogs' => function ($query) {
  606. $query->with('user');
  607. }]);
  608. if ($exportType=='all'){
  609. $waybills=$this->conditionQuery($request,$waybills);
  610. $waybills=$waybills->get();
  611. }else {
  612. $id = explode( ',',$request->data);
  613. $waybills=$waybills->whereIn('id',$id)->get();
  614. };
  615. $row=[[
  616. 'type'=>'运单类型',
  617. 'owner'=>'货主',
  618. 'source_bill'=>'上游单号',
  619. 'wms_bill_number'=>'wms订单号',
  620. 'waybill_number'=>'运单号',
  621. 'origination'=>'始发地',
  622. 'destination'=>'目的地',
  623. 'carrier'=>'承运商',
  624. 'carrier_bill'=>'承运商单号',
  625. 'warehouse_weight'=>'仓库计数(抛)',
  626. 'carrier_weight'=>'承运商计数(抛)',
  627. 'warehouse_weight_other'=>'仓库计重',
  628. 'carrier_weight_other'=>'承运商计重',
  629. 'fee'=>'运费(元)',
  630. 'pick_up_fee'=>'提货费(元)',
  631. 'other_fee'=>'其他费用(元)',
  632. 'dispatch_remark'=>'调度备注',
  633. 'created_at'=>'创建时间'
  634. ]];
  635. $feeVisible=true;
  636. if(!Gate::allows('运输管理-可见费用项')){
  637. $feeVisible=false;
  638. unset($row[0]['fee'],$row[0]['pick_up_fee'],$row[0]['other_fee'],$row[0]['collect_fee']);
  639. }
  640. $list=[];
  641. for ($i=0; $i<count($waybills);$i++){
  642. $waybill=$waybills[$i];
  643. $w=[
  644. 'type'=>isset($waybill->type)?$waybill->type:'',
  645. 'waybill_number'=>isset($waybill->waybill_number)?$waybill->waybill_number:'',
  646. 'owner'=>isset($waybill->owner->name)?$waybill->owner->name:'',
  647. 'source_bill'=>isset($waybill->source_bill)?$waybill->source_bill:'',
  648. 'wms_bill_number'=>isset($waybill->wms_bill_number)?$waybill->wms_bill_number:'',
  649. 'origination'=>isset($waybill->origination)?$waybill->origination:'',
  650. 'destination'=>isset($waybill->destination)?$waybill->destination:'',
  651. 'recipient'=>isset($waybill->recipient)?$waybill->recipient:'',
  652. 'recipient_mobile'=>isset($waybill->recipient_mobile)?$waybill->recipient_mobile:'',
  653. //'charge'=>isset($waybill->charge)?$waybill->charge:'',
  654. //'ordering_remark'=>isset($waybill->ordering_remark)?$waybill->ordering_remark:'',
  655. 'carrier'=>isset($waybill->carrier_name)?$waybill->carrier_name:'',
  656. 'carrier_bill'=>isset($waybill->carrier_bill)?$waybill->carrier_bill:'',
  657. //'origination_city'=>isset($waybill->origination_city_name)?$waybill->origination_city_name:'',
  658. //'destination_city'=>isset($waybill->destination_city_name)?$waybill->destination_city_name:'',
  659. 'warehouse_weight'=>isset($waybill->warehouse_weight)?$waybill->warehouse_weight.' '.(isset($waybill->warehouse_weight_unit_name)?$waybill->warehouse_weight_unit_name:''):'',
  660. '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:''):'',
  661. 'carrier_weight'=>isset($waybill->carrier_weight)?$waybill->carrier_weight.' '.(isset($waybill->carrier_weight_unit_name)?$waybill->carrier_weight_unit_name:''):'',
  662. '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:''):'',
  663. //'carType'=>isset($waybill->carType->name)?$waybill->carType->name.($waybill->carType->length.'米'):'',
  664. //'car_owner_info'=>isset($waybill->car_owner_info)?$waybill->car_owner_info:'',
  665. 'fee'=>isset($waybill->fee)?$waybill->fee:'',
  666. 'pick_up_fee'=>isset($waybill->pick_up_fee)?$waybill->pick_up_fee:'',
  667. 'other_fee'=>isset($waybill->other_fee)?$waybill->other_fee:'',
  668. //'collect_fee'=>isset($waybill->collect_fee)?$waybill->collect_fee:'',
  669. 'dispatch_remark'=>isset($waybill->dispatch_remark)?$waybill->dispatch_remark:'',
  670. //'waybillAuditor'=>isset($waybillAuditor)?$waybillAuditor:'',
  671. //'dispatchAuditor'=>isset($dispatchAuditor)?$dispatchAuditor:'',
  672. 'created_at'=>isset($waybill->created_at)?$waybill->created_at:''
  673. ];
  674. if(!$feeVisible){
  675. unset($w['fee'],$w['pick_up_fee'],$w['other_fee'],$w['collect_fee']);
  676. }
  677. $list[$i]=$w;
  678. }
  679. return Excel::download(new Export($row,$list), date('Y:m:d ') . '运单列表.xlsx');
  680. }
  681. public function deliveringExport($waybills){
  682. ini_set('max_execution_time',2500);
  683. ini_set('memory_limit','1526M');
  684. $row=[[
  685. 'created_at'=>"日期",
  686. 'carrier_name'=>"承运商",
  687. 'waybill_number'=>"宝时运单号",
  688. 'origination'=>"提货仓",
  689. 'owner_name'=>"货主",
  690. 'warehouse_weight_other'=>"预估重量",
  691. 'warehouse_weight'=>"预估体积",
  692. 'status'=>"状态",
  693. 'carrier_bill'=>"专线运单号",
  694. 'inquire_tel'=>"查件电话",
  695. 'amount'=>"件数",
  696. 'carrier_weight_other'=>"重量",
  697. 'carrier_weight'=>"体积",
  698. ]];
  699. $list=[];
  700. foreach ($waybills as $waybill){
  701. $w=[
  702. 'created_at'=>$waybill->created_at,
  703. 'carrier_name'=>$waybill->carrier_name,
  704. 'waybill_number'=>$waybill->waybill_number,
  705. 'origination'=>$waybill->origination,
  706. 'owner_name'=>$waybill->owner_name,
  707. 'warehouse_weight_other'=>$waybill->warehouse_weight_other,
  708. 'warehouse_weight'=>$waybill->warehouse_weight,
  709. 'status'=>$waybill->status=="已完结"?"已完成":$waybill->carrier_bill?"已提交":"待提交",
  710. 'carrier_bill'=>$waybill->carrier_bill,
  711. 'inquire_tel'=>$waybill->inquire_tel,
  712. 'amount'=>$waybill->amount,
  713. 'carrier_weight_other'=>$waybill->carrier_weight_other,
  714. 'carrier_weight'=>$waybill->carrier_weight,
  715. ];
  716. array_push($list,$w);
  717. }
  718. return Excel::download(new Export($row,$list), date('Y:m:d ') . '发运列表.xlsx');
  719. }
  720. //发运
  721. public function delivering(Request $request){
  722. if (!Auth::user())return view('exception.login');
  723. if (Auth::user()->isSuperAdmin())$waybills=Waybill::orderBy('id','DESC');
  724. else{
  725. $carriersUsers=DB::table('carrier_user')->where('user_id',Auth::id())->get();
  726. $carrierIds=array_column($carriersUsers->toArray(),'carrier_id');
  727. $waybills=Waybill::orderBy('id','DESC')->whereIn("carrier_id",$carrierIds);
  728. }
  729. if ($request->input('exportType')){
  730. return $this->conditionQuery($request,$waybills);
  731. }
  732. $waybills=$this->conditionQuery($request,$waybills);
  733. return view('waybill.delivering',compact('waybills'));
  734. }
  735. //承运商提交
  736. public function storeCarrierBill(Request $request){
  737. $errors=Validator::make($request->input(),[
  738. 'id'=>'required|integer',
  739. 'carrier_bill'=>'required',
  740. 'inquire_tel'=>'nullable',
  741. 'amount'=>'nullable|integer',
  742. 'carrier_weight'=>'required_without:carrier_weight_other|nullable|numeric',
  743. 'carrier_weight_other'=>'required_without:carrier_weight|nullable|numeric',
  744. ],[
  745. 'required'=>':attribute 为必填项',
  746. 'integer'=>':attribute 应为整数',
  747. 'numeric'=>':attribute 应为数字',
  748. 'required_with'=>':attribute 重量与体积至少存在一项',
  749. ],[
  750. 'carrier_bill'=>'专线运单号',
  751. 'inquire_tel'=>'查件电话',
  752. 'amount'=>'件数',
  753. 'carrier_weight'=>'体积',
  754. 'carrier_weight_other'=>'重量',
  755. ])->errors();
  756. if (count($errors)>0)return ["errors"=>$errors];
  757. $waybill=Waybill::find($request->input('id'));
  758. // //承运商体积
  759. // if($request->input('carrier_weight')){
  760. // $waybill->amount_unit_id=2;
  761. // }
  762. // //承运商重量
  763. // if ($request->input('carrier_weight_other')){
  764. // $waybill->amount_unit_id=1;
  765. // }
  766. if (!$waybill)return ["error"=>"未找到该运单!"];
  767. $waybill->fill($request->input());
  768. $waybill->update();
  769. return $waybill;
  770. }
  771. protected function validatorWaybill(Request $request,$id){
  772. if ($id){$wms_bill_number=$id;};
  773. $validator=Validator::make($request->input(),[
  774. 'owner_id'=>'required',
  775. 'wms_bill_number'=>['nullable','max:50',isset($wms_bill_number)?"unique:waybills,wms_bill_number,$wms_bill_number":'unique:waybills,wms_bill_number'],
  776. 'origination'=>'required|max:255',
  777. 'destination'=>'required|max:255',
  778. 'recipient'=>'required|max:50',
  779. 'recipient_mobile'=>['required','regex:/^(\d{7,11})|(1[3|4|5|7|8][0-9]\d{4,8})$/'],
  780. 'charge'=>'nullable|min:0|max:999999|numeric',
  781. 'collect_fee'=>'nullable|min:0|numeric',
  782. ],[
  783. 'required'=>':attribute 为必填项',
  784. 'alpha_num'=>':attribute 应为字母或数字',
  785. 'max'=>':attribute 字符过多或输入值过大',
  786. 'regex'=>':attribute 输入有误',
  787. 'integer'=>':attribute 应为整数',
  788. 'min'=>':attribute 不得为负',
  789. 'numeric'=>':attribute 应为数字',
  790. 'unique'=>':attribute 已存在',
  791. ],[
  792. 'owner_id'=>'货主',
  793. 'wms_bill_number'=>'WMS单号',
  794. 'origination'=>'始发地',
  795. 'destination'=>'目的地',
  796. 'recipient'=>'收件人',
  797. 'recipient_mobile'=>'收件人电话',
  798. 'charge'=>'收费',
  799. 'collect_fee'=>'到付金额',
  800. ]);
  801. return $validator;
  802. }
  803. protected function validatorWaybillDispatch(Request $request,$id){
  804. $rule=[
  805. 'carrier_id'=>'required|integer',
  806. 'carrier_bill'=>"sometimes|nullable|max:50|unique:waybills,carrier_bill,$id",
  807. 'fee'=>'sometimes|nullable|min:0|numeric|max:999999',
  808. 'other_fee'=>'sometimes|nullable|min:0|numeric|max:999999',
  809. 'charge'=>'sometimes|nullable|min:0|numeric|max:999999',
  810. 'mileage'=>'nullable|numeric|min:0',
  811. 'amount'=>'nullable|numeric|min:0',
  812. 'amount_unit_id'=>'required',
  813. 'origination_city_id'=>'sometimes|required|integer',
  814. 'destination_city_id'=>'sometimes|required|integer',
  815. 'warehouse_weight_other'=>'sometimes|nullable|min:0|numeric|max:999999',
  816. 'warehouse_weight_unit_id_other'=>'sometimes|required_with:warehouse_weight_other|nullable|integer',
  817. 'pick_up_fee'=>'sometimes|nullable|min:0|numeric|max:999999',
  818. 'warehouse_weight'=>'sometimes|nullable|min:0|numeric|max:999999',
  819. 'warehouse_weight_unit_id'=>'sometimes|required_with:warehouse_weight|nullable|integer',
  820. 'carrier_weight'=>'sometimes|nullable|min:0|numeric|max:999999',
  821. 'carrier_weight_unit_id'=>'sometimes|required_with:carrier_weight',
  822. 'carrier_weight_other'=>'sometimes|nullable|min:0|numeric|max:999999',
  823. 'carrier_weight_unit_id_other'=>'sometimes|required_with:carrier_weight_other',
  824. ];
  825. if ($request->type == '专线'){
  826. $rule['origination_city_id']='required|integer';
  827. $rule['destination_city_id']='required|integer';
  828. }
  829. $validator=Validator::make($request->input(),$rule,[
  830. 'required'=>':attribute 为必填项',
  831. 'alpha_num'=>':attribute 应为字母或数字',
  832. 'max'=>':attribute 字符过多或输入值过大',
  833. 'min'=>':attribute 不得为负',
  834. 'numeric'=>':attribute 应为数字',
  835. 'unique'=>':attribute 已存在',
  836. 'required_with'=>':attribute 未填',
  837. 'integer'=>':attribute 必须为数字',
  838. ],[
  839. 'carrier_id'=>'承运商',
  840. 'carrier_bill'=>'承运商单号',
  841. 'fee'=>'运费',
  842. 'other_fee'=>'其他费用',
  843. 'charge'=>'收费',
  844. 'mileage'=>'里程数',
  845. 'amount'=>'计数',
  846. 'amount_unit_id'=>'计数单位',
  847. 'warehouse_weight'=>'仓库计数(抛)',
  848. 'carrier_weight'=>'承运商计数(抛)',
  849. 'pick_up_fee'=>'提货费',
  850. 'destination_city_id'=>'目的市',
  851. 'carrier_weight_unit_id'=>'承运商计数单位',
  852. 'warehouse_weight_unit_id'=>'仓库计数单位',
  853. 'warehouse_weight_other'=>'仓库计数二',
  854. 'carrier_weight_other'=>'承运商计数二',
  855. 'warehouse_weight_unit_id_other'=>'仓库技数单位二',
  856. 'carrier_weight_unit_id_other'=>'承运商计数单位二',
  857. ]);
  858. return $validator;
  859. }
  860. public function addCounty(Request $request){
  861. $rule =[
  862. 'destination_city' => ['required', 'string','unique:cities,name'],
  863. ];
  864. $messages=[
  865. 'destination_city.required'=>'市/县不能为空',
  866. 'destination_city.string'=>'市/县必须是字符串',
  867. 'destination_city.unique'=>'市/县已存在',
  868. ];
  869. $validator = Validator::make($request->all(),$rule,$messages);
  870. if ($validator->fails()) {
  871. return $validator->errors();
  872. }
  873. $city=new City([
  874. 'name'=>$request->input('destination_city'),
  875. 'type'=>3,
  876. ]);
  877. $city->save();
  878. return $city;
  879. }
  880. // 运单删除 软删除
  881. public function destroy(int $id){
  882. if(!GAte::allows('运输管理-删除')){return['success'=>0,'status'=>'没有权限'];}
  883. if(is_null($id)){return ['success'=>'0','status'=>'传入id为空'];}
  884. $result = Waybill::where('id',$id)->delete();
  885. return ['success'=>$result,'status'=>$result];
  886. }
  887. // 回收站
  888. public function recycle(Request $request){
  889. if(!Gate::allows('运输管理-删除')){return redirect('/');}
  890. $user = Auth::user();
  891. $paginate = $request->input('paginate')??50;
  892. $waybills = Waybill::with(['owner','waybillAuditLogs' => function ($query) {
  893. return $query->with('user');
  894. }])->orderBy('deleted_at', 'DESC')->withTrashed()->whereNotNull('deleted_at')->paginate(50);
  895. $total = $waybills->count();
  896. $paginateParams = [];
  897. $paginateParams['paginate'] = $paginate;
  898. return view('waybill.recycle',compact('waybills','total','paginateParams'));
  899. }
  900. // 软删除恢复
  901. public function apiRestoreSelected(Request $request){
  902. DB::enableQueryLog();
  903. if(!Gate::allows('运输管理-删除')){return ['success'=>'false','fail_info'=>'没有权限'];}
  904. $ids = $request->input('ids')??'';
  905. if($ids == ''){return ['success'=>'false','fail_info'=>'没有可恢复对象'];}
  906. $waybills = Waybill::withTrashed()->whereIn('id',$ids)->get();
  907. $result = '';
  908. $waybills->each(function (Waybill $waybill){
  909. $waybill->restore();
  910. });
  911. $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  912. return ['success'=>'true','waybills'=>$waybills];
  913. }
  914. // 修改运费
  915. public function changeFee(Request $request){
  916. if(!Gate::allows('运输管理-运费')){return ['success'=>'false','fail_info'=>'没有权限'];}
  917. $wayBillId = $request->input('id');
  918. $waybillFee = $request->input('fee');
  919. if(is_null($wayBillId) or is_null($waybillFee)){
  920. return ['success'=>'false','fail_info'=>'参数异常'];
  921. }
  922. $result = Waybill::where('id',$wayBillId)->update(['fee'=>$waybillFee]);
  923. $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  924. return ['success'=>$result,'status'=>$result];
  925. }
  926. // 置顶
  927. public function waybillOnTop(Request $request){
  928. $id = $request->input('id');
  929. $detail = $request->input('detail');
  930. if(!Gate::allows('运输管理-置顶')){return ['success'=>'false','fail_info'=>'没有权限'];}
  931. if(is_null($id)){
  932. return ['success'=>'false','fail_info'=>'传参错误'];
  933. }
  934. $wayontop = WaybillOnTop::withTrashed()->where('waybill_id',$id);
  935. $result = '';
  936. if(count($wayontop->get()) == 0){
  937. $wayontop = WaybillOnTop::create(['waybill_id'=>$id,'remark'=>$detail]);
  938. $result = $wayontop->save();
  939. }else{
  940. $wayontop = WaybillOnTop::withTrashed()->where('waybill_id',$id);
  941. $result = WaybillOnTop::withTrashed()->where('waybill_id',$id)->restore();
  942. }
  943. return ['success'=>$result,'status'=>$result];
  944. }
  945. // 取消置顶
  946. public function cancelOnTop(Request $request){
  947. $id = $request->input('id');
  948. if(!Gate::allows('运输管理-置顶')){return ['success'=>'false','fail_info'=>'没有权限'];}
  949. if(is_null($id)){
  950. return ['success'=>'false','fail_info'=>'传参错误'];
  951. }
  952. $result = WaybillOnTop::where('waybill_id',$id)->forceDelete();
  953. return ['success'=>$result,'status'=>$result];
  954. }
  955. // 获取所有运单信息
  956. public function getWaybills(){
  957. $waybills = Waybill::with(['owner','wmsCommodities','waybillAuditLogs' => function ($query) {
  958. return $query->with('user');
  959. }])->selectRaw('waybills.* ,waybill_on_tops.id top_id ,waybill_on_tops.remark,waybill_on_tops.updated_at top_update')
  960. ->leftJoin('waybill_on_tops','waybill_on_tops.waybill_id','=','waybills.id')
  961. ->whereNull('waybill_on_tops.deleted_at')
  962. ->orderBy('waybill_on_tops.updated_at','desc')
  963. ->orderBy('waybills.id','desc');
  964. return $waybills;
  965. }
  966. }