1 14 package org.compiere.grid.ed; 15 16 import java.awt.*; 17 import java.awt.event.*; 18 import javax.swing.*; 19 import java.sql.*; 20 import java.util.*; 21 22 import org.compiere.apps.*; 23 import org.compiere.util.*; 24 import org.compiere.model.*; 25 import org.compiere.plaf.*; 26 import org.compiere.swing.*; 27 28 34 public final class VBPartner extends JDialog implements ActionListener 35 { 36 42 public VBPartner(Frame frame, int WindowNo) 43 { 44 super(frame, Msg.translate(Env.getCtx(), "C_BPartner_ID"), true); 45 Log.trace(Log.l1_User, "VBPartner"); 46 m_WindowNo = WindowNo; 47 try 48 { 49 jbInit(); 50 } 51 catch(Exception ex) 52 { 53 Log.error("VBPartner - " + ex.getMessage()); 54 } 55 initBPartner(); 56 AEnv.positionCenterWindow(frame, this); 58 } 60 private int m_WindowNo; 61 private int m_C_BPartner_ID = 0; 62 private Integer m_AD_User_ID = null; 63 private Integer m_C_BPartner_Location_ID = null; 64 65 private Insets m_labelInsets = new Insets(2,15,2,0); private Insets m_fieldInsets = new Insets(2,5,2,10); private GridBagConstraints m_gbc = new GridBagConstraints(); 68 private int m_line; 69 private Object [] m_greeting; 70 private VString fValue, fName, fName2, fContact, fTitle, fPhone, fFax, fPhone2, fEMail; 72 private VLocation fAddress; 73 private String OldValue="", OldName="", OldName2="", OldContact="", OldTitle="", 74 OldPhone="", OldFax="", OldPhone2="", OldEMail=""; 75 private JComboBox fGreetingBP, fGreetingC; 76 private int OldGreetingBP = -1, OldGreetingC = -1; 77 private CPanel mainPanel = new CPanel(); 79 private BorderLayout mainLayout = new BorderLayout(); 80 private CPanel centerPanel = new CPanel(); 81 private CPanel southPanel = new CPanel(); 82 private GridBagLayout centerLayout = new GridBagLayout(); 83 private ConfirmPanel confirmPanel = new ConfirmPanel(true); 84 private BorderLayout southLayout = new BorderLayout(); 85 86 90 void jbInit() throws Exception 91 { 92 mainPanel.setLayout(mainLayout); 93 southPanel.setLayout(southLayout); 94 centerPanel.setLayout(centerLayout); 95 mainLayout.setVgap(5); 96 getContentPane().add(mainPanel); 97 mainPanel.add(centerPanel, BorderLayout.CENTER); 98 mainPanel.add(southPanel, BorderLayout.SOUTH); 99 southPanel.add(confirmPanel, BorderLayout.CENTER); 100 confirmPanel.addActionListener(this); 102 } 104 107 private void initBPartner() 108 { 109 m_greeting = fillGreeting(); 111 112 m_gbc.anchor = GridBagConstraints.NORTHWEST; 114 m_gbc.gridx = 0; 115 m_gbc.gridy = 0; 116 m_gbc.gridwidth = 1; 117 m_gbc.weightx = 0; 118 m_gbc.weighty = 0; 119 m_gbc.fill = GridBagConstraints.NONE; 120 m_gbc.ipadx = 0; 121 m_gbc.ipady = 0; 122 m_line = 0; 123 124 fValue = new VString("Value", true, false, true, 30, 60, ""); 126 createLine (fValue, "Value", true, false); 127 fGreetingBP = new JComboBox (m_greeting); 129 createLine (fGreetingBP, "Greeting"); 130 fName = new VString("Name", true, false, true, 30, 60, ""); 132 createLine (fName, "Name"); 133 fName2 = new VString("Name2", false, false, true, 30, 60, ""); 135 createLine (fName2, "Name2"); 136 fGreetingC = new JComboBox (m_greeting); 138 createLine (fGreetingC, "Greeting", true, false); 139 fContact = new VString("Contact", false, false, true, 30, 60, ""); 141 createLine (fContact, "Contact"); 142 fTitle = new VString("Title", false, false, true, 30, 60, ""); 144 createLine(fTitle, "Title"); 145 fAddress = new VLocation ("C_Location_ID", false, false, true, new MLocation (Env.getCtx(), m_WindowNo)); 147 fAddress.setValue (null); 148 createLine (fAddress, "C_Location_ID", true, true); 149 fPhone = new VString("Phone", false, false, true, 30, 40, ""); 151 createLine (fPhone, "Phone", true, false); 152 fPhone2 = new VString("Phone2", false, false, true, 30, 40, ""); 154 createLine (fPhone2, "Phone2"); 155 fFax = new VString("Fax", false, false, true, 30, 40, ""); 157 createLine (fFax, "Fax"); 158 fEMail = new VString("EMail", false, false, true, 30, 40, ""); 160 createLine (fEMail, "EMail"); 161 } 163 168 private void createLine (JComponent field, String title) 169 { 170 createLine (field, title, false, false); 171 } 172 173 180 private void createLine (JComponent field, String title, boolean addSpace, boolean fill) 181 { 182 if (addSpace) 183 { 184 m_gbc.gridy = m_line++; 185 m_gbc.gridx = 1; 186 m_gbc.insets = m_fieldInsets; 187 centerPanel.add (Box.createHorizontalStrut(6), m_gbc); 188 } 189 190 m_gbc.gridy = m_line++; 192 193 m_gbc.gridx = 0; 195 m_gbc.insets = m_labelInsets; 196 m_gbc.fill = GridBagConstraints.HORIZONTAL; 197 VLabel label = new VLabel(Msg.translate(Env.getCtx(), title)); 198 centerPanel.add(label, m_gbc); 199 200 m_gbc.gridx = 1; 202 m_gbc.insets = m_fieldInsets; 203 if (fill) 204 m_gbc.fill = GridBagConstraints.HORIZONTAL; 205 else 206 m_gbc.fill = GridBagConstraints.NONE; 207 centerPanel.add(field, m_gbc); 208 } 210 214 private Object [] fillGreeting() 215 { 216 ArrayList list = new ArrayList(); 217 list.add (new KeyNamePair(-1, " ")); 219 220 String SQL = "SELECT C_Greeting_ID, Name FROM C_Greeting"; 221 222 try 223 { 224 Statement stmt = DB.createStatement(); 225 ResultSet rs = stmt.executeQuery(SQL); 226 while (rs.next()) 227 { 228 int key = rs.getInt("C_Greeting_ID"); 229 String name = rs.getString("Name"); 230 list.add (new KeyNamePair(key, name)); 231 } 232 rs.close(); 233 stmt.close(); 234 } 235 catch (SQLException e) 236 { 237 Log.error("VBPartner.fillGreeting", e); 238 } 239 return list.toArray(); 240 } 242 247 private KeyNamePair getGreeting (int key) 248 { 249 for (int i = 0; i < m_greeting.length; i++) 250 { 251 KeyNamePair p = (KeyNamePair)m_greeting[i]; 252 if (p.getKey() == key) 253 return p; 254 } 255 return new KeyNamePair(-1, " "); 256 } 258 263 public boolean loadBPartner (int C_BPartner_ID) 264 { 265 Log.trace(Log.l3_Util, "VBPartner.load - " + C_BPartner_ID); 266 if (C_BPartner_ID == m_C_BPartner_ID) 268 return true; 269 if (C_BPartner_ID == 0) 271 { 272 m_C_BPartner_ID = C_BPartner_ID; 273 return true; 274 } 275 276 m_C_BPartner_ID = C_BPartner_ID; 277 String SQL = "SELECT p.Value,p.C_Greeting_ID AS C_GreetingBP_ID,p.Name,p.Name2," 278 + "c.Name AS Contact,c.C_Greeting_ID,c.Title," 279 + "l.Phone,l.Phone2,l.Fax, c.EMail, l.C_Location_ID," 280 + "c.AD_User_ID, l.C_BPartner_Location_ID " 281 + "FROM C_BPartner p, AD_User c, C_BPartner_Location l " 282 + "WHERE p.C_BPartner_ID=c.C_BPartner_ID(+)" 283 + " AND p.C_BPartner_ID=l.C_BPartner_ID(+)" 284 + " AND p.C_BPartner_ID=?" + " ORDER BY l.IsBillTo Desc"; try 287 { 288 PreparedStatement pstmt = DB.prepareStatement(SQL); 289 pstmt.setInt(1, m_C_BPartner_ID); 290 ResultSet rs = pstmt.executeQuery(); 291 if (!rs.next()) 292 { 293 ADialog.error(m_WindowNo, this, "BPartnerNotFound"); 294 rs.close(); 295 pstmt.close(); 296 return false; 297 } 298 OldValue = rs.getString("Value"); 300 if (OldValue == null) 301 OldValue = ""; 302 fValue.setText(OldValue); 303 OldGreetingBP = rs.getInt("C_GreetingBP_ID"); 305 if (rs.wasNull()) 306 OldGreetingBP = -1; 307 fGreetingBP.setSelectedItem(getGreeting(OldGreetingBP)); 308 OldName = rs.getString("Name"); 310 if (OldName == null) 311 OldName = ""; 312 fName.setText(OldName); 313 OldName2 = rs.getString("Name2"); 315 if (OldName2 == null) 316 OldName2 = ""; 317 fName2.setText(OldName2); 318 OldGreetingC = rs.getInt("C_Greeting_ID"); 320 if (rs.wasNull()) 321 OldGreetingC = -1; 322 fGreetingC.setSelectedItem(getGreeting(OldGreetingC)); 323 OldContact = rs.getString("Contact"); 325 if (OldContact == null) 326 OldContact = ""; 327 fContact.setText(OldContact); 328 OldTitle = rs.getString("Title"); 330 if (OldTitle == null) 331 OldTitle = ""; 332 fTitle.setText(OldTitle); 333 OldPhone = rs.getString("Phone"); 335 if (OldPhone == null) 336 OldPhone = ""; 337 fPhone.setText(OldPhone); 338 OldPhone2 = rs.getString("Phone2"); 340 if (OldPhone2 == null) 341 OldPhone2 = ""; 342 fPhone2.setText(OldPhone2); 343 OldFax = rs.getString("Fax"); 345 if (OldFax == null) 346 OldFax = ""; 347 fFax.setText(OldFax); 348 OldEMail = rs.getString("EMail"); 350 if (OldEMail == null) 351 OldEMail = ""; 352 fEMail.setText(OldEMail); 353 int location = rs.getInt("C_Location_ID"); 355 fAddress.setValue (new Integer (location)); 356 m_AD_User_ID = new Integer (rs.getInt("AD_User_ID")); 358 if (rs.wasNull()) 359 m_AD_User_ID = null; 360 m_C_BPartner_Location_ID = new Integer (rs.getInt("C_BPartner_Location_ID")); 361 if (rs.wasNull()) 362 m_C_BPartner_Location_ID = null; 363 rs.close(); 365 pstmt.close(); 366 } 367 catch (SQLException e) 368 { 369 Log.error("VBPartner.loadBPartner", e); 370 ADialog.error(m_WindowNo, this, "BPartnerNotFound", e.getLocalizedMessage()); 371 return false; 372 } 373 return true; 374 } 376 377 381 public void actionPerformed(ActionEvent e) 382 { 383 if (e.getActionCommand().equals(ConfirmPanel.A_OK) && actionSave()) 385 dispose(); 386 else if (e.getActionCommand().equals(ConfirmPanel.A_CANCEL)) 388 dispose(); 389 } 391 396 private boolean actionSave() 397 { 398 Log.trace(Log.l3_Util, "VBPartner.save"); 399 400 String value = fValue.getText(); 402 if (value == null) 403 value = ""; 404 String name = fName.getText(); 405 if (value.equals("") || name.equals("")) 406 { 407 if (value.equals("")) 408 fValue.setBackground(CompierePLAF.getFieldBackground_Error()); 409 if (name.equals("")) 410 fName.setBackground(CompierePLAF.getFieldBackground_Error()); 411 return false; 412 } 413 414 String SQL = ""; 415 try 416 { 417 Statement stmt = DB.createStatement(); 418 int AD_Client_ID = Integer.parseInt(Env.getContext(Env.getCtx(), "#AD_Client_ID")); 419 int AD_Org_ID = Integer.parseInt(Env.getContext(Env.getCtx(), "#AD_Org_ID")); 420 int UpdatedBy = Integer.parseInt(Env.getContext(Env.getCtx(), "#AD_User_ID")); 421 422 KeyNamePair p = (KeyNamePair)fGreetingBP.getSelectedItem(); 423 int greetingBP = p.getKey(); 424 p = (KeyNamePair)fGreetingC.getSelectedItem(); 425 int greetingC = p.getKey(); 426 427 if (m_C_BPartner_ID == 0) 429 { 430 m_C_BPartner_ID = DB.getKeyNextNo(Env.getCtx(), m_WindowNo, "C_BPartner"); 431 Log.trace(Log.l4_Data, "Insert BPartner " + m_C_BPartner_ID); 432 String gr = "NULL"; 433 if (greetingBP > 0) 434 gr = "" + greetingBP; 435 SQL = "INSERT INTO C_BPartner" 436 + "(C_BPartner_ID," 437 + "AD_Client_ID,AD_Org_ID,IsActive,Created,CreatedBy,Updated,UpdatedBy," 438 + "Value,Name,Name2,C_Greeting_ID,IsSummary,IsOneTime,IsProspect,IsVendor,IsCustomer,IsEmployee,IsSalesRep," 439 + "AD_Language,C_BP_Group_ID,C_InvoiceSchedule_ID,C_PaymentTerm_ID," 440 + "M_PriceList_ID,M_DiscountSchema_ID,C_Dunning_ID,DocumentCopies,PaymentRule," 441 + "SO_CreditLimit, SO_CreditUsed) " 442 + "SELECT " + m_C_BPartner_ID + "," 444 + AD_Client_ID + "," + AD_Org_ID + ",'Y',SysDate," + UpdatedBy + ",SysDate," + UpdatedBy + "," 445 + DB.TO_STRING(value) + "," + DB.TO_STRING(name) + "," + DB.TO_STRING(fName2.getText()) 446 + "," + gr + ",'N','N','N','N','Y','N','N'," 447 + "AD_Language,C_BP_Group_ID,C_InvoiceSchedule_ID,C_PaymentTerm_ID," 448 + "p.M_PriceList_ID,M_DiscountSchema_ID,C_Dunning_ID,DocumentCopies,PaymentRule," 449 + "0,0 " 450 + "FROM C_BPartner p, AD_ClientInfo c " 451 + "WHERE c.AD_Client_ID=" + AD_Client_ID 452 + " AND p.C_BPartner_ID(+)=c.C_BPartnerCashTrx_ID"; 453 int i = stmt.executeUpdate(SQL); 454 if (i != 1) 455 ADialog.error(m_WindowNo, this, "BPartnerNotSaved", "\nPartner I#=" + i + ", SQL=" + SQL); 456 } 457 else if (!(OldValue.equals(value) && OldGreetingBP==greetingBP && OldName.equals(name) 459 && OldName2.equals(fName2.getText()) )) 460 { 461 Log.trace(Log.l4_Data, "Update BPartner " + m_C_BPartner_ID); 462 Integer gr = new Integer (greetingBP); 463 SQL = "UPDATE C_BPartner SET " 464 + "Value=" + DB.TO_STRING(value) + "," 465 + "C_Greeting_ID=" + (greetingBP == -1 ? "NULL" : gr.toString()) + "," 466 + "Name=" + DB.TO_STRING(name) + "," 467 + "Name2=" + DB.TO_STRING(fName2.getText()) + "," 468 + "Updated=SysDate, UpdatedBy=" + UpdatedBy 469 + " WHERE C_BPartner_ID=" + m_C_BPartner_ID; 470 int i = stmt.executeUpdate(SQL); 471 if (i != 1) 472 ADialog.error(m_WindowNo, this, "BPartnerNotSaved", "\nPartner U#=" + i + ", SQL=" + SQL); 473 } 474 475 if (m_C_BPartner_Location_ID == null && !fAddress.getValue().equals(new Integer (0))) 477 { 478 m_C_BPartner_Location_ID = new Integer (DB.getKeyNextNo(Env.getCtx(), m_WindowNo, "C_BPartner_Location")); 479 Log.trace(Log.l4_Data, "Insert BPartner Location " + m_C_BPartner_Location_ID + " (" + fAddress.getValue() + ")"); 480 SQL = "INSERT INTO C_BPartner_Location" 481 + "(C_BPartner_Location_ID, C_BPartner_ID," 482 + "AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy," 483 + "Name, C_Location_ID, Phone, Phone2, Fax)" 484 + " VALUES " 485 + "(" + m_C_BPartner_Location_ID + "," + m_C_BPartner_ID + "," 486 + AD_Client_ID + "," + AD_Org_ID + ",'Y',SysDate," + UpdatedBy + ",SysDate," + UpdatedBy + "," 487 + DB.TO_STRING(fAddress.getName()) + "," + fAddress.getValue() + "," 488 + DB.TO_STRING(fPhone.getText()) + "," + DB.TO_STRING(fPhone2.getText()) + "," 489 + DB.TO_STRING(fFax.getText()) + ")"; 490 int i = stmt.executeUpdate(SQL); 491 if (i != 1) 492 ADialog.error(m_WindowNo, this, "BPartnerNotSaved", "\nLocation I#=" + i + ", SQL=" + SQL); 493 } 494 else if (m_C_BPartner_Location_ID != null && 496 !(fAddress.getValue().equals(m_C_BPartner_Location_ID) 497 && OldPhone.equals(fPhone.getText()) && OldPhone2.equals(fPhone2.getText()) 498 && OldFax.equals(fFax.getText()) )) 499 { 500 Log.trace(Log.l4_Data, "Update BPartner Location " + m_C_BPartner_Location_ID); 501 SQL = "UPDATE C_BPartner_Location SET " 502 + "C_Location_ID=" + fAddress.getValue() + "," 503 + "Phone=" + DB.TO_STRING(fPhone.getText()) + "," 504 + "Phone2=" + DB.TO_STRING(fPhone2.getText()) + "," 505 + "Fax=" + DB.TO_STRING(fFax.getText()) + "," 506 + "Updated=SysDate, UpdatedBy=" + UpdatedBy 507 + " WHERE C_BPartner_Location_ID=" + m_C_BPartner_Location_ID; 508 int i = stmt.executeUpdate(SQL); 509 if (i != 1) 510 ADialog.error(m_WindowNo, this, "BPartnerNotSaved", "\nLocation U#=" + i + ", SQL=" + SQL); 511 } 512 513 if (!fEMail.getText().equals("") && fContact.getText().equals("")) 515 fContact.setText(fName.getText()); 516 if (m_AD_User_ID == null && !fContact.getText().equals("") 517 && 518 !(OldContact.equals(fContact.getText()) && OldTitle.equals(fTitle.getText()) 519 && OldPhone.equals(fPhone.getText()) && OldPhone2.equals(fPhone2.getText()) 520 && OldFax.equals(fFax.getText()) && OldEMail.equals(fEMail.getText()) )) 521 { 522 m_AD_User_ID = new Integer (DB.getKeyNextNo(Env.getCtx(), m_WindowNo, "AD_User")); 523 Log.trace(Log.l4_Data, "Insert BPartner User/Contact " + m_AD_User_ID); 524 String gr = "NULL"; 525 if (greetingC > 0) 526 gr = "" + greetingC; 527 SQL = "INSERT INTO AD_User" 528 + "(AD_User_ID, C_BPartner_ID," 529 + "AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy," 530 + "Name, C_Greeting_ID, Title, Phone, Phone2, Fax, EMail)" 531 + " VALUES " 532 + "(" + m_AD_User_ID + "," + m_C_BPartner_ID + "," 533 + AD_Client_ID + "," + AD_Org_ID + ",'Y',SysDate," + UpdatedBy + ",SysDate," + UpdatedBy + "," 534 + DB.TO_STRING(fContact.getText()) + "," + gr + "," 535 + DB.TO_STRING(fTitle.getText()) + "," + DB.TO_STRING(fPhone.getText()) + "," 536 + DB.TO_STRING(fPhone2.getText()) + "," + DB.TO_STRING(fFax.getText()) + "," 537 + DB.TO_STRING(fEMail.getText()) + ")"; 538 int i = stmt.executeUpdate(SQL); 539 if (i != 1) 540 ADialog.error(m_WindowNo, this, "BPartnerNotSaved", "\nContact I#=" + i + ", SQL=" + SQL); 541 } 542 else if (m_AD_User_ID != null && 544 !(OldContact.equals(fContact.getText()) && OldTitle.equals(fTitle.getText()) 545 && OldPhone.equals(fPhone.getText()) && OldPhone2.equals(fPhone2.getText()) 546 && OldFax.equals(fFax.getText()) && OldEMail.equals(fEMail.getText()) 547 && OldGreetingC==greetingC )) 548 { 549 Log.trace(Log.l4_Data, "Update BPartner User/Contact " + m_AD_User_ID); 550 if (fContact.getText().equals("")) 552 fContact.setText(name); 553 Integer gr = new Integer (greetingC); 554 SQL = "UPDATE AD_User SET " 555 + "Name=" + DB.TO_STRING(fContact.getText()) + "," 556 + "C_Greeting_ID=" + (greetingC == -1 ? "NULL" : gr.toString()) + "," 557 + "Title=" + DB.TO_STRING(fTitle.getText()) + "," 558 + "Phone=" + DB.TO_STRING(fPhone.getText()) + "," 559 + "Phone2=" + DB.TO_STRING(fPhone2.getText()) + "," 560 + "Fax=" + DB.TO_STRING(fFax.getText()) + "," 561 + "EMail=" + DB.TO_STRING(fEMail.getText()) + "," 562 + "Updated=SysDate, UpdatedBy=" + UpdatedBy 563 + " WHERE AD_User_ID=" + m_AD_User_ID; 564 int i = stmt.executeUpdate(SQL); 565 if (i != 1) 566 ADialog.error(m_WindowNo, this, "BPartnerNotSaved", "\nContact U#=" + i + ", SQL=" + SQL); 567 } 568 569 } 570 catch (SQLException e) 571 { 572 Log.error("VBPartner.actionSave\nSQL=" + SQL, e); 573 ADialog.error(m_WindowNo, this, "BPartnerNotSaved", e.getLocalizedMessage()); 574 m_C_BPartner_ID = 0; 575 return false; 576 } 577 578 return true; 579 } 581 582 586 public int getBPartner_ID() 587 { 588 return m_C_BPartner_ID; 589 } 591 592 595 class VLabel extends JLabel 596 { 597 public VLabel (String text) 598 { 599 super (text); 600 this.setHorizontalAlignment(JLabel.RIGHT); 601 } 602 } 604 } 606 | Popular Tags |