;
hCc           @   s-  d  Z  d k Z d k Z d k Z d k Z d k Z d k l Z d k l	 Z	 d k
 l Z l Z d k Z e i e i d  d   Z d f  d     YZ d	   Z e i d
  d f e i d  d f f e _ d d  Z h  e _ d   Z d   Z d   Z d   Z d   Z e d j o e   GHn d S(   s(   
   Copyright (C) 2003-2005 PimenTech SARL (http://www.pimentech.net)

   Pimengest2 is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   Pimengest2 is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public
   License along with this library; see the file COPYING.LIB.  If not,
   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.	
N(   s   ST_MTIME(   s   exists(   s   indexs   joins   fr_FRc         C   s   t  |  d d  Sd  S(   Nf0.001i   (   s   rounds   sum(   s   sum(    (    s	   genpdf.pys
   euro_round#   s    s   genpdfc           B   s   t  Z d Z d Z d Z d Z d Z d Z d Z d Z	 d Z
 d   Z d   Z d   Z d   Z d	   Z d d
  Z d   Z d   Z d   Z d   Z RS(   Ns    s   %# articles_from_genpdfs   %# prestas_from_genpdfi    c         C   s(   | |  _ | |  _ | |  _ | |  _ d  S(   N(	   s   uids   selfs   UIDs   with_headers   WITH_HEADERs   verboses   VERBOSEs   filedirs   FILEDIR(   s   selfs   uids   with_headers   verboses   filedir(    (    s	   genpdf.pys   __init__1   s    			c         C   sd   y t  | d | d  } Wn) d | d | d GHt i d  n X| i   |  _ | i	   d  S(   Ns   /s   rs   Error : no tex file s
    found on s   
i(
   s   opens   latexdirs   template_files   files   syss   exits   reads   selfs   BUFFERs   close(   s   selfs   latexdirs   template_files   file(    (    s	   genpdf.pys   init_template7   s    c         C   sr   |  i od y t | d | d  } Wn) d | d | d GHt i d  qn X| i   |  _	 | i
   n d  S(   Ns   /s   rs   Error : no tex file s
    found on s   
i(   s   selfs   WITH_HEADERs   opens   latexdirs   header_files   files   syss   exits   reads   BUFFER_HEADERs   close(   s   selfs   latexdirs   header_files   file(    (    s	   genpdf.pys   init_headerA   s    
c         C   s*  d |  i d <|  i d d j ot i i d |  i d d  d |  i d <|  i d } |  i d } xO| d f | d f f D]s\ } }
 t	 |  i |
  oFyF | i
 d	  | i d	  }	 t |	 d
 d !d	  |	 d f \ } } Wn d |  i d <qX| d j o t	 d | d f  o~ t i |  t } t i d |  t } | | j  oC y( |  i |  d | d f |  i |
 <Wqd |  i d <qXqqy( |  i |  d | d f |  i |
 <Wqd |  i d <qXqq d |  i d <q Wn|  i d d j o |  i d d j ot i i d  t	 |  i d  oT|  i d } yF | i
 d	  | i d	  }	 t |	 d
 d !d	  |	 d f \ } } Wn d |  i d <qX| d j o d |  i d |  i d <qt	 d | d f  o~ t i |  t } t i d |  t } | | j o d | d f |  i d <qy |  i |  Wqd |  i d <qXqy |  i |  Wqd |  i d <qXqd |  i d <n d |  i d <|  i |  i d <|  i | |  |  i | |  i d  x> d d f D]0 }
 |  i i d |
 d |  i |
 d  |  _ qW|  i o |  i |  i d <n | o@ |  i  |  i d <|  i! |  i d <|  i  i d d  |  i d <n |  i" o |  i GHn xw |  i i#   D]f \ }
 } | t% j o
 d } n |
 d f j o t& |  } n |  i' i d |
 d | d  |  _' qWd S(   sk   Replace values from token list to buffers.
		If a header is given, all buffer_header is inserted in buffer.s   template_entete.texs   ENTETE_FILEs   FICHIER_PIEDs   ~s	   [detect] s   
s   template_entete_img.texs   FICHIER_LOGOs   .i    is   epss   %s.%ss   %s.epss1   [detect] template d'entete : template_entete.tex
s   {{sF    \fontsize{6.5pt}{19.2pt}\selectfont{\includegraphics[width=4.7cm]{%s}sH   \fontsize{6.5pt}{19.2pt}\selectfont{\includegraphics[width=4.7cm]{%s.%s}s   DESCRIPTION_PRESTATIONs   <TMPL s   >i   s   ENTETEs   ARTICLESs   PRESTASs   Articles   Descriptions	   ARTICLES2s    N((   s   selfs   tokenss   syss   stderrs   writes   file1s   file2s   files   tokens   check_outputs   indexs   splits   lists   joins   cores	   extentions   oss   stats   ST_MTIMEs   m_time1s   m_time2s
   conversions
   convertions   BUFFER_DESCRIPTION_PRESTAs   init_templates   latexdirs   template_files   init_headers   BUFFER_HEADERs   replaces   WITH_HEADERs	   affichages   BUFFER_ARTICLESs   BUFFER_PRESTAs   VERBOSEs   itemss   strs   Nones	   TeXfilters   BUFFER(   s   selfs	   affichages   latexdirs   template_files   file2s   file1s   strs   cores	   extentions   lists   tokens   m_time2s   files   m_time1(    (    s	   genpdf.pys   replace_valuesM   s      *(* .
 
 
c         C   s   | d j o |  i |  i } d } n, | d j o |  i |  i } d } n d S|  i i |  |  i i	 d j o d Sn |  i i
   } x! | D] } | | o d Sq q Wd Sd  S(   Ns   articlei   s   prestai   i    i   (   s   types   selfs   query_articles   UIDs   reqs   rangs   query_prestas   curss   executes   rowcounts   fetchalls   ress   r(   s   selfs   types   rangs   ress   reqs   r(    (    s	   genpdf.pys
   has_remise   s     

 c         C   s  d } d } |  i d  } |  i d  } d } | o | d d 7} n | d d 7} | d 7} | o | d	 d 7} n | d
 d 7} d } | o | d 7} n | d 7} d } |  i	 |  i
 }	 |  i i |	  |  i i d j o| |  _ d }
 xt |  i i  D]}
 |  i i   } | | d 7} | | d 7} | d d j o+ | d o d | d } q{| d } n d } | o d | d <n- | d o d | d <n d | d | d <| d o d | d <n d | d d | d <| d d j o | d d j o
 d } n4 t | d | d  | d | d | d d } |  i | d | d d d | d p d | | d | d t | d | d  | | t | d  | f	 7_ qW|  i | 7_ n |  i |  i
 }	 |  i i |	  |  i i d j o| |  _ d }
 xt |  i i  D]}
 |  i i   } | o d | d <n- | d o d | d <n d | d | d <| | d  7} | | d! 7} | d  o
 d } n4 t | d  | d!  | d! | d  | d  d } |  i d" | d# p d# | d$ | d p d% | d | t | d   | f 7_ | d# t j o | d# d j o |  i d& | d# 7_ n | d t j o | d d j o |  i d' | d 7_ q)q)W|  i d j o d( |  i |  _ n |  i | 7_ n | | f Sd  S()   Ni    s   articles   prestasE   
\vspace*{0.5cm}
\begin{small}
\begin{flushright}
\tablehead{
\hline
sX   \bf Article & \bf Rf. & \bf Remise & \bf Qtt & \bf Px Unit. HT & \bf TVA & \bf Mtt HT\\s   
sK   \bf Article & \bf Rf. & \bf Qtt & \bf Px Unit. HT & \bf TVA & \bf Mtt HT\\s   
\hline}
\tabletail{\hline}
s   \begin{supertabular}[bbbbbbb]{|p{7.5cm}||>{\PBS\centering}b{1.5cm}|>{\PBS\centering}b{1cm}|>{\PBS\centering}b{1cm}|>{\PBS\raggedleft}b{2cm}|>{\PBS\raggedleft}b{1cm}|>{\PBS\raggedleft}b{1.5cm}|}s   \begin{supertabular}[bbbbbb]{|p{8.5cm}||>{\PBS\centering}b{1.5cm}|>{\PBS\centering}b{1cm}|>{\PBS\raggedleft}b{2cm}|>{\PBS\raggedleft}b{1cm}|>{\PBS\raggedleft}b{1.5cm}|}s  
\bf Prestation & \bf Rf. & \bf Description & \bf Remise & \bf TVA & \bf Mtt HT\\
\hline}
\tabletail{\hline}
\begin{supertabular}[bbbbbb]{|p{4.4cm}||>{\PBS\centering}b{1cm}|p{7cm}|>{\PBS\raggedleft}b{1cm}|>{\PBS\raggedleft}b{1cm}|>{\PBS\raggedleft}b{1.5cm}|}
s   
\bf Prestation & \bf Rf. & \bf Description & \bf TVA & \bf Mtt HT\\
\hline}
\tabletail{\hline}
\begin{supertabular}[bbbbb]{|p{5.4cm}||>{\PBS\centering}b{1cm}|p{7cm}|>{\PBS\raggedleft}b{1cm}|>{\PBS\raggedleft}b{1.5cm}|}
s1   
\end{supertabular}
\end{flushright}
\end{small}
i   s   ca_hts   ca_ttcs   declinaisons    s   codes   -s   remises    ~ & s
   %.1f %% & s   descriptions    (s   ) id   s   noms    & s.   %s %s & %s %d & %.2f %s & %.1f %% & %.2f %s\\
s   quantites
   montant_hts   montant_ttcs3   %s & %s & \footnotesize %s & %s %.1f%% & %.2f %s\\
s
   prestations   uids   ~s   {\bf %s} - s   %s\\s%   \\
{\bf Descriptif des prestations}\\(   s   total_hts	   total_ttcs   selfs
   has_remises   has_remise_articles   has_remise_prestas   template_article_heads   template_presta_heads   template_foots   query_articles   UIDs   reqs   curss   executes   rowcounts   BUFFER_ARTICLESs   is   ranges   dictfetchones   ress   refs   tvas   add_tvas
   euro_rounds   devises   query_prestas   BUFFER_PRESTAs   Nones   BUFFER_DESCRIPTION_PRESTA(   s   selfs   devises   montant_inits   template_foots   tvas   template_presta_heads   ress	   total_ttcs   total_hts   reqs   is   template_article_heads   has_remise_articles   refs   has_remise_presta(    (    s	   genpdf.pys   get_articles_prestas   s    

	 "
u	 
P""c         C   sU   t  i d |  i d | d f  } | d j o d | d f |  i d <n d  d  S(   Ns!   convert %s %s.%s > /dev/null 2>&1s   FICHIER_LOGOs   epsi    sH   \fontsize{6.5pt}{19.2pt}\selectfont{\includegraphics[width=4.7cm]{%s.%s}s   CONVERT ERROR(   s   oss   systems   selfs   tokenss   cores   out(   s   selfs   cores   out(    (    s	   genpdf.pys
   convertionT  s    #c         C   sg   y8 t  | |  i d d  } | i |  i  | i   Wn( d | |  i d GHt i	 d  n Xd S(   s0   Save the buffer in a file in 'output' directory.s   .texs   ws   Error save TeX file on %siN(
   s   opens   outputs   selfs   UIDs   files   writes   BUFFERs   closes   syss   exit(   s   selfs   outputs   file(    (    s	   genpdf.pys   savetexfile\  s     c         C   sH   t  i d | | | | | f  |  _ |  i i	   |  _
 |  i i   d S(   sR   Object connection with database.
		It loads the good 'getinfo' according to 'mode's-   dbname=%s user=%s password=%s host=%s port=%sN(   s   psycopgs   connects   DBNAMEs   DBUSERs   DBPWDs   DBHOSTs   DBPORTs   selfs   cons   cursors   curss
   autocommit(   s   selfs   DBNAMEs   DBUSERs   DBPWDs   DBHOSTs   DBPORT(    (    s	   genpdf.pys   getConnectioni  s     %c         C   s  | d } t i |  d | |  i f } t | d  t	 d | d  } t | |  t | d  d | | f } t i |  |  i o	 | GHn y ~ t i d |  Wn t i d	  n Xy^ d
 | | f } t | d  t i |  |  i o	 | GHn t i d | | | | f  Wn y d | | f } t | d  t i |  |  i o	 | GHn d | | f } t | d  t i |  |  i o	 | GHn t i d |  Wqt i d	  qXn Xd | Sd  S(   Ns   /pimengest2_debugs   %s%ss   
---
LATEX SOURCE
---
s   %s.texs   rs   
---
LATEX OUTPUT
---
s0   latex -interaction=nonstopmode %s.tex >> %s 2>&1s6   latex -interaction=nonstopmode %s.tex > /dev/null 2>&1is   dvipdf %s.dvi >> %s 2>&1s   
---
DVIPDF
---
s"   rm -rf %s.dvi %s.aux %s.log %s.texs   dvips %s.dvi >> %s 2>&1s   
---
DVIPS
---
s   ps2pdf %s.ps >> %s 2>&1s   
---
PS2PDF
---
s   rm -rf %s.ps > /dev/null 2>&1s   %s.pdf(   s	   outputdirs
   debug_files   oss   chdirs
   outputfiles   selfs   UIDs   true_outputs   logs   opens   tex_files   cmds   systems   VERBOSEs   syss   exit(   s   selfs	   outputdirs
   outputfiles   cmds
   debug_files   tex_files   true_output(    (    s	   genpdf.pys   toPDFr  sN    

	
	!
	
	(   s   __name__s
   __module__s   BUFFERs   BUFFER_HEADERs   BUFFER_ARTICLESs   BUFFER_PRESTAs   BUFFER_DESCRIPTION_PRESTAs   UIDs   WITH_HEADERs   FILEDIRs
   DEBUG_INFOs   __init__s   init_templates   init_headers   replace_valuess
   has_remises   get_articles_prestass
   convertions   savetexfiles   getConnections   toPDF(    (    (    s	   genpdf.pys   genpdf&   s&   		
		g					c         C   sX   x) t  i D] \ } } | i | |   }  q
 W|  i d d  }  |  i d d  }  |  Sd  S(   Ns   <-s    $\leftarrow$ s   ->s    $\rightarrow$ (   s	   TeXfilters   comps   pats   reps   subs   strs   replace(   s   strs   reps   pat(    (    s	   genpdf.pys	   TeXfilter  s    
 s   (?<!\\)_s   \_s   %(?!#)s   \%i    c         C   s   t  i i d |  | f  | d j o/ xz t i D] } t i | c |  d 9<q1 WnO d | |  d |  } t i i	 |  o d t i | <n t i | c |  7<d  S(   Ns    HT=%s - TTC=%s
i    id   s   %.1f(
   s   syss   stderrs   writes
   montant_hts   montant_ttcs   add_tvas   tauxs   ts   ratios   has_key(   s
   montant_hts   montant_ttcs   ratios   t(    (    s	   genpdf.pys   add_tva  s    
 c         C   s   t  |   o t |  d  } n t |  d  } t |  t d  j o | i |  | i d  n> t |  t |  j o$ | i | i    | i d  n | i   ~ d  S(   Ns   w+s   as    s   
---
(	   s   existss   strfiles   opens   files   types   msgs   writes   reads   close(   s   strfiles   msgs   file(    (    s	   genpdf.pys   log  s    
c          G   s3   t  i t  _ x |  D] } | GHq Wt  i d  d  S(   Ni(   s   syss   stderrs   stdouts   argss   msgs   exit(   s   argss   msg(    (    s	   genpdf.pys   usage  s
      	c         C   s   t  |   o d Sn d Sd  S(   Ni    i   (   s   existss
   output_dir(   s
   output_dir(    (    s	   genpdf.pys   check_output  s    c         C   s  h  d d <d d <} |  i d  } x | D] }  |  i d  \ } } | d j o | | d <q. | d	 j o | | d
 <q. | d j o | | d <q. | d j o | | d <q. | d j o | | d <q. q. W| i d  p | i d  p | i d
  o d Sn | Sd  S(   Ns   hosts	   localhosts   ports   5432s   /s   :s   us   users   ds   bases   Ps   passs   ps   hi    (   s   rets   strs   splits   bases   fs   vs   has_key(   s   strs   bases   vs   rets   f(    (    s	   genpdf.pys   extract_base  s$          3c          C   s  y2 t  i  t i d d d d d d g  \ } } Wn" t  i j
 o } t |  n Xh  d d <d d <d	 d
 <d d
 <d d <d d
 <d d
 <d d <d d <d d
 <} x@| D]8\ } }  | d j o |  | d	 <q | d j o |  | d <q | d j o |  | d <q | d j o |  | d <q | d j o d | d <q | d j o d | d <q | d j o d | d <q | d j o t t i  q | d j o |  | d <q | d j o |  | d <q | d j o d | d <q q W| d d
 j p | d d
 j p | d d
 j o t t i  n t | d  o | d d j o d | d GHt t i  n | d	 d
 j o t t i  n | Sd  S(!   s  
	genpdf.py makes a pdf with a latex template and pimengest2 database informations

	* genfacture.py
	* gendevis.py
	* genlettre.py
	
	usage : genXXX.py -u DBUID -l latexdir -f filedir --database="database info"
	        [--without-header | -e header_file] [--without-tab] [--without-pdf] [-t template] [-d outdir]
			
	-u : database uid
	--database="u:user/P:pass/d:dbname[/h:host/p:port]"
	-l : latex template dir
	-f : file dir (from clients' uploads)
	---------------
	--with-header : with header output. header file optional (in src/latex directory)
	--without-tab : don't print the 'article+presta' tabular
	--without-pdf : debug option (does not load toPDF function)
	-t : alternative latex template
	-d : output directory, default is '/tmp/pdf'
	-v : verbose
	-h : help
	i   s   ht:d:e:m:u:vl:f:s   without-headers   without-tabs   without-pdfs	   database=s   WITH_ENTETEs   WITH_PDFs   UIDs    s   TEMPLATEs   OUTDIRs   /tmp/pdfs   LATEXDIRs   FILEDIRs   VERBOSEi    s   AFFICHAGE_TABs   DATABASEs   -us   -ls   -fs
   --databases   --without-headers   --without-tabs   --without-pdfs   -hs   -ts   -ds   -vs    *** Can't find %s ***
N(   s   getopts   syss   argvs   optss   argss   errors   msgs   usages   optionss   os   as   get_optionss   __doc__s   exists(   s   as   argss   os   msgs   optionss   opts(    (    s	   genpdf.pys   get_options  sL     2`            3#s   __main__(   s   __doc__s   syss   oss   getopts   res   psycopgs   stats   ST_MTIMEs   os.paths   existss   strings   indexs   joins   locales	   setlocales   LC_TIMEs
   euro_rounds   genpdfs	   TeXfilters   compiles   comps   add_tvas   tauxs   logs   usages   check_outputs   extract_bases   get_optionss   __name__(   s	   TeXfilters   indexs   ST_MTIMEs   joins   logs   existss   extract_bases   locales   get_optionss   check_outputs   genpdfs   syss   res   add_tvas   psycopgs   usages   getopts
   euro_rounds   os(    (    s	   genpdf.pys   ?   s0   							 x	-						;
