ÿþA L T E R   P R O C E D U R E   P R F T 1 5 0 V a l i d a c a o  
 ( @ I d P r o c e s s a m e n t o   I N T ,        
   @ O r i g e m   s m a l l i n t ,     - -   0 -   A n t e s   d o   - P r o c e s s a m e n t o ,   1   -   A p ó s   P r o c e s s a m e n t o      
   @ I d N o t a F i s c a l   i n t ,      
   @ M e n s a g e m E R R O   v a r c h a r ( m a x )   O u t p u t )      
 A S      
 B E G I N      
   D e c l a r e   @ T i p o I t e m P e r m i t i d o   b i t  
   D e c l a r e   @ V a l o r P a r c e l a s   d e c i m a l  
   D e c l a r e   @ V a l o r T o t a l P e d i d o   d e c i m a l  
  
   S e t   @ M e n s a g e m E R R O   =   ' '      
  
   - - F r a n c i s c o   # 4 8 1 8 2 4   -   V e r i f i c a ç ã o   s e   a   s é r i e   a c e i t a   p e d i d o s   c o m   p r o d u t o s   e   s e r v i ç o s  
   S e l e c t   @ T i p o I t e m P e r m i t i d o   =   I s N u l l ( s e r i e . T i p o I t e m P e r m i t i d o , 0 )    
   F r o m   t m p P R F T 1 5 0   t   l e f t   j o i n   f i S e r i e N F   s e r i e   o n   t . S e r i e   =   s e r i e . S e r i e  
   W h e r e   t . I d P r o c e s s a m e n t o   =   @ I d P r o c e s s a m e n t o  
    
   s e l e c t   @ V a l o r P a r c e l a s   =   s u m ( V a l o r P a r c e l a )  
   f r o m   t m p P R F T 1 5 0 V e n c i m e n t o  
   w h e r e   I d P r o c e s s a m e n t o   =   @ I d P r o c e s s a m e n t o  
  
   s e l e c t   @ V a l o r T o t a l P e d i d o   =   V a l o r T o t a l P e d i d o    
   f r o m   t m p P R F T 1 5 0 P e d i d o   t m p   l e f t   j o i n   f t P e d i d o   p e d   o n   p e d . I d P e d i d o   =   t m p . I d P e d i d o  
   w h e r e   t m p . I d P r o c e s s a m e n t o   =   @ I d P r o c e s s a m e n t o  
  
   i f   @ V a l o r P a r c e l a s   < >   @ V a l o r T o t a l P e d i d o  
   b e g i n      
   	 S e t   @ M e n s a g e m E r r o   =   @ M e n s a g e m E r r o   +   '   -   V a l o r e s   d e   V e n c i m e n t o   d i f e r e m   d o   v a l o r   t o t a l   d o s   p e d i d o s . '  
   e n d      
  
   I f   @ O r i g e m   =   0   a n d   @ T i p o I t e m P e r m i t i d o   =   0   a n d   e x i s t s    
   (  
       S e l e c t   t o p   1   1  
       F r o m   t m p P R F T 1 5 0 P e d i d o   t m p   l e f t   j o i n   f t P e d i d o I t e m   i t e m   o n   t m p . I d P e d i d o   =   i t e m . I d P e d i d o  
       W h e r e   t m p . I d P r o c e s s a m e n t o   =   @ I d P r o c e s s a m e n t o  
           A n d   i t e m . I d S e r v i c o   i s   n o t   n u l l  
   )  
   b e g i n      
   	 S e t   @ M e n s a g e m E r r o   =   @ M e n s a g e m E r r o   +   '   -   N ã o   é   p o s s í v e l   p r o c e s s a r   a   n o t a   n a   s é r i e   i n d i c a d a   p o i s   a   m e s m a   n ã o   a c e i t a   S e r v i ç o s .   É   n e c e s s á r i o   f a z e r   o   d e s m e m b r a m e n t o   d o   m e s m o . '  
   e n d      
  
   I f   @ O r i g e m   =   0   a n d   @ T i p o I t e m P e r m i t i d o   =   1   a n d   e x i s t s    
   (  
       S e l e c t   t o p   1   1  
       F r o m   t m p P R F T 1 5 0 P e d i d o   t m p   l e f t   j o i n   f t P e d i d o I t e m   i t e m   o n   t m p . I d P e d i d o   =   i t e m . I d P e d i d o  
       W h e r e   t m p . I d P r o c e s s a m e n t o   =   @ I d P r o c e s s a m e n t o  
           A n d   i t e m . I d P r o d u t o   i s   n o t   n u l l  
   )  
   b e g i n      
   	 S e t   @ M e n s a g e m E r r o   =   @ M e n s a g e m E r r o   +   '   -   N ã o   é   p o s s í v e l   p r o c e s s a r   a   n o t a   n a   s é r i e   i n d i c a d a   p o i s   a   m e s m a   n ã o   a c e i t a   P r o d u t o s .   É   n e c e s s á r i o   f a z e r   o   d e s m e m b r a m e n t o   d o   m e s m o . '  
   e n d      
        
   - - V e r i f i c a r   s e   o   r e g i s t r o   a i n d a   e s t á   n a   t a b e l a   t e m p o r a r i a  
   i f   n o t   e x i s t s   ( S e l e c t   t o p   1   1        
         f r o m   t m p P R F T 1 5 0 P e d i d o   t          
         w h e r e   t . I d P r o c e s s a m e n t o   =   @ I d P r o c e s s a m e n t o 	 )      
   b e g i n      
       S e t   @ M e n s a g e m E r r o   =   '   -   O s   p e d i d o s   r e f e r e n t e s   a   e s t e   p r o c e s s a m e n t o   j á   f o i   f i n a l i z a d o . '      
   e n d      
  
   i f   @ O r i g e m   =   0   a n d   e x i s t s   ( S e l e c t   t o p   1   1        
                         f r o m   t m p P R F T 1 5 0 P e d i d o   t          
                             L e f t   J o i n   f t P e d i d o   p   o n   ( p . I d P e d i d o   =   t . I d P e d i d o )      
                         w h e r e   t . I d P r o c e s s a m e n t o   =   @ I d P r o c e s s a m e n t o      
                             a n d   i s N u l l ( p . S t a t u s P e d i d o , 0 )   =   0 )      
   b e g i n      
       S e t   @ M e n s a g e m E r r o   =   @ M e n s a g e m E r r o   +   '   -   E x i s t e m   P e d i d o s   e m   A b e r t o . '      
   e n d      
        
   i f   @ O r i g e m   =   0   a n d   e x i s t s   ( S e l e c t   t o p   1   1        
                         f r o m   t m p P R F T 1 5 0 P e d i d o   t          
                             L e f t   J o i n   f t P e d i d o   p   o n   ( p . I d P e d i d o   =   t . I d P e d i d o )      
                         w h e r e   t . I d P r o c e s s a m e n t o   =   @ I d P r o c e s s a m e n t o      
                             a n d   i s N u l l ( p . S t a t u s P e d i d o , 0 )   =   2 )      
   b e g i n      
       i f   ( @ M e n s a g e m E r r o   < >   ' ' )        
           S e t   @ M e n s a g e m E r r o   =   @ M e n s a g e m E r r o   +   ' < b r > '      
            
       S e t   @ M e n s a g e m E r r o   =   @ M e n s a g e m E r r o   +   '   -   E x i s t e m   P e d i d o s   j á   P r o c e s s a d o s . '      
   e n d      
      
      
   i f   @ O r i g e m   =   0   a n d   e x i s t s ( S e l e c t   T o p   1   1          
                         f r o m   t m p P R F T 1 5 0   t          
                         w h e r e   t . I d P r o c e s s a m e n t o   =   @ I d P r o c e s s a m e n t o      
                             a n d   i s n u l l ( t . i d E m p r e s a , 0 )   =   0 )      
   b e g i n      
 	 i f   ( @ M e n s a g e m E r r o   < >   ' ' )        
 	 	 S e t   @ M e n s a g e m E r r o   =   @ M e n s a g e m E r r o   +   ' < b r > '      
            
 	 S e t   @ M e n s a g e m E r r o   =   @ M e n s a g e m E r r o   +   '   -   E m p r e s a   e s t á   n u l a .   H o v e   p r o b l e m a s   n o   p r o c e s s a m e n t o   . '      
            
   e n d    
    
   i f   @ O r i g e m   =   0   a n d   e x i s t s   ( S e l e c t   t o p   1   1        
                         f r o m   t m p P R F T 1 5 0 P e d i d o   t          
                             L e f t   J o i n   f t P e d i d o M o t i v o B l o q u e i o   b l o q   o n   ( b l o q . I d P e d i d o   =   t . I d P e d i d o )  
                         w h e r e   t . I d P r o c e s s a m e n t o   =   @ I d P r o c e s s a m e n t o      
                             a n d   i s N u l l ( b l o q . T i p o B l o q u e i o , 0 )   =   1 )      
   b e g i n      
       S e t   @ M e n s a g e m E r r o   =   @ M e n s a g e m E r r o   +   '   -   E x i s t e m   P e d i d o s   B l o q u e a d o s . '      
   e n d                                      
        
   R e t u r n      
 E N D      
  
 g o  
  
 a l t e r   t a b l e   f n B a n c o  
 a d d   F l a g A t i v o F l u x o   b i t  
  
 g o  
  
 i f   e x i s t s   ( s e l e c t   1  
                         f r o m     s y s o b j e c t s  
                       w h e r e     i d   =   o b j e c t _ i d ( ' t b F e r i a d o ' )  
                         a n d       t y p e   =   ' U ' )  
       d r o p   t a b l e   t b F e r i a d o  
 g o  
  
 / * = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = * /  
 / *   T a b l e :   t b F e r i a d o                                                                                           * /  
 / * = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = * /  
 c r e a t e   t a b l e   t b F e r i a d o   (  
       I d E m p r e s a                         i n t                                     n o t   n u l l ,  
       D a t a                                   d a t e t i m e                           n o t   n u l l ,  
       D e s c r i c a o                         v a r c h a r ( 2 5 5 )                   n u l l ,  
       c o n s t r a i n t   P K _ T B F E R I A D O   p r i m a r y   k e y   ( D a t a ,   I d E m p r e s a )  
 )  
 g o  
  
 i f   e x i s t s ( s e l e c t   1   f r o m   s y s . e x t e n d e d _ p r o p e r t i e s   p   w h e r e  
             p . m a j o r _ i d   =   o b j e c t _ i d ( ' t b F e r i a d o ' )  
     a n d   p . m i n o r _ i d   =   ( s e l e c t   c . c o l u m n _ i d   f r o m   s y s . c o l u m n s   c   w h e r e   c . o b j e c t _ i d   =   p . m a j o r _ i d   a n d   c . n a m e   =   ' I d E m p r e s a ' )  
 )  
 b e g i n  
       d e c l a r e   @ C u r r e n t U s e r   s y s n a m e  
 s e l e c t   @ C u r r e n t U s e r   =   u s e r _ n a m e ( )  
 e x e c u t e   s p _ d r o p e x t e n d e d p r o p e r t y   ' M S _ D e s c r i p t i o n ' ,    
       ' u s e r ' ,   @ C u r r e n t U s e r ,   ' t a b l e ' ,   ' t b F e r i a d o ' ,   ' c o l u m n ' ,   ' I d E m p r e s a '  
  
 e n d  
  
 s e l e c t   @ C u r r e n t U s e r   =   u s e r _ n a m e ( )  
 e x e c u t e   s p _ a d d e x t e n d e d p r o p e r t y   ' M S _ D e s c r i p t i o n ' ,    
       ' I d E m p r e s a   =   0   s e r v e   p a r a   t o d a s   a s   E m p r e s a s ' ,  
       ' u s e r ' ,   @ C u r r e n t U s e r ,   ' t a b l e ' ,   ' t b F e r i a d o ' ,   ' c o l u m n ' ,   ' I d E m p r e s a '  
 g o  
  
 C R E A T E   F U N C T I O N   [ d b o ] . [ C a l c _ P r o x i m o D i a U t i l ] (                          
     @ D a t a V e n c i m e n t o   d a t e t i m e ,                          
     @ D i a s L i b e r a c a o   n u m e r i c ( 3 ) ,  
     @ T i p o C a r t e i r a   s m a l l i n t ,  
     @ I d E m p r e s a   i n t   =   0  
 )                          
 R e t u r n s   D a t e t i m e                          
 a s                          
 b e g i n                          
     I f   @ D a t a V e n c i m e n t o   i s   n u l l                            
           R e t u r n   @ D a t a V e n c i m e n t o                          
                          
     I f   @ D i a s L i b e r a c a o   i s   N u l l                          
           S e t   @ D i a s L i b e r a c a o   =   0                              
                          
     D e c l a r e   @ D a t a A u x   D a t e t i m e          
     d e c l a r e   @ F l a g D i a U t i l   s m a l l i n t                          
     d e c l a r e   @ D i a s L i b e r a c a o R e s t a n t e s   s m a l l i n t          
                  
     / * * * * *                  
         E X E M P L O   P R A T I C O :   1 2 / 1 2 / 2 0 1 5   -   S Á B A D O     D I A S   D E   L I B E R A Ç Ã O   -   0                      
         D A T A   V A L I D A   -   1 4 / 1 2 / 2 0 1 5                  
          
 	 S E   D I A S   D E   L I B E R A Ç Ã O   F O R   < >   0   -   E X E M P L O   2   D I A S   C O M   D A T A   =   2 3 / 1 2 / 2 0 1 5  
         A D I C I O N A   +   2   D I A S . . .   2 5 / 1 2 / 2 0 1 5   E   V A L I D A   E S T A   D A T A   P A R A   C H E C A R   S E   C A I   E M   U M   D I A   Ú T I L                    
         O U   N Ã O .                      
 	 D A T A   V A L I D A   -   2 8 / 1 2 / 2 0 1 5  
     * * * * * /                  
              
     i f   D a t e P a r t ( y y y y , @ D a t a V e n c i m e n t o )   > =   9 9 9 9            
     b e g i n          
         S e t   @ D a t a A u x   =   @ D a t a V e n c i m e n t o          
     e n d          
     e l s e          
     b e g i n  
         S e t   @ D a t a A u x   =   @ D a t a V e n c i m e n t o          
         S e t   @ F l a g D i a U t i l   =   0          
         S e t   @ D i a s L i b e r a c a o R e s t a n t e s   =   0          
                  
         i f   ( @ D i a s L i b e r a c a o   >   0 )   a n d   ( @ T i p o C a r t e i r a   =   2 )  
             S e t   @ D i a s L i b e r a c a o R e s t a n t e s   =   @ D i a s L i b e r a c a o                              
                  
         w h i l e   ( @ F l a g D i a U t i l   =   0 )   o r   ( @ D i a s L i b e r a c a o R e s t a n t e s   >   0 )          
         b e g i n          
             / * p u l a   f i n a l   d e   s e m a n a * /          
             S e l e c t   @ D a t a A u x   =   @ D a t a A u x   +   C a s e   D a t e P a r t ( d w , @ D a t a A u x )          
                                                       W h e n   1   T h e n   1          
                                                       W h e n   7   T h e n   2          
                                                       E l s e   0   E n d          
                                                                
             / * p u l a   o   f e r i a d o * /                                                    
             i f   E x i s t s ( s e l e c t   t o p   1   1   f r o m   t b F e r i a d o   w h e r e   D a t a   =   @ D a t a A u x   a n d   ( ( I d E m p r e s a   =   @ I d E m p r e s a )   o r   ( I d E m p r e s a   =   0 ) ) )  
                 s e t   @ D a t a A u x   =   @ D a t a A u x   +   1                
             e l s e          
             b e g i n          
                 S e t   @ F l a g D i a U t i l   =   1          
                 i f   @ D i a s L i b e r a c a o R e s t a n t e s   >   0            
                 b e g i n          
                     S e t   @ D i a s L i b e r a c a o R e s t a n t e s   =   @ D i a s L i b e r a c a o R e s t a n t e s   -   1          
                     S e t   @ D a t a A u x   =   @ D a t a A u x   +   1          
                     S e t   @ F l a g D i a U t i l   =   0          
                 e n d              
             e n d              
                      
         e n d          
     e n d                
                    
     R e t u r n   @ D a t a A u x                          
 e n d          
  
 g o  
  
 u p d a t e   f w E v e n t o O b j e t o  
 s e t   M e t o d o S i s t e m a   =   ' / / S u b s t i t u i   a   f u n ç ã o   d e   S U B M I T   d o   f o r m u l a r i o  
 i f   ( ! M u l t i p l a s C o n e x o e s )   {  
         $ ( ' ' # l o g i n _ l a b e l I d C o n e x a o ' ' ) . p a r e n t ( ) . h i d e ( ) ;  
 }  
 $ ( ' ' # f o r m ' ' ) . s u b m i t ( f u n c t i o n ( e v e n t )   {  
 e v e n t . p r e v e n t D e f a u l t ( ) ;  
 f u n c t i o n   V e r i f i c a L o g i n ( r e s p o n s e )   {        
                                 v a r   d a d o s   =   $ . p a r s e J S O N ( r e s p o n s e ) ;  
                                 i f     ( ( d a d o s . l e n g t h   >   0 )   & &   ( d a d o s [ 0 ] . L o g i n S u c e s s o   =   t r u e ) )        
                                 {        
                                         / / C h a m a   o   m é t o d o   d e   l o g i n ,   q u e   s a l v a   a s   v a r i a v e i s   n o   s e r v e r  
                                         c o n f i g u r a c a o . R e a l i z a r L o g i n ( d a d o s [ 0 ] . I d U s u a r i o ,   d a d o s [ 0 ] . I d E m p r e s a ,   d a d o s [ 0 ] . I d C o n e x a o ) ;  
                                         t r y {  
                                                 $ . a j a x ( {  
                                                         t y p e :   ' ' P O S T ' ' ,  
                                                         u r l :   u t i l s . U r l S i s t e m a ( )   +   " E m p r e s a / S e t E m p r e s a " ,  
                                                         c o n t e n t T y p e :   ' ' a p p l i c a t i o n / j s o n ;   c h a r s e t = u t f - 8 ' ' ,  
                                                         a s y n c :   f a l s e ,  
                                                         d a t a T y p e :   ' ' j s o n ' ' ,  
                                                         s u c c e s s :   f u n c t i o n   ( )   {  
                                                                 w i n d o w . l o c a t i o n   =   u r l S i s t e m a   +   " F o r m u l a r i o s / I n d e x " ;  
                                                         }                                          
                                                 } ) ;  
                                         }  
                                         c a t c h   ( e ) {  
                                                 w i n d o w . l o c a t i o n   =   u r l S i s t e m a   +   " F o r m u l a r i o s / I n d e x " ;  
                                         }  
                                 }  
        
                                 e l s e   {  
                                               a l e r t ( " A p e l i d o   e   S e n h a   n ã o   c o n f e r e m   ! " ) ;                       }  
                       } ;  
  
 v a r   p a r a m s   =   $ ( " # f o r m   i n p u t ,   # f o r m   s e l e c t " ) . s e r i a l i z e A r r a y ( ) ;  
 p a r a m s . p u s h ( { n a m e :   " g r u p o C o m a n d o " ,   v a l u e :     " A c e s s o " } ) ;  
 p a r a m s . p u s h ( { n a m e :   " n o m e C o m a n d o " ,   v a l u e :     " L o g i n " } ) ;  
 p a r a m s . p u s h ( { n a m e :   " I d E m p r e s a " ,   v a l u e :     1 } ) ;     / / T E M P O R A R I A M E N T E   E S T A   S E T A N D O   A   E M P R E S A   1  
  
 / / m e t o d o s S e r v e r B D . F o r m S u b m i t ( ' ' A c e s s o ' ' ,   ' ' L o g i n ' ' ,   p a r a m s ,   V e r i f i c a L o g i n ) ;  
 c o n f i g u r a c a o . S u b m i t D a d o s L o g i n ( p a r a m s ,   V e r i f i c a L o g i n ) ;  
        
 } ) ;  
 $ ( ' ' # s t a t u s R e g i s t r o ' ' ) . h i d e ( ) ;  
 N o v o ( ) ; '  
 w h e r e   I d F o r m u l a r i o P a i   =   ' l o g i n '  
 a n d   N o m e E v e n t o   =   ' r e a d y '  
 g o  
  
 i n s e r t   i n t o   f w V e r s a o   v a l u e s ( 1 , 4 , 1 )  
  
 g o  
  
 