<?php

function hasPermission($id, $permission){
	global $conn;
    $select = "SELECT * FROM `roles` WHERE FIND_IN_SET('$permission' ,`permissions`) AND `id` ='$id'"; 
    $res  = mysqli_query($conn,$select);
    $rows   = mysqli_num_rows($res); 

    if($rows == 1){
      	//We will do something later :)
    	return true;
    }else{
      //echo '<script>window.location.href="'. BASE_URL .'index.php?page=no_permission.php"</script>';
      return false;
    }
}

function checkLogin(){
  $current_page = explode('/', ltrim($_SERVER['REQUEST_URI'], '/'));
  $current_page = end($current_page);
  $by_pass_pages = array('login.php');
  if( !in_array($current_page, $by_pass_pages) && (!isset($_SESSION['is_logged_in']) || $_SESSION['is_logged_in'] == FALSE) ) {
    header("Location:login.php");
  }
}

function  vendorCode( $conn ){
  $last_pooja   = "SELECT code FROM vendor_master WHERE code != '' ORDER BY id DESC LIMIT 1";
  $last_result  = mysqli_query($conn, $last_pooja);

  $last_id = 0;
  if( mysqli_num_rows( $last_result ) == 1 ){
    $last_row     = mysqli_fetch_row( $last_result );

    $temp = explode('-', $last_row[0]);

    if( isset($temp[1]) && $temp[1] != ''){
      $last_id = $temp[1];
    }
  }
  $flag       = 'V';
  $receipt_no = $flag . '-' . str_pad(($last_id + 1), 4, '0', STR_PAD_LEFT);

  return $receipt_no;
}

function  employeeCode( $conn ){
  $last_pooja   = "SELECT code FROM emp_mst WHERE code != '' ORDER BY id DESC LIMIT 1";
  $last_result  = mysqli_query($conn, $last_pooja);

  $last_id = 0;
  if( mysqli_num_rows( $last_result ) == 1 ){
    $last_row     = mysqli_fetch_row( $last_result );

    $temp = explode('-', $last_row[0]);

    if( isset($temp[1]) && $temp[1] != ''){
      $last_id = $temp[1];
    }
  }
  $flag       = 'E';
  $receipt_no = $flag . '-' . str_pad(($last_id + 1), 4, '0', STR_PAD_LEFT);

  return $receipt_no;
}

function  accountCode( $conn ){
  $last_pooja   = "SELECT act_code FROM act_master WHERE act_code != '' ORDER BY id DESC LIMIT 1";
  $last_result  = mysqli_query($conn, $last_pooja);

  $last_id = 0;
  if( mysqli_num_rows( $last_result ) == 1 ){
    $last_row     = mysqli_fetch_row( $last_result );

    $temp = explode('-', $last_row[0]);

    if( isset($temp[1]) && $temp[1] != ''){
      $last_id = $temp[1];
    }
  }
  $flag       = 'ACT';
  $receipt_no = $flag . '-' . str_pad(($last_id + 1), 4, '0', STR_PAD_LEFT);

  return $receipt_no;
}

function  turckCode( $conn ){
  $last_pooja   = "SELECT truck_code FROM truck_mst WHERE truck_code != '' ORDER BY id DESC LIMIT 1";
  $last_result  = mysqli_query($conn, $last_pooja);

  $last_id = 0;
  if( mysqli_num_rows( $last_result ) == 1 ){
    $last_row     = mysqli_fetch_row( $last_result );

    $temp = explode('-', $last_row[0]);

    if( isset($temp[1]) && $temp[1] != ''){
      $last_id = $temp[1];
    }
  }
  $flag       = 'TRUCK';
  $receipt_no = $flag . '-' . str_pad(($last_id + 1), 4, '0', STR_PAD_LEFT);

  return $receipt_no;
}

function  driveCode( $conn ){
  $last_pooja   = "SELECT driver_code FROM driver_mst WHERE driver_code != '' ORDER BY id DESC LIMIT 1";
  $last_result  = mysqli_query($conn, $last_pooja);

  $last_id = 0;
  if( mysqli_num_rows( $last_result ) == 1 ){
    $last_row     = mysqli_fetch_row( $last_result );

    $temp = explode('-', $last_row[0]);

    if( isset($temp[1]) && $temp[1] != ''){
      $last_id = $temp[1];
    }
  }
  $flag       = 'D';
  $receipt_no = $flag . '-' . str_pad(($last_id + 1), 4, '0', STR_PAD_LEFT);

  return $receipt_no;
}

function  maintancetypeCode( $conn ){
  $last_pooja   = "SELECT maintance_type_code FROM maintance_type WHERE maintance_type_code != '' ORDER BY id DESC LIMIT 1";
  $last_result  = mysqli_query($conn, $last_pooja);

  $last_id = 0;
  if( mysqli_num_rows( $last_result ) == 1 ){
    $last_row     = mysqli_fetch_row( $last_result );

    $temp = explode('-', $last_row[0]);

    if( isset($temp[1]) && $temp[1] != ''){
      $last_id = $temp[1];
    }
  }
  $flag       = 'MT';
  $receipt_no = $flag . '-' . str_pad(($last_id + 1), 4, '0', STR_PAD_LEFT);

  return $receipt_no;
}

function  maintanceCode( $conn ){
  $last_pooja   = "SELECT code FROM maintance WHERE code != '' ORDER BY id DESC LIMIT 1";
  $last_result  = mysqli_query($conn, $last_pooja);

  $last_id = 0;
  if( mysqli_num_rows( $last_result ) == 1 ){
    $last_row     = mysqli_fetch_row( $last_result );

    $temp = explode('-', $last_row[0]);

    if( isset($temp[1]) && $temp[1] != ''){
      $last_id = $temp[1];
    }
  }
  $flag       = 'M';
  $receipt_no = $flag . '-' . str_pad(($last_id + 1), 4, '0', STR_PAD_LEFT);

  return $receipt_no;
}

/*function  lrCode( $conn ){
  //$last_pooja   = "SELECT lr_no FROM lr_mst WHERE lr_no != '' ORDER BY id DESC LIMIT 1";
  $last_pooja   = "SELECT MAX(lr_no) FROM lr_mst WHERE lr_no != '' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."' ORDER BY id DESC LIMIT 1";
  $last_result  = mysqli_query($conn, $last_pooja);

  $last_id = 0;
  if( mysqli_num_rows( $last_result ) == 1 ){
    $last_row     = mysqli_fetch_row( $last_result );

    $temp = explode('-', $last_row[0]);

    if( isset($temp[1]) && $temp[1] != ''){
      $last_id = $temp[1];
    }
  }
  $flag       = 'LR';
  $receipt_no = $flag . '-' . str_pad(($last_id + 1), 4, '0', STR_PAD_LEFT);

  return $receipt_no;
}*/

function lrCode( $conn ){
  $branch_mst     = "SELECT * FROM `branch_mst` WHERE `id` ='".$_SESSION['branch_id']."'";
  $branch_mst_res = mysqli_query($conn,$branch_mst);
  $branch_mst_row = mysqli_fetch_assoc($branch_mst_res);
  $prifix         = $branch_mst_row['prifix'];

  $doc = "SELECT * FROM `document_mst` WHERE `company_id` = '".$_SESSION['company_id']."' AND `branch` = '".$_SESSION['branch_id']."' AND module = 'LR'";
  $doc_res = mysqli_query($conn,$doc);
  $doc_num = mysqli_num_rows($doc_res);
  $doc_row = mysqli_fetch_assoc($doc_res);

  if($doc_num > 0){

    $last_pooja   = "SELECT MAX(`lr_no`) FROM lr_mst WHERE lr_no != '' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."' ORDER BY id DESC LIMIT 1";
    $last_result  = mysqli_query($conn, $last_pooja);
    $last_row     = mysqli_fetch_row( $last_result );
    $last_data    = $last_row[0];

    $last_id      = 0;

    if( mysqli_num_rows( $last_result ) == 1 ){
      $last_row     = mysqli_fetch_row( $last_result );
      if(mysqli_num_rows( $last_result ) > 0){
        $tmp_count  = explode('/', $last_data);
        $last_id    = $tmp_count[1];
         
      }

    }else{
      if(mysqli_num_rows( $last_result ) > 0){
       $last_id = $doc_row['start_sr_no'];  
      }
    }
    
    $receipt_no = ($last_id + 1);
    
  }else{
    $last_pooja   = "SELECT MAX(lr_no) FROM lr_mst WHERE lr_no != '' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."' ORDER BY id DESC LIMIT 1";
    $last_result  = mysqli_query($conn, $last_pooja);

    $last_id = 0;
    if( mysqli_num_rows( $last_result ) == 1 ){
      $last_row     = mysqli_fetch_row( $last_result );

      $temp = explode('/', $last_row[0]);

      if( isset($temp[1]) && $temp[1] != ''){
        $last_id = $temp[1];
      }
    }
    $receipt_no = $receipt_no = ($last_id + 1);
  }
  return $receipt_no;
}

function ContractCode( $conn ){
  $branch_mst     = "SELECT * FROM `branch_mst` WHERE `id` ='".$_SESSION['branch_id']."'";
  $branch_mst_res = mysqli_query($conn,$branch_mst);
  $branch_mst_row = mysqli_fetch_assoc($branch_mst_res);
  $prifix         = 'C';

  $doc = "SELECT * FROM `document_mst` WHERE `company_id` = '".$_SESSION['company_id']."' AND `branch` = '".$_SESSION['branch_id']."' AND module = 'CONTRACT'";
  $doc_res = mysqli_query($conn,$doc);
  $doc_num = mysqli_num_rows($doc_res);
  $doc_row = mysqli_fetch_assoc($doc_res);
  
  if($doc_num > 0){

    $last_pooja   = "SELECT MAX(`code`) FROM contract WHERE code != '' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."' ORDER BY id DESC LIMIT 1";
    $last_result  = mysqli_query($conn, $last_pooja);
    $last_row     = mysqli_fetch_row( $last_result );
    $last_data    = $last_row[0];

    $last_id      = 0;

    if( mysqli_num_rows( $last_result ) == 1 ){
      $last_row     = mysqli_fetch_row( $last_result );
      if(mysqli_num_rows( $last_result ) > 0){
        $tmp_count  = explode('/', $last_data);
        $last_id    = $tmp_count[1];
         
      }

    }else{
      if(mysqli_num_rows( $last_result ) > 0){
       $last_id = $doc_row['start_sr_no'];  
      }
    }
    
    $receipt_no = ($last_id + 1);
    
  }else{
    $last_pooja   = "SELECT MAX(code) FROM contract WHERE code != '' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."' ORDER BY id DESC LIMIT 1";
    $last_result  = mysqli_query($conn, $last_pooja);

    $last_id = 0;
    if( mysqli_num_rows( $last_result ) == 1 ){
      $last_row     = mysqli_fetch_row( $last_result );

      $temp = explode('/', $last_row[0]);

      if( isset($temp[1]) && $temp[1] != ''){
        $last_id = $temp[1];
      }
    }
    $receipt_no = $receipt_no = ($last_id + 1);
  }
  return $receipt_no;
}

function  OpeningLrCode( $conn ){
  $last_pooja   = "SELECT MAX(lr_no) FROM lr_opening WHERE lr_no != '' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."' ORDER BY id DESC LIMIT 1";
  $last_result  = mysqli_query($conn, $last_pooja);

  $last_id = 0;
  if( mysqli_num_rows( $last_result ) == 1 ){
    $last_row     = mysqli_fetch_row( $last_result );

    $temp = explode('-', $last_row[0]);

    if( isset($temp[1]) && $temp[1] != ''){
      $last_id = $temp[1];
    }
  }
  $flag       = 'LR';
  $receipt_no = $flag . '-' . str_pad(($last_id + 1), 4, '0', STR_PAD_LEFT);

  return $receipt_no;
}

/*function  lhcCode( $conn ){
  $last_pooja   = "SELECT lhc_no FROM lhc WHERE lhc_no != '' ORDER BY id DESC LIMIT 1";
  $last_result  = mysqli_query($conn, $last_pooja);

  $last_id = 0;
  if( mysqli_num_rows( $last_result ) == 1 ){
    $last_row     = mysqli_fetch_row( $last_result );

    $temp = explode('-', $last_row[0]);

    if( isset($temp[1]) && $temp[1] != ''){
      $last_id = $temp[1];
    }
  }
  $flag       = 'LHC';
  $receipt_no = $flag . '-' . str_pad(($last_id + 1), 4, '0', STR_PAD_LEFT);

  return $receipt_no;
}*/

function lhcCode( $conn ){
  $branch_mst     = "SELECT * FROM `branch_mst` WHERE `id` ='".$_SESSION['branch_id']."'";
  $branch_mst_res = mysqli_query($conn,$branch_mst);
  $branch_mst_row = mysqli_fetch_assoc($branch_mst_res);
  $prifix         = $branch_mst_row['prifix'];

  $doc = "SELECT * FROM `document_mst` WHERE `company_id` = '".$_SESSION['company_id']."' AND `branch` = '".$_SESSION['branch_id']."' AND module = 'LHC'";
  $doc_res = mysqli_query($conn,$doc);
  $doc_num = mysqli_num_rows($doc_res);
  $doc_row = mysqli_fetch_assoc($doc_res);

  if($doc_num > 0){

    $last_pooja   = "SELECT MAX(`lhc_no`) FROM lhc WHERE lhc_no != '' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."' ORDER BY id DESC LIMIT 1";
    $last_result  = mysqli_query($conn, $last_pooja);
    $last_row     = mysqli_fetch_row( $last_result );
    $last_data    = $last_row[0];

    $last_id      = 0;

    if( mysqli_num_rows( $last_result ) == 1 ){
      $last_row     = mysqli_fetch_row( $last_result );
      if(mysqli_num_rows( $last_result ) > 0){
        $tmp_count  = explode('/', $last_data);
        $last_id    = $tmp_count[1];
         
      }

    }else{
      if(mysqli_num_rows( $last_result ) > 0){
       $last_id = $doc_row['start_sr_no'];  
      }
    }
    
    $receipt_no = ($last_id + 1);
    
  }else{
    $last_pooja   = "SELECT MAX(lhc_no) FROM lhc WHERE lhc_no != '' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."' ORDER BY id DESC LIMIT 1";
    $last_result  = mysqli_query($conn, $last_pooja);

    $last_id = 0;
    if( mysqli_num_rows( $last_result ) == 1 ){
      $last_row     = mysqli_fetch_row( $last_result );

      $temp = explode('/', $last_row[0]);

      if( isset($temp[1]) && $temp[1] != ''){
        $last_id = $temp[1];
      }
    }
    $receipt_no = $receipt_no = ($last_id + 1);
  }
  return $receipt_no;
}

function ContractLhcCode( $conn ){
  $branch_mst     = "SELECT * FROM `branch_mst` WHERE `id` ='".$_SESSION['branch_id']."'";
  $branch_mst_res = mysqli_query($conn,$branch_mst);
  $branch_mst_row = mysqli_fetch_assoc($branch_mst_res);
  $prifix         = $branch_mst_row['prifix'];

  $doc = "SELECT * FROM `document_mst` WHERE `company_id` = '".$_SESSION['company_id']."' AND `branch` = '".$_SESSION['branch_id']."' AND module = 'CONTRACT LHC'";
  $doc_res = mysqli_query($conn,$doc);
  $doc_num = mysqli_num_rows($doc_res);
  $doc_row = mysqli_fetch_assoc($doc_res);

  if($doc_num > 0){

    $last_pooja   = "SELECT MAX(`code`) FROM con_lhc WHERE code != '' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."' ORDER BY id DESC LIMIT 1";
    $last_result  = mysqli_query($conn, $last_pooja);
    $last_row     = mysqli_fetch_row( $last_result );
    $last_data    = $last_row[0];

    $last_id      = 0;

    if( mysqli_num_rows( $last_result ) == 1 ){
      $last_row     = mysqli_fetch_row( $last_result );
      if(mysqli_num_rows( $last_result ) > 0){
        $tmp_count  = explode('/', $last_data);
        $last_id    = $tmp_count[1];
         
      }

    }else{
      if(mysqli_num_rows( $last_result ) > 0){
       $last_id = $doc_row['start_sr_no'];  
      }
    }
    
    $receipt_no = ($last_id + 1);
    
  }else{
    $last_pooja   = "SELECT MAX(code) FROM con_lhc WHERE code != '' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."' ORDER BY id DESC LIMIT 1";
    $last_result  = mysqli_query($conn, $last_pooja);

    $last_id = 0;
    if( mysqli_num_rows( $last_result ) == 1 ){
      $last_row     = mysqli_fetch_row( $last_result );

      $temp = explode('/', $last_row[0]);

      if( isset($temp[1]) && $temp[1] != ''){
        $last_id = $temp[1];
      }
    }
    $receipt_no = $receipt_no = ($last_id + 1);
  }
  return $receipt_no;
}

function  cpCode( $conn ){
  /*$last_pooja   = "SELECT voucher_no FROM payment WHERE flag = 'CP' ORDER BY id DESC LIMIT 1";*/

  $last_pooja   = "SELECT MAX(`voucher_no`) FROM payment WHERE flag = 'CP' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."' ORDER BY id DESC LIMIT 1";

  $last_result  = mysqli_query($conn, $last_pooja);

  $last_id = 0;
  if( mysqli_num_rows( $last_result ) == 1 ){
    $last_row     = mysqli_fetch_row( $last_result );

    $temp = explode('-', $last_row[0]);

    if( isset($temp[1]) && $temp[1] != ''){
      $last_id = $temp[1];
    }
  }
  $flag       = 'CP';
  $receipt_no = $flag . '-' . str_pad(($last_id + 1), 4, '0', STR_PAD_LEFT);

  return $receipt_no;
}

function  bpCode( $conn ){
  /*$last_pooja   = "SELECT voucher_no FROM payment WHERE flag = 'BP' ORDER BY id DESC LIMIT 1";*/

  $last_pooja   = "SELECT MAX(`voucher_no`) FROM payment WHERE flag = 'BP' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."' ORDER BY id DESC LIMIT 1";

  $last_result  = mysqli_query($conn, $last_pooja);

  $last_id = 0;
  if( mysqli_num_rows( $last_result ) == 1 ){
    $last_row     = mysqli_fetch_row( $last_result );

    $temp = explode('-', $last_row[0]);

    if( isset($temp[1]) && $temp[1] != ''){
      $last_id = $temp[1];
    }
  }
  $flag       = 'BP';
  $receipt_no = $flag . '-' . str_pad(($last_id + 1), 4, '0', STR_PAD_LEFT);

  return $receipt_no;
}


function  brCode( $conn ){
  $last_pooja   = "SELECT MAX(`voucher_no`) FROM payment WHERE flag = 'BR' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."' ORDER BY id DESC LIMIT 1";
  $last_result  = mysqli_query($conn, $last_pooja);

  $last_id = 0;
  if( mysqli_num_rows( $last_result ) == 1 ){
    $last_row     = mysqli_fetch_row( $last_result );

    $temp = explode('-', $last_row[0]);

    if( isset($temp[1]) && $temp[1] != ''){
      $last_id = $temp[1];
    }
  }
  $flag       = 'BR';
  $receipt_no = $flag . '-' . str_pad(($last_id + 1), 4, '0', STR_PAD_LEFT);

  return $receipt_no;
}

function  crCode( $conn ){
  /*$last_pooja   = "SELECT voucher_no FROM payment WHERE flag = 'CR' ORDER BY id DESC LIMIT 1";*/
  
  $last_pooja   = "SELECT MAX(`voucher_no`) FROM payment WHERE flag = 'CR' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."' ORDER BY id DESC LIMIT 1";

  $last_result  = mysqli_query($conn, $last_pooja);

  $last_id = 0;
  if( mysqli_num_rows( $last_result ) == 1 ){
    $last_row     = mysqli_fetch_row( $last_result );

    $temp = explode('-', $last_row[0]);

    if( isset($temp[1]) && $temp[1] != ''){
      $last_id = $temp[1];
    }
  }
  $flag       = 'CR';
  $receipt_no = $flag . '-' . str_pad(($last_id + 1), 4, '0', STR_PAD_LEFT);

  return $receipt_no;
}

/*function  billCode( $conn ){
  $last_pooja   = "SELECT MAX(invoice_no) FROM billing WHERE invoice_no != '' ORDER BY id DESC LIMIT 1";
  $last_result  = mysqli_query($conn, $last_pooja);

  $last_id = 0;
  if( mysqli_num_rows( $last_result ) == 1 ){
    $last_row     = mysqli_fetch_row( $last_result );

    $temp = explode('-', $last_row[0]);

    if( isset($temp[1]) && $temp[1] != ''){
      $last_id = $temp[1];
    }
  }
  $year  = date('y', strtotime(get_current_date_time()->get_date_for_db()));
    if( strtotime( risFormatDate('', 'Y-m-d') ) <= strtotime(FINANCIAL_END_DATE) ) {
    $year = $year - 1;
  }  
  $fa         = $year . ($year + 1);
  $flag       = $fa;
  $receipt_no = $flag . '-' . str_pad(($last_id + 1), 4, '0', STR_PAD_LEFT);
  return $receipt_no;
}*/

function billCode( $conn ){
  $branch_mst     = "SELECT * FROM `branch_mst` WHERE `id` ='".$_SESSION['branch_id']."'";
  $branch_mst_res = mysqli_query($conn,$branch_mst);
  $branch_mst_row = mysqli_fetch_assoc($branch_mst_res);
  $prifix         = $branch_mst_row['prifix'];

  $doc = "SELECT * FROM `document_mst` WHERE `company_id` = '".$_SESSION['company_id']."' AND `branch` = '".$_SESSION['branch_id']."' AND module = 'LHC'";
  $doc_res = mysqli_query($conn,$doc);
  $doc_num = mysqli_num_rows($doc_res);
  $doc_row = mysqli_fetch_assoc($doc_res);

  if($doc_num > 0){

    $last_pooja   = "SELECT MAX(`invoice_no`) FROM billing WHERE invoice_no != '' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."' ORDER BY id DESC LIMIT 1";
    $last_result  = mysqli_query($conn, $last_pooja);
    $last_row     = mysqli_fetch_row( $last_result );
    $last_data    = $last_row[0];

    $last_id      = 0;

    if( mysqli_num_rows( $last_result ) == 1 ){
      $last_row     = mysqli_fetch_row( $last_result );
      if(mysqli_num_rows( $last_result ) > 0){
        $tmp_count  = explode('/', $last_data);
        $last_id    = $tmp_count[1];
         
      }

    }else{
      if(mysqli_num_rows( $last_result ) > 0){
       $last_id = $doc_row['start_sr_no'];  
      }
    }
    
    $receipt_no = ($last_id + 1);
    
  }else{
    $last_pooja   = "SELECT MAX(invoice_no) FROM billing WHERE invoice_no != '' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."' ORDER BY id DESC LIMIT 1";
    $last_result  = mysqli_query($conn, $last_pooja);

    $last_id = 0;
    if( mysqli_num_rows( $last_result ) == 1 ){
      $last_row     = mysqli_fetch_row( $last_result );

      $temp = explode('/', $last_row[0]);

      if( isset($temp[1]) && $temp[1] != ''){
        $last_id = $temp[1];
      }
    }
    $receipt_no = $receipt_no = ($last_id + 1);
  }
  return $receipt_no;
}

function ResetbillCode($conn,$financial_years){
  
  $last_bill_no       = 0;
  $financial_year = $financial_years;
  

  $query = mysqli_query($conn, "SELECT max(invoice_no) FROM billing WHERE invoice_no != '' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."' AND series_code = '".$financial_year."' ORDER BY id DESC");
  $count = mysqli_num_rows($query);
  if ($count > 0) {
    if( strtotime( risFormatDate($date)) <= strtotime($financial_end_date) ) {
      $last_row     = mysqli_fetch_row( $query );

      $temp = explode('/', $last_row[0]);
      
      if( isset($temp[2]) && $temp[2] != ''){
        $last_id = $temp[2];
      }
    }else{
      $last_row     = mysqli_fetch_row( $query );
      
      $temp = explode('/', $last_row[0]);
      if( isset($temp[2]) && $temp[2] != ''){
        $last_id = $temp[2];
      }
    }
  }

  return $receipt_no = ($last_id + 1);
}

function  CBillCode( $conn ){
  $last_pooja   = "SELECT MAX(con_bill) FROM contract_bill WHERE con_bill != '' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."' ORDER BY id DESC LIMIT 1";
  $last_result  = mysqli_query($conn, $last_pooja);

  $last_id = 0;
  if( mysqli_num_rows( $last_result ) == 1 ){
    $last_row     = mysqli_fetch_row( $last_result );

    $temp = explode('-', $last_row[0]);

    if( isset($temp[1]) && $temp[1] != ''){
      $last_id = $temp[1];
    }
  }
  $year  = date('y', strtotime(get_current_date_time()->get_date_for_db()));
    if( strtotime( risFormatDate('', 'Y-m-d') ) <= strtotime(FINANCIAL_END_DATE) ) {
    $year = $year - 1;
  }  
  $fa         = $year . ($year + 1);
  $flag       = $fa;
  $receipt_no = $flag . '-' . str_pad(($last_id + 1), 4, '0', STR_PAD_LEFT);
  return $receipt_no;
}

/*function  mrCode( $conn ){
  $last_pooja   = "SELECT MAX(mr_no) FROM mr ORDER BY id ASC LIMIT 1";
  $last_result  = mysqli_query($conn, $last_pooja);

  $last_id = 0;
  if( mysqli_num_rows( $last_result ) == 1 ){
    $last_row     = mysqli_fetch_row( $last_result );

    $temp = explode('-', $last_row[0]);

    if( isset($temp[1]) && $temp[1] != ''){
      $last_id = $temp[1];
    }
  }
  $flag        = 'MR';
  $receipt_no  = $flag . '-' . str_pad(($last_id + 1), 4, '0', STR_PAD_LEFT);

  return $receipt_no;
}*/

function mrCode( $conn ){
  $branch_mst     = "SELECT * FROM `branch_mst` WHERE `id` ='".$_SESSION['branch_id']."'";
  $branch_mst_res = mysqli_query($conn,$branch_mst);
  $branch_mst_row = mysqli_fetch_assoc($branch_mst_res);
  $prifix         = $branch_mst_row['prifix'];

  $doc = "SELECT * FROM `document_mst` WHERE `company_id` = '".$_SESSION['company_id']."' AND `branch` = '".$_SESSION['branch_id']."' AND module = 'LR'";
  $doc_res = mysqli_query($conn,$doc);
  $doc_num = mysqli_num_rows($doc_res);
  $doc_row = mysqli_fetch_assoc($doc_res);

  if($doc_num > 0){

    $last_pooja   = "SELECT MAX(`mr_no`) FROM mr WHERE mr_no != '' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."' ORDER BY id DESC LIMIT 1";
    $last_result  = mysqli_query($conn, $last_pooja);
    $last_row     = mysqli_fetch_row( $last_result );
    $last_data    = $last_row[0];

    $last_id      = 0;

    if( mysqli_num_rows( $last_result ) == 1 ){
      $last_row     = mysqli_fetch_row( $last_result );
      if(mysqli_num_rows( $last_result ) > 0){
        $tmp_count  = explode('/', $last_data);
        $last_id    = $tmp_count[1];
         
      }

    }else{
      if(mysqli_num_rows( $last_result ) > 0){
       $last_id = $doc_row['start_sr_no'];  
      }
    }
    
    $receipt_no = ($last_id + 1);
    
  }else{
    $last_pooja   = "SELECT MAX(mr_no) FROM mr WHERE mr_no != '' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."' ORDER BY id DESC LIMIT 1";
    $last_result  = mysqli_query($conn, $last_pooja);

    $last_id = 0;
    if( mysqli_num_rows( $last_result ) == 1 ){
      $last_row     = mysqli_fetch_row( $last_result );

      $temp = explode('/', $last_row[0]);

      if( isset($temp[1]) && $temp[1] != ''){
        $last_id = $temp[1];
      }
    }
    $receipt_no = $receipt_no = ($last_id + 1);
  }
  return $receipt_no;
}


function GeneralSales( $conn ){
  $last_pooja   = "SELECT MAX(code) FROM general_sales WHERE code != '' ORDER BY id DESC LIMIT 1";
  $last_result  = mysqli_query($conn, $last_pooja);

  $last_id = 0;
  if( mysqli_num_rows( $last_result ) == 1 ){
    $last_row     = mysqli_fetch_row( $last_result );
    if(mysqli_num_rows( $last_result ) > 0){
      $temp = $last_row[0];  
    }
  }

  $receipt_no = str_pad(($temp + 1), 5, '0', STR_PAD_LEFT);

  return $receipt_no;
}

function GeneralPurchase( $conn ){
  $last_pooja   = "SELECT code FROM general_purchase WHERE code != '' ORDER BY id DESC LIMIT 1";
  $last_result  = mysqli_query($conn, $last_pooja);

  $last_id = 0;
  if( mysqli_num_rows( $last_result ) == 1 ){
    $last_row     = mysqli_fetch_row( $last_result );
    if(mysqli_num_rows( $last_result ) > 0){
      $temp = $last_row[0];  
    }
  }

  $receipt_no = str_pad(($temp + 1), 5, '0', STR_PAD_LEFT);

  return $receipt_no;
}

function  jvCode( $conn ){
  $last_pooja   = "SELECT jv_no FROM journal_voucher where branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."' ORDER BY jv_no DESC LIMIT 1";
  $last_result  = mysqli_query($conn, $last_pooja);

  $last_id = 0;
  if( mysqli_num_rows( $last_result ) == 1 ){
    $last_row     = mysqli_fetch_row( $last_result );

    $temp = explode('-', $last_row[0]);

    if( isset($temp[1]) && $temp[1] != ''){
      $last_id = $temp[1];
    }
  }
  $flag        = 'JV';
  $receipt_no  = $flag . '-' . str_pad(($last_id + 1), 4, '0', STR_PAD_LEFT);

  return $receipt_no;
}

function  Tracking( $conn ){
  $last_pooja   = "SELECT tracking_id FROM tracking ORDER BY id DESC LIMIT 1";
  $last_result  = mysqli_query($conn, $last_pooja);

  $last_id = 0;
  if( mysqli_num_rows( $last_result ) == 1 ){
    $last_row     = mysqli_fetch_row( $last_result );

    $temp = explode('-', $last_row[0]);

    if( isset($temp[1]) && $temp[1] != ''){
      $last_id = $temp[1];
    }
  }
  $flag        = 'TR';
  $receipt_no  = $flag . '-' . str_pad(($last_id + 1), 4, '0', STR_PAD_LEFT);

  return $receipt_no;
}

if (!function_exists('combine')) {
  function combine($array, $key, $value, $type = 1) {
    $return = array();

    foreach ((array)$array as $val) {

        if( is_object($val) ) {
            $val = (array)$val;                
        }

        if($type == 1){
            if(!empty($key)){
                $return[$val[$key]] = $val[$value];     
            } else {
                $return[] = $val[$value];
            }
        } else if($type == 2){
            if(!empty($key)){
                if(isset($return[$val[$key]])){
                    $return[$val[$key]][] = $val;
                } else {
                    $return[$val[$key]] = $val;
                }
                
            } else {
                $return[] = $val;
            } 
        } else if($type == 3){
            if(!empty($key)){
                if(isset($return[$val[$key]])){
                    $return[$val[$key]][] = $val;
                } else {
                    $return[$val[$key]][] = $val;
                }
                
            } else {
                $return[] = $val;
            } 
        } else if($type == 4){
            if(!empty($key)){
                $return[$val[$key]][$val[$value]] = $val;
            }
        }
    }

    return $return;
  }
}

if (!function_exists('getAllRecords')) {
  function getAllRecords($table) {
    global $conn;
    $result  = mysqli_query($conn, "SELECT * FROM $table");

    $return = array();
    while ($row = mysqli_fetch_assoc($result)) {
      $return[] = $row;
    }

    return $return;
  }
}

if (!function_exists('customFormatMoney')) {
    function customFormatMoney($money, $show_symbol = false, $code = 'IN') {
        if(is_null($code)){
            $symbol = 'Rs.';
        }

        if(empty($money) || $money == '') {
            $money = 0;
        }

        if(is_string($money)) {
            $money = (float)$money;
        }

        $currency   = 'INR';

        switch (strtoupper( $code )) {
            case 'IN':
                $code       = 'en_IN';
                $currency   = 'INR';
            break;
            
            default:
                $code       = 'en_IN';
                $currency   = 'INR';
            break;
        }
            
        if( in_array('intl', get_loaded_extensions()) ) {
          $fmt = new NumberFormatter( $code, NumberFormatter::CURRENCY );

          if($show_symbol){
              return $fmt->formatCurrency($money, $currency);
          } else {
              $amount = preg_replace( '/[^0-9,"-."]/', '', $fmt->formatCurrency($money, $currency));
              
              if( $currency == 'USD' ) {
                  $amount = str_replace('$', '', $amount);
              }

              return  $amount;
          }
        } else {
            $n = $money; 
            $c = is_float($n) ? 1 : number_format($n,2);
            $d = '.';
            $t = ',';
            $sign = ($n < 0) ? '-' : '';
            $i = $n = number_format(abs($n),2); 
            $j = (($j = count($i)) > 3) ? $j % 3 : 0;

            if($show_symbol) {
                return $sign .($j ? substr($i,0, $j) + $t : '').preg_replace('/(\d{3})(?=\d)/',"$1" + $t,substr($i,$j)) . ' ' . $symbol;
            } else {
                return $sign .($j ? substr($i,0, $j) + $t : '').preg_replace('/(\d{3})(?=\d)/',"$1" + $t,substr($i,$j));
            }
        }
    }
}
if (!function_exists('convertNumber2Words')) {
  function convertNumber2Words($number) {
    $minus = '';
    if( $number < 0 ) {
        $minus = 'Minus ';
        $number = abs($number);
    }

    $number         = (float)$number;
    $decimal        = round($number - ($no = floor($number)), 2) * 100;
    $hundred        = null;
    $digits_length  = strlen($no);
    $i              = 0;
    $str            = array();

    $words = array(
        0 => '',
        1 => 'One',
        2 => 'Two',
        3 => 'Three',
        4 => 'Four',
        5 => 'Five',
        6 => 'Six',
        7 => 'Seven',
        8 => 'Eight',
        9 => 'Nine',
        10 => 'Ten',
        11 => 'Eleven',
        12 => 'Twelve',
        13 => 'Thirteen',
        14 => 'Fourteen',
        15 => 'Fifteen',
        16 => 'Sixteen',
        17 => 'Seventeen',
        18 => 'Eighteen',
        19 => 'Nineteen',
        20 => 'Twenty',
        30 => 'Thirty',
        40 => 'Forty',
        50 => 'Fifty',
        60 => 'Sixty',
        70 => 'Seventy',
        80 => 'Eighty',
        90 => 'Ninety'
    );

    $digits = array('', 'Hundred','Thousand','Lakh', 'Crore');

    while( $i < $digits_length ) {
        $divider    = ($i == 2) ? 10 : 100;
        $number     = floor($no % $divider);
        $no         = floor($no / $divider);
        $i          += $divider == 10 ? 1 : 2;

        if ($number) {
            $plural = (($counter = count($str)) && $number > 9) ? 's' : null;
            $hundred = ($counter == 1 && $str[0]) ? ' and ' : null;
            $str [] = ($number < 21) ? $words[$number].' '. $digits[$counter]. $plural.' '.$hundred:$words[floor($number / 10) * 10].' '.$words[$number % 10]. ' '.$digits[$counter].$plural.' '.$hundred;
        } else {
            $str[] = null;
        }
    }

    $rupees = implode('', array_reverse($str));
    $paise  = ($decimal) ? "." . ($words[$decimal / 10] . " " . $words[$decimal % 10]) . ' Paise' : '';
    return $minus . ($rupees ? $rupees . 'Rupees Only ' : '') . $paise;
  }  
}


function LhcBalance($conn){
  $select   = "SELECT code FROM lhc_balance_history WHERE code != '' ORDER BY id DESC LIMIT 1";
  $result  = mysqli_query($conn, $select);

  $last_id = 0;
  if( mysqli_num_rows( $result ) == 1 ){
    $rows     = mysqli_fetch_row( $result );
    if(mysqli_num_rows( $result ) > 0){
      $temp = $rows[0];  
    }
  }
  
  $receipt_no = str_pad(($temp + 1), 5, '0', STR_PAD_LEFT);

  return $receipt_no;
}

function CreditNote( $conn){
  $select   = "SELECT MAX(code) AS code FROM credit_note WHERE code != '' ORDER BY id DESC LIMIT 1";
  $result  = mysqli_query($conn, $select);

  $last_id = 0;
  if( mysqli_num_rows( $result ) == 1 ){
    $rows     = mysqli_fetch_row( $result );
    if(mysqli_num_rows( $result ) > 0){
      $temp = $rows[0];  
    }
  }
  
  $receipt_no = str_pad(($temp + 1), 5, '0', STR_PAD_LEFT);

  return $receipt_no;
}

function DebitNote( $conn){
  $select   = "SELECT MAX(code) AS code FROM debit_note WHERE code != '' ORDER BY id DESC LIMIT 1";
  $result  = mysqli_query($conn, $select);

  $last_id = 0;
  if( mysqli_num_rows( $result ) == 1 ){
    $rows     = mysqli_fetch_row( $result );
    if(mysqli_num_rows( $result ) > 0){
      $temp = $rows[0];  
    }
  }
  
  $receipt_no = str_pad(($temp + 1), 5, '0', STR_PAD_LEFT);

  return $receipt_no;
}
function ResetlhcCode($conn,$financial_years){
  
  $last_bill_no       = 0;
  $financial_year = $financial_years;
  

  $query = mysqli_query($conn, "SELECT max(lhc_no) FROM lhc WHERE lhc_no != '' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."' AND series_code = '".$financial_year."' ORDER BY id DESC");
  $count = mysqli_num_rows($query);
  if ($count > 0) {
    if( strtotime( risFormatDate($date)) <= strtotime($financial_end_date) ) {
      $last_row     = mysqli_fetch_row( $query );

      $temp = explode('/', $last_row[0]);
      
      if( isset($temp[1]) && $temp[1] != ''){
        $last_id = $temp[1];
      }
    }else{
      $last_row     = mysqli_fetch_row( $query );
      
      $temp = explode('/', $last_row[0]);
      if( isset($temp[1]) && $temp[1] != ''){
        $last_id = $temp[1];
      }
    }
  }

  return $receipt_no = ($last_id + 1);
}


function ResetmrCode($conn,$financial_years){
  
  $last_bill_no       = 0;
  $financial_year = $financial_years;
  

  $query = mysqli_query($conn, "SELECT max(mr_no) FROM mr WHERE mr_no != '' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."' AND series_code = '".$financial_year."' ORDER BY id DESC");
  $count = mysqli_num_rows($query);
  if ($count > 0) {
    if( strtotime( risFormatDate($date)) <= strtotime($financial_end_date) ) {
      $last_row     = mysqli_fetch_row( $query );

      $temp = explode('/', $last_row[0]);
      
      if( isset($temp[1]) && $temp[1] != ''){
        $last_id = $temp[1];
      }
    }else{
      $last_row     = mysqli_fetch_row( $query );
      
      $temp = explode('/', $last_row[0]);
      if( isset($temp[1]) && $temp[1] != ''){
        $last_id = $temp[1];
      }
    }
  }

  return $receipt_no = ($last_id + 1);
}


function  tripCode( $conn ){
  $last_pooja   = "SELECT trip_no FROM trip WHERE trip_no != '' ORDER BY id DESC LIMIT 1";
  $last_result  = mysqli_query($conn, $last_pooja);

  $last_id = 0;
  if( mysqli_num_rows( $last_result ) == 1 ){
    $last_row     = mysqli_fetch_row( $last_result );

    $temp = explode('-', $last_row[0]);

    if( isset($temp[1]) && $temp[1] != ''){
      $last_id = $temp[1];
    }
  }
  $flag       = 'T';
  $receipt_no = $flag . '-' . str_pad(($last_id + 1), 4, '0', STR_PAD_LEFT);

  return $receipt_no;
}

function GeneralPurchase_new( $conn ){
  $last_pooja   = "SELECT code FROM general_purchase_new WHERE code != '' ORDER BY id DESC LIMIT 1";
  $last_result  = mysqli_query($conn, $last_pooja);

  $last_id = 0;
  if( mysqli_num_rows( $last_result ) == 1 ){
    $last_row     = mysqli_fetch_row( $last_result );
    if(mysqli_num_rows( $last_result ) > 0){
      $temp = $last_row[0];  
    }
  }

  $receipt_no = str_pad(($temp + 1), 5, '0', STR_PAD_LEFT);

  return $receipt_no;
}


?>