<?php
//----------------------------------------------------------------------
// src/Services/Location/AddressTools.php
//----------------------------------------------------------------------
namespace App\Services\Location;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Component\PropertyAccess\PropertyAccess;
use App\Entity\Client\Client;
use App\Entity\Client\Individual;
use App\Entity\Client\Store;
use App\Entity\Config\Config;
use App\Entity\Config\Module;
use App\Entity\Config\ModuleConfig;
use App\Entity\Location\Address;
use App\Entity\Location\Department;
use App\Entity\Platform\Phone;
use App\Entity\SocietyGroup;
use App\Services\Config\ModuleTools;
use App\Services\LogTools;
class AddressTools
{
public function __construct(ManagerRegistry $doctrine, TranslatorInterface $translator, LogTools $logTools, ModuleTools $moduleTools)
{
$this->em = $doctrine->getManager();
$this->translator = $translator;
$this->logTools = $logTools;
$this->moduleTools = $moduleTools;
$this->debug = true;
}
// Plan.io Task #3621
public function handleAddressDataForServiceOrder($decodedAddressData, SocietyGroup $societyGroup, Individual $individual)
{
if (empty($decodedAddressData))
{
return array(
'addresses' => array(),
'phones' => array(),
);
}
$addresses = array();
$phones = array();
$parsedAddressData = $this->extractAddressesFromServiceOrderUpdate($societyGroup, $decodedAddressData);
$interventionAddress = $parsedAddressData['interventionAddress'];
$billingAddress = $parsedAddressData['billingAddress'];
$otherAddress = $parsedAddressData['otherAddress'];
$addresses = $parsedAddressData['addresses'];
if ($interventionAddress !== null)
{
$individual->setAddress($interventionAddress);
$this->updateInfo($individual->getAddress());
}
if ($billingAddress !== null)
{
$individual->setBillingAddress($billingAddress);
$this->updateInfo($individual->getBillingAddress());
}
foreach ($otherAddress as $address)
{
// Attach the next ones to the Client
$individual->getClient()->addAddress($address);
}
$phoneData = $this->craftPhoneForServiceOrder($decodedAddressData, $societyGroup, $individual);
foreach ($phoneData as $phone)
{
$phones[] = $phone;
}
foreach ($phones as $phone)
{
$this->em->persist($phone);
}
return array(
'addresses' => $addresses,
'phones' => $phones,
);
}
// Plan.io Task #3621
public function craftPhoneForServiceOrder($decodedAddressData, SocietyGroup $societyGroup, Individual $individual)
{
// $this->logTools->ploopLog("[AddressTools][craftPhoneForServiceOrder] ".print_r($decodedAddressData, true));
$phones = array();
foreach ($decodedAddressData as $key => $addressData)
{
if (array_key_exists('primaryPhone', $addressData) && !empty($addressData['primaryPhone']))
{
$phone = new Phone();
$phone->setSocietyGroup($societyGroup);
$phone->setNumber($addressData['primaryPhone']);
// Attach to individual
$individual->addPhone($phone);
$phones[] = $phone;
// $this->logTools->ploopLog("[AddressTools][craftPhoneForServiceOrder] primaryPhone = ".$addressData['primaryPhone']);
}
if (array_key_exists('secondaryPhone', $addressData) && !empty($addressData['secondaryPhone']))
{
$phone = new Phone();
$phone->setSocietyGroup($societyGroup);
$phone->setNumber($addressData['secondaryPhone']);
// Attach to individual
$individual->addPhone($phone);
$phones[] = $phone;
// $this->logTools->ploopLog("[AddressTools][craftPhoneForServiceOrder] secondaryPhone = ".$addressData['secondaryPhone']);
}
if (array_key_exists('mobilePhone', $addressData) && !empty($addressData['mobilePhone']))
{
$phone = new Phone();
$phone->setSocietyGroup($societyGroup);
$phone->setNumber($addressData['mobilePhone']);
// Attach to individual
$individual->addPhone($phone);
$phones[] = $phone;
// $this->logTools->ploopLog("[AddressTools][craftPhoneForServiceOrder] mobilePhone = ".$addressData['mobilePhone']);
}
}
return $phones;
}
// Plan.io Task #3621
public function craftAddressForServiceOrder($addressData, SocietyGroup $societyGroup)
{
// $this->logTools->ploopLog("[AddressTools][craftAddressForServiceOrder] societyGroup = ".$societyGroup->displayForLog());
$address = new Address();
$address->setSocietyGroup($societyGroup);
if (array_key_exists('street', $addressData) && !empty($addressData['street']))
{
$address->setAddress($addressData['street']);
}
$complement = "";
if (array_key_exists('address1', $addressData) && !empty($addressData['address1']))
{
$complement .= $addressData['address1'];
$complement .= " ";
}
if (array_key_exists('address2', $addressData) && !empty($addressData['address2']))
{
$complement .= $addressData['address2'];
$complement .= " ";
}
if (array_key_exists('address3', $addressData) && !empty($addressData['address3']))
{
$complement .= $addressData['address3'];
$complement .= " ";
}
if (array_key_exists('address4', $addressData) && !empty($addressData['address4']))
{
$complement .= $addressData['address4'];
$complement .= " ";
}
if (array_key_exists('address5', $addressData) && !empty($addressData['address5']))
{
$complement .= $addressData['address5'];
$complement .= " ";
}
if (!empty($complement))
{
$address->setComplement($complement);
}
if (array_key_exists('postalCode', $addressData) && !empty($addressData['postalCode']))
{
$address->setPostalCode($addressData['postalCode']);
}
if (array_key_exists('city', $addressData) && !empty($addressData['city']))
{
$address->setTown($addressData['city']);
}
return $address;
}
public function updateAddressData($client)
{
if ($client === null)
{
return false;
}
$individual = $client->getIndividual();
if ($individual !== null)
{
// Reset
$addresses = $client->getAddresses();
foreach ($addresses as $address)
{
$address->setIntervention(0);
$address->setBilling(0);
$this->updateInfo($address);
//$this->em->persist($address);
}
if ($individual->getAddress() !== null)
{
$individual->getAddress()->setIntervention(1);
$this->updateInfo($individual->getAddress());
}
if ($individual->getBillingAddress() !== null)
{
$individual->getBillingAddress()->setBilling(1);
$this->updateInfo($individual->getBillingAddress());
}
return true;
}
return false;
}
public function updateInfo($address, $object = null)
{
if ($address === null)
{
return false;
}
if ($object !== null)
{
$address->setObjectRef($object->getRef());
if ($address->getSocietyGroup() === null)
{
$address->setSocietyGroup($object->getSocietyGroup());
}
}
$info = "";
$infoFull = "";
if ($address->getIntervention() == 1)
{
$info = $this->translator->trans("intervention_address_short");
$infoFull = $this->translator->trans("intervention_address_for");
}
else
{
if ($address->getBilling() == 1)
{
$info = $this->translator->trans("billing_address_short");
$infoFull = $this->translator->trans("billing_address_for");
}
else
{
$info = "";
$infoFull = $this->translator->trans("address_for");
}
}
if (!empty($address->getObjectRef()))
{
$info = $address->getObjectRef() . ", " . $info;
$infoFull = $infoFull . $address->getObjectRef();
}
else
{
// No ref => No full info
$infoFull = null;
}
if ($info == "")
{
$info = null;
}
$address->setInfo($info);
$address->setInfoFull($infoFull);
return true;
}
public function guessBillingAddressForMission($mission)
{
// Emitter ?
$client = $mission->getEmitter();
if ($client === null)
return null;
$billingAddress = $client->getBillingAddress();
if ($billingAddress === null)
{
$billingAddress = $client->getAddress();
}
if ($billingAddress !== null)
{
$mission->setBillingAddress($this->duplicateAddress($billingAddress));
return $mission->getBillingAddress();
}
return null;
}
public function guessBillingAddressForDevis($devis)
{
// Emitter ?
$client = $devis->getEmitter();
if ($client === null)
return null;
$billingAddress = $client->getBillingAddress();
if ($billingAddress === null)
{
$billingAddress = $client->getAddress();
}
if ($billingAddress !== null)
{
$devis->setBillingAddress($this->duplicateAddress($billingAddress));
return $devis->getBillingAddress();
}
return null;
}
// This is used when creating a new devis
public function guessAddressesForDevis($devis)
{
// Intervention Address = devis.mission.interventionAddress
// Billing Address = devis.emitter.billingAddress
// Intervention Address
// Intervention Address = devis.mission.interventionAddress
$mission = $devis->getMission();
$interventionAddress = null;
if ($mission !== null)
{
if ($mission->getInterventionAddress() !== null)
{
$interventionAddress = $mission->getInterventionAddress();
}
}
if ($interventionAddress === null)
{
// This should not happen
// Fallback on the intervention address from the receiver
if ($devis->getReceiver() !== null)
{
$interventionAddress = $devis->getReceiver()->getInterventionAddress();
}
}
if ($interventionAddress === null)
{
// This really should not happen
$interventionAddress = new Address();
$interventionAddress->setSocietyGroup($devis->getSocietyGroup());
$this->em->persist($interventionAddress);
}
// Billing Address
// Billing Address = devis.emitter.billingAddress
$emitter = $devis->getEmitter();
$billingAddress = $emitter->getBillingAddress();
if ($billingAddress === null)
{
// This should not happen
// Fallback on the billing address from the mission
if ($mission !== null)
{
$billingAddress = $mission->getBillingAddress();
}
}
if ($billingAddress === null)
{
// This really should not happen
$billingAddress = new Address();
$billingAddress->setSocietyGroup($devis->getSocietyGroup());
$this->em->persist($billingAddress);
}
if ($interventionAddress !== null)
{
$devis->setInterventionAddress($this->duplicateAddress($interventionAddress));
$this->updateInfo($devis->getInterventionAddress(), $devis);
}
if ($billingAddress !== null)
{
$devis->setBillingAddress($this->duplicateAddress($billingAddress));
$this->updateInfo($devis->getBillingAddress(), $devis);
}
return true;
}
// This is used when creating a new invoice from a devis
public function guessAddressesForInvoiceFromDevis($invoice, $devis)
{
if ($devis === null)
{
return false;
}
// Intervention Address = devis.interventionAddress
// Billing Address = devis.billingAddress
// Intervention Address
// Intervention Address = devis.interventionAddress
$interventionAddress = null;
if ($devis->getInterventionAddress() !== null)
{
$interventionAddress = $devis->getInterventionAddress();
}
if ($interventionAddress === null)
{
// This should not happen
// Fallback on the intervention address from the mission
if ($devis->getMission() !== null)
{
$interventionAddress = $devis->getMission()->getInterventionAddress();
}
}
if ($interventionAddress === null)
{
// This should not happen
// Fallback on the intervention address from the receiver
if ($devis->getReceiver() !== null)
{
$interventionAddress = $devis->getReceiver()->getInterventionAddress();
}
}
if ($interventionAddress === null)
{
// This really should not happen
$interventionAddress = new Address();
$interventionAddress->setSocietyGroup($devis->getSocietyGroup());
$this->em->persist($interventionAddress);
}
// Billing Address
// Billing Address = devis.billingAddress
$billingAddress = $devis->getBillingAddress();
if ($billingAddress === null)
{
// This should not happen
// Fallback on the billing address from the mission
if ($devis->getMission() !== null)
{
$billingAddress = $devis->getMission()->getBillingAddress();
}
}
if ($interventionAddress === null)
{
// This should not happen
// Fallback on the intervention address from the emitter
if ($devis->getEmitter() !== null)
{
$interventionAddress = $devis->getEmitter()->getBillingAddress();
}
}
if ($billingAddress === null)
{
// This really should not happen
$billingAddress = new Address();
$billingAddress->setSocietyGroup($invoice->getSocietyGroup());
$this->em->persist($billingAddress);
}
if ($interventionAddress !== null)
{
$invoice->setInterventionAddress($this->duplicateAddress($interventionAddress));
$this->updateInfo($invoice->getInterventionAddress(), $invoice);
}
if ($billingAddress !== null)
{
$invoice->setBillingAddress($this->duplicateAddress($billingAddress));
$this->updateInfo($invoice->getBillingAddress(), $invoice);
}
return true;
}
// This is used when creating a new credit from an invoice
public function guessAddressesForCreditFromInvoice($credit, $invoice)
{
if ($invoice === null)
{
return false;
}
// Intervention Address = invoice.interventionAddress
// Billing Address = invoice.billingAddress
// Intervention Address
// Intervention Address = invoice.interventionAddress
$interventionAddress = null;
if ($invoice->getInterventionAddress() !== null)
{
$interventionAddress = $invoice->getInterventionAddress();
}
if ($interventionAddress === null)
{
// This should not happen
// Fallback on the intervention address from the mission
if ($credit->getMission() !== null)
{
$interventionAddress = $credit->getMission()->getInterventionAddress();
}
}
if ($interventionAddress === null)
{
// This should not happen
// Fallback on the intervention address from the receiver
if ($credit->getReceiver() !== null)
{
$interventionAddress = $credit->getReceiver()->getInterventionAddress();
}
}
if ($interventionAddress === null)
{
// This really should not happen
$interventionAddress = new Address();
$interventionAddress->setSocietyGroup($credit->getSocietyGroup());
$this->em->persist($interventionAddress);
}
// Billing Address
// Billing Address = devis.billingAddress
$billingAddress = $invoice->getBillingAddress();
if ($billingAddress === null)
{
// This should not happen
// Fallback on the billing address from the mission
if ($credit->getMission() !== null)
{
$billingAddress = $credit->getMission()->getBillingAddress();
}
}
if ($interventionAddress === null)
{
// This should not happen
// Fallback on the intervention address from the emitter
if ($credit->getEmitter() !== null)
{
$interventionAddress = $credit->getEmitter()->getBillingAddress();
}
}
if ($billingAddress === null)
{
// This really should not happen
$billingAddress = new Address();
$billingAddress->setSocietyGroup($credit->getSocietyGroup());
$this->em->persist($billingAddress);
}
if ($interventionAddress !== null)
{
$credit->setInterventionAddress($this->duplicateAddress($interventionAddress));
$this->updateInfo($credit->getInterventionAddress(), $credit);
}
if ($billingAddress !== null)
{
$credit->setBillingAddress($this->duplicateAddress($billingAddress));
$this->updateInfo($credit->getBillingAddress(), $credit);
}
return true;
}
// This is used when creating a new invoice / credit for a client / mission
public function guessAddressesForFreeInvoice($invoice, $client, $mission)
{
// Intervention Address = mission.interventionAddress
// Billing Address = mission.billingAddress
// Fallback
// Intervention Address = client.interventionAddress
// Billing Address = client.billingAddress
// Intervention Address
// Intervention Address = mission.interventionAddress
$interventionAddress = null;
if ($mission !== null)
{
$interventionAddress = $mission->getInterventionAddress();
}
if ($interventionAddress === null)
{
// This should not happen
// Fallback on the intervention address from the client
if ($client !== null)
{
$interventionAddress = $client->getInterventionAddress();
}
}
if ($interventionAddress === null)
{
// This really should not happen
$interventionAddress = new Address();
$interventionAddress->setSocietyGroup($invoice->getSocietyGroup());
$this->em->persist($interventionAddress);
}
// Billing Address
// Billing Address = mission.billingAddress
$billingAddress = null;
if ($mission !== null)
{
$billingAddress = $mission->getBillingAddress();
}
if ($billingAddress === null)
{
// This should not happen
// Fallback on the billing address from the client
if ($client !== null)
{
$billingAddress = $client->getBillingAddress();
}
}
if ($billingAddress === null)
{
// This really should not happen
$billingAddress = new Address();
$billingAddress->setSocietyGroup($invoice->getSocietyGroup());
$this->em->persist($billingAddress);
}
if ($interventionAddress !== null)
{
$invoice->setInterventionAddress($this->duplicateAddress($interventionAddress));
$this->updateInfo($invoice->getInterventionAddress(), $invoice);
}
if ($billingAddress !== null)
{
$invoice->setBillingAddress($this->duplicateAddress($billingAddress));
$this->updateInfo($invoice->getBillingAddress(), $invoice);
}
return true;
}
public function getPossibleInterventionAddressesForMission($mission)
{
// What we need :
// mission.interventionAddress
// mission.receiver.allAddresses
// No duplicates
$addresses = array();
// mission.interventionAddress
if ($mission->getInterventionAddress() !== null)
{
$addresses[] = $mission->getInterventionAddress();
}
// devis.receiver.allAddresses
if ($mission->getReceiver() !== null)
{
foreach ($mission->getReceiver()->getAddresses() as $address)
{
$addresses[] = $address;
}
}
$addresses = $this->removeDuplicates($addresses);
return $addresses;
}
public function getPossibleBillingAddressesForMission($mission)
{
// What we need :
// mission.billingAddress
// mission.emitter.allAddresses
// No duplicates
$addresses = array();
// mission.billingAddress
if ($mission->getBillingAddress() !== null)
{
$addresses[] = $mission->getBillingAddress();
}
// mission.emitter.allAddresses
if ($mission->getEmitter() !== null)
{
foreach ($mission->getEmitter()->getAddresses() as $address)
{
$addresses[] = $address;
}
}
$addresses = $this->removeDuplicates($addresses);
return $addresses;
}
public function getPossibleInterventionAddressesForDevis($devis)
{
// What we need :
// devis.interventionAddress
// devis.mission.interventionAddress
// devis.receiver.allAddresses
// No duplicates
$addresses = array();
// devis.interventionAddress
if ($devis->getInterventionAddress() !== null)
{
$addresses[] = $devis->getInterventionAddress();
}
// devis.mission.interventionAddress
if ($devis->getMission() !== null && $devis->getMission()->getInterventionAddress() !== null)
{
$addresses[] = $devis->getMission()->getInterventionAddress();
}
// devis.receiver.allAddresses
if ($devis->getReceiver() !== null)
{
foreach ($devis->getReceiver()->getAddresses() as $address)
{
$addresses[] = $address;
}
}
$addresses = $this->removeDuplicates($addresses);
return $addresses;
}
public function getPossibleBillingAddressesForDevis($devis)
{
// What we need :
// devis.billingAddress
// devis.mission.billingAddress
// devis.emitter.allAddresses
// No duplicates
$addresses = array();
// devis.billingAddress
if ($devis->getBillingAddress() !== null)
{
$addresses[] = $devis->getBillingAddress();
}
// devis.mission.billingAddress
if ($devis->getMission() !== null && $devis->getMission()->getBillingAddress() !== null)
{
$addresses[] = $devis->getMission()->getBillingAddress();
}
// devis.emitter.allAddresses
if ($devis->getEmitter() !== null)
{
foreach ($devis->getEmitter()->getAddresses() as $address)
{
$addresses[] = $address;
}
}
$addresses = $this->removeDuplicates($addresses);
return $addresses;
}
public function getPossibleInterventionAddressesForInvoice($invoice)
{
// What we need :
// invoice.interventionAddress
// invoice.devis.interventionAddress
// invoice.mission.interventionAddress
// invoice.receiver.allAddresses
// No duplicates
$addresses = array();
// invoice.interventionAddress
if ($invoice->getInterventionAddress() !== null)
{
$addresses[] = $invoice->getInterventionAddress();
}
// invoice.devis.interventionAddress
foreach ($invoice->getDevisGroup() as $devis)
{
if ($devis->getInterventionAddress() !== null)
{
$addresses[] = $devis->getInterventionAddress();
}
}
// invoice.mission.interventionAddress
if ($invoice->getMission() !== null && $invoice->getMission()->getInterventionAddress() !== null)
{
$addresses[] = $invoice->getMission()->getInterventionAddress();
}
// invoice.receiver.allAddresses
if ($invoice->getReceiver() !== null)
{
foreach ($invoice->getReceiver()->getAddresses() as $address)
{
$addresses[] = $address;
}
}
$addresses = $this->removeDuplicates($addresses);
return $addresses;
}
public function getPossibleBillingAddressesForInvoice($invoice)
{
// What we need :
// invoice.billingAddress
// invoice.devis.billingAddress
// invoice.mission.billingAddress
// invoice.emitter.allAddresses
// No duplicates
$addresses = array();
// invoice.billingAddress
if ($invoice->getBillingAddress() !== null)
{
$addresses[] = $invoice->getBillingAddress();
}
// invoice.devis.billingAddress
foreach ($invoice->getDevisGroup() as $devis)
{
if ($devis->getBillingAddress() !== null)
{
$addresses[] = $devis->getBillingAddress();
}
}
// invoice.mission.billingAddress
if ($invoice->getMission() !== null && $invoice->getMission()->getBillingAddress() !== null)
{
$addresses[] = $invoice->getMission()->getBillingAddress();
}
// invoice.emitter.allAddresses
if ($invoice->getEmitter() !== null)
{
foreach ($invoice->getEmitter()->getAddresses() as $address)
{
$addresses[] = $address;
}
}
$addresses = $this->removeDuplicates($addresses);
return $addresses;
}
public function removeDuplicates($tempAddresses)
{
$addresses = array();
foreach ($tempAddresses as $tempAddress)
{
$add = true;
foreach ($addresses as $key => $address)
{
if ($address->equalsInValue($tempAddress))
{
$display = $address->getFormDisplay()." / ".$tempAddress->getInfo();
$addresses[$key]->setFormDisplay($display);
//$tempAddress->setFormDisplay($display);
$add = false;
break;
}
}
if ($add)
{
$tempAddress->setFormDisplay($tempAddress->getInfo());
$addresses[] = $tempAddress;
}
}
return $addresses;
}
public function duplicateAddress($address)
{
if ($address === null)
return null;
$a = new Address();
$a->setName($address->getName());
$a->setAddress($address->getAddress());
$a->setComplement($address->getComplement());
$a->setPostalCode($address->getPostalCode());
$a->setTown($address->getTown());
$a->setCountry($address->getCountry());
$a->setLatitude($address->getLatitude());
$a->setLongitude($address->getLongitude());
$a->setEmail($address->getEmail());
$a->setSocietyGroup($address->getSocietyGroup());
return $a;
}
public function copyAddress($source, $destination)
{
if ($source === null)
return null;
if ($destination === null)
return null;
$destination->setName($source->getName());
$destination->setAddress($source->getAddress());
$destination->setComplement($source->getComplement());
$destination->setPostalCode($source->getPostalCode());
$destination->setTown($source->getTown());
$destination->setCountry($source->getCountry());
$destination->setLatitude($source->getLatitude());
$destination->setLongitude($source->getLongitude());
$destination->setEmail($source->getEmail());
$destination->setSocietyGroup($source->getSocietyGroup());
return $destination;
}
public function getDepartmentFromAddress($address)
{
if ($address === null)
return null;
$postalCode = $address->getPostalCode();
if ($postalCode === null)
return null;
$deptRepository = $this->em->getRepository(Department::class);
// Get both the long and the sort version
$department = $deptRepository->findOneByPostalCode(substr($postalCode,0,2));
return $department;
}
public function computeAddressChanges($address, $changesSet)
{
if (count($changesSet) < 1)
return null;
$propertyAccessor = PropertyAccess::createPropertyAccessor();
$old = array();
$new = array();
$changesToWatch = array("address", "complement", "postalCode", "town", "country");
foreach ($changesSet as $key => $change)
{
if (in_array($key, $changesToWatch) && $change[1] != null)
{
$old[$key] = $change[0];
$new[$key] = $change[1];
}
}
$oldValue = "";
$newValue = "";
foreach ($changesToWatch as $key => $property)
{
if (array_key_exists($property, $old))
{
$oldValue .= $old[$property];
$newValue .= $new[$property];
}
else
{
$oldValue .= $propertyAccessor->getValue($address, $property);
$newValue .= $propertyAccessor->getValue($address, $property);
}
if ($key == 2)
{
$oldValue = substr($oldValue, 0, -1);
$newValue = substr($newValue, 0, -1);
$oldValue .= ", ";
$newValue .= ", ";
}
else
{
$oldValue .= " ";
$newValue .= " ";
}
}
// Did Something change ?
if ($oldValue === $newValue)
{
return null;
}
return array(
"old" => $oldValue,
"new" => $newValue,
);
}
public function isValid($address)
{
if($address === null)
return false;
if(empty($address->getAddress()))
return false;
if(empty($address->getPostalCode()))
return false;
if(empty($address->getTown()))
return false;
return true;
}
public function getSetLatLng(&$address, $noflush = null)
{
// $backTrace = debug_backtrace()[1];
// $method = $backTrace['function'];
// $class = $backTrace['class'];
// $trace = $class."::".$method;
// $trace = str_replace("\\", "::", $trace);
// $this->logTools->ploopLog("[AddressTools][getSetLatLng] trace = ".$trace);
$societyGroup = $address->getSocietyGroup();
if ($societyGroup === null)
{
// $this->logTools->ploopLog("[AddressTools][getSetLatLng][Die.Hard] SocietyGroup is null");
return false;
}
$codeInactive = $this->moduleTools->isInactiveByCode($societyGroup, Module::MODULE_MAPS);
if ($codeInactive)
{
// $this->logTools->ploopLog("[AddressTools][getSetLatLng][Die.Hard] Cannot find MODULE_MAPS");
return false;
}
$APIKey = $this->em->getRepository(Config::class)->findOneByName(Config::API_GOOGLE_MAPS_KEY);
if ($APIKey === null)
{
// $this->logTools->ploopLog("[AddressTools][getSetLatLng][Die.Hard] Cannot find Api Key");
return false;
}
$APIKey = $APIKey->getValue();
//Call api to retrieve lat/lng
// google map geocode api url
$url = "https://maps.googleapis.com/maps/api/geocode/json?address=" .
urlencode(
$address->getAddress() .
" " . $address->getComplement() .
" " . $address->getPostalCode() .
" " . $address->getTown()
) .
"&key=" . $APIKey;
// get the json response
$ch = curl_init();
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
// Set the url
curl_setopt($ch, CURLOPT_URL,$url);
// Execute
$result = curl_exec($ch);
// Closing
curl_close($ch);
// decode the json
$resp = json_decode($result, true);
// only increment the counter if we have a valid response
if($resp['status'] == 'OK' || $resp['status'] == 'ZERO_RESULTS')
{
$address->incrementApiCounter();
}
// response status will be 'OK', if able to geocode given address
if($resp['status'] == 'OK')
{
// get the important data
$lat = isset($resp['results'][0]['geometry']['location']['lat']) ? $resp['results'][0]['geometry']['location']['lat'] : "";
$lng = isset($resp['results'][0]['geometry']['location']['lng']) ? $resp['results'][0]['geometry']['location']['lng'] : "";
$formatted_address = isset($resp['results'][0]['formatted_address']) ? $resp['results'][0]['formatted_address'] : "";
// verify if data is complete
if ($lat && $lng)
{
// Set address lat / lng
$address->setLatitude($lat);
$address->setLongitude($lng);
//echo $lat . " / " . $lng . "\n";
if ($noflush === null)
{
$this->em->persist($address);
try
{
$this->em->flush();
}
catch (\Exception $e)
{
$this->logTools->errorlog($e->getMessage());
$this->logTools->errorlog_db($e);
}
}
$args = array(
"address" => $address,
"success" => true,
);
// $this->logTools->ploopLog("[AddressTools][getSetLatLng] All is peachy");
return true;
}
else
{
$args = array(
"address" => $address,
"success" => false,
);
// $this->logTools->ploopLog("[AddressTools][getSetLatLng][Die.Hard] Api failed miserably");
return false;
}
}
else // Fail to get the Lat/Lng : Try with CP alone ?
{
$args = array(
"address" => $address,
"success" => false,
);
// $this->logTools->ploopLog("[AddressTools][getSetLatLng][Die.Hard] Api failed miserably again");
return false;
}
}
// Plan.io Task #4188
public function getAllForClient(Client $client)
{
if ($client->getIndividual() !== null)
return $this->getAllForIndividual($client->getIndividual());
if ($client->getStore() !== null)
return $this->getAllForStore($client->getStore());
return null;
}
// Plan.io Task #4188
public function getAllForIndividual(Individual $individual)
{
$addresses = array();
// Billing first
if ($individual->getBillingAddress() !== null)
{
array_push($addresses, $individual->getBillingAddress());
}
// Fallback on simple address
if ($individual->getAddress() !== null)
{
array_push($addresses, $individual->getAddress());
}
return $addresses;
}
// Plan.io Task #4188
public function getAllForStore(Store $store)
{
$addresses = array();
if ($store->getBillingAddress() !== null)
{
array_push($addresses, $store->getBillingAddress());
}
// Get normal addres too
if ($store->getAddress() !== null)
{
array_push($addresses, $store->getAddress());
}
return $addresses;
}
// Plan.io Task #3621 #4346
public function extractAddressesFromServiceOrderUpdate(SocietyGroup $societyGroup, $decodedAddressData)
{
// $this->logTools->ploopLog("[AddressTools][extractAddressesFromServiceOrderUpdate]");
if (empty($decodedAddressData))
{
return array(
'addresses' => [],
'interventionAddress' => null,
'billingAddress' => null,
'otherAddress' => [],
);
}
$interventionAddress = null;
$billingAddress = null;
$otherAddress = array();
$addresses = array();
foreach ($decodedAddressData as $key => $addressData)
{
$address = $this->craftAddressForServiceOrder($addressData, $societyGroup);
$code = null;
if (array_key_exists('code', $addressData) && !empty($addressData['code']))
{
$code = $addressData['code'];
if ($code == 'DELIVERY')
{
// $this->logTools->ploopLog("[AddressTools][extractAddressesFromServiceOrderUpdate] DELIVERY address found : ");
// $this->logTools->ploopLog("[AddressTools][extractAddressesFromServiceOrderUpdate] ".$address->displayForLog());
$interventionAddress = $address;
$addresses[] = $address;
}
else
{
if ($code == 'INVOICE')
{
// $this->logTools->ploopLog("[AddressTools][extractAddressesFromServiceOrderUpdate] INVOICE address found : ");
// $this->logTools->ploopLog("[AddressTools][extractAddressesFromServiceOrderUpdate] ".$address->displayForLog());
$billingAddress = $address;
$addresses[] = $address;
}
else
{
// $this->logTools->ploopLog("[AddressTools][extractAddressesFromServiceOrderUpdate] Other address found : ");
// $this->logTools->ploopLog("[AddressTools][extractAddressesFromServiceOrderUpdate] ".$address->displayForLog());
$otherAddress[] = $address;
$addresses[] = $address;
}
}
}
}
foreach ($addresses as $address)
{
// Plan.io Task #4163
// SocietyGroup is needed when fetching the latitude and longitude (to check module activation)
$address->setSocietyGroup($societyGroup);
$this->getSetLatLng($address, "no persist, no flush"); // explicit call to get latitude and longitude
$this->em->persist($address);
}
return array(
'addresses' => $addresses,
'interventionAddress' => $interventionAddress,
'billingAddress' => $billingAddress,
'otherAddress' => $otherAddress,
);
}
}