<?
include('opendb.php');
function getbrled($code,$db)
{
    $rew=mysqli_query($db,"select brled from branch where brcode='$code'");
    $rerow=mysqli_fetch_row($rew);
    return $rerow[0];
}

function getbrledforce($code,$db)
{
 $rew=mysqli_query($db,"select brled from branch where brcode='$code'");
 $rerow=mysqli_fetch_row($rew);
  return $rerow[0];
}
function getnature($code,$db)
{
 $rew=mysqli_query($db,"select nature from acgroups where gname='$code'");
 $rerow=mysqli_fetch_row($rew);
  return $rerow[0];
}

function getrpled($code,$db)
{
    $rew=mysqli_query($db,"select rpled from branch where brcode='$code'");
    $rerow=mysqli_fetch_row($rew);
    return $rerow[0];
}

function getrpname($code,$db)
{
    
    $rew=mysqli_query($db,"select brname from gen4psnauto_psn_psneicherdb.branch where brcode='$code'");
    $rerow=mysqli_fetch_row($rew);
    return $rerow[0];
}

function getbrnamee($code,$db)
{
 $rew=mysqli_query($db,"select brled from gen4psnauto_psn_amplvehicle.branch where brcode='$code'");
 $rerow=mysqli_fetch_row($rew);
  return $rerow[0];
}

function getbrledname($code,$db)
{
 $rew=mysqli_query($db,"select brled from branch where brname='$code'");
 $rerow=mysqli_fetch_row($rew);
  return $rerow[0];
}

function getbrlednamee($code,$db)
{
 $rew=mysqli_query($db,"select brled from gen4psnauto_psn_ampleicher.branch where brname='$code'");
 $rerow=mysqli_fetch_row($rew);
  return $rerow[0];
}

function getrpledname($code,$db)
{
 $rew=mysqli_query($db,"select rpled from gen4psnauto_psn_psneicherdb.branch where brname='$code'");
 $rerow=mysqli_fetch_row($rew);
  return $rerow[0];
}


function getmake($code,$br,$db)
{
 $rew=mysqli_query($db,"select vehiclename from gateentry where brcode='$br' and jcno='$code'");
 $rerow=mysqli_fetch_row($rew);
  return $rerow[0];
}


function getinvamt($code,$br,$db)
{
 $rew=mysqli_query($db,"select sum(amt) from jcardinvoice where  brcode='$br' and jcno='$code' group by jcno ");
 $rerow=mysqli_fetch_row($rew);
  return $rerow[0];
}

function getinvno($code,$br,$db)
{
 $rew=mysqli_query($db,"select invno from jcardinvoice where  brcode='$br' and jcno='$code'  ");
 $rerow=mysqli_fetch_row($rew);
  return $rerow[0];
}

function getinvdate($code,$br,$db)
{
 $rew=mysqli_query($db,"select date from jcardinvoice where  brcode='$br' and jcno='$code'  ");
 $rerow=mysqli_fetch_row($rew);
  return $rerow[0];
}




function getinstype($code,$br,$db)
{
 $rew=mysqli_query($db,"select instype,insurance from gateentry where brcode='$br' and jcno='$code'");
 $rerow=mysqli_fetch_row($rew);
 $pos = $rerow[0];

if($pos=='Private')
{
$insur='Private';
}
else
{

 $sql2=mysqli_query($db,"select web from insmaster where cname='$rerow[1]'");

 if(mysqli_num_rows($sql2)>0)
{
$abc=mysqli_fetch_array($sql2) or die(mysqli_error($db));
$insur=$abc[0];
}
else
{
$insur='Insurance';
}


}
  return $insur;
}




function getchasis($code,$br,$db)
{
 $rew=mysqli_query($db,"select chasisno from gateentry where brcode='$br' and jcno='$code'");
 $rerow=mysqli_fetch_row($rew);
  return $rerow[0];
}

function getmodel($code,$br,$db)
{
 $rew=mysqli_query($db,"select model from gateentry where brcode='$br' and jcno='$code'");
 $rerow=mysqli_fetch_row($rew);
  return $rerow[0];
}

function getbrname($code,$db)
{
 $rew=mysqli_query($db,"select brname from branch where brcode='$code'");
 $rerow=mysqli_fetch_row($rew);
  return $rerow[0];
}

function getpartgroup($pcode,$db)
{
 $r=mysqli_query($db,"select group1 from partmaster where partcode='$pcode'");
 $rer=mysqli_fetch_row($r);
  return $rer[0];
}

function gethsn($pcode,$db)
{
 $r=mysqli_query($db,"select hsncode from partmaster where partcode='$pcode'");
 $rer=mysqli_fetch_row($r);
  return $rer[0];
}

function getstate($bcode,$db)
{
 $r=mysqli_query($db,"select brstate from branch where brcode='$bcode'");
 $rer=mysqli_fetch_row($r);
  return $rer[0];
}

function getregno($bcode,$br,$db)
{
 $r=mysqli_query($db,"select regno from gateentry where jcno='$bcode' and brcode='$br'");
 $rer=mysqli_fetch_row($r);
  return $rer[0];
}

function getsupplier($bcode,$br,$db)
{
 $r=mysqli_query($db,"select cname from pjobcard where invno='$bcode' and brcode='$br'");
 $rer=mysqli_fetch_row($r);
  return $rer[0];
}

function getpbill($bcode,$br,$db)
{
 $r=mysqli_query($db,"select pbillno from pjobcard where invno='$bcode' and brcode='$br'");
 $rer=mysqli_fetch_row($r);
  return $rer[0];
}


function getdays($date5,$db)
{
$dt2=$date5;
$sq="select to_days(curdate())-to_days('$dt2')";
$dtre=mysqli_query($db,$sq)or die(mysqli_error($db));
$dtrow=mysqli_fetch_row($dtre);
return $dtrow[0];

}


function log_entry($brcode,$user,$ip,$act,$db)
{
    date_default_timezone_set("Asia/Kolkata");
    $dt=date("Y-m-d H:i:s");
    $sql="insert into log values('$brcode','$user','$ip','$dt','$act')";
    mysqli_query($db,$sql) or die(mysqli_error($db));
}

function log_entry1($brcode,$user,$ip,$act,$port,$host,$db)
{
 $dt=date("Y-m-d H:i");
 $sql="insert into lognew values('$brcode','$user','$ip','$dt','$act','$port','$host')";
 mysqli_query($db,$sql) or die(mysqli_error($db));
}

function parseDate($d)
{
 if(strlen($d)<=0)
  return; 
 $ar=explode("-",$d);
 $var=$var.$ar[2]."-".$ar[1]."-".$ar[0];
 return $var;
}
function datetoHtml($d)
{
 $ar=explode("-",$d);
 $var=$var.$ar[2]."-".$ar[1]."-".$ar[0];
 return $var;
}

function month_alph($d)
{
  $ar=explode("-",$d);
  $var=$var.$ar[2]."-".$ar[1]."-".$ar[0];
  $var=strtotime($var);
  $var1= date('d-M-Y',$var);
  return $var1;
}

function month_alph_rev($d)
{
  //$ar=explode("-",$d);
 $var=$d;
  $var=strtotime($var);
  $var1= date('d-M-Y',$var);
  return $var1;
}

function datetimetoHtml($dt)
{
 $mst_date = date("YmdHis",strtotime($dt)); 

$mdate = substr($mst_date,6,2)."-".substr($mst_date,4,2)."-".substr($mst_date,0,4);

return $mdate;
}

function timedatetoHtml($dt)
{
 $mst_date = date("YmdHis",strtotime($dt)); 

$mtime = substr($mst_date,8,2).":".substr($mst_date,10,2).":".substr($mst_date,12,2);

return $mtime;
}
function dateandtimetoHtml($dt)
{
 $mst_date = date("YmdHis",strtotime($dt)); 

$mtime = substr($mst_date,6,2)."-".substr($mst_date,4,2)."-".substr($mst_date,0,4)." ".substr($mst_date,8,2).":".substr($mst_date,10,2).":".substr($mst_date,12,2);

return $mtime;
}

function expired($d)
{
 $ar=explode("-",$d);
 $dt=mktime(0,0,0,$ar[1],$ar[2],$ar[0]);
 if($dt<time())
  return true;
 else
  return false;
} 

function day($d)
{
 $ar=explode("-",$d);
 if($ar[0]>3000)
  return "Invalid Year";
 if($ar[1]>12)
  return "Invalid Month";
 if($ar[2]>31)
  return "Invalid Date";
 
 $dt=mktime(0,0,0,$ar[1],$ar[2],$ar[0]);
 return date("l",$dt); 
}
function nDays($date)
{
 $d=explode("-",$date);
 $m=$d[1];
 $y=$d[0];
 $dt2=mktime(0,0,0,$m,1,$y);
  $dd=date("M/j/Y",$dt2); 
$leap= date("L",$dt2);

 if($m==1)
  return 31;
 else if($m==2)
  {
      if($leap==0)
         return 28;
      else
          return 29;
  }
 else if($m==3)
  return 31;
 else if($m==4)
  return 30;
 else if($m==5)
  return 31;
 else if($m==6)
  return 30;
 else if($m==7)
  return 31;
 else if($m==8)
  return 31;
 else if($m==9)
  return 30;
 else if($m==10)
  return 31;
 else if($m==11)
  return 30;
 else if($m==12)
  return 31;
}

function fortextarea($val)
{
 $x=str_replace("<br>","&#10;",$val);
 $y=str_replace("<hr>","&#10;&#45;&#45;&#10;",$x);
 return $y;
}

function htmlValidate($a)
{
 return nl2br(str_replace("--","-----",$a));
}
//***************** number to words
function word($x)
{
$x=str_replace(".","-",$x);
$num=explode('-',$x); 

if($num[1]>0)
{
 $m="Rupees ".ConvertToWords($num[0])." and ".ConvertToWords($num[1])." paise";
 return $m;
}
else
{ $m= "Rupees ".ConvertToWords($num[0]);
 return $m;
}

}
 function ConvertToWords($n, $followup='')  
 {  
     if($n==0)  
     {  
         if($followup=='no')  
         {  
             return "";  
             exit();  
         }  
        else  
         {  
             return "zero";  
             exit();  
         }  
     }  
     switch($n)  
    {  
         case 1: return "One"; break;  
         case 2: return "Two"; break;  
         case 3: return "Three"; break;  
         case 4: return "Four"; break;  
         case 5: return "Five"; break;  
         case 6: return "Six"; break;  
         case 7: return "Seven"; break;  
         case 8: return "Eight"; break;  
         case 9: return "Nine"; break;  
         case 10: return "Ten"; break;  
         case 11: return "Eleven"; break;  
         case 12: return "Twelve"; break;  
         case 13: return "Thirteen"; break;  
         case 14: return "Fourteen"; break;  
         case 15: return "Fifteen"; break;  
         case 16: return "Sixteen"; break;  
         case 17: return "Seventeen"; break;  
         case 18: return "Eighteen"; break;  
         case 19: return "Nineteen"; break;  
        case 20: return "Twenty"; break;  
         case 30: return "Thirty"; break;  
         case 40: return "Forty"; break;  
         case 50: return "Fifty"; break;  
         case 60: return "Sixty"; break;  
         case 70: return "Seventy"; break;  
         case 80: return "Eighty"; break;  
         case 90: return "Ninety"; break;  
         case 100: return "One Hundred"; break;  
         case 1000: return "One Thousand"; break;  
         case 100000: return "One Lakh"; break;  
         default:  
         {  
             if($n<100)  
             {  
                 return ConvertToWords(floor($n/10)*10, 'no')." ".ConvertToWords($n%10, 'no'); break;  
             }  
             elseif($n<1000)  
             {  
                 return ConvertToWords(floor($n/100), 'no')." Hundred ".ConvertToWords($n%100, 'no'); break;  
             }  
             elseif($n<100000)  
             {  
                 return ConvertToWords(floor($n/1000), 'no')." Thousand ".ConvertToWords($n%1000, 'no'); break;  
             }  
             elseif($n<10000000)  
             {  
                 return ConvertToWords(floor($n/100000), 'no')." Lakh ".ConvertToWords($n%100000, 'no'); break;  
             }  
              elseif($n<100000000)  
             {  
                 return ConvertToWords(floor($n/100000), 'no')." Lakh ".ConvertToWords($n%100000, 'no'); break;  
             }  
             else  
             {  
                 return "Something else"; break;  
             }  
         }  
     }  
 }  

/*****************************************************************************/
function word_new($x)
{
$x=str_replace(".","-",$x);
$num=explode('-',$x); 

if($num[1]>0)
{
 $m="".ConvertToWords($num[0])." and ".ConvertToWords($num[1])." paise";
 return $m;
}
else
{ $m= "".ConvertToWords($num[0]);
 return $m;
}
}
function word1($x)
{
$x=str_replace(".","-",$x);
$num=explode('-',$x); 
/*
if(substr($num[0],0,1)==0)
{ $val.=' ';
 $x=substr($num[0],1);
 $val.=word($x);
}
*/
 if($num[0]<=20)
 {
   switch($num[0])
   {
    case 0:
     $val.=" ";
     break;
     case 1:
      $val="one";
      break;
     case 2:
      $val="two";
      break;
     case 3:
      $val="three";
      break;
     case 4:
      $val="four";
      break;
     case 5:
      $val="five";
      break;
     case 6:
      $val="six";
      break;
     case 7:
      $val="seven";
      break;
     case 8:
      $val="eight";
      break;
     case 9:
      $val="nine";
      break;
     case 10:
      $val="ten";
      break;
     case 11:
      $val="eleven";
      break;
     case 12:
      $val="twelve";
      break;
     case 13:
      $val="thirteen";
      break;
     case 14:
      $val="fourteen";
      break;
     case 15:
      $val="fifteen";
      break;
     case 16:
      $val="sixteen";
      break;
     case 17:
      $val="seventeen";
      break;
     case 18:
      $val="eighteen";
      break;
     case 19:
      $val="nineteen";
      break;
     case 20:
      $val="twenty";
      break;
}
} //end if
else if($num[0]>20 && $num[0]<30)
{
 $val=" twenty ";
 $x=substr($num[0],1);
 $val.=word($x);
}
else if($num[0]>=30 && $num[0]<40)
{
 $val=" thirty ";
 $x=substr($num[0],1);
 $val.=word($x);
}
else if($num[0]>=40 && $num[0]<50)
{
 $val=" fourty ";
 $x=substr($num[0],1);
 $val.=word($x);
}
else if($num[0]>=50 && $num[0]<60)
{
 $val=" fifty ";
 $x=substr($num[0],1);
 $val.=word($x);
}
else if($num[0]>=60 && $num[0]<70)
{
 $val=" sixty ";
 $x=substr($num[0],1);
 $val.=word($x);
}
else if($num[0]>=70 && $num[0]<80)
{
 $val=" seventy ";
 $x=substr($num[0],1);
 $val.=word($x);
}
else if($num[0]>=80 && $num[0]<90)
{
 $val=" eighty ";
 $x=substr($num[0],1);
 $val.=word($x);
}
else if($num[0]>=90 && $num[0]<100)
{
 $val=" ninety ";
 $x=substr($num[0],1);
 $val.=word($x);
}
else if($num[0]>=100 && $num[0]<200)
{
 $val="one hundred ";
 $x=substr($num[0],1);
 $val.=word($x);
}
else if($num[0]>=200 && $num[0]<300)
{
 $val=" two hundred ";
 $x=substr($num[0],1);
 $val.=word($x);
}
else if($num[0]>=300 && $num[0]<400)
{
 $val=" three hundred ";
 $x=substr($num[0],1);
 $val.=word($x);
}
else if($num[0]>=400 && $num[0]<500)
{
 $val="four hundred ";
 $x=substr($num[0],1);
 $val.=word($x);
}
else if($num[0]>=500 && $num[0]<600)
{
 $val=" five hundred ";
 $x=substr($num[0],1);
 $val.=word($x);
}
else if($num[0]>=600 && $num[0]<700)
{
 $val=" six hundred ";
 $x=substr($num[0],1);
 $val.=word($x);
}
else if($num[0]>=700 && $num[0]<800)
{
 $val=" seven hundred ";
 $x=substr($num[0],1);
 $val.=word($x);
}
else if($num[0]>=800 && $num[0]<900)
{
 $val=" eight hundred ";
 $x=substr($num[0],1);
 $val.=word($x);
}
else if($num[0]>=900 && $num[0]<1000)
{
 $val="nine hundred and ";
 $x=substr($num[0],1);
 $val.=word($x);
}
else if($num[0]>=1000 && $num[0]<10000)
{
 $x=substr($num[0],0,1); 
 $val.=word($x)." thousand ";
 $x=substr($num[0],1);
 $val.=word($x);
}
else if($num[0]>=10000 && $num[0]<100000)
{
 $x=substr($num[0],0,2); 
 $val.=word($x)." thousand ";
 $x=substr($num[0],2);
 $val.=word($x);
}
else if($num[0]>=100000 && $num[0]<1000000)
{
 $x=substr($num[0],0,1); 
 $val.=word($x)." Lakh ";
 $x=substr($num[0],1,2);
 if($x>0)
  $val.=word($x)." thousand ";
  $x=substr($num[0],3,3);
 $val.=word($x);
 //$x=substr($num[0],4);print $x;
// $val.=word($x);
}
else if($num[0]>=1000000 && $num[0]<10000000)
{
 $x=substr($num[0],0,2); 
 $val.=word($x)." Lakh ";
 $x=substr($num[0],2,2);
 if($x>0)
 $val.=word($x)." thousand ";
 $x=substr($num[0],4,1);
 if($x>0)
 $val.=word($x)." hundred ";
 $x=substr($num[0],5,2);
 if($x>0)
 $val.=word($x); 

}
//***********************************************************************

if($num[1]>0)
{
 $val.= " And ";
 if($num[1]<=20)
 {
   switch($num[1])
   {
    case 0:
     $val.=" ";
     break;
     case 1:
      $val.="one";
      break;
     case 2:
      $val.="two";
      break;
     case 3:
      $val.="three";
      break;
     case 4:
      $val.="four";
      break;
     case 5:
      $val.="five";
      break;
     case 6:
      $val.="six";
      break;
     case 7:
      $val.="seven";
      break;
     case 8:
      $val.="eight";
      break;
     case 9:
      $val.="nine";
      break;
     case 10:
      $val.="ten";
      break;
     case 11:
      $val.="eleven";
      break;
     case 12:
      $val.="twelve";
      break;
     case 13:
      $val.="thirteen";
      break;
     case 14:
      $val.="fourteen";
      break;
     case 15:
      $val.="fifteen";
      break;
     case 16:
      $val.="sixteen";
      break;
     case 17:
      $val.="seventeen";
      break;
     case 18:
      $val.="eighteen";
      break;
     case 19:
      $val.="nineteen";
      break;
     case 20:
      $val.="twenty";
      break;
}
} //endif
else if($num[1]>20 && $num[1]<30)
{
 $val.="twenty ";
 $x=substr($num[1],1);
 $val.=word($x);
}
else if($num[1]>=30 && $num[1]<40)
{
 $val.="thirty ";
 $x=substr($num[1],1);
 $val.=word($x);
}
else if($num[1]>=40 && $num[1]<50)
{
 $val.="fourty ";
 $x=substr($num[1],1);
 $val.=word($x);
}
else if($num[1]>=50 && $num[1]<60)
{
 $val.="fifty ";
 $x=substr($num[1],1);
 $val.=word($x);
}
else if($num[1]>=60 && $num[1]<70)
{
 $val.="sixty ";
 $x=substr($num[1],1);
 $val.=word($x);
}
else if($num[1]>=70 && $num[1]<80)
{
 $val.="seventy ";
 $x=substr($num[1],1);
 $val.=word($x);
}
else if($num[1]>=80 && $num[1]<90)
{
 $val.="eighty ";
 $x=substr($num[1],1);
 $val.=word($x);
}
else if($num[1]>=90 && $num[1]<100)
{
 $val.="ninety ";
 $x=substr($num[1],1);
 $val.=word($x);
}
$val.=" paise";
}
 
return strtoupper($val);
}
//*******************************
function putComa($num)
{
 if(strlen($num)==0)
  return;
 
 if(strlen(strstr($num,"."))<=0)
  { 
    $v=$num.".00";
  }
  else $v=$num;
 $a='';
    $a=explode(".",$v);
 if(strlen($a[1])==1)
  $v.="0";
//print $v;
 if(strlen($a[0])>8)
 {
  $v=substr($v,0,2).",".substr($v,2,2).",".substr($v,4,2).",".substr($v,6);
 }
 else if(strlen($a[0])>7)
 {
  $v=substr($v,0,1).",".substr($v,1,2).",".substr($v,3,2).",".substr($v,5);
 }
 else if(strlen($a[0])>6)
 {
  $v=substr($v,0,2).",".substr($v,2,2).",".substr($v,4);
 }
 else if(strlen($a[0])>5)
 {
  $v=substr($v,0,1).",".substr($v,1,2).",".substr($v,3);
 }
 else if(strlen($a[0])>4)
 {
  $v=substr($v,0,2).",".substr($v,2,6);
 }
 else if(strlen($a[0])>3)
 {
  $v=substr($v,0,1).",".substr($v,1);
 }
 return $v;
}

function nozero($x)
{
 if($x==0)
   return ' ';
 else
   return $x;
}

?>
