FinalizePayment.cpp
bool FinalizePayment::finalize(int receiptId, FPPayments::Payed &p) {
finalizePaymentForm->Init(getTotal(receiptId));
if (finalizePaymentForm->ShowModal() == mrOk) {
p = finalizePaymentForm->getPayment();
FPPayments::PaymentInfo paymentInfo = p.getInfo();
if (paymentInfo.size() > 1) {
for (FPPayments::PaymentInfo::const_iterator i = paymentInfo.begin(); i != paymentInfo.end(); i++) {
if (i->first == "cash")
generateCashPayment(receiptId, atof(i->second.c_str()));
else
generateCardPayment(receiptId, p.getTerminal(), atof(i->second.c_str()));
}
} else {
if (p.getTerminal() != 0)
generateCardPayment(receiptId, p.getTerminal());
else
generateCashPayment(receiptId);
}
return true;
}
return false;
}
//------------------------------------------------------------------------
FinalizePaymentForm.cpp
#pragma link "dxfCheckBox"
//---------------------------------------------------------------------------
#include <vcl.h>
#include "CommonHeader.h"
#pragma hdrstop
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
namespace Totalizer
{
TFinalizePaymentForm1 *FinalizePaymentForm1;
//---------------------------------------------------------------------------
__fastcall TFinalizePaymentForm1::TFinalizePaymentForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TFinalizePaymentForm1::FormCreate(TObject *Sender)
{
for(int i = 0; i< PaymentPages->PageCount; i++)
PaymentPages->Pages[i]->TabVisible = false;
PaymentPages->ActivePageIndex = 0;
for(int i = 0; i< MainPages->PageCount; i++)
MainPages->Pages[i]->TabVisible = false;
MainPages->ActivePageIndex = 0;
}
//---------------------------------------------------------------------------
void __fastcall TFinalizePaymentForm1::FormShow(TObject *Sender)
{
MainPages->ActivePageIndex = 0;
PaymentPages->ActivePageIndex = 0;
PaymentsRG->ItemIndex = 0;
TotalLabel1->Caption = FormatFloat("0.00", total);
TotalLabel2->Caption = FormatFloat("0.00", total);
CashMemo->Lines->Text = FormatFloat("0.00", total);
MCashMemo->Lines->Text = "0";
MCardMemo->Lines->Text = "0";
MTotalMemo->Lines->Text = FormatFloat("0.00", total);
CashMemo->SelectAll();
ChangeLabel->Caption = "0.00";
POSTerminalQuery->Close();
POSTerminalQuery->Open();
PaymentsRGClick(Sender);
}
//---------------------------------------------------------------------------
void __fastcall TFinalizePaymentForm1::Init(AnsiString Total)
{
cash = total = Total.ToDouble();
card = 0;
MTotalMemo->Lines->Text = Total;
}
//---------------------------------------------------------------------------
void __fastcall TFinalizePaymentForm1::CashMemoKeyPress(TObject *Sender,
char &Key)
{
if(Key != '1' && Key != '2' && Key != '3' && Key != '4' && Key != '5' &&
Key != '6' && Key != '7' && Key != '8' && Key != '9' && Key != '0' &&
Key != '.' && Key != 'n' && Key != 'r' && Key != 't' &&
Key != 'l' && Key != 27 && Key != 'b')
Key = 0;
}
//---------------------------------------------------------------------------
void __fastcall TFinalizePaymentForm1::CashMemoChange(TObject *Sender)
{
if(CashMemo->Lines->Text == "")
{
ChangeLabel->Caption = "";
return;
}
cash = CashMemo->Lines->Text.ToDouble();
ChangeLabel->Caption = FormatFloat("0.00", cash - total);
}
//---------------------------------------------------------------------------
void __fastcall TFinalizePaymentForm1::MCardMemoChange(TObject *Sender)
{
if(PaymentPages->ActivePageIndex != 2)
return;
if(MCashMemo->Lines->Text == "")
{
cash = 0;
}
else
{
cash = MCashMemo->Lines->Text.ToDouble();
}
if(MCardMemo->Lines->Text == "")
{
card = 0;
}
else
{
card = MCardMemo->Lines->Text.ToDouble();
}
float s = card + cash;
if(s == total) MTotalMemo->Font->Color = clGreen;
else if(s > total) MTotalMemo->Font->Color = clRed;
else MTotalMemo->Font->Color = clMaroon;
}
//---------------------------------------------------------------------------
const bool TFinalizePaymentForm1::isThereChange() const
{
return cash > total;
}
//---------------------------------------------------------------------------
const float TFinalizePaymentForm1::getCash() const
{
return (cash > total) ? cash : -1;
}
//---------------------------------------------------------------------------
void __fastcall TFinalizePaymentForm1::OKBtnClick(TObject *Sender)
{
if(PaymentsRG->ItemIndex && (!m_isTerminalChosen || POSTerminalQuery->Fields->Fields[0]->IsNull))
{
ShowMessage("Moля изберете пос терминал!");
return;
}
if ((PaymentsRG->ItemIndex == 0) && (FormatFloat("0.000", cash) != FormatFloat("0.000", total)) )
{
if(cash < total)
{
MessageBox(Handle, "Сумата трябва да е по-голяма или равна на стойността на протокола!", "Внимание", MB_OK);
return;
}
}
else if ( (PaymentsRG->ItemIndex == 2) && ( FormatFloat("0.000",card + cash) != FormatFloat("0.000",total)) )
{
MessageBox(Handle, "Сборът от сумата в брой и сумата, платена с карта трябва да е равна на стойността на протокола!", "Внимание", MB_OK);
return;
}
MsgLabel->Caption = "Вие приехте плащане от клиент за ";
if (PaymentsRG->ItemIndex == 0)
{
MsgLabel->Caption = MsgLabel->Caption + TotalLabel1->Caption + " лв. в брой!";
}
else if (PaymentsRG->ItemIndex == 1)
{
MsgLabel->Caption = MsgLabel->Caption + TotalLabel2->Caption + " лв. с карта на " +
POSTerminalQuery->FieldByName("POS_NAME")->AsString;
}
else
{
MsgLabel->Caption = MsgLabel->Caption + MTotalMemo->Lines->Text + " лв." +
" от които:nв брой: " + MCashMemo->Lines->Text +
" лв. и nс карта: " + MCardMemo->Lines->Text + " лв. на " +
POSTerminalQuery->FieldByName("POS_NAME")->AsString;;
}
MainPages->ActivePage = TabMessage;
}
//---------------------------------------------------------------------------
void __fastcall TFinalizePaymentForm1::MOKBtnClick(TObject *Sender)
{
ModalResult = mrOk;
}
//---------------------------------------------------------------------------
void __fastcall TFinalizePaymentForm1::MCancelBtnClick(TObject *Sender)
{
MainPages->ActivePage = TabChoice;
}
//---------------------------------------------------------------------------
void __fastcall TFinalizePaymentForm1::PaymentsRGClick(TObject *Sender)
{
PaymentPages->ActivePageIndex = PaymentsRG->ItemIndex;
if(PaymentsRG->ItemIndex == 0)
{
if(CashMemo->Text != "")
{
cash = CashMemo->Text.ToDouble();
}
else
{
cash = 0;
CashMemo->Text = "0";
}
CashMemo->SelectAll();
CashMemo->SetFocus();
}
else if(PaymentsRG->ItemIndex == 1)
{
m_isTerminalChosen = false;
m_terminalId = 0;
cash = total;
POSTerminalGrid->SetFocus();
}
else
{
m_isTerminalChosen = false;
m_terminalId = 0;
POSTerminalGridM->SetFocus();
if(MCashMemo->Text != "")
{
cash = MCashMemo->Text.ToDouble();
}
else
{
cash = 0;
MCashMemo->Text = "0";
}
if(MCardMemo->Text != "")
{
card = MCardMemo->Text.ToDouble();
}
else
{
card = 0;
MCardMemo->Text = "0";
}
}
}
//---------------------------------------------------------------------------
void __fastcall TFinalizePaymentForm1::POSTerminalQueryAfterScroll(TDataSet *DataSet)
{
m_terminalId = POSTerminalQuery->FieldByName("REF_POS_ACCOUNT_ID")->AsInteger;
}
//---------------------------------------------------------------------------
void __fastcall TFinalizePaymentForm1::POSTerminalGridCellClick(TColumn *Column)
{
m_terminalId = POSTerminalQuery->FieldByName("REF_POS_ACCOUNT_ID")->AsInteger;
m_isTerminalChosen = true;
}
//---------------------------------------------------------------------------
void __fastcall TFinalizePaymentForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
POSTerminalQuery->Close();
}
//---------------------------------------------------------------------------
FPPayments::Payed TFinalizePaymentForm1::getPayment()
{
FPPayments::Payed payment;
double sum = (cash > total) ? cash : -1;
if(PaymentsRG->ItemIndex == 0)
{
payment.setCashPayment(sum);
}
else if(PaymentsRG->ItemIndex == 1)
{
payment.setCardPayment(sum, m_terminalId);
}
else if(PaymentsRG->ItemIndex == 2)
{
payment.setCashPayment(cash);
payment.setCardPayment(card, m_terminalId);
}
return payment;
}
//---------------------------------------------------------------------------
}
I am trying to get the correct value of the payment , there is payment types : By Cash , By Card , and mixed payment , so in mixed payment , it is getting the same amount for both , i don’t know where is my mistake , i need to get off from total amount – cash , and to pass the rest of the amount to the card , when is mixed !…..