summaryrefslogtreecommitdiff
path: root/includes
diff options
context:
space:
mode:
authorLester Caine <lester@lsces.co.uk>2026-06-01 12:33:49 +0100
committerLester Caine <lester@lsces.co.uk>2026-06-01 12:33:49 +0100
commita16e3d92444befcbd857d3bf00381b559d494d20 (patch)
treebf0721976ad9111721c6f19a1d1522dc35d23d5e /includes
parentf951a616d235b1c1fe74e8cba4ecb0039749575d (diff)
downloadstock-a16e3d92444befcbd857d3bf00381b559d494d20.tar.gz
stock-a16e3d92444befcbd857d3bf00381b559d494d20.tar.bz2
stock-a16e3d92444befcbd857d3bf00381b559d494d20.zip
Fix two-arg constructor signature to work with getLibertyObject/getNewObject
All three classes only accepted one parameter; getNewObject passes (null, $contentId) so the content_id was silently dropped and the object never loaded. Coalesce $pContentId ?? $pPrimaryId preserves all existing single-arg call sites. Also guard explodeFromAssembly against non-numeric xkey via SIMILAR TO filter. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Diffstat (limited to 'includes')
-rwxr-xr-xincludes/classes/StockAssembly.php3
-rwxr-xr-xincludes/classes/StockComponent.php4
-rw-r--r--includes/classes/StockMovement.php38
3 files changed, 29 insertions, 16 deletions
diff --git a/includes/classes/StockAssembly.php b/includes/classes/StockAssembly.php
index 9ee5743..d286742 100755
--- a/includes/classes/StockAssembly.php
+++ b/includes/classes/StockAssembly.php
@@ -30,9 +30,10 @@ class StockAssembly extends StockBase {
public $pRecursiveDelete;
protected $mXrefTypeKey = 'stockassembly_types';
- public function __construct($pContentId = null) {
+ public function __construct($pAssemblyId = null, $pContentId = null) {
parent::__construct();
$this->mContentTypeGuid = STOCKASSEMBLY_CONTENT_TYPE_GUID;
+ $pContentId = $pContentId ?? $pAssemblyId;
if( $this->verifyId( $pContentId ) ) {
$this->mContentId = (int)$pContentId;
}
diff --git a/includes/classes/StockComponent.php b/includes/classes/StockComponent.php
index 0647063..35db90b 100755
--- a/includes/classes/StockComponent.php
+++ b/includes/classes/StockComponent.php
@@ -19,10 +19,10 @@ define('STOCKCOMPONENT_CONTENT_TYPE_GUID', 'stockcomponent');
class StockComponent extends StockBase {
protected $mXrefTypeKey = 'stockcomponent_types';
- public function __construct($pContentId = null) {
+ public function __construct($pComponentId = null, $pContentId = null) {
parent::__construct();
$this->mContentTypeGuid = STOCKCOMPONENT_CONTENT_TYPE_GUID;
- $this->mContentId = (int)$pContentId;
+ $this->mContentId = (int)($pContentId ?? $pComponentId);
$this->registerContentType(
STOCKCOMPONENT_CONTENT_TYPE_GUID, [
diff --git a/includes/classes/StockMovement.php b/includes/classes/StockMovement.php
index 71193e7..8918e8c 100644
--- a/includes/classes/StockMovement.php
+++ b/includes/classes/StockMovement.php
@@ -17,9 +17,10 @@ define( 'STOCKMOVEMENT_CONTENT_TYPE_GUID', 'stockmovement' );
class StockMovement extends LibertyContent {
protected $mXrefTypeKey = 'stockmovement_types';
- public function __construct( $pContentId = null ) {
+ public function __construct( $pMovementId = null, $pContentId = null ) {
parent::__construct();
$this->mContentTypeGuid = STOCKMOVEMENT_CONTENT_TYPE_GUID;
+ $pContentId = $pContentId ?? $pMovementId;
if( $this->verifyId( $pContentId ) ) {
$this->mContentId = (int)$pContentId;
}
@@ -165,33 +166,40 @@ class StockMovement extends LibertyContent {
return true;
}
- // Populate movement quantity xrefs from an assembly BOM, scaled by $pKitCount
+ // Append movement quantity xrefs from an assembly BOM (liberty_xref), scaled by $pKitCount.
+ // Appends to any existing items — safe to call multiple times for multi-assembly requisitions.
public function explodeFromAssembly( int $pAssemblyContentId, float $pKitCount = 1 ): bool {
if( !$this->isValid() || !$this->verifyId( $pAssemblyContentId ) ) {
return false;
}
- $this->StartTrans();
- $this->mDb->query(
- "DELETE FROM `".BIT_DB_PREFIX."liberty_xref`
- WHERE `content_id` = ? AND `item` IN ('SGL','PCK','SHT','VOL')",
+
+ $nextXorder = (int)$this->mDb->getOne(
+ "SELECT COALESCE( MAX(x.`xorder`) + 1, 1 ) FROM `".BIT_DB_PREFIX."liberty_xref` x
+ WHERE x.`content_id` = ? AND x.`item` IN ('SGL','PCK','SHT','VOL')",
[ $this->mContentId ]
- );
+ ) ?: 1;
+
$rows = $this->mDb->query(
- "SELECT `item_content_id`, `item_position`, `quantity_value`, `quantity_item`
- FROM `".BIT_DB_PREFIX."stock_assembly_component_map`
- WHERE `assembly_content_id` = ?
- ORDER BY `item_position`",
+ "SELECT x.`xref` AS item_content_id, x.`item` AS quantity_item,
+ CAST(x.`xkey` AS DOUBLE PRECISION) AS quantity_value
+ FROM `".BIT_DB_PREFIX."liberty_xref` x
+ WHERE x.`content_id` = ? AND x.`item` IN ('SGL','PCK','SHT','VOL')
+ AND x.`xkey` SIMILAR TO '[0-9]+(\.[0-9]+)?'
+ ORDER BY x.`xorder`",
[ $pAssemblyContentId ]
);
+
+ $this->StartTrans();
foreach( $rows as $row ) {
$bomHash = [
'content_id' => $this->mContentId,
'item' => $row['quantity_item'],
- 'xref' => $row['item_content_id'],
+ 'xref' => (int)$row['item_content_id'],
'xkey' => $row['quantity_value'] * $pKitCount,
- 'xorder' => $row['item_position'],
+ 'xorder' => $nextXorder,
];
$this->storeXref( $bomHash );
+ $nextXorder++;
}
$this->CompleteTrans();
return true;
@@ -210,6 +218,10 @@ class StockMovement extends LibertyContent {
$bindVars[] = $pListHash['ref_type'];
}
+ if( $this->verifyId( $pListHash['assembly_content_id'] ?? 0 ) ) {
+ $joinSql .= " INNER JOIN `".BIT_DB_PREFIX."liberty_xref` xasm ON xasm.`content_id` = lc.`content_id` AND xasm.`item` = 'ASSEMBLY' AND xasm.`xref` = ?";
+ $bindVars[] = (int)$pListHash['assembly_content_id'];
+ }
if( $this->verifyId( $pListHash['user_id'] ?? 0 ) ) {
$whereSql .= " AND lc.`user_id` = ?";
$bindVars[] = (int)$pListHash['user_id'];