var profile = {
<?php
if($sf_user->isAuthenticated())
{
  $username = $sf_user->getUsername();
  $user = $sf_user->getAccount();
  $user_debt = $user->getDebt();
  if ($user->getManagerId())
  {
    $manager = Doctrine::getTable('User')->findOneById($user->getManagerId());  
  }

  $user_order = Doctrine_Query::create()
    ->select("oi.client_order_id AS order_id, oi.product_count, oi.id AS product_id, p.title AS title, pp.value AS cost, os.title AS status, co.created_at AS order_time")
    ->from("OrderItem oi")
    ->leftJoin("oi.ClientOrder co")
    ->leftJoin("co.OrderStatus os")
    ->leftJoin("oi.Product p")
    ->leftJoin("p.ProductPrice pp")
    ->where("co.user_id = ?", $user->getId())
    ->andWhere("pp.price_type_id = 1")
    ->orderBy("co.created_at DESC")
    ->fetchArray();

/*    $user_order = Doctrine_Query::create()
      ->select("co.*,os.title as status")
    ->from("ClientOrder co")
    ->leftJoin("co.OrderStatus os")
    ->where("co.user_id = ?", $user->getId())
    ->orderBy("co.id", "ASC")
    ->execute();*/

/*print_r($user_order);
die();*/
  $q_corr = Doctrine_Query::create()
    ->select("c.*, uf.*, ut.*")
    ->from("Correspondence c")
    ->leftJoin("c.UserFrom uf")
    ->leftJoin("c.UserTo ut")
    ->where("c.user_id_from = ?", $user->getId())
    ->orWhere("c.user_id_to = ?", $user->getId())
    ->orderBy("c.created_at", "ASC");
   
  $corr = $q_corr->execute();

  $order_detail = array();
  $debt_detail = array();

  echo "auth: true,\n";
  echo "username: '" . $username . "',\n";
  echo "organization: '" . $user->getOrganization() . "',\n";
  echo "phone: '" . $user->getPhoneNumber() . "',\n";
  echo "city: '" . $user->getCity() . "',\n";
  echo "address: '" . $user->getAddress() . "',\n";
  echo "email: '" . $user->getEmail() . "',\n";
  echo "orders_sum_last_month: '" . $user->getOrdersSumLastMonth() . "',\n";
  echo "orders_sum_current: '" . $user->getOrdersSumCurrent() . "',\n";
  echo "sum_limit: '" . $user->getLimitBySum() . "',\n";
  echo "sum_discount: '" . $user->getCurrentDiscount() . "',\n";
  echo "debt: '" . $user->getCurrentDebt() . "',\n";
  echo "manager:[";
  if (isset($manager))
  {
    echo "{name: '" . $manager->getFname() . ' ' . $manager->getIname() . "',\n";
    echo "phone: '" . $manager->getPhoneNumber() . "'},\n";
  }
  else
  {
    echo "{name: 'отдел продаж'},\n";
  }
  echo "],\n";

  echo "debt_detail:[";
  foreach ($user_debt as $debt)
  { 
    echo "{debt_sum: '" . $debt->getDebtSum() . "',\n";
    echo "deadline: '" . ProjectUlils::rusDate($debt->getDeadline()) . "'},\n";
  }
  echo "],\n";
  
/*  echo "orders:[";
  foreach ($user_order as $product)
  { 
    echo "{id: '" . $product->getId() . "',\n";
    echo "status: '" . $product->getStatus() . "',\n";
    echo "date: '" . ProjectUlils::rusDate($product->getCreatedAt(), true) . "'},\n";
  }
  echo "],\n";*/

/*print_r($user_order);
      die();*/
  echo "orders:[";
  if (count($user_order) > 0)
  {
    $order_id = 'any_string';
    $order_count_prod = 0;
    $order_price_prod = 0;
    $count_help = 0;
    for($i = 0; $i < count($user_order); $i++)
    {
      /*print_r($user_order);
      die();*/
      $c = $i;
      if ($count_help != $user_order[$i]['order_id'])
      {
        $count_help = $user_order[$i]['order_id'];
        while ($count_help == $user_order[$i]['order_id'])
        {
           //print_r($user_order[$i+1]['order_id']);
          $order_count_prod += $user_order[$i]['product_count'];
          $order_price_prod += $user_order[$i]['cost'] * $user_order[$i]['product_count'];
          if (isset($user_order[$i+1]['order_id']))
          {
            $i++;
          }
          else
          {
            break;
          }
          
        }
      }
      $i = $c;
     /*print_r($c);
      die();*/
      if ($order_id != $user_order[$i]['order_id'])
      {
        echo "{id: '" . $user_order[$i]['order_id'] . "',\n";

        echo "count: '" . $order_count_prod . "',\n";
        echo "date: '" . ProjectUlils::rusDate($user_order[$i]['order_time'], false) . "',\n";
        echo "price: '" . $order_price_prod . "',\n";
        echo "status: '" . $user_order[$i]['status']. "',\n";
        
        echo "goods:[\n";
      
      $order_id = $user_order[$i]['order_id'];
      $order_count_prod = 0;
      $order_price_prod = 0;
      while ($order_id == $user_order[$c]['order_id'])
      {
        echo "{product_id: '" . $user_order[$c]['product_id'] . "',\n";
        echo "product: '" . $user_order[$c]['title'] . "',\n";
        echo "count: '" . $user_order[$c]['product_count'] . "',\n";
        echo "price: '" . ($user_order[$c]['cost'] * $user_order[$c]['product_count']) . "'},\n";
        $c++;
        if (!isset($user_order[$c]['order_id']))
        {
          break;
        }
      }
      $c = $i;
      $order_id = $user_order[$i]['order_id'];
      echo "]\n";
      if (isset($user_order[$i + 1]['order_id']))
      {
        if ($order_id != $user_order[$i + 1]['order_id'])
        {
          echo "},\n";
        }
        elseif ($order_id == $user_order[$i + 1]['order_id'])
        {
          echo "},\n";
        }
      }
      else
      {
        echo "},\n";
      }
      }
    }

    /*foreach ($user_order as $product)
    { 
      if ($order_id != $product->getOrderId())
      {
        echo "{id: '" . $product->getOrderId() . "',\n";
        echo "goods:[\n";
      }
      
      echo "{product_id: '" . $product->getProductId() . "',\n";
      echo "product: '" . $product->getTitle() . "',\n";
      echo "price: '" . $product->getCost() . "',\n";
      echo "count: '" . $product->getProductCount() . "',\n";
      echo "status: '" . $product->getStatus() . "',\n";
      echo "date: '" . ProjectUlils::rusDate($product->getOrderTime(), false) . "'},\n";
     
      if ($order_id != $product->getOrderId())
      {
        echo "{id: '" . $product->getOrderId() . "',\n";
        echo "goods:[\n";
      }
      $order_id = $product->getOrderId();
    }*/
  }
  echo "],\n";

  echo 'feedBack:[';
  foreach ($corr as $message)
  { 
    if ($message['status'] == 'out')
    {
      echo "{name: '" . $message->getUserFrom()->getFname() . ' ' . $message->getUserFrom()->getIname() . "',\n";
      echo "status: 'in',\n";
    }
    else
    {
      echo "{name: '" . $message->getUserFrom()->getOrganization() . "',\n"; 
      echo "status: 'out',\n";
    }

    echo "text: '" . $message->getBody() . "',\n";
    echo "date: '" . ProjectUlils::rusDate($message->getCreatedAt(), false, true) . "'},\n";
  }
  echo "],\n";
}
else
{
  echo "auth:false\n";
}
?>
};