first pass

This commit is contained in:
David Bomba 2022-05-31 21:17:18 +10:00
parent 978884f2ee
commit 3d9bb490e3
5 changed files with 152 additions and 0 deletions

View File

@ -0,0 +1,95 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Jobs\Inventory;
use App\Libraries\MultiDB;
use App\Models\Company;
use App\Models\Invoice;
use App\Models\Product;
use App\Utils\Traits\NumberFormatter;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
//todo - ensure we are MultiDB Aware in dispatched jobs
class AdjustProductInventory implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public Company $company;
public Invoice $invoice;
public array $old_invoice;
public function __construct(Company $company, Invoice $invoice, array $old_invoice = [])
{
$this->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)
{
}
}

View File

@ -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 = [

View File

@ -36,6 +36,8 @@ class Product extends BaseModel
'tax_rate1',
'tax_rate2',
'tax_rate3',
'in_stock_quantity',
'stock_notification_threshold',
];
protected $touches = [];

View File

@ -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

View File

@ -0,0 +1,43 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class InventoryManagementSchema extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('companies', function (Blueprint $table) {
$table->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()
{
//
}
}