403Webshell
Server IP : 34.87.99.72  /  Your IP : 216.73.217.31
Web Server : Apache/2.4.46 (Unix) OpenSSL/1.1.1n
System : Linux zlock-bitnami-lamp-prod-v2-vm 4.19.0-16-cloud-amd64 #1 SMP Debian 4.19.181-1 (2021-03-19) x86_64
User : bitnami ( 1000)
PHP Version : 7.4.18
Disable Function : NONE
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : ON  |  Sudo : ON  |  Pkexec : OFF
Directory :  /opt/bitnami/apache/htdocs/app/app/Http/Services/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ Back ]     

Current File : /opt/bitnami/apache/htdocs/app/app/Http/Services/ShippingMethodService.php
<?php
namespace App\Http\Services;

use App\Models\ShippingMethod;
use App\Models\ShippingType;
use Illuminate\Validation\ValidationException;
use Queriplex;
use Str;
use Pylon\Exceptions\BadRequestException;

class ShippingMethodService
{

    public function shippingMethodQueriplex($query, $request)
    {
        $order_mode = ( $request['sortDesc'] ?? false )  ? 'DESC' : 'ASC';
        $search_by = $request['searchBy'] ?? null;
        
        $queriplex = Queriplex::make($query)
            ->withFilters($request,[
                'id' => 'id',
                'name' => 'name',
                'activity' => fn($query, $value) => $query->whereActivity($value),
                'search' => ( fn($query, $value) => self::shippingMethodSearchQuery($query,$value,$search_by) ),
            ])
            ->withSort($request['sortBy'] ?? null, [
                "id" => fn($query) => $query->orderBy('id', $order_mode),
                "is_enabled" => fn($query) => $query->orderBy('is_enabled', $order_mode),
                "register_time" => fn($query) => $query->orderBy('created_at', $order_mode),
            ]);

        return $queriplex->apply();
    }

    public function shippingMethodSearchQuery($query, $value, $search_by){

        $common_searchable = [
            'id',
            'name'
        ];          
        if(in_array($search_by,$common_searchable)){
            $query->search($search_by,$value);
        }

        return null;
    }

    public function index(Array $request)
    {
        $shippingMethods = ShippingMethod::query();

        $search_by = $request['searchBy'] ?? null;

        $shippingMethods = self::shippingMethodQueriplex($shippingMethods, $request);

        $result = $shippingMethods->paginate($request['itemsPerPage'] ?? null);
        
        $result->load('shippingType', 'flatRate.geoZone', 'freeShipping.geoZone', 'weightBased.geoZone', 'dynamicWeightBased.geoZone', 'flatRate.couriers', 'boxTypeBased.couriers', 'freeShipping.couriers', 'weightBased.couriers', 'dynamicWeightBased.couriers', 'dynamicWeightBased.weights', 'boxTypeBased.boxType',);

        return $result;
    }

    public function info(Array $request)
    {
        $shippingMethod = ShippingMethod::with('shippingType', 'flatRate.geoZone', 'freeShipping.geoZone', 'weightBased.geoZone', 'dynamicWeightBased.geoZone', 'flatRate.couriers', 'boxTypeBased.couriers', 'freeShipping.couriers', 'weightBased.couriers', 'dynamicWeightBased.couriers', 'dynamicWeightBased.weights', 'boxTypeBased.boxType')->where('id',$request['id'])
            ->firstOr( function(){
                throw ValidationException::withMessages(
                    [ "id" => "The selected id is invalid." ]
                );
            });


        return $shippingMethod;
    }

    public function create(Array $request)
    {
        if(isset($request['is_enabled']))
        {
            switch ($request['shipping_type_id']) {
                case ShippingType::FLAT_RATE:
                    $courier_ids = array_map(function($q) {
                        return ['courier_id' => $q['id']];
                    }, $request['flat_rate']['couriers']);
                    ShippingMethod::whereHas('flatRate', function($q) use ($request, $courier_ids) {
                        $q->where('geo_zone_id', $request['flat_rate']['geo_zone_id'])->whereHas('couriers', function($p) use ($courier_ids) {
                            $p->whereIn('id', $courier_ids);
                        });
                    })->where('shipping_type_id', $request['shipping_type_id'])->update([
                        'is_enabled' => 0
                    ]);
                    break;
                case ShippingType::FREE_SHIPPING:
                    $courier_ids = array_map(function($q) {
                        return ['courier_id' => $q['id']];
                    }, $request['free_shipping']['couriers']);
                    ShippingMethod::whereHas('freeShipping', function($q) use ($request, $courier_ids) {
                        $q->where('geo_zone_id', $request['free_shipping']['geo_zone_id'])->whereHas('couriers', function($p) use ($courier_ids) {
                            $p->whereIn('id', $courier_ids);
                        });
                    })->where('shipping_type_id', $request['shipping_type_id'])->update([
                        'is_enabled' => 0
                    ]);
                    break;
                case ShippingType::WEIGHT_BASED:
                    $courier_ids = array_map(function($q) {
                        return ['courier_id' => $q['id']];
                    }, $request['weight_based']['couriers']);
                    ShippingMethod::whereHas('weightBased', function($q) use ($request, $courier_ids) {
                        $q->where('geo_zone_id', $request['weight_based']['geo_zone_id'])->whereHas('couriers', function($p) use ($courier_ids) {
                            $p->whereIn('id', $courier_ids);
                        });
                    })->where('shipping_type_id', $request['shipping_type_id'])->update([
                        'is_enabled' => 0
                    ]);
                    break;
                case ShippingType::DYNAMIC_WEIGHT_BASED:
                    $courier_ids = array_map(function($q) {
                        return ['courier_id' => $q['id']];
                    }, $request['dynamic_weight_based']['couriers']);
                    ShippingMethod::whereHas('dynamicWeightBased', function($q) use ($request, $courier_ids) {
                        $q->where('geo_zone_id', $request['dynamic_weight_based']['geo_zone_id'])->whereHas('couriers', function($p) use ($courier_ids) {
                            $p->whereIn('id', $courier_ids);
                        });
                    })->where('shipping_type_id', $request['shipping_type_id'])->update([
                        'is_enabled' => 0
                    ]);
                    break;
                case ShippingType::BOX_TYPE_BASED:
                    $courier_ids = array_map(function($q) {
                        return ['courier_id' => $q['id']];
                    }, $request['box_type_based']['couriers']);
                    ShippingMethod::whereHas('boxTypeBased', function($q) use ($request, $courier_ids) {
                        $q->where('box_type_id', $request['box_type_based']['box_type_id'])->whereHas('couriers', function($p) use ($courier_ids) {
                            $p->whereIn('id', $courier_ids);
                        });
                    })->where('shipping_type_id', $request['shipping_type_id'])->update([
                        'is_enabled' => 0
                    ]);
                    break;
            }
        }

        $shippingMethod = new ShippingMethod();
        $shippingMethod->name = $request['name'];
        $shippingMethod->shipping_type_id = $request['shipping_type_id'];
        $shippingMethod->is_enabled = isset($request['is_enabled']) ? 1 : 0;
        $shippingMethod->save();

        switch ($request['shipping_type_id']) {
            case ShippingType::FLAT_RATE:
                $shippingMethod->flatRate()->create([
                    'geo_zone_id' => $request['flat_rate']['geo_zone_id'],
                    'cost' => $request['flat_rate']['cost']
                ]);
                if(isset($request['flat_rate']['couriers']))
                {
                    $courier_ids = array_map(function($q) {
                        return ['courier_id' => $q['id']];
                    }, $request['flat_rate']['couriers']);
                    $shippingMethod->flatRate->couriers()->attach($courier_ids);
                }
                break;
            case ShippingType::FREE_SHIPPING:
                $shippingMethod->freeShipping()->create([
                    'geo_zone_id' => $request['free_shipping']['geo_zone_id'],
                    'subtotal' => $request['free_shipping']['subtotal']
                ]);
                if(isset($request['free_shipping']['couriers']))
                {
                    $courier_ids = array_map(function($q) {
                        return ['courier_id' => $q['id']];
                    }, $request['free_shipping']['couriers']);
                    $shippingMethod->freeShipping->couriers()->attach($courier_ids);
                }
                break;
            case ShippingType::WEIGHT_BASED:
                $shippingMethod->weightBased()->create([
                    'geo_zone_id' => $request['weight_based']['geo_zone_id'],
                    'first_weight' => $request['weight_based']['first_weight'],
                    'price_first_weight' => $request['weight_based']['price_first_weight'],
                    'repeating_weight' => $request['weight_based']['repeating_weight'],
                    'price_repeating_weight' => $request['weight_based']['price_repeating_weight'],
                    'additional_fee' => $request['weight_based']['additional_fee'] ?? 0
                ]);
                if(isset($request['weight_based']['couriers']))
                {
                    $courier_ids = array_map(function($q) {
                        return ['courier_id' => $q['id']];
                    }, $request['weight_based']['couriers']);
                    $shippingMethod->weightBased->couriers()->attach($courier_ids);
                }
                break;
            case ShippingType::DYNAMIC_WEIGHT_BASED:
                $shippingMethod->dynamicWeightBased()->create([
                    'geo_zone_id' => $request['dynamic_weight_based']['geo_zone_id']
                ]);
                if(isset($request['dynamic_weight_based']['couriers']))
                {
                    $courier_ids = array_map(function($q) {
                        return ['courier_id' => $q['id']];
                    }, $request['dynamic_weight_based']['couriers']);
                    $shippingMethod->dynamicWeightBased->couriers()->attach($courier_ids);
                }
                if(isset($request['dynamic_weight_based']['weights']))
                {
                    foreach ($request['dynamic_weight_based']['weights'] as $weight) {
                        $shippingMethod->dynamicWeightBased->weights()->create([
                            'weight' => $weight['weight'],
                            'price' => $weight['price']
                        ]);
                    }
                }
                break;
            case ShippingType::BOX_TYPE_BASED:
                $shippingMethod->boxTypeBased()->create([
                    'cost' => $request['box_type_based']['cost'],
                    'box_type_id' => $request['box_type_based']['box_type_id'],
                ]);
                if(isset($request['box_type_based']['couriers']))
                {
                    $courier_ids = array_map(function($q) {
                        return ['courier_id' => $q['id']];
                    }, $request['box_type_based']['couriers']);
                    $shippingMethod->boxTypeBased->couriers()->attach($courier_ids);
                }
                break;
        }

        return $shippingMethod;
    }

    public function update(Array $request)
    {
        $shippingMethod = ShippingMethod::where('id',$request['id'])
            ->withTrashed()
            ->firstOr( function(){
                throw ValidationException::withMessages(
                    [ "id" => "The selected id is invalid." ]
                );
            });

        if($request['is_enabled'])
        {
            switch ($request['shipping_type_id']) {
                case ShippingType::FLAT_RATE:
                    $courier_ids = array_map(function($q) {
                        return ['courier_id' => $q['id']];
                    }, $request['flat_rate']['couriers']);
                    ShippingMethod::where('id', '!=', $request['id'])->whereHas('flatRate', function($q) use ($request, $courier_ids) {
                        $q->where('geo_zone_id', $request['flat_rate']['geo_zone_id'])->whereHas('couriers', function($p) use ($courier_ids) {
                                $p->whereIn('id', $courier_ids);
                            });
                    })->where('shipping_type_id', $request['shipping_type_id'])->update([
                        'is_enabled' => 0
                    ]);
                    break;
                case ShippingType::FREE_SHIPPING:
                    $courier_ids = array_map(function($q) {
                        return ['courier_id' => $q['id']];
                    }, $request['free_shipping']['couriers']);
                    ShippingMethod::where('id', '!=', $request['id'])->whereHas('freeShipping', function($q) use ($request, $courier_ids) {
                        $q->where('geo_zone_id', $request['free_shipping']['geo_zone_id'])->whereHas('couriers', function($p) use ($courier_ids) {
                                $p->whereIn('id', $courier_ids);
                            });
                    })->where('shipping_type_id', $request['shipping_type_id'])->update([
                        'is_enabled' => 0
                    ]);
                    break;
                case ShippingType::WEIGHT_BASED:
                    $courier_ids = array_map(function($q) {
                        return ['courier_id' => $q['id']];
                    }, $request['weight_based']['couriers']);
                    $courier_ids = $shippingMethod->weightBased->couriers->pluck('id')->toArray();
                    ShippingMethod::where('id', '!=', $request['id'])->whereHas('weightBased', function($q) use ($request, $courier_ids) {
                        $q->where('geo_zone_id', $request['weight_based']['geo_zone_id'])->whereHas('couriers', function($p) use ($courier_ids) {
                                $p->whereIn('id', $courier_ids);
                            });
                    })->where('shipping_type_id', $request['shipping_type_id'])->update([
                        'is_enabled' => 0
                    ]);
                    break;
                case ShippingType::DYNAMIC_WEIGHT_BASED:
                    $courier_ids = array_map(function($q) {
                        return ['courier_id' => $q['id']];
                    }, $request['dynamic_weight_based']['couriers']);
                    ShippingMethod::where('id', '!=', $request['id'])->whereHas('dynamicWeightBased', function($q) use ($request, $courier_ids) {
                        $q->where('geo_zone_id', $request['dynamic_weight_based']['geo_zone_id'])->whereHas('couriers', function($p) use ($courier_ids) {
                                $p->whereIn('id', $courier_ids);
                            });
                    })->where('shipping_type_id', $request['shipping_type_id'])->update([
                        'is_enabled' => 0
                    ]);
                    break;
                case ShippingType::BOX_TYPE_BASED:
                    $courier_ids = array_map(function($q) {
                        return ['courier_id' => $q['id']];
                    }, $request['box_type_based']['couriers']);
                    ShippingMethod::where('id', '!=', $request['id'])->whereHas('boxTypeBased', function($q) use ($request, $courier_ids) {
                        $q->where('box_type_id', $request['box_type_based']['box_type_id'])->whereHas('couriers', function($p) use ($courier_ids) {
                                $p->whereIn('id', $courier_ids);
                            });
                    })->where('shipping_type_id', $request['shipping_type_id'])->update([
                        'is_enabled' => 0
                    ]);
                    break;
            }
        }

        // dd($request);
        switch ($request['shipping_type_id']) {
            case ShippingType::FLAT_RATE:
                $shippingMethod->flatRate()->update([
                    'geo_zone_id' => $request['flat_rate']['geo_zone_id'],
                    'cost' => $request['flat_rate']['cost']
                ]);
                if(isset($request['flat_rate']['couriers']))
                {
                    $courier_ids = array_map(function($q) {
                        return ['courier_id' => $q['id']];
                    }, $request['flat_rate']['couriers']);
                    $shippingMethod->flatRate->couriers()->detach();
                    $shippingMethod->flatRate->couriers()->attach($courier_ids);
                }
                break;
            case ShippingType::FREE_SHIPPING:
                $shippingMethod->freeShipping()->update([
                    'geo_zone_id' => $request['free_shipping']['geo_zone_id'],
                    'subtotal' => $request['free_shipping']['subtotal']
                ]);
                if(isset($request['free_shipping']['couriers']))
                {
                    $courier_ids = array_map(function($q) {
                        return ['courier_id' => $q['id']];
                    }, $request['free_shipping']['couriers']);
                    $shippingMethod->freeShipping->couriers()->detach();
                    $shippingMethod->freeShipping->couriers()->attach($courier_ids);
                }
                break;
            case ShippingType::WEIGHT_BASED:
                $shippingMethod->weightBased()->update([
                    'geo_zone_id' => $request['weight_based']['geo_zone_id'],
                    'first_weight' => $request['weight_based']['first_weight'],
                    'price_first_weight' => $request['weight_based']['price_first_weight'],
                    'repeating_weight' => $request['weight_based']['repeating_weight'],
                    'price_repeating_weight' => $request['weight_based']['price_repeating_weight'],
                    'additional_fee' => $request['weight_based']['additional_fee'] ?? 0
                ]);
                if(isset($request['weight_based']['couriers']))
                {
                    $courier_ids = array_map(function($q) {
                        return ['courier_id' => $q['id']];
                    }, $request['weight_based']['couriers']);
                    $shippingMethod->weightBased->couriers()->detach();
                    $shippingMethod->weightBased->couriers()->attach($courier_ids);
                }
                break;
            case ShippingType::DYNAMIC_WEIGHT_BASED:
                $shippingMethod->dynamicWeightBased()->update([
                    'geo_zone_id' => $request['dynamic_weight_based']['geo_zone_id']
                ]);
                if(isset($request['dynamic_weight_based']['couriers']))
                {
                    $courier_ids = array_map(function($q) {
                        return ['courier_id' => $q['id']];
                    }, $request['dynamic_weight_based']['couriers']);
                    $shippingMethod->dynamicWeightBased->couriers()->detach();
                    $shippingMethod->dynamicWeightBased->couriers()->attach($courier_ids);
                }
                if(isset($request['dynamic_weight_based']['weights']))
                {
                    $shippingMethod->dynamicWeightBased->weights()->delete();
                    foreach ($request['dynamic_weight_based']['weights'] as $weight) {
                        $shippingMethod->dynamicWeightBased->weights()->create([
                            'weight' => $weight['weight'],
                            'price' => $weight['price']
                        ]);
                    }
                }
                break;
            case ShippingType::BOX_TYPE_BASED:
                $shippingMethod->boxTypeBased()->update([
                    'cost' => $request['box_type_based']['cost'],
                    'box_type_id' => $request['box_type_based']['box_type_id'],
                ]);
                if(isset($request['box_type_based']['couriers']))
                {
                    $courier_ids = array_map(function($q) {
                        return ['courier_id' => $q['id']];
                    }, $request['box_type_based']['couriers']);
                    $shippingMethod->boxTypeBased->couriers()->detach();
                    $shippingMethod->boxTypeBased->couriers()->attach($courier_ids);
                }
                break;
        }
        $shippingMethod->update([
            'name' => $request['name'],
            'is_enabled' => $request['is_enabled']
        ]);
    }

    public function updateStatus(Array $request)
    {
        $shippingMethod = ShippingMethod::where('id',$request['id'])
            ->withTrashed()
            ->firstOr( function(){
                throw ValidationException::withMessages(
                    [ "id" => "The selected id is invalid." ]
                );
            });
            if($request['is_enabled'])
            {
                switch ($request['shipping_type_id']) {
                    case ShippingType::FLAT_RATE:
                        $courier_ids = $shippingMethod->flatRate->couriers->pluck('id')->toArray();
                        ShippingMethod::whereHas('flatRate', function($q) use ($request, $courier_ids) {
                            $q->where('geo_zone_id', $request['geo_zone_id'])->whereHas('couriers', function($p) use ($courier_ids) {
                                $p->whereIn('id', $courier_ids);
                            });
                        })->where('shipping_type_id', $request['shipping_type_id'])->update([
                            'is_enabled' => 0
                        ]);
                        break;
                    case ShippingType::FREE_SHIPPING:
                        $courier_ids = $shippingMethod->freeShipping->couriers->pluck('id')->toArray();
                        ShippingMethod::whereHas('freeShipping', function($q) use ($request, $courier_ids) {
                            $q->where('geo_zone_id', $request['geo_zone_id'])->whereHas('couriers', function($p) use ($courier_ids) {
                                $p->whereIn('id', $courier_ids);
                            });
                        })->where('shipping_type_id', $request['shipping_type_id'])->update([
                            'is_enabled' => 0
                        ]);
                        break;
                    case ShippingType::WEIGHT_BASED:
                        $courier_ids = $shippingMethod->weightBased->couriers->pluck('id')->toArray();
                        ShippingMethod::whereHas('weightBased', function($q) use ($request, $courier_ids) {
                            $q->where('geo_zone_id', $request['geo_zone_id'])->whereHas('couriers', function($p) use ($courier_ids) {
                                $p->whereIn('id', $courier_ids);
                            });
                        })->where('shipping_type_id', $request['shipping_type_id'])->update([
                            'is_enabled' => 0
                        ]);
                        break;
                    case ShippingType::DYNAMIC_WEIGHT_BASED:
                        $courier_ids = $shippingMethod->dynamicWeightBased->couriers->pluck('id')->toArray();
                        ShippingMethod::whereHas('dynamicWeightBased', function($q) use ($request, $courier_ids) {
                            $q->where('geo_zone_id', $request['geo_zone_id'])->whereHas('couriers', function($p) use ($courier_ids) {
                                $p->whereIn('id', $courier_ids);
                            });
                        })->where('shipping_type_id', $request['shipping_type_id'])->update([
                            'is_enabled' => 0
                        ]);
                        break;
                    case ShippingType::BOX_TYPE_BASED:
                        $courier_ids = $shippingMethod->boxTypeBased->couriers->pluck('id')->toArray();
                        ShippingMethod::whereHas('boxTypeBased', function($q) use ($request, $courier_ids) {
                            $q->where('box_type_id', $request['box_type_id'])->whereHas('couriers', function($p) use ($courier_ids) {
                                $p->whereIn('id', $courier_ids);
                            });
                        })->where('shipping_type_id', $request['shipping_type_id'])->update([
                            'is_enabled' => 0
                        ]);
                        break;
                }
            }

            $shippingMethod->update([
                'is_enabled' => $request['is_enabled']
            ]);


    }

    public function delete(Array $request)
    {
        $shippingMethod = ShippingMethod::where('id',$request['id'])
            ->withTrashed()
            ->firstOr( function(){
                throw ValidationException::withMessages(
                    [ "id" => "The selected id is invalid." ]
                );
            });

        switch ($shippingMethod->shipping_type_id) {
            case ShippingType::FLAT_RATE:
                $shippingMethod->flatRate->couriers()->detach();
                $shippingMethod->flatRate()->delete();
                break;
            case ShippingType::FREE_SHIPPING:
                $shippingMethod->freeShipping->couriers()->detach();
                $shippingMethod->freeShipping()->delete();
                break;
            case ShippingType::WEIGHT_BASED:
                $shippingMethod->weightBased->couriers()->detach();
                $shippingMethod->weightBased()->delete();
                break;
            case ShippingType::DYNAMIC_WEIGHT_BASED:
                $shippingMethod->dynamicWeightBased->couriers()->detach();
                $shippingMethod->dynamicWeightBased->weights()->delete();
                $shippingMethod->dynamicWeightBased()->delete();
                break;
            case ShippingType::BOX_TYPE_BASED:
                $shippingMethod->boxTypeBased->couriers()->detach();
                $shippingMethod->boxTypeBased()->delete();
                break;
        }

        $shippingMethod->restoreOrDelete();

        return $shippingMethod;
    }
}

Youez - 2016 - github.com/yon3zu
LinuXploit