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

296 lines
12 KiB
C#
Raw Normal View History

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;
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 AdvanceAdjusted { get; private set; }
public decimal CashPayments { get; private set; }
public decimal AdditionalVoids { get; private set; }
public decimal VoidsInSystem { 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 VoidsString { get; private set; }
public string DiscountString { get; private set; }
//public string PendingString { get; private set; } //
//public string CCString { 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 Thread.CurrentPrincipal.Identity.Name; }
}
#endregion
public CheckoutBI(int cashier, DateTime startDate, DateTime finishDate)
{
using (var session = SessionManager.Session)
{
//Actual Closing
this.Cashier = UserBI.GetUser(cashier);
this.StartDate = Convert.ToDateTime(string.Format("{0:dd-MMM-yyyy} 00:00:00", startDate));
this.FinishDate = Convert.ToDateTime(string.Format("{0:dd-MMM-yyyy} 23:59:59", finishDate));
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;
VoidsInSystem = GetVoids(out info);
VoidsString = info;
// Opening = dao.GetOpenings();
// Receipts = GetReceipts();
// 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();
GetDiscountBills(.1M, out info);
DiscountString = info;
ClosingBalance = Opening
+ Receipts
+ AdvanceReceipts
- CCReceipts
- 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;
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, ref info);
return amount;
}
}
private decimal GetNoCharge(out string info)
{
using (var session = SessionManager.Session)
{
decimal amount;
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, ref info);
return amount;
}
}
private decimal GetBillToCompany(out string info)
{
using (var session = SessionManager.Session)
{
decimal amount;
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, ref info);
return amount;
}
}
private decimal GetCreditCard(out string info)
{
using (var session = SessionManager.Session)
{
decimal amount;
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, ref info);
return amount;
}
}
private decimal GetVoids(out string info)
{
using (var session = SessionManager.Session)
{
decimal amount;
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, ref info);
return amount;
}
}
private decimal GetDiscountBills(decimal disount, out string info)
{
using (var session = SessionManager.Session)
{
decimal amount;
info = "\n\r--- High Discount Bills ------------------";
string query = @"
select 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, ref info);
return amount;
}
}
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, ref string info)
{
amount = 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 * (1 + x.Tax) * (1 + x.ServiceCharge) * 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;
}
}
}
}