Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 19.20 KB | None | 0 0
  1. <?php
  2. class lol extends RateService {
  3.     const RATE_TYPE_CIVIL = 'sadsadas';
  4. }
  5. $lol = new lol;
  6. $lol->saveNew(array());
  7. abstract class RateService {
  8. /*
  9.  * formatowanie kodu pozostawia wiele do życzenia częściowo standardy PSR, ale puste linie, za długie linie,
  10.  * const ERR_NUMBER_OF_DIGITS_OVER_SIX nie jest zdefiniowana
  11.  * const RATE_TYPE_LABOUR nie jest zdefiniowana
  12.  * PDO wyrzuci błąd (array jako pierwszy parametr)
  13.  * literówka w nazwie metody seveNewCivil()
  14.  * metoda saveNew jako argument powinna dostać zmienną $data, prawdopodobnie, że tablicę. 
  15.  *   Domyślam się, że $_POST['type'] to to samo co $data['type'], a jeśli nie - to powinno być ujednolicone
  16.  *   przed wywołaniem i sprawdzenie $data['type'].
  17.  * pojawiają się metody które nie istnieją
  18.  * myślę, że może to być klasa abstrakcyjna, ale zamiast self::RATE_TYPE_CIVIL itd użyłbym static::RATE_TYPE_CIVIL
  19.  * lub kod dla starej wersji php
  20.  * powtarzanie str_replace wygląda mało estetycznie ale czytelnie
  21.  * */
  22.     public $database;
  23.     public function saveNew($data, $idUser = 1) {
  24.         var_dump(static::RATE_TYPE_CIVIL);exit;
  25.         $this->database = new PDO([
  26.             'pdo'       => CRM::getDatabase(),
  27.             'host'      => DATABASE_HOSTNAME,
  28.             'dbname'    => DATABASE_BASENAME,
  29.             'user'      => DATABASE_USERNAME,
  30.             'password'  => DATABASE_PASSWORD,
  31.         ]);
  32.         $errors = [];
  33.         if ($_POST['type'] == self::RATE_TYPE_CIVIL) {
  34.             // echo('civil');
  35.             return $this->seveNewCivil($data, $idUser);
  36.         } else if ($data['type'] == self::RATE_TYPE_LABOUR) {
  37.             // echo('labour');
  38.             $data['clientDayRate'] = (float) str_replace(',', '.', $_POST['clientDayRate']);
  39.             $data['clientNightRate'] = (float) str_replace(',', '.', $data['clientNightRate']);
  40.             $data['clientOver50Rate'] = (float) str_replace(',', '.', $data['clientOver50Rate']);
  41.             $data['clientOver100Rate'] = (float) str_replace(',', '.', $data['clientOver100Rate']);
  42.             $data['clientOvernightRate'] = (float) str_replace(',', '.', $data['clientOvernightRate']);
  43.             $data['clientMonthRate'] = (float) str_replace(
  44.                             ',', '.', empty($data['clientMonthRate']) ? 0 : $data['clientMonthRate']);
  45.             $data['clientPieceRate'] = (float) str_replace(
  46.                             ',', '.', empty($data['clientPieceRate']) ? 0 : $data['clientPieceRate']);
  47.             $data['workerDayRate'] = (float) str_replace(',', '.', $data['workerDayRate']);
  48.             $data['workerMonthRate'] = (float) str_replace(
  49.                             ',', '.', empty($data['workerMonthRate']) ? 0 : $data['workerMonthRate']);
  50.             $data['workerPieceRate'] = (float) str_replace(
  51.                             ',', '.', empty($data['workerPieceRate']) ? 0 : $data['workerPieceRate']);
  52.  
  53.             if ((int) $data['occupation'] == 0) {
  54.                 $errors['occupation'] = 'Stanowisko musi zostać wybrane';
  55.             }
  56.  
  57.             if (numberOfDigitsAfterComma($data['clientDayRate']) > 6 || numberOfDigitsAfterComma($data['clientOver50Rate']) > 6 || numberOfDigitsAfterComma($data['clientOver100Rate']) > 6 || numberOfDigitsAfterComma($data['clientNightRate']) > 6 || numberOfDigitsAfterComma($data['clientOvernightRate']) > 6 || numberOfDigitsAfterComma($data['clientMonthRate']) > 6 || numberOfDigitsAfterComma($data['clientPieceRate']) > 6 || numberOfDigitsAfterComma($data['workerDayRate']) > 2 || numberOfDigitsAfterComma($data['workerMonthRate']) > 2 || numberOfDigitsAfterComma($data['workerPieceRate']) > 2
  58.             ) {
  59.                 $errors['labour_digits_over_six'] = self::ERR_NUMBER_OF_DIGITS_OVER_SIX;
  60.             }
  61.  
  62.             $rate['id_client'] = $data['client'];
  63.             $rate['client_day'] = $data['clientDayRate'];
  64.             $rate['client_night'] = $data['clientNightRate'];
  65.             $rate['client_over50'] = $data['clientOver50Rate'];
  66.             $rate['client_over100'] = $data['clientOver100Rate'];
  67.             $rate['client_overnight'] = $data['clientOvernightRate'];
  68.             $rate['worker_day'] = $data['workerDayRate'];
  69.             $rate['client_month'] = $data['clientMonthRate'];
  70.             $rate['client_piece'] = $data['clientPieceRate'];
  71.             $rate['worker_month'] = $data['workerMonthRate'];
  72.             $rate['worker_piece'] = $data['workerPieceRate'];
  73.             $rate['rate_description'] = $data['rate_description'];
  74.             $rate['id_business_activity'] = $data['id_business_activity'];
  75.  
  76.             $validateLabourRate = $this->validateLabourRate($rate + $data);
  77.             if (!empty($validateLabourRate)) {
  78.                 $errors = array_merge($errors, $validateLabourRate);
  79.             }
  80.  
  81.             if ($data['validStart'] == '' || $data['validEnd'] == '' || strtotime($data['validStart']) > strtotime($data['validEnd']) || !isDate($data['validStart']) || !isDate($data['validEnd'])
  82.             ) {
  83.                 $errors['validStart'] = 'Podano błędny zakres dat';
  84.             }
  85.             if ($this->workerRateExists($data)) {
  86.                 $errors['clientOvernightRate'] = 'Stawki (stawka pracownika) nakładają się w czasie';
  87.             }
  88.             if (!empty($errors)) {
  89.                 return $errors;
  90.             } else {
  91.                 $sql = "
  92.                    INSERT INTO rate_labour SET
  93.                        id_firma = :client,
  94.                        id_occupation = :occupation,
  95.                        client_day = :clientDayRate,
  96.                        client_night = :clientNightRate,
  97.                        client_over50 = :clientOver50Rate,
  98.                        client_over100 = :clientOver100Rate,
  99.                        client_overnight = :clientOvernightRate,
  100.                        client_month = :clientMonthRate,
  101.                        client_piece = :clientPieceRate,
  102.                        worker_day = :workerDayRate,
  103.                        worker_month = :workerMonthRate,
  104.                        worker_piece = :workerPieceRate,
  105.                        valid_start = :validStart,
  106.                        valid_end = :validEnd,
  107.                        who_add = :idUser,
  108.                        rate_description = :rateDescription,
  109.                        id_business_activity = :idBusinessActivity,
  110.                        status = 'pending'";
  111.                 $stmt = $this->database->prepare($sql);
  112.                 $stmt->bindParam(':client', $data['client']);
  113.                 $stmt->bindParam(':occupation', $data['occupation']);
  114.                 $stmt->bindParam(':clientDayRate', $data['clientDayRate'], PDO::PARAM_STR);
  115.                 $stmt->bindParam(':clientNightRate', $data['clientNightRate'], PDO::PARAM_STR);
  116.                 $stmt->bindParam(':clientOver50Rate', $data['clientOver50Rate'], PDO::PARAM_STR);
  117.                 $stmt->bindParam(':clientOver100Rate', $data['clientOver100Rate'], PDO::PARAM_STR);
  118.                 $stmt->bindParam(':clientOvernightRate', $data['clientOvernightRate'], PDO::PARAM_STR);
  119.                 $stmt->bindParam(':clientMonthRate', $data['clientMonthRate'], PDO::PARAM_STR);
  120.                 $stmt->bindParam(':clientPieceRate', $data['clientPieceRate'], PDO::PARAM_STR);
  121.                 $stmt->bindParam(':workerDayRate', $data['workerDayRate'], PDO::PARAM_STR);
  122.                 $stmt->bindParam(':workerMonthRate', $data['workerMonthRate'], PDO::PARAM_STR);
  123.                 $stmt->bindParam(':workerPieceRate', $data['workerPieceRate'], PDO::PARAM_STR);
  124.                 $stmt->bindParam(':validStart', $data['validStart'], PDO::PARAM_STR);
  125.                 $stmt->bindParam(':validEnd', $data['validEnd'], PDO::PARAM_STR);
  126.                 $stmt->bindParam(':rateDescription', $data['rate_description'], PDO::PARAM_STR);
  127.                 $businessActivity = $this->nullIfEmpty($data['id_business_activity']);
  128.                 $stmt->bindParam(':idBusinessActivity', $businessActivity);
  129.                 $stmt->bindParam(':idUser', $idUser);
  130.  
  131.                 if (!$stmt->execute()) {
  132.                     throw new Crmpl_Exception_Database($stmt);
  133.                 }
  134.  
  135.                 $idRate = $this->database->lastInsertId();
  136.  
  137.                 $rowID = $this->database->query('SELECT id_rate_labour
  138.                                                  FROM rate_labour
  139.                                                  ORDER BY id_rate_labour
  140.                                                  DESC
  141.                                                  LIMIT 1');
  142.                 $rowID = $rowID->fetch();
  143.  
  144.                 $rowClientContractType = $this->database->query('SELECT firma.nazwa,
  145.                                                                         umowa_praca_stanowisko.stanowisko
  146.                                                                 FROM rate_labour
  147.                                                                 JOIN umowa_praca_stanowisko
  148.                                                                     ON umowa_praca_stanowisko.id = rate_labour.id_occupation
  149.                                                                 JOIN firma ON
  150.                                                                   firma.id_firma = rate_labour.id_firma
  151.                                                                 WHERE rate_labour.id_rate_labour = ' . $rowID['id_rate_labour']);
  152.                 $rowClientContractType = $rowClientContractType->fetch();
  153.  
  154.                 $rateInfo['id_rate_labour'] = $rowID['id_rate_labour'];
  155.                 $rateInfo['client_name'] = $rowClientContractType['nazwa'];
  156.                 $rateInfo['occupation'] = $rowClientContractType['stanowisko'];
  157.                 $rateData['client_day'] = $data['clientDayRate'];
  158.                 $rateData['client_night'] = $data['clientNightRate'];
  159.                 $rateData['client_over50'] = $data['clientOver50Rate'];
  160.                 $rateData['client_over100'] = $data['clientOver100Rate'];
  161.                 $rateData['client_overnight'] = $data['clientOvernightRate'];
  162.                 $rateData['client_month'] = $data['clientMonthRate'];
  163.                 $rateData['client_piece'] = $data['clientPieceRate'];
  164.                 $rateData['worker_day'] = $data['workerDayRate'];
  165.                 $rateData['worker_month'] = $data['workerMonthRate'];
  166.                 $rateData['worker_piece'] = $data['workerPieceRate'];
  167.                 $rateData['valid_start'] = $data['validStart'];
  168.                 $rateData['valid_end'] = $data['validEnd'];
  169.  
  170.                 $mailData = array(
  171.                     'rateInfo' => $rateInfo,
  172.                     'rateData' => array($rateData),
  173.                     'rateAction' => 'rate_added'
  174.                 );
  175.  
  176.                 $title = "Nowa stawka do zweryfikowania";
  177.                 $notification = new Crmpl_Notifications_Model_MailNotification_Rates_Labour($mailData, $title);
  178.                 $notification->send();
  179.  
  180.                 return $idRate;
  181.             }
  182.         } else if ($_POST['type'] == self::RATE_TYPE_RECRUTATION) {
  183.  
  184.             $rate = (float) str_replace(',', '.', $data['rate']);
  185.             if ($rate == 0) {
  186.                 $errors['rate'] = 'Nieprawidłowa stawka';
  187.             }
  188.             if (strlen($data['occupation']) == 0) {
  189.                 $errors['occupation'] = 'Stanowisko musi zostać podane';
  190.             }
  191.             if ($data['validStart'] == '' || $data['validEnd'] == '' || strtotime($data['validStart']) > strtotime($data['validEnd']) || !isDate($data['validStart']) || !isDate($data['validEnd'])
  192.             ) {
  193.                 $errors['validStart'] = 'Podano błędny zakres dat';
  194.             }
  195.             if (numberOfDigitsAfterComma($data['rate']) > 6) {
  196.                 $errors['recrutation_digits_over_six'] = self::ERR_NUMBER_OF_DIGITS_OVER_SIX;
  197.             }
  198.             if ($this->workerRateExists($data)) {
  199.                 $errors['validEnd'] = 'W tym czasie obowiązuje jest identyczna rekrutacja';
  200.             }
  201.             if (!empty($errors)) {
  202.                 return $errors;
  203.             }
  204.  
  205.             $vat = $this->civilPaylistService->getTax('Stawka VAT', $data['valid_start']);
  206.  
  207.             $sql = "
  208.                INSERT INTO rate_recrutation SET
  209.                        id_client = :client,
  210.                        occupation = :occupation,
  211.                        rate = :rate,
  212.                        vat = :vat,
  213.                        valid_start = :validStart,
  214.                        valid_end = :validEnd,
  215.                        who_add = :idUser,
  216.                        status = 'pending'";
  217.             $stmt = $this->database->prepare($sql);
  218.             $stmt->bindParam(':client', $data['client'], PDO::PARAM_INT);
  219.             $stmt->bindParam(':occupation', $data['occupation'], PDO::PARAM_INT);
  220.             $stmt->bindParam(':rate', $rate, PDO::PARAM_STR);
  221.             $stmt->bindParam(':vat', $vat, PDO::PARAM_STR);
  222.             $stmt->bindParam(':validStart', $data['validStart'], PDO::PARAM_STR);
  223.             $stmt->bindParam(':validEnd', $data['validEnd'], PDO::PARAM_STR);
  224.             $stmt->bindParam(':idUser', $idUser, PDO::PARAM_INT);
  225.             $stmt->execute();
  226.             $idRate = $this->database->lastInsertId();
  227.  
  228.             $rowID = $this->database->query('SELECT id_rate_recrutation
  229.                                              FROM rate_recrutation
  230.                                              ORDER BY id_rate_recrutation
  231.                                              DESC
  232.                                              LIMIT 1')->fetch();
  233.  
  234.             $rowClientContractType = $this->database->query('SELECT firma.nazwa
  235.                                                          FROM rate_recrutation
  236.                                                          JOIN firma
  237.                                                          ON firma.id_firma = rate_recrutation.id_client
  238.                                                          WHERE rate_recrutation.id_rate_recrutation = ' . $rowID["id_rate_recrutation"]);
  239.             $rowClientContractType = $rowClientContractType->fetch();
  240.  
  241.             $data['client_name'] = $rowClientContractType['nazwa'];
  242.             $data['valid_start'] = $data['validStart'];
  243.             $data['valid_end'] = $data['validEnd'];
  244.  
  245.             $mailData = array(
  246.                 'rateInfo' => $data,
  247.                 'rateAction' => 'rate_added'
  248.             );
  249.  
  250.             $title = "Nowa stawka do zweryfikowania";
  251.             $notification = new Crmpl_Notifications_Model_MailNotification_Rates_Recrutation($mailData, $title);
  252.             $notification->send();
  253.  
  254.             return $idRate;
  255.         } else {
  256.             throw new Crm_Exception_InvalidParameter($data['type'], 'One of RateService::RATE_TYPE_*');
  257.         }
  258.     }
  259.  
  260. protected function seveNewCivil($data, $userId = 1) {
  261.         $this->database = new PDO([
  262.             'pdo'       => CRM::getDatabase(),
  263.             'host'      => DATABASE_HOSTNAME,
  264.             'dbname'    => DATABASE_BASENAME,
  265.             'user'      => DATABASE_USERNAME,
  266.             'password'  => DATABASE_PASSWORD,
  267.         ]);
  268.         $data['clientDayRate'] = $clientDayRate = (float) str_replace(',', '.', $data['clientDayRate']);
  269.         $data['clientNightRate'] = $clientNightRate = (float) str_replace(',', '.', $data['clientNightRate']);
  270.         $data['clientOtherRate'] = $clientOtherRate = (float) str_replace(',', '.', $data['clientOtherRate']);
  271.         $data['workerDayRate'] = $workerDayRate = (float) str_replace(',', '.', $data['workerDayRate']);
  272.         $data['workerNightRate'] = $workerNightRate = (float) str_replace(',', '.', $data['workerNightRate']);
  273.         $data['workerOtherRate'] = $workerOtherRate = (float) str_replace(',', '.', $data['workerOtherRate']);
  274.  
  275.         $rate['id_client'] = $data['client'];
  276.         $rate['worker_day'] = $workerDayRate;
  277.         $rate['worker_night'] = $workerNightRate;
  278.         $rate['worker_other'] = $workerOtherRate;
  279.         $rate['client_day'] = $clientDayRate;
  280.         $rate['client_night'] = $clientNightRate;
  281.         $rate['client_other'] = $clientOtherRate;
  282.         $rate['valid_start'] = $data['validStart'];
  283.         $rate['valid_end'] = $data['validEnd'];
  284.         $rate['rate_description'] = $data['rate_description'];
  285.         $rate['id_business_activity'] = $data['id_business_activity'];
  286.  
  287.         $rateData = [];
  288.         $rateData['client_day'] = $clientDayRate;
  289.         $rateData['client_night'] = $clientNightRate;
  290.         $rateData['client_other'] = $clientOtherRate;
  291.         $rateData['worker_day'] = $workerDayRate;
  292.         $rateData['worker_night'] = $workerNightRate;
  293.         $rateData['worker_other'] = $workerOtherRate;
  294.         $rateData['valid_start'] = $data['validStart'];
  295.         $rateData['valid_end'] = $data['validEnd'];
  296.  
  297.         $errors = $this->validateCivilRateRules($data, $rate);
  298.  
  299.         if (!empty($errors)) {
  300.             return $errors;
  301.         } else {
  302.             $sql = '
  303.                INSERT INTO rate_civil SET
  304.                    id_client = :client,
  305.                    contract_type = :contract_type,
  306.                    id_activity = :activity,
  307.                    client_day = :clientDayRate,
  308.                    client_night = :clientNightRate,
  309.                    client_other = :clientOtherRate,
  310.                    worker_day = :workerDayRate,
  311.                    worker_night = :workerNightRate,
  312.                    worker_other = :workerOtherRate,
  313.                    valid_start = :validStart,
  314.                    valid_end = :validEnd,
  315.                    who_add = :idUser,
  316.                    status = "pending",
  317.                    rate_description = :rateDescription,
  318.                    id_business_activity = :idBusinessActivity';
  319.             $stmt = $this->database->prepare($sql);
  320.             $stmt->bindParam(':client', $data['client'], PDO::PARAM_INT);
  321.             $stmt->bindParam(':contract_type', $data['contract_type'], PDO::PARAM_INT);
  322.             $stmt->bindParam(':activity', $data['activity'], PDO::PARAM_STR);
  323.             $stmt->bindParam(':clientDayRate', $clientDayRate, PDO::PARAM_STR);
  324.             $stmt->bindParam(':clientNightRate', $clientNightRate, PDO::PARAM_STR);
  325.             $stmt->bindParam(':clientOtherRate', $clientOtherRate, PDO::PARAM_STR);
  326.             $stmt->bindParam(':workerDayRate', $workerDayRate, PDO::PARAM_STR);
  327.             $stmt->bindParam(':workerNightRate', $workerNightRate, PDO::PARAM_STR);
  328.             $stmt->bindParam(':workerOtherRate', $workerOtherRate, PDO::PARAM_STR);
  329.             $stmt->bindParam(':validStart', $data['validStart'], PDO::PARAM_STR);
  330.             $stmt->bindParam(':validEnd', $data['validEnd'], PDO::PARAM_STR);
  331.             $stmt->bindParam(':idUser', $userId, PDO::PARAM_INT);
  332.             $stmt->bindParam(':rateDescription', $data['rate_description'], PDO::PARAM_STR);
  333.             $businessActivity = $this->nullIfEmpty($data['id_business_activity']);
  334.             $stmt->bindParam(':idBusinessActivity', $businessActivity, PDO::PARAM_INT);
  335.  
  336.             if (!$stmt->execute()) {
  337.                 throw new Crmpl_Exception_Database($stmt);
  338.             }
  339.  
  340.             $idRate = $this->database->lastInsertId();
  341.  
  342.             $this->createMailNotificationForRate($idRate, $rateData);
  343.  
  344.             return $idRate;
  345.         }
  346.     }
  347. }
  348. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement