2010-03-02 17:56:21 +00:00
using System ;
using System.Collections.Generic ;
using Tanshu.Accounts.Contracts ;
using System.Data.SqlClient ;
using System.Data ;
using Tanshu.Data.DAO ;
using Tanshu.Accounts.DAOFactory ;
namespace Tanshu.Accounts.SqlDAO
{
public class SaleVoucherMixDAO : BaseDAO , ISaleVoucherMixDAO
{
public SaleVoucherMixDAO ( IConnectionDAO connection )
2011-12-05 09:23:02 +00:00
: base ( connection )
{ }
2010-03-02 17:56:21 +00:00
public SalesBillItemBO GetDefaultSaleBillItem ( Guid productID )
{
2011-12-05 09:23:02 +00:00
const string query = @ "
2010-03-02 17:56:21 +00:00
SELECT p . ProductID , p . Name + ' ( ' + p . Units + ')' ,
p . SalePrice , p . SaleTaxID , t . Rate AS TaxRate ,
p . SaleLedgerID , l . CostCenterID
FROM Products p INNER JOIN Tax t ON p . SaleTaxID = t . TaxID INNER JOIN Ledgers l ON p . SaleLedgerID = l . LedgerID
WHERE p . ProductID = @ProductID
";
2011-12-05 09:23:02 +00:00
using ( var cmd = new SqlCommand ( query ) )
2010-03-02 17:56:21 +00:00
{
2011-12-05 09:23:02 +00:00
cmd . Parameters . AddWithValue ( "@ProductID" , productID ) ;
using ( var dr = connection . ExecuteReader ( cmd ) )
2010-03-02 17:56:21 +00:00
{
2011-12-05 09:23:02 +00:00
dr . Read ( ) ;
var mySale = new SalesBillItemBO
2010-03-02 17:56:21 +00:00
{
2011-12-05 09:23:02 +00:00
productID = dr . GetGuid ( 0 ) ,
Name = dr . GetString ( 1 ) ,
Price = dr . GetDecimal ( 2 ) ,
Tax = dr . GetDecimal ( 4 ) ,
Discount = 0 ,
location = "" ,
Printed = 0 ,
Quantity = 1
} ;
return mySale ;
2010-03-02 17:56:21 +00:00
}
}
}
2011-12-05 09:23:02 +00:00
2010-03-02 17:56:21 +00:00
public decimal GetProductDiscountLimit ( Guid productID )
{
2011-12-05 09:23:02 +00:00
const string query = @"SELECT t.DiscountLimit FROM ProductTypes t INNER JOIN Products p ON t.ProductTypeID = p.ProductTypeID WHERE p.ProductID = @ProductID" ;
2010-03-02 17:56:21 +00:00
2011-12-05 09:23:02 +00:00
decimal discountLimit ;
using ( var cmd = new SqlCommand ( query ) )
2010-03-02 17:56:21 +00:00
{
cmd . Parameters . AddWithValue ( "@ProductID" , productID ) ;
discountLimit = ( decimal ) connection . ExecuteScalar ( cmd ) ;
}
return discountLimit ;
}
public bool IsBillPrinted ( Guid voucherID )
{
2011-12-05 09:23:02 +00:00
const string query = @"SELECT Printed FROM SaleVoucher WHERE VoucherID = @VoucherID" ;
using ( var cmd = new SqlCommand ( query ) )
2010-03-02 17:56:21 +00:00
{
cmd . Parameters . AddWithValue ( "@VoucherID" , voucherID ) ;
2011-12-05 09:23:02 +00:00
return ( bool ) connection . ExecuteScalar ( cmd ) ;
2010-03-02 17:56:21 +00:00
}
}
2011-12-05 09:23:02 +00:00
public List < PendingBillsBO > GetPendingBills ( PendingType list , int floor )
2010-03-02 17:56:21 +00:00
{
2011-12-05 09:23:02 +00:00
var query = @ "
2010-03-02 17:56:21 +00:00
SELECT t . VoucherID , s . Alarm AS Alarm , s . TableID AS TableID ,
( SELECT SUM ( Amount ) FROM Inventory WHERE VoucherID = t . VoucherID ) AS Amount ,
2011-12-05 09:23:02 +00:00
s . BillID , t . CreationDate , u . Name , CAST ( 0 AS bit ) , s . KotID , t . LastEditDate , s . Printed , s . AdvanceID
FROM Vouchers t INNER JOIN SaleVoucher s ON t . VoucherID = s . VoucherID AND s . PaidStatus = @PaidStatus
2010-03-02 17:56:21 +00:00
INNER JOIN Users u ON t . UserID = u . UserID
";
if ( list = = PendingType . Alarms )
query + = "WHERE s.Alarm IS NOT NULL " ;
else
query + = "WHERE s.Alarm IS NULL " ;
2011-12-05 09:23:02 +00:00
if ( floor ! = 0 )
query + = "AND s.Floor = @Floor " ;
2010-03-02 17:56:21 +00:00
if ( list = = PendingType . Today )
query + = string . Format ( "AND t.LastEditDate >= '{0:dd-MMM-yyyy} 00:00:00' " , DateTime . Now ) ;
else if ( list = = PendingType . Week )
query + = string . Format ( "AND t.LastEditDate >= '{0:dd-MMM-yyyy} 00:00:00' " , DateTime . Now . AddDays ( - 7 ) ) ;
query + = "ORDER BY t.CreationDate " ;
2011-12-05 09:23:02 +00:00
using ( var cmd = new SqlCommand ( query ) )
2010-03-02 17:56:21 +00:00
{
2011-12-05 09:23:02 +00:00
cmd . Parameters . AddWithValue ( "@PaidStatus" , ( int ) PaidStatus . Pending ) ;
if ( floor ! = 0 )
cmd . Parameters . AddWithValue ( "@Floor" , floor ) ;
using ( var dr = connection . ExecuteReader ( cmd ) )
2010-03-02 17:56:21 +00:00
{
2011-12-05 09:23:02 +00:00
var outList = new List < PendingBillsBO > ( ) ;
while ( dr . Read ( ) )
{
var local = new PendingBillsBO ( ) ;
local . voucherID = dr . GetGuid ( 0 ) ;
local . Alarm = ! dr . IsDBNull ( 1 ) ;
if ( local . Alarm )
local . AlarmTime = dr . GetDateTime ( 1 ) ;
local . TableID = dr . GetString ( 2 ) ;
if ( ! dr . IsDBNull ( 3 ) )
local . Amount = dr . GetDecimal ( 3 ) ;
local . BillNo = dr . GetString ( 4 ) ;
local . BookingTime = dr . GetDateTime ( 5 ) ;
local . Cashier = dr . GetString ( 6 ) ;
local . Important = dr . GetBoolean ( 7 ) ;
local . Kot = dr . GetString ( 8 ) ;
local . LastEdited = dr . GetDateTime ( 9 ) ;
local . Printed = dr . GetBoolean ( 10 ) ;
if ( ! dr . IsDBNull ( 11 ) )
local . AdvanceID = dr . GetGuid ( 11 ) ;
outList . Add ( local ) ;
}
return outList ;
2010-03-02 17:56:21 +00:00
}
}
}
2011-12-05 09:23:02 +00:00
public Guid ? GetPendingVoucherID ( string tableID , int floor )
2010-03-02 17:56:21 +00:00
{
2011-12-05 09:23:02 +00:00
var query = "SELECT VoucherID FROM SaleVoucher where TableID = @TableID and PaidStatus = @PaidStatus" ;
if ( floor ! = 0 )
query + = " AND Floor = @Floor " ;
using ( var cmd = new SqlCommand ( query ) )
2010-03-02 17:56:21 +00:00
{
cmd . Parameters . AddWithValue ( "@TableID" , tableID ) ;
2011-12-05 09:23:02 +00:00
cmd . Parameters . AddWithValue ( "@PaidStatus" , ( int ) PaidStatus . Pending ) ;
if ( floor ! = 0 )
cmd . Parameters . AddWithValue ( "@Floor" , floor ) ;
using ( var dr = connection . ExecuteReader ( cmd ) )
2010-03-02 17:56:21 +00:00
{
if ( dr . Read ( ) )
{
2011-12-05 09:23:02 +00:00
return dr . GetGuid ( 0 ) ;
2010-03-02 17:56:21 +00:00
}
return null ;
}
}
}
public List < InventoryBO > SaleInventory ( Dictionary < BillItemKey , SalesBillItemBO > . ValueCollection list , Guid ? voucherID )
{
2011-12-05 09:23:02 +00:00
var localList = new Dictionary < Guid , InventoryBO > ( ) ;
foreach ( var item in list )
2010-03-02 17:56:21 +00:00
{
2011-12-05 09:23:02 +00:00
var temp = new InventoryBO ( ) ;
2010-03-02 17:56:21 +00:00
if ( localList . ContainsKey ( item . productID ) )
{
temp = localList [ item . productID ] ;
temp . Quantity + = item . Quantity ;
}
else
{
if ( voucherID . HasValue )
temp . VoucherID = voucherID . Value ;
temp . InventoryID = Guid . NewGuid ( ) ;
temp . Discount = item . Discount ;
temp . IsRateFinal = true ;
temp . ComplexProductID = null ;
temp . ProductID = item . productID ;
temp . Quantity = item . Quantity ;
temp . Rate = item . Price ;
temp . Tax = item . Tax ;
localList . Add ( item . productID , temp ) ;
}
}
2011-12-05 09:23:02 +00:00
var outList = new List < InventoryBO > ( ) ;
2010-03-02 17:56:21 +00:00
foreach ( var item in localList )
outList . Add ( item . Value ) ;
return outList ;
}
private class BillPaidClass
{
public Guid VoucherID { get ; set ; }
public string KotID { get ; set ; }
public bool Printed { get ; set ; }
}
2011-12-05 09:23:02 +00:00
public void DeclareBillsPaid ( Guid userID , List < Guid > billList , PaidStatus paidStatus )
2010-03-02 17:56:21 +00:00
{
2011-12-05 09:23:02 +00:00
var removeItems = new List < Guid > ( ) ;
var query = "SELECT VoucherID, KotID, Printed FROM SaleVoucher WHERE VoucherID IN (" ;
foreach ( var voucherID in billList )
2010-03-02 17:56:21 +00:00
{
query + = string . Format ( "'{0}', " , voucherID ) ;
}
query = query . Substring ( 0 , query . Length - 2 ) + ")" ;
2011-12-05 09:23:02 +00:00
var pbc = new List < BillPaidClass > ( ) ;
using ( var cmd = new SqlCommand ( query ) )
2010-03-02 17:56:21 +00:00
{
2011-12-05 09:23:02 +00:00
using ( var dr = connection . ExecuteReader ( cmd ) )
2010-03-02 17:56:21 +00:00
{
while ( dr . Read ( ) )
{
pbc . Add ( new BillPaidClass
{
VoucherID = dr . GetGuid ( 0 ) ,
KotID = dr . GetString ( 1 ) ,
Printed = dr . GetBoolean ( 2 )
} ) ;
}
}
}
2011-12-05 09:23:02 +00:00
foreach ( var item in pbc )
2010-03-02 17:56:21 +00:00
{
//if ((item.Printed != false) || (MessageBox.Show("Pay unprinted bill No.: " + item.KotID + " ?", "Pay unprinted bill", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes))
2011-12-05 09:23:02 +00:00
if ( ! item . Printed )
continue ;
using ( var cmd = new SqlCommand ( "UPDATE SaleVoucher SET PaidStatus = @PaidStatus WHERE VoucherID = @VoucherID; UPDATE Vouchers SET UserID = @UserID, LastEditDate = GETDATE() WHERE VoucherID = @VoucherID" ) )
2010-03-02 17:56:21 +00:00
{
2011-12-05 09:23:02 +00:00
cmd . Parameters . AddWithValue ( "@VoucherID" , item . VoucherID ) ;
cmd . Parameters . AddWithValue ( "@UserID" , userID ) ;
cmd . Parameters . AddWithValue ( "@PaidStatus" , ( int ) paidStatus ) ;
connection . ExecuteNonQuery ( cmd ) ;
2010-03-02 17:56:21 +00:00
}
2011-12-05 09:23:02 +00:00
removeItems . Add ( item . VoucherID ) ;
2010-03-02 17:56:21 +00:00
}
2011-12-05 09:23:02 +00:00
foreach ( var item in removeItems )
2010-03-02 17:56:21 +00:00
billList . Remove ( item ) ;
}
2011-12-05 09:23:02 +00:00
#region ISaleVoucherMixDAO Members
public decimal Amount ( Guid voucherID )
2010-03-02 17:56:21 +00:00
{
2011-12-05 09:23:02 +00:00
decimal amount ;
using ( var cmd = new SqlCommand ( "SELECT SUM(Amount) FROM Inventory WHERE VoucherID = @VoucherID" ) )
{
cmd . Parameters . AddWithValue ( "@VoucherID" , voucherID ) ;
amount = ( decimal ) connection . ExecuteScalar ( cmd ) ;
}
Guid ? advanceID ;
using ( var cmd = new SqlCommand ( "SELECT AdvanceID FROM SaleVoucher WHERE VoucherID = @VoucherID" ) )
2010-03-02 17:56:21 +00:00
{
cmd . Parameters . AddWithValue ( "@VoucherID" , voucherID ) ;
2011-12-05 09:23:02 +00:00
advanceID = ( Guid ? ) connection . ExecuteScalar ( cmd ) ;
}
if ( ! advanceID . HasValue )
return amount ;
using ( var cmd = new SqlCommand ( "SELECT Amount FROM Advances WHERE AdvanceID = @AdvanceID" ) )
{
cmd . Parameters . AddWithValue ( "@AdvanceID" , advanceID . Value ) ;
return amount - ( decimal ) connection . ExecuteScalar ( cmd ) ;
2010-03-02 17:56:21 +00:00
}
}
2011-12-05 09:23:02 +00:00
#endregion
2010-03-02 17:56:21 +00:00
}
}