WaybillsController.php 47 KB

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