function cw_order_messages_send_mail($from, $to, $subject_template, $body_template, $language, $crypted, $is_pdf, $files)
{
    global $tables;
    $last_message = cw_query_first("select * from {$tables['mail_spool']} order by mail_id desc limit 1");
    cw_load('doc');
    $extra_admin_emails = cw_doc_order_status_extra_admin_email();
    if (in_array($to, $extra_admin_emails) || strpos(implode(",", $extra_admin_emails), $to) !== false) {
        return;
    }
    $om_debug = true;
    if ($om_debug) {
        cw_log_add("order_messages_send_mail", array($from, $to, $subject_template, $body_template, $language, $crypted, $is_pdf, $file));
    }
    if ($om_debug) {
        cw_log_add("order_messages_send_mail", array('last_message' => $last_message));
    }
    global $smarty;
    global $current_language;
    $rnd_key = time();
    $smarty->assign('order', array('display_id' => $rnd_key));
    $language = $language ? $language : $current_language;
    $order_subject_template = 'mail/docs/customer_subj.tpl';
    $test_subject = chop(cw_display($order_subject_template, $smarty, false, $language));
    if ($om_debug) {
        cw_log_add("order_messages_send_mail", array('test_subject' => $test_subject));
    }
    $subject_parts = explode($rnd_key, $test_subject);
    $extracted_display_id = str_replace($subject_parts, "", $last_message['subject']);
    $extracted_doc_id = cw_query_first_cell("select doc_id from {$tables['docs']} where display_id='{$extracted_display_id}'");
    if (!$extracted_doc_id && strpos($last_message['subject'], "#") !== false) {
        $_parts = explode("#", cw_order_messages_remove_doc_prefix($last_message['subject']));
        $extracted_doc_id = intval(trim($_parts[1]));
    }
    if (!$extracted_doc_id) {
        if ($om_debug) {
            cw_log_add("order_messages_send_mail", '!$extracted_doc_id');
        }
        return;
    }
    if ($om_debug) {
        cw_log_add("order_messages_send_mail", array("extracted_doc_id" => $extracted_doc_id));
    }
    cw_load('doc');
    $doc_data = cw_call('cw_doc_get', array($extracted_doc_id, 8192));
    if (empty($doc_data)) {
        if ($om_debug) {
            cw_log_add("order_messages_send_mail", 'empty($doc_data)');
        }
        return;
    }
    if ($last_message['mail_to'] != $doc_data['userinfo']['email']) {
        if ($om_debug) {
            cw_log_add("order_messages_send_mail", array('$last_message[mail_to] != $doc_data[userinfo][email]', '$last_message[mail_to]' => $last_message['mail_to'], '$doc_data[userinfo][email]' => $doc_data['userinfo']['email']));
        }
        return;
    }
    global $customer_id;
    $thread_exists = cw_query_first_cell("select {$tables['order_messages_threads']}.thread_id from {$tables['order_messages_messages']}, {$tables['order_messages_threads']} where {$tables['order_messages_messages']}.subject='" . addslashes($last_message['subject']) . "' and {$tables['order_messages_messages']}.thread_id={$tables['order_messages_threads']}.thread_id and {$tables['order_messages_threads']}.doc_id='{$extracted_doc_id}'");
    if ($thread_exists) {
        $thread_id = $thread_exists;
        if ($om_debug) {
            cw_log_add("order_messages_send_mail", array('thread_exists' => $thread_exists));
        }
    } else {
        $thread_id = cw_array2insert('order_messages_threads', array('doc_id' => $extracted_doc_id, 'type' => 'A'));
        if ($om_debug) {
            cw_log_add("order_messages_send_mail", array('thread_new' => $thread_id));
        }
    }
    global $current_area, $config;
    if ($thread_id) {
        $recepient_id = $doc_data['userinfo']['customer_id'];
        if ($current_area == 'C') {
            if (!empty($config['order_messages']['default_recepient_admin_email'])) {
                $default_recepient_admin_email = $config['order_messages']['default_recepient_admin_email'];
            }
            $sender_id = cw_query_first_cell("select customer_id from {$tables['customers']} where usertype='A' and email='{$default_recepient_admin_email}'");
        } else {
            $sender_id = $customer_id;
        }
        $last_message['body'] = str_replace("<br>", "", trim(cw_order_messages_take_out_from_tags($last_message['body'], "<!--start_doc_layout-->", "<!--end_doc_layout-->", false)));
        $new_message_id = cw_array2insert('order_messages_messages', array('thread_id' => $thread_id, 'sender_id' => $sender_id, 'recepient_id' => $recepient_id, 'author_id' => $customer_id, 'date' => time(), 'subject' => addslashes($last_message['subject']), 'body' => addslashes($last_message['body']), 'read_status' => 1));
    }
    if ($om_debug) {
        cw_log_add("order_messages_send_mail", array('created new message' => $new_message_id));
    }
    return $return;
}
function cw_doc_change_status_inner(&$doc_data, $status, $advinfo)
{
    global $tables, $config, $smarty, $current_area;
    global $current_language;
    $doc_id = $doc_data['doc_id'];
    if ($advinfo) {
        $info = addslashes(cw_crypt_text($doc_data['info']['details'] . "\n--- Advanced info ---\n" . $advinfo));
        db_query("update {$tables['docs_info']} set details='" . $info . "' where doc_info_id='" . $doc_data['info']['doc_info_id'] . "'");
    }
    db_query("update {$tables['docs']} set status='{$status}', status_change='" . cw_core_get_time() . "' where doc_id='{$doc_id}'");
    if ($status != $doc_data['status']) {
        cw_event('on_doc_change_status', array($doc_data, $status));
        cw_load('web', 'email', 'accounting');
        cw_accounting_generate_movement($doc_data, 0, $doc_data['status'], $status);
        if ($current_area == 'C') {
            $session_failed_transaction++;
        }
        if ($doc_data['info']['layout_id']) {
            $layout = cw_web_get_layout_by_id($doc_data['info']['layout_id']);
        } else {
            $layout = cw_call('cw_web_get_layout', array('docs_' . $doc_data['type']), true);
        }
        $smarty->assign('layout_data', $layout);
        $smarty->assign('info', $doc_data['info']);
        $smarty->assign('products', $doc_data['products']);
        $smarty->assign('new_status', $status);
        $smarty->assign('old_status', $doc_data['status']);
        $doc_data['status'] = $status;
        $smarty->assign('order', $doc_data);
        $smarty->assign('doc', $doc_data);
        if (cw_doc_order_status_email_enabled($status, 'admin')) {
            $to_customer = $config['default_admin_language'];
            $current_language = $to_customer;
            $smarty->assign('usertype_layout', 'A');
            $smarty->assign('is_email_invoice', 'Y');
            cw_call('cw_send_mail', array($config['Company']['orders_department'], $config['Company']['orders_department'], 'mail/docs/status_changed_admin_subj.tpl', 'mail/docs/status_changed_admin.tpl', $config['default_admin_language'], true));
            $extra_admin_email = cw_doc_order_status_extra_admin_email($status);
            if (!empty($extra_admin_email)) {
                cw_call('cw_send_mail', array($config['Company']['orders_department'], $extra_admin_email, 'mail/docs/status_changed_admin_subj.tpl', 'mail/docs/status_changed_admin.tpl', $config['default_admin_language'], true));
            }
            $smarty->assign('is_email_invoice', 'N');
            $smarty->assign('usertype_layout', '');
        }
        if (cw_doc_order_status_email_enabled($status, 'customer')) {
            $to_customer = cw_query_first_cell("SELECT language FROM {$tables['customers']} WHERE customer_id='{$doc_data['userinfo']}[customer_id]'");
            if (empty($to_customer)) {
                $to_customer = $config['default_customer_language'];
            }
            $doc_data['products'] = cw_doc_translate_products($doc_data['products'], $to_customer);
            $smarty->assign('order', $doc_data);
            $current_language = $to_customer;
            $smarty->assign('is_email_invoice', 'Y');
            cw_call('cw_send_mail', array($config['Company']['orders_department'], $doc_data['userinfo']['email'], 'mail/docs/status_changed_customer_subj.tpl', 'mail/docs/status_changed_customer.tpl', null, false, true));
            $smarty->assign('is_email_invoice', 'N');
        }
        $func = 'cw_doc_change_status_' . $status;
        if (function_exists($func)) {
            cw_func_call($func, $doc_data);
        }
        // invoice approved
        if ($status == 'P' && $doc_data['type'] == 'I') {
            $smarty->assign('is_email_invoice', 'Y');
            cw_call('cw_send_mail', array($config['Company']['orders_department'], $doc_data['userinfo']['email'], 'mail/docs/customer_subj.tpl', 'mail/docs/customer_invoice_approved.tpl', null, false, true));
            $smarty->assign('is_email_invoice', 'N');
        }
        // invoice expired
        if ($status == 'F' && $doc_data['type'] == 'I') {
            $smarty->assign('is_email_invoice', 'Y');
            cw_call('cw_send_mail', array($config['Company']['orders_department'], $doc_data['userinfo']['email'], 'mail/docs/customer_subj.tpl', 'mail/docs/customer.tpl', null, false, true));
            $smarty->assign('is_email_invoice', 'N');
        }
    }
}