/** * Tests teamleader->invoicesGetCreditnotes() */ public function testSubscriptionsAddSubscription() { $time = time(); $contact = new Contact(); $contact->setForename($time); $contact->setSurname($time); $contact->setEmail($time . '@example.com'); $id = $this->teamleader->crmAddContact($contact); $contact->setId($id); $subscription = new Subscription(); $subscription->setContact($contact); $subscription->setSysDepartmentId(2131); $subscription->setTitle($time); $subscription->setDateStart($time); $subscription->setRepeatAfter('monthly'); $line1 = new SubscriptionLine(); $line1->setAmount(1); $line1->setDescription('Description ' . $time); $line1->setPrice(30); $line1->setVat('06'); $subscription->addLine($line1); $id = $this->teamleader->subscriptionsAddSubscription($subscription); $this->assertEquals($subscription->getId(), $id); }
/** * Initialize an Invoice with raw data we got from the API * * @param array $data * @param Teamleader $tl A Teamleader instance is necessary to get the contact or company from the API * @return Invoice */ public static function initializeWithRawData($data, $tl, $cachedCustomers = null) { $invoice = new Invoice(); foreach ($data as $key => $value) { switch ($key) { case 'date_paid': if ($value != -1) { $invoice->setDatePaid($value); } break; case 'paid': $invoice->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) { $invoice->setContact($cachedCustomers['contacts'][$value]); } else { $invoice->setContact($tl->crmGetContact($value)); } } elseif ($contactOrCompany == self::COMPANY) { if ($cachedCustomers) { $invoice->setCompany($cachedCustomers['companies'][$value]); } else { $invoice->setCompany($tl->crmGetCompany($value)); } } else { throw new Exception('\'For\' must be ' . self::CONTACT . ' or ' . self::COMPANY . '.'); } break; case 'items': foreach ($value as $invoiceLine) { $invoice->addLine(InvoiceLine::initializeWithRawData($invoiceLine)); } 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($invoice, $methodName), $value); } } } return $invoice; }