Пример #1
0
function tzs_edit_truck($id)
{
    $input_city = isset($_POST['input_city']) ? $_POST['input_city'] : array();
    $path_segment_distance = get_param('path_segment_distance');
    $tr_active = get_param_def('tr_active', '0');
    $tr_date_from = get_param('tr_date_from');
    $tr_date_to = get_param('tr_date_to');
    $comment = get_param('comment');
    $sh_descr = get_param('sh_descr');
    if (count($input_city) > 1) {
        $tr_city_from = $input_city[0];
        $tr_city_to = $input_city[count($input_city) - 1];
        $path_segment_cities = implode(";", $input_city);
    } else {
        $tr_city_from = get_param('tr_city_from');
        $tr_city_to = get_param('tr_city_to');
        $path_segment_cities = '';
    }
    $tr_weight = get_param_def('tr_weight', '0');
    $tr_volume = get_param_def('tr_volume', '0');
    $trans_type = get_param('trans_type');
    $tr_type = get_param_def('tr_type', '0');
    $trans_count = get_param('trans_count');
    $set_dim = isset($_POST['set_dim']);
    $tr_length = get_param('tr_length');
    $tr_height = get_param('tr_height');
    $tr_width = get_param('tr_width');
    $cost = get_param_def('cost', '0');
    $price = get_param_def('price', '0');
    $cost_curr = get_param_def('cost_curr', '1');
    $prepayment = get_param('prepayment');
    $price_query = isset($_POST['price_query']) ? 1 : 0;
    $cash = isset($_POST['cash']) ? 1 : 0;
    $nocash = isset($_POST['nocash']) ? 1 : 0;
    $way_ship = isset($_POST['way_ship']) ? 1 : 0;
    $way_debark = isset($_POST['way_debark']) ? 1 : 0;
    $soft = isset($_POST['soft']) ? 1 : 0;
    $way_prepay = isset($_POST['way_prepay']) ? 1 : 0;
    $top_loading = isset($_POST['top_loading']) ? 1 : 0;
    $side_loading = isset($_POST['side_loading']) ? 1 : 0;
    $back_loading = isset($_POST['back_loading']) ? 1 : 0;
    $full_movable = isset($_POST['full_movable']) ? 1 : 0;
    $remove_cross = isset($_POST['remove_cross']) ? 1 : 0;
    $remove_racks = isset($_POST['remove_racks']) ? 1 : 0;
    $without_gate = isset($_POST['without_gate']) ? 1 : 0;
    // Контроль пересечения дат
    $tr_date_from_str = date("Ymd", strtotime($tr_date_from));
    $tr_date_to_str = date("Ymd", strtotime($tr_date_to));
    $tr_date_from = is_valid_date($tr_date_from);
    $tr_date_to = is_valid_date($tr_date_to);
    // Замена "," на точку "." в числах
    $tr_weight = str_replace(',', '.', $tr_weight);
    $tr_volume = str_replace(',', '.', $tr_volume);
    $tr_length = str_replace(',', '.', $tr_length);
    $tr_height = str_replace(',', '.', $tr_height);
    $tr_width = str_replace(',', '.', $tr_width);
    $cost = str_replace(',', '.', $cost);
    $price = str_replace(',', '.', $price);
    $prepayment = str_replace(',', '.', $prepayment);
    $errors = array();
    if ($price_query && !is_valid_num_zero($cost) || !$price_query && !is_valid_num($cost)) {
        array_push($errors, "Неверно задана стоимость");
    }
    if ($price_query && !is_valid_num_zero($price) || !$price_query && !is_valid_num($price)) {
        array_push($errors, "Неверно задана цена");
    }
    if (!is_valid_num($cost_curr) || !isset($GLOBALS['tzs_curr'][intval($cost_curr)])) {
        array_push($errors, "Неверно задана валюта");
    }
    if ($way_prepay && (!is_valid_num($prepayment) || floatval($prepayment) > 100)) {
        array_push($errors, "Неверно задан размер предоплаты");
    }
    if (!$price_query && !$cash && !$nocash && !way_ship && !way_debark) {
        array_push($errors, "Необходимо выбрать хотя бы один способ в блоке \"Форма расчета\".");
    }
    if ($tr_date_from == null || $tr_date_to == null) {
        array_push($errors, "Неверный формат даты");
    }
    // Контроль пересечения дат
    if ($tr_date_to_str < $tr_date_from_str) {
        array_push($errors, "Дата выгрузки не может быть РАНЬШЕ даты погрузки");
    }
    if (count($input_city) > 1) {
        for ($i = 0; $i < count($input_city); $i++) {
            if (!is_valid_city($input_city[$i])) {
                array_push($errors, "Укажите пункт маршрута № " . ($i + 1));
            }
        }
    } else {
        if (!is_valid_city($tr_city_from)) {
            array_push($errors, "Неверный пункт погрузки");
        }
        if (!is_valid_city($tr_city_to)) {
            array_push($errors, "Неверный пункт разгрузки");
        }
    }
    if (!is_valid_num_zero($tr_weight)) {
        array_push($errors, "Неверно задан вес");
    }
    if (!is_valid_num_zero($tr_volume)) {
        array_push($errors, "Неверно задан объем");
    }
    if (strlen($trans_count) == 0) {
        $trans_count = '1';
    }
    if (!is_valid_num($trans_count)) {
        array_push($errors, "Неверно задано количество машин");
    }
    if (!is_numeric($trans_type) || intval($trans_type) < 1) {
        array_push($errors, "Неверно задан тип транспортного средства");
    }
    if (!is_numeric($tr_active) || intval($tr_active) < 0) {
        array_push($errors, "Неверно задан статус заявки");
    }
    if (!is_numeric($tr_type) || intval($tr_type) < 0 || intval($tr_type) > 3) {
        array_push($errors, "Неверно задан тип");
    }
    if ($set_dim) {
        if (!is_valid_num($tr_length)) {
            array_push($errors, "Неверно задана длина транспортного средства");
        }
        if (!is_valid_num($tr_width)) {
            array_push($errors, "Неверно задана ширина транспортного средства");
        }
        if (!is_valid_num($tr_height)) {
            array_push($errors, "Неверно задана высота транспортного средства");
        }
    } else {
        $tr_length = '0';
        $tr_width = '0';
        $tr_height = '0';
    }
    $user_id = get_current_user_id();
    $from_info = null;
    $to_info = null;
    if (count($errors) == 0) {
        if (count($input_city) > 1) {
            for ($i = 0; $i < count($input_city); $i++) {
                $city_info = tzs_yahoo_convert($input_city[$i]);
                if (isset($city_info["error"])) {
                    array_push($errors, "Не удалось распознать населенный пункт маршрута № " . ($i + 1) . ": " . $city_info["error"]);
                }
                if ($i == 0) {
                    $from_info = $city_info;
                }
                if ($i == count($input_city) - 1) {
                    $to_info = $city_info;
                }
            }
        } else {
            $from_info = tzs_yahoo_convert($tr_city_from);
            if (isset($from_info["error"])) {
                array_push($errors, "Не удалось распознать населенный пункт погрузки: " . $from_info["error"]);
            }
            $to_info = tzs_yahoo_convert($tr_city_to);
            if (isset($to_info["error"])) {
                array_push($errors, "Не удалось распознать населенный пункт выгрузки: " . $to_info["error"]);
            }
        }
    }
    if (count($errors) > 0) {
        tzs_print_truck_form($errors, $id > 0);
    } else {
        global $wpdb;
        $tr_date_from = date('Y-m-d', mktime(0, 0, 0, $tr_date_from['month'], $tr_date_from['day'], $tr_date_from['year']));
        $tr_date_to = date('Y-m-d', mktime(0, 0, 0, $tr_date_to['month'], $tr_date_to['day'], $tr_date_to['year']));
        $temp = $from_info['city_id'];
        $sql = "SELECT lat,lng FROM " . TZS_CITIES_TABLE . " WHERE city_id={$temp};";
        $row1 = $wpdb->get_row($sql);
        $temp = $to_info['city_id'];
        $sql = "SELECT lat,lng FROM " . TZS_CITIES_TABLE . " WHERE city_id={$temp};";
        $row2 = $wpdb->get_row($sql);
        //$dis = tzs_calculate_distance(array($tr_city_from, $tr_city_to));
        //$dis = get_param('length');
        $sh_distance = get_param('sh_distance');
        if ($id == 0) {
            $sql = $wpdb->prepare("INSERT INTO " . TZS_TRUCK_TABLE . " (time, last_edited, user_id, tr_date_from, tr_date_to, tr_city_from, tr_city_to, tr_weight, tr_volume, tr_length, tr_height, tr_width, trans_count, trans_type, active, tr_type, comment, distance," . " from_cid,from_rid,from_sid,to_cid,to_rid,to_sid,price,price_val,sh_descr, cost, cash, nocash, way_ship, way_debark, soft, way_prepay, prepayment, price_query," . " top_loading, side_loading, back_loading, full_movable, remove_cross, remove_racks, without_gate, path_segment_cities, path_segment_distances)" . " VALUES (now(), NULL, %d, %s, %s, %s, %s, %f, %f, %f, %f, %f, %d, %d, %d, %d, %s, %d, %d, %d, %d, %d, %d, %d, %f, %d, %s, %f, %d, %d, %d, %d, %d, %d, %f, %d," . " %d, %d, %d, %d, %d, %d, %d, %s, %s);", $user_id, $tr_date_from, $tr_date_to, stripslashes_deep($tr_city_from), stripslashes_deep($tr_city_to), floatval($tr_weight), floatval($tr_volume), floatval($tr_length), floatval($tr_height), floatval($tr_width), intval($trans_count), intval($trans_type), intval($tr_active), intval($tr_type), stripslashes_deep($comment), floatval($sh_distance), $from_info["country_id"], $from_info["region_id"], $from_info["city_id"], $to_info["country_id"], $to_info["region_id"], $to_info["city_id"], floatval($price), intval($cost_curr), stripslashes_deep($sh_descr), floatval($cost), intval($cash), intval($nocash), intval($way_ship), intval($way_debark), intval($soft), intval($way_prepay), floatval($prepayment), intval($price_query), intval($top_loading), intval($side_loading), intval($back_loading), intval($full_movable), intval($remove_cross), intval($remove_racks), intval($without_gate), stripslashes_deep($path_segment_cities), stripslashes_deep($path_segment_distance));
            if (false === $wpdb->query($sql)) {
                array_push($errors, "Не удалось опубликовать Ваш транспорт. Свяжитесь, пожалуйста, с администрацией сайта");
                array_push($errors, $wpdb->last_error);
                //	$errors = array_merge($errors, $dis['errors']);
                tzs_print_truck_form($errors, false);
            } else {
                //	print_errors($dis['errors']);
                echo "Ваш транспорт опубликован!";
                echo "<br/>";
                echo '<a href="/view-truck/?id=' . tzs_find_latest_truck_rec() . '&spis=new">Просмотреть транспорт</a>';
                $new_url = get_site_url() . '/my-trucks';
                echo '<meta http-equiv="refresh" content="0; url=' . $new_url . '">';
            }
        } else {
            $sql = $wpdb->prepare("UPDATE " . TZS_TRUCK_TABLE . " SET " . " last_edited=now(), tr_date_from=%s, tr_date_to=%s, tr_city_from=%s, tr_city_to=%s, tr_weight=%f, tr_volume=%f," . " tr_length=%f, tr_height=%f, tr_width=%f, trans_count=%d, trans_type=%d, tr_type=%d, comment=%s, distance=%d, " . " from_cid=%d,from_rid=%d,from_sid=%d,to_cid=%d,to_rid=%d,to_sid=%d, active=%d, price=%f, price_val=%d, sh_descr=%s," . " cost=%f, cash=%d, nocash=%d, way_ship=%d, way_debark=%d, soft=%d, way_prepay=%d, prepayment=%f, price_query=%d," . " top_loading=%d, side_loading=%d, back_loading=%d, full_movable=%d, remove_cross=%d, remove_racks=%d, without_gate=%d, path_segment_cities=%s, path_segment_distances=%s" . " WHERE id=%d AND user_id=%d;", $tr_date_from, $tr_date_to, stripslashes_deep($tr_city_from), stripslashes_deep($tr_city_to), floatval($tr_weight), floatval($tr_volume), floatval($tr_length), floatval($tr_height), floatval($tr_width), intval($trans_count), intval($trans_type), intval($tr_type), stripslashes_deep($comment), floatval($sh_distance), $from_info["country_id"], $from_info["region_id"], $from_info["city_id"], $to_info["country_id"], $to_info["region_id"], $to_info["city_id"], intval($tr_active), floatval($price), intval($cost_curr), stripslashes_deep($sh_descr), floatval($cost), intval($cash), intval($nocash), intval($way_ship), intval($way_debark), intval($soft), intval($way_prepay), floatval($prepayment), intval($price_query), intval($top_loading), intval($side_loading), intval($back_loading), intval($full_movable), intval($remove_cross), intval($remove_racks), intval($without_gate), stripslashes_deep($path_segment_cities), stripslashes_deep($path_segment_distance), $id, $user_id);
            if (false === $wpdb->query($sql)) {
                array_push($errors, "Не удалось изменить Ваш транспорт. Свяжитесь, пожалуйста, с администрацией сайта");
                array_push($errors, $wpdb->last_error);
                //	$errors = array_merge($errors, $dis['errors']);
                tzs_print_truck_form($errors, true);
            } else {
                //	print_errors($dis['errors']);
                echo "Ваш транспорт изменен";
                echo "<br/>";
                echo '<a href="/view-truck/?id=' . $id . '&spis=new">Просмотреть транспорт</a>';
                $new_url = get_site_url() . '/my-trucks';
                echo '<meta http-equiv="refresh" content="0; url=' . $new_url . '">';
            }
        }
    }
}
Пример #2
0
function tzs_edit_truck($id)
{
    $tr_active = get_param_def('tr_active', '0');
    $tr_date_from = get_param('tr_date_from');
    $tr_date_to = get_param('tr_date_to');
    $tr_city_from = get_param('tr_city_from');
    $tr_city_to = get_param('tr_city_to');
    $comment = get_param('comment');
    $sh_descr = get_param('sh_descr');
    $tr_weight = get_param_def('tr_weight', '0');
    $tr_volume = get_param_def('tr_volume', '0');
    $trans_type = get_param('trans_type');
    $tr_type = get_param_def('tr_type', '0');
    $trans_count = get_param('trans_count');
    $set_dim = isset($_POST['set_dim']);
    $tr_length = get_param('tr_length');
    $tr_height = get_param('tr_height');
    $tr_width = get_param('tr_width');
    // Контроль пересечения дат
    $tr_date_from_str = date("Ymd", strtotime($tr_date_from));
    $tr_date_to_str = date("Ymd", strtotime($tr_date_to));
    $tr_date_from = is_valid_date($tr_date_from);
    $tr_date_to = is_valid_date($tr_date_to);
    // Замена "," на точку "." в числах
    $tr_weight = str_replace(',', '.', $tr_weight);
    $tr_volume = str_replace(',', '.', $tr_volume);
    $tr_length = str_replace(',', '.', $tr_length);
    $tr_height = str_replace(',', '.', $tr_height);
    $tr_width = str_replace(',', '.', $tr_width);
    $errors = array();
    // cost
    $price = get_param_def('set_price', '0') == '1';
    $price_json = array();
    $price_json['set_price'] = $price ? 1 : 0;
    if ($price) {
        $price_val = get_param_def('price', '0');
        if (!is_valid_num($price_val)) {
            array_push($errors, "Неверно задана стоимость");
        } else {
            $price_json['price'] = floatval($price_val);
        }
        $cost_curr = get_param_def('cost_curr', '0');
        if (!is_valid_num($cost_curr) || !isset($GLOBALS['tzs_curr'][intval($cost_curr)])) {
            array_push($errors, "Неверно задана валюта");
        } else {
            $price_json['cost_curr'] = intval($cost_curr);
        }
        $payment = get_param_def('payment', null);
        if ($payment != null) {
            if ($payment != 'nocash' && $payment != 'cash' && $payment != 'mix_cash' && $payment != 'soft' && $payment != 'conv' && $payment != 'on_card') {
                array_push($errors, "Неверно задана форма оплаты");
            } else {
                $price_json['payment'] = $payment;
            }
        }
        if (isset($_POST['payment_way_nds'])) {
            $price_json['payment_way_nds'] = true;
        }
        if (isset($_POST['payment_way_ship'])) {
            $price_json['payment_way_ship'] = true;
        }
        if (isset($_POST['payment_way_debark'])) {
            $price_json['payment_way_debark'] = true;
        }
        if (isset($_POST['payment_way_barg'])) {
            $price_json['payment_way_barg'] = true;
        }
        if (isset($_POST['payment_way_prepay'])) {
            $price_json['payment_way_prepay'] = true;
            $prepayment = get_param_def('prepayment', '0');
            if (!is_valid_num($prepayment) || floatval($prepayment) > 100) {
                array_push($errors, "Неверно задан размер предоплаты");
            } else {
                $price_json['prepayment'] = floatval($prepayment);
            }
        }
    } else {
        if (isset($_POST['price_query'])) {
            $price_json['price_query'] = true;
        }
    }
    // ----
    if ($tr_date_from == null || $tr_date_to == null) {
        array_push($errors, "Неверный формат даты");
    }
    // Контроль пересечения дат
    if ($tr_date_to_str < $tr_date_from_str) {
        array_push($errors, "Дата выгрузки не может быть РАНЬШЕ даты погрузки");
    }
    if (!is_valid_city($tr_city_from)) {
        array_push($errors, "Неверный пункт погрузки");
    }
    if (!is_valid_city($tr_city_to)) {
        array_push($errors, "Неверный пункт разгрузки");
    }
    if (!is_valid_num_zero($tr_weight)) {
        array_push($errors, "Неверно задан вес");
    }
    if (!is_valid_num_zero($tr_volume)) {
        array_push($errors, "Неверно задан объем");
    }
    if (strlen($trans_count) == 0) {
        $trans_count = '1';
    }
    if (!is_valid_num($trans_count)) {
        array_push($errors, "Неверно задано количество машин");
    }
    if (!is_numeric($trans_type) || intval($trans_type) < 1) {
        array_push($errors, "Неверно задан тип ТС");
    }
    if (!is_numeric($tr_active) || intval($tr_active) < 0) {
        array_push($errors, "Неверно задан статус заявки");
    }
    if (!is_numeric($tr_type) || intval($tr_type) < 0 || intval($tr_type) > 3) {
        array_push($errors, "Неверно задан тип");
    }
    if ($set_dim) {
        if (!is_valid_num($tr_length)) {
            array_push($errors, "Неверно задана длина транспортного средства");
        }
        if (!is_valid_num($tr_width)) {
            array_push($errors, "Неверно задана ширина транспортного средства");
        }
        if (!is_valid_num($tr_height)) {
            array_push($errors, "Неверно задана высота транспортного средства");
        }
    } else {
        $tr_length = '0';
        $tr_width = '0';
        $tr_height = '0';
    }
    $user_id = get_current_user_id();
    $from_info = null;
    $to_info = null;
    if (count($errors) == 0) {
        $from_info = tzs_yahoo_convert($tr_city_from);
        if (isset($from_info["error"])) {
            array_push($errors, "Не удалось распознать населенный пункт погрузки: " . $from_info["error"]);
        }
        $to_info = tzs_yahoo_convert($tr_city_to);
        if (isset($to_info["error"])) {
            array_push($errors, "Не удалось распознать населенный пункт выгрузки: " . $to_info["error"]);
        }
    }
    if (count($errors) > 0) {
        tzs_print_truck_form($errors, $id > 0);
    } else {
        global $wpdb;
        $tr_date_from = date('Y-m-d', mktime(0, 0, 0, $tr_date_from['month'], $tr_date_from['day'], $tr_date_from['year']));
        $tr_date_to = date('Y-m-d', mktime(0, 0, 0, $tr_date_to['month'], $tr_date_to['day'], $tr_date_to['year']));
        //$dis = tzs_calculate_distance(array($tr_city_from, $tr_city_to));
        $dis = get_param('length');
        if ($id == 0) {
            $sql = $wpdb->prepare("INSERT INTO " . TZS_TRUCK_TABLE . " (time, last_edited, user_id, tr_date_from, tr_date_to, tr_city_from, tr_city_to, tr_weight, tr_volume, tr_length, tr_height, tr_width, trans_count, trans_type, active, tr_type, cost, comment, distance,from_cid,from_rid,from_sid,to_cid,to_rid,to_sid,price,price_val,sh_descr)" . " VALUES (now(), NULL, %d, %s, %s, %s, %s, %f, %f, %f, %f, %f, %d, %d, %d, %d, %s, %s, %d, %d,%d,%d,%d,%d,%d,%f,%d,%s);", $user_id, $tr_date_from, $tr_date_to, stripslashes_deep($tr_city_from), stripslashes_deep($tr_city_to), floatval($tr_weight), floatval($tr_volume), floatval($tr_length), floatval($tr_height), floatval($tr_width), intval($trans_count), intval($trans_type), intval($tr_active), intval($tr_type), stripslashes_deep(json_encode($price_json)), stripslashes_deep($comment), $dis, $from_info["country_id"], $from_info["region_id"], $from_info["city_id"], $to_info["country_id"], $to_info["region_id"], $to_info["city_id"], floatval($price_val), intval($cost_curr), stripslashes_deep($sh_descr));
            if (false === $wpdb->query($sql)) {
                array_push($errors, "Не удалось опубликовать Ваш транспорт. Свяжитесь, пожалуйста, с администрацией сайта");
                array_push($errors, $wpdb->last_error);
                //	$errors = array_merge($errors, $dis['errors']);
                tzs_print_truck_form($errors, false);
            } else {
                //	print_errors($dis['errors']);
                echo "Ваш транспорт опубликован!";
                echo "<br/>";
                echo '<a href="/view-truck/?id=' . tzs_find_latest_truck_rec() . '&spis=new">Просмотреть транспорт</a>';
            }
        } else {
            $sql = $wpdb->prepare("UPDATE " . TZS_TRUCK_TABLE . " SET " . " last_edited=now(), tr_date_from=%s, tr_date_to=%s, tr_city_from=%s, tr_city_to=%s, tr_weight=%f, tr_volume=%f," . " tr_length=%f, tr_height=%f, tr_width=%f, trans_count=%d, trans_type=%d, tr_type=%d, cost=%s, comment=%s, distance=%d, " . " from_cid=%d,from_rid=%d,from_sid=%d,to_cid=%d,to_rid=%d,to_sid=%d, active=%d, price=%f, price_val=%d, sh_descr=%s" . " WHERE id=%d AND user_id=%d;", $tr_date_from, $tr_date_to, stripslashes_deep($tr_city_from), stripslashes_deep($tr_city_to), floatval($tr_weight), floatval($tr_volume), floatval($tr_length), floatval($tr_height), floatval($tr_width), intval($trans_count), intval($trans_type), intval($tr_type), stripslashes_deep(json_encode($price_json)), stripslashes_deep($comment), $dis, $from_info["country_id"], $from_info["region_id"], $from_info["city_id"], $to_info["country_id"], $to_info["region_id"], $to_info["city_id"], intval($tr_active), floatval($price_val), intval($cost_curr), stripslashes_deep($sh_descr), $id, $user_id);
            if (false === $wpdb->query($sql)) {
                array_push($errors, "Не удалось изменить Ваш транспорт. Свяжитесь, пожалуйста, с администрацией сайта");
                array_push($errors, $wpdb->last_error);
                //	$errors = array_merge($errors, $dis['errors']);
                tzs_print_truck_form($errors, true);
            } else {
                //	print_errors($dis['errors']);
                echo "Ваш транспорт изменен";
                echo "<br/>";
                echo '<a href="/view-truck/?id=' . $id . '&spis=new">Просмотреть транспорт</a>';
            }
        }
    }
}