* @version SVN: $Id: Builder.php 7490 2010-03-29 19:53:27Z jwage $ */ class Invoice extends BaseInvoice { public function isPaid() { return $this->getIsPaid(); } public function setIsPaid($value) { if($value) { foreach($this->getItems() as $item) { $item->activateBounds(); } } else { foreach($this->getItems() as $item) { $item->deactivateBounds(); } } $this->_set('is_paid', $value); } public function getPrice() { $result = Doctrine::getTable('InvoiceItem')->createQuery() ->select('SUM((price_one * amount) + ((price_one * amount) * (dph / 100))) as sum') ->addWhere('invoice_id = ?', $this->getId()) ->fetchArray(); if(!isset($result[0]['sum'])) return 0; if(strtotime($this->getCreatedAt()) <= strtotime('2013-8-25')) return (int) $result[0]['sum']; return round($result[0]['sum'], 2); } /** * * @param mixed $user * @return boolean */ public function hasPermissions($user) { if($user instanceof sfOutputEscaper) { $user = $user->getRawValue(); } if($user instanceof myUser) { $user = $user->getGuardUser(); } if($user instanceof sfGuardUser) { if($user->getIsSuperAdmin()) { return true; } $user = $user->getProfile(); } if($user instanceof Profile) { return $this->getProfileId() == $user->getId(); } return false; } /** * * @return integer */ public function getPriceWithoutDph() { $result = Doctrine::getTable('InvoiceItem')->createQuery() ->select('SUM(price_one * amount) as sum') ->addWhere('invoice_id = ?', $this->getId()) ->fetchArray(); return round((isset($result[0]['sum']) ? $result[0]['sum'] : 0),2); } /** * * @return integer */ public function getDphFromPrice() { $result = Doctrine::getTable('InvoiceItem')->createQuery() ->select('SUM((price_one * amount) * (dph / 100)) as sum') ->addWhere('invoice_id = ?', $this->getId()) ->fetchArray(); return round((isset($result[0]['sum']) ? $result[0]['sum'] : 0),2); } /** * Nastaví defaultní hodnoty z nastavení aplikace */ public function setDefaultValues() { $this->setSupplierName(saSettings::get('invoice_supplier_name')); $this->setSupplierStreet(saSettings::get('invoice_supplier_street')); $this->setSupplierCity(saSettings::get('invoice_supplier_city')); $this->setSupplierZip(saSettings::get('invoice_supplier_zip')); $this->setSupplierIc(saSettings::get('invoice_supplier_ic')); $this->setSupplierDic(saSettings::get('invoice_supplier_dic')); $this->setMaturityDate(date('Y-m-d', strtotime('+'.saSettings::get('invoice_maturity_days', 0).' days'))); $this->setNumber(Doctrine::getTable('Invoice')->getNextNumber()); $this->setPaymentForm(saSettings::get('invoice_payment_form')); $this->setAccount(saSettings::get('invoice_account')); $this->setBank(saSettings::get('invoice_bank')); } /** * * @param Profile $profile */ public function setSubscriber(Profile $profile) { $this->setProfileId($profile->getId()); $this->setSubscriberName($profile->getName()); $this->setSubscriberStreet($profile->getStreet()); $this->setSubscriberCity($profile->getCity()); $this->setSubscriberZip($profile->getZip()); } public function getPdfName() { return 'objednavka-'.$this->getNumber().'.pdf'; } public function getCurrency() { if($this->getItems()->count() == 0) { return 'EUR'; } return $this->getItems()->getFirst()->getCurrency(); } }