unit Report;

interface

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

type
  TfrmReport = 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;
    lbFrom: TLabel;
    lbTo: TLabel;
    dtpTo: TDateTimePicker;
    btnSearch: TBitBtn;
    Panel2: TPanel;
    btnViewAll: TButton;
    GroupBox3: TGroupBox;
    rbSelectChoice: TRadioButton;
    rbSelectAll: TRadioButton;
    Panel3: TPanel;
    Panel4: TPanel;
    cmbMonth: TComboBox;
    txtYear: TEdit;

    procedure LoadReport();
    procedure ReportDaily_SelectAll();
    procedure ReportWeekly_SelectAll();
    procedure ReportMonthly_SelectAll(startingdate:string;lastday:string;fullname:string);
    procedure ReportDaily_SelectChoice();
    procedure ReportWeekly_SelectChoice();
    procedure ReportMonthly_SelectChoice(startingdate:string;lastday:string;combobox:string);
    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 rbSelectAll1Click(Sender: TObject);
    procedure rbSelectChoice1Click(Sender: TObject);
    procedure btnViewAllClick(Sender: TObject);
    procedure dtpFromChange(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure countColumns();

  private
    { Private declarations }
  public
    { Public declarations }
    ID:integer;
    NAME:string;
    date:string;
    daysCountDiff:Integer;
    ansDaysdiff:string;
    monthDaysCount_feb:Integer;
    monthDaysCount_30:Integer;
    monthDaysCount_31:Integer;
    count:Integer;
       month:string;
      day:integer;
      startingdate:string;
      endingdate:string;
      numberOfMonth:integer;
        lastDayOfmonth:TDateTime;
        lastday:string;

    end;

var
  frmReport: TfrmReport;
  nowyear:string;
  
implementation

uses DM, Math, DateUtils, Main;

{$R *.dfm}

procedure TfrmReport.FormShow(Sender: TObject);
begin
      //LoadReport();
      LoadParticularCmB();
      dtpFrom.DateTime:=Now;
      dtpTo.DateTime:=Now;
     // StatusBar1.Panels.Items[0].Text:='Columns: '+IntToStr(count);
     // StatusBar1.Panels.Items[2].Text:='Time: '+ TimeToStr(Now);
end;
procedure TfrmReport.btnPrintClick(Sender: TObject);
begin
        if (rbDaily.Checked = True) then begin
            if (rbSelectAll.Checked=True) then begin
                ReportDaily_SelectAll();
            end
            else begin
                ReportDaily_SelectChoice();
            end
        end
        else if (rbWeekly.Checked = True)then begin
            if (rbSelectAll.Checked=True) then begin
                ReportWeekly_SelectAll();
            end
            else begin
                ReportWeekly_SelectChoice();
            end
        end
        else if (rbMonthly.Checked = True)then begin
            //if (rbSelectAll.Checked=True) then begin
                //ShowMessage(startingdate);
                //ShowMessage(lastday);
                //ShowMessage(frmMain.StatusBar1.Panels.Items[1].Text);
                ReportMonthly_SelectAll(startingdate,lastday,frmMain.StatusBar1.Panels.Items[1].Text);
            //end
            {else begin
                ShowMessage(startingdate);
                ShowMessage(lastday);
                ShowMessage(cmbParticulars.Text);
                ReportMonthly_SelectChoice(startingdate,lastday,cmbParticulars.Text');
                
            end }
        end      
end;
procedure TfrmReport.cmbPartChange(Sender: TObject);
begin
      cmbPartSelect();
end;
procedure TfrmReport.rcPrintClick(Sender: TObject);
begin
        if (rbDaily.Checked = True) then begin
            if (rbSelectAll.Checked=True) then begin
                ReportDaily_SelectAll();
            end
            else begin
                ReportDaily_SelectChoice();
            end
        end
        else if (rbWeekly.Checked = True)then begin
            if (rbSelectAll.Checked=True) then begin
                ReportWeekly_SelectAll();
            end
            else begin
                ReportWeekly_SelectChoice();
            end
        end
        else if (rbMonthly.Checked = True)then begin
            //if (rbSelectAll.Checked=True) then begin
                ReportMonthly_SelectAll(startingdate,lastday,frmMain.StatusBar1.Panels.Items[1].Text);
            //end
            {else begin
                ReportMonthly_SelectChoice(startingdate,lastday,cmbParticulars.Text);
            end}
        end      
end;
procedure TfrmReport.rbDailyClick(Sender: TObject);
begin
      //dtpTo.Enabled:=False;
      dtpTo.Enabled:=False;;
      dtpFrom.Visible:=True;
      cmbMonth.Visible:=False;
      txtYear.Visible:=False;
      lbFrom.Caption:='Date:';
      GroupBox3.Visible:=True;
      cmbParticulars.Visible:=True;
      lbTo.Caption:='To:';
end;
procedure TfrmReport.rbWeeklyClick(Sender: TObject);
begin
      dtpTo.Visible:=True;
      dtpTo.Enabled:=True;
      dtpFrom.Visible:=True;
      cmbMonth.Visible:=False;
      txtYear.Visible:=False;
      lbFrom.Caption:='From:';
      lbTo.Caption:='To:';
      GroupBox3.Visible:=True;
      cmbParticulars.Visible:=True;
end;
procedure TfrmReport.rbMonthlyClick(Sender: TObject);
begin
     dtpTo.Visible:=False;
     dtpFrom.Visible:=False;
     lbFrom.Caption:='Month:';
     lbTo.Caption:='Year:';
     txtYear.Visible:=True;
     cmbMonth.Visible:=True;
     GroupBox3.Visible:=False;
     cmbParticulars.Visible:=False;
end;
procedure TfrmReport.rbSelectAll1Click(Sender: TObject);
begin
      cmbParticulars.Enabled:=False;
end;
procedure TfrmReport.rbSelectChoice1Click(Sender: TObject);
begin
      cmbParticulars.Enabled:=True;
end;
procedure TfrmReport.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;



//....................tricks, magics goes here
procedure TfrmReport.countColumns();
begin
      if DModule.TR.InTransaction then
        DModule.TR.Commit;

          DModule.QR.SQL.Clear;
          DModule.QR.SQL.Add('SELECT COUNT(COLID)');
          DModule.QR.SQL.Add('FROM COLLECTIONS');
          DModule.QR.Prepare;
          DModule.QR.Open();
            count:=DModule.QR.Fields.AsInteger[0];
          //StatusBar1.Panels.Items[0].Text:='Columns: '+IntToStr(count);
      if DModule.TR.InTransaction then
        DModule.TR.Commit;
end;

procedure TfrmReport.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 a.ColID,b.Description,c.Name,a.AmountPaid,a.Tracking_ORNo,a.IssuedDate');
    DModule.QR.SQL.Add('FROM Collections a, Particulars b, TaxPayer c where a.P_Num=b.P_Num and a.TP_ID=c.TP_ID');
    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 TfrmReport.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 a.ColID,b.Description,c.Name,a.AmountPaid,a.Tracking_ORNo,a.IssuedDate');
    DModule.QR.SQL.Add('FROM Collections a, Particulars b, TaxPayer c WHERE a.P_Num=b.P_Num and a.TP_ID=c.TP_ID and a.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('No records 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 TfrmReport.LoaddpDateSelectAll_weekly();
  var newstate:TListItem;
      fromDate,toDate:TDateTime ;
begin
       fromDate := dtpFrom.DateTime;
       toDate := dtpTo.DateTime;
       ansDaysdiff:=IntToStr(DaysBetween(toDate,fromDate));
       daysCountDiff:=6;

      if (daysCountDiff < StrToInt(ansDaysdiff)) then begin
          MessageDlg('Number of days counts greater that 7 days of a week', mtError,[mbOK],0);
          btnPrint.Enabled:=False;
      end
      else if (daysCountDiff > StrToInt(ansDaysdiff)) then begin
          MessageDlg('Number of days counts lesser that 7 days of a week', mtError,[mbOK],0);
          btnPrint.Enabled:=False;
      end
      else
        btnPrint.Enabled:=True;

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

        DModule.TR.StartTransaction;
        DModule.QR.SQL.Clear;
        DModule.QR.SQL.Add('SELECT a.ColID,b.Description,c.Name,a.AmountPaid,a.Tracking_ORNo,a.IssuedDate');
        DModule.QR.SQL.Add('FROM Collections a, Particulars b, TaxPayer c where a.P_Num=b.P_Num and a.TP_ID=c.TP_ID and a.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('No records 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 TfrmReport.LoaddpDateSelectAll_monthly();
  var newstate:TListItem;

begin
      day:=1;

      if(StrToInt(txtYear.Text) > StrToInt(nowyear))then begin
                ShowMessage('Invalid! Cannot set year in future time');
                txtYear.SetFocus;
                btnPrint.Enabled:=False;
      end
      else if(cmbMonth.Text='')then begin
         MessageDlg('Please select month',mtError,[mbOK],0);
         cmbMonth.SetFocus;
         btnPrint.Enabled:=False;
      end
      else if(txtYear.Text='')then begin
         MessageDlg('Please input year',mtError,[mbOK],0);
         cmbMonth.SetFocus;
         btnPrint.Enabled:=False;
      end

      else begin
          btnPrint.Enabled:=True;
             if (cmbMonth.Text='January')then begin
               numberOfMonth:=1;
            end
            else if (cmbMonth.Text='February')then begin
               numberOfMonth:=2;
            end
            else if (cmbMonth.Text='March')then begin
               numberOfMonth:=3;
            end
            else if (cmbMonth.Text='April')then begin
               numberOfMonth:=4;
            end
            else if (cmbMonth.Text='May')then begin
               numberOfMonth:=5;
            end
            else if (cmbMonth.Text='June')then begin
               numberOfMonth:=6;
            end
            else if (cmbMonth.Text='July')then begin
               numberOfMonth:=7;
            end
            else if (cmbMonth.Text='August')then begin
               numberOfMonth:=8;
            end
            else if (cmbMonth.Text='September')then begin
               numberOfMonth:=9;
            end
            else if (cmbMonth.Text='October')then begin
               numberOfMonth:=10;
            end
            else if (cmbMonth.Text='November')then begin
               numberOfMonth:=11;
            end
            else begin
               numberOfMonth:=12;
            end;

            startingdate:=inttostr(numberOfMonth)+'/'+ inttostr(day)+'/'+txtyear.Text;
            month:=  FormatDateTime('mmmm',strtodatetime(startingdate));
            endingdate:=inttostr(numberOfMonth)+'/'+ inttostr(day)+'/'+txtyear.Text;
            lastDayOfmonth := EncodeDate(YearOf(StrToDateTime(endingdate)), MonthOf(StrToDateTime(endingdate)), MonthDays[IsLeapYear(YearOf(StrToDateTime(endingdate))), MonthOf(StrToDateTime(endingdate))]);
            lastday:=datetostr(lastDayOfmonth);

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

              DModule.TR.StartTransaction;
              DModule.QR.SQL.Clear;
              DModule.QR.SQL.Add('SELECT a.ColID,b.Description,c.Name,a.AmountPaid,a.Tracking_ORNo,a.IssuedDate');
              DModule.QR.SQL.Add('FROM Collections a, Particulars b, TaxPayer c where a.P_Num=b.P_Num and a.TP_ID=c.TP_ID and a.ISSUEDDATE BETWEEN :a and :b ');
                DModule.QR.Params.AsString[0]:= endingdate;
                DModule.QR.Params.AsString[1]:= lastday;
              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('No records for this month',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;

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

        DModule.TR.StartTransaction;
        DModule.QR.SQL.Clear;
        DModule.QR.SQL.Add('SELECT a.ColID,b.Description,c.Name,a.AmountPaid,a.Tracking_ORNo,a.IssuedDate');
        DModule.QR.SQL.Add('FROM Collections a, Particulars b, TaxPayer c where a.P_Num=b.P_Num and a.TP_ID=c.TP_ID and b.DESCRIPTION=:a AND a.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 TfrmReport.LoaddpDateSelect_weekly();
var newstate:TListItem;
begin
daysCountDiff:=6;
ansDaysdiff:=IntToStr(DaysBetween(dtpTo.DateTime,dtpFrom.DateTime));

      if (daysCountDiff < StrToInt(ansDaysdiff)) then begin
          MessageDlg('Number of days counts greater that 7 days of a week', mtError,[mbOK],0);
          btnPrint.Enabled:=False;
      end
      else if (daysCountDiff > StrToInt(ansDaysdiff)) then begin
          MessageDlg('Number of days counts lesser that 7 days of a week', mtError,[mbOK],0);
          btnPrint.Enabled:=False;
      end

      else if (cmbParticulars.Text='') then begin
       MessageDlg('Please select particulars',mtError,[mbOK],0);
       cmbParticulars.SetFocus;
       btnPrint.Enabled:=False;
      end
    else
            btnPrint.Enabled:=True;
        if DModule.TR.InTransaction then
        DModule.TR.Commit;

    DModule.TR.StartTransaction;
    DModule.QR.SQL.Clear;
    DModule.QR.SQL.Add('SELECT a.ColID,b.Description,c.Name,a.AmountPaid,a.Tracking_ORNo,a.IssuedDate');
    DModule.QR.SQL.Add('FROM Collections a, Particulars b, TaxPayer c where a.P_Num=b.P_Num and a.TP_ID=c.TP_ID and b.DESCRIPTION=:a AND a.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 TfrmReport.LoaddpDateSelect_monthly();
  var newstate:TListItem;
      //month:string;
      //day:integer;
      //startingdate:string;
     // endingdate:string;
      //numberOfMonth:integer;
        //lastDayOfmonth:TDateTime;
        //lastday:string;
begin
      day:=1;

      if(StrToInt(txtYear.Text) > StrToInt(nowyear))then begin
                MessageDlg('Invalid! Cannot set year to future time',mtError,[mbOK],0);
                txtYear.SetFocus;
                btnPrint.Enabled:=False;
      end
      else if(cmbMonth.Text='')then begin
         MessageDlg('Please select month',mtError,[mbOK],0);
         cmbMonth.SetFocus;
         btnPrint.Enabled:=False;
      end
      else if(txtYear.Text='')then begin
         MessageDlg('Please input year',mtError,[mbOK],0);
         cmbMonth.SetFocus;
         btnPrint.Enabled:=False;
      end
      else if (cmbParticulars.Text='') then begin
       MessageDlg('Please select particulars',mtError,[mbOK],0);
       cmbParticulars.SetFocus;
       btnPrint.Enabled:=False;
      end

      else begin
        btnPrint.Enabled:=True;
             if (cmbMonth.Text='January')then begin
               numberOfMonth:=1;
            end
            else if (cmbMonth.Text='February')then begin
               numberOfMonth:=2;
            end
            else if (cmbMonth.Text='March')then begin
               numberOfMonth:=3;
            end
            else if (cmbMonth.Text='April')then begin
               numberOfMonth:=4;
            end
            else if (cmbMonth.Text='May')then begin
               numberOfMonth:=5;
            end
            else if (cmbMonth.Text='June')then begin
               numberOfMonth:=6;
            end
            else if (cmbMonth.Text='July')then begin
               numberOfMonth:=7;
            end
            else if (cmbMonth.Text='August')then begin
               numberOfMonth:=8;
            end
            else if (cmbMonth.Text='September')then begin
               numberOfMonth:=9;
            end
            else if (cmbMonth.Text='October')then begin
               numberOfMonth:=10;
            end
            else if (cmbMonth.Text='November')then begin
               numberOfMonth:=11;
            end
            else begin
               numberOfMonth:=12;
            end;

            startingdate:=inttostr(numberOfMonth)+'/'+ inttostr(day)+'/'+txtyear.Text;
            month:=  FormatDateTime('mmmm',strtodatetime(startingdate));
            endingdate:=inttostr(numberOfMonth)+'/'+ inttostr(day)+'/'+txtyear.Text;
            lastDayOfmonth := EncodeDate(YearOf(StrToDateTime(endingdate)), MonthOf(StrToDateTime(endingdate)), MonthDays[IsLeapYear(YearOf(StrToDateTime(endingdate))), MonthOf(StrToDateTime(endingdate))]);
            lastday:=datetostr(lastDayOfmonth);

            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 AND PartName =:c');
                DModule.QR.Params.AsString[0]:= endingdate;
                DModule.QR.Params.AsString[1]:= lastday;
                DModule.QR.Params.AsString[2]:=cmbParticulars.Text;
              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('No records for this month',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;


procedure TfrmReport.ReportDaily_SelectAll();
begin
        LoaddpDateSelectAll_daily();
          DModule.rep.Filename := 'reports/DailyReportSelectAll.rep';
          DModule.rep.Report.Params.Items[0].Value :=DateToStr(dtpFrom.DateTime);
          DModule.rep.Report.Params.Items[1].Value :=frmMain.fullname;
          //DModule.rep.Report.Params.Items[2].Value := frmMain.StatusBar1.Panels.Items[1].Text;
          DModule.rep.Preview := True;
          DModule.rep.Execute();
end;
procedure TfrmReport.ReportDaily_SelectChoice();
begin
MaxID();
        LoaddpDateSelect_daily();
          DModule.rep.Filename := 'reports/DailyReportSelectChoice.rep';
          DModule.rep.Report.Params.Items[0].Value :=DateToStr(dtpFrom.DateTime);
          DModule.rep.Report.Params.Items[1].Value := cmbParticulars.Text;
          DModule.rep.Report.Params.Items[2].Value :=frmMain.fullname;
          //DModule.rep.Report.Params.Items[2].Value := frmMain.StatusBar1.Panels.Items[1].Text;
          DModule.rep.Preview := True;
          DModule.rep.Execute();
end;

procedure TfrmReport.ReportWeekly_SelectAll();
begin
        LoaddpDateSelectAll_weekly();
          DModule.rep.Filename := 'reports/WeeklyReportSelectAll.rep';
          DModule.rep.Report.Params.Items[0].Value :=DateToStr(dtpFrom.DateTime);
          DModule.rep.Report.Params.Items[1].Value :=DateToStr(dtpTo.DateTime);
          DModule.rep.Report.Params.Items[2].Value :=frmMain.fullname;
          //DModule.rep.Report.Params.Items[2].Value := frmMain.StatusBar1.Panels.Items[1].Text;
          DModule.rep.Preview := True;
          DModule.rep.Execute();
end;
procedure TfrmReport.ReportWeekly_SelectChoice();
begin
        LoaddpDateSelect_weekly();
          DModule.rep.Filename := 'reports/WeeklyReportSelectChoice.rep';
          DModule.rep.Report.Params.Items[0].Value :=DateToStr(dtpFrom.DateTime);
          DModule.rep.Report.Params.Items[1].Value :=DateToStr(dtpTo.DateTime);
          DModule.rep.Report.Params.Items[2].Value :=cmbParticulars.Text;
          DModule.rep.Report.Params.Items[3].Value :=frmMain.fullname;
          DModule.rep.Preview := True;
          DModule.rep.Execute();
end;

procedure TfrmReport.ReportMonthly_SelectAll(startingdate:string;lastday:string;fullname:string);
begin
        LoaddpDateSelectAll_monthly();
          DModule.rep.Filename := 'reports/MonthlyReportSelectAll.rep';
          DModule.rep.Report.Params.Items[0].Value :=startingdate;
          DModule.rep.Report.Params.Items[1].Value :=lastday;
          DModule.rep.Report.Params.Items[2].Value :=fullname;
          DModule.rep.Preview := True;
          DModule.rep.Execute();

end;
procedure TfrmReport.ReportMonthly_SelectChoice(startingdate:string;lastday:string;combobox:string);
begin
        LoaddpDateSelect_monthly();
          DModule.rep.Filename := 'reports/MonthlyReportSelectChoice.rep';
          DModule.rep.Report.Params.Items[0].Value :=startingdate;
          DModule.rep.Report.Params.Items[1].Value :=lastday;
          DModule.rep.Report.Params.Items[2].Value :=combobox;
          DModule.rep.Report.Params.Items[3].Value := frmMain.StatusBar1.Panels.Items[1].Text;
          DModule.rep.Preview := True;
          DModule.rep.Execute();
end;

procedure TfrmReport.MaxID();
begin

 if DModule.TR.InTransaction then
        DModule.TR.Commit;
    DModule.QR.SQL.Clear();
    DModule.QR.SQL.Add('SELECT *');
    DModule.QR.SQL.Add('FROM COLLECTIONS');
    DModule.QR.Prepare;
    DModule.QR.Open();
      ID := DModule.QR.Fields.AsInteger[0];
      date :=  DModule.QR.Fields.AsString[3];
   if DModule.QR.Fields.Eof then
         if DModule.TR.InTransaction then
       DModule.TR.Commit;
end;

procedure TfrmReport.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 TfrmReport.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 a.ColID,b.PartName,c.Name,a.AmountPaid,a.Tracking_ORNo,a.IssuedDate');
        DModule.QR.SQL.Add('FROM Collections a, Particulars b, TaxPayer c where a.P_Num=b.P_Num and a.TP_ID=c.TP_ID and 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;

procedure TfrmReport.btnViewAllClick(Sender: TObject);
begin
      LoadReport();
end;

procedure TfrmReport.dtpFromChange(Sender: TObject);
begin
      {if((rbDaily.Checked=True) AND (rbSelectAll.Checked=True))then begin
         LoaddpDateSelectAll_daily();
      end
      else if((rbDaily.Checked=True) AND (rbSelectChoice.Checked=True))then begin
         LoaddpDateSelect_daily();
      end  }
end;


procedure TfrmReport.FormActivate(Sender: TObject);
begin
    nowyear:= FormatDateTime('yyyy',Now);
    txtYear.Text:=nowyear;
    //StatusBar1.Panels.Items[0].Text:='Columns: '+IntToStr(count);
end;

procedure TfrmReport.Timer1Timer(Sender: TObject);
begin
      //StatusBar1.Panels.Items[2].Text:='Time: '+ TimeToStr(Now);;
end;

end.
