WaybillsController.php 41 KB

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