dolibarr 25.0.0-alpha
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-2026 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
57
63 public function __construct($db)
64 {
65 $this->db = $db;
66
67 /*
68 $this->arrayofproducts = array();
69
70 // List of services
71 $sql = "SELECT id, ref FROM ".MAIN_DB_PREFIX."product";
72 $sql .= " WHERE entity IN (".getEntity('product').")";
73 if (!getDolGlobalString('CONTRACT_SUPPORT_PRODUCTS')) {
74 $sql .= " AND fk_product_type = 1"; // By default, only services
75 }
76 $sql .= " ORDER BY ref";
77 $result = $this->db->query($sql);
78 if ($result) {
79 $num = $this->db->num_rows($result);
80 dol_syslog("dolibarr_services_expired.modules.php:mailing_dolibarr_services_expired ".$num." services found");
81
82 $i = 0;
83 while ($i < $num) {
84 $obj = $this->db->fetch_object($result);
85 $i++;
86 $this->arrayofproducts[$obj->id] = $obj->ref;
87 }
88 } else {
89 dol_print_error($this->db);
90 }
91 */
92 }
93
94
95 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
102 public function add_to_target($mailing_id)
103 {
104 global $langs;
105
106 // phpcs:enable
107 $productid = GETPOSTINT('productid');
108
109 $cibles = array();
110 $j = 0;
111
112 if ($productid <= 0) {
113 $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("ProductOrService"));
114 $this->errors[] = $this->error;
115 return -1;
116 }
117
118 $now = dol_now();
119
120 // Request must return: id, email, name
121 $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";
122 $sql .= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."contrat as c";
123 $sql .= ", ".MAIN_DB_PREFIX."contratdet as cd, ".MAIN_DB_PREFIX."product as p";
124 $sql .= " WHERE s.entity IN (".getEntity('societe').")";
125 $sql .= " AND s.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing = ".((int) $mailing_id).")";
126 $sql .= " AND s.rowid = c.fk_soc AND cd.fk_contrat = c.rowid AND s.email != ''";
127 $sql .= " AND cd.statut= 4 AND cd.fk_product = p.rowid AND p.rowid = ".((int) $productid);
128 $sql .= " AND cd.date_fin_validite < '".$this->db->idate($now)."'";
129 $sql .= $this->getSqlToExcludeUnsubscribed('s.email');
130 $sql .= " ORDER BY s.email";
131
132 // Save target emails
133 $result = $this->db->query($sql);
134 if ($result) {
135 $num = $this->db->num_rows($result);
136 $i = 0;
137
138 dol_syslog(get_class($this)."::add_to_target ".$num." targets found");
139
140 $old = '';
141 while ($i < $num) {
142 $obj = $this->db->fetch_object($result);
143 if ($old != $obj->email) {
144 $cibles[$j] = array(
145 'email' => $obj->email,
146 'lastname' => $obj->name, // For thirdparties, lastname must be name
147 'firstname' => '', // For thirdparties, firstname is ''
148 'other' =>
149 ('DateStart='.dol_print_date($this->db->jdate($obj->date_start_real), 'day')).';'. // date start real
150 ('DateEnd='.dol_print_date($this->db->jdate($obj->date_end), 'day')).';'. // date end planned
151 ('Contract='.$obj->fk_contrat).';'.
152 ('ContactLine='.$obj->cdid),
153 'source_url' => $this->url($obj->id),
154 'source_id' => (int) $obj->id,
155 'source_type' => 'thirdparty'
156 );
157 $old = $obj->email;
158 $j++;
159 }
160
161 $i++;
162 }
163 } else {
164 dol_syslog($this->db->lasterror());
165 $this->error = $this->db->lasterror();
166 return -1;
167 }
168
169 // ----- Your code end here -----
170
171 return parent::addTargetsToDatabase($mailing_id, $cibles);
172 }
173
174
183 public function getSqlArrayForStats()
184 {
185
186 //var $statssql=array();
187 //$this->statssql[0]="SELECT field1 as label, count(distinct(email)) as nb FROM mytable WHERE email IS NOT NULL";
188
189 return array();
190 }
191
192
201 public function getNbOfRecipients($sql = '')
202 {
203 $now = dol_now();
204
205 // Example: return parent::getNbOfRecipients("SELECT count(*) as nb from dolibarr_table");
206 // Example: return 500;
207 $sql = "SELECT count(*) as nb";
208 $sql .= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."contrat as c,";
209 $sql .= " ".MAIN_DB_PREFIX."contratdet as cd, ".MAIN_DB_PREFIX."product as p";
210 $sql .= " WHERE s.entity IN (".getEntity('societe').")";
211 $sql .= " AND s.rowid = c.fk_soc AND cd.fk_contrat = c.rowid AND s.email != ''";
212 $sql .= " AND cd.statut= 4 AND cd.fk_product = p.rowid";
213 if (!getDolGlobalString('CONTRACT_SUPPORT_PRODUCTS')) {
214 $sql .= " AND p.fk_product_type = 1"; // By default, only services
215 }
216 $sql .= " AND cd.date_fin_validite < '".$this->db->idate($now)."'";
217 $sql .= $this->getSqlToExcludeUnsubscribed('s.email');
218
219 $a = parent::getNbOfRecipients($sql);
220
221 return $a;
222 }
223
230 public function formFilter()
231 {
232 global $form, $langs, $user;
233
234 $socid = 0;
235 if (!empty($user->socid)) {
236 $socid = $user->socid;
237 }
238
239 $filtertype = (getDolGlobalString('CONTRACT_SUPPORT_PRODUCTS') ? '' : 1); // '' in select_produits means all, 1 means services only
240 $showempty = $langs->trans("ProductOrService");
241
242 $s = img_picto('', 'product', 'class="pictofixedwidth"');
243 $s .= $form->select_produits(GETPOSTINT('productid'), 'productid', $filtertype, 0, 0, -1, 2, '', 0, array(), $socid, $showempty, 0, '', 0, '', null, 1);
244 /*
245 .'<select id="filter_services_expired" name="filter" class="flat">';
246 if (count($this->arrayofproducts)) {
247 $langs->loadLangs(array("products"));
248 $s .= '<option value="-1">'.$langs->trans("ProductOrService").'</option>';
249 } else {
250 $s .= '<option value="0">'.$langs->trans("ContactsAllShort").'</option>';
251 }
252 foreach ($this->arrayofproducts as $key => $val) {
253 $s .= '<option value="'.$key.'">'.$val.'</option>';
254 }
255 $s .= '</select>';
256 $s .= ajax_combobox("filter_services_expired");
257 */
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
Support class for third parties, contacts, members, users or resources.
Definition account.php:47
Parent class of emailing target selectors modules.
getSqlToExcludeUnsubscribed($emailfield)
Return the SQL fragment that excludes email addresses which opted out of emailings for the current en...
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.
dol_now($mode='gmt')
Return date for now.
GETPOSTINT($paramname, $method=0, $nodefault=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
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.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2, $allowothertags=array())
Show picto whatever it's its name (generic function)
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $allowothertags=array())
Show a picto called object_picto (generic function)