private void OnRectangleClicked(object sender, ProductClickedEventArgs e) { _POSWindowViewModel.TempProduct = e.Product; //puKeypadInput.IsOpen = true; //Keyboard.Focus(tbKeypadInput.tbAmount); AmountInputView am = new AmountInputView(); am.ShowDialog(); }
private void methodSaleCommand() { //ghi vào CSDL using (MySqlConnection conn = new MySqlConnection(ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString)) { conn.Open(); using (MySqlTransaction trans = conn.BeginTransaction()) { try { int lastInsertID; int temp_id; //tạo sale transaction using (MySqlCommand cmd = new MySqlCommand(@"INSERT INTO saletransaction (customer_id, createddate, store_id) VALUES (@customer_id, NOW(), @store_id)", conn, trans)) { cmd.Parameters.AddWithValue("@customer_id", 1); cmd.Parameters.AddWithValue("@store_id", 1); cmd.ExecuteNonQuery(); cmd.Parameters.Clear(); cmd.CommandText = "select last_insert_id()"; lastInsertID = Convert.ToInt32(cmd.ExecuteScalar()); cmd.Parameters.Clear(); } logger.Trace("Inserted saletransaction_id: {0}", lastInsertID); var copy = new ObservableCollection<Sale_Item>(_sale_items); foreach (Sale_Item si in copy) { if (si.Amount > 0) { //===giảm hàng trong inventory //1. lấy id của inventory_product using (MySqlCommand cmd = new MySqlCommand(@"SELECT _id FROM inventory_product WHERE inventory_id = @inventory_id AND product_id = @product_id", conn, trans)) { //lay id inventory_product cmd.Parameters.AddWithValue("@inventory_id", temp_inventory_id); cmd.Parameters.AddWithValue("@product_id", si.Product.ID); temp_id = Convert.ToInt32(cmd.ExecuteScalar()); } logger.Trace("inventory_product_id: {0}", temp_id); //update inventory_product using (MySqlCommand cmd = new MySqlCommand(@"UPDATE inventory_product SET `amount` = `amount` - @sale_amount WHERE _id = @inventory_product_id", conn, trans)) { cmd.Parameters.AddWithValue("@sale_amount", si.Amount); cmd.Parameters.AddWithValue("@inventory_product_id", temp_id); cmd.ExecuteNonQuery(); } //insert inventory_productitem using (MySqlCommand cmd = new MySqlCommand(@"INSERT INTO inventory_productitem (inventory_product_id, amount, item_type, user_id, createddate, description) VALUES (@inventory_product_id, @amount, 'B', @user_id, @createddate, 'ban hang')", conn, trans)) { DateTime date = DateTime.Now; cmd.Parameters.AddWithValue("@inventory_product_id", temp_id); cmd.Parameters.AddWithValue("@amount", si.Amount); cmd.Parameters.AddWithValue("@user_id", temp_user.ID); cmd.Parameters.AddWithValue("@createddate", date.ToString("yyyy-MM-dd HH:mm:ss")); cmd.ExecuteNonQuery(); } //===thêm vào saleitem using (MySqlCommand cmd = new MySqlCommand(@"INSERT INTO saleitem (product_id, saletransaction_id, price, amount, description) VALUES (@product_id, @saletransaction_id, @price, @amount, @description)", conn, trans)) { cmd.Parameters.AddWithValue("@product_id", si.Product.ID); cmd.Parameters.AddWithValue("@saletransaction_id", lastInsertID); cmd.Parameters.AddWithValue("@price", si.Price); cmd.Parameters.AddWithValue("@amount", si.Amount); cmd.Parameters.AddWithValue("@description", "Regular sale"); cmd.ExecuteNonQuery(); } } } //ghi điểm vào cho khách if (temp_customer != null) { using (MySqlCommand cmd = new MySqlCommand(@"INSERT INTO customer_point (customer_id, sale_point, createddate, expiredate, description) VALUES (@customer_id, @sale_point, @createddate, @expiredate, '')", conn, trans)) { DateTime date = DateTime.Now; //tính điểm dựa trên số tiền thanh toán cmd.Parameters.AddWithValue("@customer_id", temp_customer.ID); cmd.Parameters.AddWithValue("@sale_point", bonusPointCalcuate(_finalAmount)); cmd.Parameters.AddWithValue("@createddate", date.ToString("yyyy-MM-dd HH:mm:ss")); cmd.Parameters.AddWithValue("@expiredate", date.AddDays(90).ToString("yyyy-MM-dd HH:mm:ss")); //điểm có giá trị trong 90 ngày cmd.Parameters.AddWithValue("@description", "Regular sale"); cmd.ExecuteNonQuery(); cmd.Parameters.Clear(); } } trans.Commit(); } catch (Exception ex) { trans.Rollback(); logger.Error(ex.StackTrace); } } } //in phiếu PrintDialog pd = new PrintDialog(); FlowDocument doc = CreateFlowDocument(); doc.MaxPageWidth = 270.0; IDocumentPaginatorSource idpSource = doc; PrinterSettings ps = new PrinterSettings(); //Font font = new Font("Courier New", 15); //PaperSize psize = new PaperSize("Custom", 100, 200); //ps.DefaultPageSettings.PaperSize = psize; pd.PrintDocument(idpSource.DocumentPaginator, "Receipt"); AmountInputView am = new AmountInputView(); am.ShowDialog(); clearAll(); }