<commit_before><?php
class Billmate_Common_Model_Checkout_Order extends Varien_Object
{
    /**
     * @var null | Mage_Sales_Model_Order
     */
    protected $_order = null;

    /**
     * @var null|Mage_Sales_Model_Quote
     */
    protected $_quote = null;

    /**
     * @var array
     */
    protected $bmRequestData;

    /**
     * @var Billmate_Common_Helper_Data
     */
    protected $helper;


    public function __construct()
    {
        $this->helper = Mage::helper('billmatecommon');
    }

    /**
     * @param $quote Mage_Sales_Model_Quote
     *
     * @return bool|false|Mage_Sales_Model_Order
     */
    public function place($verifiedData, $isOrderValid, $status, $method, $quote = null)
    {
        if (is_null($quote)) {
            $quote = $this->getActualQuote();
        }

        /** @var  $quote Mage_Sales_Model_Quote */
        $orderModel = Mage::getModel('sales/order');
        $orderModel->load($quote->getId(), 'quote_id');
        if ($orderModel->getId()) {
            $this->_quote = $quote;
            $this->_order = $orderModel;
            return $orderModel;
        }

        if ($quote->getShippingAddress()->getFirstName() == null){
            if (array_key_exists('Customer', $verifiedData['data'])) {
                if (array_key_exists('Shipping', $verifiedData['data']['Customer'])) {
                    $quote->getShippingAddress()->setFirstName($verifiedData['data']['Customer']['Shipping']['firstname']);
                    $quote->getShippingAddress()->setLastName($verifiedData['data']['Customer']['Shipping']['lastname']);
                    $quote->getShippingAddress()->setStreet($verifiedData['data']['Customer']['Shipping']['street']);
                    $quote->getShippingAddress()->setCity($verifiedData['data']['Customer']['Shipping']['city']);
                    $quote->getShippingAddress()->setTelephone($verifiedData['data']['Customer']['Shipping']['phone']);
                }
                elseif (array_key_exists('Billing', $verifiedData['data']['Customer'])) {
                    $quote->getShippingAddress()->setFirstName($verifiedData['data']['Customer']['Billing']['firstname']);
                    $quote->getShippingAddress()->setLastName($verifiedData['data']['Customer']['Billing']['lastname']);
                    $quote->getShippingAddress()->setStreet($verifiedData['data']['Customer']['Billing']['street']);
                    $quote->getShippingAddress()->setCity($verifiedData['data']['Customer']['Billing']['city']);
                    $quote->getShippingAddress()->setTelephone($verifiedData['data']['Customer']['Billing']['phone']);
                }
                $quote->getShippingAddress()->save();
                $quote->save();
            }
        }

        $quote->collectTotals();
        $service = Mage::getModel('sales/service_quote', $quote);
        $service->submitAll();
        Mage::getSingleton('checkout/session')->setLastQuoteId($quote->getId())
            ->setLastSuccessQuoteId($quote->getId())
            ->clearHelperData();
        $order = $service->getOrder();
        if ($order) {
            Mage::getSingleton('checkout/session')->setLastOrderId($order->getId())
                ->setLastRealOrderId($order->getIncrementId());

        }
        $order->queueNewOrderEmail();
        $quote->setIsActive(false)->save();
        $paymentMethodStatus = $this->getHelper()->getBillmateCheckoutOrderStatus();
        $session = Mage::getSingleton('checkout/session');
        if ($order) {
            $this->_quote = $quote;
            $this->_order = $order;
            $order->addStatusHistoryComment(
                $this->getHelper()->__('Order processing completed' .
                    '<br/>Billmate status: %s  
                                <br/>' . 'Transaction ID: %s', [
                    $verifiedData['data']['status'],<commit_msg>[BUGFIX] fixed checking and correcting of shipping and billing address data before saving order<commit_after><?php
class Billmate_Common_Model_Checkout_Order extends Varien_Object
{
    /**
     * @var null | Mage_Sales_Model_Order
     */
    protected $_order = null;

    /**
     * @var null|Mage_Sales_Model_Quote
     */
    protected $_quote = null;

    /**
     * @var array
     */
    protected $bmRequestData;

    /**
     * @var Billmate_Common_Helper_Data
     */
    protected $helper;


    public function __construct()
    {
        $this->helper = Mage::helper('billmatecommon');
    }

    /**
     * @param $quote Mage_Sales_Model_Quote
     *
     * @return bool|false|Mage_Sales_Model_Order
     */
    public function place($verifiedData, $isOrderValid, $status, $method, $quote = null)
    {
        if (is_null($quote)) {
            $quote = $this->getActualQuote();
        }

        /** @var  $quote Mage_Sales_Model_Quote */
        $orderModel = Mage::getModel('sales/order');
        $orderModel->load($quote->getId(), 'quote_id');
        if ($orderModel->getId()) {
            $this->_quote = $quote;
            $this->_order = $orderModel;
            return $orderModel;
        }

        if ($quote->getShippingAddress()->getFirstname() == null ||
            $quote->getShippingAddress()->getLastname() == null ||
            $quote->getShippingAddress()->getStreet() == null ||
            $quote->getShippingAddress()->getCity() == null ||
            $quote->getShippingAddress()->getTelephone == null
        ){
            if (array_key_exists('Customer', $verifiedData)) {
                if (array_key_exists('Shipping', $verifiedData['Customer'])) {
                    $quote->getShippingAddress()->setFirstname($verifiedData['Customer']['Shipping']['firstname']);
                    $quote->getShippingAddress()->setLastname($verifiedData['Customer']['Shipping']['lastname']);
                    $quote->getShippingAddress()->setStreet($verifiedData['Customer']['Shipping']['street']);
                    $quote->getShippingAddress()->setCity($verifiedData['Customer']['Shipping']['city']);
                    $quote->getShippingAddress()->setTelephone($verifiedData['Customer']['Shipping']['phone']);
                }
                elseif (array_key_exists('Billing', $verifiedData['Customer'])) {
                    $quote->getShippingAddress()->setFirstname($verifiedData['Customer']['Billing']['firstname']);
                    $quote->getShippingAddress()->setLastname($verifiedData['Customer']['Billing']['lastname']);
                    $quote->getShippingAddress()->setStreet($verifiedData['Customer']['Billing']['street']);
                    $quote->getShippingAddress()->setCity($verifiedData['Customer']['Billing']['city']);
                    $quote->getShippingAddress()->setTelephone($verifiedData['Customer']['Billing']['phone']);
                }
            }
        }
        if ($quote->getBillingAddress()->getFirstname() == null ||
            $quote->getBillingAddress()->getLastname() == null ||
            $quote->getBillingAddress()->getStreet() == null ||
            $quote->getBillingAddress()->getCity() == null ||
            $quote->getBillingAddress()->getTelephone() == null ||
            $quote->getBillingAddress()->getCountryId() == null ||
            $quote->getBillingAddress()->getPostcode() == null
        ){
            if (array_key_exists('Customer', $verifiedData)) {
                if (array_key_exists('Billing', $verifiedData['Customer'])) {
                    $quote->getBillingAddress()->setFirstname($verifiedData['Customer']['Billing']['firstname']);
                    $quote->getBillingAddress()->setLastname($verifiedData['Customer']['Billing']['lastname']);
                    $quote->getBillingAddress()->setStreet($verifiedData['Customer']['Billing']['street']);
                    $quote->getBillingAddress()->setCity($verifiedData['Customer']['Billing']['city']);
                    $quote->getBillingAddress()->setTelephone($verifiedData['Customer']['Billing']['phone']);
                    $quote->getBillingAddress()->setPostcode($verifiedData['Customer']['Billing']['zip']);
                    $quote->getBillingAddress()->setCountryId($verifiedData['Customer']['Billing']['country']);
                }
                elseif (array_key_exists('Shipping', $verifiedData['Customer'])) {
                    $quote->getBillingAddress()->setFirstname($verifiedData['Customer']['Shipping']['firstname']);
                    $quote->getBillingAddress()->setLastname($verifiedData['Customer']['Shipping']['lastname']);
                    $quote->getBillingAddress()->setStreet($verifiedData['Customer']['Shipping']['street']);
                    $quote->getBillingAddress()->setCity($verifiedData['Customer']['Shipping']['city']);
                    $quote->getBillingAddress()->setTelephone($verifiedData['Customer']['Shipping']['phone']);
                    $quote->getBillingAddress()->setPostcode($verifiedData['Customer']['Shipping']['zip']);
                    $quote->getBillingAddress()->setCountryId($verifiedData['Customer']['Shipping']['country']);
                }
            }
        }

        $quote->collectTotals();
        $service = Mage::getModel('sales/service_quote', $quote);
        $service->submitAll();
        Mage::getSingleton('checkout/session')->setLastQuoteId($quote->getId())
            ->setLastSuccessQuoteId($quote->getId())
            ->clearHelperData();
        $order = $service->getOrder();
        if ($order) {
            Mage::getSingleton('checkout/session')->setLastOrderId($order->getId())
                ->setLastRealOrderId($order->getIncrementId());

        }
        $order->queueNewOrderEmail();
        $quote->setIsActive(false)->save();
        $paymentMethodStatus = $this->getHelper()->getBillmateCheckoutOrderStatus();
        $session = Mage::getSingleton('checkout/session');
        if ($order) {
            $this->_quote = $quote;
            $this->_order = $order;
            $order->addStatusHistoryComment(
                $this->getHelper()->__('Order processing completed' .
                    '<br/>Billmate status: %s  
                                <br/>' . 'Transaction ID: %s', [
                    $verifiedData['data']['status'],
{'fraction_non_alphanumeric': 0.17805685358255452, 'fraction_numerical': 0.0, 'mean_word_length': 2.6338875132649453, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 162, 'https://': 0, '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': '369323', 'n_tokens_mistral': 2765, 'n_tokens_neox': 2635, 'n_words': 302}