Skip to content

Commit

Permalink
Added SaleClose
Browse files Browse the repository at this point in the history
  • Loading branch information
hiqsol committed Apr 2, 2020
1 parent ebb6538 commit 69cfe7f
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 5 deletions.
2 changes: 1 addition & 1 deletion config/common.php
Expand Up @@ -112,7 +112,7 @@

'SalesSearch' => \hiqdev\billing\hiapi\sale\Search\SaleBulkSearch::class,
'SaleCreate' => \hiqdev\billing\hiapi\sale\Create\SaleCreate::class,
'SaleClose' => \hiqdev\billing\hiapi\sale\Create\SaleClose::class,
'SaleClose' => \hiqdev\billing\hiapi\sale\Close\SaleClose::class,

'MethodsSearch' => \hiqdev\billing\hiapi\method\Search\MethodBulkSearch::class,
'MethodCreate' => \hiqdev\billing\hiapi\method\Create\MethodCreate::class,
Expand Down
36 changes: 36 additions & 0 deletions src/sale/Close/SaleBulkClose.php
@@ -0,0 +1,36 @@
<?php

namespace hiqdev\billing\hiapi\sale\Close;

use hiapi\endpoints\Module\Multitenant\Tenant;
use hiapi\Core\Endpoint\BuilderFactory;
use hiapi\Core\Endpoint\Endpoint;
use hiapi\Core\Endpoint\EndpointBuilder;
use hiqdev\php\billing\sale\Sale;
use hiqdev\billing\hiapi\customer\CustomerLoader;
use hiqdev\billing\hiapi\plan\PlanLoader;
use hiqdev\billing\hiapi\target\TargetLoader;

final class SaleBulkClose
{
public function __invoke(BuilderFactory $build): Endpoint
{
return $this->create($build)->build();
}

public function create(BuilderFactory $build): EndpointBuilder
{
return $build->endpoint(self::class)
->exportTo(Tenant::ALL)
->take($build->many(SaleCloseCommand::class))
// XXX anybody can purchase? ->checkPermission('object.buy')
->middlewares(
$build->repeat(CustomerLoader::class),
$build->repeat(PlanLoader::class),
$build->repeat(TargetLoader::class),
$build->repeat(SaleCloseAction::class)
)
->return($build->many(Sale::class))
;
}
}
36 changes: 36 additions & 0 deletions src/sale/Close/SaleClose.php
@@ -0,0 +1,36 @@
<?php

namespace hiqdev\billing\hiapi\sale\Close;

use hiapi\endpoints\Module\Multitenant\Tenant;
use hiapi\Core\Endpoint\BuilderFactory;
use hiapi\Core\Endpoint\Endpoint;
use hiapi\Core\Endpoint\EndpointBuilder;
use hiqdev\php\billing\sale\Sale;
use hiqdev\billing\hiapi\customer\CustomerLoader;
use hiqdev\billing\hiapi\plan\PlanLoader;
use hiqdev\billing\hiapi\target\TargetLoader;

final class SaleClose
{
public function __invoke(BuilderFactory $build): Endpoint
{
return $this->create($build)->build();
}

public function create(BuilderFactory $build): EndpointBuilder
{
return $build->endpoint(self::class)
->exportTo(Tenant::ALL)
->take(SaleCloseCommand::class)
// XXX anybody can purchase? ->checkPermission('object.buy')
->middlewares(
CustomerLoader::class,
PlanLoader::class,
TargetLoader::class,
$build->call(SaleCloseAction::class)
)
->return(Sale::class)
;
}
}
41 changes: 41 additions & 0 deletions src/sale/Close/SaleCloseAction.php
@@ -0,0 +1,41 @@
<?php

namespace hiqdev\billing\hiapi\sale\Close;

use hiqdev\billing\hiapi\sale\SaleRepository;
use hiqdev\php\billing\plan\Plan;
use hiqdev\php\billing\sale\Sale;
use hiapi\exceptions\domain\RequiredInputException;

class SaleCloseAction
{
/**
* @var SaleRepository
*/
private $repo;

public function __construct(SaleRepository $repo)
{
$this->repo = $repo;
}

public function __invoke(SaleCloseCommand $command): Sale
{
$this->checkRequiredInput($command);
$plan = new Plan($command->plan_id, null);
$sale = new Sale(null, $command->target, $command->customer, $plan, $command->time);
$this->repo->delete($sale);

return $sale;
}

protected function checkRequiredInput(SaleCloseCommand $command)
{
if (empty($command->customer)) {
throw new RequiredInputException('customer');
}
if (empty($command->target)) {
throw new RequiredInputException('target');
}
}
}
37 changes: 37 additions & 0 deletions src/sale/Close/SaleCloseCommand.php
@@ -0,0 +1,37 @@
<?php

namespace hiqdev\billing\hiapi\sale\Close;

use hiapi\commands\BaseCommand;
use hiapi\validators\IdValidator;

class SaleCloseCommand extends BaseCommand
{
public $customer_id;

public $plan_id;

public $target_id;

public $time;

public $customer;

public $plan;

public $target;

public function rules()
{
return [
[['customer_id'], IdValidator::class],

[['plan_id'], IdValidator::class],

[['target_id'], IdValidator::class],
[['target_id'], 'required'],

[['time'], 'datetime'],
];
}
}
20 changes: 16 additions & 4 deletions src/sale/SaleRepository.php
Expand Up @@ -131,16 +131,28 @@ protected function joinPlans(&$rows)
* @param SaleInterface $sale
*/
public function save(SaleInterface $sale)
{
$call = new CallExpression('sale_object', [$this->prepareHstore($sale)]);
$command = (new Query())->select($call);
$sale->setId($command->scalar($this->db));
}

public function delete(SaleInterface $sale)
{
$call = new CallExpression('unsale_object', [$this->prepareHstore($sale)]);
$command = (new Query())->select($call);
$command->scalar($this->db);
}

private function prepareHstore(SaleInterface $sale)
{
$time = $sale->getTime();
$hstore = new HstoreExpression([

return new HstoreExpression([
'object_id' => $sale->getTarget()->getId(),
'contact_id' => $sale->getCustomer()->getId(),
'tariff_id' => $sale->getPlan() ? $sale->getPlan()->getId() : null,
'time' => $time ? $time->format('c') : null,
]);
$call = new CallExpression('sale_object', [$hstore]);
$command = (new Query())->select($call);
$sale->setId($command->scalar($this->db));
}
}

0 comments on commit 69cfe7f

Please sign in to comment.