#include "StdAfx.h"
#include "ControlsAccounts.h"
#include "../../BasicAdmin2_Utils/GenDialog/GenDialogView.h"
#include "../../BasicAdmin2_Utils/GenDialog/GenTreeDialogView.h"
#include "../../BasicAdmin2_Utils/GenView/GenLeftTreeView.h"
#include <afxcontrolbars.h> // MFC support for ribbons and control bars
#include "../../BasicAdmin2_Database/Managers/ManArrays.h"
#include "../../BasicAdmin2_Database/Managers/ManAccount.h"
#include "../../BasicAdmin2_Utils/GenView/GenGridView.h"
#include "../../Librerias/GraphParser/Globals.h"
#include "../../BasicAdmin2_Model/CustMessages.h"
ControlsAccounts::ControlsAccounts(void)
{
}
ControlsAccounts::~ControlsAccounts(void)
{
}
void ControlsAccounts::InitControls()
{
dh.form = form;
dh.param = param;
dh.Initialize();
dh.genman = (CWnd*)this;
}
void ControlsAccounts::CreateControls()
{
vector<CString> v;
v.push_back(param->langdata.Item(L"Account.Label.Parent") + L":");
v.push_back(param->langdata.Item(L"Account.Label.Type") + L":");
v.push_back(param->langdata.Item(L"Account.Label.Description") + L":");
v.push_back(param->langdata.Item(L"Account.Label.Shortcut") + L":");
v.push_back(param->langdata.Item(L"Account.Label.Currency") + L":");
v.push_back(param->langdata.Item(L"Account.Label.Observations") + L":");
dh.phelper.IsVertical = TRUE;
dh.phelper.SetLeftAlign(0,&v);
dh.phelper.SetTop();
dh.CreateLabel(v[0],&(Lbl[0]));
cboParent.IdTxt = IDCA_CBOPARENTTXT;
cboParent.IdBtn = IDCC_CBOPARENTBTN;
cboParent.IdStatic = IDCC_CBOPARENTSTC;
cboParent.dh = &dh;
cboParent.ControlSpace = param->fuentes.ctSpaceVert;
cboParent.arr = arr;
cboParent.typecontrol = TypeCboAccount;
cboParent.Create();
dh.AddRow();
dh.CreateLabel(v[1],&(Lbl[1]));
dh.CreateComboBox(IDCC_CBOTIPO,&CboTipo);
ClsAccount acc;
for (int i=0;i<=eAccountType::TaccCalculated;i++)
{
CString cad = acc.GetTypeDescription(*(param->langdata.GetCurLangInfo()), i);
CboTipo.AddString(cad);
CboTipo.SetItemData(i,i);
}
CboTipo.SetCurSel(0);
dh.AddRow();
dh.CreateLabel(v[2],&(Lbl[2]));
dh.CreateTextBox(IDCC_CBOTXTDESC,&TxtDescrip, 50);
dh.AddRow();
dh.CreateLabel(v[3],&(Lbl[3]));
dh.CreateShortCutBox(IDCC_CBOSHORTCUT,&HtkShortCut);
dh.AddRow();
dh.CreateLabel(v[4],&(Lbl[4]));
dh.CreateComboBox(IDCC_CBOMONEDA,&CboMoneda);
if (!arr->LoadedCur)
{
ManArrays arrman;
arrman.dbAdmin = &(param->dbAdmin);
arrman.LoadArrCurrencies(*arr);
}
ClsCurrency* curarr;
int j=0;
for (auto itr=arr->arrCurrencies.begin();itr!=arr->arrCurrencies.end();itr++)
{
curarr = (ClsCurrency*)(*itr).second;
CboMoneda.AddString(curarr->Description);
CboMoneda.SetItemData(j,curarr->ID);
j++;
}
if (CboMoneda.GetCount() > 0) CboMoneda.SetCurSel(0);
dh.AddRow();
dh.CreateLabel(v[5],&(Lbl[5]));
dh.CreateMultiEdit(IDCC_TXTOBS,&TxtObs,MultiEditHeight);
dh.AddRow();
dh.CreateGroupBox(dh.phelper.TopAlign,dh.phelper.TotalHeight + param->fuentes.ctSpaceVert * 2,&GroupBox);
}
void ControlsAccounts::LimpiarControles()
{
CboTipo.SetCurSel(0);
TxtDescrip.SetWindowText(L"");
HtkShortCut.SetHotKey(0,0);
if (CboMoneda.GetCount() > 0) CboMoneda.SetCurSel(0);
TxtObs.SetWindowText(L"");
int id = GetIDFromTree();
if (id == -1)
cboParent.Clean();
else
{
cboParent.Id = id;
cboParent.RefreshCaption();
}
}
int ControlsAccounts::GetIDFromTree()
{
CGenLeftTreeView* gentreeview = (CGenLeftTreeView*)handles->m_WndTreeView;
HTREEITEM titem = gentreeview->GetTreeCtrl().GetSelectedItem();
if (titem == NULL)
return -1;
else
return gentreeview->GetTreeCtrl().GetItemData(titem);
}
CString ControlsAccounts::Validar()
{
CString cad;
TxtDescrip.GetWindowTextW(cad);
if (cad == "")
{
dh.SetLblError(&Lbl[2]);
TxtDescrip.SetFocus();
return param->langdata.Item(L"Account.Errors.EnterDescription");
}
CboMoneda.GetWindowTextW(cad);
if (cad == "")
{
dh.SetLblError(&Lbl[4]);
CboMoneda.SetFocus();
return param->langdata.Item(L"Account.Errors.EnterCurrency");
}
return L"";
}
CString ControlsAccounts::SaveOK()
{
CString cad;
SetNoError();
cad = Validar();
if (cad != "") return cad;
ClsAccount* acc = new ClsAccount();
int id = CboTipo.GetItemData(CboTipo.GetCurSel());
ManAccount manc;
manc.dbAdmin = &(param->dbAdmin);
LoadAccFromForm(acc);
CGenGridView* genv = (CGenGridView*)handles->m_WndGrid;
if (ID == -1)
{
manc.AddAccount(*acc);
arr->arrAccounts[acc->ID] = (DWORD)acc;
genv->genman->AddRow(genv->con->grid,(DWORD)acc);
genv->genman->AfterNew();
}
else
{
manc.ModifyAccount(*acc);
ClsAccount* accaux = (ClsAccount*)arr->arrAccounts[acc->ID];
delete accaux;
arr->arrAccounts[acc->ID] = (DWORD)acc;
genv->genman->RefreshRow(genv->con->grid,0,(DWORD)acc);
}
CGenDialogView* gendiag = (CGenDialogView*)form;
CGenTreeDialogView* gentree = (CGenTreeDialogView*)handles->m_WndDialogTreeSearch;
CGenLeftTreeView* gentreeview = (CGenLeftTreeView*)handles->m_WndTreeView;
gentree->treeutils->AddModifyTree(ID != -1,acc->ID, acc->Description, acc->IdParent,3,gentree->mtree);
gentreeview->tutils.AddModifyTree(ID != -1,acc->ID, acc->Description, acc->IdParent,3,gentreeview->GetTreeCtrl());
gentree->treeutils->CheckFolderImages(gentree->mtree, 3);
gentreeview->tutils.CheckFolderImages(gentreeview->GetTreeCtrl(), 3);
CWnd* pant = handles->m_WndDialog->GetParent()->GetParent()->GetParent();
pant->SendMessage(MsgAccountChange,0,0);
return L"";
}
void ControlsAccounts::LoadData()
{
ManAccount manc;
manc.dbAdmin = &(param->dbAdmin);
ClsAccount* acc = new ClsAccount();
acc->ID = ID;
manc.GetAccount(*acc);
LoadFormFromAcc(acc);
delete acc;
}
void ControlsAccounts::LoadFormFromAcc(ClsAccount* acc)
{
cboParent.Id = acc->IdParent;
cboParent.RefreshCaption();
PosCbo(&CboTipo, acc->AccountType);
TxtDescrip.SetWindowTextW(acc->Description);
HtkShortCut.SetHotKey(acc->Shortcut1, acc->Shortcut2);
PosCbo(&CboMoneda, acc->IdCurrency);
TxtObs.SetWindowTextW(acc->Observations);
}
void ControlsAccounts::InitialFocus()
{
}
void ControlsAccounts::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
if (message != WM_COMMAND) return;
if (wParam == IDCC_CBOPARENTBTN)
{
SwitchToTree();
}
}
void ControlsAccounts::SwitchToTree()
{
::SetWindowLong(form->m_hWnd, GWL_ID, 0);
CGenDialogView* gendiag = (CGenDialogView*)form;
CGenTreeDialogView* gentree = (CGenTreeDialogView*)handles->m_WndDialogTreeSearch;
gentree->cbo = &cboParent;
::SetWindowLong(gentree->m_hWnd, GWL_ID, AFX_IDW_PANE_FIRST);
form->ShowWindow(SW_HIDE);
gentree->ShowWindow(SW_SHOW);
CMDIChildWndEx* frame = (CMDIChildWndEx*)form->GetParent();
frame->RecalcLayout();
gentree->mtree.SelectItem(gentree->titem);
gentree->SetFocus();
gentree->mtree.SetFocus();
}
void ControlsAccounts::LoadAccFromForm(ClsAccount* acc)
{
CString cad;
acc->AccountType = (eAccountType)CboTipo.GetItemData(CboTipo.GetCurSel());
TxtDescrip.GetWindowTextW(cad);
acc->Description = cad;
acc->IdParent = cboParent.Id;
if (!acc->IsParentRoot())
acc->Parent = (ClsAccount*)arr->arrAccounts[acc->IdParent];
acc->IdCurrency = CboMoneda.GetItemData(CboMoneda.GetCurSel());
acc->currency = (ClsCurrency*)arr->arrCurrencies[acc->IdCurrency];
WORD virtualkeycode, modifiers;
HtkShortCut.GetHotKey(virtualkeycode, modifiers);
acc->Shortcut1 = virtualkeycode;
acc->Shortcut2 = modifiers;
TxtObs.GetWindowTextW(cad);
acc->Observations = cad;
acc->ID = ID;
}
void ControlsAccounts::RefreshLabels()
{
cboParent.RefreshColors();
CGenDialogManager::RefreshLabels();
}