<?php 
$PageSecurity = 15;
include('config.php');
include('includes/session.inc');
include('includes/function_lib.php');

$sql="select * from ledger_import";
$result=DB_query($sql,$db);
while($rs=DB_fetch_array($result))
{
    $sql_ledger="select count(*) from accountledger where AccountName='".$rs['account_name']."'";
    $result_ledger=DB_query($sql_ledger,$db);
    $rs_ledger=DB_fetch_array($result_ledger);
    $count_ledger=$rs_ledger[0];

    if($count_ledger=='0')
    {
           if($rs['subgroup']!='')
           {
              $group_id=$rs['subgroup'];
           }
           else
           {
              $group_id=$rs['group_name'];
           }
        $sql_insert="insert into accountledger(CompanyId,AccountName,UnderGroup) values(3,'".$rs['account_name']."','".$group_id."')";
        $result_insert=DB_query($sql_insert,$db);
        
        $lastid=DB_Last_Insert_ID($db,'accountledger', 'AccountsID');
     
        if($rs['opening_amt_aeh']!='0.00')
        {
             if($rs['opening_type_aeh']=='Dr.')
             {
               $aeh_type="DR";
             }
             else if($rs['opening_type_aeh']=='Cr.')
             {
              $aeh_type="CR";
             }

           $sql_insert_aeh="insert into accountledger_dtls(accountledger_id,site,openingtype,openingBalance,openingdate) values('".$lastid."','".$rs['branch_aeh']."','".$aeh_type."','".$rs['opening_amt_aeh']."','2017-04-01')";
           $result_insert_aeh=DB_query($sql_insert_aeh,$db);
       } 


       if($rs['opening_amt_aehb']!='0.00')
        {
           if($rs['opening_type_aehb']=='Dr.')
             {
               $aehb_type="DR";
             }
             else if($rs['opening_type_aehb']=='Cr.')
             {
              $aehb_type="CR";
             }

           $sql_insert_aehb="insert into accountledger_dtls(accountledger_id,site,openingtype,openingBalance,openingdate) values('".$lastid."','".$rs['branch_aehb']."','".$aehb_type."','".$rs['opening_amt_aehb']."','2017-04-01')";
           $result_insert_aehb=DB_query($sql_insert_aehb,$db);
       }



       if($rs['opening_amt_opt']!='0.00')
        {
          if($rs['opening_type_opt']=='Dr.')
             {
               $opt_type="DR";
             }
             else if($rs['opening_type_opt']=='Cr.')
             {
              $opt_type="CR";
             }
           $sql_insert_opt="insert into accountledger_dtls(accountledger_id,site,openingtype,openingBalance,openingdate) values('".$lastid."','".$rs['branch_opt']."','".$opt_type."','".$rs['opening_amt_opt']."','2017-04-01')";
           $result_insert_opt=DB_query($sql_insert_opt,$db);
       } 
       if($rs['opening_amt_mm']!='0.00')
        {
           if($rs['opening_type_mm']=='Dr.')
             {
               $mm_type="DR";
             }
             else if($rs['opening_type_mm']=='Cr.')
             {
              $mm_type="CR";
             }
           $sql_insert_mm="insert into accountledger_dtls(accountledger_id,site,openingtype,openingBalance,openingdate) values('".$lastid."','".$rs['branch_mandir']."','".$mm_type."','".$rs['opening_amt_mm']."','2017-04-01')";
           $result_insert_mm=DB_query($sql_insert_mm,$db);
       } 
       if($rs['opening_amt_aph']!='0.00')
        {
          if($rs['opening_type_aph']=='Dr.')
             {
               $aph_type="DR";
             }
             else if($rs['opening_type_aph']=='Cr.')
             {
              $aph_type="CR";
             }
           $sql_insert_aph="insert into accountledger_dtls(accountledger_id,site,openingtype,openingBalance,openingdate) values('".$lastid."','".$rs['branch_aph']."','".$aph_type."','".$rs['opening_amt_aph']."','2017-04-01')";
           $result_insert_aph=DB_query($sql_insert_aph,$db);
       } 

   }    

}
?>
