Search in sources :

Example 1 with InvoiceAddressDetail

use of org.kuali.kfs.module.ar.businessobject.InvoiceAddressDetail in project cu-kfs by CU-CommunityApps.

the class ContractsGrantsInvoiceCreateDocumentServiceImpl method buildInvoiceAddressDetails.

/**
 * Builds a list of InvoiceAddressDetails based on the customer associated with the Award
 *
 * @param award    the award associated with the proposal we're building a CINV document for
 * @param document the CINV document we're creating
 * @return a List of the generated invoice address details
 */
protected List<InvoiceAddressDetail> buildInvoiceAddressDetails(ContractsAndGrantsBillingAward award, ContractsGrantsInvoiceDocument document) {
    CustomerAddress customerAddress = null;
    if (ObjectUtils.isNotNull(award.getCustomerAddressIdentifier())) {
        customerAddress = customerAddressService.getByPrimaryKey(award.getCustomerNumber(), award.getCustomerAddressIdentifier());
    }
    if (customerAddress == null) {
        customerAddress = customerAddressService.getPrimaryAddress(award.getCustomerNumber());
    }
    String documentNumber = document.getDocumentNumber();
    List<InvoiceAddressDetail> invoiceAddressDetails = new ArrayList<>();
    AtomicInteger detailNumber = new AtomicInteger(1);
    if (StringUtils.equalsIgnoreCase(ArKeyConstants.CustomerConstants.CUSTOMER_ADDRESS_TYPE_CODE_PRIMARY, customerAddress.getCustomerAddressTypeCode())) {
        document.setCustomerBillToAddressOnInvoice(customerAddress);
    }
    InvoiceAddressDetail invoiceAddressDetail = new InvoiceAddressDetail();
    invoiceAddressDetail.setCustomerNumber(customerAddress.getCustomerNumber());
    invoiceAddressDetail.setDocumentNumber(documentNumber);
    invoiceAddressDetail.setCustomerAddressIdentifier(customerAddress.getCustomerAddressIdentifier());
    invoiceAddressDetail.setDetailNumber(detailNumber.getAndIncrement());
    invoiceAddressDetail.setCustomerAddressTypeCode(customerAddress.getCustomerAddressTypeCode());
    invoiceAddressDetail.setCustomerAddressName(customerAddress.getCustomerAddressName());
    invoiceAddressDetail.setInvoiceTransmissionMethodCode(ArConstants.InvoiceTransmissionMethod.MAIL);
    invoiceAddressDetail.setCustomerLine1StreetAddress(customerAddress.getCustomerLine1StreetAddress());
    invoiceAddressDetail.setCustomerLine2StreetAddress(customerAddress.getCustomerLine2StreetAddress());
    invoiceAddressDetail.setCustomerCityName(customerAddress.getCustomerCityName());
    invoiceAddressDetail.setCustomerStateCode(customerAddress.getCustomerStateCode());
    invoiceAddressDetail.setCustomerZipCode(customerAddress.getCustomerZipCode());
    invoiceAddressDetail.setCustomerCountryCode(customerAddress.getCustomerCountryCode());
    invoiceAddressDetail.setCustomerInternationalMailCode(customerAddress.getCustomerInternationalMailCode());
    invoiceAddressDetail.setCustomerAddressInternationalProvinceName(customerAddress.getCustomerAddressInternationalProvinceName());
    if (StringUtils.isNotBlank(customerAddress.getCustomerInvoiceTemplateCode())) {
        invoiceAddressDetail.setCustomerInvoiceTemplateCode(customerAddress.getCustomerInvoiceTemplateCode());
    } else {
        AccountsReceivableCustomer customer = award.getAgency().getCustomer();
        if (ObjectUtils.isNotNull(customer) && StringUtils.isNotBlank(customer.getCustomerInvoiceTemplateCode())) {
            invoiceAddressDetail.setCustomerInvoiceTemplateCode(customer.getCustomerInvoiceTemplateCode());
        }
    }
    if (ArConstants.InvoiceTransmissionMethod.MAIL.equals(customerAddress.getInvoiceTransmissionMethodCode())) {
        invoiceAddressDetail.setSendIndicator(true);
    }
    invoiceAddressDetails.add(invoiceAddressDetail);
    CustomerAddress finalCustomerAddress = customerAddress;
    customerAddress.getCustomerAddressEmails().stream().filter(AccountsReceivableCustomerAddressEmail::isActive).forEach(email -> {
        InvoiceAddressDetail invoiceAddressDetailWithEmail = new InvoiceAddressDetail();
        invoiceAddressDetailWithEmail.setCustomerNumber(finalCustomerAddress.getCustomerNumber());
        invoiceAddressDetailWithEmail.setDocumentNumber(documentNumber);
        invoiceAddressDetailWithEmail.setCustomerAddressIdentifier(finalCustomerAddress.getCustomerAddressIdentifier());
        invoiceAddressDetailWithEmail.setDetailNumber(detailNumber.getAndIncrement());
        invoiceAddressDetailWithEmail.setCustomerEmailAddress(email.getCustomerEmailAddress());
        invoiceAddressDetailWithEmail.setInvoiceTransmissionMethodCode(ArConstants.InvoiceTransmissionMethod.EMAIL);
        if (ArConstants.InvoiceTransmissionMethod.EMAIL.equals(finalCustomerAddress.getInvoiceTransmissionMethodCode())) {
            invoiceAddressDetailWithEmail.setSendIndicator(true);
        }
        invoiceAddressDetails.add(invoiceAddressDetailWithEmail);
    });
    return invoiceAddressDetails;
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AccountsReceivableCustomer(org.kuali.kfs.integration.ar.AccountsReceivableCustomer) ArrayList(java.util.ArrayList) InvoiceAddressDetail(org.kuali.kfs.module.ar.businessobject.InvoiceAddressDetail) CustomerAddress(org.kuali.kfs.module.ar.businessobject.CustomerAddress)

Example 2 with InvoiceAddressDetail

use of org.kuali.kfs.module.ar.businessobject.InvoiceAddressDetail in project cu-kfs by CU-CommunityApps.

the class ContractsGrantsInvoiceCreateDocumentServiceImpl method populateInvoiceFromAward.

/*
     * CU Customization (KFSPTS-23675):
     * Added creationProcessType argument and its usage of it.
     */
/**
 * This method takes all the applicable attributes from the associated award object and sets those attributes into
 * their corresponding invoice attributes.
 *
 * @param award           The associated award that the invoice will be linked to.
 * @param awardAccounts
 * @param document
 * @param accountDetails  letter of credit details if we're creating via loc
 * @param locCreationType letter of credit creation type if we're creating via loc
 * @param creationProcessType The creation process type for the related invoice
 */
protected void populateInvoiceFromAward(ContractsAndGrantsBillingAward award, List<ContractsAndGrantsBillingAwardAccount> awardAccounts, ContractsGrantsInvoiceDocument document, List<ContractsGrantsLetterOfCreditReviewDetail> accountDetails, String locCreationType, ContractsAndGrantsInvoiceDocumentCreationProcessType creationProcessType) {
    if (ObjectUtils.isNotNull(award)) {
        InvoiceGeneralDetail invoiceGeneralDetail = new InvoiceGeneralDetail();
        invoiceGeneralDetail.setDocumentNumber(document.getDocumentNumber());
        invoiceGeneralDetail.setProposalNumber(award.getProposalNumber());
        invoiceGeneralDetail.setAward(award);
        Timestamp ts = new Timestamp(new java.util.Date().getTime());
        java.sql.Date today = new java.sql.Date(ts.getTime());
        AccountingPeriod currPeriod = accountingPeriodService.getByDate(today);
        BillingPeriod billingPeriod = verifyBillingFrequencyService.getStartDateAndEndDateOfPreviousBillingPeriod(award, currPeriod, creationProcessType);
        invoiceGeneralDetail.setBillingPeriod(getDateTimeService().toDateString(billingPeriod.getStartDate()) + " to " + getDateTimeService().toDateString(billingPeriod.getEndDate()));
        invoiceGeneralDetail.setLastBilledDate(billingPeriod.getEndDate());
        populateInvoiceDetailFromAward(invoiceGeneralDetail, award);
        document.setInvoiceGeneralDetail(invoiceGeneralDetail);
        // To set Bill by address identifier because it is a required field - set the value to 1 as it is never
        // being used.
        document.setCustomerBillToAddressIdentifier(1);
        // Set Invoice due date to current date as it is required field and never used.
        document.setInvoiceDueDate(dateTimeService.getCurrentSqlDateMidnight());
        document.getInvoiceAddressDetails().clear();
        ContractsAndGrantsBillingAgency agency = award.getAgency();
        if (ObjectUtils.isNotNull(agency)) {
            final List<InvoiceAddressDetail> invoiceAddressDetails = buildInvoiceAddressDetails(award, document);
            document.getInvoiceAddressDetails().addAll(invoiceAddressDetails);
        }
        if (ArConstants.BillingFrequencyValues.isMilestone(document.getInvoiceGeneralDetail())) {
            ContractsAndGrantsBillingAwardAccount awardAccount = awardAccounts.get(0);
            final List<Milestone> milestones = getContractsGrantsBillingUtilityService().getActiveMilestonesForProposalNumber(award.getProposalNumber(), awardAccount.getChartOfAccountsCode(), awardAccount.getAccountNumber());
            if (!CollectionUtils.isEmpty(milestones)) {
                document.getInvoiceMilestones().clear();
                document.getInvoiceMilestones().addAll(buildInvoiceMilestones(milestones));
            }
        } else if (ArConstants.BillingFrequencyValues.isPredeterminedBilling(document.getInvoiceGeneralDetail())) {
            ContractsAndGrantsBillingAwardAccount awardAccount = awardAccounts.get(0);
            final List<Bill> bills = getContractsGrantsBillingUtilityService().getActiveBillsForAwardAccount(award.getProposalNumber(), awardAccount.getChartOfAccountsCode(), awardAccount.getAccountNumber());
            if (!CollectionUtils.isEmpty(bills)) {
                document.getInvoiceBills().clear();
                document.getInvoiceBills().addAll(buildInvoiceBills(bills));
            }
        }
        document.getAccountDetails().clear();
        final List<InvoiceAccountDetail> invoiceAccountDetails = new ArrayList<>();
        List<InvoiceDetailAccountObjectCode> invoiceDetailAccountObjectsCodes = new ArrayList<>();
        Map<String, KualiDecimal> budgetAmountsByCostCategory = new HashMap<>();
        Integer currentYear = getUniversityDateService().getCurrentFiscalYear();
        final boolean firstFiscalPeriod = isFirstFiscalPeriod();
        final Integer fiscalYear = firstFiscalPeriod && ArConstants.BillingFrequencyValues.isTimeBased(document.getInvoiceGeneralDetail()) ? currentYear - 1 : currentYear;
        final SystemOptions systemOptions = optionsService.getOptions(fiscalYear);
        List<String> balanceTypeCodeList = new ArrayList<>();
        balanceTypeCodeList.add(systemOptions.getBudgetCheckingBalanceTypeCd());
        balanceTypeCodeList.add(systemOptions.getActualFinancialBalanceTypeCd());
        for (ContractsAndGrantsBillingAwardAccount awardAccount : awardAccounts) {
            InvoiceAccountDetail invoiceAccountDetail = buildInvoiceAccountDetailForAwardAccount(awardAccount, document.getDocumentNumber());
            final ContractsGrantsLetterOfCreditReviewDetail locReviewDetail = retrieveMatchingLetterOfCreditReviewDetail(awardAccount, accountDetails);
            List<Balance> glBalances = retrieveBalances(fiscalYear, awardAccount.getChartOfAccountsCode(), awardAccount.getAccountNumber(), balanceTypeCodeList);
            KualiDecimal awardAccountBudgetAmount = KualiDecimal.ZERO;
            KualiDecimal awardAccountCumulativeAmount = KualiDecimal.ZERO;
            for (Balance balance : glBalances) {
                if (!isBalanceCostShare(balance)) {
                    if (balance.getBalanceTypeCode().equalsIgnoreCase(systemOptions.getBudgetCheckingBalanceTypeCd())) {
                        awardAccountBudgetAmount = addBalanceToAwardAccountBudgetAmount(balance, awardAccountBudgetAmount, firstFiscalPeriod);
                        updateCategoryBudgetAmountsByBalance(balance, budgetAmountsByCostCategory, firstFiscalPeriod);
                    } else if (balance.getBalanceTypeCode().equalsIgnoreCase(systemOptions.getActualFinancialBalanceTypeCd())) {
                        awardAccountCumulativeAmount = addBalanceToAwardAccountCumulativeAmount(document, balance, award, awardAccountCumulativeAmount, firstFiscalPeriod);
                        updateCategoryActualAmountsByBalance(document, balance, award, invoiceDetailAccountObjectsCodes, firstFiscalPeriod);
                    }
                }
                invoiceAccountDetail.setTotalBudget(awardAccountBudgetAmount);
                invoiceAccountDetail.setCumulativeExpenditures(awardAccountCumulativeAmount);
            }
            invoiceAccountDetails.add(invoiceAccountDetail);
            if (!ObjectUtils.isNull(locReviewDetail) && !locReviewDetail.getClaimOnCashBalance().negated().equals(locReviewDetail.getAmountToDraw()) && ArConstants.BillingFrequencyValues.isLetterOfCredit(award)) {
                distributeAmountAmongAllAccountObjectCodes(document, awardAccount, invoiceDetailAccountObjectsCodes, locReviewDetail);
            } else {
                updateInvoiceDetailAccountObjectCodesByBilledAmount(awardAccount, invoiceDetailAccountObjectsCodes);
            }
        }
        document.getAccountDetails().addAll(invoiceAccountDetails);
        if (!ArConstants.BillingFrequencyValues.isMilestone(document.getInvoiceGeneralDetail()) && !ArConstants.BillingFrequencyValues.isPredeterminedBilling(document.getInvoiceGeneralDetail())) {
            document.getInvoiceDetailAccountObjectCodes().addAll(invoiceDetailAccountObjectsCodes);
            List<AwardAccountObjectCodeTotalBilled> awardAccountObjectCodeTotalBilleds = getAwardAccountObjectCodeTotalBilledDao().getAwardAccountObjectCodeTotalBuildByProposalNumberAndAccount(awardAccounts);
            List<ContractsGrantsInvoiceDetail> invoiceDetails = generateValuesForCategories(document.getDocumentNumber(), document.getInvoiceDetailAccountObjectCodes(), budgetAmountsByCostCategory, awardAccountObjectCodeTotalBilleds);
            document.getInvoiceDetails().addAll(invoiceDetails);
        }
        populateContractsGrantsInvoiceDocument(award, document, accountDetails, locCreationType);
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) InvoiceAccountDetail(org.kuali.kfs.module.ar.businessobject.InvoiceAccountDetail) Timestamp(java.sql.Timestamp) KualiDecimal(org.kuali.kfs.core.api.util.type.KualiDecimal) ArrayList(java.util.ArrayList) List(java.util.List) InvoiceGeneralDetail(org.kuali.kfs.module.ar.businessobject.InvoiceGeneralDetail) SystemOptions(org.kuali.kfs.sys.businessobject.SystemOptions) InvoiceAddressDetail(org.kuali.kfs.module.ar.businessobject.InvoiceAddressDetail) ContractsGrantsInvoiceDetail(org.kuali.kfs.module.ar.businessobject.ContractsGrantsInvoiceDetail) BillingPeriod(org.kuali.kfs.module.ar.businessobject.BillingPeriod) InvoiceMilestone(org.kuali.kfs.module.ar.businessobject.InvoiceMilestone) Milestone(org.kuali.kfs.module.ar.businessobject.Milestone) AccountingPeriod(org.kuali.kfs.coa.businessobject.AccountingPeriod) ContractsAndGrantsBillingAwardAccount(org.kuali.kfs.integration.cg.ContractsAndGrantsBillingAwardAccount) ContractsAndGrantsBillingAgency(org.kuali.kfs.integration.cg.ContractsAndGrantsBillingAgency) AwardAccountObjectCodeTotalBilled(org.kuali.kfs.module.ar.businessobject.AwardAccountObjectCodeTotalBilled) Date(java.sql.Date) Date(java.sql.Date) ContractsGrantsLetterOfCreditReviewDetail(org.kuali.kfs.module.ar.businessobject.ContractsGrantsLetterOfCreditReviewDetail) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Balance(org.kuali.kfs.gl.businessobject.Balance) InvoiceDetailAccountObjectCode(org.kuali.kfs.module.ar.businessobject.InvoiceDetailAccountObjectCode)

Example 3 with InvoiceAddressDetail

use of org.kuali.kfs.module.ar.businessobject.InvoiceAddressDetail in project cu-kfs by CU-CommunityApps.

the class ContractsGrantsInvoiceDocumentServiceImpl method correctContractsGrantsInvoiceDocument.

@Override
public void correctContractsGrantsInvoiceDocument(ContractsGrantsInvoiceDocument document) {
    // correct Direct Cost Invoice Details.
    for (ContractsGrantsInvoiceDetail id : document.getDirectCostInvoiceDetails()) {
        correctInvoiceDetail(id);
    }
    // correct Indirect Cost Invoice Details.
    for (ContractsGrantsInvoiceDetail id : document.getIndirectCostInvoiceDetails()) {
        correctInvoiceDetail(id);
    }
    // update correction to the InvoiceAccountDetail objects
    for (InvoiceAccountDetail id : document.getAccountDetails()) {
        correctInvoiceAccountDetail(id);
    }
    // correct invoiceDetailAccountObjectCode.
    for (InvoiceDetailAccountObjectCode invoiceDetailAccountObjectCode : document.getInvoiceDetailAccountObjectCodes()) {
        invoiceDetailAccountObjectCode.correctInvoiceDetailAccountObjectCodeExpenditureAmount();
    }
    // correct Bills
    KualiDecimal totalBillingAmount = KualiDecimal.ZERO;
    for (InvoiceBill bill : document.getInvoiceBills()) {
        bill.setEstimatedAmount(bill.getEstimatedAmount().negated());
        totalBillingAmount = totalBillingAmount.add(bill.getEstimatedAmount());
    }
    // correct Milestones
    KualiDecimal totalMilestonesAmount = KualiDecimal.ZERO;
    for (InvoiceMilestone milestone : document.getInvoiceMilestones()) {
        milestone.setMilestoneAmount(milestone.getMilestoneAmount().negated());
        totalMilestonesAmount = totalMilestonesAmount.add(milestone.getMilestoneAmount());
    }
    document.getInvoiceGeneralDetail().setTotalPreviouslyBilled(getAwardBilledToDateAmountExcludingDocument(document.getInvoiceGeneralDetail().getProposalNumber(), document.getDocumentNumber()));
    if (ArConstants.BillingFrequencyValues.isMilestone(document.getInvoiceGeneralDetail()) && CollectionUtils.isNotEmpty(document.getInvoiceMilestones())) {
        document.getInvoiceGeneralDetail().setTotalAmountBilledToDate(document.getInvoiceGeneralDetail().getTotalAmountBilledToDate().add(totalMilestonesAmount));
    } else if (ArConstants.BillingFrequencyValues.isPredeterminedBilling(document.getInvoiceGeneralDetail()) && CollectionUtils.isNotEmpty(document.getInvoiceBills())) {
        document.getInvoiceGeneralDetail().setTotalAmountBilledToDate(document.getInvoiceGeneralDetail().getTotalAmountBilledToDate().add(totalBillingAmount));
    } else {
        KualiDecimal newTotalBilled = document.getTotalCostInvoiceDetail().getInvoiceAmount().add(document.getInvoiceGeneralDetail().getTotalPreviouslyBilled());
        newTotalBilled = newTotalBilled.add(getOtherTotalBilledForAwardPeriod(document));
        document.getInvoiceGeneralDetail().setTotalAmountBilledToDate(newTotalBilled);
        calculatePreviouslyBilledAmounts(document);
    }
    for (InvoiceAddressDetail invoiceAddressDetail : document.getInvoiceAddressDetails()) {
        invoiceAddressDetail.setInitialTransmissionDate(null);
        invoiceAddressDetail.setTransmittedByPrincipalId("");
        invoiceAddressDetail.setTransmissionDate(null);
        invoiceAddressDetail.setTransmissionStatusCode("");
        invoiceAddressDetail.setTransmissionCount(0);
    }
}
Also used : ContractsGrantsInvoiceDetail(org.kuali.kfs.module.ar.businessobject.ContractsGrantsInvoiceDetail) InvoiceBill(org.kuali.kfs.module.ar.businessobject.InvoiceBill) AbstractKualiDecimal(org.kuali.kfs.core.api.util.type.AbstractKualiDecimal) KualiDecimal(org.kuali.kfs.core.api.util.type.KualiDecimal) InvoiceAccountDetail(org.kuali.kfs.module.ar.businessobject.InvoiceAccountDetail) InvoiceMilestone(org.kuali.kfs.module.ar.businessobject.InvoiceMilestone) InvoiceAddressDetail(org.kuali.kfs.module.ar.businessobject.InvoiceAddressDetail) InvoiceDetailAccountObjectCode(org.kuali.kfs.module.ar.businessobject.InvoiceDetailAccountObjectCode)

Aggregations

InvoiceAddressDetail (org.kuali.kfs.module.ar.businessobject.InvoiceAddressDetail)3 ArrayList (java.util.ArrayList)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 KualiDecimal (org.kuali.kfs.core.api.util.type.KualiDecimal)2 ContractsGrantsInvoiceDetail (org.kuali.kfs.module.ar.businessobject.ContractsGrantsInvoiceDetail)2 InvoiceAccountDetail (org.kuali.kfs.module.ar.businessobject.InvoiceAccountDetail)2 InvoiceDetailAccountObjectCode (org.kuali.kfs.module.ar.businessobject.InvoiceDetailAccountObjectCode)2 InvoiceMilestone (org.kuali.kfs.module.ar.businessobject.InvoiceMilestone)2 Date (java.sql.Date)1 Timestamp (java.sql.Timestamp)1 HashMap (java.util.HashMap)1 List (java.util.List)1 AccountingPeriod (org.kuali.kfs.coa.businessobject.AccountingPeriod)1 AbstractKualiDecimal (org.kuali.kfs.core.api.util.type.AbstractKualiDecimal)1 Balance (org.kuali.kfs.gl.businessobject.Balance)1 AccountsReceivableCustomer (org.kuali.kfs.integration.ar.AccountsReceivableCustomer)1 ContractsAndGrantsBillingAgency (org.kuali.kfs.integration.cg.ContractsAndGrantsBillingAgency)1 ContractsAndGrantsBillingAwardAccount (org.kuali.kfs.integration.cg.ContractsAndGrantsBillingAwardAccount)1 AwardAccountObjectCodeTotalBilled (org.kuali.kfs.module.ar.businessobject.AwardAccountObjectCodeTotalBilled)1 BillingPeriod (org.kuali.kfs.module.ar.businessobject.BillingPeriod)1