Overview
The sales and purchases tables manage the core transactional data in TechCore Mini ERP. Both follow a master-detail pattern where header tables (ventas/compras) store transaction summaries, and detail tables (ventasDetalle/comprasDetalle) store line items.Ventas Table
Theventas table stores sales transaction headers including customer information, totals, payment terms, and credit management.
SQL Schema
Columns
Primary key. Order/invoice number that uniquely identifies the sales transaction.
Sequential order number. Used for sorting and referencing sales orders.
Foreign key to
clientes table. Identifies the customer for this sale. Required field.Foreign key to
users table. Identifies the salesperson/user who processed the transaction. Required field.Transaction date/time. Defaults to current date/time (GETDATE()).
Sum of all line items before tax. Required field. Calculated from ventasDetalle records.
Tax amount (IVA = Impuesto al Valor Agregado / Value Added Tax). Required field.
Total amount including tax (subtotal + iva). Required field.
Payment type. Defaults to ‘CONTADO’ (cash). Valid values: ‘CONTADO’ (cash/immediate) or ‘CREDITO’ (credit/installments).
Number of months for credit payments. Nullable (only used when tipoPago = ‘CREDITO’).
Interest rate for credit payments (percentage). Defaults to 0. Used to calculate installment amounts.
Remaining balance for credit sales. Required field. Updated automatically by TR_ActualizarSaldo trigger when payments are received.
Void/cancellation flag. Defaults to 0 (valid). Set to 1 to void the transaction without deleting it. Voided sales don’t affect inventory.
Status flag. Defaults to 1 (active).
Constraints
- Primary Key:
norden - Foreign Keys:
codclienreferencesclientes(codclien)codvendreferencesusers(id)
Indexes
- IDX_ventas_codclien: Customer-based queries
- IDX_ventas_codvend: Salesperson performance reports
- IDX_ventas_fecha: Date range queries and reports
- IDX_ventas_tipoPago: Payment type filtering
- IDX_ventas_nula: Filtered index for non-voided sales (optimized for active transactions)
C# Model
VentasDetalle Table
TheventasDetalle table stores individual line items for each sale, including product details, quantities, and pricing.
SQL Schema
Columns
Primary key with auto-increment. Uniquely identifies each line item.
Foreign key to
ventas table. Links the line item to its parent sale. CASCADE DELETE ensures line items are removed when the sale is deleted.Foreign key to
productos table. Identifies the product being sold. Required field.Quantity sold. Required field.
Unit sale price at the time of transaction. Required field. Preserves historical pricing even if product prices change.
Line item subtotal (cantidad * pventa). Required field.
Constraints
- Primary Key:
id - Foreign Keys:
nordenreferencesventas(norden)ON DELETE CASCADEcodprodreferencesproductos(codprod)
Indexes
- IDX_ventasDetalle_norden: Line item lookups by order
- IDX_ventasDetalle_codprod: Product sales history queries
C# Model
Compras Table
Thecompras table stores purchase order headers including supplier information and totals.
SQL Schema
Columns
Primary key. Purchase order number that uniquely identifies the transaction.
Sequential order number. Used for sorting and referencing purchase orders.
Foreign key to
proveedores table. Identifies the supplier. Required field.Foreign key to
users table. Identifies the user who processed the purchase. Required field.Transaction date/time. Defaults to current date/time (GETDATE()).
Sum of all line items before tax. Required field. Calculated from comprasDetalle records.
Tax amount. Required field.
Total amount including tax (subtotal + iva). Required field.
Status flag. Defaults to 1 (active). Uses INT for potential future status codes.
Constraints
- Primary Key:
norden - Foreign Keys:
codprovreferencesproveedores(codprovee)codusureferencesusers(id)
Indexes
- IDX_compras_codprov: Supplier-based queries
- IDX_compras_codusu: User activity tracking
- IDX_compras_fecha: Date range queries
- IDX_compras_estado: Status filtering
C# Model
ComprasDetalle Table
ThecomprasDetalle table stores individual line items for each purchase order.
SQL Schema
Columns
Primary key with auto-increment. Uniquely identifies each line item.
Foreign key to
compras table. Links the line item to its parent purchase order. CASCADE DELETE ensures line items are removed when the purchase is deleted.Foreign key to
productos table. Identifies the product being purchased. Required field.Quantity purchased. Required field.
Unit purchase price at the time of transaction. Required field. Preserves historical pricing.
Line item subtotal (cantidad * precio). Required field.
Constraints
- Primary Key:
id - Foreign Keys:
nordenreferencescompras(norden)ON DELETE CASCADEcodprodreferencesproductos(codprod)
Indexes
- IDX_comprasDetalle_norden: Line item lookups by purchase order
- IDX_comprasDetalle_codprod: Product purchase history queries
C# Model
Entity Relationships
Inventory Trigger
Stock is automatically decreased when sales are created:Usage Notes
- Both sales and purchases use master-detail pattern with CASCADE DELETE on detail tables
- Historical pricing is preserved in detail tables (pventa/precio)
- The
nulafield in ventas allows voiding transactions without deletion - Sales support credit terms with installment tracking via planPagos table
- All monetary values use DECIMAL(18,2) for precise calculations
- Indexes are optimized for common reporting queries (by date, customer, supplier, salesperson)