WaybillsController.php 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  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('state')){
  61. $waybills=$waybills->where('state',$request->input('state'));
  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('state')){
  112. $waybills=$waybills->where('state',$request->input('state'));
  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('state')){
  163. $waybills=$waybills->where('state',$request->input('state'));
  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. 'state'=>'未审核',
  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. $waybill->fee=$fee;
  270. $waybill->waybill_price_model_id=$waybillPriceModel_id;
  271. }
  272. $waybill->save();
  273. if ($waybill->charge)$total_receivable=($waybill->charge);
  274. elseif ($waybill->collect_fee)$total_receivable=($waybill->collect_fee);
  275. $total_expense=($waybill->pick_up_fee)+($waybill->other_fee)+($waybill->fee);
  276. }
  277. if ($total_receivable){
  278. $waybillPayoff=WaybillPayoff::where('waybill_id','=',$id)->first();
  279. if ($waybillPayoff){
  280. $waybillPayoff->waybill_id=$id;
  281. $waybillPayoff->total_expense=$total_expense;
  282. $waybillPayoff->total_receivable=$total_receivable;
  283. $waybillPayoff->gross_margin=$total_receivable-$total_expense;
  284. $waybillPayoff->gross_profit_rate=(($total_receivable-$total_expense)/$total_receivable);
  285. $waybillPayoff->save();
  286. }else{
  287. WaybillPayoff::create([
  288. 'waybill_id'=>$id,
  289. 'total_expense'=>$total_expense,
  290. 'total_receivable'=>$total_receivable,
  291. 'gross_margin'=>$total_receivable-$total_expense,
  292. 'gross_profit_rate'=>(($total_receivable-$total_expense)/$total_receivable),
  293. ]);
  294. };
  295. }
  296. $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  297. return redirect('waybill')->with('successTip','运单“'.$waybill->waybill_number.'”调度成功');
  298. }
  299. }
  300. public function checkWaybillPriceModel($carrier_id,$destination_city_id,$carrier_weight,$carrier_weight_unit_id){
  301. if(!$carrier_id)return false;
  302. if(!$destination_city_id)return false;
  303. if(!$carrier_weight)return false;
  304. if(!$carrier_weight_unit_id)return false;
  305. //多个计数标准,计算价格,取最贵
  306. if ($carrier_weight[0]&&$carrier_weight[1]&&$carrier_weight_unit_id[0]&&$carrier_weight_unit_id[1]){
  307. //城市价格区间不为空
  308. $waybillPriceModelOne=WaybillPriceModel::where('carrier_id',$carrier_id)->where('city_id',$destination_city_id)
  309. ->where('range_min','<',$carrier_weight[0])->where('range_max','>=',$carrier_weight[0])
  310. ->where('unit_id',$carrier_weight_unit_id[0])->first();
  311. $waybillPriceModelTwo=WaybillPriceModel::where('carrier_id',$carrier_id)->where('city_id',$destination_city_id)
  312. ->where('range_min','<',$carrier_weight[1])->where('range_max','>=',$carrier_weight[1])
  313. ->where('unit_id',$carrier_weight_unit_id[1])->first();
  314. if ($waybillPriceModelOne&&$waybillPriceModelTwo){
  315. if ($waybillPriceModelOne->unit_price*$carrier_weight[0]>=$waybillPriceModelTwo->unit_price*$carrier_weight[1]){
  316. return $waybillPriceModelOne->id;
  317. }else{
  318. return $waybillPriceModelTwo->id;
  319. }
  320. }
  321. if ($waybillPriceModelOne)return $waybillPriceModelOne->id;
  322. if ($waybillPriceModelTwo)return $waybillPriceModelTwo->id;
  323. //价格区间为空
  324. $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();
  325. $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();
  326. if ($waybillPriceModelRangeOne&&$waybillPriceModelRangeTwo){
  327. if ($waybillPriceModelRangeOne->unit_price*$carrier_weight[0]>=$waybillPriceModelRangeTwo->unit_price*$carrier_weight[1]){
  328. return $waybillPriceModelRangeOne->id;
  329. }else{
  330. return $waybillPriceModelRangeTwo->id;
  331. }
  332. }
  333. if ($waybillPriceModelRangeOne)return $waybillPriceModelRangeOne->id;
  334. if ($waybillPriceModelRangeTwo)return $waybillPriceModelRangeTwo->id;
  335. //城市为空
  336. $city=City::where('id',$destination_city_id)->select('province_id')->first();
  337. $waybillPriceModelProvinceOne=WaybillPriceModel::whereRaw('carrier_id = ? AND province_id = ? AND unit_id = ? AND range_max >= ? AND range_min < ? AND city_id IS NULL',
  338. [$carrier_id,$city->province_id,$carrier_weight_unit_id[0],$carrier_weight[0],$carrier_weight[0]])->first();
  339. $waybillPriceModelProvinceTwo=WaybillPriceModel::whereRaw('carrier_id = ? AND province_id = ? AND unit_id = ? AND range_max >= ? AND range_min < ? AND city_id IS NULL',
  340. [$carrier_id,$city->province_id,$carrier_weight_unit_id[1],$carrier_weight[1],$carrier_weight[1]])->first();
  341. if ($waybillPriceModelProvinceOne&&$waybillPriceModelProvinceTwo){
  342. if ($waybillPriceModelProvinceOne->unit_price*$carrier_weight[0]>=$waybillPriceModelProvinceTwo->unit_price*$carrier_weight[1]){
  343. return $waybillPriceModelProvinceOne->id;
  344. }else{
  345. return $waybillPriceModelProvinceTwo->id;
  346. }
  347. }
  348. if ($waybillPriceModelProvinceOne)return $waybillPriceModelProvinceOne->id;
  349. if ($waybillPriceModelProvinceTwo)return $waybillPriceModelProvinceTwo->id;
  350. //城市价格区间都为空
  351. $waybillPriceModelProvinceRangeOne=WaybillPriceModel::whereRaw('carrier_id = ? AND province_id = ? AND unit_id = ? AND range_max IS NULL AND city_id IS NULL',
  352. [$carrier_id,$city->province_id,$carrier_weight_unit_id[0]])->first();
  353. $waybillPriceModelProvinceRangeTwo=WaybillPriceModel::whereRaw('carrier_id = ? AND province_id = ? AND unit_id = ? AND range_max IS NULL AND city_id IS NULL',
  354. [$carrier_id,$city->province_id,$carrier_weight_unit_id[1]])->first();
  355. if ($waybillPriceModelProvinceRangeOne&&$waybillPriceModelProvinceRangeTwo){
  356. if ($waybillPriceModelProvinceRangeOne->unit_price*$carrier_weight[0]>=$waybillPriceModelProvinceRangeTwo->unit_price*$carrier_weight[1]){
  357. return $waybillPriceModelProvinceRangeOne->id;
  358. }else{
  359. return $waybillPriceModelProvinceRangeOne->id;
  360. }
  361. }
  362. if ($waybillPriceModelProvinceRangeOne)return $waybillPriceModelProvinceRangeOne->id;
  363. if ($waybillPriceModelProvinceRangeOne)return $waybillPriceModelProvinceRangeOne->id;
  364. };
  365. for ($i=0;$i<count($carrier_weight);$i++){
  366. if ($carrier_weight[$i]&&$carrier_weight_unit_id[$i]){
  367. //城市价格区间不为空
  368. $waybillPriceModel=WaybillPriceModel::where('carrier_id',$carrier_id)->where('city_id',$destination_city_id)
  369. ->where('range_min','<',$carrier_weight[$i])->where('range_max','>=',$carrier_weight[$i])
  370. ->where('unit_id',$carrier_weight_unit_id[$i])->first();
  371. if($waybillPriceModel)return $waybillPriceModel->id;
  372. //价格区间为空
  373. $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();
  374. if ($waybillPriceModelRange){ return $waybillPriceModelRange->id;}
  375. //城市为空
  376. $city=City::where('id',$destination_city_id)->select('province_id')->first();
  377. $waybillPriceModelProvince=WaybillPriceModel::whereRaw('carrier_id = ? AND province_id = ? AND unit_id = ? AND range_max >= ? AND range_min < ? AND city_id IS NULL',
  378. [$carrier_id,$city->province_id,$carrier_weight_unit_id[$i],$carrier_weight[$i],$carrier_weight[$i]])->first();
  379. if ($waybillPriceModelProvince){return $waybillPriceModelProvince->id;}
  380. //城市价格区间都为空
  381. $waybillPriceModelProvinceRange=WaybillPriceModel::whereRaw('carrier_id = ? AND province_id = ? AND unit_id = ? AND range_max IS NULL AND city_id IS NULL',
  382. [$carrier_id,$city->province_id,$carrier_weight_unit_id[$i]])->first();
  383. if ($waybillPriceModelProvinceRange){return $waybillPriceModelProvinceRange->id;}
  384. }
  385. }
  386. return false;
  387. }
  388. public function isWaybillPriceModel(Request $request){
  389. $carrier_id=$request->input('carrier_id');
  390. $destination_city_id=$request->input('destination_city_id');
  391. $carrier_weight=$request->input('carrier_weight');
  392. $carrier_weight_unit_id=$request->input('carrier_weight_unit_id');
  393. $result=$this->checkWaybillPriceModel($carrier_id,$destination_city_id,$carrier_weight,$carrier_weight_unit_id);
  394. if (!$result){
  395. //单位为kg,T时
  396. $unitKG=Unit::where('name','kg')->first();
  397. $unitT=Unit::where('name','T')->first();
  398. if ($carrier_weight_unit_id[0]==$unitKG->id){
  399. $carrier_weight_unit_id[0]=$unitT->id;
  400. $carrier_weight[0]=$carrier_weight[0]/1000;
  401. $result=$this->checkWaybillPriceModel($carrier_id,$destination_city_id,$carrier_weight,$carrier_weight_unit_id);
  402. }
  403. if ($carrier_weight_unit_id[1]==$unitKG->id){
  404. $carrier_weight_unit_id[1]=$unitT->id;
  405. $carrier_weight[1]=$carrier_weight[1]/1000;
  406. $result=$this->checkWaybillPriceModel($carrier_id,$destination_city_id,$carrier_weight,$carrier_weight_unit_id);
  407. }
  408. if ($carrier_weight_unit_id[0]==$unitT->id){
  409. $carrier_weight_unit_id[0]=$unitKG->id;
  410. $carrier_weight[0]=$carrier_weight[0]*1000;
  411. $result=$this->checkWaybillPriceModel($carrier_id,$destination_city_id,$carrier_weight,$carrier_weight_unit_id);
  412. }
  413. if ($carrier_weight_unit_id[1]==$unitT->id){
  414. $carrier_weight_unit_id[1]=$unitKG->id;
  415. $carrier_weight[1]=$carrier_weight[1]*1000;
  416. $result=$this->checkWaybillPriceModel($carrier_id,$destination_city_id,$carrier_weight,$carrier_weight_unit_id);
  417. }
  418. }
  419. return ['success'=>$result];
  420. }
  421. public function waybillUpdate(Request $request, $id){
  422. if(!Gate::allows('运输管理-编辑')){ return redirect(url('/')); }
  423. $this->validatorWaybill($request,$id)->validate();
  424. $data=$request->input();
  425. $waybill=Waybill::find($id);
  426. $waybill->fill($data);
  427. if ($waybill->save()){
  428. $this->log(__METHOD__,__FUNCTION__,json_encode($waybill),Auth::user()['id']);
  429. return redirect('waybill')->with('successTip','运单“'.$waybill->waybill_number.'”修改成功');
  430. }
  431. }
  432. public function waybillAudit(Request $request){
  433. if(!Gate::allows('运输管理-运单审核')){ return redirect(url('/')); }
  434. $id=$request->input('id');
  435. $waybill=Waybill::find($id);
  436. $isAudit=WaybillAuditLog::whereRaw('waybill_id = ? and audit_stage = ?',[$id,"运单阶段"])->first();
  437. if (empty($isAudit)){
  438. $waybillAuditLog=new WaybillAuditLog([
  439. 'waybill_id'=>$id,
  440. 'audit_stage'=>'运单阶段',
  441. 'user_id'=>Auth::id(),
  442. ]);
  443. $waybillAuditLog->save();
  444. $waybillAuditLog['user']=Auth::user();
  445. $waybill->state='已审核';
  446. $result=$waybill->save();
  447. $this->log(__METHOD__,__FUNCTION__,json_encode($waybill),Auth::user()['id']);
  448. return ['success'=>$result,'state'=>$waybill->state,'waybillAuditLog'=>$waybillAuditLog];
  449. }
  450. return ['exception'=>'请勿重复审核!'];
  451. }
  452. public function waybillEdit($id){
  453. if(!Gate::allows('运输管理-编辑')){ return redirect(url('/')); }
  454. $waybill=Waybill::find($id);
  455. $owners=Owner::get();
  456. return view('waybill.waybillEdit',['waybill'=>$waybill,'owners'=>$owners]);
  457. }
  458. public function waybillRetreatAudit(Request $request){
  459. if(!Gate::allows('运输管理-调度')){ return redirect(url('/')); }
  460. $id=$request->input('id');
  461. $waybill=Waybill::find($id);
  462. WaybillAuditLog::whereRaw('waybill_id = ? and audit_stage = ?',[$id,"运单阶段"])->delete();
  463. $waybill->state='待重审';
  464. $result=$waybill->save();
  465. $this->log(__METHOD__,__FUNCTION__,json_encode($waybill),Auth::user()['id']);
  466. return ['success'=>$result,'state'=>$waybill->state];
  467. }
  468. public function waybillEndAudit(Request $request){
  469. if(!Gate::allows('运输管理-调度审核')){ return redirect(url('/')); }
  470. $id=$request->input('id');
  471. $waybill=Waybill::find($id);
  472. if (!$waybill->charge&&!$waybill->collect_fee)return ['exception'=>'收费或到付费用未填!'];
  473. if ($waybill->charge==0&&$waybill->collect_fee==0)return ['exception'=>'收费与到付费用都为0!'];
  474. if (!$waybill->carrier_weight||$waybill->carrier_weight==0)return ['exception'=>'承运商计重未填或为0!'];
  475. if (!$waybill->carrier_weight_unit_id)return ['exception'=>'承运商计重单位未选!'];
  476. $isAudit=WaybillAuditLog::whereRaw('waybill_id = ? and audit_stage = ?',[$id,"调度阶段"])->first();
  477. if (empty($isAudit)){
  478. $waybillAuditLog=new WaybillAuditLog([
  479. 'waybill_id'=>$id,
  480. 'audit_stage'=>'调度阶段',
  481. 'user_id'=>Auth::id(),
  482. ]);
  483. $waybillAuditLog->save();
  484. $waybillAuditLog['user']=Auth::user();
  485. if ($waybill->waybill_price_model_id||$waybill->type=='直发车'){
  486. $waybill->state='已完结';
  487. $result=$waybill->save();
  488. $waybillPayoff=WaybillPayoff::where('waybill_id','=',$id)->first();
  489. $waybillPayoff->load(["waybill"]);
  490. $waybillPayoff->waybill->load(["owner","carrier","origination_city","destination_city","carType","waybillAuditLogs"]);
  491. $waybillPayoff->waybill->waybillAuditLogs->load(["user"]);
  492. $waybillPayoffJson=json_encode($waybillPayoff,JSON_UNESCAPED_UNICODE);
  493. WaybillFinancialSnapshot::create([
  494. 'waybill_id'=>$id,
  495. 'json_content'=>$waybillPayoffJson,
  496. ]);
  497. }else{
  498. $waybill->state='未定义计费模型';
  499. $result=$waybill->save();
  500. $waybillPayoff=WaybillPayoff::where('waybill_id','=',$id)->first();
  501. if ($waybillPayoff){
  502. $waybillPayoff->load(["waybill"]);
  503. $waybillPayoff->waybill->load(["owner","carrier","origination_city","destination_city","carType","waybillAuditLogs"]);
  504. $waybillPayoff->waybill->waybillAuditLogs->load(["user"]);
  505. $waybillPayoffJson=json_encode($waybillPayoff,JSON_UNESCAPED_UNICODE);
  506. WaybillFinancialExcepted::create([
  507. 'waybill_id'=>$id,
  508. 'json_content'=>$waybillPayoffJson,
  509. ]);
  510. }
  511. }
  512. $this->log(__METHOD__,__FUNCTION__,$waybillPayoffJson,Auth::user()['id']);
  513. return ['success'=>$result,'state'=>$waybill->state,'waybillAuditLog'=>$waybillAuditLog];
  514. }
  515. return ['exception'=>'请勿重复审核!'];
  516. }
  517. public function waybillExport($id){
  518. $id = explode( ',',$id);
  519. $row=[[
  520. 'type'=>'运单类型',
  521. 'waybill_number'=>'运单号',
  522. 'owner'=>'货主',
  523. 'wms_bill_number'=>'WMS单号',
  524. 'origination'=>'始发地',
  525. 'destination'=>'目的地',
  526. 'recipient'=>'收件人',
  527. 'recipient_mobile'=>'收件人电话',
  528. 'charge'=>'收费(元)',
  529. 'ordering_remark'=>'下单备注',
  530. 'carrier'=>'承运商',
  531. 'carrier_bill'=>'承运商单号',
  532. 'origination_city'=>'始发市',
  533. 'destination_city'=>'目的市',
  534. 'warehouse_weight'=>'仓库计数(抛)',
  535. 'warehouse_weight_other'=>'仓库计数二 ',
  536. 'carrier_weight'=>'承运商计数(抛)',
  537. 'carrier_weight_other'=>'承运商计数二',
  538. 'carType'=>'车型',
  539. 'car_owner_info'=>'车辆信息',
  540. 'fee'=>'运费(元)',
  541. 'pick_up_fee'=>'提货费(元)',
  542. 'other_fee'=>'其他费用(元)',
  543. 'collect_fee'=>'到付金额(元)',
  544. 'dispatch_remark'=>'调度备注',
  545. 'waybillAuditor'=>'运单审核人',
  546. 'dispatchAuditor'=>'调度审核人',
  547. 'created_at'=>'创建时间'
  548. ]];
  549. $feeVisible=true;
  550. if(!Gate::allows('运输管理-可见费用项')){
  551. $feeVisible=false;
  552. unset($row[0]['fee'],$row[0]['pick_up_fee'],$row[0]['other_fee'],$row[0]['collect_fee']);
  553. }
  554. $list=[];
  555. for ($i=0; $i<count($id);$i++){
  556. $waybill=Waybill::with(['owner', 'waybillAuditLogs' => function ($query) {
  557. return $query->with('user');
  558. }])->find($id[$i]);
  559. foreach ($waybill->waybillAuditLogs as $waybillAuditLog){
  560. if ($waybillAuditLog->audit_stage=="运单阶段"){
  561. $waybillAuditor=$waybillAuditLog->user->name;
  562. }else{
  563. $waybillAuditor='';
  564. }
  565. if ($waybillAuditLog->audit_stage=="调度阶段"){
  566. $dispatchAuditor=$waybillAuditLog->user->name;
  567. }else{
  568. $dispatchAuditor='';
  569. }
  570. };
  571. $w=[
  572. 'type'=>isset($waybill->type)?$waybill->type:'',
  573. 'waybill_number'=>isset($waybill->waybill_number)?$waybill->waybill_number:'',
  574. 'owner'=>isset($waybill->owner->name)?$waybill->owner->name:'',
  575. 'wms_bill_number'=>isset($waybill->wms_bill_number)?$waybill->wms_bill_number:'',
  576. 'origination'=>isset($waybill->origination)?$waybill->origination:'',
  577. 'destination'=>isset($waybill->destination)?$waybill->destination:'',
  578. 'recipient'=>isset($waybill->recipient)?$waybill->recipient:'',
  579. 'recipient_mobile'=>isset($waybill->recipient_mobile)?$waybill->recipient_mobile:'',
  580. 'charge'=>isset($waybill->charge)?$waybill->charge:'',
  581. 'ordering_remark'=>isset($waybill->ordering_remark)?$waybill->ordering_remark:'',
  582. 'carrier'=>isset($waybill->carrier_name)?$waybill->carrier_name:'',
  583. 'carrier_bill'=>isset($waybill->carrier_bill)?$waybill->carrier_bill:'',
  584. 'origination_city'=>isset($waybill->origination_city_name)?$waybill->origination_city_name:'',
  585. 'destination_city'=>isset($waybill->destination_city_name)?$waybill->destination_city_name:'',
  586. 'warehouse_weight'=>isset($waybill->warehouse_weight)?$waybill->warehouse_weight.' '.(isset($waybill->warehouse_weight_unit_name)?$waybill->warehouse_weight_unit_name:''):'',
  587. '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:''):'',
  588. 'carrier_weight'=>isset($waybill->carrier_weight)?$waybill->carrier_weight.' '.(isset($waybill->carrier_weight_unit_name)?$waybill->carrier_weight_unit_name:''):'',
  589. '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:''):'',
  590. 'carType'=>isset($waybill->carType->name)?$waybill->carType->name.($waybill->carType->length.'米'):'',
  591. 'car_owner_info'=>isset($waybill->car_owner_info)?$waybill->car_owner_info:'',
  592. 'fee'=>isset($waybill->fee)?$waybill->fee:'',
  593. 'pick_up_fee'=>isset($waybill->pick_up_fee)?$waybill->pick_up_fee:'',
  594. 'other_fee'=>isset($waybill->other_fee)?$waybill->other_fee:'',
  595. 'collect_fee'=>isset($waybill->collect_fee)?$waybill->collect_fee:'',
  596. 'dispatch_remark'=>isset($waybill->dispatch_remark)?$waybill->dispatch_remark:'',
  597. 'waybillAuditor'=>isset($waybillAuditor)?$waybillAuditor:'',
  598. 'dispatchAuditor'=>isset($dispatchAuditor)?$dispatchAuditor:'',
  599. 'created_at'=>isset($waybill->created_at)?$waybill->created_at:''
  600. ];
  601. if(!$feeVisible){
  602. unset($w['fee'],$w['pick_up_fee'],$w['other_fee'],$w['collect_fee']);
  603. }
  604. $list[$i]=$w;
  605. }
  606. $this->log(__METHOD__,__FUNCTION__,json_encode($waybill),Auth::user()['id']);
  607. return Excel::download(new WaybillExport($row,$list), date('Y:m:d ') . '运单列表.xls');
  608. }
  609. protected function validatorWaybill(Request $request,$id){
  610. if ($id){$wms_bill_number=$id;};
  611. $validator=Validator::make($request->input(),[
  612. 'owner_id'=>'required',
  613. 'wms_bill_number'=>['nullable','max:50',isset($wms_bill_number)?"unique:waybills,wms_bill_number,$wms_bill_number":'unique:waybills,wms_bill_number'],
  614. 'origination'=>'required|max:255',
  615. 'destination'=>'required|max:255',
  616. 'recipient'=>'required|max:50',
  617. 'recipient_mobile'=>['required','regex:/^(\d{7,11})|(1[3|4|5|7|8][0-9]\d{4,8})$/'],
  618. 'charge'=>'nullable|min:0|max:999999|numeric',
  619. 'collect_fee'=>'nullable|min:0|numeric',
  620. ],[
  621. 'required'=>':attribute 为必填项',
  622. 'alpha_num'=>':attribute 应为字母或数字',
  623. 'max'=>':attribute 字符过多或输入值过大',
  624. 'regex'=>':attribute 输入有误',
  625. 'integer'=>':attribute 应为整数',
  626. 'min'=>':attribute 不得为负',
  627. 'numeric'=>':attribute 应为数字',
  628. 'unique'=>':attribute 已存在',
  629. ],[
  630. 'owner_id'=>'货主',
  631. 'wms_bill_number'=>'WMS单号',
  632. 'origination'=>'始发地',
  633. 'destination'=>'目的地',
  634. 'recipient'=>'收件人',
  635. 'recipient_mobile'=>'收件人电话',
  636. 'charge'=>'收费',
  637. 'collect_fee'=>'到付金额',
  638. ]);
  639. return $validator;
  640. }
  641. protected function validatorWaybillDispatch(Request $request,$id){
  642. if ($request->input('type')=='直发车'){
  643. $validator=Validator::make($request->input(),[
  644. 'carrier_bill'=>"nullable|max:50|unique:waybills,carrier_bill,$id",
  645. 'fee'=>'required|min:0|numeric|max:999999',
  646. 'other_fee'=>'nullable|min:0|numeric|max:999999',
  647. 'charge'=>'nullable|min:0|numeric|max:999999',
  648. ],[
  649. 'required'=>':attribute 为必填项',
  650. 'alpha_num'=>':attribute 应为字母或数字',
  651. 'max'=>':attribute 字符过多或输入值过大',
  652. 'min'=>':attribute 不得为负',
  653. 'numeric'=>':attribute 应为数字',
  654. 'unique'=>':attribute 已存在',
  655. ],[
  656. 'carrier_bill'=>'承运商单号',
  657. 'fee'=>'运费',
  658. 'other_fee'=>'其他费用',
  659. 'charge'=>'收费',
  660. ]);
  661. return $validator;
  662. }else if ($request->input('type')=='专线'){
  663. $validator=Validator::make($request->input(),[
  664. 'carrier_bill'=>"nullable|max:50|unique:waybills,carrier_bill,$id",
  665. 'pick_up_fee'=>'nullable|min:0|numeric|max:999999',
  666. 'other_fee'=>'nullable|min:0|numeric|max:999999',
  667. 'carrier_id'=>'required|integer',
  668. 'destination_city_id'=>'required|integer',
  669. 'warehouse_weight'=>'nullable|min:0|numeric|max:999999',
  670. 'carrier_weight'=>'nullable|min:0|numeric|max:999999',
  671. 'warehouse_weight_unit_id'=>'required_with:warehouse_weight|integer',
  672. 'carrier_weight_unit_id'=>'required_with:carrier_weight|integer',
  673. 'warehouse_weight_other'=>'nullable|min:0|numeric|max:999999',
  674. 'carrier_weight_other'=>'nullable|min:0|numeric|max:999999',
  675. 'warehouse_weight_unit_id_other'=>'required_with:warehouse_weight_other|integer',
  676. 'carrier_weight_unit_id_other'=>'required_with:carrier_weight_other|integer',
  677. 'charge'=>'nullable|min:0|numeric|max:999999',
  678. ],[
  679. 'required'=>':attribute 为必填项',
  680. 'alpha_num'=>':attribute 应为字母或数字',
  681. 'max'=>':attribute 字符过多或输入值过大',
  682. 'min'=>':attribute 不得为负',
  683. 'numeric'=>':attribute 应为数字',
  684. 'unique'=>':attribute 已存在',
  685. 'required_with'=>':attribute 未填',
  686. ],[
  687. 'carrier_bill'=>'承运商单号',
  688. 'warehouse_weight'=>'仓库计数(抛)',
  689. 'carrier_weight'=>'承运商计数(抛)',
  690. 'pick_up_fee'=>'提货费',
  691. 'other_fee'=>'其他费用',
  692. 'carrier_id'=>'承运商',
  693. 'destination_city_id'=>'目的市',
  694. 'carrier_weight_unit_id'=>'承运商计数单位',
  695. 'charge'=>'收费',
  696. 'warehouse_weight_unit_id'=>'仓库计数单位',
  697. 'warehouse_weight_other'=>'仓库计数二',
  698. 'carrier_weight_other'=>'承运商计数二',
  699. 'warehouse_weight_unit_id_other'=>'仓库技数单位二',
  700. 'carrier_weight_unit_id_other'=>'承运商计数单位二',
  701. ]);
  702. return $validator;
  703. }else{
  704. return false;
  705. }
  706. }
  707. }