WaybillsController.php 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Controllers\Api\thirdPart\flux\WaybillController;
  4. use App\User;
  5. use App\WaybillAuditLog;
  6. use App\WaybillOnTop;
  7. use App\WaybillPriceModel;
  8. use App\Carrier;
  9. use App\CarType;
  10. use App\City;
  11. use App\Exports\Export;
  12. use App\Owner;
  13. use App\Unit;
  14. use App\Waybill;
  15. use App\WaybillPayoff;
  16. use App\WaybillFinancialExcepted;
  17. use App\WaybillFinancialSnapshot;
  18. use Carbon\Carbon;
  19. use Illuminate\Http\Request;
  20. use Illuminate\Support\Facades\Auth;
  21. use Illuminate\Support\Facades\DB;
  22. use Illuminate\Support\Facades\Gate;
  23. use Illuminate\Support\Facades\Validator;
  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. $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 waybillExport($id,Request $request){
  540. if(!Gate::allows('运输管理-查询')){ return '没有权限'; }
  541. if ($id==-1){
  542. $id=[];
  543. $today=Carbon::now()->subDays(15);
  544. ini_set('max_execution_time',2500);
  545. ini_set('memory_limit','1526M');
  546. $waybills=Waybill::select('id');
  547. if ($request->input('waybill_number')){
  548. $waybills =$waybills->where('waybill_number','like','%'.$request->input('waybill_number').'%')->where('created_at','>',$today->format('Y-m-d'));
  549. }
  550. if ($request->input('carrier_bill')){
  551. $waybills=$waybills->where('carrier_bill','like','%'.$request->input('carrier_bill').'%')->where('created_at','>',$today->format('Y-m-d'));
  552. }
  553. if ($request->input('carrier_id')){
  554. $waybills=$waybills->where('carrier_id','=',$request->input('carrier_id'));
  555. }
  556. if ($request->input('owner_id')){
  557. $waybills=$waybills->where('owner_id','=',$request->input('owner_id'));
  558. }
  559. if ($request->input('wms_bill_number')){
  560. $waybills=$waybills->where('wms_bill_number','like','$'.$request->input('wms_bill_number').'%')->where('created_at','>',$today->format('Y-m-d'));
  561. }
  562. if ($request->input('created_at_start')){
  563. $waybills=$waybills->where('created_at','>',$request->input('created_at_start'));
  564. }
  565. if ($request->input('created_at_end')){
  566. $waybills=$waybills->where('created_at','<',$request->input('created_at_end'));
  567. }
  568. if ($request->input('status')){
  569. $waybills=$waybills->where('status',$request->input('status'));
  570. }
  571. $waybills=$waybills->get();
  572. foreach ($waybills as $waybill){
  573. array_push($id,$waybill->id);
  574. }
  575. }else $id = explode( ',',$id);
  576. if (!$id)return ;
  577. $row=[[
  578. 'type'=>'运单类型',
  579. 'owner'=>'货主',
  580. 'source_bill'=>'上游单号',
  581. 'wms_bill_number'=>'wms订单号',
  582. 'waybill_number'=>'运单号',
  583. 'origination'=>'始发地',
  584. 'destination'=>'目的地',
  585. 'carrier'=>'承运商',
  586. 'carrier_bill'=>'承运商单号',
  587. 'warehouse_weight'=>'仓库计数(抛)',
  588. 'carrier_weight'=>'承运商计数(抛)',
  589. 'warehouse_weight_other'=>'仓库计重',
  590. 'carrier_weight_other'=>'承运商计重',
  591. 'fee'=>'运费(元)',
  592. 'pick_up_fee'=>'提货费(元)',
  593. 'other_fee'=>'其他费用(元)',
  594. 'dispatch_remark'=>'调度备注',
  595. 'created_at'=>'创建时间'
  596. ]];
  597. $feeVisible=true;
  598. if(!Gate::allows('运输管理-可见费用项')){
  599. $feeVisible=false;
  600. unset($row[0]['fee'],$row[0]['pick_up_fee'],$row[0]['other_fee'],$row[0]['collect_fee']);
  601. }
  602. $list=[];
  603. for ($i=0; $i<count($id);$i++){
  604. $waybill=Waybill::with(['owner', 'waybillAuditLogs' => function ($query) {
  605. return $query->with('user');
  606. }])->find($id[$i]);
  607. foreach ($waybill->waybillAuditLogs as $waybillAuditLog){
  608. if ($waybillAuditLog->audit_stage=="运单阶段"){
  609. $waybillAuditor=$waybillAuditLog->user->name;
  610. }else{
  611. $waybillAuditor='';
  612. }
  613. if ($waybillAuditLog->audit_stage=="调度阶段"){
  614. $dispatchAuditor=$waybillAuditLog->user->name;
  615. }else{
  616. $dispatchAuditor='';
  617. }
  618. };
  619. $w=[
  620. 'type'=>isset($waybill->type)?$waybill->type:'',
  621. 'waybill_number'=>isset($waybill->waybill_number)?$waybill->waybill_number:'',
  622. 'owner'=>isset($waybill->owner->name)?$waybill->owner->name:'',
  623. 'source_bill'=>isset($waybill->source_bill)?$waybill->source_bill:'',
  624. 'wms_bill_number'=>isset($waybill->wms_bill_number)?$waybill->wms_bill_number:'',
  625. 'origination'=>isset($waybill->origination)?$waybill->origination:'',
  626. 'destination'=>isset($waybill->destination)?$waybill->destination:'',
  627. 'recipient'=>isset($waybill->recipient)?$waybill->recipient:'',
  628. 'recipient_mobile'=>isset($waybill->recipient_mobile)?$waybill->recipient_mobile:'',
  629. //'charge'=>isset($waybill->charge)?$waybill->charge:'',
  630. //'ordering_remark'=>isset($waybill->ordering_remark)?$waybill->ordering_remark:'',
  631. 'carrier'=>isset($waybill->carrier_name)?$waybill->carrier_name:'',
  632. 'carrier_bill'=>isset($waybill->carrier_bill)?$waybill->carrier_bill:'',
  633. //'origination_city'=>isset($waybill->origination_city_name)?$waybill->origination_city_name:'',
  634. //'destination_city'=>isset($waybill->destination_city_name)?$waybill->destination_city_name:'',
  635. 'warehouse_weight'=>isset($waybill->warehouse_weight)?$waybill->warehouse_weight.' '.(isset($waybill->warehouse_weight_unit_name)?$waybill->warehouse_weight_unit_name:''):'',
  636. '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:''):'',
  637. 'carrier_weight'=>isset($waybill->carrier_weight)?$waybill->carrier_weight.' '.(isset($waybill->carrier_weight_unit_name)?$waybill->carrier_weight_unit_name:''):'',
  638. '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:''):'',
  639. //'carType'=>isset($waybill->carType->name)?$waybill->carType->name.($waybill->carType->length.'米'):'',
  640. //'car_owner_info'=>isset($waybill->car_owner_info)?$waybill->car_owner_info:'',
  641. 'fee'=>isset($waybill->fee)?$waybill->fee:'',
  642. 'pick_up_fee'=>isset($waybill->pick_up_fee)?$waybill->pick_up_fee:'',
  643. 'other_fee'=>isset($waybill->other_fee)?$waybill->other_fee:'',
  644. //'collect_fee'=>isset($waybill->collect_fee)?$waybill->collect_fee:'',
  645. 'dispatch_remark'=>isset($waybill->dispatch_remark)?$waybill->dispatch_remark:'',
  646. //'waybillAuditor'=>isset($waybillAuditor)?$waybillAuditor:'',
  647. //'dispatchAuditor'=>isset($dispatchAuditor)?$dispatchAuditor:'',
  648. 'created_at'=>isset($waybill->created_at)?$waybill->created_at:''
  649. ];
  650. if(!$feeVisible){
  651. unset($w['fee'],$w['pick_up_fee'],$w['other_fee'],$w['collect_fee']);
  652. }
  653. $list[$i]=$w;
  654. }
  655. $this->log(__METHOD__,__FUNCTION__,json_encode($waybill),Auth::user()['id']);
  656. return Excel::download(new Export($row,$list), date('Y:m:d ') . '运单列表.xlsx');
  657. }
  658. public function deliveringExport($waybills){
  659. ini_set('max_execution_time',2500);
  660. ini_set('memory_limit','1526M');
  661. $row=[[
  662. 'created_at'=>"日期",
  663. 'carrier_name'=>"承运商",
  664. 'waybill_number'=>"宝时运单号",
  665. 'origination'=>"提货仓",
  666. 'owner_name'=>"货主",
  667. 'warehouse_weight_other'=>"预估重量",
  668. 'warehouse_weight'=>"预估体积",
  669. 'status'=>"状态",
  670. 'carrier_bill'=>"专线运单号",
  671. 'inquire_tel'=>"查件电话",
  672. 'amount'=>"件数",
  673. 'carrier_weight_other'=>"重量",
  674. 'carrier_weight'=>"体积",
  675. ]];
  676. $list=[];
  677. foreach ($waybills as $waybill){
  678. $w=[
  679. 'created_at'=>$waybill->created_at,
  680. 'carrier_name'=>$waybill->carrier_name,
  681. 'waybill_number'=>$waybill->waybill_number,
  682. 'origination'=>$waybill->origination,
  683. 'owner_name'=>$waybill->owner_name,
  684. 'warehouse_weight_other'=>$waybill->warehouse_weight_other,
  685. 'warehouse_weight'=>$waybill->warehouse_weight,
  686. 'status'=>$waybill->status=="已完结"?"已完成":$waybill->carrier_bill?"已提交":"待提交",
  687. 'carrier_bill'=>$waybill->carrier_bill,
  688. 'inquire_tel'=>$waybill->inquire_tel,
  689. 'amount'=>$waybill->amount,
  690. 'carrier_weight_other'=>$waybill->carrier_weight_other,
  691. 'carrier_weight'=>$waybill->carrier_weight,
  692. ];
  693. array_push($list,$w);
  694. }
  695. return Excel::download(new Export($row,$list), date('Y:m:d ') . '发运列表.xlsx');
  696. }
  697. //发运
  698. public function delivering(Request $request){
  699. if (!Auth::user())return view('exception.404',['error'=>'登录已过期']);
  700. if (Auth::user()->isSuperAdmin())$waybills=Waybill::orderBy('id','DESC');
  701. else{
  702. $carriersUsers=DB::table('carrier_user')->where('user_id',Auth::id())->get();
  703. $carrierIds=array_column($carriersUsers->toArray(),'carrier_id');
  704. $waybills=Waybill::orderBy('id','DESC')->whereIn("carrier_id",$carrierIds);
  705. }
  706. if ($request->input('exportType')){
  707. return $this->conditionQuery($request,$waybills);
  708. }
  709. $waybills=$this->conditionQuery($request,$waybills);
  710. return view('waybill.delivering',compact('waybills'));
  711. }
  712. //承运商提交
  713. public function storeCarrierBill(Request $request){
  714. $errors=Validator::make($request->input(),[
  715. 'id'=>'required|integer',
  716. 'carrier_bill'=>'required',
  717. 'inquire_tel'=>'nullable',
  718. 'amount'=>'nullable|integer',
  719. 'carrier_weight'=>'required_without:carrier_weight_other|nullable|numeric',
  720. 'carrier_weight_other'=>'required_without:carrier_weight|nullable|numeric',
  721. ],[
  722. 'required'=>':attribute 为必填项',
  723. 'integer'=>':attribute 应为整数',
  724. 'numeric'=>':attribute 应为数字',
  725. 'required_with'=>':attribute 重量与体积至少存在一项',
  726. ],[
  727. 'carrier_bill'=>'专线运单号',
  728. 'inquire_tel'=>'查件电话',
  729. 'amount'=>'件数',
  730. 'carrier_weight'=>'体积',
  731. 'carrier_weight_other'=>'重量',
  732. ])->errors();
  733. if (count($errors)>0)return ["errors"=>$errors];
  734. $waybill=Waybill::find($request->input('id'));
  735. if (!$waybill)return ["error"=>"未找到该运单!"];
  736. $waybill->fill($request->input());
  737. $waybill->update();
  738. return $waybill;
  739. }
  740. protected function validatorWaybill(Request $request,$id){
  741. if ($id){$wms_bill_number=$id;};
  742. $validator=Validator::make($request->input(),[
  743. 'owner_id'=>'required',
  744. 'wms_bill_number'=>['nullable','max:50',isset($wms_bill_number)?"unique:waybills,wms_bill_number,$wms_bill_number":'unique:waybills,wms_bill_number'],
  745. 'origination'=>'required|max:255',
  746. 'destination'=>'required|max:255',
  747. 'recipient'=>'required|max:50',
  748. 'recipient_mobile'=>['required','regex:/^(\d{7,11})|(1[3|4|5|7|8][0-9]\d{4,8})$/'],
  749. 'charge'=>'nullable|min:0|max:999999|numeric',
  750. 'collect_fee'=>'nullable|min:0|numeric',
  751. ],[
  752. 'required'=>':attribute 为必填项',
  753. 'alpha_num'=>':attribute 应为字母或数字',
  754. 'max'=>':attribute 字符过多或输入值过大',
  755. 'regex'=>':attribute 输入有误',
  756. 'integer'=>':attribute 应为整数',
  757. 'min'=>':attribute 不得为负',
  758. 'numeric'=>':attribute 应为数字',
  759. 'unique'=>':attribute 已存在',
  760. ],[
  761. 'owner_id'=>'货主',
  762. 'wms_bill_number'=>'WMS单号',
  763. 'origination'=>'始发地',
  764. 'destination'=>'目的地',
  765. 'recipient'=>'收件人',
  766. 'recipient_mobile'=>'收件人电话',
  767. 'charge'=>'收费',
  768. 'collect_fee'=>'到付金额',
  769. ]);
  770. return $validator;
  771. }
  772. protected function validatorWaybillDispatch(Request $request,$id){
  773. if ($request->input('type')=='直发车'){
  774. $validator=Validator::make($request->input(),[
  775. 'carrier_bill'=>"nullable|max:50|unique:waybills,carrier_bill,$id",
  776. 'fee'=>'required|min:0|numeric|max:999999',
  777. 'other_fee'=>'nullable|min:0|numeric|max:999999',
  778. 'charge'=>'nullable|min:0|numeric|max:999999',
  779. 'mileage'=>'nullable|numeric',
  780. 'amount'=>'nullable|numeric',
  781. ],[
  782. 'required'=>':attribute 为必填项',
  783. 'alpha_num'=>':attribute 应为字母或数字',
  784. 'max'=>':attribute 字符过多或输入值过大',
  785. 'min'=>':attribute 不得为负',
  786. 'numeric'=>':attribute 应为数字',
  787. 'unique'=>':attribute 已存在',
  788. ],[
  789. 'carrier_bill'=>'承运商单号',
  790. 'fee'=>'运费',
  791. 'other_fee'=>'其他费用',
  792. 'charge'=>'收费',
  793. 'mileage'=>'里程数',
  794. 'amount'=>'计数',
  795. ]);
  796. return $validator;
  797. }else if ($request->input('type')=='专线'){
  798. return $this->validatorWaybillZX($request,$id);
  799. }else{
  800. return false;
  801. }
  802. }
  803. protected function validatorWaybillZX(Request $request,$id){
  804. $validator=Validator::make($request->input(),[
  805. 'carrier_bill'=>"nullable|max:50|unique:waybills,carrier_bill,$id",
  806. 'pick_up_fee'=>'nullable|min:0|numeric|max:999999',
  807. 'other_fee'=>'nullable|min:0|numeric|max:999999',
  808. 'carrier_id'=>'required|integer',
  809. 'destination_city_id'=>'required|integer',
  810. 'warehouse_weight'=>'nullable|min:0|numeric|max:999999',
  811. 'warehouse_weight_unit_id'=>'required_with:warehouse_weight|nullable|integer',
  812. 'warehouse_weight_other'=>'nullable|min:0|numeric|max:999999',
  813. 'warehouse_weight_unit_id_other'=>'required_with:warehouse_weight_other|nullable|integer',
  814. 'charge'=>'nullable|min:0|numeric|max:999999',
  815. 'carrier_weight'=>'nullable|min:0|numeric|max:999999',
  816. 'carrier_weight_unit_id'=>'required_with:carrier_weight',
  817. 'carrier_weight_other'=>'nullable|min:0|numeric|max:999999',
  818. 'carrier_weight_unit_id_other'=>'required_with:carrier_weight_other',
  819. ],[
  820. 'required'=>':attribute 为必填项',
  821. 'alpha_num'=>':attribute 应为字母或数字',
  822. 'max'=>':attribute 字符过多或输入值过大',
  823. 'min'=>':attribute 不得为负',
  824. 'numeric'=>':attribute 应为数字',
  825. 'unique'=>':attribute 已存在',
  826. 'required_with'=>':attribute 未填',
  827. 'integer'=>':attribute 必须为数字',
  828. ],[
  829. 'carrier_bill'=>'承运商单号',
  830. 'warehouse_weight'=>'仓库计数(抛)',
  831. 'carrier_weight'=>'承运商计数(抛)',
  832. 'pick_up_fee'=>'提货费',
  833. 'other_fee'=>'其他费用',
  834. 'carrier_id'=>'承运商',
  835. 'destination_city_id'=>'目的市',
  836. 'carrier_weight_unit_id'=>'承运商计数单位',
  837. 'charge'=>'收费',
  838. 'warehouse_weight_unit_id'=>'仓库计数单位',
  839. 'warehouse_weight_other'=>'仓库计数二',
  840. 'carrier_weight_other'=>'承运商计数二',
  841. 'warehouse_weight_unit_id_other'=>'仓库技数单位二',
  842. 'carrier_weight_unit_id_other'=>'承运商计数单位二',
  843. ]);
  844. return $validator;
  845. }
  846. public function addCounty(Request $request){
  847. $rule =[
  848. 'destination_city' => ['required', 'string','unique:cities,name'],
  849. ];
  850. $messages=[
  851. 'destination_city.required'=>'市/县不能为空',
  852. 'destination_city.string'=>'市/县必须是字符串',
  853. 'destination_city.unique'=>'市/县已存在',
  854. ];
  855. $validator = Validator::make($request->all(),$rule,$messages);
  856. if ($validator->fails()) {
  857. return $validator->errors();
  858. }
  859. $city=new City([
  860. 'name'=>$request->input('destination_city'),
  861. 'type'=>3,
  862. ]);
  863. $city->save();
  864. return $city;
  865. }
  866. // 运单删除 软删除
  867. public function destroy(int $id){
  868. if(!GAte::allows('运输管理-删除')){return['success'=>0,'status'=>'没有权限'];}
  869. if(is_null($id)){return ['success'=>'0','status'=>'传入id为空'];}
  870. $result = Waybill::where('id',$id)->delete();
  871. return ['success'=>$result,'status'=>$result];
  872. }
  873. // 回收站
  874. public function recycle(Request $request){
  875. if(!Gate::allows('运输管理-删除')){return redirect('/');}
  876. $user = Auth::user();
  877. $paginate = $request->input('paginate')??50;
  878. $waybills = Waybill::with(['owner','waybillAuditLogs' => function ($query) {
  879. return $query->with('user');
  880. }])->orderBy('deleted_at', 'DESC')->withTrashed()->whereNotNull('deleted_at')->paginate(50);
  881. $total = $waybills->count();
  882. $paginateParams = [];
  883. $paginateParams['paginate'] = $paginate;
  884. return view('waybill.recycle',compact('waybills','total','paginateParams'));
  885. }
  886. // 软删除恢复
  887. public function apiRestoreSelected(Request $request){
  888. DB::enableQueryLog();
  889. if(!Gate::allows('运输管理-删除')){return ['success'=>'false','fail_info'=>'没有权限'];}
  890. $ids = $request->input('ids')??'';
  891. if($ids == ''){return ['success'=>'false','fail_info'=>'没有可恢复对象'];}
  892. $waybills = Waybill::withTrashed()->whereIn('id',$ids)->get();
  893. $result = '';
  894. $waybills->each(function (Waybill $waybill){
  895. $waybill->restore();
  896. });
  897. $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  898. return ['success'=>$result,'waybills'=>$waybills];
  899. }
  900. // 修改运费
  901. public function changeFee(Request $request){
  902. if(!Gate::allows('运输管理-运费')){return ['success'=>'false','fail_info'=>'没有权限'];}
  903. $wayBillId = $request->input('id');
  904. $waybillFee = $request->input('fee');
  905. if(is_null($wayBillId) or is_null($waybillFee)){
  906. return ['success'=>'false','fail_info'=>'参数异常'];
  907. }
  908. $result = Waybill::where('id',$wayBillId)->update(['fee'=>$waybillFee]);
  909. $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  910. return ['success'=>$result,'status'=>$result];
  911. }
  912. // 置顶
  913. public function waybillOnTop(Request $request){
  914. $id = $request->input('id');
  915. $detail = $request->input('detail');
  916. if(!Gate::allows('运输管理-置顶')){return ['success'=>'false','fail_info'=>'没有权限'];}
  917. if(is_null($id)){
  918. return ['success'=>'false','fail_info'=>'传参错误'];
  919. }
  920. $wayontop = WaybillOnTop::withTrashed()->where('waybill_id',$id);
  921. $result = '';
  922. if(count($wayontop->get()) == 0){
  923. $wayontop = WaybillOnTop::create(['waybill_id'=>$id,'remark'=>$detail]);
  924. $result = $wayontop->save();
  925. }else{
  926. $wayontop = WaybillOnTop::withTrashed()->where('waybill_id',$id);
  927. $result = WaybillOnTop::withTrashed()->where('waybill_id',$id)->restore();
  928. }
  929. return ['success'=>$result,'status'=>$result];
  930. }
  931. // 取消置顶
  932. public function cancelOnTop(Request $request){
  933. $id = $request->input('id');
  934. if(!Gate::allows('运输管理-置顶')){return ['success'=>'false','fail_info'=>'没有权限'];}
  935. if(is_null($id)){
  936. return ['success'=>'false','fail_info'=>'传参错误'];
  937. }
  938. $result = WaybillOnTop::where('waybill_id',$id)->forceDelete();
  939. return ['success'=>$result,'status'=>$result];
  940. }
  941. // 获取所有运单信息
  942. public function getWaybills(){
  943. $waybills = Waybill::with(['owner','wmsCommodities','waybillAuditLogs' => function ($query) {
  944. return $query->with('user');
  945. }])->selectRaw('waybills.* ,waybill_on_tops.id top_id ,waybill_on_tops.remark,waybill_on_tops.updated_at top_update')
  946. ->leftJoin('waybill_on_tops','waybill_on_tops.waybill_id','=','waybills.id')
  947. ->whereNull('waybill_on_tops.deleted_at')
  948. ->orderBy('waybill_on_tops.updated_at','desc')
  949. ->orderBy('waybills.id','desc');
  950. return $waybills;
  951. }
  952. }