dolibarr 23.0.3
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 MDW <mdeweerd@users.noreply.github.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 */
23
30require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
31require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
32require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
33
38{
42 public $element = 'accounting_account';
43
47 public $table_element = 'accounting_account';
48
52 public $picto = 'billr';
53
58 public $restrictiononfksoc = 1;
59
63 public $db;
64
68 public $id;
69
73 public $rowid;
74
80 public $datec;
81
85 public $fk_pcg_version;
86
90 public $pcg_type;
91
95 public $account_number;
96
100 public $account_parent;
101
105 public $account_category;
106
110 public $account_category_label;
111
115 public $status;
116
120 public $label;
121
125 public $labelshort;
126
130 public $fk_user_author;
131
135 public $fk_user_modif;
136
140 public $active;
141
145 public $reconcilable;
146
150 private $accountingaccount_codetotid_cache = array();
151
152
153 const STATUS_ENABLED = 1;
154 const STATUS_DISABLED = 0;
155
156
162 public function __construct($db)
163 {
164 $this->db = $db;
165
166 $this->ismultientitymanaged = 1;
167 $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
168 }
169
179 public function fetch($rowid = 0, $account_number = null, $limittocurrentchart = 0, $limittoachartaccount = '')
180 {
181 global $conf;
182
183 if ($rowid || $account_number) {
184 $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";
185 $sql .= ", ca.label as category_label";
186 $sql .= " FROM ".$this->db->prefix().$this->table_element." as a";
187 $sql .= " LEFT JOIN ".$this->db->prefix()."c_accounting_category as ca ON a.fk_accounting_category = ca.rowid";
188 $sql .= " WHERE";
189 if ($rowid) {
190 $sql .= " a.rowid = ".(int) $rowid;
191 } elseif ($account_number) {
192 $sql .= " a.account_number = '".$this->db->escape($account_number)."'";
193 $sql .= " AND a.entity = ".$conf->entity;
194 }
195 if (!empty($limittocurrentchart)) {
196 $sql .= ' AND a.fk_pcg_version IN (SELECT pcg_version FROM '.$this->db->prefix().'accounting_system WHERE rowid = '.((int) getDolGlobalInt('CHARTOFACCOUNTS')).')';
197 }
198 if (!empty($limittoachartaccount)) {
199 $sql .= " AND a.fk_pcg_version = '".$this->db->escape($limittoachartaccount)."'";
200 }
201
202 dol_syslog(get_class($this)."::fetch rowid=".$rowid." account_number=".$account_number, LOG_DEBUG);
203
204 $result = $this->db->query($sql);
205 if ($result) {
206 $obj = $this->db->fetch_object($result);
207
208 if ($obj) {
209 $this->id = $obj->rowid;
210 $this->rowid = $obj->rowid;
211 $this->ref = $obj->account_number;
212 $this->datec = $this->db->jdate($obj->datec);
213 $this->date_creation = $this->db->jdate($obj->datec);
214 $this->date_modification = $this->db->jdate($obj->tms);
215 //$this->tms = $this->datem;
216 $this->fk_pcg_version = $obj->fk_pcg_version;
217 $this->pcg_type = $obj->pcg_type;
218 $this->account_number = $obj->account_number;
219 $this->account_parent = $obj->account_parent;
220 $this->label = $obj->label;
221 $this->labelshort = $obj->labelshort;
222 $this->account_category = $obj->fk_accounting_category;
223 $this->account_category_label = $obj->category_label;
224 $this->fk_user_author = $obj->fk_user_author;
225 $this->fk_user_modif = $obj->fk_user_modif;
226 $this->active = $obj->active;
227 $this->status = $obj->active;
228 $this->reconcilable = $obj->reconcilable;
229
230 return $this->id;
231 } else {
232 return 0;
233 }
234 } else {
235 $this->error = "Error ".$this->db->lasterror();
236 $this->errors[] = "Error ".$this->db->lasterror();
237 }
238 }
239 return -1;
240 }
241
249 public function create($user, $notrigger = 0)
250 {
251 global $conf;
252 $error = 0;
253 $now = dol_now();
254
255 // Clean parameters
256 if (isset($this->fk_pcg_version)) {
257 $this->fk_pcg_version = trim($this->fk_pcg_version);
258 }
259 if (isset($this->pcg_type)) {
260 $this->pcg_type = trim($this->pcg_type);
261 }
262 if (isset($this->account_number)) {
263 $this->account_number = trim($this->account_number);
264 }
265 if (isset($this->label)) {
266 $this->label = trim($this->label);
267 }
268 if (isset($this->labelshort)) {
269 $this->labelshort = trim($this->labelshort);
270 }
271
272 if (empty($this->pcg_type) || $this->pcg_type == '-1') {
273 $this->pcg_type = 'XXXXXX';
274 }
275 // Check parameters
276 // Put here code to add control on parameters values
277
278 // Insert request
279 $sql = "INSERT INTO " . $this->db->prefix() . $this->table_element . " (";
280 $sql .= "datec";
281 $sql .= ", entity";
282 $sql .= ", fk_pcg_version";
283 $sql .= ", pcg_type";
284 $sql .= ", account_number";
285 $sql .= ", account_parent";
286 $sql .= ", label";
287 $sql .= ", labelshort";
288 $sql .= ", fk_accounting_category";
289 $sql .= ", fk_user_author";
290 $sql .= ", active";
291 $sql .= ", reconcilable";
292 $sql .= ") VALUES (";
293 $sql .= " '".$this->db->idate($now)."'";
294 $sql .= ", ".((int) $conf->entity);
295 $sql .= ", ".(empty($this->fk_pcg_version) ? 'NULL' : "'".$this->db->escape($this->fk_pcg_version)."'");
296 $sql .= ", ".(empty($this->pcg_type) ? 'NULL' : "'".$this->db->escape($this->pcg_type)."'");
297 $sql .= ", ".(empty($this->account_number) ? 'NULL' : "'".$this->db->escape($this->account_number)."'");
298 $sql .= ", ".(empty($this->account_parent) ? 0 : (int) $this->account_parent);
299 $sql .= ", ".(empty($this->label) ? "''" : "'".$this->db->escape($this->label)."'");
300 $sql .= ", ".(empty($this->labelshort) ? "''" : "'".$this->db->escape($this->labelshort)."'");
301 $sql .= ", ".(empty($this->account_category) ? 0 : (int) $this->account_category);
302 $sql .= ", ".((int) $user->id);
303 $sql .= ", ".(int) $this->active;
304 $sql .= ", ".(int) $this->reconcilable;
305 $sql .= ")";
306
307 $this->db->begin();
308
309 dol_syslog(get_class($this)."::create", LOG_DEBUG);
310 $resql = $this->db->query($sql);
311 if (!$resql) {
312 $error++;
313 $this->errors[] = "Error " . $this->db->lasterror();
314 }
315
316 if (!$error) {
317 $this->id = $this->db->last_insert_id($this->db->prefix() . $this->table_element);
318
319 // Uncomment this and change MYOBJECT to your own tag if you
320 // want this action to call a trigger.
321 //if (! $error && ! $notrigger) {
322
323 // // Call triggers
324 // $result=$this->call_trigger('MYOBJECT_CREATE',$user);
325 // if ($result < 0) $error++;
326 // // End call triggers
327 //}
328 }
329
330 // Commit or rollback
331 if ($error) {
332 foreach ($this->errors as $errmsg) {
333 dol_syslog(get_class($this) . "::create " . $errmsg, LOG_ERR);
334 $this->error .= ($this->error ? ', ' . $errmsg : $errmsg);
335 }
336 $this->db->rollback();
337 return -1 * $error;
338 } else {
339 $this->db->commit();
340 return $this->id;
341 }
342 }
343
350 public function update($user)
351 {
352 // Check parameters
353 if (empty($this->pcg_type) || $this->pcg_type == '-1') {
354 $this->pcg_type = 'XXXXXX';
355 }
356
357 $this->db->begin();
358
359 $sql = "UPDATE " . $this->db->prefix() . $this->table_element;
360 $sql .= " SET fk_pcg_version = " . ($this->fk_pcg_version ? "'" . $this->db->escape($this->fk_pcg_version) . "'" : "null");
361 $sql .= " , pcg_type = '" . $this->db->escape($this->pcg_type) . "'";
362 $sql .= " , account_number = '" . $this->db->escape($this->account_number) . "'";
363 $sql .= " , account_parent = " . (int) $this->account_parent;
364 $sql .= " , label = " . ($this->label ? "'" . $this->db->escape($this->label) . "'" : "''");
365 $sql .= " , labelshort = " . ($this->labelshort ? "'" . $this->db->escape($this->labelshort) . "'" : "''");
366 $sql .= " , fk_accounting_category = " . (empty($this->account_category) ? 0 : (int) $this->account_category);
367 $sql .= " , fk_user_modif = " . ((int) $user->id);
368 $sql .= " , active = " . (int) $this->active;
369 $sql .= " , reconcilable = " . (int) $this->reconcilable;
370 $sql .= " WHERE rowid = " . ((int) $this->id);
371
372 dol_syslog(get_class($this)."::update", LOG_DEBUG);
373 $result = $this->db->query($sql);
374 if ($result) {
375 $this->db->commit();
376 return 1;
377 } else {
378 if ($this->db->lasterrno() == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
379 $this->error = $this->db->lasterror();
380 $this->db->rollback();
381 return -2;
382 }
383
384 $this->error = $this->db->lasterror();
385 $this->db->rollback();
386 return -1;
387 }
388 }
389
395 public function checkUsage()
396 {
397 global $langs;
398
399 // TODO Looks a stupid check
400 $sql = "(SELECT fk_code_ventilation FROM ".$this->db->prefix()."facturedet";
401 $sql .= " WHERE fk_code_ventilation=".((int) $this->id).")";
402 $sql .= "UNION";
403 $sql .= " (SELECT fk_code_ventilation FROM ".$this->db->prefix()."facture_fourn_det";
404 $sql .= " WHERE fk_code_ventilation=".((int) $this->id).")";
405
406 dol_syslog(get_class($this)."::checkUsage", LOG_DEBUG);
407 $resql = $this->db->query($sql);
408
409 if ($resql) {
410 $num = $this->db->num_rows($resql);
411 if ($num > 0) {
412 $this->error = $langs->trans('ErrorAccountancyCodeIsAlreadyUse');
413 return 0;
414 } else {
415 return 1;
416 }
417 } else {
418 $this->error = $this->db->lasterror();
419 return -1;
420 }
421 }
422
430 public function delete($user, $notrigger = 0)
431 {
432 $error = 0;
433
434 $result = $this->checkUsage();
435
436 if ($result > 0) {
437 $this->db->begin();
438
439 $sql = "DELETE FROM " . $this->db->prefix() . $this->table_element;
440 $sql .= " WHERE rowid=" . ((int) $this->id);
441
442 dol_syslog(get_class($this) . "::delete sql=" . $sql);
443 $resql = $this->db->query($sql);
444 if (!$resql) {
445 $error++;
446 $this->errors[] = "Error " . $this->db->lasterror();
447 }
448
449 // Commit or rollback
450 if ($error) {
451 foreach ($this->errors as $errmsg) {
452 dol_syslog(get_class($this) . "::delete " . $errmsg, LOG_ERR);
453 $this->error .= ($this->error ? ', ' . $errmsg : $errmsg);
454 }
455 $this->db->rollback();
456 return -1 * $error;
457 } else {
458 $this->db->commit();
459 return 1;
460 }
461 } else {
462 return -1;
463 }
464 }
465
479 public function getNomUrl($withpicto = 0, $withlabel = 0, $nourl = 0, $moretitle = '', $notooltip = 0, $save_lastsearch_value = -1, $withcompletelabel = 0, $option = '')
480 {
481 global $langs, $conf, $hookmanager;
482 require_once DOL_DOCUMENT_ROOT . '/core/lib/accounting.lib.php';
483
484 if (!empty($conf->dol_no_mouse_hover)) {
485 $notooltip = 1; // Force disable tooltips
486 }
487
488 $result = '';
489
490 $url = '';
491 $labelurl = '';
492 if (empty($option) || $option == 'ledger') {
493 $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 : ''));
494 $labelurl = $langs->trans("ShowAccountingAccountInLedger");
495 } elseif ($option == 'journals') {
496 $url = DOL_URL_ROOT . '/accountancy/bookkeeping/list.php?search_accountancy_code_start=' . urlencode($this->account_number) . '&search_accountancy_code_end=' . urlencode($this->account_number);
497 $labelurl = $langs->trans("ShowAccountingAccountInJournals");
498 } elseif ($option == 'accountcard') {
499 $url = DOL_URL_ROOT . '/accountancy/admin/card.php?id=' . urlencode((string) ($this->id));
500 $labelurl = $langs->trans("ShowAccountingAccount");
501 }
502
503 // Add param to save lastsearch_values or not
504 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
505 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
506 $add_save_lastsearch_values = 1;
507 }
508 if ($add_save_lastsearch_values) {
509 $url .= '&save_lastsearch_values=1';
510 }
511
512 $picto = 'accounting_account';
513 $label = '';
514
515 if (empty($this->labelshort) || $withcompletelabel == 1) {
516 $labeltoshow = $this->label;
517 } else {
518 $labeltoshow = $this->labelshort;
519 }
520
521 $label = '<u>' . $labelurl . '</u>';
522 if (!empty($this->account_number)) {
523 $label .= '<br><b>' . $langs->trans('AccountAccounting') . ':</b> ' . length_accountg($this->account_number);
524 }
525 if (!empty($labeltoshow)) {
526 $label .= '<br><b>' . $langs->trans('Label') . ':</b> ' . $labeltoshow;
527 }
528 if ($moretitle) {
529 $label .= ' - ' . $moretitle;
530 }
531
532 $linkclose = '';
533 if (empty($notooltip)) {
534 if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
535 $label = $labelurl;
536 $linkclose .= ' alt="' . dol_escape_htmltag($label, 1) . '"';
537 }
538 $linkclose .= ' title="' . dol_escape_htmltag($label, 1) . '"';
539 $linkclose .= ' class="classfortooltip"';
540 }
541
542 $linkstart = '<a href="' . $url . '"';
543 $linkstart .= $linkclose . '>';
544 $linkend = '</a>';
545
546 if ($nourl) {
547 $linkstart = '';
548 $linkclose = '';
549 $linkend = '';
550 }
551
552 $label_link = length_accountg($this->account_number);
553 if ($withlabel) {
554 $label_link .= ' - ' . ($nourl ? '<span class="opacitymedium">' : '') . $labeltoshow . ($nourl ? '</span>' : '');
555 }
556
557 if ($withpicto) {
558 $result .= ($linkstart . img_object(($notooltip ? '' : $label), $picto, ($notooltip ? '' : 'class="classfortooltip"'), 0, 0, $notooltip ? 0 : 1) . $linkend);
559 }
560 if ($withpicto && $withpicto != 2) {
561 $result .= ' ';
562 }
563 if ($withpicto != 2) {
564 $result .= $linkstart . $label_link . $linkend;
565 }
566 global $action;
567 $hookmanager->initHooks(array($this->element . 'dao'));
568 $parameters = array('id' => $this->id, 'getnomurl' => &$result);
569 $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
570 if ($reshook > 0) {
571 $result = $hookmanager->resPrint;
572 } else {
573 $result .= $hookmanager->resPrint;
574 }
575 return $result;
576 }
577
584 public function info($id)
585 {
586 $sql = 'SELECT a.rowid, a.datec, a.fk_user_author, a.fk_user_modif, a.tms as date_modification';
587 $sql .= ' FROM ' . $this->db->prefix() . $this->table_element . ' as a';
588 $sql .= ' WHERE a.rowid = ' . ((int) $id);
589
590 dol_syslog(get_class($this) . '::info sql=' . $sql);
591 $resql = $this->db->query($sql);
592
593 if ($resql) {
594 if ($this->db->num_rows($resql)) {
595 $obj = $this->db->fetch_object($resql);
596
597 $this->id = $obj->rowid;
598
599 $this->user_creation_id = $obj->fk_user_author;
600 $this->user_modification_id = $obj->fk_user_modif;
601 $this->date_creation = $this->db->jdate($obj->datec);
602 $this->date_modification = $this->db->jdate($obj->date_modification);
603 }
604 $this->db->free($resql);
605 } else {
606 dol_print_error($this->db);
607 }
608 }
609
617 public function accountDeactivate($id, $mode = 0)
618 {
619 $result = $this->checkUsage();
620
621 $fieldtouse = 'active';
622 if ($mode == 1) {
623 $fieldtouse = 'reconcilable';
624 }
625 if ($mode == 2) {
626 $fieldtouse = 'centralized';
627 }
628
629 if ($result > 0) {
630 $this->db->begin();
631
632 $sql = "UPDATE ".$this->db->prefix().$this->table_element;
633 $sql .= " SET ".$this->db->sanitize($fieldtouse)." = 0";
634 $sql .= " WHERE rowid = ".((int) $id);
635
636 dol_syslog(get_class($this)."::accountDeactivate ".$fieldtouse, LOG_DEBUG);
637 $result = $this->db->query($sql);
638
639 if ($result) {
640 $this->db->commit();
641 return 1;
642 } else {
643 $this->error = $this->db->lasterror();
644 $this->db->rollback();
645 return -1;
646 }
647 } else {
648 return -1;
649 }
650 }
651
652
660 public function accountActivate($id, $mode = 0)
661 {
662 // phpcs:enable
663 $this->db->begin();
664
665 $fieldtouse = 'active';
666 if ($mode == 1) {
667 $fieldtouse = 'reconcilable';
668 }
669 if ($mode == 2) {
670 $fieldtouse = 'centralized';
671 }
672
673 $sql = "UPDATE ".$this->db->prefix().$this->table_element;
674 $sql .= " SET ".$this->db->sanitize($fieldtouse)." = 1";
675 $sql .= " WHERE rowid = ".((int) $id);
676
677 dol_syslog(get_class($this)."::account_activate ".$fieldtouse, LOG_DEBUG);
678 $result = $this->db->query($sql);
679 if ($result) {
680 $this->db->commit();
681 return 1;
682 } else {
683 $this->error = $this->db->lasterror();
684 $this->db->rollback();
685 return -1;
686 }
687 }
688
695 public function getLibStatut($mode = 0)
696 {
697 return $this->LibStatut($this->status, $mode);
698 }
699
700 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
708 public function LibStatut($status, $mode = 0)
709 {
710 // phpcs:enable
711 if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
712 global $langs;
713 $langs->load("users");
714 $this->labelStatus[self::STATUS_ENABLED] = $langs->transnoentitiesnoconv('Enabled');
715 $this->labelStatus[self::STATUS_DISABLED] = $langs->transnoentitiesnoconv('Disabled');
716 $this->labelStatusShort[self::STATUS_ENABLED] = $langs->transnoentitiesnoconv('Enabled');
717 $this->labelStatusShort[self::STATUS_DISABLED] = $langs->transnoentitiesnoconv('Disabled');
718 }
719
720 $statusType = 'status4';
721 if ($status == self::STATUS_DISABLED) {
722 $statusType = 'status5';
723 }
724
725 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
726 }
727
742 public function getAccountingCodeToBind(Societe $buyer, Societe $seller, Product $product, $facture, $factureDet, $accountingAccount = array(), $type = '')
743 {
744 global $hookmanager;
745 // Instantiate hooks for external modules
746 $hookmanager->initHooks(array('accountancyBindingCalculation'));
747
748 // Execute hook accountancyBindingCalculation
749 $parameters = array('buyer' => $buyer, 'seller' => $seller, 'product' => $product, 'facture' => $facture, 'factureDet' => $factureDet ,'accountingAccount' => $accountingAccount, 0 => $type);
750 $reshook = $hookmanager->executeHooks('accountancyBindingCalculation', $parameters); // Note that $action and $object may have been modified by some hooks
751
752 $result = -1; // Init for static analysis
753 if (empty($reshook)) {
754 $const_name = '';
755 if ($type == 'customer') {
756 $const_name = "SOLD";
757 } elseif ($type == 'supplier') {
758 $const_name = "BUY";
759 }
760
761 require_once DOL_DOCUMENT_ROOT . '/core/lib/company.lib.php';
762 $isBuyerInEEC = isInEEC($buyer);
763 $isSellerInEEC = isInEEC($seller);
764 $code_l = ''; // Default value for generic product/service
765 $code_p = ''; // Value for the product/service in parameter ($product)
766 $code_t = ''; // Default value of product account for the thirdparty
767 $suggestedid = '';
768
769 // Level 1 (define $code_l): Search suggested default account for product/service
770 $suggestedaccountingaccountbydefaultfor = '';
771 if ($factureDet->product_type == 1) {
772 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)
773 $code_l = getDolGlobalString('ACCOUNTING_SERVICE_' . $const_name . '_ACCOUNT');
774 $suggestedaccountingaccountbydefaultfor = '';
775 } else {
776 if ($isSellerInEEC && $isBuyerInEEC && $factureDet->tva_tx != 0) { // European intravat sale, but with a VAT
777 $code_l = getDolGlobalString('ACCOUNTING_SERVICE_' . $const_name . '_ACCOUNT');
778 } elseif ($isSellerInEEC && $isBuyerInEEC && empty($buyer->tva_intra)) { // European intravat sale, without VAT intra community number
779 $code_l = getDolGlobalString('ACCOUNTING_SERVICE_' . $const_name . '_ACCOUNT');
780 $suggestedaccountingaccountbydefaultfor = 'eecwithoutvatnumber';
781 } elseif ($isSellerInEEC && $isBuyerInEEC) { // European intravat sale
782 $code_l = getDolGlobalString('ACCOUNTING_SERVICE_' . $const_name . '_INTRA_ACCOUNT');
783 $suggestedaccountingaccountbydefaultfor = 'eec';
784 } else { // Foreign sale
785 $code_l = getDolGlobalString('ACCOUNTING_SERVICE_' . $const_name . '_EXPORT_ACCOUNT');
786 $suggestedaccountingaccountbydefaultfor = 'export';
787 }
788 }
789 } elseif ($factureDet->product_type == 0) {
790 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)
791 $code_l = getDolGlobalString('ACCOUNTING_PRODUCT_' . $const_name . '_ACCOUNT');
792 $suggestedaccountingaccountbydefaultfor = '';
793 } else {
794 if ($isSellerInEEC && $isBuyerInEEC && $factureDet->tva_tx != 0) { // European intravat sale, but with a VAT
795 $code_l = getDolGlobalString('ACCOUNTING_PRODUCT_' . $const_name . '_ACCOUNT');
796 $suggestedaccountingaccountbydefaultfor = 'eecwithvat';
797 } elseif ($isSellerInEEC && $isBuyerInEEC && empty($buyer->tva_intra)) { // European intravat sale, without VAT intra community number
798 $code_l = getDolGlobalString('ACCOUNTING_PRODUCT_' . $const_name . '_ACCOUNT');
799 $suggestedaccountingaccountbydefaultfor = 'eecwithoutvatnumber';
800 } elseif ($isSellerInEEC && $isBuyerInEEC) { // European intravat sale
801 $code_l = getDolGlobalString('ACCOUNTING_PRODUCT_' . $const_name . '_INTRA_ACCOUNT');
802 $suggestedaccountingaccountbydefaultfor = 'eec';
803 } else {
804 $code_l = getDolGlobalString('ACCOUNTING_PRODUCT_' . $const_name . '_EXPORT_ACCOUNT');
805 $suggestedaccountingaccountbydefaultfor = 'export';
806 }
807 }
808 }
809 if ($code_l == -1) {
810 $code_l = '';
811 }
812
813 // Level 2 (define $code_p): Search suggested account for product/service (similar code exists in page index.php to make automatic binding)
814 $suggestedaccountingaccountfor = '';
815 if ((($buyer->country_code == $seller->country_code) || empty($buyer->country_code))) {
816 // If buyer in same country than seller (if not defined, we assume it is same country)
817 if ($type == 'customer' && !empty($product->accountancy_code_sell)) {
818 $code_p = $product->accountancy_code_sell;
819 } elseif ($type == 'supplier' && !empty($product->accountancy_code_buy)) {
820 $code_p = $product->accountancy_code_buy;
821 }
822 $suggestedid = $accountingAccount['dom'];
823 $suggestedaccountingaccountfor = 'prodserv';
824 } else {
825 if ($isSellerInEEC && $isBuyerInEEC && $factureDet->tva_tx != 0) {
826 // European intravat sale, but with VAT
827 if ($type == 'customer' && !empty($product->accountancy_code_sell)) {
828 $code_p = $product->accountancy_code_sell;
829 } elseif ($type == 'supplier' && !empty($product->accountancy_code_buy)) {
830 $code_p = $product->accountancy_code_buy;
831 }
832 $suggestedid = $accountingAccount['dom'];
833 $suggestedaccountingaccountfor = 'eecwithvat';
834 } elseif ($isSellerInEEC && $isBuyerInEEC && empty($buyer->tva_intra)) {
835 // European intravat sale, without VAT intra community number
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']; // There is a doubt for this case. Is it an error on vat or we just forgot to fill vat number ?
842 $suggestedaccountingaccountfor = 'eecwithoutvatnumber';
843 } elseif ($isSellerInEEC && $isBuyerInEEC && (($type == 'customer' && !empty($product->accountancy_code_sell_intra)) || ($type == 'supplier' && !empty($product->accountancy_code_buy_intra)))) {
844 // European intravat sale
845 if ($type == 'customer' && !empty($product->accountancy_code_sell_intra)) {
846 $code_p = $product->accountancy_code_sell_intra;
847 } elseif ($type == 'supplier' && !empty($product->accountancy_code_buy_intra)) {
848 $code_p = $product->accountancy_code_buy_intra;
849 }
850 $suggestedid = $accountingAccount['intra'];
851 $suggestedaccountingaccountfor = 'eec';
852 } else {
853 // Foreign sale
854 if ($type == 'customer' && !empty($product->accountancy_code_sell_export)) {
855 $code_p = $product->accountancy_code_sell_export;
856 } elseif ($type == 'supplier' && !empty($product->accountancy_code_buy_export)) {
857 $code_p = $product->accountancy_code_buy_export;
858 }
859 $suggestedid = $accountingAccount['export'];
860 $suggestedaccountingaccountfor = 'export';
861 }
862 }
863
864 // Level 3 (define $code_t): Search suggested account for this thirdparty (similar code exists in page index.php to make automatic binding)
865 if (getDolGlobalString('ACCOUNTANCY_USE_PRODUCT_ACCOUNT_ON_THIRDPARTY')) {
866 if ($type == 'customer' && !empty($buyer->code_compta_product)) {
867 $code_t = $buyer->code_compta_product;
868 $suggestedid = $accountingAccount['thirdparty'];
869 $suggestedaccountingaccountfor = 'thirdparty';
870 } elseif ($type == 'supplier' && !empty($seller->code_compta_product)) {
871 $code_t = $seller->code_compta_product;
872 $suggestedid = $accountingAccount['thirdparty'];
873 $suggestedaccountingaccountfor = 'thirdparty';
874 }
875 }
876
877 // Manage Deposit
878 $account_deposit = getDolGlobalString('ACCOUNTING_ACCOUNT_' . strtoupper($type) . '_DEPOSIT');
879 if (!empty($account_deposit) && $account_deposit != '-1') {
880 if ($factureDet->desc == "(DEPOSIT)" || $facture->type == $facture::TYPE_DEPOSIT) {
881 $accountdeposittoventilated = new self($this->db);
882 if ($type == 'customer') {
883 $result = $accountdeposittoventilated->fetch(0, getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER_DEPOSIT'), 1);
884 } elseif ($type == 'supplier') {
885 $result = $accountdeposittoventilated->fetch(0, getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER_DEPOSIT'), 1);
886 }
887 if (isset($result) && $result < 0) {
888 return -1;
889 }
890
891 $code_l = (string) $accountdeposittoventilated->ref;
892 $code_p = '';
893 $code_t = '';
894 $suggestedid = (int) $accountdeposittoventilated->rowid;
895 $suggestedaccountingaccountfor = 'deposit';
896 }
897
898 // For credit note invoice, if origin invoice is a deposit invoice, force also on specific customer/supplier deposit account
899 if (!empty($facture->fk_facture_source)) {
900 $invoiceSource = new $facture($this->db);
901 $invoiceSource->fetch($facture->fk_facture_source);
902
903 if ($facture->type == $facture::TYPE_CREDIT_NOTE && $invoiceSource->type == $facture::TYPE_DEPOSIT) {
904 $accountdeposittoventilated = new self($this->db);
905 if ($type == 'customer') {
906 $accountdeposittoventilated->fetch(0, getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER_DEPOSIT'), 1);
907 } elseif ($type == 'supplier') {
908 $accountdeposittoventilated->fetch(0, getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER_DEPOSIT'), 1);
909 }
910 $code_l = (string) $accountdeposittoventilated->ref;
911 $code_p = '';
912 $code_t = '';
913 $suggestedid = (int) $accountdeposittoventilated->rowid;
914 $suggestedaccountingaccountfor = 'deposit';
915 }
916 }
917 }
918
919 // If $suggestedid could not be guessed yet, we set it from the generic default accounting code $code_l
920 if (empty($suggestedid) && empty($code_p) && !empty($code_l) && !getDolGlobalString('ACCOUNTANCY_DO_NOT_AUTOFILL_ACCOUNT_WITH_GENERIC')) {
921 if (empty($this->accountingaccount_codetotid_cache[$code_l])) {
922 $tmpaccount = new self($this->db);
923 $result = $tmpaccount->fetch(0, $code_l, 1);
924 if ($result < 0) {
925 return -1;
926 }
927 if ($tmpaccount->id > 0) {
928 $suggestedid = $tmpaccount->id;
929 }
930 $this->accountingaccount_codetotid_cache[$code_l] = $tmpaccount->id;
931 } else {
932 $suggestedid = $this->accountingaccount_codetotid_cache[$code_l];
933 }
934 }
935 return array(
936 'suggestedaccountingaccountbydefaultfor' => $suggestedaccountingaccountbydefaultfor,
937 'suggestedaccountingaccountfor' => $suggestedaccountingaccountfor,
938 'suggestedid' => $suggestedid,
939 'code_l' => $code_l,
940 'code_p' => $code_p,
941 'code_t' => $code_t,
942 );
943 } else {
944 if (is_array($hookmanager->resArray) && !empty($hookmanager->resArray)) {
945 return $hookmanager->resArray;
946 }
947 }
948
949 return -1;
950 }
951}
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.
Parent class of all other business classes (invoices, contracts, proposals, orders,...
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:171
isInEEC($object)
Return if a country of an object is inside the EEC (European Economic Community)
dol_now($mode='gmt')
Return date for now.
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $allowothertags=array())
Show a picto called object_picto (generic function)
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
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...
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.
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...
editval_textarea active