<?php

namespace ABR\Api;


use ABR\Controller\ConvertDate;
use ABR\Controller\ItemsDetails;
use ABR\Controller\ProductDetails;
use WC_Product;

class abrestan_CreateFactor
{
    public $order;
    public $order_id;


    public function create_factor($order_id)
    {
        $item_details = new ItemsDetails();
        $order = wc_get_order($order_id);
        $custmer_id = $order->get_customer_id();
        if ($custmer_id == 0) {
            return 0;
        }
        $order_currency = $order->get_currency();

        $order_items = $order->get_items();
        $order_data = $order->get_data();
        $products = array();
        foreach ($order_items as $order_item) {
            $product_details = new ProductDetails($order_item);
            $product_data = $order_item->get_data();
            $product_id = $product_details->get_product_id($product_data);


            if ($product_id != "error") {
                $product_name = $product_details->get_product_name($product_id);

                $product_quantity = $product_data['quantity'];

                $product_price = $item_details->covert_price($product_data['subtotal'] / $product_data['quantity'], $order_currency);
                $product_discount = $item_details->covert_price($product_data['subtotal'] - $product_data['total'], $order_currency);
                $product_tax = $item_details->covert_price($product_data['subtotal_tax'], $order_currency);
                $product_virtual = $product_details->virual($product_data["product_id"]);

                $product = array(
                    'name' => $product_name,
                    'w_code' => $product_id,
                    'amount' => $product_price,
                    'number' => $product_quantity,
                    'type' => $product_virtual,
                    'discount' => $product_discount,
                    'tax_complication' => $product_tax
                );
                array_push($products, $product);
            }
        }

        $jalali_date = new ConvertDate($order->get_date_created()->date("Y-m-d H:i:s"));
        $order_billing_data = $order->get_data()['billing'];

        $display_name = get_userdata($custmer_id);
        $transition = $item_details->covert_price($order->get_data()['shipping_total'], $order_currency);
        $company = get_option("abrestan_company")['companyCode'];

            $factor = array(
                "factor_date" => $jalali_date->convert_date(),
                "company_id" => $company,
                "customer" =>
                    [
                        "firstname" => $order_billing_data['first_name'],
                        "lastname" => $order_billing_data['last_name'],
                        "displayname" => $order_billing_data['first_name']." ".$order_billing_data['last_name'],
                        "w_code" => $custmer_id,
                        "type" => 1,
                        "tel" => $order_billing_data['phone'],
                        "email" => $order_billing_data['email'],
                        "city" => $order_billing_data['city'],
                        "address" => $order_billing_data['address_1'],
                        "postal_code" => $order_billing_data['postcode'],
                    ],
                "factor_items" => $products,
                "transition_cost" => $transition,

            );


            if (get_post_meta($order->get_id(), "factor_id_abr")) {
                $factor1 = get_post_meta($order->get_id(), "factor_id_abr");
                $factor_id = array(
                    "factor_id" => $factor1['0']
                );
                $factor = array_merge($factor, $factor_id);

            }
            return $factor;


    }

}