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";
50 $object->fetch($socid);
53 if ($object->code_compta ==
'411CUSTCODE') {
54 $object->code_compta =
'';
57 if ($object->code_compta_fournisseur ==
'401SUPPCODE') {
58 $object->code_compta_fournisseur =
'';
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') ) ";
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 !=
"")
74 if ($object->code_compta_fournisseur !=
"")
75 $sql .=
" bk.subledger_account = '".$this->db->escape($object->code_compta_fournisseur).
"' ";
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');
88 while ($obj = $this->
db->fetch_object(
$resql)) {
92 if ($obj->type ==
'payment_supplier')
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.
") ";
103 if ($object->code_compta !=
"") {
104 $sql .=
" bk.subledger_account = '".$this->db->escape($object->code_compta).
"' ";
106 if ($object->code_compta !=
"" && $object->code_compta_fournisseur !=
"") {
109 if ($object->code_compta_fournisseur !=
"") {
110 $sql .=
" bk.subledger_account = '".$this->db->escape($object->code_compta_fournisseur).
"' ";
114 $resql2 = $this->
db->query($sql);
116 while ($obj2 = $this->
db->fetch_object($resql2)) {
117 $ids[$obj2->rowid] = $obj2->rowid;
118 $ids_fact[] = $obj2->fact_id;
121 $this->errors[] = $this->
db->lasterror;
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;
131 if ($object->code_compta !=
"") {
132 $sql .=
" bk.subledger_account = '".$this->db->escape($object->code_compta).
"' ";
134 if ($object->code_compta !=
"" && $object->code_compta_fournisseur !=
"") {
137 if ($object->code_compta_fournisseur !=
"") {
138 $sql .=
" bk.subledger_account = '".$this->db->escape($object->code_compta_fournisseur).
"' ";
142 $resql2 = $this->
db->query($sql);
144 while ($obj2 = $this->
db->fetch_object($resql2)) {
145 $ids[$obj2->rowid] = $obj2->rowid;
148 $this->errors[] = $this->
db->lasterror;
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).
")";
162 if ($object->code_compta !=
"") {
163 $sql .=
" bk.subledger_account = '".$this->db->escape($object->code_compta).
"' ";
165 if ($object->code_compta !=
"" && $object->code_compta_fournisseur !=
"") {
168 if ($object->code_compta_fournisseur !=
"") {
169 $sql .=
" bk.subledger_account = '".$this->db->escape($object->code_compta_fournisseur).
"' ";
173 $resql2 = $this->
db->query($sql);
175 while ($obj2 = $this->
db->fetch_object($resql2)) {
176 $ids[$obj2->rowid] = $obj2->rowid;
177 $ids_fact[] = $obj2->fact_id;
180 $this->errors[] = $this->
db->lasterror;
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).
")";
190 if ($object->code_compta !=
"") {
191 $sql .=
" bk.subledger_account = '".$this->db->escape($object->code_compta).
"' ";
193 if ($object->code_compta !=
"" && $object->code_compta_fournisseur !=
"") {
196 if ($object->code_compta_fournisseur !=
"") {
197 $sql .=
" bk.subledger_account = '".$this->db->escape($object->code_compta_fournisseur).
"' ";
201 $resql2 = $this->
db->query($sql);
203 while ($obj2 = $this->
db->fetch_object($resql2)) {
204 $ids[$obj2->rowid] = $obj2->rowid;
207 $this->errors[] = $this->
db->lasterror;
213 if (count($ids) > 1) {
219 foreach ($this->errors as $errmsg) {
221 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
240 $sql =
"SELECT DISTINCT lettering_code FROM ".MAIN_DB_PREFIX.
"accounting_bookkeeping WHERE ";
241 $sql .=
" lettering_code != '' ORDER BY lettering_code DESC limit 1";
243 $result = $this->
db->query($sql);
245 $obj = $this->
db->fetch_object($result);
246 $lettre = (empty($obj->lettering_code) ?
'AAA' : $obj->lettering_code);
247 if (!empty($obj->lettering_code))
250 $this->errors[] =
'Error'.$this->db->lasterror();
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);
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);
264 $this->errors[] =
'Erreur sql'.$this->db->lasterror();
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).
"'";
277 $sql .=
" WHERE rowid IN (".implode(
',', $ids).
") AND date_validated IS NULL ";
280 dol_syslog(get_class($this).
"::update sql=".$sql, LOG_DEBUG);
284 $this->errors[] =
"Error ".$this->db->lasterror();
290 foreach ($this->errors as $errmsg) {
291 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
292 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
294 $this->
db->rollback();
dol_now($mode= 'auto')
Return date for now.
letteringThirdparty($socid)
letteringThirdparty
$conf db
API class for accounts.
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.