2010-03-02 17:56:21 +00:00
using System.Collections.Generic ;
using System.Data.SqlClient ;
using Tanshu.Accounts.Contracts ;
using System ;
using Tanshu.Data.DAO ;
2013-11-16 06:01:58 +00:00
2010-03-02 17:56:21 +00:00
namespace Tanshu.Accounts.SqlDAO
{
2013-11-16 06:01:58 +00:00
public class SalesAnalysisDAO : BaseDAO
2010-03-02 17:56:21 +00:00
{
public SalesAnalysisDAO ( IConnectionDAO connection )
: base ( connection )
{ }
2014-08-08 12:05:38 +00:00
public List < SalesAnalysisBO > GetSalesTaxReturn ( DateTime startDate , DateTime finishDate , ref decimal voids , ref decimal pending , ref decimal net , ref decimal vat , ref decimal serviceTax , ref decimal nc )
2010-03-02 17:56:21 +00:00
{
2011-12-05 09:23:02 +00:00
var list = new List < SalesAnalysisBO > ( ) ;
using ( var cmd = new SqlCommand ( ) )
2010-03-02 17:56:21 +00:00
{
2011-12-05 09:23:02 +00:00
const string query = @ "
2010-03-02 17:56:21 +00:00
SELECT NEWID ( ) ,
CAST ( i . Tax AS nvarchar ( 10 ) ) AS Section ,
SUM ( i . Quantity * i . Rate * ( 1 - i . Discount ) * ( i . Tax ) ) AS Quantity ,
SUM ( i . Quantity * i . Rate * ( 1 - i . Discount ) ) AS Net ,
SUM ( i . Amount ) AS Gross
2014-08-08 12:05:38 +00:00
FROM Vouchers v INNER JOIN Inventory i ON v . VoucherID = i . VoucherID
WHERE v . PaidStatus NOT IN ( @Pending , @Void , @Nc )
AND v . Date BETWEEN @StartDate AND @FinishDate
2010-03-02 17:56:21 +00:00
GROUP BY i . Tax
ORDER BY Section DESC ; ";
cmd . CommandText = query ;
2011-12-05 09:23:02 +00:00
cmd . Parameters . AddWithValue ( "@Pending" , ( int ) PaidStatus . Pending ) ;
cmd . Parameters . AddWithValue ( "@Void" , ( int ) PaidStatus . Void ) ;
2014-08-08 12:05:38 +00:00
cmd . Parameters . AddWithValue ( "@Nc" , ( int ) PaidStatus . Nc ) ;
2010-03-02 17:56:21 +00:00
cmd . Parameters . AddWithValue ( "@StartDate" , startDate ) ;
cmd . Parameters . AddWithValue ( "@FinishDate" , finishDate ) ;
list = BusinessObjectDAO < SalesAnalysisBO > . GetBusinessObjects ( connection . ExecuteReader ( cmd ) ) ;
}
2011-12-05 09:23:02 +00:00
using ( var cmd = new SqlCommand ( ) )
2010-03-02 17:56:21 +00:00
{
2011-12-05 09:23:02 +00:00
const string query = @ "
2014-08-08 12:05:38 +00:00
SELECT ISNULL ( SUM ( i . Quantity * i . Rate ) , 0 ) AS Amount
FROM Vouchers v
INNER JOIN Inventory i ON v . VoucherID = i . VoucherID
WHERE v . PaidStatus = @Nc
AND v . Date BETWEEN @StartDate AND @FinishDate
";
2010-03-02 17:56:21 +00:00
cmd . CommandText = query ;
2014-08-08 12:05:38 +00:00
cmd . Parameters . AddWithValue ( "@Nc" , ( int ) PaidStatus . Nc ) ;
2010-03-02 17:56:21 +00:00
cmd . Parameters . AddWithValue ( "@StartDate" , startDate ) ;
cmd . Parameters . AddWithValue ( "@FinishDate" , finishDate ) ;
2014-08-08 12:05:38 +00:00
nc = Math . Round ( ( decimal ) connection . ExecuteScalar ( cmd ) ) ;
2010-03-02 17:56:21 +00:00
}
2011-12-05 09:23:02 +00:00
using ( var cmd = new SqlCommand ( ) )
2010-03-02 17:56:21 +00:00
{
2011-12-05 09:23:02 +00:00
const string query = @ "
2014-08-08 12:05:38 +00:00
SELECT ISNULL ( SUM ( i . Amount ) , 0 ) AS Amount
FROM Vouchers v INNER JOIN Inventory i ON v . VoucherID = i . VoucherID
WHERE v . Date BETWEEN @StartDate AND @FinishDate
AND v . PaidStatus = @Void ";
2010-03-02 17:56:21 +00:00
cmd . CommandText = query ;
2011-12-05 09:23:02 +00:00
cmd . Parameters . AddWithValue ( "@Void" , ( int ) PaidStatus . Void ) ;
2010-03-02 17:56:21 +00:00
cmd . Parameters . AddWithValue ( "@StartDate" , startDate ) ;
cmd . Parameters . AddWithValue ( "@FinishDate" , finishDate ) ;
voids = Math . Round ( ( decimal ) connection . ExecuteScalar ( cmd ) ) ;
}
2011-12-05 09:23:02 +00:00
using ( var cmd = new SqlCommand ( ) )
2010-03-02 17:56:21 +00:00
{
2011-12-05 09:23:02 +00:00
const string query = @ "
2014-08-08 12:05:38 +00:00
SELECT ISNULL ( SUM ( i . Amount ) , 0 ) AS Amount
FROM Vouchers v INNER JOIN Inventory i ON v . VoucherID = i . VoucherID
WHERE v . PaidStatus = @Pending
AND v . Date BETWEEN @StartDate AND @FinishDate ";
2010-03-02 17:56:21 +00:00
cmd . CommandText = query ;
2011-12-05 09:23:02 +00:00
cmd . Parameters . AddWithValue ( "@Pending" , ( int ) PaidStatus . Pending ) ;
2010-03-02 17:56:21 +00:00
cmd . Parameters . AddWithValue ( "@StartDate" , startDate ) ;
cmd . Parameters . AddWithValue ( "@FinishDate" , finishDate ) ;
pending = Math . Round ( ( decimal ) connection . ExecuteScalar ( cmd ) ) ;
}
2011-12-05 09:23:02 +00:00
using ( var cmd = new SqlCommand ( ) )
2010-03-02 17:56:21 +00:00
{
2011-12-05 09:23:02 +00:00
const string query = @ "
2010-03-02 17:56:21 +00:00
SELECT ISNULL ( SUM ( Quantity * Rate * ( 1 - Discount ) ) , 0 ) AS Amount
2014-08-08 12:05:38 +00:00
FROM Vouchers v INNER JOIN Inventory i ON v . VoucherID = i . VoucherID
WHERE v . PaidStatus NOT IN ( @Pending , @Void , @Nc )
AND v . Date BETWEEN @StartDate AND @FinishDate ";
2010-03-02 17:56:21 +00:00
cmd . CommandText = query ;
2011-12-05 09:23:02 +00:00
cmd . Parameters . AddWithValue ( "@Pending" , ( int ) PaidStatus . Pending ) ;
cmd . Parameters . AddWithValue ( "@Void" , ( int ) PaidStatus . Void ) ;
2014-08-08 12:05:38 +00:00
cmd . Parameters . AddWithValue ( "@Nc" , ( int ) PaidStatus . Nc ) ;
2010-03-02 17:56:21 +00:00
cmd . Parameters . AddWithValue ( "@StartDate" , startDate ) ;
cmd . Parameters . AddWithValue ( "@FinishDate" , finishDate ) ;
net = Math . Round ( ( decimal ) connection . ExecuteScalar ( cmd ) ) ;
}
2011-12-05 09:23:02 +00:00
using ( var cmd = new SqlCommand ( ) )
2010-03-02 17:56:21 +00:00
{
2011-12-05 09:23:02 +00:00
const string query = @ "
2014-08-08 12:05:38 +00:00
SELECT ISNULL ( SUM ( i . Quantity * i . Rate * ( 1 - i . Discount ) * i . Vat ) , 0 ) AS Amount
FROM Vouchers v INNER JOIN Inventory i ON v . VoucherID = i . VoucherID
WHERE v . PaidStatus NOT IN ( @Pending , @Void , @Nc )
AND v . Date BETWEEN @StartDate AND @FinishDate ";
2010-03-02 17:56:21 +00:00
cmd . CommandText = query ;
2011-12-05 09:23:02 +00:00
cmd . Parameters . AddWithValue ( "@Pending" , ( int ) PaidStatus . Pending ) ;
cmd . Parameters . AddWithValue ( "@Void" , ( int ) PaidStatus . Void ) ;
2014-08-08 12:05:38 +00:00
cmd . Parameters . AddWithValue ( "@Nc" , ( int ) PaidStatus . Nc ) ;
2010-03-02 17:56:21 +00:00
cmd . Parameters . AddWithValue ( "@StartDate" , startDate ) ;
cmd . Parameters . AddWithValue ( "@FinishDate" , finishDate ) ;
2014-08-08 12:05:38 +00:00
vat = Math . Round ( ( decimal ) connection . ExecuteScalar ( cmd ) ) ;
2010-03-02 17:56:21 +00:00
}
2011-12-05 09:23:02 +00:00
using ( var cmd = new SqlCommand ( ) )
2010-03-02 17:56:21 +00:00
{
2011-12-05 09:23:02 +00:00
const string query = @ "
2014-08-08 12:05:38 +00:00
SELECT ISNULL ( SUM ( i . Quantity * i . Rate * ( 1 - i . Discount ) * i . ServiceTax ) , 0 ) AS Amount
FROM Vouchers v INNER JOIN Inventory i ON v . VoucherID = i . VoucherID
WHERE v . PaidStatus NOT IN ( @Pending , @Void , @Nc )
AND v . Date BETWEEN @StartDate AND @FinishDate ";
2010-03-02 17:56:21 +00:00
cmd . CommandText = query ;
2011-12-05 09:23:02 +00:00
cmd . Parameters . AddWithValue ( "@Pending" , ( int ) PaidStatus . Pending ) ;
cmd . Parameters . AddWithValue ( "@Void" , ( int ) PaidStatus . Void ) ;
2014-08-08 12:05:38 +00:00
cmd . Parameters . AddWithValue ( "@Nc" , ( int ) PaidStatus . Nc ) ;
2010-03-02 17:56:21 +00:00
cmd . Parameters . AddWithValue ( "@StartDate" , startDate ) ;
cmd . Parameters . AddWithValue ( "@FinishDate" , finishDate ) ;
2014-08-08 12:05:38 +00:00
serviceTax = Math . Round ( ( decimal ) connection . ExecuteScalar ( cmd ) ) ;
2010-03-02 17:56:21 +00:00
}
2014-08-08 12:05:38 +00:00
return list ;
2010-03-02 17:56:21 +00:00
}
2013-11-28 10:39:33 +00:00
public List < SalesAnalysisDetailBO > GetSaleDetail ( DateTime startDate , DateTime finishDate , Guid productGroupID )
2010-03-02 17:56:21 +00:00
{
using ( SqlCommand cmd = new SqlCommand ( ) )
{
string query = @ "
2014-08-08 12:05:38 +00:00
SELECT p . Name AS Product , pt . Name AS Section , SUM ( i . Quantity ) AS Quantity , SUM ( i . Amount ) AS Amount
FROM Vouchers v INNER JOIN Inventory i ON v . VoucherID = i . VoucherID
2010-03-02 17:56:21 +00:00
INNER JOIN Products p ON i . ProductID = p . ProductID
2013-11-28 10:39:33 +00:00
INNER JOIN ProductGroups pt ON p . ProductGroupID = pt . ProductGroupID
2014-08-08 12:05:38 +00:00
WHERE v . PaidStatus ! = @Pending AND v . PaidStatus ! = @Void
AND v . LastEditDate BETWEEN @StartDate AND @FinishDate AND p . ProductGroupID = @ProductGroupID
2010-03-02 17:56:21 +00:00
GROUP BY p . Name , pt . Name
ORDER BY Amount DESC ; ";
cmd . CommandText = query ;
2011-12-05 09:23:02 +00:00
cmd . Parameters . AddWithValue ( "@Pending" , ( int ) PaidStatus . Pending ) ;
cmd . Parameters . AddWithValue ( "@Void" , ( int ) PaidStatus . Void ) ;
2010-03-02 17:56:21 +00:00
cmd . Parameters . AddWithValue ( "@StartDate" , startDate ) ;
cmd . Parameters . AddWithValue ( "@FinishDate" , finishDate ) ;
2013-11-28 10:39:33 +00:00
cmd . Parameters . AddWithValue ( "@ProductGroupID" , productGroupID ) ;
2010-03-02 17:56:21 +00:00
return BusinessObjectDAO < SalesAnalysisDetailBO > . GetBusinessObjects ( connection . ExecuteReader ( cmd ) ) ;
}
}
public List < SalesAnalysisBO > GetSale ( DateTime startDate , DateTime finishDate )
{
2011-12-05 09:23:02 +00:00
using ( var cmd = new SqlCommand ( ) )
2010-03-02 17:56:21 +00:00
{
2011-12-05 09:23:02 +00:00
const string query = @ "
2013-11-28 10:39:33 +00:00
SELECT pg . ProductGroupID , pg . Name , SUM ( i . Quantity * i . Rate * ( 1 - i . Discount ) ) AS Net , SUM ( i . Quantity * i . Rate * ( 1 - i . Discount ) * i . Vat ) AS Vat , SUM ( i . Quantity * i . Rate * ( 1 - i . Discount ) * i . ServiceTax ) AS ServiceTax
2014-08-08 12:05:38 +00:00
FROM Vouchers v
INNER JOIN Inventory i ON v . VoucherID = i . VoucherID
2010-03-02 17:56:21 +00:00
INNER JOIN Products p ON i . ProductID = p . ProductID
2013-11-28 10:39:33 +00:00
INNER JOIN ProductGroups pg ON p . ProductGroupID = pg . ProductGroupID
2014-08-08 12:05:38 +00:00
WHERE v . PaidStatus ! = @Pending AND v . PaidStatus ! = @Void
AND v . LastEditDate BETWEEN @StartDate AND @FinishDate
2013-11-28 10:39:33 +00:00
GROUP BY pg . ProductGroupID , pg . Name
2010-03-02 17:56:21 +00:00
ORDER BY Net DESC ; ";
cmd . CommandText = query ;
2011-12-05 09:23:02 +00:00
cmd . Parameters . AddWithValue ( "@Pending" , ( int ) PaidStatus . Pending ) ;
cmd . Parameters . AddWithValue ( "@Void" , ( int ) PaidStatus . Void ) ;
2010-03-02 17:56:21 +00:00
cmd . Parameters . AddWithValue ( "@StartDate" , startDate ) ;
cmd . Parameters . AddWithValue ( "@FinishDate" , finishDate ) ;
2011-12-05 09:23:02 +00:00
var list = BusinessObjectDAO < SalesAnalysisBO > . GetBusinessObjects ( connection . ExecuteReader ( cmd ) ) ;
2013-11-28 10:39:33 +00:00
decimal net = 0 , vat = 0 , serviceTax = 0 ;
2011-12-05 09:23:02 +00:00
foreach ( var i in list )
2010-03-02 17:56:21 +00:00
{
net + = i . Net ;
2013-11-28 10:39:33 +00:00
vat + = i . Vat ;
serviceTax + = i . ServiceTax ;
2010-03-02 17:56:21 +00:00
}
2013-11-16 06:01:58 +00:00
list . Add ( new SalesAnalysisBO
2010-03-02 17:56:21 +00:00
{
2013-11-28 10:39:33 +00:00
Name = "Total" ,
Net = net ,
Vat = vat ,
ServiceTax = serviceTax
2013-11-16 06:01:58 +00:00
} ) ;
2013-11-28 10:39:33 +00:00
list . Add ( new SalesAnalysisBO ( ) { Name = " -- " } ) ;
2013-11-16 06:01:58 +00:00
return GetSettlement ( list , startDate , finishDate ) ;
}
}
private List < SalesAnalysisBO > GetSettlement ( List < SalesAnalysisBO > outList , DateTime startDate , DateTime finishDate )
{
2013-11-28 10:39:33 +00:00
outList . Add ( new SalesAnalysisBO ( ) { Name = " -- " } ) ;
2013-11-16 06:01:58 +00:00
if ( finishDate < = startDate )
return new List < SalesAnalysisBO > ( ) ;
using ( var cmd = new SqlCommand ( ) )
{
const string query = @ "
2014-08-08 12:05:38 +00:00
SELECT v . PaidStatus AS Status , Sum ( i . Amount ) AS Amount
FROM Vouchers v
2013-11-16 06:01:58 +00:00
INNER JOIN Inventory i ON v . VoucherID = i . VoucherID
WHERE v . LastEditDate BETWEEN @StartDate AND @FinishDate
2014-08-08 12:05:38 +00:00
GROUP BY v . PaidStatus
ORDER BY v . PaidStatus
2013-11-16 06:01:58 +00:00
";
cmd . CommandText = query ;
cmd . Parameters . AddWithValue ( "@StartDate" , startDate ) ;
cmd . Parameters . AddWithValue ( "@FinishDate" , finishDate ) ;
var list = BusinessObjectDAO < SettlementReportBO > . GetBusinessObjects ( connection . ExecuteReader ( cmd ) ) ;
decimal amount = 0 ;
foreach ( var item in list )
{
amount + = item . Amount ;
2013-11-28 10:39:33 +00:00
outList . Add ( new SalesAnalysisBO ( ) { Name = item . Status . ToString ( ) , Net = item . Amount } ) ;
2013-11-16 06:01:58 +00:00
}
2013-11-28 10:39:33 +00:00
outList . Add ( new SalesAnalysisBO ( ) { Name = "Total" , Net = amount } ) ;
return GetAdvances ( outList , startDate , finishDate ) ;
}
}
private List < SalesAnalysisBO > GetAdvances ( List < SalesAnalysisBO > outList , DateTime startDate , DateTime finishDate )
{
outList . Add ( new SalesAnalysisBO ( ) { Name = " -- " } ) ;
if ( finishDate < = startDate )
return new List < SalesAnalysisBO > ( ) ;
using ( var cmd = new SqlCommand ( ) )
{
string query = "SELECT a.*, ui.Name AS Cashier, ISNULL(uo.Name, '') AS UserOut FROM Advances a INNER JOIN Users ui ON a.CashierIn = ui.UserID LEFT OUTER JOIN Users uo ON a.CashierOut = uo.UserID WHERE DateIn BETWEEN @StartDate AND @FinishDate OR (DateOut is not null AND DateOut BETWEEN @StartDate AND @FinishDate)" ;
cmd . CommandText = query ;
cmd . Parameters . AddWithValue ( "@StartDate" , startDate ) ;
cmd . Parameters . AddWithValue ( "@FinishDate" , finishDate ) ;
var list = BusinessObjectDAO < AdvanceDisplayBO > . GetBusinessObjects ( connection . ExecuteReader ( cmd ) ) ;
decimal amount = 0 ;
foreach ( var item in list )
{
if ( item . DateIn > = startDate & & item . DateIn < = finishDate )
{
amount + = item . Amount ;
outList . Add ( new SalesAnalysisBO ( ) { Name = string . Format ( "Advance In {0}" , item . Cashier ) , Net = item . Amount } ) ;
}
if ( item . DateOut . HasValue & & item . DateOut > = startDate & & item . DateOut < = finishDate )
{
amount - = item . Amount ;
outList . Add ( new SalesAnalysisBO ( ) { Name = string . Format ( "Advance Out {0}" , item . UserOut ) , Net = item . Amount } ) ;
}
}
outList . Add ( new SalesAnalysisBO ( ) { Name = "Total Advance Taken" , Net = amount } ) ;
2013-11-16 06:01:58 +00:00
return outList ;
}
}
public decimal GetDetail ( PaidStatus paidStatus , DateTime startDate , DateTime finishDate )
{
const string query = @ "
2014-08-08 12:05:38 +00:00
SELECT SUM ( i . Amount ) AS Amount , SUM ( i . Quantity * i . Rate * i . Discount ) AS Discount FROM Vouchers v
INNER JOIN Inventory i ON v . VoucherID = i . VoucherID
WHERE v . LastEditDate BETWEEN @StartDate AND @FinishDate
AND v . PaidStatus = @PaidStatus
2013-11-16 06:01:58 +00:00
";
using ( var cmd = new SqlCommand ( query ) )
{
cmd . Parameters . AddWithValue ( "@PaidStatus" , ( int ) paidStatus ) ;
cmd . Parameters . AddWithValue ( "@StartDate" , startDate ) ;
cmd . Parameters . AddWithValue ( "@FinishDate" , finishDate ) ;
return ( decimal ) connection . ExecuteScalar ( cmd ) ;
2010-03-02 17:56:21 +00:00
}
}
}
}