dolibarr  16.0.5
thirdparties_services_expired.modules.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2005-2010 Laurent Destailleur <eldy@users.sourceforge.net>
3  *
4 * This file is an example to follow to add your own email selector inside
5 * the Dolibarr email tool.
6 * Follow instructions given in README file to know what to change to build
7 * your own emailing list selector.
8 * Code that need to be changed in this file are marked by "CHANGE THIS" tag.
9 */
10 
16 include_once DOL_DOCUMENT_ROOT.'/core/modules/mailings/modules_mailings.php';
17 require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
18 
19 
24 {
25  public $name = 'DolibarrContractsLinesExpired';
26  // This label is used if no translation is found for key XXX neither MailingModuleDescXXX where XXX=name is found
27  public $desc = 'Third parties with expired contract\'s lines';
28  public $require_admin = 0;
29 
30  public $require_module = array('contrat');
31 
32  public $enabled = '$conf->societe->enabled';
33 
37  public $picto = 'company';
38 
42  public $db;
43 
44  public $arrayofproducts = array();
45 
46 
52  public function __construct($db)
53  {
54  global $conf;
55 
56  $this->db = $db;
57 
58  $this->arrayofproducts = array();
59 
60  // List of services
61  $sql = "SELECT ref FROM ".MAIN_DB_PREFIX."product";
62  $sql .= " WHERE entity IN (".getEntity('product').")";
63  if (empty($conf->global->CONTRACT_SUPPORT_PRODUCTS)) {
64  $sql .= " AND fk_product_type = 1"; // By default, only services
65  }
66  $sql .= " ORDER BY ref";
67  $result = $this->db->query($sql);
68  if ($result) {
69  $num = $this->db->num_rows($result);
70  dol_syslog("dolibarr_services_expired.modules.php:mailing_dolibarr_services_expired ".$num." services found");
71 
72  $i = 0;
73  while ($i < $num) {
74  $obj = $this->db->fetch_object($result);
75  $i++;
76  $this->arrayofproducts[$i] = $obj->ref;
77  }
78  } else {
79  dol_print_error($this->db);
80  }
81  }
82 
83 
84  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
91  public function add_to_target($mailing_id)
92  {
93  // phpcs:enable
94  $key = GETPOST('filter', 'int');
95 
96  $cibles = array();
97  $j = 0;
98 
99  $product = '';
100  if ($key == '0') {
101  $this->error = "Error: You must choose a filter";
102  $this->errors[] = $this->error;
103  return $this->error;
104  }
105 
106  $product = $this->arrayofproducts[$key];
107 
108  $now = dol_now();
109 
110  // La requete doit retourner: id, email, name
111  $sql = "SELECT s.rowid as id, s.email, s.nom as name, cd.rowid as cdid, cd.date_ouverture, cd.date_fin_validite, cd.fk_contrat";
112  $sql .= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."contrat as c";
113  $sql .= ", ".MAIN_DB_PREFIX."contratdet as cd, ".MAIN_DB_PREFIX."product as p";
114  $sql .= " WHERE s.entity IN (".getEntity('societe').")";
115  $sql .= " AND s.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing=".((int) $mailing_id).")";
116  $sql .= " AND s.rowid = c.fk_soc AND cd.fk_contrat = c.rowid AND s.email != ''";
117  $sql .= " AND cd.statut= 4 AND cd.fk_product=p.rowid AND p.ref = '".$this->db->escape($product)."'";
118  $sql .= " AND cd.date_fin_validite < '".$this->db->idate($now)."'";
119  $sql .= " ORDER BY s.email";
120 
121  // Stocke destinataires dans cibles
122  $result = $this->db->query($sql);
123  if ($result) {
124  $num = $this->db->num_rows($result);
125  $i = 0;
126 
127  dol_syslog(get_class($this)."::add_to_target ".$num." targets found");
128 
129  $old = '';
130  while ($i < $num) {
131  $obj = $this->db->fetch_object($result);
132  if ($old <> $obj->email) {
133  $cibles[$j] = array(
134  'email' => $obj->email,
135  'lastname' => $obj->name, // For thirdparties, lastname must be name
136  'firstname' => '', // For thirdparties, firstname is ''
137  'other' =>
138  ('DateStart='.dol_print_date($this->db->jdate($obj->date_ouverture), 'day')).';'.
139  ('DateEnd='.dol_print_date($this->db->jdate($obj->date_fin_validite), 'day')).';'.
140  ('Contract='.$obj->fk_contrat).';'.
141  ('ContactLine='.$obj->cdid),
142  'source_url' => $this->url($obj->id),
143  'source_id' => $obj->id,
144  'source_type' => 'thirdparty'
145  );
146  $old = $obj->email;
147  $j++;
148  }
149 
150  $i++;
151  }
152  } else {
153  dol_syslog($this->db->lasterror());
154  $this->error = $this->db->lasterror();
155  return -1;
156  }
157 
158  // ----- Your code end here -----
159 
160  return parent::addTargetsToDatabase($mailing_id, $cibles);
161  }
162 
163 
172  public function getSqlArrayForStats()
173  {
174 
175  //var $statssql=array();
176  //$this->statssql[0]="SELECT field1 as label, count(distinct(email)) as nb FROM mytable WHERE email IS NOT NULL";
177 
178  return array();
179  }
180 
181 
190  public function getNbOfRecipients($sql = '')
191  {
192  $now = dol_now();
193 
194  // Example: return parent::getNbOfRecipients("SELECT count(*) as nb from dolibarr_table");
195  // Example: return 500;
196  $sql = "SELECT count(*) as nb";
197  $sql .= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."contrat as c";
198  $sql .= ", ".MAIN_DB_PREFIX."contratdet as cd, ".MAIN_DB_PREFIX."product as p";
199  $sql .= " WHERE s.entity IN (".getEntity('societe').")";
200  $sql .= " AND s.rowid = c.fk_soc AND cd.fk_contrat = c.rowid AND s.email != ''";
201  $sql .= " AND cd.statut= 4 AND cd.fk_product=p.rowid";
202  $sql .= " AND p.ref IN (".$this->db->sanitize("'".join("','", $this->arrayofproducts)."'", 1).")";
203  $sql .= " AND cd.date_fin_validite < '".$this->db->idate($now)."'";
204 
205  $a = parent::getNbOfRecipients($sql);
206 
207  return $a;
208  }
209 
216  public function formFilter()
217  {
218  global $langs;
219 
220  $s = '<select id="filter_services_expired" name="filter" class="flat">';
221  if (count($this->arrayofproducts)) {
222  $s .= '<option value="-1">'.$langs->trans("ProductOrService").'</option>';
223  } else {
224  $s .= '<option value="0">'.$langs->trans("ContactsAllShort").'</option>';
225  }
226  foreach ($this->arrayofproducts as $key => $val) {
227  $s .= '<option value="'.$key.'">'.$val.'</option>';
228  }
229  $s .= '</select>';
230  $s .= ajax_combobox("filter_services_expired");
231 
232  return $s;
233  }
234 
235 
242  public function url($id)
243  {
244  return '<a href="'.DOL_URL_ROOT.'/societe/card.php?socid='.$id.'">'.img_object('', "company").'</a>';
245  }
246 }
ajax_combobox
ajax_combobox($htmlname, $events=array(), $minLengthToAutocomplete=0, $forcefocus=0, $widthTypeOfAutocomplete='resolve', $idforemptyvalue='-1')
Convert a html select field into an ajax combobox.
Definition: ajax.lib.php:438
db
$conf db
API class for accounts.
Definition: inc.php:41
mailing_thirdparties_services_expired\__construct
__construct($db)
Constructor.
Definition: thirdparties_services_expired.modules.php:52
GETPOST
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Definition: functions.lib.php:484
dol_print_error
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
Definition: functions.lib.php:4844
MailingTargets
Parent class of emailing target selectors modules.
Definition: modules_mailings.php:32
mailing_thirdparties_services_expired\add_to_target
add_to_target($mailing_id)
This is the main function that returns the array of emails.
Definition: thirdparties_services_expired.modules.php:91
mailing_thirdparties_services_expired
Class to offer a selector of emailing targets with Rule 'services expired'.
Definition: thirdparties_services_expired.modules.php:23
mailing_thirdparties_services_expired\getSqlArrayForStats
getSqlArrayForStats()
On the main mailing area, there is a box with statistics.
Definition: thirdparties_services_expired.modules.php:172
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1603
mailing_thirdparties_services_expired\formFilter
formFilter()
This is to add a form filter to provide variant of selector If used, the HTML select must be called "...
Definition: thirdparties_services_expired.modules.php:216
img_object
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
Definition: functions.lib.php:4211
dol_now
dol_now($mode='auto')
Return date for now.
Definition: functions.lib.php:2845
mailing_thirdparties_services_expired\getNbOfRecipients
getNbOfRecipients($sql='')
Return here number of distinct emails returned by your selector.
Definition: thirdparties_services_expired.modules.php:190
mailing_thirdparties_services_expired\url
url($id)
Can include an URL link on each record provided by selector shown on target page.
Definition: thirdparties_services_expired.modules.php:242