unit p_telep;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Buttons, Db, DBTables, fcButton, fcImgBtn, jpeg, ExtCtrls,
  AdPort, OoMisc, AdTapi, RzPanel, RzButton, cxGridLevel,
  cxGridCustomTableView, cxGridCardView, cxGridDBCardView, cxClasses,
  cxControls, cxGridCustomView, cxGrid, nrclasses, nrcomm, nrcommtapi,
  nrcommbox, DBCtrls, BULabel, BUBlinkingLabel, frxClass, frxDBSet;

type
  Tf_telep = class(TForm)
    Query1: TQuery;
    RzPanel2: TRzPanel;
    Edit1: TEdit;
    Label1: TLabel;
    Label3: TLabel;
    Edit3: TEdit;
    Edit2: TEdit;
    Label2: TLabel;
    RzPanel1: TRzPanel;
    SpeedButton4: TSpeedButton;
    SpeedButton3: TSpeedButton;
    SpeedButton2: TSpeedButton;
    SpeedButton1: TSpeedButton;
    SpeedButton8: TSpeedButton;
    SpeedButton7: TSpeedButton;
    SpeedButton6: TSpeedButton;
    SpeedButton5: TSpeedButton;
    SpeedButton11: TSpeedButton;
    SpeedButton10: TSpeedButton;
    SpeedButton9: TSpeedButton;
    Query1ID: TIntegerField;
    Query1NAME: TStringField;
    Query1COMPANY: TStringField;
    Query1JOBDESCR: TStringField;
    Query1WPHONE: TStringField;
    Query1HPHONE: TStringField;
    Query1CELL: TStringField;
    Query1ADDRESS1: TStringField;
    Query1ADDRESS2: TStringField;
    Query1FAX: TStringField;
    Query1EMAIL: TStringField;
    Query1POBOX: TStringField;
    Query1NOTE: TStringField;
    RzPanel3: TRzPanel;
    BitBtn1: TRzBitBtn;
    fcImageBtn3: TRzBitBtn;
    DataSource1: TDataSource;
    Label4: TLabel;
    Edit4: TEdit;
    efrom: TEdit;
    eto: TEdit;
    Label5: TLabel;
    nrDeviceBox1: TnrDeviceBox;
    nrCommTAPI1: TnrCommTAPI;
    MYORDER: TQuery;
    DMYORDER: TDataSource;
    RzPanel4: TRzPanel;
    cxGrid1: TcxGrid;
    cxGrid1DBCardView1: TcxGridDBCardView;
    cxGrid1DBCardView1Row1: TcxGridDBCardViewRow;
    cxGrid1DBCardView1Row9: TcxGridDBCardViewRow;
    cxGrid1DBCardView1Row2: TcxGridDBCardViewRow;
    cxGrid1DBCardView1Row3: TcxGridDBCardViewRow;
    cxGrid1DBCardView1Row8: TcxGridDBCardViewRow;
    cxGrid1DBCardView1Row5: TcxGridDBCardViewRow;
    cxGrid1DBCardView1Row7: TcxGridDBCardViewRow;
    cxGrid1Level1: TcxGridLevel;
    SpeedButton12: TSpeedButton;
    SpeedButton13: TSpeedButton;
    MYORDERID: TIntegerField;
    MYORDERAUTONO: TIntegerField;
    MYORDERFNO: TStringField;
    MYORDERVAT: TFloatField;
    MYORDERSUBTOTAL: TFloatField;
    MYORDERTOTAL: TFloatField;
    MYORDERCASHIN: TFloatField;
    MYORDERCASHOUT: TFloatField;
    MYORDERTABLENO: TStringField;
    MYORDERIACTIVE: TStringField;
    MYORDERNAME: TStringField;
    MYORDERCOVER: TIntegerField;
    MYORDERISTATE: TIntegerField;
    MYORDERLASTTIME: TDateTimeField;
    MYORDERCREATETIME: TDateTimeField;
    MYORDERDISCOUNT: TFloatField;
    MYORDERPRN: TStringField;
    MYORDERCHEF: TStringField;
    MYORDERSID: TStringField;
    MYORDERCONTACT: TIntegerField;
    MYORDERZALEID: TIntegerField;
    MYORDERSDISC: TFloatField;
    MYORDERPAID: TStringField;
    RzBitBtn1: TRzBitBtn;
    point: TQuery;
    DBText1: TDBText;
    Query1CRADNO: TStringField;
    Query1POINTIN: TFloatField;
    Query1POINTOUT: TFloatField;
    Query1BALANCE: TFloatField;
    Query1TOTAL: TFloatField;
    DBText2: TDBText;
    pointID: TIntegerField;
    pointNAME: TStringField;
    pointCOMPANY: TStringField;
    pointJOBDESCR: TStringField;
    pointWPHONE: TStringField;
    pointHPHONE: TStringField;
    pointCELL: TStringField;
    pointADDRESS1: TStringField;
    pointADDRESS2: TStringField;
    pointFAX: TStringField;
    pointEMAIL: TStringField;
    pointPOBOX: TStringField;
    pointNOTE: TStringField;
    pointCRADNO: TStringField;
    pointPOINTIN: TFloatField;
    pointPOINTOUT: TFloatField;
    pointBALANCE: TFloatField;
    pointTOTAL: TFloatField;
    DPOINT: TDataSource;
    pointbal: TCurrencyField;
    DBText3: TDBText;
    Label6: TLabel;
    Query1BAL: TFloatField;
    lqty: TBUBlinkingLabel;
    RzBitBtn5: TRzBitBtn;
    contdet: TQuery;
    frxcontactlist: TfrxDBDataset;
    curr: TQuery;
    procedure SpeedButton11Click(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
    procedure Edit1KeyPress(Sender: TObject; var Key: Char);
    procedure Edit2KeyPress(Sender: TObject; var Key: Char);
    procedure Edit3KeyPress(Sender: TObject; var Key: Char);
    procedure BitBtn1Click(Sender: TObject);
    procedure Edit3Exit(Sender: TObject);
    procedure Edit1Exit(Sender: TObject);
    procedure ApdTapiDevice1TapiCallerID(CP: TObject; ID, IDName: String);
    procedure MonitorClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Edit4KeyPress(Sender: TObject; var Key: Char);
    procedure nrCommTAPI1Ring(Com: TObject; var Ignore: Boolean);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure SpeedButton12Click(Sender: TObject);
    procedure SpeedButton13Click(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure RzBitBtn1Click(Sender: TObject);
    procedure pointCalcFields(DataSet: TDataSet);
    procedure Query1CalcFields(DataSet: TDataSet);
    procedure RzBitBtn5Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  f_telep: Tf_telep;

implementation

uses p_posmodule, p_barcode2, p_repmodule, p_msgdlg, p_myinput;



{$R *.DFM}

procedure Tf_telep.SpeedButton11Click(Sender: TObject);
begin
edit1.Text:='';
end;

procedure Tf_telep.SpeedButton1Click(Sender: TObject);
begin

edit1.Text:=edit1.Text+(sender as tSpeedButton).caption

end;

procedure Tf_telep.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if key=#13 then
begin
if query1.Active then
query1.close;
query1.ParamByName('myTEL').asstring:=trim(edit1.text);
query1.open;
edit2.Text:=Query1ADDRESS1.AsString;
edit4.Text:=Query1ADDRESS2.AsString;



edit3.Text:=Query1NAME.AsString;

if query1.IsEmpty then
edit3.SetFocus
else
  BitBtn1.SetFocus;


end;


end;

procedure Tf_telep.Edit2KeyPress(Sender: TObject; var Key: Char);
begin
if key=#13 then

    edit4.SetFocus;

 
end;

procedure Tf_telep.Edit3KeyPress(Sender: TObject; var Key: Char);
begin
if key=#13 then

    edit2.SetFocus;
end;

procedure Tf_telep.BitBtn1Click(Sender: TObject);
begin

TRY
if length(edit1.Text)<4 then exit;

 if trim(edit4.Text)='' then
 edit4.text:=':'+edit1.Text;




if query1.isempty=true then
begin
QUERY1.Insert;
Query1WPHONE.AsString:=EDIT1.Text;

end
else
begin
QUERY1.EDIT;
end;
QUERY1NAME.AsString:=EDIT3.Text;
QUERY1ADDRESS1.AsString:=EDIT2.Text;
Query1CRADNO.AsString:=EDIT4.Text;

QUERY1.POST;
QUERY1.CLOSE;
query1.ParamByName('myTEL').asstring:=EDIT1.Text;;
query1.ParamByName('mycard').asstring:=EDIT4.Text;;
QUERY1.OPEN;


EXCEPT

f_msgdlg:=tf_msgdlg.create(self);
try
f_msgdlg.mymsg.Caption:='No Connection To Contact!!!';
f_msgdlg.ShowModal;
except
end;
f_msgdlg.Release;




END;
f_telep.ModalResult:=mrok;

end;

procedure Tf_telep.Edit3Exit(Sender: TObject);
begin
//if edit3.Text='' then
//edit3.SetFocus;

end;

procedure Tf_telep.Edit1Exit(Sender: TObject);
var s:string;
begin
s:=edit1.Text;
//if length(s)<1 then
//begin
//edit1.SetFocus;
//exit;
//end;
if s<>'' then
begin
if query1.Active then
query1.close;
query1.ParamByName('myTEL').asstring:=trim(edit1.text);
query1.ParamByName('myCARD').asstring:='HHH';

query1.open;
edit2.Text:=Query1ADDRESS1.AsString;
edit4.Text:=Query1CRADNO.AsString;



edit3.Text:=query1name.AsString;

if query1.IsEmpty then
BEGIN

lqty.Visible:=true;
lqty.color:=clred;
LQTY.Caption:='tel Not found!!';
 lqty.Blinking:=true;

edit3.SetFocus

END
else
begin
lqty.Visible:=false;

LQTY.Caption:='';
 lqty.Blinking:=false;
  BitBtn1.SetFocus;

  end;

  end;

end;

procedure Tf_telep.ApdTapiDevice1TapiCallerID(CP: TObject; ID,
  IDName: String);
  var c:char;s:string;
begin
c:=#13;

  s:=ID;
  if s[1]='9' then
  s[1]:=' ';
  s:=trim(s);

  edit1.Text := s;


  if trim(edit1.Text)<>'' then
  begin
  Edit1KeyPress(Edit1,c);
 // BitBtn1Click(BitBtn1);

  end;

end;

procedure Tf_telep.MonitorClick(Sender: TObject);
begin
//  ApdTapiDevice1.MonitorRecording := Monitor.Checked;
end;

procedure Tf_telep.FormShow(Sender: TObject);
begin
try
nrCommTAPI1.HangUp;
except
end;
 try
  nrCommTAPI1.DeviceIndex:=0;
    try
      nrCommTAPI1.RingsBeforeAnswer:=50;
    except
      nrCommTAPI1.RingsBeforeAnswer:=20;
    end;
    nrCommTAPI1.WaitForIncomingCall;
   except
   end; 
 QUERY1.OPEN;


try
if posmodule.CURRFACCONTACT.AsInteger<>0 then
begin
curr.ParamByName('myid').AsInteger:=posmodule.CURRFACCONTACT.AsInteger;
curr.open;
edit1.Text:=curr.fieldbyname('wphone').AsString;
curr.close;

Edit1Exit(Edit1);


end;
except
end;





 end;

procedure Tf_telep.Edit4KeyPress(Sender: TObject; var Key: Char);
begin
if key=#13 then

    BitBtn1.SetFocus;
end;

procedure Tf_telep.nrCommTAPI1Ring(Com: TObject; var Ignore: Boolean);
var c:char;s:string;
begin
try
c:=#13;
s:=(com as tnrCommTAPI).CallerID;



 if trim(s)<>'' then
  begin
if s[1]='9'  then
s[1]:=' ';
s:=trim(s);


edit1.Text:=s;
  Edit1KeyPress(Edit1,c);
 // BitBtn1Click(BitBtn1);

  end;
 except
 end;

  end;

procedure Tf_telep.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin

try
nrCommTAPI1.HangUp;
except
end;

point.close;

end;

procedure Tf_telep.SpeedButton12Click(Sender: TObject);
begin

efrom.Text:=edit2.Text;
end;

procedure Tf_telep.SpeedButton13Click(Sender: TObject);
begin
efrom.Text:=edit4.Text;
end;

procedure Tf_telep.FormDestroy(Sender: TObject);
begin
query1.close;
end;

procedure Tf_telep.RzBitBtn1Click(Sender: TObject);
var key:char;
begin
key:=#13;
     if not assigned(f_barcode2) then
f_barcode2:=tf_barcode2.create(self);

try
if f_barcode2.ShowModal=mrok then
begin
point.close;
point.paramByName('myTEL').asstring:=f_barcode2.RzButtonEdit1.text;
point.open;
if point.IsEmpty=false then
begin
Edit1.Text:=pointWPHONE.AsString;
Edit3.Text:=pointname.AsString;
Edit2.Text:=pointADDRESS1.AsString;
Edit4.Text:=pointCRADNO.AsString;

if query1.Active then
query1.close;

query1.ParamByName('myCARD').asstring:=f_barcode2.RzButtonEdit1.text;
query1.ParamByName('myTEL').asstring:='HHHH';
query1.open;

end;
end;

except
f_msgdlg:=tf_msgdlg.create(self);
try
f_msgdlg.mymsg.Caption:='Server Not Connected!!!';
f_msgdlg.ShowModal;
except
end;
f_msgdlg.Release;

end;

if  assigned(f_barcode2) then
 freeandnil(f_barcode2);


end;

procedure Tf_telep.pointCalcFields(DataSet: TDataSet);
begin
//pointbal.AsCurrency:=pointPOINTIN.AsCurrency-pointpointout.AsCurrency;
end;

procedure Tf_telep.Query1CalcFields(DataSet: TDataSet);
begin
TRY
Query1bal.AsCurrency:=Query1POINTIN.AsCurrency-Query1pointout.AsCurrency;
EXCEPT
END;
end;

procedure Tf_telep.RzBitBtn5Click(Sender: TObject);
var i:integer;s:string;
begin
//s:=inputbox('Enter The Point Out','','');
if query1.IsEmpty then exit;
f_myinput:=tf_myinput.create(self);
f_myinput.mytxt.Caption:='Enter The Point Out';
try
f_myinput.ShowModal;

s:=f_myinput.RzEdit1.Text;
except
end;
f_myinput.Release;

try
i:=strtoint(s);
if i>query1bal.AsInteger then
begin
f_msgdlg:=tf_msgdlg.create(self);
try
f_msgdlg.mymsg.Caption:='Point No > Point balance';
f_msgdlg.ShowModal;
except
end;
f_msgdlg.Release;

exit;
end;

  try
contdet.ParamByName('mymid').AsInteger:=query1ID.AsInteger;
contdet.ParamByName('mycurrfac').AsInteger:=0;

contdet.ParamByName('mypointin').AsCurrency:=0;
contdet.ParamByName('mypointout').AsCurrency:=i;
contdet.ParamByName('mytotal').AsCurrency:=0;
contdet.ParamByName('mydate').AsDate:=date;
contdet.ParamByName('mytime').AsDateTime:=time;
contdet.ExecSQL;
try
//if posmodule.CONFIGSTOUCHNOPRN.AsString='YES' then
//exit;
if FileExists(extractfilepath(application.exename)+'PointOut.fr3') then
begin
repmodule.frxReport1.LoadFromFile(extractfilepath(application.exename)+'PointOut.fr3') ;
if trim(posmodule.CONFIGSPINVOICE.AsString)<>'' then
repmodule.frxReport1.PrintOptions.Printer:=posmodule.CONFIGSPINVOICE.AsString;
repmodule.frxReport1.PrintOptions.ShowDialog:=false;

repmodule.frxReport1.PrepareReport;
repmodule.frxReport1.print;
end;
except
end;

except
end;
query1.close;
query1.open;
except
end;

end;

end.
