Skip to content

Commit

Permalink
Add : Add Invoice menu
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasDOMENECH committed Jul 30, 2020
1 parent c5d264f commit da5b9b2
Show file tree
Hide file tree
Showing 6 changed files with 196 additions and 101 deletions.
58 changes: 48 additions & 10 deletions modules/dolibarr/doli-invoice/action/class-doli-invoice-action.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand Down Expand Up @@ -116,28 +116,66 @@ 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 ) {
add_action( 'wps_invoice', $metabox['callback'], 10, 1 );
}
}

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 . '&current_page=1';
$end_url = $base_url . '&current_page=' . $number_page;

$prev_url = $base_url . '&current_page=' . ( $current_page - 1 );
$next_url = $base_url . '&current_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,
) );
}
}
Expand Down
129 changes: 59 additions & 70 deletions modules/dolibarr/doli-invoice/class/class-doli-invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,24 +82,64 @@ 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".
*
* @since 2.0.0
* @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'] ) ) {
Expand All @@ -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'],
Expand Down Expand Up @@ -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;
}
}
}
Expand Down
23 changes: 18 additions & 5 deletions modules/dolibarr/doli-invoice/view/item.view.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
<div class="table-cell table-25"><input type="checkbox" class="check"/></div>
<div class="table-cell table-full">
<ul class="reference-id">
<?php if ( ! empty( $invoice->data['external_id'] ) ) : ?>
<li><i class="fas fa-hashtag"></i>Doli : <?php echo esc_html( $invoice->data['external_id'] ); ?></li>
<?php endif; ?>
<li><i class="fas fa-calendar-alt"></i> <?php echo esc_html( $invoice->data['date']['rendered']['date_time'] ); ?></li>
</ul>
<div class="reference-title">
Expand All @@ -38,23 +41,33 @@
</ul>
</div>
<div class="table-cell table-full">
<ul class="reference-id">
<?php if ( ! empty( $invoice->data['order']->data['external_id'] ) ) : ?>
<li><i class="fas fa-hashtag"></i>Doli : <?php echo esc_html( $invoice->data['order']->data['external_id'] ); ?></li>
<?php endif; ?>
<li><i class="fas fa-calendar-alt"></i> <?php echo esc_html( date( "d/m/Y h:i" , strtotime( $invoice->data['order']->data['datec'] ) ) ); ?></li>
</ul>
<div class="reference-title">
<?php if ( ! empty( $invoice->data['order'] ) ) : ?>
<a href="<?php echo esc_attr( admin_url( 'admin.php?page=wps-order&id=' . $invoice->data['order']->data['id'] ) ); ?>"><?php echo esc_html( $invoice->data['order']->data['title'] ); ?></a>
<a href="<?php echo esc_attr( admin_url( 'admin.php?page=wps-order&id=' . $invoice->data['order']->data['external_id'] ) ); ?>"><?php echo esc_html( $invoice->data['order']->data['title'] ); ?></a>
<?php else : ?>-<?php
endif; ?>
</div>
<ul class="reference-actions">
<li><a href="<?php echo esc_attr( admin_url( 'admin.php?page=wps-order&id=' . $invoice->data['order']->data['id'] ) ); ?>"><?php esc_html_e( 'See', 'wpshop' ); ?></a></li>
<?php if ( ! empty( $invoice->data['order']->data['external_id'] ) ) : ?>
<li><a href="<?php echo esc_attr( admin_url( 'admin.php?page=wps-order&id=' . $invoice->data['order']->data['external_id'] ) ); ?>"><?php esc_html_e( 'See', 'wpshop' ); ?></a></li>
<li><a href="#" target="_blank"><?php esc_html_e( 'See in Dolibarr', 'wpshop' ); ?></a></li>
<?php endif; ?>
</ul>
</div>
<div class="table-cell table-200">
<div class="table-cell table-150">
<?php if ( ! empty( $invoice->data['tier'] ) ) : ?>
<div><strong><?php echo esc_html( $invoice->data['tier']->data['title'] ); ?></strong></div>
<div><?php echo esc_html( $invoice->data['tier']->data['contact'] ); ?></div>
<div>
<a href="<?php echo esc_attr( admin_url( 'admin.php?page=wps-third-party&id=' . $invoice->data['tier']->data['id'] ) ); ?>">
<strong><?php echo esc_html( $invoice->data['tier']->data['title'] ); ?></strong>
</a>
</div>
<div><?php echo esc_html( $invoice->data['tier']->data['address'] ); ?></div>
<div><?php echo esc_html( $invoice->data['tier']->data['zip'] ) . ' ' . esc_html( $invoice->data['tier']->data['country'] ); ?></div>
<div><?php echo esc_html( $invoice->data['tier']->data['phone'] ); ?></div>
<?php endif; ?>
Expand Down
1 change: 0 additions & 1 deletion modules/dolibarr/doli-invoice/view/list.view.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
<div class="table-cell table-150"><?php esc_html_e( 'Statut', 'wpshop' ); ?></div>
<div class="table-cell table-100"><?php esc_html_e( 'Method of payment', 'wpshop' ); ?></div>
<div class="table-cell table-100"><?php esc_html_e( 'Price TTC(€)', 'wpshop' ); ?></div>
<div class="table-cell table-100"><?php esc_html_e( 'Synchro', 'wpshop' ); ?></div>
</div>

<?php if ( ! empty( $invoices ) ) :
Expand Down
Loading

0 comments on commit da5b9b2

Please sign in to comment.