WaybillsController.php 51 KB

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