Skip to content

OBPIH-6501 Add endpoint to remove invoice item#4788

Merged
awalkowiak merged 2 commits intofeature/OBPIH-6398-partial-invoices-for-prepaid-pofrom
OBPIH-6501
Aug 21, 2024
Merged

OBPIH-6501 Add endpoint to remove invoice item#4788
awalkowiak merged 2 commits intofeature/OBPIH-6398-partial-invoices-for-prepaid-pofrom
OBPIH-6501

Conversation

@awalkowiak
Copy link
Collaborator

@awalkowiak awalkowiak commented Aug 14, 2024

✨ Description of Change

A concise summary of what is being changed. Please provide enough context for reviewers to be able to understand the change and why it is necessary. If the issue/ticket already provides enough information, you can put "See ticket" as the description.

Link to GitHub issue or Jira ticket:
https://pihemr.atlassian.net/browse/OBPIH-6501

Description:
Endpoint to remove invoice item


📈 Test Plan

We require that all code changes come paired with a method of testing them. Please select which of the following testing approaches you've included with this change:

  • Frontend automation tests (unit)
  • Backend automation tests (unit, API, smoke)
  • End-to-end tests (Playwright)
  • Manual tests (please describe below)
  • Not Applicable

Description of test plan (if applicable):
Will be tested with frontend changes


✅ Quality Checks

Please confirm and check each of the following to ensure that your change conforms to the coding standards of the project:

  • The pull request title is prefixed with the issue/ticket number (Ex: [OBS-123] for Jira, [#0000] for GitHub, or [OBS-123, OBPIH-123] if there are multiple), or with [N/A] if not applicable
  • The pull request description has enough information for someone without context to be able to understand the change and why it is needed
  • The change has tests that prove the issue is fixed / the feature works as intended

@awalkowiak awalkowiak added status: work in progress For issues or pull requests that are in progress but not yet completed status: ready for review Flags that a pull request is ready to be reviewed labels Aug 14, 2024
@github-actions github-actions bot added the domain: backend Changes or discussions relating to the backend server label Aug 14, 2024
* */
void updateInvoiceItemQuantity(String itemId, Integer quantity) {
InvoiceItem invoiceItem = InvoiceItem.get(itemId)
if (quantity > invoiceItem.quantity) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Actually, this should be checking the quantity available to invoice from the shipment item instead of the current invoice item's quantity. Will fix that on Monday.

Copy link
Member

Choose a reason for hiding this comment

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

Also, if you're checking shipment items quantity be weary of the case where an item is split across multiple shipments

Base automatically changed from OBPIH-6499 to feature/OBPIH-6398-partial-invoices-for-prepaid-po August 19, 2024 13:32
InvoiceItem prepaymentItem = orderItem.invoiceItems.find { it.isPrepaymentInvoice }
InvoiceItem inverseItem = findInverseItem(invoiceItem)
inverseItem.quantity = quantity
inverseItem.amount = quantity * prepaymentItem.unitPrice * prepaymentPercent * (-1)
Copy link
Member

Choose a reason for hiding this comment

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

can you move this amount calculation to a method so this logic doesn't need to be duplicated everywhere?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sure will do it. btw I am moving out the edit endpoint to separate PR.

throw new IllegalArgumentException("Cannot update quantity invoiced to higher value")
}
if (quantity <= 0) {
throw new IllegalArgumentException("Quantity invoiced needs to be higher than 0")
Copy link
Member

Choose a reason for hiding this comment

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

we should also have a case here for not allowing the user to edit the quantity if quantity is 0 / the shipment item is cancelled. For that case quantity must always be 0.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

But we don't want to allow cancelling items through this (editing to 0), but I double check that.

private void deleteInvoiceItem(InvoiceItem invoiceItem) {
invoiceItem.orderAdjustments?.each { OrderAdjustment oa -> oa.removeFromInvoiceItems(invoiceItem) }
invoiceItem.orderItems?.each { OrderItem oi -> oi.removeFromInvoiceItems(invoiceItem) }
invoiceItem.shipmentItems?.each { ShipmentItem si -> si.removeFromInvoiceItems(invoiceItem) }
Copy link
Member

Choose a reason for hiding this comment

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

is there no way in GORM to set this to be automatic? If an item is deleted, remove it from all relationships? It's weird to me that this needs to be manual

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@ewaterman These are many to many relations, and there is the case that cascade is for one to many only and when we still have invoice item instance on the related object (order item, order adjustment, or shipment item), then there would be thrown

org.springframework.dao.InvalidDataAccessApiUsageException: deleted object would be re-saved by cascade (remove deleted object from associations)

See:
https://spring.io/blog/2010/07/02/gorm-gotchas-part-2#deletes-do-not-cascade-period
https://grails.github.io/legacy-grails-doc/3.2.0/ref/Database%20Mapping/cascade.html

@@ -0,0 +1,18 @@
package org.pih.warehouse.invoice

enum InvoiceItemType {
Copy link
Member

Choose a reason for hiding this comment

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

If we're keeping this (I assumed we wouldn't) we should call this InvoiceItemTypeCode to leave room for adding a proper domain in the future.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@jmiranda no, were removing this, the PR was not updated to the most recent changes yet

@awalkowiak awalkowiak force-pushed the OBPIH-6501 branch 2 times, most recently from 25c3c13 to 5a7fb3b Compare August 21, 2024 07:42
@awalkowiak awalkowiak changed the title OBPIH-6501 and OBPIH-6502 Add endpoints to edit invoice item quantity and to remove … OBPIH-6501 Add endpoint to remove invoice item Aug 21, 2024
Invoice invoice = invoiceItem.invoice

// Since we can have only one of the three options I am doing this in this way (at least for now)
def relatedObject = invoiceItem.orderAdjustment ?: invoiceItem.orderItem ?: invoiceItem.shipmentItem ?: null
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this fallback for null needed? I think it should be null when the shipmentItem is null.

Comment on lines 232 to 233
Invoice invoice = invoiceItem.invoice
invoice.removeFromInvoiceItems(invoiceItem)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is there a benefit of assigning invoice to the variable? You can just do:

invoiceItem.invoice.removeFromInvoiceItems(invoiceItem)

@awalkowiak awalkowiak merged commit 98daa1d into feature/OBPIH-6398-partial-invoices-for-prepaid-po Aug 21, 2024
@awalkowiak awalkowiak deleted the OBPIH-6501 branch August 21, 2024 10:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

domain: backend Changes or discussions relating to the backend server status: ready for review Flags that a pull request is ready to be reviewed status: work in progress For issues or pull requests that are in progress but not yet completed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants