dolibarr.stockkonversion/class/actions_stockkonversion.class.php
data e1bc646c06 Stock-Konversion Modul v1.0.0
Umrechnung von Einkaufs- in Lagereinheiten bei Wareneingang.
Extrafeld "Umrechnungsfaktor" auf Produkten, Hook auf stockMovementCreate (type=3).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-12 18:16:31 +01:00

127 lines
3.7 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/**
* \file stockkonversion/class/actions_stockkonversion.class.php
* \ingroup stockkonversion
* \brief Hook-Klasse für Lagerbewegungs-Konversion
*
* Bei Wareneingang (type=3) aus Lieferantenbestellung:
* - Menge wird mit Umrechnungsfaktor multipliziert
* - Preis wird durch Umrechnungsfaktor dividiert
* - Gesamtwert (qty × price) bleibt identisch
*/
class ActionsStockkonversion
{
/** @var DoliDB */
private $db;
/** @var string[] */
public $errors = array();
/** @var string */
public $error = '';
/** @var string|null */
public $resprints;
/**
* Constructor
*
* @param DoliDB $db Database handler
*/
public function __construct($db)
{
$this->db = $db;
}
/**
* Hook bei Lagerbewegung — wird VOR dem INSERT aufgerufen
*
* Parameter qty und price sind Referenzen und können direkt geändert werden.
* Siehe mouvementstock.class.php Zeile 233-265.
*
* @param array $parameters Hook-Parameter (qty, price, type, fk_product, label als Referenzen)
* @param MouvementStock $object Lagerbewegungsobjekt
* @param string $action Aktuelle Aktion
* @return int 0=OK, <0=Fehler
*/
public function stockMovementCreate($parameters, &$object, &$action)
{
global $conf, $langs;
// Nur bei Wareneingang aus Geschäftsvorfall (type=3 = Lieferantenbestellung)
// type=0 = manueller Eingang → bewusst NICHT konvertieren
if ($parameters['type'] != 3) {
return 0;
}
// Produkt-ID prüfen
$fk_product = (int) $parameters['fk_product'];
if ($fk_product <= 0) {
return 0;
}
// Umrechnungsfaktor aus Extrafeld lesen
$sql = "SELECT umrechnungsfaktor FROM ".$this->db->prefix()."product_extrafields";
$sql .= " WHERE fk_object = ".$fk_product;
$resql = $this->db->query($sql);
if (!$resql) {
dol_syslog("StockKonversion: DB-Fehler beim Lesen des Umrechnungsfaktors: ".$this->db->lasterror(), LOG_ERR);
return 0; // Kein Abbruch — im Zweifel ohne Konversion weiterarbeiten
}
$obj = $this->db->fetch_object($resql);
$this->db->free($resql);
if (!$obj) {
return 0; // Kein Extrafeld-Eintrag vorhanden
}
$faktor = (int) $obj->umrechnungsfaktor;
// Faktor <= 1 bedeutet keine Konversion nötig
if ($faktor <= 1) {
return 0;
}
// Originaldaten für Label merken
$origQty = $parameters['qty'];
$origPrice = $parameters['price'];
// Konversion durchführen:
// Menge multiplizieren, Preis dividieren
// Gesamtwert bleibt identisch: qty × price = (qty × faktor) × (price / faktor)
$parameters['qty'] = $parameters['qty'] * $faktor;
$parameters['price'] = price2num($parameters['price'] / $faktor, 'MU');
// Label ergänzen für Nachvollziehbarkeit
$parameters['label'] .= ' [Konv: '.$origQty.'x'.$faktor.'='.$parameters['qty'].']';
dol_syslog(
"StockKonversion: Produkt ".$fk_product
." Faktor=".$faktor
." Menge ".$origQty."->".$parameters['qty']
." Preis ".$origPrice."->".$parameters['price'],
LOG_INFO
);
return 0;
}
}