narsil/Tanshu.Accounts.PointOfSale/Management/ManagementForm.cs

539 lines
21 KiB
C#

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using Tanshu.Accounts.Repository;
namespace Tanshu.Accounts.Management
{
public partial class ManagementForm : Form
{
public ManagementForm()
{
InitializeComponent();
}
private void Sale_Analysis_Form_Load(object sender, EventArgs e)
{
dtpStart.Value = DateTime.Today;
dtpFinish.Value = DateTime.Today;
}
private void btnGo_Click(object sender, EventArgs e)
{
var currentDirectory = AppDomain.CurrentDomain.BaseDirectory;
var beer = Path.Combine(currentDirectory, "beer.csv");
var sale = Path.Combine(currentDirectory, "sale.csv");
var credit = Path.Combine(currentDirectory, "credit.csv");
var error = string.Empty;
if (!File.Exists(beer))
error += "Beer not found! ";
if (!File.Exists(sale))
error += "Sale not found! ";
if (!File.Exists(credit))
error += "Credit not found";
if (!string.IsNullOrEmpty(error))
{
MessageBox.Show(error);
}
else
{
btnGo.Enabled = false;
var beerDates = GetBeer(beer);
var saleDates = GetSale(sale);
var creditDates = GetCredit(credit);
var info = string.Empty;
foreach (var item in saleDates)
{
var startDate = item.StartDate;
var finishDate = item.FinishDate;
Debug.WriteLine("Starting on " + startDate.ToShortDateString() + " to " + finishDate.ToShortDateString());
ProcessData(true, startDate, finishDate, item.Sale15 + item.Sale25);
Debug.WriteLine("Starting beer");
ProcessBeer(beerDates, startDate, finishDate);
Debug.WriteLine("Starting sale");
info += ProcessSale(item, creditDates);
Debug.WriteLine("Starting cleanup");
ProcessData(false, startDate, finishDate, item.Sale15 + item.Sale25);
Debug.WriteLine("Cleanup done");
}
MessageBox.Show(info);
btnGo.Enabled = true;
}
}
#region Get Data
protected class SaleData
{
public DateTime StartDate { get; set; }
public DateTime FinishDate { get; set; }
public decimal Sale125 { get; set; }
public decimal Sale25 { get; set; }
public decimal Sale15 { get; set; }
public decimal Sale0 { get; set; }
}
private IEnumerable<SaleData> GetSale(string sale)
{
IFormatProvider culture = new CultureInfo("en-US", true);
var startDate = dtpStart.Value.Date;
var finishDate = dtpFinish.Value.Date;
var dates = new List<SaleData>();
using (var reader = new StreamReader(File.OpenRead(sale)))
{
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(',');
if (values.Length != 6)
{
MessageBox.Show("Error reading line: " + line);
continue;
}
DateTime dateStart;
DateTime dateFinish;
if (!DateTime.TryParseExact(values[0], "dd-MM-yyyy", culture, DateTimeStyles.NoCurrentDateDefault, out dateStart))
continue;
if (!DateTime.TryParseExact(values[1], "dd-MM-yyyy", culture, DateTimeStyles.NoCurrentDateDefault, out dateFinish))
continue;
if (dateFinish.Date < startDate.Date || dateStart.Date > finishDate.Date)
continue;
dates.Add(new SaleData
{
StartDate = dateStart,
FinishDate = dateFinish,
Sale125 = TryConvert(values[2]),
Sale25 = TryConvert(values[3]),
Sale15 = TryConvert(values[4]),
Sale0 = TryConvert(values[5])
});
}
}
return dates;
}
private Dictionary<DateTime, decimal> GetCredit(string credit)
{
IFormatProvider culture = new CultureInfo("en-US", true);
var startDate = dtpStart.Value.Date.AddHours(7);
var finishDate = dtpFinish.Value.Date.AddDays(1).AddHours(7);
var dates = new Dictionary<DateTime, decimal>();
using (var reader = new StreamReader(File.OpenRead(credit)))
{
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(',');
if (values.Length != 2)
{
MessageBox.Show("Error reading line: " + line);
continue;
}
DateTime dateOut;
if (!DateTime.TryParseExact(values[0], "dd/MM/yyyy", culture, DateTimeStyles.NoCurrentDateDefault, out dateOut))
continue;
if (dateOut.Date < startDate.Date || dateOut.Date >= finishDate.Date)
continue;
var amount = TryConvert(values[1]);
if (!dates.ContainsKey(dateOut))
dates.Add(dateOut, amount);
}
}
return dates;
}
private Dictionary<DateTime, decimal> GetBeer(string beer)
{
IFormatProvider culture = new CultureInfo("en-US", true);
var startDate = dtpStart.Value.Date.AddHours(7);
var finishDate = dtpFinish.Value.Date.AddDays(1).AddHours(7);
var dates = new Dictionary<DateTime, decimal>();
using (var reader = new StreamReader(File.OpenRead(beer)))
{
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(',');
DateTime dateOut;
if (!DateTime.TryParseExact(values[0], "dd-MMM-yy", culture, DateTimeStyles.NoCurrentDateDefault, out dateOut))
continue;
if (dateOut.Date < startDate.Date || dateOut.Date >= finishDate.Date)
continue;
decimal amount = 0;
if (values.Length > 1) // Dark - Location 1, Basecode 1
amount += TryConvert(values[1]);
if (values.Length > 2) // Wheat - Location 2, Basecode 2
amount += TryConvert(values[2]);
if (values.Length > 3) // Light - Location 3, Basecode 4
amount += TryConvert(values[3]);
if (values.Length > 4) // Premium - Location 4, Basecode 3
amount += TryConvert(values[4]);
dates.Add(dateOut, amount);
}
}
return dates;
}
#endregion
private static void ProcessData(bool opening, DateTime startIn, DateTime finishIn, decimal liqTarget)
{
var startDate = startIn.AddHours(7);
var finishDate = finishIn.AddDays(1).AddHours(7);
if (opening)
{
using (var bi = new ManagementBI())
{
bi.DeleteVoid(startDate, finishDate);
}
using (var bi = new ManagementBI())
{
bi.DeleteStaff(startDate, finishDate);
}
using (var bi = new ManagementBI())
{
bi.ClearModifiers(startDate, finishDate);
}
using (var bi = new ManagementBI())
{
bi.CombineKots(startDate, finishDate);
}
using (var bi = new ManagementBI())
{
bi.RemoveBlankKots(startDate, finishDate);
}
using (var bi = new ManagementBI())
{
bi.MoveNc(startDate, finishDate, liqTarget);
}
}
else
{
using (var bi = new ManagementBI())
{
bi.SetPayments(startDate, finishDate);
}
}
}
private static void ProcessBeer(Dictionary<DateTime, decimal> dates, DateTime startIn, DateTime finishIn)
{
using (var bi = new ManagementBI())
{
var info = string.Empty;
foreach (var item in dates)
{
if (item.Key < startIn || item.Key > finishIn)
continue;
var stDt = item.Key.AddHours(7);
var fiDt = stDt.AddDays(1);
var original = bi.GetQuantity(stDt, fiDt);
if (original < item.Value)
{
info += "Original for " + item.Key.ToString() + " is " + original.ToString() + " desired is " + item.Value.ToString() + "\r\n";
}
else if (original == item.Value)
{
info += item.Key.ToString() + " is " + original.ToString() + " matches!" + "\r\n";
}
else
{
bi.SetQuantity(stDt, fiDt, item.Value);
}
}
}
}
private static string ProcessSale(SaleData item, Dictionary<DateTime, decimal> creditDates)
{
var info = string.Empty;
var startDate = item.StartDate.AddHours(7);
var finishDate = item.FinishDate.AddHours(7).AddDays(1);
var creditInfo = creditDates.Where(x => x.Key >= item.StartDate && x.Key <= item.FinishDate).ToDictionary(x => x.Key, x => x.Value);
info += "From " + startDate.ToShortDateString() + " to " + finishDate.ToShortDateString() + "\r\n";
using (var bi = new ManagementBI())
{
info += "25%\t" + bi.GetFood(.26250M, startDate, finishDate) + "\t";
var ret = bi.SetLiq(.26250M, item.Sale25, startDate, finishDate);
info += ret + "\t" + item.Sale25.ToString() + "\r\n";
}
using (var bi = new ManagementBI())
{
info += "15%\t" + bi.GetFood(.1575M, startDate, finishDate) + "\t";
var ret = bi.SetLiq(.1575M, item.Sale15, startDate, finishDate);
info += ret + "\t" + item.Sale15.ToString() + "\r\n";
}
using (var bi = new ManagementBI())
{
info += "12.5%\t" + bi.GetFood(.13125M, startDate, finishDate) + "\t";
var ret = bi.SetFood(.13125M, item.Sale125, startDate, finishDate);
info += ret + "\t" + item.Sale125.ToString() + "\r\n";
}
using (var bi = new ManagementBI())
{
info += "0%\t" + bi.GetFood(0M, startDate, finishDate) + "\t";
var ret = bi.SetFood(0M, item.Sale0, startDate, finishDate);
info += ret + "\t" + item.Sale0.ToString() + "\r\n";
}
return info;
}
private static decimal TryConvert(string amount)
{
decimal result = 0;
decimal.TryParse(amount, out result);
return result;
}
private void button1_Click(object sender, EventArgs e)
{
var startDate = dtpStart.Value.Date;
var finishDate = dtpFinish.Value.Date;
var daybook = @"
<ENVELOPE>
<HEADER>
<TALLYREQUEST>Import Data</TALLYREQUEST>
</HEADER>
<BODY>
<IMPORTDATA>
<REQUESTDESC>
<REPORTNAME>All Masters</REPORTNAME>
<STATICVARIABLES>
<SVCURRENTCOMPANY>Peitho Foods Pvt. Ltd.(2012-13)</SVCURRENTCOMPANY>
</STATICVARIABLES>
</REQUESTDESC>
<REQUESTDATA>
<TALLYMESSAGE xmlns:UDF=""TallyUDF"">
";
for (var date = startDate; date <= finishDate; date = date.AddDays(1))
{
daybook += GetVoucher(date);
}
daybook += @"
</TALLYMESSAGE>
</REQUESTDATA>
</IMPORTDATA>
</BODY>
</ENVELOPE>
";
Clipboard.SetText(daybook, TextDataFormat.Text);
}
private static string GetVoucher(DateTime date)
{
var currentStart = date.AddHours(7);
var currentFinish = date.AddDays(1).AddHours(7);
var voucher = string.Empty;
var cash = 0M;
using (var bi = new ManagementBI())
{
var sale = Math.Round(bi.GetFood(.26250M, currentStart, currentFinish));
var vat = 0M;
if (sale != 0)
{
cash += sale;
voucher += GetLedger("Sale @ 25 %", sale.ToString("#0.00"));
vat = Math.Round(bi.GetVat(.26250M, currentStart, currentFinish));
cash += vat;
voucher += GetLedger("Output Vat @ 25%", vat.ToString("#0.00"));
}
sale = Math.Round(bi.GetFood(.1575M, currentStart, currentFinish));
if (sale != 0)
{
cash += sale;
voucher += GetLedger("Sale @ 15%", sale.ToString("#0.00"));
vat = Math.Round(bi.GetVat(.1575M, currentStart, currentFinish));
cash += vat;
voucher += GetLedger("Output Vat @ 15%", vat.ToString("#0.00"));
}
sale = Math.Round(bi.GetFood(.13125M, currentStart, currentFinish));
if (sale != 0)
{
cash += sale;
voucher += GetLedger("Sale 12.5%", sale.ToString("#0.00"));
vat = Math.Round(bi.GetVat(.13125M, currentStart, currentFinish));
cash += vat;
voucher += GetLedger("Output Vat 12.5%", vat.ToString("#0.00"));
}
sale = Math.Round(bi.GetFood(0M, currentStart, currentFinish));
if (sale != 0)
{
cash += sale;
voucher += GetLedger("Sale Tax Free", sale.ToString("#0.00"));
}
vat = Math.Round(bi.GetServiceTax(currentStart, currentFinish));
if (vat != 0)
{
cash += vat;
voucher += GetLedger("Central Service Tax@3.708%", vat.ToString("#0.00"));
}
if (cash != 0)
{
voucher = GetLedger("Cash", cash.ToString("#0.00")) + voucher;
}
}
return voucher != "" ? GetVoucher(date.ToString("yyyyMMdd"), voucher) : "";
}
private static string GetVoucher(string date, string ledgers)
{
#region Voucher Template
var template = @"
<VOUCHER REMOTEID=""{0}"" VCHTYPE=""Journal"" ACTION=""Create"">
<DATE>{1}</DATE>
<GUID>{0}</GUID>
<VOUCHERTYPENAME>Journal</VOUCHERTYPENAME>
<VOUCHERNUMBER>349</VOUCHERNUMBER>
<PARTYLEDGERNAME>Cash</PARTYLEDGERNAME>
<CSTFORMISSUETYPE/>
<CSTFORMRECVTYPE/>
<FBTPAYMENTTYPE>Default</FBTPAYMENTTYPE>
<VCHGSTCLASS/>
<ENTEREDBY>Admin</ENTEREDBY>
<DIFFACTUALQTY>No</DIFFACTUALQTY>
<AUDITED>No</AUDITED>
<FORJOBCOSTING>No</FORJOBCOSTING>
<ISOPTIONAL>No</ISOPTIONAL>
<EFFECTIVEDATE>{1}</EFFECTIVEDATE>
<USEFORINTEREST>No</USEFORINTEREST>
<USEFORGAINLOSS>No</USEFORGAINLOSS>
<USEFORGODOWNTRANSFER>No</USEFORGODOWNTRANSFER>
<USEFORCOMPOUND>No</USEFORCOMPOUND>
<ALTERID> 3525</ALTERID>
<EXCISEOPENING>No</EXCISEOPENING>
<ISCANCELLED>No</ISCANCELLED>
<HASCASHFLOW>Yes</HASCASHFLOW>
<ISPOSTDATED>No</ISPOSTDATED>
<USETRACKINGNUMBER>No</USETRACKINGNUMBER>
<ISINVOICE>No</ISINVOICE>
<MFGJOURNAL>No</MFGJOURNAL>
<HASDISCOUNTS>No</HASDISCOUNTS>
<ASPAYSLIP>No</ASPAYSLIP>
<ISDELETED>No</ISDELETED>
<ASORIGINAL>No</ASORIGINAL>
{2}
</VOUCHER>
";
var servicetaxtemplate = @"";
#endregion
return string.Format(template, Guid.NewGuid(), date, ledgers);
}
private static string GetLedger(string ledgername, string amount)
{
var isDeemedPositive = string.Empty;
var isPartyLedger = string.Empty;
switch (ledgername)
{
case "Cash":
isDeemedPositive = "Yes";
isPartyLedger = "Yes";
amount = "-" + amount;
break;
case "Sale 12.5%":
isDeemedPositive = "No";
isPartyLedger = "No";
break;
case "Sale @ 15%":
isDeemedPositive = "No";
isPartyLedger = "No";
break;
case "Sale @ 25 %":
isDeemedPositive = "No";
isPartyLedger = "No";
break;
case "Sale Tax Free":
isDeemedPositive = "No";
isPartyLedger = "No";
break;
case "Output Vat 12.5%":
isDeemedPositive = "No";
isPartyLedger = "No";
break;
case "Output Vat @ 15%":
isDeemedPositive = "No";
isPartyLedger = "No";
break;
case "Output Vat @ 25%":
isDeemedPositive = "No";
isPartyLedger = "No";
break;
case "Central Service Tax@3.708%":
isDeemedPositive = "No";
isPartyLedger = "No";
break;
}
#region Voucher Template
const string template = @"
<ALLLEDGERENTRIES.LIST>
<LEDGERNAME>{0}</LEDGERNAME>
<GSTCLASS/>
<ISDEEMEDPOSITIVE>{1}</ISDEEMEDPOSITIVE>
<LEDGERFROMITEM>No</LEDGERFROMITEM>
<REMOVEZEROENTRIES>No</REMOVEZEROENTRIES>
<ISPARTYLEDGER>{2}</ISPARTYLEDGER>
<AMOUNT>{3}</AMOUNT>
</ALLLEDGERENTRIES.LIST>
";
var servicetaxtemplate = @"";
#endregion
return string.Format(template, ledgername, isDeemedPositive, isPartyLedger, amount);
}
private void button2_Click(object sender, EventArgs e)
{
using (var bi = new ManagementBI())
{
bi.FinalSanction(dtpStart.Value.Date.AddHours(7), dtpFinish.Value.Date.AddDays(1).AddHours(7));
}
}
private void button3_Click(object sender, EventArgs e)
{
button3.Enabled = false;
var startDate = dtpStart.Value.Date;
var finishDate = dtpFinish.Value.Date;
var sheet = "Date\t Bill Start\t Bill Final\t Sale 0%\t Sale 12.5%\t Sale 15%\t Sale 25%\t Vat 12.5%\t Vat 15%\t Vat 25%\t Service Tax\n";
for (var date = startDate; date <= finishDate; date = date.AddDays(1))
{
sheet += GetExcel(date);
}
Clipboard.SetText(sheet, TextDataFormat.Text);
button3.Enabled = true;
}
private static string GetExcel(DateTime date)
{
using (var bi = new ManagementBI())
{
var currentStart = date.AddHours(7);
var currentFinish = date.AddDays(1).AddHours(7);
var cash = bi.GetFirstBill(date);
if (cash == "")
return "";
cash = string.Format("{0:dd-MMM-yyyy}\t'{1}\t'{2}\t", date, cash, bi.GetLastBill(date));
cash += string.Format("{0:#0}\t", Math.Round(bi.GetFood(0M, currentStart, currentFinish)));
cash += string.Format("{0:#0}\t", Math.Round(bi.GetFood(.13125M, currentStart, currentFinish)));
cash += string.Format("{0:#0}\t", Math.Round(bi.GetFood(.1575M, currentStart, currentFinish)));
cash += string.Format("{0:#0}\t", Math.Round(bi.GetFood(.2625M, currentStart, currentFinish)));
cash += string.Format("{0:#0}\t", Math.Round(bi.GetVat(.13125M, currentStart, currentFinish)));
cash += string.Format("{0:#0}\t", Math.Round(bi.GetVat(.1575M, currentStart, currentFinish)));
cash += string.Format("{0:#0}\t", Math.Round(bi.GetVat(.2625M, currentStart, currentFinish)));
cash += string.Format("{0:#0}\n", Math.Round(bi.GetServiceTax(currentStart, currentFinish)));
return cash;
}
}
}
}