| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Services\Payments; |
| 4 |
|
| 5 |
use FluentCart\Api\CurrencySettings; |
| 6 |
use FluentCart\App\Helpers\ProductAdminHelper; |
| 7 |
use FluentCart\App\Models\ProductVariation; |
| 8 |
use FluentCart\App\Services\OrderService; |
| 9 |
use FluentCart\App\Vite; |
| 10 |
use FluentCart\Framework\Database\Orm\Collection; |
| 11 |
use FluentCart\Framework\Support\Arr; |
| 12 |
use FluentCart\Framework\Support\DateTime; |
| 13 |
|
| 14 |
class PaymentReceipt |
| 15 |
{ |
| 16 |
protected $order; |
| 17 |
|
| 18 |
public function __construct($order) |
| 19 |
{ |
| 20 |
$this->order = is_array($order) ? $order : $order->toArray(); |
| 21 |
} |
| 22 |
|
| 23 |
public function getItems() |
| 24 |
{ |
| 25 |
|
| 26 |
$this->getItemImage(); |
| 27 |
$orderItems = Arr::get($this->order, 'order_items', []); |
| 28 |
|
| 29 |
$hasAdjustmentItem = false; // Flag to track if an 'adjustment' item exists |
| 30 |
|
| 31 |
$subscriptionItem = current(array_filter($orderItems, function ($item) { |
| 32 |
return Arr::get($item, 'payment_type') === 'subscription'; |
| 33 |
})); |
| 34 |
|
| 35 |
foreach ($orderItems as $key => $item) { |
| 36 |
|
| 37 |
if (in_array(Arr::get($item, 'payment_type'), ['signup_fee', 'fee'])) { |
| 38 |
unset($orderItems[$key]); |
| 39 |
} |
| 40 |
|
| 41 |
// Check for 'adjustment' item and set the flag |
| 42 |
if (Arr::get($item, 'payment_type') == 'adjustment') { |
| 43 |
$hasAdjustmentItem = true; |
| 44 |
if ($subscriptionItem) { |
| 45 |
Arr::set($orderItems, $key . '.media_url', Arr::get($subscriptionItem, 'media_url')); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
// Check for 'subscription' item and skip it if 'adjustment' item exists |
| 50 |
if ($hasAdjustmentItem && Arr::get($item, 'payment_type') == 'subscription') { |
| 51 |
unset($orderItems[$key]); |
| 52 |
} |
| 53 |
|
| 54 |
// Check for 'subscription' item and add it if 'adjustment' item not exists |
| 55 |
if (!$hasAdjustmentItem && Arr::get($item, 'payment_type') == 'subscription') { |
| 56 |
$signupFee = 0; |
| 57 |
if (Arr::get($item, 'other_info.manage_setup_fee') == 'yes') { |
| 58 |
$signupFee = Arr::get($item, 'other_info.signup_fee'); |
| 59 |
$orderItems[$key]['signup_fee_name'] = Arr::get($item, 'other_info.signup_fee_name') . ':'; |
| 60 |
// if (Arr::get($item, 'other_info')->setup_fee_per_item == 'yes') { |
| 61 |
// $signupFee *= Arr::get($item, 'quantity'); |
| 62 |
// $orderItems[$key]['signup_fee_name'] .= ' x ' . Arr::get($item, 'quantity'); |
| 63 |
// } |
| 64 |
// $orderItems[$key]['other_info']->signup_fee = $signupFee; |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
// Check for 'adjustment' item and update the order items if 'adjustment' item exists |
| 70 |
if ($hasAdjustmentItem) { |
| 71 |
Arr::set($this->order, 'order_items', $orderItems); |
| 72 |
} |
| 73 |
|
| 74 |
return $orderItems; |
| 75 |
} |
| 76 |
|
| 77 |
public function date($format = 'd M Y') |
| 78 |
{ |
| 79 |
return DateTime::parse(Arr::get($this->order, 'created_at'))->format($format); |
| 80 |
} |
| 81 |
|
| 82 |
public function getEmail() |
| 83 |
{ |
| 84 |
$customerEmail = Arr::get($this->order, 'customer.email'); |
| 85 |
if ($customerEmail) { |
| 86 |
return $customerEmail; |
| 87 |
} |
| 88 |
return Arr::get($this->order, 'customer.billing_address.0.email', ''); |
| 89 |
} |
| 90 |
|
| 91 |
public function total() |
| 92 |
{ |
| 93 |
$totalAmount = Arr::get($this->order, 'total_amount', 0); |
| 94 |
|
| 95 |
// $totalAmount -= Arr::get($this->order,'discount_total', 0); |
| 96 |
return CurrencySettings::getPriceHtml( |
| 97 |
$totalAmount, |
| 98 |
Arr::get($this->order, 'currency') |
| 99 |
); |
| 100 |
} |
| 101 |
|
| 102 |
public function shippingCharge() |
| 103 |
{ |
| 104 |
$shippingTotal = Arr::get($this->order, 'shipping_total', 0); |
| 105 |
if (empty($shippingTotal)) { |
| 106 |
return 0; |
| 107 |
} |
| 108 |
|
| 109 |
return CurrencySettings::getPriceHtml( |
| 110 |
$shippingTotal, |
| 111 |
Arr::get($this->order, 'currency') |
| 112 |
); |
| 113 |
} |
| 114 |
|
| 115 |
public function subtotal() |
| 116 |
{ |
| 117 |
return CurrencySettings::getPriceHtml( |
| 118 |
Arr::get($this->order, 'subtotal'), |
| 119 |
Arr::get($this->order, 'currency'), |
| 120 |
); |
| 121 |
} |
| 122 |
|
| 123 |
public function formatPriceHtml($amount, $code = null) |
| 124 |
{ |
| 125 |
return CurrencySettings::getPriceHtml($amount, $code ?? Arr::get($this->order, 'currency')); |
| 126 |
} |
| 127 |
|
| 128 |
public function getItemImage() |
| 129 |
{ |
| 130 |
$orderItems = Arr::get($this->order, 'order_items', []); |
| 131 |
$variationIds = Collection::make($orderItems)->pluck('object_id')->toArray(); |
| 132 |
$productVariants = ProductVariation::query() |
| 133 |
->with(['product_detail', 'media']) |
| 134 |
->whereIn('id', $variationIds) |
| 135 |
->get(); |
| 136 |
|
| 137 |
$variantMap = Collection::make($productVariants); |
| 138 |
|
| 139 |
foreach ($orderItems as $key => &$item) { |
| 140 |
|
| 141 |
$variant = $variantMap->firstWhere('id', $item['object_id']); |
| 142 |
$mediaUrl = Vite::getAssetUrl('images/placeholder.svg'); |
| 143 |
if ($variant) { |
| 144 |
$mediaUrl = $variant->thumbnail ?: (new ProductAdminHelper())->getFeaturedMedia($variant->product_detail->featured_media); |
| 145 |
//Image Check Test |
| 146 |
//$mediaUrl = Arr::get($variant->media, 'meta_value.0.url', (new ProductAdminHelper())->getFeaturedMedia($variant->product_detail->featured_media)); |
| 147 |
} |
| 148 |
|
| 149 |
Arr::set( |
| 150 |
$this->order, 'order_items.' . $key . '.media_url', |
| 151 |
$mediaUrl |
| 152 |
); |
| 153 |
|
| 154 |
Arr::set( |
| 155 |
$this->order, |
| 156 |
'order_items.' . $key . '.currency', |
| 157 |
Arr::get($this->order, 'currency', '') |
| 158 |
); |
| 159 |
} |
| 160 |
|
| 161 |
return $orderItems; |
| 162 |
|
| 163 |
// $productThumbnails = ProductMetaResource::findByIds($variationIds)->toArray(); |
| 164 |
// $mediaMap = Collection::make($productThumbnails)->pluck('meta_value', 'object_id'); |
| 165 |
|
| 166 |
// foreach ($order_items as $key => &$item) { |
| 167 |
// if (isset($mediaMap[$item['object_id']])) { |
| 168 |
// $mediaUrl = Arr::get($mediaMap[$item['object_id']], '0.url', ''); |
| 169 |
// Arr::set( |
| 170 |
// $this->order,'order_items.'.$key.'.media_url', |
| 171 |
// $mediaUrl |
| 172 |
// ); |
| 173 |
// } else { |
| 174 |
// Arr::set( |
| 175 |
// $this->order, |
| 176 |
// 'order_items.' . $key . '.media_url', |
| 177 |
// Vite::getEnqueuePath('images/placeholder.svg') |
| 178 |
// ); |
| 179 |
// } |
| 180 |
// Arr::set( |
| 181 |
// $this->order, |
| 182 |
// 'order_items.' . $key . '.currency', |
| 183 |
// Arr::get($this->order, 'currency', '') |
| 184 |
// ); |
| 185 |
// } |
| 186 |
// return $order_items; |
| 187 |
} |
| 188 |
|
| 189 |
public function __call($name, $arguments) |
| 190 |
{ |
| 191 |
return Arr::get($this->order, $name); |
| 192 |
} |
| 193 |
|
| 194 |
public function totalDiscount($formatted = true) |
| 195 |
{ |
| 196 |
$totalDiscount = Arr::get($this->order, 'manual_discount_total') + Arr::get($this->order, 'coupon_discount_total'); |
| 197 |
|
| 198 |
if (!$formatted) { |
| 199 |
return $totalDiscount; |
| 200 |
} |
| 201 |
return CurrencySettings::getPriceHtml( |
| 202 |
$totalDiscount, |
| 203 |
Arr::get($this->order, 'currency'), |
| 204 |
); |
| 205 |
} |
| 206 |
|
| 207 |
public function discount($formatted = true) |
| 208 |
{ |
| 209 |
|
| 210 |
|
| 211 |
if (intval(Arr::get($this->order, 'discount_total') == 0)) { |
| 212 |
return 0.0; |
| 213 |
} |
| 214 |
|
| 215 |
return (CurrencySettings::getPriceHtml( |
| 216 |
Arr::get($this->order, 'manual_discount_total'), |
| 217 |
Arr::get($this->order, 'currency'), |
| 218 |
)); |
| 219 |
} |
| 220 |
|
| 221 |
public function proratedCredit() |
| 222 |
{ |
| 223 |
$proratedCredit = 0.0; |
| 224 |
$config = Arr::get($this->order, 'config', 0); |
| 225 |
if (empty($config)) { |
| 226 |
return 0.0; |
| 227 |
} |
| 228 |
|
| 229 |
if (is_string($config)) { |
| 230 |
$config = json_decode($config, true); |
| 231 |
} |
| 232 |
|
| 233 |
if (is_array($config)) { |
| 234 |
$proratedCredit = Arr::get($config, 'prorated_credit', 0.0); |
| 235 |
if (intval($proratedCredit) == 0) { |
| 236 |
return 0.0; |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
return intval($proratedCredit) > 0 ? CurrencySettings::getPriceHtml( |
| 241 |
$proratedCredit, |
| 242 |
Arr::get($this->order, 'currency'), |
| 243 |
) : 0.0; |
| 244 |
|
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
} |
| 249 |
|