<?php

/*REFERÊNCIAS*/
/*https://www.w3schools.com/js/js_json_php.asp*/
/*https://secure.php.net/manual/pt_BR/book.json.php*/


/*
	$host = "mysql02.ibdeias.hospedagemdesites.ws";
	$user = "ibdeias1";
	$pass = "rootbanco";
	$database = "ibdeias1";

  $conexao = mysqli_connect($host, $user, $pass, $database) or die(mysqli_error($conexao) . "<br>Erro na query: <br>");

  mysqli_select_db($conexao, $database) or die (mysqli_error($conexao) . "<br>Erro na query: <br>");
  
*/



include_once("connect3.php");

//obter o codigo de uma venda modelo atraves do get:

$id_venda = $_GET['order_id'];

//echo "O valor de ORDER_ID => " . $id_venda;





    $consulta_venda = "select * from oc_order where order_id = $id_venda";

  
//echo "olalalalalaa";

      $resultado = mysqli_query($conexao, $consulta_venda) or die (mysqli_error($conexao) . "<br>Erro na query: <br>". $consulta);
		  
          if (mysqli_num_rows($resultado) > 0) {
             while($row = mysqli_fetch_assoc($resultado)){



                $cabec['invoice_prefix']              = $row['invoice_prefix'];                     // 'MOB-2020-00'                    -- nao utilizam (ref. mobile) -- SET invoice_prefix
                $cabec['store_id']                    = $row['store_id'];                           // '0'                              -- store_id  
                $cabec['store_name']                  = $row['store_name'];                         // 'Saude e Sabor'                  -- store_name
                $cabec['store_url']                   = $row['store_url'];                          //'https://jp.saudeesabor.co.jp/'   -- store_url
                $cabec['customer_id']                 = $row['customer_id'];                        //'6347'                            -- customer_id 
                $cabec['customer_group_id']           = $row['customer_group_id'];                  //'1'                               -- customer_group_id 
                $cabec['firstname']                   = $row['firstname'];                          //'Reginaldo'                      -- firstname 
                $cabec['lastname']                    = $row['lastname'];                           //'Gomes'                          -- lastname 
                $cabec['email']                       = $row['email'];                              //'regygom@gmail.com'              -- email
                $cabec['telephone']                   = $row['telephone'];                          //'19999999999'                    -- telephone 
                $cabec['custom_field']                = $row['custom_field'];                       //'\"\"'                           -- custom_field 
                $cabec['payment_firstname']           = $row['payment_firstname'];                  //'Reginaldo'                      -- payment_firstname 
                $cabec['payment_lastname']            = $row['payment_lastname'];                   //'Gomes 2'                        -- payment_lastname 
                $cabec['payment_company']             = $row['payment_company'];                    //''                               -- payment_company
                $cabec['payment_address_1']           = $row['payment_address_1'];                  //'Rua teste'                      -- payment_address_1
                $cabec['payment_address_2']           = $row['payment_address_2'];                  //''                               -- payment_address_2
                $cabec['payment_city']                = $row['payment_city'];                       //'Americana'                      -- payment_city 
                $cabec['payment_postcode']            = $row['payment_postcode'];                   //'1346812'                        -- payment_postcode
                $cabec['payment_country']             = $row['payment_country'];                    //'Brazil'                         -- payment_country
                $cabec['payment_country_id']          = $row['payment_country_id'];                 //'30'                             -- payment_country_id (oc_country => 30=BR, 107=JA)
                $cabec['payment_zone']                = $row['payment_zone'];                       //'Sao Paulo'                      -- payment_zone
                $cabec['payment_zone_id']             = $row['payment_zone_id'];                    //'464'                            -- payment_zone_id
                $cabec['payment_address_format']      = $row['payment_address_format'];             //''                               -- payment_address_format
                $cabec['payment_custom_field']        = $row['payment_custom_field'];               //''                               -- payment_custom_field 
                $cabec['payment_method']              = $row['payment_method'];                     //''                               -- payment_method
                $cabec['payment_code']                = $row['payment_code'];                       //''                               -- payment_code  
                $cabec['shipping_firstname']          = $row['shipping_firstname'];                 //'Reginaldo'                      -- shipping_firstname
                $cabec['shipping_lastname']           = $row['shipping_lastname'];                  //'Gomes 2'                        -- shipping_lastname 
                $cabec['shipping_company']            = $row['shipping_company'];                   //''                               -- shipping_company 
                $cabec['shipping_address_1']          = $row['shipping_address_1'];                 //'Rua teste'                      -- shipping_address_1 
                $cabec['shipping_address_2']          = $row['shipping_address_2'];                 //''                               -- shipping_address_2 
                $cabec['shipping_city']               = $row['shipping_city'];                      //'Americana'                      -- shipping_city 
                $cabec['shipping_postcode']           = $row['shipping_postcode'];                  //'1346812'                        -- shipping_postcode 
                $cabec['shipping_country']            = $row['shipping_country'];                   //'Brazil'                         -- shipping_country
                $cabec['shipping_country_id']         = $row['shipping_country_id'];                //'30'                             -- shipping_country_id 
                $cabec['shipping_zone']               = $row['shipping_zone'];                      //'Sao Paulo'                      -- shipping_zone 
                $cabec['shipping_zone_id']            = $row['shipping_zone_id'];                   //'464'                            -- shipping_zone_id
                $cabec['shipping_address_format']     = $row['shipping_address_format'];            //''                               -- shipping_address_format 
                $cabec['shipping_custom_field']       = $row['shipping_custom_field'];              //''                               -- shipping_custom_field 
                $cabec['shipping_method']             = $row['shipping_method'];                    //''                               -- shipping_method 
                $cabec['shipping_code']               = $row['shipping_code'];                      //''                               -- shipping_code 
                $cabec['comment']                     = $row['comment'];                            //''                               -- comment 
                $cabec['total']                       = $row['total'];                              //'1980'                           -- total 
                $cabec['affiliate_id']                = $row['affiliate_id'];                       //'0'                              -- affiliate_id 
                $cabec['commission']                  = $row['commission'];                         //'0'                              -- commission  
                $cabec['marketing_id']                = $row['marketing_id'];                       //'0'                              -- marketing_id 
                $cabec['tracking']                    = $row['tracking'];                           //''                               -- tracking 
                $cabec['language_id']                 = $row['language_id'];                        //'2'                              -- language_id 
                $cabec['currency_id']                 = $row['currency_id'];                        //'4'                              -- currency_id 
                $cabec['currency_code']               = $row['currency_code'];                      //'JPY'                            -- currency_code 
                $cabec['currency_value']              = $row['currency_value'];                     //'1'                              -- currency_value
                $cabec['ip']                          = $row['ip'];                                 //'177.185.220.131'                -- ip 
                $cabec['forwarded_ip']                = $row['forwarded_ip'];                       //''                               -- forwarded_ip 
                $cabec['user_agent']                  = $row['user_agent'];                         //'App Mobile Android'             -- user_agent 
                $cabec['accept_language']             = $row['accept_language'];                    //'pt,en-US;q=0.9,en;q=0.8,ca;..   -- accept_language 
                $cabec['date_added']                  = $row['date_added'];                         //NOW()                            -- date_added  
                $cabec['date_modified']               = $row['date_modified'];                      //NOW()                            -- date_modified
                $cabec['fax']                         = $row['fax'];                                //''                               -- fax
                $cabec['deldttime_timeslot']          = $row['deldttime_timeslot'];                 //''                               -- deldtime_timeslot

                $cabec['observacao'] = 'teste de comentario';
             
                //$produtos_list[] = $produto;
                
            }

/*
            echo "<pre>";
            print_r($cabec);
            echo "</pre>";
*/

            $cabec = array_map('utf8_encode', $cabec); //resolvendo os problemas de acentuacao

            //echo json_encode($cabec, JSON_UNESCAPED_UNICODE);
            //exit;


            $consulta_itens = "select * from oc_order_product where order_id = $id_venda";

            //echo "olalalalalaa";

                  $resultado_itens = mysqli_query($conexao, $consulta_itens) or die (mysqli_error($conexao) . "<br>Erro na query: <br>". $consulta);
                  
                      if (mysqli_num_rows($resultado_itens) > 0) {
                         while($row = mysqli_fetch_assoc($resultado_itens)){



                            $item['order_id']       = $row['order_id'];                             // 14               -- order_id
                            $item['product_id']     = $row['product_id'];                           //,155              -- product_id
                  //          $item['name']           = $row['name'];                                 //,'Quibe'          -- name
                            $item['model']          = $row['model'];                                //,'product_id=14'  -- model
                            $item['quantity']       = $row['quantity'];                             //,3                -- quantity
                            $item['price']          = $row['price'];                                //,600.0000         -- price
                            $item['total']          = $row['total'];                                //,1800.0000        -- total
                            $item['tax']            = $row['tax'];                                  //,0.0000           -- tax
                            $item['reward']         = $row['reward'];                               //,18               -- reward                                                     
                         
                            $item = array_map('utf8_encode',$item);


                            //echo json_encode($item, JSON_UNESCAPED_UNICODE);
                            //exit;
                         
                            $itens_list[] = $item;
                        }


                      //json_encode($array, JSON_UNESCAPED_UNICODE)

                $status = array( 'cabecalho'   => $cabec
                                ,'itens'       => $itens_list
                                );

                
                //$status = array_map('utf8_encode', $status);

                $json = json_encode($status, JSON_UNESCAPED_UNICODE);
        
        
              echo $json;
                exit(0);
        
             
              }
            }
 


          $dados = array(
            'dados' => $json,
          );

          //echo $json;
          //exit;




         // $iniciar = curl_init('http://www.ibdeias.tecnologia.ws/recebe_tabela_inscricao.php');
           $iniciar = curl_init('http://localhost/saudesabor/recebe_venda2.php');
          
          
                                                                      
          curl_setopt($iniciar, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
          curl_setopt($iniciar, CURLOPT_POSTFIELDS, $dados);                                                                         
          curl_setopt($iniciar, CURLOPT_RETURNTRANSFER, true);                                                                      
 /*
          curl_setopt($iniciar, CURLOPT_HTTPHEADER, array(                                                                          
              'Content-Type: application/json',                                                                                
              'Content-Length: ' . strlen($json))                                                                       
          );                                                                                                                   
 */


          $result = curl_exec($iniciar);
          echo "<pre>";
          echo $result;
          echo "</pre>";

          if(!curl_exec($iniciar)){
            die('Error: "' . curl_error($iniciar) . '" - Code: ' . curl_errno($iniciar));
          }
          
        
          curl_close($iniciar);
          

          
?>