From cb11849489954aed73b75daee1eeb2f5ac83ba39 Mon Sep 17 00:00:00 2001 From: Ramon Gutierrez Date: Mon, 27 Mar 2023 04:40:14 +0800 Subject: [PATCH] Add support for customer credits, add credit manager #730 --- src/Entity/CreditTransaction.php | 92 ++++++++++++++++++++++++++++ src/Entity/Customer.php | 18 ++++++ src/Ramcar/CreditTransactionType.php | 14 +++++ src/Service/CreditManager.php | 47 ++++++++++++++ 4 files changed, 171 insertions(+) create mode 100644 src/Entity/CreditTransaction.php create mode 100644 src/Ramcar/CreditTransactionType.php create mode 100644 src/Service/CreditManager.php diff --git a/src/Entity/CreditTransaction.php b/src/Entity/CreditTransaction.php new file mode 100644 index 00000000..599a4851 --- /dev/null +++ b/src/Entity/CreditTransaction.php @@ -0,0 +1,92 @@ +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; + } +} diff --git a/src/Entity/Customer.php b/src/Entity/Customer.php index 9a41f462..271ef95e 100644 --- a/src/Entity/Customer.php +++ b/src/Entity/Customer.php @@ -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; + } } diff --git a/src/Ramcar/CreditTransactionType.php b/src/Ramcar/CreditTransactionType.php new file mode 100644 index 00000000..26a5dd38 --- /dev/null +++ b/src/Ramcar/CreditTransactionType.php @@ -0,0 +1,14 @@ + 'Purchase', + 'redemption' => 'Redemption', + ]; +} diff --git a/src/Service/CreditManager.php b/src/Service/CreditManager.php new file mode 100644 index 00000000..275261bb --- /dev/null +++ b/src/Service/CreditManager.php @@ -0,0 +1,47 @@ +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'); + } + } +}