diff options
| author | Lester Caine <lester@lsces.co.uk> | 2026-05-27 17:26:29 +0100 |
|---|---|---|
| committer | Lester Caine <lester@lsces.co.uk> | 2026-05-27 17:26:29 +0100 |
| commit | b334d4bf4168012d1f4c7c580ee317cb439648b1 (patch) | |
| tree | ec2e7696d79f88715225ca7d3374d6c9a4cac16d /import | |
| parent | ecc4499b657e1ccb65835e42007174f0352fcb8b (diff) | |
| download | stock-b334d4bf4168012d1f4c7c580ee317cb439648b1.tar.gz stock-b334d4bf4168012d1f4c7c580ee317cb439648b1.tar.bz2 stock-b334d4bf4168012d1f4c7c580ee317cb439648b1.zip | |
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 <noreply@anthropic.com>
Diffstat (limited to 'import')
| -rw-r--r-- | import/ImportSimpleComponent.php | 19 | ||||
| -rw-r--r-- | import/load_assemblies.php | 4 | ||||
| -rw-r--r-- | import/load_components.php | 2 | ||||
| -rw-r--r-- | import/load_simple_components_2.php | 73 |
4 files changed, 91 insertions, 7 deletions
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 @@ +<?php +/** + * Load components from a 6-column CSV (title, description, supplier, PN, price, URL). + * First row is a header and is skipped. Existing components (by title) are skipped + * unless clear=y is passed. + * + * Place your CSV at: stock/import/data/simple_components_2.csv + * Append ?clear=y to the URL to delete and re-import all rows. + * + * @package stock + */ + +namespace Bitweaver\Stock; + +require_once '../../kernel/includes/setup_inc.php'; + +global $gBitSystem, $gBitSmarty, $gBitDb; + +$gBitSystem->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' ); |
