redmine

Updated

1 [submodule "genpdf/lib/3dparty/Inflect"] 1 [submodule "genpdf/lib/3dparty/Inflect"]
2 path = genpdf/lib/3dparty/Inflect 2 path = genpdf/lib/3dparty/Inflect
3 url = https://github.com/mephi-ut/Inflect 3 url = https://github.com/mephi-ut/Inflect
  4 +[submodule "genpdf/3rdparty/number-to-words"]
  5 + path = genpdf/3rdparty/number-to-words
  6 + url = https://github.com/kwn/number-to-words
number-to-words @ 6ca25054
  1 +Subproject commit 6ca25054089341a162c53dd6e81cff6478e5f5f5
@@ -309,7 +309,7 @@ foreach (array('contact', 'user', 'recipient') as $key) { @@ -309,7 +309,7 @@ foreach (array('contact', 'user', 'recipient') as $key) {
309 $appointment = appointment_voip2cps($appointment_voip); 309 $appointment = appointment_voip2cps($appointment_voip);
310 }*/ 310 }*/
311 311
312 - //if ($key == "recipient") { if (!empty($_COOKIE['debug'])) { print_r($_GET);var_dump($w_id);print_r($appointment);die(); } } 312 + //if ($key == "user") { if (!empty($_COOKIE['debug'])) { print_r($_GET);var_dump($w_id);print_r($appointment);die(); } }
313 313
314 considerAppointment($key, $appointment); 314 considerAppointment($key, $appointment);
315 } 315 }
  1 +<?php
  2 +
  3 +require_once "lib/3rdparty/Inflect/Inflect.php";
  4 +require_once '/var/www/ut.mephi.ru/ut-tex/genpdf/3rdparty/number-to-words/src/NumberToWords.php';
  5 +require_once '/var/www/ut.mephi.ru/ut-tex/genpdf/3rdparty/number-to-words/src/Legacy/Numbers/Words.php';
  6 +require_once '/var/www/ut.mephi.ru/ut-tex/genpdf/3rdparty/number-to-words/src/Legacy/Numbers/Words/Locale/Ru.php';
  7 +require_once '/var/www/ut.mephi.ru/ut-tex/genpdf/3rdparty/number-to-words/src/Exception/NumberToWordsException.php';
  8 +require_once '/var/www/ut.mephi.ru/ut-tex/genpdf/3rdparty/number-to-words/src/NumberTransformer/NumberTransformer.php';
  9 +require_once '/var/www/ut.mephi.ru/ut-tex/genpdf/3rdparty/number-to-words/src/NumberTransformer/RussianNumberTransformer.php';
  10 +require_once "config.php";
  11 +
  12 +use NumberToWords\NumberToWords;
  13 +
  14 +
  15 +$_GET = array_merge ($_GET, $_POST);
  16 +header( "Content-Type: text/plain" );
  17 +//print_r ($_GET); die();
  18 +//print_r($_GET);
  19 +
  20 +$me = NULL;
  21 +
  22 +$inflect = new Inflect();
  23 +
  24 +if (!isset($_GET['signer-serialized']))
  25 + die('Not authed');
  26 +
  27 +$user_serialized = base64_decode($_GET['signer-serialized']);
  28 +$hash = $_GET['signer-signature'];
  29 +$date = $_GET['signature-date'];
  30 +
  31 +$good_dates = array(date('Ymd', strtotime('-1 day')), date('Ymd'), date('Ymd', strtotime('+1 day')));
  32 +
  33 +if (!in_array($date, $good_dates)) {
  34 + die('Bad date');
  35 +}
  36 +
  37 +$_GET['current-date'] = substr($date, 6, 2).".".substr($date, 4, 2).".".substr($date, 0, 4);
  38 +
  39 +$good_signature = 0;
  40 +foreach ($SIGNKEYS as $key) {
  41 + if (sha1($user_serialized.$key.$date) === $hash) {
  42 + $good_signature = 1;
  43 + break;
  44 + }
  45 +}
  46 +if ($good_signature == 0)
  47 + die('Bad signature');
  48 +
  49 +$user = unserialize($user_serialized);
  50 +$user_redmine = NULL;
  51 +$username_drupal = $user->login;
  52 +$username_real = $username_drupal;
  53 +
  54 +if (in_array($username_real, $ADMINS)) {
  55 + if (!empty($_GET['force-login'])) {
  56 + $username_drupal = $_GET['force-login'];
  57 + $user = json_decode(file_get_contents("https://ut.mephi.ru/getldapentry.php?username=". urlencode($username_drupal) ));
  58 + @$user->employee = json_decode(file_get_contents("https://cps.mephi.ru/?cmd=getemployeeinfo&usevoip=true&username=".urlencode($username_drupal) ));
  59 + @$user->oauth = $user_oauth;
  60 + @$user->login = $login;
  61 + //print_r($user);
  62 + $_GET['signer-appointment-id'] = empty($_GET['force-appointment-id']) ? $user->employee->WORKPOSITIONS[0]->w_id : $_GET['force-appointment-id'];
  63 + }
  64 +}
  65 +
  66 +$subordinate_units = array();
  67 +
  68 +if (!empty($user->employee->WORKPOSITIONS)) {
  69 + foreach($user->employee->WORKPOSITIONS as $workposition) {
  70 + if ($workposition->is_head) {
  71 + $subordinate_units[$workposition->subdivision->s_id] = $workposition->subdivision;
  72 + }
  73 + }
  74 +}
  75 +
  76 +
  77 +//print_r($user);
  78 +
  79 +$employee = $user->employee;
  80 +
  81 +$appointment = null;
  82 +
  83 +foreach ($employee->WORKPOSITIONS as $_appointment) {
  84 + if ($_appointment->w_id == $_GET['signer-appointment-id']) {
  85 + $appointment = $_appointment;
  86 + break;
  87 + }
  88 +}
  89 +
  90 +if (empty($employee)) {
  91 + die("The employee doesn't exists");
  92 +}
  93 +
  94 +$dont_inflect_patterns = array(
  95 + '/^нач\./',
  96 + '/^зам\./',
  97 + '/^заф\./',
  98 + '/^нач\./',
  99 + '/^\./',
  100 + '/^спец\./',
  101 +);
  102 +
  103 +$inflect_cut_patterns = array(
  104 + //'/^(.*)[иы]й[ ]/', // Commented due to task: https://tasks.mephi.ru/issues/49157
  105 + '/^\[\d+\] /',
  106 +);
  107 +
  108 +$inflect_appointment_abj_patterns = array(
  109 + '/^(.*)[иы]й[ ]/', // https://tasks.mephi.ru/issues/49157
  110 +);
  111 +
  112 +$inflect_ignore_ending_patterns = array(
  113 + '[ ].*',
  114 +);
  115 +
  116 +function _tempdir_cleanup($dir) {
  117 +# system('rm -rf "'.$dir.'"');
  118 +}
  119 +
  120 +function tempdir($dir=FALSE, $prefix='php') {
  121 + $tempfile = tempnam(sys_get_temp_dir(),'');
  122 +
  123 + if (file_exists($tempfile))
  124 + unlink($tempfile);
  125 +
  126 + mkdir($tempfile);
  127 + if (is_dir($tempfile)) {
  128 + register_shutdown_function("_tempdir_cleanup", $tempfile);
  129 + return $tempfile;
  130 + }
  131 +
  132 + return FALSE;
  133 +}
  134 +
  135 +function body_parse($body, $data) {
  136 + $data_withbraces = array();
  137 +
  138 + foreach ($data as $key => $value)
  139 + $data_withbraces['[{'.$key.'}]'] = $value;
  140 +
  141 + $body = str_replace(array_keys($data_withbraces), array_values($data_withbraces), $body);
  142 +
  143 + return $body;
  144 +}
  145 +
  146 +function error($str) {
  147 + die($str);
  148 + header('Location: '.$_GET['callback_url'].'?errmsg='.urlencode($str));
  149 + error_log($str);
  150 + exit(0);
  151 +}
  152 +
  153 +function latexSpecialChars( $string )
  154 +{
  155 + $map = array(
  156 + "#"=>"\\#",
  157 + "$"=>"\\$",
  158 + "%"=>"\\%",
  159 + "&"=>"\\&",
  160 + "~"=>"\\~{}",
  161 + "_"=>"\\_",
  162 + "^"=>"\\^{}",
  163 + "\\"=>"\\textbackslash ",
  164 + "{"=>"\\{",
  165 + "}"=>"\\}",
  166 + );
  167 + return preg_replace( "/([\^\%~\\\\#\$%&_\{\}])/e", "\$map['$1']", $string );
  168 +}
  169 +
  170 +
  171 +function my_mb_ucfirst($str, $e='utf-8') {
  172 + $fc = mb_strtoupper(mb_substr($str, 0, 1, $e), $e);
  173 + return $fc.mb_substr($str, 1, mb_strlen($str, $e), $e);
  174 +}
  175 +
  176 +function formatDate($date) {
  177 + $date = split('T', split(' ', $date)[0])[0];
  178 + $words = split('-', $date);
  179 + return $words[2].'.'.$words[1].'.'.$words[0];
  180 +}
  181 +
  182 +$file = $_GET['file'];
  183 +
  184 +$datas = array();
  185 +
  186 +switch ($file) {
  187 + case 'form/updatesalary':
  188 + case 'form/updateenddate': {
  189 + $themes_raw = json_decode(file_get_contents('https://sd.mephi.ru/api/9/get_themes.json?api_key=JY3FUioBmsu1o'), 1);
  190 +
  191 + $themes = array();
  192 + foreach($themes_raw as $theme_raw) {
  193 + $themes['1'.$theme_raw['bcod'].$theme_raw['ecod']] = $theme_raw;
  194 + }
  195 + //print_r($themes);die();
  196 +
  197 + function newFormularData($stfCardId) {
  198 + global $themes;
  199 +
  200 + $salaries = array(
  201 + '1-1' => 7800,
  202 + '1-2' => 7810,
  203 +
  204 + '2-1' => 7820,
  205 + '2-2' => 7830,
  206 + '2-3' => 7840,
  207 + '2-4' => 7850,
  208 + '2-5' => 7855,
  209 +
  210 + '3-1' => 7860,
  211 + '3-2' => 7870,
  212 + '3-3' => 7880,
  213 + '3-4' => 7890,
  214 + '3-5' => 7900,
  215 +
  216 + '4-1' => 7910,
  217 + '4-2' => 7920,
  218 + '4-3' => 7930,
  219 + '4-4' => 8240,
  220 + '4-5' => 8585,
  221 + '4-6' => 8925,
  222 + );
  223 +
  224 + $a = json_decode(file_get_contents('https://sd.mephi.ru/api/1/formulars.json?from_local_db=true&active_only=true&api_key='.SDAPI_KEY.'&filter_StfCardId='.preg_replace('/[^0-9]+/', '', $stfCardId)), 1)[0];
  225 +
  226 + $profKvalLevel = preg_replace('/[.].*+/', '', @$a['ProfKvalLevel']);
  227 + $salary = $a['RateAmount']*$salaries[(@$a['ProfKvalGroup']).'-'.$profKvalLevel];
  228 +
  229 + $employeePrefix = '';
  230 + if ($a['Cod_Tip_formular']==3 || $a['Cod_Tip_formular']==5) { // Совместитель
  231 + $as = json_decode(file_get_contents('https://sd.mephi.ru/api/1/formulars.json?from_local_db=true&active_only=true&api_key='.SDAPI_KEY.'&filter_EmpGUID'.preg_replace('/[^0-9]+/', '', $a['EmpGUID'])), 1);
  232 + $employeePrefix = '________________________________';
  233 + foreach ($as as $_a) {
  234 + if ($_a['Cod_Tip_formular'] == 2) {
  235 + $words0 = split(' ', chop($a['Name']));
  236 + $words1 = split(' ', $inflect->getInflectName($a['Name_podr'], 0));
  237 + if ($words0[count($words0)-1] == $words1[0]) {
  238 + unset($words0[count($words0)-1]);
  239 + }
  240 + $employeePrefix = join(' ', array_merge($words0, $words1));
  241 + break;
  242 + }
  243 + }
  244 + }
  245 +
  246 + $unitName = '_________________';
  247 + $unitHead = '_________________';
  248 + if (!empty($a['ChiefName'])) {
  249 + //$unitHeadFullname = $curUnit['ChiefName'];
  250 + $unitName = mb_strtoupper(chop($a['Podr_BriefName'])).' ['.$a['Code'].']';
  251 + $unitHeadFullname = preg_replace('/[ ][ ]+/', ' ', chop(str_replace(array('и.о. ', 'и.о.'), array('', ''), preg_replace('/[0-9].*+/', '', chop($a['ChiefName'])))));
  252 + $unitHeadNameWords = split(' ', preg_replace('/[А-Я][.]/', '', $unitHeadFullname));
  253 + $unitHeadLastname = $unitHeadNameWords[0];
  254 + if (mb_strtoupper($a['SurName']) == mb_strtoupper($unitHeadLastname)) {
  255 + $curUnit = json_decode(file_get_contents('https://sd.mephi.ru/api/1/units.json?from_local_db=true&active_only=true&api_key='.SDAPI_KEY.'&filter_id='.preg_replace('/[^0-9]+/', '', $a['OrgDiv'])), 1)[0];
  256 +
  257 + $parentUnit = json_decode(file_get_contents('https://sd.mephi.ru/api/1/units.json?from_local_db=true&active_only=true&api_key='.SDAPI_KEY.'&filter_id='.preg_replace('/[^0-9]+/', '', $curUnit['ParentId'])), 1)[0];
  258 + $unitName = mb_strtoupper(chop($parentUnit['BriefName'])).' ['.$parentUnit['Code'].']';
  259 + $unitHeadFullname = $parentUnit['ChiefName'];
  260 + $unitHeadNameWords = split(' ', preg_replace('/[А-Я][.]/', '', $unitHeadFullname));
  261 + $unitHeadLastname = $unitHeadNameWords[0];
  262 + }
  263 +
  264 + if (count($unitHeadNameWords) != 3) {
  265 + $employeesByLastname = json_decode(file_get_contents('https://sd.mephi.ru/api/1/formulars.json?from_local_db=true&active_only=true&api_key='.SDAPI_KEY.'&filter_SurName='.preg_replace('/[^а-яА-Я]+/', '', $unitHeadLastname)), 1);
  266 + $fullnames = array();
  267 + foreach ($employeesByLastname as $employee) {
  268 + $fullnames[$employee['SurName'].' '.$employee['FirstName'].' '.$employee['MiddleName']] = true;
  269 + }
  270 + if (count($fullnames) == 1) {
  271 + $unitHeadFullname = $fullnames[0];
  272 + $unitHeadNameWords = split(' ', $unitHeadFullname);
  273 + }
  274 + }
  275 +
  276 + if (count($unitHeadNameWords) == 3) {
  277 + $unitHead = $unitHeadNameWords[0].' '.mb_substr($unitHeadNameWords[1], 0, 1).'.'.mb_substr($unitHeadNameWords[2], 0, 1).'.';
  278 + }
  279 + }
  280 +
  281 + $numberToWords = new NumberToWords();
  282 + $numberTransformer = $numberToWords->getNumberTransformer('ru');
  283 +
  284 + $account = '_____________';
  285 + $account_manager = '_________________';
  286 +
  287 + if (!empty($themes[$a['Code']])) {
  288 + //$account = $themes[$a['Code']]['theme'];
  289 + }
  290 +
  291 + return array(
  292 + 'of-prorector' => '_________________',
  293 + 'prorector' => '_________________',
  294 + 'attorney-date' => '«___» __________ 20___г',
  295 + 'attorney-code' => '_____________',
  296 +
  297 + 'of-unit' => $unitName,
  298 + 'unit-head' => $unitHead,
  299 + 'employee' => $a['SurName'].' '.mb_substr($a['FirstName'], 0, 1).'.'.mb_substr($a['MiddleName'], 0, 1).'.',
  300 + 'employee-full' => $employeePrefix.$a['SurName'].' '.$a['FirstName'].' '.$a['MiddleName'],
  301 + 'employment-contract-date' => formatDate($a['ContDate']),
  302 + 'employment-contract-code' => $a['ContNo'],
  303 + 'ProfKvalGroup' => @$a['ProfKvalGroup'],
  304 + 'ProfKvalLevel' => $profKvalLevel,
  305 + 'Salary' => $salary.' руб.',
  306 + 'SalaryText' => $numberTransformer->toWords($salary).' рублей ноль копеек',
  307 + 'MultiplyingFactor' => '______',
  308 + 'MultiplyingFactorReason' => '_________________________________',
  309 + 'endDateWas' => formatDate($a['EndDate']),
  310 + 'endDateBecame' => $_GET['newEndDate'],
  311 + 'account' => $account,
  312 + 'account-manager' => $account_manager,
  313 + 'change-date' => '«___» __________ 20___г',
  314 + );
  315 + }
  316 + switch ($_GET['object-type']) {
  317 + case 'employee':
  318 + $employee_id = @$_GET['employee-id'];
  319 + if (empty($employee_id))
  320 + die('employee_id is empty');
  321 +
  322 + $employee = json_decode(file_get_contents("https://cps.mephi.ru/?cmd=getemployeeinfo&e_id=".urlencode($employee_id) ), 1)[$employee_id];
  323 +
  324 + foreach ($employee['WORKPOSITIONS'] as $idx => $workposition) {
  325 + if (empty($subordinate_units[$workposition['s_id']])) {
  326 + unset($employee['WORKPOSITIONS'][$idx]);
  327 + }
  328 + }
  329 +
  330 + if (empty($employee['WORKPOSITIONS'])) {
  331 + die("Нет должностей данного сотрудника в вашем подчинении");
  332 + }
  333 +
  334 + foreach ($employee['WORKPOSITIONS'] as $workposition) {
  335 + $newData = newFormularData($workposition['w_id']);
  336 + if (!empty($newData)) {
  337 + $datas[] = $newData;
  338 + }
  339 + }
  340 +
  341 +// $unit;
  342 + break;
  343 + case 'unit':
  344 + $unit_id = @$_GET['unit-id'];
  345 + if (empty($unit_id))
  346 + die('unit_id is empty');
  347 +
  348 + if (empty($subordinate_units[$unit_id]))
  349 + die('У вас нет прав для доступа к данному подразделению');
  350 +
  351 + $unit_id_asu = preg_replace('/[^0-9]+/', '', $unit_id);
  352 +
  353 + $unit_ids = array($unit_id_asu);
  354 + $check_unit_ids = array($unit_id_asu);
  355 + while ($id = array_shift($check_unit_ids)) {
  356 + $units = json_decode(file_get_contents('https://sd.mephi.ru/api/1/units.json?from_local_db=true&active_only=true&api_key='.SDAPI_KEY.'&filter_ParentId='.$id), 1);
  357 + foreach ($units as &$unit) {
  358 + array_push($unit_ids, $unit['id']);
  359 + array_push($check_unit_ids, $unit['id']);
  360 + }
  361 + }
  362 +
  363 + $formulars = array();
  364 +
  365 + foreach($unit_ids as $unit_id) {
  366 + $l_formulars = json_decode(file_get_contents('https://sd.mephi.ru/api/1/formulars.json?from_local_db=true&active_only=true&api_key='.SDAPI_KEY.'&filter_OrgDiv='.$unit_id), 1);
  367 + $formulars = array_merge($formulars, $l_formulars);
  368 + }
  369 +
  370 + foreach ($formulars as &$formular) {
  371 + $newData = newFormularData($formular['StfCardId']);
  372 + if (!empty($newData)) {
  373 + $datas[] = $newData;
  374 + }
  375 + }
  376 +
  377 + break;
  378 + default:
  379 + die('Unknown object type: '.$_GET['object-type']);
  380 + }
  381 +
  382 + break;
  383 + }
  384 +
  385 + default:
  386 + die("Don't know how to generate PDF for ".$file);
  387 +}
  388 +
  389 +//print_r($_GET); print_r($user); print_r($datas);die();
  390 +
  391 +if (empty($datas)) {
  392 + die("Нет данных для обработки");
  393 +}
  394 +
  395 +foreach ($datas as $data) {
  396 +
  397 + $tempdir = tempdir();
  398 +
  399 + if ($tempdir === FALSE)
  400 + die('Cannot create temporary directory');
  401 +
  402 + exec('cp -a /var/www/'.$_SERVER['SERVER_NAME'].'/ut-tex/mephimemo "'.$tempdir.'/"');
  403 + chdir($tempdir.'/mephimemo');
  404 + file_put_contents("request.data", print_r($data, true));
  405 +
  406 + foreach ($data as $key => &$value)
  407 + $value = latexSpecialChars($value);
  408 +
  409 + file_put_contents("request.data.fixed", print_r($data, true));
  410 +
  411 + switch ($file) {
  412 + case 'form/updatesalary':
  413 + case 'form/updateenddate':
  414 + $header = '\mmsagreementheader{'.$data['of-prorector'].'}{'.$data['attorney-date'].'}{'.$data['attorney-code'].'}{'.$data['employee-full'].'}{'.$data['employment-contract-date'].'}{'.$data['employment-contract-code'].'}{'.$data['change-date'].'}';
  415 + $footer = '\mmsagreementfooter{'.$data['prorector'].'}{'.$data['of-unit'].'}{'.$data['unit-head'].'}{'.$data['account'].'}{'.$data['account-manager'].'}{}{}{'.$data['employee'].'}';
  416 + break;
  417 + }
  418 +
  419 + $body_template = file_get_contents('template/'.$file.'.tex');
  420 + $body = body_parse($body_template, $data);
  421 +
  422 + file_put_contents('header.tex', $header);
  423 + file_put_contents('body.tex', $body);
  424 + file_put_contents('footer.tex', $footer);
  425 + file_put_contents('responsible.tex', '');
  426 + file_put_contents('urlqr.tex', '');
  427 +
  428 + exec('make');
  429 + $pdf_content = file_get_contents('memo.pdf');
  430 +
  431 + if ( empty($pdf_content) )
  432