Add support for customer credits, add credit manager #730

This commit is contained in:
Ramon Gutierrez 2023-03-27 04:40:14 +08:00
parent 6d416ee5f1
commit cb11849489
4 changed files with 171 additions and 0 deletions

View file

@ -0,0 +1,92 @@
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use DateTime;
/**
* @ORM\Entity
* @ORM\Table(name="credit_transaction")
*/
class CreditTransaction
{
// unique id
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
// customer
/**
* @ORM\OneToOne(targetEntity="Customer")
* @ORM\JoinColumn(name="customer_id", referencedColumnName="id")
*/
protected $customer;
// date transaction was recorded
/**
* @ORM\Column(type="datetime")
*/
protected $date_create;
/**
* @ORM\Column(type="integer")
* @Assert\NotBlank()
*/
protected $amount;
/**
* @ORM\Column(type="string", length=80)
* @Assert\NotBlank()
*/
protected $transaction_type;
public function __construct()
{
$this->date_create = new DateTime();
}
public function getID()
{
return $this->id;
}
public function setCustomer(Customer $customer)
{
$this->customer = $customer;
return $this;
}
public function setType($transaction_type)
{
$this->transaction_type = $transaction_type;
return $this;
}
public function getType()
{
return $this->transaction_type;
}
public function setAmount($amount)
{
$this->amount = $amount;
return $this;
}
public function getAmount()
{
return $this->amount;
}
public function getCustomer()
{
return $this->customer;
}
}

View file

@ -215,6 +215,11 @@ class Customer
*/
protected $create_source;
/**
* @ORM\Column(type="integer", options={"unsigned": true, "default":0})
*/
protected $credits;
// customer tags
/**
* @ORM\ManyToMany(targetEntity="CustomerTag", inversedBy="customers", indexBy="id")
@ -274,6 +279,8 @@ class Customer
$this->create_source = 'unknown';
$this->ratings = new ArrayCollection();
$this->credits = 0;
}
public function getID()
@ -700,4 +707,15 @@ class Customer
{
return $this->customer_user;
}
public function modifyCredits($amount)
{
$this->credits = bcadd($this->credits, $amount);
return $this;
}
public function getCredits()
{
return $this->credits;
}
}

View file

@ -0,0 +1,14 @@
<?php
namespace App\Ramcar;
class CreditTransactionType extends NameValue
{
const PURCHASE = 'purchase';
const REDEMPTION = 'redemption';
const COLLECTION = [
'purchase' => 'Purchase',
'redemption' => 'Redemption',
];
}

View file

@ -0,0 +1,47 @@
<?php
namespace App\Service;
use App\Entity\Customer;
use App\Entity\CreditTransaction;
use App\Ramcar\CreditTransactionType;
use Doctrine\ORM\EntityManagerInterface;
use Exception;
class CreditManager
{
protected $cust;
protected $em;
public function __construct(Customer $cust, EntityManagerInterface $em)
{
$this->cust = $cust;
$this->em = $em;
}
public function recordTransaction($type, $amount)
{
// make sure this is a valid transaction type
$this->validateType($type);
// update customer
$this->cust->modifyCredits($amount);
// create new record
$trans = new CreditTransaction();
$trans->setCustomer($this->cust)
->setAmount($amount)
->setType($type);
$this->em->persist($trans);
}
protected function validateType($type)
{
if (!CreditTransactionType::validate($type)) {
throw new Exception('Invalid transaction type');
}
}
}