$branch_id = $pid;
         } else {
             $branch_id = $row['insurance_id'];
             $sql = 'SELECT name FROM care_tz_company WHERE id=' . $branch_id;
             $result = $db->Execute($sql);
             $myrow = $result->FetchRow();
             $branch_name = $myrow[0];
         }
         if (!$weberp_obj->transfer_patient_to_webERP_asCustomer($pid, $persondata, $branch_id, $branch_name)) {
             $person_obj->setPatientIsTransmit2ERP($pid, 0);
         } else {
             $person_obj->setPatientIsTransmit2ERP($pid, 1);
         }
         destroy_weberp($weberp_obj);
         if (!$weberp_obj->transfer_bill_to_webERP_asSalesInvoice($pid, $bill_number, $row, $branch_id)) {
             $bill_obj->setBillIsTransmit2ERP($bill_number, 0);
         } else {
             $bill_obj->setBillIsTransmit2ERP($bill_number, 1);
         }
     }
 }
 if (!$discharge) {
     if ($user_origin == 'quotation') {
         header('Location: billing_tz_quotation.php?patient=' . $_REQUEST['patient'] . '');
     } else {
         header('Location: billing_tz_pending.php?mode=archived&bill_number=' . $bill_number);
     }
 } else {
     if ($user_origin == 'quotation') {
         header('Location: ../ambulatory/amb_clinic_discharge.php' . URL_REDIRECT_APPEND . '&user_origin=' . $user_origin . '&pn=' . $encounter . '&pyear=' . date("Y") . '&pmonth=' . date("n") . '&pday=' . date(j) . '&tb=' . str_replace("#", "", $cfg['top_bgcolor']) . '&tt=' . str_replace("#", "", $cfg['top_txtcolor']) . '&bb=' . str_replace("#", "", $cfg['body_bgcolor']) . '&d=' . $cfg['dhtml'] . '&station=' . $station . '&backpath=' . urlencode('../billing_tz/billing_tz_quotation.php') . '&dept_nr=' . $dept_nr);
     } else {