2022-05-29 04:13:09 +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-05-29 04:13:09 +02:00
*
* @ license https :// www . elastic . co / licensing / elastic - license
*/
namespace App\Services\PurchaseOrder ;
2024-03-10 15:23:52 +01:00
use App\Jobs\EDocument\CreateEDocument ;
2022-05-29 04:13:09 +02:00
use App\Models\PurchaseOrder ;
2024-03-10 15:23:52 +01:00
use App\Utils\Ninja ;
2022-05-29 04:13:09 +02:00
use App\Utils\Traits\MakesHash ;
2024-03-19 15:39:29 +01:00
use Illuminate\Support\Facades\Storage ;
2022-05-29 04:13:09 +02:00
class PurchaseOrderService
{
use MakesHash ;
2023-08-14 12:01:28 +02:00
public function __construct ( public PurchaseOrder $purchase_order )
2022-05-29 04:13:09 +02:00
{
}
2022-05-31 00:28:32 +02:00
public function createInvitations ()
{
$this -> purchase_order = ( new CreateInvitations ( $this -> purchase_order )) -> run ();
return $this ;
}
public function applyNumber ()
{
2022-07-04 07:27:09 +02:00
$this -> purchase_order = ( new ApplyNumber ( $this -> purchase_order -> vendor , $this -> purchase_order )) -> run ();
2022-05-31 00:28:32 +02:00
return $this ;
}
2022-05-29 04:13:09 +02:00
public function fillDefaults ()
{
2022-07-12 00:22:03 +02:00
$settings = $this -> purchase_order -> company -> settings ;
2022-05-29 04:13:09 +02:00
2023-02-16 02:36:09 +01:00
if ( ! $this -> purchase_order -> design_id ) {
2022-07-27 12:03:46 +02:00
$this -> purchase_order -> design_id = $this -> decodePrimaryKey ( $settings -> purchase_order_design_id );
2023-02-16 02:36:09 +01:00
}
2024-01-14 05:05:00 +01:00
2023-02-16 02:36:09 +01:00
if ( ! isset ( $this -> purchase_order -> footer ) || empty ( $this -> purchase_order -> footer )) {
2022-07-12 00:22:03 +02:00
$this -> purchase_order -> footer = $settings -> purchase_order_footer ;
2023-02-16 02:36:09 +01:00
}
2022-05-29 04:13:09 +02:00
2023-02-16 02:36:09 +01:00
if ( ! isset ( $this -> purchase_order -> terms ) || empty ( $this -> purchase_order -> terms )) {
2022-07-12 00:22:03 +02:00
$this -> purchase_order -> terms = $settings -> purchase_order_terms ;
2023-02-16 02:36:09 +01:00
}
2022-05-29 04:13:09 +02:00
2023-02-16 02:36:09 +01:00
if ( ! isset ( $this -> purchase_order -> public_notes ) || empty ( $this -> purchase_order -> public_notes )) {
2022-07-12 00:22:03 +02:00
$this -> purchase_order -> public_notes = $this -> purchase_order -> vendor -> public_notes ;
2023-02-16 02:36:09 +01:00
}
2024-01-14 05:05:00 +01:00
2023-02-16 02:36:09 +01:00
if ( $settings -> counter_number_applied == 'when_saved' ) {
2022-07-12 00:22:03 +02:00
$this -> applyNumber () -> save ();
}
2022-05-29 04:13:09 +02:00
return $this ;
}
2022-06-05 05:58:29 +02:00
2022-06-10 09:04:16 +02:00
public function triggeredActions ( $request )
{
$this -> purchase_order = ( new TriggeredActions ( $this -> purchase_order -> load ( 'invitations' ), $request )) -> run ();
return $this ;
}
2022-06-06 14:27:17 +02:00
public function getPurchaseOrderPdf ( $contact = null )
{
return ( new GetPurchaseOrderPdf ( $this -> purchase_order , $contact )) -> run ();
}
2024-03-10 15:23:52 +01:00
public function getEPurchaseOrder ( $contact = null )
{
return ( new CreateEDocument ( $this -> purchase_order )) -> handle ();
}
2024-04-04 23:42:31 +02:00
public function getEDocument ( $contact = null )
{
return $this -> getEPurchaseOrder ( $contact );
}
2024-03-10 15:23:52 +01:00
public function deleteEPurchaseOrder ()
{
$this -> purchase_order -> load ( 'invitations' );
$this -> purchase_order -> invitations -> each ( function ( $invitation ) {
try {
// if (Storage::disk(config('filesystems.default'))->exists($this->invoice->client->e_invoice_filepath($invitation).$this->invoice->getFileName("xml"))) {
2024-03-19 15:33:58 +01:00
Storage :: disk ( config ( 'filesystems.default' )) -> delete ( $this -> purchase_order -> vendor -> e_document_filepath ( $invitation ) . $this -> purchase_order -> getFileName ( " xml " ));
2024-03-10 15:23:52 +01:00
// }
// if (Ninja::isHosted() && Storage::disk('public')->exists($this->invoice->client->e_invoice_filepath($invitation).$this->invoice->getFileName("xml"))) {
if ( Ninja :: isHosted ()) {
2024-03-20 12:54:18 +01:00
Storage :: disk ( 'public' ) -> delete ( $this -> purchase_order -> vendor -> e_document_filepath ( $invitation ) . $this -> purchase_order -> getFileName ( " xml " ));
2024-03-10 15:23:52 +01:00
}
} catch ( \Exception $e ) {
nlog ( $e -> getMessage ());
}
});
return $this ;
}
2022-06-05 05:58:29 +02:00
public function setStatus ( $status )
{
$this -> purchase_order -> status_id = $status ;
return $this ;
}
public function markSent ()
{
2022-06-05 11:22:58 +02:00
$this -> purchase_order = ( new MarkSent ( $this -> purchase_order -> vendor , $this -> purchase_order )) -> run ();
2022-06-05 05:58:29 +02:00
return $this ;
}
2022-12-19 04:57:44 +01:00
public function adjustBalance ( $adjustment )
{
$this -> purchase_order -> balance += $adjustment ;
return $this ;
}
2022-07-06 12:04:59 +02:00
public function add_to_inventory ()
{
2023-02-16 02:36:09 +01:00
if ( $this -> purchase_order -> status_id >= PurchaseOrder :: STATUS_RECEIVED ) {
2022-07-07 03:20:43 +02:00
return $this -> purchase_order ;
2023-02-16 02:36:09 +01:00
}
2022-07-07 03:20:43 +02:00
$this -> purchase_order = ( new PurchaseOrderInventory ( $this -> purchase_order )) -> run ();
2022-07-06 12:04:59 +02:00
return $this ;
}
2022-07-06 11:25:22 +02:00
public function expense ()
{
$this -> markSent ();
2024-01-14 05:05:00 +01:00
2023-02-16 02:36:09 +01:00
if ( $this -> purchase_order -> expense () -> exists ()) {
2022-07-07 14:09:39 +02:00
return $this ;
2023-02-16 02:36:09 +01:00
}
2022-07-07 14:09:39 +02:00
2022-07-06 11:25:22 +02:00
$expense = ( new PurchaseOrderExpense ( $this -> purchase_order )) -> run ();
return $expense ;
}
2023-03-18 09:06:32 +01:00
public function sendEmail ( $contact = null )
{
$send_email = new SendEmail ( $this -> purchase_order , null , $contact );
return $send_email -> run ();
}
2022-06-05 11:41:19 +02:00
/**
* Saves the purchase order .
2023-10-26 04:57:44 +02:00
* @ return \App\Models\PurchaseOrder
2022-06-05 11:41:19 +02:00
*/
public function save () : ? PurchaseOrder
{
$this -> purchase_order -> saveQuietly ();
return $this -> purchase_order ;
}
2022-05-29 04:13:09 +02:00
}