Skip to content
This repository has been archived by the owner on May 24, 2021. It is now read-only.

[Work in progress]-DoliWoo debug and improvement #88

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
1 change: 1 addition & 0 deletions doliwoo.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ public function add_integration( $integrations ) {
* @return void
*/
public function reschedule_import_products() {
$this->settings = new Doliwoo_WC_Integration();
$delay = $this->settings->delay_update;
wp_clear_scheduled_hook( 'import_products' );
wp_schedule_event( time(), $delay, 'import_products' );
Expand Down
147 changes: 136 additions & 11 deletions includes/class-dolibarr.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,22 @@ public function dolibarr_create_order() {
* We use non WSDL mode to workaround Dolibarr broken declaration marking all the fields as required
* when they're not.
*/

// if no synchronization order with Dolibarr
if ( $this->settings->dolibarr_order_sync === 'no' ) {
// Do nothing
return ;
}


try {
$soap_client = new SoapClient(
null,
array(
'location' => $this->ws_endpoint . self::ORDER_ENDPOINT,
'uri' => 'http://www.dolibar.org/ns/',
'wsdl_cache' => 0,
'trace' => 1,
)
);
} catch ( SoapFault $exception ) {
Expand All @@ -108,14 +118,25 @@ public function dolibarr_create_order() {
$order->thirdparty_id = $thirdparty_id;
} else {
if ( 0 === intval( get_user_meta( $user_id, 'billing_company', true ) ) ) {
if ( wp_verify_nonce( 'woocommerce-cart' ) ) {
// CMOINON (OPEN-DSI) Correctif
//if ( wp_verify_nonce( 'woocommerce-cart' ) ) {

// Suppression de lal vérification => contrôle à revoir dans un deuxième temps
/* if ( isset( $_POST['_wpnonce'] ) && wp_verify_nonce( $_POST['_wpnonce'] , 'woocommerce-process_checkout')) {
$billing_company = $_POST['billing_company'];
} else {
// TODO: fail message?
$this->logger->add( 'doliwoo', 'Failed to verify nonce' );
exit;
}
}*/

// if it is not a company, billing_company = first_name + last_name ( as Dolibarr)
if (! $_POST['billing_company']) {
$billing_company = $_POST['billing_first_name'] . ' ' . $_POST['billing_last_name'];
} else {
$billing_company = $_POST['billing_company'];
}

update_user_meta( $user_id, 'billing_company', $billing_company );
}
$this->dolibarr_create_thirdparty_if_not_exists( $user_id );
Expand All @@ -125,7 +146,7 @@ public function dolibarr_create_order() {
$order->status = 1;

$this->create_order_lines( $order );

try {
$result = $soap_client->createOrder( $this->ws_auth, $order );
} catch ( SoapFault $exception ) {
Expand Down Expand Up @@ -159,13 +180,24 @@ public function dolibarr_create_order() {
private function dolibarr_create_thirdparty_if_not_exists(
$user_id
) {

$result = $this->dolibarr_thirdparty_exists( $user_id );

if ( null === $result ) {
// Does not exist, create it
$result = $this->dolibarr_create_thirdparty( $user_id );
update_user_meta( $user_id, 'dolibarr_id', $result['id'] );
} else {
// find in Dolibarr
// if same email => get the id of Dolibarr
if ($result['thirdparty']->email == $_POST['billing_email']) {
update_user_meta( $user_id, 'dolibarr_id', $result['thirdparty']->id );
// else create a new thirdparty in Dolibarr
} else {
$result = $this->dolibarr_create_thirdparty( $user_id );
update_user_meta( $user_id, 'dolibarr_id', $result['id'] );
}
}
update_user_meta( $user_id, 'dolibarr_id', $result['id'] );
}

/**
Expand All @@ -176,6 +208,7 @@ private function dolibarr_create_thirdparty_if_not_exists(
* @return int $result Array with the request results if it succeeds, null if there's an error
*/
private function dolibarr_thirdparty_exists( $user_id ) {

try {
$soap_client = new SoapClient(
$this->ws_endpoint . self::THIRDPARTY_ENDPOINT . self::WSDL_MODE
Expand Down Expand Up @@ -335,6 +368,7 @@ private function create_order_lines( $order ) {
* @return void
*/
public function dolibarr_import_products() {

try {
$soap_client = new SoapClient(
$this->ws_endpoint . self::PRODUCT_ENDPOINT . self::WSDL_MODE
Expand Down Expand Up @@ -449,9 +483,41 @@ private function update_product_attributes( $dolibarr_product, $post_id ) {
add_post_meta( $post_id, 'dolibarr_type', $dolibarr_product->type, true );
update_post_meta( $post_id, '_sku', $dolibarr_product->ref );
update_post_meta( $post_id, '_purchase_note', $dolibarr_product->note );
update_post_meta( $post_id, '_regular_price', $dolibarr_product->price_net );
update_post_meta( $post_id, '_sale_price', $dolibarr_product->price_net );
update_post_meta( $post_id, '_price', $dolibarr_product->price_net );


// Update in July 2016 CMOINON =>
// if in woocommerce price not include tax
// => if not price_list => price_net else multiprice[level_price]
// else woocommerce price include tax
// => if not price_list => price else multiprice_ttc[level_price]

$level = $this->settings->dolibarr_price_level ;

if ($level > count($dolibarr_product->multiprices)) {
$level = '';
}
if ( 'no' === get_option( 'woocommerce_prices_include_tax' ) ) {
if ( '' === $level) {
update_post_meta( $post_id, '_regular_price', $dolibarr_product->price_net );
update_post_meta( $post_id, '_sale_price', $dolibarr_product->price_net );
update_post_meta( $post_id, '_price', $dolibarr_product->price_net );
} else {
update_post_meta( $post_id, '_regular_price', $dolibarr_product->multiprices[$level-1] );
update_post_meta( $post_id, '_sale_price', $dolibarr_product->multiprices[$level-1] );
update_post_meta( $post_id, '_price', $dolibarr_product->multiprices[$level-1] );
}
} else {
if ( '' === $level) {
update_post_meta( $post_id, '_regular_price', $dolibarr_product->price );
update_post_meta( $post_id, '_sale_price', $dolibarr_product->price );
update_post_meta( $post_id, '_price', $dolibarr_product->price );
} else {
update_post_meta( $post_id, '_regular_price', $dolibarr_product->multiprices_ttc[$level-1] );
update_post_meta( $post_id, '_sale_price', $dolibarr_product->multiprices_ttc[$level-1] );
update_post_meta( $post_id, '_price', $dolibarr_product->multiprices_ttc[$level-1] );
}
}

update_post_meta( $post_id, '_visibility', 'visible' );
update_post_meta(
$post_id,
Expand All @@ -469,10 +535,16 @@ private function update_product_attributes( $dolibarr_product, $post_id ) {
}
}

// Product images management
if ( ! empty( $dolibarr_product->images ) ) {
$this->import_product_images( $dolibarr_product, $post_id );
}
// If synchronize images with Dolibarr
if ( 'yes' === $this->settings->dolibarr_images_sync ) {
// Check if the product has images
$the_product = $this->dolibarr_images_exit($dolibarr_product->id);

// Product images management
if ( $the_product && ! empty($the_product->images) ) {
$this->import_product_images( $the_product, $post_id );
}
}

// Cleanup
wc_delete_product_transients( $post_id );
Expand Down Expand Up @@ -520,6 +592,50 @@ private function delete_post_attachments( $post_id ) {
}
}

/**
* Check if in Dolibar there is an images => call Webservice
*
* @param $dolibar->product_id
*/

private function dolibarr_images_exit($product_id) {
try {
$soap_client = new SoapClient(
$this->ws_endpoint . self::PRODUCT_ENDPOINT . self::WSDL_MODE
);
} catch ( SoapFault $exception ) {
$this->logger->add( 'doliwoo', $exception->getMessage() );

// Do nothing.
return null;
}

// Get the product
try {
$result = $soap_client->getProductOrService(
$this->ws_auth,
$product_id
);
} catch ( SoapFault $exception ) {
$this->logger->add(
'doliwoo',
'getProductOrService request: ' . $exception->getMessage()
);
}

if ( ! ( 'OK' === $result['result']->result_code ) ) {
$this->logger->add(
'doliwoo',
'getProductOrService response: ' . $result['result']->result_code . ': ' . $result['result']->result_label
);

// Do nothing
return false;
} else {
return $result['product'] ;
}
}

/**
* Webservice calls to get the product's images
*
Expand All @@ -529,6 +645,15 @@ private function delete_post_attachments( $post_id ) {
* @return int[] Attachment IDs
*/
private function get_product_image( $dolibarr_product, $post_id ) {

// Ajout CMOINON
// Need to require these files
if ( !function_exists('media_handle_upload') ) {
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php') ;
require_once(ABSPATH . "wp-admin" . '/includes/media.php');
}

try {
$soap_client = new SoapClient(
$this->ws_endpoint . self::OTHER_ENDPOINT . self::WSDL_MODE
Expand Down
39 changes: 38 additions & 1 deletion includes/class-wc-integration.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ class Doliwoo_WC_Integration extends WC_Integration {
/** @var int[] The distant Dolibarr version */
private $dolibarr_version;

/** @var boolean Parameter to disable order synchronization */
public $dolibarr_order_sync;

/** @var boolean Parameter to disable images load */
public $dolibarr_images_sync;

/** @var int Price level to load */
public $dolibarr_price_level;

/**
* Init and hook in the integration.
*/
Expand All @@ -85,6 +94,9 @@ public function __construct() {
$this->dolibarr_entity = $this->get_option( 'dolibarr_entity' );
$this->dolibarr_category_id = $this->get_option( 'dolibarr_category_id' );
$this->dolibarr_generic_id = $this->get_option( 'dolibarr_generic_id' );
$this->dolibarr_order_sync = $this->get_option( 'dolibarr_order_sync' );
$this->dolibarr_images_sync = $this->get_option( 'dolibarr_images_sync' );
$this->dolibarr_price_level = $this->get_option( 'dolibarr_price_level' );

// Actions
add_action(
Expand Down Expand Up @@ -176,6 +188,31 @@ public function init_form_fields() {
'desc_tip' => false,
'default' => '',
),

'dolibarr_price_level' => array(
'title' => __( 'Price Level for price', 'doliwoo' ),
'description' => __( 'The level of price to load for the product. If no price list, leave blank.', 'doliwoo' ),
'type' => 'text',
'desc_tip' => false,
'default' => '',
),

'dolibarr_order_sync' => array(
'title' => __( 'Order synchronisation', 'doliwoo' ),
'desc' => __( 'Dolibarr order synchronisation', 'doliwoo' ),
'description' => __( 'If is checked the orders will be synchronized with Dolibarr ', 'doliwoo' ),
'type' => 'checkbox',
'desc_tip' => false,
'default' => 'yes'
),
'dolibarr_images_sync' => array(
'title' => __( 'Images synchronisation', 'doliwoo' ),
'desc' => __( 'Dolibarr images synchronisation', 'doliwoo' ),
'description' => __( 'If is checked the images of product in Dolibarr will be loaded in WooCommerce', 'doliwoo' ),
'type' => 'checkbox',
'desc_tip' => false,
'default' => 'yes'
),
'dolibarr_version' => array(
'title' => __( 'Dolibarr version', 'doliwoo' ),
'description' => __( 'If the webservice communication is OK, it displays your Dolibarr version', 'doliwoo' ),
Expand Down Expand Up @@ -218,7 +255,7 @@ protected function generate_info_html( $key, $data ) {
?>
<tr valign="top">
<th scope="row" class="titledesc">
<label for="<?php echo esc_attr( $field ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label>
<label for="<?php echo esc_attr( $field ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label>
<?php esc_html_e( $this->get_tooltip_html( $data ) ); ?>
</th>
<td class="forminp">
Expand Down
9 changes: 6 additions & 3 deletions includes/class-wc-tax.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,12 @@ public function get_tax_class( $tax_rate ) {
foreach ( $tax_classes as $class ) {
$rates = $this->get_rates( $class );
$rates_values = array_values( $rates );
if ( floatval( $rates_values[0]['rate'] ) === $tax_rate ) {
// Use the first class found
return $class;
// Ajout CMO - Juillet 2016
if(array_key_exists(0, $rates_values)){
if ( floatval( $rates_values[0]['rate'] ) === $tax_rate ) {
// Use the first class found
return $class;
}
}
}

Expand Down