<commit_before><?php
/**
 * PHP version 5.3
 *
 * @category Integration
 * @author   RetailCRM <<EMAIL>>
 * @license  http://retailcrm.ru Proprietary
 * @link     http://retailcrm.ru
 * @see      http://help.retailcrm.ru
 */

/**
 * Class WC_Retailcrm_Order_Item
 */
class WC_Retailcrm_Order_Item extends WC_Retailcrm_Abstracts_Data
{
    protected $filter_name = 'order_item';

    /**
     * @var array order item
     */
    protected $data = array(
        'offer' => array(),
        'productName' => '',
        'initialPrice' => 0.00,
        'quantity' => 0
    );

    /**
     * @var array
     */
    protected $settings = array();

    /**
     * WC_Retailcrm_Order_Item constructor.
     *
     * @param array $settings
     */
    public function __construct($settings)
    {
        $this->settings = $settings;
    }

    /**
     * @param WC_Order_Item_Product $item
     *
     * @return self
     */
    public function build($item)
    {
        $price = $this->calculate_price($item);
        $discount_price = $this->calculate_discount($item, $price);

        $data['productName'] = $item['name'];
        $data['initialPrice'] = (float)$price;
        $data['quantity'] = $item['qty'];

        $this->set_data_fields($data);
        $this->set_offer($item);

        if ($this->settings['api_version'] == 'v5' && round($discount_price, 2)) {
            $this->set_data_field('discountManualAmount',round($discount_price, 2));
        } elseif ($this->settings['api_version'] == 'v4' && round($discount_price, 2)) {
            $this->set_data_field('discount', round($discount_price, 2));
        }

        return $this;
    }

    /**
     * @param WC_Order_Item_Product $item
     *
     * @return void
     */
    private function set_offer(WC_Order_Item_Product $item)
    {
        $uid = ($item['variation_id'] > 0) ? $item['variation_id'] : $item['product_id'] ;
        $offer = array('externalId' => $uid);

        if (isset($this->settings['bind_by_sku']) && $this->settings['bind_by_sku'] == WC_Retailcrm_Base::YES) {
            $offer['xmlId'] = $item->get_product()->get_sku();
        }

        $this->set_data_field('offer', $offer);
    }

    /**
     * @param WC_Order_Item_Product $item
     *
     * @return float
     */
    private function calculate_price(WC_Order_Item_Product $item)
    {
        $price = ($item['line_subtotal'] / $item->get_quantity()) + ($item['line_subtotal_tax'] / $item->get_quantity());

        return round($price, 2);
    }

    /**
     * @param WC_Order_Item_Product $item
     * @param $price
     *
     * @return float|int
     */
    private function calculate_discount(WC_Order_Item_Product $item, $price)
    {
        $product_price = $item->get_total() ? $item->get_total() / $item->get_quantity() : 0;
        $product_tax  = $item->get_total_tax() ? $item->get_total_tax() / $item->get_quantity() : 0;<commit_msg>fixed getting sku from product<commit_after><?php
/**
 * PHP version 5.3
 *
 * @category Integration
 * @author   RetailCRM <<EMAIL>>
 * @license  http://retailcrm.ru Proprietary
 * @link     http://retailcrm.ru
 * @see      http://help.retailcrm.ru
 */

/**
 * Class WC_Retailcrm_Order_Item
 */
class WC_Retailcrm_Order_Item extends WC_Retailcrm_Abstracts_Data
{
    protected $filter_name = 'order_item';

    /**
     * @var array order item
     */
    protected $data = array(
        'offer' => array(),
        'productName' => '',
        'initialPrice' => 0.00,
        'quantity' => 0
    );

    /**
     * @var array
     */
    protected $settings = array();

    /**
     * WC_Retailcrm_Order_Item constructor.
     *
     * @param array $settings
     */
    public function __construct($settings)
    {
        $this->settings = $settings;
    }

    /**
     * @param WC_Order_Item_Product $item
     *
     * @return self
     */
    public function build($item)
    {
        $price = $this->calculate_price($item);
        $discount_price = $this->calculate_discount($item, $price);

        $data['productName'] = $item['name'];
        $data['initialPrice'] = (float)$price;
        $data['quantity'] = $item['qty'];

        $this->set_data_fields($data);
        $this->set_offer($item);

        if ($this->settings['api_version'] == 'v5' && round($discount_price, 2)) {
            $this->set_data_field('discountManualAmount',round($discount_price, 2));
        } elseif ($this->settings['api_version'] == 'v4' && round($discount_price, 2)) {
            $this->set_data_field('discount', round($discount_price, 2));
        }

        return $this;
    }

    /**
     * @param WC_Order_Item_Product $item
     *
     * @return void
     */
    private function set_offer(WC_Order_Item_Product $item)
    {
        $uid = ($item['variation_id'] > 0) ? $item['variation_id'] : $item['product_id'] ;
        $offer = array('externalId' => $uid);

        $product = $item->get_product();

        if (!empty($product) &&
            isset($this->settings['bind_by_sku']) &&
            $this->settings['bind_by_sku'] == WC_Retailcrm_Base::YES
        ) {
            $offer['xmlId'] = $product->get_sku();
        }

        $this->set_data_field('offer', $offer);
    }

    /**
     * @param WC_Order_Item_Product $item
     *
     * @return float
     */
    private function calculate_price(WC_Order_Item_Product $item)
    {
        $price = ($item['line_subtotal'] / $item->get_quantity()) + ($item['line_subtotal_tax'] / $item->get_quantity());

        return round($price, 2);
    }

    /**
     * @param WC_Order_Item_Product $item
     * @param $price
     *
     * @return float|int
     */
    private function calculate_discount(WC_Order_Item_Product $item, $price)
    {
        $product_price = $item->get_total() ? $item->get_total() / $item->get_quantity() : 0;
        $product_tax  = $item->get_total_tax() ? $item->get_total_tax() / $item->get_quantity() : 0;
{'fraction_non_alphanumeric': 0.16773757186337504, 'fraction_numerical': 0.005410889414947582, 'mean_word_length': 2.529236276849642, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 63, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '671102', 'n_tokens_mistral': 2024, 'n_tokens_neox': 1943, 'n_words': 318}