diff --git a/modules/dolibarr/doli-invoice/action/class-doli-invoice-action.php b/modules/dolibarr/doli-invoice/action/class-doli-invoice-action.php index b493a23..b5bab85 100644 --- a/modules/dolibarr/doli-invoice/action/class-doli-invoice-action.php +++ b/modules/dolibarr/doli-invoice/action/class-doli-invoice-action.php @@ -40,7 +40,7 @@ class Doli_Invoice_Action { public function __construct() { add_action( 'init', array( $this, 'create_tmp_invoice_dir' ) ); - // add_action( 'admin_menu', array( $this, 'callback_admin_menu' ) ); + add_action( 'admin_menu', array( $this, 'callback_admin_menu' ) , 70 ); add_action( 'wps_payment_complete', array( $this, 'create_invoice' ), 20, 1 ); @@ -116,9 +116,16 @@ public function callback_admin_menu() { */ public function callback_add_menu_page() { if ( isset( $_GET['id'] ) ) { + // Single page. $id = ! empty( $_GET['id'] ) ? (int) $_GET['id'] : 0; - $invoice = Doli_Invoice::g()->get( array( 'id' => $id ), true ); + $doli_invoice = Request_Util::get( 'invoices/' . $id ); + $wp_invoice = Doli_Order::g()->get( array( 'schema' => true ), true ); + $wp_invoice = Doli_Order::g()->doli_to_wp( $doli_invoice, $wp_invoice, true ); + + $wp_invoice->data['datec'] = \eoxia\Date_Util::g()->fill_date( $wp_invoice->data['datec'] ); + + $third_party = Third_Party::g()->get( array( 'id' => $wp_invoice->data['parent_id'] ), true ); if ( ! empty( $this->metaboxes ) ) { foreach ( $this->metaboxes as $key => $metabox ) { @@ -126,18 +133,49 @@ public function callback_add_menu_page() { } } - View_Util::exec( 'wpshop', 'doli-invoice', 'single', array( 'invoice' => $invoice ) ); + View_Util::exec( 'wpshop', 'doli-invoice', 'single', array( + 'third_party' => $third_party, + 'invoice' => $wp_invoice, + ) ); } else { - $args = array( - 'post_type' => 'wps-doli-invoice', - 'posts_per_page' => -1, - 'post_status' => 'any', - ); + // Listing page. + // @todo: Doublon avec Class Doli Order display() ? + $per_page = get_user_meta( get_current_user_id(), Doli_Invoice::g()->option_per_page, true ); + + if ( empty( $per_page ) || 1 > $per_page ) { + $per_page = Doli_Invoice::g()->limit; + } - $count = count( get_posts( $args ) ); + $s = ! empty( $_GET['s'] ) ? sanitize_text_field( $_GET['s'] ) : ''; + + $count = Doli_Invoice::g()->search( $s, array(), true ); + $number_page = ceil( $count / $per_page ); + $current_page = isset( $_GET['current_page'] ) ? (int) $_GET['current_page'] : 1; + + $base_url = admin_url( 'admin.php?page=wps-invoice' ); + + $begin_url = $base_url . '¤t_page=1'; + $end_url = $base_url . '¤t_page=' . $number_page; + + $prev_url = $base_url . '¤t_page=' . ( $current_page - 1 ); + $next_url = $base_url . '¤t_page=' . ( $current_page + 1 ); + + if ( ! empty( $s ) ) { + $begin_url .= '&s=' . $s; + $end_url .= '&s=' . $s; + $prev_url .= '&s=' . $s; + $next_url .= '&s=' . $s; + } View_Util::exec( 'wpshop', 'doli-invoice', 'main', array( - 'count' => $count, + 'number_page' => $number_page, + 'current_page' => $current_page, + 'count' => $count, + 'begin_url' => $begin_url, + 'end_url' => $end_url, + 'prev_url' => $prev_url, + 'next_url' => $next_url, + 's' => $s, ) ); } } diff --git a/modules/dolibarr/doli-invoice/class/class-doli-invoice.php b/modules/dolibarr/doli-invoice/class/class-doli-invoice.php index 3cf72fb..a49ee93 100644 --- a/modules/dolibarr/doli-invoice/class/class-doli-invoice.php +++ b/modules/dolibarr/doli-invoice/class/class-doli-invoice.php @@ -82,6 +82,26 @@ class Doli_Invoice extends Post_Class { */ protected $post_type_name = 'Doli Invoice'; + /** + * La limite par page. + * + * @since 2.0.0 + * @version 2.0.0 + * + * @var integer + */ + public $limit = 10; + + /** + * Le nom de l'option pour la limite par page. + * + * @since 2.0.0 + * @version 2.0.0 + * + * @var string + */ + public $option_per_page = 'invoice_doli_per_page'; + /** * Appel la vue "list" du module "doli-invoice". * @@ -89,17 +109,37 @@ class Doli_Invoice extends Post_Class { * @version 2.0.0 */ public function display() { - $invoices = $this->get( array( - 'post_status' => 'any', - ) ); + $dolibarr_option = get_option( 'wps_dolibarr', Settings::g()->default_settings ); + $per_page = get_user_meta( get_current_user_id(), Doli_Invoice::g()->option_per_page, true ); + + if ( empty( $per_page ) || 1 > $per_page ) { + $per_page = Doli_Invoice::g()->limit; + } + + $current_page = isset( $_GET['current_page'] ) ? (int) $_GET['current_page'] : 1; + + $s = ! empty( $_GET['s'] ) ? sanitize_text_field( $_GET['s'] ) : ''; + + $route = 'invoices?sortfield=t.rowid&sortorder=DESC&limit=' . $per_page . '&page=' . ( $current_page - 1 ); + + if ( ! empty( $s ) ) { + // La route de dolibarr ne fonctionne pas avec des caractères en base10 + $route .= '&sqlfilters=(t.ref%3Alike%3A\'%25' . $s . '%25\')'; + } + + $doli_invoices = Request_Util::get( $route ); + $invoices = $this->convert_to_wp_invoice_format( $doli_invoices ); if ( ! empty( $invoices ) ) { foreach ( $invoices as &$element ) { + $element->data['tier'] = null; $element->data['order'] = null; - if ( ! empty( $element->data['parent_id'] ) ) { - $element->data['order'] = Doli_Order::g()->get( array( 'id' => $element->data['parent_id'] ), true ); + if ( isset( $element->data['linked_objects_ids']['commande'][0] ) ) { + $doli_order = Request_Util::get( 'orders/' . $element->data['linked_objects_ids']['commande'][0] ); + $wp_order = Doli_Order::g()->get( array( 'schema' => true ), true ); + $element->data['order'] = Doli_Order::g()->doli_to_wp( $doli_order, $wp_order, true ); } if ( ! empty( $element->data['third_party_id'] ) ) { @@ -108,8 +148,6 @@ public function display() { } } - $dolibarr_option = get_option( 'wps_dolibarr', Settings::g()->default_settings ); - View_Util::exec( 'wpshop', 'doli-invoice', 'list', array( 'invoices' => $invoices, 'doli_url' => $dolibarr_option['dolibarr_url'], @@ -293,79 +331,30 @@ public function doli_to_wp( $doli_invoice, $wp_invoice, $only_convert = false ) } /** - * Ajoute une ligne sur la commande. + * Fonction de recherche. * * @since 2.0.0 * @version 2.0.0 * - * @param Doli_Order $order Les données d'une commande. - * @param array $line_data La donnée à ajouté. - */ - public function add_line( $order, $line_data ) { - $order->data['lines'][] = $line_data; - - $this->update( $order->data ); - } - - /** - * Met à jour une ligne sur la commande. + * @param string $s Le terme de la recherche. + * @param array $default_args Les arguments par défaut. + * @param boolean $count Si true compte le nombre d'élement, sinon renvoies l'ID des éléments trouvés. * - * @since 2.0.0 - * @version 2.0.0 - * - * @param Doli_Order $order Les données d'une commande. - * @param array $line_data La donnée à ajouté. + * @return array|integer Les ID des éléments trouvés ou le nombre d'éléments trouvés. */ - public function update_line( $order, $line_data ) { - $founded_line = null; - $key_line = null; - // Search line by rowid. - if ( ! empty( $order->data['lines'] ) ) { - foreach ( $order->data['lines'] as $key => $line ) { - if ( $line['rowid'] == $line_data['rowid'] ) { - $founded_line = $line; - $key_line = $key; - break; - } - } - } + public function search( $s = '', $default_args = array(), $count = false ) { + $route = 'invoices?sortfield=t.rowid&sortorder=DESC'; - if ( $founded_line != null ) { - array_splice( $order->data['lines'], $key_line, 1 ); - - $order->data['lines'][] = $line_data; - - $this->update( $order->data ); - } - } - - /** - * Supprime une ligne sur la commande. - * - * @since 2.0.0 - * @version 2.0.0 - * - * @param Doli_Order $order Les données d'une commande. - * @param integer $row_id L'id d'une ligne. - */ - public function delete_line( $order, $row_id ) { - $founded_line = null; - $key_line = null; - // Search line by rowid. - if ( ! empty( $order->data['lines'] ) ) { - foreach ( $order->data['lines'] as $key => $line ) { - if ( $line['rowid'] == $row_id ) { - $founded_line = $line; - $key_line = $key; - break; - } - } + if ( ! empty( $s ) ) { + $route .= '&sqlfilters=(t.ref%3Alike%3A\'%25' . $s . '%25\')'; } - if ( $founded_line != null ) { - array_splice( $order->data['lines'], $key_line, 1 ); + $doli_invoices = Request_Util::get( $route ); - $this->update( $order->data ); + if ( $count && ! empty( $doli_invoices ) ) { + return count( $doli_invoices ); + } else { + return 0; } } } diff --git a/modules/dolibarr/doli-invoice/view/item.view.php b/modules/dolibarr/doli-invoice/view/item.view.php index ad4e643..d9dc8fd 100644 --- a/modules/dolibarr/doli-invoice/view/item.view.php +++ b/modules/dolibarr/doli-invoice/view/item.view.php @@ -25,6 +25,9 @@