<?php
function generateACCategoryCode($group_id)
{
  $ci = &get_instance();
  $acgroup_code = $ci->db->where("acgroup_id=$group_id")->get("tbl_acgroups")->row()->acgroup_code;
  $lastACCategoryCode = $ci->db->select_max("accategory_code")->where("acgroup_id=$group_id")->get("tbl_accategories")->row()->accategory_code;
  $category_part = substr($lastACCategoryCode, 1);

  $category_part = (float)$category_part + 1;
  //if($category_part<10)$category_part="00".$category_part;
  if ($category_part < 10) $category_part = "00" . $category_part;
  elseif ($category_part < 100) $category_part = "0" . $category_part;
  return $acgroup_code . $category_part;
}
function generateAccountCode($category_id)
{
  $ci = &get_instance();
  $accategory_code = $ci->db->where("accategory_id=$category_id")->get("tbl_accategories")->row()->accategory_code;
  $lastAccountCode = $ci->db->select_max("account_code")->where("accategory_id=$category_id")->get("tbl_accounts")->row()->account_code;
  $account_part = substr($lastAccountCode, 4);

  $account_part = (float)$account_part + 1;
  //if($category_part<10)$category_part="00".$category_part;
  if ($account_part < 10) $account_part = "00" . $account_part;
  elseif ($account_part < 100) $account_part = "0" . $account_part;
  return $accategory_code . $account_part;
}
function showFiscalYear()
{
  $ci = &get_instance();
  echo getFieldfromValue("tbl_fiscalyear", "fiscalyear_year", "fiscalyear_id", $ci->session->userdata("FiscalYearID"));
}
function FYStart()
{
  $ci = &get_instance();
  // echo $ci->session->userdata("FiscalYearID");die;asd;
  $FY = getFieldfromValue("tbl_fiscalyear", "fiscalyear_from", "fiscalyear_id", $ci->session->userdata("FiscalYearID"));
  // echo NepaliDate($FY);die;
  return NepaliDate($FY);
}
function FYEnd()
{
  $ci = &get_instance();
  $FY = getFieldfromValue("tbl_fiscalyear", "fiscalyear_to", "fiscalyear_id", $ci->session->userdata("FiscalYearID"));
  return NepaliDate($FY);
}
function showAccountCategoriesSelector($fieldName, $fieldID, $condition = "1=1", $selectedID = '', $additionalClass = "")
{
  $ci = &get_instance();
  $AccountCategories = $ci->db->where("status", 1)->where($condition)->get("tbl_accategories")->result();
  $isRequired = (stripos($additionalClass, "required") !== false); // Check if "required" exists in $additionalClass
  $html = "<select name='$fieldName' id='$fieldID' class='select2 $additionalClass'";
  if ($isRequired) {
    $html .= " required";
  }
  $html .= ">";

  $html .= "<option value='' " . (($selectedID == '') ? "SELECTED" : "") . ">Select Account Category</option>";

  foreach ($AccountCategories as $AccountCategory) {
    $html .= "<option value='$AccountCategory->accategory_id'" . (($selectedID == $AccountCategory->accategory_id) ? "selected" : "") . ">" . getFieldfromValue("tbl_acgroups", "acgroup_name", "acgroup_id", $AccountCategory->acgroup_id) . " >> $AccountCategory->accategory_name</option>";
  }

  $html .= "</select>";
  echo $html;
}
function showAccountCategoriesByIds($AccountCategories = array())
{
  // pre($AccountCategories); //die;
  $ci = &get_instance();

  if ($AccountCategories) {
    $AccountCategories_a = $ci->db->where("status", 1);
    $AccountCategories_a = $AccountCategories_a->where_in("accategory_id", $AccountCategories);
    $AccountCategories_a = $AccountCategories_a->get("tbl_accategories")->result();
    $html = array();
    foreach ($AccountCategories_a as $AccountCategory) {
      $html[] = $AccountCategory->accategory_name;
    }
    echo implode(", ", $html);
  }
}


function linkVoucher($voucher_id)
{
  if($voucher_id!=0) {
  $ci = &get_instance();
  $Voucher = $ci->db->where("voucher_id", $voucher_id)->get("tbl_vouchers")->row();
  $Name = $ci->db->where("vouchertype_id", $Voucher->voucher_type)->get("tbl_vouchertypes")->row()->voucher_type . " Voucher # " . $Voucher->voucher_no;
  echo "<a class='link' target='_blank' href='" . site_url("accounts/vouchers/show_voucher/" . $voucher_id) . "'>$Name</a>";
  } else {
    echo "";
  }
}
function linkLedger($Account)
{

  $ci = &get_instance();
  echo "<a class='link' target='_blank' href='" . site_url("accounts/ledger/partyledger?from_date=" . FYStart() . "&to_date=" . FYEnd() . "&account_id=" . $Account->account_id . "&show_ledger=true") . "'>$Account->account_name</a>";
}
function linkGroup($Group)
{
  $ci = &get_instance();
  echo "<a class='link' target='_blank' href='" . site_url("accounts/reports/balance_by_category/") . "?group=" . $Group->acgroup_id . "'>$Group->acgroup_name</a>";
}
function linkCategory($Category)
{
  $ci = &get_instance();
  if ($Category) :
    echo "<a class='link' target='_blank' href='" . site_url("accounts/reports/balance_by_group/") . "?category=" . $Category->accategory_id . "'>$Category->accategory_name</a>";
  endif;
}
function myCurrency($number, $inline = false)
{
  if ($number == 0) {
    if (!$inline)
      return "<span class='currency text-right'>&nbsp;</span>";
    else
      return "<span class='text-right'>&nbsp;</span>";
  }
  if ($number < 0) return "<span class='currency text-right'>(" . number_format(abs($number), 2, ".", ",") . ")</span>";
  if (!$inline)
    return "<span class='currency text-right'>" . number_format($number, 2, ".", ",") . "</span>";
  else
    return "<span class='text-right'>" . number_format($number, 2, ".", ",") . "</span>";
}
function showNill()
{
  return "<span class='currency text-right'>Nill</span>";
}
function isVoucherExists($voucher_no)
{
  $ci = &get_instance();
  return ($ci->db->where("voucher_no", $voucher_no)->get("tbl_vouchers")->num_rows() > 0) ? true : false;
}
function isVoucherReverseable($voucher_id)
{
  $ci = &get_instance();
  return ($ci->db->select("voucher_state")->where("voucher_id", $voucher_id)->get("tbl_vouchers")->row()->voucher_state == "Entered") ? true : false;
}
function generateVoucherNo($vouchertype_id = "")
{
  $ci = &get_instance();
  $voucherTypeCondition = ($vouchertype_id != "") ? " WHERE voucher_type = '$vouchertype_id'" : "";

  $query = $ci->db->query("SELECT MAX(voucher_no) AS voucher_no FROM tbl_vouchers" . $voucherTypeCondition);
  $row = $query->row();

  $newVoucherNo = ($row->voucher_no !== null) ? $row->voucher_no + 1 : 1;

  return $newVoucherNo;
}

function countChildCategories($acgroup_id)
{
  $ci = &get_instance();
  return $ci->db->where("status", 1)->where("acgroup_id", $acgroup_id)->get("tbl_accategories")->num_rows();
}
function countChildAccounts($accategory_id)
{
  $ci = &get_instance();
  return $ci->db->where("status", 1)->where("accategory_id", $accategory_id)->get("tbl_accounts")->num_rows();
}
function getBalance($account_id)
{
  $drTotal = getDrTotal($account_id);
  $crTotal = getCrTotal($account_id);
  return $drTotal - $crTotal;
}
function getDrTotal($account_id)
{
  $ci = &get_instance();
  return $ci->db->where("status", 1)->where("account_id", $account_id)->select_sum('dr')->get("tbl_voucherdetails")->row()->dr;
}
function getCrTotal($account_id)
{
  $ci = &get_instance();
  return $ci->db->where("status", 1)->where("account_id", $account_id)->select_sum('cr')->get("tbl_voucherdetails")->row()->cr;
}