@ -1,18 +1,15 @@
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
@ -23,7 +20,6 @@ class Cron extends Controller
} e lse if ( App . config . DEBUG ) {
return true ;
} e lse {
if ( Web . isModNeko ) {
Sys . print ( " o n l y C L I . " ) ;
return false ;
@ -34,15 +30,14 @@ class Cron extends Controller
}
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,7 +46,6 @@ 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
@ -62,9 +56,9 @@ class Cron extends Controller
// sendOrdersByProductWhenOrdersClose();
}
public function doDaily ( ) {
if ( ! canRun ( ) ) return ;
if ( ! canRun ( ) )
return ;
app . event ( DailyCron ) ;
@ -78,7 +72,8 @@ class Cron extends Controller
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 ( ) ;
@ -89,11 +84,11 @@ class Cron extends Controller
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 ) ) ;
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
db . Message . manager . delete ( $date < DateTools . delta ( Date . now ( ) , - 1000.0 * 60 * 60 * 24 * 30 * 6 ) ) ;
@ -107,8 +102,6 @@ class Cron extends Controller
u . email2 = null ;
u . update ( ) ;
} * /
}
/ * *
@ -117,7 +110,6 @@ 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 ) ) ;
@ -136,7 +128,8 @@ class Cron extends Controller
// 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 ;
if ( distribs . length == 0 )
return ;
// cherche plus tard si on a pas une "grappe" de distrib
/ * while ( true ) {
@ -170,25 +163,30 @@ class Cron extends Controller
}
// toutes les distribs trouvées ont deja été traitées
if ( distribs . length == 0 ) return ;
if ( distribs . length == 0 )
return ;
// stocke cache
for ( d in distribs ) dist . push ( d . id ) ;
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 > ( ) ;
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
var orders = [ ] ;
for ( d in distribs ) {
if ( d == null || d . contract == null ) continue ;
if ( d == null || d . contract == null )
continue ;
// get orders for both type of contracts
for ( x in d . contract . getOrders ( d ) ) orders . push ( x ) ;
for ( x in d . contract . getOrders ( d ) )
orders . push ( x ) ;
}
/ *
@ -203,9 +201,14 @@ 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 : [ ] } ;
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 ) ;
@ -214,7 +217,13 @@ class Cron extends Controller
// 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 : [ ] } ;
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 ) ;
@ -226,18 +235,25 @@ class Cron extends Controller
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 : [ ] } ;
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 ) ;
@ -247,16 +263,13 @@ class Cron extends Controller
}
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
var text;
if ( db . User . UserFlags . HasEmailNotifOuverture == flag )
{
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 / > " ;
@ -266,11 +279,11 @@ class Cron extends Controller
text += " < l i > " + v + " < / l i > " ;
}
text += " < / u l > " ;
} 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 ( ) ;
@ -294,29 +307,28 @@ class Cron extends Controller
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 ) ;
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 ( ) {
var now = Date . now ( ) ;
var from = now . setHourMinute ( now . getHours ( ) , 0 ) ;
@ -350,7 +362,8 @@ class Cron extends Controller
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 ) ;
}
@ -383,12 +396,12 @@ class Cron extends Controller
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
* /
@ -408,17 +421,18 @@ class Cron extends Controller
print ( d . toString ( ) ) ;
service . PaymentService . validateDistribution ( d ) ;
}
// email
var ds = tools . ObjectListTool . deduplicateDistribsByKey ( 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 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 ) ;
}
}
/ * *
@ -429,13 +443,16 @@ class Cron extends Controller
* ( 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 ( ) {
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 ) ) {
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 ) ;
@ -459,7 +476,6 @@ class Cron extends Controller
* Email product report when orders close
* * /
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/>');
@ -467,10 +483,8 @@ class Cron extends Controller
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 / > " ) ;
}