mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-05-24 02:14:21 -04:00
Inventory management from purchase orders
This commit is contained in:
parent
6b3b50318b
commit
261a2e0ab6
@ -644,6 +644,12 @@ class PurchaseOrderController extends BaseController
|
||||
}
|
||||
break;
|
||||
|
||||
case 'add_to_inventory':
|
||||
|
||||
$purchase_order->service()->add_to_inventory();
|
||||
|
||||
return $this->itemResponse($purchase_order);
|
||||
|
||||
case 'expense':
|
||||
|
||||
if($purchase_order->expense()->exists())
|
||||
|
@ -14,6 +14,7 @@ namespace App\Http\Requests\Expense;
|
||||
use App\Http\Requests\Request;
|
||||
use App\Http\ValidationRules\Expense\UniqueExpenseNumberRule;
|
||||
use App\Models\Expense;
|
||||
use App\Models\PurchaseOrder;
|
||||
use App\Utils\Traits\MakesHash;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
@ -41,6 +42,9 @@ class StoreExpenseRequest extends Request
|
||||
if(!empty($this->client_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);
|
||||
}
|
||||
|
||||
@ -58,6 +62,10 @@ class StoreExpenseRequest extends Request
|
||||
$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']))
|
||||
$input['color'] = '';
|
||||
|
||||
|
@ -56,6 +56,7 @@ class Expense extends BaseModel
|
||||
'tax_amount3',
|
||||
'uses_inclusive_taxes',
|
||||
'calculate_tax_by_amount',
|
||||
'purchase_order_id',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
|
54
app/Services/PurchaseOrder/PurchaseOrderInventory.php
Normal file
54
app/Services/PurchaseOrder/PurchaseOrderInventory.php
Normal 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;
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -116,6 +116,13 @@ class PurchaseOrderService
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function add_to_inventory()
|
||||
{
|
||||
$this->purchase_order (new PurchaseOrderInventory($this->purchase_order))->run();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function expense()
|
||||
{
|
||||
$this->markSent();
|
||||
|
Loading…
x
Reference in New Issue
Block a user