shopperRepository->find($shopperId); } /** * Resolve the shop assigned to a customer through their employee record. * * Resolution order: * 1. Direct exception override (shop_employee_exception table) * 2. Site + service mapping (shop_employees table, with service_code) * 3. Site-only mapping (shop_employees table, service_code IS NULL) * 4. Default shop (shops.default = true) */ public function getShopFor(ShopperId $shopperId): ?Shop { $shopper = $this->getShopperFor($shopperId); return $shopper !== null ? $this->resolveShopForEmployee($shopper) : null; } public function resolveShopForEmployee(Shopper $shopper): Shop { // 1. Check for a direct exception override $exceptionShop = $this->shopRepository ->findExceptionShopFor($shopper->matricule); if ($exceptionShop !== null) { return $exceptionShop; } // 2. Try matching on site_code + service_code $serviceShop = $this->shopRepository ->findShopForSiteAndService($shopper->site, $shopper->service); if ($serviceShop !== null) { return $serviceShop; } // 3. Fall back to site_code only (service_code IS NULL) $siteShop = $this->shopRepository ->findShopForSiteAndService($shopper->site, null); if ($siteShop !== null) { return $siteShop; } // 4. Ultimate fallback: the default shop return $this->shopRepository->getDefaultShop(); } public function canCustomerPassOrdersApp(ShopperId $shopperId): bool { return $this->getShopperFor($shopperId)?->isActive() ?? false; } }