From b334d4bf4168012d1f4c7c580ee317cb439648b1 Mon Sep 17 00:00:00 2001 From: Lester Caine Date: Wed, 27 May 2026 17:26:29 +0100 Subject: Update stock menu and component importer - Menu: add List Components and Create a Component; park My Assemblies until multiple kit elves are active; keep Create an Assembly - ImportSimpleComponent: look up supplier via SCREF xkey_ext in liberty_xref rather than contact title; add #URL xref for column 5 - load_simple_components_2.php: new loader for 6-column CSV with URL - Fix fgetcsv() missing escape param deprecation in both loaders Co-Authored-By: Claude Sonnet 4.6 --- import/ImportSimpleComponent.php | 19 ++++++++-- import/load_assemblies.php | 4 +- import/load_components.php | 2 +- import/load_simple_components_2.php | 73 +++++++++++++++++++++++++++++++++++++ templates/menu_stock.tpl | 3 +- 5 files changed, 93 insertions(+), 8 deletions(-) create mode 100644 import/load_simple_components_2.php diff --git a/import/ImportSimpleComponent.php b/import/ImportSimpleComponent.php index b4f615b..1ce0dd0 100644 --- a/import/ImportSimpleComponent.php +++ b/import/ImportSimpleComponent.php @@ -32,10 +32,8 @@ function stockImportFindSupplier( string $name ): ?int { } $contentId = $gBitDb->getOne( - "SELECT lc.`content_id` - FROM `".BIT_DB_PREFIX."liberty_content` lc - INNER JOIN `".BIT_DB_PREFIX."contact` c ON c.`content_id` = lc.`content_id` - WHERE UPPER( lc.`title` ) = UPPER( ? )", + "SELECT `content_id` FROM `".BIT_DB_PREFIX."liberty_xref` + WHERE `item` = 'SCREF' AND UPPER( `xkey_ext` ) = UPPER( ? )", [ trim( $name ) ] ); @@ -93,6 +91,7 @@ function stockImportSimpleComponent( array $data, int $rowNum ): array { $supplierName = trim( $data[2] ?? '' ); $supplierPn = trim( $data[3] ?? '' ); $supplierPrice = trim( $data[4] ?? '' ); + $supplierUrl = trim( $data[5] ?? '' ); $component = new StockComponent(); $pHash = [ @@ -146,6 +145,18 @@ function stockImportSimpleComponent( array $data, int $rowNum ): array { 'last_update_date' => $gBitDb->NOW(), ] ); } + + if( !empty( $supplierUrl ) ) { + $xrefId = $gBitDb->GenID( 'liberty_xref_seq' ); + $gBitDb->associateInsert( BIT_DB_PREFIX.'liberty_xref', [ + 'xref_id' => $xrefId, + 'content_id' => $contentId, + 'item' => '#URL', + 'xorder' => 1, + 'xkey_ext' => substr( $supplierUrl, 0, 250 ), + 'last_update_date' => $gBitDb->NOW(), + ] ); + } } } diff --git a/import/load_assemblies.php b/import/load_assemblies.php index 564dd21..140b424 100644 --- a/import/load_assemblies.php +++ b/import/load_assemblies.php @@ -24,7 +24,7 @@ $gBitSystem->verifyPermission( 'p_stock_admin' ); require_once __DIR__.'/ImportAssembly.php'; -$csvFile = __DIR__.'/data/assemblies.csv'; +$csvFile = __DIR__.'/data/simple_assemblies.csv'; $loaded = 0; $skipped = 0; $errors = []; @@ -39,7 +39,7 @@ if( !file_exists( $csvFile ) ) { // Group rows by assembly title $batches = []; $rowNum = 0; - while( ( $data = fgetcsv( $handle, 1000, ',' ) ) !== false ) { + while( ( $data = fgetcsv( $handle, 1000, ',', '"', '\\' ) ) !== false ) { $rowNum++; if( $rowNum === 1 ) { continue; // skip header diff --git a/import/load_components.php b/import/load_components.php index 254af81..d70ea4e 100644 --- a/import/load_components.php +++ b/import/load_components.php @@ -34,7 +34,7 @@ if( !file_exists( $csvFile ) ) { $errors[] = 'Cannot open CSV file.'; } else { $row = 0; - while( ( $data = fgetcsv( $handle, 1000, ',' ) ) !== false ) { + while( ( $data = fgetcsv( $handle, 1000, ',', '"', '\\' ) ) !== false ) { $row++; if( $row === 1 ) { continue; // skip header diff --git a/import/load_simple_components_2.php b/import/load_simple_components_2.php new file mode 100644 index 0000000..6b2fcdf --- /dev/null +++ b/import/load_simple_components_2.php @@ -0,0 +1,73 @@ +verifyPackage( 'stock' ); +$gBitSystem->verifyPermission( 'p_stock_admin' ); + +require_once __DIR__.'/ImportSimpleComponent.php'; + +$csvFile = __DIR__.'/data/simple_components_2.csv'; +$doClear = ( ( $_REQUEST['clear'] ?? '' ) === 'y' ); +$loaded = 0; +$skipped = 0; +$deleted = 0; +$errors = []; + +if( !file_exists( $csvFile ) ) { + $errors[] = 'CSV file not found: '.$csvFile; +} else { + $handle = fopen( $csvFile, 'r' ); + if( $handle === false ) { + $errors[] = 'Cannot open CSV file.'; + } else { + $rows = []; + $rowNum = 0; + while( ( $data = fgetcsv( $handle, 1000, ',', '"', '' ) ) !== false ) { + $rowNum++; + if( $rowNum === 1 ) { + continue; // skip header + } + $rows[] = $data; + } + fclose( $handle ); + + if( $doClear ) { + foreach( $rows as $data ) { + $title = trim( $data[0] ?? '' ); + if( !empty( $title ) && stockExpungeComponentByTitle( $title ) ) { + $deleted++; + } + } + } + + foreach( $rows as $idx => $data ) { + $result = stockImportSimpleComponent( $data, $idx + 2 ); + $loaded += $result['loaded']; + $skipped += $result['skipped']; + $errors = array_merge( $errors, $result['errors'] ); + } + } +} + +$gBitSmarty->assign( 'loaded', $loaded ); +$gBitSmarty->assign( 'skipped', $skipped ); +$gBitSmarty->assign( 'deleted', $deleted ); +$gBitSmarty->assign( 'errors', $errors ); +$gBitSmarty->assign( 'csvFile', $csvFile ); + +$gBitSystem->display( 'bitpackage:stock/import_results.tpl', 'Import Components' ); diff --git a/templates/menu_stock.tpl b/templates/menu_stock.tpl index 6f23750..e251ea6 100755 --- a/templates/menu_stock.tpl +++ b/templates/menu_stock.tpl @@ -3,9 +3,10 @@ -- cgit v1.3