function cron_merge_template($form, $entry, $fileid, $invoice_number = "")
{
    $return = "";
    $formid = $entry['form_id'];
    $where = 'form_id =' . $formid;
    $params = array('where' => $where);
    $templates = gettplList($params);
    $fieldmap = gettplFieldMap($params);
    //echo print_r($templates,true);
    echo "LeadID : " . $entry['id'] . " msg: <br/>" . print_r($templates['meta']['files'], true);
    if (!empty($templates['meta']['files'])) {
        echo '<br/>File ID: ' . print_r($fileid, true);
        $return = cron_tpl_combine($entry, $templates, $fieldmap, $fileid);
        echo 'Return: ' . $return;
    } else {
        echo 'empty';
        // exit;
    }
    $letters = "";
    $invoices = "";
    if ($return != "") {
        echo "Doc : " . $return . "<br>";
        $letters = cron_LettersMerge($entry, $fieldmap);
        $invoices = cron_InvoicesMerge($entry, $fieldmap, $invoice_number);
        $doc_email_msg = TriggerDocumentEmail($entry['id']);
        if (!empty($doc_email_msg) && $doc_email_msg['status'] == "success") {
            $status = 1;
            $message = $doc_email_msg['msg'];
            cron_statusUpdate($entry['id'], $status, $message);
        } else {
            $status = 2;
            $message = "Email Sent Failed";
            cron_statusUpdate($entry['id'], $status, $message);
        }
        return true;
    } else {
        $status = 2;
        $message = "Document generation Failed";
        cron_statusUpdate($entry['id'], $status, $message);
        return false;
    }
}
function merge_template($confirmation, $form, $entry, $ajax)
{
    if (isset($_POST['gform_save_state_' . $form['id']])) {
        return $confirmation;
    }
    /* this should never happend */
    global $wpdb;
    $return = "";
    $formid = $entry['form_id'];
    $where = 'form_id =' . $formid;
    $params = array('where' => $where);
    $templates = gettplList($params);
    $fieldmap = gettplFieldMap($params);
    $data_title = RGFormsModel::get_form($formid);
    $form_title = $data_title->title;
    if (!empty($templates['meta']['files'])) {
        $return = tpl_combine($entry, $templates, $fieldmap);
    }
    $letters = "";
    $invoices = "";
    if ($return != "") {
        $letters = LettersMerge($entry, $fieldmap);
        $invoices = InvoicesMerge($entry, $fieldmap);
        //TriggerOrderEmail($entry['id']);
        $doc_email_msg = TriggerDocumentEmail($entry['id']);
        if (!empty($doc_email_msg) && $doc_email_msg['status'] == 'success') {
            $confirmation .= $doc_email_msg['msg'];
        }
        $return = str_replace("\\", "/", $return);
        //$confirmation .= " <a href='".$return."' target='_blank'>You can also download the document here</a>";
        $confirmation .= " or You can Click and Download the links below: ";
        $confirmation .= "<ol><li><a href='" . $return . "' target='_blank'>" . $form_title . "</a>.</li>";
        if (!empty($invoices)) {
            $invoices = str_replace("\\", "/", $invoices);
            //$confirmation .= " And also <a href='".$invoices."' target='_blank'>download your Invoice document here.</a>";
            $confirmation .= "<li><a href='" . $invoices . "' target='_blank'>Tax Invoice</a></li>";
        }
        if (!empty($letters)) {
            $letters = str_replace("\\", "/", $letters);
            //$confirmation .= " And also <a href='".$letters."' target='_blank'>download your Letter document here.</a>";
            $confirmation .= "<li><a href='" . $letters . "' target='_blank'>Cover Letter</a></li>";
        }
        $confirmation .= "</ol>";
        if (isset($_GET["q"]) && $_GET["type"] == "edit") {
            $id = decrypt_doc_url(rgget("q"));
            TemplateData::delete_confirmation_saved($id);
        }
    }
    return $confirmation;
}