Skip to content

OBPIH-6674 Unable to update bc and unit price through PO import when other items inluced in invoice#4846

Merged
awalkowiak merged 2 commits intorelease/0.9.2-hotfix1from
OBPIH-6674
Sep 23, 2024
Merged

OBPIH-6674 Unable to update bc and unit price through PO import when other items inluced in invoice#4846
awalkowiak merged 2 commits intorelease/0.9.2-hotfix1from
OBPIH-6674

Conversation

@alannadolny
Copy link
Collaborator

No description provided.

@alannadolny alannadolny self-assigned this Sep 18, 2024
@github-actions github-actions bot added the domain: backend Changes or discussions relating to the backend server label Sep 18, 2024
Copy link
Collaborator

@kchelstowski kchelstowski left a comment

Choose a reason for hiding this comment

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

I think we should revisit as soon as possible our ddl and add the constraints to the database. This is probably the reason why the duplicate exists - it must've been updated/inserted through the DB. I can't see any unique constraint on the budget_code table.


// There can be more than one budget code with the same code, despite the fact that the code is unique from the domain perspective.
// As a fix for that we would like to have only one active budget code with the same code, so we have to find only active
// budget codes. In case when there is only one budget code, and this one is inactive we have to throw a validation error,
Copy link
Collaborator

Choose a reason for hiding this comment

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

if you say so, why don't you throw this exception if the foundBudgetCodes.first() would be inactive?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It's threw a few lines below

// budget codes. In case when there is only one budget code, and this one is inactive we have to throw a validation error,
// so we can't just look for BudgetCode.findAllByCodeAndActive(code, true);
List<BudgetCode> foundBudgetCodes = BudgetCode.findAllByCode(code)
BudgetCode budgetCode = foundBudgetCodes.size() > 1 ? foundBudgetCodes.find { it.active } : foundBudgetCodes.first()
Copy link
Collaborator

Choose a reason for hiding this comment

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

If we have a case with multiple active, then we still might have the issue with accidentally updating the budget code on the item, perhaps it would be worth throwing an exception if we have multiple active budget codes with the same code?

@ewaterman
Copy link
Member

I agree with Kacper that I'd like to prioritize a proper fix for this.

I was curious to know how many cases of duplicate code that we have in prod:

select code, count(*) from budget_code group by code having count(*) > 1;
+------------+----------+
| code       | count(*) |
+------------+----------+
| HAUNR.0380 |        2 |
| PH0650.022 |        2 |
| SI0385.055 |        2 |
| SI0716.001 |        2 |
+------------+----------+
4 rows in set (0.211 sec)

and the offenders in detail

+----------------------------------+------------+----------------------------------------------------+----------------------------------------------------+-----------------+---------------------+---------------------+----------------------------------+----------------------------------+--------+
| id                               | code       | name                                               | description                                        | organization_id | date_created        | last_updated        | created_by_id                    | updated_by_id                    | active |
+----------------------------------+------------+----------------------------------------------------+----------------------------------------------------+-----------------+---------------------+---------------------+----------------------------------+----------------------------------+--------+
|  SI0716.001                      | SI0716.001 | Meds and Plumpy Nuts                               | Meds and Plumpy Nuts                               | NULL            | 2021-01-15 00:00:00 | 2021-01-15 00:00:00 | 8a8a9e9666194c89016635c637370b9b | 8a8a9e9666194c89016635c637370b9b | true   |
| 09f5e1167944cbae017aaac49b965793 | SI0385.055 | GAC- ANC                                           | GAC- ANC                                           | NULL            | 2021-07-15 15:24:09 | 2021-07-15 15:24:27 | 8a8a9e9666194c8901663a87e9a80b9c | 8a8a9e9666194c8901663a87e9a80b9c | true   |
| 09f5e1167b98d5fd017b9c867f1c0a35 | HAUNR.0380 | HUM Facility Maintenance - Waste Management        | HUM Facility Maintenance - Waste Management        | NULL            | 2021-08-31 14:04:25 | 2024-09-09 17:01:27 | 09f5e1167751da090177b70696d04f2d | 8a8a9e9665c4f59d0165c5101f6d0001 | false  |
| 09f5e1167b98d5fd017ba21c0a4f3da6 | PH0650.022 | Other - Immokalee                                  | Other - Immokalee                                  | NULL            | 2021-09-01 16:05:52 | 2024-09-09 17:02:02 | 09f5e1167751da090177b70696d04f2d | 8a8a9e9665c4f59d0165c5101f6d0001 | false  |
| HAUNR.0380                       | HAUNR.0380 | HUM Facility Maintenance - Waste Management        | HUM Facility Maintenance - Waste Management        | NULL            | 2025-01-04 00:00:00 | 2025-01-04 00:00:00 | 8a8a9e9666194c89016635c637370b9b | 8a8a9e9666194c89016635c637370b9b | true   |
| PH0650.022                       | PH0650.022 | Other - Immokalee                                  | Other - Immokalee                                  | NULL            | 2021-01-15 00:00:00 | 2021-01-15 00:00:00 | 8a8a9e9666194c89016635c637370b9b | 8a8a9e9666194c89016635c637370b9b | false  |
| SI0385.055                       | SI0385.055 | Social Support for Vulnerable patient and children | Social Support for Vulnerable patient and children | NULL            | 2025-01-04 00:00:00 | 2025-01-04 00:00:00 | 8a8a9e9666194c89016635c637370b9b | 8a8a9e9666194c89016635c637370b9b | true   |
| SI0716.001                       | SI0716.001 | Meds and Plumpy Nuts                               | Meds and Plumpy Nuts                               | NULL            | 2025-01-04 00:00:00 | 2025-01-04 00:00:00 | 8a8a9e9666194c89016635c637370b9b | 8a8a9e9666194c89016635c637370b9b | true   |
+----------------------------------+------------+----------------------------------------------------+----------------------------------------------------+-----------------+---------------------+---------------------+----------------------------------+----------------------------------+--------+

with only SI0716.001 and SI0385.055 having multiple active.

Given that it seems to be a very rare occurrence maybe we can fix what we have on prod manually (though we'd still potentially need a migration to fix any non-PIH environments).

While I think we should add unique constraint, I don't see a simple way to fix existing data via migration given that we'd need a way to pick which code should be the active one, which isn't immediately obvious from just looking at the data.

throw new IllegalArgumentException("Found more than one active budget code with the same code.")
}

BudgetCode budgetCode = activeBudgetCodes.size() == 0 ? foundBudgetCodes[0] : activeBudgetCodes.first()
Copy link
Member

Choose a reason for hiding this comment

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

This still feels somewhat clunky in that if there are multiple inactive codes (and no active) we just pick whatever the first one is. Why do we allow the user to set an inactive budget code? Shouldn't it simply error if there's no active code that matches?

nitpick: you're mixing your access methods here ([0] and x.first())

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

you're mixing your access methods here ([0] and x.first())

I did it on purpose, the .first method throws an error when there is no element, [0] doesn't so it will be null, and then we will get the information that we didn't find any budget code.

If there is more than one inactive budget code, we will get the error that the budget code is inactive, independently of which one inactive budget code the system will pick.

Copy link
Member

Choose a reason for hiding this comment

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

oh neat I didn't actually know that groovy made index-based accesses null safe. 👍

@awalkowiak awalkowiak merged commit 422d151 into release/0.9.2-hotfix1 Sep 23, 2024
@awalkowiak awalkowiak deleted the OBPIH-6674 branch September 23, 2024 07:49
jwalbers pushed a commit to jwalbers/openboxes that referenced this pull request Oct 29, 2024
jwalbers pushed a commit to jwalbers/openboxes that referenced this pull request Oct 31, 2024
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants