WaybillsController.php 39 KB

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