Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function ajaxGetAddressesWithDataByCustomerAndDistributorAction(int $customerId, int $distributorId) {
- //serializer construction
- $objectNormalizer = new ObjectNormalizer();
- $objectNormalizer->setCircularReferenceLimit(2);
- $objectNormalizer->setCircularReferenceHandler(function ($object) {
- return $object->getId();
- });
- $objectNormalizer->setIgnoredAttributes(['subscriber']);
- $serializer = new Serializer([new DateTimeNormalizer(), $objectNormalizer], []);
- $doctrine = $this->getDoctrine();
- //get data to make retrieving keys
- $customer = $doctrine->getRepository(Customer::class)->find($customerId);
- $distributorReference = $doctrine->getManager()->getReference(Distributor::class, $distributorId);
- $addresses = $doctrine->getRepository(Address::class)->findBy([
- 'subscriber' => $customer->getSubscriber()
- ]);
- $addressesToSend = $serializer->normalize($addresses, 'json');
- //each address => attach customerData to them
- foreach ($addressesToSend as &$address) {
- //keys to retrieve data in both customerData tables
- $keys = [
- 'customer' => $customer,
- 'distributor' => $distributorReference,
- 'address' => $doctrine->getManager()->getReference(Address::class, $address['id'])
- //, 'acceptedByDistributor' => true
- ];
- //get customerData
- $billingData = $doctrine->getRepository(CustomerDistributorBillingData::class)->findOneBy($keys);
- $deliveryData = $doctrine->getRepository(CustomerDistributorDeliveryData::class)->findOneBy($keys);
- //set customerData
- $address['clientCode'] = ($billingData) ? $billingData->getClientCode() : null;
- $address['deliveryDays'] = ($deliveryData) ? $deliveryData->getDeliveryDays() : null;
- $address['both'] = $billingData && $deliveryData;
- $address['billing'] = $billingData && !$deliveryData;
- $address['delivery'] = $deliveryData && !$billingData;
- }
- return new JsonResponse($addressesToSend);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement