<commit_before><?php
/*
  Пример создания заказа
*/

if (isset($_POST))
{
  define("MULTISHIP_DEBUG", true);

  // Подключаем необходимые библиотеки
  require_once "../multiship/multiship.php";
  require_once "../multiship/objects/order.php";
  require_once "../multiship/objects/order_item.php";
  require_once "../multiship/objects/recipient.php";
  require_once "../multiship/objects/delivery.php";
  require_once "../multiship/objects/delivery_point.php";
  require_once "../multiship/objects/request_delivery_list.php";

  // Получаем рабочий экземпляр API
  $ms_api = MultiShip::init();

  // Формируем объекты для создания заказа
  $order = new MultiShip_Order();
  $orderitem = new MultiShip_OrderItem();
  $recipient = new MultiShip_Recipient();
  $delivery = new MultiShip_Delivery();
  $delivery_point = new MultiShip_DeliveryPoint();

  // Заполняем объекты данными из формы
  foreach ($_POST as $key => $arg)
  {
    if (strpos($key, 'order_item') === 0)
    {
      $key = substr($key, 11);
      if (isset($orderitem->{$key}))
        $orderitem->{$key} = $arg[0];
      continue;
    }
    // Ключ имеет формат <имя объекта>_<имя поля> - разбиваем и инициализируем объекты
    $key = explode("_", $key, 2);
    if ($key[0] == "order")
      if (isset($order->{$key[1]}))
        $order->{$key[1]} = $arg;

    if ($key[0] == "recipient")
      if (isset($recipient->{$key[1]}))
        $recipient->{$key[1]} = $arg;
    if ($key[0] == "delivery")
      if (isset($delivery->{$key[1]}))
        $delivery->{$key[1]} = $arg;
    if ($key[0] == "deliverypoint")
      if (isset($delivery_point->{$key[1]}))
        $delivery_point->{$key[1]} = $arg;
  }

  $order->requisite = isset($ms_api->requisite_id[0]) ? $ms_api->requisite_id[0] : 0;
  $order->warehouse = isset($ms_api->warehouse_id[0]) ? $ms_api->warehouse_id[0] : 0;
  $order->sender = isset($ms_api->sender_id[0]) ? $ms_api->sender_id[0] : 0;

  if (isset($_POST['searchDeliveries']))
  {
    echo "Выберите вариант доставки";
  }

  // Создаём заказ, если нажата кнопка сохранения заказа
  if (isset($_POST['createOrder']))
  {
    $order->appendItem($orderitem);

    $order_new = $ms_api->createOrder($order, $recipient, $delivery, $delivery_point);

    if ($order_new == false && $ms_api->_error)
    {
      echo $ms_api->_error;
    }

    // Если заказ успешно сохранился - обновляем данные формы
    if ($order_new->status == "ok")
    {
      foreach ($order_new->data as $key => $value)
      {
        $_POST[$key] = $value;
      }
      echo "Заказ успешно создан. ID заказа: " . $order_new->data->order_id;
    }
    elseif ($order_new->status == "error")
    {
      echo "Ошибка: " . $order_new->data;
    }
  }

  // Собираем список вариантов доставки (см. deliverySearch.php для подробного описания)
  $delivery_list_request = new MultiShip_RequestDeliveryList(@$_POST['sentfrom_city'], @$_POST['deliverypoint_city'], @$_POST['order_weight'], @$_POST['order_width'], @$_POST['order_height'], @$_POST['order_length'], '', @$_POST['deliverypoint_index']);
  $delivery_list_request->delivery_type = "todoor";
  $to_door = $ms_api->searchDeliveryList($delivery_list_request, $delivery_point);
  $delivery_list_request->delivery_type = "pickup";
  $pickup = $ms_api->searchDeliveryList($delivery_list_request);

  $delivery_list_view = '';
  if (isset($to_door->data) && is_array($to_door->data))
  {
    foreach ($to_door->data as $variant)
    {
      $delivery_list_view .= '<li onclick="setDeliveryParam(this);" class="delivery_variant" delivery="' . $variant->delivery_id . '" direction="' . $variant->direction_id . '" price="' . $variant->price_id . '" pickuppoint="0" cost="' . $variant->cost . '">' . $variant->delivery_name . " (" . $variant->cost . " руб.)<br/><b>Доставка до двери</b></li>";
    }
  }
  if (isset($pickup->data) && is_array($pickup->data))
  {
    foreach ($pickup->data as $variant)
    {
      $delivery_list_view .= '<li onclick="setDeliveryParam(this);" class="delivery_variant" delivery="' . $variant->delivery_id . '" direction="' . $variant->direction_id . '" price="' . $variant->price_id . '" pickuppoint="' . $variant->pickuppoint_id . '" cost="' . $variant->cost . '">' . $variant->delivery_name . " (" . $variant->cost . " руб.)<br/>" . $variant->address . "</li>";
    }
  }
}

// Выводим форму создания заказа
?>
<script type="text/javascript">
  function reCalcValues() {
    var sum = parseFloat(document.getElementById('order_item_quantity_0').value) * parseFloat(document.getElementById('order_item_cost_0').value);
    var from_client = sum + parseFloat(document.getElementById('delivery_cost').value);
    document.getElementById('order_cost').value = sum || '';
    document.getElementById('order_total_cost').value = document.getElementById('order_payment_method_1').checked ? from_client || '' : '0';
  }
</script>
<link rel="stylesheet" href="css/multiship.css"/>
<form action='' method='post'>
  <table class="wide">
    <tr>
      <th colspan=2>
        <fieldset class='wide block' style='float:left;'>
          <legend>Данные заказа</legend>
          <div style="overflow-y:scroll;overflow-x:hidden;float:left;height:300px;">
            <legend>Вложения</legend>
            <fieldset class='block goods'>
              <legend>Товарная позиция <a href="#" class="sub">-</a></legend>
              Арт
              <input name='order_item_article[0]' value='<?php echo isset($_POST['order_item_article'][0]) ? $_POST['order_item_article'][0] : 'abc' ?>'><br/>
              Наим.
              <input name='order_item_name[0]' value='<?php echo isset($_POST['order_item_name'][0]) ? $_POST['order_item_name'][0] : 'товар' ?>'><br/>
              К-во<commit_msg>changed displayed cost to cost_with_rules<commit_after><?php
/*
  Пример создания заказа
*/

if (isset($_POST))
{
  define("MULTISHIP_DEBUG", true);

  // Подключаем необходимые библиотеки
  require_once "../multiship/multiship.php";
  require_once "../multiship/objects/order.php";
  require_once "../multiship/objects/order_item.php";
  require_once "../multiship/objects/recipient.php";
  require_once "../multiship/objects/delivery.php";
  require_once "../multiship/objects/delivery_point.php";
  require_once "../multiship/objects/request_delivery_list.php";

  // Получаем рабочий экземпляр API
  $ms_api = MultiShip::init();

  // Формируем объекты для создания заказа
  $order = new MultiShip_Order();
  $orderitem = new MultiShip_OrderItem();
  $recipient = new MultiShip_Recipient();
  $delivery = new MultiShip_Delivery();
  $delivery_point = new MultiShip_DeliveryPoint();

  // Заполняем объекты данными из формы
  foreach ($_POST as $key => $arg)
  {
    if (strpos($key, 'order_item') === 0)
    {
      $key = substr($key, 11);
      if (isset($orderitem->{$key}))
        $orderitem->{$key} = $arg[0];
      continue;
    }
    // Ключ имеет формат <имя объекта>_<имя поля> - разбиваем и инициализируем объекты
    $key = explode("_", $key, 2);
    if ($key[0] == "order")
      if (isset($order->{$key[1]}))
        $order->{$key[1]} = $arg;

    if ($key[0] == "recipient")
      if (isset($recipient->{$key[1]}))
        $recipient->{$key[1]} = $arg;
    if ($key[0] == "delivery")
      if (isset($delivery->{$key[1]}))
        $delivery->{$key[1]} = $arg;
    if ($key[0] == "deliverypoint")
      if (isset($delivery_point->{$key[1]}))
        $delivery_point->{$key[1]} = $arg;
  }

  $order->requisite = isset($ms_api->requisite_id[0]) ? $ms_api->requisite_id[0] : 0;
  $order->warehouse = isset($ms_api->warehouse_id[0]) ? $ms_api->warehouse_id[0] : 0;
  $order->sender = isset($ms_api->sender_id[0]) ? $ms_api->sender_id[0] : 0;

  if (isset($_POST['searchDeliveries']))
  {
    echo "Выберите вариант доставки";
  }

  // Создаём заказ, если нажата кнопка сохранения заказа
  if (isset($_POST['createOrder']))
  {
    $order->appendItem($orderitem);

    $order_new = $ms_api->createOrder($order, $recipient, $delivery, $delivery_point);

    if ($order_new == false && $ms_api->_error)
    {
      echo $ms_api->_error;
    }

    // Если заказ успешно сохранился - обновляем данные формы
    if ($order_new->status == "ok")
    {
      foreach ($order_new->data as $key => $value)
      {
        $_POST[$key] = $value;
      }
      echo "Заказ успешно создан. ID заказа: " . $order_new->data->order_id;
    }
    elseif ($order_new->status == "error")
    {
      echo "Ошибка: " . $order_new->data;
    }
  }

  // Собираем список вариантов доставки (см. deliverySearch.php для подробного описания)
  $delivery_list_request = new MultiShip_RequestDeliveryList(@$_POST['sentfrom_city'], @$_POST['deliverypoint_city'], @$_POST['order_weight'], @$_POST['order_width'], @$_POST['order_height'], @$_POST['order_length'], '', @$_POST['deliverypoint_index']);
  $delivery_list_request->delivery_type = "todoor";
  $to_door = $ms_api->searchDeliveryList($delivery_list_request, $delivery_point);
  $delivery_list_request->delivery_type = "pickup";
  $pickup = $ms_api->searchDeliveryList($delivery_list_request);

  $delivery_list_view = '';
  if (isset($to_door->data) && is_array($to_door->data))
  {
    foreach ($to_door->data as $variant)
    {
      $delivery_list_view .= '<li onclick="setDeliveryParam(this);" class="delivery_variant" delivery="' . $variant->delivery_id . '" direction="' . $variant->direction_id . '" price="' . $variant->price_id . '" pickuppoint="0" cost="' . $variant->cost_with_rules . '">' . $variant->delivery_name . " (" . $variant->cost_with_rules . " руб.)<br/><b>Доставка до двери</b></li>";
    }
  }
  if (isset($pickup->data) && is_array($pickup->data))
  {
    foreach ($pickup->data as $variant)
    {
      $delivery_list_view .= '<li onclick="setDeliveryParam(this);" class="delivery_variant" delivery="' . $variant->delivery_id . '" direction="' . $variant->direction_id . '" price="' . $variant->price_id . '" pickuppoint="' . $variant->pickuppoint_id . '" cost="' . $variant->cost_with_rules . '">' . $variant->delivery_name . " (" . $variant->cost_with_rules . " руб.)<br/>" . $variant->address . "</li>";
    }
  }
}

// Выводим форму создания заказа
?>
<script type="text/javascript">
  function reCalcValues() {
    var sum = parseFloat(document.getElementById('order_item_quantity_0').value) * parseFloat(document.getElementById('order_item_cost_0').value);
    var from_client = sum + parseFloat(document.getElementById('delivery_cost').value);
    document.getElementById('order_cost').value = sum || '';
    document.getElementById('order_total_cost').value = document.getElementById('order_payment_method_1').checked ? from_client || '' : '0';
  }
</script>
<link rel="stylesheet" href="css/multiship.css"/>
<form action='' method='post'>
  <table class="wide">
    <tr>
      <th colspan=2>
        <fieldset class='wide block' style='float:left;'>
          <legend>Данные заказа</legend>
          <div style="overflow-y:scroll;overflow-x:hidden;float:left;height:300px;">
            <legend>Вложения</legend>
            <fieldset class='block goods'>
              <legend>Товарная позиция <a href="#" class="sub">-</a></legend>
              Арт
              <input name='order_item_article[0]' value='<?php echo isset($_POST['order_item_article'][0]) ? $_POST['order_item_article'][0] : 'abc' ?>'><br/>
              Наим.
              <input name='order_item_name[0]' value='<?php echo isset($_POST['order_item_name'][0]) ? $_POST['order_item_name'][0] : 'товар' ?>'><br/>
              К-во
{'fraction_non_alphanumeric': 0.18844614581520794, 'fraction_numerical': 0.007134156951452932, 'mean_word_length': 3.807611877875366, 'pattern_counts': {'":': 0, '<': 73, '<?xml version=': 0, '>': 183, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6571302', 'n_tokens_mistral': 4141, 'n_tokens_neox': 4208, 'n_words': 669}