From e94c4eae67f914d9fb97b34e2236f9e6dc77d6cc Mon Sep 17 00:00:00 2001 From: pvincent Date: Wed, 25 Aug 2021 22:17:03 +0400 Subject: [PATCH] App.log for DEBUG --- Makefile | 1 + src/App.hx | 224 ++++--- src/controller/ContractAdmin.hx | 1052 ++++++++++++++++--------------- 3 files changed, 638 insertions(+), 639 deletions(-) diff --git a/Makefile b/Makefile index 94985ba..3b11090 100644 --- a/Makefile +++ b/Makefile @@ -74,6 +74,7 @@ templates: @make LANG=fr ctemplates msgfmt www/lang/texts_fr.po -o www/lang/texts_fr.mo chown www-data:www-data -R www + chown www-data:www-data -R lang ctemplates: (cd lang/$(LANG)/tpl; temploc2 -macros macros.mtt -output ../tmp/ *.mtt */*.mtt */*/*.mtt) diff --git a/src/App.hx b/src/App.hx index 5935665..32f3683 100755 --- a/src/App.hx +++ b/src/App.hx @@ -1,110 +1,109 @@ import db.User; import thx.semver.Version; import Common; - -class App extends sugoi.BaseApp { - public static var current : App = null; - public static var t : sugoi.i18n.translator.ITranslator; +class App extends sugoi.BaseApp { + public static var current:App = null; + public static var t:sugoi.i18n.translator.ITranslator; public static var config = sugoi.BaseApp.config; - - public var eventDispatcher :hxevents.Dispatcher; - public var plugins : Array; - + + public var eventDispatcher:hxevents.Dispatcher; + public var plugins:Array; + /** * Version management * @doc https://github.com/fponticelli/thx.semver - */ - //public static var VERSION = ([0,9,2] : Version).withPre("july"); - //public static var VERSION = ([0,9,2] : Version).withPre(MyMacros.getGitShortSHA(), MyMacros.getGitCommitDate()); + */ + // public static var VERSION = ([0,9,2] : Version).withPre("july"); + // public static var VERSION = ([0,9,2] : Version).withPre(MyMacros.getGitShortSHA(), MyMacros.getGitCommitDate()); public static var VERSION = "cagette-péi " + MyMacros.getGitCommitDate(); - - public function new(){ + + public function new() { super(); } - + public static function main() { - App.t = sugoi.form.Form.translator = new sugoi.i18n.translator.TMap(getTranslationArray(), "fr"); sugoi.BaseApp.main(); } - + /** * Init plugins and event dispatcher just before launching the app */ override public function mainLoop() { eventDispatcher = new hxevents.Dispatcher(); plugins = []; - //internal plugins + // internal plugins plugins.push(new plugin.Tutorial()); - - //optionnal plugins + + // optionnal plugins #if plugins - plugins.push( new hosted.HostedPlugIn() ); - plugins.push( new pro.ProPlugIn() ); - plugins.push( new connector.ConnectorPlugIn() ); - plugins.push( new pro.LemonwayEC() ); - plugins.push( new who.WhoPlugIn() ); + plugins.push(new hosted.HostedPlugIn()); + plugins.push(new pro.ProPlugIn()); + plugins.push(new connector.ConnectorPlugIn()); + plugins.push(new pro.LemonwayEC()); + plugins.push(new who.WhoPlugIn()); #end - + super.mainLoop(); } - - public function getCurrentGroup(){ - if (session == null) return null; - if (session.data == null ) return null; + + public function getCurrentGroup() { + if (session == null) + return null; + if (session.data == null) + return null; var a = session.data.amapId; if (a == null) { return null; - }else { - return db.Amap.manager.get(a,false); + } else { + return db.Amap.manager.get(a, false); } } - + override function beforeDispatch() { - - //send "current page" event - event( Page(this.uri) ); - + // send "current page" event + event(Page(this.uri)); + super.beforeDispatch(); } - + public function getPlugin(name:String):sugoi.plugin.IPlugIn { for (p in plugins) { - if (p.getName() == name) return p; + if (p.getName() == name) + return p; } return null; } - - public static function log(t:Dynamic) { - if(App.config.DEBUG) { - neko.Web.logMessage(Std.string(t)); //write in Apache error log - #if weblog - Weblog.log(t); //write en Weblog console (https://lib.haxe.org/p/weblog/) - #end + + public static function log(t:Dynamic, ?infos:haxe.PosInfos) { + if (App.config.DEBUG) { + // neko.Web.logMessage(Std.string(t)); // write in Apache error log + neko.Web.logMessage('[${infos.fileName}:${infos.lineNumber} ${infos.className}#${infos.methodName}] ' + Std.string(t)); } } - + public function event(e:Event) { - if(e==null) return null; + if (e == null) + return null; this.eventDispatcher.dispatch(e); return e; } - + /** * Translate DB objects fields in forms */ public static function getTranslationArray() { - //var t = sugoi.i18n.Locale.texts; - var out = new Map(); - //out.set("firstName", t._("First name") ); - //out.set("lastName", t._("Last name")); + // var t = sugoi.i18n.Locale.texts; + var out = new Map(); + // out.set("firstName", t._("First name") ); + // out.set("lastName", t._("Last name")); out.set("firstName2", "Prénom du conjoint"); out.set("lastName2", "Nom du conjoint"); out.set("email2", "e-mail du conjoint"); - //out.set("pass", t._("Password") ); - //out.set("address1", t._("address") ); - //out.set("address2", t._("address") ); + // out.set("pass", t._("Password") ); + // out.set("address1", t._("address") ); + // out.set("address2", t._("address") ); out.set("zipCode", "code postal"); out.set("city", "commune"); out.set("phone", "téléphone"); @@ -123,24 +122,24 @@ class App extends sugoi.BaseApp { out.set("txtHome", "Texte en page d'accueil pour les adhérents connectés"); out.set("txtDistrib", "Texte à faire figurer sur les listes d'émargement lors des distributions"); out.set("extUrl", "URL du site du groupe."); - + out.set("distributor1", "Distributeur 1"); out.set("distributor2", "Distributeur 2"); out.set("distributor3", "Distributeur 3"); out.set("distributor4", "Distributeur 4"); out.set("distributorNum", "Nbre de distributeurs nécessaires (de 0 à 4)"); - + out.set("startDate", "Date de début"); out.set("endDate", "Date de fin"); - + out.set("orderStartDate", "Date ouverture des commandes"); - out.set("orderEndDate", "Date fermeture des commandes"); - out.set("openingHour", "Heure d'ouverture"); - out.set("closingHour", "Heure de fermeture"); - - out.set("date", "Date de distribution"); - out.set("active", "actif"); - + out.set("orderEndDate", "Date fermeture des commandes"); + out.set("openingHour", "Heure d'ouverture"); + out.set("closingHour", "Heure de fermeture"); + + out.set("date", "Date de distribution"); + out.set("active", "actif"); + out.set("contact", "Reponsable"); out.set("vendor", "Producteur"); out.set("text", "Texte"); @@ -170,7 +169,7 @@ class App extends sugoi.BaseApp { out.set("pname", "Produit"); out.set("organic", "Agriculture biologique"); out.set("hasFloatQt", "Autoriser quantités \"à virgule\""); - + out.set("membershipRenewalDate", "Adhésions : Date de renouvellement"); out.set("membershipPrice", "Adhésions : Coût de l'adhésion"); out.set("UsersCanOrder", "Les adhérents peuvent saisir leur commande en ligne"); @@ -193,12 +192,12 @@ class App extends sugoi.BaseApp { out.set("ref", "Référence"); out.set("linkText", "Intitulé du lien"); out.set("linkUrl", "URL du lien"); - + out.set("Amap", "AMAP"); out.set("GroupedOrders", "Groupement d'achat"); out.set("ProducerDrive", "Collectif de producteurs"); out.set("FarmShop", "Vente à la ferme"); - + out.set("regOption", "Inscription de nouveaux adhérents"); out.set("Closed", "Fermé : Le coordinateur ajoute les nouveaux adhérents"); out.set("WaitingList", "Liste d'attente"); @@ -206,102 +205,95 @@ class App extends sugoi.BaseApp { out.set("Full", "Complet : Le groupe n'accepte plus de nouveaux adhérents"); out.set("percent", "Pourcentage"); out.set("pinned", "Mets en avant les produits"); - + out.set("CagetteNetwork", "Me lister dans l'annuaire des groupes Cagette.net"); out.set("unitType", "Unité"); out.set("qt", "Quantité"); out.set("Unit", "Pièce"); out.set("Kilogram", "Kilogrammes"); out.set("Gram", "Grammes"); - out.set("Litre", "Litres"); + out.set("Litre", "Litres"); out.set("htPrice", "Prix H.T"); out.set("amount", "Montant"); - + out.set("HasPayments", "Gestion des paiements"); - + out.set("byMember", "Par adhérent"); out.set("byProduct", "Par produit"); - + out.set("variablePrice", "Variable price based on weight"); return out; } - - public function populateAmapMembers() { + + public function populateAmapMembers() { return user.amap.getMembersFormElementData(); } - - public static function getMailer():sugoi.mail.IMailer { - - var mailer : sugoi.mail.IMailer = new sugoi.mail.BufferedMailer(); - - if(App.config.DEBUG){ - //Dev env : emails are written to tmp folder + public static function getMailer():sugoi.mail.IMailer { + var mailer:sugoi.mail.IMailer = new sugoi.mail.BufferedMailer(); + + if (App.config.DEBUG) { + // Dev env : emails are written to tmp folder mailer = new sugoi.mail.DebugMailer(); - }else{ - - if (sugoi.db.Variable.get("mailer") == null){ + } else { + if (sugoi.db.Variable.get("mailer") == null) { var msg = sugoi.i18n.Locale.texts._("Please configure the email settings in a this section"); - throw sugoi.ControllerAction.ErrorAction("/",msg); + throw sugoi.ControllerAction.ErrorAction("/", msg); } - if (sugoi.db.Variable.get("mailer") == "mandrill"){ - //Buffered emails with Mandrill - untyped mailer.defineFinalMailer("mandrill"); - }else{ - //Buffered emails with SMTP + if (sugoi.db.Variable.get("mailer") == "mandrill") { + // Buffered emails with Mandrill + untyped mailer.defineFinalMailer("mandrill"); + } else { + // Buffered emails with SMTP untyped mailer.defineFinalMailer("smtp"); } } return mailer; } - + /** * Send an email */ - public static function sendMail(m:sugoi.mail.Mail, ?group:db.Amap, ?listId:String, ?sender:db.User){ - - if (group == null) group = App.current.user == null ? null:App.current.user.getAmap(); - - current.event(SendEmail(m)); - - var params = group==null ? null : {remoteId:group.id}; + public static function sendMail(m:sugoi.mail.Mail, ?group:db.Amap, ?listId:String, ?sender:db.User) { + if (group == null) + group = App.current.user == null ? null : App.current.user.getAmap(); - getMailer().send(m,params,function(o){}); - + current.event(SendEmail(m)); + + var params = group == null ? null : {remoteId: group.id}; + + getMailer().send(m, params, function(o) {}); } - - public static function quickMail(to:String, subject:String, html:String,?group:db.Amap){ - var e = new sugoi.mail.Mail(); + + public static function quickMail(to:String, subject:String, html:String, ?group:db.Amap) { + var e = new sugoi.mail.Mail(); e.setSubject(subject); - e.setRecipient(to); - e.setSender(App.config.get("default_email"),"Cagette.net"); - var html = App.current.processTemplate("mail/message.mtt", {text:html,group:group}); + e.setRecipient(to); + e.setSender(App.config.get("default_email"), "Cagette.net"); + var html = App.current.processTemplate("mail/message.mtt", {text: html, group: group}); e.setHtmlBody(html); App.sendMail(e); } - + /** * process a template and returns the generated string * @param tpl * @param ctx */ public function processTemplate(tpl:String, ctx:Dynamic):String { - Reflect.setField(ctx, 'HOST', App.config.HOST); Reflect.setField(ctx, 'hDate', App.current.view.hDate); - //i18n functions + // i18n functions ctx._ = App.current.view._; ctx.__ = App.current.view.__; - + var tpl = loadTemplate(tpl); - var html = tpl.execute(ctx); + var html = tpl.execute(ctx); #if php - if ( html.substr(0, 4) == "null") html = html.substr(4); + if (html.substr(0, 4) == "null") + html = html.substr(4); #end return html; } - - - } diff --git a/src/controller/ContractAdmin.hx b/src/controller/ContractAdmin.hx index 8b5e859..70d1465 100755 --- a/src/controller/ContractAdmin.hx +++ b/src/controller/ContractAdmin.hx @@ -1,4 +1,5 @@ package controller; + import db.UserContract; import sugoi.form.elements.Checkbox; import sugoi.form.elements.Selectbox; @@ -6,67 +7,67 @@ import sugoi.form.Form; import sugoi.form.elements.StringInput; import Common; import datetime.DateTime; + using tools.ObjectListTool; using tools.DateTool; + import service.OrderService; -class ContractAdmin extends Controller -{ - - public function new() - { +class ContractAdmin extends Controller { + public function new() { super(); - if (!app.user.isContractManager()) throw Error("/", t._("You don't have the authorization to manage contracts")); + if (!app.user.isContractManager()) + throw Error("/", t._("You don't have the authorization to manage contracts")); view.nav = ["contractadmin"]; - + + App.log("first message"); } - - public function sendNav(c){ + + public function sendNav(c) { var navbar = new Array(); - var e = Nav(navbar,"contractAdmin",c.id); + var e = Nav(navbar, "contractAdmin", c.id); app.event(e); view.navbar = e.getParameters()[0]; } - + /** * Contract admin main page */ @tpl("contractadmin/default.mtt") function doDefault(?args:{old:Bool}) { - view.nav.push("default"); - + var now = Date.now(); - + var contracts; if (args != null && args.old) { - contracts = db.Contract.manager.search($amap == app.user.amap && $endDate < Date.now() ,{orderBy:-startDate},false); - }else { - contracts = db.Contract.getActiveContracts(app.user.amap, true, false); + contracts = db.Contract.manager.search($amap == app.user.amap && $endDate < Date.now(), {orderBy: -startDate}, false); + } else { + contracts = db.Contract.getActiveContracts(app.user.amap, true, false); } - //filter if current user is not manager + // filter if current user is not manager if (!app.user.isAmapManager()) { - for ( c in Lambda.array(contracts).copy()) { - if(!app.user.canManageContract(c)) contracts.remove(c); + for (c in Lambda.array(contracts).copy()) { + if (!app.user.canManageContract(c)) + contracts.remove(c); } } - - view.contracts = contracts; + + view.contracts = contracts; view.vendors = app.user.amap.getVendors(); view.places = app.user.amap.getPlaces(); checkToken(); - //Multidistribs to validate - if(app.user.isAmapManager() && app.user.amap.hasPayments()){ - var twoMonthAgo = tools.DateTool.deltaDays(now,-60); - var multidistribs = MultiDistrib.getFromTimeRange(app.user.amap,twoMonthAgo,now,db.Contract.TYPE_VARORDER); + // Multidistribs to validate + if (app.user.isAmapManager() && app.user.amap.hasPayments()) { + var twoMonthAgo = tools.DateTool.deltaDays(now, -60); + var multidistribs = MultiDistrib.getFromTimeRange(app.user.amap, twoMonthAgo, now, db.Contract.TYPE_VARORDER); /*for( md in multidistribs.copy()){ if(md.hasOnlyConstantOrders()) multidistribs.remove(md); }*/ - view.multidistribs = multidistribs; - - }else{ + view.multidistribs = multidistribs; + } else { view.multidistribs = []; } } @@ -75,341 +76,322 @@ class ContractAdmin extends Controller * Manage products */ @tpl("contractadmin/products.mtt") - function doProducts(contract:db.Contract,?args:{?enable:String,?disable:String}) { + function doProducts(contract:db.Contract, ?args:{?enable:String, ?disable:String}) { view.nav.push("products"); sendNav(contract); - - if (!app.user.canManageContract(contract)) throw Error("/", t._("Access forbidden") ); + + if (!app.user.canManageContract(contract)) + throw Error("/", t._("Access forbidden")); view.c = contract; - - //checks - if (app.user.amap.hasShopMode() && !app.user.amap.hasTaxonomy() ) { - for ( p in contract.getProducts(false)) { + + // checks + if (app.user.amap.hasShopMode() && !app.user.amap.hasTaxonomy()) { + for (p in contract.getProducts(false)) { if (p.getCategories().length == 0) { - app.session.addMessage(t._("Warning, at least one product does not have any category. Click here to add categories", {contractid:contract.id}), true); + app.session.addMessage(t._("Warning, at least one product does not have any category. Click here to add categories", + { + contractid: contract.id + }), true); break; } - } + } } - - //batch enable / disable products - if (args != null){ - - if (args.disable != null){ - var pids = Lambda.array(Lambda.map(args.disable.split("|"), function(x) return Std.parseInt(x))); + + // batch enable / disable products + if (args != null) { + if (args.disable != null) { + var pids = Lambda.array(Lambda.map(args.disable.split("|"), function(x) return Std.parseInt(x))); service.ProductService.batchDisableProducts(pids); } - - if (args.enable != null){ + + if (args.enable != null) { var pids = Lambda.array(Lambda.map(args.enable.split("|"), function(x) return Std.parseInt(x))); service.ProductService.batchEnableProducts(pids); } - } - - //generate a token + + // generate a token checkToken(); } - - + /** * - hidden page - * copy products from a contract to an other */ @admin @tpl("form.mtt") function doCopyProducts(contract:db.Contract) { - view.title = t._("Copy products in: ")+contract.name; + view.title = t._("Copy products in: ") + contract.name; var form = new Form("copy"); var contracts = app.user.amap.getActiveContracts(); - var contracts = Lambda.map(contracts, function(c) return {key:Std.string(c.id),value:Std.string(c.name) } ); - form.addElement(new sugoi.form.elements.Selectbox("source", t._("Copy products from: "),Lambda.array(contracts))); + var contracts = Lambda.map(contracts, function(c) return {key: Std.string(c.id), value: Std.string(c.name)}); + form.addElement(new sugoi.form.elements.Selectbox("source", t._("Copy products from: "), Lambda.array(contracts))); form.addElement(new sugoi.form.elements.Checkbox("delete", t._("Delete existing products (all orders will be deleted!)"))); if (form.checkToken()) { - if (form.getValueOf("delete") == "1") { - for ( p in contract.getProducts()) { + for (p in contract.getProducts()) { p.lock(); p.delete(); } } - + var source = db.Contract.manager.get(Std.parseInt(form.getValueOf("source")), false); var prods = source.getProducts(); - for ( source_p in prods) { + for (source_p in prods) { var p = new db.Product(); p.name = source_p.name; p.price = source_p.price; - //p.type = source_p.type; + // p.type = source_p.type; p.contract = contract; p.insert(); } - + throw Ok("/contractAdmin/products/" + contract.id, t._("Products copied from ") + source.name); - - } - - + view.form = form; } - + /** * displays a calendar of the current month * with all events ( contracts start and end, deliveries... ) */ @tpl('contractadmin/calendar.mtt') public function doCalendar() { - - var contracts = db.Contract.getActiveContracts(app.user.amap, true, false); - - //Events of the month in a calendar + var contracts = db.Contract.getActiveContracts(app.user.amap, true, false); + + // Events of the month in a calendar var cal = Calendar.getMonthViewMap(); - - for ( c in contracts) { - var start = c.startDate.toString().substr(0,10); - var end = c.endDate.toString().substr(0,10); - if (cal.exists( start )) { + + for (c in contracts) { + var start = c.startDate.toString().substr(0, 10); + var end = c.endDate.toString().substr(0, 10); + if (cal.exists(start)) { var v = cal.get(start); - v.push( { name: t._("Contract start ") + c.name, color:Calendar.COLOR_CONTRACT } ); - cal.set( start, v ); + v.push({name: t._("Contract start ") + c.name, color: Calendar.COLOR_CONTRACT}); + cal.set(start, v); } - if (cal.exists( end )) { + if (cal.exists(end)) { var v = cal.get(end); - v.push( { name: t._("Contract end ") +c.name, color:Calendar.COLOR_CONTRACT } ); - cal.set( end, v ); + v.push({name: t._("Contract end ") + c.name, color: Calendar.COLOR_CONTRACT}); + cal.set(end, v); } - - //deliveries - for ( d in c.getDistribs(false)) { - var start = d.date.toString().substr(0,10); - - if (cal.exists( start )) { - var v = cal.get( start ); - v.push( { name: t._("Distribution") +" "+d.contract.name, color:Calendar.COLOR_DELIVERY } ); - cal.set( start, v ); + + // deliveries + for (d in c.getDistribs(false)) { + var start = d.date.toString().substr(0, 10); + + if (cal.exists(start)) { + var v = cal.get(start); + v.push({name: t._("Distribution") + " " + d.contract.name, color: Calendar.COLOR_DELIVERY}); + cal.set(start, v); } - - if ( d.orderStartDate != null && d.orderStartDate != null ) { - var k = d.orderStartDate.toString().substr(0,10); - if (cal.exists( k )) { - var v = cal.get( k ); - v.push( { name: t._("Opening of orders ") +d.contract.name, color:Calendar.COLOR_ORDER } ); - cal.set( k , v ); + + if (d.orderStartDate != null && d.orderStartDate != null) { + var k = d.orderStartDate.toString().substr(0, 10); + if (cal.exists(k)) { + var v = cal.get(k); + v.push({name: t._("Opening of orders ") + d.contract.name, color: Calendar.COLOR_ORDER}); + cal.set(k, v); } - - var k = d.orderEndDate.toString().substr(0,10); - if (cal.exists( k )) { - var v = cal.get( k ); - v.push( { name: t._("End of orders") +d.contract.name, color:Calendar.COLOR_ORDER } ); - cal.set( k , v ); + + var k = d.orderEndDate.toString().substr(0, 10); + if (cal.exists(k)) { + var v = cal.get(k); + v.push({name: t._("End of orders") + d.contract.name, color: Calendar.COLOR_ORDER}); + cal.set(k, v); } - } - } - } - + var n = Date.now(); - view.now = new Date(n.getFullYear(),n.getMonth(),n.getDate(),0,0,0).getTime(); - view.calendar = Calendar.mapToArray( cal ); - - + view.now = new Date(n.getFullYear(), n.getMonth(), n.getDate(), 0, 0, 0).getTime(); + view.calendar = Calendar.mapToArray(cal); } - + /** * global view on orders within a timeframe */ @tpl('contractadmin/ordersByTimeFrame.mtt') - function doOrdersByTimeFrame(?from:Date, ?to:Date/*, ?place:db.Place*/){ - + function doOrdersByTimeFrame(?from:Date, ?to:Date /*, ?place:db.Place*/) { if (from == null) { - var f = new sugoi.form.Form("listBydate", null, sugoi.form.Form.FormMethod.GET); - - var now = DateTime.now(); - var from = now.snap(Month(Down)).getDate(); + + var now = DateTime.now(); + var from = now.snap(Month(Down)).getDate(); var to = now.snap(Month(Up)).add(Day(-1)).getDate(); - - - var el = new sugoi.form.elements.DatePicker("from", t._("Start date"), from,true); + + var el = new sugoi.form.elements.DatePicker("from", t._("Start date"), from, true); el.format = 'LL'; f.addElement(el); - - var el = new sugoi.form.elements.DatePicker("to", t._("End date"), to,true); + + var el = new sugoi.form.elements.DatePicker("to", t._("End date"), to, true); el.format = 'LL'; f.addElement(el); - - //var places = Lambda.map(app.user.amap.getPlaces(), function(p) return {label:p.name,value:p.id} ); - //f.addElement(new sugoi.form.elements.IntSelect("placeId", "Lieu", Lambda.array(places),app.user.amap.getMainPlace().id,true)); - + + // var places = Lambda.map(app.user.amap.getPlaces(), function(p) return {label:p.name,value:p.id} ); + // f.addElement(new sugoi.form.elements.IntSelect("placeId", "Lieu", Lambda.array(places),app.user.amap.getMainPlace().id,true)); + view.form = f; view.title = t._("Global view of orders"); app.setTemplate("form.mtt"); - + if (f.checkToken()) { - - var url = '/contractAdmin/ordersByTimeFrame/' + f.getValueOf("from").toString().substr(0, 10) +"/"+f.getValueOf("to").toString().substr(0, 10); - //var p = f.getValueOf("placeId"); - //if (p != null) url += "/"+p; - throw Redirect( url ); + var url = '/contractAdmin/ordersByTimeFrame/' + f.getValueOf("from").toString().substr(0, 10) + "/" + + f.getValueOf("to").toString().substr(0, 10); + // var p = f.getValueOf("placeId"); + // if (p != null) url += "/"+p; + throw Redirect(url); } - + return; - - }else { - - var d1 = tools.DateTool.setHourMinute(from,0,0); - var d2 = tools.DateTool.setHourMinute(to,23,59); + } else { + var d1 = tools.DateTool.setHourMinute(from, 0, 0); + var d2 = tools.DateTool.setHourMinute(to, 23, 59); var contracts = app.user.amap.getActiveContracts(true); var cconst = []; var cvar = []; - for ( c in contracts) { - if (c.type == db.Contract.TYPE_CONSTORDERS) cconst.push(c.id); - if (c.type == db.Contract.TYPE_VARORDER) cvar.push(c.id); + for (c in contracts) { + if (c.type == db.Contract.TYPE_CONSTORDERS) + cconst.push(c.id); + if (c.type == db.Contract.TYPE_VARORDER) + cvar.push(c.id); } - - //distribs - var vdistribs = db.Distribution.manager.search(($contractId in cvar) && $date >= d1 && $date <= d2 /*&& place.id==$placeId*/, false); - var cdistribs = db.Distribution.manager.search(($contractId in cconst) && $date >= d1 && $date <= d2 /*&& place.id==$placeId*/, false); - - if (vdistribs.length == 0 && cdistribs.length == 0) throw Error("/contractAdmin/ordersByDate", t._("There is no delivery at this date")); - - //varying orders - var varorders = db.UserContract.manager.search($distributionId in vdistribs.getIds() , { orderBy:userId } ); - - //constant orders + + // distribs + var vdistribs = db.Distribution.manager.search(($contractId in cvar) && $date >= d1 && $date <= d2 /*&& place.id==$placeId*/, false); + var cdistribs = db.Distribution.manager.search(($contractId in cconst) && $date >= d1 && $date <= d2 /*&& place.id==$placeId*/, false); + + if (vdistribs.length == 0 && cdistribs.length == 0) + throw Error("/contractAdmin/ordersByDate", t._("There is no delivery at this date")); + + // varying orders + var varorders = db.UserContract.manager.search($distributionId in vdistribs.getIds(), {orderBy: userId}); + + // constant orders var constorders = []; - for ( d in cdistribs) { - var orders2 = db.UserContract.manager.search($productId in d.contract.getProducts().getIds(), { orderBy:userId } ); + for (d in cdistribs) { + var orders2 = db.UserContract.manager.search($productId in d.contract.getProducts().getIds(), {orderBy: userId}); constorders = constorders.concat(Lambda.array(orders2)); } - - //merge 2 lists + + // merge 2 lists var orders = Lambda.array(varorders).concat(Lambda.array(constorders)); var orders = service.OrderService.prepare(Lambda.list(orders)); - + view.orders = orders; view.from = from; view.to = to; view.ctotal = app.params.exists("ctotal"); - } - - - } - + /** * Global view on orders in one day * * @param date */ @tpl('contractadmin/ordersByDate.mtt') - function doOrdersByDate(?date:Date,?place:db.Place){ + function doOrdersByDate(?date:Date, ?place:db.Place) { if (date == null) { - var f = new sugoi.form.Form("listBydate", null, sugoi.form.Form.FormMethod.GET); var el = new sugoi.form.elements.DatePicker("date", t._("Delivery date"), true); el.format = 'LL'; f.addElement(el); - - var places = Lambda.map(app.user.amap.getPlaces(), function(p) return {label:p.name,value:p.id} ); - f.addElement(new sugoi.form.elements.IntSelect("placeId", "Lieu", Lambda.array(places),app.user.amap.getMainPlace().id,true)); - + + var places = Lambda.map(app.user.amap.getPlaces(), function(p) return {label: p.name, value: p.id}); + f.addElement(new sugoi.form.elements.IntSelect("placeId", "Lieu", Lambda.array(places), app.user.amap.getMainPlace().id, true)); + view.form = f; view.title = t._("Global view of orders"); view.text = t._("This page allows you to have a global view on orders of all contracts"); view.text += t._("
Select a delivery date:"); app.setTemplate("form.mtt"); - + if (f.checkToken()) { - var url = '/contractAdmin/ordersByDate/' + f.getValueOf("date").toString().substr(0, 10); var p = f.getValueOf("placeId"); - if (p != null) url += "/"+p; - throw Redirect( url ); + if (p != null) + url += "/" + p; + throw Redirect(url); } - + return; - - }else { - + } else { var d1 = date.setHourMinute(0, 0); - var d2 = date.setHourMinute(23,59); + var d2 = date.setHourMinute(23, 59); var contracts = app.user.amap.getActiveContracts(true); var cconst = []; var cvar = []; - for ( c in contracts) { - if (c.type == db.Contract.TYPE_CONSTORDERS) cconst.push(c.id); - if (c.type == db.Contract.TYPE_VARORDER) cvar.push(c.id); + for (c in contracts) { + if (c.type == db.Contract.TYPE_CONSTORDERS) + cconst.push(c.id); + if (c.type == db.Contract.TYPE_VARORDER) + cvar.push(c.id); } - - //distribs - var vdistribs = db.Distribution.manager.search(($contractId in cvar) && $date >= d1 && $date <= d2 && place.id==$placeId, false); - var cdistribs = db.Distribution.manager.search(($contractId in cconst) && $date >= d1 && $date <= d2 && place.id==$placeId, false); - - if (vdistribs.length == 0 && cdistribs.length == 0) throw Error("/contractAdmin/ordersByDate", t._("There is no delivery at this date")); - - //varying orders - var varorders = db.UserContract.manager.search($distributionId in vdistribs.getIds() , { orderBy:userId } ); - - //constant orders + + // distribs + var vdistribs = db.Distribution.manager.search(($contractId in cvar) && $date >= d1 && $date <= d2 && place.id == $placeId, false); + var cdistribs = db.Distribution.manager.search(($contractId in cconst) && $date >= d1 && $date <= d2 && place.id == $placeId, false); + + if (vdistribs.length == 0 && cdistribs.length == 0) + throw Error("/contractAdmin/ordersByDate", t._("There is no delivery at this date")); + + // varying orders + var varorders = db.UserContract.manager.search($distributionId in vdistribs.getIds(), {orderBy: userId}); + + // constant orders var constorders = []; - for ( d in cdistribs) { - var orders2 = db.UserContract.manager.search($productId in d.contract.getProducts().getIds(), { orderBy:userId } ); + for (d in cdistribs) { + var orders2 = db.UserContract.manager.search($productId in d.contract.getProducts().getIds(), {orderBy: userId}); constorders = constorders.concat(Lambda.array(orders2)); } - - //merge 2 lists + + // merge 2 lists var orders = Lambda.array(varorders).concat(Lambda.array(constorders)); var orders = service.OrderService.prepare(Lambda.list(orders)); - + view.orders = orders; view.date = date; view.place = place; view.ctotal = app.params.exists("ctotal"); } } - - + /** * Global view on orders, producer view */ @tpl('contractadmin/vendorsByDate.mtt') - function doVendorsByDate(date:Date,place:db.Place){ - + function doVendorsByDate(date:Date, place:db.Place) { var d1 = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0); var d2 = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 23, 59, 59); var contracts = app.user.amap.getActiveContracts(true); var cids = Lambda.map(contracts, function(c) return c.id); - - //distribs for both types in active contracts - var distribs = db.Distribution.manager.search(($contractId in cids) && $date >= d1 && $date <= d2 && $place==place , false); - - if ( distribs.length == 0 ) throw Error("/contractAdmin/ordersByDate", t._("There is no delivery at this date")); - - var out = new Map();//key : vendor id - - for (d in distribs){ + // distribs for both types in active contracts + var distribs = db.Distribution.manager.search(($contractId in cids) && $date >= d1 && $date <= d2 && $place == place, false); + + if (distribs.length == 0) + throw Error("/contractAdmin/ordersByDate", t._("There is no delivery at this date")); + + var out = new Map(); // key : vendor id + + for (d in distribs) { var vid = d.contract.vendor.id; var o = out.get(vid); - - if (o == null){ - out.set( vid, {contract:d.contract,distrib:d,orders:service.ReportService.getOrdersByProduct( d )}); - }else{ - - //add orders with existing ones - for ( x in service.ReportService.getOrdersByProduct( d ) ){ - - //find record in existing orders - var f : Dynamic = Lambda.find(o.orders, function(a) return a.pid == x.pid); - if (f == null){ - //new product order - o.orders.push(x); - }else{ - //increment existing + + if (o == null) { + out.set(vid, {contract: d.contract, distrib: d, orders: service.ReportService.getOrdersByProduct(d)}); + } else { + // add orders with existing ones + for (x in service.ReportService.getOrdersByProduct(d)) { + // find record in existing orders + var f:Dynamic = Lambda.find(o.orders, function(a) return a.pid == x.pid); + if (f == null) { + // new product order + o.orders.push(x); + } else { + // increment existing f.quantity += untyped x.quantity; f.total += untyped x.total; } @@ -417,47 +399,44 @@ class ContractAdmin extends Controller out.set(vid, o); } } - + view.orders = Lambda.array(out); view.date = date; } - /** * Global view on orders, producer view */ @tpl('contractadmin/vendorsByTimeFrame.mtt') - function doVendorsByTimeFrame(from:Date,to:Date){ - - var d1 = tools.DateTool.setHourMinute(from,0,0); - var d2 = tools.DateTool.setHourMinute(to,23,59); + function doVendorsByTimeFrame(from:Date, to:Date) { + var d1 = tools.DateTool.setHourMinute(from, 0, 0); + var d2 = tools.DateTool.setHourMinute(to, 23, 59); var contracts = app.user.amap.getActiveContracts(true); var cids = contracts.getIds(); - - //distribs for both types in active contracts - var distribs = db.Distribution.manager.search(($contractId in cids) && $date >= d1 && $date <= d2 /*&& $place==place*/, false); - if ( distribs.length == 0 ) throw Error("/contractAdmin/", t._("There is no delivery during this period")); - - var out = new Map}>();//key : vendor id - - for (d in distribs){ + + // distribs for both types in active contracts + var distribs = db.Distribution.manager.search(($contractId in cids) && $date >= d1 && $date <= d2 /*&& $place==place*/, false); + if (distribs.length == 0) + throw Error("/contractAdmin/", t._("There is no delivery during this period")); + + var out = new Map}>(); // key : vendor id + + for (d in distribs) { var vid = d.contract.vendor.id; var o = out.get(vid); - - if (o == null){ - out.set( vid, {contract:d.contract,distrib:d,orders:service.ReportService.getOrdersByProduct(d) }); - }else{ - - //add orders with existing ones - for ( x in service.ReportService.getOrdersByProduct(d) ){ - - //find record in existing orders - var f : OrderByProduct = Lambda.find(o.orders, function(a:OrderByProduct) return a.pid == x.pid); - if (f == null){ - //new product order - o.orders.push(x); - }else{ - //increment existing + + if (o == null) { + out.set(vid, {contract: d.contract, distrib: d, orders: service.ReportService.getOrdersByProduct(d)}); + } else { + // add orders with existing ones + for (x in service.ReportService.getOrdersByProduct(d)) { + // find record in existing orders + var f:OrderByProduct = Lambda.find(o.orders, function(a:OrderByProduct) return a.pid == x.pid); + if (f == null) { + // new product order + o.orders.push(x); + } else { + // increment existing f.quantity += x.quantity; f.totalHT += x.totalHT; f.totalTTC += x.totalTTC; @@ -466,160 +445,171 @@ class ContractAdmin extends Controller out.set(vid, o); } } - + view.orders = Lambda.array(out); - - if ( app.params.exists("csv") ){ + + if (app.params.exists("csv")) { var totalHT = 0.0; var totalTTC = 0.0; - + var orders = []; - for ( x in out){ - //empty line - orders.push({"quantity":null, "pname":null, "ref":null, "priceHT":null, "priceTTC":null, "totalHT":null, "totalTTC":null}); - orders.push({"quantity":null, "pname":x.contract.vendor.name, "ref":null, "priceHT":null, "priceTTC":null, "totalHT":null, "totalTTC":null}); - - for (o in x.orders){ - if(o.vat==null) o.vat = 0; + for (x in out) { + // empty line + orders.push({ + "quantity": null, + "pname": null, + "ref": null, + "priceHT": null, + "priceTTC": null, + "totalHT": null, + "totalTTC": null + }); + orders.push({ + "quantity": null, + "pname": x.contract.vendor.name, + "ref": null, + "priceHT": null, + "priceTTC": null, + "totalHT": null, + "totalTTC": null + }); + + for (o in x.orders) { + if (o.vat == null) + o.vat = 0; orders.push({ - "quantity":view.formatNum(o.quantity), - "pname":o.pname, - "ref":o.ref, - "priceHT":view.formatNum(o.priceTTC / (1 + o.vat / 100) ), - "priceTTC":view.formatNum(o.priceTTC), - "totalHT":view.formatNum(o.totalHT), - "totalTTC":view.formatNum(o.totalTTC) + "quantity": view.formatNum(o.quantity), + "pname": o.pname, + "ref": o.ref, + "priceHT": view.formatNum(o.priceTTC / (1 + o.vat / 100)), + "priceTTC": view.formatNum(o.priceTTC), + "totalHT": view.formatNum(o.totalHT), + "totalTTC": view.formatNum(o.totalTTC) }); totalTTC += o.totalTTC; totalHT += o.totalHT; } - - //total line + + // total line orders.push({ - "quantity":null, - "pname":null, - "ref":null, - "priceHT":null, - "priceTTC":null, - "totalHT":view.formatNum(totalHT) + "", - "totalTTC":view.formatNum(totalTTC)+"" - }); + "quantity": null, + "pname": null, + "ref": null, + "priceHT": null, + "priceTTC": null, + "totalHT": view.formatNum(totalHT) + "", + "totalTTC": view.formatNum(totalTTC) + "" + }); totalTTC = 0; totalHT = 0; - - } - var fileName = t._("Orders from the ::fromDate:: to the ::toDate:: per supplier.csv", {fromDate:from.toString().substr(0, 10), toDate:to.toString().substr(0, 10)}); - sugoi.tools.Csv.printCsvDataFromObjects(orders, ["quantity", "pname", "ref", "priceHT", "priceTTC", "totalHT","totalTTC"], fileName); + } + var fileName = t._("Orders from the ::fromDate:: to the ::toDate:: per supplier.csv", + {fromDate: from.toString().substr(0, 10), toDate: to.toString().substr(0, 10)}); + sugoi.tools.Csv.printCsvDataFromObjects(orders, ["quantity", "pname", "ref", "priceHT", "priceTTC", "totalHT", "totalTTC"], fileName); return; } - + view.from = from; view.to = to; } - - + /** * Overview of orders for this contract in backoffice */ @tpl("contractadmin/orders.mtt") - function doOrders(contract:db.Contract, args:{?d:db.Distribution,?delete:db.UserContract}) { + function doOrders(contract:db.Contract, args:{?d:db.Distribution, ?delete:db.UserContract}) { view.nav.push("orders"); sendNav(contract); - - //Checking permissions - if (!app.user.canManageContract(contract)) throw Error("/", t._("You do not have the authorization to manage this contract")); - if (contract.type == db.Contract.TYPE_VARORDER && args != null && args.d == null ) { - throw Redirect("/contractAdmin/selectDistrib/" + contract.id); + + // Checking permissions + if (!app.user.canManageContract(contract)) + throw Error("/", t._("You do not have the authorization to manage this contract")); + if (contract.type == db.Contract.TYPE_VARORDER && args != null && args.d == null) { + throw Redirect("/contractAdmin/selectDistrib/" + contract.id); } - //Delete specified order with quantity of zero - if (checkToken() && args != null && args.delete != null ) { + // Delete specified order with quantity of zero + if (checkToken() && args != null && args.delete != null) { try { service.OrderService.delete(args.delete); - } catch(e:tink.core.Error){ + } catch (e:tink.core.Error) { throw Error("/contractAdmin/orders/" + contract.id, e.message); } - if(args.d!=null){ - throw Ok("/contractAdmin/orders/" + contract.id + "?d="+args.d.id, t._("The order has been deleted.")); - }else{ + if (args.d != null) { + throw Ok("/contractAdmin/orders/" + contract.id + "?d=" + args.d.id, t._("The order has been deleted.")); + } else { throw Ok("/contractAdmin/orders/" + contract.id, t._("The order has been deleted.")); } - } var d = null; - if (contract.type == db.Contract.TYPE_VARORDER ){ + if (contract.type == db.Contract.TYPE_VARORDER) { view.distribution = args.d; d = args.d; } view.c = contract; var orders = db.UserContract.getOrders(contract, d, app.params.exists("csv")); - - if ( !app.params.exists("csv") ){ - - //show orders on disabled products + + if (!app.params.exists("csv")) { + // show orders on disabled products var disabledProducts = 0; - for ( o in orders ){ - if ( !db.Product.manager.get(o.productId, false).active ) { + for (o in orders) { + if (!db.Product.manager.get(o.productId, false).active) { disabledProducts++; Reflect.setField(o, "disabled", true); } } - + view.disabledProducts = disabledProducts; - view.orders = orders; + view.orders = orders; } - } - - + /** * hidden feature : updates orders by setting current product price. */ function doUpdatePrices(contract:db.Contract, args:{?d:db.Distribution}) { - sendNav(contract); - - if (!app.user.canManageContract(contract)) throw Error("/", t._("You do not have the authorization to manage this contract")); - if (contract.type == db.Contract.TYPE_VARORDER && args.d == null ) { - throw Redirect("/contractAdmin/selectDistrib/" + contract.id); + + if (!app.user.canManageContract(contract)) + throw Error("/", t._("You do not have the authorization to manage this contract")); + if (contract.type == db.Contract.TYPE_VARORDER && args.d == null) { + throw Redirect("/contractAdmin/selectDistrib/" + contract.id); } var d = null; - if (contract.type == db.Contract.TYPE_VARORDER ){ + if (contract.type == db.Contract.TYPE_VARORDER) { view.distribution = args.d; d = args.d; } - - for ( o in contract.getOrders(d)){ + + for (o in contract.getOrders(d)) { o.lock(); o.productPrice = o.product.price; - if (contract.hasPercentageOnOrders()){ + if (contract.hasPercentageOnOrders()) { o.feesRate = contract.percentageValue; } o.update(); - } - throw Ok("/contractAdmin/orders/"+contract.id+"?d="+args.d.id, t._("Prices are now up to date.")); + throw Ok("/contractAdmin/orders/" + contract.id + "?d=" + args.d.id, t._("Prices are now up to date.")); } - + /** * Duplicate a contract */ @tpl("form.mtt") function doDuplicate(contract:db.Contract) { - sendNav(contract); - if (!app.user.isAmapManager()) throw Error("/", t._("You do not have the authorization to manage this contract")); - - view.title = "Dupliquer le contrat '"+contract.name+"'"; - var form = new Form("duplicate"); - - form.addElement(new StringInput("name", t._("Name of the new contract"), contract.name.substr(0,50) + " - copy")); - form.addElement(new Checkbox("copyProducts", t._("Copy products"),true)); - form.addElement(new Checkbox("copyDeliveries", t._("Copy deliveries"),true)); - - if (form.checkToken()) { + if (!app.user.isAmapManager()) + throw Error("/", t._("You do not have the authorization to manage this contract")); + view.title = "Dupliquer le contrat '" + contract.name + "'"; + var form = new Form("duplicate"); + + form.addElement(new StringInput("name", t._("Name of the new contract"), contract.name.substr(0, 50) + " - copy")); + form.addElement(new Checkbox("copyProducts", t._("Copy products"), true)); + form.addElement(new Checkbox("copyDeliveries", t._("Copy deliveries"), true)); + + if (form.checkToken()) { var nc = new db.Contract(); nc.name = form.getValueOf("name"); nc.startDate = contract.startDate; @@ -634,17 +624,16 @@ class ContractAdmin extends Controller nc.percentageName = contract.percentageName; nc.percentageValue = contract.percentageValue; nc.insert(); - - //give right to this contract - if(contract.contact!=null){ + + // give right to this contract + if (contract.contact != null) { var ua = db.UserAmap.get(contract.contact, contract.amap); ua.giveRight(ContractAdmin(nc.id)); } - - + if (form.getValueOf("copyProducts") == true) { var prods = contract.getProducts(); - for ( source_p in prods) { + for (source_p in prods) { var p = new db.Product(); p.name = source_p.name; p.price = source_p.price; @@ -660,8 +649,8 @@ class ContractAdmin extends Controller p.multiWeight = source_p.multiWeight; p.variablePrice = source_p.variablePrice; p.insert(); - - for (source_cat in source_p.getCategories()){ + + for (source_cat in source_p.getCategories()) { var cat = new db.ProductCategory(); cat.product = p; cat.category = source_cat; @@ -669,9 +658,9 @@ class ContractAdmin extends Controller } } } - + if (form.getValueOf("copyDeliveries") == true) { - for ( ds in contract.getDistribs()) { + for (ds in contract.getDistribs()) { var d = new db.Distribution(); d.contract = nc; d.date = ds.date; @@ -686,81 +675,87 @@ class ContractAdmin extends Controller d.insert(); } } - + app.event(DuplicateContract(contract)); - + throw Ok("/contractAdmin/view/" + nc.id, t._("The contract has been duplicated")); } - + view.form = form; } - - - + /** * Orders grouped by product */ @tpl("contractadmin/ordersByProduct.mtt") function doOrdersByProduct(contract:db.Contract, args:{?d:db.Distribution}) { - - sendNav(contract); - if (!app.user.canManageContract(contract)) throw Error("/", t._("You do not have the authorization to manage this contract")); - if (contract.type == db.Contract.TYPE_VARORDER && args.d == null ) throw Redirect("/contractAdmin/selectDistrib/" + contract.id); - - var d = args != null ? args.d : null; - if (d == null) d = contract.getDistribs(false).first(); - if (d == null) throw Error("/contractAdmin/orders/"+contract.id,t._("There is no delivery in this contract, please create at least one distribution.")); + sendNav(contract); + if (!app.user.canManageContract(contract)) + throw Error("/", t._("You do not have the authorization to manage this contract")); + if (contract.type == db.Contract.TYPE_VARORDER && args.d == null) + throw Redirect("/contractAdmin/selectDistrib/" + contract.id); - var orders = service.ReportService.getOrdersByProduct(d,app.params.exists("csv")); + var d = args != null ? args.d : null; + if (d == null) + d = contract.getDistribs(false).first(); + if (d == null) + throw Error("/contractAdmin/orders/" + contract.id, t._("There is no delivery in this contract, please create at least one distribution.")); + + var orders = service.ReportService.getOrdersByProduct(d, app.params.exists("csv")); view.orders = orders; - view.distribution = d; + view.distribution = d; view.c = contract; - } - + /** * Purchase order to print */ @tpl("contractadmin/ordersByProductList.mtt") function doOrdersByProductList(contract:db.Contract, args:{?d:db.Distribution}) { - - sendNav(contract); - if (!app.user.canManageContract(contract)) throw Error("/", t._("You do not have the authorization to manage this contract")); - if (contract.type == db.Contract.TYPE_VARORDER && args.d == null ) throw Redirect("/contractAdmin/selectDistrib/" + contract.id); - - if (contract.type == db.Contract.TYPE_VARORDER ) view.distribution = args.d; + sendNav(contract); + if (!app.user.canManageContract(contract)) + throw Error("/", t._("You do not have the authorization to manage this contract")); + if (contract.type == db.Contract.TYPE_VARORDER && args.d == null) + throw Redirect("/contractAdmin/selectDistrib/" + contract.id); + + if (contract.type == db.Contract.TYPE_VARORDER) + view.distribution = args.d; view.c = contract; view.group = contract.amap; var d = args != null ? args.d : null; - if (d == null) d = contract.getDistribs(false).first(); - if (d == null) throw t._("No delivery in this contract"); - - var orders = service.ReportService.getOrdersByProduct(d,false); + if (d == null) + d = contract.getDistribs(false).first(); + if (d == null) + throw t._("No delivery in this contract"); + + var orders = service.ReportService.getOrdersByProduct(d, false); view.orders = orders; } - + /** * Lists deliveries for this contract */ @tpl("contractadmin/deliveries.mtt") - function doDistributions(contract:db.Contract, ?args: { old:Bool } ) { + function doDistributions(contract:db.Contract, ?args:{old:Bool}) { view.nav.push("distributions"); sendNav(contract); - - if (!app.user.canManageContract(contract)) throw Error("/", t._("You do not have the authorization to manage this contract")); + + if (!app.user.canManageContract(contract)) + throw Error("/", t._("You do not have the authorization to manage this contract")); view.c = contract; - + if (args != null && args.old) { - //display also old deliveries - view.deliveries = contract.getDistribs(false); - }else { - view.deliveries = db.Distribution.manager.search($end > DateTools.delta(Date.now(), -1000.0 * 60 * 60 * 24 * 30) && $contract == contract, { orderBy:date} ); + // display also old deliveries + view.deliveries = contract.getDistribs(false); + } else { + view.deliveries = db.Distribution.manager.search($end > DateTools.delta(Date.now(), -1000.0 * 60 * 60 * 24 * 30) + && $contract == contract, + {orderBy: date}); } - - view.cycles = db.DistributionCycle.manager.search( $contract==contract && $endDate > Date.now() ,false); - + + view.cycles = db.DistributionCycle.manager.search($contract == contract && $endDate > Date.now(), false); } - + /** * Attendance to distribution */ @@ -768,45 +763,47 @@ class ContractAdmin extends Controller function doDistributionp(contract:db.Contract) { view.nav.push("distributions"); sendNav(contract); - - if (!app.user.canManageContract(contract)) throw Error("/", t._("You do not have the authorization to manage this contract")); - - var distribs = contract.getDistribs(false); - var userCount = new Map(); - var suscribers = contract.getUsers(); - for( u in suscribers) userCount[u.id] = 0; - //populate - var increment = function(user:db.User){ - if(user==null) return; + if (!app.user.canManageContract(contract)) + throw Error("/", t._("You do not have the authorization to manage this contract")); + + var distribs = contract.getDistribs(false); + var userCount = new Map(); + var suscribers = contract.getUsers(); + for (u in suscribers) + userCount[u.id] = 0; + // populate + + var increment = function(user:db.User) { + if (user == null) + return; var x = userCount[user.id]; - if(x==null){ + if (x == null) { userCount[user.id] = 1; - }else{ - userCount[user.id] = x+1; - } + } else { + userCount[user.id] = x + 1; + } } - - for ( d in distribs) { + + for (d in distribs) { increment(d.distributor1); increment(d.distributor2); increment(d.distributor3); increment(d.distributor4); } - - var out1 = new Array<{user:db.User,count:Int}>();//suscribers - var out2 = new Array<{user:db.User,count:Int}>();//not suscribers - for( k in userCount.keys()){ - if(Lambda.find(suscribers,function(s) return s.id==k)!=null){ - out1.push( { user:db.User.manager.get(k), count:userCount[k] } ); - }else{ - out2.push( { user:db.User.manager.get(k), count:userCount[k] } ); - } - - } - var num = (distribs.length*contract.distributorNum) / suscribers.length; - view.num = Std.string(num).substr(0,4); + var out1 = new Array<{user:db.User, count:Int}>(); // suscribers + var out2 = new Array<{user:db.User, count:Int}>(); // not suscribers + for (k in userCount.keys()) { + if (Lambda.find(suscribers, function(s) return s.id == k) != null) { + out1.push({user: db.User.manager.get(k), count: userCount[k]}); + } else { + out2.push({user: db.User.manager.get(k), count: userCount[k]}); + } + } + + var num = (distribs.length * contract.distributorNum) / suscribers.length; + view.num = Std.string(num).substr(0, 4); view.numRounded = Math.round(num); view.users = suscribers.length; view.distributorNum = contract.distributorNum; @@ -815,92 +812,96 @@ class ContractAdmin extends Controller view.participations = out1; view.extParticipations = out2; } - + @tpl("contractadmin/view.mtt") function doView(contract:db.Contract) { view.nav.push("view"); sendNav(contract); - - if (!app.user.canManageContract(contract)) throw Error("/", t._("You do not have the authorization to manage this contract")); + + if (!app.user.canManageContract(contract)) + throw Error("/", t._("You do not have the authorization to manage this contract")); view.c = view.contract = contract; } - - + @tpl("contractadmin/stats.mtt") - function doStats(contract:db.Contract, ?args: { stat:Int } ) { + function doStats(contract:db.Contract, ?args:{stat:Int}) { sendNav(contract); - if (!app.user.canManageContract(contract)) throw Error("/", t._("You do not have the authorization to manage this contract")); + if (!app.user.canManageContract(contract)) + throw Error("/", t._("You do not have the authorization to manage this contract")); view.c = contract; - - if (args == null) args = { stat:0 }; + + if (args == null) + args = {stat: 0}; view.stat = args.stat; var pids = contract.getProducts().map(function(x) return x.id); - switch(args.stat) { - case 0 : - //ancienneté des amapiens - - if(pids.length==0){ + switch (args.stat) { + case 0: + // ancienneté des amapiens + + if (pids.length == 0) { view.anciennete = new List(); - }else{ - view.anciennete = sys.db.Manager.cnx.request("select YEAR(u.cdate) as uyear ,count(DISTINCT u.id) as cnt from User u, UserContract up where up.userId=u.id and up.productId IN (" + pids.join(",") + ") group by uyear;").results(); + } else { + view.anciennete = sys.db.Manager.cnx.request("select YEAR(u.cdate) as uyear ,count(DISTINCT u.id) as cnt from User u, UserContract up where up.userId=u.id and up.productId IN (" + + pids.join(",") + + ") group by uyear;") + .results(); } - - case 1 : - //repartition des commandes + + case 1: + // repartition des commandes var repartition = new List(); var total = 0; var totalPrice = 0; - if(pids.length!=0){ - var repartition = sys.db.Manager.cnx.request("select sum(quantity) as quantity,productId,p.name,p.price from UserContract up, Product p where up.productId IN (" + contract.getProducts().map(function(x) return x.id).join(",") + ") and up.productId=p.id group by productId").results(); - for ( r in repartition) { + if (pids.length != 0) { + var repartition = sys.db.Manager.cnx.request("select sum(quantity) as quantity,productId,p.name,p.price from UserContract up, Product p where up.productId IN (" + + contract.getProducts().map(function(x) return x.id).join(",") + + ") and up.productId=p.id group by productId") + .results(); + for (r in repartition) { total += r.quantity; - totalPrice += r.price*r.quantity; + totalPrice += r.price * r.quantity; } for (r in repartition) { - Reflect.setField(r, "percent", Math.round((r.quantity/total)*100) ); + Reflect.setField(r, "percent", Math.round((r.quantity / total) * 100)); } - - if ( app.params.exists("csv") ){ - sugoi.tools.Csv.printCsvDataFromObjects(Lambda.array(repartition), ["quantity","productId","name","price","percent"], "stats-" + contract.name+".csv"); + + if (app.params.exists("csv")) { + sugoi.tools.Csv.printCsvDataFromObjects(Lambda.array(repartition), ["quantity", "productId", "name", "price", "percent"], + "stats-" + contract.name + ".csv"); } - } - + } + view.repartition = repartition; view.totalQuantity = total; view.totalPrice = totalPrice; - } - } - - - + /** * Efface une commande * @param uc */ function doDelete(uc:UserContract) { - if (!app.user.canManageContract(uc.product.contract)) throw Error("/", t._("You do not have the authorization to manage this contract")); + if (!app.user.canManageContract(uc.product.contract)) + throw Error("/", t._("You do not have the authorization to manage this contract")); uc.lock(); uc.delete(); - throw Ok('/contractAdmin/orders/'+uc.product.contract.id, t._("The contract has been canceled")); + throw Ok('/contractAdmin/orders/' + uc.product.contract.id, t._("The contract has been canceled")); } - @tpl("contractadmin/selectDistrib.mtt") function doSelectDistrib(c:db.Contract, ?args:{old:Bool}) { view.nav.push("orders"); sendNav(c); - + view.c = c; - if (args != null && args.old){ - view.distributions = c.getDistribs(false); - }else{ + if (args != null && args.old) { + view.distributions = c.getDistribs(false); + } else { view.distributions = c.getDistribs(true); } - } - + /** * Edit a user's orders */ @@ -908,120 +909,125 @@ class ContractAdmin extends Controller function doEdit(c:db.Contract, ?user:db.User, args:{?d:db.Distribution}) { view.nav.push("orders"); sendNav(c); - - if (!app.user.canManageContract(c)) throw Error("/", t._("You do not have the authorization to manage this contract")); - if (args.d != null && args.d.validated) throw Error("/contractAdmin/orders/" + c.id + "?d=" + args.d.id, t._("This delivery has been already validated")); - + + if (!app.user.canManageContract(c)) + throw Error("/", t._("You do not have the authorization to manage this contract")); + if (args.d != null && args.d.validated) + throw Error("/contractAdmin/orders/" + c.id + "?d=" + args.d.id, t._("This delivery has been already validated")); + view.c = view.contract = c; view.u = user; view.distribution = args.d; - - //need to select a distribution for varying orders contracts - if (c.type == db.Contract.TYPE_VARORDER && args.d == null ) { - + + // need to select a distribution for varying orders contracts + if (c.type == db.Contract.TYPE_VARORDER && args.d == null) { throw Redirect("/contractAdmin/orders/" + c.id); - - }else { - - //members of the group + } else { + // members of the group view.users = app.user.amap.getMembersFormElementData(); - - var userOrders = new Array<{order:db.UserContract,product:db.Product}>(); + + var userOrders = new Array<{order:db.UserContract, product:db.Product}>(); var products = c.getProducts(false); - - for ( p in products) { - var ua = { order:null, product:p }; - - var order : db.UserContract = null; + + for (p in products) { + var ua = {order: null, product: p}; + + var order:db.UserContract = null; if (c.type == db.Contract.TYPE_VARORDER) { - order = db.UserContract.manager.select($user == user && $productId == p.id && $distributionId==args.d.id, true); - }else { + order = db.UserContract.manager.select($user == user && $productId == p.id && $distributionId == args.d.id, true); + } else { order = db.UserContract.manager.select($user == user && $productId == p.id, true); } - - if (order != null) ua.order = order; + + if (order != null) + ua.order = order; userOrders.push(ua); } - - //form check + + // form check if (checkToken()) { - - //it's a new order, the user has been defined in the form. + // it's a new order, the user has been defined in the form. if (user == null) { user = db.User.manager.get(Std.parseInt(app.params.get("user"))); - if (user == null){ + if (user == null) { var user = app.params.get("user"); - throw t._("Unable to find user #::num::",{num:user}); + throw t._("Unable to find user #::num::", {num: user}); } - if (!user.isMemberOf(app.user.amap)) throw user + " is not member of this group"; + if (!user.isMemberOf(app.user.amap)) + throw user + " is not member of this group"; } - - //get distrib if needed - var distrib : db.Distribution = null; + + // get distrib if needed + var distrib:db.Distribution = null; if (c.type == db.Contract.TYPE_VARORDER) { distrib = db.Distribution.manager.get(Std.parseInt(app.params.get("distribution")), false); } - + var orders = []; - + for (k in app.params.keys()) { var param = app.params.get(k); if (k.substr(0, "product".length) == "product") { - - //trouve le produit dans userOrders + // trouve le produit dans userOrders var pid = Std.parseInt(k.substr("product".length)); var uo = Lambda.find(userOrders, function(uo) return uo.product.id == pid); - if (uo == null) throw t._("Unable to find product ::pid::", {pid:pid}); - - //user2 ? - var user2 : db.User = null; + if (uo == null) + throw t._("Unable to find product ::pid::", {pid: pid}); + + // user2 ? + var user2:db.User = null; var invert = false; if (app.params.get("user2" + pid) != null && app.params.get("user2" + pid) != "0") { - user2 = db.User.manager.get(Std.parseInt(app.params.get("user2"+pid))); + user2 = db.User.manager.get(Std.parseInt(app.params.get("user2" + pid))); if (user2 == null) { var user = app.params.get("user2"); - throw t._("Unable to find user #::num::",{num:user}); + throw t._("Unable to find user #::num::", {num: user}); } - if (!user2.isMemberOf(app.user.amap)) throw t._("::user:: is not part of this group",{user:user2}); - if (user.id == user2.id) throw t._("Both selected accounts must be different ones"); - + if (!user2.isMemberOf(app.user.amap)) + throw t._("::user:: is not part of this group", {user: user2}); + if (user.id == user2.id) + throw t._("Both selected accounts must be different ones"); + invert = app.params.get("invert" + pid) == "1"; } - - //filter quantity + + // filter quantity var q = 0.0; - if (uo.product.hasFloatQt ) { + if (uo.product.hasFloatQt) { param = StringTools.replace(param, ",", "."); q = Std.parseFloat(param); - }else { + } else { q = Std.parseInt(param); } - if(q==null) continue; //ignore bad qt input - - //record order + if (q == null) + continue; // ignore bad qt input + + // record order if (uo.order != null) { - //existing record + // existing record var o = OrderService.edit(uo.order, q, (app.params.get("paid" + pid) == "1"), user2, invert); - if (o != null) orders.push(o); - }else { - //new record - var o = OrderService.make(user, q, uo.product, distrib == null ? null : distrib.id, (app.params.get("paid" + pid) == "1"), user2, invert); - if (o != null) orders.push(o); + if (o != null) + orders.push(o); + } else { + // new record + var o = OrderService.make(user, q, uo.product, distrib == null ? null : distrib.id, (app.params.get("paid" + pid) == "1"), user2, + invert); + if (o != null) + orders.push(o); } } } - + app.event(MakeOrder(orders)); db.Operation.onOrderConfirm(orders); - + if (distrib != null) { - throw Ok("/contractAdmin/orders/" + c.id +"?d="+distrib.id, t._("The order has been updated")); - }else { - throw Ok("/contractAdmin/orders/" + c.id, t._("The order has been updated")); + throw Ok("/contractAdmin/orders/" + c.id + "?d=" + distrib.id, t._("The order has been updated")); + } else { + throw Ok("/contractAdmin/orders/" + c.id, t._("The order has been updated")); } } view.userOrders = userOrders; } } - }