# Sales Order Item Promised Date Default Implementation Plan

> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.

**Goal:** Make sales order line promised dates default to the sales order promised date while preserving user-edited line dates.

**Architecture:** This is a frontend-only interaction change because the backend already falls back from line promised date to order promised date on save. `SalesPlanningView.vue` will track whether a line date was manually edited, auto-fill empty/unmodified rows when the header date changes, and default newly added rows from the current header date.

**Tech Stack:** Vue 3 Composition API, existing Vite frontend, lightweight Node source-regression script.

---

### Task 1: Add Regression Coverage

**Files:**
- Create: `frontend/scripts/test-sales-order-item-promised-date-default.mjs`
- Read: `frontend/src/views/SalesPlanningView.vue`

- [ ] **Step 1: Write source-regression checks**

```js
import assert from "node:assert/strict";
import fs from "node:fs";
import path from "node:path";

const viewPath = path.resolve("src/views/SalesPlanningView.vue");
const source = fs.readFileSync(viewPath, "utf8");

assert.match(source, /promised_date_touched:\s*false/, "new sales order lines should track whether line promised date was manually edited");
assert.match(source, /@input="markSalesOrderItemPromisedDateTouched\(item\)"/, "line promised date input should mark the row as user-edited");
assert.match(source, /syncSalesOrderItemPromisedDatesFromHeader/, "header promised date changes should synchronize eligible line dates");
assert.match(source, /!item\.promised_date_touched/, "sync should only update untouched line promised dates");
assert.match(source, /promised_date:\s*salesOrderForm\.promised_date\s*\|\|\s*null/, "submit should fall back empty line dates to the sales order promised date");

console.log("sales order line promised date default checks passed");
```

- [ ] **Step 2: Run test to verify it fails**

Run: `cd frontend && node scripts/test-sales-order-item-promised-date-default.mjs`

Expected: FAIL before implementation because the source does not yet track `promised_date_touched`.

### Task 2: Implement Frontend Behavior

**Files:**
- Modify: `frontend/src/views/SalesPlanningView.vue`

- [ ] **Step 1: Add row touch tracking and defaulting**

Change `buildSalesOrderItemRow()` so each new row defaults from the current order promised date and tracks manual edits:

```js
function buildSalesOrderItemRow() {
  return {
    row_uid: `sales-order-item-${salesOrderItemSerial++}`,
    product_item_id: "",
    order_qty: 0,
    unit_price: 0,
    promised_date: salesOrderForm.promised_date || "",
    promised_date_touched: false,
    customer_part_no: ""
  };
}
```

- [ ] **Step 2: Add header-date synchronization helpers**

Add these helpers near `addSalesOrderItem()`:

```js
function syncSalesOrderItemPromisedDatesFromHeader() {
  salesOrderForm.items.forEach((item) => {
    if (!item.promised_date_touched || !item.promised_date) {
      item.promised_date = salesOrderForm.promised_date || "";
    }
  });
}

function markSalesOrderItemPromisedDateTouched(item) {
  item.promised_date_touched = true;
}
```

- [ ] **Step 3: Wire the header date watcher and line input**

Add a watcher:

```js
watch(
  () => salesOrderForm.promised_date,
  () => syncSalesOrderItemPromisedDatesFromHeader()
);
```

Update the line promised date input:

```vue
<input v-model="item.promised_date" type="date" @input="markSalesOrderItemPromisedDateTouched(item)" />
```

- [ ] **Step 4: Add submit fallback**

Change the submitted line payload:

```js
promised_date: item.promised_date || salesOrderForm.promised_date || null,
```

### Task 3: Verify

**Files:**
- Test: `frontend/scripts/test-sales-order-item-promised-date-default.mjs`
- Test: frontend production build

- [ ] **Step 1: Run source-regression script**

Run: `cd frontend && node scripts/test-sales-order-item-promised-date-default.mjs`

Expected: PASS with `sales order line promised date default checks passed`.

- [ ] **Step 2: Run frontend build**

Run: `cd frontend && npm run build`

Expected: build exits 0. The existing Vite chunk-size warning may remain.

