WaybillsController.php 41 KB

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