Support automatic generation and calculation for reimbursements
[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($from=false, $to=false)
12   {
13       $sql = "SELECT nr,date,description,price * 100 AS price FROM sales WHERE paid = 0 AND visible = 1 ORDER BY date,nr";
14       return $this->db->fetchObjectList($sql);
15   }
16
17   public function ajaxSubtotal()
18   {
19
20       $text = '';
21       $commands = '';
22       $total = 0;
23
24       foreach ($_POST['nr'] as &$nr)
25           $nr = intval($nr);
26
27       $sql = sprintf("SELECT nr,date,description,price * 100 AS price FROM sales WHERE nr IN (%s) ORDER BY date,nr",
28                      implode(', ', $_POST['nr']));
29
30       foreach ($this->db->fetchObjectList($sql) as $row) {
31           $date = substr ($row->date,6,2) . "." . substr ($row->date,4,2) . "." . substr ($row->date,0,4);
32           $text .= utf8_encode(sprintf("%d %s %-53s %8.2f\n", $row->nr, $date, substr($row->description,0,53), $row->price / 100));
33           $commands .= sprintf("infocon --date %s --pay %d\n", date('Y-m-d', time()+60*60*24), $row->nr);
34           $total += $row->price;
35       }
36
37       if ($total != 0) {
38
39           $text = "Liste der Buchungen\n" . str_repeat('=', 78) . "\n" . $text;
40           $text .= str_repeat('=', 78) . sprintf("\n%-69s %8.2f\n", 'Zwischensumme', $total / 100);
41
42           $mail = new Mail();
43           $mail->env_from(MAIL_FROM);
44           $mail->set('From', mb_encode_mimeheader(utf8_decode(sprintf("%s <%s>", MAIL_FROM_NAME, MAIL_FROM)),'latin1'));
45           $mail->set('To', MAIL_ERROR);
46           $mail->set('Subject', 'Erstattungen');
47           $mail->send($text . "\n\n" . $commands);
48       }
49
50       return true;
51   }
52
53   public function setPaid()
54   {
55     return $this->modify('paid', 1);
56   }
57
58   public function unsetPaid()
59   {
60     return $this->modify('paid', 0);
61   }
62
63   public function ajaxText(Array $data)
64   {
65     return $this->modify('description', $data['text']);
66   }
67
68   public function ajaxSetStatus(Array $data)
69   {
70     if (count($data['ids'])) {
71       $sql = sprintf('UPDATE stempel SET status = %d WHERE oid IN (%s)',
72                      $data['status'], implode(',',$data['ids']));
73       return $this->execute($sql);
74     }
75
76     return true;
77   }
78
79 }
80