سامانه صدور فاکتور و حسابداری
Mirrored from https://github.com/davoodmehraban89/Factor-easy.git
Synced Dec 4, 01:45 UTC
feat: add server-authoritative sale and purchase autopost adapter
davoodmehraban89 committed
Nov 17, 2026 at 13:52 UTC
dface0e3687e64823692c95daec846a4f3898684
1 file changed
+82
js/journal-rpc.js
+82
@@ -148,3 +148,85 @@ async function jeRpcCopyVoucher(voucherId,date){
148
async function jeRpcReadGraph(voucherId){
149
return jeRpcCall('finora_journal_read_graph',{p_organization_id:await jeRpcAssertContext(),p_voucher_id:voucherId});
150
}
151
+
152
+
153
+// Source posting is a server-owned transaction. The business source is first
154
+// persisted as a draft; the RPC then derives lines from that persisted source
155
+// and posts the voucher atomically. Only the source-to-voucher link is synced
156
+// back through the generic record store.
157
+const JE_RPC_JOURNAL_COLLECTIONS=new Set(['journalVouchers','journalLines','journalLineDimensions']);
158
+function jeRpcAssertNoGenericJournalDiff(){
159
+ if(typeof computeDiff!=='function')throw new Error('کنترل تغییرات دفتر کل در دسترس نیست؛ ثبت خودکار متوقف شد.');
160
+ const diff=computeDiff();
161
+ const dirty=[...(diff.up||[]).map(x=>x.coll),...(diff.del||[]).map(x=>String(x).split('|',1)[0])]
162
+ .filter(x=>JE_RPC_JOURNAL_COLLECTIONS.has(x));
163
+ if(dirty.length)throw new Error('تغییر محلی دفتر کل هنوز از مسیر قدیمی در صف همگامسازی است؛ ابتدا وضعیت را بررسی و تطبیق کنید. ثبت خودکار سرور اجرا نشد.');
164
+}
165
+function jeRpcSourceRecord(collection,id){
166
+ if(collection==='invoices')return getMyInvoices().find(x=>x.id===id);
167
+ if(collection==='purchases')return getMyPurchases().find(x=>x.id===id);
168
+ return null;
169
+}
170
+async function jeRpcPersistSourceRecord(collection,id){
171
+ const org=await jeRpcAssertContext(),key=collection+'|'+id;
172
+ if(!['invoices','purchases'].includes(collection))throw new Error('نوع رکورد منبع برای ثبت خودکار پشتیبانی نمیشود.');
173
+ const source=jeRpcSourceRecord(collection,id);
174
+ if(!source||source.ownerUserId!==currentUser.id||source.companyId!==afCompanyId())
175
+ throw new Error('رکورد منبع در حساب یا شرکت فعال پیدا نشد.');
176
+ jeRpcAssertNoGenericJournalDiff();
177
+ if(typeof saveDatastore!=='function'||typeof runSync!=='function'||typeof syncSnap==='undefined')
178
+ throw new Error('لایه همگامسازی برای تأیید ثبت منبع در دسترس نیست.');
179
+ saveDatastore();
180
+ await runSync();
181
+ const deadline=Date.now()+20000;
182
+ while(Date.now()<deadline){
183
+ if(currentUser?.organizationId!==org)throw new Error('سازمان فعال هنگام ذخیره رکورد منبع تغییر کرد.');
184
+ const latest=jeRpcSourceRecord(collection,id);
185
+ const expected=latest&&JSON.stringify(stripOwner(latest));
186
+ const diff=typeof computeDiff==='function'?computeDiff():{up:[],del:[]};
187
+ const sourceDirty=(diff.up||[]).some(x=>x.key===key)||(diff.del||[]).includes(key);
188
+ if(!syncRunning&&!syncPending&&!sourceDirty&&syncSnap.get(key)===expected){
189
+ jeRpcAssertNoGenericJournalDiff();
190
+ return latest;
191
+ }
192
+ if(Number(syncFailCount||0)>0)break;
193
+ await new Promise(resolve=>setTimeout(resolve,100));
194
+ }
195
+ throw new Error('تأیید ذخیره رکورد منبع از سرور دریافت نشد؛ ثبت حسابداری انجام نشد یا وضعیت باید پس از بارگذاری مجدد بررسی شود.');
196
+}
197
+async function jeRpcAutoPost(kind,sourceId){
198
+ if(!requireWrite())throw new Error('مجوز قطعیسازی حسابداری ندارید.');
199
+ const collection=kind==='sale'?'invoices':kind==='purchase'?'purchases':'';
200
+ if(!collection)throw new Error('نوع ثبت خودکار پشتیبانی نمیشود.');
201
+ const org=await jeRpcAssertContext();
202
+ let source=jeRpcSourceRecord(collection,sourceId);
203
+ if(!source||source.ownerUserId!==currentUser.id||source.companyId!==afCompanyId())
204
+ throw new Error('رکورد منبع برای شرکت فعال پیدا نشد.');
205
+ if(source.journalVoucherId){
206
+ const existing=getMyJournalVouchers().find(v=>v.id===source.journalVoucherId);
207
+ if(existing?.status==='posted')return existing;
208
+ throw new Error('رکورد منبع از قبل به سندی متصل است؛ وضعیت اتصال باید بررسی شود.');
209
+ }
210
+ if(!Number.isSafeInteger(Number(source.grandTotal))||Number(source.grandTotal)<=0)
211
+ throw new Error('مبلغ کل منبع باید عدد صحیح مثبت و امن باشد.');
212
+ const date=jeRpcDate(source.date),fy=jeFiscalForDate(date);
213
+ if(!fy||fy.status==='locked')throw new Error('سال مالی باز برای تاریخ رکورد منبع پیدا نشد.');
214
+ const sourceVersion=Number(source.accountingVersion||1);
215
+ if(!Number.isSafeInteger(sourceVersion)||sourceVersion<1)throw new Error('نسخه رویداد منبع معتبر نیست.');
216
+ // Ensure the source is committed before the server RPC attempts to lock/read it.
217
+ await jeRpcPersistSourceRecord(collection,sourceId);
218
+ source=jeRpcSourceRecord(collection,sourceId);
219
+ const header={companyId:source.companyId,fiscalYearId:fy.id,date,
220
+ description:String(source.description||((kind==='sale'?'فروش ':'خرید ')+(source.number||source.id))),
221
+ reference:String(source.reference||source.number||''),voucherType:kind,
222
+ sourceType:kind,sourceId:String(source.id),sourceVersion:String(sourceVersion)};
223
+ const result=await jeRpcCall('finora_journal_autopost',{
224
+ p_organization_id:org,p_voucher_id:afId('JV'),p_source_collection:collection,
225
+ p_source_id:String(source.id),p_header:header,p_lines:[]
226
+ });
227
+ const voucher=result?.voucher;
228
+ if(!voucher?.id||voucher.status!=='posted')
229
+ throw new Error('پاسخ ثبت خودکار سرور فاقد سند قطعی معتبر است؛ وضعیت منبع را پیش از تکرار بررسی کنید.');
230
+ await jeRpcRefresh(org);
231
+ return voucher;
232
+}