FormAddZakaz.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Data.SqlClient;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. namespace СУБД_Швейная_фабрика
  12. {
  13. public partial class FormAddZakaz : Form
  14. {
  15. public FormAddZakaz()
  16. {
  17. InitializeComponent();
  18. }
  19. public string IdZakazchik = "";
  20. string IdTkan = "", IdFur = "";
  21. public struct Tkani
  22. {
  23. public string IdTkan, NameTkan, ColorTkan, Width, Length, Description;
  24. public Image PhotoTkan;
  25. }
  26. List<Tkani> LstTkani = new List<Tkani>();
  27. void GetTkaniFromDB()
  28. {
  29. SqlConnection con = new SqlConnection(Properties.Settings.Default.dbFabrikaOAAConnectionString);
  30. con.Open();
  31. SqlCommand Query = new SqlCommand("select * from Tkan where Length > 0", con);
  32. SqlDataReader Data = Query.ExecuteReader();
  33. while (Data.Read())
  34. {
  35. Tkani Tk = new Tkani();
  36. Tk.IdTkan = Data["id_Tkan"].ToString();
  37. Tk.NameTkan = Data["Title"].ToString();
  38. Tk.ColorTkan = Data["id_color"].ToString();
  39. Tk.Width = Data["Width"].ToString();
  40. Tk.Length = Data["Length"].ToString();
  41. Tk.Description = Data["Comment"].ToString();
  42. try
  43. {
  44. byte[] PhotoByte = (byte[])Data["NamePhoto"];
  45. ImageConverter ImConverter = new ImageConverter();
  46. Tk.PhotoTkan = (Bitmap)ImConverter.ConvertFrom(PhotoByte);
  47. }
  48. catch
  49. {
  50. Tk.PhotoTkan = Properties.Resources.fabric_logo;
  51. }
  52. LstTkani.Add(Tk);
  53. }
  54. con.Close();
  55. }
  56. void FillLVTkani()
  57. {
  58. LVTkani.Items.Clear();
  59. ImageListTkani.Images.Clear();
  60. foreach (Tkani Tk in LstTkani)
  61. {
  62. ListViewItem LV = new ListViewItem(Tk.NameTkan);
  63. ImageListTkani.Images.Add(Tk.PhotoTkan);
  64. LV.ImageIndex = ImageListTkani.Images.Count - 1;
  65. LVTkani.Items.Add(LV);
  66. }
  67. }
  68. public struct Furnitura
  69. {
  70. public string IdFur, NameFur, CountFur;
  71. public Image PhotoFur;
  72. }
  73. List<Furnitura> LstFurnitura = new List<Furnitura>();
  74. void GetFurnituraFromDB()
  75. {
  76. SqlConnection con = new SqlConnection(Properties.Settings.Default.dbFabrikaOAAConnectionString);
  77. con.Open();
  78. SqlCommand Query = new SqlCommand("select * from Furnitura where countfur > 0", con);
  79. SqlDataReader Data = Query.ExecuteReader();
  80. while (Data.Read())
  81. {
  82. Furnitura Fr = new Furnitura();
  83. Fr.IdFur = Data["id_fur"].ToString();
  84. Fr.NameFur = Data["namefur"].ToString();
  85. Fr.CountFur = Data["countfur"].ToString();
  86. try
  87. {
  88. byte[] PhotoByte = (byte[])Data["photo"];
  89. ImageConverter ImConverter = new ImageConverter();
  90. Fr.PhotoFur = (Bitmap)ImConverter.ConvertFrom(PhotoByte);
  91. }
  92. catch
  93. {
  94. Fr.PhotoFur = Properties.Resources.fabric_logo;
  95. }
  96. LstFurnitura.Add(Fr);
  97. }
  98. con.Close();
  99. }
  100. void FillLVFurnitura()
  101. {
  102. LVFurnitura.Items.Clear();
  103. ImageListFur.Images.Clear();
  104. foreach (Furnitura Fr in LstFurnitura)
  105. {
  106. ListViewItem LV = new ListViewItem(Fr.NameFur);
  107. ImageListFur.Images.Add(Fr.PhotoFur);
  108. LV.ImageIndex = ImageListFur.Images.Count - 1;
  109. LVFurnitura.Items.Add(LV);
  110. }
  111. }
  112. int CutTkan(int WidthTkan, int WidthIzd, int LengthIzd, int CountIzd)
  113. {
  114. int IzdInRow1 = WidthTkan / WidthIzd;
  115. int Row1 = CountIzd / IzdInRow1;
  116. if (IzdInRow1 * Row1 < CountIzd)
  117. Row1++;
  118. int CutTkan1 = LengthIzd * Row1;
  119. int IzdInRow2 = WidthTkan / LengthIzd;
  120. int Row2 = CountIzd / IzdInRow2;
  121. if (IzdInRow2 * Row2 < CountIzd)
  122. Row2++;
  123. int CutTkan2 = WidthIzd * Row2;
  124. return Math.Min(CutTkan1, CutTkan2);
  125. }
  126. private void LVTkani_SelectedIndexChanged(object sender, EventArgs e)
  127. {
  128. if (LVTkani.SelectedItems.Count > 0)
  129. {
  130. int num = LVTkani.SelectedIndices[0];
  131. NametkanTextBox.Text = LstTkani[num].NameTkan;
  132. ColorTextBox.Text = LstTkani[num].ColorTkan;
  133. WidthTextBox.Text = LstTkani[num].Width;
  134. LengthTextBox.Text = LstTkani[num].Length;
  135. DescriptionTextBox.Text = LstTkani[num].Description;
  136. IdTkan = LstTkani[num].IdTkan;
  137. }
  138. else
  139. {
  140. NametkanTextBox.Text = "";
  141. ColorTextBox.Text = "";
  142. WidthTextBox.Text = "";
  143. LengthTextBox.Text = "";
  144. DescriptionTextBox.Text = "";
  145. IdTkan = "";
  146. }
  147. }
  148. private void LVFurnitura_SelectedIndexChanged(object sender, EventArgs e)
  149. {
  150. if (LVFurnitura.SelectedItems.Count > 0)
  151. {
  152. int num = LVFurnitura.SelectedIndices[0];
  153. TbxNameFur.Text = LstFurnitura[num].NameFur;
  154. TbxCountFurSklad.Text = LstFurnitura[num].CountFur;
  155. IdFur = LstFurnitura[num].IdFur;
  156. }
  157. else // нет выделенных элементов
  158. {
  159. TbxNameFur.Text = "";
  160. TbxCountFurSklad.Text = "";
  161. IdFur = "";
  162. }
  163. }
  164. private void BtnOK_Click(object sender, EventArgs e)
  165. {
  166. int NewCountFur = int.Parse(TbxCountFurSklad.Text) -
  167. int.Parse(TbxCountFur.Text) * int.Parse(TbxCountIzd.Text);
  168. SqlConnection con = new SqlConnection(Properties.Settings.Default.dbFabrikaOAAConnectionString);
  169. con.Open();
  170. string t1 = string.Format(@"update Furnitura
  171. set countfur = {0}
  172. where id_fur = {1}", NewCountFur, IdFur);
  173. SqlCommand query = new SqlCommand(t1, con);
  174. query.ExecuteNonQuery();
  175. con.Close();
  176. int WidthTkan = int.Parse(WidthTextBox.Text);
  177. int WidthIzd = int.Parse(DgvIzd.CurrentRow.Cells[2].Value.ToString());
  178. int LengthIzd = int.Parse(DgvIzd.CurrentRow.Cells[1].Value.ToString());
  179. int CountIzd = int.Parse(TbxCountIzd.Text);
  180. int NewLengthTkan = int.Parse(LengthTextBox.Text) -
  181. CutTkan(WidthTkan, WidthIzd, LengthIzd, CountIzd);
  182. int IdManager = 2;
  183. try
  184. {
  185. con.Open();
  186. string t = String.Format("insert into Zakaz (id_izd, id_zak, id_men, id_tkan, id_fur, CountFur, CountIzd) " +
  187. "values ({0}, {1}, {2}, {3}, {4}, {5}, {6})",
  188. LblIdIzd.Text, IdZakazchik, IdManager, IdTkan, IdFur, TbxCountFur.Text, TbxCountIzd.Text);
  189. SqlCommand query1 = new SqlCommand(t, con);
  190. query1.ExecuteNonQuery();
  191. con.Close();
  192. MessageBox.Show("Заказ сформирован.", "Внимание!",
  193. MessageBoxButtons.OK, MessageBoxIcon.Information);
  194. this.Close();
  195. }
  196. catch
  197. {
  198. MessageBox.Show("Ошибка добавления нового заказа.", "Внимание!",
  199. MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  200. }
  201. }
  202. private void FormAddZakaz_Load(object sender, EventArgs e)
  203. {
  204. // TODO: This line of code loads data into the 'db_22factoryDataSet1.Tkan' table. You can move, or remove it, as needed.
  205. this.tkanTableAdapter.Fill(this.db_22factoryDataSet1.Tkan);
  206. // TODO: This line of code loads data into the 'db_22factoryDataSet1.Izdelie' table. You can move, or remove it, as needed.
  207. this.izdelieTableAdapter.Fill(this.db_22factoryDataSet1.Izdelie);
  208. GetTkaniFromDB();
  209. FillLVTkani();
  210. GetFurnituraFromDB();
  211. FillLVFurnitura();
  212. }
  213. }
  214. }