<?php
define('FOLDER_NAME', 'rx_optical_adabraka/');
set_time_limit(90000);

function getinsurance($ins_code){
$query_rsodest_insurer = "SELECT * FROM insurance_company where company_code='$ins_code'";
$rsodest_insurer = mysql_query($query_rsodest_insurer) or die(mysql_error());
$row_rsodest_insurer=mysql_fetch_assoc($rsodest_insurer);
$ccode=$row_rsodest_insurer['company_name'];
return $ccode;
}

function searchNameById($tableName,$nameColumn,$id,$idd) {
    $query = "SELECT $nameColumn FROM $tableName WHERE $idd='$id'";
    $result = mysql_query($query) or die(mysql_error());
    $array = mysql_fetch_assoc($result);
    return $array[$nameColumn];
}

function referral_name_check($name, $process_claim_no, $courier){
    $check = mysql_num_rows(mysql_query("select * from medical_history_referral_place where referral_name = '$name' and process_claim_no = '$process_claim_no'", $courier));
    if($check > 0){
        echo 'checked=""';
    }
    
}

function get_patient_symptoms($claim_no, $courier){
    $query = mysql_query("select * from claims_details_symptoms where process_claim_no = '$claim_no'", $courier);
    $i=1;
    while ($row = mysql_fetch_assoc($query)) {
         $result .= '('.$i.') '. $row['symptoms'].'<br>';
        $i++;
    } if(strlen($result) > 0){ ?>
        <div id="symptoms_symptoms_<?php echo $i; ?>" style="display: block;"><?php echo substr($result, 0, 40) ?><i onclick="$('#symptoms_<?php echo $i; ?>').show();$('#symptoms_symptoms_<?php echo $i; ?>').hide();" style="color:red;cursor:pointer;">...See More</i></div>
        <div id="symptoms_<?php echo $i; ?>" style="display: none;"><?php echo $result; ?> <i onclick="$('#symptoms_symptoms_<?php echo $i; ?>').show();$('#symptoms_<?php echo $i; ?>').hide();" style="color:red;cursor:pointer;">...See Less</i></div>
    
    <?php } }

function get_patient_diagnosis($claim_no, $courier) {
    $query = mysql_query("select * from claims_details_query_diagnosis where process_claim_no = '$claim_no'", $courier);
    $i = 1;
    while ($row = mysql_fetch_assoc($query)) {
        $result .= '(' . $i . ') ' . $row['disease_name'] . '<br>';
        $i++;
    } if(strlen($result) > 0){ ?>
        <div id="diagnosis_diagnosis_<?php echo $i; ?>" style="display: block;"><?php echo substr($result, 0, 40) ?><i onclick="$('#diagnosis_<?php echo $i; ?>').show();$('#diagnosis_diagnosis_<?php echo $i; ?>').hide();" style="color:red;cursor:pointer;">...See More</i></div>
        <div id="diagnosis_<?php echo $i; ?>" style="display: none;"><?php echo $result; ?> <i onclick="$('#diagnosis_diagnosis_<?php echo $i; ?>').show();$('#diagnosis_<?php echo $i; ?>').hide();" style="color:red;cursor:pointer;">...See Less</i></div>
    <?php }
}

function get_patient_medical_note($claim_no, $i, $courier){
    $query = mysql_query("select * from medical_history where process_claim_no = '$claim_no'", $courier);
    $row = mysql_fetch_assoc($query); if(strlen($row['medical_note']) > 0){ ?>
    <div id="note_note_<?php echo $i; ?>" style="display: block;"><?php echo substr($row['medical_note'], 0,40)?><i onclick="$('#note_<?php echo $i;?>').show();$('#note_note_<?php echo $i;?>').hide();" style="color:red;cursor:pointer;">...See More</i></div>
    <div id="note_<?php echo $i; ?>" style="display: none;"><?php echo $row['medical_note']?> <i onclick="$('#note_note_<?php echo $i;?>').show();$('#note_<?php echo $i;?>').hide();" style="color:red;cursor:pointer;">...See Less</i></div>
<?php } }


 function patient_referral_place($process_claim_no, $courier) {
    $qry = mysql_query("select * from medical_history_referral_place where process_claim_no ='$process_claim_no'", $courier);
    while ($row = mysql_fetch_assoc($qry)) {
        echo $row['referral_name'];
    }
}


function SMS_API_RETURN($phone, $message, $sender_id) {
    $nosms = ceil(strlen($message) / 160);
    $key = "B8y19qHznfmmZ7QSA690Wi4cU";
    $message2 = urlencode($message);
    $to = trim($phone);
    $URL = "http://bulk.mnotify.net/smsapi?key=$key&to=$to&msg=$message2&sender_id=$sender_id";
    $result = file_get_contents($URL);
//    if ($result == "1000") {
//        mysql_query("INSERT INTO sms (sms_content,no_of_sms,tel,user_id) VALUES('$message','$nosms','$phone','".$_SESSION['myMM_Userid']."')");
//        return true;
//    }
}

function UserFullName($id, $courier){
  $LoginRS__query="SELECT * FROM users WHERE id='$id'";
  $LoginRS = mysql_query($LoginRS__query, $courier) or die(mysql_error());
  $row_LoginRS = mysql_fetch_assoc($LoginRS);
  
  return $row_LoginRS['title'].' '.$row_LoginRS['firstnames'].' '.$row_LoginRS['surname'];
}

function timeAgo($timestamp) {
    $datetime1 = new DateTime("now");
    $datetime2 = date_create($timestamp);
    $diff = date_diff($datetime1, $datetime2);
    $timemsg = '';
    if ($diff->y > 0) {
        $timemsg = $diff->y . ' year' . ($diff->y > 1 ? "s" : '');
    } else if ($diff->m > 0) {
        $timemsg = $diff->m . ' month' . ($diff->m > 1 ? "s" : '');
    } else if ($diff->d > 0) {
        $timemsg = $diff->d . ' day' . ($diff->d > 1 ? "s" : '');
    } else if ($diff->h > 0) {
        $timemsg = $diff->h . ' hour' . ($diff->h > 1 ? "s" : '');
    } else if ($diff->i > 0) {
        $timemsg = $diff->i . ' minute' . ($diff->i > 1 ? "s" : '');
    } else if ($diff->s > 0) {
        $timemsg = $diff->s . ' second' . ($diff->s > 1 ? "s" : '');
    }
    $timemsg = $timemsg;
    return $timemsg;
}


function ordinal($number) {
    $ends = array('th','st','nd','rd','th','th','th','th','th','th');
    if ((($number % 100) >= 11) && (($number%100) <= 13))
        return $number. 'th';
    else
        return $number. $ends[$number % 10];
}


function getpicpath($ins_code){
$query_rsodest_insurer = "SELECT * FROM insurance_company where company_code='$ins_code'";
$rsodest_insurer = mysql_query($query_rsodest_insurer) or die(mysql_error());
$row_rsodest_insurer=mysql_fetch_assoc($rsodest_insurer);
$ccode=$row_rsodest_insurer['pic_path'];
return $ccode;	
}



function getnoofpatients($user_code){
$query_rsodest_insurer = "SELECT count(id) as noofpatients FROM process_claim_no where attending_officer_id='$user_code' and consulting_room_status='pending'";
$rsodest_insurer = mysql_query($query_rsodest_insurer) or die(mysql_error());
$row_rsodest_insurer=mysql_fetch_assoc($rsodest_insurer);
$ccode=$row_rsodest_insurer['noofpatients'];
return $ccode;	
}

function patient_sub_info($no, $courier){
    $qry = mysql_query("select picpath, insurance_no1, insurance_company1,surname,othernames from patient_information where card_number = '$no'", $courier);
    $row = mysql_fetch_assoc($qry);
    return array($row['picpath'], $row['insurance_no1'], $row['insurance_company1'],$row['othernames'],$row['surname']);
}


function getprovider($pid){
$query_rsodest = "select * from provider_api where provider_id='$pid' order by facility_name";
	$rsodest = mysql_query($query_rsodest) or die(mysql_error());
	$row_rsodest = mysql_fetch_assoc($rsodest);
	$provider_name=$row_rsodest['facility_name'];
return $provider_name;	
}

function branchName($id, $courier){
  $LoginRS__query="SELECT * FROM branches WHERE id='$id'";
  $LoginRS = mysql_query($LoginRS__query, $courier) or die(mysql_error());
  $row_LoginRS = mysql_fetch_assoc($LoginRS);
  return $row_LoginRS['branch_name'];
}

function quantityAvailable($id, $branch_code=NULL) {
    if($branch_code == NULL){
        $qty_aval_text = 'quantity_available_'.$_SESSION['name_code'] ;
        $info = mysql_fetch_assoc(mysql_query("SELECT $qty_aval_text FROM service_items WHERE id = '$id'"));
    }else{
        $qty_info = mysql_fetch_assoc(mysql_query("SELECT name_code FROM branches WHERE id='$branch_code'"));
        $qty_aval_text = 'quantity_available_'.$qty_info['name_code'] ;
        $info = mysql_fetch_assoc(mysql_query("SELECT $qty_aval_text FROM service_items WHERE id = '$id' "));
    }
    return $info[$qty_aval_text];
}

function quantityAvailable2($id) {
    $info = mysql_fetch_assoc(mysql_query("SELECT quantity_available FROM stock_items WHERE item_id = '$id'"));
    return $info['quantity_available'];
}

function checkPatientLensAddition($process_claim_no, $addition, $id=1){
    $count = mysql_num_rows(mysql_query("SELECT * FROM patient_lens_addition WHERE process_claim_no='$process_claim_no' AND addition = '$addition' AND lens_id_count='$id'"));
    if($count > 0){
        echo 'checked=""';
    }else{
        echo '';
    }
}

function PatientLensAddition($process_claim_no,$id=1) {
    $all = mysql_query("SELECT * FROM patient_lens_addition WHERE process_claim_no='$process_claim_no' AND lens_id_count='$id'");
    while ($row = mysql_fetch_assoc($all)) {
        $additions.= $row['addition'].',';
    }
    return substr($additions, 0,-1);
}


function fetchDataMore($table, $data, $where = NULL, $rawdata = NULL) {
    $fields = array();
    $valstr = array();

    foreach ($data as $key) {
        $fields[] = $key;
    }

    if ($where != NULL) {
        foreach ($where as $key => $val) {
            $valstr[] = $key . " = '" . $val . "'";
        }
    }

    if ($where == NULL) {
        $sql = mysql_query("SELECT " . implode(", ", $fields) . " FROM " . $table . " $rawdata");
    } else {
        $sql = mysql_query("SELECT " . implode(", ", $fields) . " FROM " . $table . " WHERE " . implode(" AND ", $valstr) . " $rawdata");
    }
    while ($rows2 = mysql_fetch_assoc($sql)) {
        $rows[] = $rows2;
    }

    $totalRows = mysql_num_rows($sql);
    return array($rows, $totalRows);
}

function fetchDataMore2($conn, $table, $data, $where = NULL, $rawdata = NULL) {
    $fields = array();
    $valstr = array();

    foreach ($data as $key) {
        $fields[] = $key;
    }

    if ($where != NULL) {
        foreach ($where as $key => $val) {
            $valstr[] = $key . " = '".$val."'";
        }
    }

    if ($where == NULL) {
        $sql = "SELECT " . implode(", ", $fields) . " FROM " . $table . " $rawdata";
    } else {
        $sql = "SELECT " . implode(", ", $fields) . " FROM " . $table . " WHERE " . implode(" AND ", $valstr) . " $rawdata";
    }

//    $rows = mysqli_fetch_all($sql, MYSQLI_ASSOC);
//    $totalRows = mysqli_num_rows($sql);
    return array($sql, $totalRows);
}

function fetchDataOne($table, $data, $where = NULL, $rawdata = NULL) {
    $fields = array();
    $valstr = array();

    foreach ($data as $key) {
        $fields[] = $key;
    }

    if ($where != NULL) {
        foreach ($where as $key => $val) {
            $valstr[] = $key . " = '".$val."'";
        }
    }
    if ($where == NULL) {
        $sql = mysql_query("SELECT " . implode(", ", $fields) . " FROM " . $table . " $rawdata");
    } else {
        $sql = mysql_query("SELECT " . implode(", ", $fields) . " FROM " . $table . " WHERE " . implode(" AND ", $valstr) . " $rawdata");
    }

    $rows = mysql_fetch_assoc($sql);
    $totalRows = mysql_num_rows($sql);
    return array($rows, $totalRows);
}

function InsertStatementSql($conn, $table, $data) {
    $fields = array();
    $values = array();
    foreach ($data as $key => $val) {
        $fields[] = $key;
        $values[] = $val;
    }
    mysqli_query($conn, "INSERT INTO " . $table . " (" . implode(', ', $fields) . ") VALUES ('" . implode("', '", $values) . "')") or die(mysql_error());
    return mysqli_insert_id($conn);
}

function InsertStatementSqlString($table, $data) {
    $fields = array();
    $values = array();
    foreach ($data as $key => $val) {
        $fields[] = $key;
        $values[] = $val;
    }
    $result = "INSERT INTO " . $table . " (" . implode(', ', $fields) . ") VALUES ('" . implode("', '", $values) . "');";
    return $result;
}

function updateStatementSqlString($table, $data, $where, $rawdata=NULL) {
    $fields = array();
    $valstr = array();
    
    foreach ($data as $key => $val) {
        $fields[] = $key . " = '".$val."'";
    }

    foreach ($where as $key => $val) {
        $valstr[] = $key . " = '".$val."'";
    }

    $result =  "UPDATE $table SET " . implode(", ", $fields) . " WHERE " . implode(" AND ", $valstr) . " $rawdata;";
    return $result;
}

function updateStatement($conn, $table, $data, $where) {
    foreach ($data as $key => $val) {
        $valstr[] = $key . " = '" . mysqli_real_escape_string($conn, $val) . "'";
    }
    $sql = "UPDATE " . $table . " SET " . implode(', ', $valstr);
    $sql .= ($where != '') ? " WHERE " . $where . " " : '';
    mysqli_query($conn, $sql) or die(mysql_error());
    return mysqli_affected_rows($conn);
}

function delete($table, $where) {
    $sql = "DELETE FROM " . $table . " ";
    $sql .= ($where != '') ? " WHERE " . $where . " " : '';
    mysql_query($sql) or die(mysql_error());
    return mysql_affected_rows();
}

function get_patient_outstanding_balance($member_no){
	
$query_check="select * from patient_information where card_number='$member_no'";
$rscheck= mysql_query($query_check) or die(mysql_error());
$row_rscheck = mysql_fetch_assoc($rscheck);	
$totalRows_rscheck = mysql_num_rows($rscheck);

$total_deposits=$row_rscheck['patient_balance'];

$query_check="select sum(qty*unit_price) as total_owing from claims_details where member_no='$member_no' and mode_of_payment='cash' and payment_status='Owing'";
$rscheck= mysql_query($query_check) or die(mysql_error());
$row_rscheck = mysql_fetch_assoc($rscheck);	
$totalRows_rscheck = mysql_num_rows($rscheck);

$total_owing=$row_rscheck['total_owing'];

$outstanding_balance=$total_deposits-$total_owing;


return $outstanding_balance;

}

function getmonth($month){
    $array = array(1 => "January", 2 => "Feburary", 3 => "March", 4 => "April", 5 => "May", 6 => "June", 7 => "July", 8 => "August", 9 => "September", 10 => "October", 11 => "November", 12 => "December");
    $monthname=$array[$month];	
    return $monthname;
}

function getStockAvail($item_id){
    $row = mysql_fetch_assoc(mysql_query("SELECT quantity_available FROM stock_items WHERE item_id='$item_id'"));
    if(empty($row['quantity_available'])){
        $row['quantity_available'] = 0;
    }
    return $row['quantity_available'];
}

function getLensPrescription($process_claim_no, $job_number){
    $row = mysql_fetch_assoc(mysql_query("SELECT item, receipt_number FROM claims_details WHERE item_service_type='Optical Lens' AND process_claim_no='$process_claim_no' AND job_number='$job_number'"));
    return array ($row['item'], $row['receipt_number']);
}

function getJobNumber($process_claim_no, $batch_no){
    $row = mysql_fetch_assoc(mysql_query("SELECT job_number FROM claims_details WHERE item_service_type='Optical Lens' AND process_claim_no='$process_claim_no' AND po_batch_no='$batch_no'"));
    return $row['job_number'];
}

function getPatientLensPrescription($process_claim_no){
    $row = mysql_fetch_assoc(mysql_query("SELECT * FROM optical_examination WHERE process_claim_no='$process_claim_no'"));
    return $row;
}

function getPatientLensType($process_claim_no){
    $row = mysql_fetch_assoc(mysql_query("SELECT * FROM patient_lens_type WHERE process_claim_no='$process_claim_no'"));
    return $row['lens_type'];
}

function getPatientLensAddition($process_claim_no){
    $row = mysql_fetch_assoc(mysql_query("SELECT * FROM patient_lens_addition WHERE process_claim_no='$process_claim_no'"));
    return $row['addition'];
}

function getTotalLensByBatchNo($batch_no){
    $row = mysql_fetch_assoc(mysql_query("SELECT count(purchase_order_no) AS lens FROM purchase_orders WHERE po_batch_no='$batch_no'"));
    return $row['lens'];
}

function getMenuPermission($menu_name){
    $userpermid = $_SESSION['myMM_Userid'];
    $query_rsodest1 = "select a.*,b.* from securitypermissions a INNER JOIN securitylevel b on a.securitylevel_id=b.id where a.user_id='$userpermid' AND menu_name='$menu_name' order by menu_place,menu_position";
    $rsodest1 = mysql_query($query_rsodest1) or die(mysql_error());
    $row_rsodest1 = mysql_fetch_assoc($rsodest1);
    $totalRows_rsodest1 = mysql_num_rows($rsodest1);
    if ($totalRows_rsodest1 > 0) {
        return true;
    }else{
        return false;
    }
    }

    function background_color($arrival_id, $departure_id){
        $info = mysql_fetch_assoc(mysql_query("SELECT * FROM branches WHERE id='$arrival_id'"));
        $info2 = mysql_fetch_assoc(mysql_query("SELECT * FROM branches WHERE id='$departure_id'"));
        $branch_type = $info['type'];
        $branch_type2 = $info2['type'];
        if($branch_type == 'procurement'){
            $color = '#feb2b8';
        } else if($branch_type == 'lab' && $branch_type2 == 'procurement'){
            $color = '#7fbfff';
        } else if($branch_type == 'branch' && $branch_type2 == 'procurement'){
            $color = '#66b266';
        } 
        return $color;
    }

    function background_color_lab($arrival_id, $departure_id){
        $info = mysql_fetch_assoc(mysql_query("SELECT * FROM branches WHERE id='$arrival_id'"));
        $info2 = mysql_fetch_assoc(mysql_query("SELECT * FROM branches WHERE id='$departure_id'"));
        $branch_type = $info['type'];
        $branch_type2 = $info2['type'];
        if($branch_type2 == 'branch' && $branch_type2 == 'procurement'){
            $color = '#feb2b8';
        } else if($branch_type2 == 'procurement'){
            $color = '#66b266';
        }
        return $color;
    }

    function calculateLensPrice($process_claim_no, $lens_count_id){
        $optical_info = mysql_fetch_assoc(mysql_query("SELECT * FROM optical_examination WHERE process_claim_no ='$process_claim_no' AND lens_id_count='$lens_count_id'"));
        $optical_lens_type = mysql_fetch_assoc(mysql_query("SELECT lens_type FROM patient_lens_type WHERE process_claim_no='$process_claim_no' AND lens_id_count='$lens_count_id'"));
        $addition = mysql_query("SELECT * FROM patient_lens_addition WHERE process_claim_no='$process_claim_no' AND lens_id_count='$lens_count_id'");
        while($r = mysql_fetch_assoc($addition)){
            $get_price = mysql_fetch_assoc(mysql_query("SELECT price FROM lens_types WHERE parent_name='EXTRA ADDITIONS' AND lens_name='".$r['addition']."'"));

$get_price_tint = mysql_fetch_assoc(mysql_query("SELECT price FROM lens_types WHERE parent_name='OPTIMIZE TINT' AND lens_name='".$r['addition']."'"));



            $addition_price = $addition_price + $get_price['price'] + $get_price_tint['price'];
            $lens_addition_name = $lens_addition_name.$r['addition'].',';
        }
        $lens_addition_name = substr($lens_addition_name,0,-1);

        $od_sphere = str_replace("-","",$optical_info['od_sphere']);
        $os_sphere = str_replace("-","",$optical_info['os_sphere']);
        $od_cylinder = str_replace("-","",$optical_info['od_cylinder']);
        $os_cylinder = str_replace("-","",$optical_info['os_cylinder']);

        if($od_sphere == 'PLANO'){$od_sphere = 0; }
        if($os_sphere == 'PLANO'){$os_sphere = 0; }
        if($od_cylinder == 'PLANO'){$od_cylinder = 0;}
        if($os_cylinder == 'PLANO'){$os_cylinder = 0;}

        if(empty($od_cylinder)) {$od_cylinder = 1000;}
        if(empty($os_cylinder)) {$os_cylinder = 1000;}

        $lens_type_main = $optical_info['lens_type'];
        $lens_index = $optical_info['lens_index'];
        list($material, $lens_type) = explode(" - ", strtoupper($optical_lens_type['lens_type']));
        if(!empty($lens_index)){
            $price_right_eye = mysql_query("SELECT price FROM lens_types WHERE parent_name='EXTRA LENS' AND lens_name='$lens_index'");
            $price_right_eye_info = mysql_fetch_assoc($price_right_eye);
            $total_price = $price_right_eye_info['price'] + $addition_price;
        }else{
            $price_right_eye = mysql_query("SELECT * FROM lens_price WHERE material='$material' AND additions='$lens_type_main' AND lens_type='$lens_type' AND spherical_lower<='$od_sphere' AND spherical_upper>='$od_sphere' AND cylindrical_lower<='$od_cylinder' AND cylindrical_upper>='$od_cylinder'");
            $price_right_eye_info = mysql_fetch_assoc($price_right_eye);
            if($price_right_eye_info['lens_price']>0){
                $right_price = $price_right_eye_info['lens_price']/2;
            }else{
                $right_price= 0;
            }
            
            $price_left_eye = mysql_query("SELECT * FROM lens_price WHERE material='$material' AND additions='$lens_type_main' AND lens_type='$lens_type' AND spherical_lower<='$os_sphere' AND spherical_upper>='$os_sphere' AND cylindrical_lower<='$os_cylinder' AND cylindrical_upper>='$os_cylinder'");
            $price_left_eye_info = mysql_fetch_assoc($price_left_eye);
            if($price_left_eye_info['lens_price']>0){
                $left_price = $price_left_eye_info['lens_price']/2;
            }else{
                $left_price = 0;
            }
            $total_price = $right_price + $left_price + $addition_price;
    }

    if(!empty($optical_info['lens_index'])){ $lens_index = '('.$optical_info['lens_index'].')';}
        $lens_name = '1 PR '. $material.' '.$lens_type.' '.$lens_type_main.' '.$lens_index.'('.$lens_addition_name.')';
        return array($lens_name, $total_price);
    }

    function getJobLensPrescription($process_claim_no, $job_number){
        $sql = mysql_fetch_assoc(mysql_query("SELECT item FROM claims_details WHERE process_claim_no='$process_claim_no' AND job_number='$job_number' AND item_service_type='Optical Lens'"));
        return $sql['item'];
    }

    function getSightRead($process_claim_no, $lens_count_id){
        $row = mysql_fetch_assoc(mysql_query("SELECT sight_reading FROM patient_lens_type WHERE process_claim_no='$process_claim_no' AND lens_id_count='$lens_count_id'"));
        echo $row['sight_reading'];
    }

    function getDiscountAllowed($process_claim_no, $discount_id){
        $row = mysql_fetch_assoc(mysql_query("SELECT p.discount_amount FROM claims_details c, patient_visit_discounts p WHERE c.discount_id=p.id AND p.process_claim_no=c.process_claim_no AND c.process_claim_no='$process_claim_no' AND c.discount_id='$discount_id'"));
        return $row['discount_amount'];
    }

    function getItemDiscoutAmount($id, $discount_id){
        $qry = mysql_query("SELECT p.discount_rate, p.discount_amount,(c.qty*c.unit_price)*p.discount_rate AS total_discount FROM claims_details c, patient_visit_discounts p WHERE c.discount_id=p.id AND p.process_claim_no=c.process_claim_no AND c.discount_id='$discount_id' AND p.discount_used='used' AND c.id='$id'");
        if(mysql_num_rows($qry) < 1){
            $qry = mysql_query("SELECT p.discount_rate, p.discount_amount,(c.qty*c.unit_price)*p.discount_rate AS total_discount FROM claims_details_cancelled c, patient_visit_discounts p WHERE c.discount_id=p.id AND p.process_claim_no=c.process_claim_no AND c.discount_id='$discount_id' AND p.discount_used='used' AND c.id='$id'");
        }
        $row = mysql_fetch_assoc($qry);
        return $row['total_discount'];
    }

    ///////////////////////// PROSPER /////////////////////////////////////
    function getInsuranceCompany($company_code){
        $row = mysql_fetch_assoc(mysql_query("SELECT * FROM `insurance_company` WHERE company_code='$company_code'"));
        $company_name = $row['company_name'];
        if ($company_name == '') {
            $company_name = 'N/A';
        }else{
          $company_name = $company_name;  
        }
        return  $company_name;
    }


    function patient_info_details($no, $courier){
    $qry = mysql_query("select picpath, insurance_no1, insurance_company1,surname,othernames from patient_information where card_number = '$no'", $courier);
    $row = mysql_fetch_assoc($qry);
    return $row['othernames'].' '.$row['surname'];
}

function getPurchaseExpectedQty($orderno, $courier){
    $qry = mysql_query("SELECT * FROM `purchase_order_details` WHERE `purchase_order_no` = '$orderno' AND `qty_expected` <> `qty_received`", $courier);
    $row = mysql_num_rows($qry);
    if ($row > 0) {
        return true;
    }else{
        return false;
    }
}

function getPurchaseExpectedQtySingle($itemNo, $courier){
    $qry = mysql_query("SELECT * FROM `purchase_order_details` WHERE `id` = '$itemNo' AND `qty_expected` = `qty_received` ", $courier);
    $row = mysql_num_rows($qry);
    if ($row > 0) {
        return true;
    }else{
        return false;
    }
}


function getPurchaseOrderStatus($itemNo, $courier){
    $qry = mysql_query("SELECT * FROM `purchase_orders` WHERE `purchase_order_no` = '$itemNo'", $courier);
    $fetchRow = mysql_fetch_assoc($qry);
    $orderStatus = $fetchRow['order_status'];
    return $orderStatus;
}

function getQtyMismatchStatus($itemNo, $courier){
    $qry = mysql_query("SELECT * FROM `purchase_order_details` WHERE `id` = '$itemNo' AND (`qty_match` = 'Yes' || `qty_match` = 'No') ", $courier);
    $rows = mysql_fetch_assoc($qry);
    return $rows['qty_mismatch_status'];
}

function getReceiptNo($receipt_number, $courier){
    //echo"SELECT * FROM `cashier_payments` WHERE `receiptno` = '$receipt_number'";
$qry = mysql_query("SELECT * FROM `cashier_payments` WHERE `receiptno` = '$receipt_number'", $courier);
    $rows = mysql_fetch_assoc($qry);
    return $rows['receiptno'];
}
