dolibarr.idsconnect/cart_review.php
data 5f5a389809 IDS Connect v2.2 - Menü-Integration, ADL-Hooks, Admin-Erweiterung
- Menü unter Einkauf > Lieferantenbestellungen statt eigenes Top-Menü
- ADL-Buttons auf Produkt-Lieferantenpreisen per Hook (pricesuppliercard)
- Admin-Seite: Großhändler-Schnellübersicht mit Version-Check
- Dashboard: Shop-öffnen-Button (LI-Action)
- Neue Datei: class/actions_idsconnect.class.php

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-19 17:45:15 +01:00

258 lines
8.5 KiB
PHP
Executable file

<?php
/* Copyright (C) 2026 Eduard Wisch <data@data-it-solution.de>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*/
/**
* \file idsconnect/cart_review.php
* \ingroup idsconnect
* \brief Empfangenen Warenkorb prüfen und als Lieferantenbestellung übernehmen
*/
// Dolibarr laden
$res = 0;
if (!$res && !empty($_SERVER["CONTEXT_DOCUMENT_ROOT"])) {
$res = @include $_SERVER["CONTEXT_DOCUMENT_ROOT"]."/main.inc.php";
}
$tmp = empty($_SERVER['SCRIPT_FILENAME']) ? '' : $_SERVER['SCRIPT_FILENAME'];
$tmp2 = realpath(__FILE__);
$i = strlen($tmp) - 1;
$j = strlen($tmp2) - 1;
while ($i > 0 && $j > 0 && isset($tmp[$i]) && isset($tmp2[$j]) && $tmp[$i] == $tmp2[$j]) {
$i--;
$j--;
}
if (!$res && $i > 0 && file_exists(substr($tmp, 0, ($i + 1))."/main.inc.php")) {
$res = @include substr($tmp, 0, ($i + 1))."/main.inc.php";
}
if (!$res && $i > 0 && file_exists(dirname(substr($tmp, 0, ($i + 1)))."/main.inc.php")) {
$res = @include dirname(substr($tmp, 0, ($i + 1)))."/main.inc.php";
}
if (!$res && file_exists("../main.inc.php")) {
$res = @include "../main.inc.php";
}
if (!$res && file_exists("../../main.inc.php")) {
$res = @include "../../main.inc.php";
}
if (!$res) {
die("Include of main fails");
}
require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.commande.class.php';
require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
dol_include_once('/idsconnect/class/idslog.class.php');
dol_include_once('/idsconnect/class/idssupplier.class.php');
dol_include_once('/idsconnect/class/idsconnect.class.php');
dol_include_once('/idsconnect/lib/idsconnect.lib.php');
/**
* @var Conf $conf
* @var DoliDB $db
* @var Translate $langs
* @var User $user
*/
$langs->loadLangs(array("idsconnect@idsconnect", "orders", "bills"));
if (!$user->hasRight('idsconnect', 'use')) {
accessforbidden();
}
$log_id = GETPOSTINT('log_id');
$action = GETPOST('action', 'aZ09');
// Log laden
$log = new IdsLog($db);
if ($log_id > 0) {
$log->fetch($log_id);
}
// Großhändler laden
$supplier = new IdsSupplier($db);
if ($log->fk_supplier > 0) {
$supplier->fetch($log->fk_supplier);
}
// Artikel aus dem Log parsen
$items = array();
if (!empty($log->response_data)) {
$response = json_decode($log->response_data, true);
if (!empty($response['items'])) {
$items = $response['items'];
}
}
// Falls keine Artikel im Response, aus dem XML parsen
if (empty($items) && !empty($log->cart_xml)) {
$idsconnect = new IdsConnect($db);
$items = $idsconnect->parseCartXml($log->cart_xml);
if ($items === false) {
$items = array();
}
}
/*
* Actions
*/
// Lieferantenbestellung erstellen
if ($action == 'create_order' && $user->hasRight('fournisseur', 'commande', 'creer')) {
if (!verifCond(GETPOST('token', 'alpha') == newToken())) {
accessforbidden('Bad CSRF token');
}
if ($supplier->fk_soc > 0 && !empty($items)) {
$order = new CommandeFournisseur($db);
$order->socid = $supplier->fk_soc;
$order->note_private = 'Erstellt via IDS Connect aus Warenkorb vom '.dol_print_date($log->date_creation, 'dayhour').' ('.$supplier->label.')';
$db->begin();
$order_id = $order->create($user);
if ($order_id > 0) {
$line_errors = 0;
foreach ($items as $item) {
$vat_rate = !empty($item['mwst_satz']) ? $item['mwst_satz'] : 19;
$result = $order->addline(
$item['bezeichnung'], // desc
$item['einzelpreis'], // pu_ht (Stückpreis, schon durch PE geteilt)
$item['menge'], // qty
$vat_rate, // txtva
0, // txlocaltax1
0, // txlocaltax2
0, // fk_product
0, // fk_prod_fourn_price
$item['artikelnr'], // ref_supplier
0, // remise_percent
'HT' // price_base_type
);
if ($result < 0) {
$line_errors++;
}
}
if ($line_errors == 0) {
$db->commit();
// Log aktualisieren
$log->updateStatus('success', $log->response_data);
$log->updateCart($log->cart_xml, $order_id);
setEventMessages($langs->trans("IdsconnectCartImported"), null, 'mesgs');
header('Location: '.DOL_URL_ROOT.'/fourn/commande/card.php?id='.$order_id);
exit;
} else {
$db->rollback();
setEventMessages('Fehler beim Erstellen der Bestellpositionen', null, 'errors');
}
} else {
$db->rollback();
setEventMessages('Fehler beim Erstellen der Bestellung: '.$order->error, null, 'errors');
}
} else {
if ($supplier->fk_soc <= 0) {
setEventMessages('Großhändler hat keinen verknüpften Dolibarr-Lieferanten. Bitte zuerst in der Großhändler-Konfiguration zuweisen.', null, 'errors');
}
}
}
/*
* View
*/
$form = new Form($db);
llxHeader('', $langs->trans("IdsconnectCartReview"), '', '', 0, 0, '', '', '', 'mod-idsconnect page-cart_review');
print load_fiche_titre($langs->trans("IdsconnectCartReviewTitle"), '', 'fa-shopping-cart');
idsconnectShowTestModeBanner();
if (empty($items)) {
print '<div class="warning">'.$langs->trans("IdsconnectCartEmpty").'</div>';
print '<br><a href="'.DOL_URL_ROOT.'/custom/idsconnect/idsconnectindex.php" class="butAction">'.$langs->trans("Back").'</a>';
llxFooter();
$db->close();
exit;
}
// Info-Banner
print '<div class="info">';
print '<strong>'.$langs->trans("IdsconnectCartReviewInfo").'</strong><br>';
print 'Großhändler: <strong>'.htmlspecialchars($supplier->label ?: '-').'</strong>';
print ' | Empfangen: '.dol_print_date($log->date_creation, 'dayhour');
print ' | Artikel: <strong>'.count($items).'</strong>';
print '</div>';
// Artikel-Tabelle
print '<table class="noborder centpercent">';
print '<tr class="liste_titre">';
print '<th>'.$langs->trans("IdsconnectCartArticleNr").'</th>';
print '<th>'.$langs->trans("IdsconnectCartDescription").'</th>';
print '<th>'.$langs->trans("IdsconnectCartManufacturer").'</th>';
print '<th class="right">'.$langs->trans("IdsconnectCartQty").'</th>';
print '<th>'.$langs->trans("IdsconnectCartUnit").'</th>';
print '<th class="right">'.$langs->trans("IdsconnectCartUnitPrice").'</th>';
print '<th class="right">'.$langs->trans("IdsconnectCartTotalPrice").'</th>';
print '</tr>';
$total = 0;
foreach ($items as $item) {
$line_total = $item['gesamtpreis'] ?: ($item['menge'] * $item['einzelpreis']);
$total += $line_total;
print '<tr class="oddeven">';
print '<td><code>'.htmlspecialchars($item['artikelnr']).'</code></td>';
print '<td>'.htmlspecialchars($item['bezeichnung']).'</td>';
print '<td>'.htmlspecialchars($item['hersteller'] ?: '-').'</td>';
print '<td class="right">'.($item['menge']).'</td>';
print '<td>'.htmlspecialchars($item['einheit']).'</td>';
print '<td class="right">'.price($item['einzelpreis']);
// Preiseinheit-Info anzeigen wenn vorhanden (z.B. "Preis/100")
if (!empty($item['preiseinheit']) && $item['preiseinheit'] > 1) {
print '<br><span class="opacitymedium" style="font-size:0.85em">'.price($item['raw_netprice']).' / '.$item['preiseinheit'].' Stk</span>';
}
print '</td>';
print '<td class="right">'.price($line_total).'</td>';
print '</tr>';
}
// Summe
print '<tr class="liste_total">';
print '<td colspan="6" class="right"><strong>'.$langs->trans("Total").'</strong></td>';
print '<td class="right"><strong>'.price($total).'</strong></td>';
print '</tr>';
print '</table>';
// Aktionsbuttons
print '<div class="tabsAction">';
if ($user->hasRight('fournisseur', 'commande', 'creer') && $supplier->fk_soc > 0) {
print '<a class="butAction" href="'.$_SERVER['PHP_SELF'].'?log_id='.$log->id.'&action=create_order&token='.newToken().'">'.$langs->trans("IdsconnectCartCreateOrder").'</a>';
} elseif ($supplier->fk_soc <= 0) {
print '<span class="butActionRefused" title="Kein Dolibarr-Lieferant verknüpft">'.$langs->trans("IdsconnectCartCreateOrder").'</span>';
}
print '<a class="butAction" href="'.DOL_URL_ROOT.'/custom/idsconnect/idsconnectindex.php">'.$langs->trans("Back").'</a>';
print '</div>';
// XML-Details (aufklappbar)
if (!empty($log->cart_xml)) {
print '<br>';
print '<details>';
print '<summary class="opacitymedium" style="cursor:pointer;">XML-Rohdaten anzeigen</summary>';
print '<div class="idsconnect-log-detail">';
print htmlspecialchars($log->cart_xml);
print '</div>';
print '</details>';
}
llxFooter();
$db->close();