344 lines
14 KiB
C#
344 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<Voucher>()
|
|
where i.LastEditDate >= StartDate &&
|
|
i.LastEditDate <= FinishDate &&
|
|
i.User == Cashier &&
|
|
i.Void == false &&
|
|
i.Settled == SettleOption.Unsettled
|
|
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<Voucher>()
|
|
where i.LastEditDate >= StartDate &&
|
|
i.LastEditDate <= FinishDate &&
|
|
i.User == Cashier &&
|
|
i.Void == false &&
|
|
i.Settled == SettleOption.NoCharge
|
|
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<Voucher>()
|
|
where i.LastEditDate >= StartDate &&
|
|
i.LastEditDate <= FinishDate &&
|
|
i.User == Cashier &&
|
|
i.Void == false &&
|
|
i.Settled == SettleOption.BillToCompany
|
|
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<Voucher>()
|
|
where i.LastEditDate >= StartDate &&
|
|
i.LastEditDate <= FinishDate &&
|
|
i.User == Cashier &&
|
|
i.Void == false &&
|
|
i.Settled == SettleOption.Staff
|
|
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<Voucher>()
|
|
where i.LastEditDate >= StartDate &&
|
|
i.LastEditDate <= FinishDate &&
|
|
i.User == Cashier &&
|
|
i.Void == false &&
|
|
i.Settled == SettleOption.CreditCard
|
|
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<Voucher>()
|
|
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 Voucher v
|
|
inner join v.Kots k
|
|
inner join k.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", SettleOption.Unsettled)
|
|
.SetParameter("noCharge", SettleOption.NoCharge)
|
|
.SetParameter("discount", disount)
|
|
.List<Voucher>();
|
|
|
|
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 Voucher v
|
|
inner join v.Kots k
|
|
inner join k.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", SettleOption.Unsettled)
|
|
.SetParameter("noCharge", SettleOption.NoCharge)
|
|
.UniqueResult<decimal>();
|
|
return amount;
|
|
}
|
|
}
|
|
|
|
private void GetInfo(IList<Voucher> 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.Kots.Sum(x => x.Inventories.Sum(y => y.Amount));
|
|
disc = item.Kots.Sum(x => x.Inventories.Sum(y => y.Quantity * y.Rate * y.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 Voucher 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;
|
|
}
|
|
}
|
|
}
|
|
}
|