سامانه صدور فاکتور و حسابداری

feat(stage3.2): derive posted source linkage read-only from unique server voucher

davoodmehraban89 committed Oct 23, 2026 at 03:21 UTC a2919226d1d173c4cb7327a1af60ff1de98d800f
1 file changed +19 -1
js/journal-engine.js
+19 -1
@@ -150,7 +150,25 @@ function jeAutoPost(kind,source){
150 }
151 const v=jeCreateDraft({date:source.date||getJalaliNumeric(),description:title+' — '+(source.number||source.reference||source.id),sourceType:kind,sourceId:source.id,sourceVersion:Number(source.accountingVersion||1),lines});return jePost(v.id);
152 }
153 -function jeSourceVoucher(source){return source?.journalVoucherId?getMyJournalVouchers().find(v=>v.id===source.journalVoucherId):null}
153 +function jeSourceVoucher(source){
154 + if(!source)return null;
155 + if(source.journalVoucherId)return getMyJournalVouchers().find(v=>v.id===source.journalVoucherId)||null;
156 + // Stage3 server posting leaves original financial source immutable.
157 + // Read-only derived association is allowed only for ONE same-company,
158 + // same-kind, same-contract-version server voucher. Never adopt ambiguous
159 + // legacy cross-source matches or write back into the source record.
160 + if(!source.id||!source.companyId)return null;
161 + const cid=source.companyId;
162 + const isSale=getMyInvoices().some(x=>x.id===source.id&&x.companyId===cid);
163 + const isPurchase=getMyPurchases().some(x=>x.id===source.id&&x.companyId===cid);
164 + if(isSale===isPurchase)return null;
165 + const kind=isSale?'sale':'purchase';
166 + const matches=getMyJournalVouchers().filter(v=>
167 + v.companyId===cid&&v.sourceType===kind&&v.sourceId===source.id&&
168 + String(v.sourceVersion||'1')===String(source.accountingVersion||1)&&
169 + ['posted','reversed'].includes(v.status));
170 + return matches.length===1?matches[0]:null;
171 +}
172 function jeActiveSourceVoucher(source){const v=jeSourceVoucher(source);return v?.status==='posted'?v:null}
173 function jeSourceLocked(source){const v=jeSourceVoucher(source);return !!v&&['posted','reversed'].includes(v.status)}
174 function jeGuardSourceMutation(source,label){if(jeSourceLocked(source))throw new Error((label||'رکورد مالی')+' به سابقه حسابداری قطعی/برگشتی متصل است و قابل بازنویسی نیست؛ اصلاح باید با سند/رکورد اصلاحی جدید انجام شود.');return true}