narsil/Tanshu.Accounts.PointOfSale/Updates/ReplaceForm.cs
2010-03-02 23:26:21 +05:30

71 lines
3.0 KiB
C#

using System;
using System.Linq;
using System.Threading;
using System.Windows.Forms;
using Tanshu.Accounts.Helpers;
using System.Collections.Generic;
using Tanshu.Accounts.BI;
using Tanshu.Accounts.Contracts;
using System.Data.SqlClient;
using System.Configuration;
namespace Tanshu.Accounts.PointOfSale
{
public partial class ReplaceForm : Form
{
public ReplaceForm()
{
InitializeComponent();
}
private void btnGo_Click(object sender, EventArgs e)
{
if (!Thread.CurrentPrincipal.IsInRole("Security/CreateUser"))
return;
dtpFrom.Value = Convert.ToDateTime(string.Format("{0:dd-MMM-yyyy} 00:00:00", dtpFrom.Value));
dtpTo.Value = Convert.ToDateTime(string.Format("{0:dd-MMM-yyyy} 23:59:59", dtpTo.Value));
string query = string.Empty;
if (txtStatus.Text == "")
{
txtZero1.Text = GetBalance(0M, txtOne.Text, dtpFrom.Value, dtpTo.Value).ToString();
txtFour1.Text = GetBalance(.04M, txtOne.Text, dtpFrom.Value, dtpTo.Value).ToString();
txtTwelve1.Text = GetBalance(0.125M, txtOne.Text, dtpFrom.Value, dtpTo.Value).ToString();
txtZero2.Text = GetBalance(0M, txtTwo.Text, dtpFrom.Value, dtpTo.Value).ToString();
txtFour2.Text = GetBalance(.04M, txtTwo.Text, dtpFrom.Value, dtpTo.Value).ToString();
txtTwelve2.Text = GetBalance(0.125M, txtTwo.Text, dtpFrom.Value, dtpTo.Value).ToString();
txtStatus.Text = "Initial Values Loaded";
}
else
{
Update(txtOne.Text, txtTwo.Text, dtpFrom.Value, dtpTo.Value);
MessageBox.Show("Data Merged");
}
}
private decimal GetBalance(decimal tax, string database, DateTime startDate, DateTime finishDate)
{
string query = @"
SELECT ISNULL(SUM(i.Amount), 0) FROM {0}.dbo.Vouchers t INNER JOIN {0}.dbo.SaleVoucher s ON t.VoucherID = s.VoucherID
INNER JOIN {0}.dbo.Inventory i ON t.VoucherID = i.VoucherID
WHERE t.Type = 'S' AND t.Date BETWEEN @StartDate AND @FinishDate AND s.Void = 0 AND s.Paid = 1
AND i.Tax = @Tax
";
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString))
{
con.Open();
using (SqlCommand cmd = new SqlCommand(string.Format(query, database), con))
{
cmd.Parameters.AddWithValue("@StartDate", startDate);
cmd.Parameters.AddWithValue("@FinishDate", finishDate);
cmd.Parameters.AddWithValue("@Tax", tax);
return (decimal)cmd.ExecuteScalar();
}
}
}
private bool Update(string sourceDB, string targetDB, DateTime startDate, DateTime finishDate)
{
return new ManagementBI().MergeData(startDate, finishDate, sourceDB, targetDB);
}
}
}