233 $langs->loadLangs(array(
"main",
"bills",
"companies",
"products"));
235 $limit = isset($args[
'limit']) ? (int) $args[
'limit'] : 50;
239 $groupBy = isset($args[
'group_by']) ? (
string) $args[
'group_by'] :
'thirdparty';
244 $dateRange =
" AND f.datef >= '" . $this->db->idate($dateStart)
245 .
"' AND f.datef <= '" . $this->db->idate($dateEnd)
246 .
"' AND f.fk_statut IN (1, 2)";
250 $sql =
"SELECT f.rowid, f.ref, f.total_ttc, f.fk_statut, f.paye, f.datef, s.nom FROM "
251 . MAIN_DB_PREFIX .
"facture as f LEFT JOIN "
252 . MAIN_DB_PREFIX .
"societe as s ON f.fk_soc = s.rowid WHERE f.entity IN ("
255 .
" AND f.fk_soc = " . (int) $socid
256 .
" ORDER BY f.datef DESC LIMIT " . ((
int) $limit);
258 $resql = $this->db->query($sql);
260 while ($r = $this->db->fetch_object($resql)) {
261 $totalSum += (float) $r->total_ttc;
263 $statusLabel = $langs->transnoentitiesnoconv(
"Unknown");
264 if ($r->fk_statut == 1 && $r->paye == 0) {
265 $statusLabel = $langs->transnoentitiesnoconv(
"BillStatusNotPaid");
266 } elseif ($r->fk_statut == 1 && $r->paye == 1) {
267 $statusLabel = $langs->transnoentitiesnoconv(
"BillStatusStarted");
268 } elseif ($r->fk_statut == 2) {
269 $statusLabel = $langs->transnoentitiesnoconv(
"BillStatusPaid");
272 $url = DOL_URL_ROOT .
"/compta/facture/card.php?id=" . $r->rowid;
273 $refHtml =
'<a href="' . $url .
'">' . $r->ref .
'</a>';
276 $langs->transnoentitiesnoconv(
"Ref") => $refHtml,
277 $langs->transnoentitiesnoconv(
"Date") =>
dol_print_date($this->db->jdate($r->datef),
'day'),
278 $langs->transnoentitiesnoconv(
"Customer") => $r->nom,
279 $langs->transnoentitiesnoconv(
"Amount") =>
price($r->total_ttc),
280 $langs->transnoentitiesnoconv(
"Status") => $statusLabel
283 $this->db->free($resql);
289 $sanitizedSqlGroup =
'';
291 $sqlJoin =
" LEFT JOIN " . MAIN_DB_PREFIX .
"societe as s ON f.fk_soc = s.rowid";
293 if ($groupBy ===
'month') {
294 $sanitizedSqlGroup =
"DATE_FORMAT(f.datef, '%Y-%m')";
295 $colName = $langs->transnoentitiesnoconv(
"Month");
296 } elseif ($groupBy ===
'product') {
298 $sanitizedSqlGroup =
"COALESCE(p.ref, fd.description, '?')";
299 $colName = $langs->transnoentitiesnoconv(
"Product");
300 $sqlJoin .=
" INNER JOIN " . MAIN_DB_PREFIX .
"facturedet as fd ON fd.fk_facture = f.rowid LEFT JOIN "
301 . MAIN_DB_PREFIX .
"product as p ON fd.fk_product = p.rowid";
304 $sanitizedSqlGroup =
"s.nom";
305 $colName = $langs->transnoentitiesnoconv(
"Customer");
310 $amountExpr = ($groupBy ===
'product') ?
"SUM(fd.total_ttc)" :
"SUM(f.total_ttc)";
311 $countExpr = ($groupBy ===
'product') ?
"COUNT(DISTINCT f.rowid)" :
"COUNT(f.rowid)";
313 $sql =
"SELECT " . $sanitizedSqlGroup .
" as group_key, "
314 . $amountExpr .
" as total_amount, "
315 . $countExpr .
" as count_inv FROM "
316 . MAIN_DB_PREFIX .
"facture as f"
318 .
" WHERE f.entity IN (" .
getEntity(
'facture') .
")"
320 .
" GROUP BY group_key ORDER BY total_amount DESC LIMIT "
321 . ((int) max(1, $limit));
323 $resql = $this->db->query($sql);
325 while ($r = $this->db->fetch_object($resql)) {
326 $totalSum += (float) $r->total_amount;
328 $colName => $r->group_key ? $r->group_key : $langs->transnoentitiesnoconv(
'Unknown'),
329 $langs->transnoentitiesnoconv(
"Number") => (int) $r->count_inv,
330 $langs->transnoentitiesnoconv(
"Amount") =>
price($r->total_amount)
333 $this->db->free($resql);
338 return [[$langs->transnoentitiesnoconv(
"Info") => $langs->transnoentitiesnoconv(
"NoRecordFound")]];
344 $langs->transnoentitiesnoconv(
"Ref") => $langs->transnoentitiesnoconv(
"Total"),
345 $langs->transnoentitiesnoconv(
"Date") =>
"",
346 $langs->transnoentitiesnoconv(
"Customer") =>
"",
347 $langs->transnoentitiesnoconv(
"Amount") =>
price($totalSum),
348 $langs->transnoentitiesnoconv(
"Status") =>
""
352 $langs->transnoentitiesnoconv(
"Total") => $langs->transnoentitiesnoconv(
"Total"),
353 $langs->transnoentitiesnoconv(
"Amount") =>
price($totalSum)
370 $langs->loadLangs(array(
"main",
"bills",
"orders",
"propal"));
374 $type = isset($args[
'transaction_type']) ? (
string) $args[
'transaction_type'] :
'all';
378 $langs->load(
"errors");
379 return [[$langs->transnoentitiesnoconv(
"Error") => $langs->transnoentitiesnoconv(
"ErrorThirdPartyNotFound")]];
385 if ($type ==
'all' || $type ==
'invoices') {
386 $queries[] =
"SELECT 'Invoice' as source_type, rowid, ref, total_ttc as amount, datef as date_entry, fk_statut
387 FROM " . MAIN_DB_PREFIX .
"facture
388 WHERE fk_soc = " . (int) $socid .
" AND entity IN (" .
getEntity(
'facture') .
")
389 AND fk_statut IN (1, 2)";
393 if ($type ==
'all' || $type ==
'orders') {
394 $queries[] =
"SELECT 'Order' as source_type, rowid, ref, total_ttc as amount, date_commande as date_entry, fk_statut
395 FROM " . MAIN_DB_PREFIX .
"commande
396 WHERE fk_soc = " . (int) $socid .
" AND entity IN (" .
getEntity(
'commande') .
")
401 if ($type ==
'all' || $type ==
'proposals') {
402 $queries[] =
"SELECT 'Proposal' as source_type, rowid, ref, total_ttc as amount, datep as date_entry, fk_statut
403 FROM " . MAIN_DB_PREFIX .
"propal
404 WHERE fk_soc = " . (int) $socid .
" AND entity IN (" .
getEntity(
'propal') .
")
408 if (empty($queries)) {
409 return [[$langs->transnoentitiesnoconv(
"Error") =>
"Invalid transaction type"]];
412 $sql =
"SELECT * FROM (";
413 $sql .= implode(
" UNION ", $queries);
414 $sql .=
") as combined_transactions ";
416 if ($dateStart > 0) {
417 $whereParts[] =
"date_entry >= '" . $this->db->idate($dateStart) .
"'";
420 $whereParts[] =
"date_entry <= '" . $this->db->idate($dateEnd) .
"'";
423 if (!empty($whereParts)) {
424 $sql .=
" WHERE " . implode(
" AND ", $whereParts);
427 $sql .=
" ORDER BY date_entry DESC";
429 $resql = $this->db->query($sql);
434 while ($r = $this->db->fetch_object($resql)) {
435 $totalAmt += (float) $r->amount;
440 if ($r->source_type ===
'Invoice') {
441 $urlPath =
"/compta/facture/card.php?id=" . $r->rowid;
442 if ($r->fk_statut == 2) {
443 $statusTxt = $langs->transnoentitiesnoconv(
"BillStatusPaid");
444 } elseif ($r->fk_statut == 1) {
445 $statusTxt = $langs->transnoentitiesnoconv(
"BillStatusNotPaid");
447 } elseif ($r->source_type ===
'Order') {
448 $urlPath =
"/commande/card.php?id=" . $r->rowid;
449 if ($r->fk_statut == 1) {
450 $statusTxt = $langs->transnoentitiesnoconv(
"StatusOrderValidated");
451 } elseif ($r->fk_statut == 2) {
452 $statusTxt = $langs->transnoentitiesnoconv(
"StatusOrderOnProcess");
453 } elseif ($r->fk_statut == 3) {
454 $statusTxt = $langs->transnoentitiesnoconv(
"StatusOrderDelivered");
456 } elseif ($r->source_type ===
'Proposal') {
457 $urlPath =
"/comm/propal/card.php?id=" . $r->rowid;
458 if ($r->fk_statut == 1) {
459 $statusTxt = $langs->transnoentitiesnoconv(
"PropalStatusValidated");
460 } elseif ($r->fk_statut == 2) {
461 $statusTxt = $langs->transnoentitiesnoconv(
"PropalStatusSigned");
462 } elseif ($r->fk_statut == 3) {
463 $statusTxt = $langs->transnoentitiesnoconv(
"PropalStatusNotSigned");
464 } elseif ($r->fk_statut == 4) {
465 $statusTxt = $langs->transnoentitiesnoconv(
"PropalStatusBilled");
469 $fullUrl = $urlPath ? DOL_URL_ROOT . $urlPath :
"";
470 $refHtml = $fullUrl ?
'<a href="' . $fullUrl .
'">' . $r->ref .
'</a>' : $r->ref;
473 $langs->transnoentitiesnoconv(
"Type") => $langs->transnoentitiesnoconv($r->source_type),
474 $langs->transnoentitiesnoconv(
"Ref") => $refHtml,
475 $langs->transnoentitiesnoconv(
"Date") =>
dol_print_date($this->db->jdate($r->date_entry),
'day'),
476 $langs->transnoentitiesnoconv(
"Amount") =>
price($r->amount),
477 $langs->transnoentitiesnoconv(
"Status") => $statusTxt
480 $this->db->free($resql);
484 return [[$langs->transnoentitiesnoconv(
"Info") => $langs->transnoentitiesnoconv(
"NoRecordFound")]];
489 $langs->transnoentitiesnoconv(
"Type") => $langs->transnoentitiesnoconv(
"Total"),
490 $langs->transnoentitiesnoconv(
"Ref") =>
"",
491 $langs->transnoentitiesnoconv(
"Date") =>
"",
492 $langs->transnoentitiesnoconv(
"Amount") =>
price($totalAmt),
493 $langs->transnoentitiesnoconv(
"Status") =>
""
509 $langs->loadLangs(array(
"main",
"bills",
"companies"));
512 $groupBy = isset($args[
'group_by']) ? (
string) $args[
'group_by'] :
'supplier';
520 $sql =
"SELECT f.rowid, f.ref, f.total_ttc, f.datef, s.nom
521 FROM " . MAIN_DB_PREFIX .
"facture_fourn as f
522 LEFT JOIN " . MAIN_DB_PREFIX .
"societe as s ON f.fk_soc = s.rowid
523 WHERE f.entity IN (" .
getEntity(
'facture_fourn') .
")
524 AND f.fk_soc = " . (int) $socid .
"
525 AND f.datef >= '" . $this->db->idate($dateStart) .
"'
526 AND f.datef <= '" . $this->db->idate($dateEnd) .
"'
528 ORDER BY f.datef DESC";
530 $resql = $this->db->query($sql);
532 while ($r = $this->db->fetch_object($resql)) {
533 $totalSum += (float) $r->total_ttc;
535 $url = DOL_URL_ROOT .
"/fourn/facture/card.php?id=" . $r->rowid;
536 $refHtml =
'<a href="' . $url .
'">' . $r->ref .
'</a>';
539 $langs->transnoentitiesnoconv(
"Ref") => $refHtml,
540 $langs->transnoentitiesnoconv(
"Date") =>
dol_print_date($this->db->jdate($r->datef),
'day'),
541 $langs->transnoentitiesnoconv(
"Supplier") => $r->nom,
542 $langs->transnoentitiesnoconv(
"Amount") =>
price($r->total_ttc)
545 $this->db->free($resql);
548 $sanitizedSqlGroup =
"";
551 if ($groupBy ===
'month') {
552 $sanitizedSqlGroup =
"DATE_FORMAT(f.datef, '%Y-%m')";
553 $colName = $langs->transnoentitiesnoconv(
"Month");
555 $sanitizedSqlGroup =
"s.nom";
556 $colName = $langs->transnoentitiesnoconv(
"Supplier");
559 $sql =
"SELECT " . $sanitizedSqlGroup .
" as group_key, SUM(f.total_ttc) as total_amount, COUNT(f.rowid) as count_inv
560 FROM " . MAIN_DB_PREFIX .
"facture_fourn as f
561 LEFT JOIN " . MAIN_DB_PREFIX .
"societe as s ON f.fk_soc = s.rowid
562 WHERE f.entity IN (" .
getEntity(
'facture_fourn') .
")
563 AND f.datef >= '" . $this->db->idate($dateStart) .
"'
564 AND f.datef <= '" . $this->db->idate($dateEnd) .
"'
567 ORDER BY total_amount DESC";
569 $resql = $this->db->query($sql);
571 while ($r = $this->db->fetch_object($resql)) {
572 $totalSum += (float) $r->total_amount;
574 $colName => $r->group_key ? $r->group_key : $langs->transnoentitiesnoconv(
'Unknown'),
575 $langs->transnoentitiesnoconv(
"Number") => $r->count_inv,
576 $langs->transnoentitiesnoconv(
"Amount") =>
price($r->total_amount)
579 $this->db->free($resql);
584 return [[$langs->transnoentitiesnoconv(
"Info") => $langs->transnoentitiesnoconv(
"NoRecordFound")]];
589 $langs->transnoentitiesnoconv(
"Amount") =>
price($totalSum)
593 $summary[$langs->transnoentitiesnoconv(
"Ref")] = $langs->transnoentitiesnoconv(
"Total");
594 $summary[$langs->transnoentitiesnoconv(
"Date")] =
"";
595 $summary[$langs->transnoentitiesnoconv(
"Supplier")] =
"";
597 $summary[($groupBy ===
'month' ? $langs->transnoentitiesnoconv(
"Month") : $langs->transnoentitiesnoconv(
"Supplier"))] = $langs->transnoentitiesnoconv(
"Total");
598 $summary[$langs->transnoentitiesnoconv(
"Number")] =
"";
615 $langs->loadLangs(array(
"products",
"stocks"));
617 $catId = isset($args[
'category_id']) ? (int) $args[
'category_id'] : 0;
618 $warehouseId = isset($args[
'warehouse_id']) ? (int) $args[
'warehouse_id'] : 0;
619 $includeZero = isset($args[
'include_zero_stock']) ? (bool) $args[
'include_zero_stock'] :
false;
621 $sql =
"SELECT p.rowid, p.ref, p.label, p.pmp, ";
623 if ($warehouseId > 0) {
624 $sql .=
" ps.reel as stock_level ";
625 $sql .=
" FROM " . MAIN_DB_PREFIX .
"product as p ";
626 $sql .=
" LEFT JOIN " . MAIN_DB_PREFIX .
"product_stock as ps ON p.rowid = ps.fk_product ";
627 $sql .=
" WHERE ps.fk_entrepot = " . (int) $warehouseId;
629 $sql .=
" p.stock as stock_level ";
630 $sql .=
" FROM " . MAIN_DB_PREFIX .
"product as p ";
631 $sql .=
" WHERE 1=1 ";
634 $sql .=
" AND p.entity IN (" .
getEntity(
'product') .
")";
637 $sql .=
" AND p.rowid IN (SELECT fk_product FROM " . MAIN_DB_PREFIX .
"categorie_product WHERE fk_categorie = " . (int) $catId .
")";
641 if ($warehouseId > 0) {
642 $sql .=
" AND ps.reel > 0";
644 $sql .=
" AND p.stock > 0";
648 $sql .=
" ORDER BY p.ref ASC LIMIT 200";
650 $resql = $this->db->query($sql);
652 $totalValuation = 0.0;
656 while ($r = $this->db->fetch_object($resql)) {
657 $stockVal = $r->stock_level * $r->pmp;
658 $totalValuation += $stockVal;
659 $totalItems += (int) $r->stock_level;
661 $url = DOL_URL_ROOT .
"/product/card.php?id=" . $r->rowid;
662 $refHtml =
'<a href="' . $url .
'">' . $r->ref .
'</a>';
665 $langs->transnoentitiesnoconv(
"Ref") => $refHtml,
666 $langs->transnoentitiesnoconv(
"Label") => $r->label,
667 $langs->transnoentitiesnoconv(
"Stock") => $r->stock_level,
668 $langs->transnoentitiesnoconv(
"PMPValue") =>
price($r->pmp),
669 $langs->transnoentitiesnoconv(
"TotalValue") =>
price($stockVal)
672 $this->db->free($resql);
676 return [[$langs->transnoentitiesnoconv(
"Info") => $langs->transnoentitiesnoconv(
"NoRecordFound")]];
680 $langs->transnoentitiesnoconv(
"Ref") => $langs->transnoentitiesnoconv(
"Total"),
681 $langs->transnoentitiesnoconv(
"Label") =>
"",
682 $langs->transnoentitiesnoconv(
"Stock") => $totalItems,
683 $langs->transnoentitiesnoconv(
"PMPValue") =>
"",
684 $langs->transnoentitiesnoconv(
"TotalValue") =>
price($totalValuation)
700 $langs->loadLangs(array(
"compta",
"bills"));
705 $sqlIncome =
"SELECT SUM(total_ttc) as total FROM " . MAIN_DB_PREFIX .
"facture
706 WHERE entity IN (" .
getEntity(
'facture') .
")
707 AND datef >= '" . $this->db->idate($dateStart) .
"'
708 AND datef <= '" . $this->db->idate($dateEnd) .
"'
709 AND fk_statut IN (1, 2)";
711 $resIncome = $this->db->query($sqlIncome);
712 $objIncome = $this->db->fetch_object($resIncome);
713 $income = $objIncome && $objIncome->total ? (float) $objIncome->total : 0.0;
716 $sqlExpense =
"SELECT SUM(total_ttc) as total FROM " . MAIN_DB_PREFIX .
"facture_fourn
717 WHERE entity IN (" .
getEntity(
'facture_fourn') .
")
718 AND datef >= '" . $this->db->idate($dateStart) .
"'
719 AND datef <= '" . $this->db->idate($dateEnd) .
"'
722 $resExpense = $this->db->query($sqlExpense);
723 $objExpense = $this->db->fetch_object($resExpense);
724 $expense = $objExpense && $objExpense->total ? (float) $objExpense->total : 0.0;
726 $net = $income - $expense;
730 $langs->transnoentitiesnoconv(
"Category") => $langs->transnoentitiesnoconv(
"Income"),
731 $langs->transnoentitiesnoconv(
"Description") => $langs->transnoentitiesnoconv(
"BillsCustomers"),
732 $langs->transnoentitiesnoconv(
"Amount") =>
price($income)
735 $langs->transnoentitiesnoconv(
"Category") => $langs->transnoentitiesnoconv(
"Expenses"),
736 $langs->transnoentitiesnoconv(
"Description") => $langs->transnoentitiesnoconv(
"BillsSuppliers"),
737 $langs->transnoentitiesnoconv(
"Amount") =>
price($expense)
740 $langs->transnoentitiesnoconv(
"Category") => $langs->transnoentitiesnoconv(
"Total"),
741 $langs->transnoentitiesnoconv(
"Description") => $langs->transnoentitiesnoconv(
"Profit"),
742 $langs->transnoentitiesnoconv(
"Amount") =>
price($net)