From 374095aba417d2418cbaf7139d8efec47a2fe6c0 Mon Sep 17 00:00:00 2001 From: Stephen Nielson Date: Mon, 12 Feb 2024 09:47:31 -0500 Subject: [PATCH] Fixes #7216,#7217 insurance company saves (#7218) We fix #7216 where we populate the plus_four property in the address service. Fixes #7217 where we allow the record id to be set / saved on insert. We also throw exceptions if the sql command fails. Lastly we skip saving the phone number if there is nothing in the data. --- src/Services/AddressService.php | 8 +++++++- src/Services/InsuranceCompanyService.php | 22 ++++++++++++++++------ src/Services/PhoneNumberService.php | 3 ++- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/src/Services/AddressService.php b/src/Services/AddressService.php index b955c4ea466..13797ddbf50 100644 --- a/src/Services/AddressService.php +++ b/src/Services/AddressService.php @@ -14,6 +14,7 @@ namespace OpenEMR\Services; +use OpenEMR\Common\Database\QueryUtils; use Particle\Validator\Validator; class AddressService extends BaseService @@ -31,6 +32,7 @@ public function validate($insuranceCompany) $validator->optional('city')->lengthBetween(2, 255); $validator->optional('state')->lengthBetween(2, 35); $validator->optional('zip')->lengthBetween(2, 10); + $validator->optional('plus_four')->lengthBetween(2, 4); $validator->optional('country')->lengthBetween(2, 255); return $validator->validate($insuranceCompany); @@ -73,10 +75,11 @@ public function insert($data, $foreignId) $addressesSql .= " city=?,"; $addressesSql .= " state=?,"; $addressesSql .= " zip=?,"; + $addressesSql .= " plus_four=?,"; $addressesSql .= " country=?,"; $addressesSql .= " foreign_id=?"; - $addressesSqlResults = sqlInsert( + $addressesSqlResults = QueryUtils::sqlInsert( $addressesSql, array( $freshId, @@ -85,6 +88,7 @@ public function insert($data, $foreignId) $data["city"], $data["state"], $data["zip"], + $data["plus_four"] ?? null, $data["country"], $foreignId ) @@ -105,6 +109,7 @@ public function update($data, $foreignId) $addressesSql .= " city=?,"; $addressesSql .= " state=?,"; $addressesSql .= " zip=?,"; + $addressesSql .= " plus_four=?,"; $addressesSql .= " country=?"; $addressesSql .= " WHERE foreign_id=?"; @@ -116,6 +121,7 @@ public function update($data, $foreignId) $data["city"], $data["state"], $data["zip"], + $data["plus_four"] ?? null, $data["country"], $foreignId ) diff --git a/src/Services/InsuranceCompanyService.php b/src/Services/InsuranceCompanyService.php index 74fafabab01..d097abad0f0 100644 --- a/src/Services/InsuranceCompanyService.php +++ b/src/Services/InsuranceCompanyService.php @@ -134,9 +134,10 @@ public function search($search, $isAndCondition = true) $sql .= " a.city,"; $sql .= " a.state,"; $sql .= " a.zip,"; + $sql .= " a.plus_four,"; $sql .= " a.country"; $sql .= " FROM insurance_companies i "; - $sql .= " JOIN (SELECT line1,line2,city,state,zip,country,foreign_id FROM addresses) a ON i.id = a.foreign_id"; + $sql .= " JOIN (SELECT line1,line2,city,state,zip,plus_four,country,foreign_id FROM addresses) a ON i.id = a.foreign_id"; // the foreign_id here is a globally unique sequence so there is no conflict. // I don't like the assumption here as it should be more explicit what table we are pulling // from since OpenEMR mixes a bunch of paradigms. I initially worried about data corruption as phone_numbers @@ -290,6 +291,11 @@ public function insert($data) { // insurance companies need to use sequences table since they share the // addresses table with pharmacies + // I don't like actually inserting a raw id... yet if we don't allow for this + // it makes it very hard for any kind of data import that needs to maintain the same id. + if (empty($data["id"])) { + $data["id"] = generate_id(); + } $freshId = generate_id(); $sql = " INSERT INTO insurance_companies SET"; @@ -303,7 +309,8 @@ public function insert($data) $sql .= " alt_cms_id=?,"; $sql .= " cqm_sop=?"; - sqlInsert( + // throws an exception if the record doesn't insert + QueryUtils::sqlInsert( $sql, array( $freshId, @@ -352,7 +359,7 @@ public function update($data, $iid) $data["x12_receiver_id"], $data["x12_default_partner_id"] ?? null, $data["alt_cms_id"], - $data["cqm_sop"], + $data["cqm_sop"] ?? null, $iid ) ); @@ -367,10 +374,13 @@ public function update($data, $iid) return false; } - $phoneNumberResults = $this->phoneNumberService->update($data, $iid); + // no point in updating the phone if there is no phone record... + if (!empty($data['phone'])) { + $phoneNumberResults = $this->phoneNumberService->update($data, $iid); - if (!$phoneNumberResults) { - return false; + if (!$phoneNumberResults) { + return false; + } } return $iid; diff --git a/src/Services/PhoneNumberService.php b/src/Services/PhoneNumberService.php index 89135be2165..832e1aed69c 100644 --- a/src/Services/PhoneNumberService.php +++ b/src/Services/PhoneNumberService.php @@ -14,6 +14,7 @@ namespace OpenEMR\Services; +use OpenEMR\Common\Database\QueryUtils; use Particle\Validator\Validator; class PhoneNumberService extends BaseService @@ -64,7 +65,7 @@ public function insert($data, $foreignId) $phoneNumbersSql .= " type=?,"; $phoneNumbersSql .= " foreign_id=?"; - $phoneNumbersSqlResults = sqlInsert( + $phoneNumbersSqlResults = QueryUtils::sqlInsert( $phoneNumbersSql, array( $freshId,