Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
SyncPosStock
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
6
0.00% covered (danger)
0.00%
0 / 1
 handle
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace App\Console\Commands;
4
5use App\Models\admin\Product;
6use App\Service\StockService;
7use Illuminate\Console\Command;
8
9class SyncPosStock extends Command
10{
11    protected $signature = 'pos:sync-stock';
12
13    protected $description = 'Backfill/refresh the stocks table (what the POS actually sells from) from every product\'s real inventory fields (Product.stock / ProductVariation.stock_quantity). Safe to run any time — it never deletes a Stock row, only creates/updates them, so it can\'t wipe historical order data.';
14
15    public function handle()
16    {
17        // WHY THIS EXISTS: the POS "Create Sale" page only ever reads from the
18        // `stocks` table (order_details.stock_id has a real foreign key to
19        // stocks.id, so nothing else works). But product creation/editing on
20        // the current product form — and the CSV importer — save inventory on
21        // Product.stock / ProductVariation.stock_quantity instead, and never
22        // touched `stocks` at all until this session's fix. Every product
23        // created or imported before that fix has zero Stock rows and is
24        // invisible to the POS. Run this once after pulling the fix to catch
25        // those existing products up; new products stay in sync automatically
26        // going forward via ProductController::store()/update().
27        $products = Product::with('variations')->get();
28        $this->info("Syncing POS stock for {$products->count()} product(s)...");
29
30        $bar = $this->output->createProgressBar($products->count());
31        $bar->start();
32
33        foreach ($products as $product) {
34            StockService::syncStockForProduct($product);
35            $bar->advance();
36        }
37
38        $bar->finish();
39        $this->newLine(2);
40        $this->info('Done. Every product now has a matching Stock row (or one per variation) and should appear in the POS.');
41
42        return Command::SUCCESS;
43    }
44}