using Biskilog_Accounting.Server.POSModels; using Biskilog_Accounting.Shared.CustomModels; using Biskilog_Accounting.Shared.Interfaces; using Biskilog_Accounting.Shared.POSModels; using Microsoft.EntityFrameworkCore; using System.Data.Entity; using System.Runtime.CompilerServices; using static Microsoft.EntityFrameworkCore.DbLoggerCategory; namespace Biskilog_Accounting.Server.Services { /// /// Gets the KPIs/ Analysis of operations made with the software /// public class AnalyticalService : IAnalytics { private readonly BiskAcdbContext m_context; private bool m_comparisonMode; private string m_activeBranch; public AnalyticalService(BiskAcdbContext a_context, bool a_comparisonMode, string a_activeBranchId) { m_context = a_context; m_comparisonMode = a_comparisonMode; m_activeBranch = a_activeBranchId; } public IEnumerable GetCancelledSales(DateTime a_start, DateTime a_end) { //If in comparison mode, the list is fetched from all branchid of the business if (m_comparisonMode) { return from cSale in m_context.Tblcancelledtransactions join aSale in m_context.Tblcarts on cSale.Transno equals aSale.Transno into activeSale join cPurchase in m_context.Tblcustomerpurchases on cSale.Transno equals cPurchase.TransactionId into customerSales from c in customerSales.DefaultIfEmpty() join customer in m_context.Tblcustomers on c.CustomerId equals customer.CustomerId into Customers from cc in Customers.DefaultIfEmpty() where cSale.DateCancelled >= a_start && cSale.DateCancelled <= a_end select new CancelledSales { CancelledTransaction = cSale, Value = activeSale.Sum(s => s.Total), Customer = !String.IsNullOrEmpty(cc.CustomerId) ? $"{cc.Firstname} {cc.Surname}" : "Walk-IN Purchase" }; } else { return from cSale in m_context.Tblcancelledtransactions join aSale in m_context.Tblcarts on cSale.Transno equals aSale.Transno into activeSale join cPurchase in m_context.Tblcustomerpurchases on cSale.Transno equals cPurchase.TransactionId into customerSales from c in customerSales.DefaultIfEmpty() join customer in m_context.Tblcustomers on c.CustomerId equals customer.CustomerId into Customers from cc in Customers.DefaultIfEmpty() where cSale.DateCancelled >= a_start && cSale.DateCancelled <= a_end && cSale.BranchId == m_activeBranch select new CancelledSales { CancelledTransaction = cSale, Value = (from a in activeSale where a.BranchId == m_activeBranch select a.Total).Sum(), Customer = !String.IsNullOrEmpty(cc.CustomerId) ? $"{cc.Firstname} {cc.Surname}" : "Walk-IN Purchase" }; } } public IEnumerable>> GetEmployeeSales(DateTime a_start, DateTime a_end) { throw new NotImplementedException(); } public IEnumerable GetInDebtCustomers() { if (m_comparisonMode) { return from c in m_context.Tblcustomers join a in m_context.Customeraccounts on c.CustomerId equals a.CustomerId into CustomerAccounts from ac in CustomerAccounts.OrderByDescending(ac => ac.Date).Take(1).DefaultIfEmpty() where ac.Balance < 0 orderby ac.Date descending select new InDebtCustomers { Customer = c, Debt = ac != null ? ac.Balance : 0 }; } else { return null; } } public IEnumerable GetMostPurchasedItem(DateTime a_start, DateTime a_end) { throw new NotImplementedException(); } public IEnumerable GetOutOfStockItems() { //if (m_comparisonMode) //{ // return from r in m_context.Restocklevels // join i in m_context.Tblinventories on r.ProductId equals i.Pcode // join p in m_context.Tblproducts on i.Pcode equals p.Pcode // where i.Quantity == r.WarnLevel // select new ProductItem // { // Product = p, // Stock = i, // Unitofmeasure = // } //} //else { //} return null; } public IEnumerable GetPriceChanges(DateTime a_start, DateTime a_end) { if (m_comparisonMode) { return from change in m_context.Tblpricechanges join p in m_context.Tblproducts on change.Pcode equals p.Pcode where change.ChangeDate <= a_start && change.ChangeDate >= a_end select new ProductPriceChange { BranchId = change.BranchId, ChangeDate = change.ChangeDate, Pcode = change.Pcode, CountId = change.CountId, CurrentPrice = change.CurrentPrice, PreviousPrice = change.PreviousPrice, ProductName = p.ProductName }; } else { return from change in m_context.Tblpricechanges join p in m_context.Tblproducts on change.Pcode equals p.Pcode where change.ChangeDate <= a_start && change.ChangeDate >= a_end && change.BranchId == m_activeBranch select new ProductPriceChange { BranchId = change.BranchId, ChangeDate = change.ChangeDate, Pcode = change.Pcode, CountId = change.CountId, CurrentPrice = change.CurrentPrice, PreviousPrice = change.PreviousPrice, ProductName = p.ProductName }; } } public IEnumerable GetSalesTransaction(DateTime a_start, DateTime a_end) { //If in comparison mode, the list is fetched from all branchid of the business if (m_comparisonMode) { return m_context.Tblcarts.Where(t => t.Date >= a_start && t.Date <= a_end); } else { return m_context.Tblcarts.Where(t => t.Date >= a_start && t.Date <= a_end && t.BranchId == m_activeBranch); } } } }