// DialogCashReg.cpp : implementation file
//

#include "stdafx.h"
#include "POSystems.h"
#include "DialogCashReg.h"
#include "DialogConfirm.h"

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

float Discount;

/////////////////////////////////////////////////////////////////////////////
// CDialogCashReg dialog


CDialogCashReg::CDialogCashReg(CWnd* pParent /*=NULL*/)
	: CDialog(CDialogCashReg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDialogCashReg)
	m_CBurger_Qty = 0;
	m_HBurger_Qty = 0;
	m_MChicken_Qty = 0;
	m_CheeseMeal_Qty = 0;
	m_ChickMeal_Qty = 0;
	m_DessertL_Qty = 0;
	m_DessertM_Qty = 0;
	m_DessertS_Qty = 0;
	m_FriesL_Qty = 0;
	m_FriesM_Qty = 0;
	m_FriesS_Qty = 0;
	m_SDrinkL_Qty = 0;
	m_SDrinkM_Qty = 0;
	m_SDrinkS_Qty = 0;
	m_HamMeal_Qty = 0;
	m_EmpID = _T("");
	m_AmtReceived = 0.0f;
	m_Change = 0.0f;
	m_TotalCost = 0.0f;
	m_Discounts = _T("");
	m_DHamMeal = 0;
	m_DCheeseMeal = 0;
	m_DChickMeal = 0;
	m_ReceiptNo =1;
	//}}AFX_DATA_INIT
}


void CDialogCashReg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDialogCashReg)
	DDX_Control(pDX, IDC_BUTTON_CONFIRM, m_btnConfirm);
	DDX_Control(pDX, IDC_EDIT_AMTRECEIVED, m_editAmtReceived);
	DDX_Control(pDX, IDC_BUTTON_MODTRANS, m_btnModifyTrans);
	DDX_Control(pDX, IDC_HAMMEAL_QTY, m_editHamMeal);
	DDX_Control(pDX, IDC_EDIT_SDRINKS_QTY, m_editSDrinkS);
	DDX_Control(pDX, IDC_EDIT_SDRINKM_QTY, m_editSDrinkM);
	DDX_Control(pDX, IDC_EDIT_SDRINKL_QTY, m_editSDrinkL);
	DDX_Control(pDX, IDC_EDIT_HBUGER_QTY, m_editHBurger);
	DDX_Control(pDX, IDC_EDIT_HAMMEALDIS, m_editDHamMeal);
	DDX_Control(pDX, IDC_EDIT_FRIESS_QTY, m_editFriesS);
	DDX_Control(pDX, IDC_EDIT_FRIESM_QTY, m_editFriesM);
	DDX_Control(pDX, IDC_EDIT_FRIESL_QTY, m_editFriesL);
	DDX_Control(pDX, IDC_EDIT_DESSERTS_QTY, m_editDessertS);
	DDX_Control(pDX, IDC_EDIT_DESSERTM_QTY, m_editDessertM);
	DDX_Control(pDX, IDC_EDIT_DESSERTL_QTY, m_editDessertL);
	DDX_Control(pDX, IDC_EDIT_CHICKMEALDIS, m_editDChickMeal);
	DDX_Control(pDX, IDC_EDIT_CHICKMEAL_QTY, m_editChickMeal);
	DDX_Control(pDX, IDC_EDIT_CHEESEMEALDIS, m_editDCheeseMeal);
	DDX_Control(pDX, IDC_CHEESEMEAL_QTY, m_editCheeseMeal);
	DDX_Control(pDX, IDC_EDIT_MCHICKEN_QTY, m_editMChicken);
	DDX_Control(pDX, IDC_EDIT_CBURGER_QTY, m_editCBurger);
	DDX_Control(pDX, IDC_COMBO_DISCOUNTS, m_CDiscounts);
	DDX_Control(pDX, IDC_BUTTON_CALCHANGE, m_btnGetChange);
	DDX_Control(pDX, IDC_BUTTON_PRINTRECEIPT, m_btnPrintReceipt);
	DDX_Text(pDX, IDC_EDIT_CBURGER_QTY, m_CBurger_Qty);
	DDV_MinMaxInt(pDX, m_CBurger_Qty, 0, 1000);
	DDX_Text(pDX, IDC_EDIT_HBUGER_QTY, m_HBurger_Qty);
	DDV_MinMaxInt(pDX, m_HBurger_Qty, 0, 1000);
	DDX_Text(pDX, IDC_EDIT_MCHICKEN_QTY, m_MChicken_Qty);
	DDV_MinMaxInt(pDX, m_MChicken_Qty, 0, 1000);
	DDX_Text(pDX, IDC_CHEESEMEAL_QTY, m_CheeseMeal_Qty);
	DDV_MinMaxInt(pDX, m_CheeseMeal_Qty, 0, 1000);
	DDX_Text(pDX, IDC_EDIT_CHICKMEAL_QTY, m_ChickMeal_Qty);
	DDV_MinMaxInt(pDX, m_ChickMeal_Qty, 0, 1000);
	DDX_Text(pDX, IDC_EDIT_DESSERTL_QTY, m_DessertL_Qty);
	DDV_MinMaxInt(pDX, m_DessertL_Qty, 0, 1000);
	DDX_Text(pDX, IDC_EDIT_DESSERTM_QTY, m_DessertM_Qty);
	DDV_MinMaxInt(pDX, m_DessertM_Qty, 0, 1000);
	DDX_Text(pDX, IDC_EDIT_DESSERTS_QTY, m_DessertS_Qty);
	DDV_MinMaxInt(pDX, m_DessertS_Qty, 0, 1000);
	DDX_Text(pDX, IDC_EDIT_FRIESL_QTY, m_FriesL_Qty);
	DDV_MinMaxInt(pDX, m_FriesL_Qty, 0, 1000);
	DDX_Text(pDX, IDC_EDIT_FRIESM_QTY, m_FriesM_Qty);
	DDV_MinMaxInt(pDX, m_FriesM_Qty, 0, 1000);
	DDX_Text(pDX, IDC_EDIT_FRIESS_QTY, m_FriesS_Qty);
	DDV_MinMaxInt(pDX, m_FriesS_Qty, 0, 1000);
	DDX_Text(pDX, IDC_EDIT_SDRINKL_QTY, m_SDrinkL_Qty);
	DDV_MinMaxInt(pDX, m_SDrinkL_Qty, 0, 1000);
	DDX_Text(pDX, IDC_EDIT_SDRINKM_QTY, m_SDrinkM_Qty);
	DDV_MinMaxInt(pDX, m_SDrinkM_Qty, 0, 1000);
	DDX_Text(pDX, IDC_EDIT_SDRINKS_QTY, m_SDrinkS_Qty);
	DDV_MinMaxInt(pDX, m_SDrinkS_Qty, 0, 1000);
	DDX_Text(pDX, IDC_HAMMEAL_QTY, m_HamMeal_Qty);
	DDV_MinMaxInt(pDX, m_HamMeal_Qty, 0, 1000);
	DDX_Text(pDX, IDC_EDIT_EmpID, m_EmpID);
	DDX_Text(pDX, IDC_EDIT_AMTRECEIVED, m_AmtReceived);
	DDV_MinMaxFloat(pDX, m_AmtReceived, 0.f, 10000.f);
	DDX_Text(pDX, IDC_EDIT_CHANGE, m_Change);
	DDX_Text(pDX, IDC_EDIT_TOTALCOST, m_TotalCost);
	DDX_CBString(pDX, IDC_COMBO_DISCOUNTS, m_Discounts);
	DDX_Text(pDX, IDC_EDIT_HAMMEALDIS, m_DHamMeal);
	DDV_MinMaxInt(pDX, m_DHamMeal, 0, 1000);
	DDX_Text(pDX, IDC_EDIT_CHEESEMEALDIS, m_DCheeseMeal);
	DDV_MinMaxInt(pDX, m_DCheeseMeal, 0, 1000);
	DDX_Text(pDX, IDC_EDIT_CHICKMEALDIS, m_DChickMeal);
	DDV_MinMaxInt(pDX, m_DChickMeal, 0, 1000);
	DDX_Text(pDX, IDC_EDIT_RECEIPTNO, m_ReceiptNo);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDialogCashReg, CDialog)
	//{{AFX_MSG_MAP(CDialogCashReg)
	ON_BN_CLICKED(IDC_BUTTON_TOTALCOST, OnButtonTotalCost)
	ON_BN_CLICKED(IDC_BUTTON_CALCHANGE, OnButtonCalChange)
	ON_BN_CLICKED(IDC_BUTTON_PRINTRECEIPT, OnButtonPrintReceipt)
	ON_BN_CLICKED(IDC_BUTTON_NXTRANS, OnButtonNxtrans)
	ON_EN_CHANGE(IDC_EDIT_AMTRECEIVED, OnChangeEditAmtreceived)
	ON_BN_CLICKED(IDC_BUTTON_MODTRANS, OnButtonModtrans)
	ON_BN_CLICKED(IDC_BUTTON_CONFIRM, OnButtonConfirm)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDialogCashReg message handlers

void CDialogCashReg::OnButtonTotalCost() 
{
	CalculateTotalCost();	
}

void CDialogCashReg::CalculateTotalCost()
{
	float MChicken, HBurger, CBurger;
	float CheeseMeal, HamMeal, ChickMeal;
	float DessertL, DessertM, DessertS;
	float FriesL, FriesM, FriesS;
	float SDrinkL, SDrinkM, SDrinkS;
	float DChickMeal, DHamMeal, DCheeseMeal;
	float TotalCost;

	UpdateData(TRUE);

	MChicken = m_MChicken_Qty * 2.50;
	HBurger = m_HBurger_Qty * 1.30;
	CBurger = m_CBurger_Qty * 1.50;
	CheeseMeal = m_CheeseMeal_Qty * 5.00;
	ChickMeal = m_ChickMeal_Qty * 5.60;
	HamMeal = m_HamMeal_Qty * 5.30;
	DessertL = m_DessertL_Qty * 2.00;
	DessertM = m_DessertM_Qty * 1.80;
	DessertS = m_DessertS_Qty * 1.50;
	FriesL = m_FriesL_Qty * 2.20;
	FriesM = m_FriesM_Qty * 2.00;
	FriesS = m_FriesS_Qty * 1.80;
	SDrinkL = m_SDrinkL_Qty * 2.10;
	SDrinkM = m_SDrinkM_Qty * 1.80;
	SDrinkS = m_SDrinkS_Qty * 1.60;
	DChickMeal = m_DChickMeal * 5.00;
	DHamMeal = m_DHamMeal * 4.80;
	DCheeseMeal = m_DCheeseMeal * 4.40;

	TotalCost = MChicken + HBurger + CBurger
			+ CheeseMeal + HamMeal + ChickMeal
			+ DessertL + DessertM + DessertS
			+ FriesL + FriesM + FriesS
			+ SDrinkL + SDrinkM + SDrinkS
			+ DChickMeal + DHamMeal + DCheeseMeal;

	if (TotalCost == 0)
	{
		m_btnGetChange.EnableWindow(FALSE);
		m_btnPrintReceipt.EnableWindow(FALSE);
		m_CDiscounts.EnableWindow(FALSE);
		AfxMessageBox("No item selected.");
	}
	else
	{
		m_CDiscounts.EnableWindow(TRUE);
	}

	if(m_Discounts == "")
		Discount = 0;
	if(m_Discounts == "10%")
		Discount = 0.1;
	if(m_Discounts == "20%")
		Discount = 0.2;
	if(m_Discounts == "30%")
		Discount = 0.3;
	if(m_Discounts == "40%")
		Discount = 0.4;
	if(m_Discounts == "50%")
		Discount = 0.5;
	if(m_Discounts == "60%")
		Discount = 0.6;
	if(m_Discounts == "70%")
		Discount = 0.7;
	if(m_Discounts == "80%")
		Discount = 0.8;

	m_TotalCost = TotalCost * (1 - Discount);

	UpdateData(FALSE);
}

void CDialogCashReg::CalculateChange()
{

	UpdateData(TRUE);

	m_Change = m_AmtReceived - m_TotalCost;
	m_Change = (float)(int(m_Change * 100 + 0.05)) / 100;
	if (m_Change < 0.0)
	{
		CString errmsg;

		errmsg = "Amount received insufficient";
		AfxMessageBox(errmsg);
		m_AmtReceived = 0.0;
		m_Change = 0.0;
		m_btnGetChange.EnableWindow(FALSE);
		m_btnPrintReceipt.EnableWindow(FALSE);

		m_editCBurger.EnableWindow(TRUE);
		m_editHamMeal.EnableWindow(TRUE);
		m_editSDrinkS.EnableWindow(TRUE);
		m_editSDrinkM.EnableWindow(TRUE);
		m_editSDrinkL.EnableWindow(TRUE);
		m_editHBurger.EnableWindow(TRUE);
		m_editDHamMeal.EnableWindow(TRUE);
		m_editFriesS.EnableWindow(TRUE);
		m_editFriesM.EnableWindow(TRUE);
		m_editFriesL.EnableWindow(TRUE);
		m_editDessertS.EnableWindow(TRUE);
		m_editDessertM.EnableWindow(TRUE);
		m_editDessertL.EnableWindow(TRUE);
		m_editDChickMeal.EnableWindow(TRUE);
		m_editChickMeal.EnableWindow(TRUE);
		m_editDCheeseMeal.EnableWindow(TRUE);
		m_editCheeseMeal.EnableWindow(TRUE);
		m_editMChicken.EnableWindow(TRUE);
		m_editCBurger.EnableWindow(TRUE);
		m_CDiscounts.EnableWindow(TRUE);

		m_btnModifyTrans.EnableWindow(FALSE);
		m_btnConfirm.EnableWindow(FALSE);

	}
	else
	{
		m_editAmtReceived.EnableWindow(FALSE);
		m_editCBurger.EnableWindow(FALSE);
		m_editHamMeal.EnableWindow(FALSE);
		m_editSDrinkS.EnableWindow(FALSE);
		m_editSDrinkM.EnableWindow(FALSE);
		m_editSDrinkL.EnableWindow(FALSE);
		m_editHBurger.EnableWindow(FALSE);
		m_editDHamMeal.EnableWindow(FALSE);
		m_editFriesS.EnableWindow(FALSE);
		m_editFriesM.EnableWindow(FALSE);
		m_editFriesL.EnableWindow(FALSE);
		m_editDessertS.EnableWindow(FALSE);
		m_editDessertM.EnableWindow(FALSE);
		m_editDessertL.EnableWindow(FALSE);
		m_editDChickMeal.EnableWindow(FALSE);
		m_editChickMeal.EnableWindow(FALSE);
		m_editDCheeseMeal.EnableWindow(FALSE);
		m_editCheeseMeal.EnableWindow(FALSE);
		m_editMChicken.EnableWindow(FALSE);
		m_editCBurger.EnableWindow(FALSE);
		m_CDiscounts.EnableWindow(FALSE);

		m_btnModifyTrans.EnableWindow(TRUE);
		m_btnConfirm.EnableWindow(TRUE);

	}

	UpdateData(FALSE);

}


void CDialogCashReg::OnButtonCalChange() 
{
	CalculateChange();
}

void CDialogCashReg::OnButtonPrintReceipt() 
{
	
	UpdateData();

	CDialogConfirm CReceiptDlg;
	CReceiptDlg.m_Msg = "Receipt Printed.";
	CReceiptDlg.DoModal();
}

void CDialogCashReg::OnButtonNxtrans() 
{
	CDialogConfirm CMsgDlg;

	CMsgDlg.m_Msg = "The current transaction will be cleared.";	
	if (CMsgDlg.DoModal() == IDOK)
	{
		
	
		UpdateData();
		m_ReceiptNo = m_ReceiptNo+1;
		m_CBurger_Qty =0;
		m_HBurger_Qty = 0;
		m_MChicken_Qty = 0;
		m_TotalCost = 0.0;
		m_Change = 0.0;
		m_AmtReceived = 0.0;
		m_CheeseMeal_Qty = 0;
		m_ChickMeal_Qty = 0;
		m_HamMeal_Qty = 0;
		m_DessertL_Qty = 0;
		m_DessertM_Qty = 0;
		m_DessertS_Qty = 0;
		m_FriesL_Qty = 0;
		m_FriesM_Qty = 0;
		m_FriesS_Qty = 0;
		m_SDrinkL_Qty = 0;
		m_SDrinkM_Qty = 0;
		m_SDrinkS_Qty = 0;
		m_DChickMeal = 0;
		m_DCheeseMeal = 0;
		m_DHamMeal = 0;
		Discount = 0;

		m_btnPrintReceipt.EnableWindow(FALSE);
		m_btnGetChange.EnableWindow(FALSE);
		m_Discounts = "";
		m_CDiscounts.EnableWindow(FALSE);
		m_editAmtReceived.EnableWindow(TRUE);
		m_editCBurger.EnableWindow(TRUE);
		m_editHamMeal.EnableWindow(TRUE);
		m_editSDrinkS.EnableWindow(TRUE);
		m_editSDrinkM.EnableWindow(TRUE);
		m_editSDrinkL.EnableWindow(TRUE);
		m_editHBurger.EnableWindow(TRUE);
		m_editDHamMeal.EnableWindow(TRUE);
		m_editFriesS.EnableWindow(TRUE);
		m_editFriesM.EnableWindow(TRUE);
		m_editFriesL.EnableWindow(TRUE);
		m_editDessertS.EnableWindow(TRUE);
		m_editDessertM.EnableWindow(TRUE);
		m_editDessertL.EnableWindow(TRUE);
		m_editDChickMeal.EnableWindow(TRUE);
		m_editChickMeal.EnableWindow(TRUE);
		m_editDCheeseMeal.EnableWindow(TRUE);
		m_editCheeseMeal.EnableWindow(TRUE);
		m_editMChicken.EnableWindow(TRUE);
		m_editCBurger.EnableWindow(TRUE);
		m_btnModifyTrans.EnableWindow(FALSE);

		UpdateData(FALSE);
	}
}

void CDialogCashReg::OnOK() 
{
	// TODO: Add extra validation here
	
	CDialog::OnOK();
	
}

void CDialogCashReg::OnChangeEditAmtreceived() 
{
	if (m_TotalCost > 0)
		m_btnGetChange.EnableWindow(TRUE);	
}

void CDialogCashReg::OnButtonModtrans() 
{
	m_editCBurger.EnableWindow(TRUE);
	m_editHamMeal.EnableWindow(TRUE);
	m_editSDrinkS.EnableWindow(TRUE);
	m_editSDrinkM.EnableWindow(TRUE);
	m_editSDrinkL.EnableWindow(TRUE);
	m_editHBurger.EnableWindow(TRUE);
	m_editDHamMeal.EnableWindow(TRUE);
	m_editFriesS.EnableWindow(TRUE);
	m_editFriesM.EnableWindow(TRUE);
	m_editFriesL.EnableWindow(TRUE);
	m_editDessertS.EnableWindow(TRUE);
	m_editDessertM.EnableWindow(TRUE);
	m_editDessertL.EnableWindow(TRUE);
	m_editDChickMeal.EnableWindow(TRUE);
	m_editChickMeal.EnableWindow(TRUE);
	m_editDCheeseMeal.EnableWindow(TRUE);
	m_editCheeseMeal.EnableWindow(TRUE);
	m_editMChicken.EnableWindow(TRUE);
	m_editCBurger.EnableWindow(TRUE);
	m_CDiscounts.EnableWindow(TRUE);
	m_editAmtReceived.EnableWindow(TRUE);

	m_btnModifyTrans.EnableWindow(FALSE);
	m_btnConfirm.EnableWindow(FALSE);
	m_AmtReceived = 0;
	m_Change = 0;
	UpdateData(FALSE);

}

void CDialogCashReg::OnButtonConfirm() 
{
	m_btnConfirm.EnableWindow(FALSE);
	m_btnModifyTrans.EnableWindow(FALSE);
	m_btnPrintReceipt.EnableWindow(TRUE);
	m_btnGetChange.EnableWindow(FALSE);
	UpdateData(FALSE);
}
