WaybillsController.php 41 KB

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