WaybillsController.php 54 KB

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