private void generatePositionsFromExistingCustomerPositions(
      CustomerBO customer,
      List<PositionEntity> customerPositions,
      List<CustomerPositionDto> customerPositionDtos) {
    for (PositionEntity position : customerPositions) {
      for (CustomerPositionEntity entity : customer.getCustomerPositions()) {
        if (position.getId().equals(entity.getPosition().getId())) {

          CustomerPositionDto customerPosition;
          if (entity.getCustomer() != null) {
            customerPosition =
                new CustomerPositionDto(
                    entity.getCustomer().getCustomerId(),
                    entity.getPosition().getId(),
                    entity.getPosition().getName());
          } else {
            customerPosition =
                new CustomerPositionDto(
                    customer.getCustomerId(),
                    entity.getPosition().getId(),
                    entity.getPosition().getName());
          }

          customerPositionDtos.add(customerPosition);
        }
      }
    }
  }
  @Test
  public void testGetAllClosedAccounts() throws Exception {
    createInitialObjects();
    MeetingBO meetingIntCalc =
        TestObjectFactory.createMeeting(TestObjectFactory.getTypicalMeeting());
    MeetingBO meetingIntPost =
        TestObjectFactory.createMeeting(TestObjectFactory.getTypicalMeeting());

    Date startDate = new Date(System.currentTimeMillis());
    savingsOffering =
        TestObjectFactory.createSavingsProduct(
            "SavingPrd1",
            ApplicableTo.GROUPS,
            new Date(System.currentTimeMillis()),
            PrdStatus.SAVINGS_ACTIVE,
            300.0,
            RecommendedAmountUnit.PER_INDIVIDUAL,
            1.2,
            200.0,
            200.0,
            SavingsType.VOLUNTARY,
            InterestCalcType.MINIMUM_BALANCE,
            meetingIntCalc,
            meetingIntPost);
    savings =
        TestObjectFactory.createSavingsAccount(
            "432434", center, AccountState.SAVINGS_CLOSED.getValue(), startDate, savingsOffering);
    List<SavingsBO> savingsAccounts = service.getAllClosedAccounts(center.getCustomerId());
    Assert.assertEquals(1, savingsAccounts.size());
  }
 private void generateNewListOfPositions(
     CustomerBO customer,
     List<PositionEntity> customerPositions,
     List<CustomerPositionDto> customerPositionDtos) {
   for (PositionEntity position : customerPositions) {
     CustomerPositionDto customerPosition =
         new CustomerPositionDto(customer.getCustomerId(), position.getId(), position.getName());
     customerPositionDtos.add(customerPosition);
   }
 }
  @Override
  public CenterDto retrieveCenterDetailsForUpdate(Integer centerId) {

    MifosUser user =
        (MifosUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    UserContext userContext = toUserContext(user);

    CustomerBO center = customerDao.findCustomerById(centerId);

    Short officeId = center.getOffice().getOfficeId();
    String searchId = center.getSearchId();
    Short loanOfficerId = extractLoanOfficerId(center);

    CenterCreation centerCreation =
        new CenterCreation(
            officeId,
            userContext.getId(),
            userContext.getLevelId(),
            userContext.getPreferredLocale());
    List<PersonnelDto> activeLoanOfficersForBranch =
        personnelDao.findActiveLoanOfficersForOffice(centerCreation);

    List<CustomerDto> customerList =
        customerDao.findClientsThatAreNotCancelledOrClosed(searchId, officeId);

    List<CustomerPositionDto> customerPositionDtos =
        generateCustomerPositionViews(center, userContext.getLocaleId());

    DateTime mfiJoiningDate = new DateTime();
    String mfiJoiningDateAsString = "";
    if (center.getMfiJoiningDate() != null) {
      mfiJoiningDate = new DateTime(center.getMfiJoiningDate());
      mfiJoiningDateAsString =
          DateUtils.getUserLocaleDate(
              userContext.getPreferredLocale(), center.getMfiJoiningDate().toString());
    }

    AddressDto address = null;
    if (center.getAddress() != null) {
      address = Address.toDto(center.getAddress());
    }
    return new CenterDto(
        loanOfficerId,
        center.getCustomerId(),
        center.getGlobalCustNum(),
        mfiJoiningDate,
        mfiJoiningDateAsString,
        center.getExternalId(),
        address,
        customerPositionDtos,
        customerList,
        activeLoanOfficersForBranch,
        true);
  }
  public void testSuccessfulPreview() throws Exception {
    CollectionSheetEntryGridDto bulkEntry = getSuccessfulBulkEntry();
    Calendar meetinDateCalendar = new GregorianCalendar();
    int year = meetinDateCalendar.get(Calendar.YEAR);
    int month = meetinDateCalendar.get(Calendar.MONTH);
    int day = meetinDateCalendar.get(Calendar.DAY_OF_MONTH);
    meetinDateCalendar = new GregorianCalendar(year, month, day);

    request.setAttribute(Constants.CURRENTFLOWKEY, flowKey);
    SessionUtils.setAttribute(CollectionSheetEntryConstants.BULKENTRY, bulkEntry, request);
    setRequestPathInfo("/collectionsheetaction.do");
    addRequestParameter(Constants.CURRENTFLOWKEY, flowKey);
    addRequestParameter("method", "preview");
    addRequestParameter("attendanceSelected[0]", "1");
    addRequestParameter("enteredAmount[0][0]", "212.0");
    addRequestParameter("enteredAmount[1][1]", "212.0");
    addRequestParameter("enteredAmount[0][1]", "212.0");
    addRequestParameter("enteredAmount[1][0]", "212.0");
    addRequestParameter("withDrawalAmountEntered[2][2]", "100.0");
    addRequestParameter("depositAmountEntered[2][2]", "100.0");
    addRequestParameter("withDrawalAmountEntered[0][0]", "100.0");
    addRequestParameter("depositAmountEntered[0][0]", "100.0");
    addRequestDateParameter("transactionDate", day + "/" + (month + 1) + "/" + year);
    performNoErrors();
    verifyForward("preview_success");

    groupAccount = TestObjectFactory.getObject(LoanBO.class, groupAccount.getAccountId());
    clientAccount = TestObjectFactory.getObject(LoanBO.class, clientAccount.getAccountId());
    centerSavingsAccount =
        TestObjectFactory.getObject(SavingsBO.class, centerSavingsAccount.getAccountId());
    clientSavingsAccount =
        TestObjectFactory.getObject(SavingsBO.class, clientSavingsAccount.getAccountId());
    groupSavingsAccount =
        TestObjectFactory.getObject(SavingsBO.class, groupSavingsAccount.getAccountId());
    center = TestObjectFactory.getCustomer(center.getCustomerId());
    group = TestObjectFactory.getCustomer(group.getCustomerId());
    client = TestObjectFactory.getClient(client.getCustomerId());
  }
 private CustomerView getCusomerView(final CustomerBO customer) {
   CustomerView customerView = new CustomerView();
   customerView.setCustomerId(customer.getCustomerId());
   customerView.setCustomerLevelId(customer.getCustomerLevel().getId());
   customerView.setCustomerSearchId(customer.getSearchId());
   customerView.setDisplayName(customer.getDisplayName());
   customerView.setGlobalCustNum(customer.getGlobalCustNum());
   customerView.setOfficeId(customer.getOffice().getOfficeId());
   if (null != customer.getParentCustomer()) {
     customerView.setParentCustomerId(customer.getParentCustomer().getCustomerId());
   }
   customerView.setPersonnelId(customer.getPersonnel().getPersonnelId());
   customerView.setStatusId(customer.getCustomerStatus().getId());
   return customerView;
 }
  @SuppressWarnings("unchecked")
  public void testSuccessfulGet() throws Exception {

    MeetingBO meeting =
        TestObjectFactory.createMeeting(
            TestObjectFactory.getNewMeetingForToday(WEEKLY, EVERY_WEEK, CUSTOMER_MEETING));
    center = TestObjectFactory.createWeeklyFeeCenter("Center", meeting);
    group =
        TestObjectFactory.createWeeklyFeeGroupUnderCenter(
            "Group", CustomerStatus.GROUP_ACTIVE, center);
    client = TestObjectFactory.createClient("Client", CustomerStatus.CLIENT_ACTIVE, group);
    account = getLoanAccount(group, meeting);

    // Using utility method that uses builder pattern to create savings accounts - TestObjectFactory
    // was creating
    // installments for all savings accounts (which is wrong)
    TestCollectionSheetRetrieveSavingsAccountsUtils collectionSheetRetrieveSavingsAccountsUtils =
        new TestCollectionSheetRetrieveSavingsAccountsUtils();
    centerSavingsAccount =
        collectionSheetRetrieveSavingsAccountsUtils.createSavingsAccount(
            center, "cemi", "120.00", false, false);
    groupSavingsAccount =
        collectionSheetRetrieveSavingsAccountsUtils.createSavingsAccount(
            group, "gvcg", "180.00", true, false);
    clientSavingsAccount =
        collectionSheetRetrieveSavingsAccountsUtils.createSavingsAccount(
            client, "clm", "222.00", false, false);

    request.setAttribute(Constants.CURRENTFLOWKEY, flowKey);
    CustomerView customerView = new CustomerView();
    customerView.setCustomerId(center.getCustomerId());
    customerView.setCustomerSearchId(center.getSearchId());
    customerView.setCustomerLevelId(center.getCustomerLevel().getId());

    final OfficeView officeView =
        new OfficeView(
            Short.valueOf("3"), "", OfficeLevel.BRANCHOFFICE, "levelNameKey", Integer.valueOf(-1));
    final PersonnelView personnelView = new PersonnelView(Short.valueOf("3"), "");

    SessionUtils.setAttribute(
        CollectionSheetEntryConstants.COLLECTION_SHEET_ENTRY_FORM_DTO,
        createCollectionSheetDto(customerView, officeView, personnelView),
        request);

    SessionUtils.setCollectionAttribute(
        CollectionSheetEntryConstants.PAYMENT_TYPES_LIST,
        Arrays.asList(getPaymentTypeView()),
        request);
    SessionUtils.setAttribute(
        CollectionSheetEntryConstants.ISCENTERHIERARCHYEXISTS, Constants.YES, request);

    setMasterListInSession(center.getCustomerId());
    setRequestPathInfo("/collectionsheetaction.do");
    addRequestParameter("method", "get");
    addRequestParameter("officeId", "3");
    addRequestParameter("loanOfficerId", "3");
    addRequestParameter("paymentId", "1");
    addRequestParameter(Constants.CURRENTFLOWKEY, flowKey);

    Calendar meetinDateCalendar = new GregorianCalendar();
    meetinDateCalendar.setTime(getMeetingDates(meeting));
    int year = meetinDateCalendar.get(Calendar.YEAR);
    int month = meetinDateCalendar.get(Calendar.MONTH);
    int day = meetinDateCalendar.get(Calendar.DAY_OF_MONTH);
    meetinDateCalendar = new GregorianCalendar(year, month, day);
    SessionUtils.setAttribute(
        "LastMeetingDate", new java.sql.Date(meetinDateCalendar.getTimeInMillis()), request);
    addRequestDateParameter("transactionDate", day + "/" + (month + 1) + "/" + year);
    addRequestParameter("receiptId", "1");
    addRequestDateParameter("receiptDate", "20/03/2006");
    addRequestParameter("customerId", String.valueOf(center.getCustomerId().intValue()));
    actionPerform();
    verifyNoActionErrors();
    verifyNoActionMessages();
    verifyForward("get_success");
  }
  public void testGetLastMeetingDateForCustomer() throws Exception {
    MeetingBO meeting =
        TestObjectFactory.createMeeting(
            TestObjectFactory.getNewMeetingForToday(WEEKLY, EVERY_WEEK, CUSTOMER_MEETING));
    center = TestObjectFactory.createWeeklyFeeCenter("Center_Active", meeting);
    setRequestPathInfo("/collectionsheetaction.do");
    addRequestParameter("method", "getLastMeetingDateForCustomer");
    addRequestParameter(Constants.CURRENTFLOWKEY, flowKey);
    addRequestParameter("officeId", "3");
    addRequestParameter("loanOfficerId", "1");
    addRequestParameter("customerId", String.valueOf(center.getCustomerId().intValue()));

    request.setAttribute(Constants.CURRENTFLOWKEY, flowKey);
    SessionUtils.setAttribute(
        CollectionSheetEntryConstants.COLLECTION_SHEET_ENTRY_FORM_DTO,
        createDefaultCollectionSheetDto(),
        request);

    actionPerform();
    verifyNoActionErrors();
    verifyNoActionMessages();
    verifyForward("load_success");

    if (AccountingRules.isBackDatedTxnAllowed()) {
      Assert.assertEquals(
          "The value for isBackDated Trxn Allowed",
          SessionUtils.getAttribute(CollectionSheetEntryConstants.ISBACKDATEDTRXNALLOWED, request),
          Constants.YES);
      Assert.assertEquals(
          new java.sql.Date(
                  DateUtils.getDateWithoutTimeStamp(getMeetingDates(meeting).getTime()).getTime())
              .toString(),
          SessionUtils.getAttribute("LastMeetingDate", request).toString());
      Assert.assertEquals(
          new java.util.Date(
              DateUtils.getDateWithoutTimeStamp(getMeetingDates(meeting).getTime()).getTime()),
          DateUtils.getDate(
              ((BulkEntryActionForm)
                      request
                          .getSession()
                          .getAttribute(CollectionSheetEntryConstants.BULKENTRYACTIONFORM))
                  .getTransactionDate()));
    } else {
      Assert.assertEquals(
          "The value for isBackDated Trxn Allowed",
          SessionUtils.getAttribute(CollectionSheetEntryConstants.ISBACKDATEDTRXNALLOWED, request),
          Constants.NO);
      Assert.assertEquals(
          new java.sql.Date(
                  DateUtils.getDateWithoutTimeStamp(getMeetingDates(meeting).getTime()).getTime())
              .toString(),
          SessionUtils.getAttribute("LastMeetingDate", request).toString());
      Assert.assertEquals(
          DateUtils.getUserLocaleDate(
              getUserLocale(request),
              new java.sql.Date(DateUtils.getCurrentDateWithoutTimeStamp().getTime()).toString()),
          ((BulkEntryActionForm)
                  request
                      .getSession()
                      .getAttribute(CollectionSheetEntryConstants.BULKENTRYACTIONFORM))
              .getTransactionDate());
    }
  }
  public void testSuccessfulCreate() throws Exception {
    CollectionSheetEntryGridDto bulkEntry = getSuccessfulBulkEntry();
    Calendar meetingDateCalendar = new GregorianCalendar();
    int year = meetingDateCalendar.get(Calendar.YEAR);
    int month = meetingDateCalendar.get(Calendar.MONTH);
    int day = meetingDateCalendar.get(Calendar.DAY_OF_MONTH);
    meetingDateCalendar = new GregorianCalendar(year, month, day);

    Date meetingDate = new Date(meetingDateCalendar.getTimeInMillis());
    HashMap<Integer, ClientAttendanceDto> clientAttendance =
        new HashMap<Integer, ClientAttendanceDto>();
    clientAttendance.put(1, getClientAttendanceDto(1, meetingDate));
    clientAttendance.put(2, getClientAttendanceDto(2, meetingDate));
    clientAttendance.put(3, getClientAttendanceDto(3, meetingDate));

    request.setAttribute(Constants.CURRENTFLOWKEY, flowKey);
    addRequestParameter("attendanceSelected[0]", "2");

    request.setAttribute(Constants.CURRENTFLOWKEY, flowKey);
    SessionUtils.setAttribute(CollectionSheetEntryConstants.BULKENTRY, bulkEntry, request);
    setRequestPathInfo("/collectionsheetaction.do");
    addRequestParameter("method", "preview");
    addRequestParameter(Constants.CURRENTFLOWKEY, flowKey);
    addRequestDateParameter("transactionDate", day + "/" + (month + 1) + "/" + year);

    if (SUPPLY_ENTERED_AMOUNT_PARAMETERS) {
      addParametersForEnteredAmount();
      addParametersForDisbursalEnteredAmount();
    }

    performNoErrors();

    request.setAttribute(Constants.CURRENTFLOWKEY, flowKey);
    setRequestPathInfo("/collectionsheetaction.do");
    addRequestParameter("method", "create");
    addRequestParameter(Constants.CURRENTFLOWKEY, flowKey);
    addRequestParameter("attendanceSelected[0]", "2");
    addRequestDateParameter("transactionDate", day + "/" + (month + 1) + "/" + year);
    addRequestParameter("customerId", "1");

    performNoErrors();
    verifyForward("create_success");
    Assert.assertNotNull(request.getAttribute(CollectionSheetEntryConstants.CENTER));
    Assert.assertEquals(
        request.getAttribute(CollectionSheetEntryConstants.CENTER), center.getDisplayName());

    groupAccount = TestObjectFactory.getObject(LoanBO.class, groupAccount.getAccountId());
    clientAccount = TestObjectFactory.getObject(LoanBO.class, clientAccount.getAccountId());
    centerSavingsAccount =
        TestObjectFactory.getObject(SavingsBO.class, centerSavingsAccount.getAccountId());
    clientSavingsAccount =
        TestObjectFactory.getObject(SavingsBO.class, clientSavingsAccount.getAccountId());
    groupSavingsAccount =
        TestObjectFactory.getObject(SavingsBO.class, groupSavingsAccount.getAccountId());
    center = TestObjectFactory.getCustomer(center.getCustomerId());
    group = TestObjectFactory.getCustomer(group.getCustomerId());
    client = TestObjectFactory.getClient(client.getCustomerId());

    Assert.assertEquals(1, client.getClientAttendances().size());
    Assert.assertEquals(
        AttendanceType.ABSENT,
        client
            .getClientAttendanceForMeeting(new java.sql.Date(meetingDateCalendar.getTimeInMillis()))
            .getAttendanceAsEnum());
  }