hasOne(Salarie::class, 'customer_id'); } /** * Get the crm Data of a customer * * @return \LDC\LDCCRM\Models\Customer * @throws \LDC\LDCCRM\Exceptions\CrmException If cannot fetch data from crm. * @author Jérémy Munsch */ public function getApiDataAttribute(): CrmCustomer { /** @var \LDC\LDCCRM\CrmApiV2 */ $crm = app('crmAPI'); $crmCustomer = $crm->getCustomer($this->hashcode); if (!$crmCustomer) { throw new CrmException('CRM get Customer returned null'); } return $crmCustomer; } /** * Set the crm Data of a customer * @param CustomerCrmResource $resource * * @return CrmCustomer * * @throws \LDC\LDCCRM\Exceptions\CrmException If an error occurs on server side or on getting response. * @throws \LDC\LDCCRM\Exceptions\ApiErrorException If any error or \LDC\LDCCRM\Enums\ApiError occurs. * @author Jérémy Munsch * @phpcs:disable Squiz.Commenting.FunctionCommentThrowTag.WrongNumber */ public function updateApiData(CustomerCrmResource $resource): CrmCustomer { $data = $resource->toArray(null); /** @var \LDC\LDCCRM\CrmApiV2 */ $crm = app('crmAPI'); $crmCustomer = $crm->getCustomer($data['hashcode']); if (!$crmCustomer) { throw new CrmException('CRM get Customer returned null'); } $crmCustomer->fill($data); $crmCustomer->save(); return $crmCustomer; } /** * This mutate Customer attributes to array * including only non critical properties and crm values * (One that can be send to front office without security breach) * * @return array */ public function toArray() { if ($this->hashcode) { return collect(array_merge( parent::toArray(), (array)$this->apiData ))->intersectByKeys([ 'email' => '', 'firstname' => '', 'lastname' => '', 'address' => '', 'address2' => '', 'postcode' => '', 'birth_date' => '', 'city' => '', 'country' => '', 'opt_in' => '', 'phone' => '' ])->toArray(); } else { return parent::toArray(); } } }