Skip to content

Commit

Permalink
SaleAccessory observer
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelnakamura committed Jun 1, 2022
1 parent ea43992 commit 9b60fe3
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/IssSupernova.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Bildvitta\IssSupernova\Contracts\IssSupernovaFactory;
use Bildvitta\IssSupernova\Resources\RealEstateDevelopmentTypologies;
use Bildvitta\IssSupernova\Resources\RealEstateDevelopmentUnits;
use Bildvitta\IssSupernova\Resources\SaleAccessories;
use Bildvitta\IssSupernova\Resources\SalePeriodicities;
use Bildvitta\IssSupernova\Resources\Sales;
use Bildvitta\IssSupernova\Resources\Users;
Expand Down Expand Up @@ -226,4 +227,9 @@ public function salePeriodicities()
{
return new SalePeriodicities($this);
}

public function saleAccessories()
{
return new SaleAccessories($this);
}
}
76 changes: 76 additions & 0 deletions src/Observers/SaleAccessoryObserver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace Bildvitta\IssSupernova\Observers;

use Bildvitta\IssSupernova\IssSupernova;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Log;

class SaleAccessoryObserver
{
public function created($saleAccessory)
{
if (!Config::get('iss-supernova.base_uri')) {
return;
}

$saleAccessory->loadMissing(
'category',
'accessory',
'sale'
);
if ($saleAccessory->sale) {
$saleAccessory->sale->loadMissing(
'blueprint',
'unit'
);
}
$data = $saleAccessory->toArray();
$data['sync_to'] = 'sys';

try {
$issSupernova = new IssSupernova();
$response = $issSupernova->saleAccessories()->create($data);
return $response;
} catch (\Throwable $exception) {
Log::error($exception->getMessage());
throw $exception;
}
}

public function updated($saleAccessory)
{
if (!Config::get('iss-supernova.base_uri')) {
return;
}

$saleAccessory->loadMissing(
'category',
'accessory',
'sale'
);
if ($saleAccessory->sale) {
$saleAccessory->sale->loadMissing(
'blueprint',
'unit'
);
}
$data = $saleAccessory->toArray();
$data['sync_to'] = 'sys';

try {
$issSupernova = new IssSupernova();
$response = $issSupernova->saleAccessories()->update($data);
return $response;
} catch (\Throwable $exception) {
Log::error($exception->getMessage());
throw $exception;
}
}

public function deleted($saleAccessory)
{
//
}
}
31 changes: 31 additions & 0 deletions src/Resources/SaleAccessories.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Bildvitta\IssSupernova\Resources;

use Bildvitta\IssSupernova\IssSupernova;

class SaleAccessories
{
private IssSupernova $issSupernova;

public function __construct(IssSupernova $issSupernova)
{
$this->issSupernova = $issSupernova;
}

public function create($data)
{
return $this->issSupernova->request->post(
'/sales/accessories',
$data
)->throw()->object();
}

public function update($data)
{
return $this->issSupernova->request->put(
'/sales/accessories',
$data
)->throw()->object();
}
}

0 comments on commit 9b60fe3

Please sign in to comment.