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