2022-07-06 11:25:22 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2024-04-12 06:15:41 +02:00
|
|
|
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
|
2022-07-06 11:25:22 +02:00
|
|
|
*
|
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Services\PurchaseOrder;
|
|
|
|
|
|
|
|
use App\Factory\ExpenseFactory;
|
|
|
|
use App\Models\PurchaseOrder;
|
2022-07-08 09:30:25 +02:00
|
|
|
use App\Utils\Traits\GeneratesCounter;
|
2022-07-06 11:25:22 +02:00
|
|
|
|
|
|
|
class PurchaseOrderExpense
|
|
|
|
{
|
2022-07-08 09:30:25 +02:00
|
|
|
use GeneratesCounter;
|
2022-07-06 11:25:22 +02:00
|
|
|
|
|
|
|
private PurchaseOrder $purchase_order;
|
|
|
|
|
|
|
|
public function __construct(PurchaseOrder $purchase_order)
|
|
|
|
{
|
|
|
|
$this->purchase_order = $purchase_order;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
$expense = ExpenseFactory::create($this->purchase_order->company_id, $this->purchase_order->user_id);
|
2022-07-07 14:09:39 +02:00
|
|
|
|
|
|
|
$expense->amount = $this->purchase_order->uses_inclusive_taxes ? $this->purchase_order->amount : ($this->purchase_order->amount - $this->purchase_order->total_taxes);
|
|
|
|
|
2022-07-06 11:25:22 +02:00
|
|
|
$expense->date = now();
|
|
|
|
$expense->vendor_id = $this->purchase_order->vendor_id;
|
|
|
|
$expense->public_notes = $this->purchase_order->public_notes;
|
2022-07-07 14:09:39 +02:00
|
|
|
$expense->uses_inclusive_taxes = $this->purchase_order->uses_inclusive_taxes;
|
|
|
|
$expense->calculate_tax_by_amount = true;
|
2022-07-08 09:28:49 +02:00
|
|
|
$expense->private_notes = ctrans('texts.purchase_order_number_short') . " " . $this->purchase_order->number;
|
2022-12-19 11:29:58 +01:00
|
|
|
$expense->currency_id = $this->purchase_order->vendor->currency_id;
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2022-07-08 09:28:49 +02:00
|
|
|
$line_items = $this->purchase_order->line_items;
|
|
|
|
|
|
|
|
$expense->public_notes = '';
|
2022-07-08 09:30:25 +02:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
foreach ($line_items as $line_item) {
|
2022-07-08 09:28:49 +02:00
|
|
|
$expense->public_notes .= $line_item->quantity . " x " . $line_item->product_key. " [ " .$line_item->notes . " ]\n";
|
|
|
|
}
|
2022-07-07 14:09:39 +02:00
|
|
|
|
|
|
|
$tax_map = $this->purchase_order->calc()->getTaxMap();
|
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
if ($this->purchase_order->total_taxes > 0) {
|
2022-07-08 09:28:49 +02:00
|
|
|
$expense->tax_amount1 = $this->purchase_order->total_taxes;
|
|
|
|
$expense->tax_name1 = ctrans("texts.tax");
|
|
|
|
}
|
2022-07-07 14:09:39 +02:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
$expense->number = empty($expense->number) ? $this->getNextExpenseNumber($expense) : $expense->number;
|
2022-07-08 09:30:25 +02:00
|
|
|
|
2024-03-24 21:46:17 +01:00
|
|
|
if($this->purchase_order->project_id){
|
|
|
|
$expense->project_id = $this->purchase_order->project_id;
|
|
|
|
$expense->client_id = $this->purchase_order->project->client_id;
|
|
|
|
}
|
|
|
|
elseif($this->purchase_order->client_id)
|
|
|
|
$expense->client_id = $this->purchase_order->client_id;
|
|
|
|
|
2023-08-14 12:01:28 +02:00
|
|
|
$expense->saveQuietly();
|
2023-02-01 05:00:45 +01:00
|
|
|
event('eloquent.created: App\Models\Expense', $expense);
|
2022-07-06 11:25:22 +02:00
|
|
|
|
2022-07-07 14:09:39 +02:00
|
|
|
$this->purchase_order->expense_id = $expense->id;
|
2023-02-01 05:00:45 +01:00
|
|
|
$this->purchase_order->saveQuietly();
|
2022-07-07 14:09:39 +02:00
|
|
|
|
2022-07-06 11:25:22 +02:00
|
|
|
return $expense;
|
|
|
|
}
|
|
|
|
}
|