Example #1
0
 public function getPdf($invoices = array())
 {
     foreach ($invoices as $invoice) {
         if ($invoice->getOrder()->getPayment()->getMethod() != 'sofortrechnung') {
             return parent::getPdf($invoices);
         }
         break;
     }
     $this->_beforeGetPdf();
     $this->_initRenderer('invoice');
     $pdf = new Zend_Pdf();
     $this->_setPdf($pdf);
     $style = new Zend_Pdf_Style();
     $this->_setFontBold($style, 10);
     foreach ($invoices as $invoice) {
         if ($invoice->getStoreId()) {
             Mage::app()->getLocale()->emulate($invoice->getStoreId());
             Mage::app()->setCurrentStore($invoice->getStoreId());
         }
         $page = $pdf->newPage(Zend_Pdf_Page::SIZE_A4);
         $pdf->pages[] = $page;
         $order = $invoice->getOrder();
         $tid = $order->getPayment()->getPnSuTransactionId();
         if (!empty($tid)) {
             $sObj = new SofortLib_TransactionData(Mage::getStoreConfig('payment/sofort/configkey'));
             $sObj->setTransaction($tid)->sendRequest();
             if ($sObj->isError()) {
                 Mage::throwException(Mage::helper('sales')->__($sObj->getError()));
                 return;
             } else {
                 $url = $sObj->getInvoiceUrl();
                 $pdf = file_get_contents($url);
                 return Zend_Pdf::parse($pdf);
             }
         }
         /* Add image */
         $this->insertLogo($page, $invoice->getStore());
         /* Add address */
         $this->insertAddress($page, $invoice->getStore());
         /* Add head */
         $this->insertOrder($page, $order, Mage::getStoreConfigFlag(self::XML_PATH_SALES_PDF_INVOICE_PUT_ORDER_ID, $order->getStoreId()));
         $page->setFillColor(new Zend_Pdf_Color_GrayScale(1));
         $this->_setFontRegular($page);
         $page->drawText(Mage::helper('sales')->__('Invoice # ') . $invoice->getIncrementId(), 35, 780, 'UTF-8');
         /* Add table */
         $page->setFillColor(new Zend_Pdf_Color_RGB(0.93, 0.92, 0.92));
         $page->setLineColor(new Zend_Pdf_Color_GrayScale(0.5));
         $page->setLineWidth(0.5);
         $page->drawRectangle(25, $this->y, 570, $this->y - 15);
         $this->y -= 10;
         /* Add table head */
         $page->setFillColor(new Zend_Pdf_Color_RGB(0.4, 0.4, 0.4));
         $page->drawText(Mage::helper('sales')->__('Products'), 35, $this->y, 'UTF-8');
         $page->drawText(Mage::helper('sales')->__('SKU'), 255, $this->y, 'UTF-8');
         $page->drawText(Mage::helper('sales')->__('Price'), 380, $this->y, 'UTF-8');
         $page->drawText(Mage::helper('sales')->__('Qty'), 430, $this->y, 'UTF-8');
         $page->drawText(Mage::helper('sales')->__('Tax'), 480, $this->y, 'UTF-8');
         $page->drawText(Mage::helper('sales')->__('Subtotal'), 535, $this->y, 'UTF-8');
         $this->y -= 15;
         $page->setFillColor(new Zend_Pdf_Color_GrayScale(0));
         /* Add body */
         foreach ($invoice->getAllItems() as $item) {
             if ($item->getOrderItem()->getParentItem()) {
                 continue;
             }
             if ($this->y < 15) {
                 $page = $this->newPage(array('table_header' => true));
             }
             /* Draw item */
             $page = $this->_drawItem($item, $page, $order);
         }
         /* Add totals */
         $page = $this->insertTotals($page, $invoice);
         if ($invoice->getStoreId()) {
             Mage::app()->getLocale()->revert();
         }
     }
     $this->_afterGetPdf();
     return $pdf;
 }
 /**
  * notification about status change
  */
 public function notificationAction()
 {
     set_error_handler('sofort_notification_error');
     // prevent to submit confirm/cancel back
     $GLOBALS['isNotificationAction'] = true;
     $response = $this->getRequest()->getParams();
     $orderId = $response['orderId'];
     $secret = $response['secret'];
     // read notofication
     $sofort = new SofortLib_Notification();
     $transaction = $sofort->getNotification();
     //no valid parameters/xml
     if (empty($orderId) || empty($transaction)) {
         Mage::log('Notification invalid: ' . __CLASS__ . ' ' . __LINE__ . " - " . $orderId . " - " . $transaction);
         return;
     }
     if ($sofort->isError()) {
         Mage::log($sofort->getError(), Zend_Log::ERR, 'sofort_error.log');
     }
     // load current transaction data
     $transData = new SofortLib_TransactionData(Mage::getStoreConfig('payment/sofort/configkey'));
     $transData->setTransaction($transaction)->sendRequest();
     if ($transData->isError()) {
         Mage::log('Notification invalid: ' . __CLASS__ . ' ' . __LINE__ . $transData->getError());
         return;
     }
     $order = Mage::getModel('sales/order');
     $order->loadByIncrementId($orderId);
     $paymentObj = $order->getPayment()->getMethodInstance();
     $payment = $order->getPayment();
     //data of transaction doesn't match order
     if ($payment->getAdditionalInformation('sofort_transaction') != $transaction || $payment->getAdditionalInformation('sofort_secret') != $secret) {
         Mage::log('Notification invalid: ' . __CLASS__ . ' ' . __LINE__);
         return;
     }
     // BUGFIX
     // if notification for confirm + refund got the same, we need to confirm first
     if ($transData->isSofortrechnung() && $transData->isRefunded() && !$order->hasInvoices()) {
         $this->_transactionConfirmed($transData, $order, true);
         // reload order
         $order = Mage::getModel('sales/order');
         $order->loadByIncrementId($orderId);
     }
     // check if order was edit
     $sofortRechnung = Mage::getModel('pnsofortueberweisung/sofortrechnung');
     $sofortRechnung->updateOrderFromTransactionData($transData, $order);
     // check if something other change
     if ($payment->getAdditionalInformation('sofort_lastchanged') === $this->_getLastChanged($transData)) {
         return;
     }
     $payment->setAdditionalInformation('sofort_lastchanged', $this->_getLastChanged($transData))->save();
     // kauf auf Rechnung
     if ($transData->isSofortrechnung()) {
         if ($transData->isLoss()) {
             $this->_transactionLoss($transData, $order);
         } elseif ($transData->isPending() && $transData->getStatusReason() == 'confirm_invoice') {
             $this->_transactionUnconfirmed($transData, $order);
         } elseif ($transData->isPending()) {
             $this->_transactionConfirmed($transData, $order);
         } elseif ($transData->isReceived()) {
             //don't do anything
         } elseif ($transData->isRefunded()) {
             $this->_transactionRefunded($transData, $order);
         } else {
             //uups
             $order->addStatusHistoryComment($transData->getStatus() . " " . $transData->getStatusReason());
         }
         // sofortueberweisung, lastschrift
     } else {
         if ($transData->isLoss()) {
             $this->_transactionLoss($transData, $order);
         } elseif ($transData->isPending()) {
             $this->_transactionConfirmed($transData, $order);
         } elseif ($transData->isReceived()) {
             // no status change on received
             $this->_transactionReceived($transData, $order);
         } elseif ($transData->isRefunded()) {
             $this->_transactionRefunded($transData, $order);
         } else {
             //uups
             $order->addStatusHistoryComment($transData->getStatus() . " " . $transData->getStatusReason());
         }
     }
     $order->save();
 }
 /**
  * handle request to update invoice items and send request to remote service
  * 
  */
 public function postAction()
 {
     $order = $this->_initOrder();
     $params = $this->getRequest()->getParams();
     $session = Mage::getSingleton('adminhtml/session');
     $sofortRechnung = Mage::getModel('pnsofortueberweisung/sofortrechnung');
     // if everythink empty, cancel invoice
     if (!empty($params['line']) && array_sum($params['line']) == 0) {
         if ($order->canUnhold()) {
             $order->unhold();
             $order->save();
         }
         if ($order->canCancel()) {
             $order->cancel();
             $order->save();
         }
         $payment = $order->getPayment();
         $invoice = Mage::getModel('pnsofortueberweisung/sofortrechnung');
         $invoice->refund($payment, null);
     } else {
         if (!empty($params['line'])) {
             // generate old send parameters
             $invoice = $sofortRechnung->createPaymentFromOrder($order);
             // get old items
             $items = $invoice->getSofortrechnungItems();
             // new item array
             $newItems = array();
             // mark change
             $change = false;
             // check all old items
             foreach ($items['item'] as $line) {
                 // increase quantity not allowed
                 if ($line['quantity'] < $params['line'][$line['item_id']]) {
                     $this->_getSession()->addError($this->__('quantity increase is not allowed'));
                     $this->_redirect('*/*/edit', array('order_id' => $order->getId()));
                     return false;
                 }
                 // only quantity change
                 if (array_key_exists($line['item_id'], $params['line']) && $line['quantity'] != $params['line'][$line['item_id']]) {
                     $change = true;
                     $line['quantity'] = $params['line'][$line['item_id']];
                 }
                 if (!empty($params['price'][$line['item_id']])) {
                     // increase price not allowed
                     if ($line['unit_price'] < $params['price'][$line['item_id']]) {
                         $this->_getSession()->addError($this->__('price increase is not allowed'));
                         $this->_redirect('*/*/edit', array('order_id' => $order->getId()));
                         return false;
                     }
                     $change = true;
                     $line['unit_price'] = $params['price'][$line['item_id']];
                 }
                 // if quantity is greater as null
                 if (!empty($line['quantity'])) {
                     $newItems[] = $line;
                 }
             }
             // if something change we send the request
             if ($change) {
                 try {
                     $sofortRechnung->updateInvoice($order->getPayment(), $newItems, $params['comment']);
                 } catch (Exception $e) {
                     Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
                     $this->_redirect('*/*/edit', array('order_id' => $order->getId()));
                     return false;
                 }
             }
             Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('pnsofortueberweisung')->__('Item was successfully saved'));
         }
     }
     // ****** start edit local order ***************
     $payment = $order->getPayment();
     $transaction = $payment->getAdditionalInformation('sofort_transaction');
     $transData = new SofortLib_TransactionData(Mage::getStoreConfig('payment/sofort/configkey'));
     // loaded updatet data from remote host
     $transData->setTransaction($transaction)->sendRequest();
     if ($transData->isError()) {
         Mage::log('Update invalid: ' . __CLASS__ . ' ' . __LINE__ . $transData->getError());
         Mage::getSingleton('adminhtml/session')->addNotice(Mage::helper('pnsofortueberweisung')->__('Item was successfully saved, order can not change'));
         $this->_redirect('*/*');
         return;
     }
     // update local order
     $sofortRechnung->updateOrderFromTransactionData($transData, $order);
     // ****** end edit local order ***************
     $this->_redirect('adminhtml/sales_order/view', array('order_id' => $order->getId()));
     return false;
 }