diff options
| author | Lester Caine <lester@lsces.co.uk> | 2026-06-02 17:57:30 +0100 |
|---|---|---|
| committer | Lester Caine <lester@lsces.co.uk> | 2026-06-02 17:57:30 +0100 |
| commit | eb6aea780c30d09a507a61267c14e0bbc81b2678 (patch) | |
| tree | 2fb8b19e51ec4b88ef389a4e2d9008e50573597a /includes | |
| parent | 3bb1b60e08fa4382e4f715d7b94fde875d11cfd1 (diff) | |
| download | stock-eb6aea780c30d09a507a61267c14e0bbc81b2678.tar.gz stock-eb6aea780c30d09a507a61267c14e0bbc81b2678.tar.bz2 stock-eb6aea780c30d09a507a61267c14e0bbc81b2678.zip | |
Move CSV import into StockMovement::importCsv(); fix storeXref by-ref
Standalone function removed from edit_movement.php; logic lives in
the method using \$this->mDb. Named variable used for item storeXref
call — literal arrays cause fatal error (passed by reference).
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Diffstat (limited to 'includes')
| -rw-r--r-- | includes/classes/StockMovement.php | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/includes/classes/StockMovement.php b/includes/classes/StockMovement.php index a939a3d..ebcbf4e 100644 --- a/includes/classes/StockMovement.php +++ b/includes/classes/StockMovement.php @@ -280,4 +280,96 @@ class StockMovement extends LibertyContent { public static function getServiceKey(): string { return 'stock'; } + + public function importCsv( array $pQtyTypes ): array { + $result = [ 'loaded' => 0, 'skipped' => 0, 'errors' => [] ]; + $file = $_FILES['csv_file'] ?? null; + if( !$file || $file['error'] !== UPLOAD_ERR_OK ) { + return $result; + } + $handle = fopen( $file['tmp_name'], 'r' ); + if( $handle === false ) { + return $result; + } + + $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; + + $rowNum = 0; + while( ( $data = fgetcsv( $handle, 1000, ',', '"', '' ) ) !== false ) { + $rowNum++; + if( $rowNum === 1 ) { + $from = trim( $data[0] ?? '' ); + $ref = trim( $data[1] ?? '' ); + $dateStr = trim( $data[2] ?? '' ); + if( $ref !== '' ) { + $existingXrefId = $this->mDb->getOne( + "SELECT `xref_id` FROM `".BIT_DB_PREFIX."liberty_xref` + WHERE `content_id` = ? AND `item` IN ('REQN','TRANS','ORDER') ORDER BY `xorder`", + [ $this->mContentId ] + ); + $refHash = [ 'content_id' => $this->mContentId, 'item' => 'TRANS', 'xkey' => $ref, 'edit' => $from ]; + $existingXrefId ? $refHash['xref_id'] = $existingXrefId : $refHash['fAddXref'] = 1; + $this->storeXref( $refHash ); + } + if( $dateStr !== '' ) { + $parts = explode( '/', $dateStr ); + if( count( $parts ) === 3 ) { + $year = (int)$parts[2] < 100 ? 2000 + (int)$parts[2] : (int)$parts[2]; + $ts = mktime( 0, 0, 0, (int)$parts[1], (int)$parts[0], $year ); + if( $ts ) { + $this->mDb->query( + "UPDATE `".BIT_DB_PREFIX."liberty_content` SET `event_time` = ? WHERE `content_id` = ?", + [ $ts, $this->mContentId ] + ); + } + } + } + continue; + } + + $componentName = trim( $data[0] ?? '' ); + $qty = (float)trim( $data[1] ?? '' ); + $qtyOverride = strtoupper( trim( $data[2] ?? '' ) ); + + if( $componentName === '' ) { $result['skipped']++; continue; } + if( $qty <= 0 ) { + $result['errors'][] = "Row $rowNum: '$componentName' — invalid quantity, skipped."; + $result['skipped']++; + continue; + } + + $contentId = $this->mDb->getOne( + "SELECT lc.`content_id` FROM `".BIT_DB_PREFIX."liberty_content` lc + WHERE lc.`content_type_guid` = 'stockcomponent' AND lc.`title` = ?", + [ $componentName ] + ); + if( !$contentId ) { + $result['errors'][] = "Row $rowNum: '$componentName' not found, skipped."; + $result['skipped']++; + continue; + } + + $qtySrc = in_array( $qtyOverride, $pQtyTypes ) ? $qtyOverride : null; + if( !$qtySrc ) { + $placeholders = implode( ',', array_fill( 0, count( $pQtyTypes ), '?' ) ); + $qtySrc = $this->mDb->getOne( + "SELECT x.`item` FROM `".BIT_DB_PREFIX."liberty_xref` x + WHERE x.`content_id` = ? AND x.`item` IN ($placeholders) ORDER BY x.`xorder`", + array_merge( [ (int)$contentId ], $pQtyTypes ) + ) ?: 'SGL'; + } + + $itemHash = [ 'content_id' => $this->mContentId, 'item' => $qtySrc, 'xref' => (int)$contentId, 'xkey' => $qty, 'xorder' => $nextXorder ]; + $this->storeXref( $itemHash ); + $nextXorder++; + $result['loaded']++; + } + fclose( $handle ); + $this->load(); + return $result; + } } |
