dolibarr 24.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
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 $conf, $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 if (empty($this->evenunsubscribe)) {
130 $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).")";
131 }
132 $sql .= " ORDER BY s.email";
133
134 // Save target emails
135 $result = $this->db->query($sql);
136 if ($result) {
137 $num = $this->db->num_rows($result);
138 $i = 0;
139
140 dol_syslog(get_class($this)."::add_to_target ".$num." targets found");
141
142 $old = '';
143 while ($i < $num) {
144 $obj = $this->db->fetch_object($result);
145 if ($old != $obj->email) {
146 $cibles[$j] = array(
147 'email' => $obj->email,
148 'lastname' => $obj->name, // For thirdparties, lastname must be name
149 'firstname' => '', // For thirdparties, firstname is ''
150 'other' =>
151 ('DateStart='.dol_print_date($this->db->jdate($obj->date_start_real), 'day')).';'. // date start real
152 ('DateEnd='.dol_print_date($this->db->jdate($obj->date_end), 'day')).';'. // date end planned
153 ('Contract='.$obj->fk_contrat).';'.
154 ('ContactLine='.$obj->cdid),
155 'source_url' => $this->url($obj->id),
156 'source_id' => (int) $obj->id,
157 'source_type' => 'thirdparty'
158 );
159 $old = $obj->email;
160 $j++;
161 }
162
163 $i++;
164 }
165 } else {
166 dol_syslog($this->db->lasterror());
167 $this->error = $this->db->lasterror();
168 return -1;
169 }
170
171 // ----- Your code end here -----
172
173 return parent::addTargetsToDatabase($mailing_id, $cibles);
174 }
175
176
185 public function getSqlArrayForStats()
186 {
187
188 //var $statssql=array();
189 //$this->statssql[0]="SELECT field1 as label, count(distinct(email)) as nb FROM mytable WHERE email IS NOT NULL";
190
191 return array();
192 }
193
194
203 public function getNbOfRecipients($sql = '')
204 {
205 global $conf;
206
207 $now = dol_now();
208
209 // Example: return parent::getNbOfRecipients("SELECT count(*) as nb from dolibarr_table");
210 // Example: return 500;
211 $sql = "SELECT count(*) as nb";
212 $sql .= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."contrat as c,";
213 $sql .= " ".MAIN_DB_PREFIX."contratdet as cd, ".MAIN_DB_PREFIX."product as p";
214 $sql .= " WHERE s.entity IN (".getEntity('societe').")";
215 $sql .= " AND s.rowid = c.fk_soc AND cd.fk_contrat = c.rowid AND s.email != ''";
216 $sql .= " AND cd.statut= 4 AND cd.fk_product = p.rowid";
217 if (!getDolGlobalString('CONTRACT_SUPPORT_PRODUCTS')) {
218 $sql .= " AND p.fk_product_type = 1"; // By default, only services
219 }
220 $sql .= " AND cd.date_fin_validite < '".$this->db->idate($now)."'";
221 if (empty($this->evenunsubscribe)) {
222 $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).")";
223 }
224
225 $a = parent::getNbOfRecipients($sql);
226
227 return $a;
228 }
229
236 public function formFilter()
237 {
238 global $form, $langs, $user;
239
240 $socid = 0;
241 if (!empty($user->socid)) {
242 $socid = $user->socid;
243 }
244
245 $filtertype = (getDolGlobalString('CONTRACT_SUPPORT_PRODUCTS') ? '' : 1); // '' in select_produits means all, 1 means services only
246 $showempty = $langs->trans("ProductOrService");
247
248 $s = img_picto('', 'product', 'class="pictofixedwidth"');
249 $s .= $form->select_produits(GETPOSTINT('productid'), 'productid', $filtertype, 0, 0, -1, 2, '', 0, array(), $socid, $showempty, 0, '', 0, '', null, 1);
250 /*
251 .'<select id="filter_services_expired" name="filter" class="flat">';
252 if (count($this->arrayofproducts)) {
253 $langs->loadLangs(array("products"));
254 $s .= '<option value="-1">'.$langs->trans("ProductOrService").'</option>';
255 } else {
256 $s .= '<option value="0">'.$langs->trans("ContactsAllShort").'</option>';
257 }
258 foreach ($this->arrayofproducts as $key => $val) {
259 $s .= '<option value="'.$key.'">'.$val.'</option>';
260 }
261 $s .= '</select>';
262 $s .= ajax_combobox("filter_services_expired");
263 */
264
265 return $s;
266 }
267
268
275 public function url($id)
276 {
277 return '<a href="'.DOL_URL_ROOT.'/societe/card.php?socid='.$id.'">'.img_object('', "company").'</a>';
278 }
279}
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:47
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.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
dol_now($mode='gmt')
Return date for now.
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)
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $allowothertags=array())
Show a picto called object_picto (generic function)
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.