/** * 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; }
/** * Initialize an Invoiceline with raw data we got from the API * * @param array $data * @return Invoice */ public static function initializeWithRawData($data) { $invoiceLine = new InvoiceLine(); foreach ($data as $key => $value) { switch ($key) { case 'text': $invoiceLine->setDescription($value); break; case 'vat_rate': $invoiceLine->setVat($value); break; case 'account': // 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($invoiceLine, $methodName), $value); } } } return $invoiceLine; }
/** * Tests teamleader->invoicesGetCreditnotes() */ public function testInvoicesGetCreditnotes() { $time = time(); $contact = new Contact(); $contact->setForename($time); $contact->setSurname($time); $contact->setEmail($time . '@example.com'); $id = $this->teamleader->crmAddContact($contact); $contact->setId($id); $invoice = new Invoice(); $invoice->setContact($contact); $invoice->setSysDepartmentId(2131); $line1 = new InvoiceLine(); $line1->setAmount(1); $line1->setDescription('Description ' . $time); $line1->setPrice(30); $line1->setVat('06'); $invoice->addLine($line1); $id = $this->teamleader->invoicesAddInvoice($invoice); $invoice->setId($id); $creditnote = new Creditnote(); $creditnote->setInvoice($invoice); $line1 = new CreditnoteLine(); $line1->setAmount(1); $line1->setDescription('Description ' . $time); $line1->setPrice(30); $line1->setVat('06'); $creditnote->addLine($line1); $this->teamleader->invoicesAddCreditnote($creditnote); $dateFrom = strtotime(date('Y-m-d H:i:s') . " -1 day"); $dateTo = strtotime(date('Y-m-d H:i:s') . " +1 day"); $response = $this->teamleader->invoicesGetCreditnotes($dateFrom, $dateTo); $this->assertInstanceOf('SumoCoders\\Teamleader\\Invoices\\Creditnote', $response[0]); }