firegento / firegento-pdf

Generates nicer and configurable PDF for invoices, creditmemos and shippings in Magento
100 stars 62 forks source link

How to change output of totals block? #376

Open jpschm opened 6 years ago

jpschm commented 6 years ago

Hi there,

I have an issue with my pdf invoices, that you can maybe help me with. I don't know if this has been asked before, at least I didn't find anything.

When I print invoices they have the following totals included: (I'll list them with actual numbers as an example):

Subtotal (excl. tax) - 539,41 Subtotal (incl. tax) - 641,90 Tax (19%) - 102,49 Tax - 102,49 Total - 641,90

I hope I translated those correctly as in my invoice everything is in german. The original values are in the same order as above:

Zwischensumme (zzg. Steuern) Zwischensumme (inkl. Steuern) MwSt (19%) Steuer Gesamtsumme

Now I have been asked to change the output to:

Subtotal (excl. tax) - Zwischensumme (netto) Shipping (excl. tax) - Versandkosten (netto) Total (excl. tax) - Summe (netto) Tax (19%) - MwSt (19%) Total (incl. tax) - Summe (brutto)

How can I address this? Where are these fields in the invoice coming from?

From what I found these are inserted in the insertTotals function in the Abstract.php which I guess I could overwrite in my own engine. Unfortunately my PHP and/or Magento skills aren't good enough to actually understand what is happening in this function, as It seems to me the totals (or which totals to be used) aren't defined there but coming from somewhere else. Can you give me a hint on what may be the best way to change the output into something like above? Many thanks in advance.

Schrank commented 6 years ago

I think the totals are printed here: https://github.com/firegento/firegento-pdf/blob/development/src/app/code/community/FireGento/Pdf/Model/Engine/Abstract.php#L937-L966

The totals are in an array, so you can either just echo them one by one, but then if you add new totals, they won't show up.

I would order them by the key, which is the type of the total, by using uksort. Unfortunately functional programming is not common by php programmes, so this might be outside of your skill :-/

jpschm commented 6 years ago

Thanks for your reply. I didn't try it yet though, but I guess this should do the trick. Sorting the array $total with uksort after: $total->setOrder($order)->setSource($source); ab

Do you maybe have an idea on how I can add new totals to the print out too? I guess I could insert new entrys into the array too but I guess I'd have to create new classes too then because I'd need the canDisplay() setFont() and all other methods that are used there, right?

Maybe that's a bit too much because basically all I need is an additional subtotal like for example:

in Invoice: Total (excl. tax) = Subtotal (excl. tax) - Shipping & Handling (excl. tax) in Creditmemo: Total (incl. tax) = Subtotal (incl. tax) +/- Adjustment Fee/Adjustment Refund

So that there's actually already a Total (excl. tax) before the tax is added. (Or the other way around for creditmemos).

I was thinking about adding an if-block to the foreach ($uniqueTotalsForDisplay as $totalData) loop. https://github.com/firegento/firegento-pdf/blob/development/src/app/code/community/FireGento/Pdf/Model/Engine/Abstract.php#L948-L964 But I guess I Can't use the arry field ids there to identify the total after which I need to add my own one. So I was thinking about modifying the loop something like this:

//Get translation of Label to search for   
$labelToSearchFor = Mage::helper('tax')->__('Grand Total (Incl. Tax)'); 
//Get translation of label to add  
$labelToAdd = Mage::helper('tax')->__('some label');    

foreach ($uniqueTotalsForDisplay as $totalData) {
    $label = $this->fixNumberFormat($totalData['label']);

// First add total to the output array

    $lineBlock['lines'][] = array(
        array(
            'text'      => $label,
            'feed'      => 470,
            'align'     => 'right',
            'font_size' => $totalData['font_size']
        ),
        array(
            'text'      => $totalData['amount'],
            'feed'      => 540,
            'align'     => 'right',
            'font_size' => $totalData['font_size']
        ),
     );

// If total matches the searched one add own total afterwards

 if ($label == $labelToSearchFor){
    //add own total to the array
 }
}

I didn't test this yet it's just an idea. Would something like this work? Or is there a better or even easier way maybe?