File size: 6,135 Bytes
6b154f5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
import * as XLSX from 'xlsx';
import { Product } from '../types';
export const readExcelFile = (file: File): Promise<Product[]> => {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = (e) => {
try {
const data = new Uint8Array(e.target?.result as ArrayBuffer);
const workbook = XLSX.read(data, { type: 'array' });
const sheetName = workbook.SheetNames[0];
const worksheet = workbook.Sheets[sheetName];
// Convert to JSON with header row
const jsonData = XLSX.utils.sheet_to_json(worksheet, { header: 1 });
if (jsonData.length < 2) {
reject(new Error('Excel file must contain at least one data row'));
return;
}
// Parse the data into Product objects
const products: Product[] = [];
const headers = jsonData[0] as string[];
for (let i = 1; i < jsonData.length; i++) {
const row = jsonData[i] as any[];
if (row.some(cell => cell !== undefined && cell !== '')) {
const product: Product = {
id: `product-${Date.now()}-${i}`,
nameAr: row[0] || 'منتج غير محدد',
nameEn: row[1] || 'Unnamed Product',
quantity: Number(row[2]) || 0,
price: Number(row[3]) || 0,
categoryAr: row[4] || 'عام',
categoryEn: row[5] || 'General',
description: row[6] || '',
secoCode: row[7] || ''
};
products.push(product);
}
}
resolve(products);
} catch (error) {
reject(error);
}
};
reader.onerror = () => reject(new Error('Failed to read file'));
reader.readAsArrayBuffer(file);
});
};
export const downloadExcelFile = (products: Product[], filename: string = 'inventory') => {
// Get all unique dates from all products
const allDates = new Set<string>();
products.forEach(product => {
if (product.inventoryDates) {
Object.keys(product.inventoryDates).forEach(date => allDates.add(date));
}
});
const sortedDates = Array.from(allDates).sort();
// Create worksheet data
const headers = [
'المورد', 'Supplier', 'اسم المنتج', 'Product Name',
...sortedDates,
'المجموع', 'Total', 'المباع', 'Sold', 'المتبقي', 'Remaining',
'الوصف', 'Description', 'كود SECO', 'SECO Code'
];
const wsData = [
headers,
...products.map(product => {
const row = [
product.supplierAr || 'مورد غير محدد',
product.supplier || 'Unknown Supplier',
product.nameAr,
product.nameEn || product.nameAr
];
// Add inventory quantities for each date
sortedDates.forEach(date => {
row.push(product.inventoryDates?.[date] || 0);
});
// Add totals
row.push(
product.totalReceived || 0,
product.totalReceived || 0,
product.totalSold || 0,
product.totalSold || 0,
product.currentStock || product.quantity,
product.currentStock || product.quantity,
product.description || '',
product.description || '',
product.secoCode || '',
product.secoCode || ''
);
return row;
})
];
// Create workbook and worksheet
const wb = XLSX.utils.book_new();
const ws = XLSX.utils.aoa_to_sheet(wsData);
// Set column widths
const colWidths = headers.map((header, index) => {
if (header.includes('وصف') || header.includes('Description')) return { wch: 25 };
if (header.includes('منتج') || header.includes('Product') || header.includes('مورد') || header.includes('Supplier')) return { wch: 20 };
if (header.includes('/') || header.match(/\d/)) return { wch: 12 }; // Date columns
return { wch: 15 };
});
ws['!cols'] = colWidths;
// Add worksheet to workbook
XLSX.utils.book_append_sheet(wb, ws, 'المخزون - Inventory');
// Generate and download file
const timestamp = new Date().toISOString().split('T')[0];
XLSX.writeFile(wb, `${filename}_${timestamp}.xlsx`);
};
export const createSampleData = (): Product[] => {
return [
{
id: 'sample-1',
supplierAr: 'شركة التقنية المتقدمة',
supplier: 'Advanced Tech Company',
nameAr: 'لابتوب ديل',
nameEn: 'Dell Laptop',
quantity: 15,
currentStock: 15,
totalReceived: 20,
totalSold: 5,
price: 2500,
categoryAr: 'إلكترونيات',
categoryEn: 'Electronics',
description: 'لابتوب عالي الأداء',
secoCode: 'ELEC001',
importDate: '2025-01-01',
inventoryDates: {
'27/8/2025': 10,
'8/4/2025': 5,
'7/31/2025': 5
}
},
{
id: 'sample-2',
supplierAr: 'مؤسسة الملحقات الذكية',
supplier: 'Smart Accessories Corp',
nameAr: 'ماوس لاسلكي',
nameEn: 'Wireless Mouse',
quantity: 50,
currentStock: 50,
totalReceived: 60,
totalSold: 10,
price: 75,
categoryAr: 'ملحقات',
categoryEn: 'Accessories',
description: 'ماوس لاسلكي مريح',
secoCode: 'ACC002',
importDate: '2025-01-15',
inventoryDates: {
'7/26/2025': 30,
'27/8/2025': 20,
'8/6/2025': 10
}
},
{
id: 'sample-3',
supplierAr: 'متجر الألعاب الإلكترونية',
supplier: 'Gaming Electronics Store',
nameAr: 'كيبورد ميكانيكي',
nameEn: 'Mechanical Keyboard',
quantity: 25,
currentStock: 25,
totalReceived: 30,
totalSold: 5,
price: 150,
categoryAr: 'ملحقات',
categoryEn: 'Accessories',
description: 'كيبورد ميكانيكي للألعاب',
secoCode: 'ACC003',
importDate: '2025-02-01',
inventoryDates: {
'1/9/2025': 15,
'7/15/2025': 10,
'8/3/2025': 5
}
}
];
}; |