<?php
include("../classes/manager.class.php");
//ini_set('display_errors', 1);
//error_reporting(E_ALL);
$m = new Manager();
$output = '';
$supplier_id = '';
$amount = '';
$payment_type = '';
$purchase_reference_code = '';
$withholding_tax = '';
$advance_payment = 'No';
$from_bank_name = '';
$account_no = '';
$cheque_no = '';
$cheque_bank_name = '';
$cheque_date = '';
$cheque_payee = '';
$hide = '';
$party_name = '';
$bank_name = '';
$description = '';
if (!isset($_SESSION['sess_userid'])) {
    header("Location: " . PROJECT_ADMIN_URL);
    exit();
}
$current_date = date('d-m-Y');
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <?php include('includes/inc_head.php'); ?>
    <script>
        $(function () {
            $('#datepicker').pickadate({
                format: 'dd-mm-yyyy',
                selectMonths: true,
                selectYears: true,
            });
            $('#cheque_date').pickadate({
                format: 'dd-mm-yyyy',
                selectMonths: true,
                selectYears: true,
            });
        });
    </script>
</head>
<body>
<!-- Start Page Loading -->
<div id="loader-wrapper">
    <div id="loader"></div>
    <div class="loader-section section-left"></div>
    <div class="loader-section section-right"></div>
</div>
<!-- End Page Loading -->
<div id="container">
    <?php include('includes/inc_top.php'); ?>
    <div class="row">
        <div class="col s12 m12">
            <div class="">
                <h3>Purchase Payment</h3>
            </div>
            <hr>
            <?php if ($_SESSION['sess_role'] == 'VEC') {
                if (isset($_GET['view'])) { ?>
                    <a class="btn waves-effect waves-light" style="float:right;margin-left:5px;" href="?add">Add <i
                            class="mdi-content-add-box"></i></a>
                    <div class="clearfix"></div>
                <?php }
            } ?>
            <br/>
            <?php
            if (isset($_GET['add']) || isset($_GET['edit'])) {
                if (isset($_GET['edit'])) {
                    extract($_REQUEST);
                    $getPayment = Purpayment::GetPurpaymentSetupById($id);
                    $supplier_id = $getPayment->supplier_id;
                    $payment_type = $getPayment->payment_type;
                  	$purchase_reference_code = $getPayment->purchase_reference_code;
                  	$withholding_tax = $getPayment->withholding_tax;
                  	$advance_payment = $getPayment->advance_payment;
                    $amount = $getPayment->amount;
                    $party_name = $getPayment->party_name;
                    $from_bank_name = $getPayment->from_bank_name;
                    $cheque_no = $getPayment->cheque_number;
                    $cheque_bank_name = $getPayment->cheque_bank_name;
                    $cheque_payee = $getPayment->cheque_payee_name;
                    $cheque_date = date('d-m-Y', strtotime($getPayment->cheque_date));
                    $bank_name = $getPayment->bank_name;
                    $account_no = $getPayment->bank_account_number;
                    $payment_date = date('d-m-Y', strtotime($getPayment->create_date));
                    $paymentNumber = $getPayment->code;
                    $description = $getPayment->description;
                } else if (isset($_GET['add']) || isset($_GET['ref'])) {
                    $paymentfilter = " ORDER BY id DESC limit 1 ";
                    $payments = Purpayment::GetPurpaymentList($paymentfilter);
                    if (!empty($payments[0]->code)) {
                        $paymentNumber = intval($payments[0]->code);
                        $paymentNumber = $paymentNumber + 1;
                    } else {
                        $paymentNumber = __PURPAYMENT__;
                    }
                }
            }
            if (isset($_POST) && !empty($_POST)) {
                extract($_REQUEST);
                //print_r($_REQUEST);
                //exit;
                $purchasesCode = PurchaseSetup::GetPurchaseSetupById($purchase_reference_code);
                
                $data['supplier_id'] = $supplier_name;
                $data['code'] = $payment_number;
                $data['payment_type'] = $payment_type;
              	$data['purchase_reference_code'] = $purchasesCode->code;              
              	$data['advance_payment'] = $advance_payment;
          	    $data['withholding_tax'] = $withholding_tax;
                $data['amount'] = $amount;
                $data['party_name'] = $party_name;
                $data['from_bank_name'] = $from_bank_name;
                $data['cheque_number'] = $cheque_no;
                $data['cheque_bank_name'] = $cheque_bank_name;
                $data['cheque_payee_name'] = $cheque_payee;
                $data['cheque_date'] = date('Y-m-d H:i:s', strtotime(date('H:i:s'), strtotime($cheque_date)));
                $data['bank_name'] = $bank_name;
                $data['bank_account_number'] = $account_no;
                $data['description'] = $description;
                $data['create_date'] = date('Y-m-d H:i:s', strtotime(date('H:i:s'), strtotime($payment_date)));
                $data['datetime'] = 'NOW()';

                $errors = array();
                // Validate the input
                $filterPayments = " WHERE cheque_number = '$cheque_no'";
                $checkPayments = Purpayment::GetPurpaymentList($filterPayments);

                if (isset($_GET['add']) || isset($_GET['ref'])) {
                    if (!empty($checkPayments) && $payment_type == 'bank') {
                        array_push($errors, "Cheque No Already Exists");
                    }
                }


                if ((strlen($supplier_name) == 0) || (strlen($supplier_name) > 255))
                    array_push($errors, "Please enter supplier name");
                if ((strlen($payment_date) == 0))
                    array_push($errors, "Please enter payment date");
                if ((strlen($payment_type) == 0))
                    array_push($errors, "Please enter payment type");
                if ((strlen($amount) == 0 || $amount == 0))
                    array_push($errors, "Please enter payment amount");
                if ($payment_type == 'bank') {
                    if ((strlen($bank_name) == 0))
                        array_push($errors, "Please enter bank name");
                }

                if (count($errors) == 0) {
                    if (isset($_GET['add']) || isset($_GET['ref'])) {
                        $q = Purpayment::AddPurpayment($data);

                    } else {
                        $q = Purpayment::UpdatePurpaymentSetup($id, $data);
                    }
                    if ($q) {
                        $output = '';
                        //header("Location: payment.php?view&sucess"); ?>
                        <script>window.location.href = 'purpayment.php?view&sucess'</script>
                        <?php
                        echo "<div class='alert alert-success fade in'>
          <button type='button' class='close' data-dismiss='alert'>&times;</button>
          <strong>Attention!</strong> Submitted! </div>";
                    } else {
                        $output = '';
                        echo "<div class='alert alert-error fade in'>
          <button type='button' class='close' data-dismiss='alert'>&times;</button>
          <strong>Attention!</strong> Some error occured! </div>";
                    }
                } else {
                    //Prepare errors for output
                    foreach ($errors as $val) {
                        $output .= "<div class='alert alert-warning fade in'>
                        <button type='button' class='close' data-dismiss='alert'>&times;</button>
                        <strong>Attention! </strong>$val</div>";
                    }
                }
            }
            ?>
            <form action="" method="post" id="purpayment_create_form" novalidate="novalidate">
                <div class="row">
                    <div class="col s12 form-group">
                        <?php echo $output; ?>
                        <div class="row">
                            <div class="col m3 s12">
                                <label class="control-label">Sales Person *</label>
                                <input class="form-control" type="text" id="sales_person"
                                       name="sales_person" readonly
                                       value='<?php echo $_SESSION['sess_username']; ?>'/>
                                <input class="form-control" type="hidden" id="sales_person_id"
                                       name="sales_person_id"
                                       value='<?php echo $_SESSION['sess_userid']; ?>'/><br/>
                            </div>
                            <div class="col m3 s12">
                                <label class="control-label">Payment No. *</label>
                                <input class="form-control" type="text" id="payment_number"
                                       name="payment_number" readonly
                                       value='<?php echo $paymentNumber; ?>'/><br/>
                            </div>
                          <div class="col m3 s12">
                                <label class="control-label">Payment Date *</label>
                                <input class="form-control" required type="text" id="datepicker"
                                       name="payment_date"
                                       placeholder="<?= date('01-01-Y') ?>"
                                       value='<?= !empty($payment_date) ? $payment_date : $current_date; ?>'/>
                          </div>
                          <div class="col m3 s12">
                                <label class="control-label">Advance Payment*</label>
                              <select class="form-control" id="advance_payment" required name="advance_payment">
        <option value="" disabled <?= !isset($advance_payment) ? 'selected' : '' ?>>Select an option</option>
        <option value="1" <?= (isset($advance_payment) && $advance_payment == 'Yes') ? 'selected' : '' ?>>Yes</option>
        <option value="0" <?= (isset($advance_payment) && $advance_payment == 'No') || !isset($advance_payment) ? 'selected' : '' ?>>No</option>
    </select><br/>
                            </div>
                        </div>
                      
                        <div class="clearfix"></div>
                        <div class="row">
                            <div class="col m6 s12">
                                <label class="control-label">Supplier *</label>
                                <?php
                                $suppliersfilter = " WHERE status = 'active' ORDER BY name";
                                $suppliers = SupplierSetup::GetSupplierSetupList($suppliersfilter);
                                ?>
                                <select class="form-control" required id="supplier_name"
                                        name="supplier_name">
                                    <option></option>
                                    <?php
                                    if ($suppliers) {
                                        foreach ($suppliers AS $suppliersRow) {
                                            echo '<option ';
                                            if ($suppliersRow->id == $supplier_id) {
                                                echo 'selected';
                                            }
                                            echo ' value="' . $suppliersRow->id . '" >' . $suppliersRow->name . '</option>';
                                        }
                                    }
                                    ?>
                                </select><br/>
                            </div>
                            
                          
                          <div class="row">
                            <div class="col m6 s12">
                              
                                    <div class="input-field col s12">
                                        <?php
                                        if (isset($_GET)) {
                                           $filterPurchases = " WHERE supplier_id = '$supplier_id' AND status = 'Pending'";
                                        }
                                        $purchases = PurchaseSetup::GetPurchaseSetupList($filterPurchases);
                                        ?>
                                        <select class="form-control" id="purchase_reference_code"
                                                name="purchase_reference_code">
                                            <option></option>
                                            <?php
                                            //echo $invoice_reference_code;
                                            if ($purchases) {
                                                foreach ($purchases AS $purchasesRow) {
                                                    echo '<option ';
                                                    if ($purchasesRow->code == $purchase_reference_code) {
                                                        echo 'selected';
                                                    }
                                                     echo ' value="' . htmlspecialchars($purchasesRow->code) . '">' . htmlspecialchars($purchasesRow->code) . '</option>'; // Use code as value
                                                }
                                            }
                                            ?>
                                        </select>
                                        <label for="invoice_reference_code" class="control-label">Purchase Ref.</label>
                                    </div>
                               
                            </div>
                          </div>
                       
                        <div class="clearfix"></div>
                        <div class="row">
                            <div class="col m4 s12">
                                <label class="control-label">Payment Type *</label>
                                <select class="form-control" id="payment_type" required
                                        name="payment_type">
                                    <option></option>
                                    <option <?= $payment_type == 'cash' ? 'selected' : '' ?>
                                        value="cash">Cash
                                    </option>
                                    <option <?= $payment_type == 'bank' ? 'selected' : '' ?>
                                        value="bank">Bank
                                    </option>
                                    <option <?= $payment_type == 'party' ? 'selected' : '' ?>
                                        value="party">Party
                                    </option>
                                    <option <?= $payment_type == 'online' ? 'selected' : '' ?>
                                        value="online">Online
                                    </option>
                                </select><br/>
                            </div>
                            <div class="col m4 s12">
                                <label class="control-label">Amount *</label>
                                <input class="form-control" placeholder="00000.00"
                                       onkeypress="return isDoubleNumberKey(event)" required type="text"
                                       id="amount"
                                       value="<?= $amount ?>"
                                       name="amount"/><br/>

                            </div>
                          <div class="col m4 s12">
                                <label class="control-label">Withholding Tax</label>
                                <input class="form-control" type="text" id="withholding_tax"
                                       name="withholding_tax" value="<?= $withholding_tax ?>"/><br/>
                            </div>
                        </div>
                        <div class="row">
                            <div id="bankDetails">
                                <div <?= $payment_type != 'party' ? 'style="display:none;"' : '' ?>
                                    class="col m6 s12" id="party_name_detail">
                                    <label class="control-label">Party Name</label>
                                    <input class="form-control" type="text" id="party_name"
                                        <?php if ($payment_type != 'party') {
                                            echo 'disabled';
                                        } ?> name="party_name" value="<?= $party_name ?>"/><br/>
                                </div>
                                <div <?= $payment_type != 'online' ? 'style="display:none;"' : '' ?>
                                    class="col m6 s12" id="from_bank_name_detail">
                                    <label class="control-label">To Bank Name</label>
                                    <input class="form-control" type="text" id="from_bank_name"
                                        <?php if ($payment_type != 'online') {
                                            echo 'disabled';
                                        } ?> name="from_bank_name" value="<?= $from_bank_name ?>"/><br/>
                                </div>
                                <div class="clearfix"></div>
                                <br>

                                <div class="col m6 s12">
                                    <label class="control-label">Bank Name *</label>
                                    <?php
                                    $bankfilter = " WHERE status = 'active' ORDER BY bank_name";
                                    $banks = BankSetup::GetBankSetupList($bankfilter);
                                    ?>
                                    <select class="form-control" id="bank_name" required
                                            name="bank_name">
                                        <option></option>
                                        <?php
                                        if ($banks) {
                                            foreach ($banks AS $banksRow) {
                                                echo '<option ';
                                                if ($banksRow->id == $bank_name) {
                                                    echo 'selected';
                                                }
                                                echo ' value="' . $banksRow->id . '" >' . $banksRow->bank_name . '</option>';
                                            }
                                        }
                                        ?>
                                    </select><br/>
                                </div>
                                <div class="col m6 s12">
                                    <label class="control-label">Account No.</label>
                                    <input class="form-control" readonly type="text" id="account_no"
                                           name="account_no" value="<?= $account_no ?>"/><br/>
                                </div>
                            </div>
                        </div>
                        <?php if ($payment_type == 'cash' || $payment_type == 'online') {
                            $hide = 'style="display:none;"';
                        } ?>
                        <div <?= $hide ?> class="row" id="chequeDetails">
                            <div class="col m6 s12">
                                <label class="control-label">Cheque No.</label>
                                <input class="form-control" type="text" id="cheque_no"
                                    <?php if ($payment_type == 'cash' || $payment_type == 'online') {
                                        echo 'disabled';
                                    } ?> name="cheque_no" value="<?= $cheque_no ?>"/><br/>
                            </div>
                            <div class="col m6 s12">
                                <label class="control-label">Cheque Bank</label>
                                <input class="form-control" type="text" id="cheque_bank_name"
                                    <?php if ($payment_type == 'cash' || $payment_type == 'online') {
                                        echo 'disabled';
                                    } ?> name="cheque_bank_name" value="<?= $cheque_bank_name ?>"/><br/>
                            </div>
                            <div class="clearfix"></div>
                            <br>

                            <div class="col m6 s12">
                                <label class="control-label">Cheque Date</label>
                                <input class="form-control" type="text" id="cheque_date"
                                    <?php if ($payment_type == 'cash' || $payment_type == 'online') {
                                        echo 'disabled';
                                    } ?> name="cheque_date" value="<?= $cheque_date ?>"/><br/>
                            </div>
                            <div class="col m6 s12">
                                <label class="control-label">Cheque Payee</label>
                                <input class="form-control" type="text" id="cheque_payee"
                                    <?php if ($payment_type == 'cash' || $payment_type == 'online') {
                                        echo 'disabled';
                                    } ?> name="cheque_payee" value="<?= $cheque_payee ?>"/><br/>
                            </div>
                            <div class="clearfix"></div>
                            <br>
                        </div>
                        <div class="clearfix"></div>
                        <div class="row">
                            <div class="col m12 s12">
                                <label class="control-label">Description</label>
                                <input class="form-control" type="text" id="description"
                                       name="description" value="<?= $description ?>"/><br/>
                            </div>
                        </div>
                        <div class="clearfix"></div>
                        <br><br>
                    </div>
                </div>

                <div class="clearfix"></div>
                <button id="btnsubmit" type="submit" class="btn waves-effect waves-light">Submit</button>
                <button type="button" class="btn waves-effect waves-light"
                        onclick="location.href='purpayment.php?view'">Cancel
                </button>
            </form>
            <?php

            // close view
            ?>
        </div>
    </div>
</div>
<footer>
    <?php include('includes/inc_footer.php'); ?>
</footer>
</div>
</section>

<?php include('includes/inc_js.php'); ?>
<script type="application/javascript">
    <?php if(isset($_GET['edit']) && $payment_type == 'bank'){?>
    $(document).ready(function () {
        $('#bankDetails').show();
        $('#bank_name').attr('required', true);
        $('#bank_name').attr('disabled', false);
        $('#party_name').attr('required', true);
        $('#party_name').attr('disabled', false);
        $('#cheque_no').attr('disabled', false);
        $('#cheque_bank_name').attr('disabled', false);
        $('#cheque_payee').attr('disabled', false);
        $('#cheque_date').attr('disabled', false);
        $('#account_no').attr('disabled', false);
        $('#bank_name').rules('add', {
            required: true,
            messages: {
                required: "<span class='label label-warning val_mes'>Please select bank name</span>"
            }
        });
    });
    <?php }?>
    $('#payment_type').on('change', function () {
        if ($(this).val() == 'bank') {
            $('#chequeDetails').show();
            $('#party_name_detail').hide();
            $('#from_bank_name_detail').hide();

            $('#bank_name').attr('required', true);
            $('#bank_name').attr('disabled', false);
            $('#bank_name').rules('add', {
                required: true,
            });

            $('#account_no').attr('required', true);
            $('#account_no').attr('disabled', false);
            $('#account_no').rules('add', {
                required: true,
            });

            $('#from_bank_name').attr('required', false);
            $('#from_bank_name').attr('disabled', true);
            $('#from_bank_name').rules('add', {
                required: false,
            });

            $('#party_name').attr('required', false);
            $('#party_name').attr('disabled', true);
            $('#party_name').rules('add', {
                required: false,
            });

            $('#cheque_no').attr('required', true);
            $('#cheque_no').attr('disabled', false);
            $('#cheque_no').rules('add', {
                required: true,
                messages: {
                    required: "<span class='label label-warning val_mes'>Please enter cheque number</span>"
                }
            });

            $('#cheque_bank_name').attr('required', true);
            $('#cheque_bank_name').attr('disabled', false);
            $('#cheque_bank_name').rules('add', {
                required: true,
                messages: {
                    required: "<span class='label label-warning val_mes'>Please enter cheque bank name</span>"
                }
            });

            $('#cheque_payee').attr('required', true);
            $('#cheque_payee').attr('disabled', false);
            $('#cheque_payee').rules('add', {
                required: true,
                messages: {
                    required: "<span class='label label-warning val_mes'>Please enter cheque payee name</span>"
                }
            });

            $('#cheque_date').attr('required', true);
            $('#cheque_date').attr('disabled', false);
            $('#cheque_date').rules('add', {
                required: true,
                messages: {
                    required: "<span class='label label-warning val_mes'>Please enter cheque date</span>"
                }
            });
        } else if ($(this).val() == 'cash') {
            $('#chequeDetails').hide();
            $('#party_name_detail').hide();
            $('#from_bank_name_detail').hide();

            $('#bank_name').attr('required', true);
            $('#bank_name').attr('disabled', false);
            $('#bank_name').rules('add', {
                required: true,
            });

            $('#account_no').attr('required', true);
            $('#account_no').attr('disabled', false);
            $('#account_no').rules('add', {
                required: true,
                messages: {
                    required: "<span class='label label-warning val_mes'>Please enter account number</span>"
                }
            });

            $('#from_bank_name').attr('required', false);
            $('#from_bank_name').attr('disabled', true);
            $('#from_bank_name').rules('add', {
                required: false,
            });

            $('#party_name').attr('required', false);
            $('#party_name').attr('disabled', true);
            $('#party_name').rules('add', {
                required: false,
            });

            $('#cheque_no').attr('required', false);
            $('#cheque_no').attr('disabled', true);
            $('#cheque_no').rules('add', {
                required: false,
            });

            $('#cheque_bank_name').attr('required', false);
            $('#cheque_bank_name').attr('disabled', true);
            $('#cheque_bank_name').rules('add', {
                required: false,
            });

            $('#cheque_payee').attr('required', false);
            $('#cheque_payee').attr('disabled', true);
            $('#cheque_payee').rules('add', {
                required: false,
            });

            $('#cheque_date').attr('required', false);
            $('#cheque_date').attr('disabled', true);
            $('#cheque_date').rules('add', {
                required: false,
            });
        } else if ($(this).val() == 'party') {
            $('#chequeDetails').show();
            $('#party_name_detail').show();
            $('#from_bank_name_detail').hide();

            $('#bank_name').attr('required', false);
            $('#bank_name').attr('disabled', true);
            $('#bank_name').rules('add', {
                required: false,
            });

            $('#account_no').attr('required', false);
            $('#account_no').attr('disabled', true);
            $('#account_no').rules('add', {
                required: false,
            });

            $('#from_bank_name').attr('required', false);
            $('#from_bank_name').attr('disabled', true);
            $('#from_bank_name').rules('add', {
                required: false,
            });

            $('#party_name').attr('required', true);
            $('#party_name').attr('disabled', false);
            $('#party_name').rules('add', {
                required: true,
                messages: {
                    required: "<span class='label label-warning val_mes'>Please enter party name</span>"
                }
            });

            $('#cheque_no').attr('required', true);
            $('#cheque_no').attr('disabled', false);
            $('#cheque_no').rules('add', {
                required: true,
                messages: {
                    required: "<span class='label label-warning val_mes'>Please enter cheque number</span>"
                }
            });

            $('#cheque_bank_name').attr('required', true);
            $('#cheque_bank_name').attr('disabled', false);
            $('#cheque_bank_name').rules('add', {
                required: true,
                messages: {
                    required: "<span class='label label-warning val_mes'>Please enter cheque bank name</span>"
                }
            });

            $('#cheque_payee').attr('required', true);
            $('#cheque_payee').attr('disabled', false);
            $('#cheque_payee').rules('add', {
                required: true,
                messages: {
                    required: "<span class='label label-warning val_mes'>Please enter cheque payee name</span>"
                }
            });

            $('#cheque_date').attr('required', true);
            $('#cheque_date').attr('disabled', false);
            $('#cheque_date').rules('add', {
                required: true,
                messages: {
                    required: "<span class='label label-warning val_mes'>Please enter cheque date</span>"
                }
            });
        } else if ($(this).val() == 'online') {
            $('#chequeDetails').hide();
            $('#party_name_detail').hide();
            $('#from_bank_name_detail').show();

            $('#bank_name').attr('required', true);
            $('#bank_name').attr('disabled', false);
            $('#bank_name').rules('add', {
                required: true,
            });

            $('#account_no').attr('required', true);
            $('#account_no').attr('disabled', false);
            $('#account_no').rules('add', {
                required: true,
                messages: {
                    required: "<span class='label label-warning val_mes'>Please enter account number</span>"
                }
            });


            $('#from_bank_name').attr('disabled', false);

            $('#party_name').attr('required', false);
            $('#party_name').attr('disabled', true);
            $('#party_name').rules('add', {
                required: false,
                messages: {
                    required: "<span class='label label-warning val_mes'>Please enter party name</span>"
                }
            });

            $('#cheque_no').attr('required', false);
            $('#cheque_no').attr('disabled', true);
            $('#cheque_no').rules('add', {
                required: false,
                messages: {
                    required: "<span class='label label-warning val_mes'>Please enter cheque number</span>"
                }
            });

            $('#cheque_bank_name').attr('required', false);
            $('#cheque_bank_name').attr('disabled', true);
            $('#cheque_bank_name').rules('add', {
                required: false,
                messages: {
                    required: "<span class='label label-warning val_mes'>Please enter cheque bank name</span>"
                }
            });

            $('#cheque_payee').attr('required', false);
            $('#cheque_payee').attr('disabled', true);
            $('#cheque_payee').rules('add', {
                required: false,
                messages: {
                    required: "<span class='label label-warning val_mes'>Please enter cheque payee name</span>"
                }
            });

            $('#cheque_date').attr('required', false);
            $('#cheque_date').attr('disabled', true);
            $('#cheque_date').rules('add', {
                required: false,
                messages: {
                    required: "<span class='label label-warning val_mes'>Please enter cheque date</span>"
                }
            });
        }
    });
    $('#bank_name').on('change', function () {
        //if ($(this).val() != 'bank') {
        $('body.container').block({message: 'Loading...'});
        $.get('transaction/getBankDetails.php?id=' + $(this).val(), function (data) {
            $("#account_no").val(data);
            $('body.container').unblock();
        });
        $('#cheque_bank_name').val($('#bank_name :selected').text());
        //console.log();
        //}
    });
   // initialize
    $('#purchase_reference_code').material_select();

    // setup listener for custom event to re-initialize on change
    $('#purchase_reference_code').on('contentChanged', function () {
        $(this).material_select();
    });

    $('#supplier_name').on('change', function () {
        $(this).material_select();
        $('body.container').block({message: 'Loading...'});
        $.get('getFilterPurchases.php?id=' + $(this).val(), function (data) {
            $("#purchase_reference_code").empty();
            $("#purchase_reference_code").append(data);
            $('body.container').unblock();
            $('#purchase_reference_code').trigger('contentChanged');
        });
    });
  $(document).ready(function() {
    // When a purchase reference is selected, trigger an AJAX request to get the total amount
    $('#purchase_reference_code').on('change', function() {
        var purchaseId = $(this).val(); // Get the selected purchase ID
        if (purchaseId) {
            // Send AJAX request to get the total amount for the selected purchase reference
            $.ajax({
                url: 'getPurchaseTotalAmount.php', // Create a PHP file to handle this
                type: 'GET',
                data: { id: purchaseId }, // Pass the purchase ID to the server
                success: function(response) {
                    // Parse the response (assuming it's in JSON format)
                    var data = JSON.parse(response);
                    // Set the amount field with the total amount
                    $('#amount').val(data.total_amount);
                },
                error: function(xhr, status, error) {
                    console.log('Error fetching amount: ' + error);
                }
            });
        }
    });
});

  
    function isDoubleNumberKey(evt) {
        var charCode = (evt.which) ? evt.which : evt.keyCode;
        if (charCode != 46 && charCode > 31
            && (charCode < 48 || charCode > 57))
            return false;

        return true;
    }
    $(document).ready(function () {
        var fewSeconds = 2;
        $('#btnsubmit').click(function () {
            // Ajax request
            var btn = $(this);
            btn.hide();
            setTimeout(function () {
                btn.show();
            }, fewSeconds * 1000);
        });
    });
</script>
</body>
</html>