f1f231f5e074dfa038e70a67d39849e80f2b4b4d
bernd Kunden können Ihre Rechnung...

bernd authored 16 years ago

1) <?php
Bernd Wurst Added license tags for CC0,...

Bernd Wurst authored 12 years ago

2) /*
3) This file belongs to the Webinterface of schokokeks.org Hosting
4) 
Bernd Wurst Updated copyright notice (2...

Bernd Wurst authored 11 years ago

5) Written 2008-2013 by schokokeks.org Hosting, namely
Bernd Wurst Added license tags for CC0,...

Bernd Wurst authored 12 years ago

6)   Bernd Wurst <bernd@schokokeks.org>
7)   Hanno Böck <hanno@schokokeks.org>
8) 
9) To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. This software is distributed without any warranty.
10) 
11) You should have received a copy of the CC0 Public Domain Dedication along with this software. If not, see 
12) http://creativecommons.org/publicdomain/zero/1.0/
13) 
14) Nevertheless, in case you use a significant part of this code, we ask (but not require, see the license) that you keep the authors' names in place and return your changes to the public. We would be especially happy if you tell us what you're going to do with this code.
15) */
bernd Kunden können Ihre Rechnung...

bernd authored 16 years ago

16) 
17) require_once('inc/base.php');
18) require_once('inc/security.php');
19) 
Bernd Wurst Erste Version des SEPA-Mand...

Bernd Wurst authored 10 years ago

20) include("external/php-iban/php-iban.php");
21) 
22) 
bernd Kunden können Ihre Rechnung...

bernd authored 16 years ago

23) function my_invoices()
24) {
25)   $c = (int) $_SESSION['customerinfo']['customerno'];
Bernd Wurst Kleine Korrekturen Ausführu...

Bernd Wurst authored 10 years ago

26)   $result = db_query("SELECT id,datum,betrag,bezahlt,abbuchung,sepamandat FROM kundendaten.ausgestellte_rechnungen WHERE kunde={$c} ORDER BY id DESC");
bernd Kunden können Ihre Rechnung...

bernd authored 16 years ago

27)   $ret = array();
Bernd Wurst Umstellung auf PDO-Datenban...

Bernd Wurst authored 10 years ago

28)   while($line = $result->fetch())
bernd Kunden können Ihre Rechnung...

bernd authored 16 years ago

29)   	array_push($ret, $line);
30)   return $ret;
31) }
32) 
33) 
34) function get_pdf($id)
35) {
36)   $c = (int) $_SESSION['customerinfo']['customerno'];
37)   $id = (int) $id;
38)   $result = db_query("SELECT pdfdata FROM kundendaten.ausgestellte_rechnungen WHERE kunde={$c} AND id={$id}");
Bernd Wurst Umstellung auf PDO-Datenban...

Bernd Wurst authored 10 years ago

39)   if ($result->rowCount() == 0)
Bernd Wurst Erste Version des SEPA-Mand...

Bernd Wurst authored 10 years ago

40) 	  system_failure('Ungültige Rechnungsnummer oder nicht eingeloggt');
Bernd Wurst Umstellung auf PDO-Datenban...

Bernd Wurst authored 10 years ago

41)   return $result->fetch(PDO::FETCH_OBJ)->pdfdata;
bernd Kunden können Ihre Rechnung...

bernd authored 16 years ago

42) 
43) }
44) 
45) 
46) function invoice_details($id)
47) {
48)   $c = (int) $_SESSION['customerinfo']['customerno'];
49)   $id = (int) $id;
50)   $result = db_query("SELECT kunde,datum,betrag,bezahlt,abbuchung FROM kundendaten.ausgestellte_rechnungen WHERE kunde={$c} AND id={$id}");
Bernd Wurst Umstellung auf PDO-Datenban...

Bernd Wurst authored 10 years ago

51)   if ($result->rowCount() == 0)
Bernd Wurst QR-Codes für Überweisung be...

Bernd Wurst authored 10 years ago

52)   	system_failure('Ungültige Rechnungsnummer oder nicht eingeloggt');
Bernd Wurst Umstellung auf PDO-Datenban...

Bernd Wurst authored 10 years ago

53)   return $result->fetch();
bernd Kunden können Ihre Rechnung...

bernd authored 16 years ago

54) }
55) 
56) function invoice_items($id)
57) {
58)   $c = (int) $_SESSION['customerinfo']['customerno'];
59)   $id = (int) $id;
Bernd Wurst Einheit bei Rechnungsposten...

Bernd Wurst authored 11 years ago

60)   $result = db_query("SELECT id, beschreibung, datum, enddatum, betrag, einheit, brutto, mwst, anzahl FROM kundendaten.rechnungsposten WHERE rechnungsnummer={$id} AND kunde={$c}");
Bernd Wurst Umstellung auf PDO-Datenban...

Bernd Wurst authored 10 years ago

61)   if ($result->rowCount() == 0)
Bernd Wurst QR-Codes für Überweisung be...

Bernd Wurst authored 10 years ago

62)   	system_failure('Ungültige Rechnungsnummer oder nicht eingeloggt');
bernd Kunden können Ihre Rechnung...

bernd authored 16 years ago

63)   $ret = array();
Bernd Wurst Umstellung auf PDO-Datenban...

Bernd Wurst authored 10 years ago

64)   while($line = $result->fetch())
bernd Kunden können Ihre Rechnung...

bernd authored 16 years ago

65)   array_push($ret, $line);
66)   return $ret;
67) }
68) 
69) 
70) function upcoming_items()
71) {
72)   $c = (int) $_SESSION['customerinfo']['customerno'];
Bernd Wurst Einheit bei Rechnungsposten...

Bernd Wurst authored 11 years ago

73)   $result = db_query("SELECT anzahl, beschreibung, startdatum, enddatum, betrag, einheit, brutto, mwst FROM kundendaten.upcoming_items WHERE kunde={$c} ORDER BY startdatum ASC");
bernd Kunden können Ihre Rechnung...

bernd authored 16 years ago

74)   $ret = array();
Bernd Wurst Umstellung auf PDO-Datenban...

Bernd Wurst authored 10 years ago

75)   while($line = $result->fetch())
bernd Kunden können Ihre Rechnung...

bernd authored 16 years ago

76) 	  array_push($ret, $line);
77)   return $ret;
78) }
79) 
80) 
Bernd Wurst QR-Codes für Überweisung be...

Bernd Wurst authored 10 years ago

81) function generate_qrcode_image($id) 
82) {
83)   $invoice = invoice_details($id);
84)   $customerno = $invoice['kunde'];
85)   $amount = 'EUR'.sprintf('%.2f', $invoice['betrag']);
86)   $datum = $invoice['datum'];
87)   $data = 'BCD
88) 001
89) 1
90) SCT
91) GENODES1VBK
92) schokokeks.org GbR
93) DE91602911200041512006
94) '.$amount.'
95) 
96) 
Bernd Wurst Beschreibung entfernt, mach...

Bernd Wurst authored 10 years ago

97) RE '.$id.' KD '.$customerno.' vom '.$datum;
Bernd Wurst QR-Codes für Überweisung be...

Bernd Wurst authored 10 years ago

98)   
99)   $descriptorspec = array(
100)     0 => array("pipe", "r"),  // STDIN ist eine Pipe, von der das Child liest
101)     1 => array("pipe", "w"),  // STDOUT ist eine Pipe, in die das Child schreibt
102)     2 => array("pipe", "w") 
103)   );
104) 
105)   $process = proc_open('qrencode -t PNG -o - -l M', $descriptorspec, $pipes);
106) 
107)   if (is_resource($process)) {
108)     // $pipes sieht nun so aus:
109)     // 0 => Schreibhandle, das auf das Child STDIN verbunden ist
110)     // 1 => Lesehandle, das auf das Child STDOUT verbunden ist
111) 
112)     fwrite($pipes[0], $data);
113)     fclose($pipes[0]);
114) 
115)     $pngdata = stream_get_contents($pipes[1]);
116)     fclose($pipes[1]);
117) 
118)     // Es ist wichtig, dass Sie alle Pipes schließen bevor Sie
119)     // proc_close aufrufen, um Deadlocks zu vermeiden
120)     $return_value = proc_close($process);
121)   
122)     return $pngdata;
123)   } else {
124)     warning('Es ist ein interner Fehler im Webinterface aufgetreten, aufgrund dessen kein QR-Code erstellt werden kann. Sollte dieser Fehler mehrfach auftreten, kontaktieren Sie bitte die Administratoren.');
125)   }
126) }
127) 
128) 
129) function generate_bezahlcode_image($id) 
130) {
131)   $invoice = invoice_details($id);
132)   $customerno = $invoice['kunde'];
133)   $amount = str_replace('.', '%2C', sprintf('%.2f', $invoice['betrag']));
134)   $datum = $invoice['datum'];
135)   $data = 'bank://singlepaymentsepa?name=schokokeks.org%20GbR&reason=RE%20'.$id.'%20KD%20'.$customerno.'%20vom%20'.$datum.'&iban=DE91602911200041512006&bic=GENODES1VBK&amount='.$amount;
136)   
137)   $descriptorspec = array(
138)     0 => array("pipe", "r"),  // STDIN ist eine Pipe, von der das Child liest
139)     1 => array("pipe", "w"),  // STDOUT ist eine Pipe, in die das Child schreibt
140)     2 => array("pipe", "w") 
141)   );
142) 
143)   $process = proc_open('qrencode -t PNG -o -', $descriptorspec, $pipes);
144) 
145)   if (is_resource($process)) {
146)     // $pipes sieht nun so aus:
147)     // 0 => Schreibhandle, das auf das Child STDIN verbunden ist
148)     // 1 => Lesehandle, das auf das Child STDOUT verbunden ist
149) 
150)     fwrite($pipes[0], $data);
151)     fclose($pipes[0]);
152) 
153)     $pngdata = stream_get_contents($pipes[1]);
154)     fclose($pipes[1]);
155) 
156)     // Es ist wichtig, dass Sie alle Pipes schließen bevor Sie
157)     // proc_close aufrufen, um Deadlocks zu vermeiden
158)     $return_value = proc_close($process);
159)   
160)     return $pngdata;
161)   } else {
162)     warning('Es ist ein interner Fehler im Webinterface aufgetreten, aufgrund dessen kein QR-Code erstellt werden kann. Sollte dieser Fehler mehrfach auftreten, kontaktieren Sie bitte die Administratoren.');
163)   }
164) }
165) 
Bernd Wurst Vorgemerkte Lastschriften a...

Bernd Wurst authored 10 years ago

166) function get_lastschrift($rechnungsnummer) {
167)   $rechnungsnummer = (int) $rechnungsnummer;
Bernd Wurst Zeige fehlgeschlagene Abbuc...

Bernd Wurst authored 10 years ago

168)   $result = db_query("SELECT rechnungsnummer, rechnungsdatum, sl.betrag, buchungsdatum FROM kundendaten.sepalastschrift sl LEFT JOIN kundendaten.ausgestellte_rechnungen re ON (re.id=sl.rechnungsnummer) WHERE rechnungsnummer='${rechnungsnummer}' AND re.abbuchung=1");
Bernd Wurst Umstellung auf PDO-Datenban...

Bernd Wurst authored 10 years ago

169)   if ($result->rowCount() == 0) {
Bernd Wurst Vorgemerkte Lastschriften a...

Bernd Wurst authored 10 years ago

170)     return NULL;
171)   }
Bernd Wurst Umstellung auf PDO-Datenban...

Bernd Wurst authored 10 years ago

172)   $item = $result->fetch();
Bernd Wurst Vorgemerkte Lastschriften a...

Bernd Wurst authored 10 years ago

173)   return $item;
174) }
175) 
176) function get_lastschriften($mandatsreferenz)
177) {
Bernd Wurst Umstellung auf PDO-Datenban...

Bernd Wurst authored 10 years ago

178)   $mandatsreferenz = db_escape_string($mandatsreferenz);
Bernd Wurst Vorgemerkte Lastschriften a...

Bernd Wurst authored 10 years ago

179)   $result = db_query("SELECT rechnungsnummer, rechnungsdatum, betrag, buchungsdatum FROM kundendaten.sepalastschrift WHERE mandatsreferenz='${mandatsreferenz}' ORDER BY buchungsdatum DESC");
180)   $ret = array();
Bernd Wurst Umstellung auf PDO-Datenban...

Bernd Wurst authored 10 years ago

181)   while ($item = $result->fetch()) {
Bernd Wurst Vorgemerkte Lastschriften a...

Bernd Wurst authored 10 years ago

182)     $ret[] = $item;
183)   }
184)   return $ret;
185) }
Bernd Wurst QR-Codes für Überweisung be...

Bernd Wurst authored 10 years ago

186) 
Bernd Wurst Erste Version des SEPA-Mand...

Bernd Wurst authored 10 years ago

187) function get_sepamandate() 
188) {
189)   $cid = (int) $_SESSION['customerinfo']['customerno'];
Bernd Wurst Speichere Gläubiger-ID mit...

Bernd Wurst authored 10 years ago

190)   $result = db_query("SELECT id, mandatsreferenz, glaeubiger_id, erteilt, medium, gueltig_ab, gueltig_bis, erstlastschrift, kontoinhaber, adresse, iban, bic, bankname FROM kundendaten.sepamandat WHERE kunde={$cid}");
Bernd Wurst Erste Version des SEPA-Mand...

Bernd Wurst authored 10 years ago

191)   $ret = array();
Bernd Wurst Umstellung auf PDO-Datenban...

Bernd Wurst authored 10 years ago

192)   while ($entry = $result->fetch()) {
Bernd Wurst Erste Version des SEPA-Mand...

Bernd Wurst authored 10 years ago

193)     array_push($ret, $entry);
194)   }
195)   return $ret;
196) }
197) 
198) 
199) function yesterday($date) 
200) {
Bernd Wurst Umstellung auf PDO-Datenban...

Bernd Wurst authored 10 years ago

201)   $date = db_escape_string($date);
Bernd Wurst Erste Version des SEPA-Mand...

Bernd Wurst authored 10 years ago

202)   $result = db_query("SELECT '{$date}' - INTERVAL 1 DAY");
Bernd Wurst Umstellung auf PDO-Datenban...

Bernd Wurst authored 10 years ago

203)   return $result->fetch()[0];
Bernd Wurst Erste Version des SEPA-Mand...

Bernd Wurst authored 10 years ago

204) }
205) 
206) 
207) function invalidate_sepamandat($id, $date) 
208) {
209)   $cid = (int) $_SESSION['customerinfo']['customerno'];
210)   $id = (int) $id;
Bernd Wurst Umstellung auf PDO-Datenban...

Bernd Wurst authored 10 years ago

211)   $date = db_escape_string($date);
Bernd Wurst Erste Version des SEPA-Mand...

Bernd Wurst authored 10 years ago

212)   db_query("UPDATE kundendaten.sepamandat SET gueltig_bis='{$date}' WHERE id={$id} AND kunde={$cid}");
213) }
214) 
215) 
216) function sepamandat($name, $adresse, $iban, $bankname, $bic, $gueltig_ab)
217) {
218)   $cid = (int) $_SESSION['customerinfo']['customerno'];
Bernd Wurst Umstellung auf PDO-Datenban...

Bernd Wurst authored 10 years ago

219)   $name = db_escape_string($name);
220)   $adresse = db_escape_string($adresse);
221)   $iban = db_escape_string($iban);
222)   $bankname = db_escape_string($bankname);
223)   $bic = db_escape_string($bic);
224)   $gueltig_ab = db_escape_string($gueltig_ab);
Bernd Wurst Vorgemerkte Lastschriften a...

Bernd Wurst authored 10 years ago

225) 
Bernd Wurst Speichere Gläubiger-ID mit...

Bernd Wurst authored 10 years ago

226)   $first_date = date('Y-m-d');
227)   $invoices = my_invoices();
228)   foreach ($invoices as $i) {
229)     if ($i['bezahlt'] == 0 && $i['datum'] < $first_date) {
230)       $first_date = $i['datum'];
231)     }
232)   }
233)   if ($gueltig_ab < date('Y-m-d') && $gueltig_ab != $first_date) {
Bernd Wurst Erste Version des SEPA-Mand...

Bernd Wurst authored 10 years ago

234)     system_failure('Das Mandat kann nicht rückwirkend erteilt werden. Bitte geben Sie ein Datum in der Zukunft an.');
235)   }
236)   $alte_mandate = get_sepamandate();
237)   $referenzen = array();
238)   foreach ($alte_mandate as $mandat) {
239)     if ($mandat['gueltig_bis'] == NULL || $mandat['gueltig_bis'] >= $gueltig_ab) {
240)       DEBUG('Altes Mandat wird für ungültig erklärt.');
241)       DEBUG($mandat);
242)       invalidate_sepamandat($mandat['id'], yesterday($gueltig_ab));
243)     }
244)     array_push($referenzen, $mandat['mandatsreferenz']);
245)   }
246)   $counter = 1;
247)   $referenz = sprintf('K%04d-M%03d', $cid, $counter);
248)   while (in_array($referenz, $referenzen)) {
249)     $counter++;
250)     $referenz = sprintf('K%04d-M%03d', $cid, $counter);
251)   }
252)   DEBUG('Nächste freie Mandatsreferenz: '. $referenz);
253) 
Bernd Wurst Speichere Gläubiger-ID mit...

Bernd Wurst authored 10 years ago

254)   $glaeubiger_id = config('glaeubiger_id');
255) 
Bernd Wurst Erste Version des SEPA-Mand...

Bernd Wurst authored 10 years ago

256)   $today = date('Y-m-d');
Bernd Wurst Speichere Gläubiger-ID mit...

Bernd Wurst authored 10 years ago

257)   db_query("INSERT INTO kundendaten.sepamandat (mandatsreferenz, glaeubiger_id, kunde, erteilt, medium, gueltig_ab, kontoinhaber, adresse, iban, bic, bankname) VALUES ('{$referenz}', '${glaeubiger_id}', {$cid}, '{$today}', 'online', '{$gueltig_ab}', '{$name}', '{$adresse}', '{$iban}', '{$bic}', '{$bankname}')");
Bernd Wurst Erste Version des SEPA-Mand...

Bernd Wurst authored 10 years ago

258) }
259) 
260) 
Bernd Wurst QR-Codes für Überweisung be...

Bernd Wurst authored 10 years ago

261) 
Bernd Wurst Vervollständige Bankname un...

Bernd Wurst authored 10 years ago

262) function get_bank_info($iban) 
263) {
264)   if (strlen($iban) != 22 || substr($iban, 0, 2) != 'DE') {
265)     // Geht nur bei deutschen IBANs
266)     echo 'Fehler!';
267)     echo '$iban = '.$iban;
268)     echo 'strlen($iban): '.strlen($iban);
269)     echo 'substr($iban, 0, 2): '.substr($iban, 0, 2);
270)     return NULL;
271)   }
272)   $blz = substr($iban, 4, 8);
273)   // FIXME: Liste der BLZs muss vorhanden sein!
274)   $bankinfofile = dirname(__FILE__).'/bankinfo.txt';
275)   $f = file($bankinfofile);
276)   $match = '';
277)   foreach ($f as $line) {
278)     if (substr($line, 0, 9) == $blz.'1') {
279)       $match = $line;
280)       break;
281)     }
282)   }
283)   $bank = array();
284)   $bank['name'] = iconv('latin1', 'utf8', chop(substr($match, 9,58)));
285)   $bank['bic'] = chop(substr($match, 139,11));
286)   return $bank;
287) }
288) 
289) 
Bernd Wurst Ermögliche das Berechnen de...

Bernd Wurst authored 10 years ago

290) function find_iban($blz, $kto) 
291) {
292)   $iban = sprintf('DE00%08s%010s', $blz, $kto);
293)   $iban = iban_set_checksum($iban);
294)   return $iban;
295) }
296) 
297)