WaybillsController.php 50 KB

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