summaryrefslogtreecommitdiff
path: root/import
diff options
context:
space:
mode:
authorLester Caine <lester@lsces.co.uk>2026-05-28 16:12:46 +0100
committerLester Caine <lester@lsces.co.uk>2026-05-28 16:12:46 +0100
commit69173bfb38c12696a50cecf0702658dfb3749eb5 (patch)
tree8a4c32edea0e5b3d51091b24e5d7bc00a38a3dd9 /import
parent58320a6ffc85c971182f66e374946c0a50fabb08 (diff)
downloadstock-69173bfb38c12696a50cecf0702658dfb3749eb5.tar.gz
stock-69173bfb38c12696a50cecf0702658dfb3749eb5.tar.bz2
stock-69173bfb38c12696a50cecf0702658dfb3749eb5.zip
Add KitLocker group and assembly importers; extend kitlocker xref items
schema_inc.php: add KLID (group ID), KL3M (3-month sales) xref items to kitlocker group; correct sort order on KLPR and KLURL. load_kitlocker_groups.php: create group assemblies under assembly 375, tag each with KLID xref for downstream lookup. load_kitlocker_assemblies.php: create kit assemblies under their group (KLID lookup via xref), store SGL and KL3M as xref values. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Diffstat (limited to 'import')
-rw-r--r--import/load_kitlocker_assemblies.php121
-rw-r--r--import/load_kitlocker_groups.php92
2 files changed, 213 insertions, 0 deletions
diff --git a/import/load_kitlocker_assemblies.php b/import/load_kitlocker_assemblies.php
new file mode 100644
index 0000000..cff5548
--- /dev/null
+++ b/import/load_kitlocker_assemblies.php
@@ -0,0 +1,121 @@
+<?php
+/**
+ * Phase 2: Import KitlockerAssemblies.csv under their KitLocker group assemblies.
+ *
+ * CSV columns: Title, Description, SGL, KL3M, Group
+ * Group is numeric — 'G' is prepended to match the KLID xref stored by
+ * load_kitlocker_groups.php. SGL and KL3M are stored as xref values.
+ *
+ * Already-existing assemblies (matched by title) are skipped.
+ * Run load_kitlocker_groups.php first.
+ *
+ * @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 STOCK_PKG_CLASS_PATH.'StockAssembly.php';
+
+$csvFile = __DIR__.'/data/KitlockerAssemblies.csv';
+$loaded = $skipped = 0;
+$errors = [];
+
+// Build KLID → content_id map from xref table
+$klidMap = $gBitDb->getAssoc(
+ "SELECT `xkey`, `content_id` FROM `".BIT_DB_PREFIX."liberty_xref` WHERE `item`=?",
+ [ 'KLID' ]
+);
+
+if( empty( $klidMap ) ) {
+ $errors[] = 'No KLID xref entries found — run load_kitlocker_groups.php first.';
+} elseif( !file_exists( $csvFile ) ) {
+ $errors[] = "File not found: $csvFile";
+} else {
+ $fh = fopen( $csvFile, 'r' );
+ $rowNum = 0;
+ while( ($cols = fgetcsv( $fh )) !== false ) {
+ $rowNum++;
+ if( $rowNum === 1 ) continue; // header: Title, Description, SGL, KL3M, Group
+
+ $title = trim( $cols[0] ?? '' );
+ $description = trim( $cols[1] ?? '' );
+ $sgl = trim( $cols[2] ?? '' );
+ $kl3m = trim( $cols[3] ?? '' );
+ $groupNum = trim( $cols[4] ?? '' );
+ $klid = 'G'.$groupNum;
+
+ if( $title === '' || $groupNum === '' ) {
+ $skipped++;
+ continue;
+ }
+
+ if( !isset( $klidMap[$klid] ) ) {
+ $errors[] = "Row $rowNum: '$title' — group '$klid' not in KLID map, skipped.";
+ $skipped++;
+ continue;
+ }
+
+ // Skip if assembly already exists by title
+ if( $gBitDb->getOne(
+ "SELECT sa.`content_id` FROM `".BIT_DB_PREFIX."stock_assembly` sa
+ INNER JOIN `".BIT_DB_PREFIX."liberty_content` lc ON lc.`content_id` = sa.`content_id`
+ WHERE lc.`title` = ?",
+ [ $title ]
+ ) ) {
+ $skipped++;
+ continue;
+ }
+
+ $assembly = new StockAssembly();
+ $pHash = [ 'title' => $title, 'edit' => $description, 'format_guid' => 'bithtml' ];
+ if( !$assembly->store( $pHash ) ) {
+ $errors[] = "Row $rowNum: failed to create '$title'";
+ $skipped++;
+ continue;
+ }
+
+ $contentId = $assembly->mContentId;
+ $groupContentId = (int)$klidMap[$klid];
+
+ // Add as child of the group assembly
+ $gBitDb->getOne(
+ "INSERT INTO `".BIT_DB_PREFIX."stock_assembly_component_map`
+ (`assembly_content_id`, `item_content_id`, `item_position`) VALUES (?,?,NULL)",
+ [ $groupContentId, $contentId ]
+ );
+
+ // Store SGL and KL3M xrefs
+ foreach( [ 'SGL' => $sgl, 'KL3M' => $kl3m ] as $item => $value ) {
+ if( $value !== '' && is_numeric( $value ) ) {
+ $xrefId = $gBitDb->GenID( 'liberty_xref_seq' );
+ $gBitDb->associateInsert( BIT_DB_PREFIX.'liberty_xref', [
+ 'xref_id' => $xrefId,
+ 'content_id' => $contentId,
+ 'item' => $item,
+ 'xorder' => 0,
+ 'xkey' => $value,
+ 'last_update_date' => $gBitDb->NOW(),
+ ] );
+ }
+ }
+
+ $loaded++;
+ }
+ fclose( $fh );
+}
+
+$gBitSmarty->assign( 'loaded', $loaded );
+$gBitSmarty->assign( 'skipped', $skipped );
+$gBitSmarty->assign( 'deleted', 0 );
+$gBitSmarty->assign( 'errors', $errors );
+$gBitSmarty->assign( 'csvFile', $csvFile );
+$gBitSmarty->assign( 'movement', null );
+
+$gBitSystem->display( 'bitpackage:stock/import_results.tpl', 'Import KitLocker Assemblies' );
diff --git a/import/load_kitlocker_groups.php b/import/load_kitlocker_groups.php
new file mode 100644
index 0000000..c189769
--- /dev/null
+++ b/import/load_kitlocker_groups.php
@@ -0,0 +1,92 @@
+<?php
+/**
+ * Phase 1: Import KitlockerGroups.csv as child assemblies of assembly 375.
+ *
+ * CSV columns: KLID, Title
+ * Each group assembly is tagged with a KLID xref so load_kitlocker_assemblies.php
+ * can locate the correct parent by KLID lookup.
+ *
+ * Already-existing groups (matched by KLID xref) are skipped.
+ *
+ * @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 STOCK_PKG_CLASS_PATH.'StockAssembly.php';
+
+$csvFile = __DIR__.'/data/KitlockerGroups.csv';
+$parentAssemblyId = 375;
+$loaded = $skipped = 0;
+$errors = [];
+
+// Load parent assembly
+$parent = new StockAssembly( $parentAssemblyId, null );
+$parent->load();
+if( !$parent->isValid() ) {
+ $errors[] = "Parent assembly $parentAssemblyId not found.";
+} elseif( !file_exists( $csvFile ) ) {
+ $errors[] = "File not found: $csvFile";
+} else {
+ $fh = fopen( $csvFile, 'r' );
+ $rowNum = 0;
+ while( ($cols = fgetcsv( $fh )) !== false ) {
+ $rowNum++;
+ if( $rowNum === 1 ) continue; // header: KLID, Title
+
+ $klid = trim( $cols[0] ?? '' );
+ $title = trim( $cols[1] ?? '' );
+ if( $klid === '' || $title === '' ) continue;
+
+ // Skip if KLID xref already exists
+ if( $gBitDb->getOne(
+ "SELECT `content_id` FROM `".BIT_DB_PREFIX."liberty_xref` WHERE `item`=? AND `xkey`=?",
+ [ 'KLID', $klid ]
+ ) ) {
+ $skipped++;
+ continue;
+ }
+
+ $assembly = new StockAssembly();
+ $pHash = [ 'title' => $title, 'edit' => '', 'format_guid' => 'bithtml' ];
+ if( !$assembly->store( $pHash ) ) {
+ $errors[] = "Row $rowNum: failed to create '$title'";
+ continue;
+ }
+
+ $contentId = $assembly->mContentId;
+
+ // Tag with KLID xref
+ $xrefId = $gBitDb->GenID( 'liberty_xref_seq' );
+ $gBitDb->associateInsert( BIT_DB_PREFIX.'liberty_xref', [
+ 'xref_id' => $xrefId,
+ 'content_id' => $contentId,
+ 'item' => 'KLID',
+ 'xorder' => 0,
+ 'xkey' => $klid,
+ 'last_update_date' => $gBitDb->NOW(),
+ ] );
+
+ // Add as child of parent assembly 375
+ $parent->addItem( $contentId );
+
+ $loaded++;
+ }
+ fclose( $fh );
+}
+
+$gBitSmarty->assign( 'loaded', $loaded );
+$gBitSmarty->assign( 'skipped', $skipped );
+$gBitSmarty->assign( 'deleted', 0 );
+$gBitSmarty->assign( 'errors', $errors );
+$gBitSmarty->assign( 'csvFile', $csvFile );
+$gBitSmarty->assign( 'movement', null );
+
+$gBitSystem->display( 'bitpackage:stock/import_results.tpl', 'Import KitLocker Groups' );