Sindbad~EG File Manager

Current Path : /home/numerotech/mcinfo.numerotech.com/mc_info_update/core/model/
Upload File :
Current File : //home/numerotech/mcinfo.numerotech.com/mc_info_update/core/model/CMEPaymentModel.py

from sqlalchemy import create_engine, MetaData, Table, insert, select,update,delete,text
from sqlalchemy.sql import and_, or_
from core import app
import json

engine      = create_engine(app.config['DATABASE_URI'],pool_size=5000,pool_recycle=3600)


class CMEPaymentModel():
	def __init__(self):
		try:
			self.meta = MetaData()
			self.cme_attachments = Table("cme_attachments", self.meta,  autoload_with=engine)
			# self.mc_delegates = Table("mc_delegates", self.meta,  autoload_with=engine)
			
				
		except Exception as e:
			print(e)

	def checkConfData_CMEPaymentModel(self,conf_id,conf_key):
		with engine.connect() as conn:
			stmt = text("select * from conference c inner join society_applications s on s.conf_id = c.conf_id and app_type_id = 2  inner join mail_setting  m on s.mail_setting_id = m.mail_setting_id where c.conf_id = "+str(conf_id)+" and c.conf_key = '"+conf_key+"' ;")
			result = conn.execute(stmt).first()
			conn.close()
			return dict(result._mapping) if result  else None


	def delegateDataByUUID_CME(self,conf_id,conf_key,user_uuid,conf_schema):
		print("delegateDataByUUID_CME")
		with engine.connect() as conn:
			stmt = text("select d.*, s.state_name as mc_state_name from "+str(conf_schema)+".delegates d left join states s on s.state_id = d.mc_state_id  and s.country_id = 101 " 
			+ " where d.conference_id = "+str(conf_id)+" and d.user_uuid = '"+user_uuid+"'  and d.delegate_no is not null and d.delegate_no > 0 ;")
			result = conn.execute(stmt).first()
			conn.close()
			return dict(result._mapping) if result  else None		

	def getStates_CMEPaymentModel(self,country_id):
		with engine.connect() as conn:
			stmt = text("select * from states s where s.country_id = "+str(country_id)+" ;")
			result  = conn.execute(stmt).all()
			results = [dict(r._mapping) for r in result] if result else None
			conn.close()
			return results if results else None					



	def updateData_CMEPaymentModel(self,data,user_uuid,email,conf_id,conf_key,conf_schema):
		self.delegates = Table("delegates", self.meta,schema=conf_schema,  autoload_with=engine)
		with engine.connect() as conn:
			try:
				update_stmt = self.delegates.update().where(self.delegates.c.user_uuid.in_([user_uuid])).values(data) 
				conn.execute(update_stmt)
				conn.commit()
				conn.close()
				return "success"
			except Exception as e:
				conn.close()
				return str(e)

	# ------------------------ March 24, 2023 --------
	def delegateDataByDelegateNo_CMEPaymentModel(self,conf_id,conf_key,delegate_no,conf_schema):
		with engine.connect() as conn:
			stmt = text("select d.*, s.state_name as mc_state_name from "+str(conf_schema)+".delegates d left join states s on s.state_id = d.mc_state_id  and s.country_id = 101 " 
			+ " where d.conference_id = "+str(conf_id)+" and d.delegate_no = "+str(delegate_no)+"  and d.delegate_no is not null and d.delegate_no > 0 limit 1 ;")
			result = conn.execute(stmt).first()
			conn.close()
			return dict(result._mapping) if result  else None



	def updateMCData_CMEPaymentModel(self,data,delegate_no,email,conf_id,conf_key,mc_number,state_name,curr_dt,del_table_name,conf_schema):
		self.delegates = Table("delegates", self.meta,schema=conf_schema,  autoload_with=engine)
		with engine.connect() as conn:
			try:
				update_stmt = self.delegates.update().where(
					and_(
						self.delegates.c.delegate_no == delegate_no,
						self.delegates.c.conference_id == conf_id
				)
				).values(data) 
				conn.execute(update_stmt)
				conn.commit()
				conn.close()
				return "success"
			except Exception as e:
				conn.close()
				return str(e)
				
	def updateMCDataNew_CMEPaymentModel(self,data_for_update,delegate_no,conf_id,update_stmt,conf_schema):
		self.delegates = Table("delegates", self.meta,schema=conf_schema,  autoload_with=engine)
		with engine.connect() as conn:
			try:
				stmt = self.delegates.update().where(
					and_(
						self.delegates.c.delegate_no == delegate_no,
						self.delegates.c.conference_id == conf_id
				)
				).values(data_for_update) 
				conn.execute(stmt)
				conn.commit()
				conn.close()
				return "success"
			except Exception as e:
				conn.close()
				return str(e)			

	def getMCState_CMEPaymentModel(self,mc_state_id):	
		with engine.connect() as conn:
			stmt = text("SELECT state_name from states where state_id = "+str(mc_state_id)+" ; ")
			result = conn.execute(stmt).first()
			conn.close()
			return dict(result._mapping) if result  else None
		
	def insert_delegate_in_mc_delegates_CMEPaymentModel(self,conf_id,delegate_no,curr_dt,conf_schema):
		with engine.connect() as conn:
			try:
				stmt = text("insert into mc_delegates (delegate_no,prefix,full_name,email,mobile,mc_number,mc_state_id,role,reg_type_id,conference_id,created_at) select delegate_no,prefix,full_name,email,mobile,mc_number,mc_state_id,role,reg_type_id,conference_id,'"+curr_dt+"' from "+str(conf_schema)+".delegates where conference_id ="+str(conf_id)+" and delegate_no = "+str(delegate_no)+" and delegate_no is not null;")
				conn.execute(stmt)
				conn.commit()
				conn.close()
				return "success"
			except Exception as e:
				conn.close()
				return str(e)	

	def check_delegates_email_CMEPaymentModel(self,conf_id,email,delegate_no,conf_schema):
		with engine.connect() as conn:
			stmt_2 = text("select distinct u.user_type from user_types u "
					+" inner join addons  a on a.user_type_id = u.user_type_id "
					+" where a.conference_id = "+str(conf_id)+" and u.is_spouse = 1;")
			result_2 = conn.execute(stmt_2).fetchone()
			
			result_2 = dict(result_2._mapping)
			
			spouse   = result_2['user_type']
			if spouse :
				stmt = text("select * from "+str(conf_schema)+".delegates where conference_id = "+str(conf_id)+" and email = '"+str(email)+"' and delegate_no <> "+str(delegate_no)+"  and role not in ('"+str(spouse)+"') and  delegate_no is not null ;")
			else :
				stmt = text("select * from "+str(conf_schema)+".delegates where conference_id = "+str(conf_id)+" and email = '"+str(email)+"' and delegate_no <> "+str(delegate_no)+"  and  delegate_no is not null ;")
			
			result = conn.execute(stmt).first()
			conn.close()
			return dict(result._mapping) if result  else None
			

	def check_delegates_mobile_CMEPaymentModel(self,conf_id,mobile,delegate_no,conf_schema):
		with engine.connect() as conn:
			stmt_2 = text("select distinct u.user_type from user_types u "
					+" inner join addons  a on a.user_type_id = u.user_type_id "
					+" where a.conference_id = "+Str(conf_id)+" and u.is_spouse = 1;")
			result_1 = conn.execute(stmt_1).fetchone()
			result_2 = dict(result_1._mapping)
			spouse   = result_2['user_type']
			if spouse :
				stmt = text("select * from "+str(conf_schema)+".delegates where conference_id = "+str(conf_id)+" and mobile = '"+str(mobile)+"' and delegate_no <> "+str(delegate_no)+"  and role not in ('"+str(spouse)+"') and  delegate_no is not null ;")
			else :
				stmt = text("select * from "+str(conf_schema)+".delegates where conference_id = "+str(conf_id)+" and mobile = '"+str(mobile)+"' and delegate_no <> "+str(delegate_no)+"  and delegate_no is not null ;")
			
			result = conn.execute(stmt).first()
			conn.close()
			return dict(result._mapping) if result  else None



# -------------------------------------------------------------------
	
	def get_delegate(self,delegate_id,conf_id,conf_schema):
		with engine.connect() as conn:
			stmt    = text("select d.*,s.state_name,r.role as user_role from "+conf_schema+".delegates d inner join conference c on c.conf_id = d.conference_id left join users u on u.user_id = d.user_id left join user_roles ur on ur.society_id = u.society_id and c.conf_id = ur.conf_id and u.user_id = ur.user_id left join roles r on r.role_id = ur.role_id  left join states s on d.state_id = s.state_id where d.delegate_id = "+str(delegate_id)+" and d.conference_id ="+str(conf_id)+";")
			result  = conn.execute(stmt).fetchone()
			conn.close()
			return dict(result._mapping) if result else None

	def insert_payment_screenshot(self,update_payment_proof):
		with engine.connect() as conn:
			result  = conn.execute(self.cme_attachments.insert(), update_payment_proof)
			conn.commit()
			conn.close()
			return result.lastrowid

	def usp_conf_cme_generate_payment(self,unique_id,delegate_ids,user_id,payment_for,payment_method,remarks,full_name,email,mobile,is_generate,created_at,txn_id,society_id,app_type,conf_id,amount):
		connection = engine.raw_connection()
		cursor = connection.cursor()
		cursor.callproc("usp_conf_cme_generate_payment",[unique_id,delegate_ids,user_id,payment_for,payment_method,remarks,full_name,email,mobile,is_generate,created_at,txn_id,society_id,app_type,conf_id,amount])
		if cursor.description : 
			columns = [column[0] for column in cursor.description]
			results = []
			for row in cursor.fetchall():
				results.append(dict(zip(columns, row)))
			cursor.close()
			connection.commit()
			connection.close()
			return results
		else :
			cursor.close()
			connection.commit()
			connection.close()
			return None

	def check_cme_payment(self,conf_id,delegate_id,conf_schema):
		with engine.connect() as conn:
			stmt    = text("select d.delegate_id from "+conf_schema+".delegates d inner join cme_payment cp on cp.delegate_ids = d.delegate_id where d.delegate_id = "+str(delegate_id)+" and d.conference_id ="+str(conf_id)+" and d.del_status_id = 2 and cp.status = 'success' and d.is_cme_paid = 1 ;")
			result  = conn.execute(stmt).fetchone()
			conn.close()
			return dict(result._mapping) if result else None

	def get_cme_delegate_data(self,conf_id,delegate_no):
		sets = []
		try:
			connection = engine.raw_connection()
			cursor = connection.cursor()
			cursor.callproc("usp_conf_cme_get_data",[(conf_id or None),(delegate_no or None)])
			while 1:
				#(column_name, type_, ignore_, ignore_, ignore_, null_ok, column_flags)
				names = [c[0] for c in cursor.description]
				set_ = []
				while 1:
					row_raw = cursor.fetchone()
					if row_raw is None:
						break
						
					row = dict(zip(names, row_raw))
					set_.append(row)
	 
				sets.append(list(set_))
	 
				if cursor.nextset() is None:
					break
	 
				# nextset() doesn't seem to be sufficiant to tell the end.
				if cursor.description is None:
					break
		finally:
			# Return the connection to the pool (won't actually close).
			connection.close()
			
		return sets


Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists