<?php include("includes/db_config.php"); ?>
<!DOCTYPE html>
<html lang="en" dir="ltr" data-theme="light">
<meta http-equiv="content-type" content="text/html;charset=utf-8" />

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <?php include("includes/css.php");?>
    <style>
        .modal-body .form-label{font-weight:600;}.viewptdt .form-label{font-weight:600;}.viewptdt label{margin-top:10px;}
    </style>
</head>

<body class="preload-active aside-active aside-mobile-minimized aside-desktop-maximized">
    <div class="holder">
        <?php include("includes/sidebar.php");?>
        <div class="wrapper">

            <?php include("includes/header.php");?>
            <div class="content">
                <div class="container-fluid g-5">                    
                    <div class="row">
                        <div class="col-12">
                            <div class="portlet">
                                <div class="portlet-header portlet-header-bordered">
                                     <div class="col-md-9">
                                        <h3 class="portlet-title">All Sale List</h3>
                                    </div>
                                    <div class="col-md-3">
                                        <center><a href="add-sale-detail.php"><button class="btn btn-success btn-md"><i class="fa fa-plus"></i> Add New Sales Details</button></a></center>
                                    </div>
                                 </div>
                                <div class="portlet-body table-responsive">
                                    <table id="datatable-1" class="table table-bordered table-striped table-hover">
                                        <thead>
                                            <tr>
                                                <th>#</th>
                                                <th>Invoice No</th>
                                                <th>Invoice Date</th>
                                                <th>Party Name</th>
                                                <th>Received Amount</th>
                                                 <th style="text-center">Total</th>
                                                <th style="text-center">Balance</th>
                                                <th>
                                                    <center>Action</center>
                                                </th>
                                            </tr>
                                        </thead>
                                        <tbody> <?php $i=1; 
                                            $sql="select * from tbl_ah_items where type ='Sale' order by id DESC"; 
                                            $result = mysqli_query($conn, $sql); 
                                            while($res= mysqli_fetch_array($result)){
                                            $cid=$res['customer_id'];
                                            $sqlpt=mysqli_query($conn,"SELECT * from ah_party where id='".$cid."'");
                                            $restpy=mysqli_fetch_array($sqlpt);
                                            $query = mysqli_query($conn, "SELECT SUM(total_amt) as ttl FROM tbl_ah_items WHERE customer_id = '".$cid."' AND type ='Sale'");
                                            $rows = mysqli_fetch_array($query); 
                                            $sql_query = mysqli_query($conn,"SELECT SUM(received_amonut) as rttl FROM ah_sales_payment_in WHERE customer_id = '".$cid."'");
                                              $row = mysqli_fetch_array($sql_query);
                                              if(!empty($row['rttl'])){ 
                                              $data= "Bal: ". $rows['ttl'] - $row['rttl'];
                                               }else{
                                              $data= "Bal: ". $rows['ttl']; } ?>
                                            <tr>
                                                <td><?= $i;?></td>
                                                <td><?= $res['bill_no'];?></td>
                                                <td><?= $res['bill_date'];?></td>
                                                <td><?= $restpy['party_name'];?></td>
                                                <td><?= $res['received_amount'];?></td>
                                                <td><?php echo $res['balance'];?></td>
                                                <td><?= $res['total_amt'];?></td>
                                                <td>
                                                    <center>
                                                         <a href="invoice-sale-detail.php?id=<?= $res['id']; ?>"><button class="btn btn-success"> <i class="fa fa-print"></i></button></a>
                                                         <a href="add-payment-due.php?id=<?= $res['id']; ?>"><button class="btn btn-success"> <i class="fa fa-inr"></i></button></a>
                                                         <a href="view-sale-detail.php?id=<?= $res['id']; ?>"><button class="btn btn-success"> <i class="far fa-eye"></i></button></a>
                                                        <a href="edit-sale-detail.php?id=<?= $res['id']; ?>"><button class="btn btn-primary"> <i class="far fa-edit"></i></button></a>
                                                        <a href="javascript:delete_ptype_by_ID('<?php echo $res['id'] ?>');" class="btn btn-danger shadow btn-xs sharp"><i class="fa fa-trash"></i></a>
                                                    </center>
                                                </td>
                                            </tr>
                                            <?php $i++; } ?>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
          
       
            <?php include("includes/footer.php");?>
        </div>
    </div>
    <?php include("includes/js.php");?>
    <script type="text/javascript">
      function delete_ptype_by_ID(id) {
        if (confirm('Do You Want to Deleting This \nContinue anyway?')) {
            window.location.href = 'delete_sales.php?id=' + id;
        }
    }
    </script>
</body>

</html>