80 lines
2.2 KiB
PHP
80 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace App\Service;
|
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
|
|
use CrEOF\Spatial\PHP\Types\Geometry\Point;
|
|
|
|
use App\Entity\PriceTier;
|
|
|
|
class PriceTierManager
|
|
{
|
|
protected $em;
|
|
|
|
public function __construct(EntityManagerInterface $em)
|
|
{
|
|
$this->em = $em;
|
|
}
|
|
|
|
public function getItemPrice($pt_id, $item_type_id, $item_id)
|
|
{
|
|
// find the item price, given the price tier, battery id, and item type (battery)
|
|
$db_conn = $this->em->getConnection();
|
|
|
|
$ip_sql = 'SELECT ip.price AS price
|
|
FROM item_price ip
|
|
WHERE ip.price_tier_id = :pt_id
|
|
AND ip.item_type_id = :it_id
|
|
AND ip.item_id = :item_id';
|
|
|
|
$ip_stmt = $db_conn->prepare($ip_sql);
|
|
$ip_stmt->bindValue('pt_id', $pt_id);
|
|
$ip_stmt->bindValue('it_id', $item_type_id);
|
|
$ip_stmt->bindValue('item_id', $item_id);
|
|
|
|
$ip_result = $ip_stmt->executeQuery();
|
|
|
|
// results found
|
|
$actual_price = null;
|
|
|
|
// go through rows
|
|
while ($row = $ip_result->fetchAssociative())
|
|
{
|
|
// get the price
|
|
$price = $row['price'];
|
|
|
|
// actual price
|
|
$actual_price = number_format($price / 100, 2, '.', '');
|
|
}
|
|
|
|
return $actual_price;
|
|
}
|
|
|
|
public function getPriceTier(Point $coordinates)
|
|
{
|
|
$price_tier_id = 0;
|
|
|
|
if ($coordinates != null)
|
|
{
|
|
$long = $coordinates->getLongitude();
|
|
$lat = $coordinates->getLatitude();
|
|
|
|
// get location's price tier, given a set of coordinates
|
|
$query = $this->em->createQuery('SELECT s from App\Entity\SupportedArea s where st_contains(s.coverage_area, point(:long, :lat)) = true');
|
|
$area = $query->setParameter('long', $long)
|
|
->setParameter('lat', $lat)
|
|
->setMaxResults(1)
|
|
->getOneOrNullResult();
|
|
|
|
if ($area != null)
|
|
{
|
|
$price_tier = $area->getPriceTier();
|
|
if ($price_tier != null)
|
|
$price_tier_id = $price_tier->getID();
|
|
}
|
|
}
|
|
|
|
return $price_tier_id;
|
|
}
|
|
}
|