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/ProductQtyHistoryController.php


<?php

namespace App\Http\Controllers;

use App\Exports\QtyTransactionExport;
use App\Models\Product;
use App\Models\ProductCategory;
use App\Models\QtyHistory;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Maatwebsite\Excel\Facades\Excel;

class ProductQtyHistoryController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index(Request $request) // transaction
    {
        $paginate = $request->get('paginate')??25;
        $q = $request->get('q')??null;
        $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];
        }
        $product_id = $request->get('product_id');
        $in_out = $request->in_out??0;
        if($product_id){
            $qtyHistories = QtyHistory::where('product_id' , $product_id)->whereBetween('created_at', [$start_date . " 00:00:00", $end_date . " 23:59:59"])
                ->orderBy('id' , 'desc');
        }
        else{
            $qtyHistories = QtyHistory::whereBetween('created_at', [$start_date . " 00:00:00", $end_date . " 23:59:59"])
                ->orderBy('id' , 'desc');
        }

        if($q){
            $qtyHistories = $qtyHistories->where('grn' , $q);
        }

        if($request->in_out > 0){
            $qtyHistories = $qtyHistories->where('qty' , '>' , 0)->paginate($paginate);
        }else if($request->in_out < 0){
            $qtyHistories = $qtyHistories->where('qty' , '<' , 0)->paginate($paginate);
        }else{
            $qtyHistories = $qtyHistories->paginate($paginate);
        }
        return view('product-qty-histories.index' , compact('qtyHistories' , 'product_id' , 'paginate' , 'in_out' , 'start_date' , 'end_date','q'));
    }
    public function index2(Request $request) // transaction
    {
        $paginate = $request->get('paginate')??25;
        $for_sale = $request->get('for_sale');
        $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];
        }
        $products = Product::whereBetween('created_at', [$start_date . " 00:00:00", $end_date . " 23:59:59"])->where('for_sale' , $for_sale)->orderBy('id' , 'desc')->paginate($paginate);
        return view('product-qty-histories.transaction2' , compact('products'  , 'paginate' , 'for_sale' , 'start_date' , 'end_date'));
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create(Request $request)
    {
        $type = $request->get('type')??'+';
        $product_id = $request->get('product_qty_id');
        if($type && $product_id){
            $product = Product::findOrFail($product_id);
            return view('product-qty-histories.create',compact('product_id' , 'product' ,'type'));
        }
        else{
            return redirect()->back()->with('error', __('message.updated successfully'));
        }
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        $is_adjustment = false;
        if($request->is_adjustment){
            $is_adjustment = true;
        }
        $product = Product::findOrfail($request->product_id);
        $product_cost = $request->get('product_cost')??$product->sellingCost($request->weight);
        if($product->for_sale == 0){
            if($request->type == "+"){
                $request->validate([
                    'weight' => 'required',
//                    'grn' => 'required',
                ]);
            }else{
                $request->validate([
                    'weight' => 'required',
                ]);
            }
            $weight = $request->weight;
            if($request->type == '+'){
                $weight_after_transaction = $product->weight + $weight;
            }else{
                if($product->weight < $weight){
                    return redirect()->back()->with(__('message.invalid weight'));
                }
                $weight_after_transaction = $product->weight - $weight;
                $weight = $weight * (-1);
            }
            QtyHistory::create([
                'qty' => $product->qty,
                'qty_after_transaction' => $product->qty,
                'qty_before_transaction' => $product->qty,
                'product_id' => $request->product_id,
                'product_category_id' => $product->product_category_id,
                'user_id' => Auth::user()->id,
                'note' => $request->note??'',
                'grn' => $request->grn??'',
                'weight' => $weight,
                'weight_before_transaction' => $product->weight,
                'weight_after_transaction' => $weight_after_transaction,
                'is_adjustment' => $is_adjustment,
                'product_cost' => $product_cost,
            ]);
            $product->weight = $weight_after_transaction;
            $product->save();
        }else{
            if($request->type == "+"){
                $request->validate([
                    'qty' => 'required',
//                    'grn' => 'required',
                ]);
            }else{
                $request->validate([
                    'qty' => 'required',
                ]);
            }
            $qty = $request->qty;
            if($request->type == '+'){
                $qty_after_transaction = $product->qty+$qty;
            }else{
                if($product->qty<$qty){
                    return redirect()->back()->with("__('message.invalid qty')");
                }
                $qty_after_transaction = $product->qty-$qty;
                $qty = $qty*(-1);
            }
            QtyHistory::create([
                'qty' => $qty,
                'qty_after_transaction' => $qty_after_transaction,
                'qty_before_transaction' => $product->qty,
                'product_id' => $request->product_id,
                'product_category_id' => $product->product_category_id,
                'user_id' => Auth::user()->id,
                'note' => $request->note??'',
                'grn' => $request->grn??'',
                'is_adjustment' => $is_adjustment,
                'product_cost' => $product_cost,
            ]);
            $product->qty = $qty_after_transaction;
            $product->save();
        }
        return redirect()->route('products.index')->with("__('message.create successfully!')");
    }

    /**
     * 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);
        $product = $qtyHistory->product;
        if($product->for_sale){
            if($qtyHistory->qty>0){
                $product->qty = $product->qty-$qtyHistory->qty;
            }
            else{
                $product->qty = $product->qty+(-$qtyHistory->qty);
            }
        }
        else{
           // if($qtyHistory->weight_before_transaction<$qtyHistory->weight_after_transaction){
           //      $product->weight = $product->weight - $qtyHistory->weight;
           // }
           // else{
           //      $product->weight = $product->weight + $qtyHistory->weight;
           // }
           $product->weight = $qtyHistory->weight_before_transaction;
           $product->save();
        }       

        $product->save();

        $qtyHistory->delete();
        return redirect()->route('product-qty-history-option' , 'product_id=' . $product->id)->with('success' , __('message.deleted successfully'));
    }
    public function print(Request $request , $id)
    {
        $paginate = $request->get('paginate')??25;
        $q = $request->get('q')??null;
        $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];
        }
        $product_id = $request->get('product_id');
        $in_out = $request->in_out??0;
        if($product_id){
            $qtyHistories = QtyHistory::where('product_id' , $product_id)->whereBetween('created_at', [$start_date . " 00:00:00", $end_date . " 23:59:59"])->orderBy('id' , 'desc');
        }
        else{
            $qtyHistories = QtyHistory::whereBetween('created_at', [$start_date . " 00:00:00", $end_date . " 23:59:59"])->orderBy('id' , 'desc');
        }
        if($q){
            $qtyHistories = $qtyHistories->where('grn' , $q);
        }
        if($request->in_out > 0){
            $qtyHistories = $qtyHistories->where('qty' , '>' , 0)->paginate($paginate);
        }else if($request->in_out < 0){
            $qtyHistories = $qtyHistories->where('qty' , '<' , 0)->paginate($paginate);
        }else{
            $qtyHistories = $qtyHistories->paginate($paginate);
        }
        return view('product-qty-histories.print' , compact('qtyHistories' , 'product_id' , 'paginate' , 'in_out' , 'start_date' , 'end_date'));
    }
    public function print2(Request $request , $id=null)
    {
        $paginate = $request->get('paginate')??25;
        $for_sale = $request->get('for_sale');
        $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];
        }
        $products = Product::whereBetween('created_at', [$start_date . " 00:00:00", $end_date . " 23:59:59"])->where('for_sale' , $for_sale)->orderBy('id' , 'desc')->paginate($paginate);
        return view('product-qty-histories.print2' , compact('products' , 'for_sale'  , 'paginate'  , 'start_date' , 'end_date'));
    }
    public function export(Request $request , $id)
    {
        return Excel::download(new QtyTransactionExport($id,$request->get('in_out'),$request->get('start_date'),$request->get('end_date'),$request->get('paginate'),$request->get('q')),'QtyTransaction.xlsx');
    }
}

© KUJUNTI.ID