Skip to content

Commit

Permalink
Fixes openemr#7216,openemr#7217 insurance company saves (openemr#7218)
Browse files Browse the repository at this point in the history
We fix openemr#7216 where we populate the plus_four property in the address
service.

Fixes openemr#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.
  • Loading branch information
adunsulag authored Feb 12, 2024
1 parent 8ac331b commit 374095a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
8 changes: 7 additions & 1 deletion src/Services/AddressService.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

namespace OpenEMR\Services;

use OpenEMR\Common\Database\QueryUtils;
use Particle\Validator\Validator;

class AddressService extends BaseService
Expand All @@ -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);
Expand Down Expand Up @@ -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,
Expand All @@ -85,6 +88,7 @@ public function insert($data, $foreignId)
$data["city"],
$data["state"],
$data["zip"],
$data["plus_four"] ?? null,
$data["country"],
$foreignId
)
Expand All @@ -105,6 +109,7 @@ public function update($data, $foreignId)
$addressesSql .= " city=?,";
$addressesSql .= " state=?,";
$addressesSql .= " zip=?,";
$addressesSql .= " plus_four=?,";
$addressesSql .= " country=?";
$addressesSql .= " WHERE foreign_id=?";

Expand All @@ -116,6 +121,7 @@ public function update($data, $foreignId)
$data["city"],
$data["state"],
$data["zip"],
$data["plus_four"] ?? null,
$data["country"],
$foreignId
)
Expand Down
22 changes: 16 additions & 6 deletions src/Services/InsuranceCompanyService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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";
Expand All @@ -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,
Expand Down Expand Up @@ -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
)
);
Expand All @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/Services/PhoneNumberService.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

namespace OpenEMR\Services;

use OpenEMR\Common\Database\QueryUtils;
use Particle\Validator\Validator;

class PhoneNumberService extends BaseService
Expand Down Expand Up @@ -64,7 +65,7 @@ public function insert($data, $foreignId)
$phoneNumbersSql .= " type=?,";
$phoneNumbersSql .= " foreign_id=?";

$phoneNumbersSqlResults = sqlInsert(
$phoneNumbersSqlResults = QueryUtils::sqlInsert(
$phoneNumbersSql,
array(
$freshId,
Expand Down

0 comments on commit 374095a

Please sign in to comment.