dvoip
dvoip

Reputation: 43

CakePHP calculation not correct

I have a small script that generates quotes. I can add items with related discount or tax values, however the calculation does not seem to be correct as it's not subtracting the discount value. Based upon the screenshot I will attach the item subtotal should amount to 155,584.00

Please refer to my script below as well as a screenshot

image

    foreach ($items as $item) {
        $itemSubTotal = $item['quantity'] * $item['unit_price'];
        $discount_rate=$item['discount_rate'];
        $unit_price=$item['unit_price'];
        $subTotal += $itemSubTotal;
        $itemDiscount=$itemSubTotal*$discount_rate/100;
        $discount+=$itemDiscount;
        $itemTax = $itemSubTotal * ($item['tax_rate'] / 100);
        $tax += $itemTax;
        $itemSubTotal = number_format($itemSubTotal, 2, '.', ',');
        $y+=5;
        $pdf->setXY(5, $y);
        $pdf->MultiCell(10, 5, $i++, 0, 'L');
        $pdf->setXY(15, $y);
        $pdf->Cell(30, 5, $item['title'], 0, 2, 'L');
        $pdf->setXY(45, $y);
        $pdf->Cell(30, 5, $item['details'], 0, 2, 'L');
        $pdf->setXY(125, $y);
        $pdf->MultiCell(20, 5, $item['quantity'], 0, 'R');
        $pdf->setXY(145, $y);
        $pdf->MultiCell(15, 5, number_format($unit_price, 2, '.', ','), 0, 'R');
        $pdf->setXY(160, $y);
        $pdf->MultiCell(20, 5, number_format($discount_rate, 2, '.', ','), 0, 'R');
        $pdf->setXY(180, $y);
        $pdf->MultiCell(25, 5, $itemSubTotal, 0, 'R');

How can I get this fixed? Some expert advise would be greatly appreciated

Upvotes: 1

Views: 63

Answers (1)

GGG
GGG

Reputation: 670

You haven't modified the $itemSubTotal variable with the discount. you should do this before printing it:

$itemSubTotal = $itemSubTotal*($discount_rate/100);

also, according to my calculation, the item subtotal would be 83,776.00

Upvotes: 2

Related Questions