Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class lol extends RateService {
- const RATE_TYPE_CIVIL = 'sadsadas';
- }
- $lol = new lol;
- $lol->saveNew(array());
- abstract class RateService {
- /*
- * formatowanie kodu pozostawia wiele do życzenia częściowo standardy PSR, ale puste linie, za długie linie,
- * const ERR_NUMBER_OF_DIGITS_OVER_SIX nie jest zdefiniowana
- * const RATE_TYPE_LABOUR nie jest zdefiniowana
- * PDO wyrzuci błąd (array jako pierwszy parametr)
- * literówka w nazwie metody seveNewCivil()
- * metoda saveNew jako argument powinna dostać zmienną $data, prawdopodobnie, że tablicę.
- * Domyślam się, że $_POST['type'] to to samo co $data['type'], a jeśli nie - to powinno być ujednolicone
- * przed wywołaniem i sprawdzenie $data['type'].
- * pojawiają się metody które nie istnieją
- * myślę, że może to być klasa abstrakcyjna, ale zamiast self::RATE_TYPE_CIVIL itd użyłbym static::RATE_TYPE_CIVIL
- * lub kod dla starej wersji php
- * powtarzanie str_replace wygląda mało estetycznie ale czytelnie
- * */
- public $database;
- public function saveNew($data, $idUser = 1) {
- var_dump(static::RATE_TYPE_CIVIL);exit;
- $this->database = new PDO([
- 'pdo' => CRM::getDatabase(),
- 'host' => DATABASE_HOSTNAME,
- 'dbname' => DATABASE_BASENAME,
- 'user' => DATABASE_USERNAME,
- 'password' => DATABASE_PASSWORD,
- ]);
- $errors = [];
- if ($_POST['type'] == self::RATE_TYPE_CIVIL) {
- // echo('civil');
- return $this->seveNewCivil($data, $idUser);
- } else if ($data['type'] == self::RATE_TYPE_LABOUR) {
- // echo('labour');
- $data['clientDayRate'] = (float) str_replace(',', '.', $_POST['clientDayRate']);
- $data['clientNightRate'] = (float) str_replace(',', '.', $data['clientNightRate']);
- $data['clientOver50Rate'] = (float) str_replace(',', '.', $data['clientOver50Rate']);
- $data['clientOver100Rate'] = (float) str_replace(',', '.', $data['clientOver100Rate']);
- $data['clientOvernightRate'] = (float) str_replace(',', '.', $data['clientOvernightRate']);
- $data['clientMonthRate'] = (float) str_replace(
- ',', '.', empty($data['clientMonthRate']) ? 0 : $data['clientMonthRate']);
- $data['clientPieceRate'] = (float) str_replace(
- ',', '.', empty($data['clientPieceRate']) ? 0 : $data['clientPieceRate']);
- $data['workerDayRate'] = (float) str_replace(',', '.', $data['workerDayRate']);
- $data['workerMonthRate'] = (float) str_replace(
- ',', '.', empty($data['workerMonthRate']) ? 0 : $data['workerMonthRate']);
- $data['workerPieceRate'] = (float) str_replace(
- ',', '.', empty($data['workerPieceRate']) ? 0 : $data['workerPieceRate']);
- if ((int) $data['occupation'] == 0) {
- $errors['occupation'] = 'Stanowisko musi zostać wybrane';
- }
- 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
- ) {
- $errors['labour_digits_over_six'] = self::ERR_NUMBER_OF_DIGITS_OVER_SIX;
- }
- $rate['id_client'] = $data['client'];
- $rate['client_day'] = $data['clientDayRate'];
- $rate['client_night'] = $data['clientNightRate'];
- $rate['client_over50'] = $data['clientOver50Rate'];
- $rate['client_over100'] = $data['clientOver100Rate'];
- $rate['client_overnight'] = $data['clientOvernightRate'];
- $rate['worker_day'] = $data['workerDayRate'];
- $rate['client_month'] = $data['clientMonthRate'];
- $rate['client_piece'] = $data['clientPieceRate'];
- $rate['worker_month'] = $data['workerMonthRate'];
- $rate['worker_piece'] = $data['workerPieceRate'];
- $rate['rate_description'] = $data['rate_description'];
- $rate['id_business_activity'] = $data['id_business_activity'];
- $validateLabourRate = $this->validateLabourRate($rate + $data);
- if (!empty($validateLabourRate)) {
- $errors = array_merge($errors, $validateLabourRate);
- }
- if ($data['validStart'] == '' || $data['validEnd'] == '' || strtotime($data['validStart']) > strtotime($data['validEnd']) || !isDate($data['validStart']) || !isDate($data['validEnd'])
- ) {
- $errors['validStart'] = 'Podano błędny zakres dat';
- }
- if ($this->workerRateExists($data)) {
- $errors['clientOvernightRate'] = 'Stawki (stawka pracownika) nakładają się w czasie';
- }
- if (!empty($errors)) {
- return $errors;
- } else {
- $sql = "
- INSERT INTO rate_labour SET
- id_firma = :client,
- id_occupation = :occupation,
- client_day = :clientDayRate,
- client_night = :clientNightRate,
- client_over50 = :clientOver50Rate,
- client_over100 = :clientOver100Rate,
- client_overnight = :clientOvernightRate,
- client_month = :clientMonthRate,
- client_piece = :clientPieceRate,
- worker_day = :workerDayRate,
- worker_month = :workerMonthRate,
- worker_piece = :workerPieceRate,
- valid_start = :validStart,
- valid_end = :validEnd,
- who_add = :idUser,
- rate_description = :rateDescription,
- id_business_activity = :idBusinessActivity,
- status = 'pending'";
- $stmt = $this->database->prepare($sql);
- $stmt->bindParam(':client', $data['client']);
- $stmt->bindParam(':occupation', $data['occupation']);
- $stmt->bindParam(':clientDayRate', $data['clientDayRate'], PDO::PARAM_STR);
- $stmt->bindParam(':clientNightRate', $data['clientNightRate'], PDO::PARAM_STR);
- $stmt->bindParam(':clientOver50Rate', $data['clientOver50Rate'], PDO::PARAM_STR);
- $stmt->bindParam(':clientOver100Rate', $data['clientOver100Rate'], PDO::PARAM_STR);
- $stmt->bindParam(':clientOvernightRate', $data['clientOvernightRate'], PDO::PARAM_STR);
- $stmt->bindParam(':clientMonthRate', $data['clientMonthRate'], PDO::PARAM_STR);
- $stmt->bindParam(':clientPieceRate', $data['clientPieceRate'], PDO::PARAM_STR);
- $stmt->bindParam(':workerDayRate', $data['workerDayRate'], PDO::PARAM_STR);
- $stmt->bindParam(':workerMonthRate', $data['workerMonthRate'], PDO::PARAM_STR);
- $stmt->bindParam(':workerPieceRate', $data['workerPieceRate'], PDO::PARAM_STR);
- $stmt->bindParam(':validStart', $data['validStart'], PDO::PARAM_STR);
- $stmt->bindParam(':validEnd', $data['validEnd'], PDO::PARAM_STR);
- $stmt->bindParam(':rateDescription', $data['rate_description'], PDO::PARAM_STR);
- $businessActivity = $this->nullIfEmpty($data['id_business_activity']);
- $stmt->bindParam(':idBusinessActivity', $businessActivity);
- $stmt->bindParam(':idUser', $idUser);
- if (!$stmt->execute()) {
- throw new Crmpl_Exception_Database($stmt);
- }
- $idRate = $this->database->lastInsertId();
- $rowID = $this->database->query('SELECT id_rate_labour
- FROM rate_labour
- ORDER BY id_rate_labour
- DESC
- LIMIT 1');
- $rowID = $rowID->fetch();
- $rowClientContractType = $this->database->query('SELECT firma.nazwa,
- umowa_praca_stanowisko.stanowisko
- FROM rate_labour
- JOIN umowa_praca_stanowisko
- ON umowa_praca_stanowisko.id = rate_labour.id_occupation
- JOIN firma ON
- firma.id_firma = rate_labour.id_firma
- WHERE rate_labour.id_rate_labour = ' . $rowID['id_rate_labour']);
- $rowClientContractType = $rowClientContractType->fetch();
- $rateInfo['id_rate_labour'] = $rowID['id_rate_labour'];
- $rateInfo['client_name'] = $rowClientContractType['nazwa'];
- $rateInfo['occupation'] = $rowClientContractType['stanowisko'];
- $rateData['client_day'] = $data['clientDayRate'];
- $rateData['client_night'] = $data['clientNightRate'];
- $rateData['client_over50'] = $data['clientOver50Rate'];
- $rateData['client_over100'] = $data['clientOver100Rate'];
- $rateData['client_overnight'] = $data['clientOvernightRate'];
- $rateData['client_month'] = $data['clientMonthRate'];
- $rateData['client_piece'] = $data['clientPieceRate'];
- $rateData['worker_day'] = $data['workerDayRate'];
- $rateData['worker_month'] = $data['workerMonthRate'];
- $rateData['worker_piece'] = $data['workerPieceRate'];
- $rateData['valid_start'] = $data['validStart'];
- $rateData['valid_end'] = $data['validEnd'];
- $mailData = array(
- 'rateInfo' => $rateInfo,
- 'rateData' => array($rateData),
- 'rateAction' => 'rate_added'
- );
- $title = "Nowa stawka do zweryfikowania";
- $notification = new Crmpl_Notifications_Model_MailNotification_Rates_Labour($mailData, $title);
- $notification->send();
- return $idRate;
- }
- } else if ($_POST['type'] == self::RATE_TYPE_RECRUTATION) {
- $rate = (float) str_replace(',', '.', $data['rate']);
- if ($rate == 0) {
- $errors['rate'] = 'Nieprawidłowa stawka';
- }
- if (strlen($data['occupation']) == 0) {
- $errors['occupation'] = 'Stanowisko musi zostać podane';
- }
- if ($data['validStart'] == '' || $data['validEnd'] == '' || strtotime($data['validStart']) > strtotime($data['validEnd']) || !isDate($data['validStart']) || !isDate($data['validEnd'])
- ) {
- $errors['validStart'] = 'Podano błędny zakres dat';
- }
- if (numberOfDigitsAfterComma($data['rate']) > 6) {
- $errors['recrutation_digits_over_six'] = self::ERR_NUMBER_OF_DIGITS_OVER_SIX;
- }
- if ($this->workerRateExists($data)) {
- $errors['validEnd'] = 'W tym czasie obowiązuje jest identyczna rekrutacja';
- }
- if (!empty($errors)) {
- return $errors;
- }
- $vat = $this->civilPaylistService->getTax('Stawka VAT', $data['valid_start']);
- $sql = "
- INSERT INTO rate_recrutation SET
- id_client = :client,
- occupation = :occupation,
- rate = :rate,
- vat = :vat,
- valid_start = :validStart,
- valid_end = :validEnd,
- who_add = :idUser,
- status = 'pending'";
- $stmt = $this->database->prepare($sql);
- $stmt->bindParam(':client', $data['client'], PDO::PARAM_INT);
- $stmt->bindParam(':occupation', $data['occupation'], PDO::PARAM_INT);
- $stmt->bindParam(':rate', $rate, PDO::PARAM_STR);
- $stmt->bindParam(':vat', $vat, PDO::PARAM_STR);
- $stmt->bindParam(':validStart', $data['validStart'], PDO::PARAM_STR);
- $stmt->bindParam(':validEnd', $data['validEnd'], PDO::PARAM_STR);
- $stmt->bindParam(':idUser', $idUser, PDO::PARAM_INT);
- $stmt->execute();
- $idRate = $this->database->lastInsertId();
- $rowID = $this->database->query('SELECT id_rate_recrutation
- FROM rate_recrutation
- ORDER BY id_rate_recrutation
- DESC
- LIMIT 1')->fetch();
- $rowClientContractType = $this->database->query('SELECT firma.nazwa
- FROM rate_recrutation
- JOIN firma
- ON firma.id_firma = rate_recrutation.id_client
- WHERE rate_recrutation.id_rate_recrutation = ' . $rowID["id_rate_recrutation"]);
- $rowClientContractType = $rowClientContractType->fetch();
- $data['client_name'] = $rowClientContractType['nazwa'];
- $data['valid_start'] = $data['validStart'];
- $data['valid_end'] = $data['validEnd'];
- $mailData = array(
- 'rateInfo' => $data,
- 'rateAction' => 'rate_added'
- );
- $title = "Nowa stawka do zweryfikowania";
- $notification = new Crmpl_Notifications_Model_MailNotification_Rates_Recrutation($mailData, $title);
- $notification->send();
- return $idRate;
- } else {
- throw new Crm_Exception_InvalidParameter($data['type'], 'One of RateService::RATE_TYPE_*');
- }
- }
- protected function seveNewCivil($data, $userId = 1) {
- $this->database = new PDO([
- 'pdo' => CRM::getDatabase(),
- 'host' => DATABASE_HOSTNAME,
- 'dbname' => DATABASE_BASENAME,
- 'user' => DATABASE_USERNAME,
- 'password' => DATABASE_PASSWORD,
- ]);
- $data['clientDayRate'] = $clientDayRate = (float) str_replace(',', '.', $data['clientDayRate']);
- $data['clientNightRate'] = $clientNightRate = (float) str_replace(',', '.', $data['clientNightRate']);
- $data['clientOtherRate'] = $clientOtherRate = (float) str_replace(',', '.', $data['clientOtherRate']);
- $data['workerDayRate'] = $workerDayRate = (float) str_replace(',', '.', $data['workerDayRate']);
- $data['workerNightRate'] = $workerNightRate = (float) str_replace(',', '.', $data['workerNightRate']);
- $data['workerOtherRate'] = $workerOtherRate = (float) str_replace(',', '.', $data['workerOtherRate']);
- $rate['id_client'] = $data['client'];
- $rate['worker_day'] = $workerDayRate;
- $rate['worker_night'] = $workerNightRate;
- $rate['worker_other'] = $workerOtherRate;
- $rate['client_day'] = $clientDayRate;
- $rate['client_night'] = $clientNightRate;
- $rate['client_other'] = $clientOtherRate;
- $rate['valid_start'] = $data['validStart'];
- $rate['valid_end'] = $data['validEnd'];
- $rate['rate_description'] = $data['rate_description'];
- $rate['id_business_activity'] = $data['id_business_activity'];
- $rateData = [];
- $rateData['client_day'] = $clientDayRate;
- $rateData['client_night'] = $clientNightRate;
- $rateData['client_other'] = $clientOtherRate;
- $rateData['worker_day'] = $workerDayRate;
- $rateData['worker_night'] = $workerNightRate;
- $rateData['worker_other'] = $workerOtherRate;
- $rateData['valid_start'] = $data['validStart'];
- $rateData['valid_end'] = $data['validEnd'];
- $errors = $this->validateCivilRateRules($data, $rate);
- if (!empty($errors)) {
- return $errors;
- } else {
- $sql = '
- INSERT INTO rate_civil SET
- id_client = :client,
- contract_type = :contract_type,
- id_activity = :activity,
- client_day = :clientDayRate,
- client_night = :clientNightRate,
- client_other = :clientOtherRate,
- worker_day = :workerDayRate,
- worker_night = :workerNightRate,
- worker_other = :workerOtherRate,
- valid_start = :validStart,
- valid_end = :validEnd,
- who_add = :idUser,
- status = "pending",
- rate_description = :rateDescription,
- id_business_activity = :idBusinessActivity';
- $stmt = $this->database->prepare($sql);
- $stmt->bindParam(':client', $data['client'], PDO::PARAM_INT);
- $stmt->bindParam(':contract_type', $data['contract_type'], PDO::PARAM_INT);
- $stmt->bindParam(':activity', $data['activity'], PDO::PARAM_STR);
- $stmt->bindParam(':clientDayRate', $clientDayRate, PDO::PARAM_STR);
- $stmt->bindParam(':clientNightRate', $clientNightRate, PDO::PARAM_STR);
- $stmt->bindParam(':clientOtherRate', $clientOtherRate, PDO::PARAM_STR);
- $stmt->bindParam(':workerDayRate', $workerDayRate, PDO::PARAM_STR);
- $stmt->bindParam(':workerNightRate', $workerNightRate, PDO::PARAM_STR);
- $stmt->bindParam(':workerOtherRate', $workerOtherRate, PDO::PARAM_STR);
- $stmt->bindParam(':validStart', $data['validStart'], PDO::PARAM_STR);
- $stmt->bindParam(':validEnd', $data['validEnd'], PDO::PARAM_STR);
- $stmt->bindParam(':idUser', $userId, PDO::PARAM_INT);
- $stmt->bindParam(':rateDescription', $data['rate_description'], PDO::PARAM_STR);
- $businessActivity = $this->nullIfEmpty($data['id_business_activity']);
- $stmt->bindParam(':idBusinessActivity', $businessActivity, PDO::PARAM_INT);
- if (!$stmt->execute()) {
- throw new Crmpl_Exception_Database($stmt);
- }
- $idRate = $this->database->lastInsertId();
- $this->createMailNotificationForRate($idRate, $rateData);
- return $idRate;
- }
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement