Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package woto.business; import java.util.Iterator; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import java.sql.*; import woto.core.BusinessException; import woto.core.DBConnection; import woto.core.ParameterList; /** * * @author Ramkumar */ public class PaymentService extends BaseService { public JSONArray deletePayment(JSONObject methodParam) throws ClassNotFoundException { JSONArray joArray = new JSONArray(); DBConnection db = new DBConnection(true); try { int pay_id = Integer.parseInt("" + methodParam.get("pay_id")); //Get Payment detail for pay id to be deleted ParameterList paramList = new ParameterList(); paramList.add("pay_id", "" + pay_id); JSONObject delPayment = (JSONObject) db .Execute("payment.logic", "GetPaymentById", paramList.toJSONArray()).iterator().next(); int sch_id = Integer.parseInt("" + delPayment.get("sch_id")); int s_id = Integer.parseInt("" + delPayment.get("s_id")); paramList = new ParameterList(); paramList.add("s_id", "" + s_id); paramList.add("sch_id", "" + sch_id); JSONArray paymentsFromSchId1 = db.Execute("payment.logic", "GetPaymentAfterSchedule", paramList.toJSONArray()); //Delete the payment paramList = new ParameterList(); paramList.add("pay_id", "" + pay_id); db.Execute("payment.logic", "deletePayment", paramList.toJSONArray()); //Get the payment to be reshuffed. Reschuffle all schedule from deleted scheddule paramList = new ParameterList(); paramList.add("s_id", "" + s_id); paramList.add("sch_id", "" + sch_id); JSONArray paymentsFromSchId = db.Execute("payment.logic", "GetPaymentAfterSchedule", paramList.toJSONArray()); if (paymentsFromSchId.size() > 0) { //Delete all schedule from deleted pay id schedule db.Execute("payment.logic", "deletePaymentFromSchId", paramList.toJSONArray()); paramList = new ParameterList(); paramList.add("s_id", "" + s_id); paramList.add("sch_id", "" + sch_id); JSONArray paymentsFromSchId5 = db.Execute("payment.logic", "GetPaymentAfterSchedule", paramList.toJSONArray()); //REpay the amount without the deleted payid Iterator<JSONObject> iterator = paymentsFromSchId.iterator(); while (iterator.hasNext()) { JSONObject paymentFromSchId = (JSONObject) iterator.next(); Float f = Float.parseFloat("" + paymentFromSchId.get("amount")); int ramount = f.intValue(); JSONObject payment = new JSONObject(); payment.put("mem_id", paymentFromSchId.get("mem_id")); payment.put("s_id", paymentFromSchId.get("s_id")); payment.put("pay_date", paymentFromSchId.get("pay_date")); payment.put("amount", "" + ramount); payment.put("collector_id", paymentFromSchId.get("collector_id")); payment.put("created_by", paymentFromSchId.get("created_by")); joArray.addAll(payAmountAtEnd(db, payment)); } } db.commit(); } catch (Exception e) { db.rollback(); throw e; } return joArray; } public JSONArray payAmount(JSONObject methodParam) throws ClassNotFoundException { JSONArray joArray = new JSONArray(); DBConnection db = new DBConnection(true); try { int mem_id = Integer.parseInt("" + methodParam.get("mem_id")); int s_id = Integer.parseInt("" + methodParam.get("s_id")); long amount = Long.parseLong("" + methodParam.get("amount")); String pay_date = (String) methodParam.get("pay_date"); int collector_id = Integer.parseInt("" + methodParam.get("collector_id")); int created_by = Integer.parseInt("" + getUserId()); //Insert a new Bill for student ParameterList paramList = new ParameterList(); paramList.add("mem_id", "" + mem_id); paramList.add("s_id", "" + s_id); paramList.add("pay_date", pay_date); JSONArray paymentsFromDate = db.Execute("payment.logic", "getMemberPaymentFromDate", paramList.toJSONArray()); if (paymentsFromDate.size() == 0) { joArray.addAll(payAmountAtEnd(db, methodParam)); } else { //Get the list of amount pay after the min sche"select gm_id, substring(cast(t_date as char), 1, 10) p_date, sum(amount) amount, user_id from ( (select "+gmId+" gm_id,'"+ChittyUtil.ToDBDate(payDate)+"' t_date,"+amount+" amount,"+userId+" user_id,-999 trans_id) union (select gm_id,t_date,amount,user_id,p.trans_id from payment p inner join chitty.transaction t ON t.trans_id = p.trans_id where gm_id = "+gmId+" ) ) dt group by gm_id , substring(cast(t_date as char), 1, 10) , user_id order by t_date " db.Execute("payment.logic", "deletePaymentFromDate", paramList.toJSONArray()); joArray.addAll(payAmountAtEnd(db, methodParam)); //Pay the amount payed till now. Iterator<JSONObject> iterator = paymentsFromDate.iterator(); while (iterator.hasNext()) { JSONObject paymentFromDate = (JSONObject) iterator.next(); Float f = Float.parseFloat("" + paymentFromDate.get("amount")); int ramount = f.intValue(); JSONObject payment = new JSONObject(); payment.put("mem_id", paymentFromDate.get("mem_id")); payment.put("s_id", paymentFromDate.get("s_id")); payment.put("pay_date", paymentFromDate.get("pay_date")); payment.put("amount", "" + ramount); payment.put("collector_id", paymentFromDate.get("collector_id")); payment.put("created_by", paymentFromDate.get("created_by")); joArray.addAll(payAmountAtEnd(db, payment)); } } db.commit(); } catch (Exception e) { db.rollback(); throw e; } return joArray; } //Note method only insert at the last available space. public JSONArray payAmountAtEnd(DBConnection db, JSONObject methodParam) throws ClassNotFoundException { JSONArray joArray = new JSONArray(); // DBConnection db = new DBConnection(true); int mem_id = Integer.parseInt("" + methodParam.get("mem_id")); int s_id = Integer.parseInt("" + methodParam.get("s_id")); long amount = Long.parseLong("" + methodParam.get("amount")); String pay_date = (String) methodParam.get("pay_date"); int collector_id = Integer.parseInt("" + methodParam.get("collector_id")); int created_by = Integer.parseInt("" + getUserId()); //Insert a new Bill for student ParameterList paramList = new ParameterList(); paramList.add("mem_id", "" + mem_id); paramList.add("s_id", "" + s_id); JSONArray freeSchedules = db.Execute("payment.logic", "getFreeSchedule", paramList.toJSONArray()); if (freeSchedules.size() == 0) { throw new BusinessException("Payment Failed. Reason: All payment already completed."); } else { Iterator<JSONObject> iterator = freeSchedules.iterator(); while (iterator.hasNext()) { //If all amount is paid if (amount <= 0) { break; } //Read the free sch_id and balance that is not yet paid JSONObject freeSchedule = (JSONObject) iterator.next(); int sch_id = Integer.parseInt("" + freeSchedule.get("sch_id")); Float f = Float.parseFloat("" + freeSchedule.get("balance")); int balance = f.intValue(); //4 50 //5 100 long payamount = 0; if (balance >= amount) { payamount = amount; } else { payamount = balance; } amount = amount - payamount; paramList = new ParameterList(); paramList.add("s_id", "" + s_id); paramList.add("pay_date", pay_date, "Date"); paramList.add("amount", "" + payamount); paramList.add("sch_id", "" + sch_id); paramList.add("collector_id", "" + collector_id); paramList.add("created_by", "" + created_by); JSONArray payments = db.Execute("payment.logic", "insertPayment", paramList.toJSONArray()); int pay_id = Integer.parseInt((String) ((JSONObject) payments.iterator().next()).get("key_value")); JSONObject payment = new JSONObject(); payment.put("pay_id", pay_id); payment.put("sch_id", "" + sch_id); payment.put("pay_date", pay_date); payment.put("amount", "" + payamount); payment.put("collector_id", "" + collector_id); payment.put("created_by", "" + created_by); joArray.add(payment); } } // db.commit(); return joArray; } }