如何在 Magento 中退款以存储贷项凭证?
在 Magento 中,我正在尝试创建一个 Credit Memo API,并且已经找到了一个很好的起点。这使我能够创建贷项凭证。用户能够从前端做的一件事是,我看不到如何复制,如果用户从前端创建贷项凭证,他们会得到一个“退款到存储信用”选项,我需要将其添加到API。如何将贷项凭证退还至商店积分?在 Magento 中查看 Creditmemo.php 文件时,我没有看到任何与为贷项凭证创建商店信用相关的方法。
下面是我在 Magento 论坛上找到的贷项凭证 API
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
In Magento I am trying to create a Credit Memo API and have already found a good starting point. That allows me to create credit memos. The one thing that user is able to do from the front end that I can not see how to replicate is if the user creates a credit memo from the front end they get a "Refund to Store Credit" option which I need to add to the API. How do you refund a credit memo to a store credit? When looking through the Creditmemo.php file in Magento I do not see any methods pertaining to creating a store credit for the credit memo.
Below is the credit memo API I found on the Magento Forums
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 magento 的默认 API(salesOrderCreditmemoCreate)创建贷项凭证。
这是一个可以帮助您的 magento commerce 链接:LINK
You can create Credit Memo using default API of magento which is salesOrderCreditmemoCreate.
Here's a magento commerce link that may help you : LINK