1
0
mirror of synced 2025-02-22 18:03:14 +03:00
woocommerce-module/src/include/class-wc-retailcrm-orders.php

423 lines
15 KiB
PHP
Raw Normal View History

2017-06-07 16:29:57 +03:00
<?php
/**
* RetailCRM Integration.
2017-06-07 16:29:57 +03:00
*
* @package WC_Retailcrm_Orders
* @category Integration
* @author RetailCRM
2017-06-07 16:29:57 +03:00
*/
if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) :
/**
* Class WC_Retailcrm_Orders
*/
class WC_Retailcrm_Orders
{
protected $retailcrm_settings;
protected $retailcrm;
private $order = array();
private $payment = array();
2017-06-07 16:29:57 +03:00
public function __construct($retailcrm = false)
{
$this->retailcrm_settings = get_option(WC_Retailcrm_Base::$option_key);
$this->retailcrm = $retailcrm;
2017-06-07 16:29:57 +03:00
}
/**
* Upload orders to CRM
*
2018-05-28 12:56:19 +03:00
* @param bool $withCustomers
* @param array $include
* @return array $uploadOrders | null
2017-06-07 16:29:57 +03:00
*/
2018-05-28 12:56:19 +03:00
public function ordersUpload($include = array(), $withCustomers = false)
2017-06-07 16:29:57 +03:00
{
if (!$this->retailcrm) {
return null;
}
2017-06-07 16:29:57 +03:00
$orders = get_posts(array(
'numberposts' => -1,
'post_type' => wc_get_order_types('view-orders'),
2018-05-28 12:56:19 +03:00
'post_status' => array_keys(wc_get_order_statuses()),
'include' => $include
2017-06-07 16:29:57 +03:00
));
$orders_data = array();
foreach ($orders as $data_order) {
$order = wc_get_order($data_order->ID);
$this->processOrder($order);
2017-06-07 16:29:57 +03:00
$customer = $order->get_user();
2018-05-28 12:56:19 +03:00
$customers = array();
2017-06-07 16:29:57 +03:00
if ($customer != false) {
$this->order['customer']['externalId'] = $customer->get('ID');
2018-05-28 12:56:19 +03:00
if ($withCustomers === true) {
$customers[] = $customer->get('ID');
}
2017-06-07 16:29:57 +03:00
}
$orders_data[] = $this->order;
2017-06-07 16:29:57 +03:00
}
2018-05-28 12:56:19 +03:00
if ($withCustomers === true && !empty($customers)) {
if (!class_exists('WC_Retailcrm_Customers')) {
include_once(WC_Retailcrm_Base::checkCustomFile('customers'));
}
$retailcrmCustomer = new WC_Retailcrm_Customers($this->retailcrm);
$retailcrmCustomer->customersUpload($customers);
}
2017-06-07 16:29:57 +03:00
$uploadOrders = array_chunk($orders_data, 50);
foreach ($uploadOrders as $uploadOrder) {
$this->retailcrm->ordersUpload($uploadOrder);
2018-05-28 12:56:19 +03:00
time_nanosleep(0, 250000000);
2017-06-07 16:29:57 +03:00
}
return $uploadOrders;
2017-06-07 16:29:57 +03:00
}
/**
* Create order
*
* @param $order_id
*
2018-05-28 12:56:19 +03:00
* @return mixed
2017-06-07 16:29:57 +03:00
*/
public function orderCreate($order_id)
{
if (!$this->retailcrm) {
return null;
}
2017-06-07 16:29:57 +03:00
$order = wc_get_order($order_id);
$this->processOrder($order);
2017-06-07 16:29:57 +03:00
$customer = $order->get_user();
if ($customer != false) {
$search = $this->retailcrm->customersGet($customer->get('ID'));
if (!$search->isSuccessful()) {
$customer_data = array(
'externalId' => $customer->get('ID'),
'firstName' => $this->order['firstName'],
'lastName' => $this->order['lastName'],
'email' => $this->order['email']
2017-06-07 16:29:57 +03:00
);
$this->retailcrm->customersCreate($customer_data);
} else {
$this->order['customer']['externalId'] = $search['customer']['externalId'];
2017-06-07 16:29:57 +03:00
}
}
2017-08-24 08:40:05 +03:00
$this->retailcrm->ordersCreate($this->order);
2017-06-07 16:29:57 +03:00
return $order;
2017-06-07 16:29:57 +03:00
}
2017-08-24 08:40:05 +03:00
2017-06-07 16:29:57 +03:00
/**
* Edit order in CRM
2017-06-07 16:29:57 +03:00
*
* @param int $order_id
2017-06-07 16:29:57 +03:00
*
* @return WC_Order $order | null
2017-06-07 16:29:57 +03:00
*/
public function updateOrder($order_id)
{
if (!$this->retailcrm) {
return null;
}
$order = wc_get_order($order_id);
$this->processOrder($order, true);
if ($this->retailcrm_settings['api_version'] == 'v4') {
$this->order['paymentType'] = $this->retailcrm_settings[$order->get_payment_method()];
}
2017-08-24 08:40:05 +03:00
$response = $this->retailcrm->ordersEdit($this->order);
if ($response->isSuccessful() && $this->retailcrm_settings['api_version'] == 'v5') {
$this->payment = $this->orderUpdatePaymentType($order);
}
2017-08-24 08:40:05 +03:00
return $order;
2017-06-07 16:29:57 +03:00
}
/**
* Update order payment type
*
* @param WC_Order $order
2017-06-07 16:29:57 +03:00
*
* @return null | array $payment
2017-06-07 16:29:57 +03:00
*/
protected function orderUpdatePaymentType($order)
{
if (!isset($this->retailcrm_settings[$order->get_payment_method()])) {
return null;
}
2017-06-07 16:29:57 +03:00
$response = $this->retailcrm->ordersGet($order->get_id());
if ($response->isSuccessful()) {
$retailcrmOrder = $response['order'];
foreach ($retailcrmOrder['payments'] as $payment_data) {
if ($payment_data['externalId'] == $order->get_id()) {
$payment = $payment_data;
}
}
2017-06-07 16:29:57 +03:00
}
if (isset($payment) && $payment['type'] != $this->retailcrm_settings[$order->get_payment_method()]) {
$response = $this->retailcrm->ordersPaymentDelete($payment['id']);
2017-06-07 16:29:57 +03:00
if ($response->isSuccessful()) {
$payment = $this->createPayment($order);
2017-06-07 16:29:57 +03:00
return $payment;
}
2017-06-07 16:29:57 +03:00
}
2017-08-24 08:40:05 +03:00
return null;
2017-06-07 16:29:57 +03:00
}
/**
* Get order data
*
* @param WC_Order $order
*
* @return array $order_data_arr
*/
protected function getOrderData($order) {
$order_data_arr = array();
$order_info = $order->get_data();
$order_data_arr['id'] = $order_info['id'];
$order_data_arr['payment_method'] = $order->get_payment_method();
$order_data_arr['date'] = $order_info['date_created']->date('Y-m-d H:i:s');
$order_data_arr['discount_total'] = $order_info['discount_total'];
$order_data_arr['discount_tax'] = $order_info['discount_tax'];
$order_data_arr['customer_comment'] = $order->get_customer_note();
return $order_data_arr;
}
/**
* process to combine order data
*
* @param WC_Order $order
* @param boolean $update
*
* @return void
*/
protected function processOrder($order, $update = false)
2017-06-07 16:29:57 +03:00
{
if (!$order instanceof WC_Order) {
2017-06-07 16:29:57 +03:00
return;
}
$order_data_info = $this->getOrderData($order);
2017-06-07 16:29:57 +03:00
$order_data = array();
$order_data['externalId'] = $order_data_info['id'];
2017-06-07 16:29:57 +03:00
$order_data['number'] = $order->get_order_number();
$order_data['createdAt'] = trim($order_data_info['date']);
$order_data['customerComment'] = $order_data_info['customer_comment'];
2017-06-07 16:29:57 +03:00
if (!empty($order_data_info['payment_method'])
&& !empty($this->retailcrm_settings[$order_data_info['payment_method']])
&& $this->retailcrm_settings['api_version'] != 'v5'
) {
$order_data['paymentType'] = $this->retailcrm_settings[$order_data_info['payment_method']];
2017-06-07 16:29:57 +03:00
}
if ($order->get_items('shipping')) {
$shippings = $order->get_items( 'shipping' );
$shipping = reset($shippings);
2017-06-07 16:29:57 +03:00
$shipping_code = explode(':', $shipping['method_id']);
if (isset($this->retailcrm_settings[$shipping['method_id']])) {
$shipping_method = $shipping['method_id'];
} else {
$shipping_method = $shipping_code[0];
}
2017-06-07 16:29:57 +03:00
$shipping_cost = $shipping['cost'];
if (!empty($shipping_method) && !empty($this->retailcrm_settings[$shipping_method])) {
$order_data['delivery']['code'] = $this->retailcrm_settings[$shipping_method];
}
if (!empty($shipping_cost)) {
$order_data['delivery']['cost'] = $shipping_cost;
}
}
if ($this->retailcrm_settings['api_version'] != 'v5' && $order->is_paid()) {
2017-06-07 16:29:57 +03:00
$order_data['paymentStatus'] = 'paid';
}
$status = $order->get_status();
$order_data['status'] = $this->retailcrm_settings[$status];
2017-08-24 08:40:05 +03:00
$user_data_billing = $order->get_address('billing');
if (!empty($user_data_billing)) {
2017-08-24 08:40:05 +03:00
if (!empty($user_data_billing['phone'])) $order_data['phone'] = $user_data_billing['phone'];
if (!empty($user_data_billing['email'])) $order_data['email'] = $user_data_billing['email'];
2017-08-24 08:40:05 +03:00
if (!empty($user_data_billing['first_name'])) $order_data['firstName'] = $user_data_billing['first_name'];
if (!empty($user_data_billing['last_name'])) $order_data['lastName'] = $user_data_billing['last_name'];
if (!empty($user_data_billing['postcode'])) $order_data['delivery']['address']['index'] = $user_data_billing['postcode'];
if (!empty($user_data_billing['city'])) $order_data['delivery']['address']['city'] = $user_data_billing['city'];
if (!empty($user_data_billing['state'])) $order_data['delivery']['address']['region'] = $user_data_billing['state'];
2017-12-04 12:04:03 +03:00
if (!empty($user_data_billing['country'])) $order_data['countryIso'] = $user_data_billing['country'];
}
2017-08-03 08:36:36 +02:00
$user_data = $order->get_address('shipping');
2017-06-07 16:29:57 +03:00
if (!empty($user_data)) {
2017-08-24 08:40:05 +03:00
if (!empty($user_data['phone'])) $order_data['phone'] = $user_data['phone'];
if (!empty($user_data['email'])) $order_data['email'] = $user_data['email'];
2017-06-07 16:29:57 +03:00
if (!empty($user_data['first_name'])) $order_data['firstName'] = $user_data['first_name'];
2017-08-24 08:40:05 +03:00
if (!empty($user_data['last_name'])) $order_data['lastName'] = $user_data['last_name'];
2017-06-07 16:29:57 +03:00
if (!empty($user_data['postcode'])) $order_data['delivery']['address']['index'] = $user_data['postcode'];
if (!empty($user_data['city'])) $order_data['delivery']['address']['city'] = $user_data['city'];
if (!empty($user_data['state'])) $order_data['delivery']['address']['region'] = $user_data['state'];
2017-12-04 12:04:03 +03:00
if (!empty($user_data['country'])) $order_data['countryIso'] = $user_data['country'];
2017-06-07 16:29:57 +03:00
}
2017-08-24 08:40:05 +03:00
$order_data['delivery']['address']['text'] = sprintf(
"%s %s %s %s %s",
2017-08-24 08:40:05 +03:00
!empty($user_data_billing['postcode']) ? $user_data_billing['postcode'] : $user_data['postcode'],
!empty($user_data_billing['state']) ? $user_data_billing['state'] : $user_data['state'],
2017-08-24 08:40:05 +03:00
!empty($user_data_billing['city']) ? $user_data_billing['city'] : $user_data['city'],
!empty($user_data_billing['address_1']) ? $user_data_billing['address_1'] : $user_data['address_1'],
!empty($user_data_billing['address_2']) ? $user_data_billing['address_2'] : $user_data['address_2']
);
2017-06-07 16:29:57 +03:00
$order_items = array();
foreach ($order->get_items() as $item) {
$uid = ($item['variation_id'] > 0) ? $item['variation_id'] : $item['product_id'] ;
2018-02-02 12:13:45 +03:00
$price = round(($item['line_subtotal'] / $item->get_quantity()) + ($item['line_subtotal_tax'] / $item->get_quantity()), 2);
2017-08-24 08:40:05 +03:00
$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;
$price_item = $product_price + $product_tax;
$discount_price = $price - $price_item;
$order_item = array(
'offer' => array('externalId' => $uid),
'productName' => $item['name'],
'initialPrice' => (float)$price,
'quantity' => $item['qty'],
);
if ($this->retailcrm_settings['api_version'] == 'v5' && round($discount_price, 2)) {
$order_item['discountManualAmount'] = round($discount_price, 2);
} elseif ($this->retailcrm_settings['api_version'] == 'v4' && round($discount_price, 2)) {
$order_item['discount'] = round($discount_price, 2);
2017-06-07 16:29:57 +03:00
}
$order_items[] = $order_item;
}
$order_data['items'] = $order_items;
if ($this->retailcrm_settings['api_version'] == 'v5') {
$payment = array(
'amount' => $order->get_total(),
'externalId' => $order->get_id()
);
$payment['order'] = array(
'externalId' => $order->get_id()
);
if (!empty($order_data_info['payment_method']) && !empty($this->retailcrm_settings[$order_data_info['payment_method']])) {
$payment['type'] = $this->retailcrm_settings[$order_data_info['payment_method']];
}
if ($order->is_paid()) {
$payment['status'] = 'paid';
}
if ($order->get_date_paid()) {
$pay_date = $order->get_date_paid();
$payment['paidAt'] = trim($pay_date->date('Y-m-d H:i:s'));
}
if (!$update) {
$order_data['payments'][] = $payment;
}
}
$this->order = apply_filters('retailcrm_process_order', $order_data);
2017-06-07 16:29:57 +03:00
}
/**
* Create payment in CRM
*
* @param WC_Order $order
* @param int $order_id
*
* @return array $payment
*/
protected function createPayment($order)
{
$payment = array(
'amount' => $order->get_total(),
'externalId' => $order->get_id()
);
$payment['order'] = array(
'externalId' => $order->get_id()
);
if (isset($this->retailcrm_settings[$order->get_payment_method()])) {
$payment['type'] = $this->retailcrm_settings[$order->get_payment_method()];
}
if ($order->is_paid()) {
$payment['status'] = 'paid';
}
if ($order->get_date_paid()) {
$pay_date = $order->get_date_paid();
$payment['paidAt'] = trim($pay_date->date('Y-m-d H:i:s'));
}
2017-08-24 08:40:05 +03:00
$this->retailcrm->ordersPaymentCreate($payment);
return $payment;
}
/**
* @return array
*/
public function getOrder()
{
return $this->order;
}
/**
* @return array
*/
public function getPayment()
{
return $this->payment;
}
2017-06-07 16:29:57 +03:00
}
endif;