_init('orange35_custominvoicesgenerator/order'); } protected function _beforeSave() { parent::_beforeSave(); if ($this->isObjectNew() || !$this->getData('alias')) { $this->setData('date', Varien_Date::now()); $this->setData('alias', $this->generateCode()); } if ($this->isObjectNew()){ $this->setData('payment_status', 0); } return $this; } public function getPaymentsSum() { return $this->getAmount() - Mage::getModel('orange35_custominvoicesgenerator/invoice')->getPaymentsSum($this->getId()); } protected function generateCode() { $iterations = self::CODE_GENERATION_ITERATIONS; $code = ''; $unique = false; while ($unique == false && $iterations > 0) { $code = strtoupper(substr(md5(rand() . microtime(true) . rand()), 0, self::DEFAULT_CODE_LENGTH)); $code = strtoupper($code); if ($this->isValid($code)) { $unique = true; } else { $code = ''; } $iterations--; } return $code; } protected function isValid($code) { if (empty($this->_allOrders)) { $this->_allOrders = Mage::getModel('orange35_custominvoicesgenerator/order')->getCollection(); } foreach ($this->_allOrders as $order) { if ($order->getData('alias') == $code) { return false; } } return true; } public function getLink() { return Mage::getBaseUrl() . "admin/sales_custom_order/edit/id/" . $this->getId() . "/"; } public function getDescription(){ return nl2br($this->getData('description')); } }