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. $total_receivable=0;
  222. $waybill->fill($data);
  223. if ($waybill->save()){
  224. if ($waybill->type=="直发车"){
  225. if ($waybill->charge)$total_receivable=($waybill->charge);
  226. elseif ($waybill->collect_fee)$total_receivable=($waybill->collect_fee);
  227. $total_expense=($waybill->fee)+($waybill->other_fee)-($waybill->collect_fee);
  228. }else {
  229. $waybillPriceModel_id=$request->input('waybillPriceModel');
  230. if ($waybillPriceModel_id){
  231. $carrier_weight=$request->input('carrier_weight');
  232. $waybillPriceModel=WaybillPriceModel::find($waybillPriceModel_id);
  233. $carrier=Carrier::find($waybill->carrier_id);
  234. if ($carrier_weight<$waybillPriceModel->initial_weight){
  235. $fee=(($waybillPriceModel->unit_price)*($waybillPriceModel->initial_weight))+$carrier->delivery_fee;
  236. }else{
  237. $fee=(($waybillPriceModel->unit_price)*$carrier_weight)+$carrier->delivery_fee;
  238. }
  239. if ($waybillPriceModel->base_fee&&$fee<$waybillPriceModel->base_fee){
  240. $fee=$waybillPriceModel->base_fee;
  241. }
  242. $waybill->fee=$fee;
  243. $waybill->waybill_price_model_id=$waybillPriceModel_id;
  244. }
  245. $waybill->save();
  246. if ($waybill->charge)$total_receivable=($waybill->charge);
  247. elseif($waybill->collect_fee) {
  248. $total_receivable=($waybill->collect_fee);
  249. }
  250. $total_expense=($waybill->pick_up_fee)+($waybill->other_fee)+($waybill->fee);
  251. }
  252. if ($total_receivable>0){
  253. $waybillPayoff=WaybillPayoff::where('waybill_id','=',$id)->first();
  254. if ($waybillPayoff){
  255. $waybillPayoff->waybill_id=$id;
  256. $waybillPayoff->total_expense=$total_expense;
  257. $waybillPayoff->total_receivable=$total_receivable;
  258. $waybillPayoff->gross_margin=$total_receivable-$total_expense;
  259. $waybillPayoff->gross_profit_rate=(($total_receivable-$total_expense)/$total_receivable);
  260. $waybillPayoff->save();
  261. }else{
  262. WaybillPayoff::create([
  263. 'waybill_id'=>$id,
  264. 'total_expense'=>$total_expense,
  265. 'total_receivable'=>$total_receivable,
  266. 'gross_margin'=>$total_receivable-$total_expense,
  267. 'gross_profit_rate'=>(($total_receivable-$total_expense)/$total_receivable),
  268. ]);
  269. };
  270. }
  271. $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  272. return redirect('waybill/index')->with('successTip','运单“'.$waybill->waybill_number.'”调度成功');
  273. }
  274. }
  275. public function checkWaybillPriceModel($carrier_id,$destination_city_id,$carrier_weight,$carrier_weight_unit_id){
  276. //确保承运商计数与计数单位为一个数组且长度2
  277. if(!$carrier_id)return false;
  278. if(!$destination_city_id)return false;
  279. if(!$carrier_weight)return false;
  280. if(!$carrier_weight_unit_id)return false;
  281. //多个计数标准,计算价格,取最贵
  282. if ($carrier_weight[0]&&$carrier_weight[1]&&$carrier_weight_unit_id[0]&&$carrier_weight_unit_id[1]){
  283. //城市价格区间不为空
  284. $waybillPriceModelOne=WaybillPriceModel::where('carrier_id',$carrier_id)->where('city_id',$destination_city_id)
  285. ->where('range_min','<',$carrier_weight[0])->where('range_max','>=',$carrier_weight[0])
  286. ->where('unit_id',$carrier_weight_unit_id[0])->first();
  287. $waybillPriceModelTwo=WaybillPriceModel::where('carrier_id',$carrier_id)->where('city_id',$destination_city_id)
  288. ->where('range_min','<',$carrier_weight[1])->where('range_max','>=',$carrier_weight[1])
  289. ->where('unit_id',$carrier_weight_unit_id[1])->first();
  290. if ($waybillPriceModelOne&&$waybillPriceModelTwo){
  291. if ($waybillPriceModelOne->unit_price*$carrier_weight[0]>=$waybillPriceModelTwo->unit_price*$carrier_weight[1]){
  292. return $waybillPriceModelOne->id;
  293. }else{
  294. return $waybillPriceModelTwo->id;
  295. }
  296. }
  297. if ($waybillPriceModelOne)return $waybillPriceModelOne->id;
  298. if ($waybillPriceModelTwo)return $waybillPriceModelTwo->id;
  299. //价格区间为空
  300. $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();
  301. $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();
  302. if ($waybillPriceModelRangeOne&&$waybillPriceModelRangeTwo){
  303. if ($waybillPriceModelRangeOne->unit_price*$carrier_weight[0]>=$waybillPriceModelRangeTwo->unit_price*$carrier_weight[1]){
  304. return $waybillPriceModelRangeOne->id;
  305. }else{
  306. return $waybillPriceModelRangeTwo->id;
  307. }
  308. }
  309. if ($waybillPriceModelRangeOne)return $waybillPriceModelRangeOne->id;
  310. if ($waybillPriceModelRangeTwo)return $waybillPriceModelRangeTwo->id;
  311. //城市为空
  312. $city=City::where('id',$destination_city_id)->select('province_id')->first();
  313. $waybillPriceModelProvinceOne=WaybillPriceModel::whereRaw('carrier_id = ? AND province_id = ? AND unit_id = ? AND range_max >= ? AND range_min < ? AND city_id IS NULL',
  314. [$carrier_id,$city->province_id,$carrier_weight_unit_id[0],$carrier_weight[0],$carrier_weight[0]])->first();
  315. $waybillPriceModelProvinceTwo=WaybillPriceModel::whereRaw('carrier_id = ? AND province_id = ? AND unit_id = ? AND range_max >= ? AND range_min < ? AND city_id IS NULL',
  316. [$carrier_id,$city->province_id,$carrier_weight_unit_id[1],$carrier_weight[1],$carrier_weight[1]])->first();
  317. if ($waybillPriceModelProvinceOne&&$waybillPriceModelProvinceTwo){
  318. if ($waybillPriceModelProvinceOne->unit_price*$carrier_weight[0]>=$waybillPriceModelProvinceTwo->unit_price*$carrier_weight[1]){
  319. return $waybillPriceModelProvinceOne->id;
  320. }else{
  321. return $waybillPriceModelProvinceTwo->id;
  322. }
  323. }
  324. if ($waybillPriceModelProvinceOne)return $waybillPriceModelProvinceOne->id;
  325. if ($waybillPriceModelProvinceTwo)return $waybillPriceModelProvinceTwo->id;
  326. //城市价格区间都为空
  327. $waybillPriceModelProvinceRangeOne=WaybillPriceModel::whereRaw('carrier_id = ? AND province_id = ? AND unit_id = ? AND range_max IS NULL AND city_id IS NULL',
  328. [$carrier_id,$city->province_id,$carrier_weight_unit_id[0]])->first();
  329. $waybillPriceModelProvinceRangeTwo=WaybillPriceModel::whereRaw('carrier_id = ? AND province_id = ? AND unit_id = ? AND range_max IS NULL AND city_id IS NULL',
  330. [$carrier_id,$city->province_id,$carrier_weight_unit_id[1]])->first();
  331. if ($waybillPriceModelProvinceRangeOne&&$waybillPriceModelProvinceRangeTwo){
  332. if ($waybillPriceModelProvinceRangeOne->unit_price*$carrier_weight[0]>=$waybillPriceModelProvinceRangeTwo->unit_price*$carrier_weight[1]){
  333. return $waybillPriceModelProvinceRangeOne->id;
  334. }else{
  335. return $waybillPriceModelProvinceRangeOne->id;
  336. }
  337. }
  338. if ($waybillPriceModelProvinceRangeOne)return $waybillPriceModelProvinceRangeOne->id;
  339. if ($waybillPriceModelProvinceRangeOne)return $waybillPriceModelProvinceRangeOne->id;
  340. };
  341. for ($i=0;$i<count($carrier_weight);$i++){
  342. if ($carrier_weight[$i]&&$carrier_weight_unit_id[$i]){
  343. //城市价格区间不为空
  344. $waybillPriceModel=WaybillPriceModel::where('carrier_id',$carrier_id)->where('city_id',$destination_city_id)
  345. ->where('range_min','<',$carrier_weight[$i])->where('range_max','>=',$carrier_weight[$i])
  346. ->where('unit_id',$carrier_weight_unit_id[$i])->first();
  347. if($waybillPriceModel)return $waybillPriceModel->id;
  348. //价格区间为空
  349. $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();
  350. if ($waybillPriceModelRange){ return $waybillPriceModelRange->id;}
  351. //城市为空
  352. $city=City::where('id',$destination_city_id)->select('province_id')->first();
  353. $waybillPriceModelProvince=WaybillPriceModel::whereRaw('carrier_id = ? AND province_id = ? AND unit_id = ? AND range_max >= ? AND range_min < ? AND city_id IS NULL',
  354. [$carrier_id,$city->province_id,$carrier_weight_unit_id[$i],$carrier_weight[$i],$carrier_weight[$i]])->first();
  355. if ($waybillPriceModelProvince){return $waybillPriceModelProvince->id;}
  356. //城市价格区间都为空
  357. $waybillPriceModelProvinceRange=WaybillPriceModel::whereRaw('carrier_id = ? AND province_id = ? AND unit_id = ? AND range_max IS NULL AND city_id IS NULL',
  358. [$carrier_id,$city->province_id,$carrier_weight_unit_id[$i]])->first();
  359. if ($waybillPriceModelProvinceRange){return $waybillPriceModelProvinceRange->id;}
  360. }
  361. }
  362. return false;
  363. }
  364. /*三层条件:无优先级,找到第一个直接返回
  365. * 无论是否为KG||T,计数单位一为KG,计数单位一为T,计数单位二为KG,计数单位二为T
  366. * 计数一与计数二同时存在取最贵价格:
  367. * 计数一存在,二不存在:
  368. * 计数二存在,一不存在:
  369. * 城市价格区间不为空,城市价格区间都为空,城市为空,价格区间为空
  370. * */
  371. public function isWaybillPriceModel(Request $request){
  372. $carrier_id=$request->input('carrier_id');
  373. $destination_city_id=$request->input('destination_city_id');
  374. $carrier_weight=$request->input('carrier_weight');
  375. $carrier_weight_unit_id=$request->input('carrier_weight_unit_id');
  376. $validatorData=["carrier_id"=>$carrier_id,"destination_city_id"=>$destination_city_id,
  377. 'carrier_weight'=>$carrier_weight[0],"carrier_weight_unit_id"=>$carrier_weight_unit_id[0],
  378. "carrier_weight_other"=>$carrier_weight[1],"carrier_weight_unit_id_other"=>$carrier_weight_unit_id[1]];
  379. $errors=Validator::make($validatorData,[
  380. 'carrier_id'=>'required|integer',
  381. 'destination_city_id'=>'required|integer',
  382. 'carrier_weight'=>'nullable|min:0|numeric|max:999999',
  383. 'carrier_weight_unit_id'=>'required_with:carrier_weight',
  384. 'carrier_weight_other'=>'nullable|min:0|numeric|max:999999',
  385. 'carrier_weight_unit_id_other'=>'required_with:carrier_weight_other',
  386. ],[
  387. 'required'=>':attribute 为必填项',
  388. 'max'=>':attribute 字符过多或输入值过大',
  389. 'min'=>':attribute 不得为负',
  390. 'numeric'=>':attribute 应为数字',
  391. 'unique'=>':attribute 已存在',
  392. 'required_with'=>':attribute 未填',
  393. 'integer'=>':attribute 必须为数字',
  394. ],[
  395. 'carrier_weight'=>'承运商计数(抛)',
  396. 'carrier_id'=>'承运商',
  397. 'destination_city_id'=>'目的市',
  398. 'carrier_weight_unit_id'=>'承运商计数单位',
  399. 'carrier_weight_other'=>'承运商计数二',
  400. 'carrier_weight_unit_id_other'=>'承运商计数单位二',
  401. ])->errors();
  402. if (count($errors)>0)return ['error'=>$errors];
  403. $result=$this->checkWaybillPriceModel($carrier_id,$destination_city_id,$carrier_weight,$carrier_weight_unit_id);
  404. if (!$result){
  405. //单位为kg,T时
  406. $unitKG=Unit::where('name','kg')->first();
  407. $unitT=Unit::where('name','T')->first();
  408. if ($carrier_weight_unit_id[0]==$unitKG->id){
  409. $carrier_weight_unit_id[0]=$unitT->id;
  410. $carrier_weight[0]=$carrier_weight[0]/1000;
  411. $result=$this->checkWaybillPriceModel($carrier_id,$destination_city_id,$carrier_weight,$carrier_weight_unit_id);
  412. if ($result)return ['success'=>$result];
  413. }
  414. if ($carrier_weight_unit_id[1]==$unitKG->id){
  415. $carrier_weight_unit_id[1]=$unitT->id;
  416. $carrier_weight[1]=$carrier_weight[1]/1000;
  417. $result=$this->checkWaybillPriceModel($carrier_id,$destination_city_id,$carrier_weight,$carrier_weight_unit_id);
  418. if ($result)return ['success'=>$result];
  419. }
  420. if ($carrier_weight_unit_id[0]==$unitT->id){
  421. $carrier_weight_unit_id[0]=$unitKG->id;
  422. $carrier_weight[0]=$carrier_weight[0]*1000;
  423. $result=$this->checkWaybillPriceModel($carrier_id,$destination_city_id,$carrier_weight,$carrier_weight_unit_id);
  424. if ($result)return ['success'=>$result];
  425. }
  426. if ($carrier_weight_unit_id[1]==$unitT->id){
  427. $carrier_weight_unit_id[1]=$unitKG->id;
  428. $carrier_weight[1]=$carrier_weight[1]*1000;
  429. $result=$this->checkWaybillPriceModel($carrier_id,$destination_city_id,$carrier_weight,$carrier_weight_unit_id);
  430. if ($result)return ['success'=>$result];
  431. }
  432. }
  433. return ['success'=>$result];
  434. }
  435. public function waybillUpdate(Request $request, $id){
  436. if(!Gate::allows('运输管理-编辑')){ return redirect(url('/')); }
  437. $this->validatorWaybill($request,$id)->validate();
  438. $data=$request->input();
  439. $waybill=Waybill::find($id);
  440. $waybill->fill($data);
  441. if ($waybill->save()){
  442. $this->log(__METHOD__,__FUNCTION__,json_encode($waybill),Auth::user()['id']);
  443. return redirect('waybill/index')->with('successTip','运单“'.$waybill->waybill_number.'”修改成功');
  444. }
  445. }
  446. public function waybillAudit(Request $request){
  447. if(!Gate::allows('运输管理-运单审核')){ return redirect(url('/')); }
  448. $id=$request->input('id');
  449. $waybill=Waybill::find($id);
  450. $isAudit=WaybillAuditLog::whereRaw('waybill_id = ? and audit_stage = ?',[$id,"运单阶段"])->first();
  451. if (empty($isAudit)){
  452. $waybillAuditLog=new WaybillAuditLog([
  453. 'waybill_id'=>$id,
  454. 'audit_stage'=>'运单阶段',
  455. 'user_id'=>Auth::id(),
  456. ]);
  457. $waybillAuditLog->save();
  458. $waybillAuditLog['user']=Auth::user();
  459. $waybill->status='已审核';
  460. $result=$waybill->save();
  461. $this->log(__METHOD__,__FUNCTION__,json_encode($waybill),Auth::user()['id']);
  462. return ['success'=>$result,'status'=>$waybill->status,'waybillAuditLog'=>$waybillAuditLog];
  463. }
  464. return ['exception'=>'请勿重复审核!'];
  465. }
  466. public function waybillEdit($id){
  467. if(!Gate::allows('运输管理-编辑')){ return redirect(url('/')); }
  468. $waybill=Waybill::find($id);
  469. $owners=Owner::get();
  470. return view('waybill.waybillEdit',['waybill'=>$waybill,'owners'=>$owners]);
  471. }
  472. public function waybillRetreatAudit(Request $request){
  473. if(!Gate::allows('运输管理-调度')){ return redirect(url('/')); }
  474. $id=$request->input('id');
  475. $waybill=Waybill::find($id);
  476. WaybillAuditLog::whereRaw('waybill_id = ? and audit_stage = ?',[$id,"运单阶段"])->delete();
  477. $waybill->status='待重审';
  478. $result=$waybill->save();
  479. $this->log(__METHOD__,__FUNCTION__,json_encode($waybill),Auth::user()['id']);
  480. return ['success'=>$result,'status'=>$waybill->status];
  481. }
  482. public function waybillEndAudit(Request $request){
  483. if(!Gate::allows('运输管理-调度审核')){ return redirect(url('/')); }
  484. $id=$request->input('id');
  485. $waybill=Waybill::find($id);
  486. if (!$waybill->charge&&!$waybill->collect_fee)return ['exception'=>'收费或到付费用未填!'];
  487. if ($waybill->charge==0&&$waybill->collect_fee==0)return ['exception'=>'收费与到付费用都为0!'];
  488. if ($waybill->type=='专线'){
  489. if (!$waybill->carrier_weight||$waybill->carrier_weight==0)return ['exception'=>'承运商计重未填或为0!'];
  490. if (!$waybill->carrier_weight_unit_id)return ['exception'=>'承运商计重单位未选!'];
  491. }
  492. $isAudit=WaybillAuditLog::whereRaw('waybill_id = ? and audit_stage = ?',[$id,"调度阶段"])->first();
  493. if (empty($isAudit)){
  494. $waybillAuditLog=new WaybillAuditLog([
  495. 'waybill_id'=>$id,
  496. 'audit_stage'=>'调度阶段',
  497. 'user_id'=>Auth::id(),
  498. ]);
  499. $waybillAuditLog->save();
  500. $waybillAuditLog['user']=Auth::user();
  501. if ($waybill->waybill_price_model_id||$waybill->type=='直发车'){
  502. $waybill->status='已完结';
  503. $result=$waybill->save();
  504. $waybillPayoff=WaybillPayoff::where('waybill_id','=',$id)->first();
  505. $waybillPayoff->load(["waybill"]);
  506. $waybillPayoff->waybill->load(["owner","carrier","origination_city","destination_city","carType","waybillAuditLogs"]);
  507. $waybillPayoff->waybill->waybillAuditLogs->load(["user"]);
  508. $waybillPayoffJson=json_encode($waybillPayoff,JSON_UNESCAPED_UNICODE);
  509. WaybillFinancialSnapshot::create([
  510. 'waybill_id'=>$id,
  511. 'json_content'=>$waybillPayoffJson,
  512. ]);
  513. }else{
  514. $waybill->status='无模型';
  515. $result=$waybill->save();
  516. $waybillPayoff=WaybillPayoff::where('waybill_id','=',$id)->first();
  517. if ($waybillPayoff){
  518. $waybillPayoff->load(["waybill"]);
  519. $waybillPayoff->waybill->load(["owner","carrier","origination_city","destination_city","carType","waybillAuditLogs"]);
  520. $waybillPayoff->waybill->waybillAuditLogs->load(["user"]);
  521. $waybillPayoffJson=json_encode($waybillPayoff,JSON_UNESCAPED_UNICODE);
  522. WaybillFinancialExcepted::create([
  523. 'waybill_id'=>$id,
  524. 'json_content'=>$waybillPayoffJson,
  525. ]);
  526. }
  527. }
  528. $this->log(__METHOD__,__FUNCTION__,$waybillPayoffJson,Auth::user()['id']);
  529. return ['success'=>$result,'status'=>$waybill->status,'waybillAuditLog'=>$waybillAuditLog];
  530. }
  531. return ['exception'=>'请勿重复审核!'];
  532. }
  533. public function waybillExport($id,Request $request){
  534. if(!Gate::allows('运输管理-查询')){ return '没有权限'; }
  535. if ($id==-1){
  536. $id=[];
  537. $today=Carbon::now()->subDays(15);
  538. ini_set('max_execution_time',2500);
  539. ini_set('memory_limit','1526M');
  540. $waybills=Waybill::select('id');
  541. if ($request->input('waybill_number')){
  542. $waybills =$waybills->where('waybill_number','like','%'.$request->input('waybill_number').'%')->where('created_at','>',$today->format('Y-m-d'));
  543. }
  544. if ($request->input('carrier_bill')){
  545. $waybills=$waybills->where('carrier_bill','like','%'.$request->input('carrier_bill').'%')->where('created_at','>',$today->format('Y-m-d'));
  546. }
  547. if ($request->input('carrier_id')){
  548. $waybills=$waybills->where('carrier_id','=',$request->input('carrier_id'));
  549. }
  550. if ($request->input('owner_id')){
  551. $waybills=$waybills->where('owner_id','=',$request->input('owner_id'));
  552. }
  553. if ($request->input('wms_bill_number')){
  554. $waybills=$waybills->where('wms_bill_number','like','$'.$request->input('wms_bill_number').'%')->where('created_at','>',$today->format('Y-m-d'));
  555. }
  556. if ($request->input('created_at_start')){
  557. $waybills=$waybills->where('created_at','>',$request->input('created_at_start'));
  558. }
  559. if ($request->input('created_at_end')){
  560. $waybills=$waybills->where('created_at','<',$request->input('created_at_end'));
  561. }
  562. if ($request->input('status')){
  563. $waybills=$waybills->where('status',$request->input('status'));
  564. }
  565. $waybills=$waybills->get();
  566. foreach ($waybills as $waybill){
  567. array_push($id,$waybill->id);
  568. }
  569. }else $id = explode( ',',$id);
  570. if (!$id)return ;
  571. $row=[[
  572. 'type'=>'运单类型',
  573. 'owner'=>'货主',
  574. 'source_bill'=>'上游单号',
  575. 'wms_bill_number'=>'wms订单号',
  576. 'waybill_number'=>'运单号',
  577. 'origination'=>'始发地',
  578. 'destination'=>'目的地',
  579. 'carrier'=>'承运商',
  580. 'carrier_bill'=>'承运商单号',
  581. 'warehouse_weight'=>'仓库计数(抛)',
  582. 'carrier_weight'=>'承运商计数(抛)',
  583. 'warehouse_weight_other'=>'仓库计重',
  584. 'carrier_weight_other'=>'承运商计重',
  585. 'fee'=>'运费(元)',
  586. 'pick_up_fee'=>'提货费(元)',
  587. 'other_fee'=>'其他费用(元)',
  588. 'dispatch_remark'=>'调度备注',
  589. 'created_at'=>'创建时间'
  590. ]];
  591. $feeVisible=true;
  592. if(!Gate::allows('运输管理-可见费用项')){
  593. $feeVisible=false;
  594. unset($row[0]['fee'],$row[0]['pick_up_fee'],$row[0]['other_fee'],$row[0]['collect_fee']);
  595. }
  596. $list=[];
  597. for ($i=0; $i<count($id);$i++){
  598. $waybill=Waybill::with(['owner', 'waybillAuditLogs' => function ($query) {
  599. return $query->with('user');
  600. }])->find($id[$i]);
  601. foreach ($waybill->waybillAuditLogs as $waybillAuditLog){
  602. if ($waybillAuditLog->audit_stage=="运单阶段"){
  603. $waybillAuditor=$waybillAuditLog->user->name;
  604. }else{
  605. $waybillAuditor='';
  606. }
  607. if ($waybillAuditLog->audit_stage=="调度阶段"){
  608. $dispatchAuditor=$waybillAuditLog->user->name;
  609. }else{
  610. $dispatchAuditor='';
  611. }
  612. };
  613. $w=[
  614. 'type'=>isset($waybill->type)?$waybill->type:'',
  615. 'waybill_number'=>isset($waybill->waybill_number)?$waybill->waybill_number:'',
  616. 'owner'=>isset($waybill->owner->name)?$waybill->owner->name:'',
  617. 'source_bill'=>isset($waybill->source_bill)?$waybill->source_bill:'',
  618. 'wms_bill_number'=>isset($waybill->wms_bill_number)?$waybill->wms_bill_number:'',
  619. 'origination'=>isset($waybill->origination)?$waybill->origination:'',
  620. 'destination'=>isset($waybill->destination)?$waybill->destination:'',
  621. 'recipient'=>isset($waybill->recipient)?$waybill->recipient:'',
  622. 'recipient_mobile'=>isset($waybill->recipient_mobile)?$waybill->recipient_mobile:'',
  623. //'charge'=>isset($waybill->charge)?$waybill->charge:'',
  624. //'ordering_remark'=>isset($waybill->ordering_remark)?$waybill->ordering_remark:'',
  625. 'carrier'=>isset($waybill->carrier_name)?$waybill->carrier_name:'',
  626. 'carrier_bill'=>isset($waybill->carrier_bill)?$waybill->carrier_bill:'',
  627. //'origination_city'=>isset($waybill->origination_city_name)?$waybill->origination_city_name:'',
  628. //'destination_city'=>isset($waybill->destination_city_name)?$waybill->destination_city_name:'',
  629. 'warehouse_weight'=>isset($waybill->warehouse_weight)?$waybill->warehouse_weight.' '.(isset($waybill->warehouse_weight_unit_name)?$waybill->warehouse_weight_unit_name:''):'',
  630. '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:''):'',
  631. 'carrier_weight'=>isset($waybill->carrier_weight)?$waybill->carrier_weight.' '.(isset($waybill->carrier_weight_unit_name)?$waybill->carrier_weight_unit_name:''):'',
  632. '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:''):'',
  633. //'carType'=>isset($waybill->carType->name)?$waybill->carType->name.($waybill->carType->length.'米'):'',
  634. //'car_owner_info'=>isset($waybill->car_owner_info)?$waybill->car_owner_info:'',
  635. 'fee'=>isset($waybill->fee)?$waybill->fee:'',
  636. 'pick_up_fee'=>isset($waybill->pick_up_fee)?$waybill->pick_up_fee:'',
  637. 'other_fee'=>isset($waybill->other_fee)?$waybill->other_fee:'',
  638. //'collect_fee'=>isset($waybill->collect_fee)?$waybill->collect_fee:'',
  639. 'dispatch_remark'=>isset($waybill->dispatch_remark)?$waybill->dispatch_remark:'',
  640. //'waybillAuditor'=>isset($waybillAuditor)?$waybillAuditor:'',
  641. //'dispatchAuditor'=>isset($dispatchAuditor)?$dispatchAuditor:'',
  642. 'created_at'=>isset($waybill->created_at)?$waybill->created_at:''
  643. ];
  644. if(!$feeVisible){
  645. unset($w['fee'],$w['pick_up_fee'],$w['other_fee'],$w['collect_fee']);
  646. }
  647. $list[$i]=$w;
  648. }
  649. $this->log(__METHOD__,__FUNCTION__,json_encode($waybill),Auth::user()['id']);
  650. return Excel::download(new Export($row,$list), date('Y:m:d ') . '运单列表.xlsx');
  651. }
  652. protected function validatorWaybill(Request $request,$id){
  653. if ($id){$wms_bill_number=$id;};
  654. $validator=Validator::make($request->input(),[
  655. 'owner_id'=>'required',
  656. 'wms_bill_number'=>['nullable','max:50',isset($wms_bill_number)?"unique:waybills,wms_bill_number,$wms_bill_number":'unique:waybills,wms_bill_number'],
  657. 'origination'=>'required|max:255',
  658. 'destination'=>'required|max:255',
  659. 'recipient'=>'required|max:50',
  660. 'recipient_mobile'=>['required','regex:/^(\d{7,11})|(1[3|4|5|7|8][0-9]\d{4,8})$/'],
  661. 'charge'=>'nullable|min:0|max:999999|numeric',
  662. 'collect_fee'=>'nullable|min:0|numeric',
  663. ],[
  664. 'required'=>':attribute 为必填项',
  665. 'alpha_num'=>':attribute 应为字母或数字',
  666. 'max'=>':attribute 字符过多或输入值过大',
  667. 'regex'=>':attribute 输入有误',
  668. 'integer'=>':attribute 应为整数',
  669. 'min'=>':attribute 不得为负',
  670. 'numeric'=>':attribute 应为数字',
  671. 'unique'=>':attribute 已存在',
  672. ],[
  673. 'owner_id'=>'货主',
  674. 'wms_bill_number'=>'WMS单号',
  675. 'origination'=>'始发地',
  676. 'destination'=>'目的地',
  677. 'recipient'=>'收件人',
  678. 'recipient_mobile'=>'收件人电话',
  679. 'charge'=>'收费',
  680. 'collect_fee'=>'到付金额',
  681. ]);
  682. return $validator;
  683. }
  684. protected function validatorWaybillDispatch(Request $request,$id){
  685. if ($request->input('type')=='直发车'){
  686. $validator=Validator::make($request->input(),[
  687. 'carrier_bill'=>"nullable|max:50|unique:waybills,carrier_bill,$id",
  688. 'fee'=>'required|min:0|numeric|max:999999',
  689. 'other_fee'=>'nullable|min:0|numeric|max:999999',
  690. 'charge'=>'nullable|min:0|numeric|max:999999',
  691. ],[
  692. 'required'=>':attribute 为必填项',
  693. 'alpha_num'=>':attribute 应为字母或数字',
  694. 'max'=>':attribute 字符过多或输入值过大',
  695. 'min'=>':attribute 不得为负',
  696. 'numeric'=>':attribute 应为数字',
  697. 'unique'=>':attribute 已存在',
  698. ],[
  699. 'carrier_bill'=>'承运商单号',
  700. 'fee'=>'运费',
  701. 'other_fee'=>'其他费用',
  702. 'charge'=>'收费',
  703. ]);
  704. return $validator;
  705. }else if ($request->input('type')=='专线'){
  706. return $this->validatorWaybillZX($request,$id);
  707. }else{
  708. return false;
  709. }
  710. }
  711. protected function validatorWaybillZX(Request $request,$id){
  712. $validator=Validator::make($request->input(),[
  713. 'carrier_bill'=>"nullable|max:50|unique:waybills,carrier_bill,$id",
  714. 'pick_up_fee'=>'nullable|min:0|numeric|max:999999',
  715. 'other_fee'=>'nullable|min:0|numeric|max:999999',
  716. 'carrier_id'=>'required|integer',
  717. 'destination_city_id'=>'required|integer',
  718. 'warehouse_weight'=>'nullable|min:0|numeric|max:999999',
  719. 'warehouse_weight_unit_id'=>'required_with:warehouse_weight|nullable|integer',
  720. 'warehouse_weight_other'=>'nullable|min:0|numeric|max:999999',
  721. 'warehouse_weight_unit_id_other'=>'required_with:warehouse_weight_other|nullable|integer',
  722. 'charge'=>'nullable|min:0|numeric|max:999999',
  723. 'carrier_weight'=>'nullable|min:0|numeric|max:999999',
  724. 'carrier_weight_unit_id'=>'required_with:carrier_weight',
  725. 'carrier_weight_other'=>'nullable|min:0|numeric|max:999999',
  726. 'carrier_weight_unit_id_other'=>'required_with:carrier_weight_other',
  727. ],[
  728. 'required'=>':attribute 为必填项',
  729. 'alpha_num'=>':attribute 应为字母或数字',
  730. 'max'=>':attribute 字符过多或输入值过大',
  731. 'min'=>':attribute 不得为负',
  732. 'numeric'=>':attribute 应为数字',
  733. 'unique'=>':attribute 已存在',
  734. 'required_with'=>':attribute 未填',
  735. 'integer'=>':attribute 必须为数字',
  736. ],[
  737. 'carrier_bill'=>'承运商单号',
  738. 'warehouse_weight'=>'仓库计数(抛)',
  739. 'carrier_weight'=>'承运商计数(抛)',
  740. 'pick_up_fee'=>'提货费',
  741. 'other_fee'=>'其他费用',
  742. 'carrier_id'=>'承运商',
  743. 'destination_city_id'=>'目的市',
  744. 'carrier_weight_unit_id'=>'承运商计数单位',
  745. 'charge'=>'收费',
  746. 'warehouse_weight_unit_id'=>'仓库计数单位',
  747. 'warehouse_weight_other'=>'仓库计数二',
  748. 'carrier_weight_other'=>'承运商计数二',
  749. 'warehouse_weight_unit_id_other'=>'仓库技数单位二',
  750. 'carrier_weight_unit_id_other'=>'承运商计数单位二',
  751. ]);
  752. return $validator;
  753. }
  754. public function addCounty(Request $request){
  755. $rule =[
  756. 'destination_city' => ['required', 'string','unique:cities,name'],
  757. ];
  758. $messages=[
  759. 'destination_city.required'=>'市/县不能为空',
  760. 'destination_city.string'=>'市/县必须是字符串',
  761. 'destination_city.unique'=>'市/县已存在',
  762. ];
  763. $validator = Validator::make($request->all(),$rule,$messages);
  764. if ($validator->fails()) {
  765. return $validator->errors();
  766. }
  767. $city=new City([
  768. 'name'=>$request->input('destination_city'),
  769. 'type'=>3,
  770. ]);
  771. $city->save();
  772. return $city;
  773. }
  774. }