Skip to content

Commit

Permalink
Merge pull request #9 from hametuha/feature/payment-history
Browse files Browse the repository at this point in the history
Add payment history screen.
  • Loading branch information
fumikito authored Mar 13, 2023
2 parents 5ed4bc7 + b924906 commit 903e611
Show file tree
Hide file tree
Showing 18 changed files with 362 additions and 63 deletions.
12 changes: 7 additions & 5 deletions app/Hametuha/Sharee.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ protected function init() {
add_action( 'admin_enqueue_scripts', [ $this, 'admin_enqueue_scripts' ] );
// Register autoloader
add_action( 'after_setup_theme', [ $this, 'after_setup_theme' ] );
// Register command
if ( defined( 'WP_CLI' ) && \WP_CLI ) {
\WP_CLI::add_command( 'sharee', Command::class );
if ( class_exists( 'Hametuha\Sharee\Tests\StubCommands' ) ) {
\WP_CLI::add_command( 'sharee-test', \Hametuha\Sharee\Tests\StubCommands::class );
}
}
}

/**
Expand Down Expand Up @@ -67,11 +74,6 @@ public function after_setup_theme() {
call_user_func( [ $class_name, 'get_instance' ] );
}
}

// Register command
if ( defined( 'WP_CLI' ) && \WP_CLI ) {
\WP_CLI::add_command( 'sharee', Command::class );
}
}


Expand Down
14 changes: 10 additions & 4 deletions app/Hametuha/Sharee/Models/RevenueModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ function( $revenue_id ) {
* @param bool $only_with_deducting Choose only deducting.
* @return array
*/
public function get_fixed_billing( $year, $month = 0, $types = [], $only_with_deducting = false ) {
public function get_fixed_billing( $year, $month = 0, $types = [], $only_with_deducting = false, $user_id = 0 ) {
$wheres = [];
if ( $month ) {
// Search with year month.
Expand All @@ -588,6 +588,9 @@ function( $type ) {
if ( $only_with_deducting ) {
$wheres[] = $this->db->prepare( '( deducting > %d )', 0 );
}
if ( $user_id ) {
$wheres[] = $this->db->prepare( '( object_id = %d )', $user_id );
}
$wheres = 'WHERE ' . implode( ' AND ', $wheres );
$query = <<<SQL
SELECT
Expand Down Expand Up @@ -618,12 +621,15 @@ public function get_payment_list( $year, $user_id = 0 ) {
$wheres[] = sprintf( '(r.object_id = %d)', $user_id );
}
$wheres[] = '( r.status = 1 )';
$wheres[] = sprintf( '( EXTRACT(YEAR from r.fixed) = %04d )', $year );
$wheres = ' WHERE ' . implode( ' AND ', $wheres );
$query = <<<SQL
if ( 'all' !== $year ) {
$wheres[] = sprintf( '( EXTRACT(YEAR from r.fixed) = %04d )', $year );
}
$wheres = ' WHERE ' . implode( ' AND ', $wheres );
$query = <<<SQL
SELECT
SUM(r.total) AS total,
SUM(r.deducting) AS deducting,
SUM(r.tax) AS tax,
r.object_id AS user_id,
u.display_name,
fixed
Expand Down
27 changes: 27 additions & 0 deletions app/Hametuha/Sharee/Screen/PaymentList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Hametuha\Sharee\Screen;


use Hametuha\Pattern\TableScreen;
use Hametuha\Sharee\Models\RevenueModel;
use Hametuha\Sharee\Table\PaymentListTable;
use Hametuha\Sharee\Table\RewardListTable;

class PaymentList extends TableScreen {

protected $slug = 'payment-history';

protected $parent = 'users.php';

protected $table_class = PaymentListTable::class;

protected $has_search = true;

/**
* {@inheritdoc}
*/
protected function get_title() {
return __( 'Payment History', 'sharee' );
}
}
137 changes: 137 additions & 0 deletions app/Hametuha/Sharee/Table/PaymentListTable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
<?php

namespace Hametuha\Sharee\Table;

use Hametuha\Sharee\Models\RevenueModel;
use Hametuha\Sharee\Utilities\TableHelper;

/**
* Display payment history.
*
* @package sharee
*/
class PaymentListTable extends \WP_List_Table {

use TableHelper;

/**
* Get table summary.
*
* @var null
*/
public $summary = null;

/**
* {@inheritdoc}
*/
public function __construct() {
parent::__construct(
array(
'singular' => 'payment',
'plural' => 'payments',
'ajax' => false,
)
);
}

/**
* Get columns.
*
* @return array
*/
public function get_columns() {
return [
'user' => __( 'User', 'sharee' ),
'total' => __( 'Subtotal', 'sharee' ),
'deducting' => __( 'Deducting', 'sharee' ),
'tax' => __( 'VAT', 'sharee' ),
'paid_at' => __( 'Payed At', 'sharee' ),
];
}

/**
* Get items.
*/
public function prepare_items() {
// Set column header.
$this->_column_headers = [
$this->get_columns(),
[],
$this->get_sortable_columns(),
];
// Search revenues.
list( $status, $year, $monthnum, $type, $page_num, $user_id ) = $this->get_current_properties();
$model = RevenueModel::get_instance();
$this->items = $model->get_payment_list( $year, $user_id );
$this->set_pagination_args(
[
'total_items' => count( $this->items ),
'per_page' => count( $this->items ),
]
);
}

/**
* Get column
*
* @param \stdClass $item
* @param string $column_name
*/
public function column_default( $item, $column_name ) {
$model = RevenueModel::get_instance();
switch ( $column_name ) {
case 'user':
$url = add_query_arg( [
'page' => 'payment-history',
'user_id' => $item->user_id,
'year' => filter_input( INPUT_GET, 'year' ) ?: date_i18n( 'Y' ),
], admin_url( 'users.php' ) );
printf( '<a href="%s">%s</a>', esc_url( $url ), esc_html( $item->display_name ) );
break;
case 'total':
case 'tax':
case 'deducting':
echo $model->format( $item->{$column_name} );
break;
case 'paid_at':
echo mysql2date( get_option( 'date_format' ), $item->fixed );
break;
}
}

/**
* {@inheritdoc}
*/
protected function get_table_classes() {
return array_filter(
parent::get_table_classes(),
function( $c ) {
return 'fixed' !== $c;
}
);
}

/**
* {@inheritdoc}
*/
protected function extra_tablenav( $which ) {
if ( 'top' !== $which ) {
return;
}
?>
<label>
<?php esc_html_e( 'User ID', 'sharee' ); ?>
<input style="width: 3em;" type="number" value="<?php echo esc_attr( filter_input( INPUT_GET, 'user_id' ) ); ?>" name="user_id" />
</label>
<?php
$this->filter_inputs( false );
}

/**
* {@inheritdoc}
*/
public function total_record() {
return (int) $this->_pagination_args['total_items'];
}

}
6 changes: 4 additions & 2 deletions app/Hametuha/Sharee/Table/RewardListTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ class RewardListTable extends \WP_List_Table {
*/
public $summary = null;


function __construct() {
/**
* {@inheritdoc}
*/
public function __construct() {
parent::__construct(
array(
'singular' => 'user_reward',
Expand Down
9 changes: 7 additions & 2 deletions app/Hametuha/Sharee/Utilities/TableHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,23 @@ public function get_current_properties() {
if ( ! is_numeric( $month ) ) {
$month = 0;
}
$user = filter_input( INPUT_GET, 'user_id' ) ?: 0;
return [
$status,
$year,
$month,
$type,
max( 1, $this->get_pagenum() ),
$user,
];
}

/**
* Display filter input element
*
* @param bool $with_month If false, no month selector.
*/
protected function filter_inputs() {
protected function filter_inputs( $with_month = true ) {
$model = RevenueModel::get_instance();
list( $status, $year, $month, $type, $page_num ) = $this->get_current_properties();
?>
Expand All @@ -63,6 +67,7 @@ protected function filter_inputs() {
</option>
<?php endforeach; ?>
</select>
<?php if ( $with_month ) : ?>
<select name="monthnum">
<option value="all"<?php selected( 'all', $month ); ?>><?php esc_html_e( 'Every Months', 'sharee' ); ?></option>
<?php for ( $i = 1; $i <= 12; $i ++ ) : ?>
Expand All @@ -71,8 +76,8 @@ protected function filter_inputs() {
</option>
<?php endfor; ?>
</select>
<?php endif; ?>
<input type="submit" class="button" value="<?php esc_attr_e( 'Filter', 'sharee' ); ?>" />
<?php
}

}
2 changes: 1 addition & 1 deletion assets/css/admin.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion assets/css/map/admin.css.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion assets/js/billing-list-helper.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 903e611

Please sign in to comment.