From 6b3c9b4bd2cf6886b4eb1aaab8050f7e1e590dbb Mon Sep 17 00:00:00 2001 From: EliteHacker228 Date: Sat, 20 Nov 2021 18:07:44 +0500 Subject: [PATCH 01/18] update project --- controllers/IndexController.php | 35 +++++++++++++++++++++++++++++++-- core/Request.php | 11 +++++++++++ index.php | 22 +++++++++++++++++---- 3 files changed, 62 insertions(+), 6 deletions(-) diff --git a/controllers/IndexController.php b/controllers/IndexController.php index f7e8633..fb47508 100644 --- a/controllers/IndexController.php +++ b/controllers/IndexController.php @@ -8,10 +8,41 @@ class IndexController */ public $name = 'index'; - public function indexAction() + /** + * @var ArticleRepository + */ + protected $articleRepository; + + public function __construct(ArticleRepository $articleRepository) + { + $this->articleRepository = $articleRepository; + } + + + public function indexAction(Request $request) { + $articles = $this->articleRepository->getAll(); + return new Response( + $this->render('articles', [ + 'articles' => $articles + ]) + ); + } + + public function showAction(Request $request) + { + $id = $request->getQueryParameter("id"); + + $article = is_int($id) ? $this->articleRepository->getById($id) : null; + + if ($article === null) { + return new Response('Page not found', '404', 'Not found'); + } + return new Response( - 'Blank index' + $this->render('article', [ + 'article' => $article + ]) ); } diff --git a/core/Request.php b/core/Request.php index 83932c9..7ddbcc7 100644 --- a/core/Request.php +++ b/core/Request.php @@ -95,4 +95,15 @@ public function getPath() { return $this->path; } + /** + * Return get parameter by name + * @param $name + * @return mixed|null + */ + public function getQueryParameter($name) + { + return isset($this->originalGet[$name]) ? $this->originalGet[$name] : null; + } + + } \ No newline at end of file diff --git a/index.php b/index.php index 80cbf01..7e8396f 100644 --- a/index.php +++ b/index.php @@ -6,11 +6,23 @@ require_once 'core/Response.php'; require_once 'core/Router.php'; +require_once 'repositories/ArticleRepository.php'; + include_once 'config/routes.php'; +include_once 'config/database.php'; + $router = new Router($routes); $request = Request::createFromGlobals(); + +$dsn = sprintf("mysql:host=%s;dbname=%s;charset=%s", $database['database_host'], $database['database_name'], $database['charset']); +/** @var PDO $connection */ +$connection = new PDO( $dsn, $database['username'], $database['password']); + +$articleRepository = new ArticleRepository($connection); + + try { $route = $router->match($request->getPath()); } catch (\InvalidArgumentException $exception) { @@ -20,12 +32,14 @@ ]; } -$controllerClassName = sprintf('%sController', - ucfirst($route['controller'])); +$controllers = [ + 'index' => new IndexController($articleRepository), + 'helloWorld' => new HelloWorldController(), +]; +$controller = $controllers[$route['controller']]; $actionMethod = $route['action'] . 'Action'; -$controller = new $controllerClassName(); /** @var Response $response */ -$response = $controller->$actionMethod(); +$response = $controller->$actionMethod($request); $response->send(); \ No newline at end of file From e8c368909848744a1b74fc36adc3be969f3f7b4c Mon Sep 17 00:00:00 2001 From: EliteHacker228 Date: Sat, 20 Nov 2021 18:59:33 +0500 Subject: [PATCH 02/18] update sql support --- config/database.php | 9 ++++++++ config/routes.php | 4 ++++ controllers/HelloWorldController.php | 1 + repositories/ArticleRepository.php | 34 ++++++++++++++++++++++++++++ sql/article.sql | 8 +++++++ templates/article.php | 11 +++++++++ templates/articles.php | 22 ++++++++++++++++++ 7 files changed, 89 insertions(+) create mode 100644 config/database.php create mode 100644 repositories/ArticleRepository.php create mode 100644 sql/article.sql create mode 100644 templates/article.php create mode 100644 templates/articles.php diff --git a/config/database.php b/config/database.php new file mode 100644 index 0000000..92aa9a0 --- /dev/null +++ b/config/database.php @@ -0,0 +1,9 @@ + 'localhost', + 'database_name' => 'framework', + 'username' => 'root', + 'password' => null, + 'charset' => 'utf8' +]; diff --git a/config/routes.php b/config/routes.php index 70142af..652feba 100644 --- a/config/routes.php +++ b/config/routes.php @@ -5,6 +5,10 @@ 'controller' => 'index', 'action' => 'index' ], + '/show' => [ + 'controller' => 'index', + 'action' => 'show' + ], '/hello' => [ 'controller' => 'helloWorld', 'action' => 'hello' diff --git a/controllers/HelloWorldController.php b/controllers/HelloWorldController.php index 7ff0cae..956d305 100644 --- a/controllers/HelloWorldController.php +++ b/controllers/HelloWorldController.php @@ -18,6 +18,7 @@ public function helloAction() { 'title' => 'Hello page', 'text' => 'hello' ])); + } diff --git a/repositories/ArticleRepository.php b/repositories/ArticleRepository.php new file mode 100644 index 0000000..cda39c3 --- /dev/null +++ b/repositories/ArticleRepository.php @@ -0,0 +1,34 @@ +connection = $connection; + } + + /** + * Return all Articles + */ + public function getAll() + { + $statement = $this->connection->query("SELECT * FROM articles"); + return $statement->fetchAll(); + } + + /** + * Return Article by ID + */ + public function getById($id) + { + $statement = $this->connection->prepare("SELECT * FROM articles WHERE id = :id LIMIT 1"); + + $statement->execute([ + "id" => $id + ]); + + return $statement->fetch(); + } +} \ No newline at end of file diff --git a/sql/article.sql b/sql/article.sql new file mode 100644 index 0000000..7e5f3d4 --- /dev/null +++ b/sql/article.sql @@ -0,0 +1,8 @@ +CREATE TABLE IF NOT EXISTS articles ( + id INT (11) NOT NULL AUTO_INCREMENT PRIMARY KEY, + name varchar (255) not null, + body text +) ENGINE=InnoDB; + +INSERT INTO articles (name, body) VALUES ('Hello', 'Hello world long long page...'); +INSERT INTO articles (name, body) VALUES ('World', 'World hello extra long long page...'); \ No newline at end of file diff --git a/templates/article.php b/templates/article.php new file mode 100644 index 0000000..440001c --- /dev/null +++ b/templates/article.php @@ -0,0 +1,11 @@ + + + + + +

+

+ +back to articles list + + \ No newline at end of file diff --git a/templates/articles.php b/templates/articles.php new file mode 100644 index 0000000..19840d6 --- /dev/null +++ b/templates/articles.php @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file From af8248f58f8b6122f96c0e704237c63ba89e008d Mon Sep 17 00:00:00 2001 From: max Date: Sun, 21 Nov 2021 11:05:52 -0800 Subject: [PATCH 03/18] add buchgalteric table --- controllers/IndexController.php | 23 ++++++++++----- index.php | 7 +++-- repositories/AgentsRepository.php | 30 +++++++++++++++++++ sql/article.sql | 49 +++++++++++++++++++++++++++---- 4 files changed, 94 insertions(+), 15 deletions(-) create mode 100644 repositories/AgentsRepository.php diff --git a/controllers/IndexController.php b/controllers/IndexController.php index fb47508..206f3dd 100644 --- a/controllers/IndexController.php +++ b/controllers/IndexController.php @@ -12,26 +12,33 @@ class IndexController * @var ArticleRepository */ protected $articleRepository; + protected $sellsRepository; + protected $agentsRepository; - public function __construct(ArticleRepository $articleRepository) + public function __construct(ArticleRepository $articleRepository, SellsRepository $sellsRepository, AgentsRepository $agentsRepository) { $this->articleRepository = $articleRepository; + $this->sellsRepository = $sellsRepository; + $this->agentsRepository = $agentsRepository; } + //Will return table of | agent_name | num_of_sellf | sum | public function indexAction(Request $request) { - $articles = $this->articleRepository->getAll(); - return new Response( - $this->render('articles', [ - 'articles' => $articles - ]) - ); +// $articles = $this->articleRepository->getAll(); +// return new Response( +// $this->render('articles', [ +// 'articles' => $articles +// ]) +// ); +// $sells = $this->sellsRepository->getAll(); + return new Response($this->render('comission-total', ['sellsRepository' => $this->sellsRepository, 'agentsRepository' => $this->agentsRepository])); } public function showAction(Request $request) { - $id = $request->getQueryParameter("id"); + $id = (int)$request->getQueryParameter("id"); $article = is_int($id) ? $this->articleRepository->getById($id) : null; diff --git a/index.php b/index.php index 7e8396f..16a6dd8 100644 --- a/index.php +++ b/index.php @@ -7,6 +7,8 @@ require_once 'core/Router.php'; require_once 'repositories/ArticleRepository.php'; +require_once 'repositories/SellsRepository.php'; +require_once 'repositories/AgentsRepository.php'; include_once 'config/routes.php'; include_once 'config/database.php'; @@ -21,7 +23,8 @@ $connection = new PDO( $dsn, $database['username'], $database['password']); $articleRepository = new ArticleRepository($connection); - +$sellsRepository = new SellsRepository($connection); +$agentsRepository = new AgentsRepository($connection); try { $route = $router->match($request->getPath()); @@ -33,7 +36,7 @@ } $controllers = [ - 'index' => new IndexController($articleRepository), + 'index' => new IndexController($articleRepository, $sellsRepository, $agentsRepository), 'helloWorld' => new HelloWorldController(), ]; diff --git a/repositories/AgentsRepository.php b/repositories/AgentsRepository.php new file mode 100644 index 0000000..83b9d92 --- /dev/null +++ b/repositories/AgentsRepository.php @@ -0,0 +1,30 @@ +connection = $connection; + } + + public function getAgentNameById($id) + { + $statement = $this->connection->query("SELECT * FROM agents WHERE id = " . $id); + $statement->execute(); + return $statement->fetch()["full_name"]; + } + + public function getAllAgents(){ + $statement = $this->connection->query("SELECT * FROM agents"); + $statement->execute(); + return $statement->fetch(); + } + + public function getAllAgentsIds(){ + $statement = $this->connection->query("SELECT DISTINCT id FROM agents"); + $statement->execute(); + return $statement->fetchAll(); + } +} \ No newline at end of file diff --git a/sql/article.sql b/sql/article.sql index 7e5f3d4..24fa273 100644 --- a/sql/article.sql +++ b/sql/article.sql @@ -1,8 +1,47 @@ -CREATE TABLE IF NOT EXISTS articles ( +# CREATE TABLE IF NOT EXISTS articles ( +# id INT (11) NOT NULL AUTO_INCREMENT PRIMARY KEY, +# name varchar (255) not null, +# body text +# ) ENGINE=InnoDB; +# +# INSERT INTO articles (name, body) VALUES ('Hello', 'Hello world long long page...'); +# INSERT INTO articles (name, body) VALUES ('World', 'World hello extra long long page...'); + +CREATE TABLE IF NOT EXISTS agents ( id INT (11) NOT NULL AUTO_INCREMENT PRIMARY KEY, - name varchar (255) not null, - body text + full_name varchar (255) not null +) ENGINE=InnoDB; + +# INSERT INTO agents (full_name) VALUE ('Peter Ivanovich Kuznetsov'); #1 +# INSERT INTO agents (full_name) VALUE ('Andei Nikolaevich Trofimov'); #2 +# INSERT INTO agents (full_name) VALUE ('Anatolii Nikitevich Lazarev'); #3 + +# drop table apartments_sells; + +CREATE TABLE IF NOT EXISTS apartments_sells ( + agent_id INT(11) NOT NULL, + + sum INT(11) NOT NULL, + contract_number varchar (255) not null primary key , + apartment_number INT(11) NOT NULL, + living_complex varchar (255) not null, + FOREIGN KEY (agent_id) REFERENCES agents(id) ) ENGINE=InnoDB; -INSERT INTO articles (name, body) VALUES ('Hello', 'Hello world long long page...'); -INSERT INTO articles (name, body) VALUES ('World', 'World hello extra long long page...'); \ No newline at end of file +INSERT INTO apartments_sells (agent_id, sum, contract_number, apartment_number, living_complex) +VALUES (1, 125000, 101, 228, 'Bebra-town'); + +INSERT INTO apartments_sells (agent_id, sum, contract_number, apartment_number, living_complex) +VALUES (1, 14800, 102, 789, 'Golden-wind'); + +INSERT INTO apartments_sells (agent_id, sum, contract_number, apartment_number, living_complex) +VALUES (1, 59000, 103, 139, 'Jmihograd'); + +INSERT INTO apartments_sells (agent_id, sum, contract_number, apartment_number, living_complex) +VALUES (2, 75890, 201, 78, 'Bebra-town'); + +INSERT INTO apartments_sells (agent_id, sum, contract_number, apartment_number, living_complex) +VALUES (2, 65483, 202, 98, 'Jmihograd'); + +INSERT INTO apartments_sells (agent_id, sum, contract_number, apartment_number, living_complex) +VALUES (3, 69492, 301, 54, 'Golden-wind'); \ No newline at end of file From 689275b26eba8dada651586bc5317bbb33776758 Mon Sep 17 00:00:00 2001 From: max Date: Sun, 21 Nov 2021 11:10:49 -0800 Subject: [PATCH 04/18] add buchgalteric table 2 --- .idea/dataSources.xml | 12 +++++++ .idea/vcs.xml | 6 ++++ repositories/SellsRepository.php | 23 ++++++++++++ templates/comission-total.php | 61 ++++++++++++++++++++++++++++++++ 4 files changed, 102 insertions(+) create mode 100644 .idea/dataSources.xml create mode 100644 .idea/vcs.xml create mode 100644 repositories/SellsRepository.php create mode 100644 templates/comission-total.php diff --git a/.idea/dataSources.xml b/.idea/dataSources.xml new file mode 100644 index 0000000..68ff19c --- /dev/null +++ b/.idea/dataSources.xml @@ -0,0 +1,12 @@ + + + + + mysql.8 + true + com.mysql.cj.jdbc.Driver + jdbc:mysql://localhost:3306/framework + $ProjectFileDir$ + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/repositories/SellsRepository.php b/repositories/SellsRepository.php new file mode 100644 index 0000000..6074a7e --- /dev/null +++ b/repositories/SellsRepository.php @@ -0,0 +1,23 @@ +connection = $connection; + } + + public function getAll() + { + $statement = $this->connection->query("SELECT * FROM apartments_sells"); + return $statement->fetchAll(); + } + + public function getAllByAgentId($id) + { + $statement = $this->connection->query("SELECT * FROM apartments_sells WHERE agent_id = " . $id); + return $statement->fetchAll(); + } +} \ No newline at end of file diff --git a/templates/comission-total.php b/templates/comission-total.php new file mode 100644 index 0000000..9f21b4a --- /dev/null +++ b/templates/comission-total.php @@ -0,0 +1,61 @@ + + + + + + +getAllAgentsIds() as $id): ?> +

getAgentNameById($id[0]) ?>

+ +
IDName
+ + + + + + + + + getAllByAgentId($id[0]) as $sell): ?> + + + + + + + + + + + +
IDNameSumContract numberAppartment numberLiving complex name
getAgentNameById($sell['agent_id']) ?>
+

Total:

+ +

Grand total:

+ + + + + + + + + + + + + + + +getAgentNameById($sell['agent_id'])?> + + + + + + + + + + + \ No newline at end of file From 872e37611d2c6306983128a284fd7f1584df6684 Mon Sep 17 00:00:00 2001 From: max Date: Sun, 21 Nov 2021 13:11:00 -0800 Subject: [PATCH 05/18] add form for new sells registration --- config/routes.php | 8 ++++++ controllers/IndexController.php | 49 ++------------------------------- core/Request.php | 4 +++ index.php | 8 ++++-- templates/add-sell.php | 23 ++++++++++++++++ templates/welcome.php | 11 ++++++++ 6 files changed, 54 insertions(+), 49 deletions(-) create mode 100644 templates/add-sell.php create mode 100644 templates/welcome.php diff --git a/config/routes.php b/config/routes.php index 652feba..425362c 100644 --- a/config/routes.php +++ b/config/routes.php @@ -5,6 +5,14 @@ 'controller' => 'index', 'action' => 'index' ], + '/addsell' => [ + 'controller' => 'table', + 'action' => 'addSell' + ], + '/table' => [ + 'controller' => 'table', + 'action' => 'showTable' + ], '/show' => [ 'controller' => 'index', 'action' => 'show' diff --git a/controllers/IndexController.php b/controllers/IndexController.php index 206f3dd..bc388e5 100644 --- a/controllers/IndexController.php +++ b/controllers/IndexController.php @@ -2,55 +2,14 @@ class IndexController { - /** - * Action name - * @var string - */ - public $name = 'index'; - - /** - * @var ArticleRepository - */ - protected $articleRepository; - protected $sellsRepository; - protected $agentsRepository; - - public function __construct(ArticleRepository $articleRepository, SellsRepository $sellsRepository, AgentsRepository $agentsRepository) - { - $this->articleRepository = $articleRepository; - $this->sellsRepository = $sellsRepository; - $this->agentsRepository = $agentsRepository; - } - - - //Will return table of | agent_name | num_of_sellf | sum | public function indexAction(Request $request) { -// $articles = $this->articleRepository->getAll(); -// return new Response( -// $this->render('articles', [ -// 'articles' => $articles -// ]) -// ); -// $sells = $this->sellsRepository->getAll(); - return new Response($this->render('comission-total', ['sellsRepository' => $this->sellsRepository, 'agentsRepository' => $this->agentsRepository])); + return new Response($this->render('welcome')); } - public function showAction(Request $request) + public function errorAction(Request $request) { - $id = (int)$request->getQueryParameter("id"); - - $article = is_int($id) ? $this->articleRepository->getById($id) : null; - - if ($article === null) { - return new Response('Page not found', '404', 'Not found'); - } - - return new Response( - $this->render('article', [ - 'article' => $article - ]) - ); + return new Response('Sorry, but this page doesn\'t exist', '404', 'Not found'); } protected function render($templateName, $vars = []) @@ -68,6 +27,4 @@ public function __call($name, $arguments) return new Response('Sorry but this action not found', '404', 'Not found'); } - - } \ No newline at end of file diff --git a/core/Request.php b/core/Request.php index 7ddbcc7..3eea3a5 100644 --- a/core/Request.php +++ b/core/Request.php @@ -105,5 +105,9 @@ public function getQueryParameter($name) return isset($this->originalGet[$name]) ? $this->originalGet[$name] : null; } + public function getPostParameter($name) + { + return isset($this->originalPost[$name]) ? $this->originalPost[$name] : null; + } } \ No newline at end of file diff --git a/index.php b/index.php index 16a6dd8..7036d61 100644 --- a/index.php +++ b/index.php @@ -1,6 +1,7 @@ match($request->getPath()); -} catch (\InvalidArgumentException $exception) { +} catch (InvalidArgumentException $exception) { $route = [ 'controller' => 'index', 'action' => 'index' @@ -36,8 +37,9 @@ } $controllers = [ - 'index' => new IndexController($articleRepository, $sellsRepository, $agentsRepository), + 'table' => new TableController($articleRepository, $sellsRepository, $agentsRepository), 'helloWorld' => new HelloWorldController(), + 'index' => new IndexController() ]; $controller = $controllers[$route['controller']]; diff --git a/templates/add-sell.php b/templates/add-sell.php new file mode 100644 index 0000000..655f646 --- /dev/null +++ b/templates/add-sell.php @@ -0,0 +1,23 @@ + + + + + +

Add new sell to the DB

+
+

Sum of sell

+ +
+

Contract number

+ +
+

Apartment number

+ +
+

Living complex name

+ +
+ +
+ + diff --git a/templates/welcome.php b/templates/welcome.php new file mode 100644 index 0000000..3406d44 --- /dev/null +++ b/templates/welcome.php @@ -0,0 +1,11 @@ + + + + +

Welcome!

+ + + \ No newline at end of file From 83afb01d055d13692aebc33884e5758187609f76 Mon Sep 17 00:00:00 2001 From: max Date: Sun, 21 Nov 2021 13:11:10 -0800 Subject: [PATCH 06/18] add form for new sells registration --- controllers/TableController.php | 68 +++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 controllers/TableController.php diff --git a/controllers/TableController.php b/controllers/TableController.php new file mode 100644 index 0000000..a0ef684 --- /dev/null +++ b/controllers/TableController.php @@ -0,0 +1,68 @@ +articleRepository = $articleRepository; + $this->sellsRepository = $sellsRepository; + $this->agentsRepository = $agentsRepository; + } + + + //Will return table of | agent_name | num_of_sellf | sum | + public function showTableAction(Request $request) + { +// $articles = $this->articleRepository->getAll(); +// return new Response( +// $this->render('articles', [ +// 'articles' => $articles +// ]) +// ); +// $sells = $this->sellsRepository->getAll(); + return new Response($this->render('comission-total', ['sellsRepository' => $this->sellsRepository, 'agentsRepository' => $this->agentsRepository])); + } + + public function addSellAction(Request $request){ + if($request->isPost()){ + echo 'ABOBUS'; + echo $request->getPostParameter('sum'); + echo $request->getPostParameter('contract_number'); + echo $request->getPostParameter('apartment_number'); + echo $request->getPostParameter('living_complex'); + return new Response($this->render('comission-total', ['sellsRepository' => $this->sellsRepository, 'agentsRepository' => $this->agentsRepository])); + } + return new Response($this->render('add-sell')); + } + + protected function render($templateName, $vars = []) + { + ob_start(); + extract($vars); + include sprintf('templates/%s.php', $templateName); + $content = ob_get_contents(); + ob_end_clean(); + return $content; + } + + public function __call($name, $arguments) + { + return new Response('Sorry but this action not found', + '404', 'Not found'); + } + + +} \ No newline at end of file From 90b45c977a4adaa7ce41bb001fe7dffca526de6e Mon Sep 17 00:00:00 2001 From: max Date: Mon, 22 Nov 2021 02:39:01 -0800 Subject: [PATCH 07/18] Add sells adding --- config/database.php | 4 ++-- controllers/TableController.php | 15 ++++++++++----- repositories/AgentsRepository.php | 15 +++++++++++++++ repositories/SellsRepository.php | 21 +++++++++++++++++++++ templates/add-sell.php | 3 +++ templates/welcome.php | 2 +- 6 files changed, 52 insertions(+), 8 deletions(-) diff --git a/config/database.php b/config/database.php index 92aa9a0..9284f01 100644 --- a/config/database.php +++ b/config/database.php @@ -3,7 +3,7 @@ $database = [ 'database_host' => 'localhost', 'database_name' => 'framework', - 'username' => 'root', - 'password' => null, + 'username' => 'student', + 'password' => 'password', 'charset' => 'utf8' ]; diff --git a/controllers/TableController.php b/controllers/TableController.php index a0ef684..a0b9361 100644 --- a/controllers/TableController.php +++ b/controllers/TableController.php @@ -38,11 +38,16 @@ public function showTableAction(Request $request) public function addSellAction(Request $request){ if($request->isPost()){ - echo 'ABOBUS'; - echo $request->getPostParameter('sum'); - echo $request->getPostParameter('contract_number'); - echo $request->getPostParameter('apartment_number'); - echo $request->getPostParameter('living_complex'); +// echo 'ABOBUS'; + + $name = $request->getPostParameter('name'); + $agent_id = $this->agentsRepository->getIdByAgentsName($name); + $sum = (int)$request->getPostParameter('sum'); + $contract_number = $request->getPostParameter('contract_number'); + $apartment_number = $request->getPostParameter('apartment_number'); + $living_complex = $request->getPostParameter('living_complex'); + + $this->sellsRepository->addNewSell($agent_id[0], $sum, $contract_number, $apartment_number, $living_complex, $name); return new Response($this->render('comission-total', ['sellsRepository' => $this->sellsRepository, 'agentsRepository' => $this->agentsRepository])); } return new Response($this->render('add-sell')); diff --git a/repositories/AgentsRepository.php b/repositories/AgentsRepository.php index 83b9d92..a1906a7 100644 --- a/repositories/AgentsRepository.php +++ b/repositories/AgentsRepository.php @@ -27,4 +27,19 @@ public function getAllAgentsIds(){ $statement->execute(); return $statement->fetchAll(); } + + public function getIdByAgentsName($name){ + $statement = $this->connection->query("SELECT id FROM agents WHERE full_name = '" . $name . "' LIMIT 1;"); + if(!$statement) { + return [-1, -1]; + } + $statement->execute(); + return $statement->fetch(); + } + + public function addNewAgent($name){ + $statement = $this->connection->query('INSERT INTO agents (full_name) VALUE (\'' . $name . '\');'); + echo 'added new mfr'; +// $statement->execute(); + } } \ No newline at end of file diff --git a/repositories/SellsRepository.php b/repositories/SellsRepository.php index 6074a7e..118bfb4 100644 --- a/repositories/SellsRepository.php +++ b/repositories/SellsRepository.php @@ -3,10 +3,12 @@ class SellsRepository { protected $connection = null; + protected $agentsRepository; public function __construct(PDO $connection) { $this->connection = $connection; + $this->agentsRepository = new AgentsRepository($connection); } public function getAll() @@ -20,4 +22,23 @@ public function getAllByAgentId($id) $statement = $this->connection->query("SELECT * FROM apartments_sells WHERE agent_id = " . $id); return $statement->fetchAll(); } + + public function addNewSell($id, $sum, $contract_number, $apartment_number, $living_complex, $name) + { + $query = 'INSERT INTO apartments_sells (agent_id, sum, contract_number, apartment_number, living_complex) +VALUES (%d, %d, %d, %d, \'%s\');'; + $formated_query = sprintf($query, $id, $sum, $contract_number, $apartment_number, $living_complex); + $statement = $this->connection->query($formated_query); + if(!$statement) { + $this->agentsRepository->addNewAgent($name); + $id2 = $this->agentsRepository->getIdByAgentsName($name)[0]; + $formated_query2 = sprintf($query, $id2, $sum, $contract_number, $apartment_number, $living_complex); + $statement2 = $this->connection->query($formated_query2); + $statement2->execute(); + echo "ST1"; + }else{ + $statement->execute(); + echo "ST2"; + } + } } \ No newline at end of file diff --git a/templates/add-sell.php b/templates/add-sell.php index 655f646..38c112e 100644 --- a/templates/add-sell.php +++ b/templates/add-sell.php @@ -5,6 +5,9 @@

Add new sell to the DB

+

Agent's fullname

+ +

Sum of sell


diff --git a/templates/welcome.php b/templates/welcome.php index 3406d44..36ff06a 100644 --- a/templates/welcome.php +++ b/templates/welcome.php @@ -5,7 +5,7 @@

Welcome!

\ No newline at end of file From c0129f6a75e30e6402ff8282a2642c1b3e5ae292 Mon Sep 17 00:00:00 2001 From: max Date: Tue, 23 Nov 2021 07:54:19 -0800 Subject: [PATCH 08/18] Add contracts adding --- config/routes.php | 8 +++ controllers/ContractsController.php | 56 +++++++++++++++++++++ controllers/TableController.php | 4 +- entities/Contract.php | 12 +++++ entities/Sell.php | 11 +++++ index.php | 6 ++- repositories/ContractsRepository.php | 31 ++++++++++++ sql/article.sql | 74 +++++++++++++++++----------- templates/add-contract.php | 35 +++++++++++++ templates/contracts.php | 30 +++++++++++ templates/welcome.php | 2 + 11 files changed, 238 insertions(+), 31 deletions(-) create mode 100644 controllers/ContractsController.php create mode 100644 entities/Contract.php create mode 100644 entities/Sell.php create mode 100644 repositories/ContractsRepository.php create mode 100644 templates/add-contract.php create mode 100644 templates/contracts.php diff --git a/config/routes.php b/config/routes.php index 425362c..f8c4a56 100644 --- a/config/routes.php +++ b/config/routes.php @@ -5,6 +5,14 @@ 'controller' => 'index', 'action' => 'index' ], + '/contracts' => [ + 'controller' => 'contracts', + 'action' => 'showContracts' + ], + '/addcontract' => [ + 'controller' => 'contracts', + 'action' => 'showContractsForm' + ], '/addsell' => [ 'controller' => 'table', 'action' => 'addSell' diff --git a/controllers/ContractsController.php b/controllers/ContractsController.php new file mode 100644 index 0000000..775c719 --- /dev/null +++ b/controllers/ContractsController.php @@ -0,0 +1,56 @@ +contractsRepository = $contractsRepository; + } + + + public function showContractsAction(Request $request) + { + return new Response($this->render('contracts', ['contractsRepository' => $this->contractsRepository])); + } + + public function showContractsFormAction(Request $request) + { + if ($request->isPost()) { + $contract = new Contract(); + + $contract->number = $request->getPostParameter('number'); + $contract->agent_name = $request->getPostParameter('agent_name'); + $contract->living_complex = $request->getPostParameter('living_complex'); + $contract->award_type = $request->getPostParameter('award_type'); + $contract->award_size = $request->getPostParameter('award_size'); + $contract->sign_date = $request->getPostParameter('sign_date'); + $contract->expiration_date = $request->getPostParameter('expiration_date'); + + + $this->contractsRepository->addContract($contract); + return new Response($this->render('contracts', ['contractsRepository' => $this->contractsRepository])); + } + return new Response($this->render('add-contract')); + } + + protected function render($templateName, $vars = []) + { + ob_start(); + extract($vars); + include sprintf('templates/%s.php', $templateName); + $content = ob_get_contents(); + ob_end_clean(); + return $content; + } + + public function __call($name, $arguments) + { + return new Response('Sorry but this action not found', + '404', 'Not found'); + } + +} \ No newline at end of file diff --git a/controllers/TableController.php b/controllers/TableController.php index a0b9361..3ea51b6 100644 --- a/controllers/TableController.php +++ b/controllers/TableController.php @@ -1,4 +1,5 @@ isPost()){ -// echo 'ABOBUS'; + + $sell = new Sell(); $name = $request->getPostParameter('name'); $agent_id = $this->agentsRepository->getIdByAgentsName($name); diff --git a/entities/Contract.php b/entities/Contract.php new file mode 100644 index 0000000..555c31f --- /dev/null +++ b/entities/Contract.php @@ -0,0 +1,12 @@ +match($request->getPath()); @@ -39,7 +42,8 @@ $controllers = [ 'table' => new TableController($articleRepository, $sellsRepository, $agentsRepository), 'helloWorld' => new HelloWorldController(), - 'index' => new IndexController() + 'index' => new IndexController(), + 'contracts' => new ContractsController($contractsRepository) ]; $controller = $controllers[$route['controller']]; diff --git a/repositories/ContractsRepository.php b/repositories/ContractsRepository.php new file mode 100644 index 0000000..a8d44ed --- /dev/null +++ b/repositories/ContractsRepository.php @@ -0,0 +1,31 @@ +connection = $connection; + } + + public function getAll() + { + $statement = $this->connection->query("SELECT * FROM contracts"); + return $statement->fetchAll(); + } + + public function addContract(Contract $contract) + { + $query = "insert into contracts (number, agent_name, living_complex, award_type, award_size, expiration_date, sign_date) +values (%d, '%s', '%s', '%s', %d, '%s', '%s');"; + $query = sprintf($query, $contract->number, + $contract->agent_name, + $contract->living_complex, + $contract->award_type, + $contract->award_size, + $contract->expiration_date, + $contract->sign_date); + $statement = $this->connection->query($query); + } +} \ No newline at end of file diff --git a/sql/article.sql b/sql/article.sql index 24fa273..f001ba7 100644 --- a/sql/article.sql +++ b/sql/article.sql @@ -7,10 +7,11 @@ # INSERT INTO articles (name, body) VALUES ('Hello', 'Hello world long long page...'); # INSERT INTO articles (name, body) VALUES ('World', 'World hello extra long long page...'); -CREATE TABLE IF NOT EXISTS agents ( - id INT (11) NOT NULL AUTO_INCREMENT PRIMARY KEY, - full_name varchar (255) not null -) ENGINE=InnoDB; +CREATE TABLE IF NOT EXISTS agents +( + id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, + full_name varchar(255) not null +) ENGINE = InnoDB; # INSERT INTO agents (full_name) VALUE ('Peter Ivanovich Kuznetsov'); #1 # INSERT INTO agents (full_name) VALUE ('Andei Nikolaevich Trofimov'); #2 @@ -18,30 +19,45 @@ CREATE TABLE IF NOT EXISTS agents ( # drop table apartments_sells; -CREATE TABLE IF NOT EXISTS apartments_sells ( - agent_id INT(11) NOT NULL, +CREATE TABLE IF NOT EXISTS apartments_sells +( + agent_id INT(11) NOT NULL, - sum INT(11) NOT NULL, - contract_number varchar (255) not null primary key , - apartment_number INT(11) NOT NULL, - living_complex varchar (255) not null, - FOREIGN KEY (agent_id) REFERENCES agents(id) -) ENGINE=InnoDB; + sum INT(11) NOT NULL, + contract_number varchar(255) not null primary key, + apartment_number INT(11) NOT NULL, + living_complex varchar(255) not null, + FOREIGN KEY (agent_id) REFERENCES agents (id) +) ENGINE = InnoDB; -INSERT INTO apartments_sells (agent_id, sum, contract_number, apartment_number, living_complex) -VALUES (1, 125000, 101, 228, 'Bebra-town'); - -INSERT INTO apartments_sells (agent_id, sum, contract_number, apartment_number, living_complex) -VALUES (1, 14800, 102, 789, 'Golden-wind'); - -INSERT INTO apartments_sells (agent_id, sum, contract_number, apartment_number, living_complex) -VALUES (1, 59000, 103, 139, 'Jmihograd'); - -INSERT INTO apartments_sells (agent_id, sum, contract_number, apartment_number, living_complex) -VALUES (2, 75890, 201, 78, 'Bebra-town'); - -INSERT INTO apartments_sells (agent_id, sum, contract_number, apartment_number, living_complex) -VALUES (2, 65483, 202, 98, 'Jmihograd'); - -INSERT INTO apartments_sells (agent_id, sum, contract_number, apartment_number, living_complex) -VALUES (3, 69492, 301, 54, 'Golden-wind'); \ No newline at end of file +# INSERT INTO apartments_sells (agent_id, sum, contract_number, apartment_number, living_complex) +# VALUES (1, 125000, 101, 228, 'Bebra-town'); +# +# INSERT INTO apartments_sells (agent_id, sum, contract_number, apartment_number, living_complex) +# VALUES (1, 14800, 102, 789, 'Golden-wind'); +# +# INSERT INTO apartments_sells (agent_id, sum, contract_number, apartment_number, living_complex) +# VALUES (1, 59000, 103, 139, 'Jmihograd'); +# +# INSERT INTO apartments_sells (agent_id, sum, contract_number, apartment_number, living_complex) +# VALUES (2, 75890, 201, 78, 'Bebra-town'); +# +# INSERT INTO apartments_sells (agent_id, sum, contract_number, apartment_number, living_complex) +# VALUES (2, 65483, 202, 98, 'Jmihograd'); +# +# INSERT INTO apartments_sells (agent_id, sum, contract_number, apartment_number, living_complex) +# VALUES (3, 69492, 301, 54, 'Golden-wind'); + +CREATE TABLE IF NOT EXISTS contracts +( + number varchar(255) primary key, + agent_name varchar(255), + living_complex varchar(255), + award_type varchar(255) check (award_type = 'fix' or award_type = 'percent'), + award_size DECIMAL(11), + expiration_date datetime, + sign_date datetime +) ENGINE = InnoDB; + +insert into contracts (number, agent_name, living_complex, award_type, award_size, expiration_date, sign_date) +values (9827439, 'Grigoriy Mironovich Karpov', 'Three-leaf Clover', 'fix', 120000, '2021-11-23', '2022-11-25'); diff --git a/templates/add-contract.php b/templates/add-contract.php new file mode 100644 index 0000000..672f077 --- /dev/null +++ b/templates/add-contract.php @@ -0,0 +1,35 @@ + + + + + +

Add new contract to the DB

+ +

Contract number

+ +
+

Agent name

+ +
+

Living complex

+ +
+

Award type

+ +
+

Award size

+ +
+

Sign date

+ +
+

Expiration date

+ +
+ + + + diff --git a/templates/contracts.php b/templates/contracts.php new file mode 100644 index 0000000..6bc851a --- /dev/null +++ b/templates/contracts.php @@ -0,0 +1,30 @@ + + + + + + +

All contracts

+ + + + + + + + + +getAll() as $contract): ?> + + + + + + + + + + +
Contract numberAgent nameLiving complexAward typeAward sizeSign dateExpiration date
+ + \ No newline at end of file diff --git a/templates/welcome.php b/templates/welcome.php index 36ff06a..8e3b753 100644 --- a/templates/welcome.php +++ b/templates/welcome.php @@ -6,6 +6,8 @@ \ No newline at end of file From c1b6095100f75b9727163d16d8fb6a5fb1c963e1 Mon Sep 17 00:00:00 2001 From: max Date: Tue, 23 Nov 2021 09:06:12 -0800 Subject: [PATCH 09/18] Refactor sells adding page --- controllers/TableController.php | 14 +++++++------- repositories/SellsRepository.php | 33 ++++++++++++++++++-------------- 2 files changed, 26 insertions(+), 21 deletions(-) diff --git a/controllers/TableController.php b/controllers/TableController.php index 3ea51b6..a52e521 100644 --- a/controllers/TableController.php +++ b/controllers/TableController.php @@ -42,14 +42,14 @@ public function addSellAction(Request $request){ $sell = new Sell(); - $name = $request->getPostParameter('name'); - $agent_id = $this->agentsRepository->getIdByAgentsName($name); - $sum = (int)$request->getPostParameter('sum'); - $contract_number = $request->getPostParameter('contract_number'); - $apartment_number = $request->getPostParameter('apartment_number'); - $living_complex = $request->getPostParameter('living_complex'); + $sell->name = $request->getPostParameter('name'); + $sell->agent_id = $this->agentsRepository->getIdByAgentsName($sell->name)[0]; + $sell->sum = (int)$request->getPostParameter('sum'); + $sell->contract_number = $request->getPostParameter('contract_number'); + $sell->apartment_number = $request->getPostParameter('apartment_number'); + $sell->living_complex = $request->getPostParameter('living_complex'); - $this->sellsRepository->addNewSell($agent_id[0], $sum, $contract_number, $apartment_number, $living_complex, $name); + $this->sellsRepository->addNewSell($sell); return new Response($this->render('comission-total', ['sellsRepository' => $this->sellsRepository, 'agentsRepository' => $this->agentsRepository])); } return new Response($this->render('add-sell')); diff --git a/repositories/SellsRepository.php b/repositories/SellsRepository.php index 118bfb4..8ca89d4 100644 --- a/repositories/SellsRepository.php +++ b/repositories/SellsRepository.php @@ -23,22 +23,27 @@ public function getAllByAgentId($id) return $statement->fetchAll(); } - public function addNewSell($id, $sum, $contract_number, $apartment_number, $living_complex, $name) + public function addNewSell(Sell $sell) { - $query = 'INSERT INTO apartments_sells (agent_id, sum, contract_number, apartment_number, living_complex) + $template_query = 'INSERT INTO apartments_sells (agent_id, sum, contract_number, apartment_number, living_complex) VALUES (%d, %d, %d, %d, \'%s\');'; - $formated_query = sprintf($query, $id, $sum, $contract_number, $apartment_number, $living_complex); - $statement = $this->connection->query($formated_query); - if(!$statement) { - $this->agentsRepository->addNewAgent($name); - $id2 = $this->agentsRepository->getIdByAgentsName($name)[0]; - $formated_query2 = sprintf($query, $id2, $sum, $contract_number, $apartment_number, $living_complex); - $statement2 = $this->connection->query($formated_query2); - $statement2->execute(); - echo "ST1"; - }else{ - $statement->execute(); - echo "ST2"; + + $query = sprintf($template_query, + $sell->agent_id, + $sell->sum, + $sell->contract_number, + $sell->apartment_number, + $sell->living_complex); + + $statement = $this->connection->query($query); + + if (!$statement) { + + $this->agentsRepository->addNewAgent($sell->name); + $sell->agent_id = (int)$this->agentsRepository->getIdByAgentsName($sell->name)[0]; + var_dump($sell->agent_id); + $query = sprintf($template_query, $sell->agent_id, $sell->sum, $sell->contract_number, $sell->apartment_number, $sell->living_complex); + $statement = $this->connection->query($query); } } } \ No newline at end of file From 210a714d292efa8468a521cb6e7992c8f560e1ca Mon Sep 17 00:00:00 2001 From: max Date: Tue, 23 Nov 2021 13:44:56 -0800 Subject: [PATCH 10/18] Add contracts system + fixes --- controllers/TableController.php | 50 ++++++++++++++++++---------- index.php | 4 +-- repositories/AgentsRepository.php | 2 +- repositories/ContractsRepository.php | 6 ++++ repositories/SellsRepository.php | 14 +++++--- sql/article.sql | 9 ++--- templates/add-sell.php | 21 ++++++------ 7 files changed, 65 insertions(+), 41 deletions(-) diff --git a/controllers/TableController.php b/controllers/TableController.php index a52e521..bb13e53 100644 --- a/controllers/TableController.php +++ b/controllers/TableController.php @@ -12,13 +12,13 @@ class TableController /** * @var ArticleRepository */ - protected $articleRepository; + protected $contractsRepository; protected $sellsRepository; protected $agentsRepository; - public function __construct(ArticleRepository $articleRepository, SellsRepository $sellsRepository, AgentsRepository $agentsRepository) + public function __construct(ContractsRepository $contractsRepository, SellsRepository $sellsRepository, AgentsRepository $agentsRepository) { - $this->articleRepository = $articleRepository; + $this->contractsRepository = $contractsRepository; $this->sellsRepository = $sellsRepository; $this->agentsRepository = $agentsRepository; } @@ -27,32 +27,46 @@ public function __construct(ArticleRepository $articleRepository, SellsRepositor //Will return table of | agent_name | num_of_sellf | sum | public function showTableAction(Request $request) { -// $articles = $this->articleRepository->getAll(); -// return new Response( -// $this->render('articles', [ -// 'articles' => $articles -// ]) -// ); -// $sells = $this->sellsRepository->getAll(); return new Response($this->render('comission-total', ['sellsRepository' => $this->sellsRepository, 'agentsRepository' => $this->agentsRepository])); } - public function addSellAction(Request $request){ - if($request->isPost()){ + public function addSellAction(Request $request) + { + if ($request->isPost()) { $sell = new Sell(); - $sell->name = $request->getPostParameter('name'); + $contract_info = explode(', ', $request->getPostParameter('contract_info')); +// var_dump($contract_info); + $sell->name = $contract_info[1]; $sell->agent_id = $this->agentsRepository->getIdByAgentsName($sell->name)[0]; - $sell->sum = (int)$request->getPostParameter('sum'); - $sell->contract_number = $request->getPostParameter('contract_number'); - $sell->apartment_number = $request->getPostParameter('apartment_number'); - $sell->living_complex = $request->getPostParameter('living_complex'); + $sell->contract_number = (int)$contract_info[0]; + + $apartment_price = (int)$request->getPostParameter('sum'); + $sell->sum = $this->getAwardFromApartmentPrice($sell->contract_number, $apartment_price); + + $sell->apartment_number = $request->getPostParameter('apartment_number'); + $sell->living_complex = $contract_info[2]; +// var_dump($sell); $this->sellsRepository->addNewSell($sell); return new Response($this->render('comission-total', ['sellsRepository' => $this->sellsRepository, 'agentsRepository' => $this->agentsRepository])); } - return new Response($this->render('add-sell')); + return new Response($this->render('add-sell', ['contractsRepository' => $this->contractsRepository])); + } + + protected function getAwardFromApartmentPrice($contract_number, $apartment_price) + { + $contract = $this->contractsRepository->getContractByNumber($contract_number)[0]; +// var_dump($contract_number); +// var_dump($contract); + $award_type = $contract['award_type']; + var_dump($award_type); + if($award_type === 'fix'){ + return (int)$contract['award_size']; + }else{ + return (((int)$contract['award_size'])/100) * ((int)$apartment_price); + } } protected function render($templateName, $vars = []) diff --git a/index.php b/index.php index 09ff65e..72986d8 100644 --- a/index.php +++ b/index.php @@ -25,7 +25,7 @@ /** @var PDO $connection */ $connection = new PDO( $dsn, $database['username'], $database['password']); -$articleRepository = new ArticleRepository($connection); +//$articleRepository = new ArticleRepository($connection); $sellsRepository = new SellsRepository($connection); $agentsRepository = new AgentsRepository($connection); $contractsRepository = new ContractsRepository($connection); @@ -40,7 +40,7 @@ } $controllers = [ - 'table' => new TableController($articleRepository, $sellsRepository, $agentsRepository), + 'table' => new TableController($contractsRepository, $sellsRepository, $agentsRepository), 'helloWorld' => new HelloWorldController(), 'index' => new IndexController(), 'contracts' => new ContractsController($contractsRepository) diff --git a/repositories/AgentsRepository.php b/repositories/AgentsRepository.php index a1906a7..b8171d6 100644 --- a/repositories/AgentsRepository.php +++ b/repositories/AgentsRepository.php @@ -39,7 +39,7 @@ public function getIdByAgentsName($name){ public function addNewAgent($name){ $statement = $this->connection->query('INSERT INTO agents (full_name) VALUE (\'' . $name . '\');'); - echo 'added new mfr'; +// echo 'added new mfr'; // $statement->execute(); } } \ No newline at end of file diff --git a/repositories/ContractsRepository.php b/repositories/ContractsRepository.php index a8d44ed..9975e73 100644 --- a/repositories/ContractsRepository.php +++ b/repositories/ContractsRepository.php @@ -15,6 +15,12 @@ public function getAll() return $statement->fetchAll(); } + public function getContractByNumber($number) + { + $statement = $this->connection->query("SELECT * FROM contracts WHERE number = " . $number . ";"); + return $statement->fetchAll(); + } + public function addContract(Contract $contract) { $query = "insert into contracts (number, agent_name, living_complex, award_type, award_size, expiration_date, sign_date) diff --git a/repositories/SellsRepository.php b/repositories/SellsRepository.php index 8ca89d4..fe41731 100644 --- a/repositories/SellsRepository.php +++ b/repositories/SellsRepository.php @@ -34,16 +34,20 @@ public function addNewSell(Sell $sell) $sell->contract_number, $sell->apartment_number, $sell->living_complex); - $statement = $this->connection->query($query); - if (!$statement) { + if ($sell->agent_id == 0) { $this->agentsRepository->addNewAgent($sell->name); $sell->agent_id = (int)$this->agentsRepository->getIdByAgentsName($sell->name)[0]; - var_dump($sell->agent_id); - $query = sprintf($template_query, $sell->agent_id, $sell->sum, $sell->contract_number, $sell->apartment_number, $sell->living_complex); + + $query = sprintf($template_query, + $sell->agent_id, + $sell->sum, + $sell->contract_number, + $sell->apartment_number, + $sell->living_complex); $statement = $this->connection->query($query); } } -} \ No newline at end of file +} diff --git a/sql/article.sql b/sql/article.sql index f001ba7..9f9c7df 100644 --- a/sql/article.sql +++ b/sql/article.sql @@ -24,10 +24,11 @@ CREATE TABLE IF NOT EXISTS apartments_sells agent_id INT(11) NOT NULL, sum INT(11) NOT NULL, - contract_number varchar(255) not null primary key, + contract_number varchar(255) not null, apartment_number INT(11) NOT NULL, living_complex varchar(255) not null, - FOREIGN KEY (agent_id) REFERENCES agents (id) + FOREIGN KEY (agent_id) REFERENCES agents (id), + PRIMARY KEY (apartment_number, living_complex) ) ENGINE = InnoDB; # INSERT INTO apartments_sells (agent_id, sum, contract_number, apartment_number, living_complex) @@ -59,5 +60,5 @@ CREATE TABLE IF NOT EXISTS contracts sign_date datetime ) ENGINE = InnoDB; -insert into contracts (number, agent_name, living_complex, award_type, award_size, expiration_date, sign_date) -values (9827439, 'Grigoriy Mironovich Karpov', 'Three-leaf Clover', 'fix', 120000, '2021-11-23', '2022-11-25'); +# insert into contracts (number, agent_name, living_complex, award_type, award_size, expiration_date, sign_date) +# values (9827439, 'Grigoriy Mironovich Karpov', 'Three-leaf Clover', 'fix', 120000, '2021-11-23', '2022-11-25'); diff --git a/templates/add-sell.php b/templates/add-sell.php index 38c112e..f8f20a5 100644 --- a/templates/add-sell.php +++ b/templates/add-sell.php @@ -5,20 +5,19 @@

Add new sell to the DB

-

Agent's fullname

- +

Price of the appartment

+
-

Sum of sell

- -
-

Contract number

- + +

Contract number, Contractor, Living complex name

+

Apartment number

- -
-

Living complex name

- +
From 99d45ec1d4723715b5ff0e145caa7079d7717fc9 Mon Sep 17 00:00:00 2001 From: max Date: Tue, 23 Nov 2021 22:43:44 -0800 Subject: [PATCH 11/18] All UC's completed --- controllers/ContractsController.php | 64 +++++++++++++++++++++++++++- controllers/TableController.php | 22 +++++++++- index.php | 2 +- repositories/ContractsRepository.php | 5 +++ repositories/SellsRepository.php | 8 ++++ templates/add-contract.php | 19 ++++++--- templates/add-sell.php | 20 +++++++-- 7 files changed, 127 insertions(+), 13 deletions(-) diff --git a/controllers/ContractsController.php b/controllers/ContractsController.php index 775c719..352173a 100644 --- a/controllers/ContractsController.php +++ b/controllers/ContractsController.php @@ -5,10 +5,12 @@ class ContractsController { protected $contractsRepository; + protected $agentsRepository; - public function __construct(ContractsRepository $contractsRepository) + public function __construct(ContractsRepository $contractsRepository, AgentsRepository $agentsRepository) { $this->contractsRepository = $contractsRepository; + $this->agentsRepository = $agentsRepository; } @@ -30,6 +32,9 @@ public function showContractsFormAction(Request $request) $contract->sign_date = $request->getPostParameter('sign_date'); $contract->expiration_date = $request->getPostParameter('expiration_date'); + $errors = $this->getErrors($contract); + if ($errors !== null) + return new Response($this->render('add-contract', ['errors' => $errors])); $this->contractsRepository->addContract($contract); return new Response($this->render('contracts', ['contractsRepository' => $this->contractsRepository])); @@ -37,6 +42,63 @@ public function showContractsFormAction(Request $request) return new Response($this->render('add-contract')); } + protected function getErrors(Contract $contract) + { + //Contract with such number is already exists + //Agent already has contract for this complex + //fix 0-1000000 + //percent 0-10 + //expiration date > current date + + $result = []; + + $contracts = $this->contractsRepository->getContractByNumber($contract->number); + $contractsOfAgentForThisComplex = $this->contractsRepository->getContractByAgentNameAndComplexName($contract->agent_name, $contract->living_complex); + + $condition1 = (count($contracts) === 0); + + $condition2 = (count($contractsOfAgentForThisComplex) === 0); + + $condition3 = true; + if ($contract->award_type == 'fix') { + $as = (int)$contract->award_size; + if ($as > 1000000 or $as <= 0) + $condition3 = false; + } + + $condition4 = true; + if ($contract->award_type == 'percent') { + $as = (int)$contract->award_size; + if ($as > 10 or $as <= 0) + $condition4 = false; + } + + $condition5 = true; + $d1 = new DateTime($contract->expiration_date); + $d2 = new DateTime("now"); + if ($d1 <= $d2) + $condition5 = false; + + if (!$condition1) + array_push($result, 'Contract with number #' . $contract->number . ' is already exist'); + + if (!$condition2) + array_push($result, 'Agent already has contract for the complex ' . $contract->living_complex); + + if (!$condition3) + array_push($result, 'Award size of fixed award must be between 0 and 1\'000\'000'); + + if (!$condition4) + array_push($result, 'Award size of percent award must be between 0 and 10'); + + if (!$condition5) + array_push($result, 'Expiration date must be in future'); + + if (count($result) == 0) + return null; + return $result; + } + protected function render($templateName, $vars = []) { ob_start(); diff --git a/controllers/TableController.php b/controllers/TableController.php index bb13e53..15fe0e4 100644 --- a/controllers/TableController.php +++ b/controllers/TableController.php @@ -34,6 +34,7 @@ public function addSellAction(Request $request) { if ($request->isPost()) { + $sell = new Sell(); $contract_info = explode(', ', $request->getPostParameter('contract_info')); @@ -49,19 +50,38 @@ public function addSellAction(Request $request) $sell->apartment_number = $request->getPostParameter('apartment_number'); $sell->living_complex = $contract_info[2]; // var_dump($sell); + + $errors = $this->getErrors($sell); + if($errors !== null) + return new Response($this->render('add-sell', ['contractsRepository' => $this->contractsRepository, 'errors' => $errors])); + + $this->sellsRepository->addNewSell($sell); return new Response($this->render('comission-total', ['sellsRepository' => $this->sellsRepository, 'agentsRepository' => $this->agentsRepository])); } return new Response($this->render('add-sell', ['contractsRepository' => $this->contractsRepository])); } + //Esli cywectwyet contract na prodaju dannoi kvartiri - owibka, inache ok + protected function getErrors(Sell $sell){ + $result = $this->sellsRepository->getAllByLivingComplexAndApartmentNumber($sell->living_complex, $sell->apartment_number); +// var_dump($result); + //array(0) { } + //array(1) + if (count($result) == 0){ + return null; + }else{ + return ['Contract on apartment #' . $sell->apartment_number . ' is already exists. Please, enter different apartment number']; + } + } + protected function getAwardFromApartmentPrice($contract_number, $apartment_price) { $contract = $this->contractsRepository->getContractByNumber($contract_number)[0]; // var_dump($contract_number); // var_dump($contract); $award_type = $contract['award_type']; - var_dump($award_type); +// var_dump($award_type); if($award_type === 'fix'){ return (int)$contract['award_size']; }else{ diff --git a/index.php b/index.php index 72986d8..07b69f8 100644 --- a/index.php +++ b/index.php @@ -43,7 +43,7 @@ 'table' => new TableController($contractsRepository, $sellsRepository, $agentsRepository), 'helloWorld' => new HelloWorldController(), 'index' => new IndexController(), - 'contracts' => new ContractsController($contractsRepository) + 'contracts' => new ContractsController($contractsRepository, $agentsRepository) ]; $controller = $controllers[$route['controller']]; diff --git a/repositories/ContractsRepository.php b/repositories/ContractsRepository.php index 9975e73..b789573 100644 --- a/repositories/ContractsRepository.php +++ b/repositories/ContractsRepository.php @@ -21,6 +21,11 @@ public function getContractByNumber($number) return $statement->fetchAll(); } + public function getContractByAgentNameAndComplexName($agent_name, $complex_name){ + $statement = $this->connection->query("SELECT * FROM contracts WHERE agent_name = '" . $agent_name . "' AND living_complex = '" . $complex_name . "' ;"); + return $statement->fetchAll(); + } + public function addContract(Contract $contract) { $query = "insert into contracts (number, agent_name, living_complex, award_type, award_size, expiration_date, sign_date) diff --git a/repositories/SellsRepository.php b/repositories/SellsRepository.php index fe41731..ed80819 100644 --- a/repositories/SellsRepository.php +++ b/repositories/SellsRepository.php @@ -17,6 +17,14 @@ public function getAll() return $statement->fetchAll(); } + public function getAllByLivingComplexAndApartmentNumber($living_complex, $apartment_number){ + $template_query = "SELECT * FROM apartments_sells WHERE living_complex = '%s' AND apartment_number = %d"; + $query = sprintf($template_query, $living_complex, $apartment_number); + $statement = $this->connection->query($query); + $statement->execute(); + return $statement->fetchAll(); + } + public function getAllByAgentId($id) { $statement = $this->connection->query("SELECT * FROM apartments_sells WHERE agent_id = " . $id); diff --git a/templates/add-contract.php b/templates/add-contract.php index 672f077..4b4886b 100644 --- a/templates/add-contract.php +++ b/templates/add-contract.php @@ -4,15 +4,22 @@

Add new contract to the DB

+ +
    + +
  • + +
+

Contract number

- +

Agent name

- +

Living complex

- +

Award type


Award size

- +

Sign date

- +

Expiration date

- +
diff --git a/templates/add-sell.php b/templates/add-sell.php index f8f20a5..f294939 100644 --- a/templates/add-sell.php +++ b/templates/add-sell.php @@ -4,22 +4,34 @@

Add new sell to the DB

+ +
    + +
  • + +
+

Price of the appartment

- +

Contract number, Contractor, Living complex name


Apartment number

- +
- + getAll()) == 0) { + echo ''; + echo '

Form is unavailable - contracts list is empty. Please, add contract to use this form

'; + } ?> + getAll()) != 0) + echo ''; ?>
From b9d754363318f7e5718fb53b5063c3254530a231 Mon Sep 17 00:00:00 2001 From: max Date: Tue, 23 Nov 2021 22:52:04 -0800 Subject: [PATCH 12/18] delete some legacy code --- config/routes.php | 10 +---- controllers/HelloWorldController.php | 55 ---------------------------- index.php | 3 -- repositories/ArticleRepository.php | 34 ----------------- templates/article.php | 11 ------ templates/articles.php | 22 ----------- 6 files changed, 1 insertion(+), 134 deletions(-) delete mode 100644 controllers/HelloWorldController.php delete mode 100644 repositories/ArticleRepository.php delete mode 100644 templates/article.php delete mode 100644 templates/articles.php diff --git a/config/routes.php b/config/routes.php index f8c4a56..5a0a1f6 100644 --- a/config/routes.php +++ b/config/routes.php @@ -24,13 +24,5 @@ '/show' => [ 'controller' => 'index', 'action' => 'show' - ], - '/hello' => [ - 'controller' => 'helloWorld', - 'action' => 'hello' - ], - '/world' => [ - 'controller' => 'helloWorld', - 'action' => 'world' - ], + ] ]; diff --git a/controllers/HelloWorldController.php b/controllers/HelloWorldController.php deleted file mode 100644 index 956d305..0000000 --- a/controllers/HelloWorldController.php +++ /dev/null @@ -1,55 +0,0 @@ -render('template', [ - 'title' => 'Hello page', - 'text' => 'hello' - ])); - - } - - - /** - * World action - * @return Response - * - */ - public function worldAction() { - return new Response($this->render('template', [ - 'title' => 'World page', - 'text' => 'world' - ])); - } - - - protected function render($templateName, $vars = []) - { - ob_start(); - extract($vars); - include sprintf('templates/%s.php', $templateName); - $content = ob_get_contents(); - ob_end_clean(); - return $content; - } - - public function __call($name, $arguments) - { - return new Response('Sorry but this action not found', - '404', 'Not found'); - } - - -} \ No newline at end of file diff --git a/index.php b/index.php index 07b69f8..183eb23 100644 --- a/index.php +++ b/index.php @@ -1,6 +1,5 @@ new TableController($contractsRepository, $sellsRepository, $agentsRepository), - 'helloWorld' => new HelloWorldController(), 'index' => new IndexController(), 'contracts' => new ContractsController($contractsRepository, $agentsRepository) ]; diff --git a/repositories/ArticleRepository.php b/repositories/ArticleRepository.php deleted file mode 100644 index cda39c3..0000000 --- a/repositories/ArticleRepository.php +++ /dev/null @@ -1,34 +0,0 @@ -connection = $connection; - } - - /** - * Return all Articles - */ - public function getAll() - { - $statement = $this->connection->query("SELECT * FROM articles"); - return $statement->fetchAll(); - } - - /** - * Return Article by ID - */ - public function getById($id) - { - $statement = $this->connection->prepare("SELECT * FROM articles WHERE id = :id LIMIT 1"); - - $statement->execute([ - "id" => $id - ]); - - return $statement->fetch(); - } -} \ No newline at end of file diff --git a/templates/article.php b/templates/article.php deleted file mode 100644 index 440001c..0000000 --- a/templates/article.php +++ /dev/null @@ -1,11 +0,0 @@ - - - - - -

-

- -back to articles list - - \ No newline at end of file diff --git a/templates/articles.php b/templates/articles.php deleted file mode 100644 index 19840d6..0000000 --- a/templates/articles.php +++ /dev/null @@ -1,22 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file From 8a02d4db92223eb2fc68bc9f5d51b9b8433af01d Mon Sep 17 00:00:00 2001 From: max Date: Tue, 23 Nov 2021 22:53:44 -0800 Subject: [PATCH 13/18] Changed SQL scripts --- sql/article.sql | 36 ------------------------------------ 1 file changed, 36 deletions(-) diff --git a/sql/article.sql b/sql/article.sql index 9f9c7df..a466c76 100644 --- a/sql/article.sql +++ b/sql/article.sql @@ -1,24 +1,9 @@ -# CREATE TABLE IF NOT EXISTS articles ( -# id INT (11) NOT NULL AUTO_INCREMENT PRIMARY KEY, -# name varchar (255) not null, -# body text -# ) ENGINE=InnoDB; -# -# INSERT INTO articles (name, body) VALUES ('Hello', 'Hello world long long page...'); -# INSERT INTO articles (name, body) VALUES ('World', 'World hello extra long long page...'); - CREATE TABLE IF NOT EXISTS agents ( id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, full_name varchar(255) not null ) ENGINE = InnoDB; -# INSERT INTO agents (full_name) VALUE ('Peter Ivanovich Kuznetsov'); #1 -# INSERT INTO agents (full_name) VALUE ('Andei Nikolaevich Trofimov'); #2 -# INSERT INTO agents (full_name) VALUE ('Anatolii Nikitevich Lazarev'); #3 - -# drop table apartments_sells; - CREATE TABLE IF NOT EXISTS apartments_sells ( agent_id INT(11) NOT NULL, @@ -31,24 +16,6 @@ CREATE TABLE IF NOT EXISTS apartments_sells PRIMARY KEY (apartment_number, living_complex) ) ENGINE = InnoDB; -# INSERT INTO apartments_sells (agent_id, sum, contract_number, apartment_number, living_complex) -# VALUES (1, 125000, 101, 228, 'Bebra-town'); -# -# INSERT INTO apartments_sells (agent_id, sum, contract_number, apartment_number, living_complex) -# VALUES (1, 14800, 102, 789, 'Golden-wind'); -# -# INSERT INTO apartments_sells (agent_id, sum, contract_number, apartment_number, living_complex) -# VALUES (1, 59000, 103, 139, 'Jmihograd'); -# -# INSERT INTO apartments_sells (agent_id, sum, contract_number, apartment_number, living_complex) -# VALUES (2, 75890, 201, 78, 'Bebra-town'); -# -# INSERT INTO apartments_sells (agent_id, sum, contract_number, apartment_number, living_complex) -# VALUES (2, 65483, 202, 98, 'Jmihograd'); -# -# INSERT INTO apartments_sells (agent_id, sum, contract_number, apartment_number, living_complex) -# VALUES (3, 69492, 301, 54, 'Golden-wind'); - CREATE TABLE IF NOT EXISTS contracts ( number varchar(255) primary key, @@ -59,6 +26,3 @@ CREATE TABLE IF NOT EXISTS contracts expiration_date datetime, sign_date datetime ) ENGINE = InnoDB; - -# insert into contracts (number, agent_name, living_complex, award_type, award_size, expiration_date, sign_date) -# values (9827439, 'Grigoriy Mironovich Karpov', 'Three-leaf Clover', 'fix', 120000, '2021-11-23', '2022-11-25'); From f042aeb575f2941fb8df455b29650a2f5d0166ae Mon Sep 17 00:00:00 2001 From: max Date: Fri, 10 Dec 2021 12:56:50 -0800 Subject: [PATCH 14/18] add CSS styling to the webpages --- templates/add-contract.php | 93 +++++++++++++++---------- templates/add-sell.php | 83 +++++++++++++++-------- templates/comission-total.php | 123 +++++++++++++++++++++++++++------- templates/contracts.php | 75 +++++++++++++++------ templates/welcome.php | 61 ++++++++++++++++- 5 files changed, 323 insertions(+), 112 deletions(-) diff --git a/templates/add-contract.php b/templates/add-contract.php index 4b4886b..6e95617 100644 --- a/templates/add-contract.php +++ b/templates/add-contract.php @@ -1,42 +1,67 @@ + -

Add new contract to the DB

- -
    - -
  • - -
+

Add new contract to the DB

+
+
    + +
  • + +
-
-

Contract number

- -
-

Agent name

- -
-

Living complex

- -
-

Award type

- -
-

Award size

- -
-

Sign date

- -
-

Expiration date

- -
- - +
+

Contract number

+ +
+

Agent name

+ +
+

Living complex

+ +
+

Award type

+ +
+

Award size

+ +
+

Sign date

+ +
+

Expiration date

+ +
+ + +
diff --git a/templates/add-sell.php b/templates/add-sell.php index f294939..ab30fed 100644 --- a/templates/add-sell.php +++ b/templates/add-sell.php @@ -1,37 +1,62 @@ + + + +

Add new sell to the DB

+
+
    + +
  • - -
    -

    Apartment number

    - -
    - getAll()) == 0) { - echo ''; - echo '

    Form is unavailable - contracts list is empty. Please, add contract to use this form

    '; - } ?> - getAll()) != 0) - echo ''; ?> - +
+ +
+

Price of the appartment

+ +
+ +

Contract number, Contractor, Living complex name

+ +
+

Apartment number

+ +
+ getAll()) == 0) { + echo ''; + echo '

Form is unavailable - contracts list is empty. Please, add contract to use this form

'; + } ?> + getAll()) != 0) + echo ''; ?> + +
diff --git a/templates/comission-total.php b/templates/comission-total.php index 9f21b4a..0c63710 100644 --- a/templates/comission-total.php +++ b/templates/comission-total.php @@ -1,37 +1,110 @@ + - + +

See total commission payments

getAllAgentsIds() as $id): ?> -

getAgentNameById($id[0]) ?>

- -
IDName
- - - - - - - - - getAllByAgentId($id[0]) as $sell): ?> +
+

getAgentNameById($id[0]) ?>

+ +
IDNameSumContract numberAppartment numberLiving complex name
- - - - - - - + + + + + + - - -
getAgentNameById($sell['agent_id']) ?>IDNameSumContract numberAppartment numberLiving complex name
-

Total:

+ getAllByAgentId($id[0]) as $sell): ?> + + + getAgentNameById($sell['agent_id']) ?> + + + + + + + + + +

Total:

+ -

Grand total:

+
+

Grand total:

diff --git a/templates/contracts.php b/templates/contracts.php index 6bc851a..497da25 100644 --- a/templates/contracts.php +++ b/templates/contracts.php @@ -1,30 +1,61 @@ + -

All contracts

- - - - - - - - - -getAll() as $contract): ?> - - - - - - - - - - -
Contract numberAgent nameLiving complexAward typeAward sizeSign dateExpiration date
+

All contracts

+
+ + + + + + + + + + getAll() as $contract): ?> + + + + + + + + + + +
Contract numberAgent nameLiving complexAward typeAward sizeSign dateExpiration date
+
\ No newline at end of file diff --git a/templates/welcome.php b/templates/welcome.php index 8e3b753..6e51aa1 100644 --- a/templates/welcome.php +++ b/templates/welcome.php @@ -1,13 +1,70 @@ + + -

Welcome!

-
    +

    Welcome!

    + + \ No newline at end of file From 181bcef9ea1c908e9d6015b63db8faf2a3937a75 Mon Sep 17 00:00:00 2001 From: max Date: Fri, 10 Dec 2021 13:37:33 -0800 Subject: [PATCH 15/18] refactoring of controllers --- controllers/BasicController.php | 20 +++++++++++++++++++ controllers/ContractsController.php | 26 ++---------------------- controllers/IndexController.php | 21 +++---------------- controllers/TableController.php | 31 ++--------------------------- 4 files changed, 27 insertions(+), 71 deletions(-) create mode 100644 controllers/BasicController.php diff --git a/controllers/BasicController.php b/controllers/BasicController.php new file mode 100644 index 0000000..1303668 --- /dev/null +++ b/controllers/BasicController.php @@ -0,0 +1,20 @@ + current date - $result = []; $contracts = $this->contractsRepository->getContractByNumber($contract->number); @@ -98,21 +93,4 @@ protected function getErrors(Contract $contract) return null; return $result; } - - protected function render($templateName, $vars = []) - { - ob_start(); - extract($vars); - include sprintf('templates/%s.php', $templateName); - $content = ob_get_contents(); - ob_end_clean(); - return $content; - } - - public function __call($name, $arguments) - { - return new Response('Sorry but this action not found', - '404', 'Not found'); - } - } \ No newline at end of file diff --git a/controllers/IndexController.php b/controllers/IndexController.php index bc388e5..ec8e166 100644 --- a/controllers/IndexController.php +++ b/controllers/IndexController.php @@ -1,6 +1,7 @@ render('404')); } } \ No newline at end of file diff --git a/controllers/TableController.php b/controllers/TableController.php index 15fe0e4..5464f8e 100644 --- a/controllers/TableController.php +++ b/controllers/TableController.php @@ -1,7 +1,8 @@ render('comission-total', ['sellsRepository' => $this->sellsRepository, 'agentsRepository' => $this->agentsRepository])); @@ -38,7 +38,6 @@ public function addSellAction(Request $request) $sell = new Sell(); $contract_info = explode(', ', $request->getPostParameter('contract_info')); -// var_dump($contract_info); $sell->name = $contract_info[1]; $sell->agent_id = $this->agentsRepository->getIdByAgentsName($sell->name)[0]; @@ -49,7 +48,6 @@ public function addSellAction(Request $request) $sell->apartment_number = $request->getPostParameter('apartment_number'); $sell->living_complex = $contract_info[2]; -// var_dump($sell); $errors = $this->getErrors($sell); if($errors !== null) @@ -62,12 +60,8 @@ public function addSellAction(Request $request) return new Response($this->render('add-sell', ['contractsRepository' => $this->contractsRepository])); } - //Esli cywectwyet contract na prodaju dannoi kvartiri - owibka, inache ok protected function getErrors(Sell $sell){ $result = $this->sellsRepository->getAllByLivingComplexAndApartmentNumber($sell->living_complex, $sell->apartment_number); -// var_dump($result); - //array(0) { } - //array(1) if (count($result) == 0){ return null; }else{ @@ -78,32 +72,11 @@ protected function getErrors(Sell $sell){ protected function getAwardFromApartmentPrice($contract_number, $apartment_price) { $contract = $this->contractsRepository->getContractByNumber($contract_number)[0]; -// var_dump($contract_number); -// var_dump($contract); $award_type = $contract['award_type']; -// var_dump($award_type); if($award_type === 'fix'){ return (int)$contract['award_size']; }else{ return (((int)$contract['award_size'])/100) * ((int)$apartment_price); } } - - protected function render($templateName, $vars = []) - { - ob_start(); - extract($vars); - include sprintf('templates/%s.php', $templateName); - $content = ob_get_contents(); - ob_end_clean(); - return $content; - } - - public function __call($name, $arguments) - { - return new Response('Sorry but this action not found', - '404', 'Not found'); - } - - } \ No newline at end of file From d681c85d6754f04c0d74aa55bf0ecd97aa4fbb11 Mon Sep 17 00:00:00 2001 From: max Date: Fri, 10 Dec 2021 13:38:25 -0800 Subject: [PATCH 16/18] fix 404 errors handling --- index.php | 20 +++++++++++++------- templates/404.php | 23 +++++++++++++++++++++++ 2 files changed, 36 insertions(+), 7 deletions(-) create mode 100644 templates/404.php diff --git a/index.php b/index.php index 183eb23..e617cdf 100644 --- a/index.php +++ b/index.php @@ -19,11 +19,10 @@ $request = Request::createFromGlobals(); -$dsn = sprintf("mysql:host=%s;dbname=%s;charset=%s", $database['database_host'], $database['database_name'], $database['charset']); +$dsn = sprintf("mysql:host=%s;dbname=%s;charset=%s", $database['database_host'], $database['database_name'], $database['charset']); /** @var PDO $connection */ -$connection = new PDO( $dsn, $database['username'], $database['password']); +$connection = new PDO($dsn, $database['username'], $database['password']); -//$articleRepository = new ArticleRepository($connection); $sellsRepository = new SellsRepository($connection); $agentsRepository = new AgentsRepository($connection); $contractsRepository = new ContractsRepository($connection); @@ -31,10 +30,17 @@ try { $route = $router->match($request->getPath()); } catch (InvalidArgumentException $exception) { - $route = [ - 'controller' => 'index', - 'action' => 'index' - ]; + if (is_null($request->getPath())) { + $route = [ + 'controller' => 'index', + 'action' => 'index' + ]; + } else { + $route = [ + 'controller' => 'index', + 'action' => 'error' + ]; + } } $controllers = [ diff --git a/templates/404.php b/templates/404.php new file mode 100644 index 0000000..6116376 --- /dev/null +++ b/templates/404.php @@ -0,0 +1,23 @@ + + + + + +

    404

    +

    We are sorry, but whis page doesn't exists

    + + From 096f766a434fa9f1329e79054a2c763eaccc5e58 Mon Sep 17 00:00:00 2001 From: max Date: Fri, 10 Dec 2021 14:35:57 -0800 Subject: [PATCH 17/18] refactoring of errors handling and entities creation --- controllers/ContractsController.php | 64 +------------------ controllers/TableController.php | 39 ++--------- .../errors_handling/ContractsErrorHandler.php | 55 ++++++++++++++++ .../errors_handling/TableErrorHandler.php | 13 ++++ entities/Contract.php | 11 ++++ entities/Sell.php | 24 +++++++ 6 files changed, 111 insertions(+), 95 deletions(-) create mode 100644 controllers/errors_handling/ContractsErrorHandler.php create mode 100644 controllers/errors_handling/TableErrorHandler.php diff --git a/controllers/ContractsController.php b/controllers/ContractsController.php index 975bc52..7d0cda3 100644 --- a/controllers/ContractsController.php +++ b/controllers/ContractsController.php @@ -1,6 +1,7 @@ isPost()) { - $contract = new Contract(); + $contract = new Contract($request); - $contract->number = $request->getPostParameter('number'); - $contract->agent_name = $request->getPostParameter('agent_name'); - $contract->living_complex = $request->getPostParameter('living_complex'); - $contract->award_type = $request->getPostParameter('award_type'); - $contract->award_size = $request->getPostParameter('award_size'); - $contract->sign_date = $request->getPostParameter('sign_date'); - $contract->expiration_date = $request->getPostParameter('expiration_date'); - - $errors = $this->getErrors($contract); + $errors = ContractsErrorHandler::getErrors($contract, $this->contractsRepository); if ($errors !== null) return new Response($this->render('add-contract', ['errors' => $errors])); @@ -42,55 +35,4 @@ public function showContractsFormAction(Request $request) } return new Response($this->render('add-contract')); } - - protected function getErrors(Contract $contract) - { - $result = []; - - $contracts = $this->contractsRepository->getContractByNumber($contract->number); - $contractsOfAgentForThisComplex = $this->contractsRepository->getContractByAgentNameAndComplexName($contract->agent_name, $contract->living_complex); - - $condition1 = (count($contracts) === 0); - - $condition2 = (count($contractsOfAgentForThisComplex) === 0); - - $condition3 = true; - if ($contract->award_type == 'fix') { - $as = (int)$contract->award_size; - if ($as > 1000000 or $as <= 0) - $condition3 = false; - } - - $condition4 = true; - if ($contract->award_type == 'percent') { - $as = (int)$contract->award_size; - if ($as > 10 or $as <= 0) - $condition4 = false; - } - - $condition5 = true; - $d1 = new DateTime($contract->expiration_date); - $d2 = new DateTime("now"); - if ($d1 <= $d2) - $condition5 = false; - - if (!$condition1) - array_push($result, 'Contract with number #' . $contract->number . ' is already exist'); - - if (!$condition2) - array_push($result, 'Agent already has contract for the complex ' . $contract->living_complex); - - if (!$condition3) - array_push($result, 'Award size of fixed award must be between 0 and 1\'000\'000'); - - if (!$condition4) - array_push($result, 'Award size of percent award must be between 0 and 10'); - - if (!$condition5) - array_push($result, 'Expiration date must be in future'); - - if (count($result) == 0) - return null; - return $result; - } } \ No newline at end of file diff --git a/controllers/TableController.php b/controllers/TableController.php index 5464f8e..c6e4f4f 100644 --- a/controllers/TableController.php +++ b/controllers/TableController.php @@ -1,6 +1,7 @@ isPost()) { + $sell = new Sell($request, $this->agentsRepository, $this->contractsRepository); - $sell = new Sell(); - - $contract_info = explode(', ', $request->getPostParameter('contract_info')); - $sell->name = $contract_info[1]; - $sell->agent_id = $this->agentsRepository->getIdByAgentsName($sell->name)[0]; - - $sell->contract_number = (int)$contract_info[0]; - - $apartment_price = (int)$request->getPostParameter('sum'); - $sell->sum = $this->getAwardFromApartmentPrice($sell->contract_number, $apartment_price); - - $sell->apartment_number = $request->getPostParameter('apartment_number'); - $sell->living_complex = $contract_info[2]; - - $errors = $this->getErrors($sell); - if($errors !== null) + $errors = TableErrorHandler::getErrors($sell, $this->sellsRepository); + if ($errors !== null) return new Response($this->render('add-sell', ['contractsRepository' => $this->contractsRepository, 'errors' => $errors])); @@ -60,23 +48,6 @@ public function addSellAction(Request $request) return new Response($this->render('add-sell', ['contractsRepository' => $this->contractsRepository])); } - protected function getErrors(Sell $sell){ - $result = $this->sellsRepository->getAllByLivingComplexAndApartmentNumber($sell->living_complex, $sell->apartment_number); - if (count($result) == 0){ - return null; - }else{ - return ['Contract on apartment #' . $sell->apartment_number . ' is already exists. Please, enter different apartment number']; - } - } - protected function getAwardFromApartmentPrice($contract_number, $apartment_price) - { - $contract = $this->contractsRepository->getContractByNumber($contract_number)[0]; - $award_type = $contract['award_type']; - if($award_type === 'fix'){ - return (int)$contract['award_size']; - }else{ - return (((int)$contract['award_size'])/100) * ((int)$apartment_price); - } - } + } \ No newline at end of file diff --git a/controllers/errors_handling/ContractsErrorHandler.php b/controllers/errors_handling/ContractsErrorHandler.php new file mode 100644 index 0000000..750e90a --- /dev/null +++ b/controllers/errors_handling/ContractsErrorHandler.php @@ -0,0 +1,55 @@ +getContractByNumber($contract->number); + $contractsOfAgentForThisComplex = $contractsRepository->getContractByAgentNameAndComplexName($contract->agent_name, $contract->living_complex); + + $condition1 = (count($contracts) === 0); + + $condition2 = (count($contractsOfAgentForThisComplex) === 0); + + $condition3 = true; + if ($contract->award_type == 'fix') { + $as = (int)$contract->award_size; + if ($as > 1000000 or $as <= 0) + $condition3 = false; + } + + $condition4 = true; + if ($contract->award_type == 'percent') { + $as = (int)$contract->award_size; + if ($as > 10 or $as <= 0) + $condition4 = false; + } + + $condition5 = true; + $d1 = new DateTime($contract->expiration_date); + $d2 = new DateTime("now"); + if ($d1 <= $d2) + $condition5 = false; + + if (!$condition1) + array_push($result, 'Contract with number #' . $contract->number . ' is already exist'); + + if (!$condition2) + array_push($result, 'Agent already has contract for the complex ' . $contract->living_complex); + + if (!$condition3) + array_push($result, 'Award size of fixed award must be between 0 and 1\'000\'000'); + + if (!$condition4) + array_push($result, 'Award size of percent award must be between 0 and 10'); + + if (!$condition5) + array_push($result, 'Expiration date must be in future'); + + if (count($result) == 0) + return null; + return $result; + } +} \ No newline at end of file diff --git a/controllers/errors_handling/TableErrorHandler.php b/controllers/errors_handling/TableErrorHandler.php new file mode 100644 index 0000000..21bb329 --- /dev/null +++ b/controllers/errors_handling/TableErrorHandler.php @@ -0,0 +1,13 @@ +getAllByLivingComplexAndApartmentNumber($sell->living_complex, $sell->apartment_number); + if (count($result) == 0){ + return null; + }else{ + return ['Contract on apartment #' . $sell->apartment_number . ' is already exists. Please, enter different apartment number']; + } + } +} \ No newline at end of file diff --git a/entities/Contract.php b/entities/Contract.php index 555c31f..ca4fef1 100644 --- a/entities/Contract.php +++ b/entities/Contract.php @@ -9,4 +9,15 @@ class Contract public $award_size; public $sign_date; public $expiration_date; + + public function __construct($request) + { + $this->number = $request->getPostParameter('number'); + $this->agent_name = $request->getPostParameter('agent_name'); + $this->living_complex = $request->getPostParameter('living_complex'); + $this->award_type = $request->getPostParameter('award_type'); + $this->award_size = $request->getPostParameter('award_size'); + $this->sign_date = $request->getPostParameter('sign_date'); + $this->expiration_date = $request->getPostParameter('expiration_date'); + } } \ No newline at end of file diff --git a/entities/Sell.php b/entities/Sell.php index 3ff0444..9dbedf3 100644 --- a/entities/Sell.php +++ b/entities/Sell.php @@ -8,4 +8,28 @@ class Sell public $contract_number; public $apartment_number; public $living_complex; + + public function __construct(Request $request, AgentsRepository $agentsRepository, ContractsRepository $contractsRepository) + { + $contract_info = explode(', ', $request->getPostParameter('contract_info')); + $this->name = $contract_info[1]; + $this->agent_id = $agentsRepository->getIdByAgentsName($this->name)[0]; + $this->contract_number = (int)$contract_info[0]; + $apartment_price = (int)$request->getPostParameter('sum'); + $this->sum = $this->getAwardFromApartmentPrice($this->contract_number, $apartment_price, $contractsRepository); + $this->apartment_number = $request->getPostParameter('apartment_number'); + $this->living_complex = $contract_info[2]; + } + + protected function getAwardFromApartmentPrice($contract_number, $apartment_price, $contractsRepository) + { + $contract = $contractsRepository->getContractByNumber($contract_number)[0]; + $award_type = $contract['award_type']; + if ($award_type === 'fix') { + return (int)$contract['award_size']; + } else { + return (((int)$contract['award_size']) / 100) * ((int)$apartment_price); + } + } + } \ No newline at end of file From 58ad794fabfcbc3cc504063325a856326ce5d0a0 Mon Sep 17 00:00:00 2001 From: max Date: Sat, 18 Dec 2021 04:49:28 -0800 Subject: [PATCH 18/18] rework of contracts- and tables-error handlers --- controllers/TableController.php | 3 - .../errors_handling/ContractsErrorHandler.php | 91 +++++++++++++------ .../errors_handling/TableErrorHandler.php | 70 ++++++++++++-- entities/Sell.php | 5 +- index.php | 1 - repositories/SellsRepository.php | 5 + 6 files changed, 136 insertions(+), 39 deletions(-) diff --git a/controllers/TableController.php b/controllers/TableController.php index c6e4f4f..da1c6de 100644 --- a/controllers/TableController.php +++ b/controllers/TableController.php @@ -47,7 +47,4 @@ public function addSellAction(Request $request) } return new Response($this->render('add-sell', ['contractsRepository' => $this->contractsRepository])); } - - - } \ No newline at end of file diff --git a/controllers/errors_handling/ContractsErrorHandler.php b/controllers/errors_handling/ContractsErrorHandler.php index 750e90a..0394c1f 100644 --- a/controllers/errors_handling/ContractsErrorHandler.php +++ b/controllers/errors_handling/ContractsErrorHandler.php @@ -6,50 +6,87 @@ public static function getErrors(Contract $contract, ContractsRepository $contra { $result = []; + foreach ((array)$contract as $key => $value) { + if ($value == '') { + array_push($result, ucfirst(str_replace('_', ' ', $key)) . ' can not be empty'); + } else { + $funcName = 'check_' . $key; + $result = (new ContractsErrorHandler)->$funcName($contract, $contractsRepository, $result); + } + } + + if (count($result) == 0) + return null; + return $result; + } + + private function check_number($contract, ContractsRepository $contractsRepository, $result) + { $contracts = $contractsRepository->getContractByNumber($contract->number); + if (count($contracts) !== 0) + array_push($result, 'Contract with number #' . $contract->number . ' is already exist'); + return $result; + } + + private function check_agent_name($contract, ContractsRepository $contractsRepository, $result) + { $contractsOfAgentForThisComplex = $contractsRepository->getContractByAgentNameAndComplexName($contract->agent_name, $contract->living_complex); + if (count($contractsOfAgentForThisComplex) !== 0) + array_push($result, 'Agent already has contract for the complex ' . $contract->living_complex); + return $result; + } - $condition1 = (count($contracts) === 0); + private function check_living_complex($contract, ContractsRepository $contractsRepository, $result) + { + return $result; + } - $condition2 = (count($contractsOfAgentForThisComplex) === 0); + private function check_award_type(Contract $contract, ContractsRepository $contractsRepository, $result) + { + if ($contract->award_type !== 'fix' && $contract->award_type !== 'percent') + array_push($result, 'Award type must be \'fix\' or \'percent\''); - $condition3 = true; + return $result; + } + + private function check_award_size(Contract $contract, ContractsRepository $contractsRepository, $result) + { + $message = ''; + $is_award_correct = true; if ($contract->award_type == 'fix') { $as = (int)$contract->award_size; - if ($as > 1000000 or $as <= 0) - $condition3 = false; + if ($as > 1000000 or $as <= 0) { + $is_award_correct = false; + $message = 'Award size of fixed award must be between 0 and 1\'000\'000'; + } } - $condition4 = true; if ($contract->award_type == 'percent') { $as = (int)$contract->award_size; - if ($as > 10 or $as <= 0) - $condition4 = false; + if ($as > 10 or $as <= 0) { + $is_award_correct = false; + $message = 'Award size of percent award must be between 0 and 10'; + } } - $condition5 = true; - $d1 = new DateTime($contract->expiration_date); - $d2 = new DateTime("now"); - if ($d1 <= $d2) - $condition5 = false; - - if (!$condition1) - array_push($result, 'Contract with number #' . $contract->number . ' is already exist'); - - if (!$condition2) - array_push($result, 'Agent already has contract for the complex ' . $contract->living_complex); - - if (!$condition3) - array_push($result, 'Award size of fixed award must be between 0 and 1\'000\'000'); + if (!$is_award_correct) + array_push($result, $message); + return $result; + } - if (!$condition4) - array_push($result, 'Award size of percent award must be between 0 and 10'); + private function check_sign_date(Contract $contract, ContractsRepository $contractsRepository, $result) + { + $isDateCorrect = true; + $expirationDate = new DateTime($contract->expiration_date); + $currentDate = new DateTime("now"); - if (!$condition5) + if ($expirationDate <= $currentDate) array_push($result, 'Expiration date must be in future'); + return $result; + } - if (count($result) == 0) - return null; + private function check_expiration_date(Contract $contract, ContractsRepository $contractsRepository, $result) + { return $result; } } \ No newline at end of file diff --git a/controllers/errors_handling/TableErrorHandler.php b/controllers/errors_handling/TableErrorHandler.php index 21bb329..1bc0cc2 100644 --- a/controllers/errors_handling/TableErrorHandler.php +++ b/controllers/errors_handling/TableErrorHandler.php @@ -2,12 +2,70 @@ class TableErrorHandler { - public static function getErrors(Sell $sell, SellsRepository $sellsRepository){ - $result = $sellsRepository->getAllByLivingComplexAndApartmentNumber($sell->living_complex, $sell->apartment_number); - if (count($result) == 0){ - return null; - }else{ - return ['Contract on apartment #' . $sell->apartment_number . ' is already exists. Please, enter different apartment number']; + + public static function getErrors(Sell $sell, SellsRepository $sellsRepository) + { + $result = []; + + foreach ((array)$sell as $key => $value) { + if ($value == '') { + array_push($result, ucfirst(str_replace('_', ' ', $key)) . ' can not be empty'); + } else { + $funcName = 'check_' . $key; + $result = (new TableErrorHandler)->$funcName($sell, $sellsRepository, $result); + } } + + if (count($result) == 0) + return null; + return $result; + } + + private function check_contract_number(Sell $sell, SellsRepository $sellsRepository, $result) + { + $sells = $sellsRepository->getByContractNumber($sell->contract_number); + if (count($sells) === 0) + array_push($result, 'Agent with contract number ' . $sell->contract_number . ' doesn\'t exists'); + return $result; + } + + private function check_name(Sell $sell, SellsRepository $sellsRepository, $result) + { + return $result; + } + + private function check_agent_id(Sell $sell, SellsRepository $sellsRepository, $result) + { + return $result; + } + + + private function check_apartment_number(Sell $sell, SellsRepository $sellsRepository, $result) + { + if (count($sellsRepository->getAllByLivingComplexAndApartmentNumber($sell->living_complex, $sell->apartment_number)) !== 0) + array_push($result, 'Contract on apartment #' . $sell->apartment_number . ' is already exists. Please, enter different apartment number'); + return $result; + } + + private function check_living_complex(Sell $sell, SellsRepository $sellsRepository, $result) + { + $sells = $sellsRepository->getByContractNumber($sell->contract_number); + $livingComplex = $sells['living_complex']; + if (count($sells) === 0 || $livingComplex != $sell->living_complex) + array_push($result, "Agent's contract doesn't include living complex '$livingComplex'"); + return $result; + } + + private function check_sum(Sell $sell, SellsRepository $sellsRepository, $result) + { + return $result; + } + + private function check_apartment_price(Sell $sell, SellsRepository $sellsRepository, $result) + { + $sum = $sell->apartment_price; + if ($sum < 500000) + array_push($result, "Sell's sum can't be less than 500'000"); + return $result; } } \ No newline at end of file diff --git a/entities/Sell.php b/entities/Sell.php index 9dbedf3..f490533 100644 --- a/entities/Sell.php +++ b/entities/Sell.php @@ -6,6 +6,7 @@ class Sell public $agent_id; public $sum; public $contract_number; + public $apartment_price; public $apartment_number; public $living_complex; @@ -15,8 +16,8 @@ public function __construct(Request $request, AgentsRepository $agentsRepository $this->name = $contract_info[1]; $this->agent_id = $agentsRepository->getIdByAgentsName($this->name)[0]; $this->contract_number = (int)$contract_info[0]; - $apartment_price = (int)$request->getPostParameter('sum'); - $this->sum = $this->getAwardFromApartmentPrice($this->contract_number, $apartment_price, $contractsRepository); + $this->apartment_price = (int)$request->getPostParameter('sum'); + $this->sum = $this->getAwardFromApartmentPrice($this->contract_number, $this->apartment_price, $contractsRepository); $this->apartment_number = $request->getPostParameter('apartment_number'); $this->living_complex = $contract_info[2]; } diff --git a/index.php b/index.php index e617cdf..b16f4e0 100644 --- a/index.php +++ b/index.php @@ -18,7 +18,6 @@ $router = new Router($routes); $request = Request::createFromGlobals(); - $dsn = sprintf("mysql:host=%s;dbname=%s;charset=%s", $database['database_host'], $database['database_name'], $database['charset']); /** @var PDO $connection */ $connection = new PDO($dsn, $database['username'], $database['password']); diff --git a/repositories/SellsRepository.php b/repositories/SellsRepository.php index ed80819..11cf84d 100644 --- a/repositories/SellsRepository.php +++ b/repositories/SellsRepository.php @@ -31,6 +31,11 @@ public function getAllByAgentId($id) return $statement->fetchAll(); } + public function getByContractNumber($number){ + $statement = $this->connection->query("SELECT * FROM contracts WHERE number = " . $number); + return $statement->fetch(); + } + public function addNewSell(Sell $sell) { $template_query = 'INSERT INTO apartments_sells (agent_id, sum, contract_number, apartment_number, living_complex)