WaybillsController.php 46 KB

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