import sys
f = "pdf_fermia.modules.php"
s = open(f).read()
T = "\t"

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

# rename class / name / description
s = rep("class pdf_fermia extends ModelePDFFactures" if "class pdf_fermia" in s else "class pdf_octopus extends ModelePDFFactures",
        "class pdf_fermia extends ModelePDFFactures", s)
s = rep('$this->name = "octopus";', '$this->name = "fermia";', s)
s = rep("$this->description = $langs->trans('PDFOctopusDescription');",
        "$this->description = 'Modele FERMI A - situation delta + sous-ligne cumul';", s)

# helper method (only fermiLineCumul needed for A)
helper = (
    T + "/**\n"
    + T + " * FERMI A : avancement cumule (%) et montant cumule HT d'une ligne\n"
    + T + " * @param Facture $object\n"
    + T + " * @param int $i\n"
    + T + " * @return array{pct:float,ht:float}\n"
    + T + " */\n"
    + T + "protected function fermiLineCumul($object, $i)\n"
    + T + "{\n"
    + T*2 + "$l = $object->lines[$i];\n"
    + T*2 + "$prev = 0;\n"
    + T*2 + "if (method_exists($l, 'getAllPrevProgress')) {\n"
    + T*3 + "$prev = (float) $l->getAllPrevProgress($object->id);\n"
    + T*2 + "}\n"
    + T*2 + "$pct = $prev + (float) $l->situation_percent;\n"
    + T*2 + "$ht = (float) $l->subprice * (float) $l->qty * $pct / 100;\n"
    + T*2 + "return array('pct' => $pct, 'ht' => $ht);\n"
    + T + "}\n\n"
)
anchor_h = T + "public function getInfosLineLastSituation(&$object, &$current_line)"
s = rep(anchor_h, helper + anchor_h, s)

# widen progress column + keep overtitle span, before defineColumnField hook
colblock = (
    T*2 + "// ===== FERMI A : colonnes =====\n"
    + T*2 + "if (isset($this->cols['progress'])) {\n"
    + T*3 + "$this->cols['progress']['width'] = 15;\n"
    + T*3 + "if (isset($this->cols['progress']['overtitle'])) { $this->cols['progress']['overtitle']['width'] = 15 + 18; }\n"
    + T*2 + "}\n"
    + T*2 + "// ===== FIN FERMI A =====\n\n"
)
anchor_c = T*2 + "$reshook = $hookmanager->executeHooks('defineColumnField', $parameters, $this); // Note that $object may have been modified by hook"
s = rep(anchor_c, colblock + anchor_c, s)

# progress render: append cumul sub-line
old_p = "$progress = pdf_getlineprogress($object, $i, $outputlangs, $hidedetails);"
new_p = (
    "$progress = pdf_getlineprogress($object, $i, $outputlangs, $hidedetails);\n"
    + T*6 + "$fc = $this->fermiLineCumul($object, $i);\n"
    + T*6 + '$progress .= "\\ncumul ".round($fc[\'pct\'], 1)."%";'
)
s = rep(old_p, new_p, s)

# progress_amount render: append cumul montant sub-line
old_a = "$printval = price($object->lines[$i]->total_ht, 0, '', 1, -1, 2);"
new_a = (
    "$printval = price($object->lines[$i]->total_ht, 0, '', 1, -1, 2);\n"
    + T*6 + "$fcamt = $this->fermiLineCumul($object, $i);\n"
    + T*6 + '$printval .= "\\ncumul ".price($fcamt[\'ht\'], 0, \'\', 1, -1, 2);'
)
s = rep(old_a, new_a, s)

open(f, "w").write(s)
print("fermia patch OK")
