Uploading patients from a CSV using CLI and php

Hey folks! I am wanting to write a script to upload patients from a CSV file. I need to first check if the user exists, and if they do not, then create them. Here is what I have at the moment https://gist.github.com/marklocklear/da925c519a84665bac0ece71fa11e9f3.

This works fine to query whether or not the patient exists but then dies when trying to create patients. Actually, it creates the first patient, but then dies on subsequent patients with the error Duplicate entry ‘0’ for key ‘patient_data.pid’ due to the fact that the pid is being set to 0 and is not being autoincremented (assuming that is even OK).

I am a noob to openemr and am just figuring things out as I go. Is it OK to query and find the last/highest pid and just start autoincrementing from there? Ideally I’d like to call openemr methods to do this work, but do they exist? I found library/patient.inc and the newPatientData() which looks promising, but I’m not sure how to call this method from within my script.

I did try adding require_once DIR.’/interface/globals.php’ to my script in hopes of getting access to some of these methods but am getting the error Site ID is missing from session data! when I try that.

Any help or direction is appreciated.

Use databaseInsert method in patientservice.

Thanks…that looks like exactly what I need :pray: Could you share some guidance on how to make it accessible to my script?

I’ve tried adding require_once DIR.’/src/Services/PatientService.php’; and I get an error on line 25 of patient service. Also tried added globals but am getting the session error I mentioned above.

Quickest way is adopt code from existing scripts - using batch import and new patient service.

Best.

For posterity here is what I ended up with:

<?php
	if (php_sapi_name() !== 'cli') {
	    exit;
	}
	set_time_limit(0);

	session_name("OpenEMR");
	$ignoreAuth = true;
	$fake_register_globals = false;
	$sanitize_all_escapes = true;
	$_SESSION['site'] = 'default';
	$_SESSION['site_id'] = 'default';
	$_SERVER['HTTP_HOST'] = 'localhost';
	require_once __DIR__.'/interface/globals.php';

	use OpenEMR\Services\PatientService;

    $patient = array(
		"fname" => "RACHELLE",
		"lname" => "williams"
	);
	
	echo 'Begin Patient Insert';
	$patientService = new PatientService();
	$newPatient = $patientService->databaseInsert($patient);
?>