在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
答案 0 :(得分:0)
您可以使用magento的默认API创建Credit Memo,该API是 salesOrderCreditmemoCreate 。 这是一个可以帮助您的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();
}