import sys
f = "/homez.786/ridinteadu/ridindev/dolibarr/htdocs/custom/fermipdf/core/modules/facture/doc/pdf_fermi.modules.php"
s = open(f).read()
T = "\t"

def rep(old, new, s):
    if s.count(old) != 1:
        print("ANCHOR PROBLEM:", repr(old[:55]), "count=", s.count(old)); sys.exit(1)
    return s.replace(old, new, 1)

# 1) method fermiDrawAffaireBanner
method = (
    T + "/**\n"
    + T + " * FERMI : bandeau charcoal 'Affaire n° {projet} : {titre}' + 'Marche n° {ref_client}'\n"
    + T + " * @param TCPDF $pdf\n"
    + T + " * @param Facture $object\n"
    + T + " * @param Translate $outputlangs\n"
    + T + " * @return void\n"
    + T + " */\n"
    + T + "protected function fermiDrawAffaireBanner(&$pdf, $object, $outputlangs)\n"
    + T + "{\n"
    + T*2 + "$x = $this->marge_gauche;\n"
    + T*2 + "$w = $this->page_largeur - $this->marge_gauche - $this->marge_droite;\n"
    + T*2 + "$ymark = $this->tab_top_newpage - 11;\n"
    + T*2 + "$affaire = '';\n"
    + T*2 + "if (!empty($object->fk_project)) {\n"
    + T*3 + "$object->fetchProject();\n"
    + T*3 + "if (!empty($object->project)) {\n"
    + T*4 + "$affaire = $object->project->ref;\n"
    + T*4 + "if (!empty($object->project->title)) { $affaire .= ' : '.$object->project->title; }\n"
    + T*3 + "}\n"
    + T*2 + "}\n"
    + T*2 + "// bandeau charcoal, texte blanc\n"
    + T*2 + "$pdf->RoundedRect($x, $ymark, $w, 5, $this->corner_radius, '1111', 'F', array(), array(65, 65, 65));\n"
    + T*2 + "$pdf->SetTextColor(255, 255, 255);\n"
    + T*2 + "$pdf->SetFont('', 'B', 9);\n"
    + T*2 + "$pdf->SetXY($x + 1.5, $ymark + 0.5);\n"
    + T*2 + "$pdf->MultiCell($w - 3, 4, $outputlangs->convToOutputCharset('Affaire n\\xc2\\xb0 '.$affaire), 0, 'L', false);\n"
    + T*2 + "// ligne marche (sous le bandeau)\n"
    + T*2 + "$marche = (string) $object->ref_client;\n"
    + T*2 + "$datestart = '';\n"
    + T*2 + "if (!empty($object->project) && !empty($object->project->date_start)) {\n"
    + T*3 + "$datestart = ' du '.dol_print_date($object->project->date_start, 'day', false, $outputlangs);\n"
    + T*2 + "}\n"
    + T*2 + "$pdf->SetTextColor(80, 80, 80);\n"
    + T*2 + "$pdf->SetFont('', '', 8);\n"
    + T*2 + "$pdf->SetXY($x + 1.5, $ymark + 5.4);\n"
    + T*2 + "$pdf->MultiCell($w - 3, 4, $outputlangs->convToOutputCharset('March\\xc3\\xa9 n\\xc2\\xb0 '.$marche.$datestart), 0, 'L', false);\n"
    + T*2 + "$pdf->SetTextColor(0, 0, 0);\n"
    + T + "}\n\n"
)
anchor_m = T + "public function getInfosLineLastSituation(&$object, &$current_line)"
s = rep(anchor_m, method + anchor_m, s)

# 2) reserve room on detail pages for the banner
anchor_tt = "$this->tab_top_newpage = (!getDolGlobalInt('MAIN_PDF_DONOTREPEAT_HEAD') ? 42 + $top_shift : 10);"
s = rep(anchor_tt, anchor_tt + "\n" + T*4 + "$this->tab_top_newpage += 13; // FERMI : place pour le bandeau Affaire/Marche", s)

# 3) draw banner on the detail page (after page-2 pagehead)
anchor_b = (
    T*4 + "$pdf->setPage(2);\n"
    + T*4 + "$pagenb++;\n"
    + T*4 + "$this->_pagehead($pdf, $object, 0, $outputlangs, $outputlangsbis);"
)
s = rep(anchor_b, anchor_b + "\n" + T*4 + "$this->fermiDrawAffaireBanner($pdf, $object, $outputlangs);", s)

open(f, "w").write(s)
print("FERMI style banner patch OK")
