@@ -44,6 +44,8 @@ import {
4444 ValidationMap ,
4545} from './types' ;
4646import { validateOptions , validateRequired } from './validationFunction' ;
47+ import { getShouldDocSyncToERPNext } from 'src/utils/erpnextSync' ;
48+ import { ModelNameEnum } from 'models/types' ;
4749
4850export class Doc extends Observable < DocValue | Doc [ ] > {
4951 /* eslint-disable @typescript-eslint/no-floating-promises */
@@ -247,6 +249,22 @@ export class Doc extends Observable<DocValue | Doc[]> {
247249 return true ;
248250 }
249251
252+ get shouldDocSyncToERPNext ( ) {
253+ const syncEnabled = ! ! this . fyo . singles . ERPNextSyncSettings ?. isEnabled ;
254+ if ( ! syncEnabled ) {
255+ return false ;
256+ }
257+
258+ if ( ! this . schemaName || ! this . fyo . singles . ERPNextSyncSettings ) {
259+ return false ;
260+ }
261+
262+ return getShouldDocSyncToERPNext (
263+ this . fyo . singles . ERPNextSyncSettings ,
264+ this
265+ ) ;
266+ }
267+
250268 _setValuesWithoutChecks ( data : DocValueMap , convertToDocValue : boolean ) {
251269 for ( const field of this . schema . fields ) {
252270 const { fieldname, fieldtype } = field ;
@@ -912,6 +930,28 @@ export class Doc extends Observable<DocValue | Doc[]> {
912930 this . _notInserted = false ;
913931 await this . trigger ( 'afterSync' ) ;
914932 this . fyo . doc . observer . trigger ( `sync:${ this . schemaName } ` , this . name ) ;
933+
934+ if ( this . _addDocToSyncQueue && ! ! this . shouldDocSyncToERPNext ) {
935+ const isDocExistsInQueue = await this . fyo . db . getAll (
936+ ModelNameEnum . ERPNextSyncQueue ,
937+ {
938+ filters : {
939+ referenceType : this . schemaName ,
940+ documentName : this . name as string ,
941+ } ,
942+ }
943+ ) ;
944+
945+ if ( ! isDocExistsInQueue . length ) {
946+ this . fyo . doc
947+ . getNewDoc ( ModelNameEnum . ERPNextSyncQueue , {
948+ referenceType : this . schemaName ,
949+ documentName : this . name ,
950+ } )
951+ . sync ( ) ;
952+ }
953+ }
954+
915955 this . _syncing = false ;
916956 return doc ;
917957 }
0 commit comments