# SanMar Artisan Commands

All commands are run from the Laravel backend root directory.

---

## 1. `sanmar:sync`

**Full sync** — connects to SanMar SFTP, downloads all available files, and processes everything into the database.

```bash
php artisan sanmar:sync
```

### What it does
- Connects to SanMar SFTP (`ftp.sanmar.com:2200`)
- Lists and downloads all available files from `/SanMarPDD`
- Processes in this order:
  1. `SanMar_EPDD.csv` — full product + variant data (titles, descriptions, images, pricing, inventory)
  2. `sanmar_shopify.csv` — enriches with Shopify-formatted images and pricing
  3. `SanMar_SDL_N.csv` — additional product data (fallback if EPDD unavailable)
  4. `sanmar_pdd.txt` — GTIN + extended descriptions
  5. `Catalog.txt` — extended descriptions
  6. `sanmar_SaleItems.txt` — current sale items
  7. `sanmar_dip.txt` — per-warehouse inventory (most detailed)
  8. `sanmar_activeproductsexport.txt` — fallback inventory
  9. `sanmar_dp.csv` — account-specific pricing (`my_price`) *(optional — must be requested from SanMar)*
  10. `sanmar_dpIncentive.csv` — incentive pricing *(optional)*
  11. `sanmar_dpc.csv` — delta pricing changes only *(optional)*
- Logs sync result to `sanmar_sync_logs` table

### When to run
- Weekly (Sunday 11pm) via scheduler
- Manually when you need a full refresh

### Schedule (set in `routes/console.php`)
```php
Schedule::command('sanmar:sync')->weeklyOn(0, '23:00'); // Sunday 11pm
```

---

## 2. `sanmar:sync-inventory`

**Inventory-only sync** — downloads only `sanmar_dip.txt` and updates stock levels.

```bash
php artisan sanmar:sync-inventory
```

### What it does
- Connects to SanMar SFTP
- Downloads only `sanmar_dip.txt` (updated hourly by SanMar)
- Updates per-warehouse inventory in `sanmar_warehouse_inventory` table
- Updates total inventory quantity in `sanmar_variants` table
- Logs every quantity change to `sanmar_inventory_logs` table

### When to run
- Daily at 6am via scheduler
- Manually when you need a quick inventory refresh

### Schedule (set in `routes/console.php`)
```php
Schedule::command('sanmar:sync-inventory')->dailyAt('06:00');
```

---

## 3. `sanmar:process-local`

**Process local files** — processes already-downloaded files in `storage/app/sanmar/` without re-connecting to SFTP.

```bash
# Process all available local files
php artisan sanmar:process-local

# Process a specific file only
php artisan sanmar:process-local --file=epdd
```

### Available `--file` options

| Option | File | What it processes |
|---|---|---|
| `epdd` | `SanMar_EPDD.csv` | Full product + variant data, all images, pricing |
| `shopify` | `sanmar_shopify.csv` | Shopify-formatted images and pricing |
| `dip` | `sanmar_dip.txt` | Per-warehouse inventory |
| `pdd` | `sanmar_pdd.txt` | GTIN + extended descriptions |
| `catalog` | `Catalog.txt` | Extended descriptions (TAB delimited) |
| `active_products` | `sanmar_activeproductsexport.txt` | Per-warehouse inventory (fallback) |
| `sale_items` | `sanmar_SaleItems.txt` | Current sale items + pricing |
| `sdl_n` | `SanMar_SDL_N.csv` | Basic product data (fallback) |
| `daily_pricing` | `sanmar_dp.csv` | Account-specific pricing (`my_price`) |
| `incentive` | `sanmar_dpIncentive.csv` | Incentive pricing |
| `delta` | `sanmar_dpc.csv` | Delta pricing (changed prices only) |

### When to use
- When you already have files downloaded and want to re-process without hitting SFTP
- When fixing data issues without re-downloading
- When testing locally with downloaded files
- When you want to populate missing fields (e.g. `pms_color`, `color_square_image`) from an existing EPDD file

### Example — populate missing fields from existing EPDD
```bash
php artisan sanmar:process-local --file=epdd
```

---

## HTTP Endpoints (trigger via API)

These endpoints dispatch commands to the background queue — no timeout risk.

| Method | Endpoint | Triggers |
|---|---|---|
| `POST` | `/api/sanmar/sync` | `sanmar:sync` |
| `POST` | `/api/sanmar/sync-inventory` | `sanmar:sync-inventory` |
| `GET` | `/api/sanmar/status` | Returns last 10 sync logs |

---

## Database Tables Updated by Commands

| Table | Updated by |
|---|---|
| `sanmar_products` | `sanmar:sync`, `sanmar:process-local` |
| `sanmar_variants` | `sanmar:sync`, `sanmar:process-local` |
| `sanmar_warehouse_inventory` | `sanmar:sync`, `sanmar:sync-inventory`, `sanmar:process-local --file=dip` |
| `sanmar_inventory_logs` | `sanmar:sync`, `sanmar:sync-inventory` |
| `sanmar_sync_logs` | `sanmar:sync`, `sanmar:sync-inventory` |

---

## Required `.env` Variables

```env
SANMAR_FTP_HOST=ftp.sanmar.com
SANMAR_FTP_PORT=2200
SANMAR_FTP_USERNAME=your_customer_number
SANMAR_FTP_PASSWORD=your_ftp_password
```

---

## Activating the Scheduler (Bluehost cPanel)

Add a single cron job in cPanel to activate Laravel's scheduler:

```
* * * * * cd /home/your-user/path/to/backend && php artisan schedule:run >> /dev/null 2>&1
```

Laravel handles the rest — running `sanmar:sync` weekly and `sanmar:sync-inventory` daily automatically.