示例#1
0
function sbrCalc($frl_type, $rez_type, $scheme_type, $currency, $sbr_cost, $emp_cost, $frl_cost, $usr_type)
{
    session_start();
    $frl_type = intval($_POST['xjxargs']['frl_type']);
    $rez_type = intval($_POST['xjxargs']['residency']);
    $scheme_type = intval($_POST['xjxargs']['scheme_type']);
    $currency = intval($_POST['xjxargs']['currency']);
    $sbr_cost = $_POST['xjxargs']['sbr_cost'];
    $emp_cost = $_POST['xjxargs']['emp_cost'];
    $frl_cost = $_POST['xjxargs']['frl_cost'];
    $usr_type = $_POST['xjxargs']['usr_type'];
    $_POST['xjxargs'] = array('frl_type' => $frl_type, 'rez_type' => $rez_type, 'scheme_type' => $scheme_type, 'currency' => $currency, 'sbr_cost' => $sbr_cost, 'emp_cost' => $emp_cost, 'frl_cost' => $frl_cost, 'usr_type' => $usr_type);
    // Заплатка для того чтобы работала CSRF xajax
    $hash = '';
    sbr::setSbrCalc($_POST['xjxargs'], $hash);
    $_POST = $_POST['xjxargs'];
    foreach ($_POST as $k => $v) {
        if (!in_array($k, array('sbr_cost', 'frl_cost', 'emp_cost'))) {
            continue;
        }
        $_POST[$k] = str_replace(',', '.', $v);
    }
    $sbr_cost = __paramInit('money', null, 'sbr_cost', 0.0, 10);
    $emp_cost = __paramInit('money', null, 'emp_cost', 0.0, 10);
    $frl_cost = __paramInit('money', null, 'frl_cost', 0.0, 10);
    $err = 0;
    if (!$frl_type || !$frl_type || !$scheme_type || !$currency) {
        $err = 1;
    }
    if ($sbr_cost + $emp_cost + $frl_cost <= 0) {
        $err = 1;
    }
    if ($sbr_cost && $sbr_cost < sbr_stages::MIN_COST_RUR) {
        $err = 1;
        $res['msg'] = iconv('CP1251', 'UTF8', 'Минимальный бюджет проекта - ' . sbr_stages::MIN_COST_RUR . ' руб.');
    }
    if ($sbr_cost && $sbr_cost < sbr_stages::MIN_COST_RUR_PDRD && ($scheme_type == sbr::SCHEME_PDRD || $scheme_type == sbr::SCHEME_PDRD2)) {
        $err = 1;
        $res['msg'] = iconv('CP1251', 'UTF8', 'Минимальный бюджет проекта - ' . sbr_stages::MIN_COST_RUR_PDRD . ' руб.');
    }
    if (!get_uid(false) || $err) {
        $res['success'] = false;
        echo json_encode($res);
        return;
    }
    $sbr_meta = sbr_meta::getInstance();
    $sbr_meta->scheme_type = $scheme_type;
    $schemes = $sbr_meta->getSchemes();
    $emp_total = $frl_total = 0;
    $emp_tax = $frl_tax = 0;
    $tcost = $sbr_cost;
    $sch = null;
    foreach ($schemes as $id => $scheme) {
        if ($scheme['type'] != $scheme_type) {
            continue;
        }
        $sch = $scheme;
    }
    $_taxes = $taxes = array();
    $rrq = array('U' => 0, 'Ff' => $frl_type, 'P' => $currency, 'Rf' => $rez_type);
    $pct = 0;
    foreach ($sch['taxes'][0] as $id => $tax) {
        $cost = sbr_meta::calcAnyTax($tax['id'], $tax['scheme_id'], $tcost, $rrq);
        $pct = $cost / $tcost;
        if (!$pct) {
            continue;
        }
        $tax['pct'] = $pct;
        $_taxes['frl'][] = $tax;
        $frl_tax += $pct * 100;
    }
    $rrq = array('U' => 1, 'Ff' => $frl_type, 'P' => $currency, 'Rf' => $rez_type, 'C' => $tcost);
    foreach ($sch['taxes'][1] as $id => $tax) {
        $cost = sbr_meta::calcAnyTax($tax['id'], $tax['scheme_id'], $tcost, $rrq);
        $pct = $cost / $tcost;
        if (!$pct) {
            continue;
        }
        $tax['pct'] = $pct;
        $_taxes['emp'][] = $tax;
        $emp_tax += $pct * 100;
    }
    if ($sbr_cost) {
        $emp_cost = $sbr_cost + $sbr_cost * ($emp_tax / 100);
        $frl_cost = $sbr_cost - $sbr_cost * ($frl_tax / 100);
    } elseif ($emp_cost) {
        $sbr_cost = $emp_cost - $emp_cost / (100 + $emp_tax) * $emp_tax;
        $frl_cost = $sbr_cost - $sbr_cost * $frl_tax / 100;
    } elseif ($frl_cost) {
        $sbr_cost = $frl_cost + $frl_cost / (100 - $frl_tax) * $frl_tax;
        $emp_cost = $sbr_cost + $sbr_cost * ($emp_tax / 100);
    }
    if ($sbr_cost && $sbr_cost < sbr_stages::MIN_COST_RUR) {
        $err = 1;
        $res['msg'] = iconv('CP1251', 'UTF8', 'Минимальный бюджет проекта - ' . sbr_stages::MIN_COST_RUR . ' руб.');
        $res['success'] = false;
        echo json_encode($res);
        return;
    }
    if ($rez_type == sbr::RT_UABYKZ && $sbr_cost && $sbr_cost > sbr::usd2rur(sbr::MAX_COST_USD)) {
        $err = 1;
        $sum = sbr_meta::view_cost(sbr::usd2rur(sbr::MAX_COST_USD), exrates::BANK);
        $sum = html_entity_decode($sum);
        $res['msg'] = iconv('CP1251', 'UTF8', 'Максимальный бюджет ' . $sum . ', поскольку исполнитель не является резидентом Российской Федерации');
        $res['success'] = false;
        echo json_encode($res);
        return;
    }
    if ($_taxes['frl']) {
        foreach ($_taxes['frl'] as $k => $tax) {
            $cost = $sbr_cost * $tax['pct'];
            if ($tax['tax_id'] == $sbr_meta->getTaxByCode('TAX_NDFL') || $tax['tax_id'] == $sbr_meta->getTaxByCode('TAX_NDFL_NR')) {
                $cost = sbr_meta::ndfl_round($cost);
            }
            $taxes['frl'][] = array('id' => $tax['id'], 'scheme' => $scheme_type, 'cost' => sbr_meta::view_cost($cost, null, false));
            $frl_total += $cost;
        }
    }
    foreach ($_taxes['emp'] as $k => $tax) {
        $cost = $sbr_cost * $tax['pct'];
        if ($tax['tax_id'] == $sbr_meta->getTaxByCode('TAX_NDFL') || $tax['tax_id'] == $sbr_meta->getTaxByCode('TAX_NDFL_NR')) {
            $cost = sbr_meta::ndfl_round($cost);
        }
        $taxes['emp'][] = array('id' => $tax['id'], 'scheme' => $scheme_type, 'cost' => sbr_meta::view_cost($cost, null, false));
        $emp_total += $cost;
    }
    $res['usr_type'] = intval($_POST['usr_type']);
    $res['taxes'] = $taxes;
    $res['emp_total'] = sbr_meta::view_cost($emp_total, null, false);
    $res['frl_total'] = sbr_meta::view_cost($frl_total, null, false);
    $res['emp_cost'] = round($sbr_cost + $emp_total, 2);
    $res['frl_cost'] = round($sbr_cost - $frl_total, 2);
    $res['sbr_cost'] = round($sbr_cost, 2);
    $res['rating_get'] = sbr_meta::getSBRRating($res['sbr_cost']);
    $res['hash'] = $GLOBALS['host'] . '/' . sbr::NEW_TEMPLATE_SBR . '/?site=calc&hash=' . $hash;
    $res['success'] = true;
    echo json_encode($res);
    return;
}