Spaces:
Running
Running
File size: 55,848 Bytes
8ac6f9c |
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 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 |
<!DOCTYPE html>
<html lang="uk">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Інвентаризація</title>
<script src="https://cdn.tailwindcss.com"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.18.5/xlsx.full.min.js"></script>
<style>
:root {
--color-match: #10b981;
--color-missing: #ef4444;
--color-excess: #f59e0b;
--color-unchecked: #9ca3af;
}
@media (prefers-color-scheme: dark) {
:root {
--color-bg: #1f2937;
--color-text: #f3f4f6;
--color-card: #374151;
}
}
@media (prefers-color-scheme: light) {
:root {
--color-bg: #f3f4f6;
--color-text: #111827;
--color-card: #ffffff;
}
}
body {
background-color: var(--color-bg);
color: var(--color-text);
min-height: 100vh;
padding-bottom: env(safe-area-inset-bottom);
}
.tab-content {
display: none;
}
.tab-content.active {
display: block;
}
.status-match {
background-color: var(--color-match);
}
.status-missing {
background-color: var(--color-missing);
}
.status-excess {
background-color: var(--color-excess);
}
.status-unchecked {
background-color: var(--color-unchecked);
}
.toast {
animation: fadeInOut 3s ease-in-out forwards;
}
@keyframes fadeInOut {
0% { opacity: 0; transform: translateY(20px); }
10% { opacity: 1; transform: translateY(0); }
90% { opacity: 1; transform: translateY(0); }
100% { opacity: 0; transform: translateY(-20px); }
}
.progress-bar {
height: 1rem;
border-radius: 0.5rem;
background-color: #e5e7eb;
overflow: hidden;
}
.progress-fill {
height: 100%;
background: linear-gradient(90deg, var(--color-match), var(--color-excess));
transition: width 0.3s ease;
}
input[type="number"]::-webkit-inner-spin-button,
input[type="number"]::-webkit-outer-spin-button {
-webkit-appearance: none;
margin: 0;
}
input[type="number"] {
-moz-appearance: textfield;
}
</style>
</head>
<body class="font-sans">
<div class="container mx-auto px-4 py-6 max-w-4xl">
<!-- Header -->
<header class="mb-8">
<h1 class="text-3xl font-bold text-center">Інвентаризація v1.0</h1>
</header>
<!-- Tabs Navigation -->
<div class="flex border-b border-gray-300 mb-6">
<button class="tab-btn py-2 px-4 font-medium border-b-2 border-transparent hover:border-gray-400 transition" data-tab="scan">Сканування</button>
<button class="tab-btn py-2 px-4 font-medium border-b-2 border-transparent hover:border-gray-400 transition" data-tab="list">Список товарів</button>
<button class="tab-btn py-2 px-4 font-medium border-b-2 border-transparent hover:border-gray-400 transition" data-tab="results">Результати</button>
</div>
<!-- Toast Notifications -->
<div id="toast-container" class="fixed bottom-4 right-4 z-50 space-y-2"></div>
<!-- Scan Tab Content -->
<div id="scan" class="tab-content active">
<div class="space-y-6">
<!-- Input Fields -->
<div class="space-y-4">
<div>
<label for="sku-input" class="block text-sm font-medium mb-1">Артикул товару</label>
<div class="flex">
<input type="text" id="sku-input" class="flex-1 px-4 py-2 border border-gray-300 rounded-l-lg focus:outline-none focus:ring-2 focus:ring-blue-500" placeholder="Введіть або відскануйте артикул" inputmode="numeric">
<button id="sku-submit" class="px-4 py-2 bg-blue-500 text-white rounded-r-lg hover:bg-blue-600 transition">➜</button>
</div>
</div>
<div>
<label for="quantity-input" class="block text-sm font-medium mb-1">Зміна кількості</label>
<div class="flex">
<input type="number" id="quantity-input" disabled class="flex-1 px-4 py-2 border border-gray-300 rounded-l-lg focus:outline-none focus:ring-2 focus:ring-blue-500" placeholder="Число(+) або 0+Число(-)">
<button id="quantity-submit" disabled class="px-4 py-2 bg-blue-500 text-white rounded-r-lg hover:bg-blue-600 transition">➜</button>
</div>
<p class="text-xs text-gray-500 mt-1">Введіть число для додавання або 0+число для віднімання (напр. 5 або 05)</p>
</div>
</div>
<!-- Item Card -->
<div id="item-card" class="bg-white/10 p-4 rounded-lg shadow hidden">
<div class="flex justify-between items-start mb-2">
<h3 class="text-lg font-semibold" id="item-name">Назва товару</h3>
<span id="item-status" class="px-2 py-1 rounded-full text-xs font-medium text-white">Не перевірено</span>
</div>
<div class="grid grid-cols-2 gap-4">
<div>
<p class="text-sm text-gray-400">Артикул</p>
<p id="item-sku" class="font-mono">123456789</p>
</div>
<div>
<p class="text-sm text-gray-400">Очікувано</p>
<p id="item-expected">0</p>
</div>
<div>
<p class="text-sm text-gray-400">Фактично</p>
<p id="item-actual">0</p>
</div>
<div>
<p class="text-sm text-gray-400">Різниця</p>
<p id="item-difference">0</p>
</div>
</div>
</div>
<!-- Action Buttons -->
<div class="flex flex-wrap gap-2 mt-6">
<button id="open-file-btn" class="px-4 py-2 bg-green-500 text-white rounded-lg hover:bg-green-600 transition flex items-center gap-2">
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M7 16a4 4 0 01-.88-7.903A5 5 0 1115.9 6L16 6a5 5 0 011 9.9M15 13l-3-3m0 0l-3 3m3-3v12" />
</svg>
Відкрити файл
</button>
<button id="save-results-btn" disabled class="px-4 py-2 bg-blue-500 text-white rounded-lg hover:bg-blue-600 transition flex items-center gap-2">
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7H5a2 2 0 00-2 2v9a2 2 0 002 2h14a2 2 0 002-2V9a2 2 0 00-2-2h-3m-1 4l-3 3m0 0l-3-3m3 3V4" />
</svg>
Зберегти інвентаризацію
</button>
<button id="clear-data-btn" class="px-4 py-2 bg-red-500 text-white rounded-lg hover:bg-red-600 transition flex items-center gap-2">
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16" />
</svg>
Очистити дані
</button>
</div>
<input type="file" id="file-input" accept=".xlsx,.xls" class="hidden">
</div>
</div>
<!-- List Tab Content -->
<div id="list" class="tab-content">
<div class="space-y-6">
<!-- File Info -->
<div id="file-info" class="bg-white/10 p-4 rounded-lg shadow hidden">
<h3 class="font-medium mb-2">Інформація про файл</h3>
<div class="grid grid-cols-2 gap-4">
<div>
<p class="text-sm text-gray-400">Назва файлу</p>
<p id="file-name">-</p>
</div>
<div>
<p class="text-sm text-gray-400">Дата завантаження</p>
<p id="file-date">-</p>
</div>
</div>
</div>
<!-- Progress Stats -->
<div class="bg-white/10 p-4 rounded-lg shadow">
<h3 class="font-medium mb-2">Прогрес інвентаризації</h3>
<div class="progress-bar mb-2">
<div id="progress-fill" class="progress-fill" style="width: 0%"></div>
</div>
<div class="grid grid-cols-2 md:grid-cols-5 gap-2 text-sm">
<div class="bg-gray-100/10 p-2 rounded text-center">
<p class="text-gray-400">Всього</p>
<p id="total-count" class="font-bold">0</p>
</div>
<div class="bg-gray-100/10 p-2 rounded text-center">
<p class="text-gray-400">Перевірено</p>
<p id="checked-count" class="font-bold">0</p>
</div>
<div class="bg-green-500/20 p-2 rounded text-center">
<p class="text-gray-400">Відповідність</p>
<p id="match-count" class="font-bold">0</p>
</div>
<div class="bg-red-500/20 p-2 rounded text-center">
<p class="text-gray-400">Нестача</p>
<p id="missing-count" class="font-bold">0</p>
</div>
<div class="bg-yellow-500/20 p-2 rounded text-center">
<p class="text-gray-400">Надлишок</p>
<p id="excess-count" class="font-bold">0</p>
</div>
</div>
</div>
<!-- Filters -->
<div class="grid grid-cols-1 md:grid-cols-2 gap-4">
<div>
<label for="status-filter" class="block text-sm font-medium mb-1">Фільтр за статусом</label>
<select id="status-filter" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500">
<option value="all">Всі</option>
<option value="match">Відповідність</option>
<option value="missing">Нестача</option>
<option value="excess">Надлишок</option>
<option value="unchecked">Не перевірено</option>
</select>
</div>
<div>
<label for="search-input" class="block text-sm font-medium mb-1">Пошук за назвою</label>
<div class="flex">
<input type="text" id="search-input" class="flex-1 px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500" placeholder="Введіть назву товару">
<button id="clear-search" class="ml-2 px-3 py-2 bg-gray-200 text-gray-700 rounded-lg hover:bg-gray-300 transition">
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12" />
</svg>
</button>
</div>
</div>
</div>
<!-- Items Table -->
<div class="overflow-x-auto">
<table id="items-table" class="w-full border-collapse">
<thead>
<tr class="border-b border-gray-300">
<th class="px-4 py-2 text-left">Статус</th>
<th class="px-4 py-2 text-left">Артикул</th>
<th class="px-4 py-2 text-left">Назва</th>
<th class="px-4 py-2 text-right">Очікувано</th>
<th class="px-4 py-2 text-right">Фактично</th>
</tr>
</thead>
<tbody id="items-table-body">
<!-- Items will be inserted here dynamically -->
</tbody>
</table>
</div>
<div id="no-items-message" class="text-center py-8 text-gray-500">
<p>Немає товарів для відображення. Завантажте файл з даними.</p>
</div>
</div>
</div>
<!-- Results Tab Content -->
<div id="results" class="tab-content">
<div class="space-y-6">
<div class="bg-white/10 p-4 rounded-lg shadow">
<h2 class="text-xl font-bold mb-2">Результати інвентаризації</h2>
<p id="results-summary" class="text-gray-400">Знайдено 0 розбіжностей</p>
</div>
<!-- Results Table -->
<div class="overflow-x-auto">
<table id="results-table" class="w-full border-collapse">
<thead>
<tr class="border-b border-gray-300">
<th class="px-4 py-2 text-left">Статус</th>
<th class="px-4 py-2 text-left">Артикул</th>
<th class="px-4 py-2 text-left">Назва</th>
<th class="px-4 py-2 text-right">Очікувано</th>
<th class="px-4 py-2 text-right">Фактично</th>
<th class="px-4 py-2 text-right">Різниця</th>
</tr>
</thead>
<tbody id="results-table-body">
<!-- Results will be inserted here dynamically -->
</tbody>
</table>
</div>
<div id="no-results-message" class="text-center py-8 text-gray-500">
<p>Немає розбіжностей для відображення.</p>
</div>
</div>
</div>
</div>
<script>
// Global variables
let items = [];
let currentItem = null;
let fileMetadata = {
fileName: null,
fileDate: null,
totalItems: 0
};
// DOM elements
const skuInput = document.getElementById('sku-input');
const skuSubmit = document.getElementById('sku-submit');
const quantityInput = document.getElementById('quantity-input');
const quantitySubmit = document.getElementById('quantity-submit');
const itemCard = document.getElementById('item-card');
const itemName = document.getElementById('item-name');
const itemSku = document.getElementById('item-sku');
const itemExpected = document.getElementById('item-expected');
const itemActual = document.getElementById('item-actual');
const itemDifference = document.getElementById('item-difference');
const itemStatus = document.getElementById('item-status');
const openFileBtn = document.getElementById('open-file-btn');
const fileInput = document.getElementById('file-input');
const saveResultsBtn = document.getElementById('save-results-btn');
const clearDataBtn = document.getElementById('clear-data-btn');
const fileInfo = document.getElementById('file-info');
const fileName = document.getElementById('file-name');
const fileDate = document.getElementById('file-date');
const progressFill = document.getElementById('progress-fill');
const totalCount = document.getElementById('total-count');
const checkedCount = document.getElementById('checked-count');
const matchCount = document.getElementById('match-count');
const missingCount = document.getElementById('missing-count');
const excessCount = document.getElementById('excess-count');
const statusFilter = document.getElementById('status-filter');
const searchInput = document.getElementById('search-input');
const clearSearch = document.getElementById('clear-search');
const itemsTableBody = document.getElementById('items-table-body');
const noItemsMessage = document.getElementById('no-items-message');
const resultsTableBody = document.getElementById('results-table-body');
const noResultsMessage = document.getElementById('no-results-message');
const resultsSummary = document.getElementById('results-summary');
const tabContents = document.querySelectorAll('.tab-content');
const tabButtons = document.querySelectorAll('.tab-btn');
const toastContainer = document.getElementById('toast-container');
// Initialize IndexedDB
let db;
const DB_NAME = 'inventoryDB';
const DB_VERSION = 1;
const ITEMS_STORE = 'items';
const METADATA_STORE = 'metadata';
function openDB() {
return new Promise((resolve, reject) => {
const request = indexedDB.open(DB_NAME, DB_VERSION);
request.onupgradeneeded = (event) => {
const db = event.target.result;
if (!db.objectStoreNames.contains(ITEMS_STORE)) {
db.createObjectStore(ITEMS_STORE, { keyPath: 'sku' });
}
if (!db.objectStoreNames.contains(METADATA_STORE)) {
db.createObjectStore(METADATA_STORE, { keyPath: 'id' });
}
};
request.onsuccess = (event) => {
db = event.target.result;
resolve(db);
};
request.onerror = (event) => {
console.error('Error opening DB', event.target.error);
reject(event.target.error);
};
});
}
function saveItemToDB(item) {
return new Promise((resolve, reject) => {
const transaction = db.transaction([ITEMS_STORE], 'readwrite');
const store = transaction.objectStore(ITEMS_STORE);
const request = store.put(item);
request.onsuccess = () => resolve();
request.onerror = (event) => reject(event.target.error);
});
}
function saveAllItemsToDB(items) {
return new Promise((resolve, reject) => {
const transaction = db.transaction([ITEMS_STORE], 'readwrite');
const store = transaction.objectStore(ITEMS_STORE);
// Clear existing items
const clearRequest = store.clear();
clearRequest.onsuccess = () => {
// Add all new items
const requests = items.map(item => {
return new Promise((res, rej) => {
const addRequest = store.add(item);
addRequest.onsuccess = () => res();
addRequest.onerror = (event) => rej(event.target.error);
});
});
Promise.all(requests)
.then(() => resolve())
.catch(error => reject(error));
};
clearRequest.onerror = (event) => reject(event.target.error);
});
}
function getAllItemsFromDB() {
return new Promise((resolve, reject) => {
const transaction = db.transaction([ITEMS_STORE], 'readonly');
const store = transaction.objectStore(ITEMS_STORE);
const request = store.getAll();
request.onsuccess = (event) => resolve(event.target.result || []);
request.onerror = (event) => reject(event.target.error);
});
}
function clearItemsDB() {
return new Promise((resolve, reject) => {
const transaction = db.transaction([ITEMS_STORE], 'readwrite');
const store = transaction.objectStore(ITEMS_STORE);
const request = store.clear();
request.onsuccess = () => resolve();
request.onerror = (event) => reject(event.target.error);
});
}
function saveMetadataToDB(metadata) {
return new Promise((resolve, reject) => {
const transaction = db.transaction([METADATA_STORE], 'readwrite');
const store = transaction.objectStore(METADATA_STORE);
const request = store.put({ id: 'current', ...metadata });
request.onsuccess = () => resolve();
request.onerror = (event) => reject(event.target.error);
});
}
function getMetadataFromDB() {
return new Promise((resolve, reject) => {
const transaction = db.transaction([METADATA_STORE], 'readonly');
const store = transaction.objectStore(METADATA_STORE);
const request = store.get('current');
request.onsuccess = (event) => resolve(event.target.result || null);
request.onerror = (event) => reject(event.target.error);
});
}
function clearMetadataDB() {
return new Promise((resolve, reject) => {
const transaction = db.transaction([METADATA_STORE], 'readwrite');
const store = transaction.objectStore(METADATA_STORE);
const request = store.clear();
request.onsuccess = () => resolve();
request.onerror = (event) => reject(event.target.error);
});
}
// Initialize the app
async function initializeApp() {
try {
await openDB();
// Load saved data
const savedItems = await getAllItemsFromDB();
const savedMetadata = await getMetadataFromDB();
if (savedItems.length > 0) {
items = savedItems;
fileMetadata = savedMetadata || fileMetadata;
updateUI();
showToast('Відновлено попередню сесію', 'success');
}
// Set up event listeners
setupEventListeners();
// Focus on SKU input
skuInput.focus();
} catch (error) {
console.error('Initialization error:', error);
showToast('Помилка ініціалізації додатку', 'error');
}
}
// Set up event listeners
function setupEventListeners() {
// Tab switching
tabButtons.forEach(button => {
button.addEventListener('click', () => {
const tabId = button.getAttribute('data-tab');
switchTab(tabId);
});
});
// SKU input handling
skuInput.addEventListener('keydown', (e) => {
if (e.key === 'Enter') {
handleSkuInput();
}
});
skuSubmit.addEventListener('click', handleSkuInput);
// Quantity input handling
quantityInput.addEventListener('keydown', (e) => {
if (e.key === 'Enter') {
handleQuantityInput();
}
});
quantitySubmit.addEventListener('click', handleQuantityInput);
// File handling
openFileBtn.addEventListener('click', () => fileInput.click());
fileInput.addEventListener('change', handleFileUpload);
// Save results
saveResultsBtn.addEventListener('click', saveResultsToFile);
// Clear data
clearDataBtn.addEventListener('click', confirmClearData);
// Filter and search
statusFilter.addEventListener('change', updateUI);
searchInput.addEventListener('input', updateUI);
clearSearch.addEventListener('click', () => {
searchInput.value = '';
updateUI();
});
// Click outside to hide keyboard (for mobile)
document.addEventListener('click', (e) => {
if (!['sku-input', 'quantity-input', 'search-input'].includes(e.target.id)) {
document.activeElement.blur();
}
});
}
// Switch between tabs
function switchTab(tabId) {
// Update active tab button
tabButtons.forEach(button => {
if (button.getAttribute('data-tab') === tabId) {
button.classList.add('border-blue-500', 'text-blue-500');
button.classList.remove('border-transparent');
} else {
button.classList.remove('border-blue-500', 'text-blue-500');
button.classList.add('border-transparent');
}
});
// Update active tab content
tabContents.forEach(content => {
if (content.id === tabId) {
content.classList.add('active');
} else {
content.classList.remove('active');
}
});
// Focus on appropriate input
if (tabId === 'scan') {
skuInput.focus();
} else if (tabId === 'list') {
searchInput.focus();
}
// Update UI for the new tab
updateUI();
}
// Handle SKU input
function handleSkuInput() {
const sku = skuInput.value.trim();
if (!sku) {
showToast('Введіть артикул товару', 'warning');
vibrate();
return;
}
// Find item by SKU
currentItem = items.find(item => item.sku === sku);
if (currentItem) {
// Item found - display it
displayItem(currentItem);
quantityInput.disabled = false;
quantityInput.focus();
showToast(`Знайдено товар: ${currentItem.name}`, 'success');
} else {
// Item not found - ask to create new
const createNew = confirm(`Товар з артикулом ${sku} не знайдено. Створити новий запис?`);
if (createNew) {
const name = prompt('Введіть назву товару:');
if (name) {
currentItem = {
sku,
name,
expectedQuantity: 0,
actualQuantity: null,
status: 'unchecked'
};
items.push(currentItem);
saveItemToDB(currentItem)
.then(() => {
displayItem(currentItem);
quantityInput.disabled = false;
quantityInput.focus();
updateUI();
showToast('Новий товар додано', 'success');
})
.catch(error => {
console.error('Error saving new item:', error);
showToast('Помилка збереження нового товару', 'error');
});
}
} else {
skuInput.focus();
}
}
skuInput.value = '';
}
// Display item in the card
function displayItem(item) {
itemName.textContent = item.name;
itemSku.textContent = item.sku;
itemExpected.textContent = item.expectedQuantity;
itemActual.textContent = item.actualQuantity !== null ? item.actualQuantity : '-';
const difference = item.actualQuantity !== null ?
item.actualQuantity - item.expectedQuantity :
0;
itemDifference.textContent = difference !== 0 ? difference : '-';
// Update status display
itemStatus.textContent = getStatusText(item.status);
itemStatus.className = 'px-2 py-1 rounded-full text-xs font-medium text-white';
itemStatus.classList.add(`status-${item.status}`);
// Show the card if hidden
itemCard.classList.remove('hidden');
}
// Handle quantity input
function handleQuantityInput() {
if (!currentItem) {
showToast('Спочатку знайдіть товар', 'warning');
vibrate();
return;
}
const quantityStr = quantityInput.value.trim();
if (!quantityStr) {
showToast('Введіть кількість', 'warning');
vibrate();
return;
}
let newQuantity;
// Check if it's a subtraction (starts with 0)
if (quantityStr.startsWith('0') && quantityStr.length > 1) {
const subtractValue = parseInt(quantityStr.substring(1), 10);
if (isNaN(subtractValue)) {
showToast('Невірний формат кількості', 'error');
vibrate();
return;
}
newQuantity = (currentItem.actualQuantity || 0) - subtractValue;
} else {
const addValue = parseInt(quantityStr, 10);
if (isNaN(addValue)) {
showToast('Невірний формат кількості', 'error');
vibrate();
return;
}
newQuantity = (currentItem.actualQuantity || 0) + addValue;
}
// Update item
currentItem.actualQuantity = newQuantity;
currentItem.status = calculateStatus(currentItem);
// Save to DB and update UI
saveItemToDB(currentItem)
.then(() => {
displayItem(currentItem);
quantityInput.value = '';
skuInput.focus();
updateUI();
showToast('Кількість оновлено', 'success');
})
.catch(error => {
console.error('Error updating item:', error);
showToast('Помилка оновлення кількості', 'error');
});
}
// Calculate item status
function calculateStatus(item) {
if (item.actualQuantity === null) {
return 'unchecked';
}
if (item.actualQuantity === item.expectedQuantity) {
return 'match';
} else if (item.actualQuantity < item.expectedQuantity) {
return 'missing';
} else {
return 'excess';
}
}
// Get status text
function getStatusText(status) {
const statusMap = {
'match': 'Відповідність',
'missing': 'Нестача',
'excess': 'Надлишок',
'unchecked': 'Не перевірено'
};
return statusMap[status] || status;
}
// Handle file upload
function handleFileUpload(event) {
const file = event.target.files[0];
if (!file) {
return;
}
const reader = new FileReader();
reader.onload = function(e) {
try {
const data = new Uint8Array(e.target.result);
const workbook = XLSX.read(data, { type: 'array' });
// Get first sheet
const firstSheet = workbook.Sheets[workbook.SheetNames[0]];
// Convert to JSON
const jsonData = XLSX.utils.sheet_to_json(firstSheet);
if (jsonData.length === 0) {
showToast('Файл не містить даних', 'error');
return;
}
// Check if it's a results file (has status column)
const isResultsFile = jsonData[0].hasOwnProperty('Статус') ||
jsonData[0].hasOwnProperty('Status');
// Process data
const newItems = [];
jsonData.forEach(row => {
let sku, name, expected, actual;
if (isResultsFile) {
// Results file format
sku = row['Артикул'] || row['SKU'] || row['Арт.'] || '';
name = row['Назва'] || row['Name'] || row['Товар'] || '';
expected = parseInt(row['Очікувано'] || row['Expected'] || row['Кількість'] || 0, 10);
actual = parseInt(row['Фактично'] || row['Actual'] || row['Факт'] || 0, 10);
} else {
// New file format (only expected quantities)
sku = row['Артикул'] || row['SKU'] || row['Арт.'] || '';
name = row['Назва'] || row['Name'] || row['Товар'] || '';
expected = parseInt(row['Кількість'] || row['Quantity'] || row['Очікувано'] || 0, 10);
actual = null;
}
if (sku) {
newItems.push({
sku: sku.toString(),
name: name.toString(),
expectedQuantity: expected,
actualQuantity: actual,
status: actual !== null ? calculateStatus({ expectedQuantity: expected, actualQuantity: actual }) : 'unchecked'
});
}
});
if (newItems.length === 0) {
showToast('Не вдалося знайти товари у файлі', 'error');
return;
}
// Update items and metadata
items = newItems;
fileMetadata = {
fileName: file.name,
fileDate: new Date().toLocaleString(),
totalItems: newItems.length
};
// Save to DB
Promise.all([
saveAllItemsToDB(items),
saveMetadataToDB(fileMetadata)
])
.then(() => {
updateUI();
showToast(`Завантажено ${newItems.length} товарів`, 'success');
switchTab('list');
})
.catch(error => {
console.error('Error saving file data:', error);
showToast('Помилка збереження даних з файлу', 'error');
});
// Reset file input
event.target.value = '';
} catch (error) {
console.error('File processing error:', error);
showToast('Помилка обробки файлу', 'error');
}
};
reader.onerror = function() {
showToast('Помилка читання файлу', 'error');
};
reader.readAsArrayBuffer(file);
}
// Save results to file
function saveResultsToFile() {
if (items.length === 0) {
showToast('Немає даних для збереження', 'warning');
return;
}
// Prepare data for export
const exportData = items.map(item => ({
'Артикул': item.sku,
'Назва': item.name,
'Очікувано': item.expectedQuantity,
'Фактично': item.actualQuantity !== null ? item.actualQuantity : '',
'Різниця': item.actualQuantity !== null ? item.actualQuantity - item.expectedQuantity : '',
'Статус': getStatusText(item.status)
}));
// Create workbook
const wb = XLSX.utils.book_new();
const ws = XLSX.utils.json_to_sheet(exportData);
XLSX.utils.book_append_sheet(wb, ws, 'Інвентаризація');
// Add metadata sheet
const metadata = [
{ 'Параметр': 'Значення' },
{ 'Параметр': 'Назва оригінального файлу', 'Значення': fileMetadata.fileName || 'Невідомо' },
{ 'Параметр': 'Дата завантаження', 'Значення': fileMetadata.fileDate || 'Невідомо' },
{ 'Параметр': 'Дата експорту', 'Значення': new Date().toLocaleString() },
{ 'Параметр': 'Всього товарів', 'Значення': items.length },
{ 'Параметр': 'Перевірено', 'Значення': items.filter(i => i.status !== 'unchecked').length },
{ 'Параметр': 'Відповідність', 'Значення': items.filter(i => i.status === 'match').length },
{ 'Параметр': 'Нестача', 'Значення': items.filter(i => i.status === 'missing').length },
{ 'Параметр': 'Надлишок', 'Значення': items.filter(i => i.status === 'excess').length }
];
const wsMetadata = XLSX.utils.json_to_sheet(metadata);
XLSX.utils.book_append_sheet(wb, wsMetadata, 'Метадані');
// Generate file name
const fileName = `Інвентаризація_${formatDateForFileName(new Date())}.xlsx`;
// Export
XLSX.writeFile(wb, fileName);
showToast('Результати збережено у файл', 'success');
}
// Format date for file name
function formatDateForFileName(date) {
const pad = num => num.toString().padStart(2, '0');
return `${date.getFullYear()}${pad(date.getMonth() + 1)}${pad(date.getDate())}_${pad(date.getHours())}${pad(date.getMinutes())}`;
}
// Confirm clear data
function confirmClearData() {
if (items.length === 0) {
showToast('Немає даних для очищення', 'info');
return;
}
const confirmClear = confirm('Ви впевнені, що хочете очистити всі дані інвентаризації? Цю дію не можна скасувати.');
if (confirmClear) {
Promise.all([
clearItemsDB(),
clearMetadataDB()
])
.then(() => {
items = [];
fileMetadata = {
fileName: null,
fileDate: null,
totalItems: 0
};
currentItem = null;
updateUI();
showToast('Всі дані очищено', 'success');
})
.catch(error => {
console.error('Error clearing data:', error);
showToast('Помилка очищення даних', 'error');
});
}
}
// Update UI based on current state
function updateUI() {
// Update file info
if (fileMetadata.fileName) {
fileInfo.classList.remove('hidden');
fileName.textContent = fileMetadata.fileName;
fileDate.textContent = fileMetadata.fileDate;
} else {
fileInfo.classList.add('hidden');
}
// Filter items based on status and search
const statusFilterValue = statusFilter.value;
const searchQuery = searchInput.value.toLowerCase();
const filteredItems = items.filter(item => {
// Status filter
const statusMatch = statusFilterValue === 'all' ||
(statusFilterValue === 'match' && item.status === 'match') ||
(statusFilterValue === 'missing' && item.status === 'missing') ||
(statusFilterValue === 'excess' && item.status === 'excess') ||
(statusFilterValue === 'unchecked' && item.status === 'unchecked');
// Search filter
const searchMatch = searchQuery === '' ||
item.name.toLowerCase().includes(searchQuery) ||
item.sku.toLowerCase().includes(searchQuery);
return statusMatch && searchMatch;
});
// Update items table
renderItemsTable(filteredItems);
// Update results table
const resultsItems = items.filter(item => item.status === 'missing' || item.status === 'excess');
renderResultsTable(resultsItems);
// Update progress and stats
updateProgressAndStats();
// Update control states
updateControlStates();
}
// Render items table
function renderItemsTable(itemsToRender) {
itemsTableBody.innerHTML = '';
if (itemsToRender.length === 0) {
noItemsMessage.classList.remove('hidden');
return;
}
noItemsMessage.classList.add('hidden');
itemsToRender.forEach(item => {
const row = document.createElement('tr');
row.className = 'border-b border-gray-200 hover:bg-gray-100/10 transition';
row.addEventListener('click', () => {
switchTab('scan');
currentItem = item;
displayItem(item);
quantityInput.disabled = false;
quantityInput.focus();
});
// Status cell
const statusCell = document.createElement('td');
statusCell.className = 'px-4 py-2';
const statusBadge = document.createElement('span');
statusBadge.className = `px-2 py-1 rounded-full text-xs font-medium text-white status-${item.status}`;
statusBadge.textContent = getStatusText(item.status);
statusCell.appendChild(statusBadge);
row.appendChild(statusCell);
// SKU cell
const skuCell = document.createElement('td');
skuCell.className = 'px-4 py-2 font-mono';
skuCell.textContent = item.sku;
row.appendChild(skuCell);
// Name cell
const nameCell = document.createElement('td');
nameCell.className = 'px-4 py-2';
nameCell.textContent = item.name;
row.appendChild(nameCell);
// Expected cell
const expectedCell = document.createElement('td');
expectedCell.className = 'px-4 py-2 text-right';
expectedCell.textContent = item.expectedQuantity;
row.appendChild(expectedCell);
// Actual cell
const actualCell = document.createElement('td');
actualCell.className = 'px-4 py-2 text-right';
actualCell.textContent = item.actualQuantity !== null ? item.actualQuantity : '-';
row.appendChild(actualCell);
itemsTableBody.appendChild(row);
});
}
// Render results table
function renderResultsTable(itemsToRender) {
resultsTableBody.innerHTML = '';
if (itemsToRender.length === 0) {
noResultsMessage.classList.remove('hidden');
resultsSummary.textContent = 'Знайдено 0 розбіжностей';
return;
}
noResultsMessage.classList.add('hidden');
resultsSummary.textContent = `Знайдено ${itemsToRender.length} розбіжностей`;
itemsToRender.forEach(item => {
const row = document.createElement('tr');
row.className = 'border-b border-gray-200 hover:bg-gray-100/10 transition';
row.addEventListener('click', () => {
switchTab('scan');
currentItem = item;
displayItem(item);
quantityInput.disabled = false;
quantityInput.focus();
});
// Status cell
const statusCell = document.createElement('td');
statusCell.className = 'px-4 py-2';
const statusBadge = document.createElement('span');
statusBadge.className = `px-2 py-1 rounded-full text-xs font-medium text-white status-${item.status}`;
statusBadge.textContent = getStatusText(item.status);
statusCell.appendChild(statusBadge);
row.appendChild(statusCell);
// SKU cell
const skuCell = document.createElement('td');
skuCell.className = 'px-4 py-2 font-mono';
skuCell.textContent = item.sku;
row.appendChild(skuCell);
// Name cell
const nameCell = document.createElement('td');
nameCell.className = 'px-4 py-2';
nameCell.textContent = item.name;
row.appendChild(nameCell);
// Expected cell
const expectedCell = document.createElement('td');
expectedCell.className = 'px-4 py-2 text-right';
expectedCell.textContent = item.expectedQuantity;
row.appendChild(expectedCell);
// Actual cell
const actualCell = document.createElement('td');
actualCell.className = 'px-4 py-2 text-right';
actualCell.textContent = item.actualQuantity !== null ? item.actualQuantity : '-';
row.appendChild(actualCell);
// Difference cell
const differenceCell = document.createElement('td');
differenceCell.className = 'px-4 py-2 text-right font-medium';
const difference = item.actualQuantity - item.expectedQuantity;
differenceCell.textContent = difference;
if (difference > 0) {
differenceCell.classList.add('text-green-500');
} else if (difference < 0) {
differenceCell.classList.add('text-red-500');
}
row.appendChild(differenceCell);
resultsTableBody.appendChild(row);
});
}
// Update progress and statistics
function updateProgressAndStats() {
const total = items.length;
const checked = items.filter(item => item.status !== 'unchecked').length;
const match = items.filter(item => item.status === 'match').length;
const missing = items.filter(item => item.status === 'missing').length;
const excess = items.filter(item => item.status === 'excess').length;
// Update counts
totalCount.textContent = total;
checkedCount.textContent = checked;
matchCount.textContent = match;
missingCount.textContent = missing;
excessCount.textContent = excess;
// Update progress bar
const progressPercent = total > 0 ? (checked / total) * 100 : 0;
progressFill.style.width = `${progressPercent}%`;
}
// Update control states (enabled/disabled)
function updateControlStates() {
// Save button
saveResultsBtn.disabled = items.length === 0;
// Clear button
clearDataBtn.disabled = items.length === 0;
// Quantity input (enabled only if an item is selected)
quantityInput.disabled = currentItem === null;
quantitySubmit.disabled = currentItem === null;
}
// Show toast notification
function showToast(message, type) {
const toast = document.createElement('div');
toast.className = `toast px-4 py-2 rounded-lg shadow-lg text-white ${
type === 'success' ? 'bg-green-500' :
type === 'error' ? 'bg-red-500' :
type === 'warning' ? 'bg-yellow-500' : 'bg-blue-500'
}`;
toast.textContent = message;
toastContainer.appendChild(toast);
// Remove after animation
setTimeout(() => {
toast.remove();
}, 3000);
}
// Vibrate device (if supported)
function vibrate() {
if ('vibrate' in navigator) {
navigator.vibrate(50);
}
}
// Initialize the app when DOM is loaded
document.addEventListener('DOMContentLoaded', initializeApp);
</script>
<p style="border-radius: 8px; text-align: center; font-size: 12px; color: #fff; margin-top: 16px;position: fixed; left: 8px; bottom: 8px; z-index: 10; background: rgba(0, 0, 0, 0.8); padding: 4px 8px;">Made with <img src="https://enzostvs-deepsite.hf.space/logo.svg" alt="DeepSite Logo" style="width: 16px; height: 16px; vertical-align: middle;display:inline-block;margin-right:3px;filter:brightness(0) invert(1);"><a href="https://enzostvs-deepsite.hf.space" style="color: #fff;text-decoration: underline;" target="_blank" >DeepSite</a> - 🧬 <a href="https://enzostvs-deepsite.hf.space?remix=rostyslavpwork/inventarization" style="color: #fff;text-decoration: underline;" target="_blank" >Remix</a></p></body>
</html> |