|
@@ -9,6 +9,7 @@ use Illuminate\Database\Eloquent\Model;
|
|
|
use Longyi\Gift\Contracts\GiftCards as GiftCardsContract;
|
|
use Longyi\Gift\Contracts\GiftCards as GiftCardsContract;
|
|
|
use Longyi\RewardPoints\Config\TransactionType;
|
|
use Longyi\RewardPoints\Config\TransactionType;
|
|
|
use Longyi\RewardPoints\Repositories\RewardPointRepository;
|
|
use Longyi\RewardPoints\Repositories\RewardPointRepository;
|
|
|
|
|
+use function Symfony\Component\Translation\t;
|
|
|
|
|
|
|
|
class GiftCards extends Model implements GiftCardsContract
|
|
class GiftCards extends Model implements GiftCardsContract
|
|
|
{
|
|
{
|
|
@@ -212,28 +213,52 @@ class GiftCards extends Model implements GiftCardsContract
|
|
|
];
|
|
];
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Add a gift card for a customer using points
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param int $id Gift card ID from giftcards() array
|
|
|
|
|
+ * @param int $customerId Customer ID
|
|
|
|
|
+ * @return array|true
|
|
|
|
|
+ */
|
|
|
public function add($id, $customerId)
|
|
public function add($id, $customerId)
|
|
|
{
|
|
{
|
|
|
|
|
+ if (empty($customerId)) {
|
|
|
|
|
+ throw new \Exception('customer id is empty');
|
|
|
|
|
+ }
|
|
|
|
|
+ if (empty($id)) {
|
|
|
|
|
+ throw new \Exception('gift card id is empty');
|
|
|
|
|
+ }
|
|
|
$gifts = $this->giftcards();
|
|
$gifts = $this->giftcards();
|
|
|
$giftInfo = $gifts[$id] ?? null;
|
|
$giftInfo = $gifts[$id] ?? null;
|
|
|
if (!$giftInfo) {
|
|
if (!$giftInfo) {
|
|
|
- return false;
|
|
|
|
|
|
|
+ throw new \Exception("gift card not found (ID: {$id})");
|
|
|
|
|
+ }
|
|
|
|
|
+ $points = $giftInfo['points'] ?? 0;
|
|
|
|
|
+ if ($points <= 0) {
|
|
|
|
|
+ throw new \Exception("gift card points configuration error (points: {$points})");
|
|
|
}
|
|
}
|
|
|
try {
|
|
try {
|
|
|
DB::beginTransaction();
|
|
DB::beginTransaction();
|
|
|
- self::addGiftCard($customerId, $giftInfo['amount'], $giftInfo['num'], $giftInfo['expirationdate'], $giftInfo['channel'], $giftInfo['notes']);
|
|
|
|
|
- $rewardPointRepository = app(RewardPointRepository::class);//积分兑换
|
|
|
|
|
|
|
+ self::addGiftCard(
|
|
|
|
|
+ $customerId,
|
|
|
|
|
+ $giftInfo['amount'],
|
|
|
|
|
+ $giftInfo['num'],
|
|
|
|
|
+ $giftInfo['expirationdate'],
|
|
|
|
|
+ $giftInfo['channel'],
|
|
|
|
|
+ $giftInfo['notes']
|
|
|
|
|
+ );
|
|
|
|
|
+ $rewardPointRepository = app(RewardPointRepository::class);
|
|
|
$rewardPointRepository->deductPoints(
|
|
$rewardPointRepository->deductPoints(
|
|
|
$customerId,
|
|
$customerId,
|
|
|
|
|
+ $points,
|
|
|
TransactionType::GIFT_CARD_REDEEM,
|
|
TransactionType::GIFT_CARD_REDEEM,
|
|
|
- $giftInfo['points'],
|
|
|
|
|
'积分兑换礼品卡'
|
|
'积分兑换礼品卡'
|
|
|
);
|
|
);
|
|
|
DB::commit();
|
|
DB::commit();
|
|
|
|
|
+ return true;
|
|
|
} catch (\Exception $e) {
|
|
} catch (\Exception $e) {
|
|
|
DB::rollBack();
|
|
DB::rollBack();
|
|
|
- return $e->getMessage();
|
|
|
|
|
|
|
+ throw $e;
|
|
|
}
|
|
}
|
|
|
- return true;
|
|
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|