This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

apatiu27112
apatiu27112

Hi everyone. I have this:


lines:
    label: lines
    prompt: 'add'
    type: repeater
    form:
        fields:
            detail:
                label: Product
                type: text
            qty:
                label: Qty
                type: number
            price:
                label: price
                type: number
            total:
                label: total
                default: 0
                type: number
                dependsOn:
                    - qty
                    - price
bill_total:
    label: grand_total
    dependsOn:
        - lines
    type: number

bill_total is dependsOn lines. And lines[total] is dependsOn 'qty' & 'price' my problem is when lines[qty] changed. it's call filterField method on bill_total before lines[total] field, that make bill_total is wrong..

like when

  1. input qty = 1 ===> qty=1, price=0, total=0, bill_total=0
  2. input price = 5. ===> qty=1, price=5, total=5, bill_total=0. (actually bill_total should be 5 but not happen)
  3. input qty = 2. ===> qty=2, price=5, total=10, bill_total=5. (actually bill_total should be 10 but it get previous value)
mjauvin
mjauvin

what's your filterFields method?

apatiu27112
apatiu27112

this is filterFields

public function filterFields($fields, $context = null) {
    if (isset($fields->bill_total)) {
        $bill_total = 0;
        if ($fields->lines->value) {
           foreach ($fields->{'lines[sell]'}->value as $line) {
                $bill_total += $line['total'];
            }
            $fields->bill_total->value = $bill_total;
        }
    }

    if (isset($fields->total)) {
       $fields->total->value = $fields->qty->value * $fields->price->value;
    }
}

thanks for you help :)

mjauvin
mjauvin

What if you move the

if (isset($fields->total))

block at the top of the filterFields method?

apatiu27112
apatiu27112

Nothing change. It's same result

 public function filterFields($fields, $context = null) {

    if (isset($fields->total)) {
        $fields->total->value = $fields->qty->value * $fields->price->value;
    }

    if (isset($fields->bill_total)) {
        $bill_total = 0;
        if ($fields->lines->value) {
            foreach ($fields->lines->value as $line) {
                $bill_total += $line['total'];
            }
            $fields->bill_total->value = $bill_total;
        }
    }

}
apatiu27112
apatiu27112

I'm solved it now by calculate each total like this.

 if (isset($fields->total)) {
        $fields->total->value = $fields->qty->value * $fields->price->value;
    }

    if (isset($fields->bill_total)) {
        $bill_total = 0;
        if ($fields->lines->value) {
            foreach ($fields->lines->value as $line) {
                $bill_total += $line['qty'] * $line['price'];
            }
            $fields->bill_total->value = $bill_total;
        }
    }

1-6 of 6

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.