3
^j56                 @   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	m	Z	 ddl
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mZ ejd	Zeed
ddZedd Zedd Zedd ZdddZdS )z
 * @author Xiaobo Sun
 * created  on 29.11.2025
 * @copyright (C) 2025 Sogood International GmbH - all rights reserved
 * @licence
 * Unauthorized copying of this file, via any medium is strictly prohibited
 * Proprietary and confidential
    N)datetime)HttpResponse)csrf_exempt   )TaxItem)CONFIGCompanyEntrydjango)filenamereturnc             C   s$   d}t j|d| }|j jd}|S )Nz[<>:"/\\|?*+]-.)resubstrip)r
   invalid_charsZ
clean_name r   */data/git/entellipy_wy/datev/datevviews.pysanitize_filename   s    r   c             C   s*  | j jd}tt| j jd}| j jd}| j jd}tj|d}| j jd}tj|d}t| j jd}t| j jd}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}tjd| d | d | d |	 d t	|  t
||||djdd|djdd|	|
|||||||||}|rtjd ntjd }tjjtjjt}tjjtjj|dd}tj|d d! d"|||t|	||jd#f }t|d$d%d&$}|j|}|d'kr|j| W d'Q R X td(| S ))zu
    Django View : endpoint
    Used by E2 x_controller
    :param request: httpRequest
    :rtype: HttpResponse
    prefix	is_sogoodorder_idpayment_datez%d.%m.%Y %H:%M:%S
order_datepayment_full_amountpayment_already_paidtax
payment_idinvoice_urlcustomer_countryselling_countrylager_countrysystemIdsystemInvoiceIdcustomerTaxId	taxItemIdkontozprint_datev with prefix z
 order_id z
 systemId z payment_id z tax z.2fr   ,r   r   z..Zdatev_filesT)exist_okz$%s/../datev_files/%s_%s_%s_%s_%s.csvz%Y%m%dwzutf-8)encodingNzprint_datev with file_path )POSTgetboolintr   strptimefloatloggerinfostrr   replacer   companyospathdirnameabspath__file__joinmakedirsr   strftimeopenZtoDatevwriter   )requestr   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   ZtaxItemr5   source_pathZout_dir	file_pathfileZstrDr   r   r   print_datev(   sH    	2

rD   c                s  | j jd tt| j jd}|r.tjd ntjd }tjd   tj	j
tj	jt}tj|d   } fdd|D }|jtj	jd	 d
}d}g }x|D ]}tj	j|}	tj	j|	\}
}t|}|j j }W dQ R X | rtjd|	  q|
jdd }|
jdd }tjd|	 d| d|  t|t|k rF|}t|t|kr\|}tjd| d  |j| tj| qW tjd| d |  tjd}tj|}d|jd d|j d|j  dd d| d| d|dd  d|dd  d }d!d" d# d$ d% d& d' d( d) d* d+ d, d- d. d/ d0 d1 d2 d3 d4 d5 d6 d7 d8 d9 d: d; d< d= d> d? d@ dA dB dC dD dE dF dG dH dI dJ dK dL dM dN dO dP dQ dR dS dT dU dV dW dX dY dZ d[ d\ d] d^ d_ d` da db dc dd de df dg dh di dj dk dl dm dn do dp dq dr ds dt du dv dw dx dy dz d{ d| d} d~ d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d }tj	j
tj	jt}d|jd d }d||f }yjt|ddd@}|j!|d  |j!|d  x|D ]}|j!|d  qVW W dQ R X tjd| d W n: t"k
r } ztj#d| d|  W Y dd}~X nX t$||d t|ddd}|j }W dQ R X t%j&|j'dj(d}t)|S )zu
    Django View : endpoint
    Used by E2 x_controller
    :param request: httpRequest
    :rtype: HttpResponse
    r   r   r   r   zmerge_datev with prefix z/../datev_files/%s_*.csvc                s&   g | ]}t jd t j  |r|qS )z0%s_[a-zA-Z0-9-]+_[a-zA-Z0-9-]+_[\d]+_[\d]+\.csv$)r   searchescape).0f)r   r   r   
<listcomp>o   s    zmerge_datev.<locals>.<listcomp>)keyZ20301231Z20200101NzSkipping empty file: _   zfile: z : []zmerge_datev min_orderDate z max_orderDate zEurope/Berlinz""EXTF";700;21;"Buchungsstapel";13;z%Y%m%d%H%M%Sz000;;"JE";"JERA2FIBU";"";;z;"20250101";   z	;"Belege /   z!";JE;1;0;0;"EUR";;"";;;"";;;"";""zUmsatz (ohne Soll/Haben-Kz);zSoll/Haben-Kennzeichen;zWKZ Umsatz;zKurs;zBasis-Umsatz;zWKZ Basis-Umsatz;zKonto;u    Gegenkonto (ohne BU-Schlüssel);u   BU-Schlüssel;zBelegdatum;zBelegfeld 1;zBelegfeld 2;zSkonto;zBuchungstext;zPostensperre;zDiverse Adressnummer;u   Geschäftspartnerbank;zSachverhalt;zZinssperre;z
Beleglink;zBeleginfo - Art 1;zBeleginfo - Inhalt 1;zBeleginfo - Art 2;zBeleginfo - Inhalt 2;zBeleginfo - Art 3;zBeleginfo - Inhalt 3;zBeleginfo - Art 4;zBeleginfo - Inhalt 4;zBeleginfo - Art 5;zBeleginfo - Inhalt 5;zBeleginfo - Art 6;zBeleginfo - Inhalt 6;zBeleginfo - Art 7;zBeleginfo - Inhalt 7;zBeleginfo - Art 8;zBeleginfo - Inhalt 8;zKOST1 - Kostenstelle;zKOST2 - Kostenstelle;zKost-Menge;zEU-Land und UStID (Bestimmung);zEU-Steuersatz (Bestimmung);zAbw. Versteuerungsart;zSachverhalt L+L;u   Funktionsergänzung L+L;zBU 49 Hauptfunktionstyp;zBU 49 Hauptfunktionsnummer;u   BU 49 Funktionsergänzung;zZusatzinformation - Art 1;zZusatzinformation- Inhalt 1;zZusatzinformation - Art 2;zZusatzinformation- Inhalt 2;zZusatzinformation - Art 3;zZusatzinformation- Inhalt 3;zZusatzinformation - Art 4;zZusatzinformation- Inhalt 4;zZusatzinformation - Art 5;zZusatzinformation- Inhalt 5;zZusatzinformation - Art 6;zZusatzinformation- Inhalt 6;zZusatzinformation - Art 7;zZusatzinformation- Inhalt 7;zZusatzinformation - Art 8;zZusatzinformation- Inhalt 8;zZusatzinformation - Art 9;zZusatzinformation- Inhalt 9;zZusatzinformation - Art 10;zZusatzinformation- Inhalt 10;zZusatzinformation - Art 11;zZusatzinformation- Inhalt 11;zZusatzinformation - Art 12;zZusatzinformation- Inhalt 12;zZusatzinformation - Art 13;zZusatzinformation- Inhalt 13;zZusatzinformation - Art 14;zZusatzinformation- Inhalt 14;zZusatzinformation - Art 15;zZusatzinformation- Inhalt 15;zZusatzinformation - Art 16;zZusatzinformation- Inhalt 16;zZusatzinformation - Art 17;zZusatzinformation- Inhalt 17;zZusatzinformation - Art 18;zZusatzinformation- Inhalt 18;zZusatzinformation - Art 19;zZusatzinformation- Inhalt 19;zZusatzinformation - Art 20;zZusatzinformation- Inhalt 20;u   Stück;zGewicht;z
Zahlweise;zForderungsart;zVeranlagungsjahr;u   Zugeordnete Fälligkeit;z
Skontotyp;zAuftragsnummer;zBuchungstyp;u   Ust-Schlüssel (Anzahlungen);zEU-Land (Anzahlungen);zSachverhalt L+L (Anzahlungen);zEU-Steuersatz (Anzahlungen);u   Erlöskonto (Anzahlungen);zHerkunft-Kz;z	Leerfeld;zKOST-Datum;zMandatsreferenz;zSkontosperre;zGesellschaftername;zBeteiligtennummer;zIdentifikationsnummer;zZeichnernummer;zPostensperre bis;zBezeichnung SoBil-Sachverhalt;zKennzeichen SoBil-Buchung;zFestschreibung;zLeistungsdatum;zDatum Zuord.Steuerperiode;u   Fälligkeit;zGeneralumkehr (GU);zSteuersatz;zLand;zAbrechnungsreferenz;zBVV-Postion;zEU-Land und UStID (Ursprung);zEU-Steuersatz (Ursprung);ZEXTF_Buchungsstapel_Belege_z%Y%m%d_%H%M%Sz.csvz%s/../datev_files/%sr)   cp1252)r*   
u   ✅ write to z successfullyu   ❌ write to z	 failed:    rzutf-8)*r+   r,   r-   r.   r   r5   r1   r2   r6   r7   r8   r9   r:   globsortgetmtimebasenamesplitextr>   readr   warningsplitappendremovedebugpytztimezoner   nowr=   ZberaternummerZmandantennummerr?   IOErrorerrorreverse_csv_rowsbase64	b64encodeencodedecoder   )r@   r   r5   rA   pathsZmin_orderDateZmax_orderDateZdatav_objectsr7   r
   nameextinfilecontentr   r   berlinrd   Z
fileHeaderZcolumnHeader	file_namerB   rH   Zdatev_objecteZcsv_contentZ
csv_base64r   )r   r   merge_datev_   sl    




P  
(rt   c             C   sZ   | j jd}tjd|  tjjtjjt}d||f }d||f }t	||d t
dS )zu
    Django View : endpoint
    Used by E2 x_controller
    :param request: httpRequest
    :rtype: HttpResponse
    rr   zrevert_datev with file_name z%s/../datev_files/%srU   zrevert_datev successfully )r+   r,   r1   r2   r6   r7   r8   r9   r:   rg   r   )r@   rr   rA   
input_fileoutput_filer   r   r   revert_datev4  s    rw   rU   c             C   s   t | dddd}|j }W dQ R X |d| }||d }|ddd	 }t |dddd}|j| |j| W dQ R X tjd|  dS )
u   
    反转CSV数据行（保留表头不变，不修改内容格式）
    :param input_file: 输入CSV路径
    :param output_file: 输出CSV路径
    :param header_lines: 表头行数，默认2行
    rV   rS    )r*   newlineNr   r)   u(   ✅ 处理完成！文件已保存到：)r>   	readlines
writelinesr1   r2   )ru   rv   Zheader_linesrH   linesheaderdataZdata_reversedr   r   r   rg   E  s    
rg   )rU   )__doc__rh   iorW   r6   loggingcsvjsonr   r   rb   django.httpr   django.views.decorators.csrfr   shutilZclazzr   settingr   r   	getLoggerr1   r3   r   rD   rt   rw   rg   r   r   r   r   <module>   s,   

7 V