using System; using System.Collections.Generic; using System.Globalization; using System.Windows.Forms; using Tanshu.Accounts.Repository; using Tanshu.Accounts.Contracts; namespace Tanshu.Accounts.Management { public partial class QuantityForm : Form { private IList textBoxes; public QuantityForm() { InitializeComponent(); } private void Sale_Analysis_Form_Load(object sender, EventArgs e) { dtpDate.Value = DateTime.Today; textBoxes = new List(); for (int i = 0; i < 8; i++) { var text = ""; switch (i) { case 0: text = "1 - Dark"; break; case 1: text = "2 - Wheat"; break; case 2: text = "3 - Premium"; break; case 3: text = "4 - Light"; break; case 4: text = "5 - Dragon"; break; case 5: text = "6 - Festival"; break; case 6: text = "7 - Vanilla"; break; case 7: text = "8 - Strong"; break; } var label = new Label() { Name = "label" + (i + 1).ToString(CultureInfo.InvariantCulture), AutoSize = true, Text = text }; flpProducts.Controls.Add(label); var textBox = new TextBox() { Name = "textBox" + (i + 1).ToString(CultureInfo.InvariantCulture), Width = 400, Height = 22, Tag = new object[] { (i + 1), text } }; textBoxes.Add(textBox); flpProducts.Controls.Add(textBox); } } private void btnGo_Click(object sender, EventArgs e) { foreach (var textBox in textBoxes) { var startDate = dtpDate.Value.Date.AddHours(7); var finishDate = startDate.AddDays(1); var baseCode = (int)(((object[])textBox.Tag)[0]); var text = (string)(((object[])textBox.Tag)[1]); foreach (var item in textBox.Text.Split(',')) { var newQuantity = TryConvert(item); var quantity = GetQuantity(baseCode, startDate, finishDate); if (MessageBox.Show(text + " " + startDate.ToString("dd-MMM-yyyy") + " " + quantity.ToString(), "Quantity of Beer", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2) == DialogResult.Yes && quantity > newQuantity && newQuantity > 0) { MessageBox.Show(SetQuantity(baseCode, newQuantity, startDate, finishDate).ToString()); } startDate = startDate.AddDays(1); finishDate = finishDate.AddDays(1); } } } private decimal TryConvert(string amount) { decimal result = 0; decimal.TryParse(amount, out result); return result; } private decimal GetQuantity(int baseCode, DateTime startDate, DateTime finishDate) { using (var bi = new ManagementBI()) return bi.GetQuantity(baseCode, startDate, finishDate); } private decimal SetQuantity(int baseCode, decimal quantity, DateTime startDate, DateTime finishDate) { using (var bi = new ManagementBI()) return bi.SetQuantity(baseCode, quantity, startDate, finishDate); } private void btnGetClipboard_Click(object sender, EventArgs e) { //Clipboard format -- Date,BaseCode,Quantity" var fmtCsv = DataFormats.CommaSeparatedValue; // read the CSV var dataobject = Clipboard.GetDataObject(); if (dataobject == null) { MessageBox.Show(@"No Data in clipboard"); return; } var stream = (System.IO.Stream)dataobject.GetData(fmtCsv); var enc = new System.Text.UTF8Encoding(); IFormatProvider culture = new CultureInfo("en-US", true); using (var reader = new System.IO.StreamReader(stream, enc)) { using (var bi = new ManagementBI()) { string line; while ((line = reader.ReadLine()) != null) { DateTime startDate; int baseCode; decimal quantity; var data = line.Split(','); if (!DateTime.TryParseExact(data[0], "dd-MMM-yyyy", culture, DateTimeStyles.NoCurrentDateDefault, out startDate)) continue; var finishDate = startDate.AddDays(1).AddHours(7); startDate = startDate.AddHours(7); if (!int.TryParse(data[1], out baseCode)) continue; if (baseCode <= 0) continue; if (!decimal.TryParse(data[2], out quantity)) continue; bi.SetQuantity(baseCode, quantity, startDate, finishDate); } } } } } }