WaybillsController.php 42 KB

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