Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements for "PDF document data" metabox #757

Closed
wants to merge 46 commits into from

Conversation

MohamadNateqi
Copy link
Contributor

@MohamadNateqi MohamadNateqi commented Mar 28, 2024

close #756
close #760

Note: For the Pro extension compatibility, this branch should be used: https://github.com/wpovernight/woocommerce-pdf-ips-pro/pull/392.


This PR improves the way document data are displayed in the metabox. It includes a check for pending jobs and displays a message if the document is still in the queue.

It also uses WordPress heartbeat to update document data via AJAX, eliminating the need to refresh the page. However, for this to work correctly with Pro documents, the Pro plugin requires some improvements that have been done in the PR mentioned above.

For test purposes, the below snippet can be used to stop the queue from running jobs:

function wpo_disable_default_runner() {
	if ( class_exists( 'ActionScheduler' ) ) {
		remove_action( 'action_scheduler_run_queue', array( ActionScheduler::runner(), 'run' ) );
	}
}

add_action( 'init', 'wpo_disable_default_runner', 10 );

Then, the job can be manually run to test the AJAX update.


This PR also fixes the issue that prevents displaying a dependent document if it already exists. If the invoice document is a requirement for another document (such as the credit note), removing the invoice from the "PDF document data" meta box will prevent the other dependent document from being displayed. For example, when a credit note or receipt document exists, removing the invoice prevents displaying those documents, even though they've already been generated.

This issue happens on both the AJAX request and page refresh/load (AJAX request also gets the whole page content the same as page load and refreshes the meta box with new content) because the invoice is a requirement for those documents, and the is_allow() method checks for that and returns false when it cannot find an invoice.

A new filter has been added to address this issue, which needs this filter in the Pro extension.

@MohamadNateqi MohamadNateqi self-assigned this Mar 28, 2024
@MohamadNateqi MohamadNateqi requested a review from alexmigf March 28, 2024 19:05
@Terminator-Barbapapa Terminator-Barbapapa self-requested a review April 2, 2024 12:13
includes/class-wcpdf-admin.php Outdated Show resolved Hide resolved
includes/class-wcpdf-admin.php Outdated Show resolved Hide resolved
includes/class-wcpdf-admin.php Outdated Show resolved Hide resolved
includes/class-wcpdf-admin.php Outdated Show resolved Hide resolved
includes/class-wcpdf-admin.php Outdated Show resolved Hide resolved
includes/class-wcpdf-admin.php Outdated Show resolved Hide resolved
assets/js/order-script.js Outdated Show resolved Hide resolved
includes/class-wcpdf-admin.php Show resolved Hide resolved
includes/views/document-data-metabox.php Show resolved Hide resolved
includes/class-wcpdf-admin.php Show resolved Hide resolved
includes/class-wcpdf-admin.php Outdated Show resolved Hide resolved
includes/class-wcpdf-admin.php Show resolved Hide resolved
includes/class-wcpdf-admin.php Outdated Show resolved Hide resolved
@alexmigf alexmigf added this to the 3.8.2 milestone Apr 4, 2024
Improve data output method for document data meta box
@MohamadNateqi MohamadNateqi requested a review from alexmigf April 9, 2024 15:34
Copy link
Member

@alexmigf alexmigf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some reason I can't delete the Receipt:

Screenshot from 2024-04-10 11-26-56

It may not seem directly relevant, but it could be worth investigating.

The time inputs width is strange:

Screenshot from 2024-04-10 11-31-28

includes/class-wcpdf-admin.php Outdated Show resolved Hide resolved
includes/class-wcpdf-admin.php Outdated Show resolved Hide resolved
Copy link
Member

@alexmigf alexmigf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should uniform the number and date with the third party documents too?

Screenshot from 2024-04-15 16-56-08

@MohamadNateqi
Copy link
Contributor Author

MohamadNateqi commented Apr 15, 2024

Maybe we should uniform the number and date with the third party documents too?

@alexmigf Thank you for mentioning this. Fixed here: a58ff0f.

Copy link
Member

@alexmigf alexmigf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm having the "Packing Slip" duplicated, not sure why.

Screenshot from 2024-04-16 11-55-06

includes/class-wcpdf-admin.php Outdated Show resolved Hide resolved
includes/class-wcpdf-admin.php Outdated Show resolved Hide resolved
includes/class-wcpdf-admin.php Outdated Show resolved Hide resolved
includes/class-wcpdf-admin.php Outdated Show resolved Hide resolved
@MohamadNateqi
Copy link
Contributor Author

I'm having the "Packing Slip" duplicated, not sure why.

Screenshot from 2024-04-16 11-55-06

Could you please check if your Pro extension is on the related branch?

@alexmigf
Copy link
Member

It's in main.

@MohamadNateqi
Copy link
Contributor Author

It should be on the pdf-document-data branch, since as I mentioned here, I moved packing slip from the Pro extension to the Free plugin.

@alexmigf
Copy link
Member

Ok, I didn't saw that comment, I will check that again.

@alexmigf
Copy link
Member

alexmigf commented Apr 16, 2024

Seems to work fine now regarding the Packing Slip.

@MohamadNateqi MohamadNateqi requested a review from alexmigf April 16, 2024 18:02
includes/class-wcpdf-admin.php Outdated Show resolved Hide resolved
@MohamadNateqi
Copy link
Contributor Author

@alexmigf
Copy link
Member

@MohamadNateqi yes, I agree.

includes/documents/abstract-wcpdf-order-document.php Outdated Show resolved Hide resolved
@@ -1039,7 +1044,7 @@ public function ajax_crud_document() {

try {
// Allow getting documents even if their prerequisite documents doesn't exist.
add_filter( 'wpo_wcpdf_prerequisite_documents', '__return_empty_array()' );
add_filter( 'wpo_wcpdf_document_do_prerequisites_exist', '__return_true' );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe something like this would work below line 1049? Because we are not forcing the document generation at this stage, so it's safe to call the document first.

if ( false === apply_filters( 'wpo_wcpdf_bypass_prerequisites_restriction', true, 'ajax_crud_document', $document ) && ! empty( $document->get_prerequisite_documents() ) ) {
	return;
}

This can be also applied to the data_input_box_content() inside the loop I think.

And maybe inside is_allowed()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can add checking prerequisites inside the is_allowed() method, inside the condition you mentioned, but again we need to change the value that the wpo_wcpdf_bypass_prerequisites_restriction filter returns, before line 1049, since otherwise wcpdf_get_document() method won't return the document.
So, I think it's again adding a filter above the line 1049.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this commit: 08cff80, I added again checking prerequisites to is_allowed() method, but made it false as default, since otherwise it'll allow creation of documents without prerequisites, and also used the filter you mentioned.
Please let me know what you think.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could avoid having it inside is_allowed(), and call it when you need to check the is_allowed() output. I think returning an empty array seems strange to me and a bit like hacking.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we don't add checking prerequisites to the is_allowed() method, the wcpdf_get_document() method will return a document even if the prerequisites of it don't exist. so I thought maybe it should be inside that as default?

I think returning an empty array seems strange to me and a bit like hacking.

You're right.
In the recent commit, I used the filter you mentioned: wpo_wcpdf_bypass_prerequisites_restriction. What do you think about it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since a document object is required to call the do_prerequisites_exist(), to check the prerequisite before forcing the document generation, I must call the wcpdf_get_document() again without the third parameter. I'm not sure if this is a good approach.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can get the document from here: $document = WPO_WCPDF()->documents->get_document( $document_type, $order );

Copy link
Member

@alexmigf alexmigf Apr 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you add to is_allowed() I think you don't need to add on generate_document_ajax()? I haven't tested.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added that to is_allowed() method like this:

if ( ! $this->do_prerequisites_exist( $context ) ) {
	return false;
}

But since it'll bypass by default, it won't affect it. If we change the bypass to false by default, then we need to use the filter to change it anywhere we want, for example, for metabox.

I think checking prerequisites is a requirement for the wcpdf_get_document() method, so maybe it's better to keep the bypass false as default?

Also, we can use the WPO_WCPDF()->documents->get_document() method here, but I think it'll make the usage of wcpdf_get_document() and it should be something that should be considered for using of wcpdf_get_document() method.

If you agree, I will revert the related change to this matter and open another PR to address this issue, which is related to issue #760.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you agree, I will revert the related change to this matter and open another PR to address this issue, which is related to issue #760.

Yes, it's better, we continue from there.

@alexmigf alexmigf modified the milestones: 3.8.2, 3.8.3 Apr 29, 2024
@MohamadNateqi
Copy link
Contributor Author

I'm closing this PR to open new PRs to address the above issues separately.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
2 participants