File: /var/www/vhosts/creativefellows.nl/jhtaxatie.creativefellows.nl/classes/AntonController.php
<?php
class AntonController extends BaseController
{
function __construct($db_connection,$router,$user_id,$view=null,$user_role=null,$settings=null)
{
$this->db = $db_connection;
$this->router = $router;
$this->view = $view;
$this->user_role = $user_role;
$this->user_id = $user_id;
$this->settings = $settings;
$this->fields_v2 = $this->settings['enquiry_fields_v2'];
$this->siteUrl = $this->settings['siteUrl'];
}
public function listTrucks($request, $response, $args)
{
$routeName = $this->getCurrentRouteName($request);
$trucks = $this->listAntonTrucks();
return $this->view->render($response, 'request/list_anton.php', [
"router" => $this->router,
"label" => "Aanvragen",
"trucks" => $trucks,
"truck_count" => $this->truckcount,
"active_page" => $view_page,
"show_custom" => in_array($this->user_role,[2,3,14]) ? true : false,
]);
}
public function getTruck($request, $response, $args)
{
$routeName = $this->getCurrentRouteName($request);
$params = $request->getQueryParams();
$view_page = isset($params["p"]) ? $params["p"] : 1;
$truck = $this->getTruckBySerialnumber($args["id"]);
$truck->setGGfactor(1);
$this->fields_v2["Klant_anton"][2][5] = $truck->truckVersions();
$this->fields_v2["Klant_anton"][3][5] = $truck->versionsToOptions() ;//$truck->options();
$this->fields_v2["Klant_anton"][4][5] = $truck->pricing("Totaalprijs truck");
$this->fields_v2["Klant_anton"][5][5] = $truck->transport();
// form
$form = new FormData($this->fields_v2["Klant_anton"],[],$this->user_role);
$form->setAction($this->router->pathFor("request.confirm.anton",["id" => $args["id"]]));
// return data
return $this->view->render($response, 'request/get_truck.php', [
"router" => $this->router,
"label" => "Offertes klant",
"trucks" => $truck,
"pagination" => $this->pagination,
"page" => $this->router->pathFor("enquiry.dashboard"),
"form" => $form,
]);
}
public function confirmRequest($request, $response, $args)
{
// form
$post = $request->getParsedBody();
$options = $post["options"] ?? [];
$options_custom = [];
// d($options);
// truck
$truck = $this->getTruckBySerialnumber($args["id"]);
$truck->setGGfactor(1);
$versions = $truck->truckVersions();
$selected_version = $versions[$post["truck_version"]];
$truck->catalog()->setOptionPrice("adjuster",$selected_version["fork_adjuster"]);
$truck->selectedOptions($options,$options_custom);
//d($selected_version);
$this->fields_v2["Klant_anton"][2][5] = $truck->truckVersions();
$this->fields_v2["Klant_anton"][3][5] = $truck->versionsToOptions();//$truck->options();
$this->fields_v2["Klant_anton"][4][5] = $truck->pricing();
$this->fields_v2["Klant_anton"][4][5]["truck_version"] = $selected_version;
// set form
$form = new FormData($this->fields_v2["Klant_anton"],$post);
$form->setAction($this->router->pathFor("request.post.anton",["id" => $args["id"]]));
return $this->view->render($response, 'request/confirm_truck.php', [
"trucks" => $truck,
"pagination" => $this->pagination,
"form" => $form,
"buttons" => true,
]);
}
public function postRequest($request, $response, $args){
$post = $request->getParsedBody() ?? [];
$truck = $this->getTruckBySerialnumber($args["id"]);
$versions = $truck->truckVersions();
$selected_version = $versions[$post["truck_version"]];
$sth = $this->db->prepare(
"INSERT INTO truck_requests(
type,
custom,
user_id,
stock_id ,
catalog_id,
version,
company_name,
options,
price,
date
)
VALUES(
:type,
:custom,
:user_id,
:stock_id,
:catalog_id,
:version,
:company_name,
:options,
:price,
NOW()
)");
$sth->execute([
"type" => 1,
"custom" => 1,
"user_id" => $this->user_id,
"stock_id" => $truck->id(),
"catalog_id" => $truck->catalogId(),
"version" => $post["truck_version"],
"company_name" => $post["company_name"],
"options" => isset($post["options"]) ? implode(",",$post["options"]) : null,
"price" => $selected_version["price"],
]);
return $response->withRedirect( $this->router->pathFor("request.list") );
}
private function getTruckBySerialnumber(string $serial_number)
{
$sql = 'SELECT * FROM truck_stock WHERE status = 1 AND serial_number = :serial_number';
$sth = $this->db->prepare($sql);
$sth->execute(["serial_number" => $serial_number]);
$row = $sth->fetch();
return $this->truckData($row);
}
private function listAntonTrucks($view_page = 1,$filters=null)
{
// trucks
$sql = "SELECT *,price as min_price FROM truck_stock WHERE type like '%anton%' AND status = 1 AND sold = 0 ORDER BY serial_number ASC";
$sth = $this->db->prepare($sql);
$sth->execute($sql_args);
$trucks = [];
while($row = $sth->fetch())
{
$trucks[] = $this->truckData($row,false);
}
return $trucks;
}
private function truckData($data,$list_functions=true)
{
$data["url"] = $this->router->pathFor('request.get.antontruck', ['id' => $data["serial_number"]]);
$truck = new RequestTruckData($data);
$truck->setStarsFactor($this->settings["request_star_factors"]);
// current functions on truck
$functions = $this->truckFunctionDescriptions( $truck->truckOptions() );
// truck catalog
$catalog = $this->truckCatalog( $truck->typeShort() );
$catalog->setOptionLabel("adjuster","Vorkversteller");
//$catalog->setImage("adjuster","Vorkversteller");
//.// d($this->truckVersions($truck->id()) );
// die();
// truck catalog
$truck->setTruckVersions( $this->truckVersions($truck->id()) );
$truck->setFunctions($functions);
$truck->setCatalog($catalog);
return $truck;
}
private function truckVersions(int $stock_id)
{
$sql = "SELECT * from truck_version WHERE stock_id= :id ORDER BY version_id";
$sth = $this->db->prepare($sql);
$sth->execute([
"id" => $stock_id
]);
$versions = [];
while($row = $sth->fetch()) {
$versions[$row["version_id"]] = $row;
}
return $versions;
}
private function truckFunctionDescriptions(array $options = [])
{
if(empty($options)) return [];
$in_array = str_repeat('?,', count($options) - 1) . '?';
$sql = "SELECT abbr, value AS label FROM truck_stock_options WHERE abbr IN ($in_array) ORDER BY label";
$sth = $this->db->prepare($sql);
$sth->execute($options);
$truck_options = [];
while($row = $sth->fetch()) {
$truck_options[] = $row;
}
return $truck_options;
}
private function truckCatalog($truck_type)
{
$sql = 'SELECT * FROM truck_catalog WHERE type LIKE :type AND status = 1';
$sth = $this->db->prepare($sql);
$sth->execute(["type" => $truck_type."%"]);
$row = $sth->fetch() ?? [];
if(!$row) $row = [];
return new RequestCatalogData($row);
}
}