src/Services/Location/AddressTools.php line 26

Open in your IDE?
  1. <?php
  2. //----------------------------------------------------------------------
  3. // src/Services/Location/AddressTools.php
  4. //----------------------------------------------------------------------
  5. namespace App\Services\Location;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. use Symfony\Contracts\Translation\TranslatorInterface;
  8. use Symfony\Component\PropertyAccess\PropertyAccess;
  9. use App\Entity\Client\Client;
  10. use App\Entity\Client\Individual;
  11. use App\Entity\Client\Store;
  12. use App\Entity\Config\Config;
  13. use App\Entity\Config\Module;
  14. use App\Entity\Config\ModuleConfig;
  15. use App\Entity\Location\Address;
  16. use App\Entity\Location\Department;
  17. use App\Entity\Platform\Phone;
  18. use App\Entity\SocietyGroup;
  19. use App\Services\Config\ModuleTools;
  20. use App\Services\LogTools;
  21. class AddressTools
  22. {
  23.     public function __construct(ManagerRegistry $doctrineTranslatorInterface $translatorLogTools $logToolsModuleTools $moduleTools)
  24.     {
  25.         $this->em $doctrine->getManager();
  26.         $this->translator $translator;
  27.         $this->logTools $logTools;
  28.         $this->moduleTools $moduleTools;
  29.         $this->debug true;
  30.     }
  31.     // Plan.io Task #3621
  32.     public function handleAddressDataForServiceOrder($decodedAddressDataSocietyGroup $societyGroupIndividual $individual)
  33.     {
  34.         if (empty($decodedAddressData))
  35.         {
  36.             return array(
  37.                 'addresses'        =>    array(),
  38.                 'phones'        =>    array(),
  39.             );
  40.         }
  41.         $addresses = array();
  42.         $phones = array();
  43.         $parsedAddressData $this->extractAddressesFromServiceOrderUpdate($societyGroup$decodedAddressData);
  44.         $interventionAddress $parsedAddressData['interventionAddress'];
  45.         $billingAddress $parsedAddressData['billingAddress'];
  46.         $otherAddress $parsedAddressData['otherAddress'];
  47.         $addresses $parsedAddressData['addresses'];
  48.         if ($interventionAddress !== null)
  49.         {
  50.             $individual->setAddress($interventionAddress);
  51.             $this->updateInfo($individual->getAddress());
  52.         }
  53.         if ($billingAddress !== null)
  54.         {
  55.             $individual->setBillingAddress($billingAddress);
  56.             $this->updateInfo($individual->getBillingAddress());
  57.         }
  58.         foreach ($otherAddress as $address)
  59.         {
  60.             // Attach the next ones to the Client
  61.             $individual->getClient()->addAddress($address);
  62.         }
  63.         $phoneData $this->craftPhoneForServiceOrder($decodedAddressData$societyGroup$individual);
  64.         foreach ($phoneData as $phone)
  65.         {
  66.             $phones[] = $phone;
  67.         }
  68.         foreach ($phones as $phone)
  69.         {
  70.             $this->em->persist($phone);
  71.         }
  72.         return array(
  73.             'addresses'        =>    $addresses,
  74.             'phones'        =>    $phones,
  75.         );
  76.     }
  77.     // Plan.io Task #3621
  78.     public function craftPhoneForServiceOrder($decodedAddressDataSocietyGroup $societyGroupIndividual $individual)
  79.     {
  80.         // $this->logTools->ploopLog("[AddressTools][craftPhoneForServiceOrder] ".print_r($decodedAddressData, true));
  81.         $phones = array();
  82.         foreach ($decodedAddressData as $key => $addressData)
  83.         {
  84.             if (array_key_exists('primaryPhone'$addressData) && !empty($addressData['primaryPhone']))
  85.             {
  86.                 $phone = new Phone();
  87.                 $phone->setSocietyGroup($societyGroup);
  88.                 $phone->setNumber($addressData['primaryPhone']);
  89.                 // Attach to individual
  90.                 $individual->addPhone($phone);
  91.                 $phones[] = $phone;
  92.                 // $this->logTools->ploopLog("[AddressTools][craftPhoneForServiceOrder] primaryPhone = ".$addressData['primaryPhone']);
  93.             }
  94.             if (array_key_exists('secondaryPhone'$addressData) && !empty($addressData['secondaryPhone']))
  95.             {
  96.                 $phone = new Phone();
  97.                 $phone->setSocietyGroup($societyGroup);
  98.                 $phone->setNumber($addressData['secondaryPhone']);
  99.                 // Attach to individual
  100.                 $individual->addPhone($phone);
  101.                 $phones[] = $phone;
  102.                 // $this->logTools->ploopLog("[AddressTools][craftPhoneForServiceOrder] secondaryPhone = ".$addressData['secondaryPhone']);
  103.             }
  104.             if (array_key_exists('mobilePhone'$addressData) && !empty($addressData['mobilePhone']))
  105.             {
  106.                 $phone = new Phone();
  107.                 $phone->setSocietyGroup($societyGroup);
  108.                 $phone->setNumber($addressData['mobilePhone']);
  109.                 // Attach to individual
  110.                 $individual->addPhone($phone);
  111.                 $phones[] = $phone;
  112.                 // $this->logTools->ploopLog("[AddressTools][craftPhoneForServiceOrder] mobilePhone = ".$addressData['mobilePhone']);
  113.             }
  114.         }
  115.         return $phones;
  116.     }
  117.     // Plan.io Task #3621
  118.     public function craftAddressForServiceOrder($addressDataSocietyGroup $societyGroup)
  119.     {
  120.         // $this->logTools->ploopLog("[AddressTools][craftAddressForServiceOrder] societyGroup = ".$societyGroup->displayForLog());
  121.         $address = new Address();
  122.         $address->setSocietyGroup($societyGroup);
  123.         if (array_key_exists('street'$addressData) && !empty($addressData['street']))
  124.         {
  125.             $address->setAddress($addressData['street']);
  126.         }
  127.         $complement "";
  128.         if (array_key_exists('address1'$addressData) && !empty($addressData['address1']))
  129.         {
  130.             $complement .= $addressData['address1'];
  131.             $complement .= " ";
  132.         }
  133.         if (array_key_exists('address2'$addressData) && !empty($addressData['address2']))
  134.         {
  135.             $complement .= $addressData['address2'];
  136.             $complement .= " ";
  137.         }
  138.         if (array_key_exists('address3'$addressData) && !empty($addressData['address3']))
  139.         {
  140.             $complement .= $addressData['address3'];
  141.             $complement .= " ";
  142.         }
  143.         if (array_key_exists('address4'$addressData) && !empty($addressData['address4']))
  144.         {
  145.             $complement .= $addressData['address4'];
  146.             $complement .= " ";
  147.         }
  148.         if (array_key_exists('address5'$addressData) && !empty($addressData['address5']))
  149.         {
  150.             $complement .= $addressData['address5'];
  151.             $complement .= " ";
  152.         }
  153.         if (!empty($complement))
  154.         {
  155.             $address->setComplement($complement);
  156.         }
  157.         if (array_key_exists('postalCode'$addressData) && !empty($addressData['postalCode']))
  158.         {
  159.             $address->setPostalCode($addressData['postalCode']);
  160.         }
  161.         if (array_key_exists('city'$addressData) && !empty($addressData['city']))
  162.         {
  163.             $address->setTown($addressData['city']);
  164.         }
  165.         return $address;
  166.     }
  167.     public function updateAddressData($client)
  168.     {
  169.         if ($client === null)
  170.         {
  171.             return false;
  172.         }
  173.         $individual $client->getIndividual();
  174.         if ($individual !== null)
  175.         {
  176.             // Reset
  177.             $addresses $client->getAddresses();
  178.             foreach ($addresses as $address)
  179.             {
  180.                 $address->setIntervention(0);
  181.                 $address->setBilling(0);
  182.                 $this->updateInfo($address);
  183.                 //$this->em->persist($address);
  184.             }
  185.             if ($individual->getAddress() !== null)
  186.             {
  187.                 $individual->getAddress()->setIntervention(1);
  188.                 $this->updateInfo($individual->getAddress());
  189.             }
  190.             if ($individual->getBillingAddress() !== null)
  191.             {
  192.                 $individual->getBillingAddress()->setBilling(1);
  193.                 $this->updateInfo($individual->getBillingAddress());
  194.             }
  195.             return true;
  196.         }
  197.         return false;
  198.     }
  199.     public function updateInfo($address$object null)
  200.     {
  201.         if ($address === null)
  202.         {
  203.             return false;
  204.         }
  205.         if ($object !== null)
  206.         {
  207.             $address->setObjectRef($object->getRef());
  208.             if ($address->getSocietyGroup() === null)
  209.             {
  210.                 $address->setSocietyGroup($object->getSocietyGroup());
  211.             }
  212.         }
  213.         $info "";
  214.         $infoFull "";
  215.         if ($address->getIntervention() == 1)
  216.         {
  217.             $info $this->translator->trans("intervention_address_short");
  218.             $infoFull $this->translator->trans("intervention_address_for");
  219.         }
  220.         else
  221.         {
  222.             if ($address->getBilling() == 1)
  223.             {
  224.                 $info $this->translator->trans("billing_address_short");
  225.                 $infoFull $this->translator->trans("billing_address_for");
  226.             }
  227.             else
  228.             {
  229.                 $info "";
  230.                 $infoFull $this->translator->trans("address_for");
  231.             }
  232.         }
  233.         if (!empty($address->getObjectRef()))
  234.         {
  235.             $info $address->getObjectRef() . ", " $info;
  236.             $infoFull $infoFull $address->getObjectRef();
  237.         }
  238.         else
  239.         {
  240.             // No ref => No full info
  241.             $infoFull null;
  242.         }
  243.         if ($info == "")
  244.         {
  245.             $info null;
  246.         }
  247.         $address->setInfo($info);
  248.         $address->setInfoFull($infoFull);
  249.         return true;
  250.     }
  251.     public function guessBillingAddressForMission($mission)
  252.     {
  253.         // Emitter ?
  254.         $client $mission->getEmitter();
  255.         if ($client === null)
  256.             return null;
  257.         $billingAddress $client->getBillingAddress();
  258.         if ($billingAddress === null)
  259.         {
  260.             $billingAddress $client->getAddress();
  261.         }
  262.         if ($billingAddress !== null)
  263.         {
  264.             $mission->setBillingAddress($this->duplicateAddress($billingAddress));
  265.             return $mission->getBillingAddress();
  266.         }
  267.         return null;
  268.     }
  269.     public function guessBillingAddressForDevis($devis)
  270.     {
  271.         // Emitter ?
  272.         $client $devis->getEmitter();
  273.         if ($client === null)
  274.             return null;
  275.         $billingAddress $client->getBillingAddress();
  276.         if ($billingAddress === null)
  277.         {
  278.             $billingAddress $client->getAddress();
  279.         }
  280.         if ($billingAddress !== null)
  281.         {
  282.             $devis->setBillingAddress($this->duplicateAddress($billingAddress));
  283.             return $devis->getBillingAddress();
  284.         }
  285.         return null;
  286.     }
  287.     // This is used when creating a new devis
  288.     public function guessAddressesForDevis($devis)
  289.     {
  290.         // Intervention Address = devis.mission.interventionAddress
  291.         // Billing Address = devis.emitter.billingAddress
  292.         // Intervention Address
  293.         // Intervention Address = devis.mission.interventionAddress
  294.         $mission $devis->getMission();
  295.         $interventionAddress null;
  296.         if ($mission !== null)
  297.         {
  298.             if ($mission->getInterventionAddress() !== null)
  299.             {
  300.                 $interventionAddress $mission->getInterventionAddress();
  301.             }
  302.         }
  303.         if ($interventionAddress === null)
  304.         {
  305.             // This should not happen
  306.             // Fallback on the intervention address from the receiver
  307.             if ($devis->getReceiver() !== null)
  308.             {
  309.                 $interventionAddress $devis->getReceiver()->getInterventionAddress();
  310.             }
  311.         }
  312.         if ($interventionAddress === null)
  313.         {
  314.             // This really should not happen
  315.             $interventionAddress = new Address();
  316.             $interventionAddress->setSocietyGroup($devis->getSocietyGroup());
  317.             $this->em->persist($interventionAddress);
  318.         }
  319.         // Billing Address
  320.         // Billing Address = devis.emitter.billingAddress
  321.         $emitter $devis->getEmitter();
  322.         $billingAddress $emitter->getBillingAddress();
  323.         if ($billingAddress === null)
  324.         {
  325.             // This should not happen
  326.             // Fallback on the billing address from the mission
  327.             if ($mission !== null)
  328.             {
  329.                 $billingAddress $mission->getBillingAddress();
  330.             }
  331.         }
  332.         if ($billingAddress === null)
  333.         {
  334.             // This really should not happen
  335.             $billingAddress = new Address();
  336.             $billingAddress->setSocietyGroup($devis->getSocietyGroup());
  337.             $this->em->persist($billingAddress);
  338.         }
  339.         if ($interventionAddress !== null)
  340.         {
  341.             $devis->setInterventionAddress($this->duplicateAddress($interventionAddress));
  342.             $this->updateInfo($devis->getInterventionAddress(), $devis);
  343.         }
  344.         if ($billingAddress !== null)
  345.         {
  346.             $devis->setBillingAddress($this->duplicateAddress($billingAddress));
  347.             $this->updateInfo($devis->getBillingAddress(), $devis);
  348.         }
  349.         return true;
  350.     }
  351.     // This is used when creating a new invoice from a devis
  352.     public function guessAddressesForInvoiceFromDevis($invoice$devis)
  353.     {
  354.         if ($devis === null)
  355.         {
  356.             return false;
  357.         }
  358.         // Intervention Address = devis.interventionAddress
  359.         // Billing Address = devis.billingAddress
  360.         // Intervention Address
  361.         // Intervention Address = devis.interventionAddress
  362.         $interventionAddress null;
  363.         if ($devis->getInterventionAddress() !== null)
  364.         {
  365.             $interventionAddress $devis->getInterventionAddress();
  366.         }
  367.         if ($interventionAddress === null)
  368.         {
  369.             // This should not happen
  370.             // Fallback on the intervention address from the mission
  371.             if ($devis->getMission() !== null)
  372.             {
  373.                 $interventionAddress $devis->getMission()->getInterventionAddress();
  374.             }
  375.         }
  376.         if ($interventionAddress === null)
  377.         {
  378.             // This should not happen
  379.             // Fallback on the intervention address from the receiver
  380.             if ($devis->getReceiver() !== null)
  381.             {
  382.                 $interventionAddress $devis->getReceiver()->getInterventionAddress();
  383.             }
  384.         }
  385.         if ($interventionAddress === null)
  386.         {
  387.             // This really should not happen
  388.             $interventionAddress = new Address();
  389.             $interventionAddress->setSocietyGroup($devis->getSocietyGroup());
  390.             $this->em->persist($interventionAddress);
  391.         }
  392.         // Billing Address
  393.         // Billing Address = devis.billingAddress
  394.         $billingAddress $devis->getBillingAddress();
  395.         if ($billingAddress === null)
  396.         {
  397.             // This should not happen
  398.             // Fallback on the billing address from the mission
  399.             if ($devis->getMission() !== null)
  400.             {
  401.                 $billingAddress $devis->getMission()->getBillingAddress();
  402.             }
  403.         }
  404.         if ($interventionAddress === null)
  405.         {
  406.             // This should not happen
  407.             // Fallback on the intervention address from the emitter
  408.             if ($devis->getEmitter() !== null)
  409.             {
  410.                 $interventionAddress $devis->getEmitter()->getBillingAddress();
  411.             }
  412.         }
  413.         if ($billingAddress === null)
  414.         {
  415.             // This really should not happen
  416.             $billingAddress = new Address();
  417.             $billingAddress->setSocietyGroup($invoice->getSocietyGroup());
  418.             $this->em->persist($billingAddress);
  419.         }
  420.         if ($interventionAddress !== null)
  421.         {
  422.             $invoice->setInterventionAddress($this->duplicateAddress($interventionAddress));
  423.             $this->updateInfo($invoice->getInterventionAddress(), $invoice);
  424.         }
  425.         if ($billingAddress !== null)
  426.         {
  427.             $invoice->setBillingAddress($this->duplicateAddress($billingAddress));
  428.             $this->updateInfo($invoice->getBillingAddress(), $invoice);
  429.         }
  430.         return true;
  431.     }
  432.     // This is used when creating a new credit from an invoice
  433.     public function guessAddressesForCreditFromInvoice($credit$invoice)
  434.     {
  435.         if ($invoice === null)
  436.         {
  437.             return false;
  438.         }
  439.         // Intervention Address = invoice.interventionAddress
  440.         // Billing Address = invoice.billingAddress
  441.         // Intervention Address
  442.         // Intervention Address = invoice.interventionAddress
  443.         $interventionAddress null;
  444.         if ($invoice->getInterventionAddress() !== null)
  445.         {
  446.             $interventionAddress $invoice->getInterventionAddress();
  447.         }
  448.         if ($interventionAddress === null)
  449.         {
  450.             // This should not happen
  451.             // Fallback on the intervention address from the mission
  452.             if ($credit->getMission() !== null)
  453.             {
  454.                 $interventionAddress $credit->getMission()->getInterventionAddress();
  455.             }
  456.         }
  457.         if ($interventionAddress === null)
  458.         {
  459.             // This should not happen
  460.             // Fallback on the intervention address from the receiver
  461.             if ($credit->getReceiver() !== null)
  462.             {
  463.                 $interventionAddress $credit->getReceiver()->getInterventionAddress();
  464.             }
  465.         }
  466.         if ($interventionAddress === null)
  467.         {
  468.             // This really should not happen
  469.             $interventionAddress = new Address();
  470.             $interventionAddress->setSocietyGroup($credit->getSocietyGroup());
  471.             $this->em->persist($interventionAddress);
  472.         }
  473.         // Billing Address
  474.         // Billing Address = devis.billingAddress
  475.         $billingAddress $invoice->getBillingAddress();
  476.         if ($billingAddress === null)
  477.         {
  478.             // This should not happen
  479.             // Fallback on the billing address from the mission
  480.             if ($credit->getMission() !== null)
  481.             {
  482.                 $billingAddress $credit->getMission()->getBillingAddress();
  483.             }
  484.         }
  485.         if ($interventionAddress === null)
  486.         {
  487.             // This should not happen
  488.             // Fallback on the intervention address from the emitter
  489.             if ($credit->getEmitter() !== null)
  490.             {
  491.                 $interventionAddress $credit->getEmitter()->getBillingAddress();
  492.             }
  493.         }
  494.         if ($billingAddress === null)
  495.         {
  496.             // This really should not happen
  497.             $billingAddress = new Address();
  498.             $billingAddress->setSocietyGroup($credit->getSocietyGroup());
  499.             $this->em->persist($billingAddress);
  500.         }
  501.         if ($interventionAddress !== null)
  502.         {
  503.             $credit->setInterventionAddress($this->duplicateAddress($interventionAddress));
  504.             $this->updateInfo($credit->getInterventionAddress(), $credit);
  505.         }
  506.         if ($billingAddress !== null)
  507.         {
  508.             $credit->setBillingAddress($this->duplicateAddress($billingAddress));
  509.             $this->updateInfo($credit->getBillingAddress(), $credit);
  510.         }
  511.         return true;
  512.     }
  513.     // This is used when creating a new invoice / credit for a client / mission
  514.     public function guessAddressesForFreeInvoice($invoice$client$mission)
  515.     {
  516.         // Intervention Address = mission.interventionAddress
  517.         // Billing Address = mission.billingAddress
  518.         // Fallback
  519.         //         Intervention Address = client.interventionAddress
  520.         //         Billing Address = client.billingAddress
  521.         // Intervention Address
  522.         // Intervention Address = mission.interventionAddress
  523.         $interventionAddress null;
  524.         if ($mission !== null)
  525.         {
  526.             $interventionAddress $mission->getInterventionAddress();
  527.         }
  528.         if ($interventionAddress === null)
  529.         {
  530.             // This should not happen
  531.             // Fallback on the intervention address from the client
  532.             if ($client !== null)
  533.             {
  534.                 $interventionAddress $client->getInterventionAddress();
  535.             }
  536.         }
  537.         if ($interventionAddress === null)
  538.         {
  539.             // This really should not happen
  540.             $interventionAddress = new Address();
  541.             $interventionAddress->setSocietyGroup($invoice->getSocietyGroup());
  542.             $this->em->persist($interventionAddress);
  543.         }
  544.         // Billing Address
  545.         // Billing Address = mission.billingAddress
  546.         $billingAddress null;
  547.         if ($mission !== null)
  548.         {
  549.             $billingAddress $mission->getBillingAddress();
  550.         }
  551.         if ($billingAddress === null)
  552.         {
  553.             // This should not happen
  554.             // Fallback on the billing address from the client
  555.             if ($client !== null)
  556.             {
  557.                 $billingAddress $client->getBillingAddress();
  558.             }
  559.         }
  560.         if ($billingAddress === null)
  561.         {
  562.             // This really should not happen
  563.             $billingAddress = new Address();
  564.             $billingAddress->setSocietyGroup($invoice->getSocietyGroup());
  565.             $this->em->persist($billingAddress);
  566.         }
  567.         if ($interventionAddress !== null)
  568.         {
  569.             $invoice->setInterventionAddress($this->duplicateAddress($interventionAddress));
  570.             $this->updateInfo($invoice->getInterventionAddress(), $invoice);
  571.         }
  572.         if ($billingAddress !== null)
  573.         {
  574.             $invoice->setBillingAddress($this->duplicateAddress($billingAddress));
  575.             $this->updateInfo($invoice->getBillingAddress(), $invoice);
  576.         }
  577.         return true;
  578.     }
  579.     public function getPossibleInterventionAddressesForMission($mission)
  580.     {
  581.         // What we need :
  582.         //        mission.interventionAddress
  583.         //        mission.receiver.allAddresses
  584.         // No duplicates
  585.         $addresses = array();
  586.         // mission.interventionAddress
  587.         if ($mission->getInterventionAddress() !== null)
  588.         {
  589.             $addresses[] = $mission->getInterventionAddress();
  590.         }
  591.         // devis.receiver.allAddresses
  592.         if ($mission->getReceiver() !== null)
  593.         {
  594.             foreach ($mission->getReceiver()->getAddresses() as $address)
  595.             {
  596.                 $addresses[] = $address;
  597.             }
  598.         }
  599.         $addresses $this->removeDuplicates($addresses);
  600.         return $addresses;
  601.     }
  602.     public function getPossibleBillingAddressesForMission($mission)
  603.     {
  604.         // What we need :
  605.         //        mission.billingAddress
  606.         //        mission.emitter.allAddresses
  607.         // No duplicates
  608.         $addresses = array();
  609.         // mission.billingAddress
  610.         if ($mission->getBillingAddress() !== null)
  611.         {
  612.             $addresses[] = $mission->getBillingAddress();
  613.         }
  614.         // mission.emitter.allAddresses
  615.         if ($mission->getEmitter() !== null)
  616.         {
  617.             foreach ($mission->getEmitter()->getAddresses() as $address)
  618.             {
  619.                 $addresses[] = $address;
  620.             }
  621.         }
  622.         $addresses $this->removeDuplicates($addresses);
  623.         return $addresses;
  624.     }
  625.     public function getPossibleInterventionAddressesForDevis($devis)
  626.     {
  627.         // What we need :
  628.         //        devis.interventionAddress
  629.         //        devis.mission.interventionAddress
  630.         //        devis.receiver.allAddresses
  631.         // No duplicates
  632.         $addresses = array();
  633.         // devis.interventionAddress
  634.         if ($devis->getInterventionAddress() !== null)
  635.         {
  636.             $addresses[] = $devis->getInterventionAddress();
  637.         }
  638.         // devis.mission.interventionAddress
  639.         if ($devis->getMission() !== null && $devis->getMission()->getInterventionAddress() !== null)
  640.         {
  641.             $addresses[] = $devis->getMission()->getInterventionAddress();
  642.         }
  643.         // devis.receiver.allAddresses
  644.         if ($devis->getReceiver() !== null)
  645.         {
  646.             foreach ($devis->getReceiver()->getAddresses() as $address)
  647.             {
  648.                 $addresses[] = $address;
  649.             }
  650.         }
  651.         $addresses $this->removeDuplicates($addresses);
  652.         return $addresses;
  653.     }
  654.     public function getPossibleBillingAddressesForDevis($devis)
  655.     {
  656.         // What we need :
  657.         //        devis.billingAddress
  658.         //        devis.mission.billingAddress
  659.         //        devis.emitter.allAddresses
  660.         // No duplicates
  661.         $addresses = array();
  662.         // devis.billingAddress
  663.         if ($devis->getBillingAddress() !== null)
  664.         {
  665.             $addresses[] = $devis->getBillingAddress();
  666.         }
  667.         // devis.mission.billingAddress
  668.         if ($devis->getMission() !== null && $devis->getMission()->getBillingAddress() !== null)
  669.         {
  670.             $addresses[] = $devis->getMission()->getBillingAddress();
  671.         }
  672.         // devis.emitter.allAddresses
  673.         if ($devis->getEmitter() !== null)
  674.         {
  675.             foreach ($devis->getEmitter()->getAddresses() as $address)
  676.             {
  677.                 $addresses[] = $address;
  678.             }
  679.         }
  680.         $addresses $this->removeDuplicates($addresses);
  681.         return $addresses;
  682.     }
  683.     public function getPossibleInterventionAddressesForInvoice($invoice)
  684.     {
  685.         // What we need :
  686.         //        invoice.interventionAddress
  687.         //        invoice.devis.interventionAddress
  688.         //        invoice.mission.interventionAddress
  689.         //        invoice.receiver.allAddresses
  690.         // No duplicates
  691.         $addresses = array();
  692.         // invoice.interventionAddress
  693.         if ($invoice->getInterventionAddress() !== null)
  694.         {
  695.             $addresses[] = $invoice->getInterventionAddress();
  696.         }
  697.         // invoice.devis.interventionAddress
  698.         foreach ($invoice->getDevisGroup() as $devis)
  699.         {
  700.             if ($devis->getInterventionAddress() !== null)
  701.             {
  702.                 $addresses[] = $devis->getInterventionAddress();
  703.             }
  704.         }
  705.         // invoice.mission.interventionAddress
  706.         if ($invoice->getMission() !== null && $invoice->getMission()->getInterventionAddress() !== null)
  707.         {
  708.             $addresses[] = $invoice->getMission()->getInterventionAddress();
  709.         }
  710.         // invoice.receiver.allAddresses
  711.         if ($invoice->getReceiver() !== null)
  712.         {
  713.             foreach ($invoice->getReceiver()->getAddresses() as $address)
  714.             {
  715.                 $addresses[] = $address;
  716.             }
  717.         }
  718.         $addresses $this->removeDuplicates($addresses);
  719.         return $addresses;
  720.     }
  721.     public function getPossibleBillingAddressesForInvoice($invoice)
  722.     {
  723.         // What we need :
  724.         //        invoice.billingAddress
  725.         //        invoice.devis.billingAddress
  726.         //        invoice.mission.billingAddress
  727.         //        invoice.emitter.allAddresses
  728.         // No duplicates
  729.         $addresses = array();
  730.         // invoice.billingAddress
  731.         if ($invoice->getBillingAddress() !== null)
  732.         {
  733.             $addresses[] = $invoice->getBillingAddress();
  734.         }
  735.         // invoice.devis.billingAddress
  736.         foreach ($invoice->getDevisGroup() as $devis)
  737.         {
  738.             if ($devis->getBillingAddress() !== null)
  739.             {
  740.                 $addresses[] = $devis->getBillingAddress();
  741.             }
  742.         }
  743.         // invoice.mission.billingAddress
  744.         if ($invoice->getMission() !== null && $invoice->getMission()->getBillingAddress() !== null)
  745.         {
  746.             $addresses[] = $invoice->getMission()->getBillingAddress();
  747.         }
  748.         // invoice.emitter.allAddresses
  749.         if ($invoice->getEmitter() !== null)
  750.         {
  751.             foreach ($invoice->getEmitter()->getAddresses() as $address)
  752.             {
  753.                 $addresses[] = $address;
  754.             }
  755.         }
  756.         $addresses $this->removeDuplicates($addresses);
  757.         return $addresses;
  758.     }
  759.     public function removeDuplicates($tempAddresses)
  760.     {
  761.         $addresses = array();
  762.         foreach ($tempAddresses as $tempAddress)
  763.         {
  764.             $add true;
  765.             foreach ($addresses as $key => $address)
  766.             {
  767.                 if ($address->equalsInValue($tempAddress))
  768.                 {
  769.                     $display $address->getFormDisplay()." / ".$tempAddress->getInfo();
  770.                     $addresses[$key]->setFormDisplay($display);
  771.                     //$tempAddress->setFormDisplay($display);
  772.                     $add false;
  773.                     break;
  774.                 }
  775.             }
  776.             if ($add)
  777.             {
  778.                 $tempAddress->setFormDisplay($tempAddress->getInfo());
  779.                 $addresses[] = $tempAddress;
  780.             }
  781.         }
  782.         return $addresses;
  783.     }
  784.     public function duplicateAddress($address)
  785.     {
  786.         if ($address === null)
  787.             return null;
  788.         $a = new Address();
  789.         $a->setName($address->getName());
  790.         $a->setAddress($address->getAddress());
  791.         $a->setComplement($address->getComplement());
  792.         $a->setPostalCode($address->getPostalCode());
  793.         $a->setTown($address->getTown());
  794.         $a->setCountry($address->getCountry());
  795.         $a->setLatitude($address->getLatitude());
  796.         $a->setLongitude($address->getLongitude());
  797.         $a->setEmail($address->getEmail());
  798.         $a->setSocietyGroup($address->getSocietyGroup());
  799.         return $a;
  800.     }
  801.     public function copyAddress($source$destination)
  802.     {
  803.         if ($source === null)
  804.             return null;
  805.         if ($destination === null)
  806.             return null;
  807.         $destination->setName($source->getName());
  808.         $destination->setAddress($source->getAddress());
  809.         $destination->setComplement($source->getComplement());
  810.         $destination->setPostalCode($source->getPostalCode());
  811.         $destination->setTown($source->getTown());
  812.         $destination->setCountry($source->getCountry());
  813.         $destination->setLatitude($source->getLatitude());
  814.         $destination->setLongitude($source->getLongitude());
  815.         $destination->setEmail($source->getEmail());
  816.         $destination->setSocietyGroup($source->getSocietyGroup());
  817.         return $destination;
  818.     }
  819.     public function getDepartmentFromAddress($address)
  820.     {
  821.         if ($address === null)
  822.             return null;
  823.         $postalCode $address->getPostalCode();
  824.         if ($postalCode === null)
  825.             return null;
  826.         $deptRepository $this->em->getRepository(Department::class);
  827.         // Get both the long and the sort version
  828.         $department $deptRepository->findOneByPostalCode(substr($postalCode,0,2));
  829.         return $department;
  830.     }
  831.     public function computeAddressChanges($address$changesSet)
  832.     {
  833.         if (count($changesSet) < 1)
  834.             return null;
  835.         $propertyAccessor PropertyAccess::createPropertyAccessor();
  836.         $old = array();
  837.         $new = array();
  838.         $changesToWatch = array("address""complement""postalCode""town""country");
  839.         foreach ($changesSet as $key => $change)
  840.         {
  841.             if (in_array($key$changesToWatch) && $change[1] != null)
  842.             {
  843.                 $old[$key] = $change[0];
  844.                 $new[$key] = $change[1];
  845.             }
  846.         }
  847.         $oldValue "";
  848.         $newValue "";
  849.         foreach ($changesToWatch as $key => $property)
  850.         {
  851.             if (array_key_exists($property$old))
  852.             {
  853.                 $oldValue .= $old[$property];
  854.                 $newValue .= $new[$property];
  855.             }
  856.             else
  857.             {
  858.                 $oldValue .= $propertyAccessor->getValue($address$property);
  859.                 $newValue .= $propertyAccessor->getValue($address$property);
  860.             }
  861.             if ($key == 2)
  862.             {
  863.                 $oldValue substr($oldValue0, -1);
  864.                 $newValue substr($newValue0, -1);
  865.                 $oldValue .= ", ";
  866.                 $newValue .= ", ";
  867.             }
  868.             else
  869.             {
  870.                 $oldValue .= " ";
  871.                 $newValue .= " ";
  872.             }
  873.         }
  874.         // Did Something change ?
  875.         if ($oldValue === $newValue)
  876.         {
  877.             return null;
  878.         }
  879.         return array(
  880.             "old"    =>    $oldValue,
  881.             "new"    =>    $newValue,
  882.         );
  883.     }
  884.     public function isValid($address)
  885.     {
  886.         if($address === null)
  887.             return false;
  888.         if(empty($address->getAddress()))
  889.             return false;
  890.         if(empty($address->getPostalCode()))
  891.             return false;
  892.         if(empty($address->getTown()))
  893.             return false;
  894.         return true;
  895.     }
  896.     public function getSetLatLng(&$address$noflush null)
  897.     {
  898.         // $backTrace = debug_backtrace()[1];
  899.         // $method = $backTrace['function'];
  900.         // $class = $backTrace['class'];
  901.         // $trace = $class."::".$method;
  902.         // $trace = str_replace("\\", "::", $trace);
  903.         // $this->logTools->ploopLog("[AddressTools][getSetLatLng] trace = ".$trace);
  904.         $societyGroup $address->getSocietyGroup();
  905.         if ($societyGroup === null)
  906.         {
  907.             // $this->logTools->ploopLog("[AddressTools][getSetLatLng][Die.Hard] SocietyGroup is null");
  908.             return false;
  909.         }
  910.         $codeInactive $this->moduleTools->isInactiveByCode($societyGroupModule::MODULE_MAPS);
  911.         if ($codeInactive)
  912.         {
  913.             // $this->logTools->ploopLog("[AddressTools][getSetLatLng][Die.Hard] Cannot find MODULE_MAPS");
  914.             return false;
  915.         }
  916.         $APIKey $this->em->getRepository(Config::class)->findOneByName(Config::API_GOOGLE_MAPS_KEY);
  917.         if ($APIKey === null)
  918.         {
  919.             // $this->logTools->ploopLog("[AddressTools][getSetLatLng][Die.Hard] Cannot find Api Key");
  920.             return false;
  921.         }
  922.         $APIKey $APIKey->getValue();
  923.         //Call api to retrieve lat/lng
  924.         // google map geocode api url
  925.         $url "https://maps.googleapis.com/maps/api/geocode/json?address=" .
  926.                 urlencode(
  927.                             $address->getAddress() .
  928.                              " " $address->getComplement() .
  929.                             " " $address->getPostalCode() .
  930.                             " " $address->getTown()
  931.                 ) .
  932.                 "&key=" $APIKey;
  933.         // get the json response
  934.         $ch curl_init();
  935.         // Will return the response, if false it print the response
  936.         curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
  937.         curl_setopt($chCURLOPT_CONNECTTIMEOUT5);
  938.         // Set the url
  939.         curl_setopt($chCURLOPT_URL,$url);
  940.         // Execute
  941.         $result curl_exec($ch);
  942.         // Closing
  943.         curl_close($ch);
  944.         // decode the json
  945.         $resp json_decode($resulttrue);
  946.         // only increment the counter if we have a valid response
  947.         if($resp['status'] == 'OK' || $resp['status'] == 'ZERO_RESULTS')
  948.         {        
  949.             $address->incrementApiCounter();
  950.         }
  951.         // response status will be 'OK', if able to geocode given address
  952.         if($resp['status'] == 'OK')
  953.         {
  954.             // get the important data
  955.             $lat = isset($resp['results'][0]['geometry']['location']['lat']) ? $resp['results'][0]['geometry']['location']['lat'] : "";
  956.             $lng = isset($resp['results'][0]['geometry']['location']['lng']) ? $resp['results'][0]['geometry']['location']['lng'] : "";
  957.             $formatted_address = isset($resp['results'][0]['formatted_address']) ? $resp['results'][0]['formatted_address'] : "";
  958.             // verify if data is complete
  959.             if ($lat && $lng)
  960.             {
  961.                 // Set address lat / lng
  962.                 $address->setLatitude($lat);
  963.                 $address->setLongitude($lng);
  964.                 //echo $lat . " / " . $lng . "\n";
  965.                 if ($noflush === null)
  966.                 {
  967.                     $this->em->persist($address);
  968.                     try
  969.                     {
  970.                         $this->em->flush();
  971.                     }
  972.                     catch (\Exception $e)
  973.                     {
  974.                         $this->logTools->errorlog($e->getMessage());
  975.                         $this->logTools->errorlog_db($e);
  976.                     }
  977.                 }
  978.                 $args = array(
  979.                     "address"    =>    $address,
  980.                     "success"    =>    true,
  981.                 );
  982.                 // $this->logTools->ploopLog("[AddressTools][getSetLatLng] All is peachy");
  983.                 return true;
  984.             }
  985.             else
  986.             {
  987.                 $args = array(
  988.                     "address"    =>    $address,
  989.                     "success"    =>    false,
  990.                 );
  991.                 // $this->logTools->ploopLog("[AddressTools][getSetLatLng][Die.Hard] Api failed miserably");
  992.                    return false;
  993.             }
  994.         }
  995.         else // Fail to get the Lat/Lng : Try with CP alone ?
  996.         {
  997.             $args = array(
  998.                 "address"    =>    $address,
  999.                 "success"    =>    false,
  1000.             );
  1001.             // $this->logTools->ploopLog("[AddressTools][getSetLatLng][Die.Hard] Api failed miserably again");
  1002.             return false;
  1003.         }
  1004.     }
  1005.     // Plan.io Task #4188
  1006.     public function getAllForClient(Client $client)
  1007.     {
  1008.         if ($client->getIndividual() !== null)
  1009.             return $this->getAllForIndividual($client->getIndividual());
  1010.         if ($client->getStore() !== null)
  1011.             return $this->getAllForStore($client->getStore());
  1012.         return null;
  1013.     }
  1014.     // Plan.io Task #4188
  1015.     public function getAllForIndividual(Individual $individual)
  1016.     {
  1017.         $addresses = array();
  1018.         // Billing first
  1019.         if ($individual->getBillingAddress() !== null)
  1020.         {
  1021.             array_push($addresses$individual->getBillingAddress());
  1022.         }
  1023.         // Fallback on simple address
  1024.         if ($individual->getAddress() !== null)
  1025.         {
  1026.             array_push($addresses$individual->getAddress());
  1027.         }
  1028.         return $addresses;
  1029.     }
  1030.     // Plan.io Task #4188
  1031.     public function getAllForStore(Store $store)
  1032.     {
  1033.         $addresses = array();
  1034.         if ($store->getBillingAddress() !== null)
  1035.         {
  1036.             array_push($addresses$store->getBillingAddress());
  1037.         }
  1038.         // Get normal addres too
  1039.         if ($store->getAddress() !== null)
  1040.         {
  1041.             array_push($addresses$store->getAddress());
  1042.         }
  1043.         return $addresses;
  1044.     }
  1045.     // Plan.io Task #3621 #4346
  1046.     public function extractAddressesFromServiceOrderUpdate(SocietyGroup $societyGroup$decodedAddressData)
  1047.     {
  1048.         // $this->logTools->ploopLog("[AddressTools][extractAddressesFromServiceOrderUpdate]");
  1049.         if (empty($decodedAddressData))
  1050.         {
  1051.             return array(
  1052.                 'addresses'                =>    [],
  1053.                 'interventionAddress'    =>    null,
  1054.                 'billingAddress'        =>    null,
  1055.                 'otherAddress'            =>    [],
  1056.             );
  1057.         }
  1058.         $interventionAddress null;
  1059.         $billingAddress null;
  1060.         $otherAddress = array();
  1061.         $addresses = array();
  1062.         foreach ($decodedAddressData as $key => $addressData)
  1063.         {
  1064.             $address $this->craftAddressForServiceOrder($addressData$societyGroup);
  1065.             $code null;
  1066.             if (array_key_exists('code'$addressData) && !empty($addressData['code']))
  1067.             {
  1068.                 $code $addressData['code'];
  1069.                 if ($code == 'DELIVERY')
  1070.                 {
  1071.                     // $this->logTools->ploopLog("[AddressTools][extractAddressesFromServiceOrderUpdate] DELIVERY address found : ");
  1072.                     // $this->logTools->ploopLog("[AddressTools][extractAddressesFromServiceOrderUpdate] ".$address->displayForLog());
  1073.                     $interventionAddress $address;
  1074.                     $addresses[] = $address;
  1075.                 }
  1076.                 else
  1077.                 {
  1078.                     if ($code == 'INVOICE')
  1079.                     {
  1080.                         // $this->logTools->ploopLog("[AddressTools][extractAddressesFromServiceOrderUpdate] INVOICE address found : ");
  1081.                         // $this->logTools->ploopLog("[AddressTools][extractAddressesFromServiceOrderUpdate] ".$address->displayForLog());
  1082.                         $billingAddress $address;
  1083.                         $addresses[] = $address;
  1084.                     }
  1085.                     else
  1086.                     {
  1087.                         // $this->logTools->ploopLog("[AddressTools][extractAddressesFromServiceOrderUpdate] Other address found : ");
  1088.                         // $this->logTools->ploopLog("[AddressTools][extractAddressesFromServiceOrderUpdate] ".$address->displayForLog());
  1089.                         $otherAddress[] = $address;
  1090.                         $addresses[] = $address;
  1091.                     }
  1092.                 }
  1093.             }
  1094.         }
  1095.         foreach ($addresses as $address)
  1096.         {
  1097.             // Plan.io Task #4163
  1098.             // SocietyGroup is needed when fetching the latitude and longitude (to check module activation)
  1099.             $address->setSocietyGroup($societyGroup);
  1100.             $this->getSetLatLng($address"no persist, no flush"); // explicit call to get latitude and longitude
  1101.             $this->em->persist($address);
  1102.         }
  1103.         return array(
  1104.             'addresses'                =>    $addresses,
  1105.             'interventionAddress'    =>    $interventionAddress,
  1106.             'billingAddress'        =>    $billingAddress,
  1107.             'otherAddress'            =>    $otherAddress,
  1108.         );
  1109.     }
  1110. }