| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995 |
- <?php
- namespace App\Http\Controllers;
- use App\Http\Controllers\Api\thirdPart\flux\WaybillController;
- use App\User;
- use App\WaybillAuditLog;
- use App\WaybillOnTop;
- use App\WaybillPriceModel;
- use App\Carrier;
- use App\CarType;
- use App\City;
- use App\Exports\Export;
- use App\Owner;
- use App\Unit;
- use App\Waybill;
- use App\WaybillPayoff;
- use App\WaybillFinancialExcepted;
- use App\WaybillFinancialSnapshot;
- use Carbon\Carbon;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Auth;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Gate;
- use Illuminate\Support\Facades\Validator;
- use Maatwebsite\Excel\Facades\Excel;
- use Ramsey\Uuid\Uuid;
- class WaybillsController extends Controller
- {
- //超15天精确查询抽离 column前提:数据库字段名必须与request内字段名一致
- public function preciseQuery(string $column,Request $request,$waybills){
- $today=Carbon::now()->subDays(15);
- $waybillsTem=clone $waybills;
- $waybillsTem=$waybillsTem->where($column,'like','%'.$request->input($column).'%')->where('created_at','>',$today->format('Y-m-d'));
- if($waybillsTem->count()==0
- ||$waybillsTem->first()[$column]==$request->input($column)){
- $waybills=$waybills->where($column,$request->input($column));
- }else{
- $waybills=$waybillsTem;
- }
- return $waybills;
- }
- public function conditionQuery(Request $request,$waybills){
- if ($request->input('exportType')&&$request->input('exportType')==1){
- $checkData=explode(',',$request->input('checkData'));
- $waybills=$waybills->whereIn("id",$checkData)->get();
- $excel=$this->deliveringExport($waybills);
- return $excel;
- }
- if ($request->input('waybill_number')){
- $waybills=$this->preciseQuery("waybill_number",$request,$waybills);
- }
- if ($request->input('carrier_bill')){
- $waybills=$this->preciseQuery("carrier_bill",$request,$waybills);
- }
- if ($request->input('carrier_id')){
- $waybills=$waybills->where('carrier_id','=',$request->input('carrier_id'));
- }
- if ($request->input('owners')){
- $owners=array_values(json_decode($request->input('owners'),true));
- $waybills=$waybills->whereIn('owner_id',$owners);
- }
- if ($request->input('owner_id')){
- $waybills=$waybills->where('owner_id','=',$request->input('owner_id'));
- }
- if ($request->input('wms_bill_number')){
- $waybills=$this->preciseQuery("wms_bill_number",$request,$waybills);
- }
- if ($request->input('origination')){
- $waybills=$this->preciseQuery("origination",$request,$waybills);
- }
- if ($request->input('destination')){
- $waybills=$this->preciseQuery("destination",$request,$waybills);
- }
- if ($request->input('created_at_start')){
- $created_at_start=$request->input('created_at_start')." 00:00:00";
- $waybills=$waybills->where('created_at','>=',$created_at_start);
- }
- if ($request->input('created_at_end')){
- $created_at_end=$request->input('created_at_end')." 23:59:59";
- $waybills=$waybills->where('created_at','<=',$created_at_end);
- }
- if ($request->input('status')){
- $waybills=$waybills->where('status',$request->input('status'));
- }
- if ($request->input('exportType')&&$request->input('exportType')==2){
- $waybills=$waybills->get();
- $excel=$this->deliveringExport($waybills);
- return $excel;
- }
- else $waybills=$waybills->paginate($request->input('paginate')?$request->input('paginate'):50);
- if (!$waybills&&$request->input('waybill_number')){
- $waybills=Waybill::with(['owner', 'waybillAuditLogs' => function ($query) {
- return $query->with('user');
- }])->orderBy('id','DESC')->where('type','专线')->where('waybill_number',$request->input('waybill_number'))
- ->paginate($request->input('paginate')?$request->input('paginate'):50);
- }
- return $waybills;
- }
- public function index(Request $request)
- {
- if(!Gate::allows('运输管理-查询')){ return redirect(url('/')); }
- $data=$request->input();
- if ($data != null ) {
- $waybills = $this->getWaybills();
- $waybills=$this->conditionQuery($request,$waybills);
- $carries = Carrier::get();
- $owners = Owner::get();
- return view('waybill.index', ['waybills' => $waybills, 'carriers' => $carries, 'owners' => $owners,'filterData'=>$data,'uriType'=>'']);
- } else {
- $waybills = $this->getWaybills()->paginate(50);
- $carries = Carrier::get();
- $owners = Owner::get();
- return view('waybill.index', ['waybills' => $waybills, 'carriers' => $carries, 'owners' => $owners,'filterData'=>$data,'uriType'=>'']);
- }
- }
- public function indexZF(Request $request){
- if(!Gate::allows('运输管理-查询')){ return redirect(url('/')); }
- $data=$request->input();
- if ($data != null ) {
- $waybills = $this->getWaybills()->where('type','直发车');
- $waybills=$this->conditionQuery($request,$waybills);
- $carries = Carrier::get();
- $owners = Owner::get();
- return view('waybill.index', ['waybills' => $waybills, 'carriers' => $carries, 'owners' => $owners,'filterData'=>$data,'uriType'=>'ZF']);
- } else {
- $waybills = $this->getWaybills()->where('type','直发车')->paginate(50);
- $carries = Carrier::get();
- $owners = Owner::get();
- return view('waybill.index', ['waybills' => $waybills, 'carriers' => $carries, 'owners' => $owners,'filterData'=>$data,'uriType'=>'ZF']);
- }
- }
- public function indexZX(Request $request){
- if(!Gate::allows('运输管理-查询')){ return redirect(url('/')); }
- $data=$request->input();
- if ($data != null ) {
- $waybills = $this->getWaybills()->where('type','专线');
- $waybills=$this->conditionQuery($request,$waybills);
- $carries = Carrier::get();
- $owners = Owner::get();
- return view('waybill.index', ['waybills' => $waybills, 'carriers' => $carries, 'owners' => $owners,'filterData'=>$data,'uriType'=>'ZX']);
- } else {
- $waybills = $this->getWaybills()->where('type','专线')->paginate(50);
- $carries = Carrier::get();
- $owners = Owner::get();
- return view('waybill.index', ['waybills' => $waybills, 'carriers' => $carries, 'owners' => $owners,'filterData'=>$data,'uriType'=>'ZX']);
- }
- }
- public function create()
- {
- if(!Gate::allows('运输管理-录入')){ return redirect(url('/')); }
- $owners=Owner::get();
- return view('waybill.create',['owners'=>$owners]);
- }
- public function createZF()
- {
- if(!Gate::allows('运输管理-录入')){ return redirect(url('/')); }
- $owners=Owner::get();
- return view('waybill.create',['owners'=>$owners,'type'=>'直发车']);
- }
- public function createZX()
- {
- if(!Gate::allows('运输管理-录入')){ return redirect(url('/')); }
- $owners=Owner::get();
- return view('waybill.create',['owners'=>$owners,'type'=>'专线']);
- }
- public function store(Request $request)
- {
- if(!Gate::allows('运输管理-录入')){ return redirect(url('/')); }
- $id=false;
- $this->validatorWaybill($request,$id)->validate();
- $data=$request->input();
- $waybill=new Waybill([
- 'type'=>$data['type'],
- 'status'=>'未审核',
- 'waybill_number'=>Uuid::uuid1(),
- 'owner_id'=>$data['owner_id'],
- 'wms_bill_number'=>$data['wms_bill_number'],
- 'origination'=>$data['origination'],
- 'destination'=>$data['destination'],
- 'recipient'=>$data['recipient'],
- 'recipient_mobile'=>$data['recipient_mobile'],
- 'charge'=>$data['charge'],
- 'ordering_remark'=>$data['ordering_remark']
- ]);
- if (isset($data['collect_fee'])&&$data['collect_fee']>0)$waybill->collect_fee=$data['collect_fee'];
- $waybill->save();
- $number_id=$waybill->id;
- if ($data['type']=='直发车'){
- $waybill_number='BSZF'.date ("ymd").str_pad($number_id>99999?$number_id%99999:$number_id,4,"0",STR_PAD_LEFT);
- $waybill->waybill_number=$waybill_number;
- $waybill->update();
- }else{
- $waybill_number='BSZX'.date ("ymd").str_pad($number_id>99999?$number_id%99999:$number_id,4,"0",STR_PAD_LEFT);
- $waybill->waybill_number=$waybill_number;
- $waybill->update();
- }
- $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
- return redirect('waybill/index')->with('successTip','新运单“'.$waybill_number.'”录入成功');
- }
- public function edit($id)
- {
- if(!Gate::allows('运输管理-编辑')){ return redirect(url('/')); }
- $waybill=Waybill::find($id);
- $carriers=Carrier::get();
- $cities=City::get();
- $units=Unit::get();
- $carTypes=CarType::get();
- return view('waybill/edit',['waybill'=>$waybill,'carriers'=>$carriers,'cities'=>$cities,'units'=>$units,'carTypes'=>$carTypes]);
- }
- public function update(Request $request, $id)
- {
- if(!Gate::allows('运输管理-调度')){ return redirect(url('/')); }
- $waybill=Waybill::find($id);
- if (!$waybill['warehouse_weight']&&$waybill['warehouse_weight_unit_id']){
- unset($waybill['warehouse_weight_unit_id']);
- }
- if (!$waybill['warehouse_weight_other']&&$waybill['warehouse_weight_unit_id_other']){
- unset($waybill['warehouse_weight_unit_id_other']);
- }
- if (!$waybill['carrier_weight']&&$waybill['carrier_weight_unit_id']){
- unset($waybill['carrier_weight_unit_id']);
- }
- if (!$waybill['carrier_weight_other']&&$waybill['carrier_weight_unit_id_other']){
- unset($waybill['carrier_weight_unit_id_other']);
- }
- $this->validatorWaybillDispatch($request,$id)->validate();
- $data=$request->input();
- $total_receivable=0;
- $waybill->fill($data);
- if ($waybill->save()){
- if ($waybill->type=="直发车"){
- if ($waybill->charge)$total_receivable=($waybill->charge);
- elseif ($waybill->collect_fee)$total_receivable=($waybill->collect_fee);
- $total_expense=($waybill->fee)+($waybill->other_fee)-($waybill->collect_fee);
- }else {
- $waybillPriceModel_id=$request->input('waybillPriceModel');
- if ($waybillPriceModel_id){
- $carrier_weight=$request->input('carrier_weight');
- $waybillPriceModel=WaybillPriceModel::find($waybillPriceModel_id);
- $carrier=Carrier::find($waybill->carrier_id);
- if ($carrier_weight<$waybillPriceModel->initial_weight){
- $fee=(($waybillPriceModel->unit_price)*($waybillPriceModel->initial_weight))+$carrier->delivery_fee;
- }else{
- $fee=(($waybillPriceModel->unit_price)*$carrier_weight)+$carrier->delivery_fee;
- }
- if ($waybillPriceModel->base_fee&&$fee<$waybillPriceModel->base_fee){
- $fee=$waybillPriceModel->base_fee;
- }
- $waybill->fee=$fee;
- $waybill->waybill_price_model_id=$waybillPriceModel_id;
- }
- $waybill->save();
- if ($waybill->charge)$total_receivable=($waybill->charge);
- elseif($waybill->collect_fee) {
- $total_receivable=($waybill->collect_fee);
- }
- $total_expense=($waybill->pick_up_fee)+($waybill->other_fee)+($waybill->fee);
- }
- if ($total_receivable>0){
- $waybillPayoff=WaybillPayoff::where('waybill_id','=',$id)->first();
- if ($waybillPayoff){
- $waybillPayoff->waybill_id=$id;
- $waybillPayoff->total_expense=$total_expense;
- $waybillPayoff->total_receivable=$total_receivable;
- $waybillPayoff->gross_margin=$total_receivable-$total_expense;
- $waybillPayoff->gross_profit_rate=(($total_receivable-$total_expense)/$total_receivable);
- $waybillPayoff->save();
- }else{
- WaybillPayoff::create([
- 'waybill_id'=>$id,
- 'total_expense'=>$total_expense,
- 'total_receivable'=>$total_receivable,
- 'gross_margin'=>$total_receivable-$total_expense,
- 'gross_profit_rate'=>(($total_receivable-$total_expense)/$total_receivable),
- ]);
- };
- }
- $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
- return redirect('waybill/index')->with('successTip','运单“'.$waybill->waybill_number.'”调度成功');
- }
- }
- public function checkWaybillPriceModel($carrier_id,$destination_city_id,$carrier_weight,$carrier_weight_unit_id){
- //确保承运商计数与计数单位为一个数组且长度2
- if(!$carrier_id)return false;
- if(!$destination_city_id)return false;
- if(!$carrier_weight)return false;
- if(!$carrier_weight_unit_id)return false;
- //多个计数标准,计算价格,取最贵
- if ($carrier_weight[0]&&$carrier_weight[1]&&$carrier_weight_unit_id[0]&&$carrier_weight_unit_id[1]){
- //城市价格区间不为空
- $waybillPriceModelOne=WaybillPriceModel::where('carrier_id',$carrier_id)->where('city_id',$destination_city_id)
- ->where('range_min','<',$carrier_weight[0])->where('range_max','>=',$carrier_weight[0])
- ->where('unit_id',$carrier_weight_unit_id[0])->first();
- $waybillPriceModelTwo=WaybillPriceModel::where('carrier_id',$carrier_id)->where('city_id',$destination_city_id)
- ->where('range_min','<',$carrier_weight[1])->where('range_max','>=',$carrier_weight[1])
- ->where('unit_id',$carrier_weight_unit_id[1])->first();
- if ($waybillPriceModelOne&&$waybillPriceModelTwo){
- if ($waybillPriceModelOne->unit_price*$carrier_weight[0]>=$waybillPriceModelTwo->unit_price*$carrier_weight[1]){
- return $waybillPriceModelOne->id;
- }else{
- return $waybillPriceModelTwo->id;
- }
- }
- if ($waybillPriceModelOne)return $waybillPriceModelOne->id;
- if ($waybillPriceModelTwo)return $waybillPriceModelTwo->id;
- //价格区间为空
- $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();
- $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();
- if ($waybillPriceModelRangeOne&&$waybillPriceModelRangeTwo){
- if ($waybillPriceModelRangeOne->unit_price*$carrier_weight[0]>=$waybillPriceModelRangeTwo->unit_price*$carrier_weight[1]){
- return $waybillPriceModelRangeOne->id;
- }else{
- return $waybillPriceModelRangeTwo->id;
- }
- }
- if ($waybillPriceModelRangeOne)return $waybillPriceModelRangeOne->id;
- if ($waybillPriceModelRangeTwo)return $waybillPriceModelRangeTwo->id;
- //城市为空
- $city=City::where('id',$destination_city_id)->select('province_id')->first();
- $waybillPriceModelProvinceOne=WaybillPriceModel::whereRaw('carrier_id = ? AND province_id = ? AND unit_id = ? AND range_max >= ? AND range_min < ? AND city_id IS NULL',
- [$carrier_id,$city->province_id,$carrier_weight_unit_id[0],$carrier_weight[0],$carrier_weight[0]])->first();
- $waybillPriceModelProvinceTwo=WaybillPriceModel::whereRaw('carrier_id = ? AND province_id = ? AND unit_id = ? AND range_max >= ? AND range_min < ? AND city_id IS NULL',
- [$carrier_id,$city->province_id,$carrier_weight_unit_id[1],$carrier_weight[1],$carrier_weight[1]])->first();
- if ($waybillPriceModelProvinceOne&&$waybillPriceModelProvinceTwo){
- if ($waybillPriceModelProvinceOne->unit_price*$carrier_weight[0]>=$waybillPriceModelProvinceTwo->unit_price*$carrier_weight[1]){
- return $waybillPriceModelProvinceOne->id;
- }else{
- return $waybillPriceModelProvinceTwo->id;
- }
- }
- if ($waybillPriceModelProvinceOne)return $waybillPriceModelProvinceOne->id;
- if ($waybillPriceModelProvinceTwo)return $waybillPriceModelProvinceTwo->id;
- //城市价格区间都为空
- $waybillPriceModelProvinceRangeOne=WaybillPriceModel::whereRaw('carrier_id = ? AND province_id = ? AND unit_id = ? AND range_max IS NULL AND city_id IS NULL',
- [$carrier_id,$city->province_id,$carrier_weight_unit_id[0]])->first();
- $waybillPriceModelProvinceRangeTwo=WaybillPriceModel::whereRaw('carrier_id = ? AND province_id = ? AND unit_id = ? AND range_max IS NULL AND city_id IS NULL',
- [$carrier_id,$city->province_id,$carrier_weight_unit_id[1]])->first();
- if ($waybillPriceModelProvinceRangeOne&&$waybillPriceModelProvinceRangeTwo){
- if ($waybillPriceModelProvinceRangeOne->unit_price*$carrier_weight[0]>=$waybillPriceModelProvinceRangeTwo->unit_price*$carrier_weight[1]){
- return $waybillPriceModelProvinceRangeOne->id;
- }else{
- return $waybillPriceModelProvinceRangeOne->id;
- }
- }
- if ($waybillPriceModelProvinceRangeOne)return $waybillPriceModelProvinceRangeOne->id;
- if ($waybillPriceModelProvinceRangeOne)return $waybillPriceModelProvinceRangeOne->id;
- };
- for ($i=0;$i<count($carrier_weight);$i++){
- if ($carrier_weight[$i]&&$carrier_weight_unit_id[$i]){
- //城市价格区间不为空
- $waybillPriceModel=WaybillPriceModel::where('carrier_id',$carrier_id)->where('city_id',$destination_city_id)
- ->where('range_min','<',$carrier_weight[$i])->where('range_max','>=',$carrier_weight[$i])
- ->where('unit_id',$carrier_weight_unit_id[$i])->first();
- if($waybillPriceModel)return $waybillPriceModel->id;
- //价格区间为空
- $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();
- if ($waybillPriceModelRange){ return $waybillPriceModelRange->id;}
- //城市为空
- $city=City::where('id',$destination_city_id)->select('province_id')->first();
- $waybillPriceModelProvince=WaybillPriceModel::whereRaw('carrier_id = ? AND province_id = ? AND unit_id = ? AND range_max >= ? AND range_min < ? AND city_id IS NULL',
- [$carrier_id,$city->province_id,$carrier_weight_unit_id[$i],$carrier_weight[$i],$carrier_weight[$i]])->first();
- if ($waybillPriceModelProvince){return $waybillPriceModelProvince->id;}
- //城市价格区间都为空
- $waybillPriceModelProvinceRange=WaybillPriceModel::whereRaw('carrier_id = ? AND province_id = ? AND unit_id = ? AND range_max IS NULL AND city_id IS NULL',
- [$carrier_id,$city->province_id,$carrier_weight_unit_id[$i]])->first();
- if ($waybillPriceModelProvinceRange){return $waybillPriceModelProvinceRange->id;}
- }
- }
- return false;
- }
- /*三层条件:无优先级,找到第一个直接返回
- * 无论是否为KG||T,计数单位一为KG,计数单位一为T,计数单位二为KG,计数单位二为T
- * 计数一与计数二同时存在取最贵价格:
- * 计数一存在,二不存在:
- * 计数二存在,一不存在:
- * 城市价格区间不为空,城市价格区间都为空,城市为空,价格区间为空
- * */
- public function isWaybillPriceModel(Request $request){
- $carrier_id=$request->input('carrier_id');
- $destination_city_id=$request->input('destination_city_id');
- $carrier_weight=$request->input('carrier_weight');
- $carrier_weight_unit_id=$request->input('carrier_weight_unit_id');
- $validatorData=["carrier_id"=>$carrier_id,"destination_city_id"=>$destination_city_id,
- 'carrier_weight'=>$carrier_weight[0],"carrier_weight_unit_id"=>$carrier_weight_unit_id[0],
- "carrier_weight_other"=>$carrier_weight[1],"carrier_weight_unit_id_other"=>$carrier_weight_unit_id[1]];
- $errors=Validator::make($validatorData,[
- 'carrier_id'=>'required|integer',
- 'destination_city_id'=>'required|integer',
- 'carrier_weight'=>'nullable|min:0|numeric|max:999999',
- 'carrier_weight_unit_id'=>'required_with:carrier_weight',
- 'carrier_weight_other'=>'nullable|min:0|numeric|max:999999',
- 'carrier_weight_unit_id_other'=>'required_with:carrier_weight_other',
- ],[
- 'required'=>':attribute 为必填项',
- 'max'=>':attribute 字符过多或输入值过大',
- 'min'=>':attribute 不得为负',
- 'numeric'=>':attribute 应为数字',
- 'unique'=>':attribute 已存在',
- 'required_with'=>':attribute 未填',
- 'integer'=>':attribute 必须为数字',
- ],[
- 'carrier_weight'=>'承运商计数(抛)',
- 'carrier_id'=>'承运商',
- 'destination_city_id'=>'目的市',
- 'carrier_weight_unit_id'=>'承运商计数单位',
- 'carrier_weight_other'=>'承运商计数二',
- 'carrier_weight_unit_id_other'=>'承运商计数单位二',
- ])->errors();
- if (count($errors)>0)return ['error'=>$errors];
- $result=$this->checkWaybillPriceModel($carrier_id,$destination_city_id,$carrier_weight,$carrier_weight_unit_id);
- if (!$result){
- //单位为kg,T时
- $unitKG=Unit::where('name','kg')->first();
- $unitT=Unit::where('name','T')->first();
- if ($carrier_weight_unit_id[0]==$unitKG->id){
- $carrier_weight_unit_id[0]=$unitT->id;
- $carrier_weight[0]=$carrier_weight[0]/1000;
- $result=$this->checkWaybillPriceModel($carrier_id,$destination_city_id,$carrier_weight,$carrier_weight_unit_id);
- if ($result)return ['success'=>$result];
- }
- if ($carrier_weight_unit_id[1]==$unitKG->id){
- $carrier_weight_unit_id[1]=$unitT->id;
- $carrier_weight[1]=$carrier_weight[1]/1000;
- $result=$this->checkWaybillPriceModel($carrier_id,$destination_city_id,$carrier_weight,$carrier_weight_unit_id);
- if ($result)return ['success'=>$result];
- }
- if ($carrier_weight_unit_id[0]==$unitT->id){
- $carrier_weight_unit_id[0]=$unitKG->id;
- $carrier_weight[0]=$carrier_weight[0]*1000;
- $result=$this->checkWaybillPriceModel($carrier_id,$destination_city_id,$carrier_weight,$carrier_weight_unit_id);
- if ($result)return ['success'=>$result];
- }
- if ($carrier_weight_unit_id[1]==$unitT->id){
- $carrier_weight_unit_id[1]=$unitKG->id;
- $carrier_weight[1]=$carrier_weight[1]*1000;
- $result=$this->checkWaybillPriceModel($carrier_id,$destination_city_id,$carrier_weight,$carrier_weight_unit_id);
- if ($result)return ['success'=>$result];
- }
- }
- return ['success'=>$result];
- }
- public function waybillUpdate(Request $request, $id){
- if(!Gate::allows('运输管理-编辑')){ return redirect(url('/')); }
- $this->validatorWaybill($request,$id)->validate();
- $data=$request->input();
- $waybill=Waybill::find($id);
- $waybill->fill($data);
- if ($waybill->save()){
- $this->log(__METHOD__,__FUNCTION__,json_encode($waybill),Auth::user()['id']);
- return redirect('waybill/index')->with('successTip','运单“'.$waybill->waybill_number.'”修改成功');
- }
- }
- public function waybillAudit(Request $request){
- if(!Gate::allows('运输管理-运单审核')){ return redirect(url('/')); }
- $id=$request->input('id');
- $waybill=Waybill::find($id);
- $isAudit=WaybillAuditLog::whereRaw('waybill_id = ? and audit_stage = ?',[$id,"运单阶段"])->first();
- if (empty($isAudit)){
- $waybillAuditLog=new WaybillAuditLog([
- 'waybill_id'=>$id,
- 'audit_stage'=>'运单阶段',
- 'user_id'=>Auth::id(),
- ]);
- $waybillAuditLog->save();
- $waybillAuditLog['user']=Auth::user();
- $waybill->status='已审核';
- $result=$waybill->save();
- $this->log(__METHOD__,__FUNCTION__,json_encode($waybill),Auth::user()['id']);
- return ['success'=>$result,'status'=>$waybill->status,'waybillAuditLog'=>$waybillAuditLog];
- }
- return ['exception'=>'请勿重复审核!'];
- }
- public function waybillEdit($id){
- if(!Gate::allows('运输管理-编辑')){ return redirect(url('/')); }
- $waybill=Waybill::find($id);
- $owners=Owner::get();
- return view('waybill.waybillEdit',['waybill'=>$waybill,'owners'=>$owners]);
- }
- public function waybillRetreatAudit(Request $request){
- if(!Gate::allows('运输管理-调度')){ return redirect(url('/')); }
- $id=$request->input('id');
- $waybill=Waybill::find($id);
- WaybillAuditLog::whereRaw('waybill_id = ? and audit_stage = ?',[$id,"运单阶段"])->delete();
- $waybill->status='待重审';
- $result=$waybill->save();
- $this->log(__METHOD__,__FUNCTION__,json_encode($waybill),Auth::user()['id']);
- return ['success'=>$result,'status'=>$waybill->status];
- }
- public function waybillEndAudit(Request $request){
- if(!Gate::allows('运输管理-调度审核')){ return redirect(url('/')); }
- $id=$request->input('id');
- $waybill=Waybill::find($id);
- if (!$waybill->charge&&!$waybill->collect_fee)return ['exception'=>'收费或到付费用未填!'];
- if ($waybill->charge==0&&$waybill->collect_fee==0)return ['exception'=>'收费与到付费用都为0!'];
- if ($waybill->type=='专线'){
- if (!$waybill->carrier_weight||$waybill->carrier_weight==0)return ['exception'=>'承运商计重未填或为0!'];
- if (!$waybill->carrier_weight_unit_id)return ['exception'=>'承运商计重单位未选!'];
- }
- $isAudit=WaybillAuditLog::whereRaw('waybill_id = ? and audit_stage = ?',[$id,"调度阶段"])->first();
- if (empty($isAudit)){
- $waybillAuditLog=new WaybillAuditLog([
- 'waybill_id'=>$id,
- 'audit_stage'=>'调度阶段',
- 'user_id'=>Auth::id(),
- ]);
- $waybillAuditLog->save();
- $waybillAuditLog['user']=Auth::user();
- if ($waybill->waybill_price_model_id||$waybill->type=='直发车'){
- $waybill->status='已完结';
- $result=$waybill->save();
- $waybillPayoff=WaybillPayoff::where('waybill_id','=',$id)->first();
- $waybillPayoff->load(["waybill"]);
- $waybillPayoff->waybill->load(["owner","carrier","origination_city","destination_city","carType","waybillAuditLogs"]);
- $waybillPayoff->waybill->waybillAuditLogs->load(["user"]);
- $waybillPayoffJson=json_encode($waybillPayoff,JSON_UNESCAPED_UNICODE);
- WaybillFinancialSnapshot::create([
- 'waybill_id'=>$id,
- 'json_content'=>$waybillPayoffJson,
- ]);
- }else{
- $waybill->status='无模型';
- $result=$waybill->save();
- $waybillPayoff=WaybillPayoff::where('waybill_id','=',$id)->first();
- if ($waybillPayoff){
- $waybillPayoff->load(["waybill"]);
- $waybillPayoff->waybill->load(["owner","carrier","origination_city","destination_city","carType","waybillAuditLogs"]);
- $waybillPayoff->waybill->waybillAuditLogs->load(["user"]);
- $waybillPayoffJson=json_encode($waybillPayoff,JSON_UNESCAPED_UNICODE);
- WaybillFinancialExcepted::create([
- 'waybill_id'=>$id,
- 'json_content'=>$waybillPayoffJson,
- ]);
- }
- }
- $this->log(__METHOD__,__FUNCTION__,$waybillPayoffJson,Auth::user()['id']);
- return ['success'=>$result,'status'=>$waybill->status,'waybillAuditLog'=>$waybillAuditLog];
- }
- return ['exception'=>'请勿重复审核!'];
- }
- public function waybillExport($id,Request $request){
- if(!Gate::allows('运输管理-查询')){ return '没有权限'; }
- if ($id==-1){
- $id=[];
- $today=Carbon::now()->subDays(15);
- ini_set('max_execution_time',2500);
- ini_set('memory_limit','1526M');
- $waybills=Waybill::select('id');
- if ($request->input('waybill_number')){
- $waybills =$waybills->where('waybill_number','like','%'.$request->input('waybill_number').'%')->where('created_at','>',$today->format('Y-m-d'));
- }
- if ($request->input('carrier_bill')){
- $waybills=$waybills->where('carrier_bill','like','%'.$request->input('carrier_bill').'%')->where('created_at','>',$today->format('Y-m-d'));
- }
- if ($request->input('carrier_id')){
- $waybills=$waybills->where('carrier_id','=',$request->input('carrier_id'));
- }
- if ($request->input('owner_id')){
- $waybills=$waybills->where('owner_id','=',$request->input('owner_id'));
- }
- if ($request->input('wms_bill_number')){
- $waybills=$waybills->where('wms_bill_number','like','$'.$request->input('wms_bill_number').'%')->where('created_at','>',$today->format('Y-m-d'));
- }
- if ($request->input('created_at_start')){
- $waybills=$waybills->where('created_at','>',$request->input('created_at_start'));
- }
- if ($request->input('created_at_end')){
- $waybills=$waybills->where('created_at','<',$request->input('created_at_end'));
- }
- if ($request->input('status')){
- $waybills=$waybills->where('status',$request->input('status'));
- }
- $waybills=$waybills->get();
- foreach ($waybills as $waybill){
- array_push($id,$waybill->id);
- }
- }else $id = explode( ',',$id);
- if (!$id)return ;
- $row=[[
- 'type'=>'运单类型',
- 'owner'=>'货主',
- 'source_bill'=>'上游单号',
- 'wms_bill_number'=>'wms订单号',
- 'waybill_number'=>'运单号',
- 'origination'=>'始发地',
- 'destination'=>'目的地',
- 'carrier'=>'承运商',
- 'carrier_bill'=>'承运商单号',
- 'warehouse_weight'=>'仓库计数(抛)',
- 'carrier_weight'=>'承运商计数(抛)',
- 'warehouse_weight_other'=>'仓库计重',
- 'carrier_weight_other'=>'承运商计重',
- 'fee'=>'运费(元)',
- 'pick_up_fee'=>'提货费(元)',
- 'other_fee'=>'其他费用(元)',
- 'dispatch_remark'=>'调度备注',
- 'created_at'=>'创建时间'
- ]];
- $feeVisible=true;
- if(!Gate::allows('运输管理-可见费用项')){
- $feeVisible=false;
- unset($row[0]['fee'],$row[0]['pick_up_fee'],$row[0]['other_fee'],$row[0]['collect_fee']);
- }
- $list=[];
- for ($i=0; $i<count($id);$i++){
- $waybill=Waybill::with(['owner', 'waybillAuditLogs' => function ($query) {
- return $query->with('user');
- }])->find($id[$i]);
- foreach ($waybill->waybillAuditLogs as $waybillAuditLog){
- if ($waybillAuditLog->audit_stage=="运单阶段"){
- $waybillAuditor=$waybillAuditLog->user->name;
- }else{
- $waybillAuditor='';
- }
- if ($waybillAuditLog->audit_stage=="调度阶段"){
- $dispatchAuditor=$waybillAuditLog->user->name;
- }else{
- $dispatchAuditor='';
- }
- };
- $w=[
- 'type'=>isset($waybill->type)?$waybill->type:'',
- 'waybill_number'=>isset($waybill->waybill_number)?$waybill->waybill_number:'',
- 'owner'=>isset($waybill->owner->name)?$waybill->owner->name:'',
- 'source_bill'=>isset($waybill->source_bill)?$waybill->source_bill:'',
- 'wms_bill_number'=>isset($waybill->wms_bill_number)?$waybill->wms_bill_number:'',
- 'origination'=>isset($waybill->origination)?$waybill->origination:'',
- 'destination'=>isset($waybill->destination)?$waybill->destination:'',
- 'recipient'=>isset($waybill->recipient)?$waybill->recipient:'',
- 'recipient_mobile'=>isset($waybill->recipient_mobile)?$waybill->recipient_mobile:'',
- //'charge'=>isset($waybill->charge)?$waybill->charge:'',
- //'ordering_remark'=>isset($waybill->ordering_remark)?$waybill->ordering_remark:'',
- 'carrier'=>isset($waybill->carrier_name)?$waybill->carrier_name:'',
- 'carrier_bill'=>isset($waybill->carrier_bill)?$waybill->carrier_bill:'',
- //'origination_city'=>isset($waybill->origination_city_name)?$waybill->origination_city_name:'',
- //'destination_city'=>isset($waybill->destination_city_name)?$waybill->destination_city_name:'',
- 'warehouse_weight'=>isset($waybill->warehouse_weight)?$waybill->warehouse_weight.' '.(isset($waybill->warehouse_weight_unit_name)?$waybill->warehouse_weight_unit_name:''):'',
- '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:''):'',
- 'carrier_weight'=>isset($waybill->carrier_weight)?$waybill->carrier_weight.' '.(isset($waybill->carrier_weight_unit_name)?$waybill->carrier_weight_unit_name:''):'',
- '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:''):'',
- //'carType'=>isset($waybill->carType->name)?$waybill->carType->name.($waybill->carType->length.'米'):'',
- //'car_owner_info'=>isset($waybill->car_owner_info)?$waybill->car_owner_info:'',
- 'fee'=>isset($waybill->fee)?$waybill->fee:'',
- 'pick_up_fee'=>isset($waybill->pick_up_fee)?$waybill->pick_up_fee:'',
- 'other_fee'=>isset($waybill->other_fee)?$waybill->other_fee:'',
- //'collect_fee'=>isset($waybill->collect_fee)?$waybill->collect_fee:'',
- 'dispatch_remark'=>isset($waybill->dispatch_remark)?$waybill->dispatch_remark:'',
- //'waybillAuditor'=>isset($waybillAuditor)?$waybillAuditor:'',
- //'dispatchAuditor'=>isset($dispatchAuditor)?$dispatchAuditor:'',
- 'created_at'=>isset($waybill->created_at)?$waybill->created_at:''
- ];
- if(!$feeVisible){
- unset($w['fee'],$w['pick_up_fee'],$w['other_fee'],$w['collect_fee']);
- }
- $list[$i]=$w;
- }
- $this->log(__METHOD__,__FUNCTION__,json_encode($waybill),Auth::user()['id']);
- return Excel::download(new Export($row,$list), date('Y:m:d ') . '运单列表.xlsx');
- }
- public function deliveringExport($waybills){
- ini_set('max_execution_time',2500);
- ini_set('memory_limit','1526M');
- $row=[[
- 'created_at'=>"日期",
- 'carrier_name'=>"承运商",
- 'waybill_number'=>"宝时运单号",
- 'origination'=>"提货仓",
- 'owner_name'=>"货主",
- 'warehouse_weight_other'=>"预估重量",
- 'warehouse_weight'=>"预估体积",
- 'status'=>"状态",
- 'carrier_bill'=>"专线运单号",
- 'inquire_tel'=>"查件电话",
- 'amount'=>"件数",
- 'carrier_weight_other'=>"重量",
- 'carrier_weight'=>"体积",
- ]];
- $list=[];
- foreach ($waybills as $waybill){
- $w=[
- 'created_at'=>$waybill->created_at,
- 'carrier_name'=>$waybill->carrier_name,
- 'waybill_number'=>$waybill->waybill_number,
- 'origination'=>$waybill->origination,
- 'owner_name'=>$waybill->owner_name,
- 'warehouse_weight_other'=>$waybill->warehouse_weight_other,
- 'warehouse_weight'=>$waybill->warehouse_weight,
- 'status'=>$waybill->status=="已完结"?"已完成":$waybill->carrier_bill?"已提交":"待提交",
- 'carrier_bill'=>$waybill->carrier_bill,
- 'inquire_tel'=>$waybill->inquire_tel,
- 'amount'=>$waybill->amount,
- 'carrier_weight_other'=>$waybill->carrier_weight_other,
- 'carrier_weight'=>$waybill->carrier_weight,
- ];
- array_push($list,$w);
- }
- return Excel::download(new Export($row,$list), date('Y:m:d ') . '发运列表.xlsx');
- }
- //发运
- public function delivering(Request $request){
- if (!Auth::user())return view('exception.404',['error'=>'登录已过期']);
- if (Auth::user()->isSuperAdmin())$waybills=Waybill::orderBy('id','DESC');
- else{
- $carriersUsers=DB::table('carrier_user')->where('user_id',Auth::id())->get();
- $carrierIds=array_column($carriersUsers->toArray(),'carrier_id');
- $waybills=Waybill::orderBy('id','DESC')->whereIn("carrier_id",$carrierIds);
- }
- if ($request->input('exportType')){
- return $this->conditionQuery($request,$waybills);
- }
- $waybills=$this->conditionQuery($request,$waybills);
- return view('waybill.delivering',compact('waybills'));
- }
- //承运商提交
- public function storeCarrierBill(Request $request){
- $errors=Validator::make($request->input(),[
- 'id'=>'required|integer',
- 'carrier_bill'=>'required',
- 'inquire_tel'=>'nullable',
- 'amount'=>'nullable|integer',
- 'carrier_weight'=>'required_without:carrier_weight_other|nullable|numeric',
- 'carrier_weight_other'=>'required_without:carrier_weight|nullable|numeric',
- ],[
- 'required'=>':attribute 为必填项',
- 'integer'=>':attribute 应为整数',
- 'numeric'=>':attribute 应为数字',
- 'required_with'=>':attribute 重量与体积至少存在一项',
- ],[
- 'carrier_bill'=>'专线运单号',
- 'inquire_tel'=>'查件电话',
- 'amount'=>'件数',
- 'carrier_weight'=>'体积',
- 'carrier_weight_other'=>'重量',
- ])->errors();
- if (count($errors)>0)return ["errors"=>$errors];
- $waybill=Waybill::find($request->input('id'));
- if (!$waybill)return ["error"=>"未找到该运单!"];
- $waybill->fill($request->input());
- $waybill->update();
- return $waybill;
- }
- protected function validatorWaybill(Request $request,$id){
- if ($id){$wms_bill_number=$id;};
- $validator=Validator::make($request->input(),[
- 'owner_id'=>'required',
- 'wms_bill_number'=>['nullable','max:50',isset($wms_bill_number)?"unique:waybills,wms_bill_number,$wms_bill_number":'unique:waybills,wms_bill_number'],
- 'origination'=>'required|max:255',
- 'destination'=>'required|max:255',
- 'recipient'=>'required|max:50',
- 'recipient_mobile'=>['required','regex:/^(\d{7,11})|(1[3|4|5|7|8][0-9]\d{4,8})$/'],
- 'charge'=>'nullable|min:0|max:999999|numeric',
- 'collect_fee'=>'nullable|min:0|numeric',
- ],[
- 'required'=>':attribute 为必填项',
- 'alpha_num'=>':attribute 应为字母或数字',
- 'max'=>':attribute 字符过多或输入值过大',
- 'regex'=>':attribute 输入有误',
- 'integer'=>':attribute 应为整数',
- 'min'=>':attribute 不得为负',
- 'numeric'=>':attribute 应为数字',
- 'unique'=>':attribute 已存在',
- ],[
- 'owner_id'=>'货主',
- 'wms_bill_number'=>'WMS单号',
- 'origination'=>'始发地',
- 'destination'=>'目的地',
- 'recipient'=>'收件人',
- 'recipient_mobile'=>'收件人电话',
- 'charge'=>'收费',
- 'collect_fee'=>'到付金额',
- ]);
- return $validator;
- }
- protected function validatorWaybillDispatch(Request $request,$id){
- if ($request->input('type')=='直发车'){
- $validator=Validator::make($request->input(),[
- 'carrier_bill'=>"nullable|max:50|unique:waybills,carrier_bill,$id",
- 'fee'=>'required|min:0|numeric|max:999999',
- 'other_fee'=>'nullable|min:0|numeric|max:999999',
- 'charge'=>'nullable|min:0|numeric|max:999999',
- 'mileage'=>'nullable|numeric',
- 'amount'=>'nullable|numeric',
- ],[
- 'required'=>':attribute 为必填项',
- 'alpha_num'=>':attribute 应为字母或数字',
- 'max'=>':attribute 字符过多或输入值过大',
- 'min'=>':attribute 不得为负',
- 'numeric'=>':attribute 应为数字',
- 'unique'=>':attribute 已存在',
- ],[
- 'carrier_bill'=>'承运商单号',
- 'fee'=>'运费',
- 'other_fee'=>'其他费用',
- 'charge'=>'收费',
- 'mileage'=>'里程数',
- 'amount'=>'计数',
- ]);
- return $validator;
- }else if ($request->input('type')=='专线'){
- return $this->validatorWaybillZX($request,$id);
- }else{
- return false;
- }
- }
- protected function validatorWaybillZX(Request $request,$id){
- $validator=Validator::make($request->input(),[
- 'carrier_bill'=>"nullable|max:50|unique:waybills,carrier_bill,$id",
- 'pick_up_fee'=>'nullable|min:0|numeric|max:999999',
- 'other_fee'=>'nullable|min:0|numeric|max:999999',
- 'carrier_id'=>'required|integer',
- 'destination_city_id'=>'required|integer',
- 'warehouse_weight'=>'nullable|min:0|numeric|max:999999',
- 'warehouse_weight_unit_id'=>'required_with:warehouse_weight|nullable|integer',
- 'warehouse_weight_other'=>'nullable|min:0|numeric|max:999999',
- 'warehouse_weight_unit_id_other'=>'required_with:warehouse_weight_other|nullable|integer',
- 'charge'=>'nullable|min:0|numeric|max:999999',
- 'carrier_weight'=>'nullable|min:0|numeric|max:999999',
- 'carrier_weight_unit_id'=>'required_with:carrier_weight',
- 'carrier_weight_other'=>'nullable|min:0|numeric|max:999999',
- 'carrier_weight_unit_id_other'=>'required_with:carrier_weight_other',
- ],[
- 'required'=>':attribute 为必填项',
- 'alpha_num'=>':attribute 应为字母或数字',
- 'max'=>':attribute 字符过多或输入值过大',
- 'min'=>':attribute 不得为负',
- 'numeric'=>':attribute 应为数字',
- 'unique'=>':attribute 已存在',
- 'required_with'=>':attribute 未填',
- 'integer'=>':attribute 必须为数字',
- ],[
- 'carrier_bill'=>'承运商单号',
- 'warehouse_weight'=>'仓库计数(抛)',
- 'carrier_weight'=>'承运商计数(抛)',
- 'pick_up_fee'=>'提货费',
- 'other_fee'=>'其他费用',
- 'carrier_id'=>'承运商',
- 'destination_city_id'=>'目的市',
- 'carrier_weight_unit_id'=>'承运商计数单位',
- 'charge'=>'收费',
- 'warehouse_weight_unit_id'=>'仓库计数单位',
- 'warehouse_weight_other'=>'仓库计数二',
- 'carrier_weight_other'=>'承运商计数二',
- 'warehouse_weight_unit_id_other'=>'仓库技数单位二',
- 'carrier_weight_unit_id_other'=>'承运商计数单位二',
- ]);
- return $validator;
- }
- public function addCounty(Request $request){
- $rule =[
- 'destination_city' => ['required', 'string','unique:cities,name'],
- ];
- $messages=[
- 'destination_city.required'=>'市/县不能为空',
- 'destination_city.string'=>'市/县必须是字符串',
- 'destination_city.unique'=>'市/县已存在',
- ];
- $validator = Validator::make($request->all(),$rule,$messages);
- if ($validator->fails()) {
- return $validator->errors();
- }
- $city=new City([
- 'name'=>$request->input('destination_city'),
- 'type'=>3,
- ]);
- $city->save();
- return $city;
- }
- // 运单删除 软删除
- public function destroy(int $id){
- if(!GAte::allows('运输管理-删除')){return['success'=>0,'status'=>'没有权限'];}
- if(is_null($id)){return ['success'=>'0','status'=>'传入id为空'];}
- $result = Waybill::where('id',$id)->delete();
- return ['success'=>$result,'status'=>$result];
- }
- // 回收站
- public function recycle(Request $request){
- if(!Gate::allows('运输管理-删除')){return redirect('/');}
- $user = Auth::user();
- $paginate = $request->input('paginate')??50;
- $waybills = Waybill::with(['owner','waybillAuditLogs' => function ($query) {
- return $query->with('user');
- }])->orderBy('deleted_at', 'DESC')->withTrashed()->whereNotNull('deleted_at')->paginate(50);
- $total = $waybills->count();
- $paginateParams = [];
- $paginateParams['paginate'] = $paginate;
- return view('waybill.recycle',compact('waybills','total','paginateParams'));
- }
- // 软删除恢复
- public function apiRestoreSelected(Request $request){
- DB::enableQueryLog();
- if(!Gate::allows('运输管理-删除')){return ['success'=>'false','fail_info'=>'没有权限'];}
- $ids = $request->input('ids')??'';
- if($ids == ''){return ['success'=>'false','fail_info'=>'没有可恢复对象'];}
- $waybills = Waybill::withTrashed()->whereIn('id',$ids)->get();
- $result = '';
- $waybills->each(function (Waybill $waybill){
- $waybill->restore();
- });
- $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
- return ['success'=>$result,'waybills'=>$waybills];
- }
- // 修改运费
- public function changeFee(Request $request){
- if(!Gate::allows('运输管理-运费')){return ['success'=>'false','fail_info'=>'没有权限'];}
- $wayBillId = $request->input('id');
- $waybillFee = $request->input('fee');
- if(is_null($wayBillId) or is_null($waybillFee)){
- return ['success'=>'false','fail_info'=>'参数异常'];
- }
- $result = Waybill::where('id',$wayBillId)->update(['fee'=>$waybillFee]);
- $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
- return ['success'=>$result,'status'=>$result];
- }
- // 置顶
- public function waybillOnTop(Request $request){
- $id = $request->input('id');
- $detail = $request->input('detail');
- if(!Gate::allows('运输管理-置顶')){return ['success'=>'false','fail_info'=>'没有权限'];}
- if(is_null($id)){
- return ['success'=>'false','fail_info'=>'传参错误'];
- }
- $wayontop = WaybillOnTop::withTrashed()->where('waybill_id',$id);
- $result = '';
- if(count($wayontop->get()) == 0){
- $wayontop = WaybillOnTop::create(['waybill_id'=>$id,'remark'=>$detail]);
- $result = $wayontop->save();
- }else{
- $wayontop = WaybillOnTop::withTrashed()->where('waybill_id',$id);
- $result = WaybillOnTop::withTrashed()->where('waybill_id',$id)->restore();
- }
- return ['success'=>$result,'status'=>$result];
- }
- // 取消置顶
- public function cancelOnTop(Request $request){
- $id = $request->input('id');
- if(!Gate::allows('运输管理-置顶')){return ['success'=>'false','fail_info'=>'没有权限'];}
- if(is_null($id)){
- return ['success'=>'false','fail_info'=>'传参错误'];
- }
- $result = WaybillOnTop::where('waybill_id',$id)->forceDelete();
- return ['success'=>$result,'status'=>$result];
- }
- // 获取所有运单信息
- public function getWaybills(){
- $waybills = Waybill::with(['owner','wmsCommodities','waybillAuditLogs' => function ($query) {
- return $query->with('user');
- }])->selectRaw('waybills.* ,waybill_on_tops.id top_id ,waybill_on_tops.remark,waybill_on_tops.updated_at top_update')
- ->leftJoin('waybill_on_tops','waybill_on_tops.waybill_id','=','waybills.id')
- ->whereNull('waybill_on_tops.deleted_at')
- ->orderBy('waybill_on_tops.updated_at','desc')
- ->orderBy('waybills.id','desc');
- return $waybills;
- }
- }
|