unit p_control;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  RzButton, ExtCtrls, RzPanel, RzTabs, StdCtrls, DBCtrls, DBCGrids, Db,
  DBTables, cxGridCustomTableView, cxGridTableView, cxGridDBTableView,
  cxGridLevel, cxClasses, cxControls, cxGridCustomView, cxGrid, Menus,
  AdvMenus, Mask, RzEdit, RzDBEdit, ToolEdit, Execfile;

type
  Tf_control = class(TForm)
    RzPageControl1: TRzPageControl;
    tablist: TRzTabSheet;
    tabstate: TRzTabSheet;
    Tabitem: TRzTabSheet;
    RzPanel1: TRzPanel;
    Rzexit: TRzBitBtn;
    RzPanel2: TRzPanel;
    RzMenuButton2: TRzMenuButton;
    currfac: TQuery;
    dcurrfac: TDataSource;
    DBCtrlGrid1: TDBCtrlGrid;
    DBText1: TDBText;
    DBText2: TDBText;
    Label1: TLabel;
    currfacID: TIntegerField;
    currfacAUTONO: TIntegerField;
    currfacFNO: TStringField;
    currfacVAT: TFloatField;
    currfacSUBTOTAL: TFloatField;
    currfacTOTAL: TFloatField;
    currfacCASHIN: TFloatField;
    currfacCASHOUT: TFloatField;
    currfacTABLENO: TStringField;
    currfacIACTIVE: TStringField;
    currfacNAME: TStringField;
    currfacCOVER: TIntegerField;
    currfacISTATE: TIntegerField;
    currfacLASTTIME: TDateTimeField;
    currfacCREATETIME: TDateTimeField;
    currfacDISCOUNT: TFloatField;
    currfacPRN: TStringField;
    currfacCHEF: TStringField;
    currfacSID: TStringField;
    currfacCONTACT: TIntegerField;
    currfacZALEID: TIntegerField;
    currfacSDISC: TFloatField;
    cxGrid2: TcxGrid;
    cxGridDBTableView1: TcxGridDBTableView;
    cxGridLevel1: TcxGridLevel;
    Table1: TTable;
    DataSource1: TDataSource;
    Table1ID: TIntegerField;
    Table1MID: TStringField;
    Table1ITEM: TStringField;
    Table1SITE: TStringField;
    Table1STTYPE: TStringField;
    Table1QTY: TFloatField;
    Table1FRAC: TFloatField;
    Table1PACK: TFloatField;
    Table1PRICE: TFloatField;
    Table1DISCOUNT: TFloatField;
    Table1VAT: TFloatField;
    Table1COSTUS: TFloatField;
    Table1COSTLC: TFloatField;
    Table1CATREGORYID: TIntegerField;
    Table1VATAMOUNT: TFloatField;
    Table1DEBITUS: TFloatField;
    Table1CREDITUS: TFloatField;
    Table1BARCODE: TStringField;
    Table1BONENO: TStringField;
    Table1DEBITQTY: TFloatField;
    Table1CREDITQTY: TFloatField;
    Table1YESNO: TStringField;
    Table1TOTAL: TFloatField;
    Table1FDATE: TDateTimeField;
    Table1ALLQTY: TFloatField;
    Table1JOB: TIntegerField;
    Table1SID: TStringField;
    Table1SALESMAN: TIntegerField;
    Table1CONTACT: TIntegerField;
    Table1TSITE: TStringField;
    Table1DEBITLC: TFloatField;
    Table1CLC: TFloatField;
    Table1CREDITLC: TFloatField;
    Table1STQTY: TFloatField;
    Table1CURRFAC: TIntegerField;
    Table1AUTOCURRFAC: TIntegerField;
    Table1PRN: TStringField;
    Table1DESCR: TStringField;
    Table1MYPRN: TStringField;
    Table1EXTYESNO: TIntegerField;
    Table1FIDATE: TDateTimeField;
    cxGridDBTableView1SITE: TcxGridDBColumn;
    cxGridDBTableView1QTY: TcxGridDBColumn;
    cxGridDBTableView1PACK: TcxGridDBColumn;
    cxGridDBTableView1PRICE: TcxGridDBColumn;
    cxGridDBTableView1DISCOUNT: TcxGridDBColumn;
    cxGridDBTableView1VAT: TcxGridDBColumn;
    cxGridDBTableView1BARCODE: TcxGridDBColumn;
    cxGridDBTableView1TOTAL: TcxGridDBColumn;
    cxGridDBTableView1DESCR: TcxGridDBColumn;
    DBText3: TDBText;
    Label2: TLabel;
    Edit1: TEdit;
    Label3: TLabel;
    Label4: TLabel;
    Edit2: TEdit;
    invmenu: TAdvPopupMenu;
    MenuItem8: TMenuItem;
    InvoicewithDetails1: TMenuItem;
    Query1: TQuery;
    Query1SUM: TFloatField;
    RzDBEdit1: TRzDBEdit;
    Label5: TLabel;
    DataSource2: TDataSource;
    RzPanel3: TRzPanel;
    Label6: TLabel;
    RzMenuButton3: TRzMenuButton;
    Edit3: TEdit;
    DBCtrlGrid2: TDBCtrlGrid;
    DBText4: TDBText;
    DBText5: TDBText;
    Label9: TLabel;
    cxGrid1: TcxGrid;
    cxGridDBTableView2: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridDBColumn9: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    QITEMS3: TQuery;
    QITEMS3CURRFAC: TIntegerField;
    QITEMS3AUTONO: TIntegerField;
    QITEMS3TOTAL: TFloatField;
    DQITEMS3: TDataSource;
    Table2: TTable;
    DataSource3: TDataSource;
    DateEdit1: TDateEdit;
    qaudit: TQuery;
    cxGrid4: TcxGrid;
    cxGridDBTableView4: TcxGridDBTableView;
    cxGridDBColumn10: TcxGridDBColumn;
    cxGridDBColumn11: TcxGridDBColumn;
    cxGridDBColumn12: TcxGridDBColumn;
    cxGridDBColumn13: TcxGridDBColumn;
    cxGridDBColumn14: TcxGridDBColumn;
    cxGridDBColumn15: TcxGridDBColumn;
    cxGridDBColumn16: TcxGridDBColumn;
    cxGridDBColumn17: TcxGridDBColumn;
    cxGridDBColumn18: TcxGridDBColumn;
    cxGridLevel4: TcxGridLevel;
    dqaudit: TDataSource;
    cxGridDBTableView4Column1: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    CloseInvoice1: TMenuItem;
    PrintA41: TMenuItem;
    RzMenuButton1: TRzMenuButton;
    RzBitBtn1: TRzBitBtn;
    OpenInvoice01: TMenuItem;
    DBText6: TDBText;
    N1: TMenuItem;
    DetailReport1: TMenuItem;
    ChefDetailReport1: TMenuItem;
    cxGridDBTableView1Column1: TcxGridDBColumn;
    currfacUSER_: TStringField;
    DBText7: TDBText;
    Table2ID: TIntegerField;
    Table2MID: TStringField;
    Table2ITEM: TStringField;
    Table2SITE: TStringField;
    Table2STTYPE: TStringField;
    Table2QTY: TFloatField;
    Table2FRAC: TFloatField;
    Table2PACK: TFloatField;
    Table2PRICE: TFloatField;
    Table2DISCOUNT: TFloatField;
    Table2VAT: TFloatField;
    Table2COSTUS: TFloatField;
    Table2COSTLC: TFloatField;
    Table2CATREGORYID: TIntegerField;
    Table2VATAMOUNT: TFloatField;
    Table2DEBITUS: TFloatField;
    Table2CREDITUS: TFloatField;
    Table2BARCODE: TStringField;
    Table2BONENO: TStringField;
    Table2DEBITQTY: TFloatField;
    Table2CREDITQTY: TFloatField;
    Table2YESNO: TStringField;
    Table2TOTAL: TFloatField;
    Table2FDATE: TDateTimeField;
    Table2ALLQTY: TFloatField;
    Table2JOB: TIntegerField;
    Table2SID: TStringField;
    Table2SALESMAN: TIntegerField;
    Table2CONTACT: TIntegerField;
    Table2TSITE: TStringField;
    Table2DEBITLC: TFloatField;
    Table2CLC: TFloatField;
    Table2CREDITLC: TFloatField;
    Table2STQTY: TFloatField;
    Table2CURRFAC: TIntegerField;
    Table2AUTOCURRFAC: TIntegerField;
    Table2PRN: TStringField;
    Table2DESCR: TStringField;
    Table2MYPRN: TStringField;
    Table2EXTYESNO: TIntegerField;
    Table2FIDATE: TDateTimeField;
    cxGridDBTableView2Column1: TcxGridDBColumn;
    QITEMS3FDATE: TDateTimeField;
    RzBitBtn2: TRzBitBtn;
    RzBitBtn3: TRzBitBtn;
    exehash: TExecFile;
    procedure RzexitClick(Sender: TObject);
    procedure currfacAfterOpen(DataSet: TDataSet);
    procedure currfacBeforeClose(DataSet: TDataSet);
    procedure Edit1Change(Sender: TObject);
    procedure Edit2Change(Sender: TObject);
    procedure InvoicewithDetails1Click(Sender: TObject);
    procedure MenuItem8Click(Sender: TObject);
    procedure QITEMS3AfterOpen(DataSet: TDataSet);
    procedure QITEMS3BeforeClose(DataSet: TDataSet);
    procedure RzPageControl1Click(Sender: TObject);
    procedure Edit3Change(Sender: TObject);
    procedure RzMenuButton3Click(Sender: TObject);
    procedure DateEdit1Change(Sender: TObject);
    procedure CloseInvoice1Click(Sender: TObject);
    procedure PrintA41Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure RzMenuButton1Click(Sender: TObject);
    procedure RzBitBtn1Click(Sender: TObject);
    procedure OpenInvoice01Click(Sender: TObject);
    procedure DetailReport1Click(Sender: TObject);
    procedure ChefDetailReport1Click(Sender: TObject);
    procedure DBCtrlGrid1PaintPanel(DBCtrlGrid: TDBCtrlGrid;
      Index: Integer);
    procedure RzBitBtn2Click(Sender: TObject);
    procedure RzBitBtn3Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  f_control: Tf_control;

implementation

uses p_repmodule, p_posmodule, p_fromto, p_cube;

{$R *.DFM}

procedure Tf_control.RzexitClick(Sender: TObject);
begin
close;
end;

procedure Tf_control.currfacAfterOpen(DataSet: TDataSet);
begin
try
table1.open;
except
end;

end;

procedure Tf_control.currfacBeforeClose(DataSet: TDataSet);
begin
table1.close;
end;

procedure Tf_control.Edit1Change(Sender: TObject);
begin
currfac.close;
if trim(edit1.text)='' then
currfac.sql.Strings[1]:=' '
else
currfac.sql.Strings[1]:='Where autono='+trim(edit1.text);
try
currfac.open;
except
currfac.sql.Strings[1]:=' ';
currfac.open;
end;

end;



procedure Tf_control.Edit2Change(Sender: TObject);
begin
currfac.close;
if trim(edit2.text)='' then
currfac.sql.Strings[1]:=' '
else
currfac.sql.Strings[1]:='Where total='+trim(edit2.text);
try
currfac.open;
except
currfac.sql.Strings[1]:=' ';
currfac.open;
end;
end;

procedure Tf_control.InvoicewithDetails1Click(Sender: TObject);
begin

try
repmodule.frxReport1.LoadFromFile(extractfilepath(application.exename)+ 'zreport.fr3') ;
repmodule.frxReport1.ShowReport  ;
except
end;
end;

procedure Tf_control.MenuItem8Click(Sender: TObject);
var s:string;i:integer; ttt:ttime; ff:textfile;
begin

posmodule.CURrFAC.open;
posmodule.CURrFAC.LOCATE('ID',CURRFACID.AsInteger,[]);

posmodule.items.open;


if FileExists( extractfilepath(application.exename)+'hash.exe') then
begin
try
AssignFile(ff,'inv.txt');

Rewrite(ff);
writeln(ff,posmodule.currfacid.asstring);
except
end;
 CloseFile(ff);

  exehash.Execute;

end
else
begin
   try
repmodule.frxReport1.LoadFromFile(extractfilepath(application.exename)+'posinv.fr3') ;


repmodule.frxReport1.PrepareReport;
repmodule.frxReport1.PrintOptions.ShowDialog:=false;


repmodule.frxReport1.print;
except
end;


end;
posmodule.CURrFAC.last;



end;

procedure Tf_control.QITEMS3AfterOpen(DataSet: TDataSet);
begin

TABLE2.OPEN;
end;

procedure Tf_control.QITEMS3BeforeClose(DataSet: TDataSet);
begin
TABLE2.CLOSE;
end;

procedure Tf_control.RzPageControl1Click(Sender: TObject);
begin
QITEMS3.CLOSE;
qaudit.close;
  IF RzPageControl1.ActivePage=TABSTATE THEN
  BEGIN
DATEEDIT1.Text:=DATETOSTR(DATE);
QITEMS3.OPEN;
  END;
  IF RzPageControl1.ActivePage=TABitem THEN
  BEGIN

Qaudit.OPEN;
  END;



end;

procedure Tf_control.Edit3Change(Sender: TObject);
begin
QITEMS3.close;
if trim(edit3.text)='' then
QITEMS3.sql.Strings[2]:=' '
else
QITEMS3.sql.Strings[2]:=' AND AUTOCURRFAC='+trim(edit3.text);
try
QITEMS3.open;
except
QITEMS3.sql.Strings[2]:=' ';
QITEMS3.open;
end;
end;

procedure Tf_control.RzMenuButton3Click(Sender: TObject);
begin
try
repmodule.frxReport1.LoadFromFile(extractfilepath(application.exename)+ 'oldzreport.fr3') ;
repmodule.frxReport1.ShowReport  ;
except
end;
end;

procedure Tf_control.DateEdit1Change(Sender: TObject);
begin
QITEMS3.CLOSE;
QITEMS3.ParamByName('MYDATE').ASDATE:=STRTODATE(DATEEDIT1.TEXT);
QITEMS3.OPEN;
end;

procedure Tf_control.CloseInvoice1Click(Sender: TObject);
begin
if messagedlg('Are You Sure To Close The Active Invoice???',mtwarning,mbokcancel,0)<>mrok then exit;
if messagedlg('Are You Realy Sure ???',mtwarning,mbokcancel,0)<>mrok then exit;
try
posmodule.pos.StartTransaction;
posmodule.pos.execute('update CURRFAC set iactive=''False'' where autono='+currfacAUTONO.AsString);
posmodule.pos.Commit;
currfac.close;
currfac.open;
except
if posmodule.pos.InTransaction then
posmodule.pos.Rollback;
end;

end;

procedure Tf_control.PrintA41Click(Sender: TObject);
begin

posmodule.CURrFAC.open;
posmodule.CURrFAC.LOCATE('ID',CURRFACID.AsInteger,[]);

posmodule.items.open;
try
repmodule.frxReport1.LoadFromFile(extractfilepath(application.exename)+ 'posinva4.fr3') ;
repmodule.frxReport1.ShowReport  ;
except
end;
end;

procedure Tf_control.FormCreate(Sender: TObject);
begin
try
rzdbedit1.Visible:=posmodule.touser.FieldByName('ACCREPRATIO').AsBoolean;
currfac.open;
query1.open;
except
end;
end;

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

procedure Tf_control.RzMenuButton1Click(Sender: TObject);
begin
try
repmodule.frxReport1.LoadFromFile(extractfilepath(application.exename)+ 'oldposinv.fr3') ;
repmodule.frxReport1.ShowReport  ;
except
end;
end;

procedure Tf_control.RzBitBtn1Click(Sender: TObject);
begin
if messagedlg('Are You Sure To Close The Active Invoice???',mtwarning,mbokcancel,0)<>mrok then exit;
if messagedlg('Are You Realy Sure ???',mtwarning,mbokcancel,0)<>mrok then exit;
try
posmodule.pos.StartTransaction;
posmodule.pos.execute('update CURRFAC set iactive=''False'' where (iactive<>''True'')'  );
posmodule.pos.Commit;
currfac.close;
currfac.open;
except
if posmodule.pos.InTransaction then
posmodule.pos.Rollback;
end;

end;

procedure Tf_control.OpenInvoice01Click(Sender: TObject);
begin
if messagedlg('Are You Sure To Open The 0 Invoice???',mtwarning,mbokcancel,0)<>mrok then exit;
if messagedlg('Are You Realy Sure ???',mtwarning,mbokcancel,0)<>mrok then exit;
try
posmodule.pos.StartTransaction;
posmodule.pos.execute('update CURRFAC set iactive=''D'' where autono='+currfacAUTONO.AsString);
posmodule.pos.Commit;
currfac.close;
currfac.open;
except
if posmodule.pos.InTransaction then
posmodule.pos.Rollback;
end;


end;

procedure Tf_control.DetailReport1Click(Sender: TObject);
begin
try
repmodule.frxReport1.LoadFromFile(extractfilepath(application.exename)+ 'zdetails.fr3') ;
repmodule.frxReport1.ShowReport  ;
except
end;
end;

procedure Tf_control.ChefDetailReport1Click(Sender: TObject);
begin
try
repmodule.frxReport1.LoadFromFile(extractfilepath(application.exename)+ 'chef.fr3') ;
repmodule.frxReport1.ShowReport  ;
except
end;
end;

procedure Tf_control.DBCtrlGrid1PaintPanel(DBCtrlGrid: TDBCtrlGrid;
  Index: Integer);
begin
try
if    currfacTOTAL.AsCurrency=0 then
dbtext2.Color:=clred
else
dbtext2.Color:=$00E0E6ED;
except
end;
end;

procedure Tf_control.RzBitBtn2Click(Sender: TObject);
begin
if posmodule.touser.FieldByName('ACCREPRATIO').AsBoolean<>true then exit;


try


f_cube:=tf_cube.create(self);

f_cube.query1.sql.Strings[0]:='select  items.*,extract (hour from fidate) as My_Hour   from items ' ;
f_cube.query1.SQL.Strings[1]:='  ';
   f_cube.fcCube1.Active := True;

f_cube.ShowModal;

except
end  ;

f_cube.Release;
end;

procedure Tf_control.RzBitBtn3Click(Sender: TObject);
begin
if posmodule.touser.FieldByName('ACCREPRATIO').AsBoolean<>true then exit;

f_fromto:=tf_fromto.Create(self);
try

if f_fromto.ShowModal=mrok then
begin
p_posmodule.fd:=strtodate(f_fromto.fromdate.Text);
p_posmodule.td:=strtodate(f_fromto.todate.Text);
f_cube:=tf_cube.create(self);

f_cube.query1.sql.Strings[0]:='select  items3.*,extract (hour from fidate) as My_Hour   from items3 ' ;
f_cube.query1.sql.Strings[1]:='where fdate between :fromdate and :todate';

   f_cube.fcCube1.Active := True;


f_cube.ShowModal;
end;
except
end  ;
f_fromto.Release;

f_cube.Release;
end;

end.
