dolibarr 25.0.0-alpha
accountingaccount.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2013-2014 Olivier Geffroy <jeff@jeffinfo.com>
3 * Copyright (C) 2013-2024 Alexandre Spangaro <aspangaro@easya.solutions>
4 * Copyright (C) 2013-2021 Florian Henry <florian.henry@open-concept.pro>
5 * Copyright (C) 2014 Juanjo Menent <jmenent@2byte.es>
6 * Copyright (C) 2015 Ari Elbaz (elarifr) <github@accedinfo.com>
7 * Copyright (C) 2018-2025 Frédéric France <frederic.france@free.fr>
8 * Copyright (C) 2024-2026 MDW <mdeweerd@users.noreply.github.com>
9 * Copyright (C) 2026 Jose Martinez <jose.martinez@pichinov.com>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <https://www.gnu.org/licenses/>.
23 */
24
31require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
32require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
33require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
34
39{
43 public $element = 'accounting_account';
44
48 public $table_element = 'accounting_account';
49
53 public $picto = 'billr';
54
59 public $restrictiononfksoc = 1;
60
64 public $db;
65
69 public $id;
70
74 public $rowid;
75
79 public $ref;
80
86 public $datec;
87
91 public $fk_pcg_version;
92
96 public $pcg_type;
97
101 public $account_number;
102
106 public $account_parent;
107
111 public $account_category;
112
116 public $account_category_label;
117
121 public $status;
122
126 public $label;
127
131 public $labelshort;
132
136 public $fk_user_author;
137
141 public $fk_user_modif;
142
146 public $active;
147
151 public $reconcilable;
152
156 private $accountingaccount_codetotid_cache = array();
157
158
159 const STATUS_ENABLED = 1;
160 const STATUS_DISABLED = 0;
161
162
168 public function __construct($db)
169 {
170 $this->db = $db;
171
172 $this->ismultientitymanaged = 1;
173 $this->next_prev_filter = "fk_pcg_version IN (SELECT pcg_version FROM ".$this->db->prefix()."accounting_system WHERE rowid = ".((int) getDolGlobalInt('CHARTOFACCOUNTS')).")"; // Used to add a filter in Form::showrefnav method
174 }
175
185 public function fetch($rowid = 0, $account_number = null, $limittocurrentchart = 0, $limittoachartaccount = '')
186 {
187 global $conf;
188
189 if ($rowid || $account_number) {
190 $sql = "SELECT a.rowid as rowid, a.datec, a.tms, a.fk_pcg_version, a.pcg_type, a.account_number, a.account_parent, a.label, a.labelshort, a.fk_accounting_category, a.fk_user_author, a.fk_user_modif, a.active, a.reconcilable";
191 $sql .= ", ca.label as category_label";
192 $sql .= " FROM ".$this->db->prefix().$this->table_element." as a";
193 $sql .= " LEFT JOIN ".$this->db->prefix()."c_accounting_category as ca ON a.fk_accounting_category = ca.rowid";
194 $sql .= " WHERE";
195 if ($rowid) {
196 $sql .= " a.rowid = ".(int) $rowid;
197 } elseif ($account_number) {
198 $sql .= " a.account_number = '".$this->db->escape($account_number)."'";
199 $sql .= " AND a.entity = ".((int) $conf->entity);
200 }
201 if (!empty($limittocurrentchart)) {
202 $sql .= ' AND a.fk_pcg_version IN (SELECT pcg_version FROM '.$this->db->prefix().'accounting_system WHERE rowid = '.((int) getDolGlobalInt('CHARTOFACCOUNTS')).')';
203 }
204 if (!empty($limittoachartaccount)) {
205 $sql .= " AND a.fk_pcg_version = '".$this->db->escape($limittoachartaccount)."'";
206 }
207
208 dol_syslog(get_class($this)."::fetch rowid=".$rowid." account_number=".$account_number, LOG_DEBUG);
209
210 $result = $this->db->query($sql);
211 if ($result) {
212 $obj = $this->db->fetch_object($result);
213
214 if ($obj) {
215 $this->id = $obj->rowid;
216 $this->rowid = $obj->rowid;
217 $this->ref = $obj->account_number;
218 $this->datec = $this->db->jdate($obj->datec);
219 $this->date_creation = $this->db->jdate($obj->datec);
220 $this->date_modification = $this->db->jdate($obj->tms);
221 //$this->tms = $this->datem;
222 $this->fk_pcg_version = $obj->fk_pcg_version;
223 $this->pcg_type = $obj->pcg_type;
224 $this->account_number = $obj->account_number;
225 $this->account_parent = $obj->account_parent;
226 $this->label = $obj->label;
227 $this->labelshort = $obj->labelshort;
228 $this->account_category = $obj->fk_accounting_category;
229 $this->account_category_label = $obj->category_label;
230 $this->fk_user_author = $obj->fk_user_author;
231 $this->fk_user_modif = $obj->fk_user_modif;
232 $this->active = $obj->active;
233 $this->status = $obj->active;
234 $this->reconcilable = $obj->reconcilable;
235
236 return $this->id;
237 } else {
238 return 0;
239 }
240 } else {
241 $this->error = "Error ".$this->db->lasterror();
242 $this->errors[] = "Error ".$this->db->lasterror();
243 }
244 }
245 return -1;
246 }
247
255 public function create($user, $notrigger = 0)
256 {
257 global $conf;
258 $error = 0;
259 $now = dol_now();
260
261 // Clean parameters
262 if (isset($this->fk_pcg_version)) {
263 $this->fk_pcg_version = trim($this->fk_pcg_version);
264 }
265 if (isset($this->pcg_type)) {
266 $this->pcg_type = trim($this->pcg_type);
267 }
268 if (isset($this->account_number)) {
269 $this->account_number = trim($this->account_number);
270 }
271 if (isset($this->label)) {
272 $this->label = trim($this->label);
273 }
274 if (isset($this->labelshort)) {
275 $this->labelshort = trim($this->labelshort);
276 }
277
278 if (empty($this->pcg_type) || $this->pcg_type == '-1') {
279 $this->pcg_type = 'XXXXXX';
280 }
281 // Check parameters
282 // Put here code to add control on parameters values
283
284 // Insert request
285 $sql = "INSERT INTO " . $this->db->prefix() . $this->table_element . " (";
286 $sql .= "datec";
287 $sql .= ", entity";
288 $sql .= ", fk_pcg_version";
289 $sql .= ", pcg_type";
290 $sql .= ", account_number";
291 $sql .= ", account_parent";
292 $sql .= ", label";
293 $sql .= ", labelshort";
294 $sql .= ", fk_accounting_category";
295 $sql .= ", fk_user_author";
296 $sql .= ", active";
297 $sql .= ", reconcilable";
298 $sql .= ") VALUES (";
299 $sql .= " '".$this->db->idate($now)."'";
300 $sql .= ", ".((int) $conf->entity);
301 $sql .= ", ".(empty($this->fk_pcg_version) ? 'NULL' : "'".$this->db->escape($this->fk_pcg_version)."'");
302 $sql .= ", ".(empty($this->pcg_type) ? 'NULL' : "'".$this->db->escape($this->pcg_type)."'");
303 $sql .= ", ".(empty($this->account_number) ? 'NULL' : "'".$this->db->escape($this->account_number)."'");
304 $sql .= ", ".(empty($this->account_parent) ? 0 : (int) $this->account_parent);
305 $sql .= ", ".(empty($this->label) ? "''" : "'".$this->db->escape($this->label)."'");
306 $sql .= ", ".(empty($this->labelshort) ? "''" : "'".$this->db->escape($this->labelshort)."'");
307 $sql .= ", ".(empty($this->account_category) ? 0 : (int) $this->account_category);
308 $sql .= ", ".((int) $user->id);
309 $sql .= ", ".(int) $this->active;
310 $sql .= ", ".(int) $this->reconcilable;
311 $sql .= ")";
312
313 $this->db->begin();
314
315 dol_syslog(get_class($this)."::create", LOG_DEBUG);
316 $resql = $this->db->query($sql);
317 if (!$resql) {
318 $error++;
319 $this->errors[] = "Error " . $this->db->lasterror();
320 }
321
322 if (!$error) {
323 $this->id = $this->db->last_insert_id($this->db->prefix() . $this->table_element);
324
325 // Uncomment this and change MYOBJECT to your own tag if you
326 // want this action to call a trigger.
327 //if (! $error && ! $notrigger) {
328
329 // // Call triggers
330 // $result=$this->call_trigger('MYOBJECT_CREATE',$user);
331 // if ($result < 0) $error++;
332 // // End call triggers
333 //}
334 }
335
336 // Commit or rollback
337 if ($error) {
338 foreach ($this->errors as $errmsg) {
339 dol_syslog(get_class($this) . "::create " . $errmsg, LOG_ERR);
340 $this->error .= ($this->error ? ', ' . $errmsg : $errmsg);
341 }
342 $this->db->rollback();
343 return -1 * $error;
344 } else {
345 $this->db->commit();
346 return $this->id;
347 }
348 }
349
356 public function update($user)
357 {
358 // Check parameters
359 if (empty($this->pcg_type) || $this->pcg_type == '-1') {
360 $this->pcg_type = 'XXXXXX';
361 }
362
363 $this->db->begin();
364
365 $sql = "UPDATE " . $this->db->prefix() . $this->table_element;
366 $sql .= " SET fk_pcg_version = " . ($this->fk_pcg_version ? "'" . $this->db->escape($this->fk_pcg_version) . "'" : "null");
367 $sql .= " , pcg_type = '" . $this->db->escape($this->pcg_type) . "'";
368 $sql .= " , account_number = '" . $this->db->escape($this->account_number) . "'";
369 $sql .= " , account_parent = " . (int) $this->account_parent;
370 $sql .= " , label = " . ($this->label ? "'" . $this->db->escape($this->label) . "'" : "''");
371 $sql .= " , labelshort = " . ($this->labelshort ? "'" . $this->db->escape($this->labelshort) . "'" : "''");
372 $sql .= " , fk_accounting_category = " . (empty($this->account_category) ? 0 : (int) $this->account_category);
373 $sql .= " , fk_user_modif = " . ((int) $user->id);
374 $sql .= " , active = " . (int) $this->active;
375 $sql .= " , reconcilable = " . (int) $this->reconcilable;
376 $sql .= " WHERE rowid = " . ((int) $this->id);
377
378 dol_syslog(get_class($this)."::update", LOG_DEBUG);
379 $result = $this->db->query($sql);
380 if ($result) {
381 $this->db->commit();
382 return 1;
383 } else {
384 if ($this->db->lasterrno() == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
385 $this->error = $this->db->lasterror();
386 $this->db->rollback();
387 return -2;
388 }
389
390 $this->error = $this->db->lasterror();
391 $this->db->rollback();
392 return -1;
393 }
394 }
395
401 public function checkUsage()
402 {
403 global $langs;
404
405 // TODO Looks a stupid check
406 $sql = "(SELECT fk_code_ventilation FROM ".$this->db->prefix()."facturedet";
407 $sql .= " WHERE fk_code_ventilation=".((int) $this->id).")";
408 $sql .= "UNION";
409 $sql .= " (SELECT fk_code_ventilation FROM ".$this->db->prefix()."facture_fourn_det";
410 $sql .= " WHERE fk_code_ventilation=".((int) $this->id).")";
411
412 dol_syslog(get_class($this)."::checkUsage", LOG_DEBUG);
413 $resql = $this->db->query($sql);
414
415 if ($resql) {
416 $num = $this->db->num_rows($resql);
417 if ($num > 0) {
418 $this->error = $langs->trans('ErrorAccountancyCodeIsAlreadyUse');
419 return 0;
420 } else {
421 return 1;
422 }
423 } else {
424 $this->error = $this->db->lasterror();
425 return -1;
426 }
427 }
428
436 public function delete($user, $notrigger = 0)
437 {
438 $error = 0;
439
440 $result = $this->checkUsage();
441
442 if ($result > 0) {
443 $this->db->begin();
444
445 $sql = "DELETE FROM " . $this->db->prefix() . $this->table_element;
446 $sql .= " WHERE rowid=" . ((int) $this->id);
447
448 dol_syslog(get_class($this) . "::delete sql=" . $sql);
449 $resql = $this->db->query($sql);
450 if (!$resql) {
451 $error++;
452 $this->errors[] = "Error " . $this->db->lasterror();
453 }
454
455 // Commit or rollback
456 if ($error) {
457 foreach ($this->errors as $errmsg) {
458 dol_syslog(get_class($this) . "::delete " . $errmsg, LOG_ERR);
459 $this->error .= ($this->error ? ', ' . $errmsg : $errmsg);
460 }
461 $this->db->rollback();
462 return -1 * $error;
463 } else {
464 $this->db->commit();
465 return 1;
466 }
467 } else {
468 return -1;
469 }
470 }
471
485 public function getNomUrl($withpicto = 0, $withlabel = 0, $nourl = 0, $moretitle = '', $notooltip = 0, $save_lastsearch_value = -1, $withcompletelabel = 0, $option = '')
486 {
487 global $langs, $conf, $hookmanager;
488 require_once DOL_DOCUMENT_ROOT . '/core/lib/accounting.lib.php';
489
490 if (!empty($conf->dol_no_mouse_hover)) {
491 $notooltip = 1; // Force disable tooltips
492 }
493
494 $result = '';
495
496 $url = '';
497 $labelurl = '';
498 if (empty($option) || $option == 'ledger') {
499 $url = DOL_URL_ROOT . '/accountancy/bookkeeping/listbyaccount.php?search_accountancy_code_start=' . urlencode((isset($this->account_number) ? $this->account_number : '')) . '&search_accountancy_code_end=' . urlencode((isset($this->account_number) ? $this->account_number : ''));
500 $labelurl = $langs->trans("ShowAccountingAccountInLedger");
501 } elseif ($option == 'journals') {
502 $url = DOL_URL_ROOT . '/accountancy/bookkeeping/list.php?search_accountancy_code_start=' . urlencode($this->account_number) . '&search_accountancy_code_end=' . urlencode($this->account_number);
503 $labelurl = $langs->trans("ShowAccountingAccountInJournals");
504 } elseif ($option == 'accountcard') {
505 $url = DOL_URL_ROOT . '/accountancy/admin/card.php?id=' . urlencode((string) ($this->id));
506 $labelurl = $langs->trans("ShowAccountingAccount");
507 }
508
509 // Add param to save lastsearch_values or not
510 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
511 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
512 $add_save_lastsearch_values = 1;
513 }
514 if ($add_save_lastsearch_values) {
515 $url .= '&save_lastsearch_values=1';
516 }
517
518 $picto = 'accounting_account';
519 $label = '';
520
521 if (empty($this->labelshort) || $withcompletelabel == 1) {
522 $labeltoshow = $this->label;
523 } else {
524 $labeltoshow = $this->labelshort;
525 }
526
527 $label = '<u>' . $labelurl . '</u>';
528 if (!empty($this->account_number)) {
529 $label .= '<br><b>' . $langs->trans('AccountAccounting') . ':</b> ' . length_accountg($this->account_number);
530 }
531 if (!empty($labeltoshow)) {
532 $label .= '<br><b>' . $langs->trans('Label') . ':</b> ' . $labeltoshow;
533 }
534 if ($moretitle) {
535 $label .= ' - ' . $moretitle;
536 }
537
538 $linkclose = '';
539 if (empty($notooltip)) {
540 if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
541 $label = $labelurl;
542 $linkclose .= ' alt="' . dol_escape_htmltag($label, 1) . '"';
543 }
544 $linkclose .= ' title="' . dol_escape_htmltag($label, 1) . '"';
545 $linkclose .= ' class="classfortooltip"';
546 }
547
548 $linkstart = '<a href="' . $url . '"';
549 $linkstart .= $linkclose . '>';
550 $linkend = '</a>';
551
552 if ($nourl) {
553 $linkstart = '';
554 $linkclose = '';
555 $linkend = '';
556 }
557
558 $label_link = length_accountg($this->account_number);
559 if ($withlabel) {
560 $label_link .= ' - ' . ($nourl ? '<span class="opacitymedium">' : '') . $labeltoshow . ($nourl ? '</span>' : '');
561 }
562
563 if ($withpicto) {
564 $result .= ($linkstart . img_object(($notooltip ? '' : $label), $picto, ($notooltip ? '' : 'class="classfortooltip"'), 0, 0, $notooltip ? 0 : 1) . $linkend);
565 }
566 if ($withpicto && $withpicto != 2) {
567 $result .= ' ';
568 }
569 if ($withpicto != 2) {
570 $result .= $linkstart . $label_link . $linkend;
571 }
572 global $action;
573 $hookmanager->initHooks(array($this->element . 'dao'));
574 $parameters = array('id' => $this->id, 'getnomurl' => &$result);
575 $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
576 if ($reshook > 0) {
577 $result = $hookmanager->resPrint;
578 } else {
579 $result .= $hookmanager->resPrint;
580 }
581 return $result;
582 }
583
590 public function info($id)
591 {
592 $sql = 'SELECT a.rowid, a.datec, a.fk_user_author, a.fk_user_modif, a.tms as date_modification';
593 $sql .= ' FROM ' . $this->db->prefix() . $this->table_element . ' as a';
594 $sql .= ' WHERE a.rowid = ' . ((int) $id);
595
596 dol_syslog(get_class($this) . '::info sql=' . $sql);
597 $resql = $this->db->query($sql);
598
599 if ($resql) {
600 if ($this->db->num_rows($resql)) {
601 $obj = $this->db->fetch_object($resql);
602
603 $this->id = $obj->rowid;
604
605 $this->user_creation_id = $obj->fk_user_author;
606 $this->user_modification_id = $obj->fk_user_modif;
607 $this->date_creation = $this->db->jdate($obj->datec);
608 $this->date_modification = $this->db->jdate($obj->date_modification);
609 }
610 $this->db->free($resql);
611 } else {
612 dol_print_error($this->db);
613 }
614 }
615
623 public function accountDeactivate($id, $mode = 0)
624 {
625 $result = $this->checkUsage();
626
627 $fieldtouse = 'active';
628 if ($mode == 1) {
629 $fieldtouse = 'reconcilable';
630 }
631 if ($mode == 2) {
632 $fieldtouse = 'centralized';
633 }
634
635 if ($result > 0) {
636 $this->db->begin();
637
638 $sql = "UPDATE ".$this->db->prefix().$this->table_element;
639 $sql .= " SET ".$this->db->sanitize($fieldtouse)." = 0";
640 $sql .= " WHERE rowid = ".((int) $id);
641
642 dol_syslog(get_class($this)."::accountDeactivate ".$fieldtouse, LOG_DEBUG);
643 $result = $this->db->query($sql);
644
645 if ($result) {
646 $this->db->commit();
647 return 1;
648 } else {
649 $this->error = $this->db->lasterror();
650 $this->db->rollback();
651 return -1;
652 }
653 } else {
654 return -1;
655 }
656 }
657
658
666 public function accountActivate($id, $mode = 0)
667 {
668 // phpcs:enable
669 $this->db->begin();
670
671 $fieldtouse = 'active';
672 if ($mode == 1) {
673 $fieldtouse = 'reconcilable';
674 }
675 if ($mode == 2) {
676 $fieldtouse = 'centralized';
677 }
678
679 $sql = "UPDATE ".$this->db->prefix().$this->table_element;
680 $sql .= " SET ".$this->db->sanitize($fieldtouse)." = 1";
681 $sql .= " WHERE rowid = ".((int) $id);
682
683 dol_syslog(get_class($this)."::account_activate ".$fieldtouse, LOG_DEBUG);
684 $result = $this->db->query($sql);
685 if ($result) {
686 $this->db->commit();
687 return 1;
688 } else {
689 $this->error = $this->db->lasterror();
690 $this->db->rollback();
691 return -1;
692 }
693 }
694
701 public function getLibStatut($mode = 0)
702 {
703 return $this->LibStatut($this->status, $mode);
704 }
705
706 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
714 public function LibStatut($status, $mode = 0)
715 {
716 // phpcs:enable
717 if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
718 global $langs;
719 $langs->load("users");
720 $this->labelStatus[self::STATUS_ENABLED] = $langs->transnoentitiesnoconv('Enabled');
721 $this->labelStatus[self::STATUS_DISABLED] = $langs->transnoentitiesnoconv('Disabled');
722 $this->labelStatusShort[self::STATUS_ENABLED] = $langs->transnoentitiesnoconv('Enabled');
723 $this->labelStatusShort[self::STATUS_DISABLED] = $langs->transnoentitiesnoconv('Disabled');
724 }
725
726 $statusType = 'status4';
727 if ($status == self::STATUS_DISABLED) {
728 $statusType = 'status5';
729 }
730
731 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
732 }
733
751 public function getAccountingCodeToBind(Societe $buyer, Societe $seller, Product $product, $facture, $factureDet, $accountingAccount = array(), $type = '')
752 {
753 global $hookmanager;
754 // Instantiate hooks for external modules
755 $hookmanager->initHooks(array('accountancyBindingCalculation'));
756
757 // Execute hook accountancyBindingCalculation
758 $parameters = array('buyer' => $buyer, 'seller' => $seller, 'product' => $product, 'facture' => $facture, 'factureDet' => $factureDet ,'accountingAccount' => $accountingAccount, 0 => $type);
759 $reshook = $hookmanager->executeHooks('accountancyBindingCalculation', $parameters); // Note that $action and $object may have been modified by some hooks
760
761 $result = -1; // Init for static analysis
762 if (empty($reshook)) {
763 $const_name = '';
764 if ($type == 'customer') {
765 $const_name = "SOLD";
766 } elseif ($type == 'supplier') {
767 $const_name = "BUY";
768 }
769
770 require_once DOL_DOCUMENT_ROOT . '/core/lib/company.lib.php';
771 $isBuyerInEEC = isInEEC($buyer);
772 $isSellerInEEC = isInEEC($seller);
773 $code_l = ''; // Default value for generic product/service
774 $code_p = ''; // Value for the product/service in parameter ($product)
775 $code_t = ''; // Default value of product account for the thirdparty
776 $suggestedid = '';
777
778 // Level 1 (define $code_l): Search suggested default account for product/service
779 $suggestedaccountingaccountbydefaultfor = '';
780 if ($factureDet->product_type == 1) {
781 if ($buyer->country_code == $seller->country_code || empty($buyer->country_code)) { // If buyer in same country than seller (if not defined, we assume it is same country)
782 $code_l = getDolGlobalString('ACCOUNTING_SERVICE_' . $const_name . '_ACCOUNT');
783 $suggestedaccountingaccountbydefaultfor = '';
784 } else {
785 if ($isSellerInEEC && $isBuyerInEEC && $factureDet->tva_tx != 0) { // European intravat sale, but with a VAT
786 $code_l = getDolGlobalString('ACCOUNTING_SERVICE_' . $const_name . '_ACCOUNT');
787 } elseif ($isSellerInEEC && $isBuyerInEEC && empty($buyer->tva_intra)) { // European intravat sale, without VAT intra community number
788 $code_l = getDolGlobalString('ACCOUNTING_SERVICE_' . $const_name . '_ACCOUNT');
789 $suggestedaccountingaccountbydefaultfor = 'eecwithoutvatnumber';
790 } elseif ($isSellerInEEC && $isBuyerInEEC) { // European intravat sale
791 $code_l = getDolGlobalString('ACCOUNTING_SERVICE_' . $const_name . '_INTRA_ACCOUNT');
792 $suggestedaccountingaccountbydefaultfor = 'eec';
793 } else { // Foreign sale
794 $code_l = getDolGlobalString('ACCOUNTING_SERVICE_' . $const_name . '_EXPORT_ACCOUNT');
795 $suggestedaccountingaccountbydefaultfor = 'export';
796 }
797 }
798 } elseif ($factureDet->product_type == 0) {
799 if ($buyer->country_code == $seller->country_code || empty($buyer->country_code)) { // If buyer in same country than seller (if not defined, we assume it is same country)
800 $code_l = getDolGlobalString('ACCOUNTING_PRODUCT_' . $const_name . '_ACCOUNT');
801 $suggestedaccountingaccountbydefaultfor = '';
802 } else {
803 if ($isSellerInEEC && $isBuyerInEEC && $factureDet->tva_tx != 0) { // European intravat sale, but with a VAT
804 $code_l = getDolGlobalString('ACCOUNTING_PRODUCT_' . $const_name . '_ACCOUNT');
805 $suggestedaccountingaccountbydefaultfor = 'eecwithvat';
806 } elseif ($isSellerInEEC && $isBuyerInEEC && empty($buyer->tva_intra)) { // European intravat sale, without VAT intra community number
807 $code_l = getDolGlobalString('ACCOUNTING_PRODUCT_' . $const_name . '_ACCOUNT');
808 $suggestedaccountingaccountbydefaultfor = 'eecwithoutvatnumber';
809 } elseif ($isSellerInEEC && $isBuyerInEEC) { // European intravat sale
810 $code_l = getDolGlobalString('ACCOUNTING_PRODUCT_' . $const_name . '_INTRA_ACCOUNT');
811 $suggestedaccountingaccountbydefaultfor = 'eec';
812 } else {
813 $code_l = getDolGlobalString('ACCOUNTING_PRODUCT_' . $const_name . '_EXPORT_ACCOUNT');
814 $suggestedaccountingaccountbydefaultfor = 'export';
815 }
816 }
817 }
818 if ($code_l == -1) {
819 $code_l = '';
820 }
821
822 // Level 2 (define $code_p): Search suggested account for product/service (similar code exists in page index.php to make automatic binding)
823 $suggestedaccountingaccountfor = '';
824 if ((($buyer->country_code == $seller->country_code) || empty($buyer->country_code))) {
825 // If buyer in same country than seller (if not defined, we assume it is same country)
826 if ($type == 'customer' && !empty($product->accountancy_code_sell)) {
827 $code_p = $product->accountancy_code_sell;
828 } elseif ($type == 'supplier' && !empty($product->accountancy_code_buy)) {
829 $code_p = $product->accountancy_code_buy;
830 }
831 $suggestedid = $accountingAccount['dom'];
832 $suggestedaccountingaccountfor = 'prodserv';
833 } else {
834 if ($isSellerInEEC && $isBuyerInEEC && $factureDet->tva_tx != 0) {
835 // European intravat sale, but with VAT
836 if ($type == 'customer' && !empty($product->accountancy_code_sell)) {
837 $code_p = $product->accountancy_code_sell;
838 } elseif ($type == 'supplier' && !empty($product->accountancy_code_buy)) {
839 $code_p = $product->accountancy_code_buy;
840 }
841 $suggestedid = $accountingAccount['dom'];
842 $suggestedaccountingaccountfor = 'eecwithvat';
843 } elseif ($isSellerInEEC && $isBuyerInEEC && empty($buyer->tva_intra)) {
844 // European intravat sale, without VAT intra community number
845 if ($type == 'customer' && !empty($product->accountancy_code_sell)) {
846 $code_p = $product->accountancy_code_sell;
847 } elseif ($type == 'supplier' && !empty($product->accountancy_code_buy)) {
848 $code_p = $product->accountancy_code_buy;
849 }
850 $suggestedid = $accountingAccount['dom']; // There is a doubt for this case. Is it an error on vat or we just forgot to fill vat number ?
851 $suggestedaccountingaccountfor = 'eecwithoutvatnumber';
852 } elseif ($isSellerInEEC && $isBuyerInEEC && (($type == 'customer' && !empty($product->accountancy_code_sell_intra)) || ($type == 'supplier' && !empty($product->accountancy_code_buy_intra)))) {
853 // European intravat sale
854 if ($type == 'customer' && !empty($product->accountancy_code_sell_intra)) {
855 $code_p = $product->accountancy_code_sell_intra;
856 } elseif ($type == 'supplier' && !empty($product->accountancy_code_buy_intra)) {
857 $code_p = $product->accountancy_code_buy_intra;
858 }
859 $suggestedid = $accountingAccount['intra'];
860 $suggestedaccountingaccountfor = 'eec';
861 } else {
862 // Foreign sale
863 if ($type == 'customer' && !empty($product->accountancy_code_sell_export)) {
864 $code_p = $product->accountancy_code_sell_export;
865 } elseif ($type == 'supplier' && !empty($product->accountancy_code_buy_export)) {
866 $code_p = $product->accountancy_code_buy_export;
867 }
868 $suggestedid = $accountingAccount['export'];
869 $suggestedaccountingaccountfor = 'export';
870 }
871 }
872
873 // Level 3 (define $code_t): Search suggested account for this thirdparty (similar code exists in page index.php to make automatic binding)
874 if (getDolGlobalString('ACCOUNTANCY_USE_PRODUCT_ACCOUNT_ON_THIRDPARTY')) {
875 if ($type == 'customer' && !empty($buyer->code_compta_product)) {
876 $code_t = $buyer->code_compta_product;
877 $suggestedid = $accountingAccount['thirdparty'];
878 $suggestedaccountingaccountfor = 'thirdparty';
879 } elseif ($type == 'supplier' && !empty($seller->code_compta_product)) {
880 $code_t = $seller->code_compta_product;
881 $suggestedid = $accountingAccount['thirdparty'];
882 $suggestedaccountingaccountfor = 'thirdparty';
883 }
884 }
885
886 // Manage Deposit
887 $account_deposit = getDolGlobalString('ACCOUNTING_ACCOUNT_' . strtoupper($type) . '_DEPOSIT');
888 if (!empty($account_deposit) && $account_deposit != '-1') {
889 if ($factureDet->desc == "(DEPOSIT)" || $facture->type == $facture::TYPE_DEPOSIT) {
890 $accountdeposittoventilated = new self($this->db);
891 if ($type == 'customer') {
892 $result = $accountdeposittoventilated->fetch(0, getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER_DEPOSIT'), 1);
893 } elseif ($type == 'supplier') {
894 $result = $accountdeposittoventilated->fetch(0, getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER_DEPOSIT'), 1);
895 }
896 if (isset($result) && $result < 0) {
897 return -1;
898 }
899
900 $code_l = (string) $accountdeposittoventilated->ref;
901 $code_p = '';
902 $code_t = '';
903 $suggestedid = (int) $accountdeposittoventilated->rowid;
904 $suggestedaccountingaccountfor = 'deposit';
905 }
906
907 // For credit note invoice, if origin invoice is a deposit invoice, force also on specific customer/supplier deposit account
908 if (!empty($facture->fk_facture_source)) {
909 $invoiceSource = new $facture($this->db);
910 $invoiceSource->fetch($facture->fk_facture_source);
911
912 if ($facture->type == $facture::TYPE_CREDIT_NOTE && $invoiceSource->type == $facture::TYPE_DEPOSIT) {
913 $accountdeposittoventilated = new self($this->db);
914 if ($type == 'customer') {
915 $accountdeposittoventilated->fetch(0, getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER_DEPOSIT'), 1);
916 } elseif ($type == 'supplier') {
917 $accountdeposittoventilated->fetch(0, getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER_DEPOSIT'), 1);
918 }
919 $code_l = (string) $accountdeposittoventilated->ref;
920 $code_p = '';
921 $code_t = '';
922 $suggestedid = (int) $accountdeposittoventilated->rowid;
923 $suggestedaccountingaccountfor = 'deposit';
924 }
925 }
926 }
927
928 // If $suggestedid could not be guessed yet, we set it from the generic default accounting code $code_l
929 if (empty($suggestedid) && empty($code_p) && !empty($code_l) && !getDolGlobalString('ACCOUNTANCY_DO_NOT_AUTOFILL_ACCOUNT_WITH_GENERIC')) {
930 if (empty($this->accountingaccount_codetotid_cache[$code_l])) {
931 $tmpaccount = new self($this->db);
932 $result = $tmpaccount->fetch(0, $code_l, 1);
933 if ($result < 0) {
934 return -1;
935 }
936 if ($tmpaccount->id > 0) {
937 $suggestedid = $tmpaccount->id;
938 }
939 $this->accountingaccount_codetotid_cache[$code_l] = $tmpaccount->id;
940 } else {
941 $suggestedid = $this->accountingaccount_codetotid_cache[$code_l];
942 }
943 }
944 $return = array(
945 'suggestedaccountingaccountbydefaultfor' => $suggestedaccountingaccountbydefaultfor,
946 'suggestedaccountingaccountfor' => $suggestedaccountingaccountfor,
947 'suggestedid' => $suggestedid,
948 'code_l' => $code_l,
949 'code_p' => $code_p,
950 'code_t' => $code_t,
951 );
952
953 // Execute hook afterAccountancyBindingCalculation
954 // Allows a module to adjust the result computed above, instead of having to replace the whole
955 // calculation with the accountancyBindingCalculation hook. A key returned by the hook overwrites
956 // the computed one, a key left out keeps its computed value.
957 $parameters['return'] = $return;
958 $reshookafter = $hookmanager->executeHooks('afterAccountancyBindingCalculation', $parameters); // Note that $action and $object may have been modified by some hooks
959 if ($reshookafter < 0) {
960 $this->error = $hookmanager->error;
961 $this->errors = $hookmanager->errors;
962 return -1;
963 }
964 if (array_key_exists('suggestedaccountingaccountbydefaultfor', $hookmanager->resArray)) {
965 $return['suggestedaccountingaccountbydefaultfor'] = $hookmanager->resArray['suggestedaccountingaccountbydefaultfor'];
966 }
967 if (array_key_exists('suggestedaccountingaccountfor', $hookmanager->resArray)) {
968 $return['suggestedaccountingaccountfor'] = $hookmanager->resArray['suggestedaccountingaccountfor'];
969 }
970 if (array_key_exists('suggestedid', $hookmanager->resArray)) {
971 $return['suggestedid'] = $hookmanager->resArray['suggestedid'];
972 }
973 if (array_key_exists('code_l', $hookmanager->resArray)) {
974 $return['code_l'] = $hookmanager->resArray['code_l'];
975 }
976 if (array_key_exists('code_p', $hookmanager->resArray)) {
977 $return['code_p'] = $hookmanager->resArray['code_p'];
978 }
979 if (array_key_exists('code_t', $hookmanager->resArray)) {
980 $return['code_t'] = $hookmanager->resArray['code_t'];
981 }
982
983 return $return;
984 } else {
985 if (is_array($hookmanager->resArray) && !empty($hookmanager->resArray)) {
986 return $hookmanager->resArray;
987 }
988 }
989
990 return -1;
991 }
992}
length_accountg($account)
Return General accounting account with defined length (used for product and miscellaneous)
$object ref
Definition info.php:90
Class to manage accounting accounts.
LibStatut($status, $mode=0)
Return the label of a given status.
accountDeactivate($id, $mode=0)
Deactivate an account (for status active or status reconcilable)
checkUsage()
Check usage of accounting code.
update($user)
Update record.
getNomUrl($withpicto=0, $withlabel=0, $nourl=0, $moretitle='', $notooltip=0, $save_lastsearch_value=-1, $withcompletelabel=0, $option='')
Return clickable name (with picto eventually)
create($user, $notrigger=0)
Insert new accounting account in chart of accounts.
accountActivate($id, $mode=0)
Account activated.
fetch($rowid=0, $account_number=null, $limittocurrentchart=0, $limittoachartaccount='')
Load record in memory.
info($id)
Information on record.
getAccountingCodeToBind(Societe $buyer, Societe $seller, Product $product, $facture, $factureDet, $accountingAccount=array(), $type='')
Return a suggested account (from chart of accounts) to bind.
getLibStatut($mode=0)
Return the label of the status.
Class to manage products or services.
Class to manage third parties objects (customers, suppliers, prospects...)
print $langs trans("Ref").' m titre as m m statut as status
Or an array listing all the potential status of the object: array: int of the status => translated la...
Definition index.php:169
isInEEC($object)
Return if a country of an object is inside the EEC (European Economic Community)
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.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
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.
editval_textarea active
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $allowothertags=array())
Show a picto called object_picto (generic function)
dolGetStatus($statusLabel='', $statusLabelShort='', $html='', $statusType='status0', $displayMode=0, $url='', $params=array())
Output the badge of a status.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input f...
Definition html.lib.php:172
print $langs trans("Show") . '< td style="' . $timeColor . '" align="center"> s</td > badge status0 badge status4 badge status3 Error badge status8< td align="center">< span class="badge ' . $badge . '"></span ></td >< td align="center">< a href="#" class="button button-small" onclick="openLogModal(this)" data-req="' . dol_escape_htmltag($reqSafe) . '" data-res="' . dol_escape_htmltag($resSafe) . '" data-err="' . dol_escape_htmltag($errSafe) . '">< span class="fa fa-search-plus"></span ></a ></td ></tr >< tr >< td colspan="' . $colspan . '" class="opacitymedium"></td ></tr ></table ></div ></form > logModal none logModal none s a JSON string