Esempio n. 1
0
 /**
  * Creates an invoice and related items
  */
 public function create()
 {
     $post = Http::getParameter('POST');
     if (count($this->validate()) > 0) {
         throw new Exception('Invalid data entered', 400);
     }
     // create invoice
     if ($this->id == 'new') {
         $this->id = null;
     }
     $this->status = 'Drafted';
     $id = parent::create();
     // create entries
     $this->entries = array();
     foreach ($post['Invoice']['Entry'] as $entry) {
         $Entry = new Entry();
         $Entry->__bind($entry);
         $Entry->invoice_id = $id;
         $Entry->billed = Entry::__getDateTimeFromString($Entry->billed);
         if (!array_key_exists('total', $entry)) {
             $Entry->total = round($entry['quantity'] * $entry['amount_per'], 2);
         }
         $Entry->create();
         $this->entries[] = $Entry;
     }
     // create discounts
     $this->discounts = array();
     foreach ($post['Invoice']['Discount'] as $discount) {
         $Discount = new Discount();
         $Discount->__bind($discount);
         $Discount->invoice_id = $id;
         $Discount->create();
         $this->discounts[] = $Discount;
     }
     // update total
     $this->update_total();
     // update cache, see libraries/misc.php
     delete_cache_entry('invoices', $id);
     // return
     return $id;
 }