
v`I]                 @   s8  d  d l  m Z m Z d  d l m Z m Z m Z d  d l m Z d  d l	 Z	 d  d l
 Z
 d  d l Z d  d l Z d  d l Z d  d l  m Z m Z d  d l m Z m Z m Z d  d l m Z d  d l	 Z	 d  d l
 Z
 d  d l Z d  d l Z d  d l Z d  d l Z Gd d   d e  Z Gd d   d e  Z d S)	    )Resourcereqparse)jsonifyrender_templatemake_response)dbNc               @   s   e  Z d  Z d d   Z d S)Reportc       +          s  t  j   } | j d  | j d  | j d  | j d d d | j   } | d   | d } | d } | d } t j j    d d    d	 d
   }     f d d   } d d   } d d   }	 t j j	   }
 t
 |
  |
 j d d  } | t j d d  } | j d  } | j d  } | sC| d k rkt
 d  t |  d t |  d } | s~| d k rt
 d  t |  d t |  d t t j t |  t |   d  } t
 | d |  i  } g  }   r  d k r| j    n' | j d  | j d  | j d  t
 |  x| D]} t
 |  y" j   } | j d | | | g  | j   } | j   x | D] \ } } } } } } } } } } } } d t |  d  t |  d! t |  d" t |  d# t |  d$ t t |  d%  d& t t |  d%  d' t |  d( t |  d) t t |  d%  d* t t |  d%  i } qWWn5 t k
 r}  z t
 t |    WYd  d  }  ~  Xn Xt t j j	   j d+   | d, <t |  d- t |  | d. <d/ t t j j	   j d+   d0 t |	 d1   | d2 <t | d& | d* d%  | d3 <d4 | d5 <d4 | d6 <| d7 | d, d7 t j j	   j d8  d9 }! | d& d k s| d* d k rt j }" x@ | j   D]2 \ }# }$ |" j d: t |#  d; t |$   }" qW|   }% |" j d< t |% d   }" |% d }& t |&  d= }' |& |' }( d>  | |  d?  |&  d@  |'  dA  |(  dB }) |" j dC t |)   }" t j dD dE dF |" dG |! i dH dI dJ i }* t dK d dL dM dN dO dP |! i  SWd  S)QNaccount_number
start_dateend_datecall_directiondefaultr   c             S   s   t  d t |    S)Nz%.2f)strfloat)a r   ,/var/www/html/sampleapi.quiits.com/report.pytwoDecimalPoint%   s    z$Report.post.<locals>.twoDecimalPointc              S   sA   g  }  t  j j t j  } x | D] } |  j | d  q" W|  S)Nr   )r   sessionexecuteconstSQL_ACTIVE_ACCOUNTappend)account_listactive_accountsaccr   r   r   get_active_accounts'   s
    z(Report.post.<locals>.get_active_accountsc        
         s  d }  d } d } d } d } d } zyu j    } | j d   g  d } xI| j r| j   } x | D]} t d  t t | d   t t | d   | d d k r#| d t | d  d	  | d
  d } t d  t | d
  t |   t | d
  }  t d  qn | d d k rFt | d  } qn | d d k rn t | d  } t | d
  } t | d  } qn W| j   sR PqR WWn? t	 k
 r}	 z t d  t t |	   WYd  d  }	 ~	 Xn XWd  | j
   X| d  |   d } | d d  |  d } | d  |  d } |  | 7}  |  | | | 7}  | d d t | |  d  |  d  | | |  d } | d  | | |  d } | d } | |  g S)Nr    invoicezv<table id= "inbound_tab" align = center width = "100%"> 				<tr> 				<th colspan="3">Number Details</th> 				</tr><tr>poinnnnn   
did_numberz<tr> 				                <td>z</td><td>&pound;   z</td> 				                </tr>12	extensionpackage   	Exceptionz<tr> <td                 style = " text-align:right;" > <b>Number Total                 </b> </td><td style = "text-align:right;" >                  <b> &pound;z </b></td></tr> z8<tr> 				<th colspan="3">Plan Details</th> 				</tr><tr>z(<tr> 				<td>Plan Amount</td><td>&pound;z</td> 				</tr>z<tr> <td                             style = " text-align:right;" > <b>Plan Total                             </b> </td><td style = "text-align:right;" >                              <b> &pound;zH<tr>             				<th colspan="3">Seat</th>             				</tr><tr>z<tr>             				<td>Seat(z*&pound;z)</td><td>&pound;z</td>             				</tr>z<tr> <td                             style = " text-align:right;" > <b>Seat Total                             </b> </td><td style = "text-align:right;" >                              <b> &pound;z</table>)cursorcallprocrowcountfetchallprintr   r   intnextsetr(   close)
totalhtmlextensionCountamount
planAmountfreeExtensionr)   case_detailsdatae)r	   
connectionr   r   r   get_did_and_package_info-   s^    	
'
	
'
= 
z-Report.post.<locals>.get_did_and_package_infoc               S   s   d  S)Nr   r   r   r   r   send_invoice_to_emailu   s    z*Report.post.<locals>.send_invoice_to_emailc             S   sF   d } x9 t  d |   D]( } t j d d  } | t |  7} q W| S)Nr   r   r    	   )rangerandomrandintr   )notpinumr   r   r   generate_random_numx   s
    z(Report.post.<locals>.generate_random_numdayr    daysz%Yz%mr   zstart date if-z-01zend date if 400034400015400641call_reportaccount_nameemailinbound_totalinbound_answeredinbound_durationr"   inbound_costoutbound_totaloutbound_answeredoutbound_durationoutbound_costz%Y-%m-%dinvoice_datez - invoice_periodzQ##   invoice_numberinvoice_totalzdisplay:table;in_visibilityout_visibility_z%H-%M-%Sz.pdf{}z{did_and_plan}g?z<tr> <td                 style = " text-align:right;" > <b>Prepaid Amount                  </b> </td><td style = "text-align:right;" >                  <b>- &pound;z </b></td></tr> <tr> <td                 style = " text-align:right;" > <b>Monthly Sub Total                 </b> </td><td style = "text-align:right;" >                  <b> &pound;z </b></td></tr>                 <tr> <td style = " text-align:right;" > <b>Vat(20%)                                 </b> </td><td style = "text-align:right;" > <b> &pound;                                a_   </b></td></tr>                 <tr> <td style = "background-color:#303030; color:#fedb00; font-size:24px; text-align:right;" > <b>Grand Total                                                 </b> </td><td style = "background-color:#303030; color:#fedb00; font-size:24px; text-align:right;" > <b> &pound;                                                z </b></td></tr>z{total_with_vat}z9http://sampleapi.quiits.com/api/v1/generate_pdf_from_htmljsonr2   	file_nameheaderszContent-Typezapplication/jsonstatusmessageSuccessresultpdf_linkz!https://api.quiits.com/templates/) r   RequestParseradd_argument
parse_argsr   engineraw_connectiondatetimedatetodayr-   replace	timedeltastrftimer   calendar
monthranger.   r   r)   r*   r,   r0   roundr   r(   tempTEMPLATE_INVOICEitemsrequestspostr   )+selfparserargsr
   r   r   r   r;   r<   rE   rr   firstlast_month_yearlast_ylast_mcall_report_dataactive_accounts_listaccountsr)   call_detailsaccount_numnamerO   total_durationrR   inbound_call_countinbound_answered_countrS   rV   outbound_call_countoutbound_answered_countrW   r9   rd   r8   keyvalue
numberInfor1   	vatAmountnewTotal	totalHtmlresponser   )r	   r:   r   r   r}      s    



H


D


.##":

0 	*	

B
zReport.postN)__name__
__module____qualname__r}   r   r   r   r   r      s   r   c               @   s   e  Z d  Z d d   Z d S)ReportDetailedc       +   !      s
  t  j   } | j d  | j d  | j d  | j d d d | j   } | d   | d } | d } | d } t j j    d d    d	 d
   }     f d d   } d d   } d d   }	 t j j	   }
 t
 |
  |
 j d d  } | t j d d  } | j d  } | j d  } | sC| d k rkt
 d  t |  d t |  d } | s~| d k rt
 d  t |  d t |  d t t j t |  t |   d  } t
 | d |  i  } g  }   r  d k r| j    n' | j d  | j d  | j d  t
 |  x| D]} t
 |  y. j   } | j d | | | g  | j   } | j   x | D] \ } } } } } } } } } } } } d t |  d  t |  d! t |  d" t |  d# t |  d$ t t |  d%  d&  t t |  d%   d' t |  d( t |  d) t t |  d%  d*  t t |  d%   i } qWWn5 t k
 r}  z t
 t |    WYd  d  }  ~  Xn Xt t j j	   j d+   | d, <t |  d- t |  | d. <d/ t | d  d0 t |  | d1 <t | d&  t | d*  | d2 < | d2  | d2 <d3 | d4 <d3 | d5 <| d6 | d, d6 t j j	   j d7  d8 }! t | d&  d k st | d*  d k rt j }" x@ | j   D]2 \ }# }$ |" j d9 t |#  d: t |$   }" qW|   }% |" j d; t |% d   }" |% d }& t |&  d< }' |& |' }( d=  | |  d>  |&  d?  |'  d@  |(  dA }) |" j dB t |)   }" t j dC dD dE |" dF |! i dG dH dI i }* t dJ d dK dL dM dN dO |! i  SWd  S)PNr	   r
   r   r   r   r   c             S   s   t  d t |    S)Nz%.2f)r   r   )r   r   r   r   r     s    z,ReportDetailed.post.<locals>.twoDecimalPointc              S   sA   g  }  t  j j t j  } x | D] } |  j | d  q" W|  S)Nr   )r   r   r   r   r   r   )r   r   r   r   r   r   r     s
    z0ReportDetailed.post.<locals>.get_active_accountsc                 s1  d }  d } d } d } d } d } d } d } zy j    } | j d   g  d } d }	 x| j r| j   }
 xZ|
 D]R} t d  t t | d   t t | d   | d d k rO|	 d	 t | d  d
  | d  d }	 t d  t | d  t |   t | d  }  t |  t | d  } t d  q | d d k rrt | d  } q | d d k r t | d  } t | d  } t | d  } t | d  } t t |   q W| j   sd Pqd WWn? t	 k
 r)} z t d  t t |   WYd  d  } ~ Xn XWd  | j
   X| d t |  d d  |  d } | d  |  d } |  | 7}  |  | | | 7}  | d d t | |  d  |  d  | | |  d } | d  | | |  d } | |	 } | d   |  d } | d! } | |  g S)"Nr   r   r   z7<table id= "inbound_tab" align = center width = "100%">z:<tr> 				<th colspan="3">Number Details</th> 				</tr><tr>r   r    r!   z<tr> 				                <td>z</td><td>&pound;r"   z</td> 				                </tr>r#   r$   r%   r&   r'   r[   r(   z%<tr> 				<th colspan="3">Your Plan - z</th> 				</tr><tr>z(<tr> 				<td>Plan Amount</td><td>&pound;z</td> 				</tr>z<tr> <td                             style = " text-align:right;" > <b>Plan Total                             </b> </td><td style = "text-align:right;" >                              <b> &pound;z </b></td></tr> zH<tr>             				<th colspan="3">Seat</th>             				</tr><tr>z<tr>             				<td>Seat(z*&pound;z)</td><td>&pound;z</td>             				</tr>z<tr> <td                             style = " text-align:right;" > <b>Seat Total                             </b> </td><td style = "text-align:right;" >                              <b> &pound;z<tr> <td                 style = " text-align:right;" > <b>Number Total                 </b> </td><td style = "text-align:right;" >                  <b> &pound;z</table>)r)   r*   r+   r,   r-   r   r   r.   r/   r(   r0   )r1   r2   r3   r4   r5   r6   ZplanNameZnumber_totalr)   Znumber_htmlr7   r8   r9   )r	   r:   r   r   r   r;     sl    	
'
	
'#
= 

z5ReportDetailed.post.<locals>.get_did_and_package_infoc               S   s   d  S)Nr   r   r   r   r   r<   n  s    z2ReportDetailed.post.<locals>.send_invoice_to_emailc             S   sF   d } x9 t  d |   D]( } t j d d  } | t |  7} q W| S)Nr   r   r    r=   )r>   r?   r@   r   )rA   rB   rC   rD   r   r   r   rE   q  s
    z0ReportDetailed.post.<locals>.generate_random_numrF   r    rG   z%Yz%mr   zstart date ifrH   z-01zend date ifrI   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   r"   rS   rT   rU   rV   rW   z%Y-%m-%drX   z - rY   zQ#rZ   r\   r]   zdisplay:table;r^   r_   r`   z%H-%M-%Sz.pdfra   rb   z{did_and_plan}g?z<tr> <td                 style = " text-align:right;" > <b>Amount Paid                  </b> </td><td style = "text-align:right;" >                  <b>- &pound;z </b></td></tr> <tr> <td                 style = " text-align:right;" > <b>Monthly Sub Total                 </b> </td><td style = "text-align:right;" >                  <b> &pound;z </b></td></tr>                 <tr> <td style = " text-align:right;" > <b>Vat(20%)                                 </b> </td><td style = "text-align:right;" > <b> &pound;                                a_   </b></td></tr>                 <tr> <td style = "background-color:#303030; color:#fedb00; font-size:24px; text-align:right;" > <b>Grand Total                                                 </b> </td><td style = "background-color:#303030; color:#fedb00; font-size:24px; text-align:right;" > <b> &pound;                                                z </b></td></tr>z{total_with_vat}z9http://sampleapi.quiits.com/api/v1/generate_pdf_from_htmlrc   r2   rd   re   zContent-Typezapplication/jsonrf   rg   rh   ri   rj   z!https://api.quiits.com/templates/) r   rk   rl   rm   r   rn   ro   rp   rq   rr   r-   rs   rt   ru   r   rv   rw   r.   r   r)   r*   r,   r0   rx   r   r(   ry   rz   r{   r|   r}   r   )+r~   r   r   r
   r   r   r   r;   r<   rE   rr   r   r   r   r   r   r   r   r)   r   r   r   rO   r   rR   r   r   rS   rV   r   r   rW   r9   rd   r8   r   r   r   r1   r   r   r   r   r   )r	   r:   r   r   r}     s    



S


D


.)#"&"

0,	*	

B
zReportDetailed.postN)r   r   r   r}   r   r   r   r   r     s   r   )flask_restfulr   r   flaskr   r   r   appr   r|   rp   	constantsr   Zhtml_stringsry   rv   r?   r   r   r   r   r   r   <module>   s$   