<?php

  session_start();
  include('includes/connection.php');
  include('includes/checklogin.php');
  check_login();
  $userid = $_SESSION['loginid'];

// Only Use Update Process
  $db_editstatus = 0;
  $db_salesreturnidno = 0;
  $db_salesreturndate = "";
  $db_salesreturnproductname = "";
  $db_salesreturnproduct = "";
  $db_salesreturnmodel = "";
  $db_salesreturnquantity = "";
  $db_salesreturnamount = "";
  $db_salesreturnstock = "";
  $db_salesreturncostprice = "";
  $db_salesreturndescription = "";
  $db_streetaddress1 = "";
  $db_streetaddress2 = "";
  $db_city = "";
  $db_states = "";
  $db_country = "";
// Only Use Update Process

// SAVE SALES RETURN REGISTRATION
if (isset($_POST['add_new_sales_return'])) 
{
    $idno = 0;
    $salesreturndate = $_POST['salesreturn_date'];
    $salesreturnproductname = $_POST['name'];
    $salesreturnproduct = $_POST['salesreturn_product'];
    $salesreturnmodel = $_POST['salesreturn_model'];
    $salesreturnquantity = $_POST['salesreturn_quantity'];
    $salesreturnamount = $_POST['salesreturn_amount'];
    $salesreturnstock = $_POST['salesreturn_stock'];
    $salesreturncostprice = $_POST['salesreturn_costprice'];
    $salesreturndescription = $_POST['salesreturn_description'];
    $streetaddress1 = $_POST['street_address_1'];
    $streetaddress2 = $_POST['street_address_2'];
    $city = $_POST['city'];
    $states = $_POST['states'];
    $country = $_POST['country'];

// INSERT QUERY 
    $insert = "INSERT INTO `trn_salesreturn` (`salesreturn_date`, `salesreturn_productname`, `salesreturn_product`, `salesreturn_model`, `salesreturn_quantity`, `salesreturn_amount`, `salesreturn_stock`, `salesreturn_costprice`, `salesreturn_description`, `street_address1`, `street_address2`, `city` , `state`, `country`) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    $stmt = $mysqli->prepare($insert);
    $rc = $stmt->bind_param('ssssssssssssss', $salesreturndate, $salesreturnproductname, $salesreturnproduct, $salesreturnmodel, $salesreturnquantity, $salesreturnamount, $salesreturnstock, $salesreturncostprice, $salesreturndescription, $streetaddress1, $streetaddress2, $city, $states, $country);
    $stmt->execute();
    if ($stmt) 
    {
       $success = "Sales Return Created Successfully";
    } 
    else 
    {
       $err = "Please Try Again Or Try Later";
    }
// INSERT QUERY 

GOTOLOOP1:         

}
// SAVE SALES RETURN REGISTRATION

// MOVING SALES RETURN REGISTRATION
if (isset($_GET['edit_salesreturn_idno'])) 
{
    $db_editstatus = 1;
    $db_salesreturnidno = intval($_GET['edit_salesreturn_idno']); 
    $db_salesreturndate = "";
    $db_salesreturnproductname = "";
    $db_salesreturnproduct = "";
    $db_salesreturnmodel = "";
    $db_salesreturnquantity = "";
    $db_salesreturnamount = "";
    $db_salesreturnstock = "";
    $db_salesreturncostprice = "";
    $db_salesreturndescription = "";
    $db_streetaddress1 = "";
    $db_streetaddress2 = "";
    $db_city = "";
    $db_states = "";
    $db_country = "";

    $select = "SELECT * FROM `trn_salesreturn` WHERE salesreturn_idno = ?";
    $stmt = $mysqli->prepare($select);
    $stmt->bind_param('i', $db_salesreturnidno);
    $stmt->execute();
    $result = $stmt->get_result();
    while ($row = mysqli_fetch_array($result)) 
    {
        $db_salesreturndate = $row['salesreturn_date'];
        $db_salesreturnproductname = $row['salesreturn_productname'];
        $db_salesreturnproduct = $row['salesreturn_product'];
        $db_salesreturnmodel = $row['salesreturn_model'];
        $db_salesreturnquantity = $row['salesreturn_quantity'];
        $db_salesreturnamount = $row['salesreturn_amount'];
        $db_salesreturnstock = $row['salesreturn_stock'];
        $db_salesreturncostprice = $row['salesreturn_costprice'];
        $db_salesreturndescription = $row['salesreturn_description'];
        $db_streetaddress1 = $row['street_address1'];
        $db_streetaddress2 = $row['street_address2'];
        $db_city = $row['city'];
        $db_states = $row['state'];
        $db_country = $row['country'];
    }
}
// MOVING SALES RETURN REGISTRATION

// EDITING - UPDATING SALES RETURN REGISTRATION
if (isset($_POST['edit_old_sales_return'])) 
{
    $db_editstatus = 0;
    $db_salesreturndate = $_POST['salesreturn_date'];
    $db_salesreturnproductname = $_POST['name'];
    $db_salesreturnproduct = $_POST['salesreturn_product'];
    $db_salesreturnmodel = $_POST['salesreturn_model'];
    $db_salesreturnquantity = $_POST['salesreturn_quantity'];
    $db_salesreturnamount = $_POST['salesreturn_amount'];
    $db_salesreturnstock = $_POST['salesreturn_stock'];
    $db_salesreturncostprice = $_POST['salesreturn_costprice'];
    $db_salesreturndescription = $_POST['salesreturn_description'];
    $db_streetaddress1 = $_POST['street_address_1'];
    $db_streetaddress2 = $_POST['street_address_2'];
    $db_city = $_POST['city'];
    $db_states = $_POST['states'];
    $db_country = $_POST['country'];

    $update = "UPDATE `trn_salesreturn` SET salesreturn_date=?, salesreturn_productname=?, salesreturn_product=?, salesreturn_model=?, salesreturn_quantity=?, salesreturn_amount=?, salesreturn_stock=?, salesreturn_costprice=?, salesreturn_description=?, street_address1=?, street_address2=?, city=?, state=?, country=? WHERE salesreturn_idno=?";
    $stmt = $mysqli->prepare($update);
    $rc = $stmt->bind_param('ssssssssssssssi', $db_salesreturndate, $db_salesreturnproductname, $db_salesreturnproduct, $db_salesreturnmodel, $db_salesreturnquantity, $db_salesreturnamount, $db_salesreturnstock, $db_salesreturncostprice, $db_salesreturndescription, $db_streetaddress1, $db_streetaddress2, $db_city, $db_states, $db_country, $db_salesreturnidno);
    $stmt->execute();
    if ($stmt) 
    {
       $success = "Sales Return Updated Successfully";
    } 
    else 
    {
       $err = "Please Try Again Or Try Later";
    }

GOTOLOOP2:

}
// EDITING - UPDATING SALES RETURN REGISTRATION

?>


<!DOCTYPE html>
<html>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<?php include("includes/head.php"); ?>

<style>
    .select2-selection 
    {
        height: 34px !important;
        border-color: #ced4da !important;
    }

    .panel 
    {
        padding: 30px;
        margin-bottom: 20px;
        background-color: #ffffff;
        background-color: #F5F5F5; 
        border: 1px; 
        border-radius: 4px;
        -webkit-box-shadow: 0 1px 1px rgba(0, 0, 0, 0.05);
        box-shadow: 0 1px 1px rgba(0, 0, 0, 0.05);
    }

    .panel-heading 
    {
        padding: 10px 15px;
        margin: -15px -15px 15px;
        font-size: 17.5px;
        font-weight: 500;
        background-color: #E6E6E6;
        background-color:#f5f5f5;

        border: 1px solid #dddddd;
        border-top-right-radius: 3px;
        border-top-left-radius: 3px;
    }

    .panel-footer 
    {
        padding: 10px 15px;
        margin: 15px -15px -15px;
        background-color: #f5f5f5;
        border-top: 1px solid #dddddd;
        border-bottom-right-radius: 3px;
        border-bottom-left-radius: 3px;
    }

    .panel-primary 
    {
        border-color: #428bca;
    }

    .panel-primary .panel-heading 
    {
        color: #ffffff;
        background-color: #428bca;
        border-color: #428bca;
    }
</style>


<body class="hold-transition sidebar-mini layout-fixed layout-navbar-fixed">
    <div class="wrapper">
        <!-- Navbar -->
        <?php include("includes/nav.php"); ?>
        <!-- /.navbar -->

        <!-- Main Sidebar Container -->
        <?php include("includes/sidebar.php"); ?>

        <!-- Content Wrapper. Contains page content -->
        <div class="content-wrapper">
            <!-- Content Header (Page header) -->
            <section class="content-header">
                <div class="container-fluid">
                    <div class="row mb-2">
                        <div class="col-sm-6">
                            <h1>Sales Return</h1>
                        </div>
                        <div class="col-sm-6">
                            <ol class="breadcrumb float-sm-right">
                                <li class="breadcrumb-item"><a href="dashboard.php">Dashboard</a></li>
                                <li class="breadcrumb-item"><a href="sales_return_manage.php">Manage Sales Return</a></li>
                                <li class="breadcrumb-item active">Add</li>
                            </ol>
                        </div>
                    </div>
                </div><!-- /.container-fluid -->
            </section>

            <!-- Main content -->
            <section class="content">
                <div class="container-fluid">
                    <div class="row">
                        <!-- left column -->
                        <div class="col-md-12">
                            <!-- general form elements -->
                            <div class="card card-purple">
                                <div class="card-header">
                                    <h3 class="card-title">Fill out all the required fields</h3>
                                </div>
                                <!-- form start -->

                                <!-- <div class="col-md-12"> -->

                                <form method="post" enctype="multipart/form-data" role="form" action="#">
                                    <div class="card-body">

                                        <!-- ///////////////////////////////////////////////////////////////////// -->
                                        <!-- START - Sales Return -->

                                        <h3><i class="nav-icon fas fa-address-book"></i>&nbsp;&nbsp;Sales Return</h3>
                                        <div class="panel panel-default">
                                            <div class="panel-body color-white border-orange form-group form-group-sm">

                                                <div class="row">     

                                                    <div class="col-md-6 form-group">
                                                       <label for="salesreturn_date">Sales Return Date</label>
                                                       <input type="date" name="salesreturn_date" class="form-control" id="salesreturn_date" value="<?php echo $db_salesreturndate ?>">
                                                    </div>
                                                    
                                                    <div class="col-md-6 form-group">
                                                        <label for="name">Sales Return Product Name</label>
                                                        <select name="name" id="name" class="form-control">
                                                            <option>Select Return Product Name</option>
                                                            <?php 
                                                               $sql = "SELECT name FROM trn_sales ORDER BY name";
                                                               $result = $mysqli->query($sql);
                                                               while ($row = mysqli_fetch_array($result)) 
                                                               {
                                                            ?>
                                                                <option value="<?php echo $row['name']; ?>" <?php echo $db_salesreturnproductname == $row['name'] ? "SELECTED" : '' ?>><?php echo $row['name']; ?></option>
                                                            <?php  
                                                               }
                                                            ?>
                                                        </select>
                                                    </div>

                                                    <div class="col-md-6 form-group">
                                                        <label for="salesreturn_product">Sales Return Product</label>
                                                        <select name="salesreturn_product" id="salesreturn_product" class="form-control">
                                                            <option>Select Sales Return Product</option>
                                                            <?php 
                                                               $sql = "SELECT purchase_product FROM trn_sales ORDER BY purchase_product";
                                                               $result = $mysqli->query($sql);
                                                               while ($row = mysqli_fetch_array($result)) 
                                                               {
                                                            ?>
                                                                <option value="<?php echo $row['purchase_product']; ?>" <?php echo $db_salesreturnproduct == $row['purchase_product'] ? "SELECTED" : '' ?>><?php echo $row['purchase_product']; ?></option>
                                                            <?php  
                                                               }
                                                            ?>
                                                        </select>
                                                    </div>

                                                    <div class="col-md-6 form-group">
                                                        <label for="salesreturn_model">Sales Return Model</label>
                                                        <select name="salesreturn_model" id="salesreturn_model" class="form-control">
                                                            <option>Select Purchase Model</option>
                                                            <?php 
                                                               $sql = "SELECT purchase_model FROM trn_sales ORDER BY purchase_model";
                                                               $result = $mysqli->query($sql);
                                                               while ($row = mysqli_fetch_array($result)) 
                                                               {
                                                            ?>
                                                                <option value="<?php echo $row['purchase_model']; ?>" <?php echo $db_salesreturnmodel == $row['purchase_model'] ? "SELECTED" : '' ?>><?php echo $row['purchase_model']; ?></option>
                                                            <?php  
                                                               }
                                                            ?>
                                                        </select>
                                                    </div>

                                                    <div class="col-md-6 form-group">
                                                        <label for="salesreturn_quantity">Sales Return Quantity</label>
                                                        <input type="text" name="salesreturn_quantity" class="form-control" id="salesreturn_quantity"  value="<?php echo $db_salesreturnquantity ?>">
                                                        <input type="hidden" name="salesreturn_idno" class="form-control" id="salesreturn_idno" >
                                                    </div>

                                                    <div class="col-md-6 form-group">
                                                        <label for="salesreturn_amount">Sales Return Amount</label>
                                                        <input type="text" name="salesreturn_amount" class="form-control" id="salesreturn_amount" value="<?php echo $db_salesreturnamount ?>">
                                                    </div>

                                                    <div class="col-md-6 form-group">
                                                        <label for="salesreturn_stock">Sales Return Stock</label>
                                                        <input type="text" name="salesreturn_stock" class="form-control" id="salesreturn_stock" value="<?php echo $db_salesreturnstock ?>">
                                                    </div>

                                                    <div class="col-md-6 form-group">
                                                        <label for="salesreturn_costprice">Sales Return CostPrice</label>
                                                        <input type="text" name="salesreturn_costprice" class="form-control" id="salesreturn_costprice" value="<?php echo $db_salesreturncostprice ?>">
                                                    </div>                                                

                                                    <div class="col-md-6 form-group">
                                                        <label for="street_address_1">Street Address 1</label>
                                                        <input type="text" name="street_address_1" class="form-control" id="street_address_1" value="<?php echo $db_streetaddress1 ?>">
                                                    </div>

                                                    <div class="col-md-6 form-group">
                                                        <label for="street_address_2">Street Address 2</label>
                                                        <input type="text" name="street_address_2" class="form-control" id="street_address_2" value="<?php echo $db_streetaddress2 ?>">
                                                    </div>

                                                    <div class="col-md-6 form-group">
                                                        <label for="city">City</label>
                                                        <input type="text" name="city" class="form-control" id="city" value="<?php echo $db_city ?>">
                                                    </div>
                                                    
                                                    <div class="col-md-6 form-group">
                                                        <label for="states">State</label>
                                                        <select name="states" id="states" class="form-control">
                                                            <option>Select State</option>
                                                            <?php 
                                                               $sql = "SELECT state_name FROM states ORDER BY state_name";
                                                               $result = $mysqli->query($sql);
                                                               while ($row = mysqli_fetch_array($result)) 
                                                               {
                                                            ?>
                                                                <option value="<?php echo $row['state_name']; ?>" <?php echo $db_states == $row['state_name'] ? "SELECTED" : '' ?>><?php echo $row['state_name']; ?></option>
                                                            <?php  
                                                               }
                                                            ?>
                                                        </select>
                                                    </div>

                                                    <div class=" col-md-6 form-group">
                                                        <label for="country">Country </label>
                                                        <select name="country" id="country" class="form-control">
                                                            <option>Select Country</option>
                                                            <?php 
                                                               $sql = "SELECT country_name FROM countries ORDER BY country_name";
                                                               $result = $mysqli->query($sql);
                                                               while ($row = mysqli_fetch_array($result)) 
                                                               {
                                                            ?>
                                                                <option value="<?php echo $row['country_name']; ?>" <?php echo $db_country == $row['country_name'] ? "SELECTED" : '' ?>><?php echo $row['country_name']; ?></option>
                                                            <?php  
                                                               }
                                                            ?>
                                                        </select>
                                                    </div>

                                                    <div class="col-md-6 form-group">
                                                        <label for="salesreturn_description">Sales Return Description</label>
                                                        <textarea type="text" name="salesreturn_description" class="form-control" id="salesreturn_description"><?php echo $db_salesreturndescription ?></textarea>
                                                    </div>
                                              
                                                  </div>
                                               </div>
                                            </div> 
                                            
                                            
                                    <div class="card-footer"> 
                                        <?php echo isset($db_editstatus) && $db_editstatus == 1 ?  '<button type="submit" name="edit_old_sales_return" class="btn btn-success">EDIT</button>' :  '<button type="submit" name="add_new_sales_return" class="btn btn-success">SAVE</button>' ?>
                                        <input type="reset" name="cancel" Value="CANCEL" class="btn btn-danger">
                                        <a href="sales_return_manage.php" class="btn btn-warning">BACK</a>
                                    </div>
                                </form>
                            </div>
                            <!-- /.card -->
                        </div><!-- /.container-fluid -->
            </section> 
            <!-- /.content -->
        </div>
        
        <aside class="control-sidebar control-sidebar-dark">  </aside>
        <!-- /.control-sidebar -->

    </div>
    <!-- ./wrapper -->

    <!-- jQuery -->
    <script src="plugins/jquery/jquery.min.js"></script>
    <!-- Bootstrap 4 -->
    <script src="plugins/bootstrap/js/bootstrap.bundle.min.js"></script>
    <!-- bs-custom-file-input -->
    <script src="plugins/bs-custom-file-input/bs-custom-file-input.min.js"></script>
    <!-- AdminLTE App -->
    <script src="dist/js/adminlte.min.js"></script>
    <!-- AdminLTE for demo purposes -->
    <script src="dist/js/demo.js"></script>

</body>

</html>