public int addSale(Sale sale) { SSGetService getService = new SSGetService(); User user = app.getSession(); DataTable cartData = getService.getCartData(user); if (cartData.Rows.Count > 0) { using (SqlCommand command = new SqlCommand("INSERT INTO ss_sales(pids,user_id,customer_id,item_count,customer_name,location,total,discount,outstanding_balance,cleared_amount,part_payment,payment_method,bank_name)VALUES(@pids,@user_id,@customer_id,@item_count,@customer_name,@location,@total,@discount,@outstanding_balance,@cleared_amount,@part_payment,@payment_method,@bank_name)")) { command.Parameters.AddWithValue("@pids", sale.products); command.Parameters.AddWithValue("@user_id", user.id); command.Parameters.AddWithValue("@customer_id", sale.cid); command.Parameters.AddWithValue("@customer_name", sale.customer_name); command.Parameters.AddWithValue("@item_count", sale.item_count); command.Parameters.AddWithValue("@location", sale.product_location); command.Parameters.AddWithValue("@total", sale.total); command.Parameters.AddWithValue("@discount", sale.discount); command.Parameters.AddWithValue("@outstanding_balance", sale.outstanding_balance); command.Parameters.AddWithValue("@cleared_amount", sale.cleared_amount); command.Parameters.AddWithValue("@part_payment", sale.part_payment); command.Parameters.AddWithValue("@payment_method", sale.payment_method); command.Parameters.AddWithValue("@bank_name", sale.bank_name); int response = service.execute(command); if (response > 0) { using (SqlCommand salesDetailsCommand = new SqlCommand("INSERT INTO ss_sale_details(user_id, tid,pid,bid,product_name,quantity,unit_price,unit_discount,location,total,transaction_date)SELECT user_id, (select top 1 id from ss_sales l where l.user_id = " + user.id + " and l.customer_name = '" + sale.customer_name + "' order by l.transaction_date desc) tid, pid, bid, product_name, quantity, unit_price, unit_discount, location, total, created_date FROM ss_cart where user_id = " + user.id)) { if (service.execute(salesDetailsCommand) > 0) { using (SqlCommand cartResetCommand = new SqlCommand("DELETE ss_cart WHERE user_id=" + user.id)) { if (sale.payment_method == "Credit") { Customer customer = new Customer() { id = sale.cid, name = sale.customer_name }; this.addDebtor(customer, sale.total); } return(service.execute(cartResetCommand)); }; } else { return(-1); } } } else { return(-1); } } } else { return(-404); } }