| 1 17 package org.openbravo.erpCommon.ad_forms; 18 19 import org.openbravo.erpCommon.utility.SequenceIdData; 20 import org.openbravo.base.secureApp.VariablesSecureApp; 21 import java.math.*; 22 import java.util.*; 23 import javax.servlet.*; 24 import org.apache.log4j.Logger ; 25 import org.openbravo.database.ConnectionProvider; 27 import java.sql.Connection ; 28 import org.openbravo.data.FieldProvider; 29 30 31 32 public class DocInvoice extends AcctServer { 33 static Logger log4jDocInvoice = Logger.getLogger(DocInvoice.class); 34 35 private DocTax[] m_taxes = null; 36 private String SeqNo = "0"; 37 38 42 public DocInvoice(String AD_Client_ID){ 43 super(AD_Client_ID); 44 } 45 46 public void loadObjectFieldProvider(ConnectionProvider conn, String AD_Client_ID, String Id) throws ServletException{ 47 setObjectFieldProvider(DocInvoiceData.selectRegistro(conn, AD_Client_ID, Id)); 48 } 49 50 public boolean loadDocumentDetails(FieldProvider [] data,ConnectionProvider conn){ 51 DateDoc = data[0].getField("DateInvoiced"); 52 TaxIncluded = data[0].getField("IsTaxIncluded"); 53 C_BPartner_Location_ID = data[0].getField("C_BPartner_Location_ID"); 54 Amounts[AMTTYPE_Gross] = data[0].getField("GrandTotal"); 56 if (Amounts[AMTTYPE_Gross] == null) 57 Amounts[AMTTYPE_Gross] = "0"; 58 Amounts[AMTTYPE_Net] = data[0].getField("TotalLines"); 59 if (Amounts[AMTTYPE_Net] == null) 60 Amounts[AMTTYPE_Net] = "0"; 61 Amounts[AMTTYPE_Charge] = data[0].getField("ChargeAmt"); 62 if (Amounts[AMTTYPE_Charge] == null) 63 Amounts[AMTTYPE_Charge] = "0"; 64 65 loadDocumentType(); p_lines = loadLines(); 68 m_taxes = loadTaxes(); 69 m_debt_payments = loadDebtPayments(); 70 return true; 71 72 } 73 74 private DocLine[] loadLines(){ 75 ArrayList<Object > list = new ArrayList<Object >(); 76 DocLineInvoiceData[] data = null; 77 try{ 78 log4jDocInvoice.debug("############### groupLines = " + groupLines); 79 if (groupLines.equals("Y")) data = DocLineInvoiceData.selectTotal(this, Record_ID); 80 else data = DocLineInvoiceData.select(this, Record_ID); 81 }catch(ServletException e){ 82 log4jDocInvoice.warn(e); 83 } 84 if (data==null || data.length==0) return null; 85 for (int i=0;i<data.length;i++){ 86 String Line_ID = data[i].cInvoicelineId; 87 DocLine_Invoice docLine = new DocLine_Invoice (DocumentType, Record_ID, Line_ID); 88 docLine.loadAttributes(data[i], this); 89 String Qty = data[i].qtyinvoiced; 90 docLine.setQty(Qty); 91 String LineNetAmt = data[i].linenetamt; 92 String PriceList = data[i].pricelist; 93 docLine.setAmount (LineNetAmt, PriceList, Qty); 94 95 list.add (docLine); 96 } 97 DocLine[] dl = new DocLine[list.size()]; 99 list.toArray(dl); 100 return dl; 101 } 103 private DocTax[] loadTaxes(){ 104 ArrayList<Object > list = new ArrayList<Object >(); 105 DocInvoiceData [] data = null; 106 try{ 107 data = DocInvoiceData.select(this, Record_ID); 108 }catch(ServletException e){ 109 log4jDocInvoice.warn(e); 110 } 111 log4jDocInvoice.debug("############### Taxes.length = " + data.length); 112 for (int i=0;i<data.length;i++){ 114 String C_Tax_ID = data[i].cTaxId; 115 String name = data[i].name; 116 String rate = data[i].rate; 117 String taxBaseAmt = data[i].taxbaseamt; 118 String amount = data[i].taxamt; 119 DocTax taxLine = new DocTax(C_Tax_ID, name, rate, taxBaseAmt, amount); 121 list.add(taxLine); 122 } 123 DocTax[] tl = new DocTax[list.size()]; 125 list.toArray(tl); 126 return tl; 127 } 129 130 private DocLine_Payment[] loadDebtPayments(){ 131 ArrayList<Object > list = new ArrayList<Object >(); 132 DocInvoiceData [] data = null; 133 try{ 134 data = DocInvoiceData.selectDebtPayments(this, Record_ID); 135 }catch(ServletException e){ 136 log4jDocInvoice.warn(e); 137 } 138 log4jDocInvoice.debug("############### DebtPayments.length = " + data.length); 139 for (int i=0;i<data.length;i++){ 141 String Line_ID = data[i].cDebtPaymentId; 143 DocLine_Payment dpLine = new DocLine_Payment(DocumentType, Record_ID, Line_ID); 144 log4jDocInvoice.debug(" dpLine.m_Record_Id2 = " + data[i].cDebtPaymentId); 145 dpLine.m_Record_Id2 = data[i].cDebtPaymentId; 146 dpLine.C_Currency_ID_From = data[i].cCurrencyId; 147 dpLine.dpStatus = data[i].status; 148 dpLine.isReceipt = data[i].isreceipt; 149 dpLine.isPaid = data[i].ispaid; 150 dpLine.isManual = data[i].ismanual; 151 dpLine.WriteOffAmt = data[i].writeoffamt; 152 dpLine.Amount = data[i].amount; 153 list.add(dpLine); 154 } 155 DocLine_Payment[] tl = new DocLine_Payment[list.size()]; 157 list.toArray(tl); 158 return tl; 159 } 161 192 public Fact createFact (AcctSchema as,ConnectionProvider conn,Connection con,VariablesSecureApp vars) throws ServletException{ 193 log4jDocInvoice.debug("Starting create fact"); 194 Fact fact = new Fact(this, as, Fact.POST_Actual); 196 String Fact_Acct_Group_ID = SequenceIdData.getSequence(conn, "Fact_Acct_Group", vars.getClient()); 197 if (!as.isAccrual()) 199 return null; 200 201 202 203 if (DocumentType.equals(AcctServer.DOCTYPE_ARInvoice) || DocumentType.equals(AcctServer.DOCTYPE_ARProForma)){ 205 log4jDocInvoice.debug("Point 1"); 206 for (int i = 0;m_debt_payments!=null && i < m_debt_payments.length; i++){ 208 if(m_debt_payments[i].isReceipt.equals("Y")) fact.createLine(m_debt_payments[i], getAccountBPartner(C_BPartner_ID, as, true, m_debt_payments[i].dpStatus, conn), this.C_Currency_ID, getConvertedAmt(m_debt_payments[i].Amount,m_debt_payments[i].C_Currency_ID_From, this.C_Currency_ID,DateAcct,"",conn), "", Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType, conn); 209 else fact.createLine(m_debt_payments[i], getAccountBPartner(C_BPartner_ID, as, false, m_debt_payments[i].dpStatus, conn),this.C_Currency_ID, "", getConvertedAmt(m_debt_payments[i].Amount,m_debt_payments[i].C_Currency_ID_From, this.C_Currency_ID,DateAcct,"",conn), Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType, conn); 210 } 211 log4jDocInvoice.debug("The first create line"); 213 fact.createLine(null, getAccount(AcctServer.ACCTTYPE_Charge, as, conn),C_Currency_ID, "", getAmount(AcctServer.AMTTYPE_Charge), Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType,conn); 214 log4jDocInvoice.debug("m_taxes.length: " + m_taxes); 216 for (int i = 0;m_taxes!=null && i < m_taxes.length; i++){ 217 fact.createLine(null, m_taxes[i].getAccount(DocTax.ACCTTYPE_TaxDue, as, conn),C_Currency_ID, "", m_taxes[i].m_amount, Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType,conn); 218 } 219 if (p_lines!=null && p_lines.length>0) { 221 for (int i = 0; i < p_lines.length; i++) 222 fact.createLine(p_lines[i],((DocLine_Invoice)p_lines[i]).getAccount(ProductInfo.ACCTTYPE_P_Revenue, as, conn),this.C_Currency_ID, "", p_lines[i].getAmount(), Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType,conn); 223 } 224 FactLine[] fLines = fact.getLines(); 226 for (int i = 0; i < fLines.length; i++){ 227 if (fLines[i] != null){ 228 fLines[i].setLocationFromOrg(fLines[i].getAD_Org_ID(conn), true,conn); fLines[i].setLocationFromBPartner(C_BPartner_Location_ID, false,conn); } 231 } 232 } 233 else if (this.DocumentType.equals(AcctServer.DOCTYPE_ARCredit)){ 235 log4jDocInvoice.debug("Point 2"); 236 for (int i = 0;m_debt_payments!=null && i < m_debt_payments.length; i++){ 238 if(m_debt_payments[i].isReceipt.equals("Y")) fact.createLine(m_debt_payments[i], getAccountBPartner(C_BPartner_ID, as, true, m_debt_payments[i].dpStatus, conn), this.C_Currency_ID, "", getConvertedAmt(m_debt_payments[i].Amount,m_debt_payments[i].C_Currency_ID_From, this.C_Currency_ID,DateAcct,"",conn), Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType, conn); 239 else fact.createLine(m_debt_payments[i], getAccountBPartner(C_BPartner_ID, as, false, m_debt_payments[i].dpStatus, conn), this.C_Currency_ID, getConvertedAmt(m_debt_payments[i].Amount,m_debt_payments[i].C_Currency_ID_From, this.C_Currency_ID,DateAcct,"",conn), "", Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType, conn); 240 } 241 242 fact.createLine(null, getAccount(AcctServer.ACCTTYPE_Charge, as, conn),this.C_Currency_ID, getAmount(AcctServer.AMTTYPE_Charge), "", Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType,conn); 245 for (int i = 0;m_taxes!=null && i < m_taxes.length; i++) 247 fact.createLine(null, m_taxes[i].getAccount(DocTax.ACCTTYPE_TaxDue, as, conn),this.C_Currency_ID, m_taxes[i].getAmount(), "", Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType,conn); 248 for (int i = 0;p_lines!=null && i < p_lines.length; i++) 250 fact.createLine(p_lines[i],((DocLine_Invoice)p_lines[i]).getAccount(ProductInfo.ACCTTYPE_P_Revenue, as, conn),this.C_Currency_ID, p_lines[i].getAmount(), "", Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType,conn); 251 FactLine[] fLines = fact.getLines(); 253 for (int i = 0;fLines!=null && i < fLines.length; i++){ 254 if (fLines[i] != null){ 255 fLines[i].setLocationFromOrg(fLines[i].getAD_Org_ID(conn), true,conn); fLines[i].setLocationFromBPartner(C_BPartner_Location_ID, false,conn); } 258 } 259 } 260 else if (this.DocumentType.equals(AcctServer.DOCTYPE_APInvoice)){ 262 log4jDocInvoice.debug("Point 3"); 263 for (int i = 0;m_debt_payments!=null && i < m_debt_payments.length; i++){ 265 if(m_debt_payments[i].isReceipt.equals("Y")) fact.createLine(m_debt_payments[i], getAccountBPartner(C_BPartner_ID, as, true, m_debt_payments[i].dpStatus, conn), this.C_Currency_ID, getConvertedAmt(m_debt_payments[i].Amount,m_debt_payments[i].C_Currency_ID_From, this.C_Currency_ID,DateAcct,"",conn), "", Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType, conn); 266 else fact.createLine(m_debt_payments[i], getAccountBPartner(C_BPartner_ID, as, false, m_debt_payments[i].dpStatus, conn), this.C_Currency_ID, "", getConvertedAmt(m_debt_payments[i].Amount,m_debt_payments[i].C_Currency_ID_From, this.C_Currency_ID,DateAcct,"",conn), Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType, conn); 267 } 268 269 fact.createLine(null, getAccount(AcctServer.ACCTTYPE_Charge, as, conn),this.C_Currency_ID, getAmount(AcctServer.AMTTYPE_Charge), "", Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType,conn); 272 for (int i = 0;m_taxes!=null && i < m_taxes.length; i++) 274 fact.createLine(null, m_taxes[i].getAccount(DocTax.ACCTTYPE_TaxCredit, as, conn),this.C_Currency_ID, m_taxes[i].getAmount(), "", Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType,conn); 275 for (int i = 0;p_lines!=null && i < p_lines.length; i++) 277 fact.createLine(p_lines[i],((DocLine_Invoice)p_lines[i]).getAccount(ProductInfo.ACCTTYPE_P_Expense,as, conn),this.C_Currency_ID, p_lines[i].getAmount(), "", Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType,conn); 278 FactLine[] fLines = fact.getLines(); 280 for (int i = 0;fLines !=null && i < fLines.length; i++){ 281 if (fLines[i] != null){ 282 fLines[i].setLocationFromBPartner(C_BPartner_Location_ID, true,conn); fLines[i].setLocationFromOrg(fLines[i].getAD_Org_ID(conn), false,conn); } 285 } 286 updateProductInfo(as.getC_AcctSchema_ID(), conn,con); } 288 else if (this.DocumentType.equals(AcctServer.DOCTYPE_APCredit)){ 290 log4jDocInvoice.debug("Point 4"); 291 for (int i = 0;m_debt_payments!=null && i < m_debt_payments.length; i++){ 293 if(m_debt_payments[i].isReceipt.equals("Y")) fact.createLine(m_debt_payments[i], getAccountBPartner(C_BPartner_ID, as, true, m_debt_payments[i].dpStatus, conn), this.C_Currency_ID, getConvertedAmt(m_debt_payments[i].Amount,m_debt_payments[i].C_Currency_ID_From, this.C_Currency_ID,DateAcct,"",conn), "", Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType, conn); 294 else fact.createLine(m_debt_payments[i], getAccountBPartner(C_BPartner_ID, as, false, m_debt_payments[i].dpStatus, conn), this.C_Currency_ID, "", getConvertedAmt(m_debt_payments[i].Amount,m_debt_payments[i].C_Currency_ID_From, this.C_Currency_ID,DateAcct,"",conn), Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType, conn); 295 } 296 297 298 fact.createLine (null, getAccount(AcctServer.ACCTTYPE_Charge, as, conn),this.C_Currency_ID, getAmount(AcctServer.AMTTYPE_Charge),"", Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType,conn); 301 for (int i = 0;m_taxes!=null && i < m_taxes.length; i++) 303 fact.createLine (null, m_taxes[i].getAccount(DocTax.ACCTTYPE_TaxCredit, as, conn),this.C_Currency_ID, m_taxes[i].getAmount(),"", Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType,conn); 304 for (int i = 0;p_lines!=null && i < p_lines.length; i++) 306 fact.createLine (p_lines[i],((DocLine_Invoice)p_lines[i]).getAccount(ProductInfo.ACCTTYPE_P_Expense,as, conn),this.C_Currency_ID,p_lines[i].getAmount(),"", Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType,conn); 307 FactLine[] fLines = fact.getLines(); 309 for (int i = 0;fLines !=null && i < fLines.length; i++){ 310 if (fLines[i] != null){ 311 fLines[i].setLocationFromBPartner(C_BPartner_Location_ID, true,conn); fLines[i].setLocationFromOrg(fLines[i].getAD_Org_ID(conn), false,conn); } 314 } 315 } 316 else{ 317 log4jDocInvoice.warn("Doc_Invoice - DocumentType unknown: " + this.DocumentType); 318 fact = null; 319 } 320 SeqNo = "0"; 321 return fact; 322 } 324 325 331 private void updateProductInfo (String C_AcctSchema_ID,ConnectionProvider conn,Connection con){ 332 log4jDocInvoice.debug("updateProductInfo - C_Invoice_ID=" + this.Record_ID); 333 334 336 337 341 int no = 0; 342 try{ 343 no = DocInvoiceData.updateProductPO(con,conn,Record_ID); 344 log4jDocInvoice.debug("M_Product_PO - Updated=" + no); 345 346 no = DocInvoiceData.updateProductCosting(con,conn,C_AcctSchema_ID,Record_ID); 350 log4jDocInvoice.debug("M_Product_Costing - Updated=" + no); 351 }catch(ServletException e){ 352 log4jDocInvoice.warn(e); 353 } 354 } 356 360 public BigDecimal getBalance(){ 361 BigDecimal ZERO = new BigDecimal("0"); 362 BigDecimal retValue = ZERO; 363 StringBuffer sb = new StringBuffer (" ["); 364 retValue = retValue.add(new BigDecimal(getAmount(AcctServer.AMTTYPE_Gross))); 366 sb.append(getAmount(AcctServer.AMTTYPE_Gross)); 367 retValue = retValue.subtract(new BigDecimal(getAmount(AcctServer.AMTTYPE_Charge))); 369 sb.append("-").append(getAmount(AcctServer.AMTTYPE_Charge)); 370 for (int i = 0; i < m_taxes.length; i++){ 372 retValue = retValue.subtract(new BigDecimal(m_taxes[i].getAmount())); 373 sb.append("-").append(m_taxes[i].getAmount()); 374 } 375 for (int i = 0; i < p_lines.length; i++){ 377 retValue = retValue.subtract(new BigDecimal(p_lines[i].getAmount())); 378 sb.append("-").append(p_lines[i].getAmount()); 379 } 380 sb.append("]"); 381 log4jDocInvoice.debug("Balance=" + retValue + sb.toString()); 383 return retValue; 384 } 386 public String nextSeqNo(String oldSeqNo){ 387 log4jDocInvoice.debug("DocInvoice - oldSeqNo = " + oldSeqNo); 388 BigDecimal seqNo = new BigDecimal(oldSeqNo); 389 SeqNo = (seqNo.add(new BigDecimal("10"))).toString(); 390 log4jDocInvoice.debug("DocInvoice - nextSeqNo = " + SeqNo); 391 return SeqNo; 392 } 393 394 400 public final Account getAccountBPartner(String cBPartnerId, AcctSchema as,boolean isReceipt, String dpStatus, ConnectionProvider conn){ 401 DocPaymentData [] data=null; 402 try{ 403 if (log4j.isDebugEnabled()) log4j.debug("DocInvoice - getAccountBPartner - DocumentType = " + DocumentType); 404 if (isReceipt){ 405 data = DocPaymentData.selectBPartnerCustomerAcct(conn, cBPartnerId, as.getC_AcctSchema_ID(), dpStatus); 406 }else{ 407 data = DocPaymentData.selectBPartnerVendorAcct(conn, cBPartnerId, as.getC_AcctSchema_ID(), dpStatus); 408 } 409 }catch(ServletException e){ 410 log4j.warn(e); 411 } 412 String Account_ID = ""; 414 if (data != null && data.length!=0){ 415 Account_ID = data[0].accountId; 416 }else return null; 417 if (Account_ID.equals("")){ 419 log4j.warn("DocInvoice - getAccountBPartner - NO account BPartner=" 420 + cBPartnerId + ", Record=" + Record_ID+", status "+dpStatus); 421 return null; 422 } 423 Account acct = null; 425 try{ 426 acct = Account.getAccount(conn, Account_ID); 427 }catch(ServletException e){ 428 log4j.warn(e); 429 } 430 return acct; 431 } 433 434 438 public boolean getDocumentConfirmation(ConnectionProvider conn, String strRecordId) { 439 return true; 440 } 441 442 public String getServletInfo() { 443 return "Servlet for the accounting"; 444 } } 446 | Popular Tags |