OBPIH-6501 Add endpoint to remove invoice item#4788
OBPIH-6501 Add endpoint to remove invoice item#4788awalkowiak merged 2 commits intofeature/OBPIH-6398-partial-invoices-for-prepaid-pofrom
Conversation
| * */ | ||
| void updateInvoiceItemQuantity(String itemId, Integer quantity) { | ||
| InvoiceItem invoiceItem = InvoiceItem.get(itemId) | ||
| if (quantity > invoiceItem.quantity) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Also, if you're checking shipment items quantity be weary of the case where an item is split across multiple shipments
| InvoiceItem prepaymentItem = orderItem.invoiceItems.find { it.isPrepaymentInvoice } | ||
| InvoiceItem inverseItem = findInverseItem(invoiceItem) | ||
| inverseItem.quantity = quantity | ||
| inverseItem.amount = quantity * prepaymentItem.unitPrice * prepaymentPercent * (-1) |
There was a problem hiding this comment.
can you move this amount calculation to a method so this logic doesn't need to be duplicated everywhere?
There was a problem hiding this comment.
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") |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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) } |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
@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 { | |||
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
@jmiranda no, were removing this, the PR was not updated to the most recent changes yet
25c3c13 to
5a7fb3b
Compare
| 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 |
There was a problem hiding this comment.
Is this fallback for null needed? I think it should be null when the shipmentItem is null.
| Invoice invoice = invoiceItem.invoice | ||
| invoice.removeFromInvoiceItems(invoiceItem) |
There was a problem hiding this comment.
Is there a benefit of assigning invoice to the variable? You can just do:
invoiceItem.invoice.removeFromInvoiceItems(invoiceItem)
98daa1d
into
feature/OBPIH-6398-partial-invoices-for-prepaid-po
✨ Description of Change
Link to GitHub issue or Jira ticket:
https://pihemr.atlassian.net/browse/OBPIH-6501
Description:
Endpoint to remove invoice item
📈 Test Plan
Description of test plan (if applicable):
Will be tested with frontend changes
✅ Quality Checks
[OBS-123]for Jira,[#0000]for GitHub, or[OBS-123, OBPIH-123]if there are multiple), or with[N/A]if not applicable