WaybillsController.php 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\WaybillAuditLog;
  4. use App\WaybillPriceModel;
  5. use App\Carrier;
  6. use App\CarType;
  7. use App\City;
  8. use App\Exports\WaybillExport;
  9. use App\Owner;
  10. use App\Unit;
  11. use App\Waybill;
  12. use App\WaybillPayoff;
  13. use App\WaybillFinancialExcepted;
  14. use App\WaybillFinancialSnapshot;
  15. use Carbon\Carbon;
  16. use Illuminate\Http\Request;
  17. use Illuminate\Support\Facades\Auth;
  18. use Illuminate\Support\Facades\Gate;
  19. use Illuminate\Support\Facades\Validator;
  20. use Maatwebsite\Excel\Facades\Excel;
  21. use Ramsey\Uuid\Uuid;
  22. class WaybillsController extends Controller
  23. {
  24. public function index(Request $request)
  25. {
  26. if(!Gate::allows('运输管理-查询')){ return redirect(url('/')); }
  27. $data=$request->input();
  28. if ($data != null ) {
  29. $today=Carbon::now()->subDays(15);
  30. $waybills=Waybill::with(['owner', 'waybillAuditLogs' => function ($query) {
  31. return $query->with('user');
  32. }])->orderBy('id','DESC');
  33. if ($request->input('waybill_number')){
  34. $waybills =$waybills->where('waybill_number','like','%'.$request->input('waybill_number').'%')->where('created_at','>',$today->format('Y-m-d'));
  35. }
  36. if ($request->input('carrier_bill')){
  37. $waybills=$waybills->where('carrier_bill','like','%'.$request->input('carrier_bill').'%')->where('created_at','>',$today->format('Y-m-d'));
  38. }
  39. if ($request->input('carrier_id')){
  40. $waybills=$waybills->where('carrier_id','=',$request->input('carrier_id'));
  41. }
  42. if ($request->input('owner_id')){
  43. $waybills=$waybills->where('owner_id','=',$request->input('owner_id'));
  44. }
  45. if ($request->input('wms_bill_number')){
  46. $waybills=$waybills->where('wms_bill_number','like','%'.$request->input('wms_bill_number').'%')->where('created_at','>',$today->format('Y-m-d'));
  47. }
  48. if ($request->input('origination')){
  49. $waybills=$waybills->where('origination','like','%'.$request->input('origination').'%')->where('created_at','>',$today->format('Y-m-d'));
  50. }
  51. if ($request->input('destination')){
  52. $waybills=$waybills->where('destination','like','%'.$request->input('destination').'%')->where('created_at','>',$today->format('Y-m-d'));
  53. }
  54. if ($request->input('created_at_start')){
  55. $waybills=$waybills->where('created_at','>',$request->input('created_at_start'));
  56. }
  57. if ($request->input('created_at_end')){
  58. $waybills=$waybills->where('created_at','<',$request->input('created_at_end'));
  59. }
  60. if ($request->input('status')){
  61. $waybills=$waybills->where('status',$request->input('status'));
  62. }
  63. $waybills=$waybills->paginate($request->input('paginate')?$request->input('paginate'):50);
  64. if (!$waybills&&$request->input('waybill_number')){
  65. $waybills=Waybill::with(['owner', 'waybillAuditLogs' => function ($query) {
  66. return $query->with('user');
  67. }])->orderBy('id','DESC')->where('type','专线')->where('waybill_number',$request->input('waybill_number'))
  68. ->paginate($request->input('paginate')?$request->input('paginate'):50);
  69. }
  70. $carries = Carrier::get();
  71. $owners = Owner::get();
  72. return view('waybill.index', ['waybills' => $waybills, 'carriers' => $carries, 'owners' => $owners,'filterData'=>$data,'uriType'=>'']);
  73. } else {
  74. $waybills = Waybill::with(['owner', 'waybillAuditLogs' => function ($query) {
  75. return $query->with('user');
  76. }])->orderBy('id', 'DESC')->paginate(50);
  77. $carries = Carrier::get();
  78. $owners = Owner::get();
  79. return view('waybill.index', ['waybills' => $waybills, 'carriers' => $carries, 'owners' => $owners,'filterData'=>$data,'uriType'=>'']);
  80. }
  81. }
  82. public function indexZF(Request $request){
  83. if(!Gate::allows('运输管理-查询')){ return redirect(url('/')); }
  84. $data=$request->input();
  85. if ($data != null ) {
  86. $today=Carbon::now()->subDays(15);
  87. $waybills=Waybill::with(['owner', 'waybillAuditLogs' => function ($query) {
  88. return $query->with('user');
  89. }])->orderBy('id','DESC')->where('type','直发车');
  90. if ($request->input('waybill_number')){
  91. $waybills =$waybills->where('waybill_number','like','%'.$request->input('waybill_number').'%')->where('created_at','>',$today->format('Y-m-d'));
  92. }
  93. if ($request->input('carrier_bill')){
  94. $waybills=$waybills->where('carrier_bill','like','%'.$request->input('carrier_bill').'%')->where('created_at','>',$today->format('Y-m-d'));
  95. }
  96. if ($request->input('carrier_id')){
  97. $waybills=$waybills->where('carrier_id','=',$request->input('carrier_id'));
  98. }
  99. if ($request->input('owner_id')){
  100. $waybills=$waybills->where('owner_id','=',$request->input('owner_id'));
  101. }
  102. if ($request->input('wms_bill_number')){
  103. $waybills=$waybills->where('wms_bill_number','like','$'.$request->input('wms_bill_number').'%')->where('created_at','>',$today->format('Y-m-d'));
  104. }
  105. if ($request->input('created_at_start')){
  106. $waybills=$waybills->where('created_at','>',$request->input('created_at_start'));
  107. }
  108. if ($request->input('created_at_end')){
  109. $waybills=$waybills->where('created_at','<',$request->input('created_at_end'));
  110. }
  111. if ($request->input('status')){
  112. $waybills=$waybills->where('status',$request->input('status'));
  113. }
  114. $waybills=$waybills->paginate($request->input('paginate')?$request->input('paginate'):50);
  115. if (!$waybills&&$request->input('waybill_number')){
  116. $waybills=Waybill::with(['owner', 'waybillAuditLogs' => function ($query) {
  117. return $query->with('user');
  118. }])->orderBy('id','DESC')->where('type','专线')->where('waybill_number',$request->input('waybill_number'))
  119. ->paginate($request->input('paginate')?$request->input('paginate'):50);
  120. }
  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 = Waybill::with(['owner', 'waybillAuditLogs' => function ($query) {
  126. return $query->with('user');
  127. }])->where('type','直发车')->orderBy('id', 'DESC')->paginate(50);
  128. $carries = Carrier::get();
  129. $owners = Owner::get();
  130. return view('waybill.index', ['waybills' => $waybills, 'carriers' => $carries, 'owners' => $owners,'filterData'=>$data,'uriType'=>'ZF']);
  131. }
  132. }
  133. public function indexZX(Request $request){
  134. if(!Gate::allows('运输管理-查询')){ return redirect(url('/')); }
  135. $data=$request->input();
  136. if ($data != null ) {
  137. $today=Carbon::now()->subDays(15);
  138. $waybills=Waybill::with(['owner', 'waybillAuditLogs' => function ($query) {
  139. return $query->with('user');
  140. }])->orderBy('id','DESC')->where('type','专线');
  141. if ($request->input('waybill_number')){
  142. $waybills =$waybills->where('waybill_number','like','%'.$request->input('waybill_number').'%')->where('created_at','>',$today->format('Y-m-d'));
  143. }
  144. if ($request->input('carrier_bill')){
  145. $waybills=$waybills->where('carrier_bill','like','%'.$request->input('carrier_bill').'%')->where('created_at','>',$today->format('Y-m-d'));
  146. }
  147. if ($request->input('carrier_id')){
  148. $waybills=$waybills->where('carrier_id','=',$request->input('carrier_id'));
  149. }
  150. if ($request->input('owner_id')){
  151. $waybills=$waybills->where('owner_id','=',$request->input('owner_id'));
  152. }
  153. if ($request->input('wms_bill_number')){
  154. $waybills=$waybills->where('wms_bill_number','like','$'.$request->input('wms_bill_number').'%')->where('created_at','>',$today->format('Y-m-d'));
  155. }
  156. if ($request->input('created_at_start')){
  157. $waybills=$waybills->where('created_at','>',$request->input('created_at_start'));
  158. }
  159. if ($request->input('created_at_end')){
  160. $waybills=$waybills->where('created_at','<',$request->input('created_at_end'));
  161. }
  162. if ($request->input('status')){
  163. $waybills=$waybills->where('status',$request->input('status'));
  164. }
  165. $waybills=$waybills->paginate($request->input('paginate')?$request->input('paginate'):50);
  166. if (!$waybills&&$request->input('waybill_number')){
  167. $waybills=Waybill::with(['owner', 'waybillAuditLogs' => function ($query) {
  168. return $query->with('user');
  169. }])->orderBy('id','DESC')->where('type','专线')->where('waybill_number',$request->input('waybill_number'))
  170. ->paginate($request->input('paginate')?$request->input('paginate'):50);
  171. }
  172. $carries = Carrier::get();
  173. $owners = Owner::get();
  174. return view('waybill.index', ['waybills' => $waybills, 'carriers' => $carries, 'owners' => $owners,'filterData'=>$data,'uriType'=>'ZX']);
  175. } else {
  176. $waybills = Waybill::with(['owner', 'waybillAuditLogs' => function ($query) {
  177. return $query->with('user');
  178. }])->where('type','专线')->orderBy('id', 'DESC')->paginate(50);
  179. $carries = Carrier::get();
  180. $owners = Owner::get();
  181. return view('waybill.index', ['waybills' => $waybills, 'carriers' => $carries, 'owners' => $owners,'filterData'=>$data,'uriType'=>'ZX']);
  182. }
  183. }
  184. public function create()
  185. {
  186. if(!Gate::allows('运输管理-录入')){ return redirect(url('/')); }
  187. $owners=Owner::get();
  188. return view('waybill.create',['owners'=>$owners]);
  189. }
  190. public function createZF()
  191. {
  192. if(!Gate::allows('运输管理-录入')){ return redirect(url('/')); }
  193. $owners=Owner::get();
  194. return view('waybill.create',['owners'=>$owners,'type'=>'直发车']);
  195. }
  196. public function createZX()
  197. {
  198. if(!Gate::allows('运输管理-录入')){ return redirect(url('/')); }
  199. $owners=Owner::get();
  200. return view('waybill.create',['owners'=>$owners,'type'=>'专线']);
  201. }
  202. public function store(Request $request)
  203. {
  204. if(!Gate::allows('运输管理-录入')){ return redirect(url('/')); }
  205. $id=false;
  206. $this->validatorWaybill($request,$id)->validate();
  207. $data=$request->input();
  208. $waybill=new Waybill([
  209. 'type'=>$data['type'],
  210. 'status'=>'未审核',
  211. 'waybill_number'=>Uuid::uuid1(),
  212. 'owner_id'=>$data['owner_id'],
  213. 'wms_bill_number'=>$data['wms_bill_number'],
  214. 'origination'=>$data['origination'],
  215. 'destination'=>$data['destination'],
  216. 'recipient'=>$data['recipient'],
  217. 'recipient_mobile'=>$data['recipient_mobile'],
  218. 'charge'=>$data['charge'],
  219. 'collect_fee'=>isset($data['collect_fee'])?$data['collect_fee']:0,
  220. 'ordering_remark'=>$data['ordering_remark']
  221. ]);
  222. $waybill->save();
  223. $number_id=$waybill->id;
  224. if ($data['type']=='直发车'){
  225. $waybill_number='BSZF'.date ("ymd").str_pad($number_id>99999?$number_id%99999:$number_id,4,"0",STR_PAD_LEFT);
  226. $waybill->waybill_number=$waybill_number;
  227. $waybill->update();
  228. }else{
  229. $waybill_number='BSZX'.date ("ymd").str_pad($number_id>99999?$number_id%99999:$number_id,4,"0",STR_PAD_LEFT);
  230. $waybill->waybill_number=$waybill_number;
  231. $waybill->update();
  232. }
  233. $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  234. return redirect('waybill')->with('successTip','新运单“'.$waybill_number.'”录入成功');
  235. }
  236. public function edit($id)
  237. {
  238. if(!Gate::allows('运输管理-编辑')){ return redirect(url('/')); }
  239. $waybill=Waybill::find($id);
  240. $carriers=Carrier::get();
  241. $cities=City::get();
  242. $units=Unit::get();
  243. $carTypes=CarType::get();
  244. return view('waybill/edit',['waybill'=>$waybill,'carriers'=>$carriers,'cities'=>$cities,'units'=>$units,'carTypes'=>$carTypes]);
  245. }
  246. public function update(Request $request, $id)
  247. {
  248. if(!Gate::allows('运输管理-调度')){ return redirect(url('/')); }
  249. $waybill=Waybill::find($id);
  250. $this->validatorWaybillDispatch($request,$id)->validate();
  251. $data=$request->input();
  252. $waybill->fill($data);
  253. if ($waybill->save()){
  254. if ($waybill->type=="直发车"){
  255. if ($waybill->charge)$total_receivable=($waybill->charge);
  256. elseif ($waybill->collect_fee)$total_receivable=($waybill->collect_fee);
  257. $total_expense=($waybill->fee)+($waybill->other_fee)-($waybill->collect_fee);
  258. }else if ($waybill->type=="专线"){
  259. $waybillPriceModel_id=$request->input('waybillPriceModel');
  260. if ($waybillPriceModel_id){
  261. $carrier_weight=$request->input('carrier_weight');
  262. $waybillPriceModel=WaybillPriceModel::find($waybillPriceModel_id);
  263. $carrier=Carrier::find($waybill->carrier_id);
  264. if ($carrier_weight<$waybillPriceModel->initial_weight){
  265. $fee=(($waybillPriceModel->unit_price)*($waybillPriceModel->initial_weight))+$carrier->delivery_fee;
  266. }else{
  267. $fee=(($waybillPriceModel->unit_price)*$carrier_weight)+$carrier->delivery_fee;
  268. }
  269. if ($waybillPriceModel->base_fee&&$fee<$waybillPriceModel->base_fee){
  270. $fee=$waybillPriceModel->base_fee;
  271. }
  272. $waybill->fee=$fee;
  273. $waybill->waybill_price_model_id=$waybillPriceModel_id;
  274. }
  275. $waybill->save();
  276. if ($waybill->charge)$total_receivable=($waybill->charge);
  277. elseif ($waybill->collect_fee)$total_receivable=($waybill->collect_fee);
  278. $total_expense=($waybill->pick_up_fee)+($waybill->other_fee)+($waybill->fee);
  279. }
  280. if ($total_receivable&&$total_receivable>0){
  281. $waybillPayoff=WaybillPayoff::where('waybill_id','=',$id)->first();
  282. if ($waybillPayoff){
  283. $waybillPayoff->waybill_id=$id;
  284. $waybillPayoff->total_expense=$total_expense;
  285. $waybillPayoff->total_receivable=$total_receivable;
  286. $waybillPayoff->gross_margin=$total_receivable-$total_expense;
  287. $waybillPayoff->gross_profit_rate=(($total_receivable-$total_expense)/$total_receivable);
  288. $waybillPayoff->save();
  289. }else{
  290. WaybillPayoff::create([
  291. 'waybill_id'=>$id,
  292. 'total_expense'=>$total_expense,
  293. 'total_receivable'=>$total_receivable,
  294. 'gross_margin'=>$total_receivable-$total_expense,
  295. 'gross_profit_rate'=>(($total_receivable-$total_expense)/$total_receivable),
  296. ]);
  297. };
  298. }
  299. $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  300. return redirect('waybill')->with('successTip','运单“'.$waybill->waybill_number.'”调度成功');
  301. }
  302. }
  303. public function checkWaybillPriceModel($carrier_id,$destination_city_id,$carrier_weight,$carrier_weight_unit_id){
  304. //确保承运商计数与计数单位为一个数组且长度2
  305. if(!$carrier_id)return false;
  306. if(!$destination_city_id)return false;
  307. if(!$carrier_weight)return false;
  308. if(!$carrier_weight_unit_id)return false;
  309. //多个计数标准,计算价格,取最贵
  310. if ($carrier_weight[0]&&$carrier_weight[1]&&$carrier_weight_unit_id[0]&&$carrier_weight_unit_id[1]){
  311. //城市价格区间不为空
  312. $waybillPriceModelOne=WaybillPriceModel::where('carrier_id',$carrier_id)->where('city_id',$destination_city_id)
  313. ->where('range_min','<',$carrier_weight[0])->where('range_max','>=',$carrier_weight[0])
  314. ->where('unit_id',$carrier_weight_unit_id[0])->first();
  315. $waybillPriceModelTwo=WaybillPriceModel::where('carrier_id',$carrier_id)->where('city_id',$destination_city_id)
  316. ->where('range_min','<',$carrier_weight[1])->where('range_max','>=',$carrier_weight[1])
  317. ->where('unit_id',$carrier_weight_unit_id[1])->first();
  318. if ($waybillPriceModelOne&&$waybillPriceModelTwo){
  319. if ($waybillPriceModelOne->unit_price*$carrier_weight[0]>=$waybillPriceModelTwo->unit_price*$carrier_weight[1]){
  320. return $waybillPriceModelOne->id;
  321. }else{
  322. return $waybillPriceModelTwo->id;
  323. }
  324. }
  325. if ($waybillPriceModelOne)return $waybillPriceModelOne->id;
  326. if ($waybillPriceModelTwo)return $waybillPriceModelTwo->id;
  327. //价格区间为空
  328. $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();
  329. $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();
  330. if ($waybillPriceModelRangeOne&&$waybillPriceModelRangeTwo){
  331. if ($waybillPriceModelRangeOne->unit_price*$carrier_weight[0]>=$waybillPriceModelRangeTwo->unit_price*$carrier_weight[1]){
  332. return $waybillPriceModelRangeOne->id;
  333. }else{
  334. return $waybillPriceModelRangeTwo->id;
  335. }
  336. }
  337. if ($waybillPriceModelRangeOne)return $waybillPriceModelRangeOne->id;
  338. if ($waybillPriceModelRangeTwo)return $waybillPriceModelRangeTwo->id;
  339. //城市为空
  340. $city=City::where('id',$destination_city_id)->select('province_id')->first();
  341. $waybillPriceModelProvinceOne=WaybillPriceModel::whereRaw('carrier_id = ? AND province_id = ? AND unit_id = ? AND range_max >= ? AND range_min < ? AND city_id IS NULL',
  342. [$carrier_id,$city->province_id,$carrier_weight_unit_id[0],$carrier_weight[0],$carrier_weight[0]])->first();
  343. $waybillPriceModelProvinceTwo=WaybillPriceModel::whereRaw('carrier_id = ? AND province_id = ? AND unit_id = ? AND range_max >= ? AND range_min < ? AND city_id IS NULL',
  344. [$carrier_id,$city->province_id,$carrier_weight_unit_id[1],$carrier_weight[1],$carrier_weight[1]])->first();
  345. if ($waybillPriceModelProvinceOne&&$waybillPriceModelProvinceTwo){
  346. if ($waybillPriceModelProvinceOne->unit_price*$carrier_weight[0]>=$waybillPriceModelProvinceTwo->unit_price*$carrier_weight[1]){
  347. return $waybillPriceModelProvinceOne->id;
  348. }else{
  349. return $waybillPriceModelProvinceTwo->id;
  350. }
  351. }
  352. if ($waybillPriceModelProvinceOne)return $waybillPriceModelProvinceOne->id;
  353. if ($waybillPriceModelProvinceTwo)return $waybillPriceModelProvinceTwo->id;
  354. //城市价格区间都为空
  355. $waybillPriceModelProvinceRangeOne=WaybillPriceModel::whereRaw('carrier_id = ? AND province_id = ? AND unit_id = ? AND range_max IS NULL AND city_id IS NULL',
  356. [$carrier_id,$city->province_id,$carrier_weight_unit_id[0]])->first();
  357. $waybillPriceModelProvinceRangeTwo=WaybillPriceModel::whereRaw('carrier_id = ? AND province_id = ? AND unit_id = ? AND range_max IS NULL AND city_id IS NULL',
  358. [$carrier_id,$city->province_id,$carrier_weight_unit_id[1]])->first();
  359. if ($waybillPriceModelProvinceRangeOne&&$waybillPriceModelProvinceRangeTwo){
  360. if ($waybillPriceModelProvinceRangeOne->unit_price*$carrier_weight[0]>=$waybillPriceModelProvinceRangeTwo->unit_price*$carrier_weight[1]){
  361. return $waybillPriceModelProvinceRangeOne->id;
  362. }else{
  363. return $waybillPriceModelProvinceRangeOne->id;
  364. }
  365. }
  366. if ($waybillPriceModelProvinceRangeOne)return $waybillPriceModelProvinceRangeOne->id;
  367. if ($waybillPriceModelProvinceRangeOne)return $waybillPriceModelProvinceRangeOne->id;
  368. };
  369. for ($i=0;$i<count($carrier_weight);$i++){
  370. if ($carrier_weight[$i]&&$carrier_weight_unit_id[$i]){
  371. //城市价格区间不为空
  372. $waybillPriceModel=WaybillPriceModel::where('carrier_id',$carrier_id)->where('city_id',$destination_city_id)
  373. ->where('range_min','<',$carrier_weight[$i])->where('range_max','>=',$carrier_weight[$i])
  374. ->where('unit_id',$carrier_weight_unit_id[$i])->first();
  375. if($waybillPriceModel)return $waybillPriceModel->id;
  376. //价格区间为空
  377. $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();
  378. if ($waybillPriceModelRange){ return $waybillPriceModelRange->id;}
  379. //城市为空
  380. $city=City::where('id',$destination_city_id)->select('province_id')->first();
  381. $waybillPriceModelProvince=WaybillPriceModel::whereRaw('carrier_id = ? AND province_id = ? AND unit_id = ? AND range_max >= ? AND range_min < ? AND city_id IS NULL',
  382. [$carrier_id,$city->province_id,$carrier_weight_unit_id[$i],$carrier_weight[$i],$carrier_weight[$i]])->first();
  383. if ($waybillPriceModelProvince){return $waybillPriceModelProvince->id;}
  384. //城市价格区间都为空
  385. $waybillPriceModelProvinceRange=WaybillPriceModel::whereRaw('carrier_id = ? AND province_id = ? AND unit_id = ? AND range_max IS NULL AND city_id IS NULL',
  386. [$carrier_id,$city->province_id,$carrier_weight_unit_id[$i]])->first();
  387. if ($waybillPriceModelProvinceRange){return $waybillPriceModelProvinceRange->id;}
  388. }
  389. }
  390. return false;
  391. }
  392. /*三层条件:无优先级,找到第一个直接返回
  393. * 无论是否为KG||T,计数单位一为KG,计数单位一为T,计数单位二为KG,计数单位二为T
  394. * 计数一与计数二同时存在取最贵价格:
  395. * 计数一存在,二不存在:
  396. * 计数二存在,一不存在:
  397. * 城市价格区间不为空,城市价格区间都为空,城市为空,价格区间为空
  398. * */
  399. public function isWaybillPriceModel(Request $request){
  400. $carrier_id=$request->input('carrier_id');
  401. $destination_city_id=$request->input('destination_city_id');
  402. $carrier_weight=$request->input('carrier_weight');
  403. $carrier_weight_unit_id=$request->input('carrier_weight_unit_id');
  404. $result=$this->checkWaybillPriceModel($carrier_id,$destination_city_id,$carrier_weight,$carrier_weight_unit_id);
  405. if (!$result){
  406. //单位为kg,T时
  407. $unitKG=Unit::where('name','kg')->first();
  408. $unitT=Unit::where('name','T')->first();
  409. if ($carrier_weight_unit_id[0]==$unitKG->id){
  410. $carrier_weight_unit_id[0]=$unitT->id;
  411. $carrier_weight[0]=$carrier_weight[0]/1000;
  412. $result=$this->checkWaybillPriceModel($carrier_id,$destination_city_id,$carrier_weight,$carrier_weight_unit_id);
  413. if ($result)return ['success'=>$result];
  414. }
  415. if ($carrier_weight_unit_id[1]==$unitKG->id){
  416. $carrier_weight_unit_id[1]=$unitT->id;
  417. $carrier_weight[1]=$carrier_weight[1]/1000;
  418. $result=$this->checkWaybillPriceModel($carrier_id,$destination_city_id,$carrier_weight,$carrier_weight_unit_id);
  419. if ($result)return ['success'=>$result];
  420. }
  421. if ($carrier_weight_unit_id[0]==$unitT->id){
  422. $carrier_weight_unit_id[0]=$unitKG->id;
  423. $carrier_weight[0]=$carrier_weight[0]*1000;
  424. $result=$this->checkWaybillPriceModel($carrier_id,$destination_city_id,$carrier_weight,$carrier_weight_unit_id);
  425. if ($result)return ['success'=>$result];
  426. }
  427. if ($carrier_weight_unit_id[1]==$unitT->id){
  428. $carrier_weight_unit_id[1]=$unitKG->id;
  429. $carrier_weight[1]=$carrier_weight[1]*1000;
  430. $result=$this->checkWaybillPriceModel($carrier_id,$destination_city_id,$carrier_weight,$carrier_weight_unit_id);
  431. if ($result)return ['success'=>$result];
  432. }
  433. }
  434. return ['success'=>$result];
  435. }
  436. public function waybillUpdate(Request $request, $id){
  437. if(!Gate::allows('运输管理-编辑')){ return redirect(url('/')); }
  438. $this->validatorWaybill($request,$id)->validate();
  439. $data=$request->input();
  440. $waybill=Waybill::find($id);
  441. $waybill->fill($data);
  442. if ($waybill->save()){
  443. $this->log(__METHOD__,__FUNCTION__,json_encode($waybill),Auth::user()['id']);
  444. return redirect('waybill')->with('successTip','运单“'.$waybill->waybill_number.'”修改成功');
  445. }
  446. }
  447. public function waybillAudit(Request $request){
  448. if(!Gate::allows('运输管理-运单审核')){ return redirect(url('/')); }
  449. $id=$request->input('id');
  450. $waybill=Waybill::find($id);
  451. $isAudit=WaybillAuditLog::whereRaw('waybill_id = ? and audit_stage = ?',[$id,"运单阶段"])->first();
  452. if (empty($isAudit)){
  453. $waybillAuditLog=new WaybillAuditLog([
  454. 'waybill_id'=>$id,
  455. 'audit_stage'=>'运单阶段',
  456. 'user_id'=>Auth::id(),
  457. ]);
  458. $waybillAuditLog->save();
  459. $waybillAuditLog['user']=Auth::user();
  460. $waybill->status='已审核';
  461. $result=$waybill->save();
  462. $this->log(__METHOD__,__FUNCTION__,json_encode($waybill),Auth::user()['id']);
  463. return ['success'=>$result,'status'=>$waybill->status,'waybillAuditLog'=>$waybillAuditLog];
  464. }
  465. return ['exception'=>'请勿重复审核!'];
  466. }
  467. public function waybillEdit($id){
  468. if(!Gate::allows('运输管理-编辑')){ return redirect(url('/')); }
  469. $waybill=Waybill::find($id);
  470. $owners=Owner::get();
  471. return view('waybill.waybillEdit',['waybill'=>$waybill,'owners'=>$owners]);
  472. }
  473. public function waybillRetreatAudit(Request $request){
  474. if(!Gate::allows('运输管理-调度')){ return redirect(url('/')); }
  475. $id=$request->input('id');
  476. $waybill=Waybill::find($id);
  477. WaybillAuditLog::whereRaw('waybill_id = ? and audit_stage = ?',[$id,"运单阶段"])->delete();
  478. $waybill->status='待重审';
  479. $result=$waybill->save();
  480. $this->log(__METHOD__,__FUNCTION__,json_encode($waybill),Auth::user()['id']);
  481. return ['success'=>$result,'status'=>$waybill->status];
  482. }
  483. public function waybillEndAudit(Request $request){
  484. if(!Gate::allows('运输管理-调度审核')){ return redirect(url('/')); }
  485. $id=$request->input('id');
  486. $waybill=Waybill::find($id);
  487. if (!$waybill->charge&&!$waybill->collect_fee)return ['exception'=>'收费或到付费用未填!'];
  488. if ($waybill->charge==0&&$waybill->collect_fee==0)return ['exception'=>'收费与到付费用都为0!'];
  489. if ($waybill->type=='专线'){
  490. if (!$waybill->carrier_weight||$waybill->carrier_weight==0)return ['exception'=>'承运商计重未填或为0!'];
  491. if (!$waybill->carrier_weight_unit_id)return ['exception'=>'承运商计重单位未选!'];
  492. }
  493. $isAudit=WaybillAuditLog::whereRaw('waybill_id = ? and audit_stage = ?',[$id,"调度阶段"])->first();
  494. if (empty($isAudit)){
  495. $waybillAuditLog=new WaybillAuditLog([
  496. 'waybill_id'=>$id,
  497. 'audit_stage'=>'调度阶段',
  498. 'user_id'=>Auth::id(),
  499. ]);
  500. $waybillAuditLog->save();
  501. $waybillAuditLog['user']=Auth::user();
  502. if ($waybill->waybill_price_model_id||$waybill->type=='直发车'){
  503. $waybill->status='已完结';
  504. $result=$waybill->save();
  505. $waybillPayoff=WaybillPayoff::where('waybill_id','=',$id)->first();
  506. $waybillPayoff->load(["waybill"]);
  507. $waybillPayoff->waybill->load(["owner","carrier","origination_city","destination_city","carType","waybillAuditLogs"]);
  508. $waybillPayoff->waybill->waybillAuditLogs->load(["user"]);
  509. $waybillPayoffJson=json_encode($waybillPayoff,JSON_UNESCAPED_UNICODE);
  510. WaybillFinancialSnapshot::create([
  511. 'waybill_id'=>$id,
  512. 'json_content'=>$waybillPayoffJson,
  513. ]);
  514. }else{
  515. $waybill->status='无模型';
  516. $result=$waybill->save();
  517. $waybillPayoff=WaybillPayoff::where('waybill_id','=',$id)->first();
  518. if ($waybillPayoff){
  519. $waybillPayoff->load(["waybill"]);
  520. $waybillPayoff->waybill->load(["owner","carrier","origination_city","destination_city","carType","waybillAuditLogs"]);
  521. $waybillPayoff->waybill->waybillAuditLogs->load(["user"]);
  522. $waybillPayoffJson=json_encode($waybillPayoff,JSON_UNESCAPED_UNICODE);
  523. WaybillFinancialExcepted::create([
  524. 'waybill_id'=>$id,
  525. 'json_content'=>$waybillPayoffJson,
  526. ]);
  527. }
  528. }
  529. $this->log(__METHOD__,__FUNCTION__,$waybillPayoffJson,Auth::user()['id']);
  530. return ['success'=>$result,'status'=>$waybill->status,'waybillAuditLog'=>$waybillAuditLog];
  531. }
  532. return ['exception'=>'请勿重复审核!'];
  533. }
  534. public function waybillExport($id){
  535. $id = explode( ',',$id);
  536. $row=[[
  537. 'type'=>'运单类型',
  538. 'waybill_number'=>'运单号',
  539. 'owner'=>'货主',
  540. 'wms_bill_number'=>'WMS单号',
  541. 'origination'=>'始发地',
  542. 'destination'=>'目的地',
  543. 'recipient'=>'收件人',
  544. 'recipient_mobile'=>'收件人电话',
  545. 'charge'=>'收费(元)',
  546. 'ordering_remark'=>'下单备注',
  547. 'carrier'=>'承运商',
  548. 'carrier_bill'=>'承运商单号',
  549. 'origination_city'=>'始发市',
  550. 'destination_city'=>'目的市',
  551. 'warehouse_weight'=>'仓库计数(抛)',
  552. 'warehouse_weight_other'=>'仓库计数二 ',
  553. 'carrier_weight'=>'承运商计数(抛)',
  554. 'carrier_weight_other'=>'承运商计数二',
  555. 'carType'=>'车型',
  556. 'car_owner_info'=>'车辆信息',
  557. 'fee'=>'运费(元)',
  558. 'pick_up_fee'=>'提货费(元)',
  559. 'other_fee'=>'其他费用(元)',
  560. 'collect_fee'=>'到付金额(元)',
  561. 'dispatch_remark'=>'调度备注',
  562. 'waybillAuditor'=>'运单审核人',
  563. 'dispatchAuditor'=>'调度审核人',
  564. 'created_at'=>'创建时间'
  565. ]];
  566. $feeVisible=true;
  567. if(!Gate::allows('运输管理-可见费用项')){
  568. $feeVisible=false;
  569. unset($row[0]['fee'],$row[0]['pick_up_fee'],$row[0]['other_fee'],$row[0]['collect_fee']);
  570. }
  571. $list=[];
  572. for ($i=0; $i<count($id);$i++){
  573. $waybill=Waybill::with(['owner', 'waybillAuditLogs' => function ($query) {
  574. return $query->with('user');
  575. }])->find($id[$i]);
  576. foreach ($waybill->waybillAuditLogs as $waybillAuditLog){
  577. if ($waybillAuditLog->audit_stage=="运单阶段"){
  578. $waybillAuditor=$waybillAuditLog->user->name;
  579. }else{
  580. $waybillAuditor='';
  581. }
  582. if ($waybillAuditLog->audit_stage=="调度阶段"){
  583. $dispatchAuditor=$waybillAuditLog->user->name;
  584. }else{
  585. $dispatchAuditor='';
  586. }
  587. };
  588. $w=[
  589. 'type'=>isset($waybill->type)?$waybill->type:'',
  590. 'waybill_number'=>isset($waybill->waybill_number)?$waybill->waybill_number:'',
  591. 'owner'=>isset($waybill->owner->name)?$waybill->owner->name:'',
  592. 'wms_bill_number'=>isset($waybill->wms_bill_number)?$waybill->wms_bill_number:'',
  593. 'origination'=>isset($waybill->origination)?$waybill->origination:'',
  594. 'destination'=>isset($waybill->destination)?$waybill->destination:'',
  595. 'recipient'=>isset($waybill->recipient)?$waybill->recipient:'',
  596. 'recipient_mobile'=>isset($waybill->recipient_mobile)?$waybill->recipient_mobile:'',
  597. 'charge'=>isset($waybill->charge)?$waybill->charge:'',
  598. 'ordering_remark'=>isset($waybill->ordering_remark)?$waybill->ordering_remark:'',
  599. 'carrier'=>isset($waybill->carrier_name)?$waybill->carrier_name:'',
  600. 'carrier_bill'=>isset($waybill->carrier_bill)?$waybill->carrier_bill:'',
  601. 'origination_city'=>isset($waybill->origination_city_name)?$waybill->origination_city_name:'',
  602. 'destination_city'=>isset($waybill->destination_city_name)?$waybill->destination_city_name:'',
  603. 'warehouse_weight'=>isset($waybill->warehouse_weight)?$waybill->warehouse_weight.' '.(isset($waybill->warehouse_weight_unit_name)?$waybill->warehouse_weight_unit_name:''):'',
  604. '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:''):'',
  605. 'carrier_weight'=>isset($waybill->carrier_weight)?$waybill->carrier_weight.' '.(isset($waybill->carrier_weight_unit_name)?$waybill->carrier_weight_unit_name:''):'',
  606. '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:''):'',
  607. 'carType'=>isset($waybill->carType->name)?$waybill->carType->name.($waybill->carType->length.'米'):'',
  608. 'car_owner_info'=>isset($waybill->car_owner_info)?$waybill->car_owner_info:'',
  609. 'fee'=>isset($waybill->fee)?$waybill->fee:'',
  610. 'pick_up_fee'=>isset($waybill->pick_up_fee)?$waybill->pick_up_fee:'',
  611. 'other_fee'=>isset($waybill->other_fee)?$waybill->other_fee:'',
  612. 'collect_fee'=>isset($waybill->collect_fee)?$waybill->collect_fee:'',
  613. 'dispatch_remark'=>isset($waybill->dispatch_remark)?$waybill->dispatch_remark:'',
  614. 'waybillAuditor'=>isset($waybillAuditor)?$waybillAuditor:'',
  615. 'dispatchAuditor'=>isset($dispatchAuditor)?$dispatchAuditor:'',
  616. 'created_at'=>isset($waybill->created_at)?$waybill->created_at:''
  617. ];
  618. if(!$feeVisible){
  619. unset($w['fee'],$w['pick_up_fee'],$w['other_fee'],$w['collect_fee']);
  620. }
  621. $list[$i]=$w;
  622. }
  623. $this->log(__METHOD__,__FUNCTION__,json_encode($waybill),Auth::user()['id']);
  624. return Excel::download(new WaybillExport($row,$list), date('Y:m:d ') . '运单列表.xls');
  625. }
  626. protected function validatorWaybill(Request $request,$id){
  627. if ($id){$wms_bill_number=$id;};
  628. $validator=Validator::make($request->input(),[
  629. 'owner_id'=>'required',
  630. 'wms_bill_number'=>['nullable','max:50',isset($wms_bill_number)?"unique:waybills,wms_bill_number,$wms_bill_number":'unique:waybills,wms_bill_number'],
  631. 'origination'=>'required|max:255',
  632. 'destination'=>'required|max:255',
  633. 'recipient'=>'required|max:50',
  634. 'recipient_mobile'=>['required','regex:/^(\d{7,11})|(1[3|4|5|7|8][0-9]\d{4,8})$/'],
  635. 'charge'=>'nullable|min:0|max:999999|numeric',
  636. 'collect_fee'=>'nullable|min:0|numeric',
  637. ],[
  638. 'required'=>':attribute 为必填项',
  639. 'alpha_num'=>':attribute 应为字母或数字',
  640. 'max'=>':attribute 字符过多或输入值过大',
  641. 'regex'=>':attribute 输入有误',
  642. 'integer'=>':attribute 应为整数',
  643. 'min'=>':attribute 不得为负',
  644. 'numeric'=>':attribute 应为数字',
  645. 'unique'=>':attribute 已存在',
  646. ],[
  647. 'owner_id'=>'货主',
  648. 'wms_bill_number'=>'WMS单号',
  649. 'origination'=>'始发地',
  650. 'destination'=>'目的地',
  651. 'recipient'=>'收件人',
  652. 'recipient_mobile'=>'收件人电话',
  653. 'charge'=>'收费',
  654. 'collect_fee'=>'到付金额',
  655. ]);
  656. return $validator;
  657. }
  658. protected function validatorWaybillDispatch(Request $request,$id){
  659. if ($request->input('type')=='直发车'){
  660. $validator=Validator::make($request->input(),[
  661. 'carrier_bill'=>"nullable|max:50|unique:waybills,carrier_bill,$id",
  662. 'fee'=>'required|min:0|numeric|max:999999',
  663. 'other_fee'=>'nullable|min:0|numeric|max:999999',
  664. 'charge'=>'nullable|min:0|numeric|max:999999',
  665. ],[
  666. 'required'=>':attribute 为必填项',
  667. 'alpha_num'=>':attribute 应为字母或数字',
  668. 'max'=>':attribute 字符过多或输入值过大',
  669. 'min'=>':attribute 不得为负',
  670. 'numeric'=>':attribute 应为数字',
  671. 'unique'=>':attribute 已存在',
  672. ],[
  673. 'carrier_bill'=>'承运商单号',
  674. 'fee'=>'运费',
  675. 'other_fee'=>'其他费用',
  676. 'charge'=>'收费',
  677. ]);
  678. return $validator;
  679. }else if ($request->input('type')=='专线'){
  680. $validator=Validator::make($request->input(),[
  681. 'carrier_bill'=>"nullable|max:50|unique:waybills,carrier_bill,$id",
  682. 'pick_up_fee'=>'nullable|min:0|numeric|max:999999',
  683. 'other_fee'=>'nullable|min:0|numeric|max:999999',
  684. 'carrier_id'=>'required|integer',
  685. 'destination_city_id'=>'required|integer',
  686. 'warehouse_weight'=>'nullable|min:0|numeric|max:999999',
  687. 'carrier_weight'=>'nullable|min:0|numeric|max:999999',
  688. 'warehouse_weight_unit_id'=>'required_with:warehouse_weight|integer',
  689. 'carrier_weight_unit_id'=>'required_with:carrier_weight|integer',
  690. 'warehouse_weight_other'=>'nullable|min:0|numeric|max:999999',
  691. 'carrier_weight_other'=>'nullable|min:0|numeric|max:999999',
  692. 'warehouse_weight_unit_id_other'=>'required_with:warehouse_weight_other|integer',
  693. 'carrier_weight_unit_id_other'=>'required_with:carrier_weight_other|integer',
  694. 'charge'=>'nullable|min:0|numeric|max:999999',
  695. ],[
  696. 'required'=>':attribute 为必填项',
  697. 'alpha_num'=>':attribute 应为字母或数字',
  698. 'max'=>':attribute 字符过多或输入值过大',
  699. 'min'=>':attribute 不得为负',
  700. 'numeric'=>':attribute 应为数字',
  701. 'unique'=>':attribute 已存在',
  702. 'required_with'=>':attribute 未填',
  703. ],[
  704. 'carrier_bill'=>'承运商单号',
  705. 'warehouse_weight'=>'仓库计数(抛)',
  706. 'carrier_weight'=>'承运商计数(抛)',
  707. 'pick_up_fee'=>'提货费',
  708. 'other_fee'=>'其他费用',
  709. 'carrier_id'=>'承运商',
  710. 'destination_city_id'=>'目的市',
  711. 'carrier_weight_unit_id'=>'承运商计数单位',
  712. 'charge'=>'收费',
  713. 'warehouse_weight_unit_id'=>'仓库计数单位',
  714. 'warehouse_weight_other'=>'仓库计数二',
  715. 'carrier_weight_other'=>'承运商计数二',
  716. 'warehouse_weight_unit_id_other'=>'仓库技数单位二',
  717. 'carrier_weight_unit_id_other'=>'承运商计数单位二',
  718. ]);
  719. return $validator;
  720. }else{
  721. return false;
  722. }
  723. }
  724. }