Files

1240 lines
32 KiB
C++

// PMCheckDlg.cpp : implementation file
//
#include "stdafx.h"
#include "sp.h"
#include "PMCheckDlg.h"
#include "PM.h"
#include "PMViewPrompt.h"
#include "gzCurrencyFormatter.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//memory leak debugging help
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
/////////////////////////////////////////////////////////////////////////////
// CPMCheckDlg dialog
CPMCheckDlg::CPMCheckDlg(CWnd* pParent /*=NULL*/)
: CDialog(CPMCheckDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CPMCheckDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
m_pApp= (CSpApp*)AfxGetApp();
/*
rs=new GZRset("PMCHECK rs error:");
rs->SetConnect(m_pApp->strConnectString);
rs2=new GZRset("PMCHECK rs2 error:");
rs2->SetConnect(m_pApp->strConnectString);
rs3=new GZRset("PMCHECK rs3 error:");
rs3->SetConnect(m_pApp->strConnectString);
*/
//Initialize recordset pointer
rs=m_pApp->rsPool->GetRS("CPMCheckDlg (RS)");
rs2=m_pApp->rsPool->GetRS("CPMCheckDlg (RS2)");
rs3=m_pApp->rsPool->GetRS("CPMCheckDlg (RS3)");
}
void CPMCheckDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CPMCheckDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
DDX_Control(pDX, IDC_REPORT, m_rc);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CPMCheckDlg, CDialog)
//{{AFX_MSG_MAP(CPMCheckDlg)
ON_COMMAND(ID_PMVIEW_DONE, OnPmviewDone)
ON_COMMAND(ID_PMVIEW_SHOWDATEITEMS, OnPmviewShowdateitems)
ON_COMMAND(ID_PMVIEW_SHOWMETERED, OnPmviewShowmetered)
ON_COMMAND(ID_PMVIEW_SELECTEDITEM_CONVERTTOWORKORDER, OnPmviewSelecteditemConverttoworkorder)
ON_COMMAND(ID_PMVIEW_SELECTEDITEM_DELETE, OnPmviewSelecteditemDelete)
ON_COMMAND(ID_PMVIEW_SELECTEDITEM_EDIT, OnPmviewSelecteditemEdit)
ON_COMMAND(ID_PMVIEW_SELECTEDITEM_HOLDRELEASE, OnPmviewSelecteditemHoldrelease)
ON_NOTIFY(RVN_ITEMDBCLICK, IDC_REPORT, OnRvnItemDbClick)
ON_NOTIFY(RVN_COLUMNCLICK, IDC_REPORT, OnColumnClick)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CPMCheckDlg message handlers
CPMCheckDlg::~CPMCheckDlg()
{
m_pApp->rsPool->ReleaseRS(&rs->m_nID);
m_pApp->rsPool->ReleaseRS(&rs2->m_nID);
m_pApp->rsPool->ReleaseRS(&rs3->m_nID);
}
void CPMCheckDlg::OnRvnItemDbClick(NMHDR* pNMHDR, LRESULT* pResult)
{
CString q,link;
long lData;
bool isclient,ismodel;
LPNMREPORTVIEW lpnmrv = (LPNMREPORTVIEW)pNMHDR;
if(lpnmrv->iItem >=0)//-1 if clicked on invalid
{
q.Format("SELECT pmhead.id, pmhead.ismodel, pmhead.isclient, pmhead.link "
"FROM pmitems INNER JOIN pmhead ON pmitems.pmschedlink = pmhead.id "
"WHERE (((pmitems.id)=%s));",m_rc.GetItemText(lpnmrv->iItem,7));
rs2->Query(q);
if(rs2->IsEmpty())
{
AfxMessageBox("There is a problem trying to retrieve the\r\nmaster record for this item.");
return;
}
rs2->FetchField("ismodel",&ismodel);
rs2->FetchField("isclient",&isclient);
rs2->FetchField("link",&lData);
link.Format("%u",lData);
CPM d;
d.PreInitialize(isclient,ismodel,link,"Edit PM scheduled item");
d.DoModal();
FillView();
}
*pResult = FALSE;
}
//===================================
BOOL CPMCheckDlg::OnInitDialog()
{
CDialog::OnInitDialog();
m_resize.Create( this );
Security();
ShowWindow(SW_HIDE);
CWaitCursor wait;
//defaults
m_bShowDateFormat=true;
//Set the initial sort order for the columns
for(int xx=0;xx<9;xx++)
bColumnSortAsc[xx]=FALSE;
m_strOrderBy.Empty();
CString profile,q;
q.Format("SELECT users.pmprofile "
"FROM users WHERE (((users.id)=%u));",m_pApp->m_lusrID);
rs->Query(q);
rs->FetchField("pmprofile",&profile);
m_ilReport.Create(IDB_BM2, 16, 1, RGB(255,0,255));
m_rc.SetImageList(&m_ilReport);
m_rc.InsertColor(0, 0x00C0D8C0);
m_rc.InsertColor(1, ::GetSysColor(COLOR_GRAYTEXT));
m_rc.InsertColor(2, 0x00D0C0C0);
m_rc.InsertColor(3, 0x00804000);
RVCOLUMN rvc;
rvc.nFormat = RVCF_TEXT|RVCF_LEFT;//RVCF_TEXT|RVCF_EX_AUTOWIDTH|RVCF_SUBITEM_IMAGE;
rvc.iImage = 1;
rvc.lpszText = _T("Client");
rvc.iWidth = 60;
m_rc.DefineColumn(0, &rvc);
rvc.nFormat = RVCF_TEXT;
rvc.lpszText = "Date";
rvc.iWidth = 60;
m_rc.DefineColumn(1, &rvc);
rvc.lpszText = "Service meter";
rvc.iWidth = 60;
m_rc.DefineColumn(2, &rvc);
rvc.lpszText = "Last reading";
rvc.iWidth = 60;
m_rc.DefineColumn(3, &rvc);
rvc.nFormat = RVCF_TEXT;
rvc.lpszText = "Hold";
rvc.iWidth = 60;
m_rc.DefineColumn(4, &rvc);
rvc.lpszText = "Tech.";
rvc.iWidth = 60;
m_rc.DefineColumn(5, &rvc);
rvc.lpszText = "Description";
rvc.iWidth = 60;
m_rc.DefineColumn(6, &rvc);
rvc.nFormat = RVCF_TEXT | RVCF_EX_FIXEDWIDTH;
rvc.lpszText = "pmheadid";
rvc.iWidth = 0;
m_rc.DefineColumn(7, &rvc);
//setup according to users preferences
if(!profile.IsEmpty())
m_rc.GetProfile(&profile);
else
{
//not needed when setting by pref
//but if there are no preferences then you would need it
m_rc.ActivateColumn(0, 0);
m_rc.ActivateColumn(1, 1);
m_rc.ActivateColumn(2, 2);
m_rc.ActivateColumn(3, 3);
m_rc.ActivateColumn(4, 4);
m_rc.ActivateColumn(5, 5);
m_rc.ActivateColumn(6, 6);
m_rc.ActivateColumn(7, 7);
}
m_rc.ModifyStyle(0, RVS_SINGLESELECT);
//m_rc.ModifyStyle(0, RVS_SHOWCOLORALTERNATE);
//turn off the grids
m_rc.ModifyStyle(RVS_SHOWVGRID, 0);
m_rc.ModifyStyle(RVS_SHOWHGRID, 0);
//retrieve the loaner items and display them
FillView();
m_resize.Add(IDC_REPORT,0,0,100,100);
//Use the current width and height as the minimum size
m_resize.SetMinimumTrackingSize();
//===================================================
// SET WINDOW SIZE TO MATCH WORK AREA
ShowWindow(SW_SHOWMAXIMIZED);
CRect workarea;
SystemParametersInfo(SPI_GETWORKAREA,0,&workarea,0);
SetWindowPos(NULL,workarea.left,workarea.top,workarea.Width(),workarea.Height(),SWP_NOZORDER);
//===================================================
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CPMCheckDlg::FillView()
{
int x=0;
CString q,strTemp,strClient,strTech,strDetails;
COleDateTime dtDate;
long lMeter,lLastMeter,lID,lLink;
bool bHold;
bool bIsClient;
if(m_bShowDateFormat)
strTemp=" = 0";
else
strTemp=" <> 0";
/*q.Format("SELECT IIf(IsNull([clients]![company]),[clients]![first] & \" \" & [clients]![last],[clients]![company]) AS clientname, pmitems.schedate, pmitems.schedmeter, pmitems.hold, pmhead.description, pmitems.id, IIf([pmitems].[schedtech]=0,\"Any\",[users].[last] & \", \" & [users].[first]) AS techname, pmitems.link "
"FROM ((pmitems LEFT JOIN clients ON pmitems.clientlink = clients.id) LEFT JOIN pmhead ON pmitems.pmschedlink = pmhead.id) LEFT JOIN users ON pmitems.schedtech = users.id "
"WHERE (((pmitems.schedmeter)%s)) %s;",strTemp, m_strOrderBy);*/
q.Format("SELECT pmitems.isclient, IIf(IsNull([clients]![company]),[clients]![first] "
"& \" \" & [clients]![last],[clients]![company]) "
"AS clientname, pmitems.schedate, "
"pmitems.schedmeter, pmitems.hold, pmhead.description, "
"pmitems.id, IIf([pmitems].[schedtech]=0,\"Any\",[users].[last] "
"& \", \" & [users].[first]) AS techname, "
"pmitems.link FROM ((pmitems LEFT JOIN clients "
"ON pmitems.clientlink = clients.id) LEFT JOIN pmhead "
"ON pmitems.pmschedlink = pmhead.id) LEFT JOIN "
"users ON pmitems.schedtech = users.id WHERE (((pmitems.schedmeter)%s)) %s; ",strTemp, m_strOrderBy);
//vvvvvvvvvvvvvvvvvvv
//#ifdef _DEBUG
// m_pApp->ShowStuff(q);
//#endif
//^^^^^^^^^^^^^^^^^^^
rs->Query(q);
m_rc.DeleteAllItems();
if(rs->IsEmpty()) return;
do
{
//added 02/21/2003
rs->FetchField("isclient",&bIsClient);
rs->FetchField("clientname",&strClient);
//avoid the underline in the report control
//for a single &
strClient.Replace("&","&&");
rs->FetchField("schedate",&dtDate);
rs->FetchField("schedmeter",&lMeter);
rs->FetchField("hold",&bHold);
rs->FetchField("description",&strDetails);
rs->FetchField("techname",&strTech);
rs->FetchField("id",&lID);
rs->FetchField("link",&lLink);
//Set the look of the row and insert it
//CLIENT
RVITEM rvi;
rvi.iItem = x;
rvi.iSubItem = 0;
rvi.nMask = RVIM_TEXT;
//rvi.nState = 0;
//rvi.nPreview = 0;//no preview necessary
rvi.lpszText = strClient.GetBuffer(strClient.GetLength());
rvi.lParam = x;
m_rc.InsertItem(&rvi);
//DATE
if(!m_bShowDateFormat)
strTemp="n/a";
else
strTemp=dtDate.Format();
rvi.iSubItem = 1;
rvi.nMask = RVIM_TEXT;
rvi.lpszText = strTemp.GetBuffer(strTemp.GetLength());
m_rc.SetItem(&rvi);
//METER
if(m_bShowDateFormat)
strTemp="n/a";
else
strTemp.Format("%u",lMeter);
rvi.iSubItem = 2;
rvi.lpszText = strTemp.GetBuffer(strTemp.GetLength());
m_rc.SetItem(&rvi);
//LAST METER READING
//LOOK UP LAST METER READING HERE
//**************************************************************
if(m_bShowDateFormat)
strTemp="n/a";
else
{
q.Format("SELECT units.lastmeter FROM units WHERE (((units.id)=%u));",lLink);
rs2->Query(q);
if(rs2->IsEmpty())
{
strTemp="n/a";
}
else
{
rs2->FetchField("lastmeter",&lLastMeter);
strTemp.Format("%u",lLastMeter);
}
}
rvi.iSubItem = 3;
rvi.lpszText = strTemp.GetBuffer(strTemp.GetLength());
m_rc.SetItem(&rvi);
//HOLD
if(bHold)
strTemp="HELD";
else
strTemp="Active";
rvi.iSubItem = 4;
rvi.lpszText = strTemp.GetBuffer(strTemp.GetLength());
m_rc.SetItem(&rvi);
//TECH
rvi.iSubItem = 5;
rvi.lpszText = strTech.GetBuffer(strTech.GetLength());
m_rc.SetItem(&rvi);
//DETAILS
//If it's a unit, get the serial number here and
//append to details
if(!bIsClient)
{
/*q.Format("SELECT units.sn AS USN, clients.company AS OWNER, unitmodels.model AS MODEL, "
"nonclients.company_person AS MANU, unitmodels.description AS DESCRIPT "
"FROM (((pmhead LEFT JOIN units ON pmhead.link = units.id) "
"LEFT JOIN unitmodels ON units.model = unitmodels.id) "
"LEFT JOIN clients ON units.client = clients.id) "
"LEFT JOIN nonclients ON unitmodels.manufacturer "
"= nonclients.id WHERE (((pmhead.id)=%u));",lLink);*/
//FIX v1.9.4.3
q.Format("SELECT units.sn AS USN, clients.company AS OWNER, unitmodels.model "
"AS MODEL, nonclients.company_person "
"AS MANU, unitmodels.description AS DESCRIPT FROM "
"((units LEFT JOIN unitmodels ON units.model = unitmodels.id) "
"LEFT JOIN clients ON units.client = clients.id) "
"LEFT JOIN nonclients ON unitmodels.manufacturer "
"= nonclients.id WHERE (((units.id)=%u));",lLink);
//#ifdef _DEBUG
// m_pApp->ShowStuff(q);
//#endif
if(!rs2->QueryReadOnly(q)) goto BAIL;
if(rs2->IsEmpty()) goto BAIL;
//Ok, we have a record and are ready to retrieve the details
if(!rs2->FetchField("USN",&strTemp)) goto BAIL;
strDetails+=" Unit - sn:" + strTemp;
if(!rs2->FetchField("MANU",&strTemp)) goto BAIL;
strDetails+=" " + strTemp;
if(!rs2->FetchField("MODEL",&strTemp)) goto BAIL;
strDetails+=" " + strTemp;
}
BAIL:
rvi.iSubItem = 6;
rvi.lpszText = strDetails.GetBuffer(strDetails.GetLength());
m_rc.SetItem(&rvi);
//ID
strTemp.Format("%u",lID);
rvi.iSubItem = 7;
rvi.lpszText = strTemp.GetBuffer(strTemp.GetLength());
m_rc.SetItem(&rvi);
x++;
}while(rs->MoveForward());
}
void CPMCheckDlg::OnPmviewDone()
{
CString profile,q;
//save to user prefs.
m_rc.WriteProfile(&profile);
q.Format("UPDATE users SET users.pmprofile = \"%s\" "
"WHERE (((users.id)=%u));",profile,m_pApp->m_lusrID);
rs->Ex(q);
CDialog::OnOK();
}
void CPMCheckDlg::OnPmviewShowdateitems()
{
if(!m_bShowDateFormat)
FlipMeterDate();
}
void CPMCheckDlg::OnPmviewShowmetered()
{
if(m_bShowDateFormat)
FlipMeterDate();
}
//Toggle meter/date mode
void CPMCheckDlg::FlipMeterDate()
{
CMenu* mmenu = GetMenu();
CMenu* submenu = mmenu->GetSubMenu(0);
UINT state = submenu->GetMenuState(ID_PMVIEW_SHOWMETERED, MF_BYCOMMAND);
ASSERT(state != 0xFFFFFFFF);
if (state & MF_CHECKED)
{
submenu->CheckMenuItem(ID_PMVIEW_SHOWMETERED, MF_UNCHECKED | MF_BYCOMMAND);
submenu->CheckMenuItem(ID_PMVIEW_SHOWDATEITEMS, MF_CHECKED | MF_BYCOMMAND);
m_bShowDateFormat=true;
}
else
{
submenu->CheckMenuItem(ID_PMVIEW_SHOWMETERED, MF_CHECKED | MF_BYCOMMAND);
submenu->CheckMenuItem(ID_PMVIEW_SHOWDATEITEMS, MF_UNCHECKED | MF_BYCOMMAND);
m_bShowDateFormat=false;
}
FillView();
}
//SET ORDER BY CLAUSE DEPENDING ON CLICK HERE
void CPMCheckDlg::OnColumnClick(NMHDR* pNMHDR, LRESULT* pResult)
{
CWaitCursor Wait;
int x;
CString strOrder;
CString str;
CFlatHeaderCtrl* header;
header=m_rc.GetHeaderCtrl();
//FIND CLICKED COLUMN
str=header->m_szHotItemText;
if(str=="Client")
{
x=0;
goto OUTTAHERE;
}
if(str=="Date")
{
x=1;
goto OUTTAHERE;
}
if(str=="Service meter")
{
x=2;
goto OUTTAHERE;
}
if(str=="Last reading")
{
x=3;
goto OUTTAHERE;
}
if(str=="Hold")
{
x=4;
goto OUTTAHERE;
}
if(str=="Tech.")
{
x=5;
goto OUTTAHERE;
}
if(str=="Description")
{
x=6;
goto OUTTAHERE;
}
OUTTAHERE:;
bColumnSortAsc[x]=!bColumnSortAsc[x];
if(!bColumnSortAsc[x])
strOrder=" DESC";
else
strOrder.Empty();
switch(x)
{//DESC
case 0://client
m_strOrderBy.Format("ORDER BY IIf(IsNull([clients]![company]),[clients]![first] & \" \" & [clients]![last],[clients]![company]) %s, pmitems.id, pmitems.schedate, pmitems.schedmeter",strOrder);
break;
case 1://scheddate
m_strOrderBy.Format("ORDER BY pmitems.schedate %s, IIf(IsNull([clients]![company]),[clients]![first] & \" \" & [clients]![last],[clients]![company]), pmitems.id, pmitems.schedmeter",strOrder);
break;
case 2://
m_strOrderBy.Format("ORDER BY pmitems.schedmeter %s, IIf(IsNull([clients]![company]),[clients]![first] & \" \" & [clients]![last],[clients]![company]), pmitems.id, pmitems.schedate",strOrder);
break;
case 3://Last meter reading/not sorted now
m_strOrderBy="";
break;
case 4://hold
m_strOrderBy.Format("ORDER BY pmitems.hold %s, IIf(IsNull([clients]![company]),[clients]![first] & \" \" & [clients]![last],[clients]![company]), pmitems.id, pmitems.schedate, pmitems.schedmeter",strOrder);
break;
case 5://tech
m_strOrderBy.Format("ORDER BY IIf([pmitems].[schedtech]=0,\"Any\",[users].[last] & \", \" & [users].[first]) %s, IIf(IsNull([clients]![company]),[clients]![first] & \" \" & [clients]![last],[clients]![company]), pmitems.id, pmitems.schedate, pmitems.schedmeter, pmitems.hold",strOrder);
break;
case 6://
m_strOrderBy.Format("ORDER BY pmhead.description %s, IIf(IsNull([clients]![company]),[clients]![first] & \" \" & [clients]![last],[clients]![company]), pmitems.id, pmitems.schedate, pmitems.schedmeter",strOrder);
break;
}
header->SetSortColumn(header->GetHotIndex(),bColumnSortAsc[x]);
*pResult = TRUE;//TRUE means we handled it here thanks anyway
FillView();
}
void CPMCheckDlg::OnPmviewSelecteditemConverttoworkorder()
{
CString q;
int x;
x=m_rc.GetFirstSelectedItem();
if(x<0)
{
AfxMessageBox("You must select one or more items from the list first.");
return;//no selection
}
if(AfxMessageBox("Convert this item to a workorder now? \r\nAre you sure?",MB_YESNO)==IDNO) return;
//still here? OK
MakeIntoWorkorder(m_rc.GetItemText(x,7));
FillView();
}
//Delete item(s)
void CPMCheckDlg::OnPmviewSelecteditemDelete()
{
CPMViewPrompt d;
int nReturn=0,x;
CString q,strTemp,strID;
long lID;
x=m_rc.GetFirstSelectedItem();
if(x<0)
{
AfxMessageBox("You must select one or more items from the list first.");
return;//no selection
}
d.Setup("Delete items? Are you sure?", "Delete items",&nReturn);
d.DoModal();
if(nReturn==0) return;
strID=m_rc.GetItemText(x,7);
if(nReturn==1) //delete series items only
{
//delete this pmitem only.
q.Format("DELETE pmitems.* FROM pmitems WHERE (((pmitems.id)=%s));",strID);
rs2->Ex(q);
}
else
{
//DELETE HEADER RECORD
//locate header record ID
q.Format("SELECT pmitems.pmschedlink FROM pmitems WHERE (((pmitems.id)=%s));",strID);
rs2->Query(q);
if(rs2->IsEmpty())
{
AfxMessageBox("Error locating header record ID\r\nCan't do it.");
return;
}
rs2->FetchField("pmschedlink",&lID);
//delete header record
q.Format("DELETE pmhead.*, pmhead.id FROM pmhead WHERE (((pmhead.id)=%u));",lID);
rs2->Ex(q);
//delete all detail items
q.Format("DELETE pmitems.*, pmitems.pmschedlink "
"FROM pmitems WHERE (((pmitems.pmschedlink)=%u));",lID);
rs2->Ex(q);
}
FillView();
}
void CPMCheckDlg::OnPmviewSelecteditemEdit()
{
int x;
CString q,link;
long lData;
bool isclient,ismodel;
x=m_rc.GetFirstSelectedItem();
if(x<0)
{
AfxMessageBox("You must select one or more items from the list first.");
return;//no selection
}
//CLoanersEditDlg d;
//CString item=m_rc.GetItemText(lpnmrv->iItem,6);
//d.SetRentalID(&item);
//d.DoModal();
q.Format("SELECT pmhead.id, pmhead.ismodel, pmhead.isclient, pmhead.link "
"FROM pmitems INNER JOIN pmhead ON pmitems.pmschedlink = pmhead.id "
"WHERE (((pmitems.id)=%s));",m_rc.GetItemText(x,7));
//strID=m_rc.GetItemText(x,7);
rs2->Query(q);
if(rs2->IsEmpty())
{
AfxMessageBox("There is a problem trying to retrieve the\r\nmaster record for this item.");
return;
}
rs2->FetchField("ismodel",&ismodel);
rs2->FetchField("isclient",&isclient);
rs2->FetchField("link",&lData);
link.Format("%u",lData);
CPM d;
d.PreInitialize(isclient,ismodel,link,"Edit PM scheduled item");
d.DoModal();
FillView();
}
//HOLD/RELEASE
void CPMCheckDlg::OnPmviewSelecteditemHoldrelease()
{
int x;
CString q,tf;
bool bHold;
x=m_rc.GetFirstSelectedItem();
if(x<0)
{
AfxMessageBox("You must select one or more items from the list first.");
return;//no selection
}
q.Format("SELECT pmitems.hold FROM pmitems "
"WHERE (((pmitems.id)=%s));",m_rc.GetItemText(x,7));
rs2->Query(q);
if(rs2->IsEmpty())
{
AfxMessageBox("There is a problem trying to open this record.");
return;
}
rs2->FetchField("hold",&bHold);
if(bHold)
tf="False";
else
tf="True";
q.Format("UPDATE pmitems SET pmitems.hold = %s "
"WHERE (((pmitems.id)=%s));",tf,m_rc.GetItemText(x,7));
//m_pApp->ShowStuff(q);
rs2->Ex(q);
FillView();
}
//************************************************************************
// CONVERT PM ITEM TO WORKORDER
//
//************************************************************************
//make a PM Item into a workorder
void CPMCheckDlg::MakeIntoWorkorder(CString strPMItem)
{
CString q,strDescription,strNotes;
long lHeaderID,lTech,lLink,lClient,lModifier,lCategory,lProject,lWOID,lProbID;
long lSchedMeter,lHighestPMID,lRepeat,lDays, lWeeks, lDOW;
bool bIsClient,bQuick,bOnsite,bAutorpt,bAutoWO, bHold;
int nDOW,d,nTemp;
COleDateTime dtScheduled,dtNow,dtDefault,dtStartTime,dtEndTime,dtSchedStart,dtSchedStop;
dtNow=COleDateTime::GetCurrentTime();
dtDefault.SetDate(1968,03,12);
//Get all the details from the detail record first
q.Format("SELECT * "
"FROM pmitems WHERE (((pmitems.id)=%s));",strPMItem);
rs2->Query(q);
if(rs2->IsEmpty())
{
q.Format("Error converting PM Item %s to workorder",strPMItem);
AfxMessageBox(q);
return;
}
rs2->FetchField("pmschedlink",&lHeaderID);
rs2->FetchField("schedtech",&lTech);
rs2->FetchField("link",&lLink);
rs2->FetchField("isclient",&bIsClient);
rs2->FetchField("clientlink",&lClient);
rs2->FetchField("schedmeter",&lSchedMeter);
rs2->FetchField("schedate",&dtScheduled);
rs2->FetchField("autowo",&bAutoWO);
rs2->FetchField("hold",&bHold);
/*PMDAYS*/
//Get the rest of the info from the header record
q.Format("SELECT pmhead.description, pmhead.notes, pmhead.modifier, pmhead.autorpt, pmhead.dow, "
"pmhead.repeatevery, pmhead.rptdays, pmhead.rptweeks, pmhead.wotype, pmhead.woquick, "
"pmhead.woproject, pmhead.woonsite, pmhead.starttime, pmhead.endtime "
"FROM pmhead WHERE (((pmhead.id)=%u));",lHeaderID);
rs2->Query(q);
if(rs2->IsEmpty())
{
q.Format("Error converting PM head item %u to workorder",lHeaderID);
AfxMessageBox(q);
return;
}
rs2->FetchField("description",&strDescription);
rs2->FetchField("notes",&strNotes);
rs2->FetchField("modifier",&lModifier);
rs2->FetchField("wotype",&lCategory);
rs2->FetchField("woquick",&bQuick);
rs2->FetchField("woproject",&lProject);
rs2->FetchField("woonsite",&bOnsite);
rs2->FetchField("autorpt",&bAutorpt);
rs2->FetchField("dow",&lDOW);
rs2->FetchField("repeatevery",&lRepeat);
rs2->FetchField("rptdays",&lDays);
rs2->FetchField("rptweeks",&lWeeks);
rs2->FetchField("starttime",&dtStartTime);
rs2->FetchField("endtime",&dtEndTime);
//Move the date into the time fields
dtSchedStart.SetDateTime(dtScheduled.GetYear(),dtScheduled.GetMonth(),dtScheduled.GetDay(),
dtStartTime.GetHour(),dtStartTime.GetMinute(),dtStartTime.GetSecond());
// dtSchedStop
dtSchedStop.SetDateTime(dtScheduled.GetYear(),dtScheduled.GetMonth(),dtScheduled.GetDay(),
dtEndTime.GetHour(),dtEndTime.GetMinute(),dtEndTime.GetSecond());
/*PMDAYS*/
//FIXUP DATE
if(dtScheduled.GetYear()==1968)
dtScheduled=COleDateTime::GetCurrentTime();
//PROCESS INTO A WORKORDER
q.Format("INSERT INTO wo ( "
"project, client, type, closed, onsite, "
"anytime, assigntech, quick, creator, modifier, created, modified, "
"starttime, stoptime, notes, pmid ) "
"SELECT %u, %u, %u, #%s#, %s, %s, %u, %s, %u, %u, "
"#%s#, #%s#, #%s#, #%s#, \"%s\",%u;",
lProject, lClient, lCategory, bQuick ? dtScheduled.Format(_T("%m/%d/%Y %H:%M:%S")) : "03/12/1968",
bOnsite ? "True" : "False", "False",
lTech, bQuick ? "True" : "False", lModifier, lModifier,
dtNow.Format(_T("%m/%d/%Y %H:%M:%S")),dtNow.Format(_T("%m/%d/%Y %H:%M:%S")),
/*starttime*/dtSchedStart.Format(_T("%m/%d/%Y %H:%M:%S")),
/*stoptime*/dtSchedStop.Format(_T("%m/%d/%Y %H:%M:%S")),
strDescription,lHeaderID);
//PROCESS INTO A WORKORDER
//q.Format("INSERT INTO wo ( "
// "project, client, type, closed, onsite, "
// "anytime, assigntech, quick, creator, modifier, created, modified, "
// "starttime, stoptime, notes ) "
// "SELECT %u, %u, %u, #%s#, %s, False, %u, %s, %u, %u, "
// "#%s#, #%s#, #%s#, #%s#, %s;",
// lProject, lClient, lCategory, bQuick ? dtScheduled.Format(_T("%m/%d/%Y %H:%M:%S")) : "03/12/1968",
// bOnsite ? "True" : "False", lTech, bQuick ? "True" : "False",
// lModifier, lModifier, dtNow.Format(_T("%m/%d/%Y %H:%M:%S")),
// dtNow.Format(_T("%m/%d/%Y 12:00:00")),
// /*starttime*/dtSchedStart.Format(_T("%m/%d/%Y %H:%M:%S")),
// /*stoptime*/dtSchedStop.Format(_T("%m/%d/%Y %H:%M:%S")),
// strDescription);
///////m_pApp->ShowStuff(q);
rs2->Ex(q);
rs2->Close();
/*
//Fetch workorder ID just created
q.Format("SELECT wo.id FROM wo "
"WHERE (((wo.created)=#%s#) AND ((wo.creator)=%u) AND ((wo.client)=%u));",
dtNow.Format(_T("%m/%d/%Y %H:%M:%S")),lModifier,lClient);
*/
//NOTE: MODIFIED 11/03/00 added TOP 1 and order by
q.Format("SELECT TOP 1 wo.id FROM wo "
"WHERE (((wo.created)=#%s#) AND ((wo.creator)=%u) AND ((wo.client)=%u)) ORDER BY wo.id DESC;",
dtNow.Format(_T("%m/%d/%Y %H:%M:%S")),lModifier,lClient);
rs2->Query(q);
if(rs2->IsEmpty())
{
AfxMessageBox("Error: Can't locate the workorder record just created.\r\n Contact tech support with the details.");
return;
}
rs2->FetchField("id",&lWOID);
//Create a companion problem record
if(bQuick)
q.Format("INSERT INTO probs ( wolink, creator, created, pmscheduleid ) "
"SELECT %u, %u, #%s#, %u;", lWOID, lModifier, dtNow.Format(_T("%m/%d/%Y %H:%M:%S")), lHeaderID);
else
q.Format("INSERT INTO probs ( "
"wolink, creator, created, brief, notes, pmscheduleid, unit ) "
"SELECT %u, %u, #%s#, \"%s\", \"%s\", %u, %u;",
lWOID, lModifier, dtNow.Format(_T("%m/%d/%Y %H:%M:%S")), strDescription,
strNotes, lHeaderID, bIsClient ? 0 : lLink) ;//if client 0 else link=unit
rs2->Ex(q);
rs2->Close();
//Fetch problem ID just created for parts or labor entries
q.Format("SELECT probs.id FROM probs WHERE (((probs.wolink)=%u));",lWOID);
rs2->Query(q);
if(rs2->IsEmpty())
{
AfxMessageBox("Error: Can't locate the workorder record (problem id) just created.\r\n Contact tech support with the details.");
return;
}
rs2->FetchField("id",&lProbID);
//QUICK WORKORDER: CREATE A LABOUR RECORD
if(bQuick)
{
//v1.9.4.4 get and set default client rates
//various changes in this block
long lDefRate=0;
long lDefTravelRate=0;
q.Format("SELECT clients.defrate, clients.deftravelrate FROM "
"clients WHERE (((clients.id)=%u));",lClient);
rs3->QueryReadOnly(q);
if(!rs3->IsEmpty())
{
rs3->FetchField("defrate",&lDefRate);
rs3->FetchField("deftravelrate",&lDefTravelRate);
}
rs3->Close();
q.Format("INSERT INTO labor ( link, tech, rate, travrate, start, stop, details ) "
"SELECT %u, %u,%u, %u, #%s#, #%s#, \"%s\";",lProbID,lTech,lDefRate,lDefTravelRate,
dtScheduled.Format(_T("%m/%d/%Y")),dtScheduled.Format(_T("%m/%d/%Y")),
strDescription + "\r\n" + strNotes+"\r\n----------\r\n");
rs2->Ex(q);
rs2->Close();
//v1.9.4.4 end of changes in this block
/* PRE 1944 version
q.Format("INSERT INTO labor ( link, tech, start, stop, details ) "
"SELECT %u, %u, #%s#, #%s#, \"%s\";",lProbID,lTech,
dtScheduled.Format(_T("%m/%d/%Y %H:%M:%S")),dtScheduled.Format(_T("%m/%d/%Y %H:%M:%S")),
"Preventive maintenance:\r\n" + strDescription + "\r\n" + strNotes+"\r\n----------\r\n");
rs2->Ex(q);
rs2->Close();*/
}
//STANDARD WORKORDER: CHECK FOR PARTS AND CREATE WOPARTS RECORDS AS NECESSARY
if(!bQuick)
{
q.Format("SELECT pmparts.pmlink, pmparts.partnum, pmparts.quantity, parts.retail, parts.avgcost "
"FROM pmparts LEFT JOIN parts ON pmparts.partnum = parts.id "
"WHERE (((pmparts.pmlink)=%u));",lHeaderID);
rs2->QueryReadOnly(q);
if(!rs2->IsEmpty())
{
COleCurrency crData;
COleCurrency crCost;
CgzCurrencyFormatter cfm;
long lPartID;
float fQuant;
//CREATE PARTS RECORDS UNTIL NO MORE
do {
rs2->FetchField("partnum",&lPartID);
rs2->FetchField("quantity",&fQuant);
rs2->FetchField("retail",&crData);
rs2->FetchField("avgcost",&crCost);
q.Format("INSERT INTO woparts ( "
"link, partnum, quantity, price, cost, usedby, misc ) "
"SELECT %u, %u, %f, '%s', '%s', %u, Null;",lProbID,lPartID,fQuant,cfm.Format(crData),
cfm.Format(crCost),lTech);
rs3->Ex(q);
rs3->Close();
}while(rs2->MoveForward());
}
}
//RESCHEDULE TO REPLACE CONVERTED ITEM OR DELETE IF NOT RECURRING
if(!bAutorpt)
{
//Remove PM ITEM here if not recurring, else just change date/meter count:
q.Format("DELETE pmitems.* FROM pmitems WHERE pmitems.id=%s;",strPMItem);
rs2->Ex(q);
}
else
{//increment this record.
//Find out which record is the highest
q.Format("SELECT TOP 1 pmitems.id "
"FROM pmitems "
"WHERE (((pmitems.pmschedlink)=%u) AND ((pmitems.link)=%u)) "
"ORDER BY pmitems.id DESC;",lHeaderID,lLink);
rs2->Query(q);
if(rs2->IsEmpty())
{
AfxMessageBox("Error: Can't locate the highest pmschedule for rescheduling.\r\n Contact tech support with the details.");
return;
}
rs2->FetchField("id",&lHighestPMID);
//ASCERTAIN THE HIGHEST RECORD
if(lHighestPMID!=atol(strPMItem))//are were already at the highest to convert to workorder?
{//if no: load in the highest records meter and or date data for processing
q.Format("SELECT pmitems.schedate, pmitems.schedmeter FROM pmitems "
"WHERE (((pmitems.id)=%u));",lHighestPMID);
rs2->Query(q);
if(rs2->IsEmpty())
{
AfxMessageBox("Error: Can't open the highest pmschedule for rescheduling.\r\n Contact tech support with the details.");
return;
}
rs2->FetchField("schedmeter",&lSchedMeter);
rs2->FetchField("schedate",&dtScheduled);
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//INCREMENT DATE OR METER DEPENDING
if(dtScheduled.GetYear()==1968)//it's meter based
{
//Process as a meter based record
//this one's easy:
lSchedMeter+=lRepeat;
}
else //calculate next date based pmitem record
{
//prepare a simpledate class object
CSimpleDate sdTemp(dtScheduled.GetMonth(),dtScheduled.GetDay(),dtScheduled.GetYear());
//Add the months, then increment to the right
//day of week if necessary.
sdTemp.AddDays(lDays);
sdTemp.AddDays(lWeeks*7);
sdTemp.AddMonths(lRepeat);
//*******************************************************************
//ADD DAYS HERE
//*******************************************************************
//Advance to day of week selected.
//temporary version of nDOW to matchup sunday as first day of week
//make our desired date numeric base match the one returned by the getdayofweek
nDOW=(int)lDOW;
if(nDOW==7) //sunday=0 in CSimpleDate land
nTemp=0;
else
nTemp=nDOW;
d=sdTemp.GetDayOfWeek();
if(nDOW==0)//any weekday
{
if(d==0)//sunday
sdTemp.AddDays(1);//move to monday
if(d==6)//saturday
sdTemp.SubtractDays(1);//move to Friday
}
else//specific day
{
while(d!=nTemp)
{
sdTemp.AddDays(1);
d=sdTemp.GetDayOfWeek();
}
}
//set dtTemp back again.
dtScheduled.SetDate(sdTemp.GetYear(),sdTemp.GetMonth(),sdTemp.GetDay());
}
//<<<<<<<<<<<<<<<<<<<<<
//OK, at this point we have incremented either the date
//or the meter reading and can now insert the replacement record
//and then delete the old record
//done this way rather than updating existing record because were
//relying on a higher id value to indicate the farthest "flung" scheduled item
q.Format("INSERT INTO pmitems ( "
"pmschedlink, schedate, schedmeter, autowo, hold, schedtech, "
"link, isclient, clientlink) "
"SELECT %u, #%s#, %u, %s, %s, %u, %u, %s, %u;",
lHeaderID, dtScheduled.Format(_T("%m/%d/%Y")), lSchedMeter, bAutoWO ? "True" : "False",
bHold ? "True" : "False", lTech, lLink, bIsClient ? "True" : "False",
lClient);
rs2->Ex(q);
rs2->Close();
q.Format("DELETE pmitems.* FROM pmitems WHERE pmitems.id=%s;",strPMItem);
rs2->Ex(q);
}
}
}
void CPMCheckDlg::Security()
{
m_bReadOnly=false;
int x=m_pApp->Allowed(RPM,true);
if(x==0)//no access allowed
{
m_pApp->SecurityWarning();
CDialog::OnCancel();
}
if(x==2)//read only
{
m_bReadOnly=true;
CMenu* pMenu=this->GetMenu();
pMenu->EnableMenuItem(1, MF_BYPOSITION | MF_DISABLED | MF_GRAYED);
}
}