if ($sale_id) { if ($del) { // Zero out this sale and reverse its inventory update. We bring in // drug_sales twice so that the original quantity can be referenced // unambiguously. sqlStatement("UPDATE drug_sales AS dsr, drug_sales AS ds, " . "drug_inventory AS di " . "SET di.on_hand = di.on_hand + dsr.quantity, " . "ds.quantity = 0, ds.fee = 0 WHERE " . "dsr.sale_id = ? AND ds.sale_id = dsr.sale_id AND " . "di.inventory_id = ds.inventory_id", array($sale_id)); // And delete the sale for good measure. sqlStatement("DELETE FROM drug_sales WHERE sale_id = ?", array($sale_id)); } else { // Modify the sale and adjust inventory accordingly. $query = "UPDATE drug_sales AS dsr, drug_sales AS ds, " . "drug_inventory AS di " . "SET di.on_hand = di.on_hand + dsr.quantity - " . add_escape_custom($units) . ", " . "ds.quantity = ?, ds.fee = ?, " . "ds.sale_date = ? WHERE " . "dsr.sale_id = ? AND ds.sale_id = dsr.sale_id AND " . "di.inventory_id = ds.inventory_id"; sqlStatement($query, array($units, $fee, $visit_date, $sale_id)); } } else { if (!$del) { $sale_id = sellDrug($drug_id, $units, $fee, $pid, $encounter, 0, $visit_date, '', $default_warehouse); if (!$sale_id) { die(xlt("Insufficient inventory for product ID") . " \"" . text($drug_id) . "\"."); } } } } // end for // Set the main/default service provider in the new-encounter form. /******************************************************************* sqlStatement("UPDATE forms, users SET forms.user = users.username WHERE " . "forms.pid = '$pid' AND forms.encounter = '$encounter' AND " . "forms.formdir = 'newpatient' AND users.id = '$provid'"); *******************************************************************/ sqlStatement("UPDATE form_encounter SET provider_id = ?, " . "supervisor_id = ? WHERE " . "pid = ? AND encounter = ?", array($main_provid, $main_supid, $pid, $encounter)); // Save-and-Close is currently IPPF-specific but might be more generally
if (!$quantity) { $quantity = 0; } if (!$fee) { $fee = 0.0; } $inventory_id = 0; $bad_lot_list = ''; $today = date('Y-m-d'); // If there is no sale_id then this is a new dispensation. // if (!$sale_id) { // Post the order and update inventory, deal with errors. // if ($drug_id) { $sale_id = sellDrug($drug_id, $quantity, $fee, $pid, 0, $prescription_id, $today, $user); if (!$sale_id) { die(xl('Inventory is not available for this order.')); } /****************************************************************** $res = sqlStatement("SELECT * FROM drug_inventory WHERE " . "drug_id = '$drug_id' AND on_hand > 0 AND destroy_date IS NULL " . "ORDER BY expiration, inventory_id"); while ($row = sqlFetchArray($res)) { if ($row['expiration'] > $today && $row['on_hand'] >= $quantity) { break; } $tmp = $row['lot_number']; if (! $tmp) $tmp = '[missing lot number]'; if ($bad_lot_list) $bad_lot_list .= ', '; $bad_lot_list .= $tmp;
public function save(&$bill, &$prod, $main_provid = NULL, $main_supid = NULL, $default_warehouse = NULL, $mark_as_closed = false) { global $code_types; if (isset($main_provid) && $main_supid == $main_provid) { $main_supid = 0; } $copay_update = FALSE; $update_session_id = ''; $ct0 = ''; // takes the code type of the first fee type code type entry from the fee sheet, against which the copay is posted $cod0 = ''; // takes the code of the first fee type code type entry from the fee sheet, against which the copay is posted $mod0 = ''; // takes the modifier of the first fee type code type entry from the fee sheet, against which the copay is posted if (is_array($bill)) { foreach ($bill as $iter) { // Skip disabled (billed) line items. if (!empty($iter['billed'])) { continue; } $id = $iter['id']; $code_type = $iter['code_type']; $code = $iter['code']; $del = !empty($iter['del']); $units = empty($iter['units']) ? 1 : intval($iter['units']); $price = empty($iter['price']) ? 0 : 0 + trim($iter['price']); $pricelevel = empty($iter['pricelevel']) ? '' : $iter['pricelevel']; $modifier = empty($iter['mod']) ? '' : trim($iter['mod']); $justify = empty($iter['justify']) ? '' : trim($iter['justify']); $notecodes = empty($iter['notecodes']) ? '' : trim($iter['notecodes']); $provid = empty($iter['provid']) ? 0 : intval($iter['provid']); $fee = sprintf('%01.2f', $price * $units); if (!$cod0 && $code_types[$code_type]['fee'] == 1) { $mod0 = $modifier; $cod0 = $code; $ct0 = $code_type; } if ($code_type == 'COPAY') { if ($fee < 0) { $fee = $fee * -1; } if (!$id) { // adding new copay from fee sheet into ar_session and ar_activity tables $session_id = idSqlStatement("INSERT INTO ar_session " . "(payer_id, user_id, pay_total, payment_type, description, patient_id, payment_method, " . "adjustment_code, post_to_date) " . "VALUES ('0',?,?,'patient','COPAY',?,'','patient_payment',now())", array($_SESSION['authId'], $fee, $this->pid)); sqlBeginTrans(); $sequence_no = sqlQuery("SELECT IFNULL(MAX(sequence_no),0) + 1 AS increment FROM ar_activity WHERE " . "pid = ? AND encounter = ?", array($this->pid, $this->encounter)); SqlStatement("INSERT INTO ar_activity (pid, encounter, sequence_no, code_type, code, modifier, " . "payer_type, post_time, post_user, session_id, " . "pay_amount, account_code) VALUES (?,?,?,?,?,?,0,now(),?,?,?,'PCP')", array($this->pid, $this->encounter, $sequence_no['increment'], $ct0, $cod0, $mod0, $_SESSION['authId'], $session_id, $fee)); sqlCommitTrans(); } else { // editing copay saved to ar_session and ar_activity $session_id = $id; $res_amount = sqlQuery("SELECT pay_amount FROM ar_activity WHERE pid=? AND encounter=? AND session_id=?", array($this->pid, $this->encounter, $session_id)); if ($fee != $res_amount['pay_amount']) { sqlStatement("UPDATE ar_session SET user_id=?,pay_total=?,modified_time=now(),post_to_date=now() WHERE session_id=?", array($_SESSION['authId'], $fee, $session_id)); sqlStatement("UPDATE ar_activity SET code_type=?, code=?, modifier=?, post_user=?, post_time=now()," . "pay_amount=?, modified_time=now() WHERE pid=? AND encounter=? AND account_code='PCP' AND session_id=?", array($ct0, $cod0, $mod0, $_SESSION['authId'], $fee, $this->pid, $this->encounter, $session_id)); } } if (!$cod0) { $copay_update = TRUE; $update_session_id = $session_id; } continue; } # Code to create justification for all codes based on first justification if ($GLOBALS['replicate_justification'] == '1') { if ($justify != '') { $autojustify = $justify; } } if ($GLOBALS['replicate_justification'] == '1' && $justify == '' && check_is_code_type_justify($code_type)) { $justify = $autojustify; } if ($justify) { $justify = str_replace(',', ':', $justify) . ':'; } $auth = "1"; $ndc_info = ''; if (!empty($iter['ndcnum'])) { $ndc_info = 'N4' . trim($iter['ndcnum']) . ' ' . $iter['ndcuom'] . trim($iter['ndcqty']); } // If the item is already in the database... if ($id) { if ($del) { $this->logFSMessage(xl('Service deleted')); deleteBilling($id); } else { $tmp = sqlQuery("SELECT * FROM billing WHERE id = ? AND (billed = 0 or billed is NULL) AND activity = 1", array($id)); if (!empty($tmp)) { $tmparr = array('code' => $code, 'authorized' => $auth); if (isset($iter['units'])) { $tmparr['units'] = $units; } if (isset($iter['price'])) { $tmparr['fee'] = $fee; } if (isset($iter['pricelevel'])) { $tmparr['pricelevel'] = $pricelevel; } if (isset($iter['mod'])) { $tmparr['modifier'] = $modifier; } if (isset($iter['provid'])) { $tmparr['provider_id'] = $provid; } if (isset($iter['ndcnum'])) { $tmparr['ndc_info'] = $ndc_info; } if (isset($iter['justify'])) { $tmparr['justify'] = $justify; } if (isset($iter['notecodes'])) { $tmparr['notecodes'] = $notecodes; } foreach ($tmparr as $key => $value) { if ($tmp[$key] != $value) { if ('fee' == $key) { $this->logFSMessage(xl('Price changed')); } if ('units' == $key) { $this->logFSMessage(xl('Quantity changed')); } if ('provider_id' == $key) { $this->logFSMessage(xl('Service provider changed')); } sqlStatement("UPDATE billing SET `{$key}` = ? WHERE id = ?", array($value, $id)); } } } } } else { if (!$del) { $this->logFSMessage(xl('Service added')); $code_text = lookup_code_descriptions($code_type . ":" . $code); addBilling($this->encounter, $code_type, $code, $code_text, $this->pid, $auth, $provid, $modifier, $units, $fee, $ndc_info, $justify, 0, $notecodes, $pricelevel); } } } } // end for // if modifier is not inserted during loop update the record using the first // non-empty modifier and code if ($copay_update == TRUE && $update_session_id != '' && $mod0 != '') { sqlStatement("UPDATE ar_activity SET code_type = ?, code = ?, modifier = ?" . " WHERE pid = ? AND encounter = ? AND account_code = 'PCP' AND session_id = ?", array($ct0, $cod0, $mod0, $this->pid, $this->encounter, $update_session_id)); } // Doing similarly to the above but for products. if (is_array($prod)) { foreach ($prod as $iter) { // Skip disabled (billed) line items. if (!empty($iter['billed'])) { continue; } $drug_id = $iter['drug_id']; $selector = empty($iter['selector']) ? '' : $iter['selector']; $sale_id = $iter['sale_id']; // present only if already saved $units = max(1, intval(trim($iter['units']))); $price = empty($iter['price']) ? 0 : 0 + trim($iter['price']); $pricelevel = empty($iter['pricelevel']) ? '' : $iter['pricelevel']; $fee = sprintf('%01.2f', $price * $units); $del = !empty($iter['del']); $rxid = 0; $warehouse_id = empty($iter['warehouse']) ? '' : $iter['warehouse']; $somechange = false; // If the item is already in the database... if ($sale_id) { $tmprow = sqlQuery("SELECT ds.prescription_id, ds.quantity, ds.inventory_id, ds.fee, " . "ds.sale_date, di.warehouse_id " . "FROM drug_sales AS ds " . "LEFT JOIN drug_inventory AS di ON di.inventory_id = ds.inventory_id " . "WHERE ds.sale_id = ?", array($sale_id)); $rxid = 0 + $tmprow['prescription_id']; if ($del) { if (!empty($tmprow)) { // Delete this sale and reverse its inventory update. $this->logFSMessage(xl('Product deleted')); sqlStatement("DELETE FROM drug_sales WHERE sale_id = ?", array($sale_id)); if (!empty($tmprow['inventory_id'])) { sqlStatement("UPDATE drug_inventory SET on_hand = on_hand + ? WHERE inventory_id = ?", array($tmprow['quantity'], $tmprow['inventory_id'])); } } if ($rxid) { sqlStatement("DELETE FROM prescriptions WHERE id = ?", array($rxid)); } } else { // Modify the sale and adjust inventory accordingly. if (!empty($tmprow)) { foreach (array('quantity' => $units, 'fee' => $fee, 'pricelevel' => $pricelevel, 'selector' => $selector, 'sale_date' => $this->visit_date) as $key => $value) { if ($tmprow[$key] != $value) { $somechange = true; if ('fee' == $key) { $this->logFSMessage(xl('Price changed')); } if ('pricelevel' == $key) { $this->logFSMessage(xl('Price level changed')); } if ('selector' == $key) { $this->logFSMessage(xl('Template selector changed')); } if ('quantity' == $key) { $this->logFSMessage(xl('Quantity changed')); } sqlStatement("UPDATE drug_sales SET `{$key}` = ? WHERE sale_id = ?", array($value, $sale_id)); if ($key == 'quantity' && $tmprow['inventory_id']) { sqlStatement("UPDATE drug_inventory SET on_hand = on_hand - ? WHERE inventory_id = ?", array($units - $tmprow['quantity'], $tmprow['inventory_id'])); } } } if ($tmprow['inventory_id'] && $warehouse_id && $warehouse_id != $tmprow['warehouse_id']) { // Changing warehouse. Requires deleting and re-adding the sale. // Not setting $somechange because this alone does not affect a prescription. $this->logFSMessage(xl('Warehouse changed')); sqlStatement("DELETE FROM drug_sales WHERE sale_id = ?", array($sale_id)); sqlStatement("UPDATE drug_inventory SET on_hand = on_hand + ? WHERE inventory_id = ?", array($units, $tmprow['inventory_id'])); $tmpnull = null; $sale_id = sellDrug($drug_id, $units, $fee, $this->pid, $this->encounter, empty($iter['rx']) ? 0 : $rxid, $this->visit_date, '', $warehouse_id, false, $tmpnull, $pricelevel, $selector); } } // Delete Rx if $rxid and flag not set. if ($GLOBALS['gbl_auto_create_rx'] && $rxid && empty($iter['rx'])) { sqlStatement("UPDATE drug_sales SET prescription_id = 0 WHERE sale_id = ?", array($sale_id)); sqlStatement("DELETE FROM prescriptions WHERE id = ?", array($rxid)); } } } else { if (!$del) { $somechange = true; $this->logFSMessage(xl('Product added')); $tmpnull = null; $sale_id = sellDrug($drug_id, $units, $fee, $this->pid, $this->encounter, 0, $this->visit_date, '', $warehouse_id, false, $tmpnull, $pricelevel, $selector); if (!$sale_id) { die(xlt("Insufficient inventory for product ID") . " \"" . text($drug_id) . "\"."); } } } // If a prescription applies, create or update it. if (!empty($iter['rx']) && !$del && ($somechange || empty($rxid))) { // If an active rx already exists for this drug and date we will // replace it, otherwise we'll make a new one. if (empty($rxid)) { $rxid = ''; } // Get default drug attributes; prefer the template with the matching selector. $drow = sqlQuery("SELECT dt.*, " . "d.name, d.form, d.size, d.unit, d.route, d.substitute " . "FROM drugs AS d, drug_templates AS dt WHERE " . "d.drug_id = ? AND dt.drug_id = d.drug_id " . "ORDER BY (dt.selector = ?) DESC, dt.quantity, dt.dosage, dt.selector LIMIT 1", array($drug_id, $selector)); if (!empty($drow)) { $rxobj = new Prescription($rxid); $rxobj->set_patient_id($this->pid); $rxobj->set_provider_id(isset($main_provid) ? $main_provid : $this->provider_id); $rxobj->set_drug_id($drug_id); $rxobj->set_quantity($units); $rxobj->set_per_refill($units); $rxobj->set_start_date_y(substr($this->visit_date, 0, 4)); $rxobj->set_start_date_m(substr($this->visit_date, 5, 2)); $rxobj->set_start_date_d(substr($this->visit_date, 8, 2)); $rxobj->set_date_added($this->visit_date); // Remaining attributes are the drug and template defaults. $rxobj->set_drug($drow['name']); $rxobj->set_unit($drow['unit']); $rxobj->set_dosage($drow['dosage']); $rxobj->set_form($drow['form']); $rxobj->set_refills($drow['refills']); $rxobj->set_size($drow['size']); $rxobj->set_route($drow['route']); $rxobj->set_interval($drow['period']); $rxobj->set_substitute($drow['substitute']); // $rxobj->persist(); // Set drug_sales.prescription_id to $rxobj->get_id(). $oldrxid = $rxid; $rxid = 0 + $rxobj->get_id(); if ($rxid != $oldrxid) { sqlStatement("UPDATE drug_sales SET prescription_id = ? WHERE sale_id = ?", array($rxid, $sale_id)); } } } } } // end for // Set default and/or supervising provider for the encounter. if (isset($main_provid) && $main_provid != $this->provider_id) { $this->logFSMessage(xl('Default provider changed')); sqlStatement("UPDATE form_encounter SET provider_id = ? WHERE pid = ? AND encounter = ?", array($main_provid, $this->pid, $this->encounter)); $this->provider_id = $main_provid; } if (isset($main_supid) && $main_supid != $this->supervisor_id) { sqlStatement("UPDATE form_encounter SET supervisor_id = ? WHERE pid = ? AND encounter = ?", array($main_supid, $this->pid, $this->encounter)); $this->supervisor_id = $main_supid; } // Save-and-Close is currently specific to Family Planning but might be more // generally useful. It provides the ability to mark an encounter as billed // directly from the Fee Sheet, if there are no charges. if ($mark_as_closed) { $tmp1 = sqlQuery("SELECT SUM(ABS(fee)) AS sum FROM drug_sales WHERE " . "pid = ? AND encounter = ? AND billed = 0", array($this->pid, $this->encounter)); $tmp2 = sqlQuery("SELECT SUM(ABS(fee)) AS sum FROM billing WHERE " . "pid = ? AND encounter = ? AND billed = 0 AND activity = 1", array($this->pid, $this->encounter)); if ($tmp1['sum'] + $tmp2['sum'] == 0) { sqlStatement("update drug_sales SET billed = 1 WHERE " . "pid = ? AND encounter = ? AND billed = 0", array($this->pid, $this->encounter)); sqlStatement("UPDATE billing SET billed = 1, bill_date = NOW() WHERE " . "pid = ? AND encounter = ? AND billed = 0 AND activity = 1", array($this->pid, $this->encounter)); } else { // Would be good to display an error message here... they clicked // Save and Close but the close could not be done. However the // framework does not provide an easy way to do that. } } }
public function genWarehouseSelect($tagname, $toptext, $default = '', $disabled = false, $drug_id = 0, $is_sold = 0) { $s = ''; if ($this->got_warehouses) { // Normally would use generate_select_list() but it's not flexible enough here. $s .= "<select name='" . attr($tagname) . "'"; if (!$disabled) { $s .= " onchange='warehouse_changed(this);'"; } if ($disabled) { $s .= " disabled"; } $s .= ">"; $s .= "<option value=''>" . text($toptext) . "</option>"; $lres = sqlStatement("SELECT * FROM list_options " . "WHERE list_id = 'warehouse' AND activity = 1 ORDER BY seq, title"); while ($lrow = sqlFetchArray($lres)) { $s .= "<option value='" . attr($lrow['option_id']) . "'"; if ($disabled) { if ($lrow['option_id'] == $default) { $s .= " selected"; } } else { $has_inventory = sellDrug($drug_id, 1, 0, 0, 0, 0, '', '', $lrow['option_id'], true); if ((strlen($default) == 0 && $lrow['is_default'] || strlen($default) > 0 && $lrow['option_id'] == $default) && ($is_sold || $has_inventory)) { $s .= " selected"; } else { // Disable this warehouse option if not selected and has no inventory. if (!$has_inventory) { $s .= " disabled"; } } } $s .= ">" . text(xl_list_label($lrow['title'])) . "</option>\n"; } $s .= "</select>"; } return $s; }