1 | unit UBill;
|
---|
2 |
|
---|
3 | {$mode objfpc}{$H+}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Classes, SysUtils, UCore, USqlDatabase, UDatabase, UUser, dateutils;
|
---|
9 |
|
---|
10 | const
|
---|
11 | SpecificSymbol: Integer = 1;
|
---|
12 | FileFolder: string = 'doklady';
|
---|
13 | FileNamePrefix: string = 'doklad-';
|
---|
14 |
|
---|
15 | type
|
---|
16 |
|
---|
17 | { TBillItem }
|
---|
18 |
|
---|
19 | TBillItem = class
|
---|
20 | Id: Integer;
|
---|
21 | Description: string;
|
---|
22 | Quantity: Integer;
|
---|
23 | Price: Double;
|
---|
24 | procedure Load;
|
---|
25 | procedure Store;
|
---|
26 | end;
|
---|
27 |
|
---|
28 | TBillItemList = class
|
---|
29 | end;
|
---|
30 |
|
---|
31 | { TBill }
|
---|
32 |
|
---|
33 | TBill = class
|
---|
34 | private
|
---|
35 | function CreateBill: Integer;
|
---|
36 | public
|
---|
37 | Id: Integer;
|
---|
38 | Items: TBillItemList;
|
---|
39 | User: TUser;
|
---|
40 | UserId: Integer;
|
---|
41 | BillCode: string;
|
---|
42 | TimeCreate: TDateTime;
|
---|
43 | TimeTo: TDateTime;
|
---|
44 | TimeFrom: TDateTime;
|
---|
45 | TimeDue: TDateTime;
|
---|
46 | function GenerateBill: string;
|
---|
47 | procedure Load;
|
---|
48 | procedure Store;
|
---|
49 | end;
|
---|
50 |
|
---|
51 | implementation
|
---|
52 |
|
---|
53 | { TBill }
|
---|
54 |
|
---|
55 | function TBill.GenerateBill: string;
|
---|
56 | var
|
---|
57 | DbRows: TDbRows;
|
---|
58 | BillItem: TBillItem;
|
---|
59 | Total: Real;
|
---|
60 | I: Integer;
|
---|
61 | T: TUser;
|
---|
62 | begin
|
---|
63 | (*
|
---|
64 | T.FirstName:= 'ss';
|
---|
65 |
|
---|
66 | DbRows := Database.Select('finance_bills', '*', 'id=' + IntToStr(Id));
|
---|
67 | // LoadFromDbRecord(DbRows[0]);
|
---|
68 | DbRows.Free;
|
---|
69 |
|
---|
70 | DbRows := Database.Select('users', '*', 'id=' + IntToStr(UserId));
|
---|
71 | User := TUser.Create;
|
---|
72 | User.LoadFromDbRecord(DbRows[0]);
|
---|
73 | DbRows.Free;
|
---|
74 |
|
---|
75 | DbRows := Database.Select('finance_bills_items', '*', 'bill_id=' + IntToStr(Id));
|
---|
76 | for I := 0 to DbRows.Count - 1 do begin
|
---|
77 | BillItem := TBillItem.Create;
|
---|
78 | // BillItem.LoadFromDbRecord(DbRows[I]);
|
---|
79 | // Items.Add(BillItem);
|
---|
80 | end;
|
---|
81 |
|
---|
82 |
|
---|
83 | Result := '<table width="100%"><tr><td colspan="2">' +
|
---|
84 | '<font size="6"><div align="center">Faktura - daňový doklad</font></div>' +
|
---|
85 | '<hr></td></tr>' +
|
---|
86 | '<tr><td valign="top" width="50%"><strong>Dodavatel:</strong><br>' +
|
---|
87 | 'Ing. Jiří Hajda<br>' +
|
---|
88 | 'Zděchov 208<br>' +
|
---|
89 | '75607 Zděchov<br>' +
|
---|
90 | 'IČ: 75904535<br>' +
|
---|
91 | 'DIČ: CZ8303255884<br>' +
|
---|
92 | 'Účet: 218098370 / 0300<br>' +
|
---|
93 | 'Neplátce DPH<br>' +
|
---|
94 | '</td><td valign="top">' +
|
---|
95 | '<strong>Odběratel:</strong><br>' +
|
---|
96 | User.SubjectName + '<br>' +
|
---|
97 | User.Street + '<br>' +
|
---|
98 | User.PSC + ' ' + User.Town + '<br>';
|
---|
99 | if User.IC <> 0 then Result := Result + 'IČ: ' + IntToStr(User.IC) + '<br>';
|
---|
100 | if User.DIC <> '' then Result := Result + 'DIČ: ' + User.DIC + '<br>';
|
---|
101 | Result := Result + '</td></tr>' +
|
---|
102 | '<tr><td colspan="2"><hr></td></tr>' +
|
---|
103 | '<tr><td width="50%" valign="top">' +
|
---|
104 | '<strong>Platební podmínky:</strong><br>' +
|
---|
105 | 'Číslo dokladu: ' + BillCode + '<br>' +
|
---|
106 | 'Variabilní symbol: ' + IntToStr(User.Id) + '<br>' +
|
---|
107 | 'Specifický symbol: ' + IntToStr(SpecificSymbol) + '<br>' +
|
---|
108 | 'Konstantní symbol:<br>' +
|
---|
109 | 'Způsob úhrady: převodem' +
|
---|
110 | '</td><td valign="top">' +
|
---|
111 | '<br>' +
|
---|
112 | 'Datum vystavení: ' + DateToStr(TimeCreate) + '<br>' +
|
---|
113 | 'Datum zdanitel. plnění: ' + DateToStr(TimeCreate) + '<br>' +
|
---|
114 | 'Datum splatnosti: ' + DateToStr(TimeDue) + '<br>';
|
---|
115 | if(TimeFrom <> TDateTime(0)) and (TimeTo <> TDateTime(0)) then
|
---|
116 | Result := Result + 'Fakturované období: ' + DateToStr(TimeFrom) + ' - ' + DateToStr(TimeTo) + '<br>';
|
---|
117 | Result := Result + '</td></tr>' +
|
---|
118 | '<tr><td colspan="2">' +
|
---|
119 | '<hr>' +
|
---|
120 | '<br>' +
|
---|
121 | '<table border="0" width="100%">' +
|
---|
122 | '<tr><th align="left">Dodávka</th><th align="right">Množství</th><th align="right">Cena/MJ</th><th align="right">Celkem</th></tr>' +
|
---|
123 | '<tr><td colspan="4"><hr></td></tr>';
|
---|
124 | Total := 0;
|
---|
125 | for I := 0 to Items.Count - 1 do
|
---|
126 | with TBillItem(Items[I]) do
|
---|
127 | begin
|
---|
128 | Result := Result + '<tr><td>' + Description + '</td><td align="right">' + IntToStr(Quantity) + '</td><td align="right">' + FloatToStr(Price) +
|
---|
129 | ' Kč</td><td align="right">' + FloatToStr(Quantity * Price) + ' Kč</td></tr>';
|
---|
130 | Total := Total + Quantity * Price;
|
---|
131 | end;
|
---|
132 | Result := Result + '<tr><th colspan="3" align="left">Celkem</th><th align="right">' + FloatToStr(Total) + ' Kč</th></tr>';
|
---|
133 | Result := Result + '</table>' +
|
---|
134 | '</td></tr>' +
|
---|
135 | '<tr><td colspan="2"><hr></td></tr>' +
|
---|
136 | '</table>';
|
---|
137 | *)
|
---|
138 | end;
|
---|
139 |
|
---|
140 | procedure TBill.Load;
|
---|
141 | begin
|
---|
142 | (*
|
---|
143 | Id := StrToInt(DbRow.Values['id']);
|
---|
144 | BillCode := DbRow.Values['BillCode'];
|
---|
145 | TimeDue := UnixToDateTime(StrToInt(DbRow.Values['time_due']));
|
---|
146 | TimeFrom := UnixToDateTime(StrToInt(DbRow.Values['time_from']));
|
---|
147 | TimeTo := UnixToDateTime(StrToInt(DbRow.Values['time_to']));
|
---|
148 | TimeCreate := UnixToDateTime(StrToInt(DbRow.Values['time_create']));
|
---|
149 | UserId := StrToInt(DbRow.Values['user_id']);
|
---|
150 | *)
|
---|
151 | end;
|
---|
152 |
|
---|
153 | procedure TBill.Store;
|
---|
154 | var
|
---|
155 | Data: TAssociativeArray;
|
---|
156 | begin
|
---|
157 | Data := TAssociativeArray.Create;
|
---|
158 | Data.AddKeyValue('BillCode', BillCode);
|
---|
159 | Data.AddKeyValue('time_due', IntToStr(DateTimeToUnix(TimeDue)));
|
---|
160 | Data.AddKeyValue('time_from', IntToStr(DateTimeToUnix(TimeFrom)));
|
---|
161 | Data.AddKeyValue('time_to', IntToStr(DateTimeToUnix(TimeTo)));
|
---|
162 | Data.AddKeyValue('time_create', IntToStr(DateTimeToUnix(TimeCreate)));
|
---|
163 | Data.AddKeyValue('user_id', BillCode);
|
---|
164 | Database.Update('finance_bills', Data, 'id=' + IntToStr(Id));
|
---|
165 | Data.Free;
|
---|
166 | end;
|
---|
167 |
|
---|
168 | function TBill.CreateBill: Integer;
|
---|
169 | begin
|
---|
170 |
|
---|
171 | // Database.Insert('finance_bills', Itemsarray('time_create' => TimeToMysqlDateTime(time()), 'user_id' => $UserId, 'time_from' => TimeToMysqlDateTime($TimeFrom), 'time_to' => TimeToMysqlDateTime($TimeTo), 'time_due' => TimeToMysqlDateTime($TimeFrom + 15*24*3600), 'BillCode' => $BillCode));
|
---|
172 | (*
|
---|
173 | $BillId = $Database->insert_id;
|
---|
174 | foreach($Items as $Item)
|
---|
175 | {
|
---|
176 | $Database->insert('finance_bills_items', array('bill_id' => $BillId, 'description' => $Item['description'], 'price' => $Item['price'], 'quantity' => $Item['quantity']));
|
---|
177 | }
|
---|
178 | //Header('Content-Type: application/pdf');
|
---|
179 | $PdfData = $this->HtmlToPdf($this->GenerateBill($BillId));
|
---|
180 | //echo($PdfData);
|
---|
181 | file_put_contents($this->FileFolder.'/'.$this->FileNamePrefix.$BillId.'.pdf', $PdfData);
|
---|
182 | //$Database->query('UPDATE finance_bills SET pdf = 0x'.bin2hex($PdfData).' WHERE id='.$BillId);
|
---|
183 | return($BillId);
|
---|
184 | *)
|
---|
185 | end;
|
---|
186 | (*
|
---|
187 | function RegeneratePDF($BillId)
|
---|
188 | {
|
---|
189 | global $Database;
|
---|
190 |
|
---|
191 | $PdfData = $this->HtmlToPdf($this->GenerateBill($BillId));
|
---|
192 | //echo($this->FileFolder.'/'.$this->FileNamePrefix.$BillId.'.pdf');
|
---|
193 | echo(file_put_contents($this->FileFolder.'/'.$this->FileNamePrefix.$BillId.'.pdf', $PdfData));
|
---|
194 | //$Database->query('UPDATE finance_bills SET pdf = 0x'.bin2hex($PdfData).' WHERE id='.$BillId);
|
---|
195 | }
|
---|
196 |
|
---|
197 | function ShowStoredBill($BillId)
|
---|
198 | {
|
---|
199 | global $Database;
|
---|
200 |
|
---|
201 | //$DbResult = $Database->select('finance_bills', 'pdf', 'id='.$BillId);
|
---|
202 | //if($DbResult->num_rows == 1)
|
---|
203 | //{
|
---|
204 | // $DbRow = $DbResult->fetch_array();
|
---|
205 | $FileName = $this->FileFolder.'/'.$this->FileNamePrefix.$BillId.'.pdf';
|
---|
206 | if(file_exists($FileName))
|
---|
207 | {
|
---|
208 | Header('Content-Type: application/pdf');
|
---|
209 | Header('Content-Disposition: attachment; filename="'.$this->FileNamePrefix.$BillId.'.pdf"');
|
---|
210 | echo(file_get_contents($FileName));
|
---|
211 | } else echo('Faktura nenalezena');
|
---|
212 | }
|
---|
213 |
|
---|
214 | function ShowGeneratedBill($BillId)
|
---|
215 | {
|
---|
216 | global $Database;
|
---|
217 |
|
---|
218 | Header('Content-Type: application/pdf');
|
---|
219 | Header('Content-Disposition: attachment; filename="'.$this->FileNamePrefix.$BillId.'.pdf"');
|
---|
220 | echo($this->HtmlToPdf($this->GenerateBill($BillId)));
|
---|
221 | }
|
---|
222 |
|
---|
223 | function HasPDFFile($BillId)
|
---|
224 | {
|
---|
225 | return(file_exists($this->FileFolder.'/'.$this->FileNamePrefix.$BillId.'.pdf'));
|
---|
226 | }
|
---|
227 |
|
---|
228 | function HtmlToPdf($HtmlCode)
|
---|
229 | {
|
---|
230 | $Output = shell_exec('echo "'.addslashes(from_utf8($HtmlCode)).'"|htmldoc --no-numbered --webpage --charset 8859-2 -t pdf -');
|
---|
231 | return($Output);
|
---|
232 | }
|
---|
233 |
|
---|
234 | function CustomGenerate()
|
---|
235 | {
|
---|
236 | global $Database;
|
---|
237 |
|
---|
238 | $DbResult = $Database->select('finance_operations', '*', '(date="2008-02-01" AND comment="Poplatek za měsíc Únor") OR '.
|
---|
239 | '(date="2008-01-01" AND comment="Poplatek za měsíc Leden") OR (date="2007-12-01" AND comment="Poplatek za měsíc Prosinec") AND (bill_id = 0)');
|
---|
240 | while($Row = $DbResult->fetch_array())
|
---|
241 | {
|
---|
242 | echo($Row['id']."<br>\n");
|
---|
243 | $Time = MysqlDateToTime($Row['date']);
|
---|
244 | $BillId = $this->CreateBill($Row['user'], array(array('description' => 'Poplatek za připojení k síti', 'price' => (-$Row['money']), 'quantity' => 1)), $Time, $Time + (date("t", time()) - 1) * 24 * 3600);
|
---|
245 | $Database->update('finance_operations', 'id='.$Row['id'], array('bill_id' => $BillId));
|
---|
246 | }
|
---|
247 | }
|
---|
248 |
|
---|
249 | function ShowPage()
|
---|
250 | {
|
---|
251 | global $Database;
|
---|
252 | if(array_key_exists('user', $_GET))
|
---|
253 | {
|
---|
254 |
|
---|
255 | $DbResult = $Database->select('finance_bills', '*', 'user_id='.$_GET['user']);
|
---|
256 | while($Item = $DbResult->fetch_array())
|
---|
257 | {
|
---|
258 | echo('<a href="?bill='.$Item['id'].'">faktura '.$Item['id'].'</a> <a href="?billpdf='.$Item['id'].'">Uložené PDF</a> <a href="?billpdf2='.$Item['id'].'">Generované PDF</a> <a href="?regenerate='.$Item['id'].'">Přegenerovat</a><br>');
|
---|
259 | }
|
---|
260 | } else
|
---|
261 | if(array_key_exists('billpdf', $_GET))
|
---|
262 | {
|
---|
263 | $this->ShowStoredBill($_GET['billpdf']);
|
---|
264 | } else
|
---|
265 | if(array_key_exists('billpdf2', $_GET))
|
---|
266 | {
|
---|
267 | $this->ShowGeneratedBill($_GET['billpdf2']);
|
---|
268 | } else
|
---|
269 | if(array_key_exists('regenerate', $_GET))
|
---|
270 | {
|
---|
271 | $this->RegeneratePDF($_GET['regenerate']);
|
---|
272 | } else
|
---|
273 | if(array_key_exists('bill', $_GET))
|
---|
274 | {
|
---|
275 | echo($this->GenerateBill($_GET['bill']));
|
---|
276 | } else
|
---|
277 | if(array_key_exists('generate', $_GET))
|
---|
278 | {
|
---|
279 | $this->CreateBill(1, array(array('description' => 'Poplatek za připojení k síti', 'price' => 1000, 'quantity' => 1)), time(), time());
|
---|
280 | } else
|
---|
281 | {
|
---|
282 | ShowHeader('Faktury', 'Faktury');
|
---|
283 | echo('Faktury:<br>');
|
---|
284 | $DbResult = $Database->select('users', '*, CONCAT(second_name," ", first_name) as fullname', '1 ORDER BY fullname');
|
---|
285 | while($User = $DbResult->fetch_array())
|
---|
286 | {
|
---|
287 | echo('<a href="?user='.$User['id'].'">'.$User['fullname'].'</a><br>');
|
---|
288 | }
|
---|
289 | ShowFooter();
|
---|
290 | }
|
---|
291 | }
|
---|
292 | }
|
---|
293 |
|
---|
294 | $InvoiceGenerator = new InvoiceGenerator;
|
---|
295 |
|
---|
296 | ?>
|
---|
297 | *)
|
---|
298 |
|
---|
299 | { TBillItem }
|
---|
300 |
|
---|
301 | procedure TBillItem.Load;
|
---|
302 | var
|
---|
303 | DbRows: TDbRows;
|
---|
304 | begin
|
---|
305 | DbRows := Database.Select(ClassName, '*', 'Id=' + IntToStr(Id));
|
---|
306 | Quantity := StrToInt(DbRows[0].Values['quantity']);
|
---|
307 | Description := DbRows[0].Values['description'];
|
---|
308 | Price := StrToFloat(DbRows[0].Values['price']);
|
---|
309 | DbRows.Free;
|
---|
310 | end;
|
---|
311 |
|
---|
312 | procedure TBillItem.Store;
|
---|
313 | var
|
---|
314 | Data: TAssociativeArray;
|
---|
315 | begin
|
---|
316 | Data := TAssociativeArray.Create;
|
---|
317 | Data.AddKeyValue('Quantitiy', IntToStr(Quantity));
|
---|
318 | Data.AddKeyValue('Description', Description);
|
---|
319 | Data.AddKeyValue('Price', FloatToStr(Price));
|
---|
320 | Database.Insert(ClassName, Data);
|
---|
321 | end;
|
---|
322 |
|
---|
323 | end.
|
---|
324 |
|
---|