1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.kuali.kfs.module.cam.document.service;
20
21 import static org.kuali.kfs.sys.fixture.UserNameFixture.bomiddle;
22
23 import java.text.DateFormat;
24 import java.text.SimpleDateFormat;
25 import java.util.ArrayList;
26 import java.util.Calendar;
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.Map;
30
31 import org.apache.log4j.Logger;
32 import org.kuali.kfs.module.cam.CamsPropertyConstants;
33 import org.kuali.kfs.module.cam.businessobject.AssetPayment;
34 import org.kuali.kfs.module.cam.businessobject.AssetPaymentAssetDetail;
35 import org.kuali.kfs.module.cam.businessobject.AssetPaymentDetail;
36 import org.kuali.kfs.module.cam.document.AssetPaymentDocument;
37 import org.kuali.kfs.module.cam.fixture.AssetPaymentServiceFixture;
38 import org.kuali.kfs.sys.ConfigureContext;
39 import org.kuali.kfs.sys.KFSConstants;
40 import org.kuali.kfs.sys.businessobject.FinancialSystemDocumentHeader;
41 import org.kuali.kfs.sys.context.KualiTestBase;
42 import org.kuali.kfs.sys.context.SpringContext;
43 import org.kuali.kfs.sys.service.UniversityDateService;
44 import org.kuali.rice.core.api.datetime.DateTimeService;
45 import org.kuali.rice.core.api.util.type.KualiDecimal;
46 import org.kuali.rice.kew.api.WorkflowDocument;
47 import org.kuali.rice.kew.api.document.WorkflowDocumentService;
48 import org.kuali.rice.kns.service.DataDictionaryService;
49 import org.kuali.rice.krad.bo.DocumentHeader;
50 import org.kuali.rice.krad.service.BusinessObjectService;
51 import org.kuali.rice.krad.util.GlobalVariables;
52 import org.kuali.rice.krad.util.KRADPropertyConstants;
53
54 @ConfigureContext(session = bomiddle)
55 public class AssetPaymentServiceTest extends KualiTestBase {
56 private static Logger LOG = Logger.getLogger(AssetPaymentServiceTest.class);
57
58 private DateTimeService dateTimeService;
59 private UniversityDateService universityDateService;
60 private AssetPaymentService assetPaymentService;
61 private BusinessObjectService businessObjectService;
62
63 @Override
64
65 protected void setUp() throws Exception {
66 super.setUp();
67 assetPaymentService = SpringContext.getBean(AssetPaymentService.class);
68 businessObjectService = SpringContext.getBean(BusinessObjectService.class);
69 universityDateService = SpringContext.getBean(UniversityDateService.class);
70 dateTimeService = SpringContext.getBean(DateTimeService.class);
71 }
72
73 public void testAdjustPaymentAmounts_params_false_false() throws Exception {
74 AssetPayment assetPayment = AssetPaymentServiceFixture.PAYMENT1.newAssetPayment();
75 this.assetPaymentService.adjustPaymentAmounts(assetPayment, false, false);
76 assertTrue(assetPayment.getAccountChargeAmount().isPositive());
77 assertTrue(assetPayment.getAccumulatedPrimaryDepreciationAmount().isPositive());
78 assertNotNull(assetPayment.getPeriod1Depreciation1Amount());
79 assertNotNull(assetPayment.getPeriod11Depreciation1Amount());
80
81 assertTrue(assetPayment.getPeriod11Depreciation1Amount() instanceof KualiDecimal);
82 }
83
84 public void testAdjustPaymentAmounts_params_true_true() throws Exception {
85 AssetPayment assetPayment = AssetPaymentServiceFixture.PAYMENT1.newAssetPayment();
86 this.assetPaymentService.adjustPaymentAmounts(assetPayment, true, true);
87 assertTrue(assetPayment.getAccountChargeAmount().isNegative());
88 assertTrue(assetPayment.getAccumulatedPrimaryDepreciationAmount().isNegative());
89 assertNull(assetPayment.getPeriod1Depreciation1Amount());
90 assertNull(assetPayment.getPeriod11Depreciation1Amount());
91
92 assertFalse(assetPayment.getPeriod11Depreciation1Amount() instanceof KualiDecimal);
93 }
94
95 public void testAdjustPaymentAmounts_false_true() throws Exception {
96 AssetPayment assetPayment = AssetPaymentServiceFixture.PAYMENT1.newAssetPayment();
97 this.assetPaymentService.adjustPaymentAmounts(assetPayment, false, true);
98 assertTrue(assetPayment.getAccountChargeAmount().isPositive());
99 assertTrue(assetPayment.getAccumulatedPrimaryDepreciationAmount().isPositive());
100 assertNull(assetPayment.getPeriod1Depreciation1Amount());
101 assertNull(assetPayment.getPeriod2Depreciation1Amount());
102 assertNull(assetPayment.getPeriod3Depreciation1Amount());
103 assertNull(assetPayment.getPeriod4Depreciation1Amount());
104 assertNull(assetPayment.getPeriod5Depreciation1Amount());
105 assertNull(assetPayment.getPeriod6Depreciation1Amount());
106 assertNull(assetPayment.getPeriod7Depreciation1Amount());
107 assertNull(assetPayment.getPeriod8Depreciation1Amount());
108 assertNull(assetPayment.getPeriod9Depreciation1Amount());
109 assertNull(assetPayment.getPeriod10Depreciation1Amount());
110 assertNull(assetPayment.getPeriod11Depreciation1Amount());
111 assertNull(assetPayment.getPeriod12Depreciation1Amount());
112 }
113
114 public void testProcessApprovedAssetPayment() throws Exception {
115 int detailRows=0;
116 int assetRows=0;
117
118
119 AssetPaymentDocument document = AssetPaymentServiceFixture.PAYMENT1.newAssetPaymentDocument();
120 document.setDocumentHeader(getDocumentHeader());
121 document.setAssetPaymentAllocationTypeCode(CamsPropertyConstants.AssetPaymentAllocation.ASSET_DISTRIBUTION_BY_TOTAL_COST_CODE);
122
123 KualiDecimal totalDocument = new KualiDecimal(0);
124 List<AssetPaymentAssetDetail> assetPaymentAssetDetails = document.getAssetPaymentAssetDetail();
125 List<AssetPaymentDetail> assetPaymentDetails = document.getSourceAccountingLines();
126
127
128 for(AssetPaymentDetail assetPaymentDetail:assetPaymentDetails){
129 detailRows++;
130 totalDocument = totalDocument.add(assetPaymentDetail.getAmount());
131 }
132
133 Double totalHistoricalAmount = new Double(0);
134 HashMap<Long,Double>assets = new HashMap();
135 HashMap<Long,KualiDecimal>assetsNewCost = new HashMap();
136
137 Map key;
138 for(AssetPaymentAssetDetail assetPaymentAssetDetail:assetPaymentAssetDetails) {
139 assetRows++;
140 assetPaymentAssetDetail.refreshReferenceObject(CamsPropertyConstants.AssetPaymentAssetDetail.ASSET);
141 assets.put(assetPaymentAssetDetail.getCapitalAssetNumber(), new Double(assetPaymentAssetDetail.getAsset().getTotalCostAmount().toString()));
142
143 assetPaymentAssetDetail.setPreviousTotalCostAmount(assetPaymentAssetDetail.getAsset().getTotalCostAmount());
144 totalHistoricalAmount = totalHistoricalAmount + new Double(assetPaymentAssetDetail.getAsset().getTotalCostAmount().toString());
145
146 LOG.info("***Asset:"+assetPaymentAssetDetail.getCapitalAssetNumber().toString()+" Previous Cost:"+assetPaymentAssetDetail.getAsset().getTotalCostAmount());
147 }
148
149
150 LOG.info("***Saving Document:"+document.getDocumentHeader().getDocumentNumber());
151
152
153 this.businessObjectService.save(document);
154
155
156 LOG.info("***Processing Document:"+document.getDocumentNumber());
157
158 this.assetPaymentService.processApprovedAssetPayment(document);
159
160
161
162 key = new HashMap();
163 key.put(KRADPropertyConstants.DOCUMENT_NUMBER,document.getDocumentNumber());
164
165 LOG.info("***Retrieving Document:"+document.getDocumentNumber());
166
167 document = (AssetPaymentDocument) businessObjectService.findByPrimaryKey(AssetPaymentDocument.class, key);
168 document.setAssetPaymentAllocationTypeCode(CamsPropertyConstants.AssetPaymentAllocation.ASSET_DISTRIBUTION_BY_TOTAL_COST_CODE);
169 KualiDecimal calculatedAssetNewCost;
170 KualiDecimal assetOldCost;
171
172
173
174 key = new HashMap();
175 key.put(KRADPropertyConstants.DOCUMENT_NUMBER, document.getDocumentNumber());
176 List<AssetPayment> assetPayments = (List<AssetPayment>) businessObjectService.findMatching(AssetPayment.class, key);
177
178
179 assertEquals(assetPayments.size(), (assetRows*detailRows));
180
181
182
183 for (int x = 0; x < document.getAssetPaymentAssetDetail().size(); x++) {
184 AssetPaymentAssetDetail assetPaymentAssetDetail =document.getAssetPaymentAssetDetail().get(x);
185 assetPaymentAssetDetail.refreshReferenceObject("asset");
186 Long capitalAssetNumber = assetPaymentAssetDetail.getAsset().getCapitalAssetNumber();
187
188 key = new HashMap();
189 key.put(CamsPropertyConstants.Asset.CAPITAL_ASSET_NUMBER, capitalAssetNumber);
190 key.put(KRADPropertyConstants.DOCUMENT_NUMBER, document.getDocumentNumber());
191 assetPayments = (List<AssetPayment>) businessObjectService.findMatching(AssetPayment.class, key);
192
193 calculatedAssetNewCost = new KualiDecimal(assets.get(capitalAssetNumber));
194 Double previousTotalCostAmount = assets.get(capitalAssetNumber);
195 Double percentage = (previousTotalCostAmount/totalHistoricalAmount);
196
197 for (int i = 0; i < document.getSourceAccountingLines().size(); i++) {
198 AssetPaymentDetail assetPaymentDetail =(AssetPaymentDetail)document.getSourceAccountingLines().get(i);
199 Double paymentAmount = new Double(assetPaymentDetail.getAmount().toString());
200 KualiDecimal amount = new KualiDecimal(paymentAmount.doubleValue() * percentage.doubleValue());
201 calculatedAssetNewCost = calculatedAssetNewCost.add(amount);
202
203
204
205
206
207
208
209
210
211
212
213
214
215 AssetPayment assetPayment = assetPayments.get(i);
216
217 assertEquals(assetPaymentDetail.getAccountNumber(), assetPayment.getAccountNumber());
218 assertEquals(assetPaymentDetail.getChartOfAccountsCode(), assetPayment.getChartOfAccountsCode());
219 assertEquals(assetPaymentDetail.getFinancialObjectCode(), assetPayment.getFinancialObjectCode());
220 assertEquals(amount, assetPayment.getAccountChargeAmount());
221 assertEquals(assetPaymentDetail.getPostingYear(), assetPayment.getFinancialDocumentPostingYear());
222 assertEquals(assetPaymentDetail.getPostingPeriodCode(), assetPayment.getFinancialDocumentPostingPeriodCode());
223 }
224 assetsNewCost.put(capitalAssetNumber, calculatedAssetNewCost);
225 }
226
227 for (int x = 0; x < document.getAssetPaymentAssetDetail().size(); x++) {
228 AssetPaymentAssetDetail assetPaymentAssetDetail =document.getAssetPaymentAssetDetail().get(x);
229 assetPaymentAssetDetail.refreshReferenceObject("asset");
230 Long capitalAssetNumber = assetPaymentAssetDetail.getAsset().getCapitalAssetNumber();
231
232 calculatedAssetNewCost = assetsNewCost.get(capitalAssetNumber);
233
234 assertEquals(calculatedAssetNewCost, assetPaymentAssetDetail.getAsset().getTotalCostAmount());
235 }
236 }
237
238 public DocumentHeader getDocumentHeader() throws Exception {
239 WorkflowDocument workflowDocument = SpringContext.getBean(org.kuali.rice.krad.workflow.service.WorkflowDocumentService.class).createWorkflowDocument(SpringContext.getBean(DataDictionaryService.class).getDocumentTypeNameByClass(AssetPaymentDocument.class), GlobalVariables.getUserSession().getPerson());
240 FinancialSystemDocumentHeader documentHeader = new FinancialSystemDocumentHeader();
241 documentHeader.setWorkflowDocument(workflowDocument);
242 documentHeader.setDocumentNumber(workflowDocument.getDocumentId());
243 documentHeader.setFinancialDocumentStatusCode(KFSConstants.DocumentStatusCodes.APPROVED);
244 documentHeader.setExplanation("New asset payment");
245 documentHeader.setDocumentDescription("New asset payment");
246 documentHeader.setFinancialDocumentTotalAmount(KualiDecimal.ZERO);
247 return documentHeader;
248 }
249
250
251 public void testExtractPostedDatePeriod() throws Exception {
252 Calendar currentDate = Calendar.getInstance();
253
254 java.sql.Date jsqlD;
255
256 List<AssetPaymentDetail> assetPaymentDetails = new ArrayList<AssetPaymentDetail>();
257 AssetPaymentDetail assetPaymentDetail = new AssetPaymentDetail();
258 DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
259
260 int currentYear = universityDateService.getCurrentFiscalYear();
261 int testYear = universityDateService.getCurrentFiscalYear() + 1000;
262
263 currentDate.setTime(dateFormat.parse(currentYear+"-01-01"));
264 jsqlD = new java.sql.Date(currentDate.getTime().getTime());
265
266 assetPaymentDetail.setExpenditureFinancialDocumentPostedDate(jsqlD);
267 assertEquals(assetPaymentService.extractPostedDatePeriod(assetPaymentDetail),true);
268
269
270
271 currentDate.setTime(dateFormat.parse(testYear+"-01-01"));
272 jsqlD = new java.sql.Date(currentDate.getTime().getTime());
273
274 assetPaymentDetail.setExpenditureFinancialDocumentPostedDate(jsqlD);
275 assertEquals(assetPaymentService.extractPostedDatePeriod(assetPaymentDetail),false);
276 }
277 }
278