# Manufacture — business logic

The document for producing a finished item from components — the only
document type where receiving and expenditure happen at once, in a single
action: components are written off stock by recipe (bill of materials), the
finished item is received. A product is an item that itself consists of
other items (see [Concept, "Products — recipes
(BOM)"](concept_en.md#products-recipes-bom)).

## A component is picked from real stock

Just like on expenditure, a component is written off from a specific
warehouse lot — the pick fixes the item, quantity, and price at the moment
the line is added. Adding a product by its recipe unfolds the bill of
materials all at once: the product line (receiving) and all of its
component lines (expenditure) are added in one action. Adding the same
product again in the same document doesn't create duplicates — the old set
of components is removed and rebuilt from the current recipe.

## A shortage doesn't block production

If there aren't enough components in stock, production is still allowed.
The system doesn't refuse to process — it simply records the fact of the
shortage; the decision to restock components is left to the user. This
follows the system's general principle — record reality rather than
enforce the "correct" order of actions (see [Concept, "the main
principle"](concept_en.md#the-main-principle-currency-of-data-matters-more-than-convenience)
on manufacturing from components that aren't in stock).

## What happens on processing

- **Product receiving.** Every product line is received into stock. If the
  product's price isn't set explicitly, it's calculated automatically from
  the cost of the written-off components plus any extra costs — with a
  warning to the user, but without blocking processing.
- **Component expenditure.** Every component is written off from whichever
  lot was picked when the line was added — the same mechanism as an
  ordinary expenditure. "Shortage doesn't block" refers to components that
  were missing when the recipe was unfolded (they're recorded as a
  shortage and skipped); a component that *was* picked from a lot must
  still be there on processing — if someone took it in the meantime,
  processing is refused.
- **The product's supplier is inherited from its components.** If all
  components were written off from a single supplier's lots, the produced
  item is attributed to that supplier; if components came from different
  suppliers, the item is considered owned by the main company. The user
  never specifies a "product's supplier" explicitly — it's a computed
  consequence of where the components actually came from.

## Rolling back manufacture

A rollback returns the components to their lots (with the reservations and
serial numbers they used up) and removes the produced item from stock,
also reducing the project's "made" quantity if the line was produced for a
project. It's refused if the produced item has already been sold or used
further — first roll back the documents that took it.

A stock correction is formally the same document type as manufacture, but
it is processed and rolled back by its own rules ("was/became" for a lot);
the system won't process a correction as manufacture.

## The price mode is fixed for the whole document

The first product added to a document determines the price mode for the
**entire** document, not for an individual line:

- **Manual mode** — the product's price is entered by the user and isn't
  touched by the automatic calculation; every subsequent product on the
  document must also have a manually entered price.
- **Auto mode** — the price is calculated from components and extra costs,
  either on demand or automatically on processing if it isn't set
  explicitly.

The mode isn't reset even if every product on the document is deleted and
re-added — otherwise the manual-mode rule could be bypassed by recreating
the line with the same price through a different path.
