1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-11 21:52:35 +01:00
invoiceninja/app/Services/PurchaseOrder/PurchaseOrderExpense.php

69 lines
2.3 KiB
PHP
Raw Normal View History

2022-07-06 11:25:22 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2023-01-28 23:21:40 +01:00
* @copyright Copyright (c) 2023. 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;
use App\Utils\Traits\GeneratesCounter;
2022-07-06 11:25:22 +02:00
class PurchaseOrderExpense
{
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;
$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;
$line_items = $this->purchase_order->line_items;
$expense->public_notes = '';
2023-02-16 02:36:09 +01:00
foreach ($line_items as $line_item) {
$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) {
$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;
$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;
}
}