Anons79 Mini Shell

Directory : /home/aplikasiposinfo/www/langganan.fixmate.id/app/Http/Controllers/
Upload File :
Current File : /home/aplikasiposinfo/www/langganan.fixmate.id/app/Http/Controllers/AffiliateController.php

<?php

namespace App\Http\Controllers;

use App\Models\Affiliate;
use App\Models\PembayaranAffiliate;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Str;

class AffiliateController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        //
        $affiliate = null;
        $total = 0;
        if (auth()->user()->hasRole('admin')) {
            $affiliate = Affiliate::with('user','langganan')->has('user')->orderBy('created_at','DESC')->get();
            $totalPembayaran = PembayaranAffiliate::where('status','dibayar')->sum('jumlah_permintaan');
        } else {
            $affiliate = Affiliate::with('user','langganan')->has('user')->where('user_id', Auth::user()->id)->first();
            $totalPembayaran = PembayaranAffiliate::where('user_id',auth()->user()->id)->where('status','dibayar')->sum('jumlah_permintaan');
        }
        if ($affiliate && isset($affiliate->langganan)) {
            foreach ($affiliate->langganan->where('status','paket aktif') as $key => $value) {
                $total += $value->paket->getTotal() - ($value->paket->getTotal() * ($affiliate->potongan / 100));
            }
        }
        
        return view('Affiliate.index', compact('affiliate', 'total','totalPembayaran'));
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        //

        try {
            //code...
            if ($request->tipeForm == "update") {
                $affiliate = Affiliate::where("id", $request->affiliateId)->first();
               
                $potongan = $request->potongan;
                if ($potongan <= 15) {
                    $affiliate->update([
                        "persen" => $request->persen,
                        "code" => $request->code,
                        "potongan" => $request->potongan,
                    ]);
                } else {
                    return redirect()->back()->with("Error", "Potongan diskon tidak bisa lebih dari 15%");
                }
                return redirect()->back()->with("success", "sukses");

            }
            $dataRequest = [
                'user_id' => Auth::user()->id,
                'bank' => "-",
                'norek' => "-",
                'atas_nama_norek' => "-",
                'used_counter' => 0,
                'code' => Str::random(6),
            ];

            $affiliate = Affiliate::create($dataRequest);
            return redirect()->back()->with('success');
        } catch (\Throwable $th) {
            return redirect()->back()->with('error', $th->getMessage());
            //throw $th;
        }
    }

    /**
     * Display the specified resource.
     */
    public function show(string $id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(string $id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, string $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(string $id)
    {
        //
    }
}

Anons79 File Manager Version 1.0, Coded By Anons79
Email: [email protected]