the_mechanic/app/Views/sales_list.php

280 lines
12 KiB
PHP
Executable File

<?php include('layout/header.php'); ?>
<style>
.dataTables_wrapper button{
background: #f1f5f7;
color: #343a40;
border-width: 0;
}
.dataTables_wrapper .dt-buttons{
float:left;
}
.cancelled-status {
color: red;
}
.created-status {
color: blue;
}
.paid-status {
color: green;
}
</style>
<div class="container-fluid">
<div class="row">
<div class="col-12">
<div class="page-title-box page-title-box-alt">
<h4 class="page-title">Sales Order List</h4>
<div class="page-title-right">
<ol class="breadcrumb m-0">
<li class=""> <a href="<?= base_url() . "new_sale/0"; ?>" class="btn btn-primary waves-effect"> Add Sale Order </a>
</li>
</ol>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-lg-12">
<div class="card">
<div class="card-body">
<!-- <h4 class="header-title">Business List</h4> -->
<table id="datatable-sales-order" class="table table-striped dt-responsive nowrap w-100"
style="width:100% !important;">
<thead>
<tr>
<th>Order Number</th>
<th>Reg No</th>
<th>Client Name</th>
<th>Items</th>
<th>Qty</th>
<th>Total</th>
<th>Status</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php
foreach ($sales as $index => $value) :
if ($value['isactive'] == 1) : ?>
<tr class="sale_tr_<?php echo $index+1; ?>">
<td><?= $value['order_number']; ?></td>
<td><?= $value['reg_no']; ?></td>
<td><?= $value['client_name']; ?></td>
<td><?= $value['product_count']; ?></td>
<td><?= $value['product_qty']; ?></td>
<td><?= $value['total']; ?></td>
<td class="<?php
if ($value['status'] == 'Cancelled') {
echo 'cancelled-status';
} elseif ($value['status'] == 'Created') {
echo 'created-status';
} elseif ($value['status'] == 'Paid') {
echo 'paid-status';
}
?>">
<?= $value['status']; ?>
</td>
<td>
<div class="btn-group dropdown">
<a href="javascript: void(0);" class="dropdown-toggle arrow-none btn btn-light btn-sm" data-toggle="dropdown" aria-expanded="false"><i class="mdi mdi-dots-horizontal"></i></a>
<div class="dropdown-menu dropdown-menu-right">
<a href="<?= "new_sale/" . $value['sales_order_id']; ?>" class="dropdown-item edit-button" onclick="datatableSalesOrder('sale_tr_<?php echo $index+1; ?>')"><i class="ri-pencil-line mr-2 text-muted font-18 vertical-middle"></i>Edit</a>
<a href="<?= "delete_sale/" . $value['sales_order_id']; ?>" class="dropdown-item delete-button" onclick="datatableSalesOrder('sale_tr_<?php echo $index+1; ?>')"><i class="ri-delete-bin-line mr-2 text-muted font-18 vertical-middle"></i>Delete</a>
<a href="<?= "download_invoice/" . $value['sales_order_id']; ?>" class="dropdown-item edit-button"><i class="ri-download-2-fill mr-2 text-muted font-18 vertical-middle"></i>Download</a>
<a href="#" data-sales-order-id="<?= $value['sales_order_id']; ?>" class="dropdown-item preview-invoice" href="#" title="Preview Invoice"><i class="ri-book-read-line mr-2 text-muted font-18 vertical-middle"></i>Preview Invoice</a>
</div>
</div>
</td>
<?php endif?>
<?php endforeach; ?>
</tr>
</tbody>
</table>
</div> <!-- end table-responsive-->
</div>
</div> <!-- end card -->
</div> <!-- end col -->
</div>
<?php include('layout/footer.php'); ?>
<div class="modal fade" id="PreviewInvoiceModal" tabindex="-1" role="dialog" aria-labelledby="PreviewInvoiceModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content" style="width: 794px; height: 1123px;">
<div class="modal-header">
<h5 class="modal-title" id="PreviewInvoiceModalLabel"></h5>
<a href="#" class="ri-download-2-fill ml-3 text-muted font-18" id="downloadInvoiceButton" title="Download Invoice"></a>
<a href="#" class="ri-printer-fill ml-3 text-muted font-18" id="printInvoiceButton" title="Print Invoice"></a>
<button type="button" class="close" data-dismiss="modal" aria-label="Close" style="font-size: 27px; margin-bottom: 2px;">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body" style="font-family: Times New Roman, Times, sans-serif !important; font-size: 12px !important;">
</div>
</div>
</div>
</div>
<script>
var datatable_sales_order ='';
$(document).ready(function() {
datatable_sales_order = $('#datatable-sales-order').DataTable({
"order": [[0, 'desc']], // Set initial sorting to descending on the first column
"dom": 'Bfrtip', // Show export buttons
"buttons": [{extend: 'pdfHtml5',title: 'Products', text: 'PDF',},
{extend: 'print',title: 'Products',text: 'Print',},
{extend: 'copy',text: 'Copy', titleAttr: 'Copy',},
{extend: 'csv',text: 'CSV', title: 'Products', }],
"language": {
"search": "Search: " // Customize search label
},
});
//Start DataTable Paging is session is set
var savedPage = sessionStorage.getItem('currentPage');
var hightlight_tr = sessionStorage.getItem('hightlight_tr');
// console.log(savedPage);
// console.log(hightlight_tr);
// Ensure savedPage is a number
savedPage = parseInt(savedPage);
// Check if savedPage is not null and adjust if necessary
if (savedPage !== null && !isNaN(savedPage)) {
// Check if the saved page number is within the bounds of the current DataTable
var totalPages = datatable_sales_order.page.info().pages; // Total number of pages in the DataTable
if (savedPage >= totalPages) {
// adjust to the last page of the DataTable
savedPage = totalPages > 0 ? totalPages - 1 : 0;
}
// Move DataTable to the saved page and draw
datatable_sales_order.page(savedPage).draw(false);
// Select the highlighted row
var $row = $('.' + hightlight_tr);
// Smoothly scroll to the row and center it in the viewport
$('html, body').animate({
scrollTop: $row.offset().top - ($(window).height() / 2) + ($row.height() / 2)
}, 500, function() {
// After focusing, change background color
$row.css('background', '#faf6ca');
// Remove background color after 4 seconds
setTimeout(function() {
$row.css('background', '');
}, 4000);
});
}
sessionStorage.removeItem('currentPage');
sessionStorage.removeItem('hightlight_tr');
//End DataTable Paging is session is set
})
function datatableSalesOrder(value) {
console.log(value);
var currentPage = datatable_sales_order.page.info().page;
sessionStorage.setItem('currentPage', currentPage);
sessionStorage.setItem('hightlight_tr', value);
}
$(document).on('click', '.preview-invoice', function() {
var Id = $(this).data('sales-order-id');
$.ajax({
type: 'GET',
url: 'preview_invoice/'+Id,
dataType: 'json',
success: function(response) {
if (response.status == "success") {
$('#PreviewInvoiceModal').modal('show');
$('#PreviewInvoiceModalLabel').text('Invoice Preview ');
$('#PreviewInvoiceModal .modal-body').html(response.data);
$('#downloadInvoiceButton').attr('href', '<?= base_url("download_invoice/") ?>' + Id);
$('#printInvoiceButton').attr('data-id', Id);
}else{
$('#PreviewInvoiceModal').modal('hide');
$('#PreviewInvoiceModalLabel').text('');
}
},
error: function(xhr, status, error) {
// Handle error response here
console.error(xhr.responseText);
$('#PreviewInvoiceModal').modal('hide');
$('#PreviewInvoiceModalLabel').text('');
}
});
});
$(document).on('click', '#printInvoiceButton', function() {
var Id = $(this).data('id');
$.ajax({
type: 'GET',
url: 'preview_invoice/'+Id,
dataType: 'json',
success: function(response) {
if (response.status == "success") {
var newWin = window.open("", "_blank", "width=600,height=400");
newWin.document.write("<html><head><title>Print</title>");
newWin.document.write(`
<style>
@media print {
@page {
size: A4; /* Set default print size to A4 */
margin: 1cm; /* Set margin as per requirement */
}
body {
font-family: Arial, sans-serif;
}
}
</style>
`);
newWin.document.write("</head><body>");
newWin.document.write(response.data);
newWin.document.write("</body></html>");
newWin.document.close();
newWin.print();
newWin.close();
}else{
$('#PreviewInvoiceModal').modal('hide');
$('#PreviewInvoiceModalLabel').text('');
}
},
error: function(xhr, status, error) {
// Handle error response here
console.error(xhr.responseText);
$('#PreviewInvoiceModal').modal('hide');
$('#PreviewInvoiceModalLabel').text('');
}
});
});
</script>