"PAYED=?,PAYED_VALUE=?,ALREADY_PAYED=?,REAL_PAYMENT_TYPE_CODE_REG11=?,REAL_ACCOUNT_CODE_ACC02=?,"+
"ROUNDING_ACCOUNT_CODE_ACC02=?,LAST_UPDATE_USER=?,LAST_UPDATE_DATE=? "+
"WHERE COMPANY_CODE_SYS01=? AND PROGRESSIVE=?"
);
PaymentDistributionVO dVO = null;
JournalHeaderVO jhVO = null;
String creditDebitAccountCode = null;
String accountCodeTypeACC06 = null;
BigDecimal paymentValue = null;
for(int i=0;i<payDistrs.size();i++) {
dVO = (PaymentDistributionVO)payDistrs.get(i);
dVO.setProgressiveDoc27DOC28(vo.getProgressiveDOC27());
res = org.jallinone.commons.server.QueryUtilExtension.insertTable(
conn,
new UserSessionParameters(username),
dVO,
"DOC28_PAYMENT_DISTRIBUTION",
attribute2dbField,
"Y",
"N",
null,
true
);
if (res.isError()) {
throw new Exception(res.getErrorMessage());
}
paymentValue = dVO.getPaymentValueDOC28();
if (!dVO.getCurrencyCodeREG03().equals(vo.getCurrencyCodeReg03DOC27())) {
paymentValue = CurrencyConversionUtils.convertCurrencyToCurrency(
vo.getPaymentDateDOC27(),
dVO.getPaymentValueDOC28(),
vo.getCurrencyCodeReg03DOC27(),
dVO.getCurrencyCodeREG03(),
conn
);
}
pstmt2.setString(1,dVO.getPayedDOC28().booleanValue()?"Y":"N");
pstmt2.setBigDecimal(2,dVO.getAlreadyPayedDOC19().add(paymentValue));
pstmt2.setBigDecimal(3,dVO.getAlreadyPayedDOC19().add(paymentValue));
pstmt2.setString(4,vo.getPaymentTypeCodeReg11DOC27());
pstmt2.setString(5,vo.getAccountCodeAcc02DOC27());
pstmt2.setString(6,dVO.getRoundingAccountCodeAcc02DOC19());
pstmt2.setString(7,username);
pstmt2.setTimestamp(8,new java.sql.Timestamp(System.currentTimeMillis()));
pstmt2.setString(9,dVO.getCompanyCodeSys01DOC28());
pstmt2.setBigDecimal(10,dVO.getProgressiveDoc19DOC28());
pstmt2.execute();
jhVO = new JournalHeaderVO();
jhVO.setCompanyCodeSys01ACC05(vo.getCompanyCodeSys01DOC27());
if (dVO.getDocTypeDOC19().equals(ApplicationConsts.SALE_DESK_DOC_TYPE) ||
dVO.getDocTypeDOC19().equals(ApplicationConsts.SALE_INVOICE_DOC_TYPE) ||
dVO.getDocTypeDOC19().equals(ApplicationConsts.SALE_INVOICE_FROM_DN_DOC_TYPE) ||
dVO.getDocTypeDOC19().equals(ApplicationConsts.SALE_INVOICE_FROM_SD_DOC_TYPE) ||
dVO.getDocTypeDOC19().equals(ApplicationConsts.SALE_CREDIT_NOTE_DOC_TYPE) ||
dVO.getDocTypeDOC19().equals(ApplicationConsts.SALE_GENERIC_INVOICE)) {
jhVO.setAccountingMotiveCodeAcc03ACC05(ApplicationConsts.MOTIVE_INVOICE_PROCEEDS);
jhVO.setDescriptionACC05(
dVO.getDescriptionDOC19()+" - "+
t1+" "+vo.getName_1REG04()+" "+(vo.getName_2REG04()==null?"":vo.getName_2REG04())
);
// determine account codes defined for the current customer...
pstmt = conn.prepareStatement(
"select CREDIT_ACCOUNT_CODE_ACC02 from SAL07_CUSTOMERS where COMPANY_CODE_SYS01=? and PROGRESSIVE_REG04=?"
);
pstmt.setString(1,vo.getCompanyCodeSys01DOC27());
pstmt.setBigDecimal(2,vo.getProgressiveReg04DOC27());
ResultSet rset = pstmt.executeQuery();
if (!rset.next()) {
rset.close();
throw new Exception("customer not found");
}
creditDebitAccountCode = rset.getString(1);
rset.close();
pstmt.close();
accountCodeTypeACC06 = ApplicationConsts.ACCOUNT_TYPE_CUSTOMER;
}
else {
jhVO.setAccountingMotiveCodeAcc03ACC05(ApplicationConsts.MOTIVE_PURCHASE_INVOICE_PAYED);
jhVO.setDescriptionACC05(
dVO.getDescriptionDOC19()+" - "+
t2+" "+vo.getName_1REG04()+" "+(vo.getName_2REG04()==null?"":vo.getName_2REG04())
);
// determine account codes defined for the current supplier...
pstmt = conn.prepareStatement(
"select DEBIT_ACCOUNT_CODE_ACC02 from PUR01_SUPPLIERS where COMPANY_CODE_SYS01=? and PROGRESSIVE_REG04=?"
);
pstmt.setString(1,vo.getCompanyCodeSys01DOC27());
pstmt.setBigDecimal(2,vo.getProgressiveReg04DOC27());
ResultSet rset = pstmt.executeQuery();
if (!rset.next()) {
rset.close();
throw new Exception("supplier not found");
}
creditDebitAccountCode = rset.getString(1);
rset.close();
pstmt.close();
accountCodeTypeACC06 = ApplicationConsts.ACCOUNT_TYPE_SUPPLIER;
}
jhVO.setItemDateACC05(vo.getPaymentDateDOC27());
Calendar cal = Calendar.getInstance();
cal.setTime(vo.getPaymentDateDOC27());
jhVO.setItemYearACC05(new BigDecimal(cal.get(Calendar.YEAR)));
JournalRowVO jrVO = new JournalRowVO();
jrVO.setCompanyCodeSys01ACC06(jhVO.getCompanyCodeSys01ACC05());
jrVO.setAccountCodeAcc02ACC06(creditDebitAccountCode);
jrVO.setAccountCodeACC06(vo.getCustomerSupplierCodeDOC27());
jrVO.setAccountCodeTypeACC06(accountCodeTypeACC06);
if (dVO.getDocTypeDOC19().equals(ApplicationConsts.SALE_DESK_DOC_TYPE) ||
dVO.getDocTypeDOC19().equals(ApplicationConsts.SALE_INVOICE_DOC_TYPE) ||
dVO.getDocTypeDOC19().equals(ApplicationConsts.SALE_INVOICE_FROM_DN_DOC_TYPE) ||
dVO.getDocTypeDOC19().equals(ApplicationConsts.SALE_INVOICE_FROM_SD_DOC_TYPE) ||
dVO.getDocTypeDOC19().equals(ApplicationConsts.SALE_CREDIT_NOTE_DOC_TYPE) ||
dVO.getDocTypeDOC19().equals(ApplicationConsts.SALE_GENERIC_INVOICE)) {
if (dVO.getPayedDOC28().booleanValue()) {
jrVO.setCreditAmountACC06( dVO.getValueDOC19().subtract(dVO.getAlreadyPayedDOC19()) );
}
else {
jrVO.setCreditAmountACC06(paymentValue);
}
}
else {
if (dVO.getPayedDOC28().booleanValue()) {
jrVO.setDebitAmountACC06( dVO.getValueDOC19().subtract(dVO.getAlreadyPayedDOC19()) );
}
else {
jrVO.setDebitAmountACC06(paymentValue);
}
}
jrVO.setDescriptionACC06("");
jrVO.setItemYearAcc05ACC06(jhVO.getItemYearACC05());
jrVO.setProgressiveAcc05ACC06(jhVO.getProgressiveACC05());
jhVO.addJournalRow(jrVO);
jrVO = new JournalRowVO();
jrVO.setCompanyCodeSys01ACC06(jhVO.getCompanyCodeSys01ACC05());
jrVO.setAccountCodeAcc02ACC06( vo.getAccountCodeAcc02DOC27() );
jrVO.setAccountCodeACC06( vo.getAccountCodeAcc02DOC27() );
jrVO.setAccountCodeTypeACC06(ApplicationConsts.ACCOUNT_TYPE_ACCOUNT);
if (dVO.getDocTypeDOC19().equals(ApplicationConsts.SALE_DESK_DOC_TYPE) ||
dVO.getDocTypeDOC19().equals(ApplicationConsts.SALE_INVOICE_DOC_TYPE) ||
dVO.getDocTypeDOC19().equals(ApplicationConsts.SALE_INVOICE_FROM_DN_DOC_TYPE) ||
dVO.getDocTypeDOC19().equals(ApplicationConsts.SALE_INVOICE_FROM_SD_DOC_TYPE) ||
dVO.getDocTypeDOC19().equals(ApplicationConsts.SALE_CREDIT_NOTE_DOC_TYPE) ||
dVO.getDocTypeDOC19().equals(ApplicationConsts.SALE_GENERIC_INVOICE))
jrVO.setDebitAmountACC06(paymentValue);
else
jrVO.setCreditAmountACC06(paymentValue);
jrVO.setDescriptionACC06("");
jrVO.setItemYearAcc05ACC06(jhVO.getItemYearACC05());
jrVO.setProgressiveAcc05ACC06(jhVO.getProgressiveACC05());
jhVO.addJournalRow(jrVO);
// check if there is a rounding on payment...
if (dVO.getPayedDOC28().booleanValue() &&
!dVO.getAlreadyPayedDOC19().add(paymentValue).equals(dVO.getValueDOC19())) {
// another accounting movement must be performed, related to rounding...
jrVO = new JournalRowVO();
jrVO.setCompanyCodeSys01ACC06(jhVO.getCompanyCodeSys01ACC05());
jrVO.setAccountCodeAcc02ACC06( dVO.getRoundingAccountCodeAcc02DOC19() );
jrVO.setAccountCodeACC06( dVO.getRoundingAccountCodeAcc02DOC19() );
jrVO.setAccountCodeTypeACC06(ApplicationConsts.ACCOUNT_TYPE_ACCOUNT);
if (dVO.getDocTypeDOC19().equals(ApplicationConsts.SALE_DESK_DOC_TYPE) ||
dVO.getDocTypeDOC19().equals(ApplicationConsts.SALE_INVOICE_DOC_TYPE) ||
dVO.getDocTypeDOC19().equals(ApplicationConsts.SALE_INVOICE_FROM_DN_DOC_TYPE) ||
dVO.getDocTypeDOC19().equals(ApplicationConsts.SALE_INVOICE_FROM_SD_DOC_TYPE) ||
dVO.getDocTypeDOC19().equals(ApplicationConsts.SALE_CREDIT_NOTE_DOC_TYPE) ||
dVO.getDocTypeDOC19().equals(ApplicationConsts.SALE_GENERIC_INVOICE))
jrVO.setDebitAmountACC06(dVO.getValueDOC19().subtract(dVO.getAlreadyPayedDOC19().add(paymentValue)));
else
jrVO.setCreditAmountACC06(dVO.getValueDOC19().subtract(dVO.getAlreadyPayedDOC19().add(paymentValue)));
jrVO.setDescriptionACC06("");
jrVO.setItemYearAcc05ACC06(jhVO.getItemYearACC05());
jrVO.setProgressiveAcc05ACC06(jhVO.getProgressiveACC05());
jhVO.addJournalRow(jrVO);