public int addVoucherCategory(VoucherCategory category) { SSGetService getService = new SSGetService(); SSUpdateService updateService = new SSUpdateService(); VoucherCategory existingCategory = getService.getVoucherByName(category.name); if (existingCategory.name == null) { using (SqlCommand command = new SqlCommand("INSERT INTO ss_voucher_categories(name)VALUES(@name)")) { command.Parameters.AddWithValue("@name", category.name); int response = service.execute(command); if (response > 0) { this.addLog(new Log() { type = "Add Voucher Category", statement = command.CommandText, description = "Created voucher category " + existingCategory.name, }); } return(response); } } else { return(-101); } }
private void addbutton_Click(object sender, EventArgs e) { if (voucherCategoryField.Text != "" && voucherCategoryField.Text != "Category") { VoucherCategory category = new VoucherCategory() { name = voucherCategoryField.Text }; addbutton.Enabled = false; int response = addService.addVoucherCategory(category); addbutton.Enabled = true; if (response > 0) { voucherCategoryField.Text = "Category"; app.notifyTo(statusLabel, voucherCategoryField.Text + " Added Successfully", "success"); } else { if (response == -101) { app.notifyTo(statusLabel, "Oops! Voucher category " + voucherCategoryField.Text + " already exist", "warning"); } else { app.notifyTo(statusLabel, "Unable to add the new voucher category " + voucherCategoryField.Text, "warning"); } } } else { app.notifyTo(statusLabel, "Enter the voucher category!", "warning"); } }
public VoucherCategory getVoucherByName(string name) { VoucherCategory category = new VoucherCategory(); try { DataTable data = service.get("SELECT top 1 * FROM ss_voucher_categories WHERE name='" + name + "' order by created_date desc"); if (data.Rows.Count > 0) { DataRow row = data.Rows[0]; category = new VoucherCategory() { id = row.Field <Int32>("id"), name = row.Field <string>("name"), created_date = row.Field <DateTime>("created_date"), }; } return(category); } catch (Exception ex) { Console.WriteLine(ex.Message); return(category); } }
public int MakeVoucher(Voucher voucher) { SSGetService getService = new SSGetService(); VoucherCategory category = getService.getVoucherByName(voucher.type); if (category.name != null) { DataTable data = service.get("select * from ss_account"); if (data.Rows.Count > 0) { DataRow row = data.Rows[0]; int balance = row.Field <int>("balance"); int previusBalance = balance; if (balance >= voucher.amount) { balance = balance - voucher.amount; using (SqlCommand command = new SqlCommand("update ss_account set balance=@balance, previous_balance=@previous_balance")) { command.Parameters.AddWithValue("@balance", balance); command.Parameters.AddWithValue("@previous_balance", previusBalance); if (service.execute(command) > 0) { User user = app.getSession(); using (SqlCommand addCommand = new SqlCommand("INSERT INTO ss_vouchers(user_id,amount,amount_in_words,type,description)VALUES(@user_id,@amount,@amount_in_words,@type,@description)")) { addCommand.Parameters.AddWithValue("@user_id", user.id); addCommand.Parameters.AddWithValue("@amount", voucher.amount); addCommand.Parameters.AddWithValue("@amount_in_words", app.toWordsOf(voucher.amount)); addCommand.Parameters.AddWithValue("@type", voucher.type); addCommand.Parameters.AddWithValue("@description", voucher.description); return(service.execute(addCommand)); } } else { return(-1); } } } else { return(-402); } } else { return(-403); } } else { return(-404); } }