2026-02-02 13:27:35 +00:00

488 lines
20 KiB
HTML

{% extends 'base.html' %}
{% load i18n static %}
{% block title %}{% trans "POS" %} | {{ site_settings.business_name }}{% endblock %}
{% block head %}
<style>
.product-card {
cursor: pointer;
transition: transform 0.2s, box-shadow 0.2s;
border: none;
border-radius: 15px;
}
.product-card:hover {
transform: translateY(-5px);
box-shadow: 0 10px 20px rgba(0,0,0,0.1) !important;
}
.cart-container {
position: sticky;
top: 80px;
height: calc(100vh - 120px);
display: flex;
flex-direction: column;
}
.cart-items {
flex-grow: 1;
overflow-y: auto;
}
.category-badge {
cursor: pointer;
padding: 8px 15px;
border-radius: 20px;
margin-right: 5px;
margin-bottom: 5px;
display: inline-block;
background: #f8f9fa;
color: #6c757d;
transition: all 0.2s;
}
.category-badge.active {
background: var(--bs-primary);
color: white;
}
/* Invoice Print Styles */
@media print {
body * {
visibility: hidden;
}
#invoice-print, #invoice-print * {
visibility: visible !important;
}
#invoice-print {
display: block !important;
position: fixed;
left: 0;
top: 0;
width: 80mm;
padding: 5mm;
margin: 0;
font-family: 'Courier New', Courier, monospace;
font-size: 11px;
color: black;
background: white;
z-index: 9999;
}
.no-print {
display: none !important;
}
#sidebar, .top-navbar, .btn, .modal {
display: none !important;
}
main {
padding: 0 !important;
margin: 0 !important;
}
}
#invoice-print {
display: none;
width: 80mm;
margin: auto;
border: 1px solid #eee;
padding: 5mm;
background: white;
}
.invoice-header { text-align: center; margin-bottom: 10px; border-bottom: 1px dashed #000; padding-bottom: 10px; }
.invoice-title { font-size: 16px; font-weight: bold; margin-bottom: 5px; }
.invoice-details { margin-bottom: 10px; font-size: 10px; }
.invoice-table { width: 100%; border-collapse: collapse; margin-bottom: 10px; }
.invoice-table th { border-bottom: 1px solid #000; text-align: left; padding: 2px 0; font-size: 10px; }
.invoice-table td { padding: 4px 0; font-size: 10px; vertical-align: top; }
.invoice-total { border-top: 1px dashed #000; padding-top: 5px; }
.bilingual { display: flex; justify-content: space-between; font-size: 9px; color: #555; }
.rtl { direction: rtl; text-align: right; }
.ltr { direction: ltr; text-align: left; }
</style>
{% endblock %}
{% block content %}
<div class="container-fluid px-4 no-print">
<div class="row g-4">
<!-- Products Section -->
<div class="col-lg-8">
<div class="d-flex justify-content-between align-items-center mb-4">
<h4 class="fw-bold mb-0">{% trans "Point of Sale" %}</h4>
<div class="input-group w-50">
<span class="input-group-text bg-white border-end-0"><i class="bi bi-search"></i></span>
<input type="text" id="productSearch" class="form-control border-start-0" placeholder="{% trans 'Search products...' %}">
</div>
</div>
<div class="mb-4">
<div class="category-badge active" data-category="all">{% trans "All" %}</div>
{% for category in categories %}
<div class="category-badge" data-category="{{ category.id }}">
{% if LANGUAGE_CODE == 'ar' %}{{ category.name_ar }}{% else %}{{ category.name_en }}{% endif %}
</div>
{% endfor %}
</div>
<div class="row row-cols-1 row-cols-md-3 g-3" id="productGrid">
{% for product in products %}
<div class="col product-item" data-category="{{ product.category.id }}" data-name-en="{{ product.name_en|lower }}" data-name-ar="{{ product.name_ar }}">
<div class="card h-100 shadow-sm product-card p-2" onclick="addToCart({{ product.id }}, '{{ product.name_en|escapejs }}', '{{ product.name_ar|escapejs }}', {{ product.price }})">
{% if product.image %}
<img src="{{ product.image.url }}" class="card-img-top rounded-3" alt="{{ product.name_en }}" style="height: 150px; object-fit: cover;">
{% else %}
<div class="bg-light rounded-3 d-flex align-items-center justify-content-center" style="height: 150px;">
<i class="bi bi-image text-muted opacity-25" style="font-size: 3rem;"></i>
</div>
{% endif %}
<div class="card-body p-2 text-center">
<h6 class="fw-bold mb-1">
{% if LANGUAGE_CODE == 'ar' %}{{ product.name_ar }}{% else %}{{ product.name_en }}{% endif %}
</h6>
<p class="text-primary fw-bold mb-0">{{ site_settings.currency_symbol }}{{ product.price|floatformat:3 }}</p>
<small class="text-muted">{% trans "Stock" %}: {{ product.stock_quantity }}</small>
</div>
</div>
</div>
{% endfor %}
</div>
</div>
<!-- Cart Section -->
<div class="col-lg-4">
<div class="card border-0 shadow-sm rounded-4 cart-container">
<div class="card-header bg-white border-0 pt-4 px-4">
<h5 class="fw-bold">{% trans "Current Order" %}</h5>
<select id="customerSelect" class="form-select form-select-sm mt-3">
<option value="">{% trans "Walking Customer" %}</option>
{% for customer in customers %}
<option value="{{ customer.id }}">{{ customer.name }}</option>
{% endfor %}
</select>
</div>
<div class="card-body px-4 py-2 cart-items">
<div id="cartItemsList">
<!-- Cart items will be injected here -->
</div>
<div class="text-center py-5 text-muted opacity-50" id="emptyCartMsg">
<i class="bi bi-cart3 display-1 d-block mb-3"></i>
{% trans "Your cart is empty" %}
</div>
</div>
<div class="card-footer bg-light border-0 p-4 rounded-bottom-4">
<div class="d-flex justify-content-between mb-2">
<span>{% trans "Subtotal" %}</span>
<span id="subtotalAmount">{{ site_settings.currency_symbol }}0.000</span>
</div>
<div class="d-flex justify-content-between mb-3">
<span class="fw-bold fs-5">{% trans "Total" %}</span>
<span class="fw-bold fs-5 text-primary" id="totalAmount">{{ site_settings.currency_symbol }}0.000</span>
</div>
<div class="mb-3">
<label class="small text-muted mb-1">{% trans "Payment Method" %}</label>
<select id="paymentMethodSelect" class="form-select shadow-none">
{% for method in payment_methods %}
<option value="{{ method.id }}">{% if LANGUAGE_CODE == 'ar' %}{{ method.name_ar }}{% else %}{{ method.name_en }}{% endif %}</option>
{% endfor %}
</select>
</div>
<button id="payNowBtn" class="btn btn-primary w-100 py-3 fw-bold rounded-3" onclick="checkout()" disabled>
{% trans "PAY NOW" %}
</button>
</div>
</div>
</div>
</div>
</div>
<!-- Invoice Template (Hidden) -->
<div id="invoice-print">
<div class="invoice-header">
<div id="inv-logo-container" class="mb-2">
<img id="inv-logo" src="" alt="Logo" style="max-height: 50px; display: none;">
</div>
<div class="invoice-title" id="inv-business-name"></div>
<div class="bilingual"><span class="ltr">TAX INVOICE</span><span class="rtl">فاتورة ضريبية</span></div>
<div id="inv-business-address" style="font-size: 10px;"></div>
<div id="inv-business-phone" style="font-size: 10px;"></div>
<div id="inv-business-email" style="font-size: 10px;"></div>
<div class="mt-1" style="border-top: 1px solid #eee; padding-top: 2px;">
<div class="bilingual">
<span>VAT No / الرقم الضريبي:</span>
<span id="inv-vat-no"></span>
</div>
<div class="bilingual">
<span>CR No / السجل التجاري:</span>
<span id="inv-cr-no"></span>
</div>
</div>
</div>
<div class="invoice-details">
<div class="d-flex justify-content-between">
<span>Inv #: <span id="inv-id"></span></span>
<span class="rtl">رقم الفاتورة: <span id="inv-id-ar"></span></span>
</div>
<div class="d-flex justify-content-between">
<span>Date: <span id="inv-date"></span></span>
<span class="rtl">التاريخ: <span id="inv-date-ar"></span></span>
</div>
</div>
<table class="invoice-table">
<thead>
<tr>
<th>Item / الصنف</th>
<th style="text-align: center;">Qty</th>
<th style="text-align: right;">Total</th>
</tr>
</thead>
<tbody id="inv-items">
<!-- Items injected here -->
</tbody>
</table>
<div class="invoice-total">
<div class="d-flex justify-content-between fw-bold">
<span>TOTAL / المجموع</span>
<span id="inv-total"></span>
</div>
</div>
<div class="text-center mt-3" style="font-size: 10px; border-top: 1px dashed #000; padding-top: 5px;">
THANK YOU / شكراً لزيارتكم
</div>
</div>
<!-- Receipt Modal -->
<div class="modal fade no-print" id="receiptModal" tabindex="-1">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-body text-center p-4">
<i class="bi bi-check-circle-fill text-success display-1 mb-3"></i>
<h4 class="fw-bold">{% trans "Success!" %}</h4>
<p class="text-muted">{% trans "Transaction completed." %}</p>
<div class="d-grid gap-2">
<button type="button" class="btn btn-primary" onclick="printInvoice()">
<i class="bi bi-printer me-2"></i> {% trans "Print Invoice" %}
</button>
<button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal">{% trans "Close" %}</button>
</div>
</div>
</div>
</div>
</div>
{% endblock %}
{% block scripts %}
<script>
let cart = [];
let lastSaleData = null;
const lang = '{{ LANGUAGE_CODE }}';
const currency = '{{ site_settings.currency_symbol }}';
const decimalPlaces = currency === 'OMR' ? 3 : 2;
function formatAmount(amount) {
return parseFloat(amount).toFixed(decimalPlaces);
}
function addToCart(id, nameEn, nameAr, price) {
const existing = cart.find(item => item.id === id);
if (existing) {
existing.quantity += 1;
existing.line_total = existing.quantity * price;
} else {
cart.push({
id,
name: lang === 'ar' ? nameAr : nameEn,
name_en: nameEn,
name_ar: nameAr,
price,
quantity: 1,
line_total: price
});
}
renderCart();
}
function updateQuantity(id, delta) {
const item = cart.find(item => item.id === id);
if (item) {
item.quantity += delta;
if (item.quantity <= 0) {
cart = cart.filter(i => i.id !== id);
} else {
item.line_total = item.quantity * item.price;
}
}
renderCart();
}
function renderCart() {
const listContainer = document.getElementById('cartItemsList');
const emptyMsg = document.getElementById('emptyCartMsg');
const payBtn = document.getElementById('payNowBtn');
if (cart.length === 0) {
emptyMsg.classList.remove('d-none');
listContainer.innerHTML = '';
document.getElementById('subtotalAmount').innerText = `${currency} ${formatAmount(0)}`;
document.getElementById('totalAmount').innerText = `${currency} ${formatAmount(0)}`;
payBtn.disabled = true;
return;
}
emptyMsg.classList.add('d-none');
payBtn.disabled = false;
let html = '';
let total = 0;
cart.forEach(item => {
total += item.line_total;
html += `
<div class="d-flex justify-content-between align-items-center mb-3">
<div>
<div class="fw-bold small">${item.name}</div>
<div class="text-muted small">${currency} ${formatAmount(item.price)} x ${item.quantity}</div>
</div>
<div class="d-flex align-items-center gap-2">
<button class="btn btn-sm btn-outline-secondary rounded-circle" onclick="updateQuantity(${item.id}, -1)">-</button>
<span class="fw-bold">${item.quantity}</span>
<button class="btn btn-sm btn-outline-secondary rounded-circle" onclick="updateQuantity(${item.id}, 1)">+</button>
</div>
</div>
`;
});
listContainer.innerHTML = html;
document.getElementById('subtotalAmount').innerText = `${currency} ${formatAmount(total)}`;
document.getElementById('totalAmount').innerText = `${currency} ${formatAmount(total)}`;
}
function checkout() {
if (cart.length === 0) return;
const payBtn = document.getElementById('payNowBtn');
const originalText = payBtn.innerText;
payBtn.disabled = true;
payBtn.innerText = '{% trans "Processing..." %}';
const totalAmount = cart.reduce((acc, item) => acc + item.line_total, 0);
const data = {
customer_id: document.getElementById('customerSelect').value,
payment_method_id: document.getElementById('paymentMethodSelect').value,
items: cart,
total_amount: totalAmount,
paid_amount: totalAmount,
discount: 0,
payment_type: 'cash'
};
fetch('{% url "create_sale_api" %}', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-CSRFToken': '{{ csrf_token }}'
},
body: JSON.stringify(data)
})
.then(response => {
if (!response.ok) {
return response.json().then(err => { throw err; });
}
return response.json();
})
.then(data => {
if (data.success) {
lastSaleData = data;
prepareInvoice(data);
cart = [];
renderCart();
const receiptModal = new bootstrap.Modal(document.getElementById('receiptModal'));
receiptModal.show();
} else {
alert('Error: ' + data.error);
}
})
.catch(error => {
console.error('Checkout error:', error);
alert('Checkout failed: ' + (error.error || error.message || 'Unknown error'));
})
.finally(() => {
payBtn.disabled = cart.length === 0;
payBtn.innerText = originalText;
});
}
function prepareInvoice(data) {
console.log('Preparing invoice with data:', data);
const logo = document.getElementById('inv-logo');
if (data.business.logo_url) {
logo.src = data.business.logo_url;
logo.style.display = 'inline-block';
} else {
logo.style.display = 'none';
}
document.getElementById('inv-business-name').innerText = data.business.name || 'Business Name';
document.getElementById('inv-business-address').innerText = data.business.address || '';
document.getElementById('inv-business-phone').innerText = data.business.phone ? 'Tel: ' + data.business.phone : '';
document.getElementById('inv-business-email').innerText = data.business.email ? 'Email: ' + data.business.email : '';
document.getElementById('inv-vat-no').innerText = data.business.vat_number || '---';
document.getElementById('inv-cr-no').innerText = data.business.registration_number || '---';
document.getElementById('inv-id').innerText = data.sale.id;
document.getElementById('inv-id-ar').innerText = data.sale.id;
document.getElementById('inv-date').innerText = data.sale.created_at;
document.getElementById('inv-date-ar').innerText = data.sale.created_at;
let itemsHtml = '';
data.sale.items.forEach(item => {
itemsHtml += `
<tr>
<td>
<div>${item.name_en}</div>
<div class="rtl text-muted" style="font-size: 9px;">${item.name_ar}</div>
</td>
<td style="text-align: center;">${item.qty}</td>
<td style="text-align: right;">${data.business.currency} ${formatAmount(item.total)}</td>
</tr>
`;
});
document.getElementById('inv-items').innerHTML = itemsHtml;
document.getElementById('inv-total').innerText = data.business.currency + ' ' + formatAmount(data.sale.total);
}
function printInvoice() {
console.log('Printing invoice...');
window.print();
}
// Search and Category Filtering
document.getElementById('productSearch').addEventListener('input', filterProducts);
document.querySelectorAll('.category-badge').forEach(badge => {
badge.addEventListener('click', function() {
document.querySelectorAll('.category-badge').forEach(b => b.classList.remove('active'));
this.classList.add('active');
filterProducts();
});
});
function filterProducts() {
const searchTerm = document.getElementById('productSearch').value.toLowerCase();
const activeCategory = document.querySelector('.category-badge.active').dataset.category;
document.querySelectorAll('.product-item').forEach(item => {
const nameEn = item.dataset.nameEn;
const nameAr = item.dataset.nameAr;
const category = item.dataset.category;
const matchesSearch = nameEn.includes(searchTerm) || nameAr.includes(searchTerm);
const matchesCategory = activeCategory === 'all' || category === activeCategory;
if (matchesSearch && matchesCategory) {
item.classList.remove('d-none');
} else {
item.classList.add('d-none');
}
});
}
</script>
{% endblock %}