<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class modelAccounts extends Model
{
    public static function selectAccountsTypes($code)
    {

        return DB::select("SELECT * FROM `accountstypes` where `Code`='$code'");
    }

    public static function selectBanks($code)
    {

        return DB::select("SELECT * FROM `banks` where `Code`='$code'");
    }


    public static function selectValidateSubAccounts($code, $AccCode, $SubAccCode)
    {

        return DB::select("SELECT * FROM `subaccounts` where `Code`='$code' and `AccCode`='$AccCode' and `SubCode`='$SubAccCode'");
    }

    public static function selectMainAccounts($code)
    {

        return DB::select("SELECT mainaccounts.ID as ID,accountstypes.ID as acctype, mainaccounts.AccCode,mainaccounts.AccName,accountstypes.Name FROM `mainaccounts` inner join accountstypes on accountstypes.ID=mainaccounts.Acctype where  mainaccounts.Code='$code' order by mainaccounts.AccCode asc");
    }

    public static function selectSubAccounts($code, $AccCode)
    {

        return DB::select("SELECT subaccounts.ID, subaccounts.SubCode,subaccounts.SubName FROM `mainaccounts` inner join subaccounts on subaccounts.AccCode=mainaccounts.AccCode where  mainaccounts.`Code`='$code' and subaccounts.AccCode='$AccCode' order by subaccounts.AccCode asc");
    }
    public static function selectGenerateCode($code)
    {

        return DB::select("SELECT AccCode FROM `mainaccounts` where `Code`='$code' order by AccCode desc limit 1");
    }
    public static function selectGenerateSubCode($code,$AccCode)
    {

        return DB::select("SELECT SubCode FROM `subaccounts` where `Code`='$code' and AccCode='$AccCode' order by SubCode desc limit 1");
    }
    public static function SellectAccType($code,$name)
    {

        return DB::select("SELECT * FROM `accountstypes` where `Code`='$code' and Name='$name'");
    }
    public static function saveAccountType($code,$name)
    {
        return DB::insert("INSERT INTO `accountstypes` ( `Code`, `Name`) VALUES (?, ?)", [$code, $name] );
    }
    public static function saveMainAccount($code,$type,$AccCode, $AccName)
    {
        return DB::insert("INSERT INTO `mainaccounts` ( `Code`, `Acctype`, `AccCode`, `AccName`) VALUES (?, ?,?, ?)", [$code, $type,$AccCode,$AccName] );
    }

    public static function saveSubAccount($code,$AccCode,$SubAccCode, $SubAccName)
    {
        return DB::insert("INSERT INTO `subaccounts` ( `Code`, `AccCode`, `SubCode`, `SubName`) VALUES (?, ?,?, ?)", [$code, $AccCode,$SubAccCode,$SubAccName] );
    }
    public static function editSubAccount($id,$code,$AccCode,$SubAccCode, $SubAccName)
    {
        return DB::insert("UPDATE  `subaccounts` set   `SubName`=?  where Code=? and AccCode=? and SubCode=? and ID=?", [$SubAccName,$code, $AccCode,$SubAccCode,$id] );
    }

    public static function deleteSubAccount($id,$code,$AccCode)
    {
        return DB::insert("DELETE FROM   `subaccounts`  where Code=? and AccCode=?  and ID=?", [$code, $AccCode,$id] );
    }
    public static function editAccountType($code,$name, $id)
    {
        return DB::insert("UPDATE `accountstypes` set  `Name`= '$name' where ID='$id' and  `Code`='$code'"  );
    }

    public static function editMainAccount( $code,$id, $type, $AccCode, $AccName)
    {
        return DB::update("UPDATE `mainaccounts` SET  `Acctype` = ?, `AccCode` = ?, `AccName` = ? WHERE `ID` = ? and `Code` = ? ", [ $type, $AccCode, $AccName, $id,$code]);
    }

    public static function deleteMainAccount( $code,$id)
    {
        return DB::update("DELETE FROM `mainaccounts`  WHERE `ID` = ? and `Code` = ? ", [$id,$code]);
    }


    public static function deleteAccoutType($code,$ID)
    {
        return DB::delete("DELETE from `accountstypes` where `Code`='$code' and `ID`='$ID'");
    }


    public static function saveBankAccount($code,$acc,$name)
    {
        return DB::insert("INSERT INTO `banks` ( `Code`, `Name`, `Account`) VALUES (?, ?,?)", [$code, $name,$acc] );
    }

    public static function editBank($id,$code,$acc,$name)
    {
        return DB::insert("UPDATE  `banks` set  `Name`=? , `Account`=? where Code=? and ID=?", [ $name,$acc,$code,$id] );
    }

    public static function deleteBank($id,$code)
    {
        return DB::insert("DELETE FROM   `banks`  where Code=? and ID=?", [$code,$id] );
    }

}
