@ -1,48 +1,43 @@
package controller ;
import sugoi . db . Cache ;
import sugoi . Web ;
import sugoi . mail . Mail ;
import Common ;
using Lambda ;
using tools . DateTool ;
class Cron extends Controller
{
public function doDefault ( )
{
}
class Cron extends Controller {
public function doDefault ( ) { }
/ * *
* CLI only en prod
* /
function canRun ( ) {
if ( App . current . user != null && App . current . user . isAdmin ( ) ) {
if ( App . current . user != null && App . current . user . isAdmin ( ) ) {
return true ;
} e lse if ( App . config . DEBUG ) {
} e lse if ( App . config . DEBUG ) {
return true ;
} e lse {
} e lse {
if ( Web . isModNeko ) {
Sys . print ( " o n l y C L I . " ) ;
return false ;
} e lse {
} e lse {
return true ;
}
}
}
public function doMinute ( ) {
print ( " C r o n . d o M i n u t e i s c a l l e d " ) ;
if ( ! canRun ( ) ) return ;
if ( ! canRun ( ) )
return ;
app . event ( MinutelyCron ) ;
sendEmailsfromBuffer ( ) ;
}
/ * *
@ -51,54 +46,54 @@ class Cron extends Controller
* this function can b e l o c a l l y t e s t e d w i t h ` n e k o i n d e x . n c r o n / h o u r > c r o n . l o g `
* /
public function doHour ( ) {
app . event ( HourlyCron ) ;
distribNotif ( 4 , db . User . UserFlags . HasEmailNotif4h ) ; //4h before
distribNotif ( 24 , db . User . UserFlags . HasEmailNotif24h ) ; //24h before
distribNotif ( 0 , db . User . UserFlags . HasEmailNotifOuverture ) ; //on command open
distribNotif ( 4 , db . User . UserFlags . HasEmailNotif4h ) ; // 4h before
distribNotif ( 24 , db . User . UserFlags . HasEmailNotif24h ) ; // 24h before
distribNotif ( 0 , db . User . UserFlags . HasEmailNotifOuverture ) ; // on command open
distribValidationNotif ( ) ;
//sendOrdersByProductWhenOrdersClose();
// sendOrdersByProductWhenOrdersClose();
}
public function doDaily ( ) {
if ( ! canRun ( ) ) return ;
if ( ! canRun ( ) )
return ;
app . event ( DailyCron ) ;
//ERRORS MONITORING
// ERRORS MONITORING
var n = Date . now ( ) ;
var yest24h = new Date ( n . getFullYear ( ) , n . getMonth ( ) , n . getDate ( ) , 0 , 0 , 0 ) ;
var yest0h = DateTools . delta ( yest24h , - 1000 * 60 * 60 * 24 ) ;
var errors = sugoi . db . Error . manager . search ( $date < yest24h && $date > yest0h ) ;
var errors = sugoi . db . Error . manager . search ( $date < yest24h && $date > yest0h ) ;
if ( errors . length > 0 ) {
var report = new StringBuf ( ) ;
report . add ( " < h 1 > " + App . config . NAME + " : E R R O R S < / h 1 > " ) ;
for ( e in errors ) {
report . add ( " < d i v > < p r e > " + e . error + " a t U R L " + e . url + " ( u s e r : " + ( e . user != null ? e . user . toString ( ) : " n o n e " ) + " , I P : " + e . ip + " ) < / p r e > < / d i v > < h r / > " ) ;
report . add ( " < d i v > < p r e > " + e . error + " a t U R L " + e . url + " ( u s e r : " + ( e . user != null ? e . user . toString ( ) : " n o n e " ) + " , I P : " + e . ip
+ " ) < / p r e > < / d i v > < h r / > " ) ;
}
var m = new Mail ( ) ;
m . setSender ( App . config . get ( " d e f a u l t _ e m a i l " ) , " C a g e t t e . n e t " ) ;
m . setSender ( App . config . get ( " d e f a u l t _ e m a i l " ) , " C a g e t t e . n e t " ) ;
m . addRecipient ( App . config . get ( " w e b m a s t e r _ e m a i l " ) ) ;
m . setSubject ( App . config . NAME + " E r r o r s " ) ;
m . setHtmlBody ( app . processTemplate ( " m a i l / m e s s a g e . m t t " , { text : report . toString ( ) } ) ) ;
m . setSubject ( App . config . NAME + " E r r o r s " ) ;
m . setHtmlBody ( app . processTemplate ( " m a i l / m e s s a g e . m t t " , { text : report . toString ( ) } ) ) ;
App . sendMail ( m ) ;
}
// DEMO CONTRATS deletion after 7 days ( see controller.Group.doCreate() )
db . Contract . manager . delete ( $name == " C o n t r a t A M A P M a r a î c h e r E x e m p l e "
& & $startDate < DateTools . delta ( Date . now ( ) , - 1000.0 * 60 * 60 * 24 * 7 ) ) ;
db . Contract . manager . delete ( $name == " C o n t r a t P o u l e t E x e m p l e "
& & $startDate < DateTools . delta ( Date . now ( ) , - 1000.0 * 60 * 60 * 24 * 7 ) ) ;
//DEMO CONTRATS deletion after 7 days ( see controller.Group.doCreate() )
db . Contract . manager . delete ( $name == " C o n t r a t A M A P M a r a î c h e r E x e m p l e " && $startDate < DateTools . delta ( Date . now ( ) , - 1000.0 * 60 * 60 * 24 * 7 ) ) ;
db . Contract . manager . delete ( $name == " C o n t r a t P o u l e t E x e m p l e " && $startDate < DateTools . delta ( Date . now ( ) , - 1000.0 * 60 * 60 * 24 * 7 ) ) ;
//Old Messages cleaning
// Old Messages cleaning
db . Message . manager . delete ( $date < DateTools . delta ( Date . now ( ) , - 1000.0 * 60 * 60 * 24 * 30 * 6 ) ) ;
//DB cleaning : I dont know how, but some people have empty string emails...
// DB cleaning : I dont know how, but some people have empty string emails...
/ * for ( u in db . User . manager . search ( $email == " " , true ) ) {
u . email = Std . random ( 9999 ) + " @ c a g e t t e . n e t " ;
u . update ( ) ;
@ -107,8 +102,6 @@ class Cron extends Controller
u . email2 = null ;
u . update ( ) ;
} * /
}
/ * *
@ -117,28 +110,28 @@ class Cron extends Controller
* @ param flag
* /
function distribNotif ( hour : Int , flag : db . User . UserFlags ) {
//trouve les distrib qui commencent dans le nombre d'heures demandé
//on recherche celles qui commencent jusqu'à une heure avant pour ne pas en rater
var from = DateTools . delta ( Date . now ( ) , 1000.0 * 60 * 60 * ( hour - 1 ) ) ;
// trouve les distrib qui commencent dans le nombre d'heures demandé
// on recherche celles qui commencent jusqu'à une heure avant pour ne pas en rater
var from = DateTools . delta ( Date . now ( ) , 1000.0 * 60 * 60 * ( hour - 1 ) ) ;
var to = DateTools . delta ( Date . now ( ) , 1000.0 * 60 * 60 * hour ) ;
//if (App.config.DEBUG) from = DateTools.delta(from, 1000.0 * 60 * 60 * 24 * -30);
// if (App.config.DEBUG) from = DateTools.delta(from, 1000.0 * 60 * 60 * 24 * -30);
// dans le cas HasEmailNotifOuverture la date à prendre est le orderStartDate
// et non pas date qui est la date de la distribution
var distribs;
if ( db . User . UserFlags . HasEmailNotifOuverture == flag )
distribs = db . Distribution . manager . search ( $orderStartDate >= from && $orderStartDate <= to , false ) ;
if ( db . User . UserFlags . HasEmailNotifOuverture == flag )
distribs = db . Distribution . manager . search ( $orderStartDate >= from && $orderStartDate <= to , false ) ;
e lse
distribs = db . Distribution . manager . search ( $date >= from && $date <= to , false ) ;
distribs = db . Distribution . manager . search ( $date >= from && $date <= to , false ) ;
//Sys.print("distribNotif "+hour+" from "+from+" to "+to+"<br/>\n");
// Sys.print("distribNotif "+hour+" from "+from+" to "+to+"<br/>\n");
//on s'arrete immédiatement si aucune distibution trouvée
if ( distribs . length == 0 ) return ;
// on s'arrete immédiatement si aucune distibution trouvée
if ( distribs . length == 0 )
return ;
//cherche plus tard si on a pas une "grappe" de distrib
// cherche plus tard si on a pas une "grappe" de distrib
/ * while ( true ) {
var extraDistribs ;
if ( db . User . UserFlags . HasEmailNotifOuverture != flag )
@ -155,9 +148,9 @@ class Cron extends Controller
}
} * /
//on vérifie dans le cache du jour que ces distrib n'ont pas deja été traitées lors d'un cron précédent
var cacheId = Date . now ( ) . toString ( ) . substr ( 0 , 10 ) + Std . string ( flag ) ;
var dist : Array < Int > = sugoi . db . Cache . get ( cacheId ) ;
// on vérifie dans le cache du jour que ces distrib n'ont pas deja été traitées lors d'un cron précédent
var cacheId = Date . now ( ) . toString ( ) . substr ( 0 , 10 ) + Std . string ( flag ) ;
var dist: Array < Int > = sugoi . db . Cache . get ( cacheId ) ;
if ( dist != null ) {
for ( d in Lambda . array ( distribs ) ) {
if ( Lambda . exists ( dist , function ( x ) return x == d . id ) ) {
@ -165,37 +158,42 @@ class Cron extends Controller
distribs . remove ( d ) ;
}
}
} e lse {
} e lse {
dist = [ ] ;
}
//toutes les distribs trouvées ont deja été traitées
if ( distribs . length == 0 ) return ;
// toutes les distribs trouvées ont deja été traitées
if ( distribs . length == 0 )
return ;
//stocke cache
for ( d in distribs ) dist . push ( d . id ) ;
// stocke cache
for ( d in distribs )
dist . push ( d . id ) ;
Cache . set ( cacheId , dist , 24 * 60 * 60 ) ;
//We have now the distribs we want to notify about.
var distribsByContractId = new Map < Int , db . Distribution > ( ) ;
// We have now the distribs we want to notify about.
var distribsByContractId = new Map < Int , db . Distribution > ( ) ;
for ( d in distribs ) {
if ( d == null || d . contract == null ) continue ;
if ( d == null || d . contract == null )
continue ;
distribsByContractId . set ( d . contract . id , d ) ;
}
//Boucle sur les distributions pour gerer le cas de plusieurs distributions le même jour sur le même contrat
// Boucle sur les distributions pour gerer le cas de plusieurs distributions le même jour sur le même contrat
var orders = [ ] ;
for ( d in distribs ) {
if ( d == null || d . contract == null ) continue ;
//get orders for both type of contracts
for ( x in d . contract . getOrders ( d ) ) orders . push ( x ) ;
if ( d == null || d . contract == null )
continue ;
// get orders for both type of contracts
for ( x in d . contract . getOrders ( d ) )
orders . push ( x ) ;
}
/ *
* Group orders by users - group to receive separate emails by groups for t h e s a m e u s e r .
* Map key is $userId - $groupId
* /
var users = new Map < String , {
var users = new Map < String , {
user : db . User ,
distrib : db . Distribution ,
products : Array < db . UserContract > ,
@ -203,77 +201,92 @@ class Cron extends Controller
} > ( ) ;
for ( o in orders ) {
var x = users . get ( o . user . id + " - " + o . product . contract . amap . id ) ;
if ( x == null ) x = { user : o . user , distrib : null , products : [ ] , vendors : [ ] } ;
var x = users . get ( o . user . id + " - " + o . product . contract . amap . id ) ;
if ( x == null )
x = {
user : o . user ,
distrib : null ,
products : [ ] ,
vendors : [ ]
} ;
x . distrib = distribsByContractId . get ( o . product . contract . id ) ;
x . products . push ( o ) ;
users . set ( o . user . id + " - " + o . product . contract . amap . id , x ) ;
//trace (o.userId+"-"+o.product.contract.amap.id, x);Sys.print("<br/>\n");
users . set ( o . user . id + " - " + o . product . contract . amap . id , x ) ;
// trace (o.userId+"-"+o.product.contract.amap.id, x);Sys.print("<br/>\n");
// Prévenir également le deuxième user en cas des commandes alternées
if ( o . user2 != null ) {
var x = users . get ( o . user2 . id + " - " + o . product . contract . amap . id ) ;
if ( x == null ) x = { user : o . user2 , distrib : null , products : [ ] , vendors : [ ] } ;
var x = users . get ( o . user2 . id + " - " + o . product . contract . amap . id ) ;
if ( x == null )
x = {
user : o . user2 ,
distrib : null ,
products : [ ] ,
vendors : [ ]
} ;
x . distrib = distribsByContractId . get ( o . product . contract . id ) ;
x . products . push ( o ) ;
users . set ( o . user2 . id + " - " + o . product . contract . amap . id , x ) ;
//trace (o.user2.id+"-"+o.product.contract.amap.id, x);Sys.print("<br/>\n");
users . set ( o . user2 . id + " - " + o . product . contract . amap . id , x ) ;
// trace (o.user2.id+"-"+o.product.contract.amap.id, x);Sys.print("<br/>\n");
}
}
//remove zero qt orders
for ( k in users . keys ( ) ) {
// remove zero qt orders
for ( k in users . keys ( ) ) {
var x = users . get ( k ) ;
var total = 0.0 ;
for ( o in x . products ) total += o . quantity ;
if ( total == 0.0 ) users . remove ( k ) ;
for ( o in x . products )
total += o . quantity ;
if ( total == 0.0 )
users . remove ( k ) ;
}
// Dans le cas de l'ouverture de commande, ce sont tous les users qu'il faut intégrer
if ( db . User . UserFlags . HasEmailNotifOuverture == flag )
{
if ( db . User . UserFlags . HasEmailNotifOuverture == flag ) {
for ( d in distribs ) {
var memberList = d . contract . amap . getMembers ( ) ;
for ( u in memberList ) {
var x = users . get ( u . id + " - " + d . contract . amap . id ) ;
if ( x == null ) x = { user : u , distrib : null , products : [ ] , vendors : [ ] } ;
var x = users . get ( u . id + " - " + d . contract . amap . id ) ;
if ( x == null )
x = {
user : u ,
distrib : null ,
products : [ ] ,
vendors : [ ]
} ;
x . distrib = distribsByContractId . get ( d . contract . id ) ;
x . vendors . push ( d . contract . vendor ) ;
users . set ( u . id + " - " + d . contract . amap . id , x ) ;
//print(u.id+"-"+d.contract.amap.id, x);
users . set ( u . id + " - " + d . contract . amap . id , x ) ;
// print(u.id+"-"+d.contract.amap.id, x);
}
}
}
for ( u in users ) {
if ( u . user . flags . has ( flag ) ) {
for ( u in users ) {
if ( u . user . flags . has ( flag ) ) {
if ( u . user . email != null ) {
var group = u . distrib . contract . amap ;
this . t = sugoi . i18n . Locale . init ( u . user . lang ) ; //switch to the user language
this . t = sugoi . i18n . Locale . init ( u . user . lang ) ; // switch to the user language
var text;
if ( db . User . UserFlags . HasEmailNotifOuverture == flag )
{
//order opening notif
text = t . _ ( " O p e n i n g o f o r d e r s f o r t h e d e l i v e r y o f < b > : : d a t e : : < / b > " , { date : view . hDate ( u . distrib . date ) } ) ;
if ( db . User . UserFlags . HasEmailNotifOuverture == flag ) {
// order opening notif
text = t . _ ( " O p e n i n g o f o r d e r s f o r t h e d e l i v e r y o f < b > : : d a t e : : < / b > " , { date : view . hDate ( u . distrib . date ) } ) ;
text += " < b r / > " ;
text += t . _ ( " T h e f o l l o w i n g s u p p l i e r s a r e i n v o l v e d : " ) ;
text += " < b r / > < u l > " ;
for ( v in u . vendors ) {
for ( v in u . vendors ) {
text += " < l i > " + v + " < / l i > " ;
}
text += " < / u l > " ;
} e lse {
//Distribution notif to the users
} e lse {
// Distribution notif to the users
var d = u . distrib ;
text = t . _ ( " D o n o t f o r g e t t h e d e l i v e r y o n < b > : : d a y : : < / b > f r o m : : f r o m : : t o : : t o : : < b r / > " , { day : view . dDate ( d . date ) , from : view . hHour ( d . date ) , to : view . hHour ( d . end ) } ) ;
text = t . _ ( " D o n o t f o r g e t t h e d e l i v e r y o n < b > : : d a y : : < / b > f r o m : : f r o m : : t o : : t o : : < b r / > " ,
{ day : view . dDate ( d . date ) , from : view . hHour ( d . date ) , to : view . hHour ( d . end ) } ) ;
text += t . _ ( " Y o u r p r o d u c t s t o c o l l e c t : " ) + " < b r / > < u l > " ;
for ( p in u . products ) {
text += " < l i > " + p . quantity + " x " + p . product . getName ( ) ;
for ( p in u . products ) {
text += " < l i > " + p . quantity + " x " + p . product . getName ( ) ;
// Gerer le cas des contrats en alternance
if ( p . user2 != null ) {
text += " " + t . _ ( " a l t e r n a t e d w i t h " ) + " " ;
@ -291,36 +304,35 @@ class Cron extends Controller
text += t . _ ( " < b > W a r n i n g : y o u a r e i n c h a r g e o f t h e d e l i v e r y ! D o n o t f o r g e t t o p r i n t t h e a t t e n d a n c e s h e e t . < / b > " ) ;
}
try {
try {
var m = new Mail ( ) ;
m . setSender ( App . config . get ( " d e f a u l t _ e m a i l " ) , " C a g e t t e . n e t " ) ;
if ( group . contact != null ) m . setReplyTo ( group . contact . email , group . name ) ;
if ( group . contact != null )
m . setReplyTo ( group . contact . email , group . name ) ;
m . addRecipient ( u . user . email , u . user . getName ( ) ) ;
if ( u . user . email2 != null ) m . addRecipient ( u . user . email2 ) ;
m . setSubject ( group . name + " : " + t . _ ( " D i s t r i b u t i o n o n : : d a t e : : " , { date : app . view . hDate ( u . distrib . date ) } ) ) ;
m . setHtmlBody ( app . processTemplate ( " m a i l / m e s s a g e . m t t " , { text : text , group : group } ) ) ;
App . sendMail ( m , u . distrib . contract . amap ) ;
} c atch ( e : D y n a m i c ) {
app . logError ( e ) ; //email could be invalid
if ( u . user . email2 != null )
m . addRecipient ( u . user . email2 ) ;
m . setSubject ( group . name + " : " + t . _ ( " D i s t r i b u t i o n o n : : d a t e : : " , { date : app . view . hDate ( u . distrib . date ) } ) ) ;
m . setHtmlBody ( app . processTemplate ( " m a i l / m e s s a g e . m t t " , { text : text , group : group } ) ) ;
App . sendMail ( m , u . distrib . contract . amap ) ;
} c atch ( e : D y n a m i c ) {
app . logError ( e ) ; // email could be invalid
}
}
}
}
}
/ * *
* Check if t h e r e i s a m u l t i - d i s t r i b t o v a l i d a t e .
*
* Autovalidate it after 10 days
* /
function distribValidationNotif ( ) {
function distribValidationNotif ( ) {
var now = Date . now ( ) ;
var from = now . setHourMinute ( now . getHours ( ) , 0 ) ;
var to = now . setHourMinute ( now . getHours ( ) + 1 , 0 ) ;
var from = now . setHourMinute ( now . getHours ( ) , 0 ) ;
var to = now . setHourMinute ( now . getHours ( ) + 1 , 0 ) ;
var explain = t . _ ( " < p > T h i s s t e p i s i m p o r t a n t i n o r d e r t o : < / p > " ) ;
explain += t . _ ( " < u l > < l i > U p d a t e o r d e r s i f d e l i v e r e d q u a n t i t i e s a r e d i f f e r e n t f r o m o r d e r e d q u a n t i t i e s < / l i > " ) ;
@ -329,12 +341,12 @@ class Cron extends Controller
/ *
* warn administrator if a d i s t r i b u t i o n j u s t e n d e d
* /
var ds = db . Distribution . manager . search ( ! $validated && ( $end >= from ) && ( $end < to ) , false ) ;
var ds = db . Distribution . manager . search ( ! $validated && ( $end >= from ) && ( $end < to ) , false ) ;
for ( d in Lambda . array ( ds ) ) {
if ( d . contract . type != db . Contract . TYPE_VARORDER ) {
for ( d in Lambda . array ( ds ) ) {
if ( d . contract . type != db . Contract . TYPE_VARORDER ) {
ds . remove ( d ) ;
} e lse if ( ! d . contract . amap . hasPayments ( ) ) {
} e lse if ( ! d . contract . amap . hasPayments ( ) ) {
ds . remove ( d ) ;
}
}
@ -342,15 +354,16 @@ class Cron extends Controller
var ds = tools . ObjectListTool . deduplicateDistribsByKey ( ds ) ;
var view = App . current . view ;
for ( d in ds ) {
for ( d in ds ) {
// var subj = "["+d.contract.amap.name+"] " + t._("Validation of the ::date:: distribution",{date:view.hDate(d.date)});
var subj = t . _ ( " [ : : g r o u p : : ] V a l i d a t i o n o f t h e : : d a t e : : d i s t r i b u t i o n " , { group : d . contract . amap . name , date : view . hDate ( d . date ) } ) ;
var subj = t . _ ( " [ : : g r o u p : : ] V a l i d a t i o n o f t h e : : d a t e : : d i s t r i b u t i o n " , { group : d . contract . amap . name , date : view . hDate ( d . date ) } ) ;
var url = " h t t p : / / " + App . config . HOST + " / d i s t r i b u t i o n / v a l i d a t e / " + d . date . toString ( ) . substr ( 0 , 10 ) + " / " + d . place . id ;
var url = " h t t p : / / " + App . config . HOST + " / d i s t r i b u t i o n / v a l i d a t e / " + d . date . toString ( ) . substr ( 0 , 10 ) + " / " + d . place . id ;
var html = t . _ ( " < p > Y o u r d i s t r i b u t i o n j u s t f i n i s h e d , d o n ' t f o r g e t t o < b > v a l i d a t e < / b > i t < / p > " ) ;
html += explain ;
html += t . _ ( " < p > < a h r e f = ' : : d i s t r i b u r l : : ' > C l i c k h e r e t o v a l i d a t e t h e d i s t r i b u t i o n < / a > ( Y o u m u s t b e c o n n e c t e d t o y o u r g r o u p C a g e t t e ) " , { distriburl : url } ) ;
html += t . _ ( " < p > < a h r e f = ' : : d i s t r i b u r l : : ' > C l i c k h e r e t o v a l i d a t e t h e d i s t r i b u t i o n < / a > ( Y o u m u s t b e c o n n e c t e d t o y o u r g r o u p C a g e t t e ) " ,
{ distriburl : url } ) ;
App . quickMail ( d . contract . amap . contact . email , subj , html ) ;
}
@ -359,66 +372,67 @@ class Cron extends Controller
* warn administrator if a d i s t r i b u t i o n e n d e d 3 d a y s a g o
* /
var from = now . setHourMinute ( now . getHours ( ) , 0 ) . deltaDays ( - 3 ) ;
var to = now . setHourMinute ( now . getHours ( ) + 1 , 0 ) . deltaDays ( - 3 ) ;
var from = now . setHourMinute ( now . getHours ( ) , 0 ) . deltaDays ( - 3 ) ;
var to = now . setHourMinute ( now . getHours ( ) + 1 , 0 ) . deltaDays ( - 3 ) ;
//warn administrator if a distribution just ended
var ds = db . Distribution . manager . search ( ! $validated && ( $end >= from ) && ( $end < to ) , false ) ;
// warn administrator if a distribution just ended
var ds = db . Distribution . manager . search ( ! $validated && ( $end >= from ) && ( $end < to ) , false ) ;
for ( d in Lambda . array ( ds ) ) {
if ( d . contract . type != db . Contract . TYPE_VARORDER ) {
for ( d in Lambda . array ( ds ) ) {
if ( d . contract . type != db . Contract . TYPE_VARORDER ) {
ds . remove ( d ) ;
} e lse if ( ! d . contract . amap . hasPayments ( ) ) {
} e lse if ( ! d . contract . amap . hasPayments ( ) ) {
ds . remove ( d ) ;
}
}
var ds = tools . ObjectListTool . deduplicateDistribsByKey ( ds ) ;
for ( d in ds ) {
for ( d in ds ) {
// var subj = d.contract.amap.name + t._(": Validation of the delivery of the ") + App.current.view.hDate(d.date);
var subj = t . _ ( " [ : : g r o u p : : ] V a l i d a t i o n o f t h e : : d a t e : : d i s t r i b u t i o n " , { group : d . contract . amap . name , date : view . hDate ( d . date ) } ) ;
var subj = t . _ ( " [ : : g r o u p : : ] V a l i d a t i o n o f t h e : : d a t e : : d i s t r i b u t i o n " , { group : d . contract . amap . name , date : view . hDate ( d . date ) } ) ;
var url = " h t t p : / / " + App . config . HOST + " / d i s t r i b u t i o n / v a l i d a t e / " + d . date . toString ( ) . substr ( 0 , 10 ) + " / " + d . place . id ;
var url = " h t t p : / / " + App . config . HOST + " / d i s t r i b u t i o n / v a l i d a t e / " + d . date . toString ( ) . substr ( 0 , 10 ) + " / " + d . place . id ;
var html = t . _ ( " < p > R e m i n d e r : y o u h a v e a d e l i v e r y t o v a l i d a t e . < / p > " ) ;
html += explain ;
html += t . _ ( " < p > < a h r e f = ' : : d i s t r i b u r l : : ' > C l i c k h e r e t o v a l i d a t e t h e d e l i v e r y < / a > ( Y o u m u s t b e c o n n e c t e d t o y o u r C a g e t t e g r o u p ) " , { distriburl : url } ) ;
html += t . _ ( " < p > < a h r e f = ' : : d i s t r i b u r l : : ' > C l i c k h e r e t o v a l i d a t e t h e d e l i v e r y < / a > ( Y o u m u s t b e c o n n e c t e d t o y o u r C a g e t t e g r o u p ) " ,
{ distriburl : url } ) ;
App . quickMail ( d . contract . amap . contact . email , subj , html ) ;
}
/ *
* Autovalidate unvalidated distributions after 10 days
* /
var from = now . setHourMinute ( now . getHours ( ) , 0 ) . deltaDays ( 0 - db . Distribution . DISTRIBUTION_VALIDATION_LIMIT ) ;
var to = now . setHourMinute ( now . getHours ( ) + 1 , 0 ) . deltaDays ( 0 - db . Distribution . DISTRIBUTION_VALIDATION_LIMIT ) ;
var from = now . setHourMinute ( now . getHours ( ) , 0 ) . deltaDays ( 0 - db . Distribution . DISTRIBUTION_VALIDATION_LIMIT ) ;
var to = now . setHourMinute ( now . getHours ( ) + 1 , 0 ) . deltaDays ( 0 - db . Distribution . DISTRIBUTION_VALIDATION_LIMIT ) ;
print ( ' A U T O V A L I D A T I O N ' ) ;
print ( ' F i n d d i s t r i b u t i o n s f r o m $ from t o $ to ' ) ;
var ds = db . Distribution . manager . search ( ! $validated && ( $end >= from ) && ( $end < to ) , true ) ;
for ( d in Lambda . array ( ds ) ) {
if ( d . contract . type != db . Contract . TYPE_VARORDER ) {
var ds = db . Distribution . manager . search ( ! $validated && ( $end >= from ) && ( $end < to ) , true ) ;
for ( d in Lambda . array ( ds ) ) {
if ( d . contract . type != db . Contract . TYPE_VARORDER ) {
ds . remove ( d ) ;
} e lse if ( ! d . contract . amap . hasPayments ( ) ) {
} e lse if ( ! d . contract . amap . hasPayments ( ) ) {
ds . remove ( d ) ;
}
}
for ( d in ds ) {
for ( d in ds ) {
print ( d . toString ( ) ) ;
service . PaymentService . validateDistribution ( d ) ;
}
//email
// email
var ds = tools . ObjectListTool . deduplicateDistribsByKey ( ds ) ;
for ( d in ds ) {
for ( d in ds ) {
// var subj = d.contract.amap.name + t._(": Validation of the distribution of the ") + App.current.view.hDate(d.date);
var subj = t . _ ( " [ : : g r o u p : : ] V a l i d a t i o n o f t h e : : d a t e : : d i s t r i b u t i o n " , { group : d . contract . amap . name , date : view . hDate ( d . date ) } ) ;
var html = t . _ ( " < p > A s y o u d i d n o t v a l i d a t e i t m a n u a l l y a f t e r 1 0 d a y s , < b r / > t h e d e l i v e r y o f t h e : : d e l i v e r y D a t e : : h a s b e e n v a l i d a t e d a u t o m a t i c a l l y < / p > " , { deliveryDate : App . current . view . hDate ( d . date ) } ) ;
var subj = t . _ ( " [ : : g r o u p : : ] V a l i d a t i o n o f t h e : : d a t e : : d i s t r i b u t i o n " , { group : d . contract . amap . name , date : view . hDate ( d . date ) } ) ;
var html = t . _ ( " < p > A s y o u d i d n o t v a l i d a t e i t m a n u a l l y a f t e r 1 0 d a y s , < b r / > t h e d e l i v e r y o f t h e : : d e l i v e r y D a t e : : h a s b e e n v a l i d a t e d a u t o m a t i c a l l y < / p > " ,
{
deliveryDate : App . current . view . hDate ( d . date )
} ) ;
App . quickMail ( d . contract . amap . contact . email , subj , html ) ;
}
}
/ * *
@ -428,28 +442,31 @@ class Cron extends Controller
* you should consider the right amount of emails to send each minute in order to avoid overlaping and getting in concurrency problems .
* ( like " S E L E C T * F R O M B u f f e r e d M a i l W H E R E s d a t e I S N U L L O R D E R B Y c d a t e D E S C L I M I T 1 0 0 F O R U P D A T E L o c k w a i t t i m e o u t e x c e e d e d ; t r y r e s t a r t i n g t r a n s a c t i o n " )
* /
function sendEmailsfromBuffer ( ) {
function sendEmailsfromBuffer ( ) {
App . log ( " S e n d E m a i l s f r o m B u f f e r " ) ;
print ( " < h 3 > S e n d E m a i l s f r o m B u f f e r < / h 3 > " ) ;
//send
for ( e in sugoi . db . BufferedMail . manager . search ( $sdate == null , { limit : 50 , orderBy : - cdate } , false ) ) {
// send
for ( e in sugoi . db . BufferedMail . manager . search ( $sdate == null , { limit : 50 , orderBy : - cdate } , false ) ) {
e . lock ( ) ;
if ( e . isSent ( ) ) continue ;
if ( e . isSent ( ) )
continue ;
App . log ( ' S e n d E m a i l i d = ${ e . id } - t i t l e = ${ e . title } ' ) ;
print ( ' # ${ e . id } - ${ e . title } ' ) ;
e . finallySend ( ) ;
Sys . sleep ( 0.1 ) ;
}
//delete old emails
var threeMonthsAgo = DateTools . delta ( Date . now ( ) , - 1000.0 * 60 * 60 * 24 * 30 * 3 ) ;
// delete old emails
var threeMonthsAgo = DateTools . delta ( Date . now ( ) , - 1000.0 * 60 * 60 * 24 * 30 * 3 ) ;
sugoi . db . BufferedMail . manager . delete ( $cdate < threeMonthsAgo ) ;
//emails that cannot be sent
for ( e in sugoi . db . BufferedMail . manager . search ( $tries > 100 , { limit : 50 , orderBy : - cdate } , true ) ) {
if ( e . sender . email != App . config . get ( " d e f a u l t _ e m a i l " ) ) {
var str = t . _ ( " S o r r y , t h e e m a i l e n t i t l e d < b > : : t i t l e : : < / b > c o u l d n o t b e s e n t . " , { title : e . title } ) ;
App . quickMail ( e . sender . email , t . _ ( " E m a i l n o t s e n t " ) , str ) ;
// emails that cannot be sent
for ( e in sugoi . db . BufferedMail . manager . search ( $tries > 100 , { limit : 50 , orderBy : - cdate } , true ) ) {
if ( e . sender . email != App . config . get ( " d e f a u l t _ e m a i l " ) ) {
var str = t . _ ( " S o r r y , t h e e m a i l e n t i t l e d < b > : : t i t l e : : < / b > c o u l d n o t b e s e n t . " , { title : e . title } ) ;
App . quickMail ( e . sender . email , t . _ ( " E m a i l n o t s e n t " ) , str ) ;
}
e . delete ( ) ;
}
@ -458,20 +475,17 @@ class Cron extends Controller
/ * *
* Email product report when orders close
* * /
function sendOrdersByProductWhenOrdersClose ( ) {
function sendOrdersByProductWhenOrdersClose ( ) {
var range = tools . DateTool . getLastHourRange ( ) ;
// Sys.println("Time is "+Date.now()+"<br/>");
// Sys.println('Find all distributions that have closed in the last hour from ${range.from} to ${range.to} \n<br/>');
for ( d in db . Distribution . manager . search ( $orderEndDate >= range . from && $orderEndDate < range . to , false ) ) {
for ( d in db . Distribution . manager . search ( $orderEndDate >= range . from && $orderEndDate < range . to , false ) ) {
service . OrderService . sendOrdersByProductReport ( d ) ;
}
}
function print ( text ) {
Sys . println ( text + " < b r / > " ) ;
function print ( text ) {
Sys . println ( text + " < b r / > " ) ;
}
}