_init('orange35_custominvoicesgenerator/invoice'); } protected function _beforeSave() { parent::_beforeSave(); if ($this->isObjectNew()) { $this->setData('date', Varien_Date::now()); } return $this; } public function updateInvoice($data) { return $this->_updateInvoice($data); } protected function _updateInvoice($data) { /** @var $orderModel Orange35_CustomInvoicesGenerator_Model_Order */ $orderModel = Mage::getModel('orange35_custominvoicesgenerator/order')->load($this->getData('order_id')); if ($this->getId() && $orderModel->getId()) { $this->addData($data); $this->save(); $allPaymentsSum = $this->getPaymentsSum($orderModel->getId()); $amount = floatval($orderModel->getData('amount')); $amount = round($amount, 2); if ($allPaymentsSum >= $amount) { $status = Orange35_CustomInvoicesGenerator_Model_Info::STATUS_RECEIVED; } elseif ($allPaymentsSum > 0 && $allPaymentsSum < $amount) { $status = Orange35_CustomInvoicesGenerator_Model_Info::STATUS_PARTIALLY_PAID; } else { $status = Orange35_CustomInvoicesGenerator_Model_Info::STATUS_NOT_RECEIVED; } $orderModel->setData('payment_status', $status); $orderModel->save(); Mage::helper('orange35_custominvoicesgenerator')->sendAdminNotifications($this, $orderModel); } else { Mage::log('Invoice has not updated! order_id ' . $orderModel->getId() . ' or invoice_id ' . $this->getId() . ' is not found in DB', null, 'custom_invoices.log'); } return $this; } public function getPaymentsSum($orderId) { if ($orderId) { $collection = $this->getCollection()->addFilter('order_id', $orderId); $collection->getSelect()->columns('SUM(paid) as total'); $sum = floatval($collection->getFirstItem()->getData('total')); $sum = round($sum, 2); return $sum; } else return 0; } }