2011-03-24 57 views
4

在Magento中,我試圖創建一個Credit Memo API,並且已經找到了一個好的起點。這允許我創建貸項憑證。用戶能夠從前端完成的一件事,我無法看到如何複製,如果用戶從前端創建貸項憑證,他們會得到一個「退款存儲信用」選項,我需要將其添加到API。你如何將信用備忘錄退還商店信用?在查看Magento中的Creditmemo.php文件時,我沒有看到任何有關爲信用備忘錄創建商店信用的方法。如何退款以在Magento中存入貸項憑證?

下面是貸項通知單API我在Magento的論壇

class Mage_Sales_Model_Order_Creditmemo_Api extends Mage_Sales_Model_Api_Resource 
{ 
    public function __construct() 
    { 
    } 

    /** 
    * Retrive creditmemos by filters 
    * 
    * @param array $filters 
    * @return array 
    */ 
    public function items($filters = null) 
    { 
     //TODO: add full name logic 
     $collection = Mage::getResourceModel('sales/order_creditmemo_collection') 
      ->addAttributeToSelect('increment_id') 
      ->addAttributeToSelect('state'); 

     $result = array(); 

     foreach ($collection as $creditmemo) { 
      $result[] = $this->_getAttributes($creditmemo, 'creditmemo'); 
     } 

     return $result; 
    } 

    /** 
    * Retrieve creditmemo information 
    * 
    * @param string $creditmemoId 
    * @return array 
    */ 
    public function info($creditmemoId) 
    { 
     $creditmemo = Mage::getModel('sales/order_creditmemo')->load($creditmemoId); 

     if (!$creditmemo->getId()) { 
      $this->_fault('not_exists'); 
     } 

     $result = $this->_getAttributes($creditmemo, 'creditmemo'); 
     $result['order_increment_id'] = $creditmemo->getOrderIncrementId(); 

     $result['items'] = array(); 
     foreach ($creditmemo->getAllItems() as $item) { 
      $result['items'][] = $this->_getAttributes($item, 'creditmemo_item'); 
     } 

     $result['comments'] = array(); 
     foreach ($creditmemo->getCommentsCollection() as $comment) { 
      $result['comments'][] = $this->_getAttributes($comment, 'creditmemo_comment'); 
     } 

     return $result; 
    } 

    /** 
    * Create creditmemo 
    * We can save only new creditmemo. Existing creditmemos are not editable 
    */ 

    /** 
    * Create new creditmemo from order 
    * 
    * @param array  $data // Built the with the same structure as the AdminHtml form used on backend. 
    * @param string $data[order_increment_id] 
    * @param integer $data[invoice_id] 
    * @param array  $data[creditmemo][items] // for each of the items on order/invoice 
    * @param array  $data[creditmemo][items][{item_id}] // item_id from sales_flat_order_item 
    * @param boolean $data[creditmemo][items][{item_id}]['back_to_stock'] 
    * @param integer $data[creditmemo][items][{item_id}]['qty'] // Qty to return 
    * @param string $data[creditmemo][comment_text] 
    * @param string $data[creditmemo][comment_customer_notify] 
    * @param float  $data[creditmemo][shipping_amount] 
    * @param float  $data[creditmemo][adjustment_positive] 
    * @param float  $data[creditmemo][adjustment_negative] 
    * @param booleam $data[creditmemo][do_offline] 
    * @param boolean $data[creditmemo][send_email] 
    * @return array 
    */ 

    public function create($data = array()) 
    { 
     $this->tempData = $data; 

     try { 
      $creditmemo = $this->_initCreditmemo(); 
      if ($creditmemo){ 
       if (($creditmemo->getGrandTotal() <=0) && (!$creditmemo->getAllowZeroGrandTotal())) { 
        $this->_fault('credit_total', $e->getMessage()); 
       } 

       $comment = ''; 
       if (!empty($data['creditmemo']['comment_text'])) { 
        $creditmemo->addComment($data['creditmemo']['comment_text'], isset($data['creditmemo']['comment_customer_notify'])); 
        if (isset($data['creditmemo']['comment_customer_notify'])) { 
         $comment = $data['creditmemo']['comment_text']; 
        } 
       } 

       if (isset($data['creditmemo']['do_refund'])) { 
        $creditmemo->setRefundRequested(true); 
       } 
       if (isset($data['creditmemo']['do_offline'])) { 
        $creditmemo->setOfflineRequested((bool)(int)$data['creditmemo']['do_offline']); 
        $this->tempData['message'][] = 'The credit memo was done offline'; 
       } 

       $creditmemo->register(); 
       if (!empty($data['creditmemo']['send_email'])) { 
        $creditmemo->setEmailSent(true); 
       } 

       $creditmemo->getOrder()->setCustomerNoteNotify(!empty($data['creditmemo']['send_email'])); 
       $this->_saveCreditmemo($creditmemo); 
       $creditmemo->sendEmail(!empty($data['creditmemo']['send_email']), $comment); 
       $this->tempData['message'][] = 'The credit memo has been created.'; 
      } 
     } catch (Mage_Core_Exception $e) { 
      $this->_fault('data_invalid', $e->getMessage()); 
     } 
     return $this->tempData; 
    } 

    protected function _saveCreditmemo($creditmemo) 
    { 
     $transactionSave = Mage::getModel('core/resource_transaction') 
      ->addObject($creditmemo) 
      ->addObject($creditmemo->getOrder()); 
     if ($creditmemo->getInvoice()) { 
      $transactionSave->addObject($creditmemo->getInvoice()); 
     } 
     $transactionSave->save(); 

     return; 
    } 

    /** 
    * Initialize creditmemo model instance 
    * 
    * @return Mage_Sales_Model_Order_Creditmemo 
    */ 
    protected function _initCreditmemo($update = false) 
    { 
     $this->tempData['message'][] = 'init credit memo.'; 
     $creditmemo = false; 
     $creditmemoId = $this->tempData['creditmemo_id']; 
     $orderIncrementId = $this->tempData['order_increment_id']; 
     $invoiceId = $this->tempData['invoice_id']; 
     if ($creditmemoId) { 
      $creditmemo = Mage::getModel('sales/order_creditmemo')->load($creditmemoId); 
     } elseif ($orderIncrementId) { 
      $data = $this->tempData['creditmemo']; 
      $order = Mage::getModel('sales/order')->loadByIncrementId($orderIncrementId); 
      if ($invoiceId) { 
       $invoice = Mage::getModel('sales/order_invoice') 
        ->load($invoiceId) 
        ->setOrder($order); 
       $this->tempData['message'][] = 'loaded_invoice_number: '.$invoice->getId(); 
      } 
      if (!$order->canCreditmemo()) { 
       $this->tempData['message'][] = 'cannot create credit memo'; 
       if(!$order->isPaymentReview()) 
       { 
        $this->tempData['message'][] = 'cannot credit memo Payment is in review'; 
       } 
       if(!$order->canUnhold()) 
       { 
        $this->tempData['message'][] = 'cannot credit memo Order is on hold'; 
       } 
       if(abs($order->getTotalPaid()-$order->getTotalRefunded())<.0001) 
       { 
        $this->tempData['message'][] = 'cannot credit memo Amount Paid is equal or less than amount refunded'; 
       } 
       if($order->getActionFlag('edit') === false) 
       { 
        $this->tempData['message'][] = 'cannot credit memo Action Flag of Edit not set'; 
       } 
       if ($order->hasForcedCanCreditmemo()) { 
        $this->tempData['message'][] = 'cannot credit memo Can Credit Memo has been forced set'; 
       } 
       return false; 
      } 

      if(isset($data['items'])) {$savedData = $data['items'];} else { $savedData = array();} 

      $qtys = array(); 
      $backToStock = array(); 
      foreach ($savedData as $orderItemId =>$itemData) { 
       if (isset($itemData['qty'])) { 
        $qtys[$orderItemId] = $itemData['qty']; 
       } 
       if (isset($itemData['back_to_stock'])) { 
        $backToStock[$orderItemId] = true; 
       } 
      } 
      $data['qtys'] = $qtys; 

      $service = Mage::getModel('sales/service_order', $order); 
      if ($invoice) { 
       $creditmemo = $service->prepareInvoiceCreditmemo($invoice, $data); 
      } else { 
       $creditmemo = $service->prepareCreditmemo($data); 
      } 

      /** 
      * Process back to stock flags 
      */ 
      foreach ($creditmemo->getAllItems() as $creditmemoItem) { 
       $orderItem = $creditmemoItem->getOrderItem(); 
       $parentId = $orderItem->getParentItemId(); 
       if (isset($backToStock[$orderItem->getId()])) { 
        $creditmemoItem->setBackToStock(true); 
       } elseif ($orderItem->getParentItem() && isset($backToStock[$parentId]) && $backToStock[$parentId]) { 
        $creditmemoItem->setBackToStock(true); 
       } elseif (empty($savedData)) { 
        $creditmemoItem->setBackToStock(Mage::helper('cataloginventory')->isAutoReturnEnabled()); 
       } else { 
        $creditmemoItem->setBackToStock(false); 
       } 
       $this->tempData['message'][] = $orderItem->getId(); 
      } 
     } 

     return $creditmemo; 
    } 

} // Class Mage_Sales_Model_Order_Creditmemo_Api End 
+4

你正在尋找的功能在Magento的CE版本可用。它只適用於Pro和Enterprise。 – 2011-03-24 21:03:55

+0

您使用的是哪個版本的Magento? – 2011-03-25 02:22:07

+0

如上所述,您的解決方案是安裝忠誠計劃延期或禮券延期,您可以將其用作替代商店積分的方式,並且您可以使用API​​退款。 – Rabea 2014-03-29 11:34:49

回答

0

可以使用的Magento是salesOrderCreditmemoCreate的默認API創建貸項通知單找到。 這裏是一個Magento的商業鏈接,可以幫助你:LINK

public function create($orderIncrementId, $creditmemoData = null, $comment = null, $notifyCustomer = false, 
    $includeComment = false, $refundToStoreCreditAmount = null) 
{ 
    /** @var $order Mage_Sales_Model_Order */ 
    $order = Mage::getModel('sales/order')->load($orderIncrementId, 'increment_id'); 
    if (!$order->getId()) { 
     $this->_fault('order_not_exists'); 
    } 
    if (!$order->canCreditmemo()) { 
     $this->_fault('cannot_create_creditmemo'); 
    } 
    $creditmemoData = $this->_prepareCreateData($creditmemoData); 

    /** @var $service Mage_Sales_Model_Service_Order */ 
    $service = Mage::getModel('sales/service_order', $order); 
    /** @var $creditmemo Mage_Sales_Model_Order_Creditmemo */ 
    $creditmemo = $service->prepareCreditmemo($creditmemoData); 

    // refund to Store Credit 
    if ($refundToStoreCreditAmount) { 
     // check if refund to Store Credit is available 
     if ($order->getCustomerIsGuest()) { 
      $this->_fault('cannot_refund_to_storecredit'); 
     } 
     $refundToStoreCreditAmount = max(
      0, 
      min($creditmemo->getBaseCustomerBalanceReturnMax(), $refundToStoreCreditAmount) 
     ); 
     if ($refundToStoreCreditAmount) { 
      $refundToStoreCreditAmount = $creditmemo->getStore()->roundPrice($refundToStoreCreditAmount); 
      $creditmemo->setBaseCustomerBalanceTotalRefunded($refundToStoreCreditAmount); 
      $refundToStoreCreditAmount = $creditmemo->getStore()->roundPrice(
       $refundToStoreCreditAmount*$order->getStoreToOrderRate() 
      ); 
      // this field can be used by customer balance observer 
      $creditmemo->setBsCustomerBalTotalRefunded($refundToStoreCreditAmount); 
      // setting flag to make actual refund to customer balance after credit memo save 
      $creditmemo->setCustomerBalanceRefundFlag(true); 
     } 
    } 
    $creditmemo->setPaymentRefundDisallowed(true)->register(); 
    // add comment to creditmemo 
    if (!empty($comment)) { 
     $creditmemo->addComment($comment, $notifyCustomer); 
    } 
    try { 
     Mage::getModel('core/resource_transaction') 
      ->addObject($creditmemo) 
      ->addObject($order) 
      ->save(); 
     // send email notification 
     $creditmemo->sendEmail($notifyCustomer, ($includeComment ? $comment : '')); 
    } catch (Mage_Core_Exception $e) { 
     $this->_fault('data_invalid', $e->getMessage()); 
    } 
    return $creditmemo->getIncrementId(); 
} 
相關問題