You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
`;
}
}
function updateWarehouseSelect() {
const select = document.getElementById('warehouseSelect');
select.innerHTML = '';
Object.values(warehouses).forEach(wh => {
const option = document.createElement('option');
option.value = wh.id;
option.textContent = `${wh.name} (${wh.code})`;
if (wh.id === currentWarehouseId) option.selected = true;
select.appendChild(option);
});
}
function switchWarehouse() {
currentWarehouseId = document.getElementById('warehouseSelect').value;
renderWarehouseInventory();
updateCurrentWarehouseInfo();
}
function showAddWarehouse() {
editingWarehouseId = null;
document.getElementById('warehouseFormTitle').textContent = 'បន្ថែមឃ្លាំង/កម្មវិធីថ្មី';
document.getElementById('warehouseName').value = '';
document.getElementById('warehouseCode').value = '';
document.getElementById('warehouseFormContainer').classList.remove('hidden');
}
function editWarehouse() {
const wh = warehouses[currentWarehouseId];
if (wh) {
editingWarehouseId = currentWarehouseId;
document.getElementById('warehouseFormTitle').textContent = 'កែប្រែឃ្លាំង/កម្មវិធី';
document.getElementById('warehouseName').value = wh.name;
document.getElementById('warehouseCode').value = wh.code;
document.getElementById('warehouseFormContainer').classList.remove('hidden');
}
}
function cancelWarehouse() {
document.getElementById('warehouseFormContainer').classList.add('hidden');
editingWarehouseId = null;
}
function submitWarehouse() {
const name = document.getElementById('warehouseName').value.trim();
const code = document.getElementById('warehouseCode').value.trim();
if (!name || !code) {
alert('សូមបញ្ចូលព័ត៌មានពេញលេញ!');
return;
}
if (editingWarehouseId) {
warehouses[editingWarehouseId].name = name;
warehouses[editingWarehouseId].code = code;
} else {
const newId = 'wh' + Date.now();
warehouses[newId] = { id: newId, name: name, code: code, items: [] };
currentWarehouseId = newId;
}
updateWarehouseSelect();
updateCurrentWarehouseInfo();
cancelWarehouse();
}
function deleteWarehouse() {
if (Object.keys(warehouses).length <= 1) {
alert('មិនអាចលុបបានទេ! ត្រូវមានឃ្លាំងយ៉ាងតិច១។');
return;
}
const wh = warehouses[currentWarehouseId];
if (confirm('តើអ្នកប្រាកដទេថាចង់លុបឃ្លាំង "' + wh.name + '"?')) {
delete warehouses[currentWarehouseId];
currentWarehouseId = Object.keys(warehouses)[0];
updateWarehouseSelect();
renderWarehouseInventory();
updateCurrentWarehouseInfo();
}
}
function getCurrentWarehouseData() {
return warehouses[currentWarehouseId]?.items || [];
}
function setCurrentWarehouseData(data) {
if (warehouses[currentWarehouseId]) {
warehouses[currentWarehouseId].items = data;
}
}
function saveWarehouseData() {
localStorage.setItem('warehousesData', JSON.stringify(warehouses));
alert('បានរក្សាទុកទិន្នន័យ!');
}
function renderWarehouseInventory() {
const searchTerm = document.getElementById('whSearchInput').value.toLowerCase();
const data = getCurrentWarehouseData();
let filtered = data.filter(item => item.description.toLowerCase().includes(searchTerm));
let html = '';
let totals = { openingQty: 0, openingTotal: 0, inQty: 0, inTotal: 0, outQty: 0, outTotal: 0 };
filtered.forEach((item, index) => {
totals.openingQty += item.opening.qty;
totals.openingTotal += item.opening.qty * parseFloat(item.opening.price.replace(/,/g, ''));
totals.inQty += item.in.qty;
totals.inTotal += item.in.qty * parseFloat(item.in.price.replace(/,/g, ''));
totals.outQty += item.out.qty;
totals.outTotal += item.out.qty * parseFloat(item.out.price.replace(/,/g, ''));
html += '';
html += '' + (index + 1) + '';
html += '' + item.description + '';
html += '' + item.unit + '';
html += '' + item.opening.qty + '';
html += '' + item.opening.price + '';
html += '' + (item.opening.qty * parseFloat(item.opening.price.replace(/,/g, ''))).toLocaleString() + '';
html += '' + item.in.qty + '';
html += '' + item.in.price + '';
html += '' + (item.in.qty * parseFloat(item.in.price.replace(/,/g, ''))).toLocaleString() + '';
html += '' + item.out.qty + '';
html += '' + item.out.price + '';
html += '' + (item.out.qty * parseFloat(item.out.price.replace(/,/g, ''))).toLocaleString() + '';
html += '';
html += '✏️';
html += '🗑️';
html += '';
});
html += '';
html += 'សរុបទាំងអស់:';
html += '' + totals.openingQty + '';
html += '';
html += '' + totals.openingTotal.toLocaleString() + '';
html += '' + totals.inQty + '';
html += '';
html += '' + totals.inTotal.toLocaleString() + '';
html += '' + totals.outQty + '';
html += '';
html += '' + totals.outTotal.toLocaleString() + '';
html += '';
document.getElementById('warehouseInventoryBody').innerHTML = html;
}
function filterWarehouseData() {
renderWarehouseInventory();
}
function showWarehouseAddForm() {
editingWarehouseItemId = null;
document.getElementById('warehouseItemFormTitle').textContent = 'បន្ថែមសម្ភារថ្មី';
clearWarehouseItemForm();
document.getElementById('warehouseAddFormContainer').classList.remove('hidden');
}
function cancelWarehouseItemForm() {
document.getElementById('warehouseAddFormContainer').classList.add('hidden');
clearWarehouseItemForm();
editingWarehouseItemId = null;
}
function clearWarehouseItemForm() {
document.getElementById('whInputDescription').value = '';
document.getElementById('whInputUnit').value = '';
['Opening', 'In', 'Out'].forEach(type => {
document.getElementById('whInput' + type + 'Qty').value = 0;
document.getElementById('whInput' + type + 'Price').value = 0;
document.getElementById('whInput' + type + 'Total').value = 0;
});
}
function submitWarehouseItemForm() {
const formData = {
description: document.getElementById('whInputDescription').value,
unit: document.getElementById('whInputUnit').value,
opening: {
qty: parseInt(document.getElementById('whInputOpeningQty').value) || 0,
price: document.getElementById('whInputOpeningPrice').value || '0'
},
in: {
qty: parseInt(document.getElementById('whInputInQty').value) || 0,
price: document.getElementById('whInputInPrice').value || '0'
},
out: {
qty: parseInt(document.getElementById('whInputOutQty').value) || 0,
price: document.getElementById('whInputOutPrice').value || '0'
}
};
if (!formData.description) {
alert('សូមបញ្ចូលសម្ភារៈបរិក្ខារ!');
return;
}
let data = getCurrentWarehouseData();
if (editingWarehouseItemId) {
data = data.map(item => item.id === editingWarehouseItemId ? {...formData, id: editingWarehouseItemId} : item);
} else {
const newId = Date.now();
data.push({...formData, id: newId});
}
setCurrentWarehouseData(data);
renderWarehouseInventory();
updateCurrentWarehouseInfo();
cancelWarehouseItemForm();
}
function editWarehouseItem(id) {
const data = getCurrentWarehouseData();
const item = data.find(i => i.id === id);
if (item) {
editingWarehouseItemId = id;
document.getElementById('warehouseItemFormTitle').textContent = 'កែប្រែសម្ភារ';
document.getElementById('whInputDescription').value = item.description;
document.getElementById('whInputUnit').value = item.unit;
document.getElementById('whInputOpeningQty').value = item.opening.qty;
document.getElementById('whInputOpeningPrice').value = item.opening.price;
document.getElementById('whInputInQty').value = item.in.qty;
document.getElementById('whInputInPrice').value = item.in.price;
document.getElementById('whInputOutQty').value = item.out.qty;
document.getElementById('whInputOutPrice').value = item.out.price;
calculateWarehouseTotal('Opening');
calculateWarehouseTotal('In');
calculateWarehouseTotal('Out');
document.getElementById('warehouseAddFormContainer').classList.remove('hidden');
}
}
function deleteWarehouseItem(id) {
if (confirm('តើអ្នកប្រាកដទេថាចង់លុប?')) {
let data = getCurrentWarehouseData();
data = data.filter(item => item.id !== id);
setCurrentWarehouseData(data);
renderWarehouseInventory();
updateCurrentWarehouseInfo();
}
}
function downloadWarehouseJSON() {
const dataStr = JSON.stringify(warehouses, null, 2);
const dataBlob = new Blob([dataStr], {type: 'application/json'});
const url = URL.createObjectURL(dataBlob);
const link = document.createElement('a');
link.href = url;
link.download = 'warehouses_all_data.json';
link.click();
}
function downloadWarehouseCSV() {
const wh = warehouses[currentWarehouseId];
const data = getCurrentWarehouseData();
const headers = ['ល.រ', 'សម្ភារៈបរិក្ខារ', 'ឯកតាគិត', 'ដើមឆ្នាំ-ចំនួន', 'ដើមឆ្នាំ-តម្លៃរាយ', 'ដើមឆ្នាំ-តម្លៃសរុប', 'ចូល-ចំនួន', 'ចូល-តម្លៃរាយ', 'ចូល-តម្លៃសរុប', 'ចេញ-ចំនួន', 'ចេញ-តម្លៃរាយ', 'ចេញ-តម្លៃសរុប', 'កម្មវិធី', 'លេខកូដ'];
let csv = '\uFEFF' + headers.join(',') + '\n';
data.forEach((item, idx) => {
const openingTotal = item.opening.qty * parseFloat(item.opening.price.replace(/,/g, ''));
const inTotal = item.in.qty * parseFloat(item.in.price.replace(/,/g, ''));
const outTotal = item.out.qty * parseFloat(item.out.price.replace(/,/g, ''));
const row = [idx + 1, '"' + item.description + '"', '"' + item.unit + '"', item.opening.qty, item.opening.price, openingTotal, item.in.qty, item.in.price, inTotal, item.out.qty, item.out.price, outTotal, '"' + wh.name + '"', wh.code];
csv += row.join(',') + '\n';
});
const blob = new Blob([csv], {type: 'text/csv;charset=utf-8;'});
const link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = wh.code + '_' + wh.name.substring(0, 20) + '.csv';
link.click();
}
function importWarehouseJSON(event) {
const file = event.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = function(e) {
try {
warehouses = JSON.parse(e.target.result);
currentWarehouseId = Object.keys(warehouses)[0];
updateWarehouseSelect();
renderWarehouseInventory();
updateCurrentWarehouseInfo();
alert('បាននាំចូលទិន្នន័យដោយជោគជ័យ!');
} catch (error) {
alert('មានបញ្ហាក្នុងការនាំចូលទិន្នន័យ!');
}
};
reader.readAsText(file);
}
}
// ========== SIMPLE SYSTEM ==========
function initSimpleSystem() {
const saved = localStorage.getItem('simpleEquipmentData');
simpleEquipmentData = saved ? JSON.parse(saved) : defaultSimpleData;
updateSimpleYearFilter();
renderSimpleInventory();
}
function saveSimpleData() {
localStorage.setItem('simpleEquipmentData', JSON.stringify(simpleEquipmentData));
alert('បានរក្សាទុកទិន្នន័យ!');
}
function updateSimpleYearFilter() {
const years = [...new Set(simpleEquipmentData.map(item => item.year))].sort((a, b) => b - a);
const select = document.getElementById('simpleYearFilter');
select.innerHTML = 'ឆ្នាំទាំងអស់';
years.forEach(year => {
select.innerHTML += '' + year + '';
});
}
function getStatusClass(status) {
if (status === 'ល្អ') return 'bg-green-100 text-green-800';
if (status === 'មធ្យម') return 'bg-yellow-100 text-yellow-800';
if (status === 'អន់') return 'bg-orange-100 text-orange-800';
return 'bg-red-100 text-red-800';
}
function renderSimpleInventory() {
const searchTerm = document.getElementById('simpleSearchInput').value.toLowerCase();
const yearFilter = document.getElementById('simpleYearFilter').value;
let filtered = simpleEquipmentData.filter(item => {
const matchesSearch = item.description.toLowerCase().includes(searchTerm) || item.user.toLowerCase().includes(searchTerm);
const matchesYear = yearFilter === 'all' || item.year.toString() === yearFilter;
return matchesSearch && matchesYear;
});
let html = '';
let totalQty = 0;
let totalPrice = 0;
filtered.forEach((item, index) => {
totalQty += item.quantity;
totalPrice += parseInt(item.price.replace(/,/g, ''));
html += '';
html += '' + (index + 1) + '';
html += '' + item.type + '';
html += '' + item.description + '';
html += '' + item.year + '';
html += '' + item.user + '';
html += '' + item.quantity + '';
html += '' + item.price + '';
html += '' + item.status + '';
html += '';
html += '✏️';
html += '🗑️';
html += '';
});
html += '';
html += 'សរុប:';
html += '' + totalQty + '';
html += '' + totalPrice.toLocaleString() + '';
html += '';
document.getElementById('simpleInventoryBody').innerHTML = html;
}
function filterSimpleData() {
renderSimpleInventory();
}
function showSimpleAddForm() {
editingSimpleId = null;
document.getElementById('simpleFormTitle').textContent = 'បន្ថែមថ្មី';
clearSimpleForm();
document.getElementById('simpleAddFormContainer').classList.remove('hidden');
}
function cancelSimpleForm() {
document.getElementById('simpleAddFormContainer').classList.add('hidden');
clearSimpleForm();
editingSimpleId = null;
}
function clearSimpleForm() {
document.getElementById('simpleInputType').value = 'MOB';
document.getElementById('simpleInputDescription').value = '';
document.getElementById('simpleInputYear').value = new Date().getFullYear();
document.getElementById('simpleInputUser').value = '';
document.getElementById('simpleInputQuantity').value = 1;
document.getElementById('simpleInputPrice').value = '';
document.getElementById('simpleInputStatus').value = 'ល្អ';
}
function submitSimpleForm() {
const formData = {
type: document.getElementById('simpleInputType').value,
description: document.getElementById('simpleInputDescription').value,
year: parseInt(document.getElementById('simpleInputYear').value),
user: document.getElementById('simpleInputUser').value,
quantity: parseInt(document.getElementById('simpleInputQuantity').value),
price: document.getElementById('simpleInputPrice').value,
status: document.getElementById('simpleInputStatus').value
};
if (editingSimpleId) {
simpleEquipmentData = simpleEquipmentData.map(item => item.id === editingSimpleId ? {...formData, id: editingSimpleId} : item);
} else {
const newId = Math.max(...simpleEquipmentData.map(i => i.id), 0) + 1;
simpleEquipmentData.push({...formData, id: newId});
}
updateSimpleYearFilter();
renderSimpleInventory();
cancelSimpleForm();
}
function editSimpleItem(id) {
const item = simpleEquipmentData.find(i => i.id === id);
if (item) {
editingSimpleId = id;
document.getElementById('simpleFormTitle').textContent = 'កែប្រែ';
document.getElementById('simpleInputType').value = item.type;
document.getElementById('simpleInputDescription').value = item.description;
document.getElementById('simpleInputYear').value = item.year;
document.getElementById('simpleInputUser').value = item.user;
document.getElementById('simpleInputQuantity').value = item.quantity;
document.getElementById('simpleInputPrice').value = item.price;
document.getElementById('simpleInputStatus').value = item.status;
document.getElementById('simpleAddFormContainer').classList.remove('hidden');
}
}
function deleteSimpleItem(id) {
if (confirm('តើអ្នកប្រាកដទេថាចង់លុប?')) {
simpleEquipmentData = simpleEquipmentData.filter(item => item.id !== id);
updateSimpleYearFilter();
renderSimpleInventory();
}
}
function downloadSimpleJSON() {
const dataStr = JSON.stringify(simpleEquipmentData, null, 2);
const dataBlob = new Blob([dataStr], {type: 'application/json'});
const url = URL.createObjectURL(dataBlob);
const link = document.createElement('a');
link.href = url;
link.download = 'simple_equipment_data.json';
link.click();
}
function downloadSimpleCSV() {
const headers = ['ល.រ', 'តាមប្រភេទ', 'បរិយាយ', 'ប្រើប្រាស់ពីឆ្នាំ', 'ឈ្មោះអ្នកប្រើ', 'បរិមាណ', 'តម្លៃ(រៀល)', 'ស្ថានភាព'];
let csv = '\uFEFF' + headers.join(',') + '\n';
simpleEquipmentData.forEach((item, idx) => {
const row = [idx + 1, item.type, '"' + item.description + '"', item.year, '"' + item.user + '"', item.quantity, item.price, item.status];
csv += row.join(',') + '\n';
});
const blob = new Blob([csv], {type: 'text/csv;charset=utf-8;'});
const link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = 'simple_equipment_data.csv';
link.click();
}
function importSimpleJSON(event) {
const file = event.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = function(e) {
try {
simpleEquipmentData = JSON.parse(e.target.result);
updateSimpleYearFilter();
renderSimpleInventory();
alert('បាននាំចូលទិន្នន័យដោយជោគជ័យ!');
} catch (error) {
alert('មានបញ្ហាក្នុងការនាំចូលទិន្នន័យ!');
}
};
reader.readAsText(file);
}
}
function switchSimpleTab(tab) {
document.getElementById('simpleInventoryTab').classList.add('hidden');
document.getElementById('simpleYearlyTab').classList.add('hidden');
document.getElementById('simpleTrackingTab').classList.add('hidden');
document.getElementById('simple-tab-inventory').className = 'px-6 py-4 font-semibold text-gray-600 hover:bg-gray-50';
document.getElementById('simple-tab-yearly').className = 'px-6 py-4 font-semibold text-gray-600 hover:bg-gray-50';
document.getElementById('simple-tab-tracking').className = 'px-6 py-4 font-semibold text-gray-600 hover:bg-gray-50';
document.getElementById('simple' + tab.charAt(0).toUpperCase() + tab.slice(1) + 'Tab').classList.remove('hidden');
document.getElementById('simple-tab-' + tab).className = 'px-6 py-4 font-semibold bg-indigo-600 text-white';
if (tab === 'yearly') renderSimpleYearly();
if (tab === 'tracking') renderSimpleTracking();
}
function renderSimpleYearly() {
const yearlyData = {};
simpleEquipmentData.forEach(item => {
if (!yearlyData[item.year]) yearlyData[item.year] = [];
yearlyData[item.year].push(item);
});
const years = Object.keys(yearlyData).sort((a, b) => b - a);
let html = '
សម្ភារកើនក្នុងឆ្នាំ
';
years.forEach(year => {
const items = yearlyData[year];
let totalQty = 0;
let totalPrice = 0;
html += '
';
html += '
ឆ្នាំ ' + year + '
';
html += '
';
html += '';
html += '';
html += '';
html += '';
html += '';
html += '';
html += '';
html += '';
html += '';
items.forEach((item, idx) => {
totalQty += item.quantity;
totalPrice += parseInt(item.price.replace(/,/g, ''));
html += '';
html += '';
html += '';
html += '';
html += '';
html += '';
html += '';
html += '';
});
html += '';
html += '';
html += '';
html += '';
html += '';
html += '
ល.រ
បរិយាយ
ប្រភេទ
បរិមាណ
តម្លៃ(រៀល)
ស្ថានភាព
' + (idx + 1) + '
' + item.description + '
' + item.type + '
' + item.quantity + '
' + item.price + '
' + item.status + '
សរុប:
' + totalQty + '
' + totalPrice.toLocaleString() + '
';
});
html += '
';
document.getElementById('simpleYearlyTab').innerHTML = html;
}
function renderSimpleTracking() {
const summary = {};
simpleEquipmentData.forEach(item => {
if (!summary[item.description]) {
summary[item.description] = {good: 0, medium: 0, weak: 0, broken: 0, total: 0};
}
summary[item.description].total += item.quantity;
if (item.status === 'ល្អ') summary[item.description].good += item.quantity;
else if (item.status === 'មធ្យម') summary[item.description].medium += item.quantity;
else if (item.status === 'អន់') summary[item.description].weak += item.quantity;
else if (item.status === 'ខូច') summary[item.description].broken += item.quantity;
});
let html = '
តារាងតាមដានស្ថានភាពសម្ភារៈ
';
html += '
';
html += '';
html += '';
html += '';
html += '';
html += '';
html += '';
html += '';
html += '';
html += '';
html += '';
html += '';
html += '';
html += '';
html += '';
html += '';
html += '';
html += '';
let idx = 0;
let totalGood = 0, totalMedium = 0, totalWeak = 0, totalBroken = 0, grandTotal = 0;
for (const [desc, counts] of Object.entries(summary)) {
idx++;
totalGood += counts.good;
totalMedium += counts.medium;
totalWeak += counts.weak;
totalBroken += counts.broken;
grandTotal += counts.total;
const statusText = counts.broken > 0 ? 'ខូច' : counts.weak > 0 ? 'អន់' : counts.medium > 0 ? 'មធ្យម' : 'ល្អ';
html += '';
html += '';
html += '';
html += '';
html += '';
html += '';
html += '';
html += '';
html += '';
html += '';
html += '';
html += '';
}
html += '';
html += '';
html += '';
html += '';
html += '';
html += '';
html += '';
html += '';
html += '