Support automatic generation and calculation for reimbursements
authorJoey Schulze <joey@infodrom.org>
Thu, 26 Apr 2018 20:32:59 +0000 (22:32 +0200)
committerJoey Schulze <joey@infodrom.org>
Thu, 26 Apr 2018 20:51:46 +0000 (22:51 +0200)
class/sales.class.php
src/InfoCon/buch/reimbursement.wml [new file with mode: 0644]
src/InfoCon/buch/submenu.inc

index 3a0292d..326750b 100644 (file)
@@ -8,6 +8,48 @@ class Sales extends DatabaseTable {
     parent::__construct('sales', $id);
   }
 
     parent::__construct('sales', $id);
   }
 
+  public function getOpenItems($from=false, $to=false)
+  {
+      $sql = "SELECT nr,date,description,price * 100 AS price FROM sales WHERE paid = 0 AND visible = 1 ORDER BY date,nr";
+      return $this->db->fetchObjectList($sql);
+  }
+
+  public function ajaxSubtotal()
+  {
+
+      $text = '';
+      $commands = '';
+      $total = 0;
+
+      foreach ($_POST['nr'] as &$nr)
+         $nr = intval($nr);
+
+      $sql = sprintf("SELECT nr,date,description,price * 100 AS price FROM sales WHERE nr IN (%s) ORDER BY date,nr",
+                    implode(', ', $_POST['nr']));
+
+      foreach ($this->db->fetchObjectList($sql) as $row) {
+         $date = substr ($row->date,6,2) . "." . substr ($row->date,4,2) . "." . substr ($row->date,0,4);
+         $text .= utf8_encode(sprintf("%d %s %-53s %8.2f\n", $row->nr, $date, substr($row->description,0,53), $row->price / 100));
+         $commands .= sprintf("infocon --date %s --pay %d\n", date('Y-m-d', time()+60*60*24), $row->nr);
+         $total += $row->price;
+      }
+
+      if ($total != 0) {
+
+         $text = "Liste der Buchungen\n" . str_repeat('=', 78) . "\n" . $text;
+         $text .= str_repeat('=', 78) . sprintf("\n%-69s %8.2f\n", 'Zwischensumme', $total / 100);
+
+         $mail = new Mail();
+         $mail->env_from(MAIL_FROM);
+         $mail->set('From', mb_encode_mimeheader(utf8_decode(sprintf("%s <%s>", MAIL_FROM_NAME, MAIL_FROM)),'latin1'));
+         $mail->set('To', MAIL_ERROR);
+         $mail->set('Subject', 'Erstattungen');
+         $mail->send($text . "\n\n" . $commands);
+      }
+
+      return true;
+  }
+
   public function setPaid()
   {
     return $this->modify('paid', 1);
   public function setPaid()
   {
     return $this->modify('paid', 1);
diff --git a/src/InfoCon/buch/reimbursement.wml b/src/InfoCon/buch/reimbursement.wml
new file mode 100644 (file)
index 0000000..010fa89
--- /dev/null
@@ -0,0 +1,66 @@
+#include <infocon.style>
+
+<future>
+<page func=InfoCon title="Buchhaltung">
+
+<style type="text/css">
+span.in { color: green; }
+span.out { color: red; }
+span.in:hover, span.out:hover { background: yellow; border: 1px solid #ccc; }
+</style>
+
+<h3>Erstattungen / Offene Buchungen</h3>
+
+<form id="open_items">
+<?php
+    $sales = new Sales();
+    foreach ($sales->getOpenItems() as $row) {
+       $date = substr ($row->date,6,2) . "." . substr ($row->date,4,2) . "." . substr ($row->date,0,4);
+       printf('<input name="nr[]" type="checkbox" value="%d" price="%d">&nbsp;', $row->nr, $row->price);
+       printf('<span class="%s">%d&nbsp;&nbsp;%s %s (%.2f&nbsp;&euro;)</span><br>',
+              $row->price < 0 ? 'out' : 'in',
+              $row->nr, $date, $row->description, $row->price / 100);
+    }
+?>
+Zwischensumme <input type="text" id="subtotal" value="" size="5" style="background: #ececec;text-align:right;" readonly> &euro;
+<p><input type="submit" class="button" value="Bezahlen" onclick="return send_request()"> <input type="reset" class="button" value="Reset"><p>
+</form>
+
+</page>
+<protect>
+<script type="text/javascript">
+function cb_change()
+{
+    var subtotal = $('input#subtotal').val();
+    if (typeof subtotal == 'string' && !subtotal.length) subtotal = 0;
+    else subtotal = parseFloat(subtotal) * 100;
+
+    if ($(this).prop('checked'))
+       subtotal += parseFloat($(this).attr('price'));
+    else
+       subtotal -= parseFloat($(this).attr('price'));
+
+    if (subtotal == 0)
+       $('input#subtotal').val('');
+    else
+       $('input#subtotal').val(subtotal / 100);
+}
+
+function send_request()
+{
+    console.log($('form#open_items').serialize());
+
+    $.invoke('Sales/Subtotal', $('form#open_items').serialize(), function(data){
+       show_message('Mail sent');
+       $('form#open_items')[0].reset();
+    });
+
+    return false;
+}
+
+$(function(){
+       $('form#open_items input[type="checkbox"]').click(cb_change);
+       setTimeout(function(){$('form#open_items')[0].reset();}, 500);
+});
+</script>
+</protect>
index 2a4c749..94c8757 100644 (file)
@@ -1,6 +1,7 @@
 <strong>Menu</strong><br>
 
 &nbsp;<menu-item base=index text=Buchhaltung href=index.php><br>
 <strong>Menu</strong><br>
 
 &nbsp;<menu-item base=index text=Buchhaltung href=index.php><br>
+&nbsp;<menu-item base=reimbursement text=Erstattungen href=reimbursement.php><br>
 &nbsp;<menu-item base=edit text=Neu href=edit.php><br>
 
 <menu-rule>
 &nbsp;<menu-item base=edit text=Neu href=edit.php><br>
 
 <menu-rule>