1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 13:12:50 +01:00

Inventory management from purchase orders

This commit is contained in:
David Bomba 2022-07-06 20:04:59 +10:00
parent 6b3b50318b
commit 261a2e0ab6
5 changed files with 76 additions and 0 deletions

View File

@ -644,6 +644,12 @@ class PurchaseOrderController extends BaseController
} }
break; break;
case 'add_to_inventory':
$purchase_order->service()->add_to_inventory();
return $this->itemResponse($purchase_order);
case 'expense': case 'expense':
if($purchase_order->expense()->exists()) if($purchase_order->expense()->exists())

View File

@ -14,6 +14,7 @@ namespace App\Http\Requests\Expense;
use App\Http\Requests\Request; use App\Http\Requests\Request;
use App\Http\ValidationRules\Expense\UniqueExpenseNumberRule; use App\Http\ValidationRules\Expense\UniqueExpenseNumberRule;
use App\Models\Expense; use App\Models\Expense;
use App\Models\PurchaseOrder;
use App\Utils\Traits\MakesHash; use App\Utils\Traits\MakesHash;
use Illuminate\Validation\Rule; use Illuminate\Validation\Rule;
@ -41,6 +42,9 @@ class StoreExpenseRequest extends Request
if(!empty($this->client_id)) if(!empty($this->client_id))
$rules['client_id'] = 'bail|sometimes|exists:clients,id,company_id,'.auth()->user()->company()->id; $rules['client_id'] = 'bail|sometimes|exists:clients,id,company_id,'.auth()->user()->company()->id;
if(!empty($this->purchase_order_id))
$rules['purchase_order_id'] = 'bail|sometimes|exists:purchase_orders,id,company_id,'.auth()->user()->company()->id;
return $this->globalRules($rules); return $this->globalRules($rules);
} }
@ -58,6 +62,10 @@ class StoreExpenseRequest extends Request
$input['currency_id'] = (string)auth()->user()->company()->settings->currency_id; $input['currency_id'] = (string)auth()->user()->company()->settings->currency_id;
} }
if (! array_key_exists('purchase_order_id', $input) || strlen($input['purchase_order_id']) == 0) {
$input['purchase_order_id'] = $this->decodePrimaryKey($input['purchase_order_id']);
}
if(array_key_exists('color', $input) && is_null($input['color'])) if(array_key_exists('color', $input) && is_null($input['color']))
$input['color'] = ''; $input['color'] = '';

View File

@ -56,6 +56,7 @@ class Expense extends BaseModel
'tax_amount3', 'tax_amount3',
'uses_inclusive_taxes', 'uses_inclusive_taxes',
'calculate_tax_by_amount', 'calculate_tax_by_amount',
'purchase_order_id',
]; ];
protected $casts = [ protected $casts = [

View File

@ -0,0 +1,54 @@
<?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\Services\PurchaseOrder;
use App\Factory\ExpenseFactory;
use App\Jobs\Mail\NinjaMailer;
use App\Jobs\Mail\NinjaMailerJob;
use App\Jobs\Mail\NinjaMailerObject;
use App\Mail\Admin\InventoryNotificationObject;
use App\Models\Product;
use App\Models\PurchaseOrder;
class PurchaseOrderInventory
{
private PurchaseOrder $purchase_order;
public function __construct(PurchaseOrder $purchase_order)
{
$this->purchase_order = $purchase_order;
}
public function run()
{
$line_items = $this->purchase_order->line_items;
foreach($line_items as $item)
{
$p = Product::where('product_key', $item->product_key)->where('company_id', $this->purchase_order->company_id)->first();
if(!$p)
continue;
$p->in_stock_quantity += $item->quantity;
$p->saveQuietly();
}
return $this->purchase_order;
}
}

View File

@ -116,6 +116,13 @@ class PurchaseOrderService
return $this; return $this;
} }
public function add_to_inventory()
{
$this->purchase_order (new PurchaseOrderInventory($this->purchase_order))->run();
return $this;
}
public function expense() public function expense()
{ {
$this->markSent(); $this->markSent();