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

set invoice status automatically after creation #11084

Conversation

abdellani
Copy link
Member

@abdellani abdellani commented Jun 20, 2023

What? Why?

What should we test?

Possible scenarios:
Scenario 1:

  • Create an invoice for an order
  • the status must be 'active'

Scenario 2:

  • edit order note
  • try to create a new invoice
  • it should only update the existing invoice ( you'll not see a new invoice)

Scenario 3:

  • edit line items
  • try to create a new invoice
  • a new invoice should be created and set to active, the old invoice should be updated to cancelled.

Release notes

Changelog Category: Technical changes

The title of the pull request will be included in the release notes.

Dependencies

Documentation updates

@abdellani abdellani force-pushed the update_invoice_status_automatically branch 2 times, most recently from 0436221 to d5bb178 Compare June 20, 2023 10:03
@abdellani abdellani force-pushed the update_invoice_status_automatically branch 3 times, most recently from 06b85cd to 00d4731 Compare June 20, 2023 10:57
Copy link
Contributor

@jibees jibees left a comment

Choose a reason for hiding this comment

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

I've one comment and one question. Thanks @abdellani ! 🙏


expect {
click_link 'New Invoice'
sleep 2
Copy link
Contributor

Choose a reason for hiding this comment

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

This one was strongly needed? I'd love to avoid as much as possible this kind of sleep in the code...

Copy link
Member Author

Choose a reason for hiding this comment

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

what do you think about replacing it with page.driver.wait_for_network_idle? ( thanks @filipefurtad0 for sharing)

Copy link
Contributor

Choose a reason for hiding this comment

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

I think it's better, even if I tend to avoid this as well. But if it's strongly needed, let's do this!

Copy link
Collaborator

@rioug rioug Jul 1, 2023

Choose a reason for hiding this comment

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

I would try to check something UI related instead of waiting, if possible (I am not familiar with this page)
Fixed in this commit : 7196baf

app/models/invoice.rb Outdated Show resolved Hide resolved
@abdellani abdellani force-pushed the update_invoice_status_automatically branch from 00d4731 to 19fcf2a Compare June 22, 2023 10:25
@abdellani
Copy link
Member Author

rspec ./spec/lib/reports/orders_and_fulfillment/orders_cycle_supplier_totals_report_spec.rb:85
is passing locally! I don't see why it's not passing on the pipeline

Copy link
Member

@mkllnk mkllnk left a comment

Choose a reason for hiding this comment

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

Nice code.

Zooming out to the big picture, I'm wondering if we need the status flag. If the newest invoice is always the active one, can't we just use that? Otherwise we always have the challenge that the data in the database is inconsistent for a tiny moment and we could end up with two active invoices if they are created at the same time (DB transactions not seeing each other).

But thinking about the implementation, it would be complicated and less efficient to query the status by comparing the timestamp to all other invoices belonging to the same order. So I agree that the status flag is good to have.

I would still suggest the following changes:

  • Define a default value for status on the database level. This needs a migration but then we don't need the before_validation callback.
  • Remove set_active.

@@ -3,7 +3,8 @@
class Invoice < ApplicationRecord
belongs_to :order, class_name: 'Spree::Order'
serialize :data, Hash
before_validation :serialize_order
before_validation :serialize_order, :set_active
Copy link
Member

Choose a reason for hiding this comment

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

This will set the flag every time the invoice is saved.

Comment on lines 19 to 21
def set_active
self.status = 'active'
end
Copy link
Member

Choose a reason for hiding this comment

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

This would override an existing value as well. It will be impossible to set the status to another value, even for test purposes.

@mkllnk mkllnk force-pushed the update_invoice_status_automatically branch from 6f95e3b to 63dd780 Compare June 29, 2023 02:05
Copy link
Collaborator

@rioug rioug left a comment

Choose a reason for hiding this comment

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

Looks good 👍

@rioug
Copy link
Collaborator

rioug commented Jul 1, 2023

I think we need an extra review to double checked @mkllnk changes, @abdellani could you have a look ?

@s33dco
Copy link

s33dco commented Jul 1, 2023

Hiya, forgive me jumping in.
Has changing the :status from a string -> integer been considered?, this could be fixed up in one migration and the values reset in a rake task, a default could be then set at the db level, for active and new instances would be initialized in that status? 0: Active. Would have assumed an invoice could only have a handful of state.

@rioug
Copy link
Collaborator

rioug commented Jul 2, 2023

Hi @s33dco What would be the benefit of having an integer instead of a string ? To me it's a downgrade, with a string you would need to figure out what the status number mean.

@mkllnk
Copy link
Member

mkllnk commented Jul 3, 2023

Instead of an integer, we could change status, :string to :active, :boolean. That would make more sense to me. Or maybe even better in this case: :cancelled, :boolean. That fits better into the workflow. New invoices are always uncancelled and the creation process cancels previous invoices.

I think I would prefer that unless there are plans for more states, @abdellani?

@s33dco
Copy link

s33dco commented Jul 3, 2023 via email

@rioug
Copy link
Collaborator

rioug commented Jul 3, 2023

Indeed using an Enum for the status here would work, it wasn't clear to me that's what you were suggesting in your original comment. Thanks!

@s33dco
Copy link

s33dco commented Jul 4, 2023

i figured an invoice would have more statuses than active / inactive, for example. paid, cancelled, overdue etc.

@abdellani
Copy link
Member Author

i figured an invoice would have more statuses than active / inactive, for example. paid, cancelled, overdue etc.

According to the specifications, we only have two statuses: active/canceled (+ only the latest invoice will be active).

I'm not against the idea of using Enum (which will take less space on the storage compared to a string), but I think that it'll not be consistent with the other models Spree::Order, Payment, Shipment, and Adjustment.
On all those models, we're using the state_machines-activerecord Gem which expects a string column to store the state (not sure if it can be configured to use an integer).

For the other statuses like paid, and canceled... we're tracking those on the Order model (+ there are other state machines on payments and shipment). An invoice can be generated only when an order is on the state completed or resumed.

I like the idea of using :cancelled, :boolean from @mkllnk

@abdellani abdellani force-pushed the update_invoice_status_automatically branch 3 times, most recently from ea72573 to 6066afb Compare July 5, 2023 17:19
@abdellani
Copy link
Member Author

@mkllnk
I squashed all the commits because I needed to edit all the previous changes.

@abdellani abdellani requested review from mkllnk and rioug July 5, 2023 17:22
Copy link
Collaborator

@rioug rioug left a comment

Choose a reason for hiding this comment

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

Looks good 👍 ! I learnt about squish today.

@abdellani abdellani force-pushed the update_invoice_status_automatically branch from 6066afb to 6131343 Compare July 11, 2023 08:07
@abdellani abdellani force-pushed the update_invoice_status_automatically branch from 6131343 to 4b003aa Compare July 11, 2023 08:15
Copy link
Member

@mkllnk mkllnk left a comment

Choose a reason for hiding this comment

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

Cool, nice work!

@filipefurtad0 filipefurtad0 self-assigned this Jul 21, 2023
@filipefurtad0 filipefurtad0 added the pr-staged-uk staging.openfoodnetwork.org.uk label Jul 21, 2023
@filipefurtad0
Copy link
Contributor

filipefurtad0 commented Jul 21, 2023

Hey @abdellani ,

Scenario 1

- Create an invoice for an order
- the status must be 'active'

Before this PR, an error 500 was triggered, on this scenario:

https://app.bugsnag.com/yaycode/openfoodnetwork-uk/errors/64ba9aa8525cb30007b1caf3?event_id=64ba9aa800bee437b4360000&i=sk&m=nw

After staging, we can see the status is now attributed:

image

Scenario 2

_edit order note
try to create a new invoice
it should only update the existing invoice ( you'll not see a new invoice)_

Creating a new invoice (this requires clicking New Invoice) updates the exiting invoice and adds the Additional Information section, with the note contents:

image

Scenario 3

_edit line items
try to create a new invoice
a new invoice should be created and set to active, the old invoice should be updated to cancelled._

Also here, it is necessary to click clicking New Invoice, then a new invoice is generated and the previous one set to cancelled:

image

This looks good 👌

It also adds some missing translations - so it closes #11262.
I see there might be some overlap with #11252...
I'll merge and see it it needs some reconciliation.

Thanks @abdellani !! This improves things quite a lot. Merging.

@filipefurtad0 filipefurtad0 merged commit 0d0beda into openfoodfoundation:master Jul 21, 2023
2 checks passed
@filipefurtad0 filipefurtad0 removed the pr-staged-uk staging.openfoodnetwork.org.uk label Jul 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Invoices] Set an invoice status after creating the first invoice
6 participants