/**
  * Get handle from number
  *
  * @param integer $no
  * @return object|integer
  */
 public function getHandle($no)
 {
     if (is_object($no) and isset($no->Id)) {
         return $no;
     }
     return $this->client->Unit_FindByNumber(['number' => $no])->Unit_FindByNumberResult;
 }
 /**
  * Get all Products of a specific Product Group
  *
  * @param integer $no
  * @return object
  */
 public function products($no)
 {
     $handle = $this->getHandle($no);
     $productHandles = $this->client->ProductGroup_GetProducts(['productGroupHandle' => $handle])->ProductGroup_GetProductsResult->ProductHandle;
     $product = new Product($this->client_raw);
     return $product->getArrayFromHandles($productHandles);
 }
 /**
  * Subscribers
  *
  * @param $handles
  * @return mixed
  */
 public function subscribers($handles)
 {
     $subscriber = $this->client->Subscriber_GetDataArray(['entityHandles' => $handles])->Subscriber_GetDataArrayResult;
     $debtor = new Debtor($this->client_raw);
     $subscriber->debtor = $debtor->get($subscriber->SubscriberData->DebtorHandle);
     return $subscriber;
 }
 /**
  * Set Quotation Line unit
  *
  * @param object $invoiceLineHandle
  * @param integer $unit
  * @return bool
  */
 public function unit($invoiceLineHandle, $unit)
 {
     $units = new Unit($this->client_raw);
     $unitHandle = $units->getHandle($unit);
     $this->client->CurrentInvoiceLine_SetUnit(['currentInvoiceLineHandle' => $invoiceLineHandle, 'valueHandle' => $unitHandle]);
     return true;
 }
 /**
  * Set Quotation Line unit by unit number
  *
  * @param object $orderLineHandle
  * @param $unit
  * @return bool
  */
 public function unit($orderLineHandle, $unit)
 {
     $units = new Unit($this->client_raw);
     $unitHandle = $units->getHandle($unit);
     $this->client->OrderLine_SetUnit(['orderLineHandle' => $orderLineHandle, 'valueHandle' => $unitHandle]);
     return true;
 }
 /**
  * Set Quotation Line product by product number
  *
  * @param  mixed $quotationLineHandle
  * @param  integer $product
  * @return boolean
  */
 public function product($quotationLineHandle, $product)
 {
     $products = new Product($this->client_raw);
     $productHandle = $products->getHandle($product);
     $this->client->QuotationLine_SetProduct(['quotationLineHandle' => $quotationLineHandle, 'valueHandle' => $productHandle]);
     return true;
 }
 /**
  * Update debtor (unfinished)
  *
  * @param $data
  * @return bool
  * @throws Exception
  */
 public function update($data)
 {
     $debitorHandle = ['Number' => $data['number']];
     $groupHandle = ['Number' => $data['group_number']];
     $currencyHandle = ['Code' => $data['currency_code']];
     $termHandle = ['Name' => $data['payment_term_name']];
     $templateHandle = ['Name' => $data['template_name']];
     $contactHandle = ['externalId' => $data['externalId']];
     $data = ['data' => (object) ['Handle' => $debitorHandle, 'Number' => $data['number'], 'VatZone' => $vat_zone, 'IsAccessible' => true, 'Name' => $params['company'], 'Email' => $params['email'], 'Address' => $params['address'], 'PostalCode' => $params['postalcode'], 'City' => $params['city'], 'VatNumber' => $params['vat'], 'Country' => $params['country'], 'DebtorGroupHandle' => $groupHandle, 'CurrencyHandle' => $currencyHandle, 'TermOfPaymentHandle' => $termHandle, 'LayoutHandle' => $templatecollectionHandle, 'AttentionHandle' => $contact_handle]];
     try {
         $updatedDebitorHandle = $this->client->Debtor_UpdateFromData($data)->Debtor_UpdateFromDataResult;
     } catch (Exception $e) {
         throw new Exception('Could not update Debitor');
     }
     if (isset($contact_handle->Id)) {
         $contactData = ['data' => ['Handle' => $contact_handle, 'Id' => $contact_handle->Id, 'ExternalId' => $params['external'], 'DebtorHandle' => $updatedDebitorHandle, 'Name' => $params['name'], 'Number' => $params['external'], 'Email' => $params['email'], 'IsToReceiveEmailCopyOfOrder' => false, 'IsToReceiveEmailCopyOfInvoice' => false]];
         try {
             $contact = $this->client->DebtorContact_UpdateFromData($contactData)->DebtorContact_UpdateFromDataResult;
         } catch (Exception $e) {
             throw new Exception('Could not update Debitor Contact information');
         }
     }
     if (!empty($params['debitor_ean'])) {
         $debtorEan = ['debtorHandle' => $debitor, 'valueHandle' => $params['debitor_ean']];
         $this->client->Debtor_SetEan($debtorEan);
     }
     return true;
 }
 /**
  * Create a new Debtor Group
  *
  * @param  string  $name
  * @param  integer $account
  * @return object
  */
 public function create($name, $account)
 {
     $all = $this->all();
     $number = end($all)->Number + 1;
     $accounts = new Account($this->client_raw);
     $accountHandle = $accounts->getHandle($account);
     $groupHandle = $this->client->DebtorGroup_Create(["number" => $number, "name" => $name, "accountHandle" => $accountHandle])->DebtorGroup_CreateResult;
     return $this->get($number);
 }
 /**
  * Delete order
  *
  * @param integer $no
  * @return bool
  */
 public function delete($no)
 {
     $handle = $this->getHandle($no);
     try {
         $this->client->Order_Delete(['OrderHandle' => $handle]);
     } catch (Exception $e) {
         return false;
     }
     return true;
 }
 /**
  * Create a new Quotation to a specific Debtor
  *
  * @param integer $debtorNumber
  * @param Closure $callback
  * @return mixed
  * @throws Exception
  */
 public function create($debtorNumber, Closure $callback)
 {
     $debtor = new Debtor($this->client_raw);
     $debtorHandle = $debtor->getHandle($debtorNumber);
     $quotationHandle = $this->client->Quotation_Create(['debtorHandle' => $debtorHandle])->Quotation_CreateResult;
     if (!$quotationHandle->Id) {
         throw new Exception("Error: creating Quotation.");
     }
     $this->lines = new QuotationLines($this->client_raw, $quotationHandle);
     call_user_func($callback, $this->lines);
     return $this->get($quotationHandle);
 }
 /**
  * Get from number
  *
  * @param integer $no
  * @return mixed
  */
 public function get($no)
 {
     $handle = $this->getHandle($no);
     return $this->client->Subscription_GetData(['entityHandle' => $handle])->Subscription_GetDataResult;
 }
 /**
  * Returns the price for the product in the PriceGroup
  * If no special price is found then the sales price is returned instead
  *
  * @param  integer $groupNo
  * @param  string $productNo
  * @return float
  */
 public function getPrice($groupNo, $productNo)
 {
     return $this->client->PriceGroup_GetPrice(['priceGroupHandle' => ['Number' => $groupNo], 'productHandle' => ['Number' => $productNo]])->PriceGroup_GetPriceResult;
 }
 /**
  * Get Debtor Groups from handles
  *
  * @param array $handles
  * @return object
  */
 public function getArrayFromHandles($handles)
 {
     return $this->client->Account_GetDataArray(['entityHandles' => $handles])->Account_GetDataArrayResult->AccountData;
 }
 /**
  * Get the price of a product by currency code
  *
  * @param string $number
  * @param string $code
  * @return string
  */
 public function getPriceByCurrency($number, $code)
 {
     return $this->client->ProductPrice_GetPrice(['productPriceHandle' => ['Id1' => $number, 'Id2' => $code]])->ProductPrice_GetPriceResult;
 }
 /**
  * Book a current Invoice
  *
  * @param  integer $invoiceNumber
  * @return object
  */
 public function book($invoiceNumber)
 {
     $handle = $this->getHandle($invoiceNumber);
     $number = $this->client->Invoice_Book(['invoiceHandle' => $handle])->Invoice_BookResult;
     return $number;
 }
 /**
  * Get data from debtor entry handle
  *
  * @param stdClass $handle
  * @return mixed
  */
 public function getData($handle)
 {
     return $this->client->DebtorEntry_GetData(['entityHandle' => $handle])->DebtorEntry_GetDataResult;
 }