public int addPayment(Payment payment) { SSGetService getService = new SSGetService(); SSUpdateService updateService = new SSUpdateService(); User user = app.getSession(); Customer customer = getService.getCustomerByName(payment.customer_name); if (customer.name != null) { if (payment.transaction_type == "Payment") { Debtor debtor = getService.getDebtorById(customer.id); if (debtor.name != null) { int clearedAmount; int outstanding; clearedAmount = outstanding = 0; //perform the caculation if (payment.transaction_amount >= debtor.amount) { clearedAmount = (debtor.amount); outstanding = (debtor.amount - payment.transaction_amount); } else { clearedAmount = payment.transaction_amount; outstanding = (debtor.amount - payment.transaction_amount); } //add payment entry using (SqlCommand command = new SqlCommand("INSERT INTO ss_payments(cid,user_id,customer_name,transaction_amount,cleared_amount,amount_in_words,transaction_type,payment_mode,description)VALUES(@cid,@user_id,@customer_name,@transaction_amount,@cleared_amount,@amount_in_words,@transaction_type,@payment_mode,@description)")) { command.Parameters.AddWithValue("@cid", customer.id); command.Parameters.AddWithValue("@user_id", user.id); command.Parameters.AddWithValue("@customer_name", customer.name); command.Parameters.AddWithValue("@transaction_amount", payment.transaction_amount); command.Parameters.AddWithValue("@cleared_amount", clearedAmount); command.Parameters.AddWithValue("@amount_in_words", app.toWordsOf(payment.transaction_amount)); command.Parameters.AddWithValue("@transaction_type", payment.transaction_type); command.Parameters.AddWithValue("@payment_mode", payment.payment_mode); command.Parameters.AddWithValue("@description", payment.description); if (service.execute(command) > 0) { if (outstanding < 0) { //update customer balance if any if (customer.account_balance > 0 || customer.account_balance < 0) { customer.account_balance += Math.Abs(outstanding); } else { customer.account_balance = Math.Abs(outstanding); } updateService.updateCustomerBalance(customer); } //update debtor balance if (clearedAmount >= debtor.amount) { debtor.amount = 0; } else { debtor.amount = outstanding; } 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; balance = balance + payment.transaction_amount; using (SqlCommand updateCommand = new SqlCommand("update ss_account set balance=@balance, previous_balance=@previous_balance")) { updateCommand.Parameters.AddWithValue("@balance", balance); updateCommand.Parameters.AddWithValue("@previous_balance", previusBalance); if (service.execute(updateCommand) > 0) { return(updateService.updateDebtorBalance(debtor)); } else { return(-1); } } } else { return(-1); } } else { return(-1); } } } else { if (payment.description == "Purchasing Payment") { using (SqlCommand command = new SqlCommand("INSERT INTO ss_payments(cid,user_id,customer_name,transaction_amount,cleared_amount,amount_in_words,transaction_type,payment_mode,description)VALUES(@cid,@user_id,@customer_name,@transaction_amount,@cleared_amount,@amount_in_words,@transaction_type,@payment_mode,@description)")) { command.Parameters.AddWithValue("@cid", customer.id); command.Parameters.AddWithValue("@user_id", user.id); command.Parameters.AddWithValue("@customer_name", customer.name); command.Parameters.AddWithValue("@transaction_amount", payment.transaction_amount); command.Parameters.AddWithValue("@cleared_amount", payment.cleared_amount); command.Parameters.AddWithValue("@amount_in_words", app.toWordsOf(payment.transaction_amount)); command.Parameters.AddWithValue("@transaction_type", payment.transaction_type); command.Parameters.AddWithValue("@payment_mode", payment.payment_mode); command.Parameters.AddWithValue("@description", payment.description); if (service.execute(command) > 0) { 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; balance = balance + payment.transaction_amount; using (SqlCommand updateCommand = new SqlCommand("update ss_account set balance=@balance, previous_balance=@previous_balance")) { updateCommand.Parameters.AddWithValue("@balance", balance); updateCommand.Parameters.AddWithValue("@previous_balance", previusBalance); return(service.execute(updateCommand)); } } else { return(-1); } } else { return(-1); } } } else { return(-202); } } } else { int clearedAmount = 0; int outstanding = 0; if (payment.payment_mode == "Credit") { Debtor debtor = getService.getDebtorByIdEvenIfZero(customer.id); if (debtor.name != null) { debtor.amount += payment.transaction_amount; int res = updateService.updateDebtorBalance(debtor); } else { this.addDebtor(customer, payment.transaction_amount); } } else { Debtor debtor = getService.getDebtorByIdEvenIfZero(customer.id); if (debtor.name != null) { if (debtor.amount >= payment.transaction_amount) { debtor.amount = (debtor.amount - payment.transaction_amount); clearedAmount = payment.transaction_amount; updateService.updateDebtorBalance(debtor); } else { clearedAmount = debtor.amount; outstanding = (payment.transaction_amount - debtor.amount); debtor.amount = 0; updateService.updateDebtorBalance(debtor); if (outstanding > 0) { customer.account_balance += outstanding; updateService.updateCustomerBalance(customer); } } } else { customer.account_balance += payment.transaction_amount; updateService.updateCustomerBalance(customer); } } using (SqlCommand command = new SqlCommand("INSERT INTO ss_adjustments(cid,customer_name,transaction_amount,cleared_amount,transaction_type,payment_mode,description)VALUES(@cid,@customer_name,@transaction_amount,@cleared_amount,@transaction_type,@payment_mode,@description)")) { command.Parameters.AddWithValue("@cid", customer.id); command.Parameters.AddWithValue("@customer_name", customer.name); command.Parameters.AddWithValue("@transaction_amount", payment.transaction_amount); command.Parameters.AddWithValue("@cleared_amount", clearedAmount); command.Parameters.AddWithValue("@transaction_type", payment.transaction_type); command.Parameters.AddWithValue("@payment_mode", payment.payment_mode); command.Parameters.AddWithValue("@description", payment.description); 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 (payment.payment_mode == "Credit") { balance = balance + payment.transaction_amount; } using (SqlCommand updateCommand = new SqlCommand("update ss_account set balance=@balance, previous_balance=@previous_balance, last_modified_date=getdate()")) { updateCommand.Parameters.AddWithValue("@balance", balance); updateCommand.Parameters.AddWithValue("@previous_balance", previusBalance); if (service.execute(updateCommand) > 0) { return(service.execute(command)); } else { return(-1); } } } else { return(-1); } } } } else { return(-404); } }