PageRenderTime 25ms CodeModel.GetById 16ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/beta/cashReg/void.asp

http://github.com/khaneh/Orders
ASP | 291 lines | 172 code | 37 blank | 82 comment | 21 complexity | a87e38deddeb4690aa07003dc81dd617 MD5 | raw file
  1<%@LANGUAGE="VBSCRIPT" CODEPAGE="1256"%>
  2<!--#include file="../config.asp" -->
  3<!--#include File="../include_farsiDateHandling.asp"-->
  4<%
  5'CashRegister (9)
  6act = request("act")
  7
  8if act = "voidReceipt" then
  9	ON ERROR RESUME NEXT
 10		ReceiptID = clng(request("Receipt"))
 11		if Err.Number<>0 then
 12			Err.clear
 13			conn.close
 14			response.redirect "top.asp?errMsg=" & Server.URLEncode("����� ������ ����� ����.")
 15		end if
 16	ON ERROR GOTO 0
 17
 18	'----------
 19	' Findng Receipt 
 20	'----------
 21	mySQL = "SELECT ISNULL(CashAmount, 0) AS CashAmount, ISNULL(ChequeAmount, 0) AS ChequeAmount, ChequeQtty, SYS, Voided FROM Receipts WHERE (ID = '"& ReceiptID & "')"
 22	Set RS1= conn.Execute(mySQL)
 23
 24	if RS1.eof then
 25		conn.close
 26		response.redirect "top.asp?errmsg=" & Server.URLEncode("���! <br> ���� ������� ���� �����.")
 27	else
 28		VoidDate=		ShamsiToday()
 29		Voided=			RS1("Voided")
 30		CashAmount=		cdbl(RS1("CashAmount"))
 31		ChequeAmount=	cdbl(RS1("ChequeAmount"))
 32		ChequeQtty=		RS1("ChequeQtty")
 33		SYS=			RS1("SYS")
 34		RS1.close
 35
 36		if Voided then
 37			conn.close
 38			response.redirect "../"& SYS& "/AccountReport.asp?act=showReceipt&receipt="& ReceiptID & "&errmsg=" & Server.URLEncode("���!��� ��� ������ ���� ����<br><br>���� ���� ��� ���.")
 39		end if
 40'		mySQL="SELECT CashRegisterLines.ID AS CashRegLine, ISNULL(Receipts.CashAmount, 0) AS CashAmount, ISNULL(Receipts.ChequeAmount, 0) AS ChequeAmount, Receipts.ChequeQtty, Receipts.SYS, CashRegisterLines.CashReg, CashRegisterLines.Voided, CashRegisters.IsOpen, CashRegisters.Cashier FROM CashRegisters INNER JOIN CashRegisterLines ON CashRegisters.ID = CashRegisterLines.CashReg INNER JOIN Receipts ON CashRegisterLines.Link = Receipts.ID WHERE (CashRegisterLines.Type = 1) AND (CashRegisterLines.Link = '"& ReceiptID & "')"
 41'		Changed By kid 821228
 42
 43		'----------
 44		'	Finding the CashRegister Line for this Receipt
 45		'	(Type=1 means Receipt in CashRegisterLineTypes)
 46		'----------
 47		mySQL = "SELECT CashRegisterLines.ID AS CashRegLine, CashRegisterLines.isA, CashRegisterLines.CashReg, CashRegisters.IsOpen, CashRegisterLines.Voided, CashRegisters.Cashier FROM CashRegisters INNER JOIN CashRegisterLines ON CashRegisters.ID = CashRegisterLines.CashReg WHERE (CashRegisterLines.Type = 1) AND (CashRegisterLines.Link = '"& ReceiptID & "')"
 48		Set RS1= conn.Execute(mySQL)
 49		if NOT RS1.eof then
 50			' There is a CashRegister Line for this Receipt
 51			HasCashRegLine =	true
 52			CashRegLine =		RS1("CashRegLine")
 53			CashReg =			RS1("CashReg")
 54			IsOpen =			RS1("IsOpen")
 55			Cashier =			RS1("Cashier")
 56			isA =				RS1("isA")
 57		else
 58			' There is NOT a CashRegister Line for this Receipt
 59			HasCashRegLine =	false
 60		end if
 61		RS1.close
 62		'------------Check cash sitll open before void receipt
 63		if (not isOpen) and (HasCashRegLine) then 
 64			conn.close
 65			response.redirect "top.asp?errmsg=" & Server.URLEncode("��� ������ ����� �� ������ ��� �� ���� ���� ���!<br>�� ���������� ���� ���� ����")
 66		end if
 67
 68		if NOT ( Auth(9 , 7) OR (IsOpen AND Cashier=session("ID")) ) then
 69			' Doesn't Have the Priviledge to VOID the RECEIPT/PAYMENT 
 70			conn.close
 71			response.redirect "top.asp?errmsg=" & Server.URLEncode("���!<br>��� ���� ���� ���� ����� ��� ������ �� ������.")
 72		end if
 73
 74	  '**************************** Voiding (AR/AP/AO)Item of Receipt  ****************
 75	  '***
 76		'*********  Finding the (AR/AP/AO)Item of Receipt 
 77		'*** Type = 2 means (AR/AP/AO)Item is a Receipt
 78		mySQL="SELECT ID, Account FROM "& SYS & "Items WHERE (Type=2) AND (Link = '"& ReceiptID & "')"
 79		Set RS1=conn.Execute(mySQL)
 80		ItemID = RS1("ID")
 81		Account = RS1("Account")
 82
 83		'*********  Finding other (AR/AP/AO)Items related to this Item
 84		mySQL="SELECT ID AS RelationID, Debit"& SYS & "Item, Amount FROM "& SYS & "ItemsRelations WHERE (Credit"& SYS & "Item = '"& ItemID & "')"
 85		Set RS1=conn.Execute(mySQL)
 86		do while not (RS1.eof)
 87			'*********  Adding back the amount in the relation, to the debit (AR/AP/AO)Item ...
 88			conn.Execute("UPDATE "& SYS & "Items SET RemainedAmount=RemainedAmount+ '"& RS1("Amount") & "', FullyApplied=0 WHERE (ID = '"& RS1("Debit"& SYS & "Item") & "')")
 89
 90			'*********  Deleting the relation
 91			conn.Execute("DELETE FROM "& SYS & "ItemsRelations WHERE ID='"& RS1("RelationID") & "'")
 92			
 93			RS1.movenext
 94		loop
 95
 96		'*********  Voiding the (AR/AP/AO)Item
 97		conn.Execute("UPDATE "& SYS & "Items SET RemainedAmount=0, FullyApplied=0, Voided=1 WHERE (ID = '"& ItemID & "')")
 98
 99		'**************************************************************
100		'*			Decreasing Account's (AR/AP/AO) Balance  
101		'*			('because we have removed a CREDIT Item )
102		'**************************************************************
103		mySQL="UPDATE Accounts SET "& SYS & "Balance = "& SYS & "Balance - '"& CashAmount + ChequeAmount & "'  WHERE (ID ='"& Account & "')"
104		conn.Execute(mySQL)
105		
106	  '***
107	  '***---------------- End of Voiding (AR/AP/AO)Item  ----------------
108		
109
110		' ######################################################### 
111		'				SET ALL RECEIVED CHEQUES STATUS 
112		'				TO  RETURNED
113		' #########################################################
114		' Status = 4  means Returned
115		mySQL="UPDATE ReceivedCheques SET LastStatus = 4 , LastUpdatedDate =N'"& VoidDate & "', LastUpdatedBy = '"& session("ID") & "' WHERE (Receipt = '"& ReceiptID & "')"
116		conn.Execute(mySQL)
117
118		' ######################################################### 
119		'					VOID the RECEIPT ...
120		' #########################################################
121		mySQL="UPDATE Receipts SET Voided=1,VoidedBy='"& session("ID") & "',VoidedDate=N'"& VoidDate & "' WHERE (ID = '"& ReceiptID & "')"
122		conn.Execute(mySQL)
123
124		if HasCashRegLine then
125			' ######################################################### 
126			'					VOID the CASH REG LINE  ...
127			' #########################################################
128			mySQL="UPDATE CashRegisterLines SET Voided = 1 WHERE (ID = '"& CashRegLine & "')"
129			conn.Execute(mySQL)
130				' ######################################################### 
131				'				CHANGING CASH REGISTER BALANCE 
132				'				(decreasing)
133				' #########################################################
134				if isA then 
135					mySQL="UPDATE CashRegisters SET CashAmountA = CashAmountA - '"& CashAmount & "', ChequeAmount = ChequeAmount - '"& ChequeAmount & "', ChequeQtty = ChequeQtty - '"& ChequeQtty & "' WHERE (ID ='"& CashReg & "')"
136				else
137					mySQL="UPDATE CashRegisters SET CashAmountB = CashAmountB - '"& CashAmount & "', ChequeAmount = ChequeAmount - '"& ChequeAmount & "', ChequeQtty = ChequeQtty - '"& ChequeQtty & "' WHERE (ID ='"& CashReg & "')"
138				end if
139				conn.Execute(mySQL)
140		end if
141	end if
142
143	conn.close
144
145	if HasCashRegLine then
146		response.redirect "CashRegReport.asp"
147	else
148		response.redirect "../"& SYS & "/AccountReport.asp?act=showReceipt&receipt=" & ReceiptID
149	end if
150
151elseif act = "voidPayment" then
152
153	ON ERROR RESUME NEXT
154		PaymentID = clng(request("Payment"))
155		if Err.Number<>0 then
156			Err.clear
157			conn.close
158			response.redirect "top.asp?errMsg=" & Server.URLEncode("����� ������ ����� ����.")
159		end if
160	ON ERROR GOTO 0
161
162	'----------
163	' Findng Payment 
164	'----------
165	mySQL = "SELECT ISNULL(CashAmount, 0) AS CashAmount, ISNULL(ChequeAmount, 0) AS ChequeAmount, SYS, Voided FROM Payments WHERE (ID = '"& PaymentID & "')"
166	Set RS1= conn.Execute(mySQL)
167
168	if RS1.eof then
169		conn.close
170		response.redirect "top.asp?errmsg=" & Server.URLEncode("���! <br> ���� ������� ���� �����.")
171	else
172		VoidDate=		ShamsiToday()
173		Voided=			RS1("Voided")
174		CashAmount=		cdbl(RS1("CashAmount"))
175		ChequeAmount=	cdbl(RS1("ChequeAmount"))
176		SYS=			RS1("SYS")
177		RS1.close
178
179		if Voided then
180			conn.close
181			response.redirect "../"& SYS& "/AccountReport.asp?act=showPayment&payment="& PaymentID & "&errmsg=" & Server.URLEncode("���!��� ��� ������ ���� ����<br><br>���� ���� ��� ���.")
182		end if
183		'----------
184		'	Finding the CashRegister Line for this Payment
185		'	(Type=2 means Payment in CashRegisterLineTypes)
186		'----------
187		mySQL = "SELECT CashRegisterLines.ID AS CashRegLine,CashRegisterLines.isA, CashRegisterLines.CashReg, CashRegisters.IsOpen, CashRegisterLines.Voided, CashRegisters.Cashier FROM CashRegisters INNER JOIN CashRegisterLines ON CashRegisters.ID = CashRegisterLines.CashReg WHERE (CashRegisterLines.Type = 2) AND (CashRegisterLines.Link = '"& PaymentID & "')"
188		Set RS1= conn.Execute(mySQL)
189		isOpen=false
190		if NOT RS1.eof then
191			' There is a CashRegister Line for this Payment
192			HasCashRegLine =	true
193			CashRegLine =		RS1("CashRegLine")
194			CashReg =			RS1("CashReg")
195			IsOpen =			RS1("IsOpen")
196			Cashier =			RS1("Cashier")
197			isA =				RS1("isA")
198		else
199			' There is NOT a CashRegister Line for this Payment
200			HasCashRegLine =	false
201		end if
202		RS1.close
203		'------------Check cash sitll open before void payment
204		'response.write mySQL
205		'response.end
206		if (not isOpen) and (HasCashRegLine) then 
207			conn.close
208			response.redirect "top.asp?errmsg=" & Server.URLEncode("��� ������ ����� �� ������ ��� �� ���� ���� ���!<br>�� ���������� ���� ���� ����")
209		end if
210
211		if NOT ( Auth(9 , 7) OR (IsOpen AND Cashier=session("ID")) ) then
212			' Doesn't Have the Priviledge to VOID the RECEIPT/PAYMENT 
213			conn.close
214			response.redirect "top.asp?errmsg=" & Server.URLEncode("���!<br>��� ���� ���� ���� ����� ��� ������ �� ������.")
215		end if
216
217	  '**************************** Voiding (AR/AP/AO)Item of Payment  ****************
218	  '***
219		'*********  Finding the (AR/AP/AO)Item of Payment 
220		'*** Type = 5 means (AR/AP/AO)Item is a Payment
221		mySQL="SELECT ID, Account FROM "& SYS & "Items WHERE (Type=5) AND (Link = '"& PaymentID & "')"
222		Set RS1=conn.Execute(mySQL)
223		ItemID = RS1("ID")
224		Account = RS1("Account")
225
226		'*********  Finding other (AR/AP/AO)Items related to this Item
227		mySQL="SELECT ID AS RelationID, Credit"& SYS & "Item, Amount FROM "& SYS & "ItemsRelations WHERE (Debit"& SYS & "Item = '"& ItemID & "')"
228		Set RS1=conn.Execute(mySQL)
229		do while not (RS1.eof)
230			'*********  Adding back the amount in the relation, to the debit (AR/AP/AO)Item ...
231			conn.Execute("UPDATE "& SYS & "Items SET RemainedAmount=RemainedAmount+ '"& RS1("Amount") & "', FullyApplied=0 WHERE (ID = '"& RS1("Credit"& SYS & "Item") & "')")
232
233			'*********  Deleting the relation
234			conn.Execute("DELETE FROM "& SYS & "ItemsRelations WHERE ID='"& RS1("RelationID") & "'")
235			
236			RS1.movenext
237		loop
238
239	  '******************  Voiding the (AR/AP/AO)Item	*****************
240	  '***
241		conn.Execute("UPDATE "& SYS & "Items SET RemainedAmount=0, FullyApplied=0, Voided=1 WHERE (ID = '"& ItemID & "')")
242
243		'**************************************************************
244		'*			Increasing Account's (AR/AP/AO) Balance  
245		'*			('because we have removed a DEBIT Item )
246		'**************************************************************
247		mySQL="UPDATE Accounts SET "& SYS & "Balance = "& SYS & "Balance + '"& CashAmount + ChequeAmount & "'  WHERE (ID ='"& Account & "')"
248		conn.Execute(mySQL)
249		
250	  '***
251	  '***---------------- End of Voiding (AR/AP/AO)Item ----------------
252		
253
254		' ######################################################### 
255		'					VOID the PAYMENT ...
256		' #########################################################
257		mySQL="UPDATE Payments SET Voided=1,VoidedBy='"& session("ID") & "',VoidedDate=N'"& VoidDate & "' WHERE (ID = '"& PaymentID & "')"
258		conn.Execute(mySQL)
259
260		if HasCashRegLine then
261			' ######################################################### 
262			'					VOID the CASH REG LINE  ...
263			' #########################################################
264			mySQL="UPDATE CashRegisterLines SET Voided=1 WHERE (ID = '"& CashRegLine & "')"
265			conn.Execute(mySQL)
266				' ######################################################### 
267				'				CHANGING CASH REGISTER BALANCE 
268				'				(increasing)
269				' #########################################################
270				if isA then 
271					mySQL="UPDATE CashRegisters SET CashAmountA = CashAmountA + "& CashAmount & " WHERE (ID ="& CashReg & ")"
272				else
273					mySQL="UPDATE CashRegisters SET CashAmountB = CashAmountB + "& CashAmount & " WHERE (ID ="& CashReg & ")"
274				end if
275				'mySQL="UPDATE CashRegisters SET CashAmount = CashAmount + '"& CashAmount & "' WHERE (ID ='"& CashReg & "')"
276				conn.Execute(mySQL)
277		end if
278	end if
279
280	conn.close
281
282	if HasCashRegLine then
283		response.redirect "CashRegReport.asp"
284	else
285		response.redirect "../"& SYS & "/AccountReport.asp?act=showPayment&payment=" & PaymentID
286	end if
287else
288	conn.close
289	response.redirect "top.asp?errMsg=" & Server.URLEncode("���.")
290end if
291%>