summaryrefslogtreecommitdiff
path: root/import/ImportComponent.php
blob: 15f87e7068546e3d7ffbb440363f882c433c6515 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
/**
 * Stock component CSV import record loader.
 *
 * CSV column layout (0-based):
 *   0  title           Component name / description
 *   1  data            Long description (optional)
 *   2  part_number     Supplier part number → #SUP xkey  (optional)
 *   3  quantity_value  Numeric stock quantity             (optional, default 1)
 *   4  quantity_item   SGL / PCK / SHT / VOL             (optional, default SGL)
 *
 * @package stock
 */

use Bitweaver\Stock\StockComponent;
use Bitweaver\Liberty\LibertyContent;

function StockComponentRecordLoad( array $data ): bool {
	$title = trim( $data[0] ?? '' );
	if( empty( $title ) ) {
		return false;
	}

	$component = new StockComponent();

	$pHash = [
		'title'      => $title,
		'edit'       => trim( $data[1] ?? '' ),
		'format_guid' => 'plain',
	];

	if( !$component->store( $pHash ) ) {
		return false;
	}

	// Supplier xref (#SUP) — part number in xkey; xref (contact_id) left empty for manual linking
	$partNumber = trim( $data[2] ?? '' );
	if( $partNumber !== '' ) {
		$xrefHash = [
			'content_id' => $component->mContentId,
			'item'       => '#SUP',
			'xkey'       => $partNumber,
			'fAddXref'   => 1,
		];
		$component->storeXref( $xrefHash );
	}

	// Quantity xref (SGL/PCK/SHT/VOL)
	$qtyValue = isset( $data[3] ) && is_numeric( trim( $data[3] ) ) ? (float)trim( $data[3] ) : null;
	$qtySrc   = strtoupper( trim( $data[4] ?? 'SGL' ) );
	if( !in_array( $qtySrc, [ 'SGL', 'PCK', 'SHT', 'VOL' ] ) ) {
		$qtySrc = 'SGL';
	}
	if( $qtyValue !== null ) {
		$xrefHash = [
			'content_id' => $component->mContentId,
			'item'       => $qtySrc,
			'xkey'       => $qtyValue,
			'fAddXref'   => 1,
		];
		$component->storeXref( $xrefHash );
	}

	return true;
}