/** * Initialize an Invoice with raw data we got from the API * * @param array $data * @return Invoice */ public static function initializeWithRawData($data, $tl, $cachedCustomers = null) { $creditnote = new Creditnote(); foreach ($data as $key => $value) { switch ($key) { case 'date_paid': if ($value != 0) { $creditnote->setDatePaid($value); } break; case 'paid': $creditnote->setPaid((bool) $value); break; // Ignore 'for' and 'contact_or_company' until the id is given // Ignore 'for' and 'contact_or_company' until the id is given case 'for': case 'contact_or_company': break; case 'for_id': case 'contact_or_company_id': $contactOrCompany = null; $contactOrCompanyId = null; // Check if contact or copany are given via a 'for' property or a 'contact_or_company' property if (isset($data['for'])) { $contactOrCompany = $data['for']; } elseif (isset($data['contact_or_company'])) { $contactOrCompany = $data['contact_or_company']; } if (isset($data['for_id'])) { $contactOrCompanyId = $data['for_id']; } elseif (isset($data['contact_or_company_id'])) { $contactOrCompanyId = $data['contact_or_company_id']; } if ($contactOrCompany == self::CONTACT) { if ($cachedCustomers) { $creditnote->setContact($cachedCustomers['contacts'][$value]); } else { $creditnote->setContact($tl->crmGetContact($value)); } } elseif ($contactOrCompany == self::COMPANY) { if ($cachedCustomers) { $creditnote->setCompany($cachedCustomers['companies'][$value]); } else { $creditnote->setContact($tl->crmGetCompany($value)); } } else { throw new Exception('\'For\' must be ' . self::CONTACT . ' or ' . self::COMPANY . '.'); } break; case 'items': foreach ($value as $creditnoteLine) { $creditnote->addLine(creditnoteLine::initializeWithRawData($creditnoteLine)); } break; case 'discount_info': // Todo break; default: // Ignore empty values if ($value == '') { continue; } $methodName = 'set' . str_replace(' ', '', ucwords(str_replace('_', ' ', $key))); if (!method_exists(__CLASS__, $methodName)) { if (Teamleader::DEBUG) { var_dump($key, $value); throw new Exception('Unknown method (' . $methodName . ')'); } } else { call_user_func(array($creditnote, $methodName), $value); } } } return $creditnote; }