//"procmg.sc","Procedury dokumentu magazynowego","\Procedury\",0,1.0.0,SYSTEM

#include "Procedury Produkcja"
#include "MobileWarehouse - include"

int Sub OnAddNewDokument(long lId)
	OnAddNewDokument = 0
EndSub

int Sub OnBeforeInsert(long lId)
	OnBeforeInsert = DocumentToBuffer(0)
EndSub

int Sub OnAfterInsert(long lId)
	UnlockMg(lId)
	OnAfterInsert = 0
EndSub

int Sub OnKhChanged(long lId)
	OnKhChanged = 0
EndSub

int Sub OnBeforeEdit(long lId)
	int canEditFromOperation = CheckDocumentInOperation(lId, 1)
	int confirmEdit = 0
	if(canEditFromOperation != 0) then				
		confirmEdit = CannotEditDocumentInOperation()
	endif	
	
	int block = 0
	if(confirmEdit != 0) then				
		OnBeforeEdit = confirmEdit
	else	
		block = IsMgLocked(lId)
		if(block == 0) then
			LockMg(lId)
		endif
		OnBeforeEdit = 0
	endif
EndSub

int Sub OnBeforeBuforEdit(long lId)
	int block = IsMgLocked(lId)
	int createdByMobile = IsCreatedByMobile(lId)
	int createdByMobileLock = 0
	int canEditFromOperation = 0
	int confirmEdit = 0
	if(block == 0) then		
		if(createdByMobile == 0) then
			canEditFromOperation = CheckDocumentInOperation(lId, 1)
			if(canEditFromOperation != 0) then				
				confirmEdit = CannotEditDocumentInOperation()
			endif
			
			if(confirmEdit != 0) then
				OnBeforeBuforEdit = confirmEdit
			else				
				LockMg(lId)
				OnBeforeBuforEdit = 0	
			endif				
		else
			LockMg(lId)
			createdByMobileLock = DocumentCreatedByMobile()
			if(createdByMobileLock != 0) then
				UnlockMg(lId)
			endif
			OnBeforeBuforEdit = createdByMobileLock
		endif	
	else
		OnBeforeBuforEdit = DocumentInEditMode()
	endif
	//OnBeforeBuforEdit = 0
EndSub

int Sub OnAfterUpdate(long lId)
	UnlockMg(lId)
	OnAfterUpdate = 0
EndSub

int Sub OnAfterBuforUpdate(long lId)		
	UnlockMg(lId)	
	OnAfterBuforUpdate = 0
EndSub

int Sub OnBeforeDelete(long lId)
	int canDeleteFromOperation = CheckDocumentInOperation(lId, 1)
	int confirmDelete = 0
	if(canDeleteFromOperation != 0) then				
		confirmDelete = CannotDeleteDocument()
	endif	
	
	if(confirmDelete != 0) then				
		OnBeforeDelete = confirmDelete
	else
		UnlockMg(lId)
		OnBeforeDelete = 0
	endif		
EndSub

int Sub OnBeforeBuforDelete(long lId)
	int block = IsMgLocked(lId)
	int createdByMobile = IsCreatedByMobile(lId)
	int createdByMobileLock = 0
	int canDeleteFromOperation = 0
	int confirmDelete = 0
	if(block == 0) then
		if(createdByMobile == 0) then
			canDeleteFromOperation = CheckDocumentInOperation(lId, 1)
			if(canDeleteFromOperation != 0) then				
				confirmDelete = CannotDeleteDocument()
			endif	
			
			if(confirmDelete != 0) then				
				OnBeforeBuforDelete = confirmDelete
			else
				UnlockMg(lId)
				OnBeforeBuforDelete = 0
			endif	
		else
			createdByMobileLock = DocumentCreatedByMobile()
			if(createdByMobileLock == 0) then
				UnlockMg(lId)
			endif
			OnBeforeBuforDelete = createdByMobileLock
		endif
	else
		OnBeforeBuforDelete = DocumentInEditMode()
	endif
EndSub

int Sub OnAfterDelete(long lId)
	UnlockMg(lId)
	OnAfterDelete = 0
EndSub

int Sub OnAfterBuforDelete(long lId)
	UnlockMg(lId)
	OnAfterBuforDelete = 0
EndSub

int Sub OnBeforeAnuluj(long lId)	
	int canCancelFromOperation = CheckDocumentInOperation(lId, 1)
	int confirmCancel = 0
	if(canCancelFromOperation != 0) then				
		confirmCancel = CannotDeleteDocument()
	endif	
	
	if(confirmCancel != 0) then				
		OnBeforeAnuluj = confirmCancel
	else
		OnBeforeAnuluj = 0
	endif		
EndSub

int Sub OnTwChanged(long lId)
	OnTwChanged = 0
EndSub

int Sub OnBeforeCancel(long lId)
	OnBeforeCancel = 0
EndSub

int Sub OnAfterCancel(long lId)
	UnlockMg(lId)
	OnAfterCancel = 0
EndSub