Return  &&  Pipop-00
** Delete File Ho_Cls.fxp 
** Comp Ho_Cls.Prg

** เริ่มทำงาน 
Clear 
** mDateCls={01/01/2025} && วันที่ต้องการ CLS กรณีเป็น Fox 2.6
mDateCls=Ctod('01/01/2025')  && กรณีเป็น Visual Fox
mYearRoom=2022 && เก็บไว้ 2 ปี 
mDirCUR='Transac' && ตำแหน่งข้อมูล ปัจจุบัน
mDirBACK='2024' && ตำแหน่งข้อมูลที่เก็บไว้

Do ClsDate  && เริ่มลบข้อมูลเก่าๆ
Do ClsGuest  && ลบลูกค้าที่ไม่มีการใช้งาน
Do DupGuest  && ลบลูกค้าที่ซ้ำกัน 
Do Ar2Return && นำลูกค้าที่ค้างจ่ายเข้าระบบ
Return 



Procedure Ar2Return && นำลูกค้าที่ค้างจ่ายเข้าระบบ
? 'Procedure Ar2Return--Start'
Close Data
Use (mDirBACK+'\RoomHead') In 0 Order RmCode Alias oRoomHead
Use (mDirBACK+'\RoomTran') In 0 Order RmCode Alias oRoomTran
Use (mDirBACK+'\RoomHd2') In 0 Order RmCode Alias oRoomHD2
Use (mDirBACK+'\Guest')  In 0 Order GuestCode Alias oGuest

Use (mDirCUR+'\RoomHead')   In 0 Order RmCode Alias nRoomHead
Use (mDirCUR+'\RoomTRan')  In 0 Order RmCode Alias nRoomTran
Use (mDirCUR+'\RoomHd2')  In 0 Order RmCode Alias nRoomHD2
Use (mDirCUR+'\Guest') In 0 Order GuestCode Alias nGuest

tempnum=0
Select oRoomHead
Scan For oRoomHead.RmDebt<>oRoomHead.Payment
	Select oRoomHead
	Scatter Memvar
	If !Seek(m.RmCode,'nRoomHead')
		tempnum=tempnum+oRoomHead.RmDebt
		Insert Into nRoomHead From Memvar 

		Select oRoomTran 
		If Seek(oRoomHead.RmCode,'oRoomTran')	
			Select oRoomTran
			Scan While oRoomTran.RmCode=oRoomHead.RmCode
				Scatter Memvar 
				Delete 
				Insert Into nRoomTran From Memvar 	
			EndScan 
		Endif 

		If Seek(oRoomHead.RmCode,'oRoomHD2') .and. !Seek(oRoomHead.RmCode,'nRoomHD2')	
			Select oRoomHD2
			Scatter Memvar 
			Delete 
			Insert Into nRoomHD2 From Memvar 	
		Endif 
		If Seek(oRoomHead.GuestCode,'oGuest') .and. !Seek(oRoomHead.GuestCode,'nGuest')
			Select oGuest
			Scatter Memvar 	
			Insert Into nGuest From Memvar 	
		Endif 
		Select oRoomHead
		Delete 
	Endif 
	Select oRoomHead
EndScan 
? 'Procedure Ar2Return--END'
? ''
? '===Finnish===='
Close Data
Return 




Procedure ClsDate && เริ่มลบข้อมูลเก่าๆ 
? 'Procedure ClsDate--Start'
Close Data
***
Use Room Exclu 
? '==>ClsDate '+Dbf()
Delete All For !Empty(RoomDate) .and. Status='B' .and. Room4Flag='D' .and. RoomDate<mDateCls
Pack 
Close Data
***
Use RoomIN Exclu 
? '==>ClsDate '+Dbf()
Delete All For !Empty(RoomDate) .and. RoomDate<mDateCls
Pack 
Close Data
***
Use RoomOpen Exclu 
? '==>ClsDate '+Dbf()
Delete All For !Empty(OpenDate) .and. OpenDate<mDateCls
Pack 
Close Data
***
Use ChgPrice Exclu 
? '==>ClsDate '+Dbf()
Delete All For !Empty(Date_Chg) .and. Date_Chg<mDateCls
Pack 
Close Data
***
Use DoorOpen Exclu 
? '==>ClsDate '+Dbf()
Delete All For !Empty(OpenDrDt) .and. OpenDrDt<mDateCls
Pack 
Close Data
***
Use LoGinUse Exclu 
? '==>ClsDate '+Dbf()
Delete All For !Empty(LoGinSysDT) .and. LoGinSysDT<mDateCls
Pack 
Close Data
? '==>ClsDate ,'
***
Use RoomHead In 0 Exclu Alias RoomHead Order RmCode 
Use RoomTran In 0 Exclu Alias RoomTran Order RmCode
Use RoomHD2 In 0 Exclu Alias RoomHD2 Order RmCode
Use Room In 0 Share Noupdate Alias Room 
Use RoomCode In 0 Share Noupdate Alias RoomCode
Create Cursor TempCode (mRmCode C(10))
Index On mRmCode Tag mRmCode
Selec RoomCode 
Scan 
	If !Empty(RoomCode.Rent_No) 
		m.mRmCode=RoomCode.Rent_No	
		Insert Into TempCode From Memvar 
	Endif 
	If !Empty(RoomCode.Rent_n_No) 
		m.mRmCode=RoomCode.Rent_n_No
		Insert Into TempCode From Memvar 
	Endif 
EndScan 
Select Room 
Scan 
	If !Empty(Room.BkFirstNo) 
		m.mRmCode=Room.BkFirstNo
		Insert Into TempCode From Memvar 
	Endif 

	If !Empty(Room.ReCeiptno) 
		m.mRmCode=Room.ReCeiptno
		Insert Into TempCode From Memvar 
	Endif 
	If !Empty(Room.FirstDocno) 
		m.mRmCode=Room.FirstDocno
		Insert Into TempCode From Memvar 
	Endif 
EndScan 
Select RoomHead 
Scan 
	Do Case 
	Case Empty(RoomHead.HoSysDate)
		Delete 
	Case Seek(RoomHead.RmCode,'TempCode')
		****
	Case HoSysDate<mDateCls
		Delete 
	EndCase
EndScan
Select RoomHead 
Pack 
****
Select RoomTran 
Scan 
	If !Seek(RoomTran.RmCode,'RoomHead')
		Delete 
	Endif 
EndScan 
Select RoomTran 
Pack 
****
Select RoomHD2 
Scan 
	If !Seek(RoomHD2.RmCode,'RoomHead')
		Delete 
	Endif 
EndScan 
Select RoomHD2 
Pack 
Close Data
? 'Procedure ClsDate--END'
? ''
Return 




Procedure ClsGuest  && ลบลูกค้าที่ไม่มีการใช้งาน
? 'Procedure ClsGuest--Start'
Close Data
Use Room In 0 Exclu Alias Room 
Use Guest In 0 Exclu Order GuestCode Alias Guest 
Use Guesttrn In 0 Exclu Order GuestCode Alias Guesttrn
Use (mDirBACK+'\RoomHead') In 0 Share Noupdate Alias HeadTran 
Select HeadTran
If Tagno('GuestCode')=0
	? Dbf()+' No found Index GuestCode '
	? ''
	? 'หยุดการทำงาน ให้ทำงานใหม่'
	Wait 
	Close Data
	Use (mDirBACK+'\RoomHead') Exclu
	Index On GuestCode Tag GuestCode 
	Close Data
	Return 
Endif 
Set Order To GuestCode
Select Room 
If Tagno('GuestCode')=0
	Index On GuestCode Tag GuestCode 
Endif 
Set Order To GuestCode
Select HeadTran
Set Filter To Year(HoSysDate)>=mYearRoom
Select Guest
Scan 
	If !Seek(Guest.GuestCode,'HeadTran') .and. !Seek(Guest.GuestCode,'Room')
		? '==>ลบ '+Alltrim(Guest.GuestName)
		Delete 
	Endif 
EndScan 
Select Guest
Pack 
***
Select GuestTrn 
Scan 
	If !Seek(GuestTrn.GuestCode,'Guest')
		? '==>ลบ '+Alltrim(GuestTrn.GuestCode)
		Delete 
	Endif 
EndScan 
Select GuestTrn 
Pack 
Select HeadTran
Set Filter To 
Close Data
? 'Procedure ClsGuest--END'
? ''
Return 



Procedure DupGuest && ลบลูกค้าที่ชื่อซ้ำกัน 
? 'Procedure DupGuest--Start'
Close Data
Use Room In 0 Exclu Alias Room 
Use Guest In 0 Exclu Alias Guest Order GuestName 
Use RoomHead In 0 Share Noupdate Alias RoomHead 
Select RoomHead
If Tagno('GuestCode')=0
	? Dbf()+' No found Index GuestCode '
	? 'หยุดการทำงาน ให้ทำงานใหม่'
	Wait 
	Close Data
	Use RoomHead Exclu
	Index On GuestCode Tag GuestCode 
	Close Data
	Return 
Endif 
Set Order To GuestCode
Sele Room 
If Tagno('GuestCode')=0
	Index On GuestCode Tag GuestCode 
Endif 
Set Order To GuestCode
Selec Guest
mDupName='XXX'
Scan 
	If Guest.GuestName=mDupName .and. !Seek(Guest.GuestCode,'RoomHead') .and. !Seek(Guest.GuestCode,'Room')
		? '==>ลบ '+Alltrim(Guest.GuestName)
		Delete 
	Endif
	mDupName=Guest.GuestName
EndScan 
Selec Guest
Pack 
Close Data
? 'Procedure DupGuest--END'
? ''
Return 


