WaybillsController.php 54 KB

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