Periodically reload page
[infodrom.org/service.infodrom.org] / class / sales.class.php
1 <?php
2
3 class Sales extends DatabaseTable {
4
5   public function __construct($id=false)
6   {
7     $this->idcolumn = 'nr';
8     parent::__construct('sales', $id);
9   }
10
11   public function getOpenItems($only_in=null)
12   {
13       if (!is_null($only_in)) {
14           if ($only_in === true)
15               $cond = 'price >= 0 AND ';
16           else
17               $cond = 'price < 0 AND ';
18       }
19       else $cond = '';
20       $sql = "SELECT nr,date,description,price * 100 AS price FROM sales WHERE ${cond}paid = 0 AND visible = 1 ORDER BY date,nr";
21       return $this->db->fetchObjectList($sql);
22   }
23
24   public function ajaxSubtotal()
25   {
26
27       $text = '';
28       $commands = '';
29       $total = 0;
30
31       foreach ($_POST['nr'] as &$nr)
32           $nr = intval($nr);
33
34       $sql = sprintf("SELECT nr,date,description,price * 100 AS price FROM sales WHERE nr IN (%s) ORDER BY date,nr",
35                      implode(', ', $_POST['nr']));
36
37       foreach ($this->db->fetchObjectList($sql) as $row) {
38           $date = substr ($row->date,6,2) . "." . substr ($row->date,4,2) . "." . substr ($row->date,0,4);
39           $text .= utf8_encode(sprintf("%d %s %-53s %8.2f\n", $row->nr, $date, substr($row->description,0,53), $row->price / 100));
40           $commands .= sprintf("infocon --date %s --pay %d\n", date('Y-m-d', time()+60*60*24), $row->nr);
41           $total += $row->price;
42       }
43
44       if ($total != 0) {
45           $subject = 'Erstattungen';
46           if (strlen($_POST['title']))
47               $subject .= ' ' . $_POST['title'];
48
49           $text = "Liste der Buchungen\n" . str_repeat('=', 78) . "\n" . $text;
50           $text .= str_repeat('=', 78) . sprintf("\n%-69s %8.2f\n", 'Zwischensumme', $total / 100);
51
52           $mail = new Mail();
53           $mail->env_from(MAIL_FROM);
54           $mail->set('From', mb_encode_mimeheader(utf8_decode(sprintf("%s <%s>", MAIL_FROM_NAME, MAIL_FROM)),'latin1'));
55           $mail->set('To', MAIL_ERROR);
56           $mail->set('Subject', mb_encode_mimeheader($subject,'latin1'));
57           $mail->send($text . "\n\n" . $commands);
58       }
59
60       return true;
61   }
62
63   public function setPaid()
64   {
65     return $this->modify('paid', 1);
66   }
67
68   public function unsetPaid()
69   {
70     return $this->modify('paid', 0);
71   }
72
73   public function ajaxEditDescription(Array $data)
74   {
75       return $this->modify('description', utf8_decode($data['content']));
76   }
77
78   public function ajaxGetUID(Array $data)
79   {
80       return array('description' => utf8_encode($this->data->description),
81                    'uid' => is_null($this->data->uid) ? '' : $this->data->uid);
82   }
83
84   public function ajaxSetUID(Array $data)
85   {
86       return $this->modify('uid', strlen($data['uid']) ? utf8_decode($data['uid']) : NULL);
87   }
88
89 }
90