예제 #1
0
 /**
  * Sets the invoice's payment status to paid
  *
  * @param  Invoice $invoice
  * @return bool
  */
 public function invoicesSetInvoicePaid(Invoice $invoice)
 {
     $fields['invoice_id'] = $invoice->getId();
     $rawData = $this->doCall('setInvoicePaid.php', $fields);
     if ($rawData == 'OK') {
         $invoice->setPaid(true);
         return true;
     }
     return false;
 }
예제 #2
0
 /**
  * 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;
 }