}
    }
}
if (!isset($Patient)) {
    ShowCustomerSearchFields($rootpath, $theme, $db);
}
if (isset($_POST['Search']) or isset($_POST['Go1']) or isset($_POST['Go2']) or isset($_POST['Next']) or isset($_POST['Previous'])) {
    $PatientResult = CustomerSearchSQL($db);
    if (DB_num_rows($PatientResult) == 0) {
        prnMsg(_('No patient records contain the selected text') . ' - ' . _('please alter your search criteria and try again'), 'info');
        echo '<br />';
    }
}
//end of if search
if (isset($PatientResult)) {
    ShowReturnedCustomers($PatientResult);
}
if (isset($Patient)) {
    $sql = "SELECT name,\n\t\t\t\tclientsince,\n\t\t\t\tsalestype,\n\t\t\t\tphoneno\n\t\t\t\tFROM debtorsmaster\n\t\t\t\tLEFT JOIN custbranch\n\t\t\t\tON debtorsmaster.debtorno=custbranch.debtorno\n\t\t\t\tWHERE debtorsmaster.debtorno='" . $Patient[0] . "'\n\t\t\t\tAND branchcode='" . $Patient[1] . "'";
    $result = DB_query($sql, $db);
    $mydebtorrow = DB_fetch_array($result);
    if ($_SESSION['Care2xDatabase'] != 'None' and $_SESSION['Items']['Lines'] == 0) {
        $Care2xSQL = "SELECT " . $_SESSION['Care2xDatabase'] . ".care_encounter_prescription.article_item_number,\n\t\t\t\t\t\t\t" . $_SESSION['Care2xDatabase'] . ".care_encounter_prescription.nr,\n\t\t\t\t\t\t\tpartcode,\n\t\t\t\t\t\t\ttotal_dosage,\n\t\t\t\t\t\t\tprescribe_date\n\t\t\t\t\t\tFROM " . $_SESSION['Care2xDatabase'] . ".care_encounter_prescription\n\t\t\t\t\t\tLEFT JOIN " . $_SESSION['Care2xDatabase'] . ".care_tz_drugsandservices\n\t\t\t\t\t\tON " . $_SESSION['Care2xDatabase'] . ".care_encounter_prescription.article_item_number=" . $_SESSION['Care2xDatabase'] . ".care_tz_drugsandservices.item_id\n\t\t\t\t\t\tLEFT JOIN stockmaster\n\t\t\t\t\t\tON " . $_SESSION['Care2xDatabase'] . ".care_tz_drugsandservices.partcode=stockmaster.stockid\n\t\t\t\t\t\tLEFT JOIN stockcategory\n\t\t\t\t\t\tON stockmaster.categoryid=stockcategory.categoryid\n\t\t\t\t\t\tLEFT JOIN " . $_SESSION['Care2xDatabase'] . ".care_encounter\n\t\t\t\t\t\tON " . $_SESSION['Care2xDatabase'] . ".care_encounter.encounter_nr=" . $_SESSION['Care2xDatabase'] . ".care_encounter_prescription.encounter_nr\n\t\t\t\t\t\tAND stockcategory.stocktype='X'\n\t\t\t\t\t\tWHERE " . $_SESSION['Care2xDatabase'] . ".care_encounter.pid='" . $Patient[0] . "'\n\t\t\t\t\t\tAND " . $_SESSION['Care2xDatabase'] . ".care_encounter_prescription.bill_number=''";
        $Care2xResult = DB_query($Care2xSQL, $db);
        $i = 0;
        while ($MyCare2xRow = DB_fetch_array($Care2xResult)) {
            $PriceSQL = "SELECT price\n\t\t\t\t\t\tFROM prices\n\t\t\t\t\t\tWHERE stockid='" . $MyCare2xRow['partcode'] . "'\n\t\t\t\t\t\tAND typeabbrev='" . $mydebtorrow['salestype'] . "'\n\t\t\t\t\t\tAND '" . $MyCare2xRow['prescribe_date'] . "' between startdate and enddate";
            $PriceResult = DB_query($PriceSQL, $db);
            if (DB_num_rows($PriceResult) == 0) {
                $Price = 0;
            } else {
        $_SESSION['Items' . $identifier]->PhoneNo = $myrow['phoneno'];
        $_SESSION['Items' . $identifier]->Email = $myrow['email'];
        $_SESSION['Items' . $identifier]->Location = $myrow['defaultlocation'];
        $_SESSION['Items' . $identifier]->DeliverBlind = $myrow['deliverblind'];
        $_SESSION['Items' . $identifier]->DeliveryDays = $myrow['estdeliverydays'];
        $_SESSION['Items' . $identifier]->LocationName = $myrow['locationname'];
    } else {
        prnMsg(_('Sorry, your account has been put on hold for some reason, please contact the credit control personnel.'), 'warn');
        include 'includes/footer.inc';
        exit;
    }
}
if ($_SESSION['RequireCustomerSelection'] == 1 or !isset($_SESSION['Items' . $identifier]->DebtorNo) or $_SESSION['Items' . $identifier]->DebtorNo == '') {
    ShowCustomerSearchFields($rootpath, $theme, $db);
    if (isset($result_CustSelect)) {
        ShowReturnedCustomers($result_CustSelect);
    }
    //end if results to show
    //end if RequireCustomerSelection
} else {
    //dont require customer selection
    // everything below here only do if a customer is selected
    if (isset($_POST['CancelOrder'])) {
        $OK_to_delete = 1;
        //assume this in the first instance
        if ($_SESSION['ExistingOrder' . $identifier] != 0) {
            //need to check that not already dispatched
            $sql = "SELECT qtyinvoiced\n\t\t\t\t\tFROM salesorderdetails\n\t\t\t\t\tWHERE orderno='" . $_SESSION['ExistingOrder' . $identifier] . "'\n\t\t\t\t\t\tAND qtyinvoiced>0";
            $InvQties = DB_query($sql, $db);
            if (DB_num_rows($InvQties) > 0) {
                $OK_to_delete = 0;
            break;
        }
    }
    if ($i == count($_POST)) {
        prnMsg(_('Unable to identify the selected customer'), 'error');
    } else {
        $_POST['Customer'] = $_POST['SelectedCustomer' . $i];
        $_POST['Branch'] = $_POST['SelectedBranch' . $i];
        unset($_POST['Search']);
    }
}
if (empty($_POST['Customer']) and empty($_SESSION['CreditItems' . $identifier]->DebtorNo)) {
    ShowCustomerSearchFields($rootpath, $theme, $db);
}
if (isset($result)) {
    ShowReturnedCustomers($result);
}
//end if results to show
if (isset($_POST['Customer']) and $_POST['Customer'] != '') {
    /*will only be true if page called from customer selection form
    	parse the $Select string into customer code and branch code */
    $_SESSION['CreditItems' . $identifier]->Branch = $_POST['Branch'];
    $_SESSION['CreditItems' . $identifier]->DebtorNo = $_POST['Customer'];
    /*Now retrieve customer information - name, salestype, currency, terms etc */
    $sql = "SELECT debtorsmaster.name,\n\t\t\t\t\tdebtorsmaster.salestype,\n\t\t\t\t\tdebtorsmaster.currcode,\n\t\t\t\t\tcurrencies.rate\n\t\t\t\tFROM debtorsmaster,\n\t\t\t\t\tcurrencies\n\t\t\t\tWHERE debtorsmaster.currcode=currencies.currabrev\n\t\t\t\t\tAND debtorsmaster.debtorno = '" . $_SESSION['CreditItems' . $identifier]->DebtorNo . "'";
    $ErrMsg = _('The customer record of the customer selected') . ': ' . $_SESSION['CreditItems' . $identifier]->DebtorNo . ' ' . _('cannot be retrieved because');
    $DbgMsg = _('The SQL used to retrieve the customer details and failed was');
    $result = DB_query($sql, $db, $ErrMsg, $DbgMsg);
    $myrow = DB_fetch_array($result);
    $_SESSION['RequireCustomerSelection'] = 0;
    $_SESSION['CreditItems' . $identifier]->CustomerName = $myrow['name'];