517159f09fb7e48e91c49cbc51f97085f2991446
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 Lizenzinfos in eigenes Modu...

Bernd Wurst authored 10 years ago

5) Written 2008-2014 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 Weitere Prepared-Statement-...

Bernd Wurst authored 10 years ago

26)   $result = db_query("SELECT id,datum,betrag,bezahlt,abbuchung,sepamandat FROM kundendaten.ausgestellte_rechnungen WHERE kunde=? ORDER BY id DESC", array($c));
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;
Bernd Wurst Weitere Prepared-Statement-...

Bernd Wurst authored 10 years ago

38)   $result = db_query("SELECT pdfdata FROM kundendaten.ausgestellte_rechnungen WHERE kunde=:c AND id=:id",array(":c" => $c, ":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;
Bernd Wurst Weitere Prepared-Statement-...

Bernd Wurst authored 10 years ago

50)   $result = db_query("SELECT kunde,datum,betrag,bezahlt,abbuchung FROM kundendaten.ausgestellte_rechnungen WHERE kunde=:c AND id=:id",array(":c" => $c, ":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 Weitere Prepared-Statement-...

Bernd Wurst authored 10 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",array(":c" => $c, ":id" => $id));
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 Weitere Prepared-Statement-...

Bernd Wurst authored 10 years ago

73)   $result = db_query("SELECT anzahl, beschreibung, startdatum, enddatum, betrag, einheit, brutto, mwst FROM kundendaten.upcoming_items WHERE kunde=? ORDER BY startdatum ASC", array($c));
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 Rechnungen zu rückgeb...

Bernd Wurst authored 9 years ago

168)   $result = db_query("SELECT rechnungsnummer, rechnungsdatum, sl.betrag, buchungsdatum, sl.status FROM kundendaten.sepalastschrift sl LEFT JOIN kundendaten.ausgestellte_rechnungen re ON (re.sepamandat=sl.mandatsreferenz) WHERE rechnungsnummer=? AND re.abbuchung=1", array($rechnungsnummer));
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 Zeige Rechnungen zu rückgeb...

Bernd Wurst authored 9 years ago

178)   $result = db_query("SELECT rechnungsnummer, rechnungsdatum, betrag, buchungsdatum, status FROM kundendaten.sepalastschrift WHERE mandatsreferenz=? ORDER BY buchungsdatum DESC", array($mandatsreferenz));
Bernd Wurst Vorgemerkte Lastschriften a...

Bernd Wurst authored 10 years ago

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

Bernd Wurst authored 10 years ago

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

Bernd Wurst authored 10 years ago

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

Bernd Wurst authored 10 years ago

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

Bernd Wurst authored 10 years ago

186) function get_sepamandate() 
187) {
188)   $cid = (int) $_SESSION['customerinfo']['customerno'];
Bernd Wurst Weitere Prepared-Statement-...

Bernd Wurst authored 10 years ago

189)   $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=?", array($cid));
Bernd Wurst Erste Version des SEPA-Mand...

Bernd Wurst authored 10 years ago

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

Bernd Wurst authored 10 years ago

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

Bernd Wurst authored 10 years ago

192)     array_push($ret, $entry);
193)   }
194)   return $ret;
195) }
196) 
197) 
198) function yesterday($date) 
199) {
Bernd Wurst * Weitere Module auf prepar...

Bernd Wurst authored 10 years ago

200)   $result = db_query("SELECT ? - INTERVAL 1 DAY", array($date));
Bernd Wurst Umstellung auf PDO-Datenban...

Bernd Wurst authored 10 years ago

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

Bernd Wurst authored 10 years ago

202) }
203) 
204) 
205) function invalidate_sepamandat($id, $date) 
206) {
Bernd Wurst Weitere Prepared-Statement-...

Bernd Wurst authored 10 years ago

207)   $args = array(":cid" => (int) $_SESSION['customerinfo']['customerno'],
208)                 ":id" => (int) $id,
209)                 ":date" => $date);
210)   db_query("UPDATE kundendaten.sepamandat SET gueltig_bis=:date WHERE id=:id AND kunde=:cid", $args);
Bernd Wurst Erste Version des SEPA-Mand...

Bernd Wurst authored 10 years ago

211) }
212) 
213) 
214) function sepamandat($name, $adresse, $iban, $bankname, $bic, $gueltig_ab)
215) {
216)   $cid = (int) $_SESSION['customerinfo']['customerno'];
Bernd Wurst Vorgemerkte Lastschriften a...

Bernd Wurst authored 10 years ago

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

Bernd Wurst authored 10 years ago

218)   $first_date = date('Y-m-d');
219)   $invoices = my_invoices();
220)   foreach ($invoices as $i) {
221)     if ($i['bezahlt'] == 0 && $i['datum'] < $first_date) {
222)       $first_date = $i['datum'];
223)     }
224)   }
225)   if ($gueltig_ab < date('Y-m-d') && $gueltig_ab != $first_date) {
Bernd Wurst Erste Version des SEPA-Mand...

Bernd Wurst authored 10 years ago

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

Bernd Wurst authored 10 years ago

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

Bernd Wurst authored 10 years ago

248)   $today = date('Y-m-d');
Bernd Wurst Weitere Prepared-Statement-...

Bernd Wurst authored 10 years ago

249)   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)",
250)           array(":referenz" => $referenz, ":glaeubiger_id" => $glaeubiger_id, ":cid" => $cid, 
251)                 ":today" => $today, ":gueltig_ab" => $gueltig_ab, ":name" => $name, ":adresse" => $adresse, 
252)                 ":iban" => $iban, ":bic" => $bic, ":bankname" => $bankname));
Bernd Wurst Erste Version des SEPA-Mand...

Bernd Wurst authored 10 years ago

253) }
254) 
255) 
Bernd Wurst QR-Codes für Überweisung be...

Bernd Wurst authored 10 years ago

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

Bernd Wurst authored 10 years ago

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

Bernd Wurst authored 10 years ago

285) function find_iban($blz, $kto) 
286) {
287)   $iban = sprintf('DE00%08s%010s', $blz, $kto);
288)   $iban = iban_set_checksum($iban);
289)   return $iban;
290) }
291) 
292)