summaryrefslogtreecommitdiff
path: root/add_component.php
diff options
context:
space:
mode:
authorLester Caine <lester@lsces.co.uk>2026-06-03 17:04:44 +0100
committerLester Caine <lester@lsces.co.uk>2026-06-03 17:04:44 +0100
commit687fe972b2bc693e15491aadb5a7c74e0f20ef85 (patch)
tree0c8747141fd1e56a0b0c4b64601a1b2abd9f254c /add_component.php
parent4f3243d80abc15dbc5a22ffb03330e2290de1a4d (diff)
downloadstock-687fe972b2bc693e15491aadb5a7c74e0f20ef85.tar.gz
stock-687fe972b2bc693e15491aadb5a7c74e0f20ef85.tar.bz2
stock-687fe972b2bc693e15491aadb5a7c74e0f20ef85.zip
stock: add BOM component add page with autocomplete
- add_component.php: find component by title and store BOM xref; redirect to component_order.php on success, or edit_component.php (title pre-filled) if component not found - add_component.tpl: form with datalist autocomplete via lookup_component.php - includes/lookup_component.php: JSON endpoint for component title search - edit_component.php: pre-fill title from request for new components - view_xref_bom_group.tpl: add button now goes to add_component.php Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Diffstat (limited to 'add_component.php')
-rw-r--r--add_component.php92
1 files changed, 92 insertions, 0 deletions
diff --git a/add_component.php b/add_component.php
new file mode 100644
index 0000000..d33919c
--- /dev/null
+++ b/add_component.php
@@ -0,0 +1,92 @@
+<?php
+/**
+ * Add a single BOM component to an assembly.
+ * Finds existing component by title and stores the BOM xref,
+ * then redirects to component_order.php so position can be adjusted.
+ * If the component title is not found, redirects to edit_component.php to create it.
+ *
+ * @package stock
+ */
+
+namespace Bitweaver\Stock;
+
+use Bitweaver\KernelTools;
+use Bitweaver\Liberty\LibertyXref;
+
+require_once '../kernel/includes/setup_inc.php';
+
+global $gBitSystem, $gBitSmarty, $gBitDb, $gBitUser;
+
+include_once STOCK_PKG_INCLUDE_PATH.'assembly_lookup_inc.php';
+
+if( !$gContent->isValid() ) {
+ $gBitSystem->fatalError( 'No valid assembly specified.' );
+}
+$gContent->verifyUpdatePermission();
+
+$validItems = [ 'SGL' => 'Single unit', 'PCK' => 'Pack', 'SHT' => 'Sheet (H x W)', 'VOL' => 'Volume' ];
+$errors = [];
+
+if( !empty( $_REQUEST['fCancel'] ) ) {
+ header( 'Location: '.$gContent->getEditUrl() );
+ die;
+}
+
+if( !empty( $_REQUEST['fAddComponent'] ) ) {
+ $title = trim( $_REQUEST['component_title'] ?? '' );
+ $item = strtoupper( trim( $_REQUEST['item'] ?? 'SGL' ) );
+ $xkey = trim( $_REQUEST['xkey'] ?? '' );
+
+ if( !array_key_exists( $item, $validItems ) ) {
+ $item = 'SGL';
+ }
+
+ if( $title === '' ) {
+ $errors[] = KernelTools::tra( 'Component title is required.' );
+ } else {
+ // Find existing component by title, or create one
+ $compId = (int)$gBitDb->getOne(
+ "SELECT lc.`content_id` FROM `".BIT_DB_PREFIX."liberty_content` lc
+ WHERE lc.`content_type_guid`='".STOCKCOMPONENT_CONTENT_TYPE_GUID."' AND lc.`title`=?",
+ [ $title ]
+ );
+
+ if( !$compId ) {
+ header( 'Location: '.STOCK_PKG_URL.'edit_component.php?title='.urlencode( $title ).'&assembly_content_id='.$gContent->mContentId );
+ die;
+ }
+
+ if( $compId && empty( $errors ) ) {
+ // Place new row at end of current BOM
+ $maxXorder = (int)$gBitDb->getOne(
+ "SELECT MAX(`xorder`) FROM `".BIT_DB_PREFIX."liberty_xref`
+ WHERE `content_id`=? AND `item` IN ('SGL','PCK','SHT','VOL')",
+ [ $gContent->mContentId ]
+ );
+
+ $xrefObj = new LibertyXref();
+ $xrefObj->mContentTypeGuid = STOCKASSEMBLY_CONTENT_TYPE_GUID;
+ $pHash = [
+ 'content_id' => $gContent->mContentId,
+ 'item' => $item,
+ 'xorder' => $maxXorder + 10,
+ 'xref' => $compId,
+ 'xkey' => $xkey,
+ 'xkey_ext' => trim( $_REQUEST['xkey_ext'] ?? '' ) ?: null,
+ 'edit' => trim( $_REQUEST['edit'] ?? '' ) ?: null,
+ ];
+ if( $xrefObj->store( $pHash ) ) {
+ header( 'Location: '.STOCK_PKG_URL.'component_order.php?content_id='.$gContent->mContentId );
+ die;
+ } else {
+ $errors[] = KernelTools::tra( 'Failed to store BOM entry.' );
+ }
+ }
+ }
+}
+
+$gBitSmarty->assign( 'validItems', $validItems );
+$gBitSmarty->assign( 'errors', $errors );
+$gBitSmarty->assign( 'lookupUrl', STOCK_PKG_URL.'includes/lookup_component.php' );
+
+$gBitSystem->display( 'bitpackage:stock/add_component.tpl', KernelTools::tra('Add Component').': '.$gContent->getTitle(), [ 'display_mode' => 'edit' ] );