<?php
$this->load->view('master/common/header.php', $data);
$base_name = $data['base_name'];


if(isset($_POST['dboy']))
{
 
   
   $datas=['db_id'=>$_POST['db_id']];
   $this->db->where('payment_id',$_POST['payment_id']);
   $this->db->update('tbl_payment',$datas);
}
?>

 <div class="content">
        
        <!-- Start Content-->
        <div class="container-fluid">
            
            <!-- start page title -->
            <div class="row">
                <div class="col-12">
                    <div class="page-title-box">
                        <div class="page-title-right">
                            <ol class="breadcrumb m-0">
                                <li class="breadcrumb-item"><a href="javascript: void(0);"><?= $base_name; ?></a></li>
                                <li class="breadcrumb-item active text-capitalize"><?= $page; ?></li>
                            </ol>
                        </div>
                        <h4 class="page-title text-uppercase"><?= $page; ?></h4>
                    </div>
                </div>
            </div>    
            <!-- end page title --> 

            <div class="row">
                <div class="col-md-12 col-xl-12">
                    <div class="card-box">
                        <div class="row">
                            <div class="col-md-12 col-12 mb-2">
                               <!-- <form class="form-inline float-left" action='' method="post">
                                    <div class="form-group mr-2">
                                        <select  class="form-control" name="shipping_status" required>
                                            <option value="">Shipping Status</option>
                                            <option value="Pending">Pending Order</option>
                                            <option value="Arrive">Out for delivery</option>
                                            <option value="Delivered">Delivered</option>
                                            <option value="Cancelled">Cancelled</option>
                                        </select>
                                    </div>
                                    <button type="submit" name="serch2" class="btn btn-dark">GO</button>
                                </form>-->
                                <form class="form-inline float-right" action='' method="post">
                                    <div class="form-group mr-2">
                                        <input type="date" class="form-control" name="to" id="exampleInputName2" placeholder="Jane Doe">
                                    </div>
                                    <div class="form-group mr-2">
                                        <label for="exampleInputEmail2" class="mr-2">TO</label>
                                        <input type="date" class="form-control" name="from" id="exampleInputEmail2" placeholder="jane.doe@example.com">
                                    </div>
                                <button type="submit" name="serch" class="btn btn-dark">GO</button>
                                </form>
                            </div>
                            <div class="col-sm-12 table-responsive">
                                <table id="orderdata" class="table table-bordered display nowrap" style="width:100%">
                                    <thead>
                                        <tr>
                                            <th>S. No.</th>
                                            <th>Order id</th>
                                            <th>Customer Details</th>
                                            <th>Product Details</th>
                                            <th>Payment Information</th>
                                            <th>Paid Amount</th>
                                            <th>Payment Status</th>
                                            <th>Shipping Status</th>
                                            <th>Action</th>
                                        </tr>
                                    </thead>
                                    
                                    <tbody>
                                        
<?php
$i = 0;
$db_id=$_SESSION['admin']['phone'];
if (isset($_POST['serch2'])) {
    $statement = $this->db->query("SELECT * FROM tbl_payment WHERE shipping_status=?,db_id=? ORDER by id DESC", array(
        $_POST['shipping_status'],$_SESSION['admin']['phone']
    ));
} else if (isset($_POST['serch'])) {
    $date1 = $_POST['to'];
    $date  = date_create($_POST['from']);
    date_add($date, date_interval_create_from_date_string("1 days"));
    $date2 = date_format($date, "Y/m/d");
    
    $statement = $this->db->query("SELECT * FROM tbl_payment where db_id='$db_id' AND shipping_status='Arrive' AND (payment_date BETWEEN '$date1' AND '$date2') ORDER by id DESC");
} else {
    $statement = $this->db->query("SELECT * FROM tbl_payment where db_id='$db_id' AND shipping_status='Arrive' ORDER by id DESC");
}

$result = $statement->result_array();
foreach ($result as $row) {
    $i++;
    
      $db_id= $row['db_id'];
?>
                   <tr class="<?php
    if ($row['payment_status'] == 'Pending') {
        echo 'bg-soft-warning';
    } else {
        echo 'bg-soft-success';
    }
?>">
                        <td><?= $i; ?></td>
                        <td><?= $row['payment_id']; ?></td>
                        <td>
                            <b>Id:</b> <?= $row['customer_id']; ?><br>
                            <b>Name:</b> <?= $row['customer_name']; ?><br>
                            <b>Email:</b> <?= $row['customer_email']; ?><br><br>
                         
                        
                            
                            
                            

                            
                            
                        </td>
                        <td>
                           <?php
    $qty              = 0;
    $qty_X_unit_price = 0;
    $qty_X_pv = 0;
    $shipping_cost    = 0;
    $shipping_cost2   = 0;
    $aXXX             = 0;
    $unit_price       = 0;
    $tt               = 0;
    $ss               = 0;
    $percentage       = 0;
    $discounts_tt     = 0;
    $orders_tt        = 0;
    $shipping_cost_tt = 0;
    $coupon_id        = 0;
    $cc               = 0;
    
    
    $statement1 = $this->db->query("SELECT * FROM tbl_order WHERE payment_id=?", array(
        $row['payment_id']
    ));
    $result1    = $statement1->result_array();
    
        echo '<div class="p-2 bg-white overflow-auto" style="height:120px;">';
    foreach ($result1 as $row1) {
        echo '<b>Product Name:</b> ' . $row1['product_name'];
        echo '<br>(<b>Size:</b> ' . $row1['size'];
        // echo ', <b>Color:</b> ' . $row1['color'] . ')';
        echo ', <b>Quantity:</b> ';
        echo $qty = $row1['quantity'];
        echo ', <b>Unit Price:</b> ';
        echo $unit_price = $row1['unit_price'];
        echo ')';
        echo '<hr class="mt-1 mb-1">';
        
        $qty_X_unit_price += $qty * $unit_price;
        $qty_X_pv += $qty * $row1['pv'];
        
    }
    
    
    echo ' <b>Sub Total:</b>  &#8377; ';
    echo number_format($qty_X_unit_price, 2);
    echo '<br>';
    
    $statement5 = $this->db->query("SELECT * FROM applied_shipping_cost WHERE payment_id=?", array(
        $row['payment_id']
    ));
    $result5    = $statement5->result_array();
    foreach ($result5 as $row5) {
        $shipping_cost = $row5['shipping_cost'];
    }
    if ($shipping_cost) {
        $shipping_cost2 = '&#8377;' . ' ' . number_format($shipping_cost, 2);
        
        $qty_X_unit_price_shipping_cost = $qty_X_unit_price + $shipping_cost;
        $aXXX                           = $qty_X_unit_price_shipping_cost;
    } else {
        $shipping_cost2 = 'FREE';
        $aXXX           = $qty_X_unit_price;
    }
    
    echo ' <b>Shipping Cost :</b> ' . $shipping_cost2 . '<br>';
    
    $statement5 = $this->db->query("SELECT * FROM applied_coupon WHERE payment_id=?", array(
        $row['payment_id']
    ));
    $result5    = $statement5->result_array();
    foreach ($result5 as $row5) {
        $coupon_id = $row5['coupon_id'];
    }
    
    $statement5 = $this->db->query("SELECT * FROM tbl_coupon WHERE coupon_id=?", array(
        $coupon_id
    ));
    $result5    = $statement5->result_array();
    foreach ($result5 as $row5) {
        $percentage = $row5['percentage'];
    }
    if (isset($percentage)) {
        $percentage3 = number_format($percentage, 2);
        $percentage2 = $ss / 100;
        $percentage4 = number_format($percentage2, 2);
        
        $ff = $percentage3 * $percentage4;
        echo ' <b>Discount :</b> &#8377; ';
        echo $cc = number_format($ff, 2);
        echo '<br>';
        $aXXX = $aXXX - $ff;
    }
    
    
    echo ' <b>Self Benift :</b>  &#8377; ';
    echo number_format($qty_X_pv, 2) . '<br>';
    echo ' <b>Final Total :</b>  &#8377; ';
    echo number_format($aXXX-$qty_X_pv, 2) . '<br>';
    
        echo '</div>';
    
    
    
    
    
    
    $tt += $aXXX;
    $discounts_tt += $cc;
    $orders_tt += $qty_X_unit_price;
    $shipping_cost_tt += $shipping_cost;
    
?>
                       </td>
                        <td>
                            <?php
    if ($row['payment_method'] == 'PayPal'):
?>
                               <b>Payment Method:</b> <?= '<span class="text-danger"><b>' . $row['payment_method'] . '</b></span>'; ?><br>
                                <b>Payment Id:</b> <?= $row['payment_id']; ?><br>
                                <b>Date:</b> <?= date('d-m-Y', strtotime($row['payment_date'])); ?><br>
                                <b>Transaction Id:</b> <?= $row['txnid']; ?><br>
                            
                            
                            <?php
    elseif ($row['payment_method'] == 'Stripe'):
?>
                               <b>Payment Method:</b> <?= '<span class="text-danger"><b>' . $row['payment_method'] . '</b></span>'; ?><br>
                                <b>Payment Id:</b> <?= $row['payment_id']; ?><br>
                                <b>Date:</b> <?= date('d-m-Y', strtotime($row['payment_date'])); ?><br>
                                <b>Transaction Id:</b> <?= $row['txnid']; ?><br>
                                <b>Card Number:</b> <?= $row['card_number']; ?><br>
                                <b>Card CVV:</b> <?= $row['card_cvv']; ?><br>
                                <b>Expire Month:</b> <?= $row['card_month']; ?><br>
                                <b>Expire Year:</b> <?= $row['card_year']; ?><br>
                        
                        
                            <?php
    elseif ($row['payment_method'] == 'COD'):
?>
                               <b>Not Payment Yet <br>Order by:</b> <?= '<span class="text-danger"><b>' . $row['payment_method'] . '</b></span>'; ?><br>
                                <b>Order ID:</b> <?= $row['payment_id']; ?><br>
                                <b>Date:</b> <?= date('d-m-Y h:i:s A', strtotime($row['payment_date'])); ?><br>
                        
                            <?php
    endif;
?>
                       </td>
                        <td><?php
    
    if ($row['payment_method'] == 'COD') {
        echo '0';
    } else {
        echo $row['paid_amount'];
    }
?>
                       </td>
                        <td>
                            <?php
    if ($row['payment_method'] == 'COD') {
        
        echo 'No Payment<br>Booking By COD';
    } else
        echo $row['payment_status'];
    
?>
                           
                          
                            <br><br>
                            <?php
    if ($row['payment_status'] == 'Pending') {
?>
                                   <a href="<?=base_url('master/order/order_change_status');?>?id=<?= $row['id']; ?>&task=Completed&payment_id=<?= $row['payment_id']; ?>" class="btn btn-warning btn-xs">Make Completed</a>
                                    <?php
    }
    
    
   ?>
   
   








                       </td>
                        <td>
                            <?= $row['shipping_status']; ?>
                           <br><br>
                            <?php
    if ($row['payment_status'] == 'Completed') {
        if ($row['shipping_status'] == 'Pending') {
?>
                                   <a href="<?=base_url('master/order/shipping_change_status');?>?id=<?= $row['id']; ?>&task=Arrive&cust_id=<?= $row['customer_id']; ?>&payment_id=<?= $row['payment_id']; ?>" class="btn btn-warning btn-xs">Make Shipped</a>
                                    <a href="<?=base_url('master/order/shipping_change_status');?>?id=<?= $row['id']; ?>&task=Cancelled&cust_id=<?= $row['customer_id']; ?>&payment_id=<?= $row['payment_id']; ?>" class="btn btn-danger btn-xs">Cancle Order</a>
                                    <?php
        }
        
        elseif ($row['shipping_status'] == 'Completed' && $row['delivery_date'] != '') {
?>
                                   <b class="btn btn-xs btn-success"> Completed</b>
                                    <?php
        } elseif ($row['shipping_status'] == 'Cancelled' && $row['cancelled_date'] != '') {
?>
                                   <b class="btn btn-xs btn-danger"> Cancelled</b>
                                    <?php
        } elseif ($row['shipping_status'] == 'Returned' && $row['cancelled_date'] != '') {
?>
                                   <b class="btn btn-xs btn-danger"> Returned</b>
                                    <?php
        } elseif ($row['shipping_status'] == 'Arrive' && $row['shipping_date'] != '') {
?>
 <a href="#" data-toggle="modal" data-target="#models-<?= $i; ?>"class="btn btn-warning btn-xs">Make Completed</a><br>
                                    <!--<a href="<?=base_url('master/order/shipping_change_status');?>?id=<?= $row['id']; ?>&task=Completed&cust_id=<?= $row['customer_id']; ?>&payment_id=<?= $row['payment_id']; ?>" class="btn btn-warning btn-xs">Make Completed</a>-->
                                     <?php
        }
    }
?>



 <div id="models-<?= $i; ?>" class="modal fade" role="dialog">
                                <div class="modal-dialog">
                                    <div class="modal-content">
                                        <form action="<?=base_url('master/order/shipping_change_status1');?>?id=<?= $row['id']; ?>&task=Completed&cust_id=<?= $row['customer_id']; ?>&payment_id=<?= $row['payment_id']; ?>" method="GET">
                                        <div class="modal-body" style="font-size: 14px">
                                       
                                          
                                            
                                         
                                            
                                                
                                                  
                                                    <input type="text" name="db_id" placeholder="Delevery Code No." value="<?php echo $row->db_id;?>" class="form-control" >
                                                
                                          
                                        </div>
                                        <div class="modal-footer">
                                            <button type="button" class="btn btn-dark" data-dismiss="modal">Close</button> 
                                            <button type="submit" class="btn btn-success" name="dboy" >Make Completed</button>
                                        </div>
                                          </form>
                                    </div>
                                </div>
                            </div>
                       </td>
                        <td>
                            <!--<a href="#" class="btn btn-danger btn-xs" data-href="order-delete.php?id=<?= $row['id']; ?>" data-toggle="modal" data-target="#confirm-delete">Delete</a>-->
                             <a href="<?=base_url('master/order/bill_print');?>?id=<?= $row['payment_id']; ?>" class="btn btn-success btn-xs">Bill Print</a>
                             <a href="<?=base_url('master/order/bill_pdf');?>?id=<?= $row['payment_id']; ?>" class="btn btn-warning btn-xs">PDF</a>
                             <a href="<?=base_url('master/order/mini_bill_print');?>?id=<?= $row['payment_id']; ?>" class="btn btn-outline-success btn-xs">Mini Bill Print</a>
                        </td>
                    </tr>
                    <?php
}
?>
                                   </tbody>
                            </table>
                            </div>
                        </div>
                    </div>
                    <div class="card-box">
                        <table class="table table-bordered text-right text-bold bg-gray">
                            <thead class="text-center">
                                <tr>
                                    <th> Total Order Amount</th>
                                    <th>Total Shipping Costs</th>
                                    <th>Total Discounts</th>
                                    <th>Sub Total </th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr>
                                    <td><?= (isset($orders_tt)) ? '&#8377;'.number_format($orders_tt,2) : 'NAN'; ?></td>
                                    <td><?= (isset($shipping_cost_tt)) ? '&#8377;'.number_format($shipping_cost_tt,2) : 'NAN' ;?></td>
                                    <td><?= (isset($discounts_tt)) ? '&#8377;'.number_format($discounts_tt,2) : 'NAN' ;?></td>
                                    <td><?= (isset($tt)) ? '&#8377;'.number_format($tt,2) : 'NAN' ;?></td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
                <!-- /.col-md-6 -->
            </div>
            <!-- /.row -->
        </div>
        <!-- /.container-fluid -->
    </div>
    <!-- /.content -->
</div>
<!-- /.content-wrapper -->
<?php
$this->load->view('master/common/footer.php');
?>
<script type="text/javascript">
    function deletepayment() {
        if (confirm("Are You Sure You Want TO Delete payment ?")) {
            return true;
        }
        return false;
    }
</script>
<script>
    $(document).ready(function() {
        $('#orderdata').DataTable();
    });
</script>