dolibarr 21.0.0-beta
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 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
4 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
5 *
6* This file is an example to follow to add your own email selector inside
7* the Dolibarr email tool.
8* Follow instructions given in README file to know what to change to build
9* your own emailing list selector.
10* Code that need to be changed in this file are marked by "CHANGE THIS" tag.
11*/
12
18include_once DOL_DOCUMENT_ROOT.'/core/modules/mailings/modules_mailings.php';
19require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
20
21
26{
30 public $name = 'DolibarrContractsLinesExpired';
31
35 public $desc = 'Third parties with expired contract\'s lines';
36
40 public $require_admin = 0;
41
45 public $require_module = array('contrat');
46
50 public $enabled = 'isModEnabled("societe")';
51
55 public $picto = 'company';
56
60 public $arrayofproducts = array();
61
62
68 public function __construct($db)
69 {
70 global $conf;
71
72 $this->db = $db;
73
74 $this->arrayofproducts = array();
75
76 // List of services
77 $sql = "SELECT ref FROM ".MAIN_DB_PREFIX."product";
78 $sql .= " WHERE entity IN (".getEntity('product').")";
79 if (!getDolGlobalString('CONTRACT_SUPPORT_PRODUCTS')) {
80 $sql .= " AND fk_product_type = 1"; // By default, only services
81 }
82 $sql .= " ORDER BY ref";
83 $result = $this->db->query($sql);
84 if ($result) {
85 $num = $this->db->num_rows($result);
86 dol_syslog("dolibarr_services_expired.modules.php:mailing_dolibarr_services_expired ".$num." services found");
87
88 $i = 0;
89 while ($i < $num) {
90 $obj = $this->db->fetch_object($result);
91 $i++;
92 $this->arrayofproducts[$i] = $obj->ref;
93 }
94 } else {
95 dol_print_error($this->db);
96 }
97 }
98
99
100 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
107 public function add_to_target($mailing_id)
108 {
109 global $conf;
110
111 // phpcs:enable
112 $key = GETPOSTINT('filter');
113
114 $cibles = array();
115 $j = 0;
116
117 $product = '';
118 if ($key == '0') {
119 $this->error = "Error: You must choose a filter";
120 $this->errors[] = $this->error;
121 return -1;
122 }
123
124 $product = $this->arrayofproducts[$key];
125
126 $now = dol_now();
127
128 // La requete doit retourner: id, email, name
129 $sql = "SELECT s.rowid as id, s.email, s.nom as name, cd.rowid as cdid, cd.date_ouverture as date_start_real, cd.date_fin_validite as date_end, cd.fk_contrat";
130 $sql .= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."contrat as c";
131 $sql .= ", ".MAIN_DB_PREFIX."contratdet as cd, ".MAIN_DB_PREFIX."product as p";
132 $sql .= " WHERE s.entity IN (".getEntity('societe').")";
133 $sql .= " AND s.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing=".((int) $mailing_id).")";
134 $sql .= " AND s.rowid = c.fk_soc AND cd.fk_contrat = c.rowid AND s.email != ''";
135 $sql .= " AND cd.statut= 4 AND cd.fk_product=p.rowid AND p.ref = '".$this->db->escape($product)."'";
136 $sql .= " AND cd.date_fin_validite < '".$this->db->idate($now)."'";
137 if (empty($this->evenunsubscribe)) {
138 $sql .= " AND NOT EXISTS (SELECT rowid FROM ".MAIN_DB_PREFIX."mailing_unsubscribe as mu WHERE mu.email = s.email and mu.entity = ".((int) $conf->entity).")";
139 }
140 $sql .= " ORDER BY s.email";
141
142 // Stocke destinataires dans cibles
143 $result = $this->db->query($sql);
144 if ($result) {
145 $num = $this->db->num_rows($result);
146 $i = 0;
147
148 dol_syslog(get_class($this)."::add_to_target ".$num." targets found");
149
150 $old = '';
151 while ($i < $num) {
152 $obj = $this->db->fetch_object($result);
153 if ($old != $obj->email) {
154 $cibles[$j] = array(
155 'email' => $obj->email,
156 'lastname' => $obj->name, // For thirdparties, lastname must be name
157 'firstname' => '', // For thirdparties, firstname is ''
158 'other' =>
159 ('DateStart='.dol_print_date($this->db->jdate($obj->date_start_real), 'day')).';'. // date start real
160 ('DateEnd='.dol_print_date($this->db->jdate($obj->date_end), 'day')).';'. // date end planned
161 ('Contract='.$obj->fk_contrat).';'.
162 ('ContactLine='.$obj->cdid),
163 'source_url' => $this->url($obj->id),
164 'source_id' => (int) $obj->id,
165 'source_type' => 'thirdparty'
166 );
167 $old = $obj->email;
168 $j++;
169 }
170
171 $i++;
172 }
173 } else {
174 dol_syslog($this->db->lasterror());
175 $this->error = $this->db->lasterror();
176 return -1;
177 }
178
179 // ----- Your code end here -----
180
181 return parent::addTargetsToDatabase($mailing_id, $cibles);
182 }
183
184
193 public function getSqlArrayForStats()
194 {
195
196 //var $statssql=array();
197 //$this->statssql[0]="SELECT field1 as label, count(distinct(email)) as nb FROM mytable WHERE email IS NOT NULL";
198
199 return array();
200 }
201
202
211 public function getNbOfRecipients($sql = '')
212 {
213 global $conf;
214
215 $now = dol_now();
216
217 // Example: return parent::getNbOfRecipients("SELECT count(*) as nb from dolibarr_table");
218 // Example: return 500;
219 $sql = "SELECT count(*) as nb";
220 $sql .= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."contrat as c";
221 $sql .= ", ".MAIN_DB_PREFIX."contratdet as cd, ".MAIN_DB_PREFIX."product as p";
222 $sql .= " WHERE s.entity IN (".getEntity('societe').")";
223 $sql .= " AND s.rowid = c.fk_soc AND cd.fk_contrat = c.rowid AND s.email != ''";
224 $sql .= " AND cd.statut= 4 AND cd.fk_product=p.rowid";
225 $sql .= " AND p.ref IN (".$this->db->sanitize("'".implode("','", $this->arrayofproducts)."'", 1).")";
226 $sql .= " AND cd.date_fin_validite < '".$this->db->idate($now)."'";
227 if (empty($this->evenunsubscribe)) {
228 $sql .= " AND NOT EXISTS (SELECT rowid FROM ".MAIN_DB_PREFIX."mailing_unsubscribe as mu WHERE mu.email = s.email and mu.entity = ".((int) $conf->entity).")";
229 }
230
231 $a = parent::getNbOfRecipients($sql);
232
233 return $a;
234 }
235
242 public function formFilter()
243 {
244 global $langs;
245
246 $s = img_picto('', 'product', 'class="pictofixedwidth"').'<select id="filter_services_expired" name="filter" class="flat">';
247 if (count($this->arrayofproducts)) {
248 $langs->loadLangs(array("products"));
249 $s .= '<option value="-1">'.$langs->trans("ProductOrService").'</option>';
250 } else {
251 $s .= '<option value="0">'.$langs->trans("ContactsAllShort").'</option>';
252 }
253 foreach ($this->arrayofproducts as $key => $val) {
254 $s .= '<option value="'.$key.'">'.$val.'</option>';
255 }
256 $s .= '</select>';
257 $s .= ajax_combobox("filter_services_expired");
258
259 return $s;
260 }
261
262
269 public function url($id)
270 {
271 return '<a href="'.DOL_URL_ROOT.'/societe/card.php?socid='.$id.'">'.img_object('', "company").'</a>';
272 }
273}
$id
Definition account.php:48
ajax_combobox($htmlname, $events=array(), $minLengthToAutocomplete=0, $forcefocus=0, $widthTypeOfAutocomplete='resolve', $idforemptyvalue='-1', $morecss='')
Convert a html select field into an ajax combobox.
Definition ajax.lib.php:459
Parent class of emailing target selectors modules.
Class to offer a selector of emailing targets with Rule 'services expired'.
getSqlArrayForStats()
On the main mailing area, there is a box with statistics.
getNbOfRecipients($sql='')
Return here number of distinct emails returned by your selector.
formFilter()
This is to add a form filter to provide variant of selector If used, the HTML select must be called "...
url($id)
Can include an URL link on each record provided by selector shown on target page.
add_to_target($mailing_id)
This is the main function that returns the array of emails.
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
dol_now($mode='auto')
Return date for now.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79