3
$i-                @   s  d 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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mZ ddlmZ ddlmZ ddlmZ ddlmZ ddlZdd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlm Z  ddlm!Z! ddlm"Z" ddlm#Z# ddlm$Z$ ddlm%Z% ddlm&Z& ddlm'Z' ddlm(Z( ddl)m*Z* ddl+m,Z, ej-dZ.g Z/g Z0g Z1g Z2g Z3g Z4g Z5g Z6g Z7edd Z8edd Z9edd Z:ed d! Z;ed"d# Z<ed$d% Z=ed&d' Z>ed(d) Z?d*d+ Z@d,d- ZAd.d/ ZBed0d1 ZCed2d3 ZDed4d5 ZEd6d7 ZFd8d9 ZGd:d; ZHd<d= ZId>d? ZJd@dA ZKedBdC ZLedDdE ZMedFdG ZNedHdI ZOedJdK ZPdS )Lz
 * @author Xiaobo Sun
 * created  on 23.06.2018
 * @copyright (C) 2018 Sogood International GmbH - all rights reserved
 * @licence
 * Unauthorized copying of this file, via any medium is strictly prohibited
 * Proprietary and confidential
    N)PdfFileMergerPdfFileReaderPdfFileWriter)csrf_exempt)canvas)mm)A4   )letter)	newletter)deliverynote)picklist)stockup)frgeneratepdf)esgeneratepdf)ukgeneratepdf)itgeneratepdf)nachlieferung)
gutschrift)itgutschrift)ukgutschrift)frgutschrift)EmailMessage)HttpResponsedjangoc       +   )   C   sX  | j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd	}	| j jd
}
| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd } | j jd!}!| j jd"}"| j jd#}#| j jd$}$| j jd%}%| j jd&}&| j jd'}'td(d)}(d*|(d+< tjtj |||||||||	|
|||||||||||||||||||||| |!|"|#|$|%|&|'(})|)j }*|(j|* |(S ),zz
    Django View : endpoint
    Used by Goodone document.php
    :param request: httpRequest
    :rtype: HttpResponse
    	is_sogoodplatform_id
order_datetypetaxcustomerCompanycustomerTitlecustomerSurnamecustomerFirstnamecustomerStreetcustomerPostcodecustomerCitycustomerCountrycustomerMailcustomerTelephonecustomerShippingCompanycustomerShippingSurnamecustomerShippingFirstnamecustomerShippingStreetcustomerShippingPostcodecustomerShippingCitycustomerShippingCountrypaymentMethodpaymentDetailspaidSumpaidTaxshippingMethodshippingDatails	soldItemscommentmemo_abdocNoshippingDatedocDateshow_customer_name_in_docvat_nr
arbeitsortcustomerShippingTelephone	deal_withzapplication/pdf)content_typez"attachment; filename="invoice.pdf"zContent-Disposition)	POSTgetr   r
   SogoodPrintableioBytesIOdowrite)+requestr   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   dealWithresponsesogood_printpdf_blob rO   &/data/git/entellipy_wy/pdf/pdfviews.py	print_pdf<   s    	


rQ   c       .   +   C   s  | j jd}tt|}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd	}	| j jd
}
| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd } | j jd!}!| j jd"}"| j jd#}#| j jd$}$| j jd%}%| j jd&}&| j jd'}'| j jd(}(| j jd)})tt|)})td*d+}*d,|*d-< tjtj |||||||||	|
|||||||||||||||||||||| |!|"|#|$|%|&|'|(|)*}+|+j	 },t
j|,jd.}-t|-S )/zz
    Django View : endpoint
    Used by Goodone document.php
    :param request: httpRequest
    :rtype: HttpResponse
    r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   country
isKauflandzapplication/pdf)rB   z"attachment; filename="invoice.pdf"zContent-Dispositionzutf-8)rC   rD   boolintr   r   rE   rF   rG   rH   base64	b64encodedecode).rJ   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rK   rR   
iskauflandrL   rM   rN   
pdf_base64rO   rO   rP   new_print_pdf   s    	

r[   c       .   *   C   s  | j jd}tt|}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd	}	| j jd
}
| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd } | j jd!}!| j jd"}"| j jd#}#| j jd$}$| j jd%}%| j jd&}&| j jd'}'| j jd(}(| j jd)})tt|)})td*d+}*d,|*d-< tjtj |||||||||	|
|||||||||||||||||||||| |!|"|#|$|%|&|'|()}+|+j	 },t
j|,jd.}-t|-S )/z
       Django View : endpoint
       Used by Goodone document.php
       :param request: httpRequest
       :rtype: HttpResponse
       r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rR   rS   zapplication/pdf)rB   z"attachment; filename="invoice.pdf"zContent-Dispositionzutf-8)rC   rD   rT   rU   r   r   rE   rF   rG   rH   rV   rW   rX   ).rJ   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rK   rR   rY   rL   rM   rN   rZ   rO   rO   rP   uk_print_pdf  s    	

r\   c       .   *   C   s  | j jd}tt|}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd	}	| j jd
}
| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd } | j jd!}!| j jd"}"| j jd#}#| j jd$}$| j jd%}%| j jd&}&| j jd'}'| j jd(}(| j jd)})tt|)})td*d+}*d,|*d-< tjtj |||||||||	|
|||||||||||||||||||||| |!|"|#|$|%|&|'|()}+|+j	 },t
j|,jd.}-t|-S )/z
       Django View : endpoint
       Used by Goodone document.php
       :param request: httpRequest
       :rtype: HttpResponse
       r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rR   rS   zapplication/pdf)rB   z"attachment; filename="invoice.pdf"zContent-Dispositionzutf-8)rC   rD   rT   rU   r   r   rE   rF   rG   rH   rV   rW   rX   ).rJ   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rK   rR   rY   rL   rM   rN   rZ   rO   rO   rP   es_print_pdfv  s    	

r]   c       .   *   C   s  | j jd}tt|}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd	}	| j jd
}
| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd } | j jd!}!| j jd"}"| j jd#}#| j jd$}$| j jd%}%| j jd&}&| j jd'}'| j jd(}(| j jd)})tt|)})td*d+}*d,|*d-< tjtj |||||||||	|
|||||||||||||||||||||| |!|"|#|$|%|&|'|()}+|+j	 },t
j|,jd.}-t|-S )/z
       Django View : endpoint
       Used by Goodone document.php
       :param request: httpRequest
       :rtype: HttpResponse
       r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rR   rS   zapplication/pdf)rB   z"attachment; filename="invoice.pdf"zContent-Dispositionzutf-8)rC   rD   rT   rU   r   r   rE   rF   rG   rH   rV   rW   rX   ).rJ   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rK   rR   rY   rL   rM   rN   rZ   rO   rO   rP   fr_print_pdf  s    	

r^   c       .   *   C   s  | j jd}tt|}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd	}	| j jd
}
| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd } | j jd!}!| j jd"}"| j jd#}#| j jd$}$| j jd%}%| j jd&}&| j jd'}'| j jd(}(| j jd)})tt|)})td*d+}*d,|*d-< tjtj |||||||||	|
|||||||||||||||||||||| |!|"|#|$|%|&|'|()}+|+j	 },t
j|,jd.}-t|-S )/zz
    Django View : endpoint
    Used by Goodone document.php
    :param request: httpRequest
    :rtype: HttpResponse
    r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rR   rS   zapplication/pdf)rB   z"attachment; filename="invoice.pdf"zContent-Dispositionzutf-8)rC   rD   rT   rU   r   r   rE   rF   rG   rH   rV   rW   rX   ).rJ   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rK   rR   rY   rL   rM   rN   rZ   rO   rO   rP   it_print_pdfL  s    	

r_   c             C   s"   | j jd}tj|}t|ddS )zz
    Django View : endpoint
    Used by Goodone document.php
    :param request: httpRequest
    :rtype: HttpResponse
    rR   zapplication/json; charset=utf-8)rB   )rC   rD   r   ZgetCountryEmailr   )rJ   rR   
email_inforO   rO   rP   email_message  s    	
ra   c       @   ,   C   s  | j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd	}	| j jd
}
| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd } | j jd!}!| j jd"}"| j jd#}#| j jd$}$| j jd%}%| j jd&}&| j jd'}'| j jd(}(| j jd)})| j jd*}*| j jd+}+| j jd,},| j jd-}-| j jd.}.| j jd/}/tjd0|" d1  td2d3}0d4|0d5< tjtj |||||||||	|
||||||||||||||||||||| |!|"|#|$|%|&|'|(|)|*|+}1|1j	 }2t
j|2jd6}3|0j|2 | j jd}| j jd+}+|+r
tjd7|+  n|d8krd9}+nd:}+tjjtjjt}4|/d;krld<|4|+|f }5td=|5  t|5d>}6|6j|2 nNtjj|4d?}7tj|7d@dA dB|4|+|dC f }5td=|5  t|5d>}6|6j|2 dD}8tj|dEdF}9|9dG }:xzt|:D ]n\};}<|<dH dIk	rt|<dH r|8|<dH 7 }8|<dJ dIk	r0|8dKt|<dL  7 }8|;t|:dM kr|8dN7 }8qW g }=|/d;krdOt|+krtjtj||"|8 tj  }=nRt|+dPkrt!jtj||"|8 t!j  }=n$t|+dQkrt"jtj||"|8 t"j  }=nt|+dRkrt#jtj||"|8 t#j  }=nt|+dSkr>t$jtj||"|8 t$j  }=nt|+dTkrjt%jtj||"|8 t%j  }=ntt|+dUkrt&jtj||"|8 t&j  }=nHt|+d:krt'jtj||"|8 t'j  }=nt(jtj||"|8 t(j  }=|/d;kr0tj)tj |=}>|>j	 }?d<|4|+dVf }5td=|5  t|5d>}6|6j|? n|=j*  tdW t+||+|4|||||	|
|||||||||||||&|*|,|-|.||" t,||+|4|||||	|
||||||||||||||&|*|,|-|.||"|%||/  t|3S )XzY
    Django View : endpoint
    :param request: httpRequest
    :rtype: HttpResponse
    r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r@   r1   r2   r3   r4   r5   r6   r7   elementsr8   invoiceCommentorderId	billingNruserIduserIdPlattformpayment_dateinvoice_dateshipping_daterA   working_daysprefixweightcountOfPackagesmemoisOneLieferscheinz$print_deliverynote with the orderId z
 -------->zapplication/pdf)rB   z"attachment; filename="invoice.pdf"zContent-Dispositionzutf-8z#print_deliverynote with the prefix Truesogoodmaimai24falsez%s/../pdf_files/%s-%s.pdfzfile path is  %swbz../pdf_merge_filesT)exist_okz%s/../pdf_merge_files/%s-%s.pdfz-merge F)strictitemsstammbezeichnungN
packstueckz (X) quantityr	   
EKIDEITFRz	DE-SOGOODz	FR-SOGOODz	IT-SOGOODr   z!skip picklist, list_clone cleared)-rC   rD   loggerinfor   r   rE   rF   rG   rH   rV   rW   rX   rI   ospathdirnameabspath__file__printopenjoinmakedirsjsonloads	enumeratestrlenlist_ekiappendr   Ordercopylist_delist_itlist_frlist_desogoodlist_frsgoodlist_itsogoodlist_maimai24list_othersZPicklistPrintableclear
write_xlsx	write_csv)@rJ   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r@   r1   r2   r3   r4   r5   r6   r7   rb   r8   rc   rd   re   rf   rg   rh   ri   rj   rK   rk   rl   rm   rn   ro   rp   rL   rM   rN   rZ   source_path	file_pathfilemerge_backup_dirZproductNameloaded_json
items_jsonixZ
list_cloneZpicklist_printZpicklist_pdf_blobrO   rO   rP   print_deliverynote  s   
















r   c                C   s   t jd|  dt|ks>dt|ks>dt|ks>dt|kr|dkrt| |||||||||	|
||||||||||||||||||||S |dkrt| |||||||||	|
||||||||||||||||||||S d S d S )Nzwrite_csv with orderId z	DE-SOGOODz	FR-SOGOODzDE-EKIr   rt   italy)r   r   r   write_csv_ekiwrite_csv_italy) r   rl   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r@   rh   rk   rm   rn   ro   r7   rd   rg   rb   rp   rO   rO   rP   r     s     8r   c       O   /   C   sX  t jd|  |}|r|n
|r"|nd} |r.|n
|r6|nd}!|!d7 }!|!|rL|n
|rT|nd7 }!|rb|n
|rj|nd}"|rv|n
|	r~|	nd}#|r|n
|
r|
nd}$|r|n
|r|nd}%|r|nd}&d}'d}(d})d}*d}+d},|}-d}.|}/d}0d}1d}2d}3d}4d}5d}6d}7d}8d}9d}:d};d}<d}=d}>d}?d}@yltj|dd}Atj|dd}B|Ad }CxFt|CD ]:\}D}Et jd	t|D  |* rP|Ed
 d k	rP|Ed
 }*qPW |Bd }Fxt|FD ]t\}D}Et jdt|D  d}G|Ed d k	r|G|Ed 7 }G|Ed d k	r|Gdt|Ed  7 }G|Ddkr|G}1n|Ddkr|G}2n|Ddkr.|G}3n|Ddkr>|G}4n|DdkrN|G}5n|Ddkr^|G}6n|Ddkrn|G}7n|Ddkr~|G}8nz|Ddkr|G}9nj|Ddkr|G}:nZ|Ddkr|G};nJ|Ddkr|G}<n:|Ddkr|G}=n*|Ddkr|G}>n|Ddkr|G}?n
t jd |Ed d k	r|.t|Ed 7 }.qW |*d kr*d!}*| d"kr8d#nd$}+|+},tjj	|d%}H|Hj
d&}'|'}(|d'krl|H}In|Htjt|d( }I|Ij
d&})W n. tk
r }J zt j|J W Y d d }J~JX nX || |!|"|#|$|%|&|'|(|)|*|+|,|-|.|/|0|1|2|3|4|5|6|7|8|9|:|;|<|=|>|?g!g}Kd)|||f }Ltd*|L  t|Ld+}M|M" tj|Mtjd,d-}N|Nj|K W d Q R X d S ).Nzwrite_csv_eki with orderId rw    r   
ZustellungF)rx   ry   z#print_deliverynote CSV items index marketplacerb   z%print_deliverynote CSV element index namecountz x r	                        	   
               zsize of solditems over 15eanHitflipKauflandrq   rr   rs   z%d.%m.%Y %H:%M:%Sz%d.%m.%YX)daysz%s/../csv_files/%s-%s.csvzcsv file path is  %sw;)quoting	delimiter)r   r   r   r   r   r   errorrU   datetimestrptimestrftime	timedelta	Exceptionr   r   csvwriterQUOTE_NONNUMERIC	writerows)Or   rl   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r@   rh   rk   rm   rn   ro   r7   rd   rg   rb   csv_Auftragsnummer_1Zcsv_company_2Z
csv_Name_3Zcsv_Strasse_4Z	csv_PLZ_5Z	csv_Ort_6Zcsv_Telefon_7Zcsv_Email_8Zcsv_Kaufdatum_9Zcsv_Verladedatum_10Zcsv_Lieferdatum_11Zcsv_Plattform_12Zcsv_seller_13Zcsv_Versender_14Zcsv_Gewicht_15Zcsv_Anzahl_16Zcsv_Memo_17Zcsv_ZustellungOderAbholung_18Zcsv_Produktbezeichnung1_19Zcsv_Produktbezeichnung2_20Zcsv_Produktbezeichnung3_21Zcsv_Produktbezeichnung4_22Zcsv_Produktbezeichnung5_23Zcsv_Produktbezeichnung6_24Zcsv_Produktbezeichnung7_25Zcsv_Produktbezeichnung8_26Zcsv_Produktbezeichnung9_27Zcsv_Produktbezeichnung10_28Zcsv_Produktbezeichnung11_29Zcsv_Produktbezeichnung12_30Zcsv_Produktbezeichnung13_31Zcsv_Produktbezeichnung14_32Zcsv_Produktbezeichnung15_33Zcsv_Category_XXZitemLoadJsonr   r   r   r   Zelements_jsonZproduktbezeichnungpayment_date_objdelivery_date_objenmscsv_file_pathfr   rO   rO   rP   r   *  s    





















r   c       F   "   C   s  t jd|  |r|nd}d}d}d} d}!d}"|}#d}$d}%d}&d}'d}(d})d}*d	}+|},d}-|rb|n
|rj|nd}-|-d
7 }-|-|r|n
|r|nd7 }-d}.|r|n
|r|nd}.d}/|r|n
|	r|	nd}0|r|n
|
r|
nd}1d}2d}3d}4|}5|-}6|r|n
|r|nd}7|r|nd}8|7}9d}:yZtj|dd};|;d }<xt|<D ]\}=}>|>d d k	rX|"|>d 7 }"n|"|>d 7 }"|>d d k	r|"dt|>d  7 }"|=t|<d kr|"d7 }"|>d d k	r|&t|>d 7 }&|  r|>d d k	r|>d } |% r4|>d d k	r4|%|>d 7 }%q4W | dkrd} | dkr dnd}!tjj	|d }?|?j
d!}|d"krL|?}@n|?tjt|d# }@|@j
d!}W n. tk
r }A zt j|A W Y d d }A~AX nX |'|(|)|*|+|,|-|.|/|0|1|2|3|4|5|6|7|8|9|:gg}Bd$|||f }Ctd%|C  t|Cd&}D|D  tj|Dtjd'}E|Ej|B W d Q R X d S )(Nzwrite_csv_italy with orderId rw   r   r   16013893001601Cr   BSr   r   F)rx   ry   rz   r8   r{   z x r|   r	   r   r   categoryr   r   rq   rr   rs   z%d.%m.%Y %H:%M:%Sz%d.%m.%Yr   )r   z%s/../csv_files/%s-%s.csvzcsv file path is  %sr   )r   )r   r   r   r   r   r   r   rU   r   r   r   r   r   r   r   r   r   r   r   r   )Fr   rl   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r@   rh   rk   rm   rn   ro   r7   rd   csv_Email_7csv_Kaufdatum_8csv_Lieferdatum_9csv_Plattform_10csv_seller_11csv_Produktbezeichnung_12csv_Memo_15csv_ZustellungOderAbholung_16csv_Category_17csv_Anzahl_14csv_VABCCM_1csv_VABCTR_2csv_VABLNP_3csv_VABCBO_4csv_VABTSP_5csv_VABRMN_6csv_VABRSD_7csv_VABIND_8csv_VABNOT_9csv_VABCAD_10csv_VABLOD_11csv_VABPRD_12csv_VABNZD_13csv_VABNCL_14csv_VABPKB_15csv_VABNRC_16csv_VABTRC_17csv_VABEMD_18csv_VABCEL_19csv_VABTNO_20r   r   r   r   r   r   r   r   r   r   r   rO   rO   rP   r     s    





r   c             C   s`  | j jd}| j jd}|s.tjd tdS tjd|  tj|dd}|d }g }xt|D ]\}}|d d k	rt	|d r|d }|d	 d k	rt	|d	 r|d	 }	|d
 d k	rt	|d
 r|d
 }
tjd| d |	 d |
  |j
tj||	|
 q`W tjtj |}|j }tjjtjjt}d||df }td|  t|d}|j| tdS )Nrl   collisz/stockup_pdf failed because of missing of prefixz$stockup failed due to missing prefixzstockup_pdf with prefix F)rx   r   titler   zstockup_pdf with ean z name z
 quantity z%s/../pdf_files/%s-%s.pdfr   zfile path is  %sru   zstockup pdfs)rC   rD   r   r   r   r   r   r   r   r   r   r   ColliStockupPrintablerF   rG   rH   r   r   r   r   r   r   r   rI   )rJ   rl   r   r   collis_jsonlist_collisr   r   r   r   r   stockup_printstockup_pdf_blobr   r   r   rO   rO   rP   stockup_pdfV  s4    


r  c             C   s.  | j jd}| j jd}|s.tjd tdS tjd|  tj|dd}|d }g }xt|D ]\}}|d d k	rt	|d r|d }|d	 d k	rt	|d	 r|d	 }	|d
 d k	rt	|d
 r|d
 }
tjd| d |	 d |
  |j
tj||	|
 q`W tjtj |}|j }tj|jd}t|S )Nrl   r   z/stockup_pdf failed because of missing of prefixz$stockup failed due to missing prefixzstockup_pdf with prefix F)rx   r   r   r   zstockup_pdf with ean z name z
 quantity zutf-8)rC   rD   r   r   r   r   r   r   r   r   r   r   r  r  rF   rG   rH   rV   rW   rX   )rJ   rl   r   r   r  r  r   r   r   r   r   r  r  rZ   rO   rO   rP   eki_stockup_pdfu  s,    
r  c                s.  | j jd  s"tjd tdS tjd   tjjtjj	t
}tj|d   } fdd|D }|jtjjd t }d	}x2|D ]*}t|}d
|krd|kr|t|j7 }qW d}x||D ]t}t|}d
|krd|krt|||\}}n t }x|jD ]}	|j|	 qW tj }
|j|
 |
jd	 |j|
 qW tjd}tjj|}|jd}|d| f  }t|d}|j| W d Q R X tjd|  tjj |d}tj!|dd xb|D ]Z}tjj"|}tjj#|\}}| d| }tjj ||}t$j%|| tjd|  qW x|D ]}tj&| qW dt' krJt(j)  nt' dkrbt*j)  nt' dkrzt+j)  nt' dkrt,j)  nht' dkrt-j)  nPt' dkrt.j)  n8t' dkrt/j)  n t' dkrt0j)  nt1j)  t2 | t3  t4j5t|dj6 j7d }t|S )!Nrl   z-merge_pdf failed because of missing of prefixz"merge failed due to missing prefixzmerge_pdf with prefix z/../pdf_files/%s-*.pdfc                s&   g | ]}t jd t j  |r|qS )z%s-[\w\d]+\.pdf$)researchescape).0r   )rl   rO   rP   
<listcomp>  s    zmerge_pdf.<locals>.<listcomp>)keyr   r   r   r	   zEurope/Berlinz%Y-%m-%d.%H%M%Sz/../pdf_files/%s.%s.pdfru   zMerged PDF saved to: z../pdf_merge_filesT)rv   z-mergezBackup copied to: r~   r   r   r   z	DE-SOGOODz	IT-SOGOODz	FR-SOGOODrs   rbzutf-8)8rC   rD   r   r   r   r   r   r   r   r   r   globsortgetmtimer   r   r   pagesadd_page_numbersr   addPagerF   rG   rI   seekr   pytztimezoner   nowr   r   r   r   basenamesplitextshutilcopy2remover   r   r   r   r   r   r   r   r   r   r   	merge_csv
merge_xlsxrV   rW   readrX   )rJ   r   pathsZ
pdf_mergertotal_pagesr   readerstarting_pager   pageZtemp_pdf_ioZberlinr  formatted_timeZmerged_pdf_pathfileobjr   filenamer   extZnew_filenameZbackup_pathrZ   rO   )rl   rP   	merge_pdf  s    

















r+  c       
      C   s   t  }t| j}xt|D ]}| j| }tj }tj|td}|j	dd |j
dt dt d|| |f  |j  |jd t|jd }	|j|	 |j| qW ||| fS )N)pagesizeZ	Helveticar   d   r   z	%d von %dr   )r   r   r  rangerF   rG   r   ZCanvasr   ZsetFontZdrawCentredStringr   saver  r   Z	mergePager  )
r$  r%  r#  r   	num_pagesr   r&  packetcZpage_with_numberrO   rO   rP   r    s    



r  c       	         s   t jd   tjjtjjt}tj|d   } fdd|D }|jtjj	d |spt jd  d  d S |d| f  }t
|d	6}x.|D ]&}t
|}|j|j  W d Q R X qW W d Q R X x|D ]}tj| qW d S )
Nzmerge_csv with prefix z/../csv_files/%s-*.csvc                s&   g | ]}t jd t j  |r|qS )z%s-\d+\.csv$)r	  r
  r  )r  r   )rl   rO   rP   r    s    zmerge_csv.<locals>.<listcomp>)r  zmerge_csv the z
 is empty!z/../csv_files/%s.%s.csvr   )r   r   r   r   r   r   r   r  r  r  r   rI   r!  r  )	rl   r'  r   r"  Zcombined_csv_filepathoutfilefnameinfiler   rO   )rl   rP   r    s    

&
r  c             C   sd   t jjt jjt}tj|d|   }|j  |sHtjd|  d  d S x|D ]}t j	| qNW d S )Nz/../xlsx_files/%s-*.xlsxzmerge_excel the z
 is empty!)
r   r   r   r   r   r  r  r   r   r  )rl   r   r"  r   rO   rO   rP   r     s    
r   c             C   s   t jd|  |dkrTt| |||||||||	|
|||||||||||||||||S |dkrt jd t| |||||||||	|
|||||||||||||||||S t jd d S d S )Nzwrite_xlsx with orderId z	DE-SOGOODr   zwrite_xlsx_italy DUPAzwrite_xlsx_none DUPA)r   r   write_xlsx_ekiwrite_xlsx_italy)r   rl   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r+   r,   r-   r.   r/   r0   r@   rh   rk   rm   rn   ro   r7   rd   rO   rO   rP   r   ,  s|    

r   c       D   "   C   s  t jd|  |r|nd}d}d}d}d} d}!d}"|}#d}$d}%d}&d}'d}(d})d	}*|}+d},|rb|n
|rj|nd},|,d
7 },|,|r|n
|r|nd7 },d}-|r|n
|r|nd}-d}.|r|n
|	r|	nd}/|r|n
|
r|
nd}0d}1d}2d}3|}4|,}5|r|n
|r|nd}6|r|nd}7|6}8d}9yZtj|dd}:|:d };xt|;D ]\}<}=|=d d k	rX|!|=d 7 }!n|!|=d 7 }!|=d d k	r|!dt|=d  7 }!|<t|;d kr|!d7 }!|=d d k	r|"t|=d 7 }"| r|=d d k	r|=d }|% r4|=d d k	r4|%|=d 7 }%q4W |dkrd}| dkr dnd} tjj	|d }>|>j
d!}|d"krL|>}?n|>tjt|d# }?|?j
d!}W n. tk
r }@ zt j|@ W Y d d }@~@X nX |&|'|(|)|*|+|,|-|.|/|0|1|2|3|4|5|6|7|8|9gg}Atj|A}Bd$|||f }Ct jd%|C  |Bj|Cdd& td'|C  d S )(Nzwrite_xlsx_italy with orderId rw   r   r   r   r   r   r   r   r   r   r   r   F)rx   ry   rz   r8   r{   z x r|   r	   r   r   r   r   r   r   rq   rr   rs   z%d.%m.%Y %H:%M:%Sz%d.%m.%Yr   )r   z%s/../xlsx_files/%s-%s.xlsxexcel_file_path)indexzExcel file path is %s)r   r   r   r   r   r   r   rU   r   r   r   r   r   r   pd	DataFrameto_excelr   )Dr   rl   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r+   r,   r-   r.   r/   r0   r@   rh   rk   rm   rn   ro   r7   rd   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   datadfr8  rO   rO   rP   r7    s    





r7  c       7       C   s  t jd|  |}|r|n
|r"|nd}|d7 }||r8|n
|r@|nd7 }|rN|n
|rV|nd}|rb|n
|	rj|	nd}|rv|n
|
r~|
nd} |r|n
|r|nd}!|r|nd}"d}#d}$d}%d}&d}'|}(d})|}*d}+d},yTtj|dd}-|-d }.xt|.D ]\}/}0|0d	 d k	r|'|0d	 7 }'n|'|0d
 7 }'|0d d k	r@|'dt|0d  7 }'|/t|.d krZ|'d7 }'|0d d k	rx|)t|0d 7 })|% r|0d d k	r|0d }%|, r|0d d k	r|,|0d 7 },qW |%dkrd}%| dkrdnd}&tjj	|d}1|1j
d}#|dkr |1}2n|1tjt|d }2|2j
d}$W n. tk
rN }3 zt j|3 W Y d d }3~3X nX |g|g|g|g| g|!g|"g|#g|$g|%g|&g|'g|(g|)g|*g|+g|,gd}4tj|4}5d|||f }6t jd|6  |5j|6dd td |6  d S )!Nzwrite_xlsx_eki with orderId rw   r   r   r   F)rx   ry   rz   r8   r{   z x r|   r	   r}   r   r   r   r   r   rq   rr   rs   z%d.%m.%Y %H:%M:%Sz%d.%m.%Yr   )r   )ZAuftragsnummerNameZStrasseZPLZZOrtZTelefonEmailZ	KaufdatumZLieferdatumZ	PlattformZSellerZProduktbezeichnungZGewichtZAnzahlZMemoZZustellungOderAbholungZCategoryz%s/../xlsx_files/%s-%s.xlsxr8  )r9  zExcel file path is %s)r   r   r   r   r   r   r   rU   r   r   r   r   r   r   r:  r;  r<  r   )7r   rl   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r+   r,   r-   r.   r/   r0   r@   rh   rk   rm   rn   ro   r7   rd   r   Z
csv_Name_2Zcsv_Strasse_3Z	csv_PLZ_4Z	csv_Ort_5Zcsv_Telefon_6r   r   r   r   r   r   Zcsv_Gewicht_13r   r   r   r   r   r   r   r   r   r   r   r=  r>  r8  rO   rO   rP   r6    s    




r6  c             C   s  | j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}	| j jd	}
| j jd
}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}tjd| d  tdd}d|d< tjtj ||||||||	|
||||||||||||}|j	 }t
j|jd}t|dd}|S )zi
        Django View : endpoint
        :param request: httpRequest
        :rtype: HttpResponse
        r   zOriginale BestellNrZBestelldatumr   Zcompanyr   ZlastNameZ	firstNameZstreetZ
postalCodeZcityZ
countryISOmailphonerb   rm   rn   ro   ZErstellungsdatumzVersand bis spaetestensrg   xNachlieferungIdz$print_deliverynote with the orderId z
 -------->zapplication/pdf)rB   z"attachment; filename="invoice.pdf"zContent-Dispositionzutf-8z
text/plain)rC   rD   r   r   r   r   rE   rF   rG   rH   rV   rW   rX   )rJ   r   r   r   rd   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   rb   rm   rn   ro   ZerstellungsdatumZversandrg   rC  rL   rM   rN   rZ   rO   rO   rP   print_nachlieferungU  sh    

rD  c       '   $   C   s(  | j jd}tt|}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd	}	| j jd
}
| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd } | j jd!}!| j jd"}"td#d$}#d%|#d&< tjtj |||||||||	|
|||||||||||||||||||||| |!|"#}$|$j	 }%t
j|%jd'}&t|&S )(zz
    Django View : endpoint
    Used by Goodone document.php
    :param request: httpRequest
    :rtype: HttpResponse
    r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   rA   rR   rd   systemIdzapplication/pdf)rB   z"attachment; filename="invoice.pdf"zContent-Dispositionzutf-8)rC   rD   rT   rU   r   r   rE   rF   rG   rH   rV   rW   rX   )'rJ   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   rK   rR   rd   rE  rL   rM   rN   rZ   rO   rO   rP   print_gutschrift  s    	

rF  c       '   $   C   s(  | j jd}tt|}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd	}	| j jd
}
| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd } | j jd!}!| j jd"}"td#d$}#d%|#d&< tjtj |||||||||	|
|||||||||||||||||||||| |!|"#}$|$j	 }%t
j|%jd'}&t|&S )(zz
    Django View : endpoint
    Used by Goodone document.php
    :param request: httpRequest
    :rtype: HttpResponse
    r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   rA   rR   rd   rE  zapplication/pdf)rB   z"attachment; filename="invoice.pdf"zContent-Dispositionzutf-8)rC   rD   rT   rU   r   r   rE   rF   rG   rH   rV   rW   rX   )'rJ   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   rK   rR   rd   rE  rL   rM   rN   rZ   rO   rO   rP   print_it_gutschrift  s    	

rG  c       '   $   C   s(  | j jd}tt|}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd	}	| j jd
}
| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd } | j jd!}!| j jd"}"td#d$}#d%|#d&< tjtj |||||||||	|
|||||||||||||||||||||| |!|"#}$|$j	 }%t
j|%jd'}&t|&S )(zz
    Django View : endpoint
    Used by Goodone document.php
    :param request: httpRequest
    :rtype: HttpResponse
    r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   rA   rR   rd   rE  zapplication/pdf)rB   z"attachment; filename="invoice.pdf"zContent-Dispositionzutf-8)rC   rD   rT   rU   r   r   rE   rF   rG   rH   rV   rW   rX   )'rJ   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   rK   rR   rd   rE  rL   rM   rN   rZ   rO   rO   rP   print_uk_gutschriftW  s    	

rH  c       '   $   C   s(  | j jd}tt|}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd	}	| j jd
}
| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd}| j jd } | j jd!}!| j jd"}"td#d$}#d%|#d&< tjtj |||||||||	|
|||||||||||||||||||||| |!|"#}$|$j	 }%t
j|%jd'}&t|&S )(zz
    Django View : endpoint
    Used by Goodone document.php
    :param request: httpRequest
    :rtype: HttpResponse
    r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   rA   rR   rd   rE  zapplication/pdf)rB   z"attachment; filename="invoice.pdf"zContent-Dispositionzutf-8)rC   rD   rT   rU   r   r   rE   rF   rG   rH   rV   rW   rX   )'rJ   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   rK   rR   rd   rE  rL   rM   rN   rZ   rO   rO   rP   print_fr_gutschrift  s    	

rI  )Q__doc__rV   rF   r  r   loggingr   r   r	  r   Zpandasr:  r  ZPyPDF2r   r   r   django.views.decorators.csrfr   Zreportlab.pdfgenr   Zreportlab.lib.unitsr   Zreportlab.lib.pagesizesr   r  rw   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   Zemailmessager   django.httpr   	getLoggerr   r   r   r   r   r   r   r   r   r   rQ   r[   r\   r]   r^   r_   ra   r   r   r   r   r  r  r+  r  r  r   r   r7  r6  rD  rF  rG  rH  rI  rO   rO   rO   rP   <module>   s   
clkkkj  d /~U\sZD___