source: trunk/finance/bills.php@ 297

Last change on this file since 297 was 297, checked in by george, 15 years ago
  • Opraveno: Generování časů vystavení a splatnosti u nových dokladů.
File size: 10.5 KB
Line 
1<?php
2
3class Bill extends Module
4{
5 var $SpecificSymbol = 1; // počítačová sít
6 var $FileFolder = 'doklady';
7 var $FileNamePrefix = 'doklad-';
8
9 function GenerateBill($BillId)
10 {
11 global $SpecificSymbol;
12
13 $DbResult = $this->Database->select('FinanceBills', '*', 'Id='.$BillId);
14 $Bill = $DbResult->fetch_assoc();
15
16 $DbResult = $this->Database->select('Subject', '*', 'Id='.$Bill['Subject']);
17 $Subject = $DbResult->fetch_assoc();
18
19 $Dodavka = array();
20 $DbResult = $this->Database->select('FinanceBillsItems', '*', 'Bill='.$BillId);
21 while($Item = $DbResult->fetch_assoc())
22 {
23 $Dodavka[$Item['Id']] = $Item;
24 }
25
26 $PaymentType = array('převodem', 'hotově');
27
28 switch($Bill['Type'])
29 {
30 case 'invoice':
31 $Output = '<table width="100%"><tr><td colspan="2">'.
32 '<font size="6"><div align="center">Faktura - daňový doklad</font></div>'.
33 '<hr></td></tr>'.
34 '<tr><td valign="top" width="50%"><strong>Dodavatel:</strong><br>'.
35 'Ing. Jiří Hajda<br>'.
36 'Zděchov 208<br>'.
37 '75607 Zděchov<br>'.
38 'IČ: 75904535<br>'.
39 'DIČ: CZ8303255884<br>'.
40 'Účet: 218098370 / 0300<br>'.
41 'Neplátce DPH<br>'.
42 '</td><td valign="top">'.
43 '<strong>Odběratel:</strong><br>'.
44 $Subject['Name'].'<br>'.
45 $Subject['AddressStreet'].'<br>'.
46 $Subject['AddressPSC'].' '.$Subject['AddressTown'].'<br>';
47 if($Subject['IC'] != 0) $Output .= 'IČ: '.$Subject['IC'].'<br>';
48 if($Subject['DIC'] != '') $Output .= 'DIČ: '.$Subject['DIC'].'<br>';
49 $Output .=
50 '</td></tr>'.
51 '<tr><td colspan="2"><hr></td></tr>'.
52 '<tr><td width="50%" valign="top">'.
53 '<strong>Platební podmínky:</strong><br>'.
54 'Číslo dokladu: '.$Bill['BillCode'].'<br>'.
55 'Variabilní symbol: '.$Subject['Id'].'<br>'.
56 'Specifický symbol: '.$this->SpecificSymbol.'<br>'.
57 'Konstantní symbol:<br>'.
58 'Způsob úhrady: '.$PaymentType[$Bill['Cash']].
59 '</td><td valign="top">'.
60 '<br>'.
61 'Datum vystavení: '.HumanDate($Bill['TimeCreate']).'<br>'.
62 'Datum zdanitel. plnění: '.HumanDate($Bill['TimeCreate']).'<br>'.
63 'Datum splatnosti: '.HumanDate($Bill['TimeDue']).'<br>';
64 $Output .= '</td></tr>'.
65 '<tr><td colspan="2">'.
66 '<hr>'.
67 '<br>'.
68 '<table border="0" width="100%">'.
69 '<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>'.
70 '<tr><td colspan="4"><hr></td></tr>';
71
72 $Total = 0;
73 foreach($Dodavka as $Polozka)
74 {
75 $Output .= '<tr><td>'.$Polozka['Description'].'</td><td align="right">'.$Polozka['Quantity'].'</td><td align="right">'.$Polozka['Price'].'&nbsp;Kč</td><td align="right">'.($Polozka['Quantity'] * $Polozka['Price']).'&nbsp;Kč</td></tr>';
76 $Total += ($Polozka['Quantity'] * $Polozka['Price']);
77 }
78 $Output .= '<tr><th colspan="3" align="left">Celkem</th><th align="right">'.$Total.'&nbsp;Kč</th></tr>';
79 $Output .= '</table>'.
80 '</td></tr>'.
81 '<tr><td colspan="2"><hr></td></tr>'.
82 '</table>';
83 break;
84 case 'income':
85 $Output = '<table width="100%" border="1" cellspacing="0" cellpadding="3"><tr><td width="50%">'.
86 '<strong>Firma:</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</td><td width="50%" valign="top">'.
94 '<font size="5"><strong>PŘÍJMOVÝ POKLADNÍ DOKLAD</strong></font><br><br>'.
95 'Číslo dokladu: '.$Bill['BillCode'].'<br>'.
96 'Datum vystavení: '.HumanDate($Bill['TimeCreate']).'<br>'.
97 '</td></tr>'.
98 '<tr><td colspan="2"><strong>Přijato od:</strong><br>'.
99 $Subject['Name'].'<br>'.
100 $Subject['AddressStreet'].'<br>'.
101 $Subject['AddressPSC'].' '.$Subject['AddressTown'].'<br>';
102 if($Subject['IC'] != 0) $Output .= 'IČ: '.$Subject['IC'].'<br>';
103 if($Subject['DIC'] != '') $Output .= 'DIČ: '.$Subject['DIC'].'<br>';
104 $Total = 0;
105 $Description = '';
106 foreach($Dodavka as $Polozka)
107 {
108 $Description .= $Polozka['Description'].'<br>';
109 $Total += ($Polozka['Quantity'] * $Polozka['Price']);
110 }
111 $Output .= '</td></tr>'.
112 '<tr><td colspan="2"><strong>Částka:</strong> '.$Total.' Kč<br><br>'.
113 '</td></tr>'.
114 '<tr><td colspan="2"><strong>Účel platby:</strong><br>'.$Description.'</td></tr>'.
115 '<tr><td><br><br>Podpis příjemce:</td><td><br><br>Podpis pokladníka:</td></tr>';
116 $Output .= '</table>';
117 break;
118 }
119 return($Output);
120 }
121
122 function CreateBill($SubjectId, $Items, $TimeCreate, $TimeDue, $BillCode = '', $Type = 'invoice')
123 {
124 $this->Database->insert('FinanceBills', array('TimeCreate' => TimeToMysqlDateTime($TimeCreate), 'Subject' => $SubjectId, 'TimeDue' => TimeToMysqlDateTime($TimeDue), 'BillCode' => $BillCode, 'Type' => $Type));
125 $BillId = $this->Database->insert_id;
126 foreach($Items as $Item)
127 {
128 $this->Database->insert('FinanceBillsItems', array('Bill' => $BillId, 'Description' => $Item['Description'], 'Price' => $Item['Price'], 'Quantity' => $Item['Quantity']));
129 }
130 //Header('Content-Type: application/pdf');
131 $PdfData = $this->HtmlToPdf($this->GenerateBill($BillId));
132 //echo($PdfData);
133 file_put_contents($this->FileFolder.'/'.$this->FileNamePrefix.$BillId.'.pdf', $PdfData);
134 //$this->Database->query('UPDATE finance_bills SET pdf = 0x'.bin2hex($PdfData).' WHERE id='.$BillId);
135 return($BillId);
136 }
137
138 function CreateIncomeBill($SubjectId, $Description, $Price, $BillCode)
139 {
140 $this->Database->insert('FinanceBills', array('TimeCreate' => TimeToMysqlDateTime(time()), 'Subject' => $SubjectId, 'BillCode' => $BillCode, 'Type' => 'income'));
141 $BillId = $this->Database->insert_id;
142 $this->Database->insert('FinanceBillsItems', array('Bill' => $BillId, 'Description' => $Description, 'Price' => $Price, 'Quantity' => 1));
143 //Header('Content-Type: application/pdf');
144 $PdfData = $this->HtmlToPdf($this->GenerateBill($BillId));
145 //echo($PdfData);
146 file_put_contents($this->FileFolder.'/'.$this->FileNamePrefix.$BillId.'.pdf', $PdfData);
147 //$this->Database->query('UPDATE finance_bills SET pdf = 0x'.bin2hex($PdfData).' WHERE id='.$BillId);
148 return($BillId);
149 }
150
151 function RegeneratePDF($BillId)
152 {
153 global $Database;
154
155 $PdfData = $this->HtmlToPdf($this->GenerateBill($BillId));
156 //echo($this->FileFolder.'/'.$this->FileNamePrefix.$BillId.'.pdf');
157 echo(file_put_contents($this->FileFolder.'/'.$this->FileNamePrefix.$BillId.'.pdf', $PdfData));
158 //$this->Database->query('UPDATE finance_bills SET pdf = 0x'.bin2hex($PdfData).' WHERE id='.$BillId);
159 }
160
161 function ShowStoredBill($BillId)
162 {
163 global $Database;
164
165 //$DbResult = $this->Database->select('finance_bills', 'pdf', 'id='.$BillId);
166 //if($DbResult->num_rows == 1)
167 //{
168 // $DbRow = $DbResult->fetch_array();
169 $FileName = $this->FileFolder.'/'.$this->FileNamePrefix.$BillId.'.pdf';
170 if(file_exists($FileName))
171 {
172 Header('Content-Type: application/pdf');
173 Header('Content-Disposition: attachment; filename="'.$this->FileNamePrefix.$BillId.'.pdf"');
174 echo(file_get_contents($FileName));
175 } else echo('Faktura nenalezena');
176 }
177
178 function ShowGeneratedBill($BillId)
179 {
180 global $Database;
181
182 Header('Content-Type: application/pdf');
183 Header('Content-Disposition: attachment; filename="'.$this->FileNamePrefix.$BillId.'.pdf"');
184 echo($this->HtmlToPdf($this->GenerateBill($BillId)));
185 }
186
187 function HasPDFFile($BillId)
188 {
189 return(file_exists($this->FileFolder.'/'.$this->FileNamePrefix.$BillId.'.pdf'));
190 }
191
192 function HtmlToPdf($HtmlCode)
193 {
194 $Output = shell_exec('echo "'.addslashes(FromUTF8($HtmlCode)).'"|htmldoc --no-numbered --webpage --charset 8859-2 -t pdf -');
195 return($Output);
196 }
197
198 function CustomGenerate()
199 {
200 global $Database;
201
202 $DbResult = $this->Database->select('finance_operations', '*', '(date="2008-02-01" AND comment="Poplatek za měsíc Únor") OR '.
203 '(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)');
204 while($Row = $DbResult->fetch_array())
205 {
206 echo($Row['id']."<br>\n");
207 $Time = MysqlDateToTime($Row['date']);
208 $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);
209 $this->Database->update('finance_operations', 'id='.$Row['id'], array('bill_id' => $BillId));
210 }
211 }
212
213 function ShowPage()
214 {
215 global $Database;
216 if(array_key_exists('subject', $_GET))
217 {
218
219 $DbResult = $this->Database->select('FinanceBills', '*', 'Subject='.$_GET['subject']);
220 while($Item = $DbResult->fetch_array())
221 {
222 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>');
223 }
224 } else
225 if(array_key_exists('billpdf', $_GET))
226 {
227 $this->ShowStoredBill($_GET['billpdf']);
228 } else
229 if(array_key_exists('billpdf2', $_GET))
230 {
231 $this->ShowGeneratedBill($_GET['billpdf2']);
232 } else
233 if(array_key_exists('regenerate', $_GET))
234 {
235 $this->RegeneratePDF($_GET['regenerate']);
236 } else
237 if(array_key_exists('bill', $_GET))
238 {
239 echo($this->GenerateBill($_GET['bill']));
240 } else
241 if(array_key_exists('generate', $_GET))
242 {
243 $this->CreateBill(1, array(array('Description' => 'Poplatek za připojení k síti', 'Price' => 1000, 'Quantity' => 1)), time(), time());
244 } else
245 {
246 ShowHeader('Doklady', 'Doklady');
247 echo('Faktury:<br />');
248 $DbResult = $this->Database->select('Subject', '*', '1 ORDER BY Name');
249 while($Subject = $DbResult->fetch_assoc())
250 {
251 echo('<a href="?user='.$Subject['Id'].'">'.$Subject['Name'].'</a><br />');
252 }
253 ShowFooter();
254 }
255 }
256}
257
258?>
Note: See TracBrowser for help on using the repository browser.