// YearForm.cpp : implementation file
//
#include "stdafx.h"
#include "LYFZSendMsg.h"
#include "YearForm.h"
#include "MyMdi.H"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// YearForm

IMPLEMENT_DYNCREATE(YearForm, CFormView)

YearForm::YearForm()
	: CFormView(YearForm::IDD)
{
	//{{AFX_DATA_INIT(YearForm)
	m_year = CTime::GetCurrentTime ().GetYear ();
	m_month = CTime::GetCurrentTime ().GetMonth ();
	m_day = CTime::GetCurrentTime ().GetDay ();
	m_bInit=0;
	m_timestmap=0;
	m_radio1 = 0;
	//}}AFX_DATA_INIT
	//m_date = CTime::GetCurrentTime ().Format ("%Y-%m-%d");
}

YearForm::~YearForm()
{
}

void YearForm::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(YearForm)
	DDX_Control(pDX, IDC_SPIN3, m_spinday);
	DDX_Control(pDX, IDC_SPIN2, m_spinmonth);
	DDX_Control(pDX, IDC_SPIN1, m_spinyear);
	DDX_Control(pDX, IDC_LIST2, m_List1);
	DDX_Control(pDX, IDC_STATIC1, m_static1);
	DDX_Text(pDX, IDC_EDITyear, m_year);
	DDV_MinMaxUInt(pDX, m_year, 1900, 3000);
	DDX_Text(pDX, IDC_EDITmonth, m_month);
	DDV_MinMaxUInt(pDX, m_month, 1, 12);
	DDX_Text(pDX, IDC_EDITday, m_day);
	DDV_MinMaxUInt(pDX, m_day, 1, 31);
	DDX_Radio(pDX, IDC_RADIO3, m_radio1);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(YearForm, CFormView)
	//{{AFX_MSG_MAP(YearForm)
	ON_BN_CLICKED(IDC_BUTclose, OnBUTclose)
	ON_EN_CHANGE(IDC_EDITyear, OnChangeEDITyear)
	ON_EN_CHANGE(IDC_EDITmonth, OnChangeEDITmonth)
	ON_EN_CHANGE(IDC_EDITday, OnChangeEDITday)
	ON_BN_CLICKED(IDC_RADIO3, OnRadio3)
	ON_BN_CLICKED(IDC_RADIO4, OnRadio4)
	ON_BN_CLICKED(IDC_RADIO5, OnRadio5)
	ON_BN_CLICKED(IDC_RADIO6, OnRadio6)
	ON_BN_CLICKED(IDC_RADIO7, OnRadio7)
	ON_BN_CLICKED(IDC_RADIO8, OnRadio8)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// YearForm diagnostics

#ifdef _DEBUG
void YearForm::AssertValid() const
{
	CFormView::AssertValid();
}

void YearForm::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// YearForm message handlers

void YearForm::OnInitialUpdate() 
{
	CFormView::OnInitialUpdate();
	// TODO: Add your specialized code here and/or call the base class
	CMyMdi Mdi;
	Mdi.SetSubView((CWnd*)GetParent(), (CWnd*)this);
	
	// Here we create the outbar control using the splitter as its parent
	// and setting its id to the first pane.
	CRect rc2;
	GetWindowRect(rc2);
	::MoveWindow(m_hWnd,g_rc.left,g_rc.top,g_rc.Width(),g_rc.Height(),TRUE);

 	EnumChildWindows(m_hWnd,(WNDENUMPROC)EnumChildProc,0);
	 


	m_static1.SetFont (&g_titlefont);


	m_List1.SetHeadings("��Ŀ����,300;���,100;��Ŀ���,100;����/֧��,100;����,100" );
 	m_List1.LoadColumnInfo (107);
 
	m_spinyear.SetRange (1900, 3000);
	m_spinmonth.SetRange (1, 12);
	m_spinday.SetRange (1, 31);
	
	m_bInit=1;
	DateChange();
}

void YearForm::FillGrid()
{
	m_List1.DeleteAllItems2 ();
	int ii=0;
	m_List1.m_arLabels.SetSize( m_List1array.GetSize (), 1 );
	int count=0;
	for(ii=0; ii<m_List1.m_arLabels.GetSize (); ii++)
	{
 
		m_List1.m_arLabels.ElementAt (count++).Copy (m_List1array.ElementAt (ii));
 
	}
	m_List1.m_arLabels.SetSize(count, 1);
	ii=count;
	m_List1.m_LabelCount=ii;
	m_List1.SetItemCountEx (ii);

	CString str;
	str.Format ("����:%d", ii);
	SetDlgItemText(IDC_STATIC2, str);
}

void YearForm::OnBUTclose() 
{
	// TODO: Add your control notification handler code here
	GetParent()->SendMessage(WM_CLOSE);
}

void YearForm::OnChangeEDITyear() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CFormView::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	    DateChange();
	// TODO: Add your control notification handler code here
	
}

void YearForm::OnChangeEDITmonth() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CFormView::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	  //  DateChange();
	// TODO: Add your control notification handler code here
	
}

void YearForm::OnChangeEDITday() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CFormView::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	//    DateChange();
	// TODO: Add your control notification handler code here
	
}
extern int GetYearPos(int year);
void YearForm::DateChange()
{
	if(m_bInit==0)return;
//	if(::GetTickCount ()-m_timestmap<500)return;
//	m_timestmap=::GetTickCount ();

	UpdateData();
	int i;
	int g_nYearposTemp=g_nYearpos;
	BOOL bLastYear=0;
	BOOL bThisYear=0;
	if(m_year==atoi(g_date.Left (4)) && g_hisyeararray.GetSize () )
		bThisYear=1;
	else if(m_year==atoi(g_date.Left (4))-1 && g_hisyeararray.GetSize ())
		bLastYear=1;
	g_nYearpos=GetYearPos(m_year);

 	g_sendhead.bsql=0;
	g_sendhead.code[0]=14;
	g_sendhead.code[1]=13;
	g_sendhead.code[2]=16;
	g_sendhead.tabcount=3;
	CString sql,strdate,strdate2;
	strdate.Format ("%04d-%02d-%02d", m_year, 1, 1);
	strdate2.Format ("%04d-%02d-%02d", m_year, 12, 31);
	m_static1.SetWindowText (strdate.Left (4)+"������");

	sql.Format ("dat>='"+strdate+"' and dat<='"+strdate2+"';dat>='"+strdate+"' and dat<='"+strdate2+"';date>='"+strdate+"' and date<='"+strdate2+"'");
	g_pMainWnd->ProcessChatMessageRequest2(sql);
	if(g_bSendOK==0)
	{
		g_nYearpos=g_nYearposTemp;
		return;
	}
	DataToArray(&List2array,&List3array,&List11array); 
	if(bLastYear)//�����ȥ��, ��ӽ���IJ���
	{
	g_nYearpos=-1;//����
	g_sendhead.bsql=0;
	g_sendhead.code[0]=16;
	g_sendhead.tabcount=1;
	CString sql,strdate,strdate2;
	strdate.Format ("%04d-%02d-%02d", m_year, 1, 1);
	strdate2.Format ("%04d-%02d-%02d", m_year, 12, 31);
	sql.Format ("date>='"+strdate+"' and date<='"+strdate2+"'");
	g_pMainWnd->ProcessChatMessageRequest2(sql);
	if(g_bSendOK==0)
	{
		g_nYearpos=g_nYearposTemp;
		return;
	}
	CArray<CStringArray, CStringArray>ThisYearList11array;
	DataToArray(&ThisYearList11array);
	int oldsize=List11array.GetSize ();
	int newsize=ThisYearList11array.GetSize ();
	List11array.SetSize(oldsize+ThisYearList11array.GetSize ());
	for(i=oldsize; i<oldsize+ThisYearList11array.GetSize (); i++)
	{
		List11array.ElementAt (i).Copy(ThisYearList11array.ElementAt (i-oldsize));
	}
	}
 	else if(bThisYear)//����ǽ���, ���ȥ��IJ���
	{
	g_nYearpos=0;//ȥ��
	g_sendhead.bsql=0;
	g_sendhead.code[0]=16;
	g_sendhead.tabcount=1;
	CString sql,strdate,strdate2;
	strdate.Format ("%04d-%02d-%02d", m_year, 1, 1);
	strdate2.Format ("%04d-%02d-%02d", m_year, 12, 31);
	sql.Format ("date>='"+strdate+"' and date<='"+strdate2+"'");
	g_pMainWnd->ProcessChatMessageRequest2(sql);
	if(g_bSendOK==0)
	{
		g_nYearpos=g_nYearposTemp;
		return;
	}
	CArray<CStringArray, CStringArray>LastYearList11array;
	DataToArray(&LastYearList11array);
	int oldsize=List11array.GetSize ();
	int newsize=LastYearList11array.GetSize ();
	List11array.SetSize(oldsize+LastYearList11array.GetSize ());
	for(i=oldsize; i<oldsize+LastYearList11array.GetSize (); i++)
	{
		List11array.ElementAt (i).Copy(LastYearList11array.ElementAt (i-oldsize));
	}
	}

    g_nYearpos=g_nYearposTemp;
 	KindChange();
}

void YearForm::KindChange()
{
	CString str;
	int pos=0;
	int dindanshu=0;
	float income=0;
	float payout=0;
	int count=List2array.GetSize ()+List3array.GetSize ()+List11array.GetSize ();
	m_List1array.RemoveAll ();
	m_List1array.SetSize(count);
	int i=0;

	if(m_radio1==0)
	{
    for( i=0; i<List11array.GetSize (); i++)
	{
		str=List11array.ElementAt (i).ElementAt (0);
		str+=";"+List11array.ElementAt (i).ElementAt (3);
		if(!List11array.ElementAt (i).ElementAt (1).IsEmpty ())
		{
			str+=";�ͻ�:"+List11array.ElementAt (i).ElementAt (1);
			if(!List11array.ElementAt (i).ElementAt (2).IsEmpty ())
				str+=","+List11array.ElementAt (i).ElementAt (2);

		}
		else
			str+=";�ͻ�:"+List11array.ElementAt (i).ElementAt (2);


		m_List1array.ElementAt (pos).Add(str);
		str=List11array.ElementAt (i).ElementAt (4);
		m_List1array.ElementAt (pos).Add(str);
		income+=atof(str);
		m_List1array.ElementAt (pos).Add(List11array.ElementAt (i).ElementAt (5));
		m_List1array.ElementAt (pos).Add("����");
		m_List1array.ElementAt (pos).Add(List11array.ElementAt (i).ElementAt (7));
		if(List11array.ElementAt (i).ElementAt (5)=="ԤԼ�տ�")
			dindanshu++;
		pos++;
	}


	for( i=0; i<List2array.GetSize (); i++)
	{
		str=List2array.ElementAt (i).ElementAt (1)+";"+List2array.ElementAt (i).ElementAt (4)+";"+List2array.ElementAt (i).ElementAt (6);
		m_List1array.ElementAt (pos).Add(str);
		str=List2array.ElementAt (i).ElementAt (2);
		m_List1array.ElementAt (pos).Add(str);
		income+=atof(str);
		m_List1array.ElementAt (pos).Add("��������");
		m_List1array.ElementAt (pos).Add("����");
		m_List1array.ElementAt (pos).Add(List2array.ElementAt (i).ElementAt (3));
		pos++;
	}

	for( i=0; i<List3array.GetSize (); i++)
	{
		str=List3array.ElementAt (i).ElementAt (1)+";"+List3array.ElementAt (i).ElementAt (4);
		m_List1array.ElementAt (pos).Add(str);
		str=List3array.ElementAt (i).ElementAt (2);
		m_List1array.ElementAt (pos).Add(str);
		payout+=atof(str);
		m_List1array.ElementAt (pos).Add("�̶�����");
		m_List1array.ElementAt (pos).Add("֧��");
		m_List1array.ElementAt (pos).Add(List3array.ElementAt (i).ElementAt (3));
		pos++;
	}
	}
	else if(m_radio1==1)//����
	{
    for( i=0; i<List11array.GetSize (); i++)
	{
		str=List11array.ElementAt (i).ElementAt (0);
		str+=";"+List11array.ElementAt (i).ElementAt (3);
		if(!List11array.ElementAt (i).ElementAt (1).IsEmpty ())
		{
			str+=";�ͻ�:"+List11array.ElementAt (i).ElementAt (1);
			if(!List11array.ElementAt (i).ElementAt (2).IsEmpty ())
				str+=","+List11array.ElementAt (i).ElementAt (2);

		}
		else
			str+=";�ͻ�:"+List11array.ElementAt (i).ElementAt (2);


		m_List1array.ElementAt (pos).Add(str);
		str=List11array.ElementAt (i).ElementAt (4);
		m_List1array.ElementAt (pos).Add(str);
		income+=atof(str);
		m_List1array.ElementAt (pos).Add(List11array.ElementAt (i).ElementAt (5));
		m_List1array.ElementAt (pos).Add("����");
		m_List1array.ElementAt (pos).Add(List11array.ElementAt (i).ElementAt (7));
		if(List11array.ElementAt (i).ElementAt (5)=="ԤԼ�տ�")
			dindanshu++;
		pos++;
	}


	for( i=0; i<List2array.GetSize (); i++)
	{
		str=List2array.ElementAt (i).ElementAt (1)+";"+List2array.ElementAt (i).ElementAt (4)+";"+List2array.ElementAt (i).ElementAt (6);
		m_List1array.ElementAt (pos).Add(str);
		str=List2array.ElementAt (i).ElementAt (2);
		m_List1array.ElementAt (pos).Add(str);
		income+=atof(str);
		m_List1array.ElementAt (pos).Add("��������");
		m_List1array.ElementAt (pos).Add("����");
		m_List1array.ElementAt (pos).Add(List2array.ElementAt (i).ElementAt (3));
		pos++;
	}
	}
	else if(m_radio1==2)//֧��
	{
	for( i=0; i<List3array.GetSize (); i++)
	{
		str=List3array.ElementAt (i).ElementAt (1)+";"+List3array.ElementAt (i).ElementAt (4);
		m_List1array.ElementAt (pos).Add(str);
		str=List3array.ElementAt (i).ElementAt (2);
		m_List1array.ElementAt (pos).Add(str);
		payout+=atof(str);
		m_List1array.ElementAt (pos).Add("�̶�����");
		m_List1array.ElementAt (pos).Add("֧��");
		m_List1array.ElementAt (pos).Add(List3array.ElementAt (i).ElementAt (3));
		pos++;
	}
	}
	else if(m_radio1==3)//����
	{
    for( i=0; i<List11array.GetSize (); i++)
	{
		if(List11array.ElementAt (i).ElementAt (5)!="ԤԼ�տ�")continue;
		dindanshu++;
		str=List11array.ElementAt (i).ElementAt (0);
		str+=";"+List11array.ElementAt (i).ElementAt (3);
		if(!List11array.ElementAt (i).ElementAt (1).IsEmpty ())
		{
			str+=";�ͻ�:"+List11array.ElementAt (i).ElementAt (1);
			if(!List11array.ElementAt (i).ElementAt (2).IsEmpty ())
				str+=","+List11array.ElementAt (i).ElementAt (2);

		}
		else
			str+=";�ͻ�:"+List11array.ElementAt (i).ElementAt (2);


		m_List1array.ElementAt (pos).Add(str);
		str=List11array.ElementAt (i).ElementAt (4);
		m_List1array.ElementAt (pos).Add(str);
		income+=atof(str);
		m_List1array.ElementAt (pos).Add(List11array.ElementAt (i).ElementAt (5));
		m_List1array.ElementAt (pos).Add("����");
		m_List1array.ElementAt (pos).Add(List11array.ElementAt (i).ElementAt (7));

		pos++;
	}
	}
	else if(m_radio1==4)//���ռ���
	{
    for( i=0; i<List11array.GetSize (); i++)
	{
		if(List11array.ElementAt (i).ElementAt (5)!="���ղ���")continue;
		str=List11array.ElementAt (i).ElementAt (0);
		str+=";"+List11array.ElementAt (i).ElementAt (3);
		if(!List11array.ElementAt (i).ElementAt (1).IsEmpty ())
		{
			str+=";�ͻ�:"+List11array.ElementAt (i).ElementAt (1);
			if(!List11array.ElementAt (i).ElementAt (2).IsEmpty ())
				str+=","+List11array.ElementAt (i).ElementAt (2);

		}
		else
			str+=";�ͻ�:"+List11array.ElementAt (i).ElementAt (2);


		m_List1array.ElementAt (pos).Add(str);
		str=List11array.ElementAt (i).ElementAt (4);
		m_List1array.ElementAt (pos).Add(str);
		income+=atof(str);
		m_List1array.ElementAt (pos).Add(List11array.ElementAt (i).ElementAt (5));
		m_List1array.ElementAt (pos).Add("����");
		m_List1array.ElementAt (pos).Add(List11array.ElementAt (i).ElementAt (7));

		pos++;
	}
	}
	else if(m_radio1==5)//ѡƬ����
	{
    for( i=0; i<List11array.GetSize (); i++)
	{
		if(List11array.ElementAt (i).ElementAt (5)!="ѡƬ����")continue;
		str=List11array.ElementAt (i).ElementAt (0);
		str+=";"+List11array.ElementAt (i).ElementAt (3);
		if(!List11array.ElementAt (i).ElementAt (1).IsEmpty ())
		{
			str+=";�ͻ�:"+List11array.ElementAt (i).ElementAt (1);
			if(!List11array.ElementAt (i).ElementAt (2).IsEmpty ())
				str+=","+List11array.ElementAt (i).ElementAt (2);

		}
		else
			str+=";�ͻ�:"+List11array.ElementAt (i).ElementAt (2);


		m_List1array.ElementAt (pos).Add(str);
		str=List11array.ElementAt (i).ElementAt (4);
		m_List1array.ElementAt (pos).Add(str);
		income+=atof(str);
		m_List1array.ElementAt (pos).Add(List11array.ElementAt (i).ElementAt (5));
		m_List1array.ElementAt (pos).Add("����");
		m_List1array.ElementAt (pos).Add(List11array.ElementAt (i).ElementAt (7));

		pos++;
	}
	}

	m_List1array.SetSize(pos);
	str.Format ("%f", income);
	::ConvertToPrice (str);
	SetDlgItemText(IDC_EDITmoney1, str);

	str.Format ("%f", payout);
	::ConvertToPrice (str);
	SetDlgItemText(IDC_EDITmoney2, str);

	str.Format ("%f", income-payout);
	::ConvertToPrice (str);
	SetDlgItemText(IDC_EDITmoney3, str);

	str.Format ("%d", dindanshu);
	SetDlgItemText(IDC_EDITmoney4, str);
    FillGrid();
}

void YearForm::OnRadio3() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	KindChange();
}

void YearForm::OnRadio4() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	KindChange();	
}

void YearForm::OnRadio5() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	KindChange();	
}

void YearForm::OnRadio6() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	KindChange();	
}

void YearForm::OnRadio7() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	KindChange();	
}

void YearForm::OnRadio8() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	KindChange();	
}