function buildChatVouchers($typecond = "")
{
    global $RESPONSE, $INTERNAL, $GROUPS, $CONFIG;
    if (empty($_POST["p_ct_r"])) {
        return;
    }
    if ($_POST["p_ct_r"] == XML_CLIP_NULL) {
        $_POST["p_ct_r"] = 0;
    }
    $RESPONSE->ChatVouchers = "";
    if (!empty($CONFIG["db"]["cct"])) {
        $types = array();
        foreach ($INTERNAL[CALLER_SYSTEM_ID]->Groups as $gid) {
            if (isset($GROUPS[$gid]) && is_array($GROUPS[$gid]->ChatVouchersRequired)) {
                foreach ($GROUPS[$gid]->ChatVouchersRequired as $vid) {
                    if (!isset($types[$vid])) {
                        $types[$vid] = $vid;
                        if (!empty($typecond)) {
                            $typecond .= " OR ";
                        }
                        $typecond .= "`t1`.`tid`='" . $vid . "'";
                    }
                }
            }
        }
        if (!empty($typecond) && is_numeric($_POST["p_ct_r"])) {
            if ($result = queryDB(true, $d = "SELECT *,`t1`.`id` AS `voucherid` FROM `" . DB_PREFIX . DATABASE_COMMERCIAL_CHAT_VOUCHERS . "` AS `t1` INNER JOIN `" . DB_PREFIX . DATABASE_COMMERCIAL_CHAT_TYPES . "` AS `t2` ON `t1`.`tid`=`t2`.`id` WHERE `t1`.`edited` > " . $_POST["p_ct_r"] . " AND (" . $typecond . ") ORDER BY `t1`.`edited` ASC LIMIT " . DATA_ITEM_LOADS . ";")) {
                while ($row = DBManager::FetchArray($result)) {
                    $voucher = new CommercialChatVoucher($row);
                    $RESPONSE->ChatVouchers .= $voucher->GetXML();
                }
            }
        }
    }
}
 function Void()
 {
     CommercialChatVoucher::SetVoucherParams(true, $this->Paid);
     $this->Voided = true;
 }
function processChatActions()
{
    global $INTERNAL, $RVISITOR;
    $count = 0;
    while (isset($_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_va"])) {
        $type = $_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_vd"];
        if ($type == "OperatorSignOff") {
            $op = $INTERNAL[$_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_ve_0"]];
            $op->SignOff();
        } else {
            if ($type == "SendChatTranscriptTo") {
                $value = 1;
                while (!empty($_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_ve_" . $value])) {
                    queryDB(true, "UPDATE `" . DB_PREFIX . DATABASE_CHAT_ARCHIVE . "` SET `transcript_sent`=0,`transcript_receiver`='" . DBManager::RealEscape($_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_ve_0"]) . "' WHERE `transcript_sent`=1 AND `chat_id`='" . DBManager::RealEscape($_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_ve_" . $value]) . "' LIMIT 1;");
                    $value++;
                }
                sendChatTranscripts(true);
            } else {
                if ($type == "CreatePublicGroup") {
                    $room = new UserGroup();
                    $room->IsDynamic = true;
                    $room->Id = $_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_ve_0"];
                    $room->Descriptions["EN"] = $_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_ve_1"];
                    $room->Owner = $_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_ve_2"];
                    $room->Save();
                    $room->AddMember($_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_ve_2"], !empty($_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_ve_3"]));
                } else {
                    if ($type == "DeletePublicGroup") {
                        $room = new UserGroup();
                        $room->Id = $_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_ve_0"];
                        $room->Destroy();
                    } else {
                        if ($type == "JoinPublicGroup") {
                            $room = new UserGroup();
                            $room->Id = $_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_ve_0"];
                            $room->AddMember($_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_ve_2"], !empty($_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_ve_3"]));
                        } else {
                            if ($type == "QuitPublicGroup") {
                                $room = new UserGroup();
                                $room->Id = $_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_ve_0"];
                                $room->RemoveMember($_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_ve_1"]);
                            } else {
                                if ($type == "StartOverlayChat") {
                                    $chat = new VisitorChat($_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_va"], $_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_vb"]);
                                    $chat->RequestInitChat(CALLER_SYSTEM_ID);
                                } else {
                                    if ($type == "AddVisitorComment") {
                                        $visitor = new Visitor($_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_ve_0"]);
                                        $visitor->SaveComment(CALLER_SYSTEM_ID, $_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_ve_1"]);
                                    } else {
                                        if ($type == "DownloadRecentHistory") {
                                            $RVISITOR = new Visitor($_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_ve_0"]);
                                            $RVISITOR->LoadRecentVisits(true, $_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_ve_1"]);
                                        } else {
                                            if ($type == "SetTranslation") {
                                                $chat = new VisitorChat($_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_va"], $_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_vb"]);
                                                $chat->ChatId = $_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_ve_0"];
                                                $chat->SetTranslation($_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_ve_1"]);
                                            } else {
                                                if ($type == "SetChatTicketParam") {
                                                    $ticket = new CommercialChatVoucher("", $_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_ve_0"]);
                                                    $ticket->Load();
                                                    $ticket->SetVoucherParams(!empty($_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_ve_1"]), !empty($_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_ve_2"]), !empty($_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_ve_3"]), !empty($_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_ve_4"]), !empty($_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_ve_5"]), !empty($_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_ve_6"]));
                                                } else {
                                                    if (strlen($_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_vb"]) > 0 && strlen($_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_va"]) > 0) {
                                                        $chat = new VisitorChat($_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_va"], $_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_vb"]);
                                                        $chat->ChatId = $_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_vc"];
                                                        $chat->Load();
                                                        if ($type == "SetCallMeBackStatus") {
                                                            $chat->SetCallMeBackStatus($_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_ve_0"]);
                                                        } else {
                                                            if ($type == "JoinChatInvisible") {
                                                                $chat->JoinChat(CALLER_SYSTEM_ID, true, !empty($_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_ve_0"]));
                                                            } else {
                                                                if ($type == "JoinChat") {
                                                                    $chat->JoinChat(CALLER_SYSTEM_ID, false, !empty($_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_ve_0"]));
                                                                } else {
                                                                    if ($type == "SetPriority") {
                                                                        $chat->SetPriority($_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_ve_0"]);
                                                                    } else {
                                                                        if ($type == "SetTargetOperator") {
                                                                            $chat->SetTargetOperator($_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_ve_0"]);
                                                                        } else {
                                                                            if ($type == "SetTargetGroup") {
                                                                                $chat->SetTargetGroup($_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_ve_0"]);
                                                                            } else {
                                                                                if ($type == "AcceptChat") {
                                                                                    $chat->InternalActivate();
                                                                                } else {
                                                                                    if ($type == "CloseChat") {
                                                                                        $chat->InternalClose(CALLER_SYSTEM_ID);
                                                                                    } else {
                                                                                        if ($type == "TakeChat") {
                                                                                            $chat->TakeChat(CALLER_SYSTEM_ID, $_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_ve_0"]);
                                                                                        } else {
                                                                                            if ($type == "DeclineChat") {
                                                                                                $chat->InternalDecline(CALLER_SYSTEM_ID);
                                                                                            } else {
                                                                                                if ($type == "LeaveChat") {
                                                                                                    $chat->LeaveChat(CALLER_SYSTEM_ID);
                                                                                                }
                                                                                            }
                                                                                        }
                                                                                    }
                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        $count++;
    }
}
function buildChatVouchers($typecond = "")
{
    if (empty($_POST["p_ct_r"])) {
        return;
    }
    if ($_POST["p_ct_r"] == XML_CLIP_NULL) {
        $_POST["p_ct_r"] = 0;
    }
    Server::$Response->ChatVouchers = "";
    if (!empty(Server::$Configuration->Database["cct"])) {
        $types = array();
        foreach (Server::$Operators[CALLER_SYSTEM_ID]->Groups as $gid) {
            if (isset(Server::$Groups[$gid]) && is_array(Server::$Groups[$gid]->ChatVouchersRequired)) {
                foreach (Server::$Groups[$gid]->ChatVouchersRequired as $vid) {
                    if (!isset($types[$vid])) {
                        $types[$vid] = $vid;
                        if (!empty($typecond)) {
                            $typecond .= " OR ";
                        }
                        $typecond .= "`t1`.`tid`='" . $vid . "'";
                    }
                }
            }
        }
        if (!empty($typecond) && is_numeric($_POST["p_ct_r"])) {
            if ($result = DBManager::Execute(true, $d = "SELECT *,`t1`.`id` AS `voucherid` FROM `" . DB_PREFIX . DATABASE_COMMERCIAL_CHAT_VOUCHERS . "` AS `t1` INNER JOIN `" . DB_PREFIX . DATABASE_COMMERCIAL_CHAT_TYPES . "` AS `t2` ON `t1`.`tid`=`t2`.`id` WHERE `t1`.`edited` > " . $_POST["p_ct_r"] . " AND (" . $typecond . ") ORDER BY `t1`.`edited` ASC LIMIT " . DATA_ITEM_LOADS . ";")) {
                while ($row = DBManager::FetchArray($result)) {
                    $voucher = new CommercialChatVoucher($row);
                    Server::$Response->ChatVouchers .= $voucher->GetXML();
                }
            }
        }
    }
}
    $html = str_replace("<!--voucher_id-->", base64UrlEncode($ticket->Id), $html);
    $html = str_replace("<!--server-->", LIVEZILLA_URL, $html);
    $ofc = !empty($_POST["form_ofc"]) ? "&amp;ofc=MQ__" : "";
    if (!empty($_POST["form_extends"]) && !empty($_POST["form_group"])) {
        $html = str_replace("<!--co-->", "&amp;co=" . base64UrlEncode($_POST["form_extends"]) . "&amp;intgroup=" . base64UrlEncode($_POST["form_group"]) . $ofc, $html);
    } else {
        if (!empty($_POST["form_group"])) {
            $html = str_replace("<!--co-->", "&amp;intgroup=" . base64UrlEncode($_POST["form_group"]) . $ofc, $html);
        } else {
            $html = str_replace("<!--co-->", $ofc, $html);
        }
    }
    exit($html);
} else {
    if (!empty($_GET["confirm"]) && $_GET["confirm"] == "1" && !empty($_GET["vc"]) && strlen(base64UrlDecode($_GET["vc"])) == 16) {
        require LIVEZILLA_PATH . "_lib/functions.pp.paypal.inc.php";
        $voucher = new CommercialChatVoucher("", base64UrlDecode($_GET["vc"]));
        if ($voucher->Load()) {
            if (PayProvValidatePayment($voucher->Price)) {
                languageSelect($voucher->Language);
                $voucher->SetPaymentDetails(PayProvGetPaymentId(), PayProvGetPayerId(), PayProvGetPaymentDetails());
                if (empty($PAYMENTERROR)) {
                    $voucher->SetVoucherParams(!empty($voucher->Voided), true, false, false, false, true, base64UrlDecode($_GET[GET_EXTERN_GROUP]));
                } else {
                    $voucher->SetVoucherParams(!empty($voucher->Voided), false, false, false, false);
                }
            }
        }
    }
}
unloadDataProvider();
 function UpdateUserStatus($_internalActivated, $_internalClosed, $_internalDeclined, $_externalActivated, $_externalClose)
 {
     if (!empty($this->ChatId)) {
         $this->Status = $_externalClose || $_internalDeclined || $_internalClosed ? CHAT_CLOSED : $this->Status;
         if ($_internalActivated) {
             queryDB(true, "UPDATE `" . DB_PREFIX . DATABASE_VISITOR_CHATS . "` SET `internal_active`='1',`allocated`='" . DBManager::RealEscape(time()) . "' WHERE `internal_active`=0 AND `chat_id`='" . DBManager::RealEscape($this->ChatId) . "' LIMIT 1;");
             if (DBManager::GetAffectedRowCount() == 1) {
                 queryDB(true, "UPDATE `" . DB_PREFIX . DATABASE_VISITOR_CHAT_OPERATORS . "` SET `status`=0 WHERE `chat_id`='" . DBManager::RealEscape($this->ChatId) . "' AND `user_id`='" . DBManager::RealEscape(CALLER_SYSTEM_ID) . "';");
                 queryDB(true, "UPDATE `" . DB_PREFIX . DATABASE_VISITOR_CHAT_OPERATORS . "` SET `status`=9,`ltime`=" . time() . ",`jtime`=0 WHERE `chat_id`='" . DBManager::RealEscape($this->ChatId) . "' AND `user_id`!='" . DBManager::RealEscape(CALLER_SYSTEM_ID) . "' AND `status`<=1;");
             }
         } else {
             if ($_externalClose && empty($this->InternalClosed)) {
                 $update = "`external_close`='1',`exit`='" . DBManager::RealEscape(time() + 1) . "'";
             } else {
                 if ($_externalClose && !empty($this->InternalClosed)) {
                     $update = "`external_close`='1'";
                 } else {
                     if ($_internalClosed && empty($this->InternalClosed)) {
                         $update = "`internal_closed`='1',`exit`='" . DBManager::RealEscape(time() + 1) . "'";
                     } else {
                         if ($_internalDeclined && empty($this->InternalDeclined)) {
                             $update = "`internal_declined`='1'";
                         } else {
                             $update = "`external_active`='1'";
                         }
                     }
                 }
             }
             if (($_internalClosed || $_externalClose) && !empty($this->AllocatedTime)) {
                 UserGroup::RemoveNonPersistantMember($this->SystemId);
                 //queryDB(true,"DELETE FROM `".DB_PREFIX.DATABASE_GROUP_MEMBERS."` WHERE `user_id`='".DBManager::RealEscape($this->SystemId)."';");
                 $params = $this->CalculateChatResponseTime();
                 $update .= ",`response_time`=" . $params[0] . ",`chat_posts`=" . $params[1];
             }
             queryDB(true, "UPDATE `" . DB_PREFIX . DATABASE_VISITOR_CHATS . "` SET " . $update . " WHERE `chat_id`='" . DBManager::RealEscape($this->ChatId) . "' LIMIT 1;");
         }
         queryDB(true, "UPDATE `" . DB_PREFIX . DATABASE_CHAT_ARCHIVE . "` SET `endtime`=" . $this->LastActive . (!empty($this->AllocatedTime) ? ",`time`=" . $this->AllocatedTime : "") . " WHERE `chat_id`='" . DBManager::RealEscape($this->ChatId) . "' LIMIT 1;");
     }
     if (!empty($this->ChatVoucherId)) {
         $ticket = new CommercialChatVoucher(null, $this->ChatVoucherId);
         $ticket->UpdateVoucherChatTime(0);
     }
 }
function sendChatTranscripts($_custom = false)
{
    global $CONFIG, $INTERNAL, $GROUPS, $INPUTS;
    initData(array("INTERNAL", "INPUTS"));
    closeChats();
    $defmailbox = Mailbox::GetDefaultOutgoing();
    $result = queryDB(false, "SELECT `voucher_id`,`subject`,`customs`,`internal_id`,`transcript_text`,`transcript_receiver`,`email`,`chat_id`,`fullname`,`group_id` FROM `" . DB_PREFIX . DATABASE_CHAT_ARCHIVE . "` WHERE `chat_type`=1 AND `endtime`>0 AND `closed`>0 AND `transcript_sent`=0 LIMIT 1;");
    if ($result) {
        while ($row = DBManager::FetchArray($result)) {
            if (empty($row["transcript_text"])) {
                continue;
            }
            queryDB(true, "UPDATE `" . DB_PREFIX . DATABASE_CHAT_ARCHIVE . "` SET `transcript_sent`=1 WHERE `chat_id`='" . DBManager::RealEscape($row["chat_id"]) . "' LIMIT 1;");
            $rcvs = str_replace(array("%fullname%", "%efullname%"), $row["fullname"], $row["transcript_text"]);
            $rcvs = str_replace(array("%email%", "%eemail%"), $row["email"], $rcvs);
            $rcvs = str_replace("%rating%", getRatingAVGFromChatId($row["chat_id"]), $rcvs);
            $subject = $row["subject"];
            $customs = @unserialize($row["customs"]);
            $fakeSender = "";
            foreach ($INPUTS as $index => $input) {
                if ($input->Active && $input->Custom && !isset($GROUPS[$row["group_id"]]->TicketInputsHidden[$index])) {
                    $cv = "";
                    if ($input->Type == "CheckBox") {
                        $cv = !empty($customs[$input->Name]) ? "<!--lang_client_yes-->" : "<!--lang_client_no-->";
                    } else {
                        if (!empty($customs[$input->Name]) || $input->Type == "ComboBox") {
                            $cv = $input->GetClientValue(@$customs[$input->Name]);
                        }
                    }
                    $rcvs = str_replace("%custom" . $index . "%", $cv, $rcvs);
                }
            }
            $rcvs = applyReplacements($rcvs);
            $rcvs = Mailbox::FinalizeEmail($rcvs . "\r\n\r\n<!--lz_ref_link-->");
            $mailbox = null;
            if (!empty($row["group_id"]) && isset($GROUPS[$row["group_id"]]) && !empty($GROUPS[$row["group_id"]]->ChatEmailOut)) {
                $mailbox = Mailbox::GetById($GROUPS[$row["group_id"]]->ChatEmailOut);
            }
            $mailbox = !empty($mailbox) ? $mailbox : $defmailbox;
            if ((!empty($CONFIG["gl_soct"]) || $_custom) && !empty($row["transcript_receiver"])) {
                sendMail($mailbox, $row["transcript_receiver"], $mailbox->Email, $rcvs, $subject);
            }
            if (!empty($CONFIG["gl_scto"]) && !$_custom) {
                initData(array("INTERNAL"));
                $receivers = array();
                $resulti = queryDB(true, "SELECT `sender`,`receiver` FROM `" . DB_PREFIX . DATABASE_POSTS . "` WHERE `chat_id`='" . DBManager::RealEscape($row["chat_id"]) . "';");
                if ($resulti) {
                    while ($rowi = DBManager::FetchArray($resulti)) {
                        if (!empty($INTERNAL[$rowi["sender"]]) && !in_array($rowi["sender"], $receivers)) {
                            $receivers[] = $rowi["sender"];
                        } else {
                            if (!empty($INTERNAL[$rowi["receiver"]]) && !in_array($rowi["receiver"], $receivers)) {
                                $receivers[] = $rowi["receiver"];
                            } else {
                                continue;
                            }
                        }
                        sendMail($mailbox, $INTERNAL[$receivers[count($receivers) - 1]]->Email, $mailbox->Email, $rcvs, $subject);
                    }
                }
            }
            if (!empty($CONFIG["gl_sctg"]) && !$_custom) {
                initData(array("GROUPS"));
                sendMail($mailbox, $GROUPS[$row["group_id"]]->Email, $mailbox->Email, $rcvs, $subject);
            }
            if (!empty($mailbox) && !empty($CONFIG["gl_scct"])) {
                if (!empty($CONFIG["gl_uvec"])) {
                    if (Mailbox::IsValidEmail($row["transcript_receiver"])) {
                        $fakeSender = $row["transcript_receiver"];
                    } else {
                        if (Mailbox::IsValidEmail($row["email"])) {
                            $fakeSender = $row["email"];
                        }
                    }
                }
                sendMail($mailbox, $CONFIG["gl_scct"], $mailbox->Email, $rcvs, $subject, false, null, $fakeSender);
            }
            if (!empty($row["voucher_id"])) {
                $ticket = new CommercialChatVoucher(null, $row["voucher_id"]);
                $ticket->Load();
                $ticket->SendStatusEmail();
            }
        }
    }
    if (!empty($CONFIG["gl_rm_chats"]) && $CONFIG["gl_rm_chats_time"] == 0) {
        queryDB(true, "DELETE FROM `" . DB_PREFIX . DATABASE_CHAT_ARCHIVE . "` WHERE `transcript_sent` = '1';");
    }
}
 public static function GetMatchingVoucher($_groupId, $_voucherid)
 {
     global $CONFIG, $GROUPS;
     initData(array("GROUPS"));
     $ticket = new CommercialChatVoucher("", $_voucherid);
     $ticket->Load();
     $result = queryDB(true, $d = "SELECT *,`t1`.`id` AS `voucherid`, SUM(`chat_time_max`) AS `chat_time_max`, SUM(`chat_time`) AS `chat_time`, SUM(`chat_sessions`) AS `chat_sessions`, SUM(`chat_sessions_max`) AS `chat_sessions_max`, MAX(`expires`) AS `expires` FROM `" . DB_PREFIX . DATABASE_COMMERCIAL_CHAT_VOUCHERS . "` AS `t1` INNER JOIN `" . DB_PREFIX . DATABASE_COMMERCIAL_CHAT_TYPES . "` AS `t2` ON `t1`.`tid`=`t2`.`id` WHERE `t1`.`id`='" . DBManager::RealEscape($_voucherid) . "' ORDER BY `created` ASC;");
     while ($row = @DBManager::FetchArray($result)) {
         if (!empty($CONFIG["db"]["cct"][$row["tid"]])) {
             $ticket = new CommercialChatVoucher($row);
             if (in_array($row["tid"], $GROUPS[$_groupId]->ChatVouchersRequired)) {
                 return $ticket;
             }
         }
     }
     return null;
 }
 static function GetMatchingVoucher($_groupId, $_voucherid)
 {
     Server::InitDataBlock(array("GROUPS", "DBCONFIG"));
     $ticket = new CommercialChatVoucher("", $_voucherid);
     $ticket->Load();
     $result = DBManager::Execute(true, $d = "SELECT *,`t1`.`id` AS `voucherid`, SUM(`chat_time_max`) AS `chat_time_max`, SUM(`chat_time`) AS `chat_time`, SUM(`chat_sessions`) AS `chat_sessions`, SUM(`chat_sessions_max`) AS `chat_sessions_max`, MAX(`expires`) AS `expires` FROM `" . DB_PREFIX . DATABASE_COMMERCIAL_CHAT_VOUCHERS . "` AS `t1` INNER JOIN `" . DB_PREFIX . DATABASE_COMMERCIAL_CHAT_TYPES . "` AS `t2` ON `t1`.`tid`=`t2`.`id` WHERE `t1`.`id`='" . DBManager::RealEscape($_voucherid) . "' ORDER BY `created` ASC;");
     while ($row = @DBManager::FetchArray($result)) {
         if (!empty(Server::$Configuration->Database["cct"][$row["tid"]])) {
             $ticket = new CommercialChatVoucher($row);
             if (in_array($row["tid"], Server::$Groups[$_groupId]->ChatVouchersRequired)) {
                 return $ticket;
             }
         }
     }
     return null;
 }