<?php require_once('../auth/auth.php');require_once('../auth/acid_balance.php');require_once('../auth/function.php'); ?>
<?php
if(isset($_POST["group"])){
     $group    = $_POST["group"];
     $category = $_POST["category"];
     $brand    = $_POST["brand"];
     $name     = $_POST["name"];
     $code     = $_POST["code"];
     $qty      = $_POST["qty"];
     $pmrp     = $_POST["pmrp"];
     $mnrp     = $_POST["mnrp"];
     $mxrp     = $_POST["mxrp"];
     $details  = $_POST["details"];
     $location = $_POST["location"];
     $sku      = $_POST["sku"];
     $utype    = $_POST["utype"];
     $ptax     = $_POST["ptax"];
     $stax     = $_POST["stax"];
     $batch    = $_POST["batch"];
     $notice   = $_POST["notice"];
     $weight   = $_POST["weight"];
     $disc     = $_POST["disc"];
     $active   = 1;
     $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'];

for ($i = 0; $i < count($group); $i++){
    
    $invoice = invoice("SOP");

    if (!empty($group[$i])) {
        $pgid = mysqli_fetch_assoc(mysqli_query($conn,"SELECT pgid FROM item_group WHERE coid='$coid' AND bid='$bid' AND pgroup_name='$group[$i]' LIMIT 1"))['pgid'];
        $item_gid = $pgid>0?$pgid:$grpid;       
    } else {
        $item_gid=$grpid;
    }

    if (!empty($category[$i])) {
        $pcid = mysqli_fetch_assoc(mysqli_query($conn,"SELECT pcid FROM item_catagory WHERE coid='$coid' AND bid='$bid' AND pcat_name='$category[$i]' LIMIT 1"))['pcid'];
        $item_cid = $pcid>0?$pcid:$ctgid;       
    } else {
        $item_cid=$ctgid; 
    }

    if (!empty($brand[$i])) {
        $pbid = mysqli_fetch_assoc(mysqli_query($conn,"SELECT pbid FROM item_brand WHERE coid='$coid' AND bid='$bid' AND pbrand_name='$brand[$i]' LIMIT 1"))['pbid'];
        $item_brnd = $pbid>0?$pbid:$brnid;       
    } else {
        $item_brnd=$brnid;
    }

    if (!empty($utype[$i])) {
        $puid = mysqli_fetch_assoc(mysqli_query($conn,"SELECT puid FROM pro_unit WHERE coid='$coid' AND unit_name='$utype[$i]' LIMIT 1"))['puid'];
        $u_type = $puid>0?$puid:$untid;       
    } else {
        $u_type=$untid;
    }

    if (!empty($ptax[$i])) {
        $ptid = 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 tax_group='$ptax[$i]' LIMIT 1"))['ptid'];
        $p_tax = $ptid>0?$ptid:$ptxid;       
    } else {
        $p_tax=$ptxid; 
    }

    if (!empty($stax[$i])) {
        $sptid = 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 tax_group='$stax[$i]' LIMIT 1"))['ptid'];
        $s_tax = $sptid>0?$sptid:$psxid;       
    } else {
        $s_tax=$psxid;
    }

    if (!empty($batch[$i])) {
        $batid = mysqli_fetch_assoc(mysqli_query($conn,"SELECT batid FROM batch_no WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND batch_no='$batch[$i]' LIMIT 1"))['batid'];
        $batch_id = $batid>0?$batid:$btcid;       
    } else {
        $batch_id=0;
    }


    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','$item_gid', '$item_cid', '$item_brnd', '$location[$i]', '$batch_id', '$code[$i]', '$sku[$i]', 'P','$name[$i]','$details[$i]','$qty[$i]','$u_type','$pmrp[$i]','$pmrp[$i]','$mnrp[$i]','$mxrp[$i]','$disc[$i]','$p_tax','$s_tax','$notice[$i]','$weight[$i]','$op_date',1)") or die(mysqli_error($conn));
    $litem = mysqli_insert_id($conn);

   if ($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`, `amount`, `net_amount`, `t_amount`, `details`, `userid`, `datetime`, `active`) 
            VALUES ('$coid','$bid','$brid','$cr_acid', '$invoice', 'SOP', '$bwid', '$litem', '$code[$i]', 'P','+','$qty[$i]','$qty[$i]','$pmrp[$i]','$pmrp[$i]','$mxrp[$i]','$pmrp[$i]','$pmrp[$i]',($pmrp[$i]*$qty[$i]),'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','Opining Item','opening',($pmrp[$i]*$qty[$i]),'$op_date','$time','$uid','$uid','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));                     
   }
 }
}

?>