<?php
session_start();

if($_SESSION['dbName']==""){   
    header("location:../index.php");
}

include("config.inc.php");
include("function.inc.php");



$vMainCompanyName=explode('_',$_SESSION['dbName']);  
$DirCompany  = $vMainCompanyName[0];
$DirYear	 = str_replace('ACC','',$vMainCompanyName[1]); 

$connection = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass'],true,65536) or die("Could not connect to DB");
mysql_select_db($_SESSION['dbName'],$connection) or die("Could not find DB");

if(	$_SESSION['encodingmode'] == 'utf8' || $_SESSION['encodingmode'] == 'utf-8'){
	mysql_query("SET NAMES 'utf8'"); 
	mysql_query('SET CHARACTER SET utf8'); 
	mysql_set_charset('utf8', $connection);
}
set_time_limit(0);


//User Access
$querylog=" select ifNull(login_belongstogrp,0) As FI_BelongsToGrp from ".$_SESSION['dbLabel'].".login where user_account='".$_SESSION['useraccount']."' and Upper(Login_Schema) = Upper('".$_SESSION['dbName']."')  ";
$resultlog=mysql_query($querylog,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultlog)){
	extract($rows);
}



//Read From Setup Table							
$querysetup= " select DecimalBase, DecimalBase1, DecimalBase2, base1, base2, grid_color1, grid_color2, Ledger_ValidateProject from setup ";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
while($rowsetup=mysql_fetch_array($resultsetup)){
	extract($rowsetup);
}
 
// var_dump($_GET);
// var_dump($_POST);
if($action == 'LiveAlloc'){
    $myLinks = explode(",--,",$links);

    for($i=1; $i <= count($myLinks)-1 ;$i++){
         
        // echo"<BR>";
        $Sides = explode(',__ __',$myLinks[$i]);
        // echo "<Hr>";
        // print_r($Sides);
        // echo "</Hr>";

        $CurrDebit = explode(',',$Sides[0]);
        // echo "<BR>".
        $CurrDebitJV = $CurrDebit[0];
        // echo "<BR>".
        $CurrDebitAmount = $CurrDebit[1];
        $CurrCredits = explode( '__ __' , $Sides[1]);


        for($j = 0; $j <  count($CurrCredits); $j++) {
            $CurrC = explode(':',$CurrCredits[$j]);
            // echo "<br> -C-JV-ID-". 
            $currCreditJVID =  $CurrC[0];
            // echo "<br> -C-JV-AMNT-".  
            $currCreditJVAmount= preg_replace('/[ ,]+/', '', $CurrC[1]);
 

            if($curr == $base1){

                // keep amount and amount1 and convert to base2 
                $currCreditJVAmount1 = $currCreditJVAmount;

                // echo "<BR>". 
                $convertedAmount2 = "select convertcurFunc('$base1', '$base2', now(), 0, 0, '$base1', '$base2', $currCreditJVAmount)";
                $resConvAmnt2 = mysql_query($convertedAmount2,$connection);
                $rowConvAmnt2 = mysql_fetch_array($resConvAmnt2);
                $currCreditJVAmount2 = $rowConvAmnt2[0]; 

            }else if($curr == $base2){
                
                // keep amount and amount2 and convert to base1 from amount 
                  
                 $currCreditJVAmount2 = $currCreditJVAmount;
 
                //  echo "<BR>". 
                 $convertedAmount2 = "select convertcurFunc('$base2', '$base1', now(), 0, 0, '$base1', '$base2', $currCreditJVAmount)";
                 $resConvAmnt2 = mysql_query($convertedAmount2,$connection);
                 $rowConvAmnt2 = mysql_fetch_array($resConvAmnt2);
                 $currCreditJVAmount1 = $rowConvAmnt2[0]; 
                 
            }else{
                // keep amount convert to base 1 and base 2
                 

                // echo "<BR>". 
                $convertedAmount1 = "select convertcurFunc('$curr', '$base1', now(), 0, 0, '$base1', '$base2', $currCreditJVAmount)";
                $resConvAmnt1 = mysql_query($convertedAmount1,$connection);
                $rowConvAmnt1 = mysql_fetch_array($resConvAmnt1);
                $currCreditJVAmount1 = $rowConvAmnt1[0]; 

                
                // echo "<BR>". 
                $convertedAmount2 = "select convertcurFunc('$base2', '$base1', now(), 0, 0, '$base1', '$base2', $currCreditJVAmount)";
                $resConvAmnt2 = mysql_query($convertedAmount2,$connection);
                $rowConvAmnt2 = mysql_fetch_array($resConvAmnt2);
                $currCreditJVAmount2 = $rowConvAmnt2[0]; 
                
            }

            // Getting Debit Sid   --- Type
            // echo "<BR>". 
            $QueryJvType = "select JOURNAL_TYPE from journal where journal_id = $CurrDebitJV;";
            $resJvType = mysql_query($QueryJvType,$connection);
            $rowJvType = mysql_fetch_array($resJvType);
            $JVTYPE = $rowJvType[0]; 


            // if Supplier add the currdb to( journalID C )
            // echo "<BR>". 
            $queryins = "INSERT INTO ".$_SESSION['dbLabel'].".`alloclink`(`AllocLink_id`, `AllocLink_date`, `journal_idD`, `journal_idC`, `currency_code`, `amount`, `amountbase1`, `amountbase2`, `AllocComp`, `AllocYear`, `allocRemark`, `trans_id`, `transdt_id`,TransType,ledger_number) VALUES 
		    (null, now(), '$CurrDebitJV', '". $currCreditJVID ."', '$curr',  $currCreditJVAmount , $currCreditJVAmount1, $currCreditJVAmount2, '$DirCompany', '$DirYear', 'direct Alloc', '$PReceiptsID', '', '$JVTYPE','$fromledger');";
            mysql_query($queryins,$connection);
        }
    }

    header ("Location: ./Form_AdvancedPaymentAllocation1.php?fromledger=".$fromledger);
}





?>