Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.IO;
- using System;
- namespace BH_ZmSalesEventsReport
- {
- public class Program : BusinessObjects.Interfaces.IRuntime
- {
- public int Start { get { return 500; } }
- public int Sleep { get { return 500; } }
- private static string todayDate = DateTime.Now.ToString("d");
- private static string reportsSendingLogPath = Lift.LiftManager.ConfigProvider.GetValue("LogDir") + "reports_sending.log";
- private const string LOG_TAG = "ZM_SALES_EVENTS_MANAGER";
- public void Run(object obj)
- {
- if (IsToday7DaysToMonthEnd())
- {
- Lift.LiftManager.Logger.Write(LOG_TAG, "Today is the date to send ZM Selling Events Reports.");
- Console.WriteLine(todayDate);
- if (!wereReportsAlreadySentToday())
- {
- CallReportGenerator();
- WriteReporstSendDateToFile();
- Lift.LiftManager.Logger.Write(LOG_TAG, "Reports have been sent.");
- }
- else
- {
- Lift.LiftManager.Logger.Write(LOG_TAG, "Reports have already been sent today.");
- }
- }
- }
- private static void WriteReporstSendDateToFile()
- {
- Lift.LiftManager.Logger.Write(LOG_TAG, "Preparing to write '{0}' to reports_sending.log", todayDate);
- try
- {
- using (StreamWriter sw = File.CreateText(reportsSendingLogPath))
- {
- sw.WriteLine(todayDate);
- Lift.LiftManager.Logger.Write(LOG_TAG, "Reports_sending.log was updated");
- }
- }
- catch (Exception e)
- {
- Lift.LiftManager.Logger.Write(LOG_TAG, e.ToString());
- }
- }
- private static bool wereReportsAlreadySentToday()
- {
- List<string> reportsSendingDates = null;
- try
- {
- reportsSendingDates = File.ReadLines(reportsSendingLogPath).ToList();
- }
- catch (Exception e)
- {
- Lift.LiftManager.Logger.Write(LOG_TAG, e.ToString());
- }
- if(reportsSendingDates != null && reportsSendingDates.Contains(todayDate))
- {
- return true;
- }
- return false;
- }
- public void Stop()
- {
- throw new NotImplementedException();
- }
- private static bool IsToday7DaysToMonthEnd()
- {
- DateTime currentDate = DateTime.Today;
- int currentMonth = currentDate.Month;
- int currentYear = currentDate.Year;
- int currentDay = currentDate.Day;
- int daysInCurrentMonth = DateTime.DaysInMonth(currentYear, currentMonth);
- int daysBeforeMonthEnd = 7;
- int dayWhenReportShouldBeGenerated = daysInCurrentMonth - daysBeforeMonthEnd;
- if (currentDay == dayWhenReportShouldBeGenerated)
- {
- return true;
- }
- return false;
- }
- private static void CallReportGenerator()
- {
- try
- {
- System.ServiceModel.BasicHttpBinding binding = new System.ServiceModel.BasicHttpBinding();
- binding.Security.Mode = System.ServiceModel.BasicHttpSecurityMode.TransportCredentialOnly;
- binding.Security.Transport.ClientCredentialType = System.ServiceModel.HttpClientCredentialType.Basic;
- FASR.IntegrationClient proxy =
- new FASR.IntegrationClient(binding, new System.ServiceModel.EndpointAddress(Lift.LiftManager.ConfigProvider.GetValue("wsdl_url")));
- proxy.ClientCredentials.UserName.UserName = Lift.LiftManager.ConfigProvider.GetValue("wsdl_user");
- proxy.ClientCredentials.UserName.Password = Lift.LiftManager.ConfigProvider.GetValue("wsdl_password");
- var generateReportsForZM = new FASR.RequiredSellingEventReport_GenerateReportsForZM_Call();
- generateReportsForZM.OnBehalfOf = Lift.LiftManager.ConfigProvider.GetValue("wsdl_user");
- generateReportsForZM.Record = new FASR.RequiredSellingEventReportRecord();
- FASR.ExecuteRequest executeRequest = new FASR.ExecuteRequest();
- executeRequest.OperationCalls = new FASR.OperationCall[1];
- executeRequest.OperationCalls[0] = generateReportsForZM;
- proxy.Execute(executeRequest);
- }
- catch (Exception e)
- {
- Lift.LiftManager.Logger.Write(LOG_TAG, e.ToString());
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement