public async Task <PayableDaysTrendAnalysisResp> Handle(PurchasesTrendAnalysis request, CancellationToken cancellationToken)
            {
                var response = new PayableDaysTrendAnalysisResp {
                    Datasets = new List <PayableDays>(), Status = new APIResponseStatus {
                        Message = new APIResponseMessage()
                    }
                };

                try
                {
                    #region LATER

                    //var payable = (from a in _dataContext.cor_paymentterms
                    //               join b in _dataContext.inv_invoice on a.PaymentTermId equals b.PaymentTermId
                    //               join c in _dataContext.cor_supplier on b.SupplierId equals c.SupplierId
                    //               select
                    //              new SupplierData
                    //              {
                    //                  Date = a.CompletionDate ?? DateTime.UtcNow,
                    //                  SuppliertypeId = c.SupplierTypeId,
                    //                  SuppliierId = b.SupplierId,
                    //                  CompletionDate = a.CompletionDate,
                    //                  InvoiceGenerated = a.InvoiceGenerated
                    //              }).ToList();

                    //var groupedpayables = payable.GroupBy(u => u.SuppliertypeId).Select(grp => grp.ToList()).ToList();


                    //var groupedpurchases = payable.GroupBy(u => u.SuppliertypeId).Select(grp => grp.ToList().Select(f => new SupplierData
                    //{
                    //    SuppliertypeId = f.SuppliertypeId,
                    //    Date = f.Date,
                    //    CompletionDate = f.CompletionDate,
                    //    InvoiceGenerated = f.InvoiceGenerated,
                    //    SuppliierId = f.SuppliierId,
                    //})).ToList();
                    //var pyablesday = new List<FinalDataAnalytics>();
                    //foreach (var gp in groupedpurchases)
                    //{
                    //    var purchs = 0;
                    //    var pybls = 0;
                    //    var sequence1 = gp.Where(q => q.InvoiceGenerated && q.CompletionDate == null).ToList().Select(q => q.Date.TimeOfDay);
                    //    if(sequence1.ToList().Count() > 0)
                    //    {
                    //         pybls = (sequence1.Aggregate((sum, next) => sum + next)).DaysTrendAnalysis;
                    //    }
                    //    var sequence2 = gp.Where(q => q.InvoiceGenerated && q.CompletionDate != null).ToList().Select(q => q.Date.TimeOfDay);
                    //    if (sequence2.ToList().Count() > 0)
                    //    {
                    //         purchs = (sequence2.Aggregate((sum, next) => sum + next)).DaysTrendAnalysis;
                    //    }

                    //    var pbDaysTrendAnalysis = new FinalDataAnalytics
                    //    {
                    //        SuppliertypeName = _dataContext.cor_suppliertype.FirstOrDefault(q => q.SupplierTypeId == gp.FirstOrDefault().SuppliertypeId)?.SupplierTypeName,
                    //        PayableDaysTrendAnalysis = purchs / pybls * 365
                    //    };
                    //    pyablesday.Add(pbDaysTrendAnalysis);
                    //}

                    //List<int> dataValue = new List<int>();
                    //var Labels = new List<string>();
                    //var datas = new List<PayableDaysTrendAnalysis>();

                    //foreach (var w in pyablesday)
                    //{
                    //    Labels.Add(w.SuppliertypeName);
                    //    dataValue.Add(w.PayableDaysTrendAnalysis);
                    //}
                    //var dataset = new PayableDaysTrendAnalysis
                    //{
                    //    Label = "Payable DaysTrendAnalysis",
                    //    BorderColor = "",
                    //    Data = dataValue.ToArray(),
                    //    Fill = false,
                    //};
                    //datas.Add(dataset);
                    //response.PayableDaysTrendAnalysis = datas;
                    //response.Labels = Labels.ToArray();
                    #endregion

                    var suppliertypes = _dataContext.cor_suppliertype.ToList();

                    var gls      = new List <int>();
                    var debitGls = new List <int>();

                    debitGls = suppliertypes.Select(r => r.DebitGL).Distinct().ToList();
                    gls.AddRange(debitGls);

                    var reportgls = new ReportGls
                    {
                        Trans = gls
                    };

                    var transactions = await _financeServer.GetFinTransactions(reportgls);

                    if (!transactions.Status.IsSuccessful)
                    {
                        response.Status = transactions.Status;
                        return(response);
                    }

                    List <decimal> dataValue = new List <decimal>();
                    var            Labels    = new List <string>();
                    var            datas     = new List <PayableDays>();


                    var byYearGroup = transactions.Trans.GroupBy(r => r.TransactionDate.Year).Select(grp => grp.ToList()).ToList();
                    foreach (var group in byYearGroup)
                    {
                        var Year  = group.FirstOrDefault().TransactionDate.Year.ToString();
                        var total = group.Where(t => debitGls.Contains(t.SubGL)).ToList().Sum(r => r.DebitAmount);
                        Labels.Add(Year);
                        dataValue.Add(total);
                    }


                    var dataset = new PayableDays
                    {
                        Label       = "Purchases Trend Analysis",
                        BorderColor = "",
                        Data        = dataValue.ToArray(),
                        Fill        = false,
                    };
                    datas.Add(dataset);
                    response.Datasets = datas;
                    response.Labels   = Labels.ToArray();

                    // transactions.CreditEntries

                    return(await Task.Run(() => response));
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
Exemplo n.º 2
0
            public async Task <PayableDaysTrendAnalysisResp> Handle(PayableDaysCalcultion request, CancellationToken cancellationToken)
            {
                var response = new PayableDaysTrendAnalysisResp {
                    Datasets = new List <PayableDays>(), Status = new APIResponseStatus {
                        Message = new APIResponseMessage()
                    }
                };

                try
                {
                    var suppliertypes = _dataContext.cor_suppliertype.ToList();

                    var gls       = new List <int>();
                    var creditGls = new List <int>();
                    var debitGls  = new List <int>();

                    creditGls = suppliertypes.Select(r => r.CreditGL).Distinct().ToList();
                    debitGls  = suppliertypes.Select(r => r.DebitGL).Distinct().ToList();
                    gls.AddRange(creditGls);
                    gls.AddRange(debitGls);

                    var reportgls = new ReportGls
                    {
                        Trans = gls
                    };

                    var transactions = await _financeServer.GetFinTransactions(reportgls);

                    if (!transactions.Status.IsSuccessful)
                    {
                        response.Status = transactions.Status;
                        return(response);
                    }

                    List <decimal> dataValue = new List <decimal>();
                    var            Labels    = new List <string>();
                    var            datas     = new List <PayableDays>();


                    var byTyppeGroup = (from a in transactions.Trans
                                        from b in suppliertypes
                                        orderby a.TransactionDate
                                        where gls.Contains(a.SubGL) select new DayCalc
                    {
                        typeid = b.SupplierTypeId,
                        DebitAmount = a.DebitAmount,
                        typename = b.SupplierTypeName,
                        CreditAmount = a.CreditAmount,
                        SubGl = a.SubGL,
                        tranId = a.TransactionId,
                        RunningBal = a.RunningBal ?? 0
                    }).ToList().GroupBy(r => r.typeid).Select(grp => grp.ToList()).ToList();
                    foreach (var group in byTyppeGroup)
                    {
                        var type  = group.FirstOrDefault().typename;
                        var total = group.Where(t => t.typeid == group.FirstOrDefault().typeid).ToList().Sum(r => r.DebitAmount) / group.Where(t => t.typeid == group.FirstOrDefault().typeid).ToList().OrderBy(t => t.tranId).FirstOrDefault().RunningBal * 365;
                        Labels.Add(type);
                        dataValue.Add(total);
                    }


                    var dataset = new PayableDays
                    {
                        Label       = "Purchases Analysis",
                        BorderColor = "",
                        Data        = dataValue.ToArray(),
                        Fill        = false,
                    };
                    datas.Add(dataset);
                    response.Datasets = datas;
                    response.Labels   = Labels.ToArray();

                    // transactions.CreditEntries

                    return(await Task.Run(() => response));
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }