<?php

if ($customer_id) {
    cw_load('salesman', 'doc');
    cw_cleanup_target($customer_id);
    $current_level = cw_salesman_current_level($customer_id);
    $target = cw_salesman_get_target($customer_id);
    $smarty->assign('current_level', $current_level);
    $smarty->assign('salesman_target', $target);
    $smarty->assign('salesman_reach', $target['target'] - $current_level);
    $smarty->assign('salesman_reached', cw_salesman_is_reached($customer_id));
    $premiums_selected = cw_salesman_is_selected($customer_id);
    $smarty->assign('salesman_selected', $premiums_selected);
    $smarty->assign('salesman_premiums', cw_salesman_get_premiums($customer_id, $current_language, " and active=1" . ($premiums_selected ? " and selected=1" : "")));
    if ($action == 'premiums' && !$premiums_selected && is_array($choosed_premium)) {
        foreach ($choosed_premium as $id => $val) {
            db_query("update {$tables['salesman_premiums']} set selected=1 where id='{$id}' and customer_id='{$customer_id}'");
        }
    }
    # kornev, select orders
    $previous_customer_id_date =& cw_session_register('previous_customer_id_date');
    $curtime = cw_core_get_time();
    $start_dates[] = $previous_customer_id_date;
    # Since last customer_id
    $start_dates[] = mktime(0, 0, 0, date("m", $curtime), date("d", $curtime), date("Y", $curtime));
    $start_week = $curtime - date("w", $curtime) * 24 * 3600;
    # Week starts since Sunday
    $start_dates[] = mktime(0, 0, 0, date("m", $start_week), date("d", $start_week), date("Y", $start_week));
    $start_dates[] = mktime(0, 0, 0, date("m", $curtime), 1, date("Y", $curtime));
    foreach ($start_dates as $start_date) {
        $date_condition = "and d.doc_info_id=di.doc_info_id and d.date>='{$start_date}' and d.date<='{$curtime}' and di.salesman_customer_id='{$customer_id}'";
<?php

if (!$addons['Salesman']) {
    cw_header_location('index.php');
}
cw_load('salesman');
$location[] = array(cw_get_langvar_by_name('lbl_targets_premiums'), '');
if (!$user) {
    $salesmen = cw_query("select customer_id from {$tables['customers']} where usertype='B'");
    $smarty->assign('salesmen', $salesmen);
    $smarty->assign('main', 'targets_list');
    cw_display('admin/index.tpl', $smarty);
    exit(0);
}
cw_cleanup_target($user);
$current_level = cw_salesman_current_level($user);
$target = cw_salesman_get_target($user);
$reached = cw_salesman_is_reached($user);
$smarty->assign('salesman_reached', $reached);
$premiums_selected = cw_salesman_is_selected($user);
if (!$reached) {
    if ($action == 'update_target') {
        $date_fields = array('' => array('start_date' => 0, 'end_date' => 1));
        cw_core_process_date_fields($posted_data, $date_fields);
        $posted_data['salesman_customer_id'] = $user;
        cw_array2insert('salesman_target', $posted_data, true, array('target', 'start_date', 'end_date', 'salesman_customer_id'));
        cw_header_location('index.php?target=targets&user='******'add' && $data_new['title']) {
        $data_new['salesman_customer_id'] = $user;
        cw_array2insert('salesman_premiums', $data_new, true);