<?php

namespace App\Models;

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

class modelProcessCashBook extends Model
{
    public static function saveTransaction($Code,$Entrydate,$AccCode,$AccSubCode,$ContraCode,$ContraSubCode,$narr,$reff, $DR,$CR,$user,$src,$tax,$taxid,$disc,$qty,$price,$total,$batch,$curr,$Rate,$mdes,$ttype)
    {
        return DB::insert("INSERT INTO `transactions`(`Code`, `EntryDate`, `Narration`, `Refference`, `Accode`, `AccSubCode`, `ContraCode`, `ContraSubCode`, `DR`, `CR`, `Addedby`, `Source`, `Qnty`, `Price`, `Tax`, `TaxID`, `Discount`, `Total`, `Batch`,Currency,Rate,MNarration,TType)
        VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?,?,?,?)", [$Code, $Entrydate, $narr, $reff, $AccCode, $AccSubCode, $ContraCode, $ContraSubCode, $DR, $CR, $user, $src,$qty,$price,$tax,$taxid,$disc,$total,$batch,$curr,$Rate,$mdes,$ttype] );
    }
    public static function selectgl($code)
    {

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

    public static function selectgls($code)
    {
        return DB::select("SELECT  IF(subaccounts.SubCode IS NOT NULL, CONCAT(mainaccounts.AccCode, '/', subaccounts.SubCode), mainaccounts.AccCode) AS AccCode, IF(subaccounts.SubName IS NOT NULL, CONCAT(mainaccounts.AccName, '/', subaccounts.SubName), mainaccounts.AccName) AS AccName FROM mainaccounts LEFT JOIN subaccounts ON mainaccounts.AccCode = subaccounts.AccCode and mainaccounts.Code=subaccounts.Code where mainaccounts.Code='$code'");

    }
      public static function selectSuppliers($code)
    {

        return DB::select("SELECT  `AccCode` as AccCode1, `SupplierCode` as AccCode, `Name` FROM `suppliers` where `Code`='$code'");
    }

    public static function selectcurrency($code)
    {
        return DB::select("SELECT * FROM currencys where Code='$code'");

    }
    public static function selectTransReff($code, $reff)
    {
        return DB::select("SELECT * FROM transactions where Code='$code' and Batch='$reff' limit 1");

    }
    public static function selectHomeCurrency($code)
    {
        return DB::select("SELECT * FROM homecurrency where Code='$code'");

    }
    public static function selectRate($code,$currency)
    {
        return DB::select("SELECT * FROM `rates` WHERE Code='$code' and Currency='$currency'  order by EntryDate desc limit 1");

    }

}
