// VC6ExDlg.cpp : implementation file
//

#include "stdafx.h"
#include "stdlib.h"
#include "VC6Ex.h"
#include "VC6ExDlg.h"
#include "DlgProxy.h"
#include "fprnm1c.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

IFprnM45 ECR;

/////////////////////////////////////////////////////////////////////////////
// CVC6ExDlg dialog

IMPLEMENT_DYNAMIC(CVC6ExDlg, CDialog);

CVC6ExDlg::CVC6ExDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CVC6ExDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CVC6ExDlg)
	//}}AFX_DATA_INIT
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_pAutoProxy = NULL;
}

CVC6ExDlg::~CVC6ExDlg()
{
	if (m_pAutoProxy != NULL)
		m_pAutoProxy->m_pDialog = NULL;
}

void CVC6ExDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CVC6ExDlg)
	DDX_Control(pDX, IDC_BTNSTART, m_btnStart);
	DDX_Control(pDX, IDC_BTNSHOWPROPERTIES, m_btnShowProperties);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CVC6ExDlg, CDialog)
	//{{AFX_MSG_MAP(CVC6ExDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
		ON_BN_CLICKED(IDC_BTNSHOWPROPERTIES, OnBtnshowproperties)
	ON_BN_CLICKED(IDC_BTNSTART, OnBtnstart)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CVC6ExDlg message handlers

BOOL CVC6ExDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CVC6ExDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

HCURSOR CVC6ExDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

BOOL CVC6ExDlg::CanExit()
{
	if (m_pAutoProxy != NULL)
	{
		ShowWindow(SW_HIDE);
		return FALSE;
	}

	return TRUE;
}

void Demo()
{
//  
  ECR.SetDeviceEnabled(true);
  if (ECR.GetResultCode() != 0) return;

//   
  if (ECR.GetStatus() != 0) return;

//       
  if (ECR.GetFiscal())
    if (AfxMessageBox(" !    ?", MB_ICONEXCLAMATION + MB_YESNO) == IDNO)
      return;

//    ,   
  if (ECR.GetCheckState() != 0)
    if (ECR.CancelCheck() != 0) return;

//     Z-
  if (ECR.GetSessionOpened())
  {
    //     
    ECR.Set_Password("30");
    //      
    ECR.SetMode(3);
    if (ECR.SetMode() != 0) return;
    //  
    ECR.SetReportType(1);
    if (ECR.Report() != 0) return;
  };

//    
  //   
  ECR.Set_Password("1");
  //    
  ECR.SetMode(1);
  if (ECR.SetMode() != 0) return;

//   
  //  
  ECR.SetName("");
  ECR.SetPrice(10.45);
  ECR.SetQuantity(1);
  ECR.SetDepartment(2);
  if (ECR.Registration() != 0) return;
  //     
  ECR.SetPercents(10);
  ECR.SetDestination(1);
  if (ECR.PercentsDiscount() != 0) return;
  //  
  ECR.SetName("");
  ECR.SetPrice(25);
  ECR.SetQuantity(5);
  ECR.SetDepartment(1);
  if (ECR.Registration() != 0) return;
  //     
  ECR.Set_Summ(10.40);
  ECR.SetDestination(0);
  if (ECR.SummDiscount() != 0) return;
  //         
  ECR.SetTypeClose(0);
  if (ECR.CloseCheck() != 0) return;

//   
  //  
  ECR.SetName("");
  ECR.SetPrice(10.45);
  ECR.SetQuantity(1);
  ECR.SetDepartment(2);
  if (ECR.Registration() != 0) return;
  //  
  ECR.SetName("-");
  ECR.SetPrice(25.00);
  ECR.SetQuantity(5);
  ECR.SetDepartment(1);
  if (ECR.Registration() != 0) return;
  //   
  if (ECR.Storno() != 0) return;
  //  
  ECR.SetName("");
  ECR.SetPrice(25);
  ECR.SetQuantity(5);
  ECR.SetDepartment(1);
  if (ECR.Registration() != 0) return;
  //     
  ECR.Set_Summ(50);
  ECR.SetDestination(0);
  if (ECR.SummDiscount() != 0) return;
  //         
  ECR.Set_Summ(100);
  ECR.SetTypeClose(0);
  if (ECR.Delivery() != 0) return;

// 
  //  
  ECR.SetName("Dirol");
  ECR.SetPrice(7);
  ECR.SetQuantity(1);
  if (ECR.Annulate() != 0) return;
  //  
  ECR.SetName("Orbit");
  ECR.SetPrice(8);
  ECR.SetQuantity(2);
  if (ECR.Annulate() != 0) return;
  //  
  ECR.SetTypeClose(0);
  if (ECR.CloseCheck() != 0) return;

// 
  //  
  ECR.SetName("");
  ECR.SetPrice(10.45);
  ECR.SetQuantity(1);
  if (ECR.Return() != 0) return;
  //  
  ECR.SetName("");
  ECR.SetPrice(99.99);
  ECR.SetQuantity(1.235);
  if (ECR.Return() != 0) return;
  //     
  ECR.Set_Summ(50);
  ECR.SetDestination(0);
  if (ECR.SummDiscount() != 0) return;
  //  
  ECR.SetTypeClose(0);
  if (ECR.CloseCheck() != 0) return;

//  
  ECR.Set_Summ(400.50);
  if (ECR.CashIncome() != 0) return;

//  
  ECR.Set_Summ(121.34);
  if (ECR.CashOutcome() != 0) return;

// X - 
  //    
  ECR.Set_Password("29");
  //      
  ECR.SetMode(2);
  if (ECR.SetMode() != 0) return;
  //  
  ECR.SetReportType(2);
  if (ECR.Report() != 0) return;

// Z - 
  //     
  ECR.Set_Password("30");
  //      
  ECR.SetMode(3);
  if (ECR.SetMode() != 0) return;
  //  
  ECR.SetReportType(1);
  if (ECR.Report() != 0) return;

//    ,  -        
  if (ECR.ResetMode() != 0) return;

//  
  ECR.SetDeviceEnabled(false);
  if (ECR.GetResultCode() != 0) return;

  AfxMessageBox("   ", MB_ICONINFORMATION + MB_OK);
}

int CVC6ExDlg::DoModal() 
{
//     
//       ,     
	COleException *e = new COleException;
	try
	{
		if (!ECR.CreateDispatch("AddIn.FprnM45", e))
			throw e;
		ECR.SetApplicationHandle((long)m_hWnd);
	}
	catch (CException *e)
	{
		AfxMessageBox("      !", MB_ICONERROR + MB_OK);
		e->Delete();
		return 0;
	};
	e->Delete();
	int result = CDialog::DoModal();
	ECR.ReleaseDispatch();
	return result;
}

void CVC6ExDlg::OnBtnshowproperties() 
{
	ECR.ShowProperties();
}

void CVC6ExDlg::OnBtnstart() 
{
	m_btnStart.EnableWindow(false);
	m_btnShowProperties.EnableWindow(false);

	Demo();
	if (ECR.GetResultCode() != 0)
		AfxMessageBox(" : " + ECR.GetResultDescription() + "!", MB_ICONERROR + MB_OK);

	m_btnStart.EnableWindow(true);
	m_btnShowProperties.EnableWindow(true);
}
