Ejemplo n.º 1
0
        /// <summary>
        /// Downloads converted files from converter site, processes them as SettlementHistory
        /// and persists them to the database.
        /// <summary>
        public void Process(PantherClient pantherClient)
        {
            var getConverterResults  = converter.QueryAllAsync();
            var getSettlementHeaders = pantherClient.GetSettlementsAsync();

            Task.WaitAll(getConverterResults, getSettlementHeaders);

            IEnumerable <ZamzarResult> results           = getConverterResults.Result;
            List <SettlementHistory>   settlementHeaders = getSettlementHeaders.Result;

            List <Task> tasks = new List <Task>();

            foreach (ZamzarResult result in results)
            {
                string            settlementId     = GetSettlementId(result);
                SettlementHistory settlementHeader = GetBySettlementId(settlementHeaders, settlementId);
                if (settlementHeader != null)
                {
                    tasks.Add(ProcessResultAsync(result, settlementHeader));
                }
                else
                {
                    System.Console.WriteLine($"SettlementId {settlementId} not found on panther.");
                }
            }
            Task.WaitAll(tasks.ToArray());
        }
Ejemplo n.º 2
0
        public List <SettlementHistory> Parse(string html)
        {
            HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
            doc.LoadHtml(html);
            string path = "html/body/div[@class='container']/div[@class='row-fluid']/div[@class='span10']/div[@class='row-fluid']/table/tbody/tr";
            var    rows = doc.DocumentNode.SelectNodes(path);

            if (rows == null)
            {
                return(null);
            }

            List <SettlementHistory> settlements = new List <SettlementHistory>();

            foreach (var row in rows)
            {
                var nodes = row.SelectNodes("td");
                SettlementHistory settlement = ParseSettlement(nodes);
                if (settlement != null)
                {
                    settlements.Add(settlement);
                }
            }
            return(settlements);
        }
Ejemplo n.º 3
0
        private DateTime GetLastCreditDate(SettlementHistory settlement)
        {
            DateTime?creditDate = ConvertDate(
                settlement.Credits.OrderByDescending(s => ConvertDate(s.CreditDate))
                .FirstOrDefault().CreditDate);

            if (creditDate != null)
            {
                return((DateTime)creditDate);
            }
            else
            {
                return(settlement.SettlementDate);
            }

            DateTime?ConvertDate(string date)
            {
                if (!string.IsNullOrEmpty(date))
                {
                    return(DateTime.Parse(date));
                }
                else
                {
                    return(null);
                }
            }
        }
Ejemplo n.º 4
0
        private async Task DeleteSettlementAsync(CosmosClient cosmosClient, SettlementHistory settlement)
        {
            Container    container    = cosmosClient.GetContainer(databaseId, typeof(SettlementHistory).Name);
            PartitionKey partitionKey = new PartitionKey(settlement.CompanyId);
            await container.DeleteItemAsync <SettlementHistory>(settlement.SettlementId, partitionKey);

            System.Console.WriteLine($"Deleted: {settlement.SettlementId}");
        }
Ejemplo n.º 5
0
        public static SettlementHistory Parse(string filename)
        {
            SettlementHistoryParser parser = new SettlementHistoryParser(
                filename, GetSettlementIdFromFile(filename));
            SettlementHistory settlement = parser.Parse();

            return(settlement);
        }
Ejemplo n.º 6
0
        private DateTime GetSheetSettlementDate(SettlementHistory settlement)
        {
            DateTime sheetSettlementDate = settlement.SettlementDate.AddDays(7);

            if (sheetSettlementDate.DayOfWeek != DayOfWeek.Friday)
            {
                throw new ApplicationException($"Settlement date must be a Friday: {settlement.SettlementDate}");
            }
            return(sheetSettlementDate);
        }
Ejemplo n.º 7
0
        public async Task <SettlementHistory> GetSettlementAsync(string settlementId, string companyId)
        {
            using (var cosmosClient = GetCosmosClient())
            {
                Container container = cosmosClient.GetContainer(databaseId,
                                                                typeof(SettlementHistory).Name);

                SettlementHistory settlement =
                    await container.ReadItemAsync <SettlementHistory>(settlementId,
                                                                      new PartitionKey(double.Parse(companyId)));

                return(settlement);
            }
        }
Ejemplo n.º 8
0
        private int GetTruckForDriver(SettlementHistory settlement, string driver)
        {
            var credit = settlement.Credits.Where(c => c.Driver == driver)
                         .FirstOrDefault();

            if (credit != null)
            {
                return(credit.TruckId);
            }
            else
            {
                throw new ApplicationException($"Unable to find a truckid for driver {driver} in settlement {settlement.id}");
            }
        }
Ejemplo n.º 9
0
 public async Task SaveSettlementHistoryAsync(SettlementHistory settlement)
 {
     try
     {
         using (CosmosClient cosmosClient = GetCosmosClient())
         {
             await AddItemsToContainerAsync <SettlementHistory>(cosmosClient, settlement);
         }
     }
     catch (Exception e)
     {
         System.Console.WriteLine($"Error attempting to write settlement {settlement.SettlementId} to CosmosDb\n\t" + e.Message);
         throw e;
     }
 }
Ejemplo n.º 10
0
        public SettlementHistory Parse()
        {
            try
            {
                SettlementHistoryWorkbook workbook = new SettlementHistoryWorkbook(_filename);

                SettlementHistory settlement = new SettlementHistory();
                settlement.SettlementId   = this._settlementId;
                settlement.Credits        = GetCredits(workbook);
                settlement.Deductions     = GetDeductions(workbook);
                settlement.SettlementDate = GetLastCreditDate(settlement);
                return(settlement);
            }
            catch (Exception e)
            {
                System.Console.WriteLine($"Error parsing {_filename}: {_settlementId}.  Error:\n\t{e.Message}");
                return(null);
            }
        }
Ejemplo n.º 11
0
        private SettlementHistory ParseSettlement(HtmlNodeCollection nodes)
        {
            SettlementHistory settlement = null;

            if (nodes.Count > 4 && !IsVoid(nodes[5]))
            {
                settlement                 = new SettlementHistory();
                settlement.CompanyId       = companyId;
                settlement.SettlementId    = nodes[0].InnerText;
                settlement.SettlementDate  = ParseDate(nodes[1]);
                settlement.CheckAmount     = ParseDollar(nodes[2]);
                settlement.ARAmount        = ParseDollar(nodes[3]);
                settlement.DeductionAmount = ParseDollar(nodes[4]);
                //System.Console.WriteLine(settlement.SettlementId + ": " + settlement.CheckAmount);
            }


            return(settlement);
        }
Ejemplo n.º 12
0
        private bool SaveFileToDatabase(string filename, SettlementHistory settlement)
        {
            SettlementHistory parsedSettlement = SettlementHistoryParser.Parse(filename);

            if (parsedSettlement != null)
            {
                settlement.Credits    = parsedSettlement.Credits;
                settlement.Deductions = parsedSettlement.Deductions;

                repository.SaveSettlementHistoryAsync(settlement).Wait();
                System.Console.WriteLine($"Saved {settlement.SettlementId} to db.");
                return(true);
            }
            else
            {
                System.Console.WriteLine($"Unable to parse {filename}.");
                return(false);
            }
        }
Ejemplo n.º 13
0
        private async Task ProcessResultAsync(ZamzarResult result, SettlementHistory settlement)
        {
            string filename = result.target_files[0].name;

            if (!File.Exists(filename))
            {
                filename = await DownloadFromConverter(converter, result,
                                                       settlement.CompanyId.ToString());

                settlement.ConvertedTimestamp = DateTime.Now;
            }

            if (filename != null)
            {
                if (SaveFileToDatabase(filename, settlement))
                {
                    await converter.DeleteAsync(result.target_files[0].id);
                }
            }
        }
Ejemplo n.º 14
0
        private static void PrintSettlementHeader(string settlementId, string companyId)
        {
            System.Console.WriteLine($"Querying for {settlementId} in company: {companyId}");

            SettlementHistory settlement = null;

            Task.Run(async() => {
                SettlementRepository repo = new SettlementRepository();
                settlement = await repo.GetSettlementAsync(settlementId, companyId);
            }).Wait();

            if (settlement != null)
            {
                System.Console.WriteLine($"{settlement.id}, {settlement.SettlementDate}, {settlement.WeekNumber}");
            }
            else
            {
                System.Console.WriteLine("Settlement not found");
            }
        }
Ejemplo n.º 15
0
        public static List <SettlementHistory> ParseLocalFiles(string company)
        {
            int companyId = int.Parse(company);
            List <SettlementHistory> settlements = new List <SettlementHistory>();

            string[] settlementFiles = Directory.GetFiles(company, "*.xlsx");

            foreach (var filename in settlementFiles)
            {
                if (filename.Contains("~$"))
                {
                    System.Console.WriteLine($"Skipping temp file {filename}.");
                    continue;
                }

                SettlementHistory settlement = Parse(filename);
                settlement.CompanyId = companyId;
                settlements.Add(settlement);
                System.Console.WriteLine($"Parsed: {filename} with {settlement.Credits.Count} credits.");
            }

            return(settlements);
        }
Ejemplo n.º 16
0
        public string Generate(int year, int[] weeks, string driver)
        {
            string             outputFile = null;
            SettlementWorkbook workbook   = null;

            try
            {
                foreach (int week in weeks)
                {
                    SettlementHistory settlement = GetSettlement(week, driver);
                    if (settlement == null)
                    {
                        System.Console.WriteLine($"No settlement found for {driver} on week {week}.");
                        continue;
                    }
                    int truck = GetTruckForDriver(settlement, driver);
                    IEnumerable <Deduction> deductions = settlement.Deductions.Where(d => d.TruckId == truck);
                    IEnumerable <Credit>    credits    = settlement.Credits.Where(c => c.TruckId == truck);

                    if (workbook == null)
                    {
                        workbook   = new SettlementWorkbook(year, truck, driver);
                        outputFile = workbook.Create();
                    }
                    if (workbook == null)
                    {
                        System.Console.WriteLine($"No workbook created for truck {truck} on week {week}.");
                        continue;
                    }

                    if (workbook.Truck != truck)
                    {
                        workbook.Truck = truck;
                    }

                    workbook.AddSheet(week, GetSheetSettlementDate(settlement));
                    workbook.AddSettlementId(settlement.SettlementId);

                    //
                    // TODO: currently this is derived *IF* we find fuel charges, however
                    // this logic is actually based on whether the DRIVER is setup for
                    // comchek or not.  Need to fix this.
                    //
                    #warning FIX THIS: Need to get Comchek flag from Driver.
                    bool   ignoreComchek = false;
                    double fuel          = _fuelRepository.GetFuelCharges(year, week, truck);
                    if (fuel > 0)
                    {
                        workbook.AddFuelCharge(fuel);
                        ignoreComchek = true;
                    }

                    workbook.AddCredits(credits, ignoreComchek);

                    double occInsurance = GetOccupationalInsurance(deductions);
                    if (occInsurance > 0)
                    {
                        workbook.AddOccupationalInsurance(occInsurance);
                    }

                    workbook.Save();

                    System.Console.WriteLine($"Created {outputFile} with {credits.Count()} credit(s), ${fuel.ToString("0.00")} in fuel from {settlement.id}:{settlement.SettlementDate.ToString("yyyy-MM-dd")} for company {settlement.CompanyId}.");
                }
            }
            catch (Exception e)
            {
                System.Console.WriteLine($"Error generating workbook {outputFile ?? "null"}\n\t{e.Message}");
            }
            finally
            {
                if (workbook != null)
                {
                    workbook.Dispose();
                }
            }

            return(outputFile);
        }
Ejemplo n.º 17
0
 public static string GetLocalFileName(SettlementHistory settlement)
 {
     return(GetLocalFileName(settlement.CompanyId.ToString(), settlement.SettlementId));
 }
Ejemplo n.º 18
0
 private bool Exists(SettlementHistory settlement)
 {
     return(File.Exists(GetLocalFileName(settlement)));
 }