dolibarr 21.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 *
5* This file is an example to follow to add your own email selector inside
6* the Dolibarr email tool.
7* Follow instructions given in README file to know what to change to build
8* your own emailing list selector.
9* Code that need to be changed in this file are marked by "CHANGE THIS" tag.
10*/
11
17include_once DOL_DOCUMENT_ROOT.'/core/modules/mailings/modules_mailings.php';
18require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
19
20
25{
26 public $name = 'DolibarrContractsLinesExpired';
27 // This label is used if no translation is found for key XXX neither MailingModuleDescXXX where XXX=name is found
28 public $desc = 'Third parties with expired contract\'s lines';
29 public $require_admin = 0;
30
31 public $require_module = array('contrat');
32
33 public $enabled = 'isModEnabled("societe")';
34
38 public $picto = 'company';
39
40 public $arrayofproducts = array();
41
42
48 public function __construct($db)
49 {
50 global $conf;
51
52 $this->db = $db;
53
54 $this->arrayofproducts = array();
55
56 // List of services
57 $sql = "SELECT ref FROM ".MAIN_DB_PREFIX."product";
58 $sql .= " WHERE entity IN (".getEntity('product').")";
59 if (!getDolGlobalString('CONTRACT_SUPPORT_PRODUCTS')) {
60 $sql .= " AND fk_product_type = 1"; // By default, only services
61 }
62 $sql .= " ORDER BY ref";
63 $result = $this->db->query($sql);
64 if ($result) {
65 $num = $this->db->num_rows($result);
66 dol_syslog("dolibarr_services_expired.modules.php:mailing_dolibarr_services_expired ".$num." services found");
67
68 $i = 0;
69 while ($i < $num) {
70 $obj = $this->db->fetch_object($result);
71 $i++;
72 $this->arrayofproducts[$i] = $obj->ref;
73 }
74 } else {
75 dol_print_error($this->db);
76 }
77 }
78
79
80 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
87 public function add_to_target($mailing_id)
88 {
89 global $conf;
90
91 // phpcs:enable
92 $key = GETPOSTINT('filter');
93
94 $cibles = array();
95 $j = 0;
96
97 $product = '';
98 if ($key == '0') {
99 $this->error = "Error: You must choose a filter";
100 $this->errors[] = $this->error;
101 return -1;
102 }
103
104 $product = $this->arrayofproducts[$key];
105
106 $now = dol_now();
107
108 // La requete doit retourner: id, email, name
109 $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";
110 $sql .= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."contrat as c";
111 $sql .= ", ".MAIN_DB_PREFIX."contratdet as cd, ".MAIN_DB_PREFIX."product as p";
112 $sql .= " WHERE s.entity IN (".getEntity('societe').")";
113 $sql .= " AND s.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing=".((int) $mailing_id).")";
114 $sql .= " AND s.rowid = c.fk_soc AND cd.fk_contrat = c.rowid AND s.email != ''";
115 $sql .= " AND cd.statut= 4 AND cd.fk_product=p.rowid AND p.ref = '".$this->db->escape($product)."'";
116 $sql .= " AND cd.date_fin_validite < '".$this->db->idate($now)."'";
117 if (empty($this->evenunsubscribe)) {
118 $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).")";
119 }
120 $sql .= " ORDER BY s.email";
121
122 // Stocke destinataires dans cibles
123 $result = $this->db->query($sql);
124 if ($result) {
125 $num = $this->db->num_rows($result);
126 $i = 0;
127
128 dol_syslog(get_class($this)."::add_to_target ".$num." targets found");
129
130 $old = '';
131 while ($i < $num) {
132 $obj = $this->db->fetch_object($result);
133 if ($old != $obj->email) {
134 $cibles[$j] = array(
135 'email' => $obj->email,
136 'lastname' => $obj->name, // For thirdparties, lastname must be name
137 'firstname' => '', // For thirdparties, firstname is ''
138 'other' =>
139 ('DateStart='.dol_print_date($this->db->jdate($obj->date_start_real), 'day')).';'. // date start real
140 ('DateEnd='.dol_print_date($this->db->jdate($obj->date_end), 'day')).';'. // date end planned
141 ('Contract='.$obj->fk_contrat).';'.
142 ('ContactLine='.$obj->cdid),
143 'source_url' => $this->url($obj->id),
144 'source_id' => $obj->id,
145 'source_type' => 'thirdparty'
146 );
147 $old = $obj->email;
148 $j++;
149 }
150
151 $i++;
152 }
153 } else {
154 dol_syslog($this->db->lasterror());
155 $this->error = $this->db->lasterror();
156 return -1;
157 }
158
159 // ----- Your code end here -----
160
161 return parent::addTargetsToDatabase($mailing_id, $cibles);
162 }
163
164
173 public function getSqlArrayForStats()
174 {
175
176 //var $statssql=array();
177 //$this->statssql[0]="SELECT field1 as label, count(distinct(email)) as nb FROM mytable WHERE email IS NOT NULL";
178
179 return array();
180 }
181
182
191 public function getNbOfRecipients($sql = '')
192 {
193 global $conf;
194
195 $now = dol_now();
196
197 // Example: return parent::getNbOfRecipients("SELECT count(*) as nb from dolibarr_table");
198 // Example: return 500;
199 $sql = "SELECT count(*) as nb";
200 $sql .= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."contrat as c";
201 $sql .= ", ".MAIN_DB_PREFIX."contratdet as cd, ".MAIN_DB_PREFIX."product as p";
202 $sql .= " WHERE s.entity IN (".getEntity('societe').")";
203 $sql .= " AND s.rowid = c.fk_soc AND cd.fk_contrat = c.rowid AND s.email != ''";
204 $sql .= " AND cd.statut= 4 AND cd.fk_product=p.rowid";
205 $sql .= " AND p.ref IN (".$this->db->sanitize("'".implode("','", $this->arrayofproducts)."'", 1).")";
206 $sql .= " AND cd.date_fin_validite < '".$this->db->idate($now)."'";
207 if (empty($this->evenunsubscribe)) {
208 $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).")";
209 }
210
211 $a = parent::getNbOfRecipients($sql);
212
213 return $a;
214 }
215
222 public function formFilter()
223 {
224 global $langs;
225
226 $s = img_picto('', 'product', 'class="pictofixedwidth"').'<select id="filter_services_expired" name="filter" class="flat">';
227 if (count($this->arrayofproducts)) {
228 $langs->loadLangs(array("products"));
229 $s .= '<option value="-1">'.$langs->trans("ProductOrService").'</option>';
230 } else {
231 $s .= '<option value="0">'.$langs->trans("ContactsAllShort").'</option>';
232 }
233 foreach ($this->arrayofproducts as $key => $val) {
234 $s .= '<option value="'.$key.'">'.$val.'</option>';
235 }
236 $s .= '</select>';
237 $s .= ajax_combobox("filter_services_expired");
238
239 return $s;
240 }
241
242
249 public function url($id)
250 {
251 return '<a href="'.DOL_URL_ROOT.'/societe/card.php?socid='.$id.'">'.img_object('', "company").'</a>';
252 }
253}
$id
Definition account.php:39
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:457
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.