private void button2_Click(object sender, EventArgs e) { try { comboBox1.Enabled = false; RepresentitiveBillDetail newRepresentiveBd = new RepresentitiveBillDetail(); Representitive NameRep = (Representitive)comboBox1.SelectedItem; Representitive rep = context.Representitives.FirstOrDefault(r => r.Name == NameRep.Name); RepresentitiveBill newRepresentitveBill = new RepresentitiveBill() { Representitive_NationalID = rep.NationalID }; context.RepresentitiveBills.Add(newRepresentitveBill); context.SaveChanges(); RepresentitiveBill RepresentitveBillJustAdded = context.RepresentitiveBills.OrderByDescending(r => r.ID).FirstOrDefault(); foreach (var item in supproductlocalList) { newRepresentiveBd.GivenAmount = item.GivenAmount; newRepresentiveBd.Date = item.Date; newRepresentiveBd.ProductObj_ID = item.ProductObj_ID; newRepresentiveBd.GivenAmountPrice = item.GivenAmountPrice; newRepresentiveBd.RepresentitiveBillObj_ID = RepresentitveBillJustAdded.ID; context.RepresentitiveBillDetails.Add(newRepresentiveBd); context.SaveChanges(); Product pro = context.Products.FirstOrDefault(p => p.ID == item.ProductObj_ID); if (pro.AmountInStock > item.GivenAmount) { pro.AmountInStock = pro.AmountInStock - item.GivenAmount; context.SaveChanges(); } else { MessageBox.Show("الكمية المطلوبة أكبر من الكمية الموجوده فى المخزن"); } } supproductlocalList.Clear(); } catch { } }
private void button2_Click(object sender, EventArgs e) { Supplyer sup = context.Supplyers.FirstOrDefault(s => s.Name == SupplyerCombo.Text); SupplyerBill suppleyBill = new SupplyerBill() { supplyer_ID = sup.ID, PaidPrice = 0, DeptPrice = 0 }; context.SupplyerBills.Add(suppleyBill); context.SaveChanges(); SupplyerBill supBill = context.SupplyerBills.OrderByDescending(s => s.ID).FirstOrDefault(); SupplyerBillDetail supplyerBDetails; foreach (var item in supperList) { supplyerBDetails = new SupplyerBillDetail(); supplyerBDetails.Amount = item.Amount; supplyerBDetails.Date = item.Date; supplyerBDetails.Paid = 0; supplyerBDetails.Debt = 0; supplyerBDetails.AmountPrice = item.AmountPrice; supplyerBDetails.productOpj_ID = item.productOpj_ID; supplyerBDetails.AdeyMortagaType = item.AdeyMortagaType; supplyerBDetails.SupplyerBillobj_ID = supBill.supplyer_ID; context.SupplyerBillDetails.Add(supplyerBDetails); Product UpdateProduct = context.Products.FirstOrDefault(p => p.ID == supplyerBDetails.productOpj_ID); if (supplyerBDetails.AdeyMortagaType == "عادى") { UpdateProduct.AmountInStock = UpdateProduct.AmountInStock + supplyerBDetails.Amount; } if (supplyerBDetails.AdeyMortagaType == "مرتجع") { UpdateProduct.AmountInStock = UpdateProduct.AmountInStock - supplyerBDetails.Amount; } context.SaveChanges(); } }
private void button13_Click(object sender, EventArgs e) { try { Category newCat = new Category(); foreach (var item in supproductlocalList) { newCat.Name = item.CatName; context.Categories.Add(newCat); context.SaveChanges(); } MessageBox.Show("لقد تم أضافة الأصناف"); } catch { } }
private void btn_save_supllier_Click(object sender, EventArgs e) { try { Supplyer newSuppyer = new Supplyer(); foreach (var item in supproductlocalList) { newSuppyer.Name = item.Name; newSuppyer.Phone = item.Phone; newSuppyer.Address = item.Address; storeContext.Supplyers.Add(newSuppyer); storeContext.SaveChanges(); } } catch (Exception) { MessageBox.Show("هناك عميل موجود بالفعل "); } }
private void btn_save_Client_Click(object sender, EventArgs e) { try { Client newClient = new Client(); foreach (var item in supproductlocalList) { newClient.NationalID = item.NationalId; newClient.Name = item.Name; newClient.Phone = item.Phone; storeContext.Clients.Add(newClient); storeContext.SaveChanges(); } MessageBox.Show("لقد تم أضافة العميل"); } catch (Exception) { MessageBox.Show("هناك عميل موجود بالفعل "); } }
private void btn_save_reps_Click(object sender, EventArgs e) { try { Representitive newRepres = new Representitive(); foreach (var item in supproductlocalList) { newRepres.Name = item.Name; newRepres.NationalID = item.NationalId; newRepres.Phone = item.Phone; newRepres.Salary = item.salary; newRepres.TargetSale = item.TargetSale; storeContext.Representitives.Add(newRepres); storeContext.SaveChanges(); } } catch (Exception) { MessageBox.Show("هناك مندوب موجود بالفعل "); } }
private void button2_Click(object sender, EventArgs e) { //compoOfClients.Enabled = false; //RepresentitiveBillDetail newRepresentiveBd = new RepresentitiveBillDetail(); //ClientBill CLientBillJustAdded = context.ClientBills.OrderByDescending(r => r.ID).FirstOrDefault(); //ClientBillDetail clientBDetails = new ClientBillDetail(); //double totalPrice = 0; //string trypeClientBill = null; //foreach (var item in supperList) //{ // clientBDetails.Amount = item.Amount; // clientBDetails.Date = item.Date; // clientBDetails.Type = item.Type; // trypeClientBill = item.Type; // clientBDetails.AmountPrice = item.AmountPrice; // clientBDetails.Paid = 0; // clientBDetails.Debt = 0; // clientBDetails.ClientBillObj_ID = CLientBillJustAdded.ID; // clientBDetails.ProductOpj_ID = item.ProductOpj_ID; // clientBDetails.AdeyMortagaType = item.AdeyMortagaType; // context.ClientBillDetails.Add(clientBDetails); // totalPrice = totalPrice + clientBDetails.AmountPrice; // ClientBillDetail ClientBillJustAdded = context.ClientBillDetails.OrderByDescending(cB => cB.ID).FirstOrDefault(); // x = (Representitive)rpresentiveCombo.SelectedItem; // var prdN = productComboFromRepresentive.Text.ToString(); // var prd = context.Products.Where(n => n.Name == prdN).FirstOrDefault(); // //amount of representitveBillDetails // var y = context.RepresentitiveBillDetails // .Where(p => p.RepresentitiveBill.Representitive_NationalID == x.NationalID && p.ProductObj_ID == prd.ID).FirstOrDefault(); // clientBDetails.RepresentitiveBillDetailsObj_ID = y.ID; // ClientBillDetail cUpdated = context.ClientBillDetails.OrderByDescending(c => c.ID).FirstOrDefault(); // // cUpdated.RepresentitiveBillDetail.GivenAmount = cUpdated.RepresentitiveBillDetail.GivenAmount - item.Amount; // //context.SaveChanges(); // RepresentitiveBillDetail rebDeta = context.RepresentitiveBillDetails.FirstOrDefault(r => r.ID == cUpdated.RepresentitiveBillDetailsObj_ID); // if (rebDeta.GivenAmount > 0 && MortagaAdeyCombo.SelectedItem.ToString() == "عادى") // { // rebDeta.GivenAmount = rebDeta.GivenAmount + item.Amount; // } // else // { // prd.AmountInStock = prd.AmountInStock + item.Amount; // } // context.SaveChanges(); //} //Client ClientObj = (Client)compoOfClients.SelectedItem; ////Representitive rep = context.Clients.FirstOrDefault(r => r.Name == NameRep.Name); //double paid = int.Parse(PaidNumericUpdown.Value.ToString()); //double deptPrice = totalPrice - paid; //ClientBill NewClientBill = new ClientBill() //{ // ClientObj_NationalID = ClientObj.NationalID, // TotalPrice = totalPrice, // type = trypeClientBill, // PaidPrice = paid, // DeptPrice = deptPrice //}; ////lapelTotalPrice.Text = totalPrice.ToString(); ////lapelTotalPrice.Visible = true; //if (flag == true) //{ // context.ClientBills.Add(NewClientBill); // context.SaveChanges(); // flag = false; //} compoOfClients.Enabled = false; RepresentitiveBillDetail newRepresentiveBd = new RepresentitiveBillDetail(); Client ClientSelected = context.Clients.FirstOrDefault(c => c.Name == compoOfClients.Text); ClientBill clientBilld = new ClientBill() { ClientObj_NationalID = ClientSelected.NationalID, type = TypeBillCombo.Text, }; context.ClientBills.Add(clientBilld); context.SaveChanges(); ClientBill CLientBillJustAdded = context.ClientBills.OrderByDescending(r => r.ID).FirstOrDefault(); ClientBillDetail clientBDetails = new ClientBillDetail(); double totalPrice = 0; string trypeClientBill = null; foreach (var item in supperList) { clientBDetails.Amount = item.Amount; clientBDetails.Date = item.Date; clientBDetails.Type = item.Type; trypeClientBill = item.Type; clientBDetails.AmountPrice = item.AmountPrice; clientBDetails.Paid = 0; clientBDetails.Debt = 0; clientBDetails.ClientBillObj_ID = CLientBillJustAdded.ID; clientBDetails.ProductOpj_ID = item.ProductOpj_ID; clientBDetails.AdeyMortagaType = item.AdeyMortagaType; context.ClientBillDetails.Add(clientBDetails); totalPrice = totalPrice + clientBDetails.AmountPrice; ClientBillDetail ClientBillJustAdded = context.ClientBillDetails.OrderByDescending(cB => cB.ID).FirstOrDefault(); x = (Representitive)rpresentiveCombo.SelectedItem; var prdN = productComboFromRepresentive.Text.ToString(); var prd = context.Products.Where(n => n.Name == prdN).FirstOrDefault(); //amount of representitveBillDetails var y = context.RepresentitiveBillDetails .Where(p => p.RepresentitiveBill.Representitive_NationalID == x.NationalID && p.ProductObj_ID == prd.ID).FirstOrDefault(); clientBDetails.RepresentitiveBillDetailsObj_ID = y.ID; ClientBillDetail cUpdated = context.ClientBillDetails.OrderByDescending(c => c.ID).FirstOrDefault(); // cUpdated.RepresentitiveBillDetail.GivenAmount = cUpdated.RepresentitiveBillDetail.GivenAmount - item.Amount; //context.SaveChanges(); RepresentitiveBillDetail rebDeta = context.RepresentitiveBillDetails.FirstOrDefault(r => r.ID == cUpdated.RepresentitiveBillDetailsObj_ID); if (rebDeta.GivenAmount > 0 && MortagaAdeyCombo.SelectedItem.ToString() == "عادى") { rebDeta.GivenAmount = rebDeta.GivenAmount - item.Amount; } else { prd.AmountInStock = prd.AmountInStock + item.Amount; } context.SaveChanges(); } Client ClientObj = (Client)compoOfClients.SelectedItem; //Representitive rep = context.Clients.FirstOrDefault(r => r.Name == NameRep.Name); double paid = int.Parse(PaidNumericUpdown.Value.ToString()); double deptPrice = totalPrice - paid; ClientBill NewClientBill = new ClientBill() { ClientObj_NationalID = ClientObj.NationalID, TotalPrice = totalPrice, type = trypeClientBill, PaidPrice = paid, DeptPrice = deptPrice }; //lapelTotalPrice.Text = totalPrice.ToString(); //lapelTotalPrice.Visible = true; if (flag == true) { context.ClientBills.Add(NewClientBill); context.SaveChanges(); flag = false; } }
private void button16_Click(object sender, EventArgs e) { try { for (int i = 0; i < listView1.Items.Count; i++) { //get values entered by the user string ProdName = listView1.Items[i].SubItems[0].Text; //check if the product already exists if (context.Products.Where(c => c.Name.Contains(ProdName)).FirstOrDefault() != null) { if (MessageBox.Show("من الممكن ان يكون المنتج موجودا بالفعل \n هل تريد الإضافة على أي حال؟", "تأكيد", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { // user clicked yes string catName = listView1.Items[i].SubItems[1].Text; int? catID = context.Categories.FirstOrDefault(c => c.Name == catName).ID; int Amount = int.Parse(listView1.Items[i].SubItems[2].Text); // int.TryParse(listView1.Items[i].SubItems[2].Text, out int Amount); int wholeSalePrice = int.Parse(listView1.Items[i].SubItems[4].Text); string measuringUnit = listView1.Items[i].SubItems[3].Text; //int.TryParse(listView1.Items[i].SubItems[4].Text, out int wholeSalePrice); int retailPrice = int.Parse(listView1.Items[i].SubItems[5].Text); //int.TryParse(listView1.Items[i].SubItems[5].Text, out int retailPrice); //Save changes to the database Product NewProduct = new Product { Name = ProdName, CategoryObj_ID = catID, AmountInStock = Amount, WholesalePrice = wholeSalePrice, RetailPrice = retailPrice }; context.Products.Add(NewProduct); context.SaveChanges(); //add the measuring unit int lastAddedProductID = context.Products.OrderByDescending(p => p.ID).FirstOrDefault().ID; MeasuringUnit meauringUnit = new MeasuringUnit { UnitName = measuringUnit, PID = lastAddedProductID }; context.MeasuringUnits.Add(meauringUnit); context.SaveChanges(); } } else { string catName = listView1.Items[i].SubItems[1].Text; int? catID = context.Categories.FirstOrDefault(c => c.Name == catName).ID; int Amount = int.Parse(listView1.Items[i].SubItems[2].Text); // int.TryParse(listView1.Items[i].SubItems[2].Text, out int Amount); string measuringUnit = listView1.Items[i].SubItems[3].Text; int wholeSalePrice = int.Parse(listView1.Items[i].SubItems[4].Text); // int.TryParse(listView1.Items[i].SubItems[4].Text, out int wholeSalePrice); // int.TryParse(listView1.Items[i].SubItems[5].Text, out int retailPrice); int retailPrice = int.Parse(listView1.Items[i].SubItems[5].Text); //Save product changes to the database Product NewProduct = new Product { Name = ProdName, CategoryObj_ID = catID, AmountInStock = Amount, WholesalePrice = wholeSalePrice, RetailPrice = retailPrice }; context.Products.Add(NewProduct); context.SaveChanges(); //add the measuring unit and save it to DB int lastAddedProductID = context.Products.OrderByDescending(p => p.ID).FirstOrDefault().ID; MeasuringUnit meauringUnit = new MeasuringUnit { UnitName = measuringUnit, PID = lastAddedProductID }; context.MeasuringUnits.Add(meauringUnit); context.SaveChanges(); } } } catch (Exception) { MessageBox.Show("حدث خطأ أثناء حفظ البيانات، من فضلك حاول مرة أخرى"); } }