
<?php
session_start();
error_reporting(0);
@ini_set('display_errors', 0);
$id_user=$_SESSION['id'];
$branch=$_SESSION['branch_id'];
$business=$_SESSION['business_id'];
?>
<!DOCTYPE html>
<html>
    <script type="text/javascript">
    function FormatCurrency(ctrl) {
                  //Check if arrow keys are pressed - we want to allow navigation around textbox using arrow keys
                  if (event.keyCode == 37 || event.keyCode == 38 || event.keyCode == 39 || event.keyCode == 40) {
                      return;
                  }

                  var val = ctrl.value;

                  val = val.replace(/,/g, "")
                  ctrl.value = "";
                  val += '';
                  x = val.split('.');
                  x1 = x[0];
                  x2 = x.length > 1 ? '.' + x[1] : '';

                  var rgx = /(\d+)(\d{3})/;

                  while (rgx.test(x1)) {
                      x1 = x1.replace(rgx, '$1' + ',' + '$2');
                  }

                  ctrl.value = x1 + x2;
              }

              function CheckNumeric() {
                  return event.keyCode >= 48 && event.keyCode <= 57 || event.keyCode == 46;
              }
</script>
<?php
    include("../includes/header-links.php");
    
$permissions = mysqli_fetch_array(mysqli_query($con,"SELECT * FROM  
  permissions,users WHERE  permissions.role_id = users.role 
  AND users.id='$id_user' AND users.business_id ='$business' AND users.branch_id ='$branch'"));

if($permissions['nav_products'] != 1){
?>
<script>window.location="../main/dashboard.php"</script>
<?php
}
?>
    
    <head>

    
    <link rel="stylesheet" href="../data-table-css/bootstrap-table.css">
    <link rel="stylesheet" href="../data-table-css/bootstrap-editable.css">
        
    </head>
    
    <body>
        <?php include '../includes/header.php'; ?>
        <?php
                                                $checkarrangement=mysqli_fetch_array(mysqli_query($conn,"SELECT * from receipt_settings where branch_id='$branch'"));
                                                $arrange_products=$checkarrangement['arrange_products'];

                                                
                                                
                                                   ?>
        <div class="container-fluid">
    
            <div class="row-fluid">
                <?php include '../includes/sidebar.php'; ?>
                <!--/span-->
                <div class="span10" id="content">
          

                     <div class="row-fluid">
                        <!-- block -->
                        <div class="block">
            
                            <div class="navbar navbar-inner block-header">
                                <div class="muted pull-left">Products in stock</div>
                            </div>
                            <div class="block-content collapse in">
              
                                <div class="span12">
<div class="row">
  <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">

  </div>
  <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
  </div>     
</div>
                                   
        
                                    
                                  <div class="btn-group">
                                         <a href="#myModalUpload" data-toggle="modal" class="btn btn-success"><i class="icon-plus icon-white"></i>&nbsp;Upload</a>
                                      </div>

                                                 <div id="myModalUpload" class="modal hide">
                      <div class="modal-header" style="background-color: #08c; color: white;">
                        <button data-dismiss="modal" class="close" type="button">&times;</button>
                        <h3>Upload New Product</h3>
                      </div>
                      <div class="modal-body">
                        <form action="" method="POST" enctype="multipart/form-data"> 
                         
                      <div align="center" class="col-lg-12">
                        <label>Product File</label>
                        
                        <input type="file" name="file" class="form-control"  style="width: 95%;" required="required">
                      </div>
                       
                      
                        <div class="form-actions">
                                          <button type="submit" name="upload" class="btn btn-primary">Save </button>
                                          <button class="btn btn-danger" data-dismiss='modal'>Cancel</a>
                                        </div>  
                        </form>
                      </div>
                    </div>

                    <?php


if(isset($_POST['upload'])){
  include '../includes/connection.php';
  include '../phpexcel/PHPExcel/IOFactory.php';

$allowedFileType = [
        'application/vnd.ms-excel',
        'text/xls',
        'text/xlsx',
        'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
    ];


$tt=$business."".$branch."".date('Y')."".date('m')."".date('d')."".date('H')."".date('i')."".date('s');

    if (in_array($_FILES["file"]["type"], $allowedFileType)) {

        $targetPath = '../uploads/' .$tt."".$_FILES['file']['name'];
        move_uploaded_file($_FILES['file']['tmp_name'], $targetPath);

$objectphpexcel=PHPExcel_IOFactory::load($targetPath);

foreach ($objectphpexcel->getWorksheetIterator() as $worksheet) {
  $highstRow=$worksheet->getHighestRow();
  for ($row=2; $row <= $highstRow; $row++) { 
    $product_name = mysqli_real_escape_string($con,$worksheet->getCellByColumnAndRow(0,$row)->getValue());
    $Ucategory = mysqli_real_escape_string($con,$worksheet->getCellByColumnAndRow(1,$row)->getValue());
    $pcode = mysqli_real_escape_string($con,$worksheet->getCellByColumnAndRow(2,$row)->getValue());
    $xlquantity = mysqli_real_escape_string($con,$worksheet->getCellByColumnAndRow(3,$row)->getValue());
    $xlminquantity = mysqli_real_escape_string($con,$worksheet->getCellByColumnAndRow(4,$row)->getValue());
    $xlbuying_price = mysqli_real_escape_string($con,$worksheet->getCellByColumnAndRow(5,$row)->getValue());
    $unitprice = mysqli_real_escape_string($con,$worksheet->getCellByColumnAndRow(6,$row)->getValue());
    $xlresreve_price = mysqli_real_escape_string($con,$worksheet->getCellByColumnAndRow(7,$row)->getValue());
   $xlhasstock = mysqli_real_escape_string($con,$worksheet->getCellByColumnAndRow(8,$row)->getValue());
    $Units = mysqli_real_escape_string($con,$worksheet->getCellByColumnAndRow(9,$row)->getValue());
    $xlvat = mysqli_real_escape_string($con,$worksheet->getCellByColumnAndRow(10,$row)->getValue());
    if ($xlminquantity!='') {$minquantity=$xlminquantity;}else{$minquantity=0;}
    if ($xlbuying_price!='') {$buying_price=$xlbuying_price;}else{$buying_price=0;}
    if ($xlresreve_price!='') {$reserve_price=$xlresreve_price;}else{$reserve_price=0;}
    if ($xlquantity!='') {$quantity=$xlquantity;}else{$quantity=0;}
    if ($xlhasstock!='') {$hasstock=$xlhasstock;}else{$hasstock=1;}
    if ($xlvat!='') {$vat=$xlvat;}else{$vat=0;}
    if ($product_name!='' || $Ucategory!='' || $unitprice!='' ) {
          $sql0 = mysqli_query($con,"SELECT * from products where (product_name='$product_name' || product_code='$pcode' && product_code!='') && branch_id='$branch' && status=1");
          if(mysqli_num_rows($sql0)>0){
            // echo"<script>alert('Failed! Product name or Product Code already exists'),history.go(-1)</script>";
          }
          else{ 
            if ($Ucategory!='') {
              $sqlcat = mysqli_query($con,"SELECT * from product_categories where category_name='$Ucategory' && branch_id='$branch' limit 1 ") or die(mysqli_error($con));
                  $num0 = mysqli_num_rows($sqlcat);
                  if($num0>0){
                  $ctre=mysqli_fetch_array($sqlcat);
                  $category=$ctre['category_id'];
                  }
                  else{
                  $sqlinsertcategory = mysqli_query($con,"INSERT INTO product_categories (category_name,branch_id,business_id,AddedBy)
                    VALUES ('$Ucategory', '$branch','$business','$id_user')");
                  $ctre = mysqli_fetch_array(mysqli_query($con,"SELECT * from product_categories where category_name='$Ucategory' && branch_id='$branch' limit 1 "));
                $category=$ctre['category_id'];

                }
            }else{
              $category=0;
            }
            if ($Units!='') {
              $unitcheck = mysqli_query($con,"SELECT * from units where Name='$Units' && branch_id='$branch' limit 1 ") or die(mysqli_error($con));
                  $numu = mysqli_num_rows($unitcheck);
                  if($numu>0){
                  $unitget=mysqli_fetch_array($unitcheck);
                  echo"units".$unit=$unitget['UnitID'];
                  }
                  else{
                  $sqluni = mysqli_query($con,"INSERT INTO units(Name, Symbol,branch_id,AddedBy)
          VALUES ('$Units', '$Units','$branch', '$id_user')");
                  $unitget = mysqli_fetch_array(mysqli_query($con,"SELECT * from units where Name='$Units' && branch_id='$branch' limit 1 "));
                echo"unitss".$unit=$unitget['UnitID'];

                }
            }else{
              $unit=0;
            }
                 
                
                    $id_user=$_SESSION['id'];
                    $unitpricewhole='0';
                    $sql = mysqli_query($con,"INSERT INTO products (category,product_name,product_code,quantity,has_stock,VAT,minimum_quantity,Units, price,price_whole,reserve_price,branch_id,business_id,AddedBy)
                      VALUES ('$category','$product_name','$pcode','$quantity','$hasstock','$vat','$minquantity', '$unit','$unitprice','$unitpricewhole','$reserve_price','$branch','$business','$id_user')");

                    if ($sql) {
                      $activity="Added a new product ".$product_name;
                 

                        $log_sql=mysqli_query($con,"INSERT INTO logs(user_id, activity,branch_id,business_id) VALUES ('$id_user','$activity','$branch','$business')");

                        if ($log_sql) {
                              $id_user=$_SESSION['id'];
                              $branch=$_SESSION['branch_id'];
                              $business=$_SESSION['business_id'];

                              $sqlstockcheck = mysqli_query($con,"SELECT * FROM products where id=(select max(id) from products) and AddedBy='$id_user'");
                              if (mysqli_num_rows($sqlstockcheck) > 0) {
                                  // output data of each row
                                  $rowsto = mysqli_fetch_assoc($sqlstockcheck);
                                    $productid=$rowsto['id'];
                                    $selling_price=$rowsto['price'];
                                    $selling_price_whol=$rowsto['price_whole'];
                                    if (strlen($selling_price_whol)==0) {
                                     $selling_price_whole=0;
                                    }else{
                                      $selling_price_whole=$selling_price_whol;
                                    }
                                    $min_quantity=$rowsto['minimum_quantity'];
                                    $quantitys=$rowsto['quantity'];
                                    // $buying_price=$_GET['buying_price'];
                                  $sql2insertstock = mysqli_query($con,"INSERT INTO stock(product_name,buying_price, selling_price,selling_price_whole,quantity,min_quantity,branch_id,business_id,AddedBy)
                                    VALUES('$productid','$buying_price','$selling_price','$selling_price_whole','$quantitys','$min_quantity','$branch','$business','$id_user' )");
                                  if($sql2insertstock){
                                    echo "success";
                                     
                                  }else{
                                    echo "Failed to insert stock";

                                  }
                              } else {
                                  echo "0 results";
                              }


                             


                        } else {
                            echo "Error: Failed to add log <br>" . mysqli_error($con);
                            }
                
                            } else {
                          echo "Error: " . $sql . "<br>" . mysqli_error($con);
                             }
            
            }
      
    }else{
   echo "No Match";
    }




  } 
 
   
 } 

}else{

}
?><meta http-equiv="refresh" content="0;URL='products.php'" /> <?php
}


?>
                                   <?php 
                                   //if($_SESSION['role']=="admin"|| $_SESSION['role']=="Super Admin"){
                                   if( $permissions['add_product'] == 1){
                                    ?>
                                            <div class="btn-group">
                                         <a href="#myModal" data-toggle="modal" class="btn btn-success"><i class="icon-plus icon-white"></i>&nbsp;Add New</a>
                                      </div>
                                          <div class="btn-group">
                                         <a href="stock.php" class="btn btn-primary"><i class="icon-briefcase icon-white"></i>&nbsp;Stock</a>
                                      </div>
                                      <div class="btn-group">
                                       <a href="product_category.php" ><button class="btn btn-warning"> <i class="icon-indent-left"></i> Product Categories</button></a>
                                     </div>
                                     <div class="btn-group">
                                       <a href="services.php" ><button class="btn btn-warning"> <i class="icon-indent-left"></i> Servcies</button></a>
                                     </div>

                                    <?php
                                   }
                                   ?>
                                      

                                      <!--Model begins here -->
                                      <div id="myModal" class="modal hide">
                      <div class="modal-header" style="background-color: #08c; color: white;">
                        <button data-dismiss="modal" class="close" type="button">&times;</button>
                        <h3>Add new Product</h3>
                      </div>
                      <div class="modal-body" style="max-height: 550px;">
                        <form action="" method="POST" enctype="multipart/form-data"> 

                         <div align="center" class="span12">
                         <div align="center" class="span6">
                          <label>Category</label>
                        
                        <select name="category" class="form-control" required style="width: 95%; height: 30px;">
                          <option disabled selected="" value="">Select Category</option>
                         <?php 
                         $cat=mysqli_query($con,"SELECT * from product_categories where status=1 && branch_id='$branch'");
                         while ( $rowc=mysqli_fetch_array($cat)) {
                           ?>
                           <option value="<?php echo $rowc['category_id'];?>"><?php echo $rowc['category_name'];?></option>
                           <?php
                         }
                          ?>
                         
                          
                        </select>
                      </div>
                      <div align="center" class="span6">
                        <label>Product Name</label>
                        
                        <input type="text" name="name" class="form-control" placeholder="Product Name" style="width: 95%;" required="required">
                      </div>
                      <?php
                      if ($gbusiness_settings['include_services']==1) {
                        ?>
                      <div align="center" class="col-lg-12">
                        <label>Has Stock</label>
                        
                        <select name="hasstock" class="form-control" onchange="HidePCodeMin(this.value)"  style="width: 95%;" required="required">
                          <option value="" disabled="" selected="">Select One</option>
                          <option value="1">Yes</option>
                          <option value="0">No</option>
                        </select>
                      </div>
                      <script type="text/javascript">
                        function HidePCodeMin(val) {
                          if (val==1) {
                          document.getElementById("PcodeMin").style.display="block";


                          }else{
                          document.getElementById("PcodeMin").style.display="none";

                          }
                        }
                      </script>
                        <?php
                       
                      }else{
                        ?>
                        <input type="hidden" name="hasstock" value="1">

                        <?php

                      }
                      ?>
                      <?php
                      if ($gbusiness_settings['vat_legibility']==1) {
                        ?>
                      <div align="center" class="col-lg-12">
                        <label>Legible for VAT</label>
                        
                        <select name="vat" class="form-control"  style="width: 95%;" required="required">
                          <option value="" disabled="" selected="">Select One</option>
                          <option value="0">No</option>
                          <option value="1">Yes</option>
                        </select>
                      </div>
                      <?php
                       
                      }else{
                        ?>
                        <input type="hidden" name="vat" value="0">

                        <?php

                      }
                      ?>
                      
                       <div align="center" id="PcodeMin" class="span12">
                       <div align="center" class="span5">
                        <label>Product Code </label>
                        
                        <input type="text" name="pcode" class="form-control" placeholder="Product Code" style="width: 95%;">
                      </div>

                      <div align="center" class="span6">
                        <label>Minimum Quantity</label>
                        <input type="number" name="minquantity" value="1" class="form-control" style="width: 105%;" required="">
                      </div>
                      </div>
                      <div align="center" class="span5">
                        <label>Units</label>
                        <select name="units" class="form-control" required style="width: 95%;">
                          <option value="">Select units</option >
                          <?php
                          include("../includes/connection.php");
    
                          $query = mysqli_query($con,"select * from units where branch_id='$branch'");
                          while($row0=mysqli_fetch_array($query)){
                          ?>
                          <option value="<?php echo $row0['UnitID'];?>"><?php echo$row0['Symbol'];?></option>
                          <?php
                          
                          }
                          ?>
                          
                        </select>
                      </div>
                      <div align="center" class="span6">
                        <label>Unit Selling Price</label>
                        <input type="text" onkeypress="return CheckNumeric()" onkeyup="FormatCurrency(this)" name="unitprice" class="form-control" placeholder="Unit Price" style="width: 105%;" required="required">
                      </div>
                      <?php
                      if ($business_type==1) {
                        ?>
                        <div align="center" class="col-lg-12">
                        <label>Wholesale Unit Selling Price</label>
                        <input type="text" onkeypress="return CheckNumeric()" onkeyup="FormatCurrency(this)" name="unitpricewhole" class="form-control" placeholder="Unit Price for whole sale" style="width: 95%;" value="0">
                      </div>
                        <?php
                      }
                      ?>
                      <?php
                      if ($gbusiness_settings['inlude_reserve_prices']==1) {
                        ?>
                      <div align="center" class="col-lg-12">
                        <label>Reserve Price</label>
                        <input type="text" onkeypress="return CheckNumeric()" onkeyup="FormatCurrency(this)" name="reserve_price" class="form-control" placeholder="Reserve Price" style="width: 95%;" required="required">
                      </div>
                      <?php
                        }else{
                          ?>
                          <input type="hidden" name="reserve_price" value="0">
                          <?php
                        }
                        ?>
                      
                      
                        <div class="form-actions">
                                          <button type="submit" name="add" class="btn btn-primary">Save Product</button>
                                          <a href="Products.php" class="btn btn-danger">Cancel</a>
                                        </div>  
                        </form>
                      </div>
                      </div>
                    </div>
                    <!--Model Ends here -->
                                   
                                   <?php
include '../includes/connection.php';
       // if($_SESSION['role']=="Super Admin"){
if($permissions['business_id'] != 0 && $permissions['branch_id']==0){

               if ($arrange_products=="Aphabetical Order") {
       
              $sql = mysqli_query($con,"SELECT *,(select branch_name from branches where branch_id=products.branch_id) as bchname FROM products where status=1 && has_stock=1 && business_id='$business' order by product_name");
             
             }else if ($arrange_products=="By Category") {
              $sql = mysqli_query($con,"SELECT *,(select branch_name from branches where branch_id=products.branch_id) as bchname FROM products where status=1 && has_stock=1 && business_id='$business' order by category");
        
     
              }else if ($arrange_products=="By Quantity") {
              $sql = mysqli_query($con,"SELECT *,(select branch_name from branches where branch_id=products.branch_id) as bchname FROM products where status=1 && has_stock=1 && business_id='$business' order by quantity DESC");

                }else{
                  $sql = mysqli_query($con,"SELECT *,(select branch_name from branches where branch_id=products.branch_id) as bchname FROM products where status=1 && has_stock=1 && business_id='$business' ");

                }
        
      }else{

               if ($arrange_products=="Aphabetical Order") {
             $sql = mysqli_query($con,"SELECT * FROM products where status=1 && has_stock=1 && branch_id='$branch' order by product_name");
               

             }else if ($arrange_products=="By Category") {
             $sql = mysqli_query($con,"SELECT * FROM products where status=1 && has_stock=1 && branch_id='$branch' order by category");
     
              }else if ($arrange_products=="By Quantity") {
             $sql = mysqli_query($con,"SELECT * FROM products where status=1 && has_stock=1 && branch_id='$branch' order by quantity DESC");

                }else{
             $sql = mysqli_query($con,"SELECT * FROM products where status=1 && has_stock=1 && branch_id='$branch'");
                 
                }
        
      }                // Create connection

//$result = mysqli_query($con, $sql);

if (mysqli_num_rows($sql) > 0) {
    // output data of each row
    


                                   ?>
        </div>
                                    <div class="datatable-dashv1-list custom-datatable-overright">
                                    <div id="toolbar">
                                        <select class="form-control">
                                                <option value="">Export Basic</option>
                                                <option value="all">Export All</option>
                                                <option value="selected">Export Selected</option>
                                            </select>
                                    </div>
                                    <table  id="table" data-toggle="table" data-pagination="true" data-search="true" data-show-columns="true" data-show-pagination-switch="true" data-show-refresh="true" data-key-events="true" data-show-toggle="true" data-resizable="true" data-cookie="true"
                                        data-cookie-id-table="saveId" data-show-export="true" data-click-to-select="true" data-toolbar="#toolbar" data-export-options='{"fileName": "Testing_filename"}'>
                                        <thead>
                                            <tr>
                                                <th>ID</th>
                                                <th>Category</th>
                                                <th>Product Name</th>
                                                <th>Product Code</th>
                                                <th>Quantity</th>
                                                <th>Price</th>
                                                <?php if($business_type==1){ ?>
                                                <th>Wholesale price</th>
                                                <?php } ?>
                                                <?php 
                                                //if($_SESSION['role']=="admin"){ 
                                                // if($permissions['business_id'] != 0 && $permissions['branch_id']==0){
                                                if( $permissions['edit_product'] == 1){
                                                ?>
                                                <th>Action</th>
                                                <?php } ?>

                                             </tr>
                                        </thead>
                                        <tbody>
                                        
                                        <?php
                                        $no=0;
while($row = mysqli_fetch_assoc($sql)) {
   $no++;

        ?>
<tr class="gradeU">
                                                <td><?php echo $no; ?></td>
                                                <?php
                                                 $categ=$row['category']; 
                                                $sqlc=mysqli_fetch_array(mysqli_query($con,"SELECT * FROM product_categories where category_id='$categ'"));
                                                 ?>
                                                <td><?php echo $sqlc['category_name']; ?></td>
                                                <td><?php echo $row['product_name']; ?></td>
                                                <td><?php echo $row['product_code']; ?></td>
                                               <!-- <td><img src="<?php// echo $row['ProductImage'];?>" alt="no image" width="100px" height="100px"/></td>-->
                                                <td><?php echo $row['quantity']; ?></td>
                                                <td class="center"><?php echo number_format($row['price']); ?></td>
                                                <?php if($business_type==1){ ?>
                                                <td class="center"><?php echo number_format($row['price_whole']); ?></td>
                                                <?php } ?>
                                                <?php
                                                // if($_SESSION['role']=="admin"){
                                                if( $permissions['edit_product'] == 1){
                                                 ?>
                                                <td class="center">
                                                <?php $newid=$row['id'];
                                                    $prodname=$row['product_name'];?>
                                                
                                                  <a href="editproduct.php?id=<?php echo $row['id']; ?>"><button class="btn btn-primary"><i class="icon-edit icon-white"></i></button></a>
                                                  <a href="deleteconfirm.php?id=<?php echo base64_encode($row['id']); ?>&prodname=<?php echo base64_encode($prodname); ?>"><button class="btn btn-danger"><i class="icon-trash icon-white"></i></button></a>
                                                  
                  <div id="myAlert" class="modal hide">
                      <div class="modal-header">
                        <button data-dismiss="modal" class="close" type="button">&times;</button>
                        <h3>Warning !</h3>
                      </div>
                      <div class="modal-body">
                        <p>Are you sure you want to delete this product?</p>
                      </div>
                      <div class="modal-footer">
                        <a href="deleteproduct.php?id=<?php echo $newid; ?>"><button class="btn btn-primary"> Yes</button></a>
                        <a data-dismiss="modal" class="btn" href="#">No</a>
                      </div>
                    </div>
                                            
                                                </td>
                                                <?php 
                                              } 
                                              ?>
                                            </tr>
                                            
                                            <?php


    }
} else {
    echo "No products added yet!";
}

mysqli_close($con);

 ?>
                                            
                                        </tbody>
                                    </table>
                                  </div>
                                    
                                </div>
                            </div>
                        </div>
                        <!-- /block -->
                    </div>
                </div>
            </div>
            <hr>
            <?php include '../includes/footer.php'; ?>
        </div>
        <!--/.fluid-container-->

        <!--==========Footer js======-->
    <?php require_once("../includes/footerjs.php");?>
        <!--==========Footer js======-->
       
 

<?php


if(isset($_POST['add'])){
  include '../includes/connection.php';
$category=mysqli_real_escape_string($con,ucwords($_POST['category']));
$product_name=mysqli_real_escape_string($con,ucwords($_POST['name']));
$pcode=mysqli_real_escape_string($con,$_POST['pcode']);
$vat=mysqli_real_escape_string($con,$_POST['vat']);
$minquantity=mysqli_real_escape_string($con,$_POST['minquantity']);
$sql0 = mysqli_query($con,"SELECT * from products where (product_name='$product_name' || product_code='$pcode' && product_code!='') && branch_id='$branch' && status=1");
if(mysqli_num_rows($sql0)>0){
  echo"<script>alert('Failed! Product name or Product Code already exists'),history.go(-1)</script>";
}
else{ 
      
$id_user=$_SESSION['id'];
  
$hasstock=$_POST['hasstock'];
$unit=$_POST['units'];
 $bad_symbols = array(",", ".");

$unitprice= mysqli_real_escape_string($con,$_POST['unitprice']);
$reserve_price= mysqli_real_escape_string($con,$_POST['reserve_price']);
$unitpricewhole= mysqli_real_escape_string($con,$_POST['unitpricewhole']);
$unitprice= str_replace($bad_symbols, "", $unitprice);
$reserve_price= str_replace($bad_symbols, "", $reserve_price);
$unitpricewhole= str_replace($bad_symbols, "", $unitpricewhole);
$sql = mysqli_query($con,"INSERT INTO products (category,product_name,product_code,has_stock,minimum_quantity,Units, price,price_whole,reserve_price,VAT,branch_id,business_id,AddedBy)
VALUES ('$category','$product_name','$pcode','$hasstock','$minquantity', '$unit','$unitprice','$unitpricewhole','$reserve_price','$vat','$branch','$business','$id_user')");

if ($sql) {


    $id_user=$_SESSION['id'];
  $activity="Added a new product ".$product_name;
     

 $log_sql=mysqli_query($con,"INSERT INTO logs(user_id, activity,branch_id,business_id) VALUES 
('$id_user','$activity','$branch','$business')");

if ($log_sql) {

  ?>
    <meta http-equiv="refresh" content="0;URL='defaultdata.php'" />  
    <?php


} else {
    echo "Error: Failed to add log <br>" . mysqli_error($con);
}
    
    
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($con);
}
}
mysqli_close($con);
}

?>
<script src="../data-table-js/bootstrap-table.js"></script>
    <script src="../data-table-js/tableExport.js"></script>
    <script src="../data-table-js/data-table-active.js"></script>
    <script src="../data-table-js/bootstrap-table-editable.js"></script>
    <script src="../data-table-js/bootstrap-editable.js"></script>
    <script src="../data-table-js/bootstrap-table-resizable.js"></script>
    <script src="../data-table-js/colResizable-1.5.source.js"></script>
    <script src="../data-table-js/bootstrap-table-export.js"></script>
    </body>
</html>
