import os
import re

report_path = r'c:\fuentes\proyectos delphi\RAUL_ASENCIO\GESTION\reportes\RecetaDetalle.fr3'
backup_path = r'c:\fuentes\proyectos delphi\RAUL_ASENCIO\GESTION\reportes\RecetaDetalle.fr3.bak'

# Leer el archivo original para extraer el PropData del logo
with open(report_path, 'r', encoding='utf-8') as f:
    orig_content = f.read()

# Crear backup
with open(backup_path, 'w', encoding='utf-8') as f:
    f.write(orig_content)

m = re.search(r'Picture\.PropData="([^"]+)"', orig_content)
if not m:
    raise Exception("No se encontró Picture.PropData en el reporte original.")
logo_prop_data = m.group(1)

xml_content = f"""<?xml version='1.0' encoding='utf-8'?>
<TfrxReport Version="2022.2.7" DotMatrixReport="False" IniFile="\\Software\\Fast Reports" PreviewOptions.Buttons="167935" PreviewOptions.Zoom="1" PrintOptions.Printer="Default" PrintOptions.PrintOnSheet="0" ReportOptions.CreateDate="46178,5184803472" ReportOptions.Description.Text="" ReportOptions.LastChange="46178,5397437616" ScriptLanguage="PascalScript" ScriptText.Text="procedure DetailIngredientesOnBeforePrint(Sender: TfrxComponent);&#13;&#10;begin&#13;&#10;  DetailIngredientes.Visible := (&lt;frxDBDetalle.&quot;ingrediente_codigo&quot;&gt; &lt;&gt; Null) and (Trim(VarToStr(&lt;frxDBDetalle.&quot;ingrediente_codigo&quot;&gt;)) &lt;&gt; '');&#13;&#10;end;&#13;&#10;&#13;&#10;begin&#13;&#10;&#13;&#10;end.">
  <Datasets>
    <item DataSet="frxDBMaster" DataSetName="frxDBmaster" />
    <item DataSet="frxDBDetalle" DataSetName="frxDBDetalle" />
  </Datasets>
  <TfrxDataPage Name="Data" HGuides.Text="" VGuides.Text="" Height="1000" Left="0" Top="0" Width="1000" />
  <TfrxReportPage Name="Page1" HGuides.Text="" VGuides.Text="" PaperWidth="210" PaperHeight="297" PaperSize="9" LeftMargin="15" RightMargin="15" TopMargin="15" BottomMargin="15" ColumnWidth="0" ColumnPositions.Text="" Frame.Typ="0" MirrorMode="0">
    <TfrxGroupHeader Name="GroupHeaderReceta" FillType="ftBrush" FillGap.Top="0" FillGap.Left="0" FillGap.Bottom="0" FillGap.Right="0" Frame.Typ="0" Height="125" Left="0" Top="18,89765" Width="680,3154" Condition="frxDBDetalle.&quot;receta_id&quot;" StartNewPage="True" KeepTogether="True">
      <TfrxPictureView Name="Picture1" AllowVectorExport="True" Left="464,88219" Top="0" Width="207,87415" Height="68" Frame.Typ="0" Picture.PropData="{logo_prop_data}" HightQuality="False" Transparent="False" TransparentColor="16777215" />
      <TfrxMemoView Name="TitleMemo" AllowVectorExport="True" Left="0" Top="10" Width="450" Height="28" Font.Charset="1" Font.Color="8388608" Font.Height="-22" Font.Name="Arial" Font.Style="1" Frame.Typ="0" HAlign="haLeft" ParentFont="False" VAlign="vaCenter" Text="FICHA TÉCNICA DE ELABORACIÓN" />
      <TfrxMemoView Name="RecetaSubTitle" AllowVectorExport="True" Left="0" Top="38" Width="450" Height="18" Font.Charset="1" Font.Color="5263440" Font.Height="-12" Font.Name="Arial" Font.Style="2" Frame.Typ="0" HAlign="haLeft" ParentFont="False" Text="Fórmula e instrucciones de elaboración por procesos" />
      <TfrxLineView Name="TitleLine" AllowVectorExport="True" Left="0" Top="62" Width="680" Height="0" Color="8388608" Frame.Typ="4" Frame.Width="2" />
      <TfrxMemoView Name="LblReceta" AllowVectorExport="True" Left="0" Top="72" Width="70" Height="24" Font.Charset="1" Font.Color="0" Font.Height="-14" Font.Name="Arial" Font.Style="1" Frame.Typ="0" ParentFont="False" VAlign="vaCenter" Text="Receta:" />
      <TfrxMemoView Name="ValReceta" AllowVectorExport="True" Left="75" Top="70" Width="420" Height="26" Font.Charset="1" Font.Color="8388608" Font.Height="-16" Font.Name="Arial" Font.Style="1" Frame.Typ="0" ParentFont="False" VAlign="vaCenter" Text="[frxDBDetalle.&quot;receta_descripcion&quot;]" />
      <TfrxMemoView Name="LblCode" AllowVectorExport="True" Left="505" Top="72" Width="65" Height="20" Font.Charset="1" Font.Color="0" Font.Height="-12" Font.Name="Arial" Font.Style="1" Frame.Typ="0" HAlign="haRight" ParentFont="False" VAlign="vaCenter" Text="Código:" />
      <TfrxMemoView Name="ValCode" AllowVectorExport="True" Left="575" Top="72" Width="105" Height="20" Font.Charset="1" Font.Color="0" Font.Height="-12" Font.Name="Arial" Font.Style="0" Frame.Typ="0" ParentFont="False" VAlign="vaCenter" Text="[frxDBDetalle.&quot;receta_codigo&quot;]" />
      <TfrxMemoView Name="LblVer" AllowVectorExport="True" Left="505" Top="95" Width="65" Height="18" Font.Charset="1" Font.Color="0" Font.Height="-11" Font.Name="Arial" Font.Style="1" Frame.Typ="0" HAlign="haRight" ParentFont="False" VAlign="vaCenter" Text="Versión:" />
      <TfrxMemoView Name="ValVer" AllowVectorExport="True" Left="575" Top="95" Width="105" Height="18" Font.Charset="1" Font.Color="0" Font.Height="-11" Font.Name="Arial" Font.Style="0" Frame.Typ="0" ParentFont="False" VAlign="vaCenter" Text="[frxDBDetalle.&quot;receta_version&quot;]" />
      <TfrxLineView Name="RecetaDivLine" AllowVectorExport="True" Left="0" Top="118" Width="680" Height="0" Color="12632256" Frame.Typ="4" />
    </TfrxGroupHeader>
    <TfrxGroupHeader Name="GroupHeaderProceso" FillType="ftBrush" FillGap.Top="0" FillGap.Left="0" FillGap.Bottom="0" FillGap.Right="0" Frame.Typ="0" Height="56" Left="0" Top="150" Width="680,3154" Condition="frxDBDetalle.&quot;proceso_key&quot;" KeepTogether="True">
      <TfrxMemoView Name="ProcBox" AllowVectorExport="True" Left="0" Top="4" Width="680" Height="24" Color="16119285" Frame.Color="12632256" Frame.Typ="15" ParentFont="False" Text="" />
      <TfrxMemoView Name="ProcTitle" AllowVectorExport="True" Left="10" Top="5" Width="380" Height="22" Font.Charset="1" Font.Color="1202458" Font.Height="-13" Font.Name="Arial" Font.Style="1" Frame.Typ="0" ParentFont="False" VAlign="vaCenter" Text="FASE [frxDBDetalle.&quot;proceso_orden&quot;]: [frxDBDetalle.&quot;proceso_nombre&quot;]" />
      <TfrxMemoView Name="ProcParams" AllowVectorExport="True" Left="395" Top="5" Width="275" Height="22" Font.Charset="1" Font.Color="5263440" Font.Height="-11" Font.Name="Arial" Font.Style="2" Frame.Typ="0" HAlign="haRight" ParentFont="False" VAlign="vaCenter" Text="[IIF(&#60;frxDBDetalle.&#34;duracion_minutos&#34;&#62; &#62; 0, '⏱ ' + VarToStr(&#60;frxDBDetalle.&#34;duracion_minutos&#34;&#62;) + ' min   ', '')][IIF(&#60;frxDBDetalle.&#34;temperatura&#34;&#62; &#62; 0, '🌡 ' + VarToStr(&#60;frxDBDetalle.&#34;temperatura&#34;&#62;) + ' °C   ', '')][IIF(&#60;frxDBDetalle.&#34;ph&#34;&#62; &#62; 0, 'pH ' + VarToStr(&#60;frxDBDetalle.&#34;ph&#34;&#62;), '')]" />
      <TfrxMemoView Name="HColIngCode" AllowVectorExport="True" Left="20" Top="32" Width="100" Height="22" Font.Charset="1" Font.Color="0" Font.Height="-11" Font.Name="Arial" Font.Style="1" Frame.Color="12632256" Frame.Typ="15" Fill.BackColor="14737632" ParentFont="False" VAlign="vaCenter" Text=" Código" />
      <TfrxMemoView Name="HColIngDesc" AllowVectorExport="True" Left="120" Top="32" Width="370" Height="22" Font.Charset="1" Font.Color="0" Font.Height="-11" Font.Name="Arial" Font.Style="1" Frame.Color="12632256" Frame.Typ="15" Fill.BackColor="14737632" ParentFont="False" VAlign="vaCenter" Text=" Descripción del Ingrediente" />
      <TfrxMemoView Name="HColIngCant" AllowVectorExport="True" Left="490" Top="32" Width="110" Height="22" Font.Charset="1" Font.Color="0" Font.Height="-11" Font.Name="Arial" Font.Style="1" Frame.Color="12632256" Frame.Typ="15" Fill.BackColor="14737632" HAlign="haRight" ParentFont="False" VAlign="vaCenter" Text="Cantidad " />
      <TfrxMemoView Name="HColIngUnidad" AllowVectorExport="True" Left="600" Top="32" Width="80" Height="22" Font.Charset="1" Font.Color="0" Font.Height="-11" Font.Name="Arial" Font.Style="1" Frame.Color="12632256" Frame.Typ="15" Fill.BackColor="14737632" HAlign="haCenter" ParentFont="False" VAlign="vaCenter" Text="Unidad" />
    </TfrxGroupHeader>
    <TfrxMasterData Name="DetailIngredientes" FillType="ftBrush" FillGap.Top="0" FillGap.Left="0" FillGap.Bottom="0" FillGap.Right="0" Frame.Typ="0" Height="20" Left="0" Top="210" Width="680,3154" ColumnWidth="0" ColumnGap="0" DataSet="frxDBDetalle" DataSetName="frxDBDetalle" RowCount="0" OnBeforePrint="DetailIngredientesOnBeforePrint">
      <TfrxMemoView Name="IngCode" AllowVectorExport="True" Left="20" Top="0" Width="100" Height="20" Font.Charset="1" Font.Color="5263440" Font.Height="-11" Font.Name="Arial" Font.Style="0" Frame.Color="14474460" Frame.Typ="15" ParentFont="False" VAlign="vaCenter" Text="[frxDBDetalle.&quot;ingrediente_codigo&quot;]" />
      <TfrxMemoView Name="IngDesc" AllowVectorExport="True" Left="120" Top="0" Width="370" Height="20" Font.Charset="1" Font.Color="0" Font.Height="-11" Font.Name="Arial" Font.Style="0" Frame.Color="14474460" Frame.Typ="15" ParentFont="False" VAlign="vaCenter" Text="[frxDBDetalle.&quot;ingrediente_descripcion&quot;]" />
      <TfrxMemoView Name="IngCant" AllowVectorExport="True" Left="490" Top="0" Width="110" Height="20" Font.Charset="1" Font.Color="0" Font.Height="-11" Font.Name="Arial" Font.Style="0" Frame.Color="14474460" Frame.Typ="15" HAlign="haRight" ParentFont="False" VAlign="vaCenter" DisplayFormat.FormatStr="%2.1f" DisplayFormat.Kind="fkNumeric" Text="[frxDBDetalle.&quot;cantidad_base&quot;] " />
      <TfrxMemoView Name="IngUnidad" AllowVectorExport="True" Left="600" Top="0" Width="80" Height="20" Font.Charset="1" Font.Color="0" Font.Height="-11" Font.Name="Arial" Font.Style="0" Frame.Color="14474460" Frame.Typ="15" HAlign="haCenter" ParentFont="False" VAlign="vaCenter" Text="[frxDBDetalle.&quot;unidad&quot;]" />
    </TfrxMasterData>
    <TfrxGroupFooter Name="GroupFooterProceso" FillType="ftBrush" FillGap.Top="0" FillGap.Left="0" FillGap.Bottom="0" FillGap.Right="0" Frame.Typ="0" Height="26" Left="0" Top="248" Width="680,3154">
      <TfrxMemoView Name="SubtotalProcLabel" AllowVectorExport="True" Left="120" Top="3" Width="370" Height="18" Font.Charset="1" Font.Color="1202458" Font.Height="-10" Font.Name="Arial" Font.Style="1" Frame.Typ="0" HAlign="haRight" ParentFont="False" VAlign="vaCenter" Text="Subtotal Fase [frxDBDetalle.&quot;proceso_orden&quot;] ([frxDBDetalle.&quot;proceso_nombre&quot;]):" />
      <TfrxMemoView Name="SubtotalProcVal" AllowVectorExport="True" Left="490" Top="3" Width="110" Height="18" Font.Charset="1" Font.Color="1202458" Font.Height="-10" Font.Name="Arial" Font.Style="1" Frame.Color="12632256" Frame.Typ="8" HAlign="haRight" ParentFont="False" VAlign="vaCenter" DisplayFormat.FormatStr="%2.1f" DisplayFormat.Kind="fkNumeric" Text="[SUM(&#60;frxDBDetalle.&#34;cantidad_base&#34;&#62;,DetailIngredientes)] " />
      <TfrxMemoView Name="SubtotalProcKg" AllowVectorExport="True" Left="600" Top="3" Width="80" Height="18" Font.Charset="1" Font.Color="5263440" Font.Height="-9" Font.Name="Arial" Font.Style="0" Frame.Typ="0" HAlign="haCenter" ParentFont="False" VAlign="vaCenter" Text="[frxDBDetalle.&quot;unidad&quot;]" />
      <TfrxLineView Name="GroupFooterLine" AllowVectorExport="True" Left="20" Top="22" Width="660" Height="0" Color="14474460" Frame.Style="fsDot" Frame.Typ="4" />
    </TfrxGroupFooter>
    <TfrxGroupFooter Name="GroupFooterReceta" FillType="ftBrush" FillGap.Top="0" FillGap.Left="0" FillGap.Bottom="0" FillGap.Right="0" Frame.Typ="0" Height="36" Left="0" Top="280" Width="680,3154">
      <TfrxMemoView Name="TotalRecetaBox" AllowVectorExport="True" Left="0" Top="4" Width="680" Height="26" Color="15792370" Frame.Color="8388608" Frame.Typ="15" Frame.Width="1" ParentFont="False" Text="" />
      <TfrxMemoView Name="TotalRecetaLabel" AllowVectorExport="True" Left="10" Top="6" Width="470" Height="22" Font.Charset="1" Font.Color="8388608" Font.Height="-11" Font.Name="Arial" Font.Style="1" Frame.Typ="0" HAlign="haRight" ParentFont="False" VAlign="vaCenter" Text="TOTAL RECETA ([frxDBDetalle.&quot;receta_descripcion&quot;]):" />
      <TfrxMemoView Name="TotalRecetaVal" AllowVectorExport="True" Left="485" Top="6" Width="115" Height="22" Font.Charset="1" Font.Color="8388608" Font.Height="-11" Font.Name="Arial" Font.Style="1" Frame.Typ="0" HAlign="haRight" ParentFont="False" VAlign="vaCenter" DisplayFormat.FormatStr="%2.1f" DisplayFormat.Kind="fkNumeric" Text="[SUM(&#60;frxDBDetalle.&#34;cantidad_base&#34;&#62;,DetailIngredientes)] " />
      <TfrxMemoView Name="TotalRecetaKg" AllowVectorExport="True" Left="600" Top="6" Width="75" Height="22" Font.Charset="1" Font.Color="8388608" Font.Height="-10" Font.Name="Arial" Font.Style="1" Frame.Typ="0" HAlign="haLeft" ParentFont="False" VAlign="vaCenter" Text="g  ([FormatFloat('#,##0.000', SUM(&#60;frxDBDetalle.&#34;cantidad_base&#34;&#62;,DetailIngredientes) / 1000)] Kg)" />
    </TfrxGroupFooter>
    <TfrxPageFooter Name="PageFooter1" FillType="ftBrush" FillGap.Top="0" FillGap.Left="0" FillGap.Bottom="0" FillGap.Right="0" Frame.Typ="0" Height="30" Left="0" Top="330" Width="680,3154">
      <TfrxLineView Name="FooterLine" AllowVectorExport="True" Left="0" Top="5" Width="680" Height="0" Color="12632256" Frame.Typ="4" />
      <TfrxMemoView Name="FooterDate" AllowVectorExport="True" Left="0" Top="10" Width="300" Height="15" Font.Charset="1" Font.Color="8421504" Font.Height="-11" Font.Name="Arial" Font.Style="0" Frame.Typ="0" ParentFont="False" Text="Fecha Impresión: [Date]" />
      <TfrxMemoView Name="PageNum" AllowVectorExport="True" Left="540" Top="10" Width="140" Height="15" Font.Charset="1" Font.Color="8421504" Font.Height="-11" Font.Name="Arial" Font.Style="0" Frame.Typ="0" HAlign="haRight" ParentFont="False" Text="Pág. [Page#] de [TotalPages#]" />
    </TfrxPageFooter>
  </TfrxReportPage>
</TfrxReport>
"""

with open(report_path, 'w', encoding='utf-8') as f:
    f.write(xml_content)

print(f"Reporte generado exitosamente en: {report_path}")
