<?php

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

class ModulesController extends Controller
{
    public function index()
    {
        $cashbooks = modelModules::selectcashbook(Session::get('PropertyCode'));
        $Accounts = modelModules::selectAccounts(Session::get('PropertyCode'));

        return view('/CashBook', compact('cashbooks','Accounts'));

    }

    public function addcashbook(Request $request)
    {
       // Validate the request data
            $request->validate([
                'name' => 'required|string',
                 'account' => 'required|string'
            ]);

            $name = $request->input('name');
            $account = $request->input('account');


            try
            {


            $exists = modelModules::selectcashbookByCode(Session::get('PropertyCode'),$account);

            if ($exists)
            {

                return redirect()->route('cashbook')->withErrors(['error' => 'Cashbook with '.$account.' already exists.']);
            }

            $result = $accounts = modelModules::savecashbook(Session::get('PropertyCode'), $account,$name);

            if ($result)
            {
                 return redirect()->route('cashbook')->with('success', 'Cash Book Saved successfully.');
           }
            else
            {
                return redirect()->route('cashbook')->withErrors(['error' => 'Failed to save Cash Book.']);
            }

        } catch (\Exception $e) {

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

        }

        public function editcashbook(Request $request)
        {
           // Validate the request data
                $request->validate([
                    'name' => 'required|string',
                     'account' => 'required|string',
                     'id' => 'required|string'
                ]);

                $account = $request->input('account');
                $name = $request->input('name');
                $id = $request->input('id');


                try
                {


                $exists = DB::table('cashbook')->where('ID', $id)->exists();

                if ($exists)
                {


                $result =  modelModules::editcashbook($id,Session::get('PropertyCode'), $account,$name);

                if ($result)
                {
                     return redirect()->route('cashbook')->with('success', 'Cash Book Updated Successfully.');
               }
                else
                {
                    return redirect()->route('cashbook')->withErrors(['error' => 'Failed to Update Cash Book.']);
                }


                }
                else
                {

                    return redirect()->route('cashbook')->withErrors(['error' => 'Cash Book Not Found.']);


                }



            } catch (\Exception $e) {

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

            }
            public function deletecashbook( $id)
        {


                try
                {


                $exists = DB::table('cashbook')->where('ID', $id)->exists();

                if ($exists)
                {


                $result =modelModules::deletecashbook($id,Session::get('PropertyCode'));

                if ($result)
                {
                     return redirect()->route('cashbook')->with('success', 'Cash Book Removed Successfully.');
               }
                else
                {
                    return redirect()->route('cashbook')->withErrors(['error' => 'Failed to Remove Cash Book.']);
                }


                }
                else
                {

                    return redirect()->route('cashbook')->withErrors(['error' => 'Cash Book Not Found.']);


                }



            } catch (\Exception $e) {

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

            }




            public function journalsindex()
            {
                $cashbooks = modelModules::selectjournals(Session::get('PropertyCode'));
                $Accounts = modelModules::selectAccounts(Session::get('PropertyCode'));

                return view('/Journals', compact('cashbooks','Accounts'));

            }

            public function addjournals(Request $request)
            {
               // Validate the request data
                    $request->validate([
                        'source' => 'required|string',
                         'account' => 'required|string'
                    ]);

                    $name = $request->input('source');
                    $account = $request->input('account');


                    try
                    {
                    $exists = modelModules::selectjournalsByCode(Session::get('PropertyCode'),$account);

                    if ($exists)
                    {

                        return redirect()->route('journals')->withErrors(['error' => 'Journal with '.$account.' already exists.']);
                    }

                    $result = $accounts = modelModules::savejournals(Session::get('PropertyCode'), $account,$name);

                    if ($result)
                    {
                         return redirect()->route('journals')->with('success', 'Journal Saved successfully.');
                   }
                    else
                    {
                        return redirect()->route('journals')->withErrors(['error' => 'Failed to save Journal.']);
                    }

                } catch (\Exception $e) {

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

                }

                public function editjournals(Request $request)
                {
                   // Validate the request data
                        $request->validate([
                            'name' => 'required|string',
                             'account' => 'required|string',
                             'id' => 'required|string'
                        ]);

                        $account = $request->input('account');
                        $name = $request->input('name');
                        $id = $request->input('id');


                        try
                        {


                        $exists = DB::table('journals')->where('ID', $id)->exists();

                        if ($exists)
                        {


                        $result =  modelModules::editjournals($id,Session::get('PropertyCode'), $account,$name);

                        if ($result)
                        {
                             return redirect()->route('journals')->with('success', 'Journal Updated Successfully.');
                       }
                        else
                        {
                            return redirect()->route('journals')->withErrors(['error' => 'Failed to Update Journal.']);
                        }


                        }
                        else
                        {

                            return redirect()->route('journals')->withErrors(['error' => 'Journal Not Found.']);


                        }



                    } catch (\Exception $e) {

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

                    }
                    public function deletejournals( $id)
                {


                        try
                        {


                        $exists = DB::table('journals')->where('ID', $id)->exists();

                        if ($exists)
                        {


                        $result =modelModules::deletejournals($id,Session::get('PropertyCode'));

                        if ($result)
                        {
                             return redirect()->route('journals')->with('success', 'Journal Removed Successfully.');
                       }
                        else
                        {
                            return redirect()->route('journals')->withErrors(['error' => 'Failed to Remove Journal.']);
                        }


                        }
                        else
                        {

                            return redirect()->route('journals')->withErrors(['error' => 'Journal Not Found.']);


                        }



                    } catch (\Exception $e) {

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

                    }



                    public function otheraccountsindex()
                    {
                        $cashbooks = modelModules::selectotheraccounts(Session::get('PropertyCode'));
                        $Accounts = modelModules::selectAccounts(Session::get('PropertyCode'));

                        return view('/OtherAccounts', compact('cashbooks','Accounts'));

                    }

                    public function addotheraccounts(Request $request)
                    {
                       // Validate the request data
                            $request->validate([
                                'name' => 'required|string',
                                 'account' => 'required|string'
                            ]);

                            $name = $request->input('name');
                            $account = $request->input('account');


                            try
                            {

                                $exists = DB::table('otheraccounts')->where('Name', $name)  ->where('Code', Session::get('PropertyCode'))->exists();

                                if ($exists)
                                {
                                    return redirect()->route('otheraccounts')->withErrors(['error' => 'Transaction Type Already Exist.']);
                                }

                            $result = $accounts = modelModules::saveotheraccounts(Session::get('PropertyCode'), $account,$name);

                            if ($result)
                            {
                                 return redirect()->route('otheraccounts')->with('success', 'Account Saved successfully.');
                           }
                            else
                            {
                                return redirect()->route('otheraccounts')->withErrors(['error' => 'Failed to save Account.']);
                            }

                        } catch (\Exception $e) {

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

                        }

                        public function editotheraccounts(Request $request)
                        {
                           // Validate the request data
                                $request->validate([
                                    'name' => 'required|string',
                                     'account' => 'required|string',
                                     'id' => 'required|string'
                                ]);

                                $account = $request->input('account');
                                $name = $request->input('name');
                                $id = $request->input('id');


                                try
                                {


                                $exists = DB::table('otheraccounts')->where('ID', $id)->exists();

                                if ($exists)
                                {


                                $result =  modelModules::editotheraccounts($id,Session::get('PropertyCode'), $account,$name);

                                if ($result)
                                {
                                     return redirect()->route('otheraccounts')->with('success', 'Account Updated Successfully.');
                               }
                                else
                                {
                                    return redirect()->route('otheraccounts')->withErrors(['error' => 'Failed to Update Account.']);
                                }


                                }
                                else
                                {

                                    return redirect()->route('otheraccounts')->withErrors(['error' => 'Account Not Found.']);


                                }



                            } catch (\Exception $e) {

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

                            }
                            public function deleteotheraccounts( $id)
                        {


                                try
                                {


                                $exists = DB::table('otheraccounts')->where('ID', $id)->exists();

                                if ($exists)
                                {


                                $result =modelModules::deleteotheraccounts($id,Session::get('PropertyCode'));

                                if ($result)
                                {
                                     return redirect()->route('otheraccounts')->with('success', 'Account Removed Successfully.');
                               }
                                else
                                {
                                    return redirect()->route('otheraccounts')->withErrors(['error' => 'Failed to Remove Account.']);
                                }


                                }
                                else
                                {

                                    return redirect()->route('otheraccounts')->withErrors(['error' => 'Account Not Found.']);


                                }



                            } catch (\Exception $e) {

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

                            }














}
