708 lines
36 KiB
PHP
Executable File
708 lines
36 KiB
PHP
Executable File
<?php include('layout/header.php'); ?>
|
|
|
|
<style>
|
|
.large-checkbox {
|
|
transform: scale(1.5); /* Adjust the scale value as needed */
|
|
-webkit-transform: scale(1.5); /* Safari and Chrome */
|
|
-moz-transform: scale(1.5); /* Firefox */
|
|
-ms-transform: scale(1.5); /* Internet Explorer */
|
|
-o-transform: scale(1.5); /* Opera */
|
|
margin: 10px; /* Adjust the margin as needed to avoid layout issues */
|
|
}
|
|
</style>
|
|
|
|
<div class="row">
|
|
<div class="col-12">
|
|
<div class="page-title-box page-title-box-alt">
|
|
<h4 class="page-title"><?php echo $page_name?></h4>
|
|
<div class="page-title-right">
|
|
<a href="<?= base_url() . "purchase_index"; ?>" class="btn btn-secondary waves-effect">Cancel</a>
|
|
<ol class="breadcrumb m-0">
|
|
|
|
</li>
|
|
<!-- <li class="breadcrumb-item"><a href="javascript: void(0);">Tables</a></li>
|
|
<li class="breadcrumb-item active">Datatables</li> -->
|
|
</ol>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="row">
|
|
<div class="col-md-12">
|
|
<div class="card">
|
|
<div class="card-body">
|
|
<h4 class="header-title"></h4>
|
|
<form action="<?= base_url() . "add_purchase"; ?>" method="post" id="purchaseForm">
|
|
<input type="hidden" id="purchase_order_id" name="purchase_order_id" value="<?= isset($purchase['purchase_order_id']) ? $purchase['purchase_order_id'] : '' ?>"readonly>
|
|
<h4 class="header-title">Purchase Details :</h4><br>
|
|
<div class="form-row">
|
|
<div class="form-group col-md-4">
|
|
<label for="make" class="col-form-label">Vendor<span class="text-danger">*</span></label>
|
|
<select class="form-control vendor-select SelExample" name="vendor_id" id="vendor_id" <?php echo in_array($vendor_id, array_column($vendor, 'vendor_id')) ? 'disabled' : ''; ?>>
|
|
<option value="0">Select Vendor</option>
|
|
<?php foreach ($vendor as $item): ?>
|
|
<option value="<?= $item['vendor_id'] ?>" <?= isset($item['vendor_id']) && $vendor_id == $item['vendor_id'] ? 'selected' : '' ?>>
|
|
<?= $item['vendor_name'] ?>
|
|
</option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
|
|
|
|
|
|
<div class="form-group col-md-4">
|
|
<label for="contact_person_name" class="col-form-label">Contact Person Name<span class="text-danger">*</span></label>
|
|
<input type="text" class="form-control" name="contact_person_name" id="contact_person_name" placeholder="Contact Person Name"value="<?= isset($purchase['contact_person_name']) ? $purchase['contact_person_name'] : '' ?>" required>
|
|
</div>
|
|
<div class="form-group col-md-4">
|
|
<label for="contact_person_mobile" class="col-form-label">Contact Person Mobile<span class="text-danger">*</span></label>
|
|
<input type="text" class="form-control" name="contact_person_mobile" id="contact_person_mobile" placeholder="Contact Person Mobile" value="<?= isset($purchase['contact_person_mobile']) ? $purchase['contact_person_mobile'] : '' ?>"maxlength="10" minlength="10" onkeypress = "return onlyNumbers(event)" required>
|
|
</div>
|
|
<div class="form-group col-md-4">
|
|
<label for="order_date" class="col-form-label">Order Date</label>
|
|
<input type="date" class="form-control" name="order_date" id="order_date" value="<?= empty($purchase['order_date']) ? date('Y-m-d') : date('Y-m-d', strtotime($purchase['order_date'])) ?>" >
|
|
</div>
|
|
<?php if (!empty($purchase["status"])): ?>
|
|
<div class="form-group col-md-4">
|
|
<label for="order_date" class="col-form-label">Return Date</label>
|
|
<input type="date" class="form-control" name="return_date" id="return_date" value="<?= empty($purchase['return_date']) ? date('Y-m-d') : date('Y-m-d', strtotime($purchase['return_date'])) ?>" >
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
<div class="form-row">
|
|
<div class="form-group col-md-3">
|
|
<label for="billing_address" class="col-form-label">Billing Addresss<span class="text-danger">*</span></label>
|
|
<input type="text" class="form-control" name="billing_address" id="billing_address" placeholder="Billing Addresss"value="<?= isset($business['address']) ? $business['address'] : (isset($purchase['billing_address']) ? $purchase['billing_address'] : '') ?>" required>
|
|
</div>
|
|
<div class="form-group col-md-3" >
|
|
<label for="billing_state" class="col-form-label">Billing State</label>
|
|
<input type="text" class="form-control" name="billing_state" id="billing_state" placeholder="Billing State"value="<?= isset($business['state']) ? $business['state'] : (isset($purchase['billing_state']) ? $purchase['billing_state'] : '') ?>" >
|
|
</div>
|
|
<div class="form-group col-md-3">
|
|
<label for="Billing City" class="col-form-label">Billing City<span class="text-danger">*</span></label>
|
|
<input type="text" class="form-control" name="billing_city" id="billing_city" placeholder="Billing City"value="<?= isset($business['city']) ? $business['city'] : (isset($purchase['billing_city']) ? $purchase['billing_city'] : '') ?>" required>
|
|
</div>
|
|
<div class="form-group col-md-3">
|
|
<label for="Billing Postal Code" class="col-form-label">Billing Pin Code<span class="text-danger">*</span></label>
|
|
<input type="text" class="form-control" name="billing_postal_code" id="billing_postal_code" placeholder="Billing Pin Code"value="<?= isset($business['postal_code']) ? $business['postal_code'] : (isset($purchase['billing_postal_code']) ? $purchase['billing_postal_code'] : '') ?>" required>
|
|
</div>
|
|
|
|
</div><br>
|
|
|
|
<div class="form-row">
|
|
<div class="form-group col-md-3">
|
|
<label for="shipping_address" class="col-form-label">Shipping Addresss<span class="text-danger">*</span></label>
|
|
<input type="text" class="form-control" name="shipping_address" id="shipping_address" placeholder="Shipping Addresss"value="<?= isset($branch['address']) ? $branch['address'] : (isset($purchase['shipping_address']) ? $purchase['shipping_address'] : '') ?>" required>
|
|
</div>
|
|
<div class="form-group col-md-3" >
|
|
<label for="shipping_state" class="col-form-label">Shipping State</label>
|
|
<input type="text" class="form-control" name="shipping_state" id="shipping_state" placeholder="Shipping State"value="<?= isset($branch['state']) ? $branch['state'] : (isset($purchase['shipping_state']) ? $purchase['shipping_state'] : '') ?>" >
|
|
</div>
|
|
<div class="form-group col-md-3">
|
|
<label for="Shipping City" class="col-form-label">Shipping City<span class="text-danger">*</span></label>
|
|
<input type="text" class="form-control" name="shipping_city" id="shipping_city" placeholder="Shipping City"value="<?= isset($branch['city']) ? $branch['city'] : (isset($purchase['shipping_city']) ? $purchase['shipping_city'] : '') ?>" required>
|
|
</div>
|
|
<div class="form-group col-md-3">
|
|
<label for="Shipping Postal Code" class="col-form-label">Shipping Pin Code<span class="text-danger">*</span></label>
|
|
<input type="text" class="form-control" name="shipping_postal_code" id="shipping_postal_code" placeholder="Shipping Pin Code"value="<?= isset($branch['postal_code']) ? $branch['postal_code'] : (isset($purchase['shipping_postal_code']) ? $purchase['shipping_postal_code'] : '') ?>" required>
|
|
</div>
|
|
|
|
</div><br>
|
|
<div class="form-row">
|
|
<?php if (!empty($purchase["status"])): ?>
|
|
|
|
<div class="form-group col-md-3">
|
|
<label for="shipping_address" class="col-form-label">Bill Number<span class="text-danger">*</span></label>
|
|
<input type="text" class="form-control" name="bill_number" id="bill_number" placeholder="Bill Number"value="<?= isset($purchase['bill_number']) ? $purchase['bill_number'] : '' ?>" required>
|
|
</div>
|
|
|
|
<?php endif; ?>
|
|
<div class="form-group col-md-3">
|
|
<label for="Status" class="col-form-label">Status
|
|
<?php if (isset($purchase['status']) && $purchase['status'] !== ''): ?>
|
|
(<?= $purchase['status'] ?>)
|
|
<?php endif; ?></label>
|
|
<select class="form-control status-select" name="purchase_status" data-toggle="select2">
|
|
<?php if (isset($purchase['status']) && !isset($reorder)): ?>
|
|
<?php if ($purchase['status'] === 'Cancel'): ?>
|
|
<option value="">Select Status</option>
|
|
<option value="Received" <?= $purchase['status'] === 'Received' ? 'selected' : '' ?>>Received</option>
|
|
<?php else: ?>
|
|
<option value="">Select Status</option>
|
|
<option value="Received" <?= $purchase['status'] === 'Received' ? 'selected' : '' ?>>Received</option>
|
|
<option value="Cancel" <?= $purchase['status'] === 'Cancel' ? 'selected' : '' ?>>Cancel</option>
|
|
<?php endif; ?>
|
|
<?php else: ?>
|
|
<!-- <option value="PO Issued" selected>PO Issued</option> -->
|
|
<option value="PO Issued" <?= isset($purchase['status']) && !isset($reorder) && $purchase['status'] === 'PO Issued' ? 'selected' : '' ?>>PO Issued</option>
|
|
<option value="Received" <?= isset($purchase['status']) && !isset($reorder) && $purchase['status'] === 'Received' ? 'selected' : '' ?>>Received</option>
|
|
<!-- <option value="Paid">Paid</option> -->
|
|
<?php endif; ?>
|
|
</select>
|
|
</div>
|
|
</div>
|
|
<div class="form-row" id="itemTableContainer">
|
|
<div class="form-group col-md-12">
|
|
<h4>Item Details</h4>
|
|
<br />
|
|
<table class="table table-bordered" id="itemTable">
|
|
<thead>
|
|
<tr>
|
|
<th>Item Details</th>
|
|
<th class="header-qty <?php if (!empty($purchase["status"]) && !isset($reorder)) echo 'd-none'; ?>">Qty</th>
|
|
<th class="header-original-qty <?php if (empty($purchase["status"]) || isset($reorder)) echo 'd-none'; ?>">Original Qty</th>
|
|
<th class="header-rcvd-qty <?php if (empty($purchase["status"]) || isset($reorder)) echo 'd-none'; ?>">Rcvd Qty</th>
|
|
<th class="header-received <?php if (empty($purchase["status"]) || isset($reorder)) echo 'd-none'; ?>">Received</th>
|
|
<th class="header-amount <?php if (empty($purchase["status"]) || isset($reorder)) echo 'd-none'; ?>">Amount</th>
|
|
<th hidden></th>
|
|
<th></th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
|
|
<!-- Dynamically add rows for item details -->
|
|
<?php if (isset($purchase_product) && !empty($purchase_product)) : ?>
|
|
<?php foreach ($purchase_product as $purchasechild) :
|
|
if ($purchasechild['isactive'] == 1) : ?>
|
|
<tr>
|
|
<td style="width:10%;">
|
|
|
|
<select class="form-control book-select SelExample purchase_order_product_edit" name="item_details[]" required data-toggle="select2" id="" style="width: 280px !important;" readonly>
|
|
<option value="">Select a Product</option>
|
|
<?php foreach ($product as $value) : ?>
|
|
<?php
|
|
$value_name = $value["product_name"];
|
|
$value_manufacturer_name = (!empty($value["manufacturer_name"]) ? ' - ' . $value["manufacturer_name"] : '') ;
|
|
if ($value['per'] == 'ml') {
|
|
$value_ml = ' ('.$value['ml'].$value['per'].')';
|
|
} else {
|
|
$value_ml = '';
|
|
}
|
|
?>
|
|
<?php if ((int)$value["isactive"] === 1) : ?>
|
|
<option value="<?= $value["product_id"] ?>" <?= isset($purchasechild['product_id']) && $purchasechild['product_id'] == $value["product_id"] ? 'selected' : '' ?>>
|
|
<?= $value_name . $value_ml . $value_manufacturer_name ?>
|
|
</option>
|
|
<?php endif; ?>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</td>
|
|
<!-- <?php // if(!isset($reorder)) { ?> -->
|
|
<td class="row-original-qty d-none" style="width:5%;">
|
|
<?php echo $purchasechild['qty']; ?>
|
|
</td>
|
|
<!-- <?php // }else{ ?> -->
|
|
<td class="row-qty" style="width:5%;">
|
|
<input type="number" class="form-control item-quantity" name="quantity[]" min="0" value="<?= isset($purchasechild['received_qty']) && $purchasechild['received_qty'] != null ? ( (!isset($reorder) || $purchasechild['is_selected'] == 0) ? $purchasechild['received_qty'] : $purchasechild['qty'] - $purchasechild['received_qty'] ) : $purchasechild['qty'] ?>" >
|
|
</td>
|
|
<!-- <?php // } ?> -->
|
|
<!-- <?php // if(!isset($reorder) && $purchase['purchase_order_id'] > 0) { ?> -->
|
|
<td class="row-rcvd-qty d-none" style="width:5%;">
|
|
<input type="number" class="form-control item-quantity" name="quantity[]" min="0" value="<?= isset($purchasechild['received_qty']) && $purchasechild['received_qty'] != null ? ( (!isset($reorder) || $purchasechild['is_selected'] == 0) ? $purchasechild['received_qty'] : $purchasechild['qty'] - $purchasechild['received_qty'] ) : $purchasechild['qty'] ?>" <?= (!isset($reorder) && $purchase['purchase_order_id'] > 0) ? 'max="' . $purchasechild['qty'] . '"' : '' ?> >
|
|
</td>
|
|
<!-- <?php // } ?> -->
|
|
|
|
<!-- <?php // if(!isset($reorder)) { ?> -->
|
|
<td class="row-received d-none" style="width:2%;">
|
|
<input type="hidden" name="selected_items[]" value="<?= (isset($purchasechild['is_selected']) && $purchasechild['is_selected'] == 1) ? 'checked' : 'unchecked' ?>"> <!-- Hidden input for unchecked checkboxes -->
|
|
<input type="checkbox" class="form-control-check-input received-product large-checkbox" <?php echo (isset($purchasechild['is_selected']) && $purchasechild['is_selected'] == 1) ? 'checked' : ''; ?> >
|
|
</td>
|
|
<td class="row-amount d-none" style="width:5%;">
|
|
<input type="number" class="form-control item-quantity" name="amount[]" value="<?= isset($purchasechild['amount']) ? $purchasechild['amount'] : '' ?>">
|
|
</td>
|
|
<!-- <?php // }?> -->
|
|
<td hidden><input type="hidden" value="<?= isset($purchasechild['purchase_order_child_id']) ? $purchasechild['purchase_order_child_id'] : '' ?>"name="purchase_order_child_id[]"></td>
|
|
<td style="width:5%;">
|
|
<center><i class="fa fa-trash remove-item"></i></center>
|
|
</td>
|
|
</tr>
|
|
<?php endif; ?>
|
|
<?php endforeach; ?>
|
|
<?php endif; ?>
|
|
<!-- You can add more rows as needed using JavaScript -->
|
|
</tbody>
|
|
</table>
|
|
<?php if (empty($purchase["status"]) || isset($reorder)): ?>
|
|
<div class="form-group text-right m-b-0">
|
|
<!-- Show the "Add More Item" button only if invoice_type is not 1 -->
|
|
<a class="btn" id="addItem">
|
|
<h4><i class="fa fa-plus" aria-hidden="true"></i> Add Product</h4>
|
|
</a>
|
|
</div>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- <h4 class="header-title">Terms & Condition :</h4><br>
|
|
<div class="form-row">
|
|
<div class="form-group col-md-12">
|
|
<label for="inputPassword4" class="col-form-label">Terms & Condition</label>
|
|
<textarea class="form-control" name="terms_condition" placeholder="Terms & Condition" ><?= isset($sales['terms_condition']) ? $sales['terms_condition'] : 'Please check the products properly before purchase. Products once sold cannot be returned.' ?></textarea>
|
|
</div>
|
|
</div> -->
|
|
<?php if (!isset($purchase['status']) || $purchase['status'] !== 'Received' || isset($reorder)) : ?>
|
|
<button type="submit" id="editaddbutton"class="btn btn-primary" style="float:right;">Submit</button>
|
|
<?php endif; ?>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<?php include('layout/footer.php'); ?>
|
|
</div>
|
|
|
|
<script>
|
|
|
|
productarray = [];
|
|
$(document).ready(function() {
|
|
var value = "<?php echo isset($purchase['status']) ? $purchase['status'] : ''; ?>";
|
|
if (value) {
|
|
console.log(value);
|
|
hideandshow(value);
|
|
}
|
|
|
|
|
|
// Function to fetch products based on vendor ID
|
|
function fetchVendorProducts(vendorId) {
|
|
if (vendorId !== '') {
|
|
// Perform AJAX request to fetch product information
|
|
$.ajax({
|
|
url: '<?php echo base_url().'getVendorProducts'?>', // Replace with the actual backend endpoint
|
|
method: 'POST',
|
|
dataType: 'json',
|
|
data: {
|
|
vendor_id: vendorId
|
|
},
|
|
success: function(response) {
|
|
console.log(response);
|
|
// Assuming response contains product data
|
|
// You can handle product data as per your requirements
|
|
// For example, updating UI or populating a dropdown with products
|
|
// Here, I'm just logging the product array to the console
|
|
productarray = response.product;
|
|
$('#contact_person_name').val(response.VendorData.contact_person_name1);
|
|
$('#contact_person_mobile').val(response.VendorData.contact_person_mobile1);
|
|
console.log(productarray);
|
|
},
|
|
error: function(xhr, status, error) {
|
|
// Handle error
|
|
console.error('Error fetching product information:', error);
|
|
}
|
|
});
|
|
} else {
|
|
console.error('Vendor ID not found');
|
|
}
|
|
}
|
|
|
|
// Event listener for vendor selection change
|
|
$('.vendor-select').change(function() {
|
|
var purchase_order_id = <?php echo $purchase_order_id; ?>;
|
|
var vendorId = $(this).val();
|
|
fetchVendorProducts(vendorId);
|
|
if(purchase_order_id == 0){
|
|
$('tbody').empty();
|
|
}
|
|
|
|
});
|
|
|
|
$(document).on('change', 'select[name="purchase_status"]', function() {
|
|
var status = $(this).val();
|
|
console.log('Status selected : ', status);
|
|
hideandshow(status); // Call the function with selected status
|
|
});
|
|
|
|
function hideandshow(status) {
|
|
console.log('Function Status : ', status);
|
|
if (status === "Received") {
|
|
console.log('if selected:', status);
|
|
$('.header-original-qty, .header-rcvd-qty, .header-received, .header-amount').removeClass('d-none');
|
|
$('.row-original-qty, .row-rcvd-qty, .row-received, .row-amount').removeClass('d-none');
|
|
$('.header-qty').addClass('d-none');
|
|
$('.row-qty').addClass('d-none');
|
|
|
|
}else{
|
|
console.log('else selected:', status);
|
|
$('.header-qty').removeClass('d-none');
|
|
$('.row-qty').removeClass('d-none');
|
|
$('.header-original-qty, .header-rcvd-qty, .header-received, .header-amount').addClass('d-none');
|
|
$('.row-original-qty, .row-rcvd-qty, .row-received, .row-amount').addClass('d-none');
|
|
}
|
|
}
|
|
|
|
// Edit scenario: Populate products based on stored vendor ID
|
|
var storedVendorId = "<?php echo isset($purchase['vendor_id']) ? $purchase['vendor_id'] : ''; ?>";
|
|
if (storedVendorId !== '') {
|
|
// Trigger change event to fetch products based on stored vendor ID
|
|
$('#vendor_id').val(storedVendorId).change();
|
|
}
|
|
});
|
|
|
|
<?php
|
|
$product_json = json_encode($product);
|
|
?>
|
|
<?php
|
|
$productAll_json = json_encode($productAll);
|
|
?>
|
|
var productAll = JSON.parse(<?php echo $productAll_json; ?>);
|
|
|
|
|
|
|
|
$(document).ready(function() {
|
|
function validateProductAdded() {
|
|
var rowCount = $('#itemTable tbody tr').length;
|
|
if (rowCount < 1) {
|
|
toastr.warning("Please add at least one product.");
|
|
// alert("Please add at least one product.");
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
// Event listener for form submission
|
|
$('form').submit(function() {
|
|
return validateProductAdded();
|
|
});
|
|
|
|
var products = <?php echo $product_json; ?>;
|
|
|
|
// Function to calculate amount based on rate and quantity
|
|
function calculateAmount(row) {
|
|
var rate = $(row).find('.item-rate').val();
|
|
var quantity = $(row).find('.item-quantity').val();
|
|
var amount = rate * quantity;
|
|
$(row).find('.item-amount').val(amount);
|
|
|
|
// Retrieve CGST and SGST values from the database for the selected product
|
|
var productId = $(row).find('select[name="item_details[]"]').val();
|
|
var product = products.find(function(item) {
|
|
return item.product_id == productId;
|
|
});
|
|
var cgst = parseFloat(product.cgst);
|
|
var sgst = parseFloat(product.sgst);
|
|
|
|
// Calculate total tax by adding CGST and SGST
|
|
var tax = cgst + sgst;
|
|
$(row).find('.item-tax').val(tax.toFixed(2));
|
|
}
|
|
|
|
// Event listener for product selection
|
|
$(document).on('change', 'select[name="item_details[]"]', function() {
|
|
var productId = $(this).val();
|
|
var quantity = $(this).closest('tr').find('.item-quantity').val(1);
|
|
|
|
|
|
console.log(productAll);
|
|
var product = productAll.find(function(item) {
|
|
return item.product_id == productId;
|
|
});
|
|
console.log(product);
|
|
if (product) {
|
|
|
|
// Access unit price from product and set it as rate
|
|
var unitPrice = parseFloat(product.unit_price); // Convert to float if necessary
|
|
$(this).closest('tr').find('.item-rate').val(unitPrice);
|
|
$(quantity).attr('max', product.qty_stock);
|
|
// Calculate amount and update amount input field
|
|
calculateAmount($(this).closest('tr'));
|
|
}
|
|
});
|
|
|
|
// Event listener for quantity change
|
|
$(document).on('input', '.item-quantity', function() {
|
|
calculateAmount($(this).closest('tr'));
|
|
updateCalculations(); // Added for updating calculations when quantity changes
|
|
});
|
|
|
|
// Function to calculate subtotal
|
|
function calculateSubtotal() {
|
|
var subtotal = 0;
|
|
$('.item-amount').each(function() {
|
|
subtotal += parseFloat($(this).val()) || 0;
|
|
});
|
|
return subtotal;
|
|
}
|
|
|
|
// Function to calculate total tax
|
|
function calculateTax() {
|
|
|
|
var tax = 0;
|
|
$('.item-tax').each(function() {
|
|
var taxPercentage = parseFloat($(this).val()) || 0;
|
|
var amount = parseFloat($(this).closest('tr').find('.item-amount').val()) || 0;
|
|
var taxValue = (taxPercentage / 100) * amount; // Convert percentage to absolute value
|
|
tax += taxValue;
|
|
});
|
|
return tax;
|
|
}
|
|
|
|
|
|
|
|
// Function to calculate total discount
|
|
function calculateDiscount() {
|
|
var totalDiscount = 0;
|
|
$('.item-discount-amount').each(function(index, element) {
|
|
var discountAmount = parseFloat($(element).val()) || 0;
|
|
var discountType = $(element).closest('tr').find('.item-discount-type').val();
|
|
if (discountType === '%') {
|
|
var rate = $(element).closest('tr').find('.item-rate').val();
|
|
var quantity = $(element).closest('tr').find('.item-quantity').val();
|
|
var amount = rate * quantity;
|
|
discountAmount = amount * discountAmount / 100;
|
|
}
|
|
totalDiscount += discountAmount;
|
|
});
|
|
return totalDiscount;
|
|
}
|
|
|
|
// Function to calculate total
|
|
function calculateTotal() {
|
|
var subtotal = calculateSubtotal();
|
|
var tax = calculateTax();
|
|
var discount = calculateDiscount();
|
|
var total = subtotal + tax - discount;
|
|
// return total;
|
|
return Math.round(total);
|
|
}
|
|
|
|
// Update subtotal, tax, and total
|
|
function updateCalculations() {
|
|
$('#subtotal').val(calculateSubtotal().toFixed(2));
|
|
$('#invoicetax').val(calculateTax().toFixed(2));
|
|
$('#discount').val(calculateDiscount().toFixed(2));
|
|
$('#grandtotal').val(calculateTotal().toFixed(2));
|
|
}
|
|
|
|
// Event listener for discount change
|
|
$(document).on('input', '.item-discount-amount, .item-discount-type', function() {
|
|
updateCalculations();
|
|
});
|
|
|
|
// Calculate initial values on page load
|
|
updateCalculations();
|
|
|
|
// Event listener for "Add More Item" button
|
|
$('#addItem').click(function() {
|
|
if ($('#vendor_id').val() != 0) {
|
|
console.log(productarray);
|
|
|
|
var valuesArray = $('.purchase_order_product').map(function() {
|
|
return $(this).val();
|
|
}).get();
|
|
|
|
console.log(valuesArray);
|
|
var newRow = '<tr>' + '<td><select class="form-control book-select SelExample purchase_order_product" name="item_details[]" required data-toggle="select2" style="width: 280px !important;"><option value="">Select a Product</option>';
|
|
|
|
for (var i = 0; i < productarray.length; i++)
|
|
{
|
|
var product = productarray[i];
|
|
|
|
var value_ml ='';
|
|
if (product.per == 'ml') {
|
|
value_ml = "("+product.ml+product.per+")";
|
|
}
|
|
if (valuesArray.indexOf(product.product_id.toString()) === -1) {
|
|
|
|
newRow += '<option value="' + product.product_id + '">' + product.product_name + value_ml +
|
|
(product.manufacturer_name ? ' - ' + product.manufacturer_name : '') +
|
|
'</option>';
|
|
}
|
|
}
|
|
newRow += '</select></td>' +
|
|
'<td style="width:10%;"><input type="number" class="form-control item-quantity" min="0" name="quantity[]"/></td>' +
|
|
|
|
|
|
'<td><center><i class="fa fa-trash remove-item"></i></center></td>' +
|
|
'</tr>';
|
|
|
|
|
|
$('#itemTable tbody').append(newRow);
|
|
initializeSelect2();
|
|
|
|
}else{
|
|
|
|
}
|
|
});
|
|
|
|
// Event listener for removing item
|
|
$(document).on('click', '.remove-item', function() {
|
|
$(this).closest('tr').remove();
|
|
updateCalculations(); // Added for updating calculations when an item is removed
|
|
});
|
|
});
|
|
|
|
|
|
|
|
</script>
|
|
<script>
|
|
// Validate form before submission
|
|
$('form').submit(function() {
|
|
// var status = $('select[name="purchase_status"]').val();
|
|
// if (status === "") {
|
|
// alert("Please select a status.");
|
|
// return false; // Prevent form submission
|
|
// }
|
|
// If all validations pass, allow form submission
|
|
return true;
|
|
});
|
|
</script>
|
|
<script>
|
|
// Event listener for removing item
|
|
// Event listener for removing item
|
|
$(document).on('click', '.remove-item', function() {
|
|
var purchase_order_child_id = $(this).closest('tr').find('input[name="purchase_order_child_id[]"]').val();
|
|
|
|
// AJAX request to delete the sales order product
|
|
$.ajax({
|
|
url: '<?php echo base_url()."delete_purchase_product"?>',
|
|
method: 'POST',
|
|
data: { purchase_order_child_id: purchase_order_child_id },
|
|
success: function(response) {
|
|
// Check if the deletion was successful
|
|
if (response.success) {
|
|
// If successful, remove the row from the table
|
|
$(this).closest('tr').remove();
|
|
updateCalculations(); // Update calculations after removing the row
|
|
} else {
|
|
// If not successful, display an error message
|
|
// alert('Error occurred while deleting the item.');
|
|
}
|
|
},
|
|
error: function(xhr, status, error) {
|
|
console.error('Error occurred while deleting the item:', error);
|
|
}
|
|
});
|
|
});
|
|
$(document).ready(function() {
|
|
// Event listener for customer selection change
|
|
$('.vendor-select').change(function() {
|
|
console.log(vendors);
|
|
var vendorId = $(this).val();
|
|
if (vendorId !== '') {
|
|
// Find the selected client in the client data
|
|
var selctedVendor = vendors.find(function(vendor) {
|
|
return vendor.vendor_id == vendorId;
|
|
});
|
|
|
|
|
|
|
|
$('input[name="contact_person_name"]').val(selctedVendor.contact_person_name1);
|
|
$('input[name="contact_person_mobile"]').val(selctedVendor.contact_person_mobile1);
|
|
|
|
}
|
|
});
|
|
});
|
|
|
|
</script>
|
|
|
|
<script>
|
|
// Define the clients array and populate it with data
|
|
var vendors = <?php echo json_encode($vendor); ?>;
|
|
|
|
</script>
|
|
<script>
|
|
function onlyNumbers(event){
|
|
var charcode;
|
|
charcode = event.which || event.keyCode;
|
|
if(charcode>= 48 && charcode <= 57)return true;
|
|
return false;
|
|
}
|
|
</script>
|
|
|
|
<link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-beta.1/dist/css/select2.min.css" rel="stylesheet">
|
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/js/select2.min.js"></script>
|
|
|
|
<script>
|
|
$(document).ready(function(){
|
|
// Initialize select2
|
|
$(".SelExample").select2();
|
|
});
|
|
function initializeSelect2() {
|
|
$('.SelExample').select2({
|
|
width: '249px' // Adjust width as needed
|
|
});
|
|
}
|
|
</script>
|
|
|
|
<style>
|
|
.select2-container--default .select2-results__option--highlighted[aria-selected]{
|
|
color:#ffffff;
|
|
background: #526dee!important;
|
|
}
|
|
.select2-container .select2-selection--single{
|
|
height: 36px;
|
|
border:1px solid #ced4da;
|
|
}
|
|
.select2-container--default .select2-selection--single .select2-selection__rendered{
|
|
line-height: 36px;
|
|
}
|
|
.select2-container--default .select2-selection--single .select2-selection__arrow{
|
|
top:4px;
|
|
}
|
|
|
|
</style>
|
|
<script>
|
|
$('#editaddbutton').click(function(event) {
|
|
|
|
|
|
if ($('#purchase_order_id').val()) {
|
|
$('#vendor_id').prop('disabled', false);
|
|
|
|
var isAnyChecked = false;
|
|
$('.received-product').each(function() {
|
|
if ($(this).is(':checked')) {
|
|
isAnyChecked = true;
|
|
return false;
|
|
}
|
|
});
|
|
|
|
var someCondition = !isAnyChecked;
|
|
|
|
if (someCondition) {
|
|
event.preventDefault();
|
|
toastr.warning("Please Check Received Product.");
|
|
}
|
|
}
|
|
});
|
|
|
|
|
|
|
|
</script>
|
|
|
|
|
|
<script>
|
|
$(document).ready(function() {
|
|
// Attach change event listener to checkboxes
|
|
$('.form-control-check-input').change(function() {
|
|
// Check if checkbox is checked
|
|
if ($(this).is(':checked')) {
|
|
$(this).prev('input[type="hidden"]').val('checked'); // Set value to '1'
|
|
} else {
|
|
$(this).prev('input[type="hidden"]').val('unchecked'); // Set value to '0'
|
|
}
|
|
});
|
|
|
|
|
|
$(".purchase_order_product_edit").next(".select2-container").css({
|
|
'pointer-events': 'none',
|
|
});
|
|
// $('#select2-insurer-container').css({
|
|
// 'background-color': '#ebebe0',
|
|
// });
|
|
});
|
|
// Prevent multiple submissions by disabling the button on first click
|
|
document.querySelector('#purchaseForm').addEventListener('submit', function (e) {
|
|
const submitButton = this.querySelector('button[type="submit"]');
|
|
submitButton.disabled = true; // Disable the button
|
|
submitButton.textContent = 'Processing...'; // Optional: Show loading text
|
|
});
|
|
|
|
|
|
|
|
|
|
</script>
|