narsil/Tanshu.Accounts.SqlDAO/BusinessLayer/CheckoutBI.cs

343 lines
14 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Data.SqlClient;
using Tanshu.Data.DAO;
using Tanshu.Accounts.SqlDAO;
using Tanshu.Accounts.Entities;
using Tanshu.Accounts.Entities.Auth;
using NHibernate.Criterion;
using Tanshu.Accounts.Contracts;
namespace Tanshu.Accounts.Repository
{
public class CheckoutBI
{
#region Properties
public decimal Opening { get; private set; }
public decimal Receipts { get; private set; }
public decimal AdvanceReceipts { get; private set; }
public decimal CCReceipts { get; private set; }
public decimal NCReceipts { get; private set; }
public decimal BTCReceipts { get; private set; }
public decimal StaffReceipts { get; private set; }
public decimal AdvanceAdjusted { get; private set; }
public decimal CashPayments { get; private set; }
public decimal AdditionalVoids { get; private set; }
public decimal VoidsInSystem { get; private set; }
public decimal Discount { get; private set; }
public decimal PendingBills { get; private set; }
public decimal NetSales { get; private set; }
public decimal ClosingBalance { get; private set; }
public decimal RetainedOvernight { get; private set; }
public decimal CashDeposited { get; private set; } //
public decimal Excess { get; private set; } //
public string Status { get; private set; } //
public string Cashiers { get; private set; } //
public User Cashier { get; private set; }
public decimal OldPending { get; private set; }
public decimal OldReceipts { get; private set; }
public decimal OldVoided { get; private set; }
public DateTime StartDate { get; private set; }
public DateTime FinishDate { get; private set; }
public string PendingString { get; private set; }
public string CCString { get; private set; }
public string NCString { get; private set; }
public string BTCString { get; private set; }
public string StaffString { get; private set; }
public string VoidsString { get; private set; }
public string DiscountString { get; private set; }
public string PaymentString { get; private set; }
public string Manager
{
get { return Session.User.Name; }
}
#endregion
public CheckoutBI(int cashier, DateTime startDate, DateTime finishDate)
{
using (var session = SessionManager.Session)
{
this.Cashier = UserBI.GetUser(cashier);
this.StartDate = startDate.Date.AddHours(6);
this.FinishDate = finishDate.Date.AddDays(1).AddHours(5);
string info;
PendingBills = GetPending(out info);
PendingString = info;
CCReceipts = GetCreditCard(out info);
CCString = info;
NCReceipts = GetNoCharge(out info);
NCString = info;
BTCReceipts = GetBillToCompany(out info);
BTCString = info;
StaffReceipts = GetStaffBills(out info);
StaffString = info;
VoidsInSystem = GetVoids(out info);
VoidsString = info;
// Opening = dao.GetOpenings();
// Receipts = GetReceipts();
PaymentString = "";
// CashPayments = dao.GetPayments();
// AdditionalVoids = dao.GetAdditionalVoids();
// RetainedOvernight = dao.GetRetainedOvernight();
AdvanceReceipts = GetAdvancesReceived();
AdvanceAdjusted = GetAdvancesAdjusted();
// PaymentString = dao.GetPaymentString();
// OldPending = dao.GetOldPending();
// OldReceipts = dao.GetOldReceipts();
// OldVoided = dao.GetOldVoided();
Cashiers = GetActiveCashiers();
NetSales = GetNetSales();
Discount = GetDiscountBills(.1M, out info);
DiscountString = info;
ClosingBalance = Opening
+ Receipts
+ AdvanceReceipts
- CCReceipts
- BTCReceipts
- AdvanceAdjusted
- CashPayments
- AdditionalVoids
+ NetSales;
}
}
private decimal GetAdvancesAdjusted()
{
using (var session = SessionManager.Session)
{
var amt = session.QueryOver<Advance>()
.Where(i => i.DateOut >= StartDate && i.DateOut <= FinishDate && i.CashierOut == Cashier)
.SelectList(k => k.SelectSum(j => j.Amount))
.SingleOrDefault<decimal>();
return amt;
}
}
private decimal GetAdvancesReceived()
{
using (var session = SessionManager.Session)
{
var amt = session.QueryOver<Advance>()
.Where(i => i.DateIn >= StartDate && i.DateIn <= FinishDate && i.CashierIn == Cashier)
.SelectList(k => k.SelectSum(j => j.Amount))
.SingleOrDefault<decimal>();
return amt;
}
}
public void Calculate(decimal cashDeposited, decimal retainedOvernight)
{
this.CashDeposited = cashDeposited;
this.RetainedOvernight = retainedOvernight;
Excess = CashDeposited - ClosingBalance;
Status = string.Format("{0:Extra Cash Rs #,##0.00; Cash Short Rs #,##0.00;Cash Perfect}", Excess);
}
private decimal GetPending(out string info)
{
using (var session = SessionManager.Session)
{
decimal amount;
decimal discount;
info = "\n\r--- Pending Bills ------------------------";
var list = (from i in session.QueryOver<SaleVoucher>()
where i.LastEditDate >= StartDate &&
i.LastEditDate <= FinishDate &&
i.User == Cashier &&
i.Void == false &&
i.Settled == SettleOptionFactory.OUnsettled
select i).List();
GetInfo(list, out amount, out discount, ref info);
return amount;
}
}
private decimal GetNoCharge(out string info)
{
using (var session = SessionManager.Session)
{
decimal amount;
decimal discount;
info = "\n\r--- No Charge ----------------------------";
var list = (from i in session.QueryOver<SaleVoucher>()
where i.LastEditDate >= StartDate &&
i.LastEditDate <= FinishDate &&
i.User == Cashier &&
i.Void == false &&
i.Settled == SettleOptionFactory.ONoCharge
select i).List();
GetInfo(list, out amount, out discount, ref info);
return amount;
}
}
private decimal GetBillToCompany(out string info)
{
using (var session = SessionManager.Session)
{
decimal amount;
decimal discount;
info = "\n\r--- Bill To Company ----------------------";
var list = (from i in session.QueryOver<SaleVoucher>()
where i.LastEditDate >= StartDate &&
i.LastEditDate <= FinishDate &&
i.User == Cashier &&
i.Void == false &&
i.Settled == SettleOptionFactory.OBillToCompany
select i).List();
GetInfo(list, out amount, out discount, ref info);
return amount;
}
}
private decimal GetStaffBills(out string info)
{
using (var session = SessionManager.Session)
{
decimal amount;
decimal discount;
info = "\n\r--- Staff Bills --------------------------";
var list = (from i in session.QueryOver<SaleVoucher>()
where i.LastEditDate >= StartDate &&
i.LastEditDate <= FinishDate &&
i.User == Cashier &&
i.Void == false &&
i.Settled == SettleOptionFactory.OStaff
select i).List();
GetInfo(list, out amount, out discount, ref info);
return amount;
}
}
private decimal GetCreditCard(out string info)
{
using (var session = SessionManager.Session)
{
decimal amount;
decimal discount;
info = "\n\r--- Credit Card Bills --------------------";
var list = (from i in session.QueryOver<SaleVoucher>()
where i.LastEditDate >= StartDate &&
i.LastEditDate <= FinishDate &&
i.User == Cashier &&
i.Void == false &&
i.Settled == SettleOptionFactory.OCreditCard
select i).List();
GetInfo(list, out amount, out discount, ref info);
return amount;
}
}
private decimal GetVoids(out string info)
{
using (var session = SessionManager.Session)
{
decimal amount;
decimal discount;
info = "\n\r--- Void Bills ---------------------------";
var list = (from i in session.QueryOver<SaleVoucher>()
where i.LastEditDate >= StartDate &&
i.LastEditDate <= FinishDate &&
i.User == Cashier &&
i.Void == true
select i).List();
GetInfo(list, out amount, out discount, ref info);
return amount;
}
}
private decimal GetDiscountBills(decimal disount, out string info)
{
using (var session = SessionManager.Session)
{
decimal amount;
decimal discount;
info = "\n\r--- High Discount Bills ------------------";
string query = @"
select distinct(v) from SaleVoucher v
inner join v.Inventories i
where v.Date >= :startDate and v.Date <= :finishDate and v.User = :cashierID and v.Void = false and v.Settled != :unSettled and v.Settled != :noCharge
and i.Discount >= :discount";
var list = session.CreateQuery(query)
.SetParameter("startDate", StartDate)
.SetParameter("finishDate", FinishDate)
.SetParameter("cashierID", Cashier.UserID)
.SetParameter("unSettled", SettleOptionFactory.Unsettled)
.SetParameter("noCharge", SettleOptionFactory.NoCharge)
.SetParameter("discount", disount)
.List<SaleVoucher>();
GetInfo(list, out amount, out discount, ref info);
return discount;
}
}
private decimal GetNetSales()
{
using (var session = SessionManager.Session)
{
decimal amount = 0;
string query = @"
select sum(i.Amount) from SaleVoucher v
inner join v.Inventories i
where v.Date >= :startDate and v.Date <= :finishDate and v.User = :cashierID and v.Void = false and v.Settled != :unSettled and v.Settled != :noCharge";
amount = session.CreateQuery(query)
.SetParameter("startDate", StartDate)
.SetParameter("finishDate", FinishDate)
.SetParameter("cashierID", Cashier.UserID)
.SetParameter("unSettled", SettleOptionFactory.Unsettled)
.SetParameter("noCharge", SettleOptionFactory.NoCharge)
.UniqueResult<decimal>();
return amount;
}
}
private void GetInfo(IList<SaleVoucher> list, out decimal amount, out decimal discount, ref string info)
{
amount = 0;
discount = 0;
if (list.Count == 0)
{
info = string.Empty;
return;
}
foreach (var item in list)
{
decimal amt, disc;
amt = item.Inventories.Sum(x => x.Amount);
disc = item.Inventories.Sum(x => x.Quantity * x.Rate * x.Discount);
info += string.Format("\n\r{0:dd-MMM-yyyy HH:mm:ss} {1} {2}", item.Date, item.BillID, item.Customer.Name);
info += string.Format("\n\rAmount: {0:#0.00} :: Discount: {1:#0.00}", amt, disc);
info += "\n\r------------------------------------------";
amount += amt;
discount += disc;
}
}
private string GetActiveCashiers()
{
string cashiers = "";
using (var session = SessionManager.Session)
{
string query = @"
select distinct(u.Name) from SaleVoucher v
inner join v.User u
where v.Date >= :startDate and v.Date <= :finishDate";
var list = session.CreateQuery(query)
.SetParameter("startDate", StartDate)
.SetParameter("finishDate", FinishDate)
.List<string>();
foreach (var item in list)
cashiers += item + ", ";
return cashiers;
}
}
}
}