diff --git a/app/Jobs/Inventory/AdjustProductInventory.php b/app/Jobs/Inventory/AdjustProductInventory.php new file mode 100644 index 000000000000..60ebdf121538 --- /dev/null +++ b/app/Jobs/Inventory/AdjustProductInventory.php @@ -0,0 +1,95 @@ +company = $company; + $this->invoice = $invoice; + $this->old_invoice = $old_invoice; + } + + /** + * Execute the job. + * + * + * @return false + */ + public function handle() + { + MultiDB::setDb($this->company->db); + + if(count($this->old_invoice) > 0) + return $this->existingInventoryAdjustment(); + + return $this->newInventoryAdjustment(); + + } + + private function newInventoryAdjustment() + { + + $line_items = $this->invoice->line_items; + + foreach($line_items as $item) + { + + $p = Product::where('product_key', $item->product_key)->where('company_id', $this->company->id)->where('in_stock_quantity', '>', 0)->first(); + $p->in_stock_quantity -= $item->quantity; + $p->save(); + + //check threshols and notify user + + if($p->stock_notification_threshold && $p->in_stock_quantity <= $p->stock_notification_threshold) + $this->notifyStockLevels($p, 'product'); + elseif($this->company->stock_notification_threshold && $p->in_stock_quantity <= $this->company->stock_notification_threshold){ + $this->notifyStocklevels($p, 'company'); + } + } + + } + + private function existingInventoryAdjustment() + { + + } + + private function notifyStocklevels(Product $product, string $notification_level) + { + + } + +} diff --git a/app/Models/Company.php b/app/Models/Company.php index 8a08e6f6bb37..63392d8e06e1 100644 --- a/app/Models/Company.php +++ b/app/Models/Company.php @@ -103,6 +103,9 @@ class Company extends BaseModel 'markdown_email_enabled', 'stop_on_unpaid_recurring', 'use_quote_terms_on_conversion', + 'enable_applying_payments', + 'track_inventory', + 'inventory_notification_threshold', ]; protected $hidden = [ diff --git a/app/Models/Product.php b/app/Models/Product.php index 44ba4c668ef2..0988e54bcd79 100644 --- a/app/Models/Product.php +++ b/app/Models/Product.php @@ -36,6 +36,8 @@ class Product extends BaseModel 'tax_rate1', 'tax_rate2', 'tax_rate3', + 'in_stock_quantity', + 'stock_notification_threshold', ]; protected $touches = []; diff --git a/app/Services/Invoice/InvoiceService.php b/app/Services/Invoice/InvoiceService.php index fdb69920eab7..df74ee4fb73b 100644 --- a/app/Services/Invoice/InvoiceService.php +++ b/app/Services/Invoice/InvoiceService.php @@ -13,6 +13,7 @@ namespace App\Services\Invoice; use App\Events\Invoice\InvoiceWasArchived; use App\Jobs\Entity\CreateEntityPdf; +use App\Jobs\Inventory\AdjustProductInventory; use App\Jobs\Invoice\InvoiceWorkflowSettings; use App\Jobs\Util\UnlinkFile; use App\Libraries\Currency\Conversion\CurrencyApi; @@ -564,6 +565,14 @@ class InvoiceService return $this; } + public function adjustInventory() + { + if($this->invoice->company->track_inventory) + AdjustProductInventory::dispatch($this->invoice->company, $this->invoice, null); + + return $this; + } + /** * Saves the invoice. * @return Invoice object diff --git a/database/migrations/2022_05_31_101504_inventory_management_schema.php b/database/migrations/2022_05_31_101504_inventory_management_schema.php new file mode 100644 index 000000000000..ae88786331b0 --- /dev/null +++ b/database/migrations/2022_05_31_101504_inventory_management_schema.php @@ -0,0 +1,43 @@ +boolean('enable_applying_payments')->default(0); + $table->boolean('track_inventory')->default(0); + $table->integer('inventory_notification_threshold')->nullable(); + + }); + + Schema::table('products', function (Blueprint $table){ + $table->integer('in_stock_quantity')->nullable(); + $table->integer('stock_notification_threshold')->nullable(); + }); + + + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + // + } +} +