-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclsExport.pas
227 lines (212 loc) · 6.65 KB
/
clsExport.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
unit clsExport;
interface
uses
Vcl.DBGrids, Vcl.Forms, Vcl.Grids, System.SysUtils, Vcl.Dialogs,
System.Classes,clsLogging;
type
TExport = class(TObject)
private
{ private declarations }
fGrid: TDBGrid;
fStringGrid: TStringGrid;
fFileName: string;
csvExport: TextFile;
fSave: TSaveDialog;
Self2: TComponent;
protected
{ protected declarations }
public
{ public declarations }
constructor Create(sGrid: TDBGrid; sFileName: string);
procedure ExportToCSV;
procedure ExportToHTML(sFrom: string);
published
{ published declarations }
end;
implementation
{ TExport }
constructor TExport.Create(sGrid: TDBGrid; sFileName: string);
var
I: Integer;
begin
fGrid := sGrid;
fFileName := sFileName;
fStringGrid := TStringGrid.Create(Self2);
fStringGrid.RowCount := fGrid.DataSource.DataSet.RecordCount + 1;
fStringGrid.ColCount := fGrid.FieldCount;
for I := 0 to fGrid.FieldCount - 1 do
begin
fStringGrid.Cells[I, 0] := fGrid.Fields[I].DisplayName;
end;
fGrid.DataSource.DataSet.First;
while not fGrid.DataSource.DataSet.Eof do
begin
for I := 0 to fGrid.FieldCount - 1 do
begin
fStringGrid.Cells[I, fGrid.DataSource.DataSet.RecNo] :=
fGrid.DataSource.DataSet.Fields[I].AsString;
end;
fGrid.DataSource.DataSet.Next;
end;
end;
procedure TExport.ExportToCSV;
var
I: Integer;
objLog: TLog;
begin
fSave := TSaveDialog.Create(Self2);
fSave.Title := 'Choose where to save exported file';
fSave.InitialDir := GetHomePath;
fSave.Filter := 'CSV File|*.csv';
fSave.DefaultExt := 'csv';
fSave.FilterIndex := 1;
if fSave.Execute then
begin
fFileName := fSave.FileName;
AssignFile(csvExport, fFileName);
Rewrite(csvExport);
for I := 0 to fStringGrid.RowCount - 1 do
begin
Writeln(csvExport, fStringGrid.Rows[I].CommaText);
end;
CloseFile(csvExport);
end
else
begin
MessageDlg('Exporting Cancelled', mtInformation, [mbOK], 0);
end;
fSave.Free;
fStringGrid.Free;
objLog:= TLog.Create;
objLog.WriteLog('INFO','Exporting CSV File : Successful');
objLog.Free;
end;
procedure TExport.ExportToHTML(sFrom: string);
var
HTMLFile: TextFile;
I, iTemp: Integer;
rSum: Real;
K: Integer;
objLog : TLog;
begin
AssignFile(HTMLFile, GetEnvironmentVariable('appdata') + '/POS 2.0/HTML/' +
sFrom + '.html');
Rewrite(HTMLFile);
Writeln(HTMLFile,
'<!DOCTYPE html><html><head><meta name="viewport" content="width=device-width, initial-scale=1">'
+ '<meta charset="utf-8">' +
' <title>POS 2.0 HTML REPORTS</title>'+
' <script src="https://printjs-4de6.kxcdn.com/print.min.js"></script>' +
'<link rel="stylesheet" type="text/css" href="https://printjs-4de6.kxcdn.com/print.min.css">'
+ ' <link rel="stylesheet" media="all" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.7.1/css/bulma.min.css">'
+ '<script defer src="https://use.fontawesome.com/releases/v5.0.7/js/all.js"></script>'
+ '</head>');
Writeln(HTMLFile,
'<body><section class="section"><div id="print" class="container"><center><h1 class="title">POS 2.0 '
+ sFrom + ' Report</h1>' + '<p class="subtitle">Last Generated On ' +
DateTimeToStr(Now) + '</p></center>');
Writeln(HTMLFile,
'<section class="section"><div class="container"><center><table class="table is-bordered is-striped is-narrow is-hoverable is-fullwidth">');
Writeln(HTMLFile, '<thead>');
Writeln(HTMLFile, '<tr class="is-selected">');
for I := 0 to fStringGrid.ColCount - 1 do
begin
Writeln(HTMLFile, '<th>' + fStringGrid.Cells[I, 0] + '</th>');
end;
Writeln(HTMLFile, '</tr>');
Writeln(HTMLFile, '</thead>');
Writeln(HTMLFile, '<tbody>');
for I := 1 to fStringGrid.RowCount - 1 do
begin
Writeln(HTMLFile, '<tr>');
for K := 0 to fStringGrid.ColCount - 1 do
begin
Writeln(HTMLFile, '<th>' + fStringGrid.Cells[K, I] + '</th>');
end;
Writeln(HTMLFile, '</tr>');
end;
if sFrom = 'Transaction' then
begin
rSum := 0;
Writeln(HTMLFile, '<tr>');
Writeln(HTMLFile, '<th>Total :</th>');
Writeln(HTMLFile, '<th> </th>');
for I := 1 to fStringGrid.RowCount - 1 do
begin
rSum := StrToFloat(fStringGrid.Cells[2, I]) + rSum;
end;
Writeln(HTMLFile, '<th>' + floattostrf(rSum, ffCurrency, 100, 2) + '</th>');
rSum := 0;
for I := 1 to fStringGrid.RowCount - 1 do
begin
rSum := StrToFloat(fStringGrid.Cells[3, I]) + rSum;
end;
Writeln(HTMLFile, '<th>' + floattostrf(rSum, ffCurrency, 100, 2) + '</th>');
rSum := 0;
for I := 1 to fStringGrid.RowCount - 1 do
begin
rSum := StrToFloat(fStringGrid.Cells[4, I]) + rSum;
end;
Writeln(HTMLFile, '<th>' + floattostrf(rSum, ffFixed, 100, 0) + '</th>');
Writeln(HTMLFile, '<th> </th>');
Writeln(HTMLFile, '<th> </th>');
Writeln(HTMLFile, '</tr>');
end;
if sFrom = 'Product' then
begin
rSum := 0;
Writeln(HTMLFile, '<tr>');
Writeln(HTMLFile, '<th>Total :</th>');
Writeln(HTMLFile, '<th> </th>');
for I := 1 to fStringGrid.RowCount - 1 do
begin
rSum := StrToFloat(fStringGrid.Cells[2, I]) + rSum;
end;
Writeln(HTMLFile, '<th>' + floattostrf(rSum, ffCurrency, 100, 2) + '</th>');
rSum := 0;
for I := 1 to fStringGrid.RowCount - 1 do
begin
rSum := StrToFloat(fStringGrid.Cells[3, I]) + rSum;
end;
Writeln(HTMLFile, '<th>' + floattostrf(rSum, ffCurrency, 100, 2) + '</th>');
Writeln(HTMLFile, '</tr>');
end;
if sFrom = 'Stock' then
begin
rSum := 0;
Writeln(HTMLFile, '<tr>');
Writeln(HTMLFile, '<th>Total :</th>');
Writeln(HTMLFile, '<th> </th>');
for I := 1 to fStringGrid.RowCount - 1 do
begin
rSum := StrToFloat(fStringGrid.Cells[2, I]) + rSum;
end;
Writeln(HTMLFile, '<th>' + floattostrf(rSum, ffFixed, 100, 0) + '</th>');
Writeln(HTMLFile, '</tr>');
end;
if sFrom = 'GiftCard' then
begin
rSum := 0;
Writeln(HTMLFile, '<tr>');
Writeln(HTMLFile, '<th>Total :</th>');
Writeln(HTMLFile, '<th> </th>');
Writeln(HTMLFile, '<th> </th>');
for I := 1 to fStringGrid.RowCount - 1 do
begin
rSum := StrToFloat(fStringGrid.Cells[3, I]) + rSum;
end;
Writeln(HTMLFile, '<th>' + floattostrf(rSum, ffFixed, 100, 0) + '</th>');
Writeln(HTMLFile, '</tr>');
end;
Writeln(HTMLFile, '</tbody>');
Writeln(HTMLFile, '</table>');
Writeln(HTMLFile, '<a onclick="printJS(' + #96 + 'print' + #96 + ',' + #96 +
'html' + #96 +
')" class="button is-primary is-large is-rounded">Print Report</a>');
Writeln(HTMLFile, '</center></div></section></body></html>');
CloseFile(HTMLFile);
objLog:= TLog.Create;
objLog.WriteLog('INFO','Exporting HTML Report : Success');
objLog.Free;
end;
end.