1 17 package org.openbravo.erpCommon.ad_forms; 18 19 20 import org.openbravo.erpCommon.utility.*; 21 import org.openbravo.base.secureApp.HttpSecureAppServlet; 22 import org.openbravo.base.secureApp.VariablesSecureApp; 23 import org.openbravo.exception.*; 24 import java.math.*; 25 import java.sql.*; 26 import java.io.*; 27 import java.util.*; 28 import javax.servlet.*; 29 import org.apache.log4j.Logger; 30 import org.openbravo.erpCommon.utility.DateTimeData; 31 import org.openbravo.database.ConnectionProvider; 33 import org.openbravo.data.FieldProvider; 34 35 36 37 public abstract class AcctServer extends HttpSecureAppServlet { 38 static Logger log4j = Logger.getLogger(AcctServer.class); 39 public String batchSize = "100"; 40 41 public BigDecimal ZERO = new BigDecimal("0"); 42 43 public String groupLines = ""; 44 public String Qty = null; 45 public static String tableName = ""; 46 public static String strDateColumn = ""; 47 public static String AD_Table_ID = ""; 48 public String AD_Client_ID = ""; 49 public String AD_Org_ID = ""; 50 public String Status = ""; 51 public String C_BPartner_ID = ""; 52 public String C_BPartner_Location_ID = ""; 53 public String M_Product_ID = ""; 54 public String AD_OrgTrx_ID = ""; 55 public String C_SalesRegion_ID = ""; 56 public String C_Project_ID = ""; 57 public String C_Campaign_ID = ""; 58 public String C_Activity_ID = ""; 59 public String C_LocFrom_ID = ""; 60 public String C_LocTo_ID = ""; 61 public String User1_ID = ""; 62 public String User2_ID = ""; 63 public String Name = ""; 64 public String DocumentNo = ""; 65 public String DateAcct = ""; 66 public String DateDoc = ""; 67 public String C_Period_ID = ""; 68 public String C_Currency_ID = ""; 69 public String C_DocType_ID = ""; 70 public String C_Charge_ID = ""; 71 public String ChargeAmt = ""; 72 public String C_BankAccount_ID = ""; 73 public String C_CashBook_ID = ""; 74 public String M_Warehouse_ID = ""; 75 public String Posted = ""; 76 public String DocumentType = ""; 77 public String TaxIncluded = ""; 78 public String GL_Category_ID = ""; 79 public String GL_Budget_ID = ""; 80 public String Record_ID = ""; 81 82 protected static final String NO_CURRENCY = "-1"; 83 public String m_IsOpening = "N"; 85 86 public Fact[] m_fact = null; 87 public AcctSchema[] m_as = null; 88 89 private FieldProvider objectFieldProvider []; 90 91 public String [] Amounts = new String [4]; 92 93 public DocLine[] p_lines = new DocLine[0]; 94 public DocLine_Payment[] m_debt_payments = new DocLine_Payment[0]; 95 96 98 public boolean MultiCurrency = false; 99 100 101 public static final int AMTTYPE_Gross = 0; 102 public static final int AMTTYPE_Net = 1; 103 public static final int AMTTYPE_Charge = 2; 104 105 public static final int AMTTYPE_Invoice = 0; 106 public static final int AMTTYPE_Allocation = 1; 107 public static final int AMTTYPE_Discount = 2; 108 public static final int AMTTYPE_WriteOff = 3; 109 110 111 public static final String STATUS_NotPosted = "N"; 112 113 public static final String STATUS_NotBalanced = "b"; 114 115 public static final String STATUS_NotConvertible = "c"; 116 117 public static final String STATUS_PeriodClosed = "p"; 118 119 public static final String STATUS_InvalidAccount = "i"; 120 121 public static final String STATUS_PostPrepared = "y"; 122 123 public static final String STATUS_Posted = "Y"; 124 125 public static final String STATUS_Error = "E"; 126 127 128 public static final String DOCTYPE_ARInvoice = "ARI"; 129 130 public static final String DOCTYPE_ARCredit = "ARC"; 131 132 public static final String DOCTYPE_ARReceipt = "STT"; 134 public static final String DOCTYPE_ARProForma = "ARF"; 135 136 137 public static final String DOCTYPE_APInvoice = "API"; 138 139 public static final String DOCTYPE_APCredit = "APC"; 140 141 public static final String DOCTYPE_APPayment = "APP"; 142 143 144 public static final String DOCTYPE_BankStatement = "CMB"; 145 146 public static final String DOCTYPE_CashJournal = "CMC"; 147 148 public static final String DOCTYPE_Allocation = "CMA"; 149 150 151 public static final String DOCTYPE_Amortization = "AMZ"; 152 153 154 public static final String DOCTYPE_MatShipment = "MMS"; 155 156 public static final String DOCTYPE_MatReceipt = "MMR"; 157 158 public static final String DOCTYPE_MatInventory = "MMI"; 159 160 public static final String DOCTYPE_MatMovement = "MMM"; 161 162 public static final String DOCTYPE_MatProduction = "MMP"; 163 164 165 public static final String DOCTYPE_MatMatchInv = "MXI"; 166 167 public static final String DOCTYPE_MatMatchPO = "MXP"; 168 169 170 public static final String DOCTYPE_GLJournal = "GLJ"; 171 172 173 public static final String DOCTYPE_POrder = "POO"; 174 175 public static final String DOCTYPE_SOrder = "SOO"; 176 177 public static final String DOCTYPE_DPManagement = "DPM"; 179 180 181 182 183 public static final String ACCTTYPE_Charge = "0"; 184 public static final String ACCTTYPE_C_Receivable = "1"; 185 public static final String ACCTTYPE_V_Liability = "2"; 186 public static final String ACCTTYPE_V_Liability_Services = "3"; 187 188 189 public static final String ACCTTYPE_UnallocatedCash = "10"; 190 public static final String ACCTTYPE_BankInTransit = "11"; 191 public static final String ACCTTYPE_PaymentSelect = "12"; 192 public static final String ACCTTYPE_WriteOffDefault = "13"; 193 public static final String ACCTTYPE_BankInTransitDefault = "14"; 194 public static final String ACCTTYPE_ConvertChargeDefaultAmt= "15"; 195 public static final String ACCTTYPE_ConvertGainDefaultAmt = "16"; 196 197 198 199 public static final String ACCTTYPE_CashAsset = "20"; 200 public static final String ACCTTYPE_CashTransfer = "21"; 201 public static final String ACCTTYPE_CashExpense = "22"; 202 public static final String ACCTTYPE_CashReceipt = "23"; 203 public static final String ACCTTYPE_CashDifference = "24"; 204 205 206 public static final String ACCTTYPE_DiscountExp = "30"; 207 public static final String ACCTTYPE_DiscountRev = "31"; 208 public static final String ACCTTYPE_WriteOff = "32"; 209 210 211 public static final String ACCTTYPE_BankAsset = "40"; 212 public static final String ACCTTYPE_InterestRev = "41"; 213 public static final String ACCTTYPE_InterestExp = "42"; 214 public static final String ACCTTYPE_ConvertChargeLossAmt= "43"; 215 public static final String ACCTTYPE_ConvertChargeGainAmt= "44"; 216 217 218 219 public static final String ACCTTYPE_InvDifferences = "50"; 220 public static final String ACCTTYPE_NotInvoicedReceipts = "51"; 221 222 223 public static final String ACCTTYPE_ProjectAsset = "61"; 224 public static final String ACCTTYPE_ProjectWIP = "62"; 225 226 227 public static final String ACCTTYPE_PPVOffset = "60"; 228 229 public String BP_C_SalesRegion_ID = ""; 232 233 234 235 int errors=0; 236 int success=0; 237 238 242 public AcctServer (String m_AD_Client_ID){ 243 AD_Client_ID = m_AD_Client_ID; 244 if (log4j.isDebugEnabled()) log4j.debug("AcctServer - LOADING ARRAY: " + m_AD_Client_ID); 245 m_as = AcctSchema.getAcctSchemaArray (this, m_AD_Client_ID); 246 } 248 public void setBatchSize(String newbatchSize) { 249 batchSize = newbatchSize; 250 } 251 252 public void run(VariablesSecureApp vars) throws IOException, ServletException{ 253 AD_Client_ID = vars.getClient(); 254 try { 255 Connection con = getTransactionConnection(); 256 String strIDs = ""; 257 if (log4j.isDebugEnabled()) log4j.debug("AcctServer - Run - TableName = " + tableName); 258 AcctServerData [] data = AcctServerData.select(this,tableName, AD_Client_ID, strDateColumn, 0, Integer.valueOf(batchSize).intValue()); 259 if(data==null)if (log4j.isDebugEnabled()) log4j.debug("AcctServer - Run -Select inicial realizada N = " + data.length + " - Key: " + data[0].id); 260 for (int i=0;data != null && i<data.length;i++){ 261 strIDs += data[i].getField("ID") + ", "; 262 if (!post(data[i].getField("ID"),false, vars,this,con)) { 263 releaseRollbackConnection(con); 264 return; 265 } 266 } 267 if (log4j.isDebugEnabled()) log4j.debug("AcctServer - Run -" + data.length + " IDs [" + strIDs + "]"); 268 releaseCommitConnection(con); 271 } catch (NoConnectionAvailableException ex) { 272 throw new ServletException("@CODE=NoConnectionAvailable"); 273 } catch (SQLException ex2) { 274 throw new ServletException("@CODE=" + Integer.toString(ex2.getErrorCode()) + "@" + ex2.getMessage()); 275 } 276 } 277 278 285 public static AcctServer get (String AD_Table_ID, String AD_Client_ID) throws ServletException{ 286 AcctServer acct = null; 287 if (log4j.isDebugEnabled()) log4j.debug("get - table: "+AD_Table_ID); 288 switch (Integer.parseInt(AD_Table_ID)){ 289 case 318: 290 acct = new DocInvoice (AD_Client_ID); 291 acct.tableName = "C_Invoice"; 292 acct.AD_Table_ID = "318"; 293 acct.strDateColumn = "DateAcct"; 294 acct.reloadAcctSchemaArray(); 295 acct.groupLines = AcctServerData.selectGroupLines(acct, AD_Client_ID); 296 break; 297 303 case 800060: 304 acct = new DocAmortization (AD_Client_ID); 305 acct.tableName = "A_Amortization"; 306 acct.AD_Table_ID = "800060"; 307 acct.strDateColumn = "DateAcct"; 308 acct.reloadAcctSchemaArray(); 309 break; 310 311 case 800176: 312 if (log4j.isDebugEnabled()) log4j.debug("AcctServer - Get DPM"); 313 acct = new DocDPManagement (AD_Client_ID); 314 acct.tableName = "C_DP_Management"; 315 acct.AD_Table_ID = "800176"; 316 acct.strDateColumn = "DateAcct"; 317 acct.reloadAcctSchemaArray(); 318 break; 319 case 407: 320 acct = new DocCash (AD_Client_ID); 321 acct.tableName = "C_Cash"; 322 acct.strDateColumn = "DateAcct"; 323 acct.AD_Table_ID = "407"; 324 acct.reloadAcctSchemaArray(); 325 break; 326 case 392: 327 acct = new DocBank (AD_Client_ID); 328 acct.tableName = "C_Bankstatement"; 329 acct.strDateColumn = "StatementDate"; 330 acct.AD_Table_ID = "392"; 331 acct.reloadAcctSchemaArray(); 332 break; 333 case 259: 334 acct = new DocOrder (AD_Client_ID); 335 acct.tableName = "C_Order"; 336 acct.strDateColumn = "DateAcct"; 337 acct.AD_Table_ID = "259"; 338 acct.reloadAcctSchemaArray(); 339 break; 340 case 800019: 341 acct = new DocPayment (AD_Client_ID); 342 acct.tableName = "C_Settlement"; 343 acct.strDateColumn = "Dateacct"; 344 acct.AD_Table_ID = "800019"; 345 acct.reloadAcctSchemaArray(); 346 break; 347 case 319: 348 acct = new DocInOut (AD_Client_ID); 349 acct.tableName = "M_InOut"; 350 acct.strDateColumn = "DateAcct"; 351 acct.AD_Table_ID = "319"; 352 acct.reloadAcctSchemaArray(); 353 break; 354 case 321: 355 acct = new DocInventory (AD_Client_ID); 356 acct.tableName = "M_Inventory"; 357 acct.strDateColumn = "MovementDate"; 358 acct.AD_Table_ID = "321"; 359 acct.reloadAcctSchemaArray(); 360 break; 361 case 323: 362 acct = new DocMovement (AD_Client_ID); 363 acct.tableName = "M_Movement"; 364 acct.strDateColumn = "MovementDate"; 365 acct.AD_Table_ID = "323"; 366 acct.reloadAcctSchemaArray(); 367 break; 373 case 224: 374 if (log4j.isDebugEnabled()) log4j.debug("AcctServer - Before OBJECT CREATION"); 375 acct = new DocGLJournal (AD_Client_ID); 376 acct.tableName = "GL_Journal"; 377 acct.strDateColumn = "DateAcct"; 378 acct.AD_Table_ID = "224"; 379 acct.reloadAcctSchemaArray(); 380 break; 396 } 397 if (acct == null) log4j.warn("AcctServer - get - Unknown AD_Table_ID=" + AD_Table_ID); 398 else if (log4j.isDebugEnabled()) log4j.debug("AcctServer - get - AcctSchemaArray length=" + (acct.m_as).length); 399 if (log4j.isDebugEnabled()) log4j.debug("AcctServer - get - AD_Table_ID=" + AD_Table_ID); 400 return acct; 401 } 403 public void reloadAcctSchemaArray() throws ServletException{ 404 if (log4j.isDebugEnabled()) log4j.debug("AcctServer - reloadAcctSchemaArray - " + this.AD_Table_ID); 405 AcctSchema acct = null; 406 ArrayList<Object > new_as = new ArrayList<Object >(); 407 for (int i=0;i<(this.m_as).length;i++){ 408 acct = m_as[i]; 409 if(AcctSchemaData.selectAcctSchemaTable(this, acct.m_C_AcctSchema_ID, this.AD_Table_ID)){ 410 new_as.add(new AcctSchema(this,acct.m_C_AcctSchema_ID)); 411 413 } 414 } 415 AcctSchema[] retValue = new AcctSchema [new_as.size()]; 416 new_as.toArray(retValue); 417 if (log4j.isDebugEnabled()) log4j.debug("AcctServer - RELOADING ARRAY: " + retValue.length); 418 this.m_as = retValue; 419 422 } 423 424 425 public boolean post(String strClave, boolean force,VariablesSecureApp vars,ConnectionProvider conn,Connection con)throws ServletException{ 426 Record_ID = strClave; 427 if (log4j.isDebugEnabled()) log4j.debug("post " + strClave+" tablename: "+tableName); 428 try{ 430 if (AcctServerData.update(con,conn,tableName,strClave)!=1){ 431 log4j.warn("AcctServer - Post -Cannot lock Document - ignored: " + tableName + "_ID=" + strClave); 432 return false; 433 } 434 if (log4j.isDebugEnabled()) log4j.debug("AcctServer - Post -TableName -" + tableName + "- ad_client_id -" + AD_Client_ID + "- " + tableName + "_id -" + strClave); 435 try{ 436 loadObjectFieldProvider(this,AD_Client_ID,strClave); 437 }catch(ServletException e){ 438 log4j.warn(e); 439 } 440 FieldProvider data [] = getObjectFieldProvider(); 441 if (getDocumentConfirmation(conn, Record_ID) && post(data,force,vars,conn,con)){ 445 success++; 446 }else errors++; 447 } 449 catch (ServletException e){ 450 log4j.warn(e); 451 return false; 452 } 453 return true; 454 } 455 456 private boolean post(FieldProvider [] data, boolean force,VariablesSecureApp vars,ConnectionProvider conn,Connection con)throws ServletException{ 457 if (log4j.isDebugEnabled()) log4j.debug("post data" + C_Currency_ID); 458 if (!loadDocument(data,force,conn)){ 459 log4j.warn("AcctServer - post - Error loading document"); 460 return false; 461 } 462 m_fact = new Fact [m_as.length]; 468 469 boolean OK = true; 471 if (log4j.isDebugEnabled()) log4j.debug("AcctServer - Post -Beforde the loop - C_CURRENCY_ID = " + C_Currency_ID); 472 for (int i = 0; OK && i < m_as.length; i++){ 473 if (log4j.isDebugEnabled()) log4j.debug("AcctServer - Post - Before the postLogic - C_CURRENCY_ID = " + C_Currency_ID); 474 Status = postLogic (i, conn,con, vars); 475 if (log4j.isDebugEnabled()) log4j.debug("AcctServer - Post - After postLogic"); 476 if (!Status.equals(STATUS_Posted)) 477 OK = false; 478 } 479 if (log4j.isDebugEnabled()) log4j.debug("AcctServer - Post - Before the postCommit - C_CURRENCY_ID = " + C_Currency_ID); 480 Status = postCommit (Status, conn, vars,con); 482 483 if (!Status.equals(STATUS_Posted)){ 485 String AD_Message = "PostingError-" + Status; 487 String Text = Status; 489 String Reference = DocumentType 491 + " - " + DocumentNo 492 + " - " + DateDoc; 493 String AD_User_ID = "0"; 494 insertNote(AD_Client_ID, AD_Org_ID, AD_User_ID, 495 AD_Table_ID, Record_ID, AD_Message, Text, Reference,vars, conn,con); 496 } 497 498 for (int i = 0; i < m_fact.length; i++) 500 if (m_fact[i] != null) m_fact[i].dispose(); 501 p_lines = null; 502 503 return Status.equals(STATUS_Posted); 504 } 506 512 private final String postCommit (String status,ConnectionProvider conn,VariablesSecureApp vars,Connection con)throws ServletException{ 513 log4j.debug("AcctServer - postCommit Sta=" + status + " DT=" + DocumentType + " ID=" + Record_ID); 514 Status = status; 515 try{ 516 if (status.equals(AcctServer.STATUS_Posted)){ 519 if (m_fact!=null && m_fact.length!=0) { 520 log4j.debug("AcctServer - postCommit - m_fact.length = " + m_fact.length); 521 for (int i = 0; i < m_fact.length; i++){ 522 if (m_fact[i] != null && m_fact[i].save(con,conn, vars)); 523 else{ 524 unlock(conn,con); 526 return AcctServer.STATUS_Error; 527 } 528 } 529 } 530 } 531 if (!save(conn,con)){ unlock(conn,con); 535 return AcctServer.STATUS_Error; 536 } 537 } 540 catch (Exception e){ 541 log4j.warn("AcctServer - postCommit" + e); 542 status = AcctServer.STATUS_Error; 543 unlock(conn,con); 545 } 546 return status; 547 } 549 554 private final boolean save (ConnectionProvider conn, Connection con){ 555 int no = 0; 557 try{ 558 no = AcctServerData.updateSave(con,conn,tableName,Status,Record_ID); 559 }catch(ServletException e){ 560 log4j.warn(e); 561 } 562 return no==1; 563 } 565 566 569 private void unlock(ConnectionProvider conn,Connection con){ 570 int i =0; 571 try{ 572 i = AcctServerData.updateUnlock(con,conn,tableName,Record_ID); 573 }catch(ServletException e){ 574 log4j.warn("AcctServer - Document locked: -" + e); 575 } 576 581 } 583 584 585 public boolean loadDocument(FieldProvider[] data, boolean force,ConnectionProvider conn){ 586 if (log4j.isDebugEnabled()) log4j.debug("loadDocument " + data.length); 587 588 Status = STATUS_Error; 589 String Name = ""; 590 AD_Client_ID=data[0].getField("AD_Client_ID"); 591 AD_Org_ID=data[0].getField("AD_Org_ID"); 592 C_BPartner_ID=data[0].getField("C_BPartner_ID"); 593 M_Product_ID=data[0].getField("M_Product_ID"); 594 AD_OrgTrx_ID=data[0].getField("AD_OrgTrx_ID"); 595 C_SalesRegion_ID=data[0].getField("C_SalesRegion_ID"); 596 C_Project_ID=data[0].getField("C_Project_ID"); 597 C_Campaign_ID=data[0].getField("C_Campaign_ID"); 598 C_Activity_ID=data[0].getField("C_Activity_ID"); 599 C_LocFrom_ID=data[0].getField("C_LocFrom_ID"); 600 C_LocTo_ID=data[0].getField("C_LocTo_ID"); 601 User1_ID=data[0].getField("User1_ID"); 602 User2_ID=data[0].getField("User2_ID"); 603 604 Name=data[0].getField("Name"); 605 DocumentNo = data[0].getField("DocumentNo"); 606 DateAcct = data[0].getField("DateAcct"); 607 DateDoc = data[0].getField("DateDoc"); 608 C_Period_ID = data[0].getField("C_Period_ID"); 609 C_Currency_ID = data[0].getField("C_Currency_ID"); 610 C_DocType_ID = data[0].getField("C_DocType_ID"); 611 C_Charge_ID = data[0].getField("C_Charge_ID"); 612 ChargeAmt = data[0].getField("ChargeAmt"); 613 C_BankAccount_ID = data[0].getField("C_BankAccount_ID"); 614 if (log4j.isDebugEnabled()) log4j.debug("AcctServer - loadDocument - C_BankAccount_ID : " + C_BankAccount_ID); 615 Posted = data[0].getField("Posted"); 616 if (!loadDocumentDetails(data, conn)) loadDocumentType(); 617 if ((DateAcct == null || DateAcct == "") && (DateDoc != null && DateDoc != "")) 619 DateAcct = DateDoc; 620 else if ((DateDoc == null || DateDoc == "") && (DateAcct != null && DateAcct != "")) 621 DateDoc = DateAcct; 622 if (DocumentNo == null || DocumentNo.length() == 0) 624 DocumentNo = Name; 625 628 String error = ""; 631 if (AD_Table_ID == null || AD_Table_ID == "") 632 error += " AD_Table_ID"; 633 if (Record_ID == null || Record_ID == "") 634 error += " Record_ID"; 635 if (AD_Client_ID == null || AD_Client_ID == "") 636 error += " AD_Client_ID"; 637 if (AD_Org_ID == null || AD_Org_ID == "") 638 error += " AD_Org_ID"; 639 if (C_Currency_ID == null || C_Currency_ID == "") 640 error += " C_Currency_ID"; 641 if (DateAcct == null || DateAcct == "") 642 error += " DateAcct"; 643 if (DateDoc == null || DateDoc == "") 644 error += " DateDoc"; 645 if (error.length() > 0){ 646 log4j.warn("AcctServer - loadDocument - " + DocumentNo + " - Mandatory info missing: " + error); 647 return false; 648 } 649 650 if (force){ 652 if (Posted.equals("Y") && !isPeriodOpen()){ log4j.warn("AcctServer - loadDocument - " + DocumentNo + " - Period Closed for already posted document"); 654 return false; 655 } 656 int no =0; 658 try{ 659 no = AcctServerData.delete(this,tableName,Record_ID); 660 }catch(ServletException e){ 661 log4j.warn(e); 662 } 663 } 665 else if (Posted.equals("Y")){ 666 log4j.warn("AcctServer - loadDocument - " + DocumentNo + " - Document already posted"); 667 return false; 668 } 669 return true; 671 } 673 674 public void loadDocumentType(){ 675 try{ 677 if (C_DocType_ID != null && C_DocType_ID !=""){ 678 AcctServerData[] data = AcctServerData.selectDocType(this,C_DocType_ID); 679 DocumentType = data[0].docbasetype; 680 GL_Category_ID = data[0].glCategoryId; 681 } 682 if (GL_Category_ID == ""){ 684 AcctServerData[] data = AcctServerData.selectGLCategory(this,AD_Client_ID,DocumentType); 685 if (data!=null && data.length != 0)GL_Category_ID = data[0].glCategoryId; 686 } 687 if (DocumentType == "")log4j.warn("AcctServer - loadDocumentType - No DocType for GL Info"); 688 if (GL_Category_ID == ""){ 689 AcctServerData[] data = AcctServerData.selectDefaultGLCategory(this,AD_Client_ID); 690 GL_Category_ID = data[0].glCategoryId; 691 } 692 }catch(ServletException e){ 693 log4j.warn(e); 694 } 695 if (GL_Category_ID == "")log4j.warn("AcctServer - loadDocumentType - No GL Info"); 696 } 698 699 public boolean insertNote (String AD_Client_ID, String AD_Org_ID, String AD_User_ID,String AD_Table_ID, String Record_ID,String AD_MessageValue, String Text, String Reference,VariablesSecureApp vars,ConnectionProvider conn,Connection con){ 700 701 if (AD_MessageValue == "" || AD_MessageValue.length() == 0) 702 throw new IllegalArgumentException ("AcctServer - insertNote - required parameter missing - AD_Message"); 703 704 if (Text == null) 706 Text = ""; 707 if (Text.length() > 2000) 708 Text = Text.substring(0,1999); 709 if (Reference == null) 710 Reference = ""; 711 if (Reference.length() > 60) 712 Reference = Reference.substring(0,59); 713 String CompiereSys = "N"; 717 int no=0; 718 try{ 719 String AD_Note_ID = SequenceIdData.getSequence(this, "AD_Note", vars.getClient()); 720 721 722 no = AcctServerData.insertNote(con,conn, AD_Note_ID, AD_Client_ID, AD_Org_ID, AD_User_ID, Text, Reference, AD_Table_ID, Record_ID, AD_MessageValue); 724 725 if (no == 0){ 728 no = AcctServerData.insertNote(con,conn, AD_Note_ID, AD_Client_ID, AD_Org_ID, AD_User_ID, Text, Reference, AD_Table_ID, Record_ID,"NoMessageFound"); 730 } 731 }catch(ServletException e){ 732 log4j.warn(e); 733 } 734 735 return no == 1; 736 } 738 743 private final String postLogic (int index,ConnectionProvider conn,Connection con,VariablesSecureApp vars) throws ServletException{ 744 if (!m_as[index].isSuspenseBalancing() && !isBalanced()) 746 return STATUS_NotBalanced; 747 748 if (!isConvertible(m_as[index], conn)) 750 return STATUS_NotConvertible; 751 752 if (!isPeriodOpen()) 755 return STATUS_PeriodClosed; 756 758 try{ 760 m_fact[index] = createFact (m_as[index], conn,con, vars); 761 }catch (Exception e){ 762 log4j.warn(e); 763 } 764 if (m_fact[index] == null) 765 return STATUS_Error; 766 Status = STATUS_PostPrepared; 767 768 if (!m_fact[index].isSourceBalanced()) 771 m_fact[index].balanceSource(conn); 772 774 if (!m_fact[index].isSegmentBalanced(conn)) 777 m_fact[index].balanceSegments(conn); 778 780 if (!m_fact[index].isAcctBalanced()) 782 m_fact[index].balanceAccounting(conn); 783 784 return STATUS_Posted; 785 } 787 791 public boolean isBalanced(){ 792 if (MultiCurrency) 794 return true; 795 boolean retValue = getBalance().equals("0"); 797 if (retValue) if (log4j.isDebugEnabled()) log4j.debug("AcctServer - isBalanced - " + DocumentNo); 798 else log4j.warn("AcctServer - is not Balanced - " + DocumentNo); 799 return retValue; 800 } 802 807 public boolean isConvertible (AcctSchema acctSchema, ConnectionProvider conn) throws ServletException{ 808 if (C_Currency_ID.equals("-1")){ 810 return true; 812 } 813 Vector<Object > set = new Vector<Object >(); 815 set.addElement(C_Currency_ID); 816 for (int i = 0; p_lines != null && i < p_lines.length; i++){ 817 String currency = p_lines[i].m_C_Currency_ID; 818 set.addElement(currency); 819 } 820 821 if (set.size() == 1 && acctSchema.m_C_Currency_ID.equals(C_Currency_ID)){ 823 return true; 825 } 826 boolean convertible = true; 827 for (int i=0;i<set.size();i++){ 828 String currency = (String )set.elementAt(i); 830 if (currency==null) currency=""; 831 if (!currency.equals(acctSchema.m_C_Currency_ID)){ 833 String amt = getConvertedAmt("1", currency, acctSchema.m_C_Currency_ID, 835 DateAcct, acctSchema.m_CurrencyRateType, AD_Client_ID, AD_Org_ID, conn); 836 if (amt == null){ 838 convertible = false; 839 log4j.warn ("AcctServer - isConvertible NOT from " + currency + " - " + DocumentNo); 840 }else if (log4j.isDebugEnabled()) log4j.debug ("AcctServer - isConvertible from " + currency); 841 } 842 } 843 return convertible; 845 } 847 848 855 public String getAmount(int AmtType){ 856 if (AmtType < 0 || Amounts == null || AmtType >= Amounts.length) 857 return null; 858 return (Amounts[AmtType].equals(""))?"0":Amounts[AmtType]; 859 } 861 865 public String getAmount(){ 866 return Amounts[0]; 867 } 869 878 public static String getConvertedAmt (String Amt, String CurFrom_ID, String CurTo_ID, 879 String ConvDate, String RateType, ConnectionProvider conn){ 880 if (log4j.isDebugEnabled()) log4j.debug("AcctServer - getConvertedAmount no client nor org"); 881 return getConvertedAmt (Amt, CurFrom_ID, CurTo_ID, ConvDate, RateType, "","", conn); 882 } 883 884 public static String getConvertedAmt (String Amt, String CurFrom_ID, String CurTo_ID, 885 String ConvDate, String RateType, String client, String org, ConnectionProvider conn){ 886 if (log4j.isDebugEnabled()) log4j.debug("AcctServer - getConvertedAmount - starting method - Amt : " + Amt + " - CurFrom_ID : " + CurFrom_ID + " - CurTo_ID : " + CurTo_ID + "- ConvDate: "+ConvDate+" - RateType:"+RateType+" - client:"+client+"- org:"+org); 887 888 if (Amt.equals("")) 889 throw new IllegalArgumentException ("AcctServer - getConvertedAmt - required parameter missing - Amt"); 890 if (CurFrom_ID.equals(CurTo_ID) || Amt.equals("0")) 891 return Amt; 892 AcctServerData[] data = null; 893 try{ 894 if (ConvDate == "")ConvDate = DateTimeData.today(conn); 895 if (RateType == null || RateType.equals("")) RateType = "S"; 897 data = AcctServerData.currencyConvert(conn,Amt,CurFrom_ID,CurTo_ID,ConvDate,RateType, client, org); 898 }catch(ServletException e){ 899 log4j.warn (e); 900 } 901 if (data==null || data.length == 0){ 902 904 return ""; 905 }else { 906 if (log4j.isDebugEnabled()) log4j.debug("getConvertedAmount - converted:"+data[0].converted); 907 return data[0].converted; 908 } 909 } 911 912 916 public boolean isPeriodOpen(){ 917 setC_Period_ID(); 919 boolean open = (!C_Period_ID.equals("")); 920 if (open) 921 if (log4j.isDebugEnabled()) log4j.debug("AcctServer - isPeriodOpen - " + DocumentNo); 922 else 923 log4j.warn("AcctServer - isPeriodOpen NO - " + DocumentNo); 924 return open; 925 } 927 931 public void setC_Period_ID(){ 932 if (C_Period_ID != null) return; 933 if (log4j.isDebugEnabled()) log4j.debug("AcctServer - setC_Period_ID - AD_Client_ID - " + AD_Client_ID + "--DateAcct - " + DateAcct + "--DocumentType -" + DocumentType); 934 AcctServerData [] data=null; 935 try{ 936 if (log4j.isDebugEnabled()) log4j.debug("setC_Period_ID - inside try - AD_Client_ID - " + AD_Client_ID + " -- DateAcct - " + DateAcct + " -- DocumentType - " + DocumentType); 937 data = AcctServerData.periodOpen(this,AD_Client_ID,DocumentType,DateAcct); 938 }catch (ServletException e){ 939 log4j.warn(e); 940 } 941 C_Period_ID = data[0].period; 942 if (log4j.isDebugEnabled()) log4j.debug ("AcctServer - setC_Period_ID - "+ AD_Client_ID + "/" + DateAcct + "/" + DocumentType + " => " + C_Period_ID); 943 } 945 946 969 public static String getValidCombination (String AD_Client_ID, String AD_Org_ID, 970 String C_AcctSchema_ID, String Account_ID, String base_ValidCombination_ID, String Alias, String AD_User_ID, 971 String M_Product_ID, String C_BPartner_ID, String AD_OrgTrx_ID, 972 String C_LocFrom_ID, String C_LocTo_ID, String C_SRegion_ID, String C_Project_ID, 973 String C_Campaign_ID, String C_Activity_ID, String User1_ID, String User2_ID,ConnectionProvider conn){ 974 String retValue = ""; 975 StringBuffer sb = new StringBuffer ("C_ValidCombination_Get["); 976 sb.append("v,"); 977 sb.append("AD_Client_ID=").append(AD_Client_ID).append(","); 980 sb.append("AD_Org_ID=").append(AD_Org_ID).append(","); 982 sb.append("C_AcctSchema_ID=").append(C_AcctSchema_ID).append(","); 984 sb.append("Account_ID=").append(Account_ID).append(", "); 986 sb.append("BaseValidCombination_ID=").append(base_ValidCombination_ID).append(","); 989 sb.append("MustBeFullyQualified='Y',"); 991 sb.append("Alias='").append(Alias).append("',"); 993 sb.append("AD_User_ID=").append(AD_User_ID).append(", "); 995 sb.append("M_Product_ID=").append(M_Product_ID).append(","); 998 sb.append("C_BPartner_ID=").append(C_BPartner_ID).append(","); 1000 sb.append("AD_OrgTrx_ID=").append(AD_OrgTrx_ID).append(","); 1002 sb.append("C_LocFrom_ID=").append(C_LocFrom_ID).append(","); 1004 sb.append("C_LocTo_ID=").append(C_LocTo_ID).append(", "); 1006 sb.append("C_SalesRegion_ID=").append(C_SRegion_ID).append(","); 1008 sb.append("C_Project_ID=").append(C_Project_ID).append(","); 1010 sb.append("C_Campaign_ID=").append(C_Campaign_ID).append(","); 1012 sb.append("C_Activity_ID=").append(C_Activity_ID).append(","); 1014 sb.append("User1_ID=").append(User1_ID).append(","); 1016 sb.append("User2_ID=").append(User2_ID).append(")"); 1018 1019 RespuestaCS_AcctServer data = null; 1020 try{ 1021 data = AcctServerData.callGetValidCombination(conn, AD_Client_ID, AD_Org_ID, C_AcctSchema_ID, Account_ID, base_ValidCombination_ID, "Y", Alias, AD_User_ID, M_Product_ID, C_BPartner_ID, AD_OrgTrx_ID, C_LocFrom_ID, C_LocTo_ID, C_SRegion_ID, C_Project_ID, C_Campaign_ID, C_Activity_ID, User1_ID, User2_ID); 1022 }catch(ServletException e){ 1023 log4j.warn(e); 1024 } 1025 retValue = data.validCombination; 1027 return retValue; 1029 } 1031 1053 public int match(VariablesSecureApp vars,ConnectionProvider conn, Connection con){ 1054 int counter = 0; 1056 AcctServerData [] data=null; 1058 try{ 1059 data= AcctServerData.selectMatch(conn,AD_Client_ID); 1060 }catch(ServletException e){ 1061 log4j.warn(e); 1062 } 1063 for (int i = 0;i<data.length;i++){ 1064 BigDecimal qty1 = new BigDecimal(data[i].qty1); 1065 BigDecimal qty2 = new BigDecimal(data[i].qty2); 1066 BigDecimal Qty = qty1.min(qty2); 1067 if (Qty.toString().equals("0")) 1068 continue; 1069 String dateTrx1 = data[i].datetrx1; 1071 String dateTrx2 = data[i].datetrx2; 1072 String compare = ""; 1073 try{ 1074 compare = DateTimeData.compare(conn, dateTrx1, dateTrx2); 1075 }catch(ServletException e){ 1076 log4j.warn(e); 1077 } 1078 String DateTrx = dateTrx1; 1079 if (compare.equals("-1")) 1080 DateTrx = dateTrx2; 1081 String strQty = Qty.toString(); 1083 String strDateTrx = DateTrx; 1084 String AD_Client_ID = data[i].adClientId; 1085 String AD_Org_ID = data[i].adOrgId; 1086 String C_InvoiceLine_ID = data[i].cInvoicelineId; 1087 String M_InOutLine_ID = data[i].mInoutlineId; 1088 String M_Product_ID = data[i].mProductId; 1089 if (createMatchInv(AD_Client_ID, AD_Org_ID,M_InOutLine_ID, C_InvoiceLine_ID,M_Product_ID, strDateTrx, strQty, vars, conn,con)==1) 1091 counter++; 1092 } 1093 return counter; 1095 } 1097 1108 private int createMatchInv (String AD_Client_ID, String AD_Org_ID,String M_InOutLine_ID, String C_InvoiceLine_ID,String 1109 M_Product_ID, String DateTrx, String Qty,VariablesSecureApp vars,ConnectionProvider conn, Connection con){ 1110 int no = 0; 1112 try{ 1113 String M_MatchInv_ID = SequenceIdData.getSequence(conn, "M_MatchInv", vars.getClient()); 1114 no = AcctServerData.insertMatchInv(con,conn, M_MatchInv_ID, AD_Client_ID, AD_Org_ID, M_InOutLine_ID, C_InvoiceLine_ID, M_Product_ID, DateTrx, Qty); 1116 }catch(ServletException e){ 1117 log4j.warn(e); 1118 } 1119 return no; 1120 } 1122 1128 public final Account getAccount (String AcctType, AcctSchema as,ConnectionProvider conn){ 1129 BigDecimal AMT = null; 1130 BigDecimal ZERO = new BigDecimal("0"); 1131 AcctServerData [] data=null; 1132 try{ 1134 1135 if (AcctType.equals(ACCTTYPE_Charge)){ AMT = new BigDecimal(getAmount(AMTTYPE_Charge)); 1138 int cmp = AMT.compareTo(ZERO); 1140 if (cmp == 0) 1142 return null; 1143 else if (cmp < 0) 1144 data = AcctServerData.selectExpenseAcct(conn, C_Charge_ID, as.getC_AcctSchema_ID()); 1145 else 1146 data = AcctServerData.selectRevenueAcct(conn, C_Charge_ID, as.getC_AcctSchema_ID()); 1147 }else if (AcctType.equals(ACCTTYPE_V_Liability)){ 1149 data = AcctServerData.selectLiabilityAcct(conn, C_BPartner_ID, as.getC_AcctSchema_ID()); 1150 }else if (AcctType.equals(ACCTTYPE_V_Liability_Services)){ 1151 data = AcctServerData.selectLiabilityServicesAcct(conn, C_BPartner_ID, as.getC_AcctSchema_ID()); 1152 }else if (AcctType.equals(ACCTTYPE_C_Receivable)){ 1153 data = AcctServerData.selectReceivableAcct(conn, C_BPartner_ID, as.getC_AcctSchema_ID()); 1154 }else if (AcctType.equals(ACCTTYPE_UnallocatedCash)){ 1155 data = AcctServerData.selectUnallocatedCashAcct(conn, C_BankAccount_ID, as.getC_AcctSchema_ID()); 1156 }else if (AcctType.equals(ACCTTYPE_BankInTransit)){ 1157 data = AcctServerData.selectInTransitAcct(conn, C_BankAccount_ID, as.getC_AcctSchema_ID()); 1158 }else if (AcctType.equals(ACCTTYPE_BankInTransitDefault)){ 1159 data = AcctServerData.selectInTransitDefaultAcct(conn, as.getC_AcctSchema_ID()); 1160 }else if (AcctType.equals(ACCTTYPE_ConvertChargeDefaultAmt)){ 1161 data = AcctServerData.selectConvertChargeDefaultAmtAcct(conn, as.getC_AcctSchema_ID()); 1162 }else if (AcctType.equals(ACCTTYPE_ConvertGainDefaultAmt)){ 1163 data = AcctServerData.selectConvertGainDefaultAmtAcct(conn, as.getC_AcctSchema_ID()); 1164 }else if (AcctType.equals(ACCTTYPE_PaymentSelect)){ 1165 data = AcctServerData.selectPaymentSelectAcct(conn, C_BankAccount_ID, as.getC_AcctSchema_ID()); 1166 }else if (AcctType.equals(ACCTTYPE_WriteOffDefault)){ 1167 data = AcctServerData.selectWriteOffDefault(conn, as.getC_AcctSchema_ID()); 1168 }else if (AcctType.equals(ACCTTYPE_DiscountExp)){ 1169 data = AcctServerData.selectDiscountExpAcct(conn, C_BPartner_ID, as.getC_AcctSchema_ID()); 1170 }else if (AcctType.equals(ACCTTYPE_DiscountRev)){ 1171 data = AcctServerData.selectDiscountRevAcct(conn, C_BPartner_ID, as.getC_AcctSchema_ID()); 1172 }else if (AcctType.equals(ACCTTYPE_WriteOff)){ 1173 data = AcctServerData.selectWriteOffAcct(conn, C_BPartner_ID, as.getC_AcctSchema_ID()); 1174 }else if (AcctType.equals(ACCTTYPE_ConvertChargeLossAmt)){ 1175 data = AcctServerData.selectConvertChargeLossAmt(conn, C_BankAccount_ID, as.getC_AcctSchema_ID()); 1176 }else if (AcctType.equals(ACCTTYPE_ConvertChargeGainAmt)){ 1177 data = AcctServerData.selectConvertChargeGainAmt(conn, C_BankAccount_ID, as.getC_AcctSchema_ID()); 1178 }else if (AcctType.equals(ACCTTYPE_BankAsset)){ 1179 data = AcctServerData.selectAssetAcct(conn, C_BankAccount_ID, as.getC_AcctSchema_ID()); 1180 }else if (AcctType.equals(ACCTTYPE_InterestRev)){ 1181 data = AcctServerData.selectInterestRevAcct(conn, C_BankAccount_ID, as.getC_AcctSchema_ID()); 1182 }else if (AcctType.equals(ACCTTYPE_InterestExp)){ 1183 data = AcctServerData.selectInterestExpAcct(conn, C_BankAccount_ID, as.getC_AcctSchema_ID()); 1184 }else if (AcctType.equals(ACCTTYPE_CashAsset)){ 1185 data = AcctServerData.selectCBAssetAcct(conn, C_CashBook_ID, as.getC_AcctSchema_ID()); 1186 }else if (AcctType.equals(ACCTTYPE_CashTransfer)){ 1187 data = AcctServerData.selectCashTransferAcct(conn, C_CashBook_ID, as.getC_AcctSchema_ID()); 1188 }else if (AcctType.equals(ACCTTYPE_CashExpense)){ 1189 data = AcctServerData.selectCBExpenseAcct(conn, C_CashBook_ID, as.getC_AcctSchema_ID()); 1190 }else if (AcctType.equals(ACCTTYPE_CashReceipt)){ 1191 data = AcctServerData.selectCBReceiptAcct(conn, C_CashBook_ID, as.getC_AcctSchema_ID()); 1192 }else if (AcctType.equals(ACCTTYPE_CashDifference)){ 1193 data = AcctServerData.selectCBDifferencesAcct(conn, C_CashBook_ID, as.getC_AcctSchema_ID()); 1194 }else if (AcctType.equals(ACCTTYPE_InvDifferences)){ 1195 data = AcctServerData.selectWDifferencesAcct(conn, M_Warehouse_ID, as.getC_AcctSchema_ID()); 1196 }else if (AcctType.equals(ACCTTYPE_NotInvoicedReceipts)){ 1197 if (log4j.isDebugEnabled()) log4j.debug("AcctServer - getAccount - ACCTYPE_NotInvoicedReceipts - C_BPartner_ID - " + C_BPartner_ID); 1198 data = AcctServerData.selectNotInvoicedReceiptsAcct(conn, C_BPartner_ID, as.getC_AcctSchema_ID()); 1199 }else if (AcctType.equals(ACCTTYPE_ProjectAsset)){ 1200 data = AcctServerData.selectPJAssetAcct(conn, C_Project_ID, as.getC_AcctSchema_ID()); 1201 }else if (AcctType.equals(ACCTTYPE_ProjectWIP)){ 1202 data = AcctServerData.selectPJWIPAcct(conn, C_Project_ID, as.getC_AcctSchema_ID()); 1203 }else if (AcctType.equals(ACCTTYPE_PPVOffset)){ 1204 data = AcctServerData.selectPPVOffsetAcct(conn, as.getC_AcctSchema_ID()); 1205 }else{ 1206 log4j.warn("AcctServer - getAccount - Not found AcctType=" + AcctType); 1207 return null; 1208 } 1209 }catch(ServletException e){ 1211 log4j.warn(e); 1212 } 1213 String Account_ID = ""; 1215 if (data != null && data.length!=0){ 1216 Account_ID = data[0].accountId; 1217 }else return null; 1218 if (Account_ID.equals("")){ 1220 log4j.warn("AcctServer - getAccount - NO account Type=" 1221 + AcctType + ", Record=" + Record_ID); 1222 return null; 1223 } 1224 Account acct = null; 1227 try{ 1228 acct = Account.getAccount(conn, Account_ID); 1229 }catch(ServletException e){ 1230 log4j.warn(e); 1231 } 1232 return acct; 1233 } 1235 public FieldProvider [] getObjectFieldProvider(){ 1236 return objectFieldProvider; 1237 } 1238 1239 public void setObjectFieldProvider(FieldProvider [] fieldProvider){ 1240 objectFieldProvider = fieldProvider; 1241 } 1242 1243 public abstract void loadObjectFieldProvider(ConnectionProvider conn, String AD_Client_ID, String Id) throws ServletException; 1244 1245 public abstract boolean loadDocumentDetails(FieldProvider[] data,ConnectionProvider conn); 1246 1250 public abstract BigDecimal getBalance(); 1251 1252 1257 public abstract Fact createFact (AcctSchema as,ConnectionProvider conn,Connection con,VariablesSecureApp vars) throws ServletException; 1258 1259 public abstract boolean getDocumentConfirmation(ConnectionProvider conn, String strRecordId); 1260 1261 public String getInfo(VariablesSecureApp vars) { 1262 return(Utility.messageBD(this, "Created", vars.getLanguage()) + "=" + success ); 1263 } 1265 public boolean checkDocuments() throws ServletException{ 1266 if(m_as.length==0) return false; 1267 AcctServerData [] docTypes = AcctServerData.selectDocTypes(this, AD_Table_ID, AD_Client_ID); 1268 for (int i=0;i<docTypes.length;i++){ 1270 AcctServerData [] data = AcctServerData.selectDocuments(this, tableName, docTypes[i].name, strDateColumn); 1271 if (log4j.isDebugEnabled()) log4j.debug("AcctServer - not posted "+docTypes[i].name+" documets length-" + data.length); 1272 if(data!=null && data.length>0) { 1273 if (!data[0].id.equals("")) return true; 1274 } 1275 } 1276 return false; 1277 } 1279 public String getServletInfo() { 1280 return "Servlet for the accounting"; 1281 } } 1283 | Popular Tags |