WaybillsController.php 55 KB

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