FormOrder.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Data.SqlClient;
  6. using System.Diagnostics;
  7. using System.Drawing;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12. using static СУБД_Книжный_клуб.FormMain;
  13. using Excel = Microsoft.Office.Interop.Excel;
  14. namespace СУБД_Книжный_клуб
  15. {
  16. public partial class FormOrder : Form
  17. {
  18. public FormOrder()
  19. {
  20. InitializeComponent();
  21. }
  22. SqlConnection Con = new SqlConnection(FormMain.TxtCon);
  23. /// <summary>
  24. /// Вывод списка товаров в заказе
  25. /// </summary>
  26. void FillListProduct()
  27. {
  28. DgvProductList.Rows.Clear();
  29. for (int i = 0; i <= LstOrder.Count-1; i++)
  30. {
  31. string TxtQuery = @"SELECT Product.IdProduct, Product.Photo,
  32. Product.NameProduct, Manufacturer.NameManufacturer, Product.Cost,
  33. Product.Discount, Product.CountProduct, Product.Description
  34. FROM Product INNER JOIN Manufacturer ON Product.IdManufacturer =
  35. Manufacturer.IdManufacturer
  36. where IdProduct =" + LstOrder[i].IdProduct;
  37. SqlCommand Query = new SqlCommand(TxtQuery, Con);
  38. Con.Open();
  39. SqlDataReader Res = Query.ExecuteReader();
  40. Res.Read();
  41. string Info = $@"Товар: {Res["NameProduct"]}
  42. Производитель: {Res["NameManufacturer"]}
  43. Описание: {Res["Description"]}
  44. Цена: {Res["Cost"]}
  45. Скидка: {Res["Discount"]}
  46. Кол-во на складе: {Res["CountProduct"]}";
  47. try
  48. {
  49. DgvProductList.Rows.Add(Image.FromFile(Application.StartupPath + "\\Фото\\" + Res["Photo"]), Info, LstOrder[i].CountInOrder);
  50. }
  51. catch
  52. {
  53. DgvProductList.Rows.Add( Image.FromFile(Application.StartupPath + "\\Фото\\blank.jpg" + Res["Photo"]), Info, LstOrder[i].CountInOrder);
  54. }
  55. Con.Close();
  56. }
  57. }
  58. public List<FormMain.OrderItem> LstOrder = new List<FormMain.OrderItem>();
  59. private void BtnCheckOut_Click(object sender, EventArgs e)
  60. {
  61. if (LstOrder.Count == 0)
  62. {
  63. MessageBox.Show("Нет товаров в заказе!","Внимание!",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
  64. return;
  65. }
  66. Random Rnd = new Random();
  67. int Code = Rnd.Next(100, 1000);
  68. int DeliveryDay = 3;
  69. for (int i = 0; i <= LstOrder.Count - 1; i++)
  70. if (LstOrder[i].CountInStorage <= 3)
  71. {
  72. DeliveryDay = 6;
  73. break;
  74. }
  75. Con.Open();
  76. string TxtQuery = $@"insert into [Order] (IdUser, IdPickupPoint,DateOrder, Code,IdOrderStatus)
  77. Values (4,'{CmbPickUpPoint.SelectedValue.ToString()}',GetDate(),{Code},1)
  78. Select SCOPE_Identity()";
  79. SqlCommand Com = new SqlCommand(TxtQuery,Con);
  80. string IdOrder = Com.ExecuteScalar().ToString();
  81. for (int i = 0; i<= LstOrder.Count-1; i++)
  82. {
  83. TxtQuery = $@"insert into OrderProduct (IdOrder, IdProduct, CountProduct)
  84. Values ({IdOrder},{LstOrder[i].IdProduct},{LstOrder[i].CountInOrder})";
  85. Com = new SqlCommand(TxtQuery, Con);
  86. Com.ExecuteNonQuery();
  87. int NewCount = LstOrder[i].CountInStorage - LstOrder[i].CountInOrder;
  88. if (NewCount < 0 )
  89. NewCount= 0;
  90. TxtQuery = $@"Update Product Set CountProduct = {NewCount}
  91. where IdProduct ={LstOrder[i].IdProduct}";
  92. //
  93. Com = new SqlCommand(TxtQuery, Con);
  94. Com.ExecuteNonQuery();
  95. }
  96. Con.Close();
  97. FillListProduct();
  98. MessageBox.Show("Заказ сформирован. Код для получения: "+ Code,"Внимание!", MessageBoxButtons.OK, MessageBoxIcon.Information) ;
  99. if (SaveToPDF.ShowDialog() != DialogResult.OK)
  100. {
  101. LstOrder.Clear();
  102. Close();
  103. }
  104. Excel.Application ExApp = new Excel.Application();
  105. //ExApp.Visible= true;
  106. ExApp.Workbooks.Open(Application.StartupPath + "\\Заказ.xlsx",null,true);
  107. Excel.Worksheet MyList = ExApp.Worksheets.get_Item(1);
  108. double Sum = 0, DiscountSum = 0;
  109. CalcOrder(ref Sum, ref DiscountSum);
  110. MyList.Cells[1, 2] = DateTime.Now.ToString("dd.MM.yyyy");
  111. MyList.Cells[2, 2] = IdOrder;
  112. MyList.Cells[3, 2] =Sum ;
  113. MyList.Cells[4, 2] = DiscountSum ;
  114. MyList.Cells[5, 2] = CmbPickUpPoint.Text;
  115. MyList.Cells[6, 2] = DeliveryDay + "д.";
  116. MyList.Cells[7, 2] = Code;
  117. for (int i = 0; i <= LstOrder.Count - 1; i++)
  118. MyList.Cells[i+9, 1] = $"{LstOrder[i].NameProduct} - {LstOrder[i].CountInOrder}шт.";
  119. MyList.ExportAsFixedFormat(Excel.XlFixedFormatType.xlTypePDF, SaveToPDF.FileName);
  120. ExApp.DisplayAlerts = false;
  121. ExApp.Quit();
  122. Process.Start(SaveToPDF.FileName);
  123. LstOrder.Clear();
  124. Close();
  125. }
  126. private void FormOrder_Load(object sender, EventArgs e)
  127. {
  128. // TODO: This line of code loads data into the 'bookClubKMADataSet1.PickupPoint' table. You can move, or remove it, as needed.
  129. this.pickupPointTableAdapter.Fill(this.bookClubKMADataSet1.PickupPoint);
  130. FillListProduct();
  131. OutPutInfo();
  132. }
  133. private void DgvProductList_CellValueChanged(object sender, DataGridViewCellEventArgs e)
  134. {
  135. if (DgvProductList.CurrentRow == null)
  136. return;
  137. int n = DgvProductList.CurrentRow.Index;
  138. int OldNum = LstOrder[n].CountInOrder;
  139. try
  140. {
  141. LstOrder[n].CountInOrder = int.Parse(DgvProductList.CurrentRow.Cells[2].Value.ToString());
  142. }
  143. catch
  144. {
  145. DgvProductList.CurrentRow.Cells[2].Value = OldNum;
  146. MessageBox.Show("Произошла ошибка. Кол-во товара должно быть числом.", "Внимаение!", MessageBoxButtons.OK, MessageBoxIcon.Error);
  147. }
  148. if (LstOrder[n].CountInOrder < 0)
  149. {
  150. DgvProductList.CurrentRow.Cells[2].Value = OldNum;
  151. MessageBox.Show("Кол-во не должно быть отрицательным!", "Внимаение!", MessageBoxButtons.OK, MessageBoxIcon.Error);
  152. }
  153. if (LstOrder[n].CountInOrder == 0)
  154. {
  155. if (MessageBox.Show("Вы точно хотите удалить товар?", "Внимание!", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
  156. {
  157. DgvProductList.CurrentRow.Cells[2].Value = OldNum;
  158. return;
  159. }
  160. else
  161. {
  162. LstOrder.RemoveAt(e.RowIndex);
  163. FillListProduct();
  164. }
  165. }
  166. OutPutInfo();
  167. }
  168. /// <summary>
  169. /// Вывод информации о сумме заказа и сумме скидки
  170. /// </summary>
  171. public void OutPutInfo()
  172. {
  173. double Sum = 0, DiscountSum = 0;
  174. FormMain.CalcOrder(ref Sum, ref DiscountSum);
  175. LblSum.Text = $"Сумма заказа (с учетом скидки): {Sum}";
  176. LblDiscount.Text = $"Скидка: {DiscountSum}";
  177. }
  178. private void FormOrder_FormClosing(object sender, FormClosingEventArgs e)
  179. {
  180. DgvProductList.EndEdit();
  181. DgvProductList.Update();
  182. FillListProduct();
  183. }
  184. private void BtnDell_Click(object sender, EventArgs e)
  185. {
  186. if (LstOrder.Count == 0)
  187. {
  188. MessageBox.Show("Нет товаров в заказе!", "Внимаение!", MessageBoxButtons.OK, MessageBoxIcon.Error);
  189. return;
  190. }
  191. int n = DgvProductList.CurrentRow.Index;
  192. if (MessageBox.Show("Вы точно хотите удалить товар?", "Внимание!", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  193. {
  194. LstOrder.RemoveAt(n);
  195. FillListProduct();
  196. }
  197. OutPutInfo();
  198. }
  199. private void CmbPickUpPoint_SelectedIndexChanged(object sender, EventArgs e)
  200. {
  201. this.Text = CmbPickUpPoint.SelectedValue.ToString();
  202. }
  203. }
  204. }