private function buildTransferReversal($xml)
 {
     $transferReversal = new TransferReversal();
     $transferReversal->setRequestId((string) $xml->RequestId);
     $transferReversal->setOriginalRequestId((string) $xml->OriginalRequestId);
     $transferReversal->setTransactionReference((string) $xml->TransactionReference);
     $transactionHistory = new TransactionHistory();
     $tmpTransaction = new Transaction();
     $transaction = $xml->TransactionHistory->Transaction;
     $tmpTransaction->setType((string) $transaction->Type);
     $tmpTransaction->setSystemTraceAuditNumber((string) $transaction->SystemTraceAuditNumber);
     $tmpTransaction->setNetworkReferenceNumber((string) $transaction->NetworkReferenceNumber);
     $tmpTransaction->setSettlementDate((string) $transaction->SettlementDate);
     $tmpResponse = new Response();
     $response = $transaction->Response;
     $tmpResponse->setCode((string) $response->Code);
     $tmpResponse->setDescription((string) $response->Description);
     $tmpTransaction->setSubmitDateTime((string) $transaction->SubmitDateTime);
     $tmpTransaction->setResponse($tmpResponse);
     $transactionHistory->setTransaction($tmpTransaction);
     $transferReversal->setTransactionHistory($transactionHistory);
     return $transferReversal;
 }
  static public function retrieveByClient($status, $client_id = null)
  {
//echo $status;
    $types= array('all','sell','purchase','switch');
    $statuses= array('all','pending','completed','cancelled');    
    if(!in_array($status,$statuses))
    if(!in_array($status,$types))    
      die('Wrong status when getting tranaction collection!');
    $IsTypeOriented = in_array($status,$statuses) ? false: true;
    if(!$client_id)
      die('Should add case for all clients!');


    $transactions= array();
    if($IsTypeOriented)
      $addition = ($status!= 'all') ? " AND types = '$status'" : "";
    else
      $addition = ($status!= 'all') ? " AND status = '$status'" : "";
      
    $query = " SELECT t.id, t.created_at, t.comment_user, t.comment_admin, a.name aname, i.fund_name name,
    i.ISIN code, t.status, t.types, t.amount1  FROM transactions t, custody_ac a, fund i
    WHERE t.id_client = '$client_id' AND a.id = t.id_account AND i.id = t.id_isin1 $addition ";
    
//echo $query;

    $qres=mysql_query($query) or die(mysql_error());
    while($row=mysql_fetch_assoc($qres))
    {
      if(trim($row['id_isin2']))
      {
        $query = " SELECT name, code FROM isin WHERE id = '".trim($row['id_isin2'])."' LIMIT 1";
        //die($query);
        $qres1=mysql_query($query);
        $crow=mysql_fetch_assoc($qres1);
      }


      $transaction = new Transaction();
      $transaction->setId($row['id']);
      $transaction->setStatus($row['status']);
      $transaction->setType($row['types']);
      $transaction->setCommentUser($row['comment_user']);
      $transaction->setCommentAdmin($row['comment_admin']);
      $transaction->setSecurity1($row['name']);
      $transaction->setAmount1($row['amount1']);
      $transaction->setSecurity2($crow['name']?$crow['name']:0);
      $transaction->setAmount2($row['amount2']);
      $transaction->setIsin($row['code']);
      $transaction->setIsin2($crow['code']);
      $transaction->setAccount($row['aname']);
      $transaction->setDatetime($row['created_at']);
      $transactions[] = $transaction;

    }

    return $transactions;
  }
 private function buildTransfer($xml)
 {
     $transfer = new Transfer();
     $transfer->setRequestId((string) $xml->RequestId);
     $transfer->setTransactionReference((string) $xml->TransactionReference);
     $transactionHistory = new TransactionHistory();
     $transactionArray = array();
     foreach ($xml->TransactionHistory->Transaction as $transaction) {
         $tmpTransaction = new Transaction();
         $tmpTransaction->setType((string) $transaction->Type);
         $tmpTransaction->setSystemTraceAuditNumber((string) $transaction->SystemTraceAuditNumber);
         $tmpTransaction->setNetworkReferenceNumber((string) $transaction->NetworkReferenceNumber);
         $tmpTransaction->setSettlementDate((string) $transaction->SettlementDate);
         $tmpResponse = new Response();
         $response = $transaction->Response;
         $tmpResponse->setCode((string) $response->Code);
         $tmpResponse->setDescription((string) $response->Description);
         $tmpTransaction->setSubmitDateTime((string) $transaction->SubmitDateTime);
         $tmpTransaction->setResponse($tmpResponse);
         array_push($transactionArray, $tmpTransaction);
     }
     $transactionHistory->setTransaction($transactionArray);
     $transfer->setTransactionHistory($transactionHistory);
     return $transfer;
 }
 public static function readTransaction($str_xml)
 {
     // Parser
     $parser = new xmlParser($str_xml);
     // <transaction>
     $data = $parser->getResult('transaction');
     $transaction = new Transaction();
     // <transaction> <lastEventDate>
     if (isset($data["lastEventDate"])) {
         $transaction->setLastEventDate($data["lastEventDate"]);
     }
     // <transaction> <date>
     if (isset($data["date"])) {
         $transaction->setDate($data["date"]);
     }
     // <transaction> <code>
     if (isset($data["code"])) {
         $transaction->setCode($data["code"]);
     }
     // <transaction> <reference>
     if (isset($data["reference"])) {
         $transaction->setReference($data["reference"]);
     }
     // <transaction> <type>
     if (isset($data["type"])) {
         $transaction->setType(new TransactionType($data["type"]));
     }
     // <transaction> <status>
     if (isset($data["status"])) {
         $transaction->setStatus(new TransactionStatus($data["status"]));
     }
     if (isset($data["paymentMethod"]) && is_array($data["paymentMethod"])) {
         // <transaction> <paymentMethod>
         $paymentMethod = new PaymentMethod();
         // <transaction> <paymentMethod> <type>
         if (isset($data["paymentMethod"]['type'])) {
             $paymentMethod->setType(new PaymentMethodType($data["paymentMethod"]['type']));
         }
         // <transaction> <paymentMethod> <code>
         if (isset($data["paymentMethod"]['code'])) {
             $paymentMethod->setCode(new PaymentMethodCode($data["paymentMethod"]['code']));
         }
         $transaction->setPaymentMethod($paymentMethod);
     }
     // <transaction> <grossAmount>
     if (isset($data["grossAmount"])) {
         $transaction->setGrossAmount($data["grossAmount"]);
     }
     // <transaction> <discountAmount>
     if (isset($data["discountAmount"])) {
         $transaction->setDiscountAmount($data["discountAmount"]);
     }
     // <transaction> <feeAmount>
     if (isset($data["feeAmount"])) {
         $transaction->setFeeAmount($data["feeAmount"]);
     }
     // <transaction> <netAmount>
     if (isset($data["netAmount"])) {
         $transaction->setNetAmount($data["netAmount"]);
     }
     // <transaction> <extraAmount>
     if (isset($data["extraAmount"])) {
         $transaction->setExtraAmount($data["extraAmount"]);
     }
     // <transaction> <installmentCount>
     if (isset($data["installmentCount"])) {
         $transaction->setInstallmentCount($data["installmentCount"]);
     }
     if (isset($data["items"]['item']) && is_array($data["items"]['item'])) {
         $items = array();
         $i = 0;
         if (isset($data["items"]['item'][0])) {
             foreach ($data["items"]['item'] as $key => $value) {
                 $item = self::parseTransactionItem($value);
                 $items[$i] = $item;
                 $i++;
             }
         } else {
             $items[0] = self::parseTransactionItem($data["items"]['item']);
         }
         // <transaction> <items>
         $transaction->setItems($items);
     }
     if (isset($data["sender"])) {
         // <transaction> <sender>
         $sender = new Sender();
         // <transaction> <sender> <name>
         if (isset($data["sender"]["name"])) {
             $sender->setName($data["sender"]["name"]);
         }
         // <transaction> <sender> <email>
         if (isset($data["sender"]["email"])) {
             $sender->setEmail($data["sender"]["email"]);
         }
         if (isset($data["sender"]["phone"])) {
             // <transaction> <sender> <phone>
             $phone = new Phone();
             // <transaction> <sender> <phone> <areaCode>
             if (isset($data["sender"]["phone"]["areaCode"])) {
                 $phone->setAreaCode($data["sender"]["phone"]["areaCode"]);
             }
             // <transaction> <sender> <phone> <number>
             if (isset($data["sender"]["phone"]["number"])) {
                 $phone->setNumber($data["sender"]["phone"]["number"]);
             }
             $sender->setPhone($phone);
         }
         $transaction->setSender($sender);
     }
     if (isset($data["shipping"]) && is_array($data["shipping"])) {
         // <transaction> <shipping>
         $shipping = new Shipping();
         // <transaction> <shipping> <type>
         if (isset($data["shipping"]["type"])) {
             $shipping->setType(new ShippingType($data["shipping"]["type"]));
         }
         // <transaction> <shipping> <cost>
         if (isset($data["shipping"]["cost"])) {
             $shipping->setCost($data["shipping"]["cost"]);
         }
         if (isset($data["shipping"]["address"]) && is_array($data["shipping"]["address"])) {
             // <transaction> <shipping> <address>
             $address = new Address();
             // <transaction> <shipping> <address> <street>
             if (isset($data["shipping"]["address"]["street"])) {
                 $address->setStreet($data["shipping"]["address"]["street"]);
             }
             // <transaction> <shipping> <address> <number>
             if (isset($data["shipping"]["address"]["number"])) {
                 $address->setNumber($data["shipping"]["address"]["number"]);
             }
             // <transaction> <shipping> <address> <complement>
             if (isset($data["shipping"]["address"]["complement"])) {
                 $address->setComplement($data["shipping"]["address"]["complement"]);
             }
             // <transaction> <shipping> <address> <city>
             if (isset($data["shipping"]["address"]["city"])) {
                 $address->setCity($data["shipping"]["address"]["city"]);
             }
             // <transaction> <shipping> <address> <state>
             if (isset($data["shipping"]["address"]["state"])) {
                 $address->setState($data["shipping"]["address"]["state"]);
             }
             // <transaction> <shipping> <address> <district>
             if (isset($data["shipping"]["address"]["district"])) {
                 $address->setDistrict($data["shipping"]["address"]["district"]);
             }
             // <transaction> <shipping> <address> <postalCode>
             if (isset($data["shipping"]["address"]["postalCode"])) {
                 $address->setPostalCode($data["shipping"]["address"]["postalCode"]);
             }
             // <transaction> <shipping> <address> <country>
             if (isset($data["shipping"]["address"]["country"])) {
                 $address->setCountry($data["shipping"]["address"]["country"]);
             }
             $shipping->setAddress($address);
         }
         // <transaction> <shipping>
         $transaction->setShipping($shipping);
     }
     return $transaction;
 }