<commit_before><?php
/**
 * Client Purchase Model
 *
 * @category  Sailthru
 * @package   Sailthru_Email
 *
 */
class Sailthru_Email_Model_Client_Purchase extends Sailthru_Email_Model_Client
{

    /**
     * Create Cart data to post to API
     *
     * @param Mage_Sales_Model_Quote $quote
     * @param string $email
     * @param string $eventType
     * @return boolean
     */
    public function sendCart(Mage_Sales_Model_Quote $quote, $email = null, $eventType = null)
    {
        try{
            if ($eventType){
                $this->_eventType = $eventType;
            }

            if (!$email) {
                if(!$email = $quote->getCustomerEmail()) {
                    Mage::logException("Unable to post purchase: customer email is not defined.");
                    return false;
                }
            }

            $data = array(
                    'email' => $email,
                    'items' => $this->_getItems($quote->getAllVisibleItems()),
                    'incomplete' => 1,
                    'reminder_time' => '+' . Mage::helper('sailthruemail')->getReminderTime() . ' min',
                    'reminder_template' => Mage::getStoreConfig('sailthru/email/abandoned_cart_template', $quote->getStoreId()),
                    'message_id' => $this->getMessageId()
                    );

            $response = $this->apiPost('purchase', $data);

            if (array_key_exists('error',$response)){
                return $this->handleError($response, $quote, $email);
            } else {
                return true;
            }
        } catch (Exception $e) {
            Mage::logException($e);
        }
    }

    /**
     * Route errors
     *
     * @param array $response
     * @param Mage_Sales_Model_Quote $quote
     * @param string $email
     * @return boolean
     *
     * @todo For future iterations, use switch statement to handle multiple error messages.
     */
    public function handleError($response, $quote, $email)
    {
            if($response['error'] == 14) {
                /**
                 * Response Error 14 means that an unknown template was passed in the API call.
                 * This normally happens for first time API calls or when the name of the template has
                 * been changed, http://getstarted.sailthru.com/api/api-response-errors.  We'll
                 * therefore need to create a template to pass in the call.  One condition for the
                 * template to be created is that the sender email must be verified so please check
                 * https://my.sailthru.com/verify to make sure that the send email is listed there.
                 */
                return $this->createAbandonedCartEmail($quote, $email);
            } else {
                Mage::throwException('Unknown purchase response error: ' . json_encode($response));
            }

    }

    /**
     * Notify Sailthru that a purchase has been made. This automatically cancels
     * any scheduled abandoned cart email.
     *
     */
    public function sendOrder(Mage_Sales_Model_Order $order, $customer)
    {
        try{
            $this->_eventType = 'placeOrder';

            $data = array(
                    'email' => $customer->getEmail(),
                    'items' => $this->_getItems($order->getAllVisibleItems()),
                    'adjustments' => $this->_getAdjustments($order),
                    'message_id' => $this->getMessageId(),
                    'send_template' => 'purchase receipt',
                    'date' => $order->getCreatedAt() . ' ' . Mage::app()->getLocale()->getTimeZone(),
                    'tenders' => $this->_getTenders($order)
                    );
            /**
             * Send order data to purchase API
             */
            $responsePurchase = $this->apiPost('purchase', $data);

            /**
             * Send customer data to user API
             */
            $responseUser = Mage::getModel('sailthruemail/client_user')->sendCustomerData($customer);
        }catch (Exception $e) {
            Mage::logException($e);
            return false;
        }
    }

    /**
     * Prepare data on items in cart or order.
     *
     * @return type
     */
    protected function _getItems($items)
    {
        try {
            $data = array();
            foreach($items as $item) {
                $_item = array();

                if ($item->getProductType() == 'configurable') {
                    $options = $item->getProduct()->getTypeInstance(true)->getOrderOptions($item->getProduct());
                    $_item['title'] = $options['simple_name'];
                    $_item['id'] = $options['simple_sku'];
                    if ($vars = $this->_getVars($options)) {
                        $_item['vars'] = $vars;
                    }
                } else {
                    $_item['id'] = $item->getSku();

                    $_item['title'] = $item->getName();
                }

                if (get_class($item) == 'Mage_Sales_Model_Order_Item' ) {
                    $_item['qty'] = intval($item->getQtyOrdered());
                } else {
                    $_item['qty'] = intval($item->getQty());
                }

                $_item['url'] = $item->getProduct()->getProductUrl();
                $_item['price'] = Mage::helper('sailthruemail')->formatAmount($item->getProduct()->getPrice());

                if ($tags = $this->_getTags($item->getProductId())) {
                    $_item['tags'] = $tags;
                }

                $data[] = $_item;
            }
            return $data;
        } catch (Exception $e) {
             Mage::logException($e);
            return false;
        }
    }

    /**
     * Get order adjustments
     * @param Mage_Sales_Model_Order $order
     * @return array
     */
    protected function _getAdjustments(Mage_Sales_Model_Order $order)
    {<commit_msg>variant issue with getItems<commit_after><?php
/**
 * Client Purchase Model
 *
 * @category  Sailthru
 * @package   Sailthru_Email
 *
 */
class Sailthru_Email_Model_Client_Purchase extends Sailthru_Email_Model_Client
{

    /**
     * Create Cart data to post to API
     *
     * @param Mage_Sales_Model_Quote $quote
     * @param string $email
     * @param string $eventType
     * @return boolean
     */
    public function sendCart(Mage_Sales_Model_Quote $quote, $email = null, $eventType = null)
    {
        try{
            if ($eventType){
                $this->_eventType = $eventType;
            }

            if (!$email) {
                if(!$email = $quote->getCustomerEmail()) {
                    Mage::logException("Unable to post purchase: customer email is not defined.");
                    return false;
                }
            }

            $data = array(
                    'email' => $email,
                    'items' => $this->_getItems($quote->getAllVisibleItems()),
                    'incomplete' => 1,
                    'reminder_time' => '+' . Mage::helper('sailthruemail')->getReminderTime() . ' min',
                    'reminder_template' => Mage::getStoreConfig('sailthru/email/abandoned_cart_template', $quote->getStoreId()),
                    'message_id' => $this->getMessageId()
                    );

            $response = $this->apiPost('purchase', $data);

            if (array_key_exists('error',$response)){
                return $this->handleError($response, $quote, $email);
            } else {
                return true;
            }
        } catch (Exception $e) {
            Mage::logException($e);
        }
    }

    /**
     * Route errors
     *
     * @param array $response
     * @param Mage_Sales_Model_Quote $quote
     * @param string $email
     * @return boolean
     *
     * @todo For future iterations, use switch statement to handle multiple error messages.
     */
    public function handleError($response, $quote, $email)
    {
            if($response['error'] == 14) {
                /**
                 * Response Error 14 means that an unknown template was passed in the API call.
                 * This normally happens for first time API calls or when the name of the template has
                 * been changed, http://getstarted.sailthru.com/api/api-response-errors.  We'll
                 * therefore need to create a template to pass in the call.  One condition for the
                 * template to be created is that the sender email must be verified so please check
                 * https://my.sailthru.com/verify to make sure that the send email is listed there.
                 */
                return $this->createAbandonedCartEmail($quote, $email);
            } else {
                Mage::throwException('Unknown purchase response error: ' . json_encode($response));
            }

    }

    /**
     * Notify Sailthru that a purchase has been made. This automatically cancels
     * any scheduled abandoned cart email.
     *
     */
    public function sendOrder(Mage_Sales_Model_Order $order, $customer)
    {
        try{
            $this->_eventType = 'placeOrder';

            $data = array(
                    'email' => $customer->getEmail(),
                    'items' => $this->_getItems($order->getAllVisibleItems()),
                    'adjustments' => $this->_getAdjustments($order),
                    'message_id' => $this->getMessageId(),
                    'send_template' => 'purchase receipt',
                    'date' => $order->getCreatedAt() . ' ' . Mage::app()->getLocale()->getTimeZone(),
                    'tenders' => $this->_getTenders($order)
                    );
            /**
             * Send order data to purchase API
             */
            $responsePurchase = $this->apiPost('purchase', $data);

            /**
             * Send customer data to user API
             */
            $responseUser = Mage::getModel('sailthruemail/client_user')->sendCustomerData($customer);
        }catch (Exception $e) {
            Mage::logException($e);
            return false;
        }
    }

    /**
     * Prepare data on items in cart or order.
     *
     * @return type
     */
    protected function _getItems($items)
    {
        try {
            $data = array();
            $configurableSkus = array();

            foreach($items as $item) {
                $_item = array();
                $_vars = array();
                if ($item->getProductType() == 'configurable') {
                    $parentIds[] = $item->getParentItemId();
                    $options = $item->getProduct()->getTypeInstance(true)->getOrderOptions($item->getProduct());
                    $_item['id'] = $options['simple_sku'];
                    $_item['title'] = $options['simple_name'];
                    $_vars = $this->_getVars($options);
                    $configurableSkus[] = $options['simple_sku'];
                } elseif (!in_array($item->getSku(),$configurableSkus)) {
                    $_item['id'] = $item->getSku();
                    $_item['title'] = $item->getName();
                } else {
                    $_item['id'] = null;
                }

                if ($_item['id']) {
                    if (get_class($item) == 'Mage_Sales_Model_Order_Item' ) {
                        $_item['qty'] = intval($item->getQtyOrdered());
                    } else {
                        $_item['qty'] = intval($item->getQty());
                    }

                    $_item['url'] = $item->getProduct()->getProductUrl();
                    $_item['price'] = Mage::helper('sailthruemail')->formatAmount($item->getProduct()->getPrice());

                    if ($_vars) {
                        $_item['vars'] = $_vars;
                    }

                    if ($tags = $this->_getTags($item->getProductId())) {
                        $_item['tags'] = $tags;
                    }

                    $data[] = $_item;
                }
            }
            return $data;
        } catch (Exception $e) {
             Mage::logException($e);
            return false;
        }
    }

    /**
     * Get order adjustments
     * @param Mage_Sales_Model_Order $order
     * @return array
     */
    protected function _getAdjustments(Mage_Sales_Model_Order $order)
    {
{'fraction_non_alphanumeric': 0.12892240330819751, 'fraction_numerical': 0.0008108327252087894, 'mean_word_length': 1.56744379683597, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 109, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2556314', 'n_tokens_mistral': 3335, 'n_tokens_neox': 3145, 'n_words': 718}