Advertisement
Guest User

Untitled

a guest
Apr 4th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.90 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7. using System;
  8.  
  9. namespace BH_ZmSalesEventsReport
  10. {
  11.     public class Program : BusinessObjects.Interfaces.IRuntime
  12.     {
  13.         public int Start   { get { return 500; } }
  14.         public int Sleep   { get { return 500; } }
  15.         private static string todayDate = DateTime.Now.ToString("d");
  16.         private static string reportsSendingLogPath = Lift.LiftManager.ConfigProvider.GetValue("LogDir") + "reports_sending.log";
  17.         private const string LOG_TAG = "ZM_SALES_EVENTS_MANAGER";
  18.  
  19.         public void Run(object obj)
  20.         {
  21.  
  22.             if (IsToday7DaysToMonthEnd())
  23.             {                
  24.                 Lift.LiftManager.Logger.Write(LOG_TAG, "Today is the date to send ZM Selling Events Reports.");
  25.                 Console.WriteLine(todayDate);
  26.                 if (!wereReportsAlreadySentToday())
  27.                 {
  28.                     CallReportGenerator();
  29.                     WriteReporstSendDateToFile();
  30.                     Lift.LiftManager.Logger.Write(LOG_TAG, "Reports have been sent.");
  31.                 }
  32.                 else
  33.                 {
  34.                     Lift.LiftManager.Logger.Write(LOG_TAG, "Reports have already been sent today.");
  35.                 }
  36.             }
  37.         }
  38.  
  39.         private static void WriteReporstSendDateToFile()
  40.         {
  41.             Lift.LiftManager.Logger.Write(LOG_TAG, "Preparing to write '{0}' to reports_sending.log", todayDate);
  42.             try
  43.             {
  44.                 using (StreamWriter sw = File.CreateText(reportsSendingLogPath))
  45.                 {
  46.                     sw.WriteLine(todayDate);
  47.                     Lift.LiftManager.Logger.Write(LOG_TAG, "Reports_sending.log was updated");
  48.                 }
  49.              }
  50.             catch (Exception e)
  51.             {
  52.                 Lift.LiftManager.Logger.Write(LOG_TAG, e.ToString());
  53.             }
  54.         }
  55.  
  56.         private static bool wereReportsAlreadySentToday()
  57.         {
  58.             List<string> reportsSendingDates = null;
  59.             try
  60.             {
  61.                 reportsSendingDates = File.ReadLines(reportsSendingLogPath).ToList();
  62.             }
  63.             catch (Exception e)
  64.             {
  65.                 Lift.LiftManager.Logger.Write(LOG_TAG, e.ToString());
  66.             }
  67.            
  68.             if(reportsSendingDates != null && reportsSendingDates.Contains(todayDate))
  69.             {
  70.                 return true;
  71.             }
  72.             return false;
  73.         }
  74.  
  75.         public void Stop()
  76.         {
  77.             throw new NotImplementedException();
  78.         }
  79.  
  80.         private static bool IsToday7DaysToMonthEnd()
  81.         {
  82.             DateTime currentDate = DateTime.Today;
  83.  
  84.             int currentMonth = currentDate.Month;
  85.             int currentYear = currentDate.Year;
  86.             int currentDay = currentDate.Day;
  87.  
  88.             int daysInCurrentMonth = DateTime.DaysInMonth(currentYear, currentMonth);
  89.  
  90.             int daysBeforeMonthEnd = 7;
  91.  
  92.             int dayWhenReportShouldBeGenerated = daysInCurrentMonth - daysBeforeMonthEnd;
  93.  
  94.             if (currentDay == dayWhenReportShouldBeGenerated)
  95.             {
  96.                 return true;
  97.             }
  98.             return false;
  99.         }
  100.  
  101.         private static void CallReportGenerator()
  102.         {
  103.             try
  104.             {
  105.                 System.ServiceModel.BasicHttpBinding binding = new System.ServiceModel.BasicHttpBinding();
  106.                 binding.Security.Mode = System.ServiceModel.BasicHttpSecurityMode.TransportCredentialOnly;
  107.                 binding.Security.Transport.ClientCredentialType = System.ServiceModel.HttpClientCredentialType.Basic;
  108.  
  109.                 FASR.IntegrationClient proxy =
  110.                     new FASR.IntegrationClient(binding, new System.ServiceModel.EndpointAddress(Lift.LiftManager.ConfigProvider.GetValue("wsdl_url")));
  111.  
  112.                 proxy.ClientCredentials.UserName.UserName = Lift.LiftManager.ConfigProvider.GetValue("wsdl_user");
  113.                 proxy.ClientCredentials.UserName.Password = Lift.LiftManager.ConfigProvider.GetValue("wsdl_password");
  114.  
  115.                 var generateReportsForZM = new FASR.RequiredSellingEventReport_GenerateReportsForZM_Call();
  116.                 generateReportsForZM.OnBehalfOf = Lift.LiftManager.ConfigProvider.GetValue("wsdl_user");
  117.                 generateReportsForZM.Record = new FASR.RequiredSellingEventReportRecord();
  118.  
  119.                 FASR.ExecuteRequest executeRequest = new FASR.ExecuteRequest();
  120.                 executeRequest.OperationCalls = new FASR.OperationCall[1];
  121.                 executeRequest.OperationCalls[0] = generateReportsForZM;
  122.  
  123.                 proxy.Execute(executeRequest);
  124.             }
  125.             catch (Exception e)
  126.             {
  127.                 Lift.LiftManager.Logger.Write(LOG_TAG, e.ToString());
  128.             }
  129.         }
  130.  
  131.     }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement