فینورا پرو - سامانه جامع حسابداری و صدور فاکتور

fix(stock): guard integrated adjustments and refresh existing Kardex modal

davoodmehraban89 committed Oct 31, 2026 at 22:18 UTC 661fb64d69b93e2a7df7a13c0af9e948007e6071
1 file changed +50 -8
src/views/StockKardexModal.js
+50 -8
@@ -3,6 +3,7 @@
3 // ============================================================
4
5 import { ProductController } from '../controllers/ProductController.js';
6 +import { RetailPostingService } from '../core/RetailPostingService.js';
7 import { Modal } from '../core/Modal.js';
8 import { Toast } from '../core/Toast.js';
9 import { Formatters } from '../utils/Formatters.js';
@@ -19,22 +20,29 @@ class StockKardexModalImpl {
20
21 const kardex = await ProductController.getKardex(productId);
22
22 - const body = this._buildBody(product, kardex);
23 + const retailEnabled = await RetailPostingService.isEnabled();
24 + const body = this._buildBody(product, kardex, retailEnabled);
25 const footer = this._buildFooter(product);
26
25 - Modal.open({
27 + const modal = Modal.open({
28 title: `📊 کاردکس کالا: ${product.name}`,
29 body,
30 footer,
31 size: 'lg'
32 });
33 + modal.card.dataset.stockKardexProductId = String(productId);
34
35 window.__kardexProductId = productId;
36 }
37
35 - _buildBody(product, kardex) {
38 + _buildBody(product, kardex, retailEnabled = false) {
39 const isService = product.trackInventory === false;
40 const minStock = Number(product.minStock) || 0;
41 + const manualAdjustmentControl = isService
42 + ? '<small style="color:var(--text-muted)">این مورد خدمت است و موجودی انبار برای آن پیگیری نمی‌شود.</small>'
43 + : retailEnabled
44 + ? '<small style="color:var(--text-muted)">در شرکت یکپارچه، اصلاح موجودی فقط از طریق عملیات انبارگردانی انجام می‌شود.</small>'
45 + : `<button class="btn btn-secondary btn-inline" style="font-size:12px;min-height:34px;padding:4px 12px" onclick="window.StockKardexModal.openAdjustModal('${product.id}')">⚙️ اصلاح دستی موجودی</button>`;
46 const currentStock = kardex.finalBalance;
47 const isLow = !isService && currentStock <= minStock && currentStock > 0;
48 const isOut = !isService && currentStock <= 0;
@@ -120,9 +128,7 @@ class StockKardexModalImpl {
128 <div style="font-size:12.5px;color:var(--text-muted)">
129 ریز حرکات انبار (${Formatters.toPersianDigits(kardex.rows.length)} مورد)
130 </div>
123 - <button class="btn btn-secondary btn-inline" style="font-size:12px;min-height:34px;padding:4px 12px" onclick="window.StockKardexModal.openAdjustModal('${product.id}')">
124 - ⚙️ اصلاح دستی موجودی
125 - </button>
131 + ${manualAdjustmentControl}
132 </div>
133
134 <div class="table-wrap" style="max-height:400px;overflow-y:auto">
@@ -157,6 +163,14 @@ class StockKardexModalImpl {
163 async openAdjustModal(productId) {
164 const product = await ProductController.getProduct(productId);
165 if (!product) return;
166 + if (product.trackInventory === false) {
167 + Toast.warning('برای خدمت امکان اصلاح موجودی وجود ندارد');
168 + return;
169 + }
170 + if (await RetailPostingService.isEnabled()) {
171 + Toast.warning('در شرکت یکپارچه، اصلاح موجودی از طریق انبارگردانی انجام می‌شود');
172 + return;
173 + }
174 const currentStock = await ProductController.getStock(productId);
175
176 const body = `
@@ -189,6 +203,15 @@ class StockKardexModalImpl {
203 }
204
205 async saveAdjust(productId) {
206 + const product = await ProductController.getProduct(productId);
207 + if (!product || product.trackInventory === false) {
208 + Toast.warning('کالای انبارگردانی‌شونده معتبر نیست');
209 + return;
210 + }
211 + if (await RetailPostingService.isEnabled()) {
212 + Toast.warning('در شرکت یکپارچه، اصلاح موجودی از طریق انبارگردانی انجام می‌شود');
213 + return;
214 + }
215 let newQty;
216 try {
217 newQty = NumberInput.parseStrict(document.getElementById('adjQty'), { decimals: 3 });
@@ -215,9 +238,10 @@ class StockKardexModalImpl {
238
239 Toast.success('موجودی اصلاح شد');
240
218 - // بستن و باز کردن مجدد کاردکس
241 + // Close only the adjustment child and refresh its existing Kardex parent.
242 + // Reopening a second Kardex dialog would leave a stale parent underneath.
243 Modal.close();
220 - setTimeout(() => this.open(productId), 300);
244 + await this._refreshOpenKardex(productId);
245
246 // رفرش جدول کالاها اگه بازه
247 if (window.ProductsView && window.ProductsView.reload) {
@@ -234,6 +258,24 @@ class StockKardexModalImpl {
258 }
259 }
260
261 + async _refreshOpenKardex(productId) {
262 + const modal = Modal.stack.at(-1);
263 + const product = await ProductController.getProduct(productId);
264 + if (!product) return;
265 + const kardex = await ProductController.getKardex(productId);
266 + if (modal?.card?.isConnected &&
267 + modal.card.dataset.stockKardexProductId === String(productId)) {
268 + const retailEnabled = await RetailPostingService.isEnabled();
269 + const body = modal.card.querySelector('.modal-body');
270 + const title = modal.card.querySelector('.modal-title');
271 + if (body) body.innerHTML = this._buildBody(product, kardex, retailEnabled);
272 + if (title) title.textContent = `📊 کاردکس کالا: ${product.name}`;
273 + modal.card.focus({ preventScroll: true });
274 + return;
275 + }
276 + await this.open(productId);
277 + }
278 +
279 // ============================================================
280 // چاپ کاردکس
281 // ============================================================