<?php require_once('../auth/auth.php');require_once('../auth/acid_balance.php');require_once('../auth/function.php'); ?>
<?php
if(!empty($_FILES['csv_file']['name']))
{
    $filename= mysqli_real_escape_string($conn,$_FILES["csv_file"]["tmp_name"]);
    $file_data = fopen($filename, 'r');

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


     $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"));
     $cr_acid = $op_row['acid'];
     $cr_gcatid = $op_row['gcatid'];

     $inv_row= mysqli_fetch_assoc(mysqli_query($conn,"SELECT acid,gcatid FROM accounts WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND ac_type='stock' LIMIT 1"));
     $dr_acid = $inv_row['acid'];
     $dr_gcatid = $inv_row['gcatid']; 

     $grpid= mysqli_fetch_assoc(mysqli_query($conn,"SELECT pgid FROM item_group WHERE coid='$coid' AND bid='$bid' AND default_set='primary' ORDER BY pgid ASC LIMIT 1"))['pgid'];

     $ctgid= mysqli_fetch_assoc(mysqli_query($conn,"SELECT pcid FROM item_catagory WHERE coid='$coid' AND bid='$bid' AND default_set='primary' ORDER BY pcid ASC LIMIT 1"))['pcid'];

     $brnid= mysqli_fetch_assoc(mysqli_query($conn,"SELECT pbid FROM item_brand WHERE coid='$coid' AND bid='$bid' AND default_set='primary' ORDER BY pbid ASC LIMIT 1"))['pbid'];

     $untid= mysqli_fetch_assoc(mysqli_query($conn,"SELECT puid FROM pro_unit WHERE coid='$coid' AND default_set='primary' ORDER BY puid ASC LIMIT 1"))['puid'];

     $ptxid= mysqli_fetch_assoc(mysqli_query($conn,"SELECT ptid FROM pro_tax WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND sp_type='purchase' AND default_set='primary' ORDER BY ptid ASC LIMIT 1"))['ptid'];

     $psxid= mysqli_fetch_assoc(mysqli_query($conn,"SELECT ptid FROM pro_tax WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND sp_type='sales' AND default_set='primary' ORDER BY ptid ASC LIMIT 1"))['ptid'];

     $btcid= mysqli_fetch_assoc(mysqli_query($conn,"SELECT batid FROM batch_no WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND default_set='primary' ORDER BY batid ASC LIMIT 1"))['batid'];

     $bwid= mysqli_fetch_assoc(mysqli_query($conn,"SELECT bwid FROM business_w_house WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND default_set='primary' ORDER BY bwid ASC LIMIT 1"))['bwid'];
   
$count = 0;
while (($row = fgetcsv($file_data, 1000000, ",")) !== FALSE)
 {
    $count++;
    if ($count == 1) { continue; }    
    $invoice      = invoice("SOP");

    $pgroup_name  = mysqli_real_escape_string($conn,$row[0]);
    $pcat_name    = mysqli_real_escape_string($conn,$row[1]);
    $pbrand_name  = mysqli_real_escape_string($conn,$row[2]);
    $item_name    = mysqli_real_escape_string($conn,$row[3]);
    $item_code    = mysqli_real_escape_string($conn,$row[4]);
    $item_qty     = $row[5];
    $mrp_purchase = $row[6];
    $min_sales    = $row[7];
    $mrp_sales    = $row[8];
    $item_details = mysqli_real_escape_string($conn,$row[9]);
    $location     = mysqli_real_escape_string($conn,$row[10]);
    $item_sku     = mysqli_real_escape_string($conn,$row[11]);
    $unit_name    = mysqli_real_escape_string($conn,$row[12]);
    $ptax_group   = mysqli_real_escape_string($conn,$row[13]);
    $stax_group   = mysqli_real_escape_string($conn,$row[14]);
    $batch_no     = mysqli_real_escape_string($conn,$row[15]);
    $item_notif   = mysqli_real_escape_string($conn,$row[16]);
    $item_weight  = mysqli_real_escape_string($conn,$row[17]);
    $item_disc    = mysqli_real_escape_string($conn,$row[18]);

    mysqli_query($conn,"INSERT INTO `item`(`coid`, `bid`, `brid`, `pgid`, `pcid`, `pbid`,`location`, `pro_batchid`, `item_code`, `item_sku`, `pro_type`, `item_name`, `item_details`, `item_qty`, `pro_unit`, `mrp_purchase`, `avg_cost`, `min_sales`, `mrp_sales`, `item_disc`, `pro_ptax`, `pro_stax`, `item_notif`, `item_weight`, `item_op_date`, `active`) 
        VALUES ('$coid','$bid','$brid',
        COALESCE((SELECT pgid FROM item_group WHERE coid='$coid' AND bid='$bid' AND pgroup_name='$pgroup_name' LIMIT 1),'$grpid'),
        COALESCE((SELECT pcid FROM item_catagory WHERE coid='$coid' AND bid='$bid' AND pcat_name='$pcat_name' LIMIT 1),'$ctgid'),
        COALESCE((SELECT pbid FROM item_brand WHERE coid='$coid' AND bid='$bid' AND pbrand_name='$pbrand_name' LIMIT 1),'$brnid'),
        '$location',
        COALESCE((SELECT batid FROM batch_no WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND batch_no='$batch_no' LIMIT 1),'$btcid'),
        '$item_code', '$item_sku', 'P','$item_name','$item_details',0,
        COALESCE((SELECT puid FROM pro_unit WHERE coid='$coid' AND unit_name='$unit_name' LIMIT 1),'$untid'),
        '$mrp_purchase','$mrp_purchase','$min_sales','$mrp_sales','$item_disc',
        COALESCE((SELECT ptid FROM pro_tax WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND sp_type='purchase' AND tax_group='$ptax_group' LIMIT 1),'$ptxid'),
        COALESCE((SELECT ptid FROM pro_tax WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND sp_type='sales' AND tax_group='$stax_group' LIMIT 1),'$psxid'),'$item_notif','$item_weight','$op_date',1)") or die(mysqli_error($conn));
    $litem = mysqli_insert_id($conn);

   if ($item_qty>0 && $litem>0) {
       
       mysqli_query($conn,"INSERT INTO `order_list`(`coid`, `bid`, `brid`, `acid`, `invoice`, `order_type`, `wh_id`, `pro_id`, `item_code`, `pro_type`, `plus`, `qty`, `run_qty`, `pu_price`, `avg_price`, `sale_price`, `change_slprice`, `amount`, `t_amount`, `net_amount`, `details`, `userid`, `datetime`, `active`) 
            VALUES ('$coid','$bid','$brid','$cr_acid', '$invoice', 'SOP', '$bwid', '$litem', '$item_disc', 'P','+','$item_qty','$item_qty','$mrp_purchase','$mrp_purchase','$mrp_sales','$mrp_sales','$mrp_purchase',($mrp_purchase*$item_qty),($mrp_purchase*$item_qty),'Opining Item','$uid','$op_date','1')") or die(mysqli_error($conn));

       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', 'SOP','$item_name','opening',($mrp_purchase*$item_qty),'$op_date','$time','$uid','$us_em_id','1')") or die(mysqli_error($conn));                     
   }
 }

       $dr_balance = balance($dr_acid)['ac_balance'];
       $cr_balance = balance($cr_acid)['ac_balance'];
       mysqli_query($conn,"UPDATE accounts SET balance='$dr_balance' WHERE coid='$coid' AND acid ='$dr_acid'") or die(mysqli_error($conn));
       mysqli_query($conn,"UPDATE accounts SET balance='$cr_balance' WHERE coid='$coid' AND acid ='$cr_acid'") or die(mysqli_error($conn));  

}

?>