<?php require_once('../auth/auth.php'); ?>
<?php
if(isset($_POST["outlet_id"])){
     $outlet_id  = $_POST["outlet_id"];
     $ac_name    = $_POST["ac_name"];
     $ac_display = $_POST["ac_display"];
     $ac_number  = $_POST["ac_number"];
     $ac_address = $_POST["ac_address"];
     $ac_route   = $_POST["ac_route"];
     $ac_balance = $_POST["ac_balance"];
     $ac_nid     = $_POST["ac_nid"];
     $ac_genarel = $_POST["ac_genarel"];
     $invoice    = $_POST["invoice"];
     $active     =1;


    $ac_type    = $_POST["ac_type"];
    $gcatid    = $_POST["gcatid"];


$op_row= mysqli_fetch_assoc(mysqli_query($conn,"SELECT acid,gcatid FROM accounts WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND ac_type='opening' LIMIT 1"));

$op_acid = $op_row['acid'];
$op_gcatid = $op_row['gcatid'];

$op_date = $co_f_year_start.' '.$time;

for ($i = 0; $i < count($outlet_id); $i++){

    mysqli_query($conn,"INSERT INTO `accounts`(`coid`, `bid`, `brid`, `gcatid`, `outlet_id`, `ac_name`, `ac_alise`, `ac_number`, `ac_details`, `ac_area`, `opdate`, `balance`, `em_id`,`userid`, `active`) VALUES ('$coid','$bid','$brid','$gcatid ','$outlet_id[$i]','$ac_name[$i]','$ac_display[$i]','$ac_number[$i]','$ac_address[$i]','$ac_route[$i]','$op_date','$ac_balance[$i]','1','$uid','$active')")or die(mysqli_error($conn));

    $lastid = mysqli_insert_id($conn);

    mysqli_query($conn,"INSERT INTO `account_info`( `coid`, `bid`, `brid`, `acid`,`g_note`) VALUES ('$coid','$bid','$brid','$lastid','$ac_genarel[$i]')")or die(mysqli_error($conn));


    if ($ac_balance[$i] > 0) {
        $amount  = $ac_balance[$i];
        $dr_acid = $lastid;
        $cr_acid = $op_acid;

        $dr_gcatid = $gcatid;
        $cr_gcatid = $op_gcatid;
    } else {
        $amount  = - $ac_balance[$i];
        $dr_acid = $op_acid;
        $cr_acid = $lastid;

        $dr_gcatid = $op_gcatid;
        $cr_gcatid = $gcatid;
    }


    mysqli_query($conn,"INSERT INTO transaction(coid,bid,brid,dr_acid,cr_acid,e_acid,dr_gcatid,cr_gcatid,invoice,ttype,details,note,amount,date,ptime,userid,em_id,active) 
                    VALUES ('$coid','$bid','$brid','$dr_acid', '$cr_acid', '$uid', '$dr_gcatid', '$cr_gcatid', '$invoice[$i]', 'OP','Opening Balance','opening','$amount','$op_date','$time','$uid','$us_em_id','1')")or die (mysql_error());

 }
}

?>