public void setProductDelegateCallbackFn(Product product) { setProductInfo(product); }
private void toolStripButton1_Click(object sender, EventArgs e) { if(txtId.Text == string.Empty || txtName.Text == string.Empty || txtQC.Text == string.Empty || txtSalePriceStock.Text == string.Empty) { MessageBox.Show("Chưa nhập đủ dữ liệu sản phẩm"); return; } using (var db = new SMEntities()) { // Bind DataSource Product p = new Product(); p.prod_id = txtId.Text.ToString(); p.prod_name = txtName.Text.ToString(); p.quantity_control = int.Parse(txtQC.Text.ToString()); p.sale_price_by_stock = decimal.Parse(txtSalePriceStock.Text.ToString()); p.sale_price_by_unit = decimal.Parse(txtSalePriceUnit.Text.ToString()); // Get default values p.quantity_by_stock = 0; p.quantity_by_unit = 0; p.date_added = DateTime.Now; p.date_modified = p.date_added; // Check as if products has been existed var query = db.Products.Where(t => t.prod_id.Equals(p.prod_id)); if (!query.Any())// Not exist { db.Products.Add(p); db.SaveChanges(); clearEntries(); } else { // Accept change Name and sale price var result = MessageBox.Show(this, "Sản phẩm đã có trong dữ liệu.\n Bạn có muốn lưu tên và giá bán hiện tại?", "Lưu sản phẩm hàng", MessageBoxButtons.YesNo); if (result == DialogResult.Yes) { Product updatePro = query.First(); updatePro.prod_name = p.prod_name; updatePro.sale_price_by_stock = p.sale_price_by_stock; updatePro.sale_price_by_unit = p.sale_price_by_unit; db.SaveChanges(); clearEntries(); } } } }
private void toolStripImport_Click(object sender, EventArgs e) { OpenFileDialog dlg = new OpenFileDialog(); if (dlg.ShowDialog(this) == DialogResult.OK) { object missing = Type.Missing; Excel.Range range; Excel.Worksheet sheet = null; Excel.Workbook wb = null; Excel.Application excelApp = new Excel.Application(); wb = excelApp.Workbooks.Open(dlg.FileName, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing); sheet = (Excel.Worksheet)wb.Worksheets[1]; range = sheet.UsedRange; if (range != null) { using (var db = new SMEntities()) { // Bind DataSource int nRows = range.Rows.Count; int num; decimal d; bool parsed; for (int iRow = 2; iRow <= nRows; iRow++) { Product p = new Product(); // Get ID range = (Excel.Range)sheet.Cells[iRow, 2]; p.prod_id = range.Text; // Get Name range = (Excel.Range)sheet.Cells[iRow, 3]; p.prod_name = range.Text; // Get Quality_control range = (Excel.Range)sheet.Cells[iRow, 4]; parsed = int.TryParse(range.Text, out num); if (parsed) { p.quantity_control = num; } else p.quantity_control = 1; // Get Sale_price_by_stock range = (Excel.Range)sheet.Cells[iRow, 5]; parsed = decimal.TryParse(range.Text, out d); if (parsed) { p.sale_price_by_stock = d; } else p.sale_price_by_stock = 0; // Set sale_price_by_unit range = (Excel.Range)sheet.Cells[iRow, 6]; parsed = decimal.TryParse(range.Text, out d); if (parsed) { p.sale_price_by_unit = d; } else p.sale_price_by_unit = 0; // Check as if product has been existed var prod = db.Products.Where(t => t.prod_id.Equals(p.prod_id)); if (!prod.Any())//Not exist { db.Products.Add(p); db.SaveChanges(); } } } } wb.Close(); excelApp.Quit(); Marshal.ReleaseComObject(wb); Marshal.ReleaseComObject(sheet); } }
private void setProductInfo(Product product) { txtId.Text = product.prod_id.ToString(); txtName.Text = product.prod_name.ToString(); txtQC.Text = product.quantity_control.ToString(); txtSalePriceStock.Text = product.sale_price_by_stock.ToString(); txtSalePriceUnit.Text = product.sale_price_by_unit.ToString(); using (var db = new SMEntities()) { Product prod = db.Products.Where(p => p.prod_id.Equals(product.prod_id)) .Include(p => p.Stocks) .First(); prod.Stocks.OrderByDescending(s => s.quantity_by_stock); dataGridView.DataSource = prod.Stocks.ToList(); decimal total = 0; foreach (var stock in prod.Stocks) { total += stock.base_price_by_stock * stock.quantity_by_stock + stock.base_price_by_unit * stock.quantity_by_unit; } lblTotal.Text = total.ToString("#,##0.000"); } }