// PartsDlg.cpp : implementation file // #include "stdafx.h" #include "sp.h" #include "PartsDlg.h" #include "NonClientsDlg.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CPartsDlg dialog CPartsDlg::CPartsDlg(CWnd* pParent /*=NULL*/) : CDialog(CPartsDlg::IDD, pParent) , m_strCurrentVendorID(_T("")) { //{{AFX_DATA_INIT(CPartsDlg) // NOTE: the ClassWizard will add member initialization here //}}AFX_DATA_INIT m_bAddMode=false; m_pApp= (CSpApp*)AfxGetApp(); /* rs=new GZRset("Error: Parts dialog"); rs->SetConnect(m_pApp->strConnectString); cbrs=new GZRset("Error: Parts dialog combo box recordset\r\n"); cbrs->SetConnect(m_pApp->strConnectString); */ //Initialize recordset pointer rs=m_pApp->rsPool->GetRS("CPartsDlg (RS)"); cbrs=m_pApp->rsPool->GetRS("CPartsDlg (CBRS)"); rsPrint=m_pApp->rsPool->GetRSPrint("CPartsDlg (rsPrint)"); cfm = new CgzCurrencyFormatter; m_pstrReturnValue=NULL; m_strSelectedPart.Empty(); m_bNoRecord=true; } CPartsDlg::~CPartsDlg() { rs->Close(); cbrs->Close(); rsPrint->Close(); delete cfm; } void CPartsDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CPartsDlg) DDX_Control(pDX, IDC_LBLMODIFIED, m_lblModified); DDX_Control(pDX, IDC_LBLPART, m_lblPart); DDX_Control(pDX, IDC_EDRETAIL, m_edRetail); DDX_Control(pDX, IDC_EDPARTNUM, m_edPartNumber); DDX_Control(pDX, IDC_EDNOTES, m_edNotes); DDX_Control(pDX, IDC_EDDESCRIPTION, m_edDescription); DDX_Control(pDX, IDC_EDCOST, m_edCost); DDX_Control(pDX, IDC_CKACTIVE, m_ckActive); DDX_Control(pDX, IDC_CBREPLACEDBY, m_cbReplacedBy); DDX_Control(pDX, IDC_CBPARTS, m_cbParts); DDX_Control(pDX, IDC_BTNDONE, m_btnDone); DDX_Control(pDX, IDC_BTNDELETE, m_btnDelete); DDX_Control(pDX, IDC_BTNADD, m_btnAdd); //}}AFX_DATA_MAP DDX_Control(pDX, IDC_LBL_VENDORS, m_lblVendors); DDX_Control(pDX, IDC_CBVENDORS, m_cbVendors); } BEGIN_MESSAGE_MAP(CPartsDlg, CDialog) //{{AFX_MSG_MAP(CPartsDlg) ON_EN_KILLFOCUS(IDC_EDRETAIL, OnKillfocusEdretail) ON_EN_KILLFOCUS(IDC_EDPARTNUM, OnKillfocusEdpartnum) ON_EN_KILLFOCUS(IDC_EDNOTES, OnKillfocusEdnotes) ON_EN_KILLFOCUS(IDC_EDDESCRIPTION, OnKillfocusEddescription) ON_EN_KILLFOCUS(IDC_EDCOST, OnKillfocusEdcost) ON_BN_CLICKED(IDC_CKACTIVE, OnCkactive) ON_CBN_CLOSEUP(IDC_CBREPLACEDBY, OnCloseupCbreplacedby) ON_CBN_CLOSEUP(IDC_CBPARTS, OnCloseupCbparts) ON_BN_CLICKED(IDC_BTNDONE, OnBtndone) ON_BN_CLICKED(IDC_BTNDELETE, OnBtndelete) ON_BN_CLICKED(IDC_BTNADD, OnBtnadd) ON_BN_CLICKED(IDC_BTNPRINT, OnBtnprint) //}}AFX_MSG_MAP ON_STN_CLICKED(IDC_LBL_VENDORS, OnStnClickedLblVendors) ON_CBN_CLOSEUP(IDC_CBVENDORS, OnCbnCloseupCbvendors) END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CPartsDlg message handlers //************************************************************ //SAVE EDIT CONTROL FIELD //************************************************************ bool CPartsDlg::SaveField(CEdit *edControl,CString fldname,bool AllowEmpty) { if(m_bAddMode || m_bNoRecord) return true;//dont attempt to update CString str; //do nothing if not changed if(edControl->GetModify()!=TRUE) return true; edControl->GetWindowText(str); //dont save empty fields if not allowed to if(!AllowEmpty) { if(str.IsEmpty()) { edControl->Undo(); return false; } } rs->UpdateField(fldname,&str); rs->SaveRecord(); UpdateModified(); return true; } //************************************************************ //SAVE GZCOMBO CONTROL FIELD //************************************************************ bool CPartsDlg::SaveField(CgzCombo *cbControl,CString fldname) { if(m_bAddMode || m_bNoRecord) return true;//dont attempt to update CString str; long lData; str=cbControl->GetCurrentRowID(); if(str.IsEmpty()) return false;//shouldn't happen but... lData=atol(str); rs->UpdateField(fldname,&lData); rs->SaveRecord(); UpdateModified(); return true; } //************************************************************ //SAVE CheckBox CONTROL FIELD //************************************************************ bool CPartsDlg::SaveField(CButton *ckControl,CString fldname) { if(m_bAddMode || m_bNoRecord) return true;//dont attempt to update bool bData=false; BOOL BData; BData=ckControl->GetCheck(); if(BData==TRUE) bData=true; rs->UpdateField(fldname,&bData); rs->SaveRecord(); UpdateModified(); return true; } //************************************************ //UPDATE MODIFIED DATA //************************************************* bool CPartsDlg::UpdateModified() { //updates modified by and modified on fields rs->UpdateField("modifier",&m_pApp->m_lusrID); COleDateTime dtData; dtData=COleDateTime::GetCurrentTime(); rs->UpdateField("modified",&dtData); rs->SaveRecord(); return true; } //************************************************************ //SAVE Currency in an edit CONTROL FIELD //************************************************************ bool CPartsDlg::SaveCurrencyField(CEdit *crControl,CString fldname) { if(m_bAddMode || m_bNoRecord) return true;//dont attempt to update CString str; COleCurrency crData; crControl->GetWindowText(str); if(str.IsEmpty()) str="0"; if(crData.ParseCurrency(str)!=TRUE) { AfxMessageBox("Could not interpret your entry in one or more currency fields\r\n" "I can't save those fields until they are changed."); return false; } rs->UpdateField(fldname,&crData); rs->SaveRecord(); UpdateModified(); return true; } void CPartsDlg::OnKillfocusEdretail() { SaveCurrencyField(&m_edRetail,"retail"); } void CPartsDlg::OnKillfocusEdpartnum() { SaveField(&m_edPartNumber,"partnumber",false); if(!m_bAddMode) FillList(); } void CPartsDlg::OnKillfocusEdnotes() { SaveField(&m_edNotes,"notes",true); } void CPartsDlg::OnKillfocusEddescription() { SaveField(&m_edDescription,"description",true); if(!m_bAddMode) FillList(); } void CPartsDlg::OnKillfocusEdcost() { SaveCurrencyField(&m_edCost,"avgcost"); } void CPartsDlg::OnCkactive() { SaveField(&m_ckActive,"active"); } void CPartsDlg::OnCloseupCbreplacedby() { SaveField(&m_cbReplacedBy,"supersededby"); } void CPartsDlg::OnCbnCloseupCbvendors() { SaveField(&m_cbVendors,"supplier"); m_strCurrentVendorID=m_cbVendors.GetCurrentRowID(); } //************************************* void CPartsDlg::OnCloseupCbparts() { //FILL FIELDS CString strData,q; bool bData; COleCurrency crData; //added Nov 19th 2000 COleDateTime dtModified; long lData; strData=m_cbParts.GetCurrentRowID(); if(strData=="0") { ClearFields(); m_bNoRecord=true; return; } m_bNoRecord=false; q.Format("SELECT parts.* FROM parts " "WHERE (((parts.id)=%s));",m_cbParts.GetCurrentRowID()); rs->Query(q); if(rs->IsEmpty()) { m_bNoRecord=true; return; } //REPLACED BY rs->FetchField("supersededby",&lData); strData.Format("%u",lData); m_cbReplacedBy.Select(strData); //ACTIVE rs->FetchField("active",&bData); m_ckActive.SetCheck(bData ? TRUE : FALSE); //COST rs->FetchField("avgcost",&crData); m_edCost.SetWindowText(cfm->Format(crData)); //RETAIL rs->FetchField("retail",&crData); m_edRetail.SetWindowText(cfm->Format(crData)); //DESCRIPTION rs->FetchField("description",&strData); m_edDescription.SetWindowText(strData); //NOTES rs->FetchField("notes",&strData); m_edNotes.SetWindowText(strData); //PART NUMBER rs->FetchField("partnumber",&strData); m_edPartNumber.SetWindowText(strData); //added Nov 19th 2000 //LAST MODIFIED rs->FetchField("modified",&dtModified); m_lblModified.SetWindowText(dtModified.Format()); rs->FetchField("supplier",&lData); m_strCurrentVendorID.Format("%u",lData); m_cbVendors.Select(lData); m_strSelectedPart=m_cbParts.GetCurrentRowID(); } //************************************* void CPartsDlg::OnBtndone() { if(m_pstrReturnValue!=NULL) *m_pstrReturnValue=m_strSelectedPart; CDialog::OnOK(); } //*********************************** void CPartsDlg::OnBtndelete() { CString q; if(m_bAddMode) { m_bAddMode=false; m_lblPart.ShowWindow(TRUE); m_cbParts.ShowWindow(TRUE); m_btnDelete.SetWindowText("Delete"); m_btnAdd.SetWindowText("Add"); m_btnDone.ShowWindow(TRUE); m_strSelectedPart.Empty(); FillList(); return; } //DELETE..... //can't delete the built in sn... if(m_strSelectedPart=="0") { return; } //check if unused and deletable //IN PMPARTS??? q.Format("SELECT pmparts.partnum FROM pmparts " "WHERE (((pmparts.partnum)=%s));",m_strSelectedPart); cbrs->Query(q); if(!cbrs->IsEmpty()) { AfxMessageBox("DATA INTEGRITY PROTECTION:\r\n" "This part appears in one or more\r\n" "Preventive Maintenance schedules\r\n" "and cannot be deleted at this time."); return; } //IN WOPARTS??? q.Format("SELECT woparts.partnum FROM woparts " "WHERE (((woparts.partnum)=%s));",m_strSelectedPart); cbrs->Query(q); if(!cbrs->IsEmpty()) { AfxMessageBox("DATA INTEGRITY PROTECTION:\r\n" "This part appears in one or more workorders\r\n" "and cannot be deleted at this time."); return; } //IN PARTS as a supersede??? q.Format("SELECT parts.supersededby FROM parts " "WHERE ((( parts.supersededby)=%s));",m_strSelectedPart); cbrs->Query(q); if(!cbrs->IsEmpty()) { AfxMessageBox("DATA INTEGRITY PROTECTION:\r\n" "This part appears in one or more other part\r\n" "records as a replacement part number\r\n" "and cannot be deleted at this time."); return; } if(AfxMessageBox("Permanently delete part?",MB_YESNO)==IDYES) { q.Format("DELETE parts.*, parts.id " "FROM parts WHERE (((parts.id)=%s));",m_strSelectedPart); rs->Ex(q); m_strSelectedPart=""; FillList(); } } //***************************************************** void CPartsDlg::OnBtnadd() { CString strData,newpn; bool bData; long lData; COleCurrency crCost,crRetail; if(!m_bAddMode) { //go add mode DisableAll(false); m_bAddMode=true; m_btnDelete.SetWindowText("CANCEL"); m_btnAdd.SetWindowText("SAVE"); m_btnDone.ShowWindow(FALSE); m_lblPart.ShowWindow(FALSE); m_cbParts.ShowWindow(FALSE); ClearFields(); return; } else {//SAVE Record m_edPartNumber.GetWindowText(newpn); if(newpn.IsEmpty()) { AfxMessageBox("A part number is required to proceed"); return; } //SCAN CURRENCY FIELDS //COST m_edCost.GetWindowText(strData); if(strData.IsEmpty()) strData="0"; if(crCost.ParseCurrency(strData)!=TRUE) { AfxMessageBox("I don't understand your entry in the cost field\r\n" "Please change it to a valid currency value."); return; } //RETAIL m_edRetail.GetWindowText(strData); if(strData.IsEmpty()) strData="0"; if(crRetail.ParseCurrency(strData)!=TRUE) { AfxMessageBox("I don't understand your entry in the retail price field\r\n" "Please change it to a valid currency value."); return; } //hack as keeps saying recordset is not open rs->Query("SELECT parts.* from parts WHERE parts.id = 0;"); if(!rs->AddNewRecord()) return; //SAVE PARTNUMBER rs->UpdateField("partnumber",&newpn); //SAVE CREATOR INFO COleDateTime dtData; rs->UpdateField("creator",&m_pApp->m_lusrID); rs->UpdateField("modifier",&m_pApp->m_lusrID); dtData=COleDateTime::GetCurrentTime(); rs->UpdateField("created",&dtData); rs->UpdateField("modified",&dtData); //REPLACED BY strData=m_cbReplacedBy.GetCurrentRowID(); lData=atol(strData); rs->UpdateField("supersededby",&lData); //Vendor strData=m_cbVendors.GetCurrentRowID(); lData=atol(strData); rs->UpdateField("supplier",&lData); //ACTIVE bData=(m_ckActive.GetCheck() ? true : false); rs->UpdateField("active",&bData); //COST / RETAIL rs->UpdateField("avgcost",&crCost); rs->UpdateField("retail",&crRetail); //DESCRIPTION m_edDescription.GetWindowText(strData); rs->UpdateField("description",&strData); //NOTES m_edNotes.GetWindowText(strData); rs->UpdateField("notes",&strData); //ATTEMPT TO SAVE if(!rs->SaveRecord()) return; m_bAddMode=false; m_lblPart.ShowWindow(TRUE); m_cbParts.ShowWindow(TRUE); m_btnDelete.SetWindowText("Delete"); m_btnAdd.SetWindowText("Add"); m_btnDone.ShowWindow(TRUE); //hack as keeps saying recordset is not open rs->Query("SELECT parts.* from parts;"); FillList(); //m_cbParts.SelectString(-1,newpn); //OnCloseupCbparts(); return; } } //**************************************************** //Fill list box with items void CPartsDlg::FillList() { CString strData; CString strIndex; long lData; m_cbParts.Clear(); m_cbReplacedBy.Clear(); strData="< No replacement >"; m_cbReplacedBy.AddRow(strData,strIndex); cbrs->Query("SELECT parts.id, [partnumber] & \" - \" " "& [description] AS name FROM parts " "ORDER BY parts.partnumber;"); if(cbrs->IsEmpty()) { DisableAll(true); return; } cbrs->MoveFirst(); do { cbrs->FetchField("name",&strData); cbrs->FetchField("id",&lData); strIndex.Format("%u",lData); m_cbParts.AddRow(strData,strIndex); m_cbReplacedBy.AddRow(strData,strIndex); }while(cbrs->MoveForward()); //pretend user has selected so that other fields get filled in if(m_strSelectedPart.IsEmpty()) //first time in { m_cbParts.SetCurSel(0); m_strSelectedPart=m_cbParts.GetCurrentRowID(); } else//something valid was selected before so stick with it m_cbParts.Select(m_strSelectedPart); m_cbReplacedBy.Select("0"); OnCloseupCbparts(); } BOOL CPartsDlg::OnInitDialog() { CDialog::OnInitDialog(); Security(); //FUTURE: Add a search by description ability //make a "hyperlink" m_lblVendors.SetTextColor(RGB(0,0,255)); m_lblVendors.SetFontUnderline(TRUE); m_lblVendors.SetLink(TRUE); m_lblVendors.SetLinkCursor(m_pApp->hcHand); //empty url means it will not launch explorer //and can be used as a button instead m_lblVendors.SetLinkURL(""); //added 02/06/2002 m_edDescription.SetLimitText(50); m_edPartNumber.SetLimitText(50); FillList(); FillVendorList(); return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE } void CPartsDlg::ClearFields() { m_cbReplacedBy.Select("0"); m_ckActive.SetCheck(TRUE); m_edCost.SetWindowText("0"); m_edDescription.SetWindowText(""); m_edNotes.SetWindowText(""); m_edPartNumber.SetWindowText(""); m_edRetail.SetWindowText("0"); } void CPartsDlg::SetReturnValue(CString *str) { m_pstrReturnValue=str; } void CPartsDlg::DisableAll(bool disable) { BOOL e=disable ? FALSE : TRUE; m_btnDelete.EnableWindow(e); m_cbParts.EnableWindow(e); m_cbReplacedBy.EnableWindow(e); m_ckActive.EnableWindow(e); m_edCost.EnableWindow(e); m_edDescription.EnableWindow(e); m_edNotes.EnableWindow(e); m_edPartNumber.EnableWindow(e); m_edRetail.EnableWindow(e); } void CPartsDlg::Security() { //m_bReadOnly=false; int x=m_pApp->Allowed(RPARTS,true); if(x==0)//no access allowed { m_pApp->SecurityWarning(); CDialog::OnCancel(); } if(x==2)//read only { // m_bReadOnly=true; m_btnAdd.ShowWindow(FALSE); m_btnDelete.ShowWindow(FALSE); m_cbReplacedBy.EnableWindow(FALSE); m_ckActive.EnableWindow(FALSE); m_edCost.SetReadOnly(TRUE); m_edDescription.SetReadOnly(TRUE); m_edNotes.SetReadOnly(TRUE); m_edPartNumber.SetReadOnly(TRUE); m_edRetail.SetReadOnly(TRUE); } } void CPartsDlg::OnBtnprint() { CString q,strReport; q="SELECT parts.* FROM parts ORDER BY parts.partnumber;"; //dont allow this to creep into a release build #ifdef _DEBUG // m_pApp->ShowStuff(q); #endif rsPrint->Query(q); if(!rsPrint->IsEmpty()) { #ifdef _DEBUG m_pApp->CreateTTX("parts.ttx",rsPrint->RecordSetPointer()); #endif m_pApp->PrintCMReportRDC("Parts",rsPrint->RecordSetPointer()); } else { AfxMessageBox("There is no data to print!"); } rsPrint->Close(); } void CPartsDlg::OnOK() {} void CPartsDlg::OnStnClickedLblVendors() { CNonClientsDlg d; CString strTempVendor=m_strCurrentVendorID; if(strTempVendor=="0") strTempVendor.Empty(); long ltype=3;//manufacturers suppliers d.SetType(<ype); d.m_strSelectedContact=strTempVendor; d.m_pstrReturnValue=&strTempVendor; //&m_strCurrentVendorID; d.DoModal(); FillVendorList(); } void CPartsDlg::FillVendorList(void) { CString q,strData,strIndex; long lData; m_cbVendors.Clear(); //default, no selection m_cbVendors.AddRow(" ","0"); q.Format("SELECT nonclients.*, nonclienttypes.description AS LISTNAME " "FROM nonclients LEFT JOIN nonclienttypes ON " "nonclients.type = nonclienttypes.id " "WHERE (((nonclients.type)=3)) " "ORDER BY nonclients.company_person;"); if(!cbrs->QueryReadOnly(q)) return; //v1.9.4.4 change from a return to just skip over if no records //so that the selection will take place of the N/A. default. if(!cbrs->IsEmpty()) { do{ cbrs->FetchField("company_person",&strData); cbrs->FetchField("id",&lData); strIndex.Format("%u",lData); m_cbVendors.AddRow(strData,strIndex); }while(cbrs->MoveForward()); } //default on empty (constructed) is zero if(m_strCurrentVendorID.IsEmpty()) m_strCurrentVendorID="0"; m_cbVendors.Select(m_strCurrentVendorID); }