app/Services/ and handle complex operations, data validation, and coordination between models.
Core Principles
- Thin Controllers: All business logic resides in services
- Transaction Management: Critical operations use database transactions
- Cache Invalidation: Services handle cache management automatically
- Tenant Isolation: All operations are tenant-scoped for security
- Audit Logging: Services log all significant operations
ProductService
Manages product creation, updates, variants, and packaging types. Location:app/Services/ProductService.php
create()
Creates a new product with variants and packaging types.Product data including name, type, variants, and packaging
The tenant instance
The shop instance
Created product with relationships loaded
update()
Updates an existing product.The product to update
Updated product data
Updated product with fresh relationships
updateVariant()
Updates a product variant. Automatically adjusts packaging type prices if variant price changes.The variant to update
Updated variant data
Updated variant with relationships
OrderService
Handles order lifecycle management including creation, status transitions, and fulfillment. Location:app/Services/OrderService.php
createOrder()
Creates a new order with items.Tenant instance
Shop instance
Array of order items
User creating the order
Customer user (optional)
Created order with items and totals calculated
confirmOrder()
Confirms an order and reserves inventory.Order to confirm
User confirming the order
Confirmed order with reserved inventory
Stock Reservation Logic
Stock Reservation Logic
When an order is confirmed:
- Uses pessimistic locking to prevent race conditions
- Checks available stock (quantity - reserved_quantity)
- Atomically increments reserved_quantity
- Throws exception if insufficient stock
- Updates order status to CONFIRMED
fulfillOrder()
Fulfills a confirmed order by deducting inventory and creating stock movements.Confirmed order to fulfill
User fulfilling the order
Fulfilled order with status updated to PROCESSING
cancelOrder()
Cancels an order and releases reserved inventory.Order to cancel
User canceling the order
Cancellation reason
Cancelled order
CustomerService
Manages customer accounts, addresses, credit limits, and search. Location:app/Services/CustomerService.php
create()
Creates a new customer account.Customer data
Tenant instance
Created customer with address
list()
Get paginated customer list with filters.Tenant instance
User requesting the list
Filter options
Paginated customer collection
POSService
Handles Point-of-Sale operations including quick sales and session management. Location:app/Services/POSService.php
createQuickSale()
Creates an immediate sale with payment and stock deduction.Shop where sale is occurring
Array of sale items
Customer ID (optional)
Payment method (cash, card, mobile_money)
Cash amount tendered
Additional options (discount_amount, notes, etc.)
Completed order with DELIVERED status
POS Sale Process
POS Sale Process
- Validates stock availability using pessimistic locking
- Creates order with DELIVERED status
- Calculates totals including tax and discounts
- Creates order items
- Records stock movements
- Creates payment record
- Returns completed order
getSessionSummary()
Get sales summary for current user’s POS session.Shop instance
Start date (YYYY-MM-DD)
End date (YYYY-MM-DD)
Session statistics including total sales, revenue, and payment method breakdowns
CartService
Manages shopping cart operations for e-commerce storefronts. Location:app/Services/CartService.php
getCart()
Retrieves or creates a cart for the current session or customer.Shop instance
Customer ID for logged-in users
Cart instance
addItem()
Adds a product to the cart.Cart instance
Product variant ID
Quantity to add
Packaging type ID (optional)
Created or updated cart item
getCartSummary()
Calculates cart totals including shipping and tax.Cart instance
Cart summary with items, subtotal, shipping, tax, and total
mergeGuestCartIntoCustomerCart()
Merges a guest cart into a customer cart upon login.Guest session ID
Customer ID
Shop ID
Merged customer cart
Additional Services
- StockMovementService
- PayrollService
- ReportService
- TaxService
- CheckoutService
- NotificationService
- ApprovalService
- SupplierService
- PaymentGateway
Location:
app/Services/StockMovementService.phpHandles all inventory adjustments with audit trails.Key Methods
adjustStock()- Record inventory changesrecordSale()- Record sale stock movementcheckStockAvailability()- Validate stock levelsgetAvailableStock()- Get current available quantity
Service Best Practices
Transaction Management
Transaction Management
All services use database transactions for data consistency:
Cache Invalidation
Cache Invalidation
Services automatically invalidate relevant caches:
Tenant Isolation
Tenant Isolation
All queries are scoped to the current tenant:
Error Handling
Error Handling
Services throw exceptions with clear messages: