<?php
include 'includes/config.php';
session_start();

if (!$_SESSION['email']) {
    header('location:login.php');
}
?>
<?php

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    // Get the form data
    $expense_category_id = $_POST['expense_category'];
    $bank_id = $_POST['bank_id'];
    $amount = $_POST['amount'];
    $note = $_POST['note'];
    $date = $_POST['date'];

    if (empty($expense_category_id) || empty($bank_id) || empty($amount) || empty($note) || empty($date)) {
        echo "<script>alert('Please fill all required fields.');</script>";
    } else {
        $conn->beginTransaction();

        try {
            $sql_expense = "INSERT INTO expense (expense_category_id, bank_id, amount, note, date) 
                            VALUES (:expense_category_id, :bank_id, :amount, :note, :date)";
            
            $stmt_expense = $conn->prepare($sql_expense);
            $stmt_expense->bindParam(':expense_category_id', $expense_category_id, PDO::PARAM_INT);
            $stmt_expense->bindParam(':bank_id', $bank_id, PDO::PARAM_INT);
            $stmt_expense->bindParam(':amount', $amount, PDO::PARAM_STR);
            $stmt_expense->bindParam(':note', $note, PDO::PARAM_STR);
            $stmt_expense->bindParam(':date', $date, PDO::PARAM_STR);
            $stmt_expense->execute();

            $expense_id = $conn->lastInsertId();

            $status = 'expense'; 
            $sql_debit = "INSERT INTO bank_debit_amount (expense_id, bank_id, amount, note, date, status) 
                          VALUES (:expense_id, :bank_id, :amount, :note, :date, :status)";
            
            $stmt_debit = $conn->prepare($sql_debit);
            $stmt_debit->bindParam(':expense_id', $expense_id, PDO::PARAM_INT); 
            $stmt_debit->bindParam(':bank_id', $bank_id, PDO::PARAM_INT);
            $stmt_debit->bindParam(':amount', $amount, PDO::PARAM_STR);
            $stmt_debit->bindParam(':note', $note, PDO::PARAM_STR);
            $stmt_debit->bindParam(':date', $date, PDO::PARAM_STR);
            $stmt_debit->bindParam(':status', $status, PDO::PARAM_STR);
            $stmt_debit->execute();

            // Commit the transaction
            $conn->commit();

            // Display success alert and redirect
            echo "<script>
                    alert('Expense added successfully!');
                    window.location.href = 'add_expense.php'; // Redirect to add_expense.php
                  </script>";
            exit; 

        } catch (Exception $e) {
            $conn->rollBack();

            echo "<script>alert('Error inserting data: " . $e->getMessage() . "');</script>";
        }
    }
}
?>




<!doctype html>
<html lang="en">

<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>Add Expense</title>
    <!-- Favicon -->
    <link rel="shortcut icon" href="images/favicon.ico" />
    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <!-- Typography CSS -->
    <link rel="stylesheet" href="css/typography.css">
    <!-- Style CSS -->
    <link rel="stylesheet" href="css/style.css">
    <!-- Responsive CSS -->
    <link rel="stylesheet" href="css/responsive.css">
    <style>
    ion-icon {
        font-size: 20px;
        margin-bottom: -5px;
    }
    </style>
</head>

<body>
    <!-- loader Start -->
    <div id="loading">
        <div id="loading-center">
        </div>
    </div>
    <!-- loader END -->
    <!-- Wrapper Start -->
    <div class="wrapper">
        <!-- Sidebar  -->
        <?php
        include 'includes/sidebar.php';
        ?>
        <!-- TOP Nav Bar -->
        <?php include 'includes/header.php'; ?>
        <!-- TOP Nav Bar END -->
        <!-- Page Content  -->
        <div id="content-page" class="content-page">
            <div class="container-fluid">
                <div class="row">
                    <div class="col-sm-12">
                        <div class="iq-card">
                            <div class="iq-card-header d-flex justify-content-between row">
                                <div class="iq-header-title col-lg-6">
                                    <h4 class="card-title"><a href="list_expense.php">Expense</a> > Add Expense
                                    </h4>
                                </div>
                            </div>
                            <div class="iq-card-body" style="overflow:auto;">
                                <form action="" method="POST">
                                    <div class="row">

                                        <div class="col-lg-3">
                                            <div class="form-group">
                                                <label for="" class="label-control">Expense Category:*</label>
                                                <?php
$sql = "SELECT * FROM expense_category";
$stmt = $conn->query($sql);
?>

                                                <select name="expense_category" class="form-control"
                                                    id="category_select">
                                                    <option value="">Please Select</option>
                                                    <?php
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
        echo "<option value='" . $row['id'] . "'>" . htmlspecialchars($row['expense_category']) . "</option>";
    }
    ?>
                                                </select>

                                            </div>
                                        </div>

                                        <div class="col-lg-3">
                                            <div class="form-group">
                                                <label for="" class="label-control">Date:*</label>
                                                <input type="date" class="form-control" name="date"
                                                    placeholder="Please Enter Unit (Than)" id="">
                                            </div>
                                        </div>


                                        <?php
$sql = "SELECT id, bank_name FROM banks";
$stmt = $conn->query($sql);
?>

                                        <div class="col-lg-3">
                                            <div class="form-group">
                                                <label for="" class="label-control">Account*</label>
                                                <select name="bank_id" class="form-control" required>
                                                    <option value="">Please Select</option>
                                                    <?php
            while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
                echo "<option value='" . $row['id'] . "'>" . htmlspecialchars($row['bank_name']) . "</option>";
            }
            ?>
                                                </select>
                                            </div>
                                        </div>

                                        <div class="col-lg-3">
                                            <div class="form-group">
                                                <label for="" class="label-control">Total amount:*</label>
                                                <input type="number" class="form-control" name="amount"
                                                    placeholder="Total amount" required>
                                            </div>
                                        </div>
                                        <div class="col-lg-12">
                                            <div class="form-group">
                                                <label for="" class="control-label">Expense Note:</label>
                                                <textarea name="note" class="form-control" required></textarea>
                                            </div>
                                        </div>
                                        <div class="col-lg-12">
                                            <button class="btn btn-primary">Submit</button>
                                        </div>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

        </div>
    </div>
    </div>
    <!-- Wrapper END -->
    <!-- Footer -->
    <?php include 'includes/footer.php'; ?>
    <nav class="iq-float-menu">
        <input type="checkbox" href="#" class="iq-float-menu-open" name="menu-open" id="menu-open" />
        <label class="iq-float-menu-open-button" for="menu-open">
            <span class="lines line-1"></span>
            <span class="lines line-2"></span>
            <span class="lines line-3"></span>
        </label>
        <button class="iq-float-menu-item bg-info" data-toggle="tooltip" data-placement="top" title="Direction Mode"
            data-mode="rtl"><i class="ri-text-direction-r"></i></button>
        <button class="iq-float-menu-item bg-danger" data-toggle="tooltip" data-placement="top" title="Color Mode"
            id="dark-mode" data-active="true"><i class="ri-sun-line"></i></button>
        <button class="iq-float-menu-item bg-warning" data-toggle="tooltip" data-placement="top" title="Comming Soon"><i
                class="ri-palette-line"></i></button>
    </nav>
    <!-- Footer END -->
    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="js/jquery.min.js"></script>
    <!-- Rtl and Darkmode -->
    <script src="js/rtl.js"></script>
    <script src="js/customizer.js"></script>
    <script src="js/popper.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <!-- Appear JavaScript -->
    <script src="js/jquery.appear.js"></script>
    <!-- Countdown JavaScript -->
    <script src="js/countdown.min.js"></script>
    <!-- Counterup JavaScript -->
    <script src="js/waypoints.min.js"></script>
    <script src="js/jquery.counterup.min.js"></script>
    <!-- Wow JavaScript -->
    <script src="js/wow.min.js"></script>
    <!-- Apexcharts JavaScript -->
    <script src="js/apexcharts.js"></script>
    <!-- Slick JavaScript -->
    <script src="js/slick.min.js"></script>
    <!-- Select2 JavaScript -->
    <script src="js/select2.min.js"></script>
    <!-- Owl Carousel JavaScript -->
    <script src="js/owl.carousel.min.js"></script>
    <!-- Magnific Popup JavaScript -->
    <script src="js/jquery.magnific-popup.min.js"></script>
    <!-- Smooth Scrollbar JavaScript -->
    <script src="js/smooth-scrollbar.js"></script>
    <!-- lottie JavaScript -->
    <script src="js/lottie.js"></script>
    <!-- Chart Custom JavaScript -->
    <script src="js/chart-custom.js"></script>
    <!-- Custom JavaScript -->
    <script src="js/custom.js"></script>
</body>

<!-- Mirrored from templates.iqonic.design/vito/html/main/data-table.html by HTTrack Website Copier/3.x [XR&CO'2014], Wed, 21 Aug 2024 10:45:18 GMT -->

</html>