unit DailyReport;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons, ComCtrls, ExtCtrls, Menus;

type
  TfrmDailyReport = class(TForm)
    Panel1: TPanel;
    GroupBox1: TGroupBox;
    lvDailyRep: TListView;
    PopupMenu1: TPopupMenu;
    rcPrint: TMenuItem;
    GroupBox2: TGroupBox;
    rbDaily: TRadioButton;
    rbWeekly: TRadioButton;
    rbMonthly: TRadioButton;
    gbDateSearch: TGroupBox;
    dtpFrom: TDateTimePicker;
    cmbParticulars: TComboBox;
    btnPrint: TBitBtn;
    Label1: TLabel;
    Label2: TLabel;
    dtpTo: TDateTimePicker;
    btnSearch: TBitBtn;
    Panel2: TPanel;
    rbSelectAll: TRadioButton;
    rbSelectChoice: TRadioButton;

    procedure LoadReport();
    procedure Report();
    procedure MaxID();
    procedure LoadParticularCmB();
    procedure btnPrintClick(Sender: TObject);
    procedure cmbPartSelect();
    procedure LoaddpDateSelectAll_daily();
    procedure LoaddpDateSelectAll_weekly();
    procedure LoaddpDateSelectAll_monthly();
    procedure LoaddpDateSelect_daily();
    procedure LoaddpDateSelect_weekly();
    procedure LoaddpDateSelect_monthly();
    procedure FormShow(Sender: TObject);
    procedure cmbPartChange(Sender: TObject);
    procedure rcPrintClick(Sender: TObject);
    procedure rbDailyClick(Sender: TObject);
    procedure btnSearchClick(Sender: TObject);
    procedure rbWeeklyClick(Sender: TObject);
    procedure rbMonthlyClick(Sender: TObject);
    procedure rbSelectAllClick(Sender: TObject);
    procedure rbSelectChoiceClick(Sender: TObject);

  private
    { Private declarations }
  public
    { Public declarations }
    ID:integer;
    NAME:string;
    date:string;
  end;

var
  frmDailyReport: TfrmDailyReport;

implementation

uses DM;

{$R *.dfm}
procedure TfrmDailyReport.FormShow(Sender: TObject);
begin
      LoadReport();
      LoadParticularCmB();
      dtpFrom.DateTime:=Now;
      dtpTo.DateTime:=Now;
end;
procedure TfrmDailyReport.btnPrintClick(Sender: TObject);
begin
        Report();
end;
procedure TfrmDailyReport.cmbPartChange(Sender: TObject);
begin
      cmbPartSelect();
end;
procedure TfrmDailyReport.rcPrintClick(Sender: TObject);
begin
        Report();
end;
procedure TfrmDailyReport.rbDailyClick(Sender: TObject);
begin
      dtpTo.Enabled:=False;
end;
procedure TfrmDailyReport.rbWeeklyClick(Sender: TObject);
begin
      dtpTo.Enabled:=True;
end;
procedure TfrmDailyReport.rbMonthlyClick(Sender: TObject);
begin
    dtpTo.Enabled:=True;
end;
procedure TfrmDailyReport.rbSelectAllClick(Sender: TObject);
begin
      cmbParticulars.Enabled:=False;
end;
procedure TfrmDailyReport.rbSelectChoiceClick(Sender: TObject);
begin
      cmbParticulars.Enabled:=True;
end;
procedure TfrmDailyReport.btnSearchClick(Sender: TObject);
begin
       if (rbDaily.Checked=True)then begin
          if(rbSelectAll.Checked=True) then begin
             LoaddpDateSelectAll_daily();
          end
          else
          LoaddpDateSelect_daily();
       end
       else if (rbWeekly.Checked=True) then begin
          if(rbSelectAll.Checked=True) then begin
            LoaddpDateSelectAll_weekly();
          end
          else
          LoaddpDateSelect_weekly();
       end
       else if (rbMonthly.Checked=True)then begin
          if(rbSelectAll.Checked=True) then begin
             LoaddpDateSelectAll_monthly();
          end
          else
          LoaddpDateSelect_monthly();
       end
end;



//....................dirty works here
procedure TfrmDailyReport.LoadReport();
var newState : TListItem;

begin

    if DModule.TR.InTransaction then
        DModule.TR.Commit;

    DModule.TR.StartTransaction;
    DModule.QR.SQL.Clear;
    DModule.QR.SQL.Add('SELECT ColID,PartName,Name,AmountPaid,Tracking_ORNo,IssuedDate');
    DModule.QR.SQL.Add('FROM Collections');
    DModule.QR.Prepare;

    DModule.QR.Open();
    lvDailyRep.Items.BeginUpdate;
    lvDailyRep.Items.Clear;
    if DModule.QR.Fields.IsNull[0] then Exit;
    while not DModule.QR.Eof do begin
        newState := lvDailyRep.Items.Add;
        newState.Caption := DModule.QR.Fields.AsString[0];
        newState.SubItems.Add(DModule.QR.Fields.AsString[5]);
        newState.SubItems.Add(DModule.QR.Fields.AsString[1]);
        newState.SubItems.Add(DModule.QR.Fields.AsString[2]);
        newState.SubItems.Add(DModule.QR.Fields.AsString[4]);
        newState.SubItems.Add(DModule.QR.Fields.AsString[3]);

        DModule.QR.Next;
    end;
    if DModule.TR.InTransaction then
        DModule.TR.Commit;
    lvDailyRep.Items.EndUpdate;
end;

procedure TfrmDailyReport.LoaddpDateSelectAll_Daily();
var newstate:TListItem;
begin
       if DModule.TR.InTransaction then
        DModule.TR.Commit;

        DModule.TR.StartTransaction;
        DModule.QR.SQL.Clear;
        DModule.QR.SQL.Add('SELECT ColID,PartName,Name,AmountPaid,Tracking_ORNo,IssuedDate');
        DModule.QR.SQL.Add('FROM Collections WHERE ISSUEDDATE=:a ');
          DModule.QR.Params.AsString[0]:= DateToStr(dtpFrom.DateTime);
        DModule.QR.Prepare;
        DModule.QR.Open();

          lvDailyRep.Items.BeginUpdate;
          lvDailyRep.Items.Clear;

          if DModule.QR.Fields.IsNull[0] then Exit;
          if DModule.QR.Eof then begin
              MessageDlg('Search not found',mtError,[mbOK],0);
          end;
              while not DModule.QR.Eof do begin
                newState := lvDailyRep.Items.Add;
                newState.Caption := DModule.QR.Fields.AsString[0];
                newState.SubItems.Add(DModule.QR.Fields.AsString[5]);
                newState.SubItems.Add(DModule.QR.Fields.AsString[1]);
                newState.SubItems.Add(DModule.QR.Fields.AsString[2]);
                newState.SubItems.Add(DModule.QR.Fields.AsString[4]);
                newState.SubItems.Add(DModule.QR.Fields.AsString[3]);
                  DModule.QR.Next;
            end;
            if DModule.TR.InTransaction then
                DModule.TR.Commit;
                lvDailyRep.Items.EndUpdate;
end;

procedure TfrmDailyReport.LoaddpDateSelectAll_weekly();
var newstate:TListItem;
begin
       if DModule.TR.InTransaction then
        DModule.TR.Commit;

        DModule.TR.StartTransaction;
        DModule.QR.SQL.Clear;
        DModule.QR.SQL.Add('SELECT ColID,PartName,Name,AmountPaid,Tracking_ORNo,IssuedDate');
        DModule.QR.SQL.Add('FROM Collections WHERE ISSUEDDATE between :a and :b ');
          DModule.QR.Params.AsString[0]:= DateToStr(dtpFrom.DateTime);
          DModule.QR.Params.AsString[1]:= DateToStr(dtpTo.DateTime);
        DModule.QR.Prepare;
        DModule.QR.Open();

          lvDailyRep.Items.BeginUpdate;
          lvDailyRep.Items.Clear;

          if DModule.QR.Fields.IsNull[0] then Exit;
          if DModule.QR.Eof then begin
              MessageDlg('Search not found',mtError,[mbOK],0);
          end;
              while not DModule.QR.Eof do begin
                newState := lvDailyRep.Items.Add;
                newState.Caption := DModule.QR.Fields.AsString[0];
                newState.SubItems.Add(DModule.QR.Fields.AsString[5]);
                newState.SubItems.Add(DModule.QR.Fields.AsString[1]);
                newState.SubItems.Add(DModule.QR.Fields.AsString[2]);
                newState.SubItems.Add(DModule.QR.Fields.AsString[4]);
                newState.SubItems.Add(DModule.QR.Fields.AsString[3]);
                  DModule.QR.Next;
            end;
            if DModule.TR.InTransaction then
                DModule.TR.Commit;
                lvDailyRep.Items.EndUpdate;
end;

procedure TfrmDailyReport.LoaddpDateSelectAll_monthly();
var newstate:TListItem;
begin
       if DModule.TR.InTransaction then
        DModule.TR.Commit;

        DModule.TR.StartTransaction;
        DModule.QR.SQL.Clear;
        DModule.QR.SQL.Add('SELECT ColID,PartName,Name,AmountPaid,Tracking_ORNo,IssuedDate');
        DModule.QR.SQL.Add('FROM Collections WHERE ISSUEDDATE between :a and :b ');
          DModule.QR.Params.AsString[0]:= DateToStr(dtpFrom.DateTime);
          DModule.QR.Params.AsString[1]:= DateToStr(dtpTo.DateTime); 
        DModule.QR.Prepare;
        DModule.QR.Open();

          lvDailyRep.Items.BeginUpdate;
          lvDailyRep.Items.Clear;

          if DModule.QR.Fields.IsNull[0] then Exit;
          if DModule.QR.Eof then begin
              MessageDlg('Search not found',mtError,[mbOK],0);
          end;
              while not DModule.QR.Eof do begin
                newState := lvDailyRep.Items.Add;
                newState.Caption := DModule.QR.Fields.AsString[0];
                newState.SubItems.Add(DModule.QR.Fields.AsString[5]);
                newState.SubItems.Add(DModule.QR.Fields.AsString[1]);
                newState.SubItems.Add(DModule.QR.Fields.AsString[2]);
                newState.SubItems.Add(DModule.QR.Fields.AsString[4]);
                newState.SubItems.Add(DModule.QR.Fields.AsString[3]);
                  DModule.QR.Next;
            end;
            if DModule.TR.InTransaction then
                DModule.TR.Commit;
                lvDailyRep.Items.EndUpdate;
end;

procedure TfrmDailyReport.LoaddpDateSelect_daily();
var newstate:TListItem;
begin
    if (cmbParticulars.Text='') then begin
       MessageDlg('Please select particulars',mtError,[mbOK],0);
    end
    else begin
       if DModule.TR.InTransaction then
        DModule.TR.Commit;

        DModule.TR.StartTransaction;
        DModule.QR.SQL.Clear;
        DModule.QR.SQL.Add('SELECT ColID,PartName,Name,AmountPaid,Tracking_ORNo,IssuedDate');
        DModule.QR.SQL.Add('FROM Collections WHERE PARTNAME=:a AND ISSUEDDATE=:b ');
          DModule.QR.Params.AsString[0]:= cmbParticulars.Text;
          DModule.QR.Params.AsString[1]:= DateToStr(dtpFrom.DateTime);
        DModule.QR.Prepare;
        DModule.QR.Open();

          lvDailyRep.Items.BeginUpdate;
          lvDailyRep.Items.Clear;

          if DModule.QR.Fields.IsNull[0] then Exit;
          if DModule.QR.Eof then begin
              MessageDlg('Search not found',mtError,[mbOK],0);
          end;
              while not DModule.QR.Eof do begin
                newState := lvDailyRep.Items.Add;
                newState.Caption := DModule.QR.Fields.AsString[0];
                newState.SubItems.Add(DModule.QR.Fields.AsString[5]);
                newState.SubItems.Add(DModule.QR.Fields.AsString[1]);
                newState.SubItems.Add(DModule.QR.Fields.AsString[2]);
                newState.SubItems.Add(DModule.QR.Fields.AsString[4]);
                newState.SubItems.Add(DModule.QR.Fields.AsString[3]);
                  DModule.QR.Next;
            end;
            if DModule.TR.InTransaction then
                DModule.TR.Commit;
                lvDailyRep.Items.EndUpdate;
    end//end sa else
end;


procedure TfrmDailyReport.LoaddpDateSelect_weekly();
var newstate:TListItem;
begin
       if DModule.TR.InTransaction then
        DModule.TR.Commit;

    DModule.TR.StartTransaction;
    DModule.QR.SQL.Clear;
    DModule.QR.SQL.Add('SELECT ColID,PartName,Name,AmountPaid,Tracking_ORNo,IssuedDate');
    DModule.QR.SQL.Add('FROM Collections WHERE PARTNAME=:a AND ISSUEDDATE BETWEEN :b and :c ');
      DModule.QR.Params.AsString[0]:= cmbParticulars.Text;
      DModule.QR.Params.AsString[1]:= DateToStr(dtpFrom.DateTime);
      DModule.QR.Params.AsString[2]:=DateToStr(dtpTo.DateTime);
    DModule.QR.Prepare;

    DModule.QR.Open();
    lvDailyRep.Items.BeginUpdate;
    lvDailyRep.Items.Clear;
    if DModule.QR.Fields.IsNull[0] then Exit;
    while not DModule.QR.Eof do begin
        newState := lvDailyRep.Items.Add;
        newState.Caption := DModule.QR.Fields.AsString[0];
        newState.SubItems.Add(DModule.QR.Fields.AsString[5]);
        newState.SubItems.Add(DModule.QR.Fields.AsString[1]);
        newState.SubItems.Add(DModule.QR.Fields.AsString[2]);
        newState.SubItems.Add(DModule.QR.Fields.AsString[4]);
        newState.SubItems.Add(DModule.QR.Fields.AsString[3]);

        DModule.QR.Next;
    end;
    if DModule.TR.InTransaction then
        DModule.TR.Commit;
    lvDailyRep.Items.EndUpdate;
end;

procedure TfrmDailyReport.LoaddpDateSelect_monthly();
var newstate:TListItem;
begin
       if DModule.TR.InTransaction then
        DModule.TR.Commit;

        DModule.TR.StartTransaction;
        DModule.QR.SQL.Clear;
        DModule.QR.SQL.Add('SELECT ColID,PartName,Name,AmountPaid,Tracking_ORNo,IssuedDate');
        DModule.QR.SQL.Add('FROM Collections WHERE PARTNAME=:a AND ISSUEDDATE BETWEEN :b and :c ');
          DModule.QR.Params.AsString[0]:= cmbParticulars.Text;
          DModule.QR.Params.AsString[1]:= DateToStr(dtpFrom.DateTime);
          DModule.QR.Params.AsString[2]:=DateToStr(dtpTo.DateTime);
        DModule.QR.Prepare;
        DModule.QR.Open();

        lvDailyRep.Items.BeginUpdate;
        lvDailyRep.Items.Clear;

          if DModule.QR.Fields.IsNull[0] then Exit;
          if DModule.QR.Eof then begin
              MessageDlg('Search not found',mtError,[mbOK],0);
          end;
            while not DModule.QR.Eof do begin
            newState := lvDailyRep.Items.Add;
            newState.Caption := DModule.QR.Fields.AsString[0];
            newState.SubItems.Add(DModule.QR.Fields.AsString[5]);
            newState.SubItems.Add(DModule.QR.Fields.AsString[1]);
            newState.SubItems.Add(DModule.QR.Fields.AsString[2]);
            newState.SubItems.Add(DModule.QR.Fields.AsString[4]);
            newState.SubItems.Add(DModule.QR.Fields.AsString[3]);
              DModule.QR.Next;
    end;
          if DModule.TR.InTransaction then
              DModule.TR.Commit;
          lvDailyRep.Items.EndUpdate;
end;


procedure TfrmDailyReport.Report();
begin
MaxID();
      DModule.rep.Filename := 'reports/SumColRemit_CTC.rep';
      //DModule.rep.Report.Params.Items[0].Value :=ID;
      DModule.rep.Report.Params.Items[0].Value :=cmbParticulars.Text;
      DModule.rep.Report.Params.Items[1].Value :=DateToStr(dtpFrom.DateTime);
      DModule.rep.Report.Params.Items[2].Value :=DateToStr(dtpTo.DateTime);
      DModule.rep.Preview := True;
      DModule.rep.Execute();
end;

procedure TfrmDailyReport.MaxID();
begin
 if DModule.TR.InTransaction then
        DModule.TR.Commit;
    DModule.QR.SQL.Clear();
    DModule.QR.SQL.Add('SELECT *');
    DModule.QR.SQL.Add('FROM ClearanceDetails');
    DModule.QR.Prepare;
    DModule.QR.Open();
     ID := DModule.QR.Fields.AsInteger[0];
     NAME := DModule.QR.Fields.AsString[2];
     date :=  DModule.QR.Fields.AsString[6];
   if DModule.QR.Fields.Eof then
         if DModule.TR.InTransaction then
       DModule.TR.Commit;
end;

procedure TfrmDailyReport.LoadParticularCmB();
begin
      if DModule.TR.InTransaction then
        DModule.TR.Commit;

    DModule.TR.StartTransaction;
    DModule.QR.SQL.Clear();
    DModule.QR.SQL.Add('SELECT Description');
    DModule.QR.SQL.Add('FROM Particulars');
    DModule.QR.Prepare;
    DModule.QR.Open();

    cmbParticulars.Items.BeginUpdate;
    cmbParticulars.Items.Clear;
    if DModule.QR.Fields.IsNull[0] then Exit;
    while not DModule.QR.Eof do begin
        cmbParticulars.Items.Add(DModule.QR.Fields.AsString[0]);
        DModule.QR.Next;
        end;
    if DModule.TR.InTransaction then
        DModule.TR.Commit;
    cmbParticulars.Items.EndUpdate;
end;


procedure TfrmDailyReport.cmbPartSelect();
var newstate:TListItem;
begin
        if DModule.TR.InTransaction then
        DModule.TR.Commit;

    DModule.TR.StartTransaction;
    DModule.QR.SQL.Clear;
    DModule.QR.SQL.Add('SELECT ColID,PartName,Name,AmountPaid,Tracking_ORNo,IssuedDate');
    DModule.QR.SQL.Add('FROM Collections WHERE PARTNAME=:a');
      DModule.QR.Params.AsString[0]:= cmbParticulars.Text;
    DModule.QR.Prepare;

    DModule.QR.Open();
    lvDailyRep.Items.BeginUpdate;
    lvDailyRep.Items.Clear;
    if DModule.QR.Fields.IsNull[0] then Exit;
    while not DModule.QR.Eof do begin
        newState := lvDailyRep.Items.Add;
        newState.Caption := DModule.QR.Fields.AsString[0];
        newState.SubItems.Add(DModule.QR.Fields.AsString[5]);
        newState.SubItems.Add(DModule.QR.Fields.AsString[1]);
        newState.SubItems.Add(DModule.QR.Fields.AsString[2]);
        newState.SubItems.Add(DModule.QR.Fields.AsString[4]);
        newState.SubItems.Add(DModule.QR.Fields.AsString[3]);

        DModule.QR.Next;
    end;
    if DModule.TR.InTransaction then
        DModule.TR.Commit;
    lvDailyRep.Items.EndUpdate;
end;

end.
