<?php   include_once('config/db.php');   include_once('config/check.php');  

if (filter_has_var(INPUT_POST, "submit")) 
    try{
            $date =            htmlspecialchars (strtoupper($_POST['date']));
            $date =            date("Y-m-d", strtotime($date));
            $name =                  htmlspecialchars ( $_POST['name']);
            $code =                  htmlspecialchars ( $_POST['code']);
            $uom =                        htmlspecialchars ( $_POST['uom']);
            $ctype =  2 ;//                     htmlspecialchars ( $_POST['ctype']);
            $category =                   htmlspecialchars ( $_POST['category']);
            $relevel =                    htmlspecialchars ( $_POST['reorderlevel']);
            //$cost =                       htmlspecialchars ( $_POST['cost']);
            $warehouse =                  htmlspecialchars ( $_POST['warehouse']);
            $status =                     htmlspecialchars ( $_POST['status']);
            $description =           htmlspecialchars ( $_POST['description']);

            $maintype = 2 ;
            $cogs = 176 ;


      


    // sales   

    $stype=5; $sgroup=7 ; $ssubgroup=20 ;
    $sql = 'INSERT INTO coa ( date,type,cgroup,subgroup,name,description) VALUES(:date,:type,:group,:subgroup,:name,:desc )';
    $conn->prepare($sql)->execute (['date'=> $date,'type'=>$stype,'group'=>$sgroup,'subgroup'=>$ssubgroup,'name'=>$name,'desc' =>$description]);
    $salecoa  = $conn->lastInsertId(); 

    // creating coa stock 
    $sql = 'INSERT INTO coa ( date,type,cgroup,subgroup,name,description) VALUES(:date,:type,:group,:subgroup,:name,:desc )';
    $conn->prepare($sql)->execute (['date'=> $date,'type'=>$maintype,'group'=>$ctype,'subgroup'=>$category,'name'=>$name,'desc' =>$description]);
    $stockcoa  = $conn->lastInsertId(); 

     
// Adding into item
$sql = 'INSERT INTO pitem (date,stockcoa,salecoa,cogs,name,code,uom,type,category,reorderlevel,warehouse,status,description,uid) 
                          VALUES(:date,:stockcoa,:salecoa,:cogs,:name,:code,:uom,:type,:category,:rlevel,:whouse,:status,:desc,:uid )';
            $conn->prepare($sql)->execute(['date' =>$date,'stockcoa' =>$stockcoa ,'salecoa' =>$salecoa,'cogs' =>$cogs,'name' => $name,'code' => $code,'uom' => $uom,'type' => $ctype,'category'=>$category ,
                                           'rlevel'=>$relevel,'whouse' =>$warehouse,
                                           'status'=>$status,'desc'=> $description,'uid'=>$uid   ]);




            header("location:item_fg.php");

        } catch(Exception $e) { echo 'Message: ' .$e->getMessage();}

?>

<!DOCTYPE html>
<html>
<head>
<?php include_once('inc/link.php');   ?>  
</head>
<body <?php include_once('inc/nav.php');  ?>>
<div class="wrapper">

  <!-- Main Header -->
    <header class="main-header">

    <!-- Logo -->
       <?php include_once('inc/brand.php');   ?>

    <!-- Header Navbar -->
    <?php include_once('inc/navbar.php');   ?>

  </header>
 
  <!-- Left side column. contains the logo and sidebar -->
    <aside class="main-sidebar">

    <!-- sidebar: style can be found in sidebar.less -->
     <?php include_once('inc/menu.php');   ?>
    <!-- /.sidebar -->
  </aside>

  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">
        <section class="content-header">
          <h1>   <i class="fa fa-barcode"></i> Add Products   </h1>
            <ol class="breadcrumb">
            <!-- <li><a href="#"><i class="fa fa-dashboard"></i> </a></li>
            <li><a href="#"> </a></li>
            <li class="active"> </li> -->
            <li><a href="invitemsl.php" class="btn btn-default btn-md"> <i class="fa fa-cubes"></i> View All Listed Products   </a></li>
            </ol>
        </section>


<!-- Main content -->

  <section class="content container-fluid"> 
        <div class="box box-default box-xs">
            <div class="box-body">
                <div class="row">
                    <form class="form-horizontal add_form "  action="" method="post"  >
                        <div class="">    
                                       
                            <div class="col-sm-2"> 
                                <label > Date added </label>  
                                <input type="text"   autocomplete="off" class="form-control datepicker" name="date"   required=""   > 
                            </div>                                             
                            <div class="col-sm-4"> 
                                <label > Name  </label>  
                                    <input type="text"  name="name" value="" autocomplete="off" class="form-control"   required=""   > 
                            </div>                                                  
                            <div class="col-md-2">
                                <label > Code  </label>                          
                                <input type="text" class="form-control" name="code" required="" autocomplete="off" >
                            </div>    
                            <div class="col-md-4">
                                <label > Description  </label>                          
                                <input type="text" class="form-control" name="description" required="" autocomplete="off" >
                            </div>    
                            <div class="col-sm-3"> 
                                <label > Item Uom </label>  
                                    <select class="form-control  js-example-basic-single "  required="" name="uom" style="width:100%" >
                                        <?php $stmt = $conn->prepare("SELECT * FROM aduom  ");
                                        $stmt->execute(); foreach ($stmt as $row) {?>
                                                <option></option>
                                                <option value="<?php echo $row['id']; ?>"><?php
                                                    echo $row['name']; ?></option> <?php } ?>
                                    </select>                                         
                            </div>                                           
                            <div class="col-sm-3"> 
                                <label > Item Type </label>  
                                    <select class="form-control js-example-basic-single   "  required="" name="ctype" style="width:100%" >
                                        <?php $stmt = $conn->prepare("SELECT * FROM invgroup  ");
                                        $stmt->execute(); foreach ($stmt as $row) {?>
                                                <option></option>
                                                <option value="<?php echo $row['coa']; ?>"><?php
                                                    echo $row['name']; ?></option> <?php } ?>
                                    </select>                                         
                            </div>  
                            <div class="col-sm-3"> 
                                <label > Item Category </label>  
                                    <select class="form-control js-example-basic-single   "  required="" name="category" style="width:100%" >
                                        <?php $stmt = $conn->prepare("SELECT * FROM coa_group_sub where cgroup=17 or cgroup=18 or cgroup=2 and status=1 ");
                                        $stmt->execute(); foreach ($stmt as $row) {?>
                                                <option></option>
                                                <option value="<?php echo $row['id']; ?>"><?php
                                                    echo $row['name']; ?></option> <?php } ?>
                                    </select>                                         
                            </div> 
                            <div class="col-md-3">
                                <label >Item Reorder level    </label>                          
                                <input type="text" class="form-control" name="reorderlevel" required="" autocomplete="off" >
                            </div>  
                            
                               
                            <div class="col-sm-3"> 
                                <label > Warehouse stored  </label>  
                                    <select class="form-control js-example-basic-single  "  required="" name="warehouse" style="width:100%" >
                                        <?php $stmt = $conn->prepare("SELECT * FROM invwarehouse  ");
                                        $stmt->execute(); foreach ($stmt as $row) {?>
                                                <option></option>
                                                <option value="<?php echo $row['id']; ?>"><?php
                                                    echo $row['name']; ?></option> <?php } ?>
                                    </select>                                         
                            </div> 
                                                              
                            <div class="col-sm-2"> 
                                <label > Item Status   </label>  
                                    <select class="form-control   "  required="" name="status" style="width:100%" >
                                        <?php $stmt = $conn->prepare("SELECT * FROM pitemactive  ");
                                        $stmt->execute(); foreach ($stmt as $row) {?>
                                                
                                                <option value="<?php echo $row['id']; ?>"><?php
                                                    echo $row['name']; ?></option> <?php } ?>
                                    </select>                                         
                            </div>  
                           
                            <div class="col-md-3">
                                <label >Selling Price   </label>                          
                                <input type="text" class="form-control" name="cost" required="" autocomplete="off" >
                            </div> 
                                                               
                                                                                                          
                        </div>                           
                      
                       
                        
                        
                        <!-- submit button -->
                        <div class="form-group">                              
                            <div class="col-sm-offset-4 col-sm-10" style="padding:0.5rem;">
                                    <button type="submit"  name="submit" class="btn btn-default col-md-3">  <i class="fa fa-cubes"></i>  Add Items   </button> </div>
                        </div> 
                    </form>               
                </div>
            </div>
        </div>
                                
    </section>

</div>
  <!-- /.content-wrapper -->
  
  <!-- Main Footer -->
  <footer class="main-footer">
    <?php  include_once('inc/footer.php'); ?>
  </footer>
  
</div>
                         
<!-- ./wrapper -->

<!-- REQUIRED JS SCRIPTS -->
<?php include_once('inc/linkjs.php') ?>

</body>
</html>