%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /var/www/projetos/takthua.com.br/wp-content/plugins/wc-crm-integration/includes/
Upload File :
Create Path :
Current File : /var/www/projetos/takthua.com.br/wp-content/plugins/wc-crm-integration/includes/class-crm-order.php

<?php

require_once __DIR__ . '/class-integra-client.php';

/**
 * The file that defines the core plugin class
 *
 * A class definition that includes attributes and functions used across both the
 * public-facing side of the site and the admin area.
 *
 * @link       https://theprogramer.com.br
 * @since      1.0.0
 *
 * @package    Wc_Crm_Integration
 * @subpackage Wc_Crm_Integration/includes
 */

/**
 * The Customer CRM class.
 *
 * @since      1.0.0
 * @package    Wc_Crm_Integration
 * @subpackage Wc_Crm_Integration/includes
 * @author     Thiago Miranda <theprogramer@theprogramer.com.br>
 */
class CRM_Order extends Integra_Client {

    private $id;
    private $id_api;
    private $status;
    private $customer;
    private $weight;
    private $shipping_total;
    private $date_created;
    private $items;
    private $shipping_address;
    private $shipping_method;
    private $billing_total;
    // Presenteado
    private $payment_method;

    public function get_id() {
        return $this->id;
    }

    public function get_id_api() {
        return $this->id_api;
    }

    public function get_status() {
        return $this->status;
    }

    public function set_status($status) {
        $_status = 0;
        switch ($status) {
            case 1:
                // Em Orçamento
                break;
            case 2:
                // Aguardando Aprovação
                // $_status = "wc-on-hold";
                break;
            case 3:
                // Aprovado
                break;
            case 6:
                // Pendente
                // $_status = "wc-pending";
                break;
            case 7:
                // Remetido
                break;
            case 9:
                // Cancelado
                $_status = "wc-cancelled";
                break;
            default:
                $_status = "";
        }
        $this->status = $_status;
    }

    public function get_customer() {
        return $this->customer;
    }

    public function get_weight_total() {
        return $this->weight_total;
    }

    public function get_shipping_total() {
        return $this->shipping_total;
    }

    public function get_date_created() {
        return $this->date_created;
    }

    public function get_items() {
        return $this->items;
    }

    public function get_shipping_address() {
        return $this->shipping_address;
    }

    public function get_shipping_method() {
        return $this->shipping_method;
    }

    public function get_billing_total() {
        return $this->billing_total;
    }

    public function get_payment_method() {
        return $this->payment_method;
    }

	/**
	 * Initialize class.
	 *
	 * @since    1.0.0
	 */
	public function __construct() {
        parent::__construct();
    }

    public function init_from_id($id = '') {
        $this->parse($this->call_api('GET', '/api/loja/pedido/' . $id, null));
    }

    public function to_crm() {
        $order = wc_get_order( $this->get_id() );
        $customer = new WC_Customer($order->get_customer_id());
        $crm_order = array(
            "IdPedidoLoja" => (int)$order->get_id(),
            "TotalPeso" => 10, // function TotalPeso
            "ValorFrete" => (float)$order->get_shipping_total(),
            "DataPedido" => $order->get_date_created()->date("Y-m-d"),
            "IdClienteApi" => (int)get_user_meta( $customer->get_id(),  'integra_crm_id', true ),
            "Itens" => array(),
            "IdEnderecoEntrega" => (int)get_user_meta( $customer->get_id(),  'integra_crm_address_id', true ),
            "IdTipoEntrega" => 1,
            "TotalPedido" => (float)$order->get_total(),
            "Presenteado" => array(),
            "FormaPagamento" => array(
                array(
                    "IdFormaPagamento" => 27,
                    "Valor" => (float)$order->get_total(),
                    "IdCondicao" => 1,
                    "Data" => $order->get_date_paid()->date("Y-m-d")
                )
            )
        );
        foreach ( $order->get_items() as $item_id => $item ) {
            $product = wc_get_product($item->get_product_id());
            array_push(
                $crm_order["Itens"],
                array(
                    "IdProduto" => (int)$product->get_sku(),
                    "Quantidade" => $item->get_quantity(),
                    "Preco" => (float)$product->get_price(),
                    "PrecoPor" => (float)$product->get_sale_price(),
                    "TipoDesconto" => "",
                    "ValorDesconto" => 0.00,
                    "TotalItem" => (float)$item->get_subtotal()
                )
            );
        }
        if ($order->get_shipping_first_name() !== "") {
            $crm_order["Presenteado"] = array(
                "Nome" => $order->get_shipping_first_name() . " " . $order->get_shipping_last_name(),
                "Cep" => $customer->get_shipping_postcode(),
                "Endereco" => $order->get_shipping_address_1(),
                "Complemento" => $order->get_shipping_address_2(),
                "Bairro" => $order->get_meta( '_shipping_neighborhood' ),
                "Cidade" => $order->get_shipping_city(),
                "UF" => $order->get_shipping_state(),
                "DDD" => substr(preg_replace('/\D/', '', $customer->get_billing_phone()), 0, 2),
                "Telefone" => substr(preg_replace('/\D/', '', $customer->get_billing_phone()), 2),
                "DDDTelefoneOpcional" => "",
                "TelefoneOpcional" => "",
                "DDDCel" => "",
                "Celular" => "",
                "Email" => "",
                "PresenteadoPessoaJuridica" => false,
                "DataCadastro" => $order->get_date_created()->date("Y-m-d"),
                "Numero" => $order->get_meta( '_shipping_number' ),
                "Ramal" => "",
                "RamalOpcional" => "",
                "IdEnderecoLoja" => ""
            );
        }
        return $crm_order;
    }

    public function submit_to_crm() {
        $result = json_decode($this->submit('pedido', $this->to_crm()));
        if ($result->{"RealizadoComSucesso"}) {
            $order = wc_get_order( $this->get_id() );
            $order->add_order_note(__('Pedido enviado para o Integra'));
        } else {
            throw new Exception("Ocorreu um erro durante o cadastro. ERRO: #5740 <br/>" . $result . " - " . json_decode($this->to_crm()));
        }
    }

    public function init_from_order($order_id) {
        $order = wc_get_order( $order_id );
        $customer = new WC_Customer($order->get_customer_id());
        $this->id = $order_id;
        $this->id_api = null;
        $this->status = null;
        $this->customer = null;
        $this->weight = 0;
        $this->shipping_total = $order->get_shipping_total();
        $this->date_created = $order->get_date_created();
        $this->items = array();
        $this->shipping_address = null;
        $this->shipping_method = null;
        $this->billing_total = null;
        // Presenteado
        $this->payment_method = null;
        return $this;
    }

    /**
     * Parse content
     */
    private function parse($content) {
        $content = json_decode($content, true);
        $this->id = $content['IdPedidoLoja'] ?? '';
        $this->id_api = $content['IdPedidoAPI'] ?? '';
        $this->status = $content['Situacao'] ?? '';
    }
}

Zerion Mini Shell 1.0