Árvore de páginas

Versões comparadas

Chave

  • Esta linha foi adicionada.
  • Esta linha foi removida.
  • A formatação mudou.

...

Bloco de código
languagesql
titleExemplo de ponto de entrada mntng.prw
linenumberstrue
collapsetrue
#include "PROTHEUS.ch"
User Function MNTNG()
  
    Local cId := PARAMIXB[1] //Indica o momento da chamada do PE
    Local oWS := PARAMIXB[2] //Objeto com referência ao webservice
    Local oParser
    Local aArea
    Local aAreaSTJ
    Local nInput := 0
    Local aDate
    Local cType
    Local lIsDone
    Local cTask
    Local cCode
    Local nAmount
    Local cOrder
    Local cPlan
    Local aDate
    Local lOk
	Local cAddress := ""
	Local cCodBem  := ""

    If cId == "CREATE_VALID_ORDER" .OR. cId == "UPDATE_VALID_ORDER" //valida inclusão e alteração da ordem
        //Bloco a seguir realiza validações dos insumos
        If FWJsonDeserialize(oWS:GetContent(),@oParser) //Parse da string no formato Json
            If AttIsMemberOf( oParser, "inputs" ) .And. Len( oParser:inputs ) > 0

                cOrder  := IIf( AttIsMemberOf( oParser, 'code' ), oParser:code, '' )
                cPlan   := IIf( AttIsMemberOf( oParser, 'plan'  ), oParser:plan, '000000' )
                //Bloco abaixo realiza validações nos insumos
                For nInput := 1 To Len( oParser:inputs )

                    oInput := oParser:inputs[ nInput ]

                    cType   := IIf( AttIsMemberOf( oInput , 'type'  ), oInput:type, '' )
                    lIsDone := IIf( AttIsMemberOf( oInput , 'isDone'), oInput:isDone, .F. )
                    
                    //Neste exemplo haverá validações somente para produtos realizados
                    If cType != "P" .Or. !lIsDone 
                        Loop
                    EndIf

                    aDate   := { dDatabase, '08:00' }
                    lOk     := .T.

                    If AttIsMemberOf( oInput, "startDate" )
                        aDate := StrTokArr( oInput:startDate, " " )
                    ElseIf AttIsMemberOf( oInput, "date" )
                        aDate := StrTokArr( oInput:date, " " )
                    EndIf
                    aDate := { STOD( aDate[1] ), aDate[2] }
                    
                    //Neste exemplo haverá validações somente quando informar data anterior a data atual
                    If aDate[1] >= dDatabase
                        Loop
                    EndIf

                    cTask   := IIf( AttIsMemberOf( oInput , 'task'), oInput:task, '' )
                    cCode   := IIf( AttIsMemberOf( oInput , 'code'), oInput:code, '' )
                    nAmount := IIf( AttIsMemberOf( oInput , 'amount'), oInput:amount, 0 )

                    If cId == "CREATE_VALID_ORDER"
                        lOk := .F.
                    Else
                        
                        /*Trecho abaixo verifica se o insumo já está gravado no banco
                            caso exista não há necessidade de validação*/

                        dbSelectArea("STL")
                        dbSetOrder( 1 )
                        If !dbSeek( xFilial('STL') + PADR( cOrder, Len( STL->TL_ORDEM ) )+ ;
                                    PADR( cPlan, Len( STL->TL_PLANO ) ) + PADR( cTask, Len( STL->TL_TAREFA ) ) + ;
                                    cType + PADR( cCode, Len( STL->TL_CODIGO ) ) )
                            lOk := .F.
                        Else
                            lOk := .F.
                            //Verificação abaixo para garantir que o insumo não existe no banco
                            While !STL->( Eof() ) .AND. STL->TL_FILIAL + STL->TL_ORDEM + STL->TL_PLANO +;
                                STL->TL_TAREFA + STL->TL_TIPOREG + STL->TL_CODIGO == xFilial('STL') + PADR( cOrder, Len( STL->TL_ORDEM ) )+ ;
                                    PADR( cPlan, Len( STL->TL_PLANO ) ) + PADR( cTask, Len( STL->TL_TAREFA ) ) + ;
                                    cType + PADR( cCode, Len( STL->TL_CODIGO ) )

                                If Val( STL->TL_SEQRELA ) > 0 ; //verifica se é insumo realizado
                                    .And. nAmount == STL->TL_QUANTID //comparação de quantidade
                                    lOk := .T. //já está gravado e não há necessidade de validar
                                    Exit
                                EndIf
                                dbSelectArea("STL")
                                dbSkip()
                            EndDo
                        EndIf
                    EndIf

                    If !lOk
                        Return 'Não é permitido realizar insumos do tipo produto com a data retroativa. ' + ;
                                    'Verifique o insumo/produto ' + cCode + ' data ' + Dtoc( aDate[1] )
                    EndIf

                Next nInput
            EndIf
        EndIf
    ElseIf cId == "CANCEL_VALID" //valida cancelamento da ordem
        If FWJsonDeserialize(oWS:GetContent(),@oParser) //Parse da string no formato Json
            If Empty( oParser:message )//verifica campo observação foi passado vazio
                Return "A observação do cancelamento é obrigatória."
            EndIf
        EndIf
     
	ElseIf cId == "FINISH_VALID_ORDER"
	
		If FWJsonDeserialize(oWS:GetContent(), @oParser)
			If Empty( oParser:observation ) //verifica campo observação foi passado vazio
                Return "Campo observação deve ser informado."
            EndIf
		EndIf
	
    ElseIf cId == "FINISHED_ORDER"       
        If FWJsonDeserialize(oWS:GetContent(), @oParser)
             
            aArea := GetArea()
            aAreaSTJ := STJ->(GetArea())
 
 
            dbSelectArea("STJ")
            dbSetOrder(1)
            If dbSeek(xFilial("STJ") + oParser:order + oParser:plan)
                Reclock("STJ", .F.)
                STJ->TJ_DTPRINI := STJ->TJ_DTMRINI
                STJ->TJ_HOPRINI := STJ->TJ_HOMRINI
                STJ->TJ_DTPRFIM := STJ->TJ_DTMRFIM
                STJ->TJ_HOPRFIM := STJ->TJ_HOMRFIM
                MsUnlock()
            Endif
 
            RestArea(aAreaSTJ)
            RestArea(aArea)
        Endif
         
    ElseIf cId == "FILTER_PRODUCT" //adiciona filtro para busca de produtos
       Return " AND B1_GRUPO = '97'"

	ElseIf cId == "DESCRIPTION_LOCALIZATION" // Altera descrição de localização do bem
    	cCodBem := PARAMIXB[3]

        dbSelectArea("ST9")
        dbSetOrder(1)
        If dbSeek( xFilial("ST9") + cCodBem ) .And. !Empty( ST9->T9_CLIENTE + ST9->T9_LOJACLI )
            cAddress := Posicione("SA1",1,xFilial("SA1") + ST9->T9_CLIENTE + ST9->T9_LOJACLI,"SA1->A1_END")
        EndIf

        Return Alltrim( cAddress )
	
	ElseIf cId == 'FILTER_ORDER'

    	Return ' AND STJ.TJ_DTMPINI = ' + ValToSQL( cToD( '24/11/2020' ) )
        
    ElseIf cId == 'FILTER_REQUEST'

    	Return ' AND TQB.TQB_DTABER = ' + ValToSQL( cToD( '24/11/2020' ) )
	
	ElseIf cId == 'FINISH_COUNTER_1'

          aAreaSTP := STP->( GetArea() )Return 150
          aAreaSTJ := STJ->( GetArea() )

    ElseIf      cCode  := Posicione( 'STJ', 1, xFilial( 'STJ' ) + oWS:order + oWs:plan, 'TJ_CODBEM' )cId == 'FINISH_COUNTER_2'

          cDate  := StrTokArr( oWs:endDate, ' ' )[1]
          cHour  := oWs:counter[1]:time

          dbSelectArea( 'STP' )
          dbSetOrder( 5 ) // TP_FILIAL + TP_CODBEM + TP_DTLEITU + TP_HORA
          dbSeek( xFilial( 'STP') + cCode + cDate + cHour, .T. )
          
          If STP->( !EoF() )

               If STP->TP_CODBEM == cCode .And. ( STP->TP_DTLEITU <= sToD( cDate ) .Or.;
                    ( STP->TP_DTLEITU == sToD( cDate) .And. STP->TP_HORA <= cHour ) )
                    
                    nCount := STP->TP_POSCONT

               Else

                    dbSkip(-1)

                    If STP->( !BoF() ) .And. STP->TP_CODBEM == cCode

                         nCount := STP->TP_POSCONT
                    
                    EndIf

               EndIf

          Else

               dbSkip(-1)

               If STP->( !BoF() ) .And. STP->TP_CODBEM == cCode

                    nCount := STP->TP_POSCONT
                    
               EndIf

          EndIf

          RestArea( aAreaSTJ )
          RestArea( aAreaSTP )
 
          Return nCount
         
    ElseIf cId == 'FINISH_COUNTER_2'

          aAreaTPP := TPP->( GetArea() )
          aAreaSTJ := STJ->( GetArea() )

          cCode  := Posicione( 'STJ', 1, xFilial( 'STJ' ) + oWS:order + oWs:plan, 'TJ_CODBEM' )
          cDate  := StrTokArr( oWs:endDate, ' ' )[1]
          cHour  := oWs:counter[1]:time

          dbSelectArea( 'TPP' )
          dbSetOrder( 5 ) // TPP_FILIAL + TPP_CODBEM + TPP_DTLEIT + TPP_HORA
          dbSeek( xFilial( 'STP') + cCode + cDate + cHour, .T. )
          
          If TPP->( !EoF() )

               If TPP->TPP_CODBEM == cCode .And. ( TPP->TPP_DTLEIT <= sToD( cDate ) .Or.;
                    ( TPP->TPP_DTLEIT == sToD( cDate ) .And. TPP->TPP_HORA <= cHour ) )
                    
                    nCount := TPP->TPP_POSCONT

               Else

                    dbSkip(-1)

                    If TPP->( !BoF() ) .And. TPP->TPP_CODBEM == cCode

                         nCount := TPP->TPP_POSCON
                    
                    EndIf

               EndIf

          Else

               dbSkip(-1)

               If TPP->( !BoF() ) .And. TPP->TPP_CODBEM == cCode

                    nCount := TPP->TPP_POSCON
                    
               EndIf

          EndIf

          RestArea( aAreaSTJ )
          RestArea( aAreaTPP )
 
          Return nCountReturn 222
 
    EndIf
 
Return