|
@@ -105,18 +105,49 @@ class GiftCards extends Model implements GiftCardsContract
|
|
|
throw new \Exception('gift num is not enough');
|
|
throw new \Exception('gift num is not enough');
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+ $expirationDate = self::calculateExpirationDate($expirationdate);
|
|
|
static::create([
|
|
static::create([
|
|
|
'giftcard_number' => self::generateGiftCardNumber(),
|
|
'giftcard_number' => self::generateGiftCardNumber(),
|
|
|
'giftcard_amount' => $giftcardAmount,
|
|
'giftcard_amount' => $giftcardAmount,
|
|
|
'used_giftcard_amount' => 0,
|
|
'used_giftcard_amount' => 0,
|
|
|
'remaining_giftcard_amount' => $giftcardAmount,
|
|
'remaining_giftcard_amount' => $giftcardAmount,
|
|
|
'customer_id' => $customerId,
|
|
'customer_id' => $customerId,
|
|
|
- 'expirationdate' => now()->addDays($expirationdate),
|
|
|
|
|
|
|
+ 'expirationdate' => $expirationDate,
|
|
|
'giftcard_status' => 1,
|
|
'giftcard_status' => 1,
|
|
|
'channel' => $channel,
|
|
'channel' => $channel,
|
|
|
'notes' => $notes
|
|
'notes' => $notes
|
|
|
]);
|
|
]);
|
|
|
}
|
|
}
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Calculate expiration date from various input types.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param mixed $expirationdate Days (int), date string, or Carbon instance
|
|
|
|
|
+ * @return \Carbon\Carbon Calculated expiration date
|
|
|
|
|
+ * @throws \Exception If the input type is invalid
|
|
|
|
|
+ */
|
|
|
|
|
+ protected static function calculateExpirationDate($expirationdate): Carbon
|
|
|
|
|
+ {
|
|
|
|
|
+ if ($expirationdate instanceof Carbon) {
|
|
|
|
|
+ return $expirationdate;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (is_string($expirationdate)) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ return Carbon::parse($expirationdate);
|
|
|
|
|
+ } catch (\Exception $e) {
|
|
|
|
|
+ throw new \Exception("Invalid date format: {$expirationdate}. Use Y-m-d format.");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (is_int($expirationdate) || is_float($expirationdate)) {
|
|
|
|
|
+ if ($expirationdate <= 0) {
|
|
|
|
|
+ throw new \Exception('Expiration days must be greater than 0');
|
|
|
|
|
+ }
|
|
|
|
|
+ return now()->addDays((int) $expirationdate);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ throw new \Exception('Invalid expiration date type. Must be integer (days), string (Y-m-d), or Carbon instance.');
|
|
|
|
|
+ }
|
|
|
public static function generateGiftCardNumber()
|
|
public static function generateGiftCardNumber()
|
|
|
{
|
|
{
|
|
|
do {
|
|
do {
|
|
@@ -132,4 +163,68 @@ class GiftCards extends Model implements GiftCardsContract
|
|
|
|
|
|
|
|
return $code;
|
|
return $code;
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ public function giftcards()
|
|
|
|
|
+ {
|
|
|
|
|
+ return [
|
|
|
|
|
+ 1 => [
|
|
|
|
|
+ 'id' => 1,
|
|
|
|
|
+ 'name' => 'Gift Card 1',
|
|
|
|
|
+ 'amount' => 20.00,
|
|
|
|
|
+ 'points' => 20,
|
|
|
|
|
+ 'num' => 0,
|
|
|
|
|
+ 'expirationdate' => 30,
|
|
|
|
|
+ 'channel' => 'activity_26_04_21',
|
|
|
|
|
+ 'notes' => '拉新活动添加'
|
|
|
|
|
+ ],
|
|
|
|
|
+ 2 => [
|
|
|
|
|
+ 'id' => 2,
|
|
|
|
|
+ 'name' => 'Gift Card 2',
|
|
|
|
|
+ 'amount' => 30.00,
|
|
|
|
|
+ 'points' => 30,
|
|
|
|
|
+ 'num' => 0,
|
|
|
|
|
+ 'expirationdate' => 30,
|
|
|
|
|
+ 'channel' => 'activity_26_04_21',
|
|
|
|
|
+ 'notes' => '拉新活动添加'
|
|
|
|
|
+ ],
|
|
|
|
|
+ 3 => [
|
|
|
|
|
+ 'id' => 3,
|
|
|
|
|
+ 'name' => 'Gift Card 3',
|
|
|
|
|
+ 'amount' => 40.00,
|
|
|
|
|
+ 'points' => 40,
|
|
|
|
|
+ 'num' => 0,
|
|
|
|
|
+ 'expirationdate' => 30,
|
|
|
|
|
+ 'channel' => 'activity_26_04_21',
|
|
|
|
|
+ 'notes' => '拉新活动添加'
|
|
|
|
|
+ ],
|
|
|
|
|
+ 4 => [
|
|
|
|
|
+ 'id' => 4,
|
|
|
|
|
+ 'name' => 'Gift Card 4',
|
|
|
|
|
+ 'amount' => 50.00,
|
|
|
|
|
+ 'points' => 50,
|
|
|
|
|
+ 'num' => 0,
|
|
|
|
|
+ 'expirationdate' => 30,
|
|
|
|
|
+ 'channel' => 'activity_26_04_21',
|
|
|
|
|
+ 'notes' => '拉新活动添加'
|
|
|
|
|
+ ],
|
|
|
|
|
+ ];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public function add($id)
|
|
|
|
|
+ {
|
|
|
|
|
+ $gifts = $this->giftcards();
|
|
|
|
|
+ $giftInfo = $gifts[$id] ?? null;
|
|
|
|
|
+ if (!$giftInfo) {
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ try {
|
|
|
|
|
+ DB::beginTransaction();
|
|
|
|
|
+ self::addGiftCard($id, $giftInfo['amount'], $giftInfo['num'], $giftInfo['expirationdate'], $giftInfo['channel'], $giftInfo['notes']);
|
|
|
|
|
+ DB::commit();
|
|
|
|
|
+ } catch (\Exception $e) {
|
|
|
|
|
+ DB::rollBack();
|
|
|
|
|
+ throw $e;
|
|
|
|
|
+ }
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|