********** internal backend functions **********

----- library/auth.inc ------

NOTICE: Please note that this Backend Spec is a development document, and was used during the developmental stages of OpenEMR. It was last updated for OpenEMR version 1.3, and therefore does not completely detail all of the newer library functions.

--------------------------------------------------------

we will ALWAYS look for $_GET['auth'], if it is login we will do so, if it is logout we will do so. all our post vars must be set to have a chance getting in.  auth.inc should ALWAYS BE CALLED ABSOLUTELY FIRST.

authNewSession(username, md5sum of password, provider)
returns boolean, is pass checks out or not.  if true, also sets:
	$_SESSION['authUser'] = current username logged in
	$_SESSION['authPass'] = md5sum of logged in users password
	$_SESSION['authProvider'] = the current provider
	$_SESSION['authId'] = user Id number (record id)

authCheckSession()
returns boolean for whether current cookie checks out

authCloseSession()
returns nothing, runs a session_destroy

authLoginScreen()
returns nothing. includes js file, then login.inc, and die()

addUser(username, password, info, authorized w/ default yes)
adds a user to the system with the given credentials
returns the unique ID of the user with the given information
password is an MD5 hash

delUser(id)
deletes the user with the given ID from the system

changePassword(id, password)
changes the password of the user with the given id to password
password is an MD5 hash

getUserList(cols w/default *, limit w/default all, start w/default 0)
returns a nested array of the user information specified in cols

getProviderList(cols w/default *, limit w/ default all, start w/ default
0)
return a nested array of cols from the groups table

addGroup(groupname)
add a new group name to the system
returns the ID of the group

delGroup(groupname)
deletes the group from the system

----- library/sql.inc - Matthew Beale, Ted Dziuba -----
we will ALWAYS create a connection to sql, and set $GLOBALS['dbh']  to our database connection.

sqlConnect()
returns a database handle, connected to localhost, user openemr, db openemr

sqlStatement(statement)
returns the query result resource.

sqlClose()
returns whatever mysql_close returns.

sqlInsert(statement)
returns the last inserted id after running your insert.

sqlFetchArray(mysql query result resource)
returns the current mysql_fetch_array result.

sqlQuery(statement)
returns the first result from the database.


********** public backend functions **********

----- library/lists.inc - Matthew Beale -----
used for accessing openemr.lists in the db, we require_once('sql.inc')

getListById(id, columns - default all)
returns the first record returned, the most recent (although there _should_ only be one.

getListByType(patient id, type, wanted columns w/ deafult all, active - default 'all', limit default "all", offset default "0")
returns a multi-dimensional array or record lines returned.

addList(patient id, entry type, title, comments, activity state w/ default active)
returns the id generated by the insert

disappearList (id)
returns true

reappearList (id)
returns true

----- library/transactions.inc - Matt Koch, Matthew Beale ------
used for accessing openemr.transactions in the db, we require_once('sql.inc')

getTransById(id, columns - default all)
returns the first record returned, the most recent (although there _should_ only be one.

getTransByPid(patient id, columns w/ default all)
returns a multi-dimensional array or record lines returned.

newTransaction(patient id, body text, title, authorized w/ default "0")
returns id of inserted record

authorizeTransaction(id, authorized w/ default '1')
returns useless data. if you want it to return something let us know.

----- library/pnotes.inc - Matt Koch, Matthew Beale -----
used for accessing openemr.pnotes in the db, we require_once('sql.inc')

getByPnoteId(id, columns - default all)
returns the first record returned, the most recent (although there _should_ only be one.

getPnotesByDate(date, activity w/ default "1", columns w/ default "*", patient id - default "%", limit w/ default "all", start w/ default 0)
returns a multi-dimensional array or record lines returned.

getPnotesByPid(patient id, activity w/ default "1", columns w/ default all, limit - 10, start - 0)
returns a multi-dimensional array or record lines returned.

addPnote(patient id, body text, authorized w/ default "0", activity w/ default "1")
returns the id of the insert.

authorizePnote(id, authorize w/ default "1")
returns useless data. if you want it to return something let us know.

disappearPnote (id)
returns true

reappearPnote (id)
returns true

----- library/onotes.inc - Ted Dziuba, Matthew Beale -----
used for accessing openemr.onotes, we require('sql.inc')

getOnoteById(id, cols wanted w/default all)
returns the first record returned, the most recent

getOnoteByDate(date, activity w/ default "1" (can take 0, 1, or all), cols wanted w/default all,limit - "all", offset w/ default "0")
returns a multi-dimensional array or record of lines returned

addOnote(body text, $activity w/ default "1")
returns the id you just generated, dont you feel lucky.

disappearOnote (id)
returns true

reappearOnote (id)
returns true

----- library/forms.inc - Ted Dziuba, Matthew Koch -----

getFormById(id, cols - "*")
returns form with specified id

getFormsByPID (pid, columns w/ default all)
returns form with specified PID

getFormByEncounter(pid, encounter, columns w/ default "form_id, form_name")
return forms matching encounter in multi-dimensional array

addForm(encounter, form_name, form_id, formdir, pid, authorized w/ default "0")
adds form with specified values, returns id

authorizeForm(id, authorize w/ default "1")
returns useless data. if you want it to return something let us know.

function getEncounters($pid)
returns array of all encounters filed under a given pid, in order by date descending

----- library/billing.inc - Matthew Beale, Ted Dziuba -----

getBillingById(id, cols w/ default all)
returns billing record from id

getBillingByPid(pid, columns w/ default all)
returns billing record from pid

getBillingByEncounter(pid,encounter id, columns w/ defaults "code_type, code, code_text")
return billings matching encounter in multi-dimensional array

addBilling(encounter, code_type, code, $code_text, pid, authorized w/ default "0")
adds billing entry with values, returns id

authorizeBilling(id, authorize w/ default "1")
returns useless data. if you want it to return something let us know

function deleteBilling($id)
makes a billing entry not active - misnomer - does not actually delete it, but makes it invisible to interface
----- library/log.inc - Matthew Beale, Ted Dziuba -----

newEvent(event(login||logout||backup||view), user, groupname, comment w/ default "")
return id generated in insert

getEventByDate($date, $cols w/ default all)
returns nested array of results


----- library/patient.inc - Matthew Beale, Ted Dziuba -----

DEPRECATED
getPatientById(id, cols w/default *, type w/ default "primary") << "table_name_col_name, table2_name_col2_name"
return array of patient information in cols of the patient with pid id

DEPRECATED
getPatientByLname(lname, cols w/default *, type w/ default "primary") << as above
return array of patient information in cols of the patient with lname

DEPRECATED
getPatients(cols w/default "patient_data_name, patient_data_fname, patient_data_lname")
returns nested array of said values.

newPatientData(		$title, //all default "" unless specified
			$fname,
                        $lname,
                        $mname,
                        $sex,
                        $DOB,
                        $street,
                        $postal_code,
                        $city,
                        $state,
                        $country_code,
                        $ss,
                        $occupation,
                        $phone_home,
                        $phone_biz,
                        $phone_contact,
                        $contact_relationship,
			$pid w/ default "MAX(pid)+1"
		)
returns generated pid, new or old.

function newEmployerData(       $pid,
				$name, //all default "" unless specified
                                $street,
                                $postal_code,
                                $city,
                                $state,
                                $country
                        )
returns generated id

function newInsuranceData(      $pid,
				$type, //all default "" unless specified
                                $provider,
                                $policy_number,
                                $group_number,
                                $subscriber,
                                $subscriber_relationship,
                                $subscriber_ss,
                                $subscriber_DOB,
                                $subscriber_street,
                                $subscriber_postal_code,
                                $subscriber_city,
                                $subscriber_state,
                                $subscriber_country,
                                $subscriber_employer,
                                $subscriber_employer_street,
                                $subscriber_employer_postal_code,
                                $subscriber_employer_state,
                                $subscriber_employer_country,
                                $copay
                        )
returns generated id

function newHistoryData(        $pid,
				$coffee, //all default "" unless specified
                                $tobacco,
                                $alchohol,
                                $sleep_patterns,
                                $exercise_patterns,
                                $seatbelt_use,
                                $counseling,
                                $hazardous_activities,
                                $last_breast_exam,
                                $last_mammogram,
                                $last_gynocological_exam,
                                $last_rectal_exam,
                                $last_prostate_exam,
                                $last_phyiscal_exam,
                                $last_sigmoidoscopy_colonoscopy,
                                $history_mother,
                                $history_father,
                                $history_siblings,
                                $history_offspring,
                                $history_spouse,
                                $relatives_cancer,
                                $relatives_tuberculosis,
                                $relatives_diabetes,
                                $relatives_high_blood_pressite,
                                $relatives_heart_problems,
                                $relatives_stroke,
                                $relatives_epilepsy,
                                $relatives_mental_illness,
                                $relatives_suicide,
                                $cataract_surgery,
                                $tonsillectomy,
                                $appendectomy,
                                $cholecystestomy,
                                $heart_surgery,
                                $hysterectomy,
                                $hernia_repair,
                                $hip_replacement
                                )
returns id generated

updatePatientData(pid, array of new datas:
	$passed['colname'] = "value string"
returns id generated

updateInsuranceData(pid, array of new datas:
        $passed['colname'] = "value string"
returns id generated

updateEmployerData(pid, array of new datas:
        $passed['colname'] = "value string"
returns id generated
	
updateHistoryData(pid, array of new datas:
        $passed['colname'] = "value string"
returns id generated
	
getPatientData(pid, cols w/ default *)
returns array of cols specified by given with pid id

getHistoryData(pid, cols w/ default *)
returns array of cols specified by given with pid id

getInsuranceData(pid, type w/ default primary, cols w/ default *)
return array of cols specified by given from patient with pid id and type primary or secondary

getEmployerData(pid, cols w/ default *)
return array of cols specified by given of patient with pid id

getPatientLnames(lname, cols w/default = "pid, lname, fname, mname, id", orderby w/ default "lname ASC", limit w/ default "all", start w/ default "0")
return array of cols specified by given of patients with last name lname

getPatientIds(cols w/ default "pid, id, lname, fname, mname", orderby w/ default "id ASC", limit w/ default "all", start w/ default "0")
returns array of cols specified by cols of all unique pids

----- library/report.inc - Matthew Beale -----

getPatientReport(pid)
returns nested array of column name containing 'content' at time of change and 'date'

getEmployeeReport(pid)
returns nested array of column name containing 'content' at time of change and 'date'

getHistoryReport(pid)
returns nested array of column name containing 'content' at time of change and 'date

getInsuranceReport(pid, type)
returns nested array of column name containing 'content' at time of change and 'date


----- library/encounter.inc - Matthew Stack -----

function setencounter(encounter)
set the global encounter session variable - if encounter is "", then it creates an entry in the forms table to represent a blank encounter

----- library/pid.inc - Matthew Stack -----

function setpid(pid)
set the global pid session variable










