Skip to content

Commit

Permalink
Switch to more general invoices tables.
Browse files Browse the repository at this point in the history
  • Loading branch information
remcotolsma committed Feb 29, 2024
1 parent 97a6934 commit 9d074d1
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 25 deletions.
13 changes: 8 additions & 5 deletions admin/page-billing.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,15 @@
LEFT JOIN
(
SELECT
project_invoice.project_id,
SUM( project_invoice.seconds ) AS project_billed_time,
SUM( project_invoice.amount ) AS project_billed_amount,
GROUP_CONCAT( DISTINCT project_invoice.invoice_number ) AS project_invoice_numbers
invoice_line.project_id,
SUM( invoice_line.seconds ) AS project_billed_time,
SUM( invoice_line.amount ) AS project_billed_amount,
GROUP_CONCAT( DISTINCT invoice.invoice_number ) AS project_invoice_numbers
FROM
$wpdb->orbis_projects_invoices AS project_invoice
$wpdb->orbis_invoices AS invoice
INNER JOIN
$wpdb->orbis_invoices_lines AS invoice_line
ON invoice_line.invoice_id = invoice.id
GROUP BY
project_invoice.project_id
) AS project_invoice_totals ON project_invoice_totals.project_id = project.id
Expand Down
4 changes: 2 additions & 2 deletions classes/AdminProjectPostType.php
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ public function save_project_invoices( $post_id ) {
$data['user_id'] = get_current_user_id();
$format['user_id'] = '%d';

$wpdb->insert( $wpdb->orbis_projects_invoices, $data, $format );
$wpdb->insert( $wpdb->orbis_invoices, $data, $format );
} elseif ( $delete ) {
if ( $number == $final_invoice_number ) { // WPCS: loose comparison ok.
$final_invoice_number = null;
Expand All @@ -409,7 +409,7 @@ public function save_project_invoices( $post_id ) {
);
} else {
$result = $wpdb->update(
$wpdb->orbis_projects_invoices,
$wpdb->orbis_invoices,
$data,
[ 'id' => $id ],
$format,
Expand Down
18 changes: 3 additions & 15 deletions classes/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,9 @@ function () {
public function init() {
global $wpdb;

$wpdb->orbis_projects = $wpdb->prefix . 'orbis_projects';
$wpdb->orbis_projects_invoices = $wpdb->prefix . 'orbis_projects_invoices';
$wpdb->orbis_projects = $wpdb->prefix . 'orbis_projects';
$wpdb->orbis_invoices = $wpdb->prefix . 'orbis_invoices';
$wpdb->orbis_invoices_lines = $wpdb->prefix . 'orbis_invoices_lines';

$version = '1.1.3';

Expand Down Expand Up @@ -120,26 +121,13 @@ public function install() {
KEY post_id (post_id),
KEY principal_id (principal_id)
) $charset_collate;
CREATE TABLE $wpdb->orbis_projects_invoices (
id BIGINT(16) UNSIGNED NOT NULL AUTO_INCREMENT,
project_id BIGINT(16) UNSIGNED NOT NULL,
invoice_number VARCHAR(32) NOT NULL,
amount FLOAT NOT NULL,
seconds BIGINT(16) DEFAULT NULL,
user_id BIGINT(20) UNSIGNED DEFAULT NULL,
create_date DATETIME DEFAULT NULL,
PRIMARY KEY (id),
KEY project_id (project_id)
) $charset_collate;
";

require_once ABSPATH . 'wp-admin/includes/upgrade.php';

\dbDelta( $sql );

\maybe_convert_table_to_utf8mb4( $wpdb->orbis_projects );
\maybe_convert_table_to_utf8mb4( $wpdb->orbis_projects_invoices );
}

/**
Expand Down
13 changes: 11 additions & 2 deletions classes/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,16 +141,25 @@ public function get_invoices() {
$wpdb->prepare(
"
SELECT
*
invoice.*,
user.display_name
FROM
$wpdb->orbis_projects_invoices AS invoice
$wpdb->orbis_invoices AS invoice
LEFT JOIN
$wpdb->orbis_invoices_lines AS invoice_line
ON invoice_line.invoice_id = invoice.id
LEFT JOIN
$wpdb->users AS user
ON invoice.user_id = user.ID
WHERE
invoice.project_id = %d
OR
invoice_line.project_id = %d
GROUP BY
invoice.id
;
",
$project_id,
$project_id
)
);
Expand Down
2 changes: 1 addition & 1 deletion templates/project-invoices.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
?>
<tr id="post-<?php the_ID(); ?>">
<td>
<?php echo esc_html( date_format( new DateTime( $invoice->create_date ), 'd-m-Y' ) ); ?>
<?php echo esc_html( date_format( new DateTime( $invoice->created_at ), 'd-m-Y' ) ); ?>
</td>
<td>
<?php
Expand Down

0 comments on commit 9d074d1

Please sign in to comment.