db_query("update {$tables['discount_coupons']} set status='{$v['status']}' where coupon='{$coupon}'");
    }
    $top_message["content"] = cw_get_langvar_by_name("msg_discount_coupons_upd");
    cw_header_location("index.php?target={$target}");
}
if ($action == 'delete' && is_array($posted_data)) {
    foreach ($posted_data as $coupon => $v) {
        if ($v['to_delete']) {
            db_query("delete from {$tables['discount_coupons']} where coupon='{$coupon}'");
        }
    }
    $top_message["content"] = cw_get_langvar_by_name("msg_discount_coupons_upd");
    cw_header_location("index.php?target={$target}");
}
if ($action == 'add') {
    cw_core_process_date_fields($add_coupon, array('' => array('expire' => 0)));
    switch ($apply_to) {
        case '':
            $add_coupon['product_id'] = 0;
            $add_coupon['category_id'] = 0;
            break;
        case 'product':
            $add_coupon['category_id'] = 0;
            $add_coupon['apply_product_once'] = $add_coupon['how_to_apply_p'];
            break;
        case 'category':
            $add_coupon['product_id'] = 0;
            if ($add_coupon['how_to_apply_c'] == 1) {
                $add_coupon['apply_product_once'] = 1;
            } elseif ($how_to_apply_c == 2) {
                $add_coupon['apply_product_once'] = 0;
    case 'is_logged':
    case 'current_area':
    case 'REQUEST_URI':
    case 'REQUEST_METHOD':
    case 'GET_POST':
    case 'target_code':
    case 'cwsid':
    case 'HTTP_REFERER':
    case 'REDIRECT_URL':
        $orderby_qry = " order by `{$logging_search['sortby']}` " . ($logging_search['sortdir'] ? 'desc' : 'asc');
        break;
}
if ($REQUEST_METHOD == "POST") {
    if ($action == "filter_logs") {
        $date_fields = array('date' => array('date_start' => 0, 'date_end' => 1));
        cw_core_process_date_fields($logs_filter, $date_fields, array());
        foreach ($log_columns as $lc_k => $lc_v) {
            if (!intval($lc_v['fixed'])) {
                $log_columns[$lc_k]['display'] = !empty($logs_cols[$lc_k]);
            }
        }
        $logging_filter = $logs_filter;
    } elseif ($action == 'archive_logs') {
        if (!file_exists($var_dirs['logs_archive'])) {
            mkdir($var_dirs['logs_archive']);
        }
        $curr_year = date('Y');
        if (!file_exists($var_dirs['logs_archive'] . '/' . $curr_year)) {
            mkdir($var_dirs['logs_archive'] . '/' . $curr_year);
        }
        $curr_month = date('m');
        $search_data['users'][$usertype] = 0;
    }
    cw_header_location("index.php?target={$target}&mode=search");
} elseif ($action == 'delete_search_load') {
    if ($current_loaded_search_id) {
        db_query("delete from {$tables['saved_search']} where ss_id = '{$current_loaded_search_id}'");
        $current_loaded_search_id = 0;
        $save_search_id = 0;
        $search_data['users'][$usertype] = 0;
    }
    cw_header_location("index.php?target={$target}&mode=search");
} elseif ($REQUEST_METHOD == "POST") {
    $current_loaded_search_id = 0;
    $date_fields = array('admin' => array('creation_date_start' => 0, 'creation_date_end' => 1, 'modify_date_start' => 0, 'modify_date_end' => 1), 'orders' => array('order_date_start' => 0, 'order_date_end' => 1), 'marketing' => array('docs_start' => 0, 'docs_end' => 1), 'saldo' => array('date_start' => 0, 'date_end' => 1));
    $multiple_fields = array('sale' => array('membership', 'sales_manager', 'language'), 'mail' => array('news_list'));
    cw_core_process_date_fields($posted_data, $date_fields, $multiple_fields);
    if (!empty($posted_data)) {
        $posted_data['orders']['orders_product'] = intval($orders_product);
        // for now it accepts only one ID
        $search_data['users'][$usertype] = $posted_data;
        // TODO: remove js_tab based on custom smarty block js_tabs
        $search_data['users'][$usertype]['js_tab'] = $js_tab;
    }
    $search_data['users'][$usertype]['search_sections'] = $search_sections;
    //    if (!empty($search_data['users'][$usertype]['orders'])) {
    //        cw_call('cw_doc_save_history_totals_by_customer', array(array()));
    //    }
    if (!empty($search_data['users'][$usertype]['orders']['orders_count_from']) || !empty($search_data['users'][$usertype]['orders']['orders_count_to']) || !empty($search_data['users'][$usertype]['orders']['avg_subtotal_from']) || !empty($search_data['users'][$usertype]['orders']['avg_subtotal_to']) || !empty($search_data['users'][$usertype]['orders']['total_spent_from']) || !empty($search_data['users'][$usertype]['orders']['total_spent_to'])) {
        db_query("delete from {$tables['customers_docs_stats_processed_docs']}");
        db_query("delete from {$tables['customers_docs_stats']}");
        $valid_statuses = cw_query_column("select code from {$tables['order_statuses']} where inventory_decreasing=1");
$presaved_content_section =& cw_session_register('presaved_content_section', array());
$file_upload_data =& cw_session_register('file_upload_data');
$top_message =& cw_session_register('top_message');
cw_load('category', 'image', 'attributes', 'files');
cw_image_clear(array('cms_images'));
if ($REQUEST_METHOD == 'POST') {
    switch ($action) {
        case 'update_content_section':
        case 'add_new_content_section':
            if ($action == 'update_content_section' && empty($contentsection_id)) {
                cw_header_location('index.php?target=cms&mode=add');
            }
            if (!empty($content_section) && is_array($content_section)) {
                //$content_section = array_map('trim', $content_section);
                cw_core_process_date_fields($content_section, array(0 => array('start_date' => 0, 'end_date' => 1)));
                $content_section['orderby'] = intval($content_section['orderby']);
                $content_section['display_on_404'] = empty($content_section['display_on_404']) ? 'N' : 'Y';
                $content_section['active'] = empty($content_section['active']) ? 'N' : 'Y';
                $content_section['parse_smarty_tags'] = !empty($content_section['parse_smarty_tags']);
                $presaved_content_section = $content_section;
                /*
                 * Validation
                 */
                cw_include('include/lib/formvalidator/formvalidator.php', INCLUDE_NO_GLOBALS);
                // Errors
                $validator = new FormValidator();
                $validator->addValidation("service_code", "req", cw_get_langvar_by_name('msg_ab_err_servicecode_is_empty'));
                $validator->addValidation("service_code", "varname", cw_get_langvar_by_name('msg_ab_err_wrong_servicecode_format'));
                if (!$validator->ValidateForm($content_section)) {
                    cw_add_top_message($validator->GetErrors(), 'E');
<?php

cw_load('cart', 'files', 'mail', 'user');
if ($action == 'update') {
    $date_fields = array('' => array('event_date' => 0));
    cw_core_process_date_fields($event_details, $date_fields);
    if (!$event_details['title']) {
        $top_message = array('type' => 'E', 'content' => cw_get_langvar_by_name('err_giftreg_required_fields_msg'));
        $event_details_sess =& cw_session_register('event_details_sess', $event_details);
        cw_header_location("index.php?target=giftreg_manage&event_id={$event_id}");
    }
    $event_id = cw_query_first_cell("select event_id from {$tables['giftreg_events']} where event_id='{$event_id}' and customer_id='{$customer_id}'");
    if (!$event_id) {
        $count = cw_query_first_cell("select count(*) FROM {$tables['giftreg_events']} WHERE customer_id='{$customer_id}'");
        if ($count < $config['estore_gift']['events_lists_limit']) {
            $event_id = cw_array2insert('giftreg_events', array('customer_id' => $customer_id));
        } else {
            $error = 'events_limit_exceeded';
        }
    }
    if (empty($error)) {
        cw_array2update('giftreg_events', $event_details, "event_id='{$event_id}'", array('status', 'title', 'event_date', 'description', 'html_content', 'guestbook'));
    } else {
        $top_message = array('type' => 'E', 'content' => cw_get_langvar_by_name('err_giftreg_events_lists_limit'));
    }
    cw_header_location("index.php?target=gifts&mode=events&event_id={$event_id}");
}
if ($action == 'guestbook') {
    $events_creator = cw_query_first_cell("select customer_id from {$tables['giftreg_events']} where event_id='{$event_id}' and guestbook=1");
    if (!$events_creator) {
        cw_header_location("index.php?target={$target}&mode=events");
<?php

$saved_post_data =& cw_session_register('saved_post_data');
$filter_surveys =& cw_session_register('filter_surveys', array());
if ($action == 'update_details' && is_array($posted_data)) {
    cw_core_process_date_fields($posted_data, array('' => array('valid_from_date' => 0, 'expires_data' => 1)), null);
    $fields_to_update = array('survey_type', 'valid_from_date', 'expires_data', 'publish_results', 'display_on_frontpage');
    if ($edited_language == $config['default_admin_language']) {
        $fields_to_update = array_merge($fields_to_update, array('header', 'footer', 'complete', 'survey'));
    }
    cw_array2update('surveys', $posted_data, "survey_id = '{$survey_id}'");
    $posted_data['code'] = $edited_language;
    $posted_data['survey_id'] = $survey_id;
    cw_array2insert('surveys_lng', $posted_data, true, array('survey_id', 'survey', 'header', 'footer', 'complete', 'code'));
    $top_message['content'] = cw_get_langvar_by_name('txt_survey_is_modifyed');
    cw_header_location("index.php?target={$target}&survey_id=" . $survey_id);
}
if ($action == 'update_structure' && is_array($posted_data)) {
    $fields_to_update = array('answers_type', 'col', 'orderby');
    $fields_to_update_answer = array('textbox_type', 'orderby');
    if ($edited_language == $config['default_admin_language']) {
        $fields_to_update = array_merge($fields_to_update, array('question'));
        $fields_to_update_answer = array_merge($fields_to_update_answer, array('answer'));
    }
    foreach ($posted_data as $qid => $d) {
        if ($qid == 0 && empty($d['question'])) {
            continue;
        }
        if ($qid == 0) {
            $qid = cw_array2insert('survey_questions', array('survey_id' => $survey_id, 'question' => $d['question']), false, array('survey_id', 'question'));
        }
cw_load('map', 'warehouse', 'product', 'accounting', 'taxes', 'doc');
if (in_array($action, array('update', 'delete')) || in_array($mode, array('delete', 'delete_all'))) {
    cw_include('include/orders/process.php');
}
if ($action == 'print' && is_array($doc_ids)) {
    global $doc_id;
    $doc_id = implode(',', array_keys($doc_ids));
    cw_include('include/orders/order.php');
}
if ($action == 'reset') {
    $search_data['profit_reports'] = array();
    cw_header_location("index.php?target={$target}&mode=search");
}
if ($REQUEST_METHOD == "POST" && is_array($posted_data)) {
    $date_fields = array('basic' => array('creation_date_start' => 0, 'creation_date_end' => 1));
    cw_core_process_date_fields($posted_data, $date_fields);
    $posted_data['js_tab'] = $js_tab;
    $search_data['profit_reports'][$docs_type] = $posted_data;
    $search_data['profit_reports'][$docs_type]['search_sections'] = $search_sections;
    cw_header_location("index.php?target={$target}&mode=search");
}
if (empty($search_data['profit_reports'][$docs_type])) {
    $search_data['profit_reports'] = array();
    $date = getdate(cw_core_get_time());
    $search_data['profit_reports'][$docs_type] = array('basic' => array('creation_date_start' => mktime(0, 0, 0, $date['mon'], 1, $date['year']), 'creation_date_end' => mktime(23, 59, 59, $date['mon'], $date['mday'], $date['year'])), 'search_sections' => array('tab_search_orders' => 1));
}
if (empty($search_data['profit_reports'][$docs_type]['sort_field'])) {
    $search_data['profit_reports'][$docs_type]['sort_field'] = 'product';
    $search_data['profit_reports'][$docs_type]['sort_direction'] = 0;
}
if ($sort && in_array($sort, array('product_id', 'product', 'cost', 'avg_price', 'avg_profit', 'qty', 'total_cost', 'total_sales', 'total_profit', 'margin', 'markup'))) {
function cw_doc_update_quotes($doc_id, $quotes)
{
    global $tables;
    if ($quotes) {
        $sum_comission = 0;
        foreach ($quotes as $quote_id => $quote) {
            cw_core_process_date_fields($quote, array('' => array('exp_mail_before' => 0, 'exp_mail_after' => 0, 'exp_date' => 0)));
            if (!$quote['exp_date']) {
                unset($quote['exp_date']);
            }
            $quote_info = cw_query_first("select * from {$tables['docs_quotes']} where doc_quote_id='{$quote_id}'");
            if ($quote['paid'] == $quote_info['total']) {
                $quote['status'] = 'C';
            }
            if (in_array($quote['status'], array('C', 'P'))) {
                $quote['paid'] = $quote_info['total'];
                $sum_comission += $quote_info['commission'];
            }
            cw_array2update('docs_quotes', $quote, "doc_quote_id='{$quote_id}'");
        }
        $doc = cw_call('cw_doc_get', array($doc_id));
        cw_doc_update_commissions($doc_id, $doc, $sum_comission);
    }
}
<?php

define('AOM', 1);
$aom_orders =& cw_session_register('aom_orders');
cw_load('doc', 'aom', 'taxes', 'warehouse', 'ean', 'product', 'cart', 'cart_process', 'category', 'web');
$error_message = '';
if ($action == 'functions') {
    cw_display('addons/pos/printer_functions_ajax.tpl', $smarty);
    exit(0);
}
if ($action == 'search_products') {
    cw_core_process_date_fields($posted_data, null, array('' => array('manufacturers', 'categories', 'suppliers')));
    $aom_data = array();
    $aom_data['categories_orig'] = $posted_data['categories_orig'];
    $aom_data['categories'] = $posted_data['categories'];
    if ($posted_data['attribute_names']['manufacturer_id'] && !empty($posted_data['attribute_names']['manufacturer_id'][0])) {
        $aom_data['attribute_names']['manufacturer_id'] = $posted_data['attribute_names']['manufacturer_id'];
    }
    $aom_data['product_id'] = $posted_data['product_id'];
    $aom_data['by_title'] = true;
    $aom_data['by_keywords'] = true;
    $aom_data['by_shortdescr'] = true;
    $aom_data['by_fulldescr'] = true;
    $aom_data['by_eancode'] = true;
    $aom_data['by_sku'] = true;
    $aom_data['substring'] = $posted_data['substring'];
    $aom_data['substring_exact'] = $posted_data['substring_exact'];
    $aom_data['limit'] = 30;
    $aom_data['flat_search'] = 1;
    $aom_data['sort_field'] = 'product';
    $aom_data['sort_direction'] = 1;
     if (!empty($delete_contentsections) && is_array($delete_contentsections)) {
         $delete_contentsections = array_keys($delete_contentsections);
         db_query("DELETE FROM {$tables['cms']} WHERE contentsection_id IN ('" . implode("','", $delete_contentsections) . "')");
         db_query("DELETE FROM {$tables['cms_restrictions']} WHERE contentsection_id IN ('" . implode("','", $delete_contentsections) . "')");
         db_query("DELETE FROM {$tables['cms_user_counters']} WHERE contentsection_id IN ('" . implode("','", $delete_contentsections) . "')");
         db_query("DELETE FROM {$tables['cms_alt_languages']} WHERE contentsection_id IN ('" . implode("','", $delete_contentsections) . "')");
         foreach ($delete_contentsections as $contentsection_id) {
             cw_image_delete($contentsection_id, 'cms_images');
             cw_call('cw_attributes_cleanup', array($contentsection_id, 'AB'));
         }
     }
     break;
 case 'update_filters':
     if (!empty($filter) && is_array($filter)) {
         $date_fields = array(0 => array('start_filter_date' => 0, 'end_filter_date' => 1));
         cw_core_process_date_fields($filter, $date_fields);
         $contentsections_filter = array('type' => $filter['type'], 'name' => stripslashes($filter['name']), 'target' => $filter['target'], 'url' => stripslashes($filter['url']), 'skin' => $filter['skin'], 'service_code' => stripslashes($filter['service_code']), 'start_date' => intval($filter['start_filter_date']), 'end_date' => intval($filter['end_filter_date']), 'sort_field' => 'service_code', 'sort_direction' => 0, 'attributes' => $attributes, 'offers' => $content_section['offers']);
         if (!empty($content_section_clean_urls) && is_array($content_section_clean_urls)) {
             $contentsections_filter['clean_urls'] = array();
             foreach ($content_section_clean_urls as $cs_filter_cu) {
                 $filter_clean_url = trim($cs_filter_cu['value']);
                 if (!empty($filter_clean_url)) {
                     $contentsections_filter['clean_urls'][] = $filter_clean_url;
                 }
             }
         }
         if (!empty($restricted_attributes)) {
             $contentsections_filter['restricted_attributes'] = $restricted_attributes;
         }
     }
     break;
        cw_product_update_system_info($product_id, array('supplier_customer_id' => $product_data['supplier']));
        cw_group_edit_copy_system_info($product_id, array('supplier_customer_id' => $product_data['supplier']));
        cw_warehouse_recalculate($product_id);
        cw_product_filter_recalculate_price_ranges();
        // tags
        if (!empty($product_data['tags'])) {
            $tags = explode(',', $product_data['tags']);
            cw_tags_set_product_tags($tags, $product_id);
        } else {
            cw_tags_clear_product_tags($product_id);
        }
    } else {
        cw_add_top_message($fillerror, 'E');
        $product_modified_data = $product_data;
        $product_modified_data['product_id'] = $product_id;
        cw_core_process_date_fields($product_modified_data, null, array('' => array('membership_ids', 'status')));
        if ($file_upload_data['products_images_thumb']) {
            $file_upload_data['products_images_thumb']['is_redirect'] = false;
            $product_modified_data['image_thumb'] = $file_upload_data['products_images_thumb'];
        }
        if ($file_upload_data['products_images_det']) {
            $file_upload_data['products_images_det']['is_redirect'] = false;
            $product_modified_data['image_det'] = $file_upload_data['products_images_det'];
        }
    }
    cw_refresh($product_id);
}
if ($REQUEST_METHOD == "POST" && $action == "attributes_modify" && (AREA_TYPE == 'A' || AREA_TYPE == 'V')) {
    $product_data['attributes'] = $attributes;
    cw_call('cw_attributes_save', array('item_id' => $product_id, 'item_type' => 'P', 'attributes' => $product_data['attributes'], 'language' => $edited_language, array('is_default' => false)));
    $top_message = array('content' => cw_get_langvar_by_name('msg_adm_product_wholesale_upd'), 'type' => 'I');