@Test
  public void testFind3rdPartyPayRecordsByBill() throws Exception {

    BillingONCHeader1Dao daoBONCH =
        (BillingONCHeader1Dao) SpringUtils.getBean(BillingONCHeader1Dao.class);
    BillingONCHeader1 bONCHeader1 = new BillingONCHeader1();
    EntityDataGenerator.generateTestDataForModelClass(bONCHeader1);

    int billingNo = 1;

    BillingONPayment bONPayment1 = new BillingONPayment();
    EntityDataGenerator.generateTestDataForModelClass(bONPayment1);
    Date date1 = new Date(dfm.parse("20110101").getTime());
    bONPayment1.setBillingNo(billingNo);
    bONPayment1.setPaymentDate(date1);

    BillingONPayment bONPayment2 = new BillingONPayment();
    EntityDataGenerator.generateTestDataForModelClass(bONPayment2);
    Date date2 = new Date(dfm.parse("20110701").getTime());
    bONPayment2.setBillingNo(billingNo);
    bONPayment2.setPaymentDate(date2);

    BillingONPayment bONPayment3 = new BillingONPayment();
    EntityDataGenerator.generateTestDataForModelClass(bONPayment3);
    Date date3 = new Date(dfm.parse("20110301").getTime());
    bONPayment3.setBillingNo(billingNo);
    bONPayment3.setPaymentDate(date3);

    daoBONCH.persist(bONCHeader1);
    dao.persist(bONPayment1);
    dao.persist(bONPayment2);
    dao.persist(bONPayment3);

    List<BillingONPayment> result = dao.find3rdPartyPayRecordsByBill(bONCHeader1);
    List<BillingONPayment> expectedResult =
        new ArrayList<BillingONPayment>(
            Arrays.asList(
                bONPayment1,
                bONPayment3,
                bONPayment2)); /// add the three payment obj in the expected order and assert =
    // expected result

    Logger logger = MiscUtils.getLogger();

    if (result.size() != expectedResult.size()) {
      logger.warn("Array sizes do not match.");
      fail("Array sizes do not match.");
    }

    for (int i = 0; i < expectedResult.size(); i++) {
      if (!expectedResult.get(i).equals(result.get(i))) {
        logger.warn("Items not sorted by Billing Payment Date.");
        fail("Items not sorted by Billing Payment Date.");
      }
    }
    assertTrue(true);
  }
  public boolean add3rdBillExt(Map<String, String> mVal, int id) {
    boolean retval = true;
    String[] temp = {
      "billTo",
      "remitTo",
      "total",
      "payment",
      "refund",
      "provider_no",
      "gst",
      "payDate",
      "payMethod"
    };
    String demoNo = mVal.get("demographic_no");
    String dateTime = UtilDateUtilities.getToday("yyyy-MM-dd HH:mm:ss");
    mVal.put("payDate", dateTime);

    BillingONPaymentDao billingONPaymentDao = SpringUtils.getBean(BillingONPaymentDao.class);
    BillingONPayment newPayment = new BillingONPayment();
    BillingONCHeader1 ch1 = cheaderDao.find(id);
    newPayment.setBillingOnCheader1(ch1);
    newPayment.setPaymentDate(UtilDateUtilities.StringToDate(dateTime));

    for (int i = 0; i < temp.length; i++) {
      BillingONExt b = new BillingONExt();
      b.setBillingNo(id);
      b.setDemographicNo(Integer.valueOf(demoNo));
      b.setKeyVal(temp[i]);
      b.setValue(mVal.get(temp[i]));
      b.setDateTime(new Date());
      b.setStatus('1');
      b.setPaymentId(0);
      newPayment.getBillingONExtItems().add(b);
    }

    billingONPaymentDao.persist(newPayment);

    return retval;
  }
  @SuppressWarnings("unchecked")
  public boolean add3rdBillExt(Map<String, String> mVal, int id, Vector vecObj) {
    BillingClaimHeader1Data claim1Obj = (BillingClaimHeader1Data) vecObj.get(0);
    boolean retval = true;
    String[] temp = {
      "billTo",
      "remitTo",
      "total",
      "payment",
      "discount",
      "provider_no",
      "gst",
      "payDate",
      "payMethod"
    };
    String demoNo = mVal.get("demographic_no");
    String dateTime = UtilDateUtilities.getToday("yyyy-MM-dd HH:mm:ss");
    mVal.put("payDate", dateTime);
    String paymentSumParam = null;
    String paymentDateParam = null;
    String paymentTypeParam = null;
    String provider_no = mVal.get("provider_no");
    for (int i = 0; i < temp.length; i++) {
      String val = mVal.get(temp[i]);
      if ("discount".equals(temp[i])) {
        val =
            mVal.get(
                "total_discount"); // 'refund' stands for write off, here totoal_discount is write
                                   // off
      }
      if ("payment".equals(temp[i])) {
        val = mVal.get("total_payment");
      }
      BillingONExt billingONExt = new BillingONExt();
      billingONExt.setBillingNo(id);
      billingONExt.setDemographicNo(Integer.parseInt(demoNo));
      billingONExt.setKeyVal(StringEscapeUtils.escapeSql(temp[i]));
      billingONExt.setValue(StringEscapeUtils.escapeSql(val));
      billingONExt.setDateTime(new Date());
      billingONExt.setStatus('1');
      extDao.persist(billingONExt);

      if (i == 3) paymentSumParam = mVal.get("total_payment"); // total_payment
      else if (i == 7) paymentDateParam = mVal.get(temp[i]); // paymentDate
      else if (i == 8) paymentTypeParam = mVal.get(temp[i]); // paymentMethod
    }

    if (paymentSumParam != null) {
      BillingONPaymentDao billingONPaymentDao =
          (BillingONPaymentDao) SpringUtils.getBean("billingONPaymentDao");
      BillingPaymentTypeDao billingPaymentTypeDao =
          (BillingPaymentTypeDao) SpringUtils.getBean("billingPaymentTypeDao");
      BillingONCHeader1 ch1 = cheaderDao.find(id);
      Date paymentDate = null;
      try {
        paymentDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(paymentDateParam);
      } catch (ParseException ex) {
        _logger.error("add3rdBillExt wrong date format " + paymentDateParam);
        return retval;
      }

      // allow user to override with the text box added
      String paymentDateOverride = mVal.get("payment_date");
      if (paymentDateOverride != null && paymentDateOverride.length() > 0) {
        try {
          paymentDate =
              new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(paymentDateOverride + " 00:00:00");
        } catch (ParseException ex) {
          _logger.error("add3rdBillExt wrong date format " + paymentDateOverride);
          return retval;
        }
      }

      if (paymentTypeParam == null || paymentTypeParam.equals("")) {
        paymentTypeParam = "1";
      }
      BillingPaymentType type = billingPaymentTypeDao.find(Integer.parseInt(paymentTypeParam));
      BillingONPayment payment = null;

      if (paymentSumParam != null) {
        payment = new BillingONPayment();
        payment.setTotal_payment(BigDecimal.valueOf(Double.parseDouble(paymentSumParam)));
        payment.setTotal_discount(
            BigDecimal.valueOf(Double.parseDouble(mVal.get("total_discount"))));
        payment.setTotal_refund(new BigDecimal(0));
        payment.setPaymentDate(paymentDate);
        payment.setBillingOnCheader1(ch1);
        payment.setBillingNo(id);
        payment.setCreator(claim1Obj.getCreator());
        payment.setPaymentTypeId(Integer.parseInt(paymentTypeParam));

        // payment.setBillingPaymentType(type);
        billingONPaymentDao.persist(payment);
        addItemPaymentRecord(
            (List) vecObj.get(1),
            id,
            payment.getId(),
            Integer.parseInt(paymentTypeParam),
            paymentDate);
        addCreate3rdInvoiceTrans(
            (BillingClaimHeader1Data) vecObj.get(0),
            (List<BillingItemData>) vecObj.get(1),
            payment);
      }
    }
    return retval;
  }
  private void addCreate3rdInvoiceTrans(
      BillingClaimHeader1Data billHeader,
      List<BillingItemData> billItemList,
      BillingONPayment billOnPayment) {
    if (billItemList.size() < 1) {
      return;
    }
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Timestamp updateTs = new Timestamp(new Date().getTime());
    BillingOnTransaction billTrans = null;
    BillingOnTransactionDao billTransDao =
        (BillingOnTransactionDao) SpringUtils.getBean(BillingOnTransactionDao.class);
    for (BillingItemData billItem : billItemList) {
      billTrans = new BillingOnTransaction();
      billTrans.setActionType(BillingDataHlp.ACTION_TYPE.C.name());
      try {
        billTrans.setAdmissionDate(sdf.parse(billHeader.getAdmission_date()));
      } catch (Exception e) {
        billTrans.setAdmissionDate(null);
      }
      try {
        billTrans.setBillingDate(sdf.parse(billHeader.getBilling_date()));
      } catch (Exception e) {
        billTrans.setBillingDate(null);
      }

      billTrans.setBillingNotes(billHeader.getComment());
      billTrans.setBillingOnItemPaymentId(Integer.parseInt(billItem.getId()));
      billTrans.setCh1Id(Integer.parseInt(billHeader.getId()));
      billTrans.setClinic(billHeader.getClinic());
      billTrans.setCreator(billHeader.getCreator());
      billTrans.setDemographicNo(Integer.parseInt(billHeader.getDemographic_no()));
      billTrans.setDxCode(billItem.getDx());
      billTrans.setFacilityNum(billHeader.getFacilty_num());
      billTrans.setManReview(billHeader.getMan_review());
      billTrans.setPaymentDate(billOnPayment.getPaymentDate());
      billTrans.setPaymentId(billOnPayment.getId());
      billTrans.setPaymentType(billOnPayment.getPaymentTypeId());
      billTrans.setPayProgram(billHeader.getPay_program());
      billTrans.setProviderNo(billHeader.getProviderNo());
      billTrans.setProvince(billHeader.getProvince());
      billTrans.setRefNum(billHeader.getRef_num());
      billTrans.setServiceCode(billItem.getService_code());
      billTrans.setServiceCodeInvoiced(billItem.getFee());
      try {
        billTrans.setServiceCodeDiscount(new BigDecimal(billItem.getDiscount()));
      } catch (Exception e) {
        billTrans.setServiceCodeDiscount(BigDecimal.ZERO);
      }
      billTrans.setServiceCodeNum(billItem.getSer_num());
      try {
        billTrans.setServiceCodePaid(new BigDecimal(billItem.getPaid()));
      } catch (Exception e) {
        billTrans.setServiceCodePaid(BigDecimal.ZERO);
      }
      try {
        billTrans.setServiceCodeRefund(new BigDecimal(billItem.getRefund()));
      } catch (Exception e) {
        billTrans.setServiceCodeRefund(BigDecimal.ZERO);
      }
      billTrans.setStatus(billHeader.getStatus());
      billTrans.setSliCode(billHeader.getLocation());
      billTrans.setUpdateDatetime(updateTs);
      billTrans.setUpdateProviderNo(billHeader.getCreator());
      billTrans.setVisittype(billHeader.getVisittype());
      billTransDao.persist(billTrans);
    }
  }
  @Test
  public void testFind3rdPartyPayRecordsByBillBillingONCHeader1DateDate() throws Exception {

    BillingONCHeader1Dao daoBONCH =
        (BillingONCHeader1Dao) SpringUtils.getBean(BillingONCHeader1Dao.class);

    BillingONCHeader1 bONCHeader1 = new BillingONCHeader1();
    EntityDataGenerator.generateTestDataForModelClass(bONCHeader1);
    Date startDate = new Date(dfm.parse("20101230").getTime());
    Date endDate = new Date(dfm.parse("20120101").getTime());

    BillingONPayment bONPayment1 = new BillingONPayment();
    EntityDataGenerator.generateTestDataForModelClass(bONPayment1);
    Date Date1 = new Date(dfm.parse("20110102").getTime());
    bONPayment1.setBillingNo(1);
    bONPayment1.setPaymentDate(Date1);

    BillingONPayment bONPayment2 = new BillingONPayment();
    EntityDataGenerator.generateTestDataForModelClass(bONPayment2);
    Date Date2 = new Date(dfm.parse("20110302").getTime());
    bONPayment2.setBillingNo(1);
    bONPayment2.setPaymentDate(Date2);

    BillingONPayment bONPayment3 = new BillingONPayment();
    EntityDataGenerator.generateTestDataForModelClass(bONPayment3);
    Date Date3 = new Date(dfm.parse("20110502").getTime());
    bONPayment3.setBillingNo(1);
    bONPayment3.setPaymentDate(Date3);

    /// out of bound dates
    BillingONPayment bONPayment4 = new BillingONPayment();
    EntityDataGenerator.generateTestDataForModelClass(bONPayment4);
    Date Date4 = new Date(dfm.parse("20090502").getTime());
    bONPayment4.setBillingNo(1);
    bONPayment4.setPaymentDate(Date4);

    BillingONPayment bONPayment5 = new BillingONPayment();
    EntityDataGenerator.generateTestDataForModelClass(bONPayment5);
    Date Date5 = new Date(dfm.parse("20130502").getTime());
    bONPayment5.setBillingNo(1);
    bONPayment5.setPaymentDate(Date5);

    daoBONCH.persist(bONCHeader1);
    dao.persist(bONPayment1);
    dao.persist(bONPayment2);
    dao.persist(bONPayment3);

    List<BillingONPayment> result =
        dao.find3rdPartyPayRecordsByBill(bONCHeader1, startDate, endDate);
    List<BillingONPayment> expectedResult =
        new ArrayList<BillingONPayment>(Arrays.asList(bONPayment1, bONPayment2, bONPayment3));

    Logger logger = MiscUtils.getLogger();

    if (result.size() != expectedResult.size()) {
      logger.warn("Array sizes do not match.");
      fail("Array sizes do not match.");
    }
    for (int i = 0; i < expectedResult.size(); i++) {
      if (!expectedResult.get(i).equals(result.get(i))) {
        logger.warn("Items not sorted by Billing Payment Date.");
        fail("Items not sorted by Billing Payment Date.");
      }
    }
    assertTrue(true);
  }