Aucune description

OrderForm.cs 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data.SQLite;
  5. using System.Runtime.CompilerServices;
  6. using System.Threading;
  7. using System.Windows.Forms;
  8. namespace BulkPrinting
  9. {
  10. public partial class OrderForm : ObservedForm
  11. {
  12. private class OrderItem : INotifyPropertyChanged
  13. {
  14. private int _quantity;
  15. public int Quantity {
  16. get { return _quantity; }
  17. set
  18. {
  19. _quantity = value;
  20. _notifyPropertyChanged("ToString");
  21. }
  22. }
  23. public event PropertyChangedEventHandler PropertyChanged;
  24. private void _notifyPropertyChanged([CallerMemberName] String propertyName = "")
  25. {
  26. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  27. }
  28. public int ProductId;
  29. public string ProductDescription;
  30. public decimal FaceValue;
  31. public decimal DiscountPercentage;
  32. public override string ToString() {
  33. return this.Quantity.ToString() + " X " + this.ProductDescription;
  34. }
  35. }
  36. private BindingList<OrderItem> SelectedProducts = new BindingList<OrderItem>();
  37. public BatchForm SourceForm;
  38. public OrderForm()
  39. {
  40. InitializeComponent();
  41. }
  42. private void OrderForm_Load(object sender, EventArgs e)
  43. {
  44. SourceForm.Enabled = false;
  45. this.lstSelectedProducts.DataSource = SelectedProducts;
  46. this.lstSelectedProducts.DisplayMember = "ToString";
  47. bool Result = Utility.RESTRequest<ICollection<NetworkCatalogue>>(ref Globals.ProductCatalogue, "/api/products/");
  48. if (Result == true)
  49. {
  50. //lstSelectedProducts.Items.Clear();
  51. lstProductCatalogue.Items.Clear();
  52. foreach (NetworkCatalogue Network in Globals.ProductCatalogue) {
  53. foreach (ProductSubCatalogue Product in Network.Products)
  54. {
  55. lstProductCatalogue.Items.Add(Product);
  56. }
  57. }
  58. this.txtReference.Text = Globals.SessionData.Credentials.Payload.User.FirstName;
  59. }
  60. else {
  61. MessageBox.Show("Could not retrieve product catalogue. Please ensure that you are online.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  62. this.Close();
  63. }
  64. decimal RemainingUserLimit = 0;
  65. this.lblRemainingBalance.Text = "R" + Globals.SessionData.Credentials.Payload.User.Account.Balance.ToString("0.##");
  66. RemainingUserLimit = Utility.CheckRemainingUserLimit(UserLimits.UserLimitTypes.BulkOrder);
  67. if (RemainingUserLimit > -1)
  68. {
  69. this.lblRemainingAllowance.Text = "R" + RemainingUserLimit.ToString("0.##");
  70. this.lblRemainingAllowanceText.Show();
  71. this.lblRemainingAllowance.Show();
  72. }
  73. else
  74. {
  75. this.lblRemainingAllowanceText.Hide();
  76. this.lblRemainingAllowance.Hide();
  77. }
  78. }
  79. private void btnPlaceOrder_Click(object sender, EventArgs e)
  80. {
  81. if (this.SelectedProducts.Count==0)
  82. {
  83. MessageBox.Show("Please add products to the order list on the right before clicking the Order button.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  84. return;
  85. }
  86. if (this.txtReference.Text == "")
  87. {
  88. MessageBox.Show("Please enter a reference code.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  89. return;
  90. }
  91. string OrderReviewMessage = "Please ensure that you carefully review the list of products you wish to order:\n";
  92. decimal TotalCost = 0;
  93. for (int i = 0; i < this.SelectedProducts.Count; i++)
  94. {
  95. OrderItem OrderedItem = this.SelectedProducts[i];
  96. decimal ItemCost = OrderedItem.Quantity * OrderedItem.FaceValue * (1 - (OrderedItem.DiscountPercentage / 100));
  97. TotalCost += ItemCost;
  98. OrderReviewMessage += "R" + ItemCost.ToString("0.##") + " - " + OrderedItem.Quantity.ToString() + " X " + OrderedItem.ProductDescription + "\n";
  99. }
  100. OrderReviewMessage += "______________________\nTotal Order Cost: R" + TotalCost.ToString("0.##") + "\n";
  101. OrderReviewMessage += "Remaining Balance After Order: R" + (Globals.SessionData.Credentials.Payload.User.Account.Balance - TotalCost).ToString("0.##") + "\n\n";
  102. OrderReviewMessage += "Would you like to proceed with the order?";
  103. DialogResult PlaceOrder = MessageBox.Show(OrderReviewMessage, "Proceed?", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
  104. if (PlaceOrder == DialogResult.Yes) {
  105. string InternalReference = Utility.GetNextInternalReference();
  106. Globals.DB.ExecuteNonQuery(
  107. "INSERT INTO Orders (UserId,UserName,OrderDate,OrderCost,BalanceAfterOrder,InternalReference) VALUES (@userid,@username,datetime('now'),@ordercost,@balanceafterorder,@internalreference)",
  108. new SQLiteParameter("@userid", Globals.SessionData.Credentials.Payload.User.Id),
  109. new SQLiteParameter("@username", Globals.SessionData.Credentials.Payload.User.FirstName + " " + Globals.SessionData.Credentials.Payload.User.Surname),
  110. new SQLiteParameter("@ordercost", TotalCost),
  111. new SQLiteParameter("@balanceafterorder", Globals.SessionData.Credentials.Payload.User.Account.Balance - TotalCost),
  112. new SQLiteParameter("@internalreference", InternalReference));
  113. int OrderId = int.Parse(Globals.DB.ExecuteScalar("SELECT MAX(Id) FROM Orders").ToString());
  114. lock (Globals.DB.WriteLock)
  115. {
  116. using (var trans = Globals.DB.BeginTransaction())
  117. {
  118. var Sql = "INSERT INTO OrderedItems (OrderId,Cost,Quantity,Description,ProductId) VALUES (@orderId,@cost,@quantity,@description,@productid)";
  119. using (var Command = Globals.DB.CreateCommand(Sql, trans))
  120. {
  121. for (int i = 0; i < this.SelectedProducts.Count; i++)
  122. {
  123. OrderItem OrderedItem = this.SelectedProducts[i];
  124. decimal ItemCost = OrderedItem.Quantity * OrderedItem.FaceValue * (1 - (OrderedItem.DiscountPercentage / 100));
  125. Command.Parameters.Clear();
  126. Command.Parameters.AddWithValue("@orderId", OrderId);
  127. Command.Parameters.AddWithValue("@cost", ItemCost);
  128. Command.Parameters.AddWithValue("@quantity", OrderedItem.Quantity);
  129. Command.Parameters.AddWithValue("@description", OrderedItem.ProductDescription);
  130. Command.Parameters.AddWithValue("@productid", OrderedItem.ProductId);
  131. Command.ExecuteNonQuery();
  132. }
  133. }
  134. trans.Commit();
  135. }
  136. }
  137. this.Hide();
  138. string CustomerReference = txtReference.Text;
  139. ProgressDialog Progress = new ProgressDialog();
  140. Progress.Show();
  141. Thread t = new Thread(() => {
  142. var db = Globals.DB;
  143. //using (var db = Utility.OpenDBConnection())
  144. {
  145. Progress.UpdateProgressText("Please wait...");
  146. Progress.UpdateProgressBar(0);
  147. for (int i = 0; i < this.SelectedProducts.Count; i++)
  148. {
  149. OrderItem OrderedItem = this.SelectedProducts[i];
  150. Progress.UpdateProgressText("Placing order for " + OrderedItem.Quantity.ToString() + " of " + OrderedItem.ProductDescription + "...");
  151. OrderPlacementData OrderData = new OrderPlacementData();
  152. OrderData.ProductId = OrderedItem.ProductId;
  153. OrderData.Quantity = OrderedItem.Quantity;
  154. OrderData.CustomerReference = CustomerReference;
  155. OrderData.InternalReference = InternalReference;
  156. BatchListing OrderedBatch = new BatchListing();
  157. bool OrderResult = Utility.RESTRequest<OrderPlacementData, BatchListing>(OrderData, ref OrderedBatch, "/api/batches/");
  158. if (OrderedBatch.Batch == null)
  159. {
  160. string ErrorMessage = "An error occurred while attempting to order " + OrderedItem.Quantity.ToString() + " of " + OrderedItem.ProductDescription;
  161. if (i < (SelectedProducts.Count - 1))
  162. {
  163. ErrorMessage += "\nPress OK to continue attempting to order remaining items.";
  164. }
  165. MessageBox.Show(ErrorMessage, "Error Ordering", MessageBoxButtons.OK, MessageBoxIcon.Error);
  166. continue;
  167. }
  168. //TODO: Handle failed order
  169. Utility.SaveBatch(db, OrderedBatch.Batch);
  170. SourceForm.NewBatchAvailable();
  171. Globals.SessionData.Credentials.Payload.User.Account.Balance = OrderedBatch.RemainingBalance;
  172. Utility.AddUserUsage(db, UserLimits.UserLimitTypes.BulkOrder, OrderedBatch.Batch.Cost);
  173. Progress.UpdateProgressBar((int)Math.Round(100 * (((decimal)i + 1) / (decimal)SelectedProducts.Count)));
  174. if (OrderedBatch.Batch.DeliveredQuantity == 0)
  175. {
  176. string ErrorMessage = "Your order for " +
  177. OrderedItem.Quantity.ToString() + " of " + OrderedItem.ProductDescription +
  178. " could not be fulfilled as no vouchers are available.";
  179. if (i < (SelectedProducts.Count - 1))
  180. {
  181. ErrorMessage += "\nPress OK to continue attempting to order remaining items.";
  182. }
  183. MessageBox.Show(ErrorMessage, "Error Ordering", MessageBoxButtons.OK, MessageBoxIcon.Error);
  184. }
  185. Thread.Sleep(50);
  186. }
  187. Progress.Invoke((Action)(() => Progress.Close()));
  188. this.Invoke((Action)(() => { SourceForm.PopulateGrid(); this.Close(); }));
  189. }
  190. });
  191. t.Start();
  192. }
  193. }
  194. private void btnCancel_Click(object sender, EventArgs e)
  195. {
  196. this.Close();
  197. }
  198. private void btnAddProduct_Click(object sender, EventArgs e)
  199. {
  200. if (this.lstProductCatalogue.SelectedIndex == -1)
  201. {
  202. MessageBox.Show("Please select a product from the left list to add to the order list on the right.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  203. return;
  204. }
  205. if (this.numQuantity.Value < 1)
  206. {
  207. MessageBox.Show("Please select a valid quantity.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  208. return;
  209. }
  210. decimal TotalValue = 0;
  211. decimal TotalCost = 0;
  212. ProductSubCatalogue SelectedProduct = (ProductSubCatalogue)this.lstProductCatalogue.SelectedItem;
  213. bool ProductFound = false;
  214. decimal SelectionCost;
  215. foreach (OrderItem OrderedItem in this.SelectedProducts)
  216. {
  217. TotalValue += OrderedItem.Quantity * OrderedItem.FaceValue;
  218. TotalCost += OrderedItem.Quantity * OrderedItem.FaceValue * (1 - (OrderedItem.DiscountPercentage / 100));
  219. }
  220. foreach (OrderItem OrderedItem in this.SelectedProducts)
  221. {
  222. if (SelectedProduct.Id == OrderedItem.ProductId)
  223. {
  224. TotalValue += (decimal)this.numQuantity.Value * OrderedItem.FaceValue;
  225. SelectionCost = (decimal)this.numQuantity.Value * OrderedItem.FaceValue * (1 - (OrderedItem.DiscountPercentage / 100));
  226. TotalCost += SelectionCost;
  227. if (TotalCost > Globals.SessionData.Credentials.Payload.User.Account.Balance)
  228. {
  229. MessageBox.Show("Your balance is insufficient to add this selection (R" + SelectionCost.ToString("0.##") + ").", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  230. return;
  231. }
  232. if (!Utility.IsValueWithinRemainingUserLimit(UserLimits.UserLimitTypes.BulkOrder,TotalCost))
  233. {
  234. MessageBox.Show("Adding this selection (R" + SelectionCost.ToString("0.##") + ") exceeds your user limit for the day.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  235. return;
  236. }
  237. ProductFound = true;
  238. OrderedItem.Quantity += (int)this.numQuantity.Value;
  239. }
  240. }
  241. if (ProductFound == false)
  242. {
  243. TotalValue += (decimal)this.numQuantity.Value * SelectedProduct.FaceValue;
  244. SelectionCost = (decimal)this.numQuantity.Value * SelectedProduct.FaceValue * (1 - (SelectedProduct.DiscountPercentage / 100));
  245. TotalCost += SelectionCost;
  246. if (TotalCost > Globals.SessionData.Credentials.Payload.User.Account.Balance)
  247. {
  248. MessageBox.Show("Your balance is insufficient to add this selection (R" + SelectionCost.ToString("0.##") + ").", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  249. return;
  250. }
  251. if (!Utility.IsValueWithinRemainingUserLimit(UserLimits.UserLimitTypes.BulkOrder, TotalCost))
  252. {
  253. MessageBox.Show("Adding this selection (R" + SelectionCost.ToString("0.##") + ") exceeds your user limit for the day.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  254. return;
  255. }
  256. OrderItem NewOrderItem = new OrderItem();
  257. NewOrderItem.ProductId = SelectedProduct.Id;
  258. NewOrderItem.ProductDescription = SelectedProduct.Description;
  259. NewOrderItem.Quantity = (int)this.numQuantity.Value;
  260. NewOrderItem.FaceValue = SelectedProduct.FaceValue;
  261. NewOrderItem.DiscountPercentage = SelectedProduct.DiscountPercentage;
  262. this.SelectedProducts.Add(NewOrderItem);
  263. }
  264. this.lblTotalValue.Text = "R"+TotalValue.ToString("0.##");
  265. this.lblTotalCost.Text = "R"+TotalCost.ToString("0.##");
  266. }
  267. private void btnRemoveProduct_Click(object sender, EventArgs e)
  268. {
  269. if (this.lstProductCatalogue.SelectedIndex != -1)
  270. {
  271. this.SelectedProducts.Remove((OrderItem)this.lstSelectedProducts.SelectedItem);
  272. }
  273. }
  274. private void OrderForm_FormClosing(object sender, FormClosingEventArgs e)
  275. {
  276. SourceForm.Enabled = true;
  277. }
  278. }
  279. }