noalyss-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Noalyss-commit] [noalyss] 16/35: Limit email by folder


From: Dany De Bontridder
Subject: [Noalyss-commit] [noalyss] 16/35: Limit email by folder
Date: Tue, 24 May 2016 21:25:39 +0000 (UTC)

sparkyx pushed a commit to branch master
in repository noalyss.

commit a7ba936a721d082d0eb8028a8ea64a2e93fb3370
Author: Dany De Bontridder <address@hidden>
Date:   Tue Apr 19 13:31:27 2016 +0200

    Limit email by folder
---
 include/ajax/ajax_admin.php        |    7 +++
 include/class/class_dossier.php    |   18 +++++---
 include/constant.php               |    9 +++-
 include/dossier.inc.php            |   19 +++++++-
 include/lib/class_sendmail.php     |   86 +++++++++++++++++++++++++++++++++++-
 include/sql/patch/ac-upgrade16.sql |   20 +++++++++
 6 files changed, 147 insertions(+), 12 deletions(-)

diff --git a/include/ajax/ajax_admin.php b/include/ajax/ajax_admin.php
index f376239..8226cb0 100644
--- a/include/ajax/ajax_admin.php
+++ b/include/ajax/ajax_admin.php
@@ -209,6 +209,13 @@ if (in_array($op, array('modele_drop', 'modele_modify', 
'folder_modify', 'folder
         echo _('Description').' : <br>';
         echo $wDesc->input('desc', $dos->get_parameter('desc'));
         echo '<br>';
+        
+        echo _('Max. email / jour (-1 = illimité)')    ;
+        $max_email_input=new INum('max_email');
+        $max_email_input->value=$dos->get_parameter('max_email');
+        $max_email_input->prec=0;
+        echo $max_email_input->input();
+        echo '<br>';
         echo HtmlInput::submit('upd', _('Modifie'));
         echo '</form>';
         $content=ob_get_clean();
diff --git a/include/class/class_dossier.php b/include/class/class_dossier.php
index 6e5f7c6..9b9fd78 100644
--- a/include/class/class_dossier.php
+++ b/include/class/class_dossier.php
@@ -43,7 +43,8 @@ class Dossier
 
     private static $variable=array("id"=>"dos_id",
         "name"=>"dos_name",
-        "desc"=>"dos_description");
+        "desc"=>"dos_description",
+        "max_email"=>'dos_email');
 
     function __construct($p_id)
     {
@@ -176,7 +177,9 @@ class Dossier
             exit('gDossier Invalide : '.$id);
     }
 
-    /*!\brief return a string to put to gDossier into a GET */
+    /*!
+     * \brief return a string to put to gDossier into a GET 
+     */
 
     static function get()
     {
@@ -247,20 +250,21 @@ class Dossier
                         array($this->dos_name, $this->dos_id))!=0)
             return;
 
-        $sql="update ac_dossier set dos_name=$1,dos_description=$2 ".
-                " where dos_id = $3";
+        $sql="update ac_dossier set dos_name=$1,dos_description=$2 
,dos_email=$3".
+                " where dos_id = $4";
         $res=$this->cn->exec_sql(
                 $sql,
                 array(trim($this->dos_name),
-            trim($this->dos_description),
-            $this->dos_id)
+                      trim($this->dos_description),
+                      $this->dos_email,
+                      $this->dos_id)
         );
     }
 
     public function load()
     {
 
-        $sql="select dos_name,dos_description from ac_dossier where dos_id=$1";
+        $sql="select dos_name,dos_description,dos_email from ac_dossier where 
dos_id=$1";
 
         $res=$this->cn->exec_sql(
                 $sql, array($this->dos_id)
diff --git a/include/constant.php b/include/constant.php
index 24b0cf5..3bff115 100644
--- a/include/constant.php
+++ b/include/constant.php
@@ -106,7 +106,7 @@ if ( ! defined ("SYSINFO_DISPLAY")) {
 }
 define ("DBVERSION",125);
 define ("MONO_DATABASE",25);
-define ("DBVERSIONREPO",16);
+define ("DBVERSIONREPO",17);
 define ('NOTFOUND','--not found--');
 define ("MAX_COMPTE",4);
 define ('MAX_ARTICLE',5);
@@ -296,4 +296,9 @@ define ("SQL_LIST_UNPAID_INVOICE_DATE_LIMIT" ,"
         where (jr_rapt is null or jr_rapt = '')
         and to_date(to_char(jr_ech,'DD.MM.YYYY'),'DD.MM.YYYY') < 
to_date(to_char(now(),'DD.MM.YYYY'),'DD.MM.YYYY')
         and jr_valid = true" );
-?>
+
+/**
+ * Exception
+ */
+// Limit email exceeds parameter
+define ('EMAIL_LIMIT',1002);
\ No newline at end of file
diff --git a/include/dossier.inc.php b/include/dossier.inc.php
index df28c47..4cabf85 100644
--- a/include/dossier.inc.php
+++ b/include/dossier.inc.php
@@ -38,8 +38,10 @@ if ( isset ($_POST['upd']) && isNumber($dossier_id) == 1 && 
$dossier_id != -1)
     $dos=new dossier($dossier_id);
     $name=HtmlInput::default_value_post('name', "--vide--");
     $desc=HtmlInput::default_value_post('desc', "--vide--");
+     $max_email=HtmlInput::default_value_post("max_email", -1);
     $dos->set_parameter('name',$name);
     $dos->set_parameter('desc',$desc);
+    $dos->set_parameter("max_email", $max_email);
     $dos->save();
 }
 echo '<div class="content" style="width:80%;margin-left:10%">';
@@ -89,12 +91,13 @@ if ( isset ($_POST["DATABASE"]) )
      * Insert new dossier with description
      */
     $desc=HtmlInput::default_value_post("DESCRIPTION","");
+    $max_email=HtmlInput::default_value_post("max_email", -1);
     try
     {
         $repo->start();
 
-        $Res=$repo->exec_sql("insert into ac_dossier(dos_name,dos_description)
-                           values ($1,$2)",array($dos,$desc));
+        $Res=$repo->exec_sql("insert into 
ac_dossier(dos_name,dos_description,dos_email)
+                           values ($1,$2)",array($dos,$desc,$max_email));
         $l_id=$repo->get_current_seq('dossier_id');
         $repo->commit();
     }
@@ -334,6 +337,18 @@ if ( $sa == 'list' )
                                          <TD><?php echo _('Description');
     ?></td><td>  <TEXTAREA  class="input_text"  COLS="60" ROWS="2" 
NAME="DESCRIPTION" ></TEXTAREA> </TD>
                                           </TR>
+<tr>
+    <td>
+    <?php echo _('Max. email / jour (-1 = illimité)')    ;?>
+    </td>
+    <td>
+    <?php
+        $max_email_input=new INum('max_email');
+        $max_email_input->prec=0;
+        echo $max_email_input->input();
+    ?>
+    </td>
+</tr>
                                           <TR> <TD><?php echo _('Modèle');
     ?></td><td>  <?php   echo $template;
     ?> </TD></TR>
diff --git a/include/lib/class_sendmail.php b/include/lib/class_sendmail.php
index 40c8c3a..ae8f297 100644
--- a/include/lib/class_sendmail.php
+++ b/include/lib/class_sendmail.php
@@ -93,7 +93,7 @@ class Sendmail
             $name = $array[$i];
             if (trim($this->$name) == "")
             {
-                throw new Exception($name ._(" est vide"));
+                throw new Exception( sprintf(_("%s est vide",$name)));
             }
         }
     }
@@ -149,11 +149,95 @@ class Sendmail
      */
     function send()
     {
+        if ( $this->can_send() == false )            throw new 
Exception(_('Email non envoyé'),EMAIL_LIMIT);
         //SEND Mail
         if (!mail($this->mailto, $this->subject, "", $this->content))
         {
             throw new Exception('send failed');
         }
+        // Increment email amount
+        $repo =new Database();
+        $date=date('Ymd');
+        $dossier=Dossier::id();
+        $this->increment_mail($repo,$dossier,$date);
+    }
+    /**
+     * Check if email can be sent from a folder
+     * @return boolean
+     */
+    function can_send() {
+        /**
+         * if send from a dossier , then  check limit of this dossier 
+         * otherwise send true
+         */
+        $dossier=HtmlInput::default_value_request("gDossier", -1);
+        if ($dossier == -1 ) return true;
+        
+        /**
+         * Compare max value in repo 
+         */
+        $repo =new Database();
+        $date=date('Ymd');
+        // get max email 
+        $max_email = $this->get_max_email($repo,$dossier);
+        // 
+        // This folder cannot send email
+        if ($max_email == 0 ) return false;
+        // 
+        // This folder has unlimited email 
+        if ($max_email == -1 ) return true;
+
+        // get email sent today from account_repository
+        $email_sent = $this->get_email_sent($repo,$dossier , $date);
+        // 
+        if (   $email_sent >= $max_email) return false;
+               
+        return true;
+    }
+    /**
+     * return max email the folder can send
+     * @param $p_repo Database 
+     * @param $p_dossier_id int
+     */
+    function get_max_email(Database $p_repo,$p_dossier_id)
+    {
+        $max_email = $p_repo->get_value("select dos_email from ac_dossier 
where dos_id=$1",
+            array($p_dossier_id));
+        return $max_email;
+    }
+    /**
+     * Return the amount of send emails for the date (YYYYMMDD)
+     * @param Database $p_repo
+     * @param $p_dossier_id int 
+     * @param $p_date string YYYYMMDD
+     */
+    function get_email_sent(Database $p_repo,$p_dossier_id,$p_date)
+    {
+        
+        $email_sent = $p_repo->get_value ('select de_sent_email from 
dossier_sent_email where dos_id = $1 and de_date=$2',
+                array($p_dossier_id,$p_date));
+        return $email_sent;
+                
+                
+    }
+    /**
+     * Add $p_amount_email to email sent
+     * @param $p_repo Database 
+     * @param $p_dossier int id of the folder (dossier.dos_id)
+     * @param $p_date string (YYYYMMDD)
+     */
+    function increment_mail(Database $p_repo,$p_dossier,$p_date)
+    {
+        $email_sent = $this->get_email_sent($p_repo,$p_dossier,$p_date);
+        if (  $email_sent == 0 ){
+                $p_repo->exec_sql("insert into 
public.dossier_sent_email(de_sent_email,dos_id,de_date) values($1,$2,$3)",
+                        array(1,$p_dossier,$p_date));
+                return;
+        } else {
+            // update + sp_emaoun_email
+            $p_repo->exec_sql("update dossier_sent_email set 
de_sent_email=de_sent_email+1 where dos_id=$1 and de_date=$2",
+                    array($p_dossier,$p_date));
+        }
     }
 
 }
diff --git a/include/sql/patch/ac-upgrade16.sql 
b/include/sql/patch/ac-upgrade16.sql
new file mode 100644
index 0000000..65933d5
--- /dev/null
+++ b/include/sql/patch/ac-upgrade16.sql
@@ -0,0 +1,20 @@
+begin;
+ALTER TABLE public.ac_dossier ADD dos_email int4 NULL DEFAULT -1;
+comment on column public.ac_dossier.dos_email is 'Max emails per day : 0 none 
, -1 unlimited or  max value';
+
+CREATE TABLE public.dossier_sent_email (
+       id serial primary key,
+       de_date varchar(8) NOT NULL,
+       de_sent_email int4 NOT NULL,
+       dos_id int4 NOT NULL
+);
+comment on table public.dossier_sent_email is 'Count the sent email by folder';
+comment on column public.dossier_sent_email.id is 'primary key';
+comment on column public.dossier_sent_email.de_date is 'Date YYYYMMDD';
+comment on column public.dossier_sent_email.de_sent_email  is 'Number of sent 
emails';
+comment on column public.dossier_sent_email.dos_id is 'Link to ac_dossier';
+
+alter table public.dossier_sent_email add constraint de_ac_dossier_fk foreign 
key (dos_id) references ac_dossier (dos_id ) on update cascade on delete 
cascade;
+alter table public.dossier_sent_email add constraint de_date_dos_id_ux unique 
(de_date,dos_id);
+select upgrade_repo(17);
+commit;



reply via email to

[Prev in Thread] Current Thread [Next in Thread]