<?php

namespace App\Http\Controllers;
use App\Models\modelTransactionslistions;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Session;


class TransactionsListingController extends Controller
{

    public function index($Type)
    {


            try
            {
                $currencys=modelTransactionslistions::selectcurrency(Session::get('PropertyCode'));

                $Accounts=modelTransactionslistions::selectTenants(Session::get('PropertyCode'));
                if($Type=="Tenant")
            {
                $Accounts=modelTransactionslistions::selectTenants(Session::get('PropertyCode'));
            }
            else   if($Type=="Customer")
            {

                $Accounts=modelTransactionslistions::selectCustomer(Session::get('PropertyCode'));

            }
            else  if($Type=="Supplier")
            {
                $Accounts=modelTransactionslistions::selectSupplier(Session::get('PropertyCode'));

            }
            $name=  $Type;
            $code=  "0000";
            $openingBalance=0;
            $from="";
            $to="";
            $currnames="";

                $trans=modelTransactionslistions::selectTrans1(Session::get('PropertyCode'));

                return view('/Transactionlisting', compact('Accounts','Type','trans','name','code','currencys','from','to','currnames','openingBalance'));



        } catch (\Exception $e) {

            return redirect()->route('otheraccounts')->withErrors(['error' => 'An error occurred!!' . $e->getMessage()]);
        }

        }

        public function fetchTransactions(Request $request)
        {

            $request->validate([
                'Type' => 'required|string',
                 'from' => 'required|string',
                  'to' => 'required|string',
                  'curr' => 'required|string'

            ]);



            $Rate=1;
            $accounts = $request->input('account');
            $from = $request->input('from');
            $to = $request->input('to');
            $curr = $request->input('curr');
            $accounts = explode(':', $accounts);
            $AccCode = $accounts[0];
            $AccSubCode = isset($accounts[1]) ? $accounts[1] : '0';
            $Type = $request->input('Type');



            $currname=modelTransactionslistions::selectcurrencyName(Session::get('PropertyCode'),$curr);
            $currnames=$currname[0]->Name;

            $Accounts=modelTransactionslistions::selectTenants(Session::get('PropertyCode'));

            if($Type=="Tenant")
            {
                $Accounts=modelTransactionslistions::selectTenants(Session::get('PropertyCode'));
            }
            else   if($Type=="Customer")
            {
                $Accounts=modelTransactionslistions::selectCustomer(Session::get('PropertyCode'));
            }
            else  if($Type=="Supplier")
            {
                $Accounts=modelTransactionslistions::selectSupplier(Session::get('PropertyCode'));

            }
            $name=  $Accounts[0]->AccName;
            $code=  $Accounts[0]->AccSubCode;
            $currencys=modelTransactionslistions::selectcurrency(Session::get('PropertyCode'));
            $openingBalance=0;

            $trans=modelTransactionslistions::selectTrans(Session::get('PropertyCode'),$AccCode,$AccSubCode,$curr, $from , $to, $Type);
            $transOpening=$trans;

            $selectHomeCurrency=modelTransactionslistions::selectHomeCurrency(Session::get('PropertyCode'));

            if(count($selectHomeCurrency)>0)
            {
                $homeCurrency=$selectHomeCurrency[0]->Currency;
                if($curr==$homeCurrency)
                {


                    $trans=modelTransactionslistions::selectHomeTrans(Session::get('PropertyCode'),$AccCode,$AccSubCode,$curr, $from , $to, $Type);

                    $transOpening=modelTransactionslistions::selectHomeTransOpening(Session::get('PropertyCode'),$AccCode,$AccSubCode,$curr, $from , $to, $Type);
                    $openingBalance=$transOpening[0]->total;


                }
                else
                {
                    $myRateValidation=modelTransactionslistions::selectRate(Session::get('PropertyCode'),$curr,$AccCode,$AccSubCode, $from, $to);

                    if(count($myRateValidation)==0)
                    {
                        return redirect()->route('Transactionlisting',['Type'=>$Type])->withErrors(['error' => 'Cannot Run This Report With The Selected Currency' ]);
                    }
                    else
                    {

                    $trans=modelTransactionslistions::selectTrans(Session::get('PropertyCode'),$AccCode,$AccSubCode,$curr, $from , $to, $Type);
                    $transOpening=modelTransactionslistions::selectTransOpeningBalance(Session::get('PropertyCode'),$AccCode,$AccSubCode,$curr, $from , $to, $Type);
                    $openingBalance=$transOpening[0]->total;

                }

                }

            }
            else
            {
                return redirect()->route('Transactionlisting',['Type'=>$Type])->withErrors(['error' => 'Home Currency Not Found' ]);

            }













            return view('/Transactionlisting', compact('Accounts','Type','trans','name','code','currencys','from','to','currnames','openingBalance'));

        }



}
