PageRenderTime 51ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/App_Code/Account/DSAccount.cs

https://github.com/mailekah/AgapeConnect1
C# | 380 lines | 278 code | 77 blank | 25 comment | 35 complexity | a26dd2ded3af3584d6dcb3212a0cee55 MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using MinistryViewDS;
  6. using DotNetNuke.Entities.Users;
  7. using DotNetNuke.Entities.Portals;
  8. using DotNetNuke.Entities.Modules;
  9. using System.IO;
  10. using System.Xml;
  11. /// <summary>
  12. /// Summary description for Account
  13. /// </summary>
  14. public class DSAccount
  15. {
  16. #region Constructors
  17. public DSAccount(string countryUrl, string ssoGuid, string accountCode, DateTime dateFrom, DateTime dateTo, string staffProfileCode)
  18. {
  19. _countryUrl = countryUrl;
  20. _ssoGuid = ssoGuid;
  21. if (dateFrom == null)
  22. { _dateFrom = FirstDayOfMonthFromDateTime(DateTime.Today); }
  23. else
  24. { _dateFrom = dateFrom; }
  25. if (dateTo == null)
  26. { _dateTo = LastDayOfMonthFromDateTime(DateTime.Today); }
  27. else
  28. { _dateTo = dateTo; }
  29. if (accountCode == null)
  30. { _accountCode = ""; }
  31. else
  32. { _accountCode = accountCode; }
  33. if (staffProfileCode == null)
  34. { _profileCode = getProfile(accountCode); }
  35. else
  36. { _profileCode = staffProfileCode; }
  37. RefreshData();
  38. }
  39. #endregion
  40. #region Non-Static Properties
  41. private DateTime? _dateFrom;
  42. public DateTime? DateFrom
  43. {
  44. get { return _dateFrom; }
  45. set
  46. {
  47. _dateFrom = value;
  48. }
  49. }
  50. private DateTime? _dateTo;
  51. public DateTime? DateTo
  52. {
  53. get { return _dateTo; }
  54. set
  55. {
  56. _dateTo = value;
  57. }
  58. }
  59. private string _profileCode;
  60. public string ProfileCode
  61. {
  62. get { return _profileCode; }
  63. set
  64. {
  65. _profileCode = value;
  66. }
  67. }
  68. private string _accountCode;
  69. public string AccountCode
  70. {
  71. get { return _accountCode; }
  72. set
  73. {
  74. _profileCode = value;
  75. _profileCode = getProfile(_accountCode);
  76. }
  77. }
  78. private FinancialAccount[] _financialAccount;
  79. public FinancialAccount[] FinancialAccount
  80. {
  81. get { return _financialAccount; }
  82. set { _financialAccount = value; }
  83. }
  84. private IQueryable<FinancialTransaction> _transactions;
  85. public IQueryable<FinancialTransaction> Transactions
  86. {
  87. get
  88. {
  89. if(_transactions==null) getTransactions();
  90. return _transactions;
  91. }
  92. }
  93. private string _countryUrl;
  94. public string CountryUrl
  95. {
  96. get { return _countryUrl; }
  97. set
  98. {
  99. _countryUrl = value;
  100. }
  101. }
  102. #endregion
  103. #region Static Properties
  104. private string _pgtId { get; set; }
  105. private string _ssoGuid { get; set; }
  106. private int _portalId { get; set; }
  107. private int _userId { get; set; }
  108. private MinistryViewDSServices _tnt = new MinistryViewDSServices();
  109. private myAccounts _myAccount;
  110. public myAccounts MyAccounts
  111. {
  112. get { return _myAccount; }
  113. set { getSummary(); }
  114. }
  115. #endregion
  116. #region Basic Methods
  117. private DateTime FirstDayOfMonthFromDateTime(DateTime dateTime)
  118. {
  119. return new DateTime(dateTime.Year, dateTime.Month, 1);
  120. }
  121. private DateTime LastDayOfMonthFromDateTime(DateTime dateTime)
  122. {
  123. DateTime firstDayOfTheMonth = new DateTime(dateTime.Year, dateTime.Month, 1);
  124. return firstDayOfTheMonth.AddMonths(1).AddDays(-1);
  125. }
  126. private string getProfile(string AccountCode)
  127. {
  128. return _myAccount
  129. .Countries.Where<Country>(c => c.URL == _countryUrl).First<Country>()
  130. .Profiles.Where(cp => AccountCode == cp.Accounts
  131. .Where<FinancialAccounts>(ac => ac.AccountID == AccountCode).First().AccountID.ToString())
  132. .First().ProfileCode.ToString();
  133. }
  134. public double BalanceForAccount(string AccountCode)
  135. {
  136. return _myAccount.Countries.Where<Country>(c => c.URL == _countryUrl).First<Country>()
  137. .Profiles.Where<CountryProfile>(p => p.ProfileCode == _profileCode).First()
  138. .Accounts.Where<FinancialAccounts>(a => a.AccountID == AccountCode)
  139. .Sum<FinancialAccounts>(accts => accts.Balance);
  140. }
  141. public double BalanceForProfile(string ProfileCode)
  142. {
  143. return _myAccount.Countries.Where<Country>(c => c.URL == _countryUrl).First<Country>()
  144. .Profiles.Where<CountryProfile>(p => p.ProfileCode == _profileCode).First()
  145. .Accounts.Sum<FinancialAccounts>(accts => accts.Balance);
  146. }
  147. public void RefreshData()
  148. {
  149. getSummary();
  150. }
  151. #endregion
  152. #region Web Service Methods
  153. private void getSummary()
  154. {
  155. if ((_ssoGuid == null)||(_pgtId == null))
  156. {
  157. setProps();
  158. }
  159. if (string.IsNullOrEmpty(_pgtId))
  160. {
  161. setPgtId();
  162. }
  163. try
  164. {
  165. _myAccount = _tnt.GetSummary(_pgtId, _ssoGuid);
  166. if (_myAccount == null)
  167. {
  168. }
  169. }
  170. catch (Exception)
  171. {
  172. _myAccount = null;
  173. }
  174. }
  175. static public string GetProxyTicketFromCAS(string targetService, string _pgt)
  176. {
  177. string pt = string.Empty;
  178. string server = "https://thekey.me/cas/";
  179. string validateurl = server + "proxy?targetService=" + targetService + "&pgt=" + _pgt.Trim().ToString();
  180. System.IO.Stream s;
  181. try
  182. {
  183. System.Net.WebClient wc = new System.Net.WebClient();
  184. s = wc.OpenRead(validateurl);
  185. }
  186. catch (Exception e)
  187. {
  188. // error
  189. return pt;
  190. }
  191. StreamReader streamReader = new StreamReader(s);
  192. XmlDocument doc = new XmlDocument();
  193. doc.Load(streamReader);
  194. XmlNamespaceManager NamespaceMgr = new XmlNamespaceManager(doc.NameTable);
  195. NamespaceMgr.AddNamespace("cas", "http://www.yale.edu/tp/cas");
  196. XmlNode SuccessNode = doc.SelectSingleNode("/cas:serviceResponse/cas:proxySuccess", NamespaceMgr);
  197. if (!(SuccessNode == null))
  198. {
  199. XmlNode ProxyTicketNode = SuccessNode.SelectSingleNode("./cas:proxyTicket", NamespaceMgr);
  200. if (!(ProxyTicketNode == null))
  201. return ProxyTicketNode.InnerText;
  202. }
  203. return pt;
  204. }
  205. static public double getAccountBalance(string GUID, string PGTIOU, string CountryURL, string AccountCode)
  206. {
  207. string PGTID = (string) HttpContext.Current.Session["pgtId"] ;
  208. if(PGTID==null)
  209. PGTID = new theKeyProxyTicket.PGTCallBack().RetrievePGTCallback("CASAUTH", "thecatsaysmeow3", PGTIOU);
  210. HttpContext.Current.Session.Add("pgtId", PGTID) ;
  211. var dTnT = new dynamicTnT.TntMPDDataServerWebService ();
  212. dTnT.Url = CountryURL + "dataquery/dataqueryservice.asmx";
  213. dTnT.Discover();
  214. string sessionId = (string)HttpContext.Current.Session["TnT-" + dTnT.Url];
  215. if (string.IsNullOrEmpty(sessionId))
  216. {
  217. string service = dTnT.GetServiceName();
  218. string pt = GetProxyTicketFromCAS(service, PGTID);
  219. string UserName;
  220. bool IsReg;
  221. sessionId = dTnT.Login(service, pt, true, out UserName, out IsReg);
  222. HttpContext.Current.Session.Add("TnT-" + dTnT.Url, sessionId);
  223. }
  224. var resp = dTnT.GetStaffProfiles(sessionId);
  225. foreach( var prof in resp)
  226. {
  227. dynamicTnT.FinancialAccount[] fa ;
  228. bool isT;
  229. var desg= dTnT.GetStaffProfileSummary(sessionId, prof.Code,out fa,out isT );
  230. var mycodes= fa.Where(x => x.Code==AccountCode);
  231. if( mycodes.Count() >0)
  232. return (double) mycodes.First().EndingBalance ;
  233. }
  234. return 0;
  235. // tntTransactions = dTnT.GetFinancialTransactions(sessionId, MyProfiles.SelectedValue, _startDate, _endDate, (MyAccounts.SelectedValue == "All Accounts" ? "" : MyAccounts.SelectedValue), false, out tntAccounts);
  236. // StartingBalance.Text = tntAccounts.Sum(x => x.BeginningBalance).ToString("0");
  237. //mvds.Url = CountryURL + "dataquery/dataqueryservice.asmx";
  238. // mvds.Discover() ;
  239. // string service = mvds.GetServiceName(
  240. // var x = mvds.login(
  241. // myAccounts acs = new MinistryViewDSServices().GetSummary(PGTID, GUID);
  242. // var ctry = acs.Countries.Where<Country>(c => c.URL == CountryURL) ;
  243. // if(ctry.Count()>0)
  244. // {
  245. // var pFiles = from c in ctry.First().Profiles where c.Accounts.Any(ac => ac.AccountID == AccountCode) select c ;
  246. // if (pFiles.Count()>0)
  247. // {
  248. // var Acct = from c in pFiles.First().Accounts where c.AccountID==AccountCode select c.Balance ;
  249. // if (Acct.Count()>0)
  250. // return Acct.First();
  251. // }
  252. // }
  253. // return 0.0;
  254. }
  255. public void getTransactions()
  256. {
  257. if (!(string.IsNullOrEmpty(_countryUrl) || string.IsNullOrEmpty(_ssoGuid)))
  258. {
  259. try
  260. {
  261. _transactions = _tnt.GetFinancialTransactions(_countryUrl,
  262. _ssoGuid, _profileCode,
  263. (DateTime)_dateFrom, (DateTime)_dateTo, _accountCode,
  264. false, ref _financialAccount).AsQueryable<FinancialTransaction>();
  265. }
  266. catch (Exception e)
  267. {
  268. _transactions = null;
  269. }
  270. }
  271. else { _transactions = null; }
  272. }
  273. private void setProps()
  274. {
  275. PortalSettings portalInfo = (PortalSettings)HttpContext.Current.Items["PortalSettings"];
  276. _userId = portalInfo.UserId;
  277. _portalId = portalInfo.PortalId;
  278. _ssoGuid = UserController.GetUserById(_portalId, _userId).Profile.GetPropertyValue("ssoGUID");
  279. }
  280. private void setPgtId()
  281. {
  282. string pgtiou = UserController.GetUserById(_portalId, _userId).Profile.GetPropertyValue("GCXPGTIOU");
  283. if (!string.IsNullOrEmpty(pgtiou))
  284. {
  285. try
  286. {
  287. _pgtId = new theKeyProxyTicket.PGTCallBack().RetrievePGTCallback("CASAUTH", "thecatsaysmeow3", pgtiou);
  288. HttpContext.Current.Session.Add("pgtId",_pgtId);
  289. }
  290. catch
  291. {
  292. _pgtId = string.Empty;
  293. }
  294. //KeyUser.KeyUser kUser = new KeyUser.KeyUser(new Guid("de4f5f1a-faf8-db81-ad4b-9e4244aabad7"), new Guid("3181c762-195d-4229-817e-c8f665ff3aa4"));
  295. //_pgtId = kUser.ProxyTicket; //= null;
  296. }
  297. else
  298. {
  299. _pgtId = string.Empty;
  300. }
  301. }
  302. #endregion
  303. }