narsil/Tanshu.Accounts.Repository/CheckoutBI.cs
2018-08-24 16:11:33 +05:30

108 lines
3.7 KiB
C#

using RestSharp;
using System;
using System.Collections.Generic;
using Tanshu.Accounts.Contracts;
using Tanshu.Accounts.Entities;
using Tanshu.Common.Helpers;
namespace Tanshu.Accounts.Repository
{
public class CheckoutBI
{
public Dictionary<SettleOption, int> amounts = new Dictionary<SettleOption, int>();
public Dictionary<SettleOption, string> info = new Dictionary<SettleOption, string>();
private User _cashier;
public IList<User> Cashiers { get; private set; }
public DateTime StartDate { get; private set; }
public DateTime FinishDate { get; private set; }
public string Manager
{
get { return Session.User.Name; }
}
public CheckoutBI(DateTime startDate, DateTime finishDate)
{
StartDate = startDate.Date.AddHours(6);
FinishDate = finishDate.Date.AddDays(1).AddHours(5);
Cashiers = GetActiveCashiers();
}
public User Cashier
{
get
{
return _cashier;
}
set
{
_cashier = value;
GetBillDetails();
}
}
public string ActiveCashiers
{
get
{
var cashiers = "";
foreach (var item in Cashiers)
cashiers += item.Name + ", ";
return cashiers;
}
}
public void GetBillDetails()
{
if (FinishDate <= StartDate)
return;
var request = new RestRequest();
request.Resource = "Checkout/{id}.json";
request.AddParameter("id", Cashier.UserID, ParameterType.UrlSegment);
request.AddQueryParameter("s", StartDate.ToString("dd-MMM-yyyy HH:mm"));
request.AddQueryParameter("f", FinishDate.ToString("dd-MMM-yyyy HH:mm"));
var data = JsonStore.Execute<dynamic>(request);
foreach (var item in data)
{
var setOp = (SettleOption)int.Parse(item.Key);
if (!amounts.ContainsKey(setOp))
{
amounts.Add(setOp, 0);
info.Add(setOp, string.Format("\n\r--- {0} ", setOp.Display()).PadRight(44, '-'));
}
foreach (var so in item.Value)
{
string customer = "";
string billID = "";
string date = "";
int amount = 0;
foreach (var items in so)
{
if (items.Key == "Date")
date = items.Value;
else if (items.Key == "BillID")
billID = items.Value;
else if (items.Key == "Customer")
customer = items.Value;
else if (items.Key == "Amount")
amount = (int)items.Value;
}
amounts[setOp] += amount;
info[setOp] += string.Format("\n\r{0} {1} {2}", date, billID, customer);
info[setOp] += string.Format("\n\rAmount: {0:#0.00}", amount);
info[setOp] += "\n\r------------------------------------------";
}
}
}
private List<User> GetActiveCashiers()
{
var request = new RestRequest();
request.Resource = "Cashiers.json";
request.AddQueryParameter("s", StartDate.ToString("dd-MMM-yyyy HH:mm"));
request.AddQueryParameter("f", FinishDate.ToString("dd-MMM-yyyy HH:mm"));
return JsonStore.Execute<List<User>>(request);
}
}
}