<?php

namespace App\Http\Controllers;

use App\Models\modelProcessJournal;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Session;

class ProcessJournalController extends Controller
{
    public function index( $Type)
    {

            try
            {
                $journal="";

                $currencys=modelProcessJournal::selectcurrency(Session::get('PropertyCode'));
                $Accounts=modelProcessJournal::selectTenants(Session::get('PropertyCode'));
                $Jts=modelProcessJournal::selectJournalTransactions(Session::get('PropertyCode'),Session::get('RegNo'),$Type);
                $journal=$Accounts[0]->AccCode;

                $JournalType=$Type;

                if($JournalType=="Tenant")
                {
                    $Accounts=modelProcessJournal::selectTenants(Session::get('PropertyCode'));
                    $journal=$Accounts[0]->AccCode;
                }
                else if($JournalType=="Customer")
                {
                    $Accounts=modelProcessJournal::selectCustomers(Session::get('PropertyCode'));
                    $journal=$Accounts[0]->AccCode;
                }
                else if($JournalType=="Supplier")
                {
                    $Accounts=modelProcessJournal::selectSuppliers(Session::get('PropertyCode'));
                    $journal=$Accounts[0]->AccCode;
                }
                else if($JournalType=="GL")
                {
                    $Accounts=modelProcessJournal::selectGL(Session::get('PropertyCode'));
                    $journal=$Accounts[0]->Code;
                    $JournalType="General";
                }

                return view('/ProcessJournal', compact('JournalType','Accounts','journal','Jts','currencys'));

        } catch (\Exception $e) {

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

        }

        public function savejournaltransaction(Request $request)
        {
           // Validate the request data
                $request->validate([
                    'source' => 'required|string',
                    'AccCode' => 'required|string',
                    'date' => 'required|string',
                    'narr' => 'required|string',
                    'reff' => 'required|string',
                    'tr' => 'required|string',
                    'account' => 'required|string',
                    'amount' => 'required|string'
                ]);

                $src = $request->input('source');
                $AccCode = $request->input('AccCode');
                $date = $request->input('date');
                $narr = $request->input('narr');
                $reff = $request->input('reff');
                $tr = $request->input('tr');
                $ContraCode = $request->input('account');
                $amount = $request->input('amount');
                $user=Session::get('RegNo');

                $AccSubCode="0";
                $ContraSubCode="0";
                $contra = $request->input('contra');

                if($contra=="Reff")
                {

                    $TransReff=modelProcessCashBook::selectTransReff(Session::get('PropertyCode'),($request->input('reff')));
                if(count($TransReff)>0)
                {

                    $ContraCode =$TransReff[0]->Accode;
                    $ContraSubCode = $TransReff[0]->AccSubCode;

                }

                else
                {

                    return redirect()->route('processjournal')->withErrors(['error' => 'Refference Not Found' ]);
                }

                }
                else
                {

                    $parts = explode('/', $contra);
                    $ContraCode = $parts[0];
                    $ContraSubCode = isset($parts[1]) ? $parts[1] : '0';

            }

                $Rate=1;
                $curr = $request->input('curr');
                $tr = $request->input('tr');
                $id= "0";
                $disc="0";
                $src = "CashBook";
                $AccCode = $request->input('account');
                $date = $request->input('date');
                $narr = $request->input('narr');
                $reff = $request->input('reff');
                $amount =  $request->input('amount');
                $price ="0";
                $AccSubCode = "0";
                $tax="0";
                $taxid="0";
                $qty = "0";
                $gl = "0";
                $batch="CashBook";
                $mdes="CashBook";
                $user=Session::get('RegNo');

                $DR=$amount;
                $CR=0;
                $total="0";
                $selectHomeCurrency=modelProcessCashBook::selectHomeCurrency(Session::get('PropertyCode'));
                if(count($selectHomeCurrency)>0)
                {

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

                    }
                    else
                    {

                        $selectRate=modelProcessCashBook::selectRate(Session::get('PropertyCode'),$curr);

                        if(count($selectRate)>0)
                        {

                            $Rate=$selectRate[0]->Rate;

                            $DR= $DR/$Rate;

                        }
                        else
                        {
                            return redirect()->route('receipt',['Type'=>$src])->withErrors(['error' => 'No rate set for this currency' ]);

                        }

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

                if($tr=="DR")
                {

                    $CR=$DR;
                    $DR="0";

                }
                else
                if($tr=="CR")
                {

                    $CR="0";
                    $DR=$DR;

                }
                try
                {
                    $DR="0";
                    $CR="0";
                    if($tr=="DR")
                    {
                        $DR=$amount;
                        $CR="0";
                    }
                    else if($tr=="CR")
                    {
                        $DR="0";
                        $CR=$amount;

                    }
                $result = modelProcessJournal::saveJournal(Session::get('PropertyCode'),$AccCode,$AccSubCode,$ContraCode,$ContraSubCode, $date,$narr,$reff, $DR,$CR,$user,$src);
                if ($result)
                {
                     return redirect()->route('processjournal')->with('success', 'Proccessed Successfully.');
               }
                else
                {
                    return redirect()->route('processjournal')->withErrors(['error' => 'Failed to Proccess.']);
                }

            } catch (\Exception $e) {

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

            }

        }

