Example #1
0
 /**
  * @param Response $response
  *
  * @return Transaction{}
  */
 public static function map(Response $response)
 {
     // Get the raw DOMDocument response
     $responseDOM = $response->getResponseDocument();
     // All tags for the transaction
     $transactionTags = array('code' => 'setCode', 'currency' => 'setCurrency', 'date' => 'setDate', 'period' => 'setPeriod', 'invoicenumber' => 'setInvoiceNumber', 'office' => 'setOffice', 'duedate' => 'setDueDate', 'origin' => 'setOrigin', 'number' => 'setNumber', 'freetext1' => 'setFreetext1', 'freetext2' => 'setFreetext2', 'freetext3' => 'setFreetext3');
     // Make new Transaction Object
     $transactions = array();
     // Get the top level transaction element
     $transactionElements = $responseDOM->getElementsByTagName('transaction');
     foreach ($transactionElements as $transactionElement) {
         // create new transaction
         $transaction = new Transaction();
         // set the result
         $result = $transactionElement->getAttribute('result');
         $transaction->setResult($result);
         // Set the destiny/location
         $location = $transactionElement->getAttribute('location');
         $transaction->setDestiny($location);
         // Set the raise warning
         $raiseWarning = $transactionElement->getAttribute('raisewarning');
         $transaction->setRaiseWarning($raiseWarning);
         // Go through each tag and call the method if a value is set
         foreach ($transactionTags as $tag => $method) {
             $_tag = $transactionElement->getElementsByTagName($tag)->item(0);
             if (isset($_tag) && isset($_tag->textContent)) {
                 $transaction->{$method}($_tag->textContent);
             }
             // msg
             if (isset($_tag) && $_tag->hasAttribute('msg')) {
                 $message = new Message();
                 $message->setType($_tag->getAttribute('msgtype'));
                 $message->setMessage($_tag->getAttribute('msg'));
                 $message->setField($tag);
                 $transaction->addMessage($message);
             }
         }
         $lineTags = array('dim1' => 'setDim1', 'dim2' => 'setDim2', 'value' => 'setValue', 'debitcredit' => 'setDebitCredit', 'description' => 'setDescription', 'rate' => 'setRate', 'basevalue' => 'setBaseValue', 'reprate' => 'setRepRate', 'vatcode' => 'setVatCode', 'vattotal' => 'setVatTotal', 'vatvalue' => 'setVatValue', 'vatbasetotal' => 'setVatBaseTotal', 'customersupplier' => 'setCustomerSupplier', 'basevalueopen' => 'setBaseValueOpen', 'valueopen' => 'setValueOpen', 'repvalue' => 'setRepValue', 'matchlevel' => 'setMatchLevel', 'matchstatus' => 'setMatchStatus');
         foreach ($transactionElement->getElementsByTagName('line') as $lineDOM) {
             $temp_line = new TransactionLine();
             $lineType = $lineDOM->getAttribute('type');
             $temp_line->setType($lineType);
             $lineID = $lineDOM->getAttribute('id');
             $temp_line->setID($lineID);
             foreach ($lineTags as $tag => $method) {
                 $_tag = $lineDOM->getElementsByTagName($tag)->item(0);
                 if (isset($_tag) && isset($_tag->textContent)) {
                     $temp_line->{$method}($_tag->textContent);
                 }
             }
             $transaction->addLine($temp_line);
             unset($lineType);
             unset($temp_line);
         }
         // add
         $transactions[] = $transaction;
     }
     // for backwards compatibility, return only the first instance if there is only one
     return count($transactions) == 1 ? $transactions[0] : $transactions;
 }
 /**
  * Turns a passed Transaction class into the required markup for interacting
  * with Twinfield.
  *
  * This method doesn't return anything, instead just adds the transaction
  * to this DOMDocument instance for submission usage.
  *
  * @param \Pronamic\Twinfield\Transaction\Transaction $transaction
  */
 public function addTransaction(Transaction $transaction)
 {
     // Transaction
     $transactionElement = $this->createElement('transaction');
     $transactionElement->setAttribute('destiny', $transaction->getDestiny());
     $this->transactionsElement->appendChild($transactionElement);
     // Header
     $headerElement = $this->createElement('header');
     $transactionElement->appendChild($headerElement);
     $officeElement = $this->createElement('office', $transaction->getOffice());
     $headerElement->appendChild($officeElement);
     $codeElement = $this->createElement('code', $transaction->getCode());
     $headerElement->appendChild($codeElement);
     $dateElement = $this->createElement('date', $transaction->getDate());
     $headerElement->appendChild($dateElement);
     if ($transaction->getPeriod() !== null) {
         $periodElement = $this->createElement('period', $transaction->getPeriod());
         $headerElement->appendChild($periodElement);
     }
     if ($transaction->getInvoiceNumber() !== null) {
         $invoiceNumberElement = $this->createElement('invoicenumber', $transaction->getInvoiceNumber());
         $headerElement->appendChild($invoiceNumberElement);
     }
     if ($transaction->getDueDate() !== null) {
         $dueDateElement = $this->createElement('duedate', $transaction->getDueDate());
         $headerElement->appendChild($dueDateElement);
     }
     if ($transaction->getFreetext1() !== null) {
         $freetext1Element = $this->createElement('freetext1', $transaction->getFreetext1());
         $headerElement->appendChild($freetext1Element);
     }
     if ($transaction->getFreetext2() !== null) {
         $freetext2Element = $this->createElement('freetext2', $transaction->getFreetext2());
         $headerElement->appendChild($freetext2Element);
     }
     if ($transaction->getFreetext3() !== null) {
         $freetext3Element = $this->createElement('freetext3', $transaction->getFreetext3());
         $headerElement->appendChild($freetext3Element);
     }
     $linesElement = $this->createElement('lines');
     $transactionElement->appendChild($linesElement);
     // Lines
     foreach ($transaction->getLines() as $transactionLine) {
         /* @var $transactionLine \Pronamic\Twinfield\Transaction\TransactionLine */
         $lineElement = $this->createElement('line');
         $lineElement->setAttribute('type', $transactionLine->getType());
         $lineElement->setAttribute('id', $transactionLine->getID());
         $linesElement->appendChild($lineElement);
         $dim1Element = $this->createElement('dim1', $transactionLine->getDim1());
         $dim2Element = $this->createElement('dim2', $transactionLine->getDim2());
         $value = $transactionLine->getValue();
         $value = number_format($value, 2, '.', '');
         $valueElement = $this->createElement('value', $value);
         $debitCreditElement = $this->createElement('debitcredit', $transactionLine->getDebitCredit());
         if ($transactionLine->getType() != 'total' && $transactionLine->getVatCode() !== null) {
             $vatCodeElement = $this->createElement('vatcode', $transactionLine->getVatCode());
             $lineElement->appendChild($vatCodeElement);
         }
         $descriptionNode = $this->createTextNode(substr($transactionLine->getDescription(), 0, 40));
         $descriptionElement = $this->createElement('description');
         $descriptionElement->appendChild($descriptionNode);
         $lineElement->appendChild($dim1Element);
         $lineElement->appendChild($dim2Element);
         $lineElement->appendChild($valueElement);
         $lineElement->appendChild($debitCreditElement);
         $invoicenumber = $transactionLine->getInvoicenumber();
         if (!empty($invoicenumber)) {
             $invoicenumberElement = $this->createElement('invoicenumber', $invoicenumber);
             $lineElement->appendChild($invoicenumberElement);
         }
         $performanceType = $transactionLine->getPerformanceType();
         if (!empty($performanceType)) {
             $perfElement = $this->createElement('performancetype', $performanceType);
             $lineElement->appendChild($perfElement);
         }
         $currencyDate = $transactionLine->getCurrencyDate();
         if (!empty($currencyDate)) {
             $currencyDateElement = $this->createElement('currencydate', $currencyDate);
             $lineElement->appendChild($currencyDateElement);
         }
         $vatValue = $transactionLine->getVatValue();
         if (!empty($vatValue)) {
             $vatElement = $this->createElement('vatvalue', $vatValue);
             $lineElement->appendChild($vatElement);
         }
         $lineElement->appendChild($descriptionElement);
     }
 }