dolibarr  13.0.2
lettering.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2004-2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3  * Copyright (C) 2013 Olivier Geffroy <jeff@jeffinfo.com>
4  * Copyright (C) 2013-2019 Alexandre Spangaro <aspangaro@open-dsi.fr>
5  * Copyright (C) 2018 Frédéric France <frederic.france@netlogic.fr>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <https://www.gnu.org/licenses/>.
19  */
20 
27 include_once DOL_DOCUMENT_ROOT."/accountancy/class/bookkeeping.class.php";
28 include_once DOL_DOCUMENT_ROOT."/societe/class/societe.class.php";
29 include_once DOL_DOCUMENT_ROOT."/core/lib/date.lib.php";
30 
34 class Lettering extends BookKeeping
35 {
42  public function letteringThirdparty($socid)
43  {
44  global $conf;
45 
46  $error = 0;
47 
48  $object = new Societe($this->db);
49  $object->id = $socid;
50  $object->fetch($socid);
51 
52 
53  if ($object->code_compta == '411CUSTCODE') {
54  $object->code_compta = '';
55  }
56 
57  if ($object->code_compta_fournisseur == '401SUPPCODE') {
58  $object->code_compta_fournisseur = '';
59  }
60 
64  $sql = "SELECT DISTINCT bk.rowid, bk.doc_date, bk.doc_type, bk.doc_ref, bk.subledger_account, ";
65  $sql .= " bk.numero_compte , bk.label_compte, bk.debit , bk.credit, bk.montant ";
66  $sql .= " , bk.sens , bk.code_journal , bk.piece_num, bk.date_lettering, bu.url_id , bu.type ";
67  $sql .= " FROM ".MAIN_DB_PREFIX."accounting_bookkeeping as bk";
68  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."bank_url as bu ON(bk.fk_doc = bu.fk_bank AND bu.type IN ('payment', 'payment_supplier') ) ";
69  $sql .= " WHERE ( ";
70  if ($object->code_compta != "")
71  $sql .= " bk.subledger_account = '".$this->db->escape($object->code_compta)."' ";
72  if ($object->code_compta != "" && $object->code_compta_fournisseur != "")
73  $sql .= " OR ";
74  if ($object->code_compta_fournisseur != "")
75  $sql .= " bk.subledger_account = '".$this->db->escape($object->code_compta_fournisseur)."' ";
76 
77  $sql .= " ) AND (bk.date_lettering ='' OR bk.date_lettering IS NULL) ";
78  $sql .= " AND (bk.lettering_code != '' OR bk.lettering_code IS NULL) ";
79  $sql .= ' AND bk.date_validated IS NULL ';
80  $sql .= $this->db->order('bk.doc_date', 'DESC');
81 
82  // echo $sql;
83  //
84  $resql = $this->db->query($sql);
85  if ($resql) {
86  $num = $this->db->num_rows($resql);
87 
88  while ($obj = $this->db->fetch_object($resql)) {
89  $ids = array();
90  $ids_fact = array();
91 
92  if ($obj->type == 'payment_supplier')
93  {
94  $sql = 'SELECT DISTINCT bk.rowid, facf.ref, facf.ref_supplier, payf.fk_bank, facf.rowid as fact_id';
95  $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn facf ";
96  $sql .= " INNER JOIN ".MAIN_DB_PREFIX."paiementfourn_facturefourn as payfacf ON payfacf.fk_facturefourn=facf.rowid";
97  $sql .= " INNER JOIN ".MAIN_DB_PREFIX."paiementfourn as payf ON payfacf.fk_paiementfourn=payf.rowid";
98  $sql .= " INNER JOIN ".MAIN_DB_PREFIX."accounting_bookkeeping as bk ON (bk.fk_doc = payf.fk_bank AND bk.code_journal='".$this->db->escape($obj->code_journal)."')";
99  $sql .= " WHERE payfacf.fk_paiementfourn = '".$this->db->escape($obj->url_id)."' ";
100  $sql .= " AND facf.entity = ".$conf->entity;
101  $sql .= " AND code_journal IN (SELECT code FROM ".MAIN_DB_PREFIX."accounting_journal WHERE nature=4 AND entity=".$conf->entity.") ";
102  $sql .= " AND ( ";
103  if ($object->code_compta != "") {
104  $sql .= " bk.subledger_account = '".$this->db->escape($object->code_compta)."' ";
105  }
106  if ($object->code_compta != "" && $object->code_compta_fournisseur != "") {
107  $sql .= " OR ";
108  }
109  if ($object->code_compta_fournisseur != "") {
110  $sql .= " bk.subledger_account = '".$this->db->escape($object->code_compta_fournisseur)."' ";
111  }
112  $sql .= " ) ";
113 
114  $resql2 = $this->db->query($sql);
115  if ($resql2) {
116  while ($obj2 = $this->db->fetch_object($resql2)) {
117  $ids[$obj2->rowid] = $obj2->rowid;
118  $ids_fact[] = $obj2->fact_id;
119  }
120  } else {
121  $this->errors[] = $this->db->lasterror;
122  return -1;
123  }
124  if (count($ids_fact)) {
125  $sql = 'SELECT bk.rowid, facf.ref, facf.ref_supplier ';
126  $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn facf ";
127  $sql .= " INNER JOIN ".MAIN_DB_PREFIX."accounting_bookkeeping as bk ON( bk.fk_doc = facf.rowid AND facf.rowid IN (".implode(',', $ids_fact)."))";
128  $sql .= " WHERE bk.code_journal IN (SELECT code FROM ".MAIN_DB_PREFIX."accounting_journal WHERE nature=3 AND entity=".$conf->entity.") ";
129  $sql .= " AND facf.entity = ".$conf->entity;
130  $sql .= " AND ( ";
131  if ($object->code_compta != "") {
132  $sql .= " bk.subledger_account = '".$this->db->escape($object->code_compta)."' ";
133  }
134  if ($object->code_compta != "" && $object->code_compta_fournisseur != "") {
135  $sql .= " OR ";
136  }
137  if ($object->code_compta_fournisseur != "") {
138  $sql .= " bk.subledger_account = '".$this->db->escape($object->code_compta_fournisseur)."' ";
139  }
140  $sql .= ") ";
141 
142  $resql2 = $this->db->query($sql);
143  if ($resql2) {
144  while ($obj2 = $this->db->fetch_object($resql2)) {
145  $ids[$obj2->rowid] = $obj2->rowid;
146  }
147  } else {
148  $this->errors[] = $this->db->lasterror;
149  return -1;
150  }
151  }
152  } elseif ($obj->type == 'payment') {
153  $sql = 'SELECT DISTINCT bk.rowid, fac.ref, fac.ref, pay.fk_bank, fac.rowid as fact_id';
154  $sql .= " FROM ".MAIN_DB_PREFIX."facture fac ";
155  $sql .= " INNER JOIN ".MAIN_DB_PREFIX."paiement_facture as payfac ON payfac.fk_facture=fac.rowid";
156  $sql .= " INNER JOIN ".MAIN_DB_PREFIX."paiement as pay ON payfac.fk_paiement=pay.rowid";
157  $sql .= " INNER JOIN ".MAIN_DB_PREFIX."accounting_bookkeeping as bk ON (bk.fk_doc = pay.fk_bank AND bk.code_journal='".$this->db->escape($obj->code_journal)."')";
158  $sql .= " WHERE payfac.fk_paiement = '".$this->db->escape($obj->url_id)."' ";
159  $sql .= " AND bk.code_journal IN (SELECT code FROM ".MAIN_DB_PREFIX."accounting_journal WHERE nature=4 AND entity=".$conf->entity.") ";
160  $sql .= " AND fac.entity IN (".getEntity('invoice', 0).")"; // We don't share object for accountancy
161  $sql .= " AND ( ";
162  if ($object->code_compta != "") {
163  $sql .= " bk.subledger_account = '".$this->db->escape($object->code_compta)."' ";
164  }
165  if ($object->code_compta != "" && $object->code_compta_fournisseur != "") {
166  $sql .= " OR ";
167  }
168  if ($object->code_compta_fournisseur != "") {
169  $sql .= " bk.subledger_account = '".$this->db->escape($object->code_compta_fournisseur)."' ";
170  }
171  $sql .= " )";
172 
173  $resql2 = $this->db->query($sql);
174  if ($resql2) {
175  while ($obj2 = $this->db->fetch_object($resql2)) {
176  $ids[$obj2->rowid] = $obj2->rowid;
177  $ids_fact[] = $obj2->fact_id;
178  }
179  } else {
180  $this->errors[] = $this->db->lasterror;
181  return -1;
182  }
183  if (count($ids_fact)) {
184  $sql = 'SELECT bk.rowid, fac.ref, fac.ref_supplier ';
185  $sql .= " FROM ".MAIN_DB_PREFIX."facture fac ";
186  $sql .= " INNER JOIN ".MAIN_DB_PREFIX."accounting_bookkeeping as bk ON( bk.fk_doc = fac.rowid AND fac.rowid IN (".implode(',', $ids_fact)."))";
187  $sql .= " WHERE code_journal IN (SELECT code FROM ".MAIN_DB_PREFIX."accounting_journal WHERE nature=2 AND entity=".$conf->entity.") ";
188  $sql .= " AND fac.entity IN (".getEntity('invoice', 0).")"; // We don't share object for accountancy
189  $sql .= " AND ( ";
190  if ($object->code_compta != "") {
191  $sql .= " bk.subledger_account = '".$this->db->escape($object->code_compta)."' ";
192  }
193  if ($object->code_compta != "" && $object->code_compta_fournisseur != "") {
194  $sql .= " OR ";
195  }
196  if ($object->code_compta_fournisseur != "") {
197  $sql .= " bk.subledger_account = '".$this->db->escape($object->code_compta_fournisseur)."' ";
198  }
199  $sql .= " ) ";
200 
201  $resql2 = $this->db->query($sql);
202  if ($resql2) {
203  while ($obj2 = $this->db->fetch_object($resql2)) {
204  $ids[$obj2->rowid] = $obj2->rowid;
205  }
206  } else {
207  $this->errors[] = $this->db->lasterror;
208  return -1;
209  }
210  }
211  }
212 
213  if (count($ids) > 1) {
214  $result = $this->updateLettering($ids);
215  }
216  }
217  }
218  if ($error) {
219  foreach ($this->errors as $errmsg) {
220  dol_syslog(__METHOD__.' '.$errmsg, LOG_ERR);
221  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
222  }
223  return -1 * $error;
224  } else {
225  return 1;
226  }
227  }
228 
235  public function updateLettering($ids = array(), $notrigger = false)
236  {
237  $error = 0;
238  $lettre = 'AAA';
239 
240  $sql = "SELECT DISTINCT lettering_code FROM ".MAIN_DB_PREFIX."accounting_bookkeeping WHERE ";
241  $sql .= " lettering_code != '' ORDER BY lettering_code DESC limit 1";
242 
243  $result = $this->db->query($sql);
244  if ($result) {
245  $obj = $this->db->fetch_object($result);
246  $lettre = (empty($obj->lettering_code) ? 'AAA' : $obj->lettering_code);
247  if (!empty($obj->lettering_code))
248  $lettre++;
249  } else {
250  $this->errors[] = 'Error'.$this->db->lasterror();
251  $error++;
252  }
253 
254  $sql = "SELECT SUM(ABS(debit)) as deb, SUM(ABS(credit)) as cred FROM ".MAIN_DB_PREFIX."accounting_bookkeeping WHERE ";
255  $sql .= " rowid IN (".implode(',', $ids).") AND date_validated IS NULL";
256  $result = $this->db->query($sql);
257  if ($result) {
258  $obj = $this->db->fetch_object($result);
259  if (!(round(abs($obj->deb), 2) === round(abs($obj->cred), 2))) {
260  $this->errors[] = 'Total not exacts '.round(abs($obj->deb), 2).' vs '.round(abs($obj->cred), 2);
261  $error++;
262  }
263  } else {
264  $this->errors[] = 'Erreur sql'.$this->db->lasterror();
265  $error++;
266  }
267 
268  // Update request
269 
270  $now = dol_now();
271 
272  if (!$error)
273  {
274  $sql = "UPDATE ".MAIN_DB_PREFIX."accounting_bookkeeping SET";
275  $sql .= " lettering_code='".$this->db->escape($lettre)."'";
276  $sql .= " , date_lettering = '".$this->db->idate($now)."'"; // todo correct date it's false
277  $sql .= " WHERE rowid IN (".implode(',', $ids).") AND date_validated IS NULL ";
278  $this->db->begin();
279 
280  dol_syslog(get_class($this)."::update sql=".$sql, LOG_DEBUG);
281  $resql = $this->db->query($sql);
282  if (!$resql) {
283  $error++;
284  $this->errors[] = "Error ".$this->db->lasterror();
285  }
286  }
287 
288  // Commit or rollback
289  if ($error) {
290  foreach ($this->errors as $errmsg) {
291  dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
292  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
293  }
294  $this->db->rollback();
295  return -1 * $error;
296  } else {
297  $this->db->commit();
298  return 1;
299  }
300  }
301 }
Class Lettering.
dol_now($mode= 'auto')
Return date for now.
letteringThirdparty($socid)
letteringThirdparty
$conf db
API class for accounts.
Definition: inc.php:54
Class to manage Ledger (General Ledger and Subledger)
Class to manage third parties objects (customers, suppliers, prospects...)
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
updateLettering($ids=array(), $notrigger=false)
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1232