Magento RMA API for openErp module - XmlRpc call

1. here is the model define:

<global>
        <models>
            <openerpconnector>
                <class>Openlabs_OpenERPConnector_Model</class>
            </openerpconnector>

 

2. here is the resource api define:

    plese look at <model> node

    it is important

 <sales_rma translate="title" module="Openlabs_OpenERPConnector">
                <model>openerpconnector/sales_rma_api</model>
                <title>Custom Sales RMA API</title>
                <methods>
                    <search translate="title" module="Openlabs_OpenERPConnector">
                        <title>Return the list of products ids that match with the filter</title>
                        <method>search</method>
                    </search>
                    <info translate="title" module="Openlabs_OpenERPConnector">
                        <title>Return the rma and detail items and history info</title>
                        <method>info</method>
                    </info>
                     <list translate="title" module="Openlabs_OpenERPConnector">
                        <title>Return the rma list info by filter data</title>
                        <method>retrieveRmas</method>
                    </list>
                    <update translate="title" module="Openlabs_OpenERPConnector">
                        <title>update rma and rma items status by rma id and status data</title>
                        <method>update</method>
                    </update>
                    <delete translate="title" module="Openlabs_OpenERPConnector">
                        <title>delete rma and rma items status and histories by rma incremnt id</title>
                        <method>delete</method>
                    </delete>
                </methods>
           </sales_rma>

 3. Api.php

<?php

/**
 *
 * @author     Mohammed NAHHAS
 * @package Openlabs_OpenERPConnector
 *
 */

class Openlabs_OpenERPConnector_Model_Sales_Rma_Api extends Mage_Sales_Model_Api_Resource {

    /**
     * Return the list of products ids that match with the filter
     * The filter imported is required
     * @param  array
     * @return array
     */
    public function search($data) {

        $result = array();
        
       $collection = Mage::getModel("enterprise_rma/rma")->getCollection();
                //->addAttributeToFilter('imported', array('eq' => $data['imported']));

            if(isset($data['limit'])) {
                $collection->setPageSize($data['limit']);
                $collection->setOrder('entity_id', 'ASC');
            }

            if(isset($data['filters']) && is_array($data['filters'])) {
                $filters = $data['filters'];
                foreach($filters as $field => $value) {
                    $collection->addFieldToFilter($field, $value);
                }
            }

            foreach ($collection as $order) {
                $result[] =  $order['increment_id'];
            }
            return $result;
    }
    
    /**
     * Retrieve full order information
     *
     * @param string $orderIncrementId
     * @return array
     */
    public function info($increment_id)
    {
    	$rma = null;
    	$collection =  Mage::getModel("enterprise_rma/rma")->getCollection()
                ->addFieldToFilter('increment_id', array('eq' => $increment_id));
    	
    	foreach ($collection as $rma) {
    		$rma = $rma;
    		break;
    	}
    	
    	$result = $this->_getAttributes($rma, 'rma');
    	$result['items'] = array();
    
    	$item_collection = Mage::getModel("enterprise_rma/item")->getCollection()->addFieldToFilter(
    		'rma_entity_id', $rma->getId()
    	);
    	
    	foreach ($item_collection as $item) {
    		$result['items'][] = $this->_getAttributes($item, 'item');
    	}
    	$result['status_history'] = array();
    
    	$history_collection =  Mage::getModel('enterprise_rma/rma_status_history')->getCollection()
    	->addFieldToFilter('rma_entity_id',$rma->getId());
    	
    	foreach ($history_collection as $history) {
    		$result['status_history'][] = $this->_getAttributes($history, 'rma_status_history');
    	}
    
    	return $result;
    }
    
    public function retrieveRmas($data) {
    	$result = array();
    		$collection = Mage::getModel("enterprise_rma/rma")->getCollection();
    
    		if(isset($data['limit'])) {
    			$collection->setPageSize($data['limit']);
    			$collection->setOrder('entity_id', 'ASC');
    		}
    
    		if(isset($data['filters']) && is_array($data['filters'])) {
    			$filters = $data['filters'];
    			foreach($filters as $field => $value) {
    				$collection->addFieldToFilter($field, $value);
    			}
    		}
    
    		foreach ($collection as $rma) {
    			$tmp = $this->_getAttributes($rma, 'rma');
    			$result[] = $tmp;
    		}
    		return $result;
    }
    
     public function update($rma_id, $data) {
    	
    	
    	$collection =  Mage::getModel("enterprise_rma/rma")->getCollection()
    	->addFieldToFilter('increment_id', array('eq' => $rma_id));
    	 
    	foreach ($collection as $rma) {
    		$rma = $rma;
    		break;
    	}
    	
    	$rma_items = Mage::getModel("enterprise_rma/item")->getCollection()->addFieldToFilter(
    		'rma_entity_id', $rma->getId()
    	);
    	
    	
    	if (isset ($data['status']) && !empty($data['status'])) {
    		$status = $data['status'];
	    	//update items status
	    	foreach ($rma_items as $item) {
	    		$qty_requested = $item->getData('qty_requested');
	    		if ($status == Enterprise_Rma_Model_Rma_Source_Status::STATE_AUTHORIZED) {
	    			$item->setData('qty_authorized' , $qty_requested);
	    		} else if ($status == Enterprise_Rma_Model_Rma_Source_Status::STATE_APPROVED) {
	    			$item->setData('qty_approved' , $qty_requested);
	    		} else if ($status == Enterprise_Rma_Model_Rma_Source_Status::STATE_RECEIVED) {
	    			$item->setData('qty_returned' , $qty_requested);
	    		}
	    		if ($status == Enterprise_Rma_Model_Rma_Source_Status::STATE_CLOSED) {
	    			$item->setStatus(Enterprise_Rma_Model_Rma_Source_Status::STATE_DENIED);
	    		} else {
	    			$item->setStatus($status);
	    		}
	    		$item->save();
	    	}
	    	if ($status == Enterprise_Rma_Model_Rma_Source_Status::STATE_APPROVED) {
	    		//确认完成的订单
	    		$rma->setData('status', Enterprise_Rma_Model_Rma_Source_Status::STATE_PROCESSED_CLOSED);
	    		
	    	} elseif ($status == Enterprise_Rma_Model_Rma_Source_Status::STATE_REJECTED) {
	    		//确认拒绝收到货物的订单
	    		$rma->setData('status', Enterprise_Rma_Model_Rma_Source_Status::STATE_CLOSED);
	    		
	    	} else {
	    		$rma->setData('status', $status);
	    	}
	    	$rma->save();
	    	return true;
	    } else {
	    		$this->_fault('data_invalid', "Error, the attribut 'status' need to be specified");
	    		
	    }
}
      
    public function delete($increment_id) {
    	try {
	    	// get rma entity
	    	$collection =  Mage::getModel("enterprise_rma/rma")->getCollection()
	    	->addFieldToFilter('increment_id', array('eq' => $increment_id));
	
	    	foreach ($collection as $rma) {
	    		$rma = $rma;
	    		break;
	    	}
	    	
	    	//get grid entity
	    	$collection_grid =  Mage::getModel("enterprise_rma/grid")->getCollection()
	    	->addFieldToFilter('increment_id', array('eq' => $increment_id)); 
	    	foreach ($collection_grid as $rma_grid) {
	    		$grid = $rma_grid;
	    		break;
	    	}
	    
	    	// get history entity
	    	$history_collection =  Mage::getModel('enterprise_rma/rma_status_history')->getCollection()
	    	->addFieldToFilter('rma_entity_id',$rma->getId());
	    	
	    	foreach ($history_collection as $hc) {
	    		$hc = $hc;
	    		$hc->delete();
	    	}
	    	
	    	//get rma_shipping_label
	    	$shipping_collection =  Mage::getModel('enterprise_rma/shipping')->getCollection()
	    	->addFieldToFilter('rma_entity_id',$rma->getId());
	    	foreach ($shipping_collection as $sc) {
	    		$sc->delete();
	    	}
	    	
	    	
	    	//get items
	    	$rma_items = Mage::getModel("enterprise_rma/item")->getCollection()->addFieldToFilter(
	    			'rma_entity_id', $rma->getId()
	    	);
	    	
	    	foreach ($rma_items as $item) {
	    		$item->delete();
	    	}
	    	
	    	$grid->delete();
	    	$rma->delete();
	    	return true;
    	} catch (Exception $e) {
    	    var_dump($e->getMessage());
    		$this->_fault('data_invalid', $e->getMessage());
    	}
    	
    }
    
    protected function _getAttributes($object, $type, array $attributes = null)
    {
    	$result = array();
    
    	if (!is_object($object)) {
    		return $result;
    	}
    
    	foreach ($object->getData() as $attribute=>$value) {
    		$result[$attribute] = $value;
    	}
    
    	if (isset($this->_attributesMap['global'])) {
    		foreach ($this->_attributesMap['global'] as $alias=>$attributeCode) {
    			$result[$alias] = $object->getData($attributeCode);
    		}
    	}
    
    	if (isset($this->_attributesMap[$type])) {
    		foreach ($this->_attributesMap[$type] as $alias=>$attributeCode) {
    			$result[$alias] = $object->getData($attributeCode);
    		}
    	}
    
    	return $result;
    }
    
}

 

4. call example:

require_once 'abstract.php';

/**
 * Magento Compiler Shell Script
 *
 * @category    Mage
 * @package     Mage_Shell
 * @author      Magento Core Team <[email protected]>
 */
class Mage_Shell_Test extends Mage_Shell_Abstract
{

    /**
     * Run script
     *
     */
    public function run()
    {
    	$client = new Zend_XmlRpc_Client('http://lifemore.svn-test.com/index.php/api/xmlrpc');
    	$api_user = 'uername';
    	$api_password = '******';
    	
    	$session = $client->call('login', array($api_user, $api_password));

    	$params = array(array('filters'=>array('increment_id'=>array('eq'=>'300000005'))));
    	
    	
    	$r = $client->call('call', array($session, 'sales_rma.search',$params));
      
    
    	var_dump($r);
        
    }
}

$shell = new Mage_Shell_Test();
$shell->run();

 

 

你可能感兴趣的:(Module)