Пример #1
0
 public function __construct()
 {
     parent::__construct();
     security::logged_in();
     $this->db = my_db::open();
 }
Пример #2
0
 protected function save()
 {
     $db = my_db::open();
     $db->set($this->id, 'acc', array('acc_id' => $this->acc_id->value(), 'name' => $this->name->value(), 'total' => $this->total));
     util::redirect('acc', 'load', array('id' => $this->id));
 }
Пример #3
0
 public function post()
 {
     $this->message = '';
     $this->name->post();
     $this->date->post();
     $this->total = 0;
     foreach ($this->entry as $entry) {
         $entry['acc_id']->post();
         $entry['amount']->post();
         $this->total += (double) $entry['amount']->value();
     }
     switch (key($_POST['action'])) {
         case 'set':
             break;
         case 'commit':
             // validate
             if (abs($this->total) < 0.01) {
                 if (count($this->entry) > 1) {
                     $ok = true;
                     foreach ($this->entry as $entry) {
                         if (abs($entry['amount']->value()) < 0.01) {
                             $ok = false;
                         }
                     }
                     if ($ok) {
                         $this->db = my_db::open();
                         $this->db->query('begin');
                         $this->id = $this->db->set($this->id, 'trn', array('name' => $this->name->value(), 'dte' => $this->date->value()));
                         $ids = array();
                         foreach ($this->entry as $entry) {
                             if ($entry['id']) {
                                 $ids[] = $entry['id'];
                             }
                         }
                         if ($ids) {
                             $this->db->query('delete from ent where trn_id=? and id not in (' . join(',', $ids) . ')', $this->id);
                         }
                         foreach ($this->entry as &$entry) {
                             $entry['id'] = $this->db->set($entry['id'], 'ent', array('trn_id' => $this->id, 'acc_id' => $entry['acc_id']->value(), 'amount' => $entry['amount']->value()));
                         }
                         unset($entry);
                         $this->db->query('commit');
                         util::redirect('trn', 'load', array('id' => $this->id));
                     } else {
                         $this->message = 'One or more entries has a balance of 0.00';
                     }
                 } else {
                     $this->message = 'There must be two or more entries to commit.';
                 }
             } else {
                 $this->message = 'Your total must be 0.00';
             }
             break;
         case 'cancel':
             util::redirect('secure');
         case 'remove':
             $i = key($_POST['action']['remove']);
             $this->total -= $this->entry[$i]['amount']->value();
             unset($this->entry[$i]);
             break;
         case 'add':
             $this->add_entry();
             break;
     }
     util::redirect($this);
 }