WaybillsController.php 56 KB

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