function (doc) {
    // !code util/mvp.js
    if(isChildVisitForm(doc)) {
        var indicators = get_indicators(doc);

        if (indicators.child_dob && indicators.child_dob.value &&
            indicators.last_muac && indicators.last_muac.value) {
            var meta = doc.form.meta,
                last_muac_indicators = {},
                visit_indicators = {},
                age = get_age_from_dob(indicators.child_dob.value, meta.timeEnd),
                last_muac_date = new Date(indicators.last_muac.value),
                visit_date = new Date(meta.timeEnd),
                case_id = get_case_id(doc);

            if (age >= 180*MS_IN_DAY && age < 1825*MS_IN_DAY) {
                if (indicators.cur_muac && indicators.cur_muac.value) {
                    try {
                        var cur_muac = parseFloat(indicators.cur_muac.value);
                        last_muac_indicators["muac_reading"] = case_id;
                        if ((cur_muac < 125.0 && cur_muac > 49) || (cur_muac <12.5)) {
                            last_muac_indicators["muac_wasting"] = case_id;
                        }
                    } catch (e) {
                        // do nothing
                    }
                }
                if (visit_date >= last_muac_date
                    && indicators.muac && indicators.muac.value) {
                    var visit_diff = visit_date.getTime() - last_muac_date.getTime();
                    if (visit_diff < 30*MS_IN_DAY) {
                        visit_indicators["routine_muac"] = case_id;
                    }
                }
            }

            if (age < 1825*MS_IN_DAY && indicators.cur_muac && indicators.cur_muac.value) {
                try {
                    var cur_muac_under5 = parseFloat(indicators.cur_muac.value);
                    visit_indicators["active_gam"] = {
                        "_id": case_id,
                        "value": (cur_muac_under5 < 125.0 && cur_muac_under5 > 49.0) ? 1 : 0 || (cur_muac_under5 < 12.5) ? 1 : 0
                    }
                } catch (e) {
                    // do nothing
                }
            }

            emit_special(doc, last_muac_date, last_muac_indicators, [doc._id]);
            emit_special(doc, visit_date, visit_indicators, [doc._id]);
        }
    }
}

@@ -609,32 +609,177 @@
 _case_id(doc);%0A%0A
+            if (visit_date %3E= last_muac_date) %7B%0A                var visit_diff = visit_date.getTime() - last_muac_date.getTime();%0A            %7D%0A%0A
             if (
@@ -1399,214 +1399,52 @@
 if (
-visit_date %3E= last_muac_date%0A                    && indicators.muac && indicators.muac.value) %7B%0A                    var visit_diff = visit_date.getTime() - last_muac_date.getTime();%0A                    if (
+indicators.muac && indicators.muac.value && 
 visi
@@ -1460,36 +1460,32 @@
 30*MS_IN_DAY) %7B%0A
-    
                 
@@ -1532,38 +1532,16 @@
 ase_id;%0A
-                    %7D%0A
         
@@ -1581,24 +1581,48 @@
     if (age 
+%3E= 180*MS_IN_DAY && age 
 %3C 1825*MS_IN
@@ -1640,32 +1640,48 @@
 icators.cur_muac
+%0A               
  && indicators.c
@@ -1685,32 +1685,61 @@
 s.cur_muac.value
+ && visit_diff %3C 30*MS_IN_DAY
 ) %7B%0A            

Fix GAM