function cw_barcode_print_doc($doc_id, $options)
{
    global $tables, $user_account;
    $layout = cw_web_get_layout_by_id($options['template_id']);
    $doc = cw_doc_get($doc_id);
    $amount = $options['amount'];
    if (!$amount) {
        $amount = 1;
    }
    foreach ($doc['products'] as $product) {
        if (in_array($doc['type'], array('P', 'R', 'Q'))) {
            $product['supplier_code'] = $product['productcode'];
        } else {
            $product['supplier_code'] = cw_query_first_cell("select productcode from {$tables['products_supplied_amount']} where product_id='{$product_id}' order by date limit 1");
        }
        if ($layout['data']['use_tax']) {
            $taxes = $product['extra_data']['taxes'];
            cw_get_products_taxes($product, $user_account, false, $taxes, true);
            $_tmp_price = $product['price'];
            $product['price'] = $product['list_price'];
            cw_get_products_taxes($product, $user_account, false, $taxes, true);
            $product['list_price'] = $product['display_price'];
            $product['price'] = $_tmp_price;
        } else {
            $product['display_price'] = $product['price'];
        }
        for ($i = 0; $i < $product['amount'] * $amount; $i++) {
            $products[] = $product;
        }
    }
    cw_barcode_print($products, $layout, $options, 0);
}
function cw_product_stages_send_emails()
{
    global $tables, $smarty, $config;
    cw_load('doc');
    //select all active product stages settings
    $stages_settings = cw_query("select ps.title, ps.default_status, ps.default_period, ps.subject, ps.body, psp.* from {$tables['product_stages_product_settings']} psp inner join {$tables['product_stages_library']} ps on ps.stage_lib_id=psp.stage_lib_id where psp.active=1");
    foreach ($stages_settings as $stage_setting) {
        $stage_statuses = array();
        if (!empty($stage_setting['status']) && $stage_setting['status'] != -1) {
            $stage_statuses = unserialize($stage_setting['status']);
        } elseif ($stage_setting['status'] == -1 && !empty($stage_setting['default_status'])) {
            $stage_statuses = unserialize($stage_setting['default_status']);
        }
        if (empty($stage_statuses)) {
            continue;
        }
        if ($stage_setting['period'] != -1) {
            $stage_period = $stage_setting['period'];
        } else {
            $stage_period = $stage_setting['default_period'];
        }
        $smarty->assign('stage_message_subject', $stage_setting['subject']);
        $smarty->assign('stage_message_body', $stage_setting['body']);
        //find all ordered products which have been bought within stage period and which are linked to current stage_setting
        $date_past = cw_product_stages_date_in_past($stage_period);
        $ordered_items = cw_query($s = "select di.* from {$tables['docs_items']} di inner join {$tables['docs']} d on d.doc_id=di.doc_id and d.date >= '{$date_past}' where di.product_id = '{$stage_setting['product_id']}'");
        //print("$s <br/>\n");
        foreach ($ordered_items as $doc_item) {
            $doc_data = array();
            //get status change history
            $status_log = cw_query("select * from {$tables['docs_statuses_log']} where doc_id='{$doc_item['doc_id']}' and status in ('" . implode("','", $stage_statuses) . "')");
            foreach ($status_log as $st_log) {
                $days_passed = cw_product_stages_days_passed($st_log['date']);
                if ($days_passed >= $stage_period) {
                    $stage_is_processed = cw_query_first_cell("select count(*) from {$tables['product_stages_process_log']} where setting_id='{$stage_setting['setting_id']}' and doc_item_id='{$doc_item['item_id']}' and status='{$st_log['status']}'");
                    if (!$stage_is_processed) {
                        $smarty->assign('stage_status', $st_log['status']);
                        $smarty->assign('stage_status_date', $st_log['date']);
                        if (empty($doc_data)) {
                            $doc_data = cw_doc_get($doc_item['doc_id'], 8192);
                            $smarty->assign('info', $doc_data['info']);
                            $smarty->assign('products', $doc_data['products']);
                            $smarty->assign('order', $doc_data);
                            $smarty->assign('doc', $doc_data);
                            $smarty->assign('userinfo', $doc_data['userinfo']);
                            cw_log_add('stage_process', array($st_log, $stage_setting, $doc_data));
                        }
                        cw_call('cw_send_mail', array($config['Company']['orders_department'], $doc_data['userinfo']['email'], 'addons/product_stages/mail/stage_notification_subj.tpl', 'addons/product_stages/mail/stage_notification.tpl'));
                        cw_array2insert('product_stages_process_log', array('setting_id' => $stage_setting['setting_id'], 'doc_item_id' => $doc_item['item_id'], 'status' => $st_log['status'], 'date' => time()));
                    }
                }
            }
        }
    }
}
function cw_aom_update_order($cart, $old_products = array(), $is_invoice = false)
{
    global $tables, $config, $addons, $app_main_dir, $dhl_ext_country;
    $cart = cw_call('cw_aom_recalculate_totals', array($cart));
    cw_doc_update($cart['doc_id'], $cart, array(), $is_invoice);
    # kornev
    # pos orders have to be completed in any case
    if ($cart['type'] == 'G') {
        # kornev, because we need item_id
        $cart = cw_doc_get($cart['doc_id']);
        cw_doc_change_status_inner($cart, 'C', '');
    }
}
        $orders['Q'][] = cw_query_first_cell("select count(*) from {$tables['docs']} as d, {$tables['docs_info']} as di where d.type='O' and d.status='Q' {$date_condition}");
        $gross_total[] = price_format(cw_query_first_cell("select sum(total) from {$tables['docs']} as d, {$tables['docs_info']} as di where 1 {$date_condition}"));
        $total_paid[] = price_format(cw_query_first_cell("select sum(total) from {$tables['docs']} as d, {$tables['docs_info']} as di where (status='P' OR status='C') {$date_condition}"));
    }
    $smarty->assign('orders', $orders);
    $smarty->assign('gross_total', $gross_total);
    $smarty->assign('total_paid', $total_paid);
    # kornev, last order
    $last_order_id = cw_query_first_cell("select doc_id from {$tables['docs']} as d, {$tables['docs_info']} as di where d.doc_info_id=di.doc_info_id and di.salesman_customer_id='{$customer_id}' order by date desc limit 1");
    if ($last_order_id) {
        $last_order = cw_doc_get($last_order_id);
        $smarty->assign('last_order', $last_order);
    }
    # kornev, child salesmans
    include $app_main_dir . "/include/affiliates.php";
    # kornev, salesman general stats
    include $app_main_dir . "/include/stats.php";
    # kornev, last 10 orders
    cw_load('doc');
    $last_orders = cw_query("select doc_id FROM {$tables['orders']} {$tables['docs']} as d, {$tables['docs_info']} as di where d.doc_info_id=di.doc_info_id and di.salesman_customer_id='{$customer_id}' ORDER BY date DESC LIMIT 10");
    if ($last_orders) {
        foreach ($last_orders as $k => $val) {
            $last_orders[$k] = cw_doc_get($val['doc_id']);
        }
    }
    $smarty->assign('last_orders', $last_orders);
    $smarty->assign('main', 'main');
} else {
    $smarty->assign('main', 'welcome');
}
$smarty->assign('mode', $mode);
        list($from_time, $to_time) = cw_core_get_time_frame("-1 month");
    } elseif ($shippment_search == 4) {
        $from_time = cw_core_strtotime($from_date);
        $to_time = cw_core_strtotime($to_date);
    }
    $where[] = "type='S'";
    if ($from_time) {
        $where[] = "date >= '{$from_time}'";
    }
    if ($to_time) {
        $where[] = "date <= '{$to_time}'";
    }
    $count_query = cw_db_generate_query(array('count(*)'), $from_tbls, '', $where, '', '', $orderbys);
    $total_items = cw_query_first_cell($count_query);
    $navigation = cw_core_get_navigation($target, $total_items, $page);
    $navigation['script'] = "indes.php?target={$target}&mode={$mode}";
    $smarty->assign('navigation', $navigation);
    if ($total_items > 0) {
        $ship_docs = cw_query(cw_db_generate_query($fields, $from_tbls, '', $where, '', '', $orderbys) . " LIMIT {$navigation['first_page']}, {$navigation['objects_per_page']}");
        if (is_array($ship_docs)) {
            foreach ($ship_docs as $k => $v) {
                $ship_docs[$k] = cw_doc_get($v['doc_id']);
            }
        }
        $smarty->assign('ship_docs', $ship_docs);
    }
    $smarty->assign('current_section', false);
    $smarty->assign('home_style', 'iframe');
}
$smarty->assign('mode', $mode);
$smarty->assign('main', 'manager');
</script>
<?php 
        exit;
    } else {
        cw_header_location("index.php?target=error_message&error=access_denied&id=42");
    }
}
$all_ups_shipping_labels = array();
$is_first_ups_label = true;
foreach ($doc_ids as $id => $v) {
    if ($update != "Y") {
        $e_type = cw_query_first_cell("SELECT value FROM {$tables['docs_extras']} WHERE doc_id = '{$id}' AND khash = 'shipping_label_error'");
        $l_type = cw_query_first_cell("SELECT value FROM {$tables['docs_extras']} WHERE doc_id = '{$id}' AND khash = 'shipping_label_type'");
    }
    $is_true = false;
    $order = cw_doc_get($id);
    if (empty($order)) {
        continue;
    }
    if ($update == "Y" && !array_key_exists($id, $up_orders)) {
        continue;
    }
    $addon = cw_get_shipping_addon($order['order']['shipping_id']);
    if (empty($e_type) && empty($l_type) && $update != "N" || $addon == "ups.php") {
        if (!empty($addon) && file_exists($app_main_dir . "/addons/shipping_label_generator/" . $addon)) {
            $response = array();
            include $app_main_dir . "/addons/shipping_label_generator/" . $addon;
            if ($response['result'] != 'ok') {
                db_query("REPLACE INTO {$tables['docs_extras']} (doc_id,khash,value) VALUES ('{$id}','shipping_label_error','" . $response['error'] . "')");
                db_query("REPLACE INTO {$tables['docs_extras']} (doc_id,khash,value) VALUES ('{$id}','shipping_label_type','')");
                db_query("REPLACE INTO {$tables['docs_extras']} (doc_id,khash,value) VALUES ('{$id}','shipping_label','')");
    }
    $out_products = cw_aom_add_new_products($aom_orders[$doc_id], $pids, $vars, $amounts, $discounts, array(), $is_old);
    $doc_data = cw_doc_get($doc_id, 0);
    $aom_orders[$doc_id] = cw_aom_normalize_after_update($aom_orders[$doc_id], $doc_data);
}
if ($action == 'update_discount' && !$aom_orders[$doc_id]['saved']) {
    if ($param == 'gd_value' && ($current_area == 'G' && $accl['100002'] || $current_area != 'G')) {
        $aom_orders[$doc_id]['pos']['gd_value'] = $value;
    }
    if ($param == 'gd_type' && ($current_area == 'G' && $accl['100002'] || $current_area != 'G')) {
        $aom_orders[$doc_id]['pos']['gd_type'] = $value;
    }
    if ($param == 'vd_value' && ($current_area == 'G' && $accl['100003'] || $current_area != 'G')) {
        $aom_orders[$doc_id]['pos']['vd_value'] = $value;
    }
    $doc_data = cw_doc_get($doc_id, 0);
    $aom_orders[$doc_id] = cw_aom_normalize_after_update($aom_orders[$doc_id], $doc_data);
}
if ($action == 'update_payment' && !$aom_orders[$doc_id]['saved']) {
    if ($param == 'gp_payment') {
        $aom_orders[$doc_id]['pos']['payment'] = $value;
    }
    if ($param == 'gp_paid_by_cc') {
        $aom_orders[$doc_id]['pos']['paid_by_cc'] = $value;
    }
    if (!$aom_orders[$doc_id]['pos']['paid_by_cc']) {
        if ($aom_orders[$doc_id]['pos']['payment']) {
            $aom_orders[$doc_id]['pos']['change'] = $aom_orders[$doc_id]['pos']['payment'] - $aom_orders[$doc_id]['info']['total'];
        }
    } else {
        $aom_orders[$doc_id]['pos']['payment'] = $aom_orders[$doc_id]['pos']['change'] = 0;
<?php

cw_load('doc');
$allowed_order_status = cw_doc_get_allowed_statuses();
if (!in_array($status_code, $allowed_order_status) || empty($status_code)) {
    cw_close_window();
}
$order_status_data = cw_query_first("select * from {$tables['order_statuses']} where code='{$status_code}'");
$doc_id = cw_query_first_cell("select doc_id from {$tables['docs']} order by doc_id desc limit 1");
$doc_data = cw_doc_get($doc_id, 8192);
$doc_data['status'] = $status_code;
cw_load('web');
if ($doc_data['info']['layout_id']) {
    $layout = cw_web_get_layout_by_id($doc_data['info']['layout_id']);
} else {
    $layout = cw_web_get_layout('docs_' . $doc_data['type']);
}
if ($preview_area == 'admin') {
    $smarty->assign('usertype_layout', 'A');
}
$smarty->assign('product_layout_elements', cw_call('cw_web_get_product_layout_elements', array()));
$smarty->assign('preview_area', $preview_area);
$smarty->assign('layout_data', $layout);
$smarty->assign('info', $doc_data['info']);
$smarty->assign('products', $doc_data['products']);
$smarty->assign('order', $doc_data);
$smarty->assign('doc', $doc_data);
$smarty->assign('main', 'preview_order_emails');
$smarty->assign('home_style', 'iframe');
define('PREVENT_XML_OUT', true);
// need simple HTML out if controller called as ajax via $.load()