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.purap.fixture;
20  
21  import java.math.BigDecimal;
22  import java.util.ArrayList;
23  import java.util.List;
24  
25  import org.kuali.kfs.module.purap.PurapConstants;
26  import org.kuali.kfs.module.purap.businessobject.CreditMemoAccount;
27  import org.kuali.kfs.module.purap.businessobject.CreditMemoItem;
28  import org.kuali.kfs.module.purap.businessobject.PaymentRequestAccount;
29  import org.kuali.kfs.module.purap.businessobject.PaymentRequestItem;
30  import org.kuali.kfs.module.purap.businessobject.PurApAccountingLine;
31  import org.kuali.kfs.module.purap.businessobject.PurApItem;
32  import org.kuali.kfs.module.purap.businessobject.PurchasingItem;
33  import org.kuali.kfs.module.purap.businessobject.RequisitionAccount;
34  import org.kuali.kfs.module.purap.businessobject.RequisitionItem;
35  import org.kuali.kfs.module.purap.document.PaymentRequestDocument;
36  import org.kuali.kfs.module.purap.document.RequisitionDocument;
37  import org.kuali.kfs.module.purap.document.VendorCreditMemoDocument;
38  import org.kuali.kfs.sys.businessobject.SourceAccountingLine;
39  import org.kuali.kfs.sys.fixture.AccountingLineFixture;
40  import org.kuali.rice.core.api.util.type.KualiDecimal;
41  
42  public enum PurapAccountingServiceFixture {
43  
44      PREQ_PRORATION_ONE_ACCOUNT(
45              PurapTestConstants.AmountsLimits.SMALL_POSITIVE_AMOUNT,PurapConstants.PRORATION_SCALE,PaymentRequestAccount.class,
46              PurApAccountingLineFixture.BASIC_ACCOUNT_1),
47      PREQ_PRORATION_TWO_ACCOUNTS(
48              PurapTestConstants.AmountsLimits.SMALL_POSITIVE_AMOUNT,PurapConstants.PRORATION_SCALE,PaymentRequestAccount.class,
49              PurApAccountingLineFixture.ACCOUNT_50_PERCENT,
50              PurApAccountingLineFixture.ACCOUNT_50_PERCENT),
51      PREQ_PRORATION_THIRDS(
52              PurapTestConstants.AmountsLimits.SMALL_POSITIVE_AMOUNT,PurapConstants.PRORATION_SCALE,PaymentRequestAccount.class,
53              PurApAccountingLineFixture.ACCOUNT_ONE_THIRD,
54              PurApAccountingLineFixture.ACCOUNT_ONE_THIRD,
55              PurApAccountingLineFixture.ACCOUNT_ONE_THIRD_PLUS_ONE_HUNDREDTH),
56      PREQ_PRORATION_ONE_ACCOUNT_ZERO_TOTAL(
57              PurapTestConstants.AmountsLimits.ZERO,PurapConstants.PRORATION_SCALE,PaymentRequestAccount.class,
58              PurApAccountingLineFixture.BASIC_ACCOUNT_1),
59      PREQ_PRORATION_TWO_ACCOUNTS_ZERO_TOTAL(
60              PurapTestConstants.AmountsLimits.ZERO,PurapConstants.PRORATION_SCALE,PaymentRequestAccount.class,
61              PurApAccountingLineFixture.ACCOUNT_50_PERCENT,
62              PurApAccountingLineFixture.ACCOUNT_50_PERCENT),
63      PREQ_PRORATION_THIRDS_ZERO_TOTAL(
64              PurapTestConstants.AmountsLimits.ZERO,PurapConstants.PRORATION_SCALE,PaymentRequestAccount.class,
65              PurApAccountingLineFixture.ACCOUNT_ONE_THIRD,
66              PurApAccountingLineFixture.ACCOUNT_ONE_THIRD,
67              PurApAccountingLineFixture.ACCOUNT_ONE_THIRD_PLUS_ONE_HUNDREDTH),
68      REQ_PRORATION_ONE_ACCOUNT(
69              PurapTestConstants.AmountsLimits.SMALL_POSITIVE_AMOUNT,PurapConstants.PRORATION_SCALE,RequisitionAccount.class,
70              PurApAccountingLineFixture.BASIC_ACCOUNT_1),
71      REQ_PRORATION_TWO_ACCOUNTS(
72              PurapTestConstants.AmountsLimits.SMALL_POSITIVE_AMOUNT,PurapConstants.PRORATION_SCALE,RequisitionAccount.class,
73              PurApAccountingLineFixture.ACCOUNT_50_PERCENT,
74              PurApAccountingLineFixture.ACCOUNT_50_PERCENT),
75      REQ_PRORATION_THIRDS(
76              PurapTestConstants.AmountsLimits.SMALL_POSITIVE_AMOUNT,PurapConstants.PRORATION_SCALE,RequisitionAccount.class,
77              PurApAccountingLineFixture.ACCOUNT_ONE_THIRD,
78              PurApAccountingLineFixture.ACCOUNT_ONE_THIRD,
79              PurApAccountingLineFixture.ACCOUNT_ONE_THIRD_PLUS_ONE_HUNDREDTH),
80      REQ_PRORATION_ONE_ACCOUNT_ZERO_TOTAL(
81              PurapTestConstants.AmountsLimits.ZERO,PurapConstants.PRORATION_SCALE,RequisitionAccount.class,
82              PurApAccountingLineFixture.BASIC_ACCOUNT_1),
83      REQ_PRORATION_TWO_ACCOUNTS_ZERO_TOTAL(
84              PurapTestConstants.AmountsLimits.ZERO,PurapConstants.PRORATION_SCALE,RequisitionAccount.class,
85              PurApAccountingLineFixture.ACCOUNT_50_PERCENT,
86              PurApAccountingLineFixture.ACCOUNT_50_PERCENT),
87      REQ_PRORATION_THIRDS_ZERO_TOTAL(
88              PurapTestConstants.AmountsLimits.ZERO,PurapConstants.PRORATION_SCALE,RequisitionAccount.class,
89              PurApAccountingLineFixture.ACCOUNT_ONE_THIRD,
90              PurApAccountingLineFixture.ACCOUNT_ONE_THIRD,
91              PurApAccountingLineFixture.ACCOUNT_ONE_THIRD_PLUS_ONE_HUNDREDTH),
92      CREDIT_MEMO_ONE_ACCOUNT(
93              PurapTestConstants.AmountsLimits.SMALL_POSITIVE_AMOUNT,PurapConstants.PRORATION_SCALE,CreditMemoAccount.class,
94              PurApAccountingLineFixture.BASIC_ACCOUNT_1),
95      REQ_SUMMARY_ONE_ITEM_ONE_ACCOUNT( new RequisitionItemFixture[] { RequisitionItemFixture.REQ_QTY_UNRESTRICTED_ITEM_1 },
96              new AccountingLineFixture[] { AccountingLineFixture.PURAP_LINE1 },
97              new Integer[] {0} ),
98      REQ_SUMMARY_ONE_ITEM_TWO_ACCOUNTS( new RequisitionItemFixture[] { RequisitionItemFixture.REQ_QTY_UNRESTRICTED_ITEM_1 },
99              new AccountingLineFixture[] { AccountingLineFixture.PURAP_LINE1,
100                                           AccountingLineFixture.PURAP_LINE2},
101             new Integer[] {0,0} ),
102     REQ_SUMMARY_TWO_ITEMS_ONE_ACCOUNT( new RequisitionItemFixture[] { RequisitionItemFixture.REQ_QTY_UNRESTRICTED_ITEM_1,
103                                                                       RequisitionItemFixture.REQ_QTY_UNRESTRICTED_ITEM_2 },
104             new AccountingLineFixture[] { AccountingLineFixture.PURAP_LINE1 },
105             new Integer[] {0,1} ),
106             ;
107 
108     KualiDecimal totalAmount;
109     Integer percentScale;
110     Class accountClass;
111     List<SourceAccountingLine> sourceAccountingLineList = new ArrayList<SourceAccountingLine>();
112     List<PurApAccountingLine> purApAccountingLineList = new ArrayList<PurApAccountingLine>();
113     AccountingLineFixture accountingLineFixture[] = {
114             AccountingLineFixture.PURAP_LINE1,
115             AccountingLineFixture.PURAP_LINE2,
116             AccountingLineFixture.PURAP_LINE3};
117     List<PurApItem> items = new ArrayList<PurApItem>();
118 
119     /**
120      * Constructs a PurapAccountingServiceFixture.java.
121      *
122      * @param totalAmount
123      * @param percentScale
124      * @param accountClass
125      * @param purApAcctLineFixtures
126      */
127     private PurapAccountingServiceFixture(
128             KualiDecimal totalAmount,
129             Integer percentScale,
130             Class accountClass,
131             PurApAccountingLineFixture ... purApAcctLineFixtures) {
132 
133         this.totalAmount = totalAmount;
134         this.percentScale = percentScale;
135         this.accountClass = accountClass;
136 
137         for( int i = 0; i < purApAcctLineFixtures.length; i++ ) {
138             PurApAccountingLineFixture purApAcctLineFixture = purApAcctLineFixtures[i];
139             PurApAccountingLine purApAcctLine = purApAcctLineFixture.createPurApAccountingLine(accountClass, accountingLineFixture[i]);
140             BigDecimal pct = purApAcctLine.getAccountLinePercent();
141             pct = pct.divide(new BigDecimal(100));
142             purApAcctLine.setAmount(totalAmount.multiply(new KualiDecimal(pct.setScale(KualiDecimal.SCALE, KualiDecimal.ROUND_BEHAVIOR))));
143             purApAccountingLineList.add(purApAcctLine);
144             sourceAccountingLineList.add(purApAcctLine.generateSourceAccountingLine());
145         }
146     }
147 
148     /**
149      * Constructs a PurapAccountingServiceFixture.java.
150      *
151      * @param itemFixtures
152      */
153     private PurapAccountingServiceFixture(RequisitionItemFixture[] itemFixtures, AccountingLineFixture[] acctLineFixtures, Integer[] positions) {
154         for(RequisitionItemFixture itemFixture : itemFixtures) {
155             PurchasingItem item = itemFixture.createRequisitionItem();
156             items.add(item);
157         }
158         for( int i = 0; i < acctLineFixtures.length; i++ ) {
159             AccountingLineFixture acctLineFixture = acctLineFixtures[i];
160             SourceAccountingLine sourceLine = null;
161             try {
162                 sourceLine = acctLineFixture.createSourceAccountingLine();
163                 sourceAccountingLineList.add(sourceLine);
164                 PurApAccountingLine purApAccountingLine = PurApAccountingLineFixture.BASIC_ACCOUNT_1.createPurApAccountingLine(RequisitionAccount.class, acctLineFixture);
165                 if(positions[i] != null) {
166                     items.get(positions[i].intValue()).getSourceAccountingLines().add(purApAccountingLine);
167                 }
168             }
169             catch (Exception e) {
170                 e.printStackTrace();
171             }
172         }
173     }
174 
175     /**
176      * Adds an identical total and a list of source accounting lines to each item of a given RequisitionDocument.
177      *
178      * @param   req     A RequisitionDocument
179      * @return          The same document, with the totals and source accounting line lists added to its items
180      */
181     private RequisitionDocument augmentRequisitionDocument(RequisitionDocument req) {
182         List<RequisitionItem> augmentedItems = new ArrayList<RequisitionItem>();
183         for(RequisitionItem item : (List<RequisitionItem>)req.getItems()) {
184             item.setTotalAmount(this.totalAmount);
185 
186             // fix amounts
187             for (PurApAccountingLine purApAccountingLine: purApAccountingLineList) {
188                 purApAccountingLine.setAmount(item.calculateExtendedPrice().multiply(new KualiDecimal(purApAccountingLine.getAccountLinePercent())).divide(new KualiDecimal(100)));
189             }
190 
191             item.setSourceAccountingLines(purApAccountingLineList);
192             augmentedItems.add(item);
193         }
194         req.setItems(augmentedItems);
195         return req;
196     }
197 
198     /**
199      * Creates a minimal RequisitionDocument populated with one item with the current total amount and set of
200      * source accounting lines.
201      *
202      * @return  A RequisitionDocument with one, populated item
203      */
204     public RequisitionDocument generateRequisitionDocument_OneItem() {
205         RequisitionDocument req = RequisitionDocumentFixture.REQ_ONLY_REQUIRED_FIELDS.createRequisitionDocument();
206         return augmentRequisitionDocument(req);
207     }
208 
209     /**
210      * Creates a minimal RequisitionDocument populated with two items, each with the current total amount and
211      * set of source accounting lines.
212      *
213      * @return  A RequisitionDocument with two, populated items
214      */
215     public RequisitionDocument generateRequisitionDocument_TwoItems() {
216         RequisitionDocument req = RequisitionDocumentFixture.REQ_TWO_ITEMS.createRequisitionDocument();
217         return augmentRequisitionDocument(req);
218     }
219 
220     /**
221      * Adds an identical total and a list of source accounting lines to each item of a given PaymentRequestDocument.
222      *
223      * @param preq      A PaymentRequestDocument
224      * @return          The same document, with the totals and source accounting line lists add to its items
225      */
226     private PaymentRequestDocument augmentPaymentRequestDocument(PaymentRequestDocument preq) {
227         List<PaymentRequestItem> augmentedItems = new ArrayList<PaymentRequestItem>();
228         for(PaymentRequestItem item : (List<PaymentRequestItem>)preq.getItems()) {
229             item.setTotalAmount(this.totalAmount);
230             item.setSourceAccountingLines(purApAccountingLineList);
231             augmentedItems.add(item);
232         }
233         preq.setItems(augmentedItems);
234         return preq;
235     }
236 
237     /**
238      * Creates a minimal PaymentRequestDocument populated with one item with the current total amount and set of
239      * source accounting lines.
240      *
241      * @return  A PaymentRequestDocument with one, populated item
242      */
243     public PaymentRequestDocument generatePaymentRequestDocument_OneItem() {
244         PaymentRequestDocument preq = PaymentRequestDocumentFixture.PREQ_ONLY_REQUIRED_FIELDS.createPaymentRequestDocument();
245         return augmentPaymentRequestDocument(preq);
246     }
247 
248     /**
249      * Creates a minimal PaymentRequestDocument populated with two items, each with the current total amount and
250      * set of source accounting lines.
251      *
252      * @return  A PaymentRequestDocument with two, populated items
253      */
254     public PaymentRequestDocument generatePaymentRequestDocument_TwoItems() {
255         PaymentRequestDocument preq = PaymentRequestDocumentFixture.PREQ_TWO_ITEM.createPaymentRequestDocument();
256         return augmentPaymentRequestDocument(preq);
257     }
258 
259 
260     private VendorCreditMemoDocument augmentVendorCreditMemoDocument(VendorCreditMemoDocument vcm) {
261         List<CreditMemoItem> augmentedItems = new ArrayList<CreditMemoItem>();
262         for(CreditMemoItem item : (List<CreditMemoItem>)vcm.getItems()) {
263             item.setTotalAmount(this.totalAmount);
264             item.setSourceAccountingLines(purApAccountingLineList);
265             augmentedItems.add(item);
266         }
267         vcm.setItems(augmentedItems);
268         return vcm;
269     }
270 
271     public VendorCreditMemoDocument generateVendorCreditMemoDocument_OneItem() {
272         VendorCreditMemoDocument vcm = CreditMemoDocumentFixture.CM_ONLY_REQUIRED_FIELDS.createCreditMemoDocument();
273         return augmentVendorCreditMemoDocument(vcm);
274     }
275 
276     public KualiDecimal getTotalAmount() {
277         return totalAmount;
278     }
279 
280     public void setTotalAmount(KualiDecimal totalAmount) {
281         this.totalAmount = totalAmount;
282     }
283 
284     public Integer getPercentScale() {
285         return percentScale;
286     }
287 
288     public void setPercentScale(Integer percentScale) {
289         this.percentScale = percentScale;
290     }
291 
292     public Class getAccountClass() {
293         return accountClass;
294     }
295 
296     public void setAccountClass(Class accountClass) {
297         this.accountClass = accountClass;
298     }
299 
300     public List<SourceAccountingLine> getSourceAccountingLineList() {
301         return sourceAccountingLineList;
302     }
303 
304     public void setSourceAccountingLineList(List<SourceAccountingLine> sourceAccountingLineList) {
305         this.sourceAccountingLineList = sourceAccountingLineList;
306     }
307 
308     public List<PurApAccountingLine> getPurApAccountingLineList() {
309         return purApAccountingLineList;
310     }
311 
312     public void setPurApAccountingLineList(List<PurApAccountingLine> purApAccountingLineList) {
313         this.purApAccountingLineList = purApAccountingLineList;
314     }
315 
316     public List<PurApItem> getItems() {
317         return items;
318     }
319 
320     public void setItems(List<PurApItem> items) {
321         this.items = items;
322     }
323 
324 }