Files
Facilitor/APPL/API2/model_invoices.inc
Jos Groot Lipman 9e11332d82 Versie 5.4.3 Gold A patches
svn path=/Website/trunk/; revision=23291
2014-11-10 13:25:20 +00:00

201 lines
11 KiB
PHP

<% /*
$Revision$
$Id$
File: model_invoices.inc
Description: Factuur model. Dit bestand heeft niets met interfacing te maken
maar werkt uitsluitend op JSON-data
Parameters:
Context:
Notes: TODO: definitieve naamgeving verifieren
TODO: autorisaties voor scope=fo|bo
We had a discussion. The references (order, contract, purchaseorder) are now present by their foreign nodes.
We ourselves want the creditor of the invoice in the result, it is logically a property of an invoice
which we technically do not store with the invoice. Formally the creditor should be obtained by a subsequent
call to the corresponing API. Alternatively we could add those 3 optional nodes with an include option
which is merely designed for 1:n relations, not n:1 like this one.
We do not want to slide down from our purest REST forms (yet) so we made the compromise of these 2 readonly fields.
Let's see how it holds.
*/
%>
<!-- #include file="../Shared/discxalg3d.inc"-->
<!-- #include file="../fin/fin.inc" -->
<!-- #include file="./model_invoicelines.inc"-->
<%
model_invoices =
{
module: "FIN",
table: "fin_factuur",
primary: "fin_factuur_key",
records_name: "invoices",
record_name: "invoice",
// Het interne factuurnr is de id, het externe nummer is name
fields: [{ name: "id", dbs: "fin_factuur_key", typ: "key", filter: "exact" },
{ name: "invoicedate", dbs: "fin_factuur_datum", typ: "datetime", track: L("lcl_fin_findate"), filter: "range" },
/*readonly*/ { name: "creditor", dbs: "prs_bedrijf_key", typ: "key", foreign: "prs_bedrijf", track: L("lcl_ord_company_uit"), filter: "exact",
sql: "COALESCE(mld_opdr.mld_uitvoerende_keys, bes_bestelopdr.prs_bedrijf_key, cnt_contract.cnt_prs_bedrijf_key)" },
/*readonly*/ { name: "referencetype", dbs: "dummy", typ: "varchar", filter: "exact",
sql: "DECODE(fin_factuur.mld_opdr_key, NULL, DECODE(fin_factuur.bes_bestelopdr_key, NULL, DECODE(fin_factuur.cnt_contract_key, NULL, '?', 'C'), 'B'), 'O')" },
{ name: "name", dbs: "fin_factuur_nr", typ: "varchar", track: L("lcl_fin_invoice_nr_extern"), filter: "exact" },
{ name: "order", dbs: "mld_opdr_key", typ: "key", foreign: "mld_opdr", filter: "exact"},
{ name: "contract", dbs: "cnt_contract_key", typ: "key", foreign: "cnt_contract", filter: "exact"},
{ name: "purchaseorder", dbs: "bes_bestelopdr_key", typ: "key", foreign: "bes_bestelopdr", filter: "exact"},
{ name: "total", dbs: "fin_factuur_totaal", typ: "float", track: L("lcl_fin_totaal_bedrag"), filter: "range" },
{ name: "vat", dbs: "fin_factuur_totaal_btw", typ: "float", track: L("lcl_fin_totaal_bedrag_btw"), filter: "range" },
{ name: "status", dbs: "fin_factuur_statuses_key", typ: "key", foreign: fin.getfinstatustext, track: L("lcl_fin_fin_status"), filter: "exact" },
{ name: "accountingperiod", dbs: "fin_factuur_boekmaand", typ: "varchar", track: L("lcl_fin_divide_period"), filter: "exact" },
{ name: "reference", dbs: "fin_factuur_debiteur_nr", typ: "varchar", track: L("lcl_fin_debtor_nr"), filter: "like" },
{ name: "costtype", dbs: "prs_kostensoort_key", typ: "key", foreign: "prs_kostensoort", track: L("lcl_shared_charge_type"), filter: "exact" },
{ name: "contact", dbs: "prs_perslid_key_user", typ: "key", foreign: "prs_perslid", track: L("lcl_mld_name"), filter: "exact" },
{ name: "remark", dbs: "fin_factuur_opmerking", typ: "varchar", track: L("lcl_fin_remark"), filter: "like" }
],
includes: {"invoicelines": { model: model_invoicelines,
joinfield: "fin_factuur_key"
}
},
_pre_analyze_fields: function (params, jsondata) /* analyseer inkomende jsondata, common voor PUT en POST */
{
// De factuurbedragen worden berekend uit de factuurregels. Eventuele waarden die hier gezet zijn negeren.
jsondata.invoice.total = 0;
jsondata.invoice.vat = 0;
// Status van 3 (foute import) moet bij saven op 2 worden gezet.
var fin_status_key = jsondata.invoice.status;
if (fin_status_key == 3) fin_status_key = 2;
// Status 6 (Akkoord) moet bij saven op 2 (Ingevoerd) worden gezet. Factuur dient indien nodig weer gefiatteerd te worden.
if (fin_status_key == 6) fin_status_key = 2;
jsondata.invoice.status = fin_status_key;
},
_analyze_fields: function (fields, params, jsondata) /* analyseer inkomende data, common voor PUT en POST */
{
},
_validate_fields: function (fields, params, jsondata) /* valideer fields, alle constraints die niet door de database worden afgevangen */
{
},
REST_GET: function _GET(params)
{
var scope = getQParamSafe("scope", "fe");
var autfunction = { fe : "WEB_FINUSE", fo : "WEB_FINFOF", bo : "WEB_FINBOF" } [scope];
params.authparams = user.checkAutorisation(autfunction, null, null, true); // pessimistisch
var query = api2.sqlfields(params, model_invoices );
query.wheres.push("fin_factuur_verwijder IS NULL");
if (/* self */ 0)
{
query.wheres.push("prs_perslid_key_user=" + user_key);
}
else
{
// TODO: disc3d conditie voor de geldendende autfunction toevoegen
}
query.tables.push("mld_opdr");
query.wheres.push("fin_factuur.mld_opdr_key = mld_opdr.mld_opdr_key(+)");
query.tables.push("bes_bestelopdr");
query.wheres.push("fin_factuur.bes_bestelopdr_key = bes_bestelopdr.bes_bestelopdr_key(+)");
query.tables.push("cnt_contract");
query.wheres.push("fin_factuur.cnt_contract_key = cnt_contract.cnt_contract_key(+)");
var wheres = api2.sqlfilter(params, model_invoices);
query.wheres = query.wheres.concat(wheres);
var sql = "SELECT " + query.selects.join(", ")
+ " FROM " + query.tables.join(", ")
+ " WHERE " + query.wheres.join(" AND " )
+ " ORDER BY fin_factuur_key";
var json = api2.sql2json (params, sql, model_invoices );
return json;
},
REST_PUT: function (params, jsondata, the_key) /* update invoice */
{
var fin_key = the_key;
var this_fin = fin.func_enabled_factuur(fin_key);
user.auth_required_or_abort(this_fin.canChange); // Geen wijzigingen toestaan bij onvoldoende rechten.
//
model_invoices._pre_analyze_fields(params, jsondata);
var fields = api2.update_fields(params, model_invoices, jsondata); // Build updater
model_invoices._analyze_fields(fields, params, jsondata);
model_invoices._validate_fields(fields, params, jsondata);
//
var wheres = [" fin_factuur_key = " + fin_key];
var finUpd = buildTrackingUpdate("fin_factuur", wheres.join(" AND " ), fields, { noValidateToken: true });
var err = Oracle.Execute(finUpd.sql, true);
if (err.friendlyMsg)
abort_with_warning(err.friendlyMsg);
var fintrack = api2.process_includes(params, model_invoices, jsondata, the_key);
// update nog tracken
if (fin_key > 0)
{
shared.trackaction("FINUPD",
fin_key,
L("lcl_fin_is_finupdtrack").format(fin_key) + (finUpd.trackarray.length > 0? "\n" : "") + finUpd.trackarray.join("\n"));
};
return { key: fin_key };
},
REST_POST: function (params, jsondata) /* new invoice */
{
var this_fin = fin.func_enabled_factuur(-1);
user.auth_required_or_abort(this_fin.canChange); // Geen wijzigingen toestaan bij onvoldoende rechten.
params.isNew = true; // negeer eventuele bestaande keys
model_invoices._pre_analyze_fields(params, jsondata);
var fields = api2.update_fields(params, model_invoices, jsondata); // Build updater
model_invoices._analyze_fields(fields, params, jsondata);
model_invoices._validate_fields(fields, params, jsondata);
fields.push({ dbs: "fin_factuur_key", typ: "key", seq: "fin_s_fin_factuur_key" });
var finIns = buildInsert("fin_factuur", fields, { noValidateToken: true} );
var factuur_key = finIns.sequences["fin_factuur_key"];
var sql = "BEGIN "
+ finIns.sql +";"
+ "END;";
var err = Oracle.Execute(sql, true);
if (err.friendlyMsg)
abort_with_warning(err.friendlyMsg);
var fintrack = api2.process_includes(params, model_invoices, jsondata, factuur_key);
shared.trackaction("FINNEW", factuur_key);
return { key: factuur_key };
},
REST_DELETE: function (params, jsondata, the_key) /* delete invoice */
{
var factuur_key = the_key;
var this_fin = fin.func_enabled_factuur(factuur_key);
user.auth_required_or_abort(this_fin.canDelete);
if (this_fin.canDelete) // Dubbel op: bij canDelete=false komt hij hier niet meer.
{
// Verwijderdatum van de factuur zetten en niet fysiek verwijderen
var sql = "UPDATE fin_factuur"
+ " SET fin_factuur_verwijder = SYSDATE"
+ " WHERE fin_factuur_verwijder IS NULL"
+ " AND fin_factuur_key = " + factuur_key;
oRs = Oracle.Execute(sql);
}
}
}
%>