summaryrefslogtreecommitdiff
path: root/edit_movement.php
diff options
context:
space:
mode:
authorLester Caine <lester@lsces.co.uk>2026-06-02 17:57:30 +0100
committerLester Caine <lester@lsces.co.uk>2026-06-02 17:57:30 +0100
commiteb6aea780c30d09a507a61267c14e0bbc81b2678 (patch)
tree2fb8b19e51ec4b88ef389a4e2d9008e50573597a /edit_movement.php
parent3bb1b60e08fa4382e4f715d7b94fde875d11cfd1 (diff)
downloadstock-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 'edit_movement.php')
-rw-r--r--edit_movement.php94
1 files changed, 2 insertions, 92 deletions
diff --git a/edit_movement.php b/edit_movement.php
index 429d390..a28f3cf 100644
--- a/edit_movement.php
+++ b/edit_movement.php
@@ -32,96 +32,6 @@ $refTypes = $gBitDb->getAssoc(
ORDER BY xi.`item`"
);
-function stockProcessMovementCsv( StockMovement $pContent, array $pQtyTypes ): array {
- global $gBitDb;
- $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)$gBitDb->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')",
- [ $pContent->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 = $gBitDb->getOne(
- "SELECT `xref_id` FROM `".BIT_DB_PREFIX."liberty_xref`
- WHERE `content_id` = ? AND `item` IN ('REQN','TRANS','ORDER') ORDER BY `xorder`",
- [ $pContent->mContentId ]
- );
- $refHash = [ 'content_id' => $pContent->mContentId, 'item' => 'TRANS', 'xkey' => $ref, 'edit' => $from ];
- $existingXrefId ? $refHash['xref_id'] = $existingXrefId : $refHash['fAddXref'] = 1;
- $pContent->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 ) {
- $gBitDb->query( "UPDATE `".BIT_DB_PREFIX."liberty_content` SET `event_time` = ? WHERE `content_id` = ?", [ $ts, $pContent->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 = $gBitDb->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 = $gBitDb->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';
- }
-
- $pContent->storeXref( [ 'content_id' => $pContent->mContentId, 'item' => $qtySrc, 'xref' => (int)$contentId, 'xkey' => $qty, 'xorder' => $nextXorder ] );
- $nextXorder++;
- $result['loaded']++;
- }
- fclose( $handle );
- $pContent->load();
- return $result;
-}
-
if( !empty( $_REQUEST['fSave'] ) ) {
$isNew = !$gContent->isValid();
if( $gContent->store( $_REQUEST ) ) {
@@ -130,7 +40,7 @@ if( !empty( $_REQUEST['fSave'] ) ) {
$gContent->storeXref( $typeHash );
}
if( $isNew && !empty( $_FILES['csv_file']['tmp_name'] ) && $_FILES['csv_file']['error'] === UPLOAD_ERR_OK ) {
- $csvResult = stockProcessMovementCsv( $gContent, $qtyTypes );
+ $csvResult = $gContent->importCsv( $qtyTypes );
$gBitSmarty->assign( 'csvLoaded', $csvResult['loaded'] );
$gBitSmarty->assign( 'csvSkipped', $csvResult['skipped'] );
$gBitSmarty->assign( 'csvErrors', $csvResult['errors'] );
@@ -150,7 +60,7 @@ if( !empty( $_REQUEST['fSave'] ) ) {
if( !$file || $file['error'] !== UPLOAD_ERR_OK ) {
$gContent->mErrors[] = KernelTools::tra( 'No file uploaded or upload error.' );
} else {
- $csvResult = stockProcessMovementCsv( $gContent, $qtyTypes );
+ $csvResult = $gContent->importCsv( $qtyTypes );
$gBitSmarty->assign( 'csvLoaded', $csvResult['loaded'] );
$gBitSmarty->assign( 'csvSkipped', $csvResult['skipped'] );
$gBitSmarty->assign( 'csvErrors', $csvResult['errors'] );