View Javadoc
1   /*
2    * The Kuali Financial System, a comprehensive financial management system for higher education.
3    * 
4    * Copyright 2005-2014 The Kuali Foundation
5    * 
6    * This program is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU Affero General Public License as
8    * published by the Free Software Foundation, either version 3 of the
9    * License, or (at your option) any later version.
10   * 
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU Affero General Public License for more details.
15   * 
16   * You should have received a copy of the GNU Affero General Public License
17   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  package org.kuali.kfs.module.ar.fixture;
20  
21  import org.apache.commons.lang.StringUtils;
22  import org.kuali.kfs.module.ar.businessobject.AccountsReceivableDocumentHeader;
23  import org.kuali.kfs.module.ar.businessobject.CustomerAddress;
24  import org.kuali.kfs.module.ar.businessobject.CustomerInvoiceDetail;
25  import org.kuali.kfs.module.ar.document.CustomerInvoiceDocument;
26  import org.kuali.kfs.module.ar.document.service.AccountsReceivablePendingEntryService;
27  import org.kuali.kfs.module.ar.document.service.CustomerAddressService;
28  import org.kuali.kfs.module.ar.document.service.CustomerInvoiceDocumentService;
29  import org.kuali.kfs.sys.DocumentTestUtils;
30  import org.kuali.kfs.sys.context.SpringContext;
31  import org.kuali.rice.kew.api.WorkflowDocument;
32  import org.kuali.rice.kew.api.exception.WorkflowException;
33  import org.kuali.rice.krad.bo.DocumentHeader;
34  import org.kuali.rice.krad.service.DocumentService;
35  import org.kuali.rice.krad.util.ObjectUtils;
36  
37  public enum CustomerInvoiceDocumentFixture {
38  
39      BASE_CIDOC_NO_CUSTOMER(null, // customerNumber
40              "UA", // processingChartOfAccountsCode
41              "VPIT", // processingOrganizationCode
42              null,
43              null, //billByChartOfAccountsCode
44              null //billedByOrganizationCode
45      ),
46  
47      BASE_CIDOC_WITH_CUSTOMER("ABB2", // customerNumber
48              "UA", // processingChartOfAccountsCode
49              "VPIT", // processingOrganizationCode
50              null, null, null),
51  
52      BASE_CIDOC_WITH_CUSTOMER_WITH_BILLING_INFO("ABB2", // customerNumber
53              "UA", // processingChartOfAccountsCode
54              "VPIT", // processingOrganizationCode
55              null,
56              "UA", //billByChartOfAccountsCode
57              "VPIT" //billedByOrganizationCode
58      ),
59  
60      REVERSAL_CIDOC("ABB2", // customerNumber
61              "UA", // processingChartOfAccountsCode
62              "VPIT", // processingOrganizationCode
63              "123456", null, null);
64  
65      public String customerNumber;
66      public String processingChartOfAccountsCode;
67      public String processingOrganizationCode;
68      public String financialDocumentInErrorNumber;
69      public String billByChartOfAccountsCode;
70      public String billedByOrganizationCode;
71  
72      private CustomerInvoiceDocumentFixture( String customerNumber, String processingChartOfAccountsCode, String processingOrganizationCode, String financialDocumentInErrorNumber, String billByChartOfAccountsCode, String billedByOrganizationCode ){
73          this.customerNumber = customerNumber;
74          this.processingOrganizationCode = processingOrganizationCode;
75          this.processingChartOfAccountsCode = processingChartOfAccountsCode;
76          this.financialDocumentInErrorNumber = financialDocumentInErrorNumber;
77          this.billByChartOfAccountsCode = billByChartOfAccountsCode;
78          this.billedByOrganizationCode = billedByOrganizationCode;
79      }
80  
81      /**
82       * This method creates a customer invoice document based on the passed in customer fixture and customer invoice detail fixture array
83       *
84       * @param customerFixture
85       * @param customerInvoiceDetailFixtures
86       * @return
87       */
88      public CustomerInvoiceDocument createCustomerInvoiceDocument(CustomerFixture customerFixture, CustomerInvoiceDetailFixture[] customerInvoiceDetailFixtures) throws WorkflowException {
89  
90          CustomerInvoiceDocument customerInvoiceDocument = createCustomerInvoiceDocument( customerInvoiceDetailFixtures );
91          customerInvoiceDocument.getAccountsReceivableDocumentHeader().setCustomerNumber(customerFixture.customerNumber);
92          return customerInvoiceDocument;
93      }
94  
95  
96      /**
97       * This method creates a customer invoice document based on the passed in customer fixture and customer invoice detail fixture array
98       *
99       * @param customerInvoiceDetailFixtures
100      * @return
101      */
102     public CustomerInvoiceDocument createCustomerInvoiceDocument(CustomerInvoiceDetailFixture[] customerInvoiceDetailFixtures) throws WorkflowException {
103 
104         CustomerInvoiceDocument customerInvoiceDocument = null;
105         try {
106             customerInvoiceDocument = DocumentTestUtils.createDocument(SpringContext.getBean(DocumentService.class), CustomerInvoiceDocument.class);
107         }
108         catch (WorkflowException e) {
109             throw new RuntimeException("Document creation failed.");
110         }
111 
112         // Just verify the workflow pieces
113         DocumentHeader documentHeader = customerInvoiceDocument.getDocumentHeader();
114         WorkflowDocument workflowDocument = documentHeader.getWorkflowDocument();
115 
116         //probably should change this to use values set in fixture
117         SpringContext.getBean(CustomerInvoiceDocumentService.class).setupDefaultValuesForNewCustomerInvoiceDocument(customerInvoiceDocument);
118 
119         customerInvoiceDocument.getFinancialSystemDocumentHeader().setFinancialDocumentInErrorNumber(financialDocumentInErrorNumber);
120         if( StringUtils.isNotEmpty(billByChartOfAccountsCode)){
121             customerInvoiceDocument.setBillByChartOfAccountCode(billByChartOfAccountsCode);
122         }
123         if( StringUtils.isNotEmpty(billedByOrganizationCode)){
124             customerInvoiceDocument.setBilledByOrganizationCode(billedByOrganizationCode);
125         }
126 
127         //  this is a little hacky, as some of these dont even have a customer attached,
128         // but these are required fields now
129         //customerInvoiceDocument.setCustomerBillToAddressIdentifier(1);
130         //customerInvoiceDocument.setCustomerShipToAddressIdentifier(1);
131 
132         //set AR doc Header
133         AccountsReceivableDocumentHeader arDocHeader = null;
134 
135         if(ObjectUtils.isNull(customerInvoiceDocument.getAccountsReceivableDocumentHeader())) {
136             arDocHeader = new AccountsReceivableDocumentHeader();
137             customerInvoiceDocument.setAccountsReceivableDocumentHeader(arDocHeader);
138         } else {
139             arDocHeader = customerInvoiceDocument.getAccountsReceivableDocumentHeader();
140         }
141         arDocHeader.setCustomerNumber(customerNumber);
142         arDocHeader.setProcessingChartOfAccountCode( processingChartOfAccountsCode );
143         arDocHeader.setProcessingOrganizationCode( processingOrganizationCode );
144         arDocHeader.setDocumentNumber(customerInvoiceDocument.getDocumentNumber());
145         //customerInvoiceDocument.setAccountsReceivableDocumentHeader(arDocHeader);
146 
147         // refresh to set the Customer on the arDocHeader so setaddressoninvoice doesn't throw an NPE
148         arDocHeader.refresh();
149 
150         CustomerAddressService customerAddressService = SpringContext.getBean(CustomerAddressService.class);
151         CustomerAddress customerShipToAddress = customerAddressService.getPrimaryAddress(customerNumber);
152         CustomerAddress customerBillToAddress = customerShipToAddress;
153         if (ObjectUtils.isNotNull(customerShipToAddress)) {
154             customerInvoiceDocument.setCustomerShipToAddress(customerShipToAddress);
155             customerInvoiceDocument.setCustomerShipToAddressOnInvoice(customerShipToAddress);
156             customerInvoiceDocument.setCustomerShipToAddressIdentifier(customerShipToAddress.getCustomerAddressIdentifier());
157 
158             customerInvoiceDocument.setCustomerBillToAddress(customerBillToAddress);
159             customerInvoiceDocument.setCustomerBillToAddressOnInvoice(customerBillToAddress);
160             customerInvoiceDocument.setCustomerBillToAddressIdentifier(customerBillToAddress.getCustomerAddressIdentifier());
161         }
162 
163         AccountsReceivablePendingEntryService accountsReceivablePendingEntryService = SpringContext.getBean(AccountsReceivablePendingEntryService.class);
164 
165         //associated customer invoice detail fixtures with invoice document
166         if ( customerInvoiceDetailFixtures != null ){
167             for (CustomerInvoiceDetailFixture customerInvoiceDetailFixture : customerInvoiceDetailFixtures) {
168                 CustomerInvoiceDetail detail = customerInvoiceDetailFixture.addTo(customerInvoiceDocument);
169                 // FIXME Set the accountsReceivableObjectCode
170                 String accountsReceivableObjectCode = accountsReceivablePendingEntryService.getAccountsReceivableObjectCode(detail);
171                 detail.setAccountsReceivableObjectCode(accountsReceivableObjectCode);
172             }
173         }
174 
175         //customerInvoiceDocument.refreshReferenceObject("paymentFinancialObject");
176         //customerInvoiceDocument.getPaymentFinancialObject().refresh();
177 
178         SpringContext.getBean(DocumentService.class).saveDocument(customerInvoiceDocument);
179         customerInvoiceDocument = (CustomerInvoiceDocument) SpringContext.getBean(DocumentService.class).getByDocumentHeaderId(customerInvoiceDocument.getDocumentNumber());
180         return customerInvoiceDocument;
181     }
182 }