Hash do iFood

Foto de Anderson

Categoria: 

iFood

Exemplo de tratamento do arquivo do pedido iFood para Harbour ou xHarbour baixado:

#include "hbclass.ch"
#include "inkey.ch"
#include "hbclass.ch"
#include "directry.ch"
#include "set.ch"

FUNCTION ImportaArquivo( cFileName)

   LOCAL aPagamento, aItem, aSubItem, aBenefits, aShipValues

   aPedido := hb_JsonDecode( hb_MemoRead( cFileName ) )    // Este é o arquivo JSON

   xFormaE := aPedido[ "orderType" ]
   xFormaE := IIF( xFormaE = "DELIVERY", 'D' ,IF(xFormaE = "INDOOR", 'M' , 'B' ))      // Delivery = entrega  Pickup = balcao   Indoor = Mesa

   xDataEnt:= xDataE := ""
   xID     := aPedido[ "id" ]
   xTipoVe := AllTrim( aPedido[ "orderType" ] )

   lErro   := .T.
   BEGIN SEQUENCE WITH __BreakBlock()
      zmesa   := aPedido[ "indoor" ][ "table" ]
      xFormaE := IF( xFormaE = "M" .AND. zMesa = "BALCAO", "B", "M" )
      lErro   := .F.
   ENDSEQUENCE
   IF lErro
      zmesa := ""
   ENDIF

   xQuando := IF( aPedido[ "orderTiming" ] = "IMMEDIATE",'I','P')   // Delivery/Entrega Imediata ou Programada
   xCanal  := aPedido[ "salesChannel" ]
   xDataRet:= IF( xQuando = "I","IMEDIATA","")

   // Dados da Entrega  DELIVERY
   IF xFormaE = "D"
      xModo   := aPedido[ "delivery" ][ "mode" ]
      xQuemE  := aPedido[ "delivery" ][ "deliveredBy" ]
      zdata   := aPedido[ "delivery" ][ "deliveryDateTime" ]
      xDataRet:= IIF( xQuando = "I","IMEDIATA","")
      xEndere :=  aPedido[ "delivery" ][ "deliveryAddress" ][ "formattedAddress" ]
      xEndere :=  Upper( XmlTransform( xEndere ) )
      xBairro :=  Upper( XmlTransform( Trim( aPedido[ "delivery" ][ "deliveryAddress" ][ "neighborhood" ] )))
      lErro   := .T.
      BEGIN SEQUENCE WITH __BreakBlock()
         xcomple := Trim(Upper( XmlTransform(( aPedido[ "delivery" ][ "deliveryAddress" ][ "complement" ]))))
         xEndere := xEndere + " "+xcomple
         lErro   := .F.
      ENDSEQUENCE
      IF lErro
         xcomple := ""
      ENDIF
      lErro   := .T.
      BEGIN SEQUENCE WITH __BreakBlock()
         xRefere := Trim(Upper( XmlTransform(( aPedido[ "delivery" ][ "deliveryAddress" ][ "reference" ]))))
         lErro   := .F.
      ENDSEQUENCE
      IF lErro
         xRefere := ""
      ENDIF
      xCEP    := aPedido[ "delivery" ][ "deliveryAddress" ][ "postalCode" ]
      xCidade := Trim(Upper( aPedido[ "delivery" ][ "deliveryAddress" ][ "city" ]))
      xLatitud:= aPedido[ "delivery" ][ "deliveryAddress" ][ "coordinates" ][ "latitude" ]
      xLongtud:= aPedido[ "delivery" ][ "deliveryAddress" ][ "coordinates" ][ "longitude" ]
   ENDIF

   zPedido := aPedido[ "displayId" ]
   zdata   := aPedido[ "createdAt" ]
   xData   := DateFromJson( zData )
   xHora   := Substr(zdata,12,5)
   zdata   := aPedido[ "preparationStartDateTime" ]

   // Dados do Cliente
   xIDCli    := aPedido[ "customer" ][ "id" ]
   xNome     :=  Upper( XmlTransform (aPedido[ "customer" ][ "name" ] ) )
   lErro     := .T.
   BEGIN SEQUENCE WITH __BreakBlock()
      xTelefone := aPedido[ "customer" ][ "phone" ][ "localizer" ]
      lErro  := .F.
   ENDSEQUENCE
   IF lErro
      xTelefone   := zPedido
   ENDIF
   xIDpedi   := aPedido[ "customer" ][ "ordersCountOnMerchant" ]
   lErro     := .T.
   BEGIN SEQUENCE WITH __BreakBlock()
      xCPF   := aPedido[ "customer" ][ "documentNumber" ]
      lErro  := .F.
   ENDSEQUENCE
   IF lErro
      xCPF   := ""
   ENDIF

   // Dados da Retirada  PICKUP_AREA
   IF xTipoVe = "TAKEOUT"
      xModoRet:= aPedido[ "takeout" ][ "mode" ]
      zdata   := aPedido[ "takeout" ][ "takeoutDateTime" ]
      xDataRet:= Dtoc(DateFromJson( zData ) ) +" as "+Substr(zdata,12,5)+"hs."
      lErro := .T.
      BEGIN SEQUENCE WITH __BreakBlock()
         xObsRet := aPedido[ "takeout" ][ "observations" ]
         lErro := .F.
      ENDSEQUENCE
      IF lErro
         xObsRet := ""
      ENDIF
   ENDIF

   // Dados da Mesa  INDOOR
   xObsI := ""
   IF xFormaE = "M"
      xModoI  := aPedido[ "indoor" ][ "mode" ]
      xmesaI  := Val( Subst( aPedido[ "indoor" ][ "table" ],5,3 ) )
      zdata   := aPedido[ "indoor" ][ "deliveryDateTime" ]
      xDataI  := Dtoc( DateFromJson( zData ) )+" as "+Substr(zdata,12,5)
      xObsI   := aPedido[ "indoor" ][ "observations" ]
      xDataret:= Dtoc( DateFromJson( zData ) ) +" as "+Substr(zdata,12,5)+"hs."
      xObsI   := Dtoc( DateFromJson( zData ) ) +" as "+Substr(zdata,12,5)+"hs."
   ENDIF

   // Dados da Data e ENTREGA Programada  (scheduled)
   IF xQuando = "P" .AND. xFormaE # "M"
      zdata1  := aPedido[ "schedule" ][ "deliveryDateTimeStart" ]
      zdata   := aPedido[ "schedule" ][ "deliveryDateTimeEnd" ]
      xDataret:= Dtoc( DateFromJson( zData ) ) +" das "+Substr(zdata1,12,5)+" as "+Substr(zdata,12,5)+"hs."
   ENDIF

   // Dados do Desconto  BENEFITS
   nDesResta:=nDesIfood:=nTotal:=0;kObsVal:=""
   BEGIN SEQUENCE WITH __BreakBlock()
      FOR EACH aBenefits IN aPedido[  "benefits" ]
         nTotal += aBenefits[ "value" ]
         FOR EACH aShipValues IN aBenefits[ "sponsorshipValues" ]
            IF aShipValues[ "name" ] == "MERCHANT"
               IF aShipValues[ "value" ] # 0
                  nDesResta += aShipValues[ "value" ]
                  kObsVal   += "- VOUCHER_RESTAURANTE- R$ "+AllTrim(Str(aShipValues[ "value" ],10,2))
               ENDIF
            ELSE
               nDesIfood += aShipValues[ "value" ]
               IF aShipValues[ "value" ] # 0
                  kObsVal   += "- VOUCHER_IFOOD- R$ "+AllTrim(Str(aShipValues[ "value" ],10,2))
               ENDIF
            ENDIF
         NEXT
      NEXT
   ENDSEQUENCE

   // Totais
   xSubTo := aPedido[ "total" ][ "subTotal" ]
   xTaxa  := aPedido[ "total" ][ "deliveryFee" ]
   xDesco := aPedido[ "total" ][ "benefits" ]
   xPedcl := aPedido[ "total" ][ "orderAmount" ]

   // Forma Pagamento
   xPagoPG:= aPedido[ "payments" ][ "prepaid" ]
   xApagar:= aPedido[ "payments" ][ "pending" ]
   KobsPag:= "PAGAMENTO "

Caso tenha dificuldade em linkar a lib de conversão do JSon para Hash, use esta função abaixo que faz o mesmo:

Function JSontoHash( cStringJson )
/***
* Converte string formato Json em Hash
*/
Local hJson := {=>}
cStringJson := StrTran( cStringJson,':[','=>{')
cStringJson := StrTran( cStringJson,'":"','" => "')
cStringJson := StrTran( cStringJson,'[','{')
cStringJson := StrTran( cStringJson,']','}')
cStringJson := StrTran( cStringJson,'":null','"=>nil')
cStringJson := StrTran( cStringJson,'":true' ,'"=>.t.' )
cStringJson := StrTran( cStringJson,'":false','"=>.f.')
cStringJson := StrTran( cStringJson,'":','"=>')
cStringJson := StrTran( cStringJson,"\/","/" )
hJSon := &( cStringJson )
Return hJson

Faça o download desses programas abaixo.

Downloads: 

ifood.prg — Baixado 258 vezes
Total votes: 0