<%@language = "javascript" %>
<% /*
    $Revision$
    $Id$

    File:           cnt_factuurschema

    Description:    Aanroep voor model_cnt_factuurschema

    Context:        Zal altijd aangeroepen worden met (include) model=schedule

    Notes:          cnt_factuurschema is een include van cnt_contract
                    Daarom doen we hier ook model cnt_contract en zorgen voor
                    een 'include-aanroep'
*/
%>
<!-- #include file="../scf/scaffolding.inc" -->
<!-- #include file="../api2/api2_tools.inc" -->
<!-- #include file="../api2/model_contracts.inc" -->
<%
var this_model = new model_contracts();

var sqltotal = "SELECT SUM (fin_factuur_totaal)"
             + "  FROM fin_factuur f"
             + " WHERE f.fin_factuur_boekmaand  = cnt_factuurschema.cnt_factuurschema_boekmaand"
             + "   AND f.cnt_contract_key = cnt_factuurschema.cnt_contract_key"
             + "   AND f.fin_factuur_statuses_key <> 1" // afgewezen telt sowieso niet mee
             + "   AND f.fin_factuur_verwijder IS NULL"

this_model.includes.schedule.model.fields["fintotal"] =
    { dbs: "fintotal",
      label: L("lcl_mld_invoiced"),
      sql: "(" + sqltotal + ")",
      total: true,
      iscurrency: true,
      typ: 'float'
    }

if (mode == "edit" && getQParamInt("id", -1) < 0)
{
    var cnt_key = getQParamInt("cnt_contract_key");
    var sql = "SELECT 'x'"
            + " FROM cnt_factuurschema"
            + " WHERE cnt_contract_key =" + cnt_key ;
    var oRs = Oracle.Execute( sql );
    var hasFactuurschema = !oRs.EOF;
    oRs.close();

    var sql = "SELECT cnt_contract_kosten"
            + "     , 1+TRUNC(MONTHS_BETWEEN(cnt_contract_looptijd_tot, cnt_contract_looptijd_van)) maanden"
            + "     , cnt_contract_looptijd_van"
            + "     , cnt_contract_looptijd_tot"
            + "     , cnt_contract_kosten"
            + "  FROM cnt_contract"
            + " WHERE cnt_contract_key = " + cnt_key;
    var oRs = Oracle.Execute(sql);
    var kosten = oRs("cnt_contract_kosten").Value;
    var maanden = oRs("maanden").Value;
    var start  = oRs("cnt_contract_looptijd_van").Value;
    oRs.Close()
    var maandbedrag = (kosten/maanden).toFixed(2);

    this_model.includes.schedule.model.fields["amount"].defaultvalue = maandbedrag;

    if (!hasFactuurschema) // Genereer knop erbij
    {
        this_model.includes.schedule.model.fields["tekst"] =
        {
            label: L("lcl_cnt_genschema_hint").format(maanden),
            typ: "label"
        }
        this_model.includes.schedule.model.fields["generate"] =
            {
              label: L("lcl_cnt_genschema"),
              typ: 'check0'
            }
    }
    this_model.includes.schedule.model["edit"] = {
           "requires": {
                 js: ["./cnt_factuurschema.js"]
               }
           }

    // Listbox met de factuurperioden toevoegen.
    var maandperioden = [];
    for (maand = 0; maand < maanden; maand++)
    {
        var periode = addMonth(start, maand);
        maandperioden.push(periode);
        maandperioden.push(periode);
    }
    this_model.includes.schedule.model.fields.accountingperiod.LOV = maandperioden.join(";");
}

function addMonth(datum,n)
{
    var d = new Date(datum);
    var m = d.getMonth()+n;

    d.setMonth(m);
    var maand = d.getMonth() + 1
    var datestring = d.getFullYear() + "-" + padout(maand);
    return datestring;
}


var org_save = scaffolding_save;
scaffolding_save = function(model, scf_params)
{
    if (getFParam("generate", "off") == "on")
    {
        protectRequest.validateToken();
        var cnt_key = getQParamInt( "cnt_contract_key" );
        var amount = getFParamFloat("amount");
        var description = getFParam("description");
        var sql = "BEGIN"
                + "  cnt.generatefactuurschema({0}, {1});".format(cnt_key, amount)
                + "END;";
        var err = Oracle.Execute(sql, true);
        if (err.friendlyMsg)
            abort_with_warning(err.friendlyMsg);
        var result = { success: true };
        Response.ContentType = "application/json";
        Response.Write(JSON.stringify(result));
    }
    else
        org_save(model, scf_params);
}

scaffolding(this_model,
    { "included": {
        "schedule" : {
            "list": {
                "columns": [
                    "accountingperiod",
                    "amount",
                    "description",
                    "fintotal"
                ],
                "orderby": [,
                    "cnt_contract_key",
                    "accountingperiod"
                 ]
            },
            "edit": {
                "modal": true,
                "requires": {
                  js: ["./cnt_factuurschema.js"]
                }
            }
        }
      }
    });
%>
<% ASPPAGE_END(); %> 
