Example #1
0
 /**
  * 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;
 }
Example #2
0
 /**
  * Download a pdf of the creditnote
  *
  * @param Creditnote $creditnote
  * @return
  */
 public function invoicesDownloadCreditnotePDF(Creditnote $creditnote, $headers = false)
 {
     if ($headers) {
         header('Content-type: application/pdf');
     }
     return $this->doCall('downloadInvoicePDF.php', array('creditnote_id' => $creditnote->getId()));
 }
Example #3
0
 /**
  * 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]);
 }