unit WeeklyReport;

interface

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

type
  TfrmWeeklyReport = class(TForm)
    Panel1: TPanel;
    GroupBox1: TGroupBox;
    lvweeklyRep: TListView;
    dpDate: TDateTimePicker;
    Label1: TLabel;
    Label2: TLabel;
    cmbParticular: TComboBox;
    btnPrint: TBitBtn;

    procedure LoadReport();
    procedure Report();
    procedure MaxID();
    procedure btnPrintClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure LoaddpDateSelect();
    procedure cmbPartSelect();
    procedure LoadParticularCmB();
    procedure cmbParticularChange(Sender: TObject);
    procedure GroupBox1Click(Sender: TObject);
    procedure dpDateChange(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    id:integer;
    name:string;
    date:string;
  end;

var
  frmWeeklyReport: TfrmWeeklyReport;

implementation

uses DM;

{$R *.dfm}

procedure TfrmWeeklyReport.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();
    lvweeklyRep.Items.BeginUpdate;
    lvweeklyRep.Items.Clear;
    if DModule.QR.Fields.IsNull[0] then Exit;
    while not DModule.QR.Eof do begin
        newState := lvweeklyRep.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;
    lvweeklyRep.Items.EndUpdate;
end;

procedure TfrmWeeklyReport.Report();
//var primary:integer;
begin
MaxID();
      DModule.rep.Filename := 'reports/SumColRemit_Others.rep';
      DModule.rep.Report.Params.Items[0].Value :=id;
      DModule.rep.Report.Params.Items[1].Value :=name;
      DModule.rep.Report.Params.Items[2].Value :=date;
      DModule.rep.Preview := True;
      DModule.rep.Execute();
end;

procedure TfrmWeeklyReport.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 TfrmWeeklyReport.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();

    cmbParticular.Items.BeginUpdate;
    //cmbPart.Items.Clear;
    if DModule.QR.Fields.IsNull[0] then Exit;
    while not DModule.QR.Eof do begin
        cmbParticular.Items.Add(DModule.QR.Fields.AsString[0]);

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


procedure TfrmWeeklyReport.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]:= cmbParticular.Text;
    DModule.QR.Prepare;

    DModule.QR.Open();
    lvweeklyRep.Items.BeginUpdate;
    lvweeklyRep.Items.Clear;
    if DModule.QR.Fields.IsNull[0] then Exit;
    while not DModule.QR.Eof do begin
        newState := lvweeklyRep.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;
    lvweeklyRep.Items.EndUpdate;
end;

procedure TfrmWeeklyReport.LoaddpDateSelect();
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(dpDate.DateTime);
    DModule.QR.Prepare;

    DModule.QR.Open();
    lvweeklyRep.Items.BeginUpdate;
    lvweeklyRep.Items.Clear;
    if DModule.QR.Fields.IsNull[0] then Exit;
    while not DModule.QR.Eof do begin
        newState := lvweeklyRep.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;
    lvweeklyRep.Items.EndUpdate;
end;

procedure TfrmWeeklyReport.btnPrintClick(Sender: TObject);
begin
      Report();
end;

procedure TfrmWeeklyReport.FormShow(Sender: TObject);
begin
      dpDate.DateTime:=Now;
      LoadReport();

end;

procedure TfrmWeeklyReport.cmbParticularChange(Sender: TObject);
begin
      LoadParticularCmB();
end;

procedure TfrmWeeklyReport.GroupBox1Click(Sender: TObject);
begin
    LoaddpDateSelect();
end;

procedure TfrmWeeklyReport.dpDateChange(Sender: TObject);
begin
      LoaddpDateSelect();
end;

end.
