WaybillsController.php 39 KB

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