Advertisement
Guest User

Untitled

a guest
Aug 1st, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.73 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: vrtc
  5.  * Date: 08.12.15
  6.  * Time: 11:40
  7.  */
  8. require_once("config.php");
  9.  
  10. $mysqli = new mysqli(DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
  11. if (mysqli_connect_errno()) {
  12.     printf("Подключение к серверу MySQL невозможно. Код ошибки: %s\n", mysqli_connect_error());
  13.     exit;
  14. }
  15.  
  16. $mysqli->set_charset("utf8");
  17.  
  18. $query = $mysqli->query("SELECT * FROM `" . DB_PREFIX . "order` WHERE kkm = 0 AND payment_code= 'payanyway_pcard'");
  19.  
  20. if ($query->num_rows == 0) {
  21.     return null;
  22. } else {
  23.  
  24.     $i = 0;
  25.     while ($row = $query->fetch_array(MYSQLI_ASSOC)) {
  26.         $orders[$i] = $row;
  27.  
  28.         $i++;
  29.     }
  30.  
  31.  
  32.     $getInfo = getInfo();
  33.  
  34.  
  35.     //если смена закрыта
  36.     if (isset($getInfo['Info']['SessionState']) && $getInfo['Info']['SessionState'] == 3) {
  37.         $Zreport = [
  38.             'Command' => "ZReport",
  39.             'NumDevice' => 2,
  40.             'CashierName' => ''
  41.  
  42.         ];
  43.         $zResult = connect($Zreport);
  44.  
  45.         OpenShift();
  46.         $getInfo = getInfo();
  47.  
  48.     }
  49.     //если смена открыта , но давно
  50.     if (isset($getInfo['Info']['SessionState']) && $getInfo['Info']['SessionState'] == 1) {
  51.         OpenShift();
  52.         $getInfo = getInfo();
  53.     }
  54.  
  55.  
  56.     if (isset($getInfo['Info']['SessionState']) && $getInfo['Info']['SessionState'] == 2) {
  57.         foreach ($orders as $order) {
  58.             $queryP = $mysqli->query("SELECT * FROM `" . DB_PREFIX . "order_product` WHERE order_id = " . (int)$order['order_id'] . " ");
  59.  
  60.             while ($row = $queryP->fetch_array(MYSQLI_ASSOC)) {
  61.                 $products[$i] = $row;
  62.  
  63.                 $i++;
  64.             }
  65.  
  66.             $kkmProducts = [];
  67.  
  68.             foreach ($products as $product) {
  69.                 $kkmProducts[] = [
  70.                     'Register' => [
  71.                         'Name' => $product['name'],
  72.                         'Quantity' => (int)$product['quantity'],
  73.                         'Price' => (int)$product['price'],
  74.                         'Amount' => (int)$product['total'],
  75.  
  76.                     ]
  77.                 ];
  78.             }
  79.  
  80.             $RegisterCheck = [
  81.                 'Command' => "RegisterCheck",
  82.                 'NumDevice' => 2,
  83.                 'IsFiscalCheck' => true,
  84.                 'NotPrint' => false,
  85.                 'ClientAddress' => $order['email'],
  86.                 'CheckStrings' => $kkmProducts,
  87.                 'Cash' => 0,
  88.                 'CashLessType1' => (int)$order['total']
  89.  
  90.             ];
  91.  
  92.             // var_dump($RegisterCheck); exit;
  93.  
  94.  
  95.             $RegisterCheck = connect($RegisterCheck);
  96.  
  97.             //если чек напечатан
  98.             if (isset($RegisterCheck['Status']) && ($RegisterCheck['Status'] == 1 || $RegisterCheck['Status'] == 0)) {
  99.                 $mysqli->query("UPDATE `" . DB_PREFIX . "order` SET kkm = 1 WHERE order_id = " . (int)$order['order_id'] . "  ");
  100.             }
  101.  
  102.             //var_dump($RegisterCheck);
  103.  
  104.         }
  105.  
  106.  
  107.     }
  108. }
  109.  
  110. function OpenShift()
  111. {
  112.     $OpenShift = [
  113.         'Command' => "OpenShift",
  114.         'NumDevice' => 2,
  115.         'CashierName' => ''
  116.  
  117.     ];
  118.     return connect($OpenShift);
  119. }
  120.  
  121. function getInfo()
  122. {
  123.     $GetDataKKT = [
  124.         'Command' => "GetDataKKT",
  125.         'NumDevice' => 2,
  126.         'CashierName' => ''
  127.  
  128.     ];
  129.     return connect($GetDataKKT);
  130. }
  131.  
  132. function connect($data)
  133. {
  134.     $user = "";
  135.     $password = "";
  136.     $url = "";
  137.  
  138.     $dataCommand = json_encode($data);
  139.     $ch = curl_init();
  140. //коннектим к кассе
  141.     curl_setopt($ch, CURLOPT_URL, $url."/Execute");
  142.     curl_setopt($ch, CURLOPT_POSTFIELDS, $dataCommand);
  143.     curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
  144.     curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  145.     curl_setopt($ch, CURLOPT_POST, true);
  146. //нельзя в урле передать порт, поэтому тут костыль подставляем
  147.     curl_setopt($ch, CURLOPT_PORT, 1195);
  148.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  149.     curl_setopt($ch, CURLOPT_USERPWD, $user . ':' . $password);
  150.     curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  151.     curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  152.             'Accept: application/json',
  153.             'Content-Type: application/json')
  154.     );
  155.  
  156.     if (curl_exec($ch) === false) {
  157.         //в пизду
  158.         echo 'Curl error: ' . curl_error($ch);
  159.     }
  160.     $errors = curl_error($ch);
  161.     $result = curl_exec($ch);
  162.     $returnCode = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE);
  163.     curl_close($ch);
  164. //код ответа
  165.     echo $returnCode;
  166. //ошибки если есть
  167.     var_dump($errors);
  168. //тело ответа
  169.     return json_decode($result, true);
  170. }
  171.  
  172. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement