Skip to content

Commit fecffa9

Browse files
committed
no message
1 parent a04f415 commit fecffa9

65 files changed

Lines changed: 1485 additions & 174 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

sprint1/API/app/Http/Controllers/Controller.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,18 @@ class Controller extends BaseController
8282
private function makeXML($xml, $status = 200, array $headers = [], $xmlRoot = 'response', $encoding = null)
8383
{
8484
if (is_array($xml)) {
85+
// Check if array has numeric keys (sequential array)
86+
if (array_keys($xml) === range(0, count($xml) - 1)) {
87+
$xml = ['item' => $xml];
88+
}
8589
$xml = ArrayToXml::convert($xml, $xmlRoot, true, $encoding);
8690
} elseif (is_object($xml) && method_exists($xml, 'toArray')) {
87-
$xml = ArrayToXml::convert($xml->toArray(), $xmlRoot, true, $encoding);
88-
} elseif (is_string($xml)) {
89-
$xml = $xml;
90-
} else {
91+
$arrayData = $xml->toArray();
92+
if (array_keys($arrayData) === range(0, count($arrayData) - 1)) {
93+
$arrayData = ['item' => $arrayData];
94+
}
95+
$xml = ArrayToXml::convert($arrayData, $xmlRoot, true, $encoding);
96+
} elseif (!is_string($xml)) {
9197
$xml = '';
9298
}
9399
if (!isset($headers['Content-Type'])) {

sprint2/API/app/Http/Controllers/Controller.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,17 @@ class Controller extends BaseController
8383
private function makeXML($xml, $status = 200, array $headers = [], $xmlRoot = 'response', $encoding = null)
8484
{
8585
if (is_array($xml)) {
86+
if (array_keys($xml) === range(0, count($xml) - 1)) {
87+
$xml = ['item' => $xml];
88+
}
8689
$xml = ArrayToXml::convert($xml, $xmlRoot, true, $encoding);
8790
} elseif (is_object($xml) && method_exists($xml, 'toArray')) {
88-
$xml = ArrayToXml::convert($xml->toArray(), $xmlRoot, true, $encoding);
89-
} elseif (is_string($xml)) {
90-
$xml = $xml;
91-
} else {
91+
$arrayData = $xml->toArray();
92+
if (array_keys($arrayData) === range(0, count($arrayData) - 1)) {
93+
$arrayData = ['item' => $arrayData];
94+
}
95+
$xml = ArrayToXml::convert($arrayData, $xmlRoot, true, $encoding);
96+
} elseif (!is_string($xml)) {
9297
$xml = '';
9398
}
9499
if (!isset($headers['Content-Type'])) {

sprint3/API/app/Http/Controllers/Controller.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,17 @@ class Controller extends BaseController
9090
private function makeXML($xml, $status = 200, array $headers = [], $xmlRoot = 'response', $encoding = null)
9191
{
9292
if (is_array($xml)) {
93+
if (array_keys($xml) === range(0, count($xml) - 1)) {
94+
$xml = ['item' => $xml];
95+
}
9396
$xml = ArrayToXml::convert($xml, $xmlRoot, true, $encoding);
9497
} elseif (is_object($xml) && method_exists($xml, 'toArray')) {
95-
$xml = ArrayToXml::convert($xml->toArray(), $xmlRoot, true, $encoding);
96-
} elseif (is_string($xml)) {
97-
$xml = $xml;
98-
} else {
98+
$arrayData = $xml->toArray();
99+
if (array_keys($arrayData) === range(0, count($arrayData) - 1)) {
100+
$arrayData = ['item' => $arrayData];
101+
}
102+
$xml = ArrayToXml::convert($arrayData, $xmlRoot, true, $encoding);
103+
} elseif (!is_string($xml)) {
99104
$xml = '';
100105
}
101106
if (!isset($headers['Content-Type'])) {

sprint4/API/app/Http/Controllers/Controller.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,17 @@ class Controller extends BaseController
8383
private function makeXML($xml, $status = 200, array $headers = [], $xmlRoot = 'response', $encoding = null)
8484
{
8585
if (is_array($xml)) {
86+
if (array_keys($xml) === range(0, count($xml) - 1)) {
87+
$xml = ['item' => $xml];
88+
}
8689
$xml = ArrayToXml::convert($xml, $xmlRoot, true, $encoding);
8790
} elseif (is_object($xml) && method_exists($xml, 'toArray')) {
88-
$xml = ArrayToXml::convert($xml->toArray(), $xmlRoot, true, $encoding);
89-
} elseif (is_string($xml)) {
90-
$xml = $xml;
91-
} else {
91+
$arrayData = $xml->toArray();
92+
if (array_keys($arrayData) === range(0, count($arrayData) - 1)) {
93+
$arrayData = ['item' => $arrayData];
94+
}
95+
$xml = ArrayToXml::convert($arrayData, $xmlRoot, true, $encoding);
96+
} elseif (!is_string($xml)) {
9297
$xml = '';
9398
}
9499
if (!isset($headers['Content-Type'])) {

sprint5-performance/API/app/Http/Controllers/Controller.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,16 @@ class Controller extends BaseController
9494
private function makeXML($xml, $status = 200, array $headers = [], $xmlRoot = 'response', $encoding = null)
9595
{
9696
if (is_array($xml)) {
97+
if (array_keys($xml) === range(0, count($xml) - 1)) {
98+
$xml = ['item' => $xml];
99+
}
97100
$xml = ArrayToXml::convert($xml, $xmlRoot, true, $encoding);
98101
} elseif (is_object($xml) && method_exists($xml, 'toArray')) {
99-
$xml = ArrayToXml::convert($xml->toArray(), $xmlRoot, true, $encoding);
102+
$arrayData = $xml->toArray();
103+
if (array_keys($arrayData) === range(0, count($arrayData) - 1)) {
104+
$arrayData = ['item' => $arrayData];
105+
}
106+
$xml = ArrayToXml::convert($arrayData, $xmlRoot, true, $encoding);
100107
} elseif (!is_string($xml)) {
101108
$xml = '';
102109
}

sprint5-performance/API/app/Models/Invoice.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,16 @@ class Invoice extends BaseModel
122122

123123
protected $filters = ['in', 'sort', 'starts_with'];
124124
protected $table = 'invoices';
125-
protected $fillable = ['user_id', 'invoice_date', 'invoice_number', 'additional_discount_percentage', 'additional_discount_amount', 'billing_street', 'billing_city', 'billing_state', 'billing_country', 'billing_postal_code', 'subtotal', 'total'];
125+
protected $fillable = ['user_id', 'invoice_date', 'invoice_number', 'additional_discount_percentage', 'additional_discount_amount', 'eco_discount_percentage', 'eco_discount_amount', 'billing_street', 'billing_city', 'billing_state', 'billing_country', 'billing_postal_code', 'subtotal', 'total'];
126126
protected $hidden = ['updated_at', 'document'];
127127

128128
protected $casts = array(
129129
"total" => "double",
130130
"subtotal" => "double",
131131
"additional_discount_amount" => "double",
132-
"additional_discount_percentage" => "double"
132+
"additional_discount_percentage" => "double",
133+
"eco_discount_amount" => "double",
134+
"eco_discount_percentage" => "double"
133135
);
134136

135137
public function user(): BelongsTo

sprint5-performance/API/app/Models/Product.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
* @OA\Property(property="product_image_id", type="string", example=1),
2424
* @OA\Property(property="is_location_offer", type="boolean", example=1),
2525
* @OA\Property(property="is_rental", type="boolean", example=0),
26+
* @OA\Property(property="co2_rating", type="string", enum={"A", "B", "C", "D", "E"}, example="B"),
2627
* }
2728
* )
2829
*
@@ -38,6 +39,8 @@
3839
* @OA\Property(property="is_location_offer", type="boolean", example=1),
3940
* @OA\Property(property="is_rental", type="boolean", example=0),
4041
* @OA\Property(property="in_stock", type="boolean", example=0),
42+
* @OA\Property(property="co2_rating", type="string", enum={"A", "B", "C", "D", "E"}, example="B"),
43+
* @OA\Property(property="is_eco_friendly", type="boolean", example=true),
4144
* @OA\Property(property="brand", ref="#/components/schemas/BrandResponse"),
4245
* @OA\Property(property="category", ref="#/components/schemas/CategoryResponse"),
4346
* @OA\Property(property="product_image", ref="#/components/schemas/ImageResponse")
@@ -49,9 +52,9 @@ class Product extends BaseModel
4952
use HasFactory, FilterQueryString, HasUlids;
5053

5154
protected $table = 'products';
52-
protected $fillable = ['name', 'description', 'category_id', 'brand_id', 'price', 'product_image_id', 'is_location_offer', 'is_rental', 'stock'];
55+
protected $fillable = ['name', 'description', 'category_id', 'brand_id', 'price', 'product_image_id', 'is_location_offer', 'is_rental', 'stock', 'co2_rating'];
5356
protected $hidden = ['brand_id', 'category_id', 'product_image_id', 'stock', 'created_at', 'updated_at'];
54-
protected $appends = ['in_stock'];
57+
protected $appends = ['in_stock', 'is_eco_friendly'];
5558
protected $filters = ['between', 'sort'];
5659

5760
protected $casts = array(
@@ -87,4 +90,24 @@ public function getInStockAttribute()
8790
return $this->stock > 0;
8891
}
8992

93+
public function getIsEcoFriendlyAttribute()
94+
{
95+
return in_array(strtoupper($this->co2_rating ?? ''), ['A', 'B']);
96+
}
97+
98+
public function scopeEcoFriendly($query)
99+
{
100+
return $query->whereIn('co2_rating', ['A', 'B', 'a', 'b']);
101+
}
102+
103+
public function scopeWithFilters($query, array $filters)
104+
{
105+
return $query->when($filters['eco_friendly'] ?? null, function ($q, $ecoFriendly) {
106+
if ($ecoFriendly == '1' || $ecoFriendly === true || $ecoFriendly === 'true') {
107+
return $q->ecoFriendly();
108+
}
109+
return $q;
110+
});
111+
}
112+
90113
}

sprint5-performance/API/app/Services/InvoiceService.php

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ public function createInvoice(array $data, $cartId)
6161
$cart = Cart::with('cartItems', 'cartItems.product')->findOrFail($cartId);
6262

6363
$subTotalPrice = 0;
64+
$ecoFriendlyCount = 0;
65+
$totalProductCount = 0;
6466

6567
foreach ($cart->cartItems as $cartItem) {
6668
$quantity = $cartItem['quantity'];
@@ -82,16 +84,33 @@ public function createInvoice(array $data, $cartId)
8284
]);
8385

8486
$subTotalPrice += $discountedPrice ? $quantity * $discountedPrice : $quantity * $unitPrice;
87+
88+
// Count eco-friendly products (CO2 rating A or B)
89+
$totalProductCount += $quantity;
90+
if (isset($cartItem['product']->co2_rating) && in_array(strtoupper($cartItem['product']->co2_rating), ['A', 'B'])) {
91+
$ecoFriendlyCount += $quantity;
92+
}
8593
}
8694

8795
$discountAmount = $subTotalPrice * ($cart->additional_discount_percentage / 100);
88-
$totalPrice = $subTotalPrice - $discountAmount;
96+
97+
// Calculate eco-friendly discount (5% if more than 50% of products are eco-friendly)
98+
$ecoDiscountPercentage = 0;
99+
$ecoDiscountAmount = 0;
100+
if ($totalProductCount > 0 && ($ecoFriendlyCount / $totalProductCount) > 0.5) {
101+
$ecoDiscountPercentage = 5;
102+
$ecoDiscountAmount = ($subTotalPrice - $discountAmount) * ($ecoDiscountPercentage / 100);
103+
}
104+
105+
$totalPrice = $subTotalPrice - $discountAmount - $ecoDiscountAmount;
89106

90107
$invoice->update([
91108
'subtotal' => $subTotalPrice,
92109
'total' => $totalPrice,
93110
'additional_discount_percentage' => $cart->additional_discount_percentage,
94111
'additional_discount_amount' => $discountAmount,
112+
'eco_discount_percentage' => $ecoDiscountPercentage,
113+
'eco_discount_amount' => $ecoDiscountAmount,
95114
]);
96115

97116
Log::info('Invoice finalized', ['invoice_id' => $invoice->id, 'total' => $totalPrice]);
@@ -153,7 +172,14 @@ public function getInvoice($id, $isAdmin)
153172
{
154173
Log::info('Fetching invoice', ['invoice_id' => $id, 'is_admin' => $isAdmin]);
155174

156-
$query = Invoice::with('invoicelines', 'invoicelines.product', 'payment', 'payment.payment_details');
175+
$query = Invoice::with([
176+
'invoicelines',
177+
'invoicelines.product' => function ($query) {
178+
$query->select('id', 'name', 'description', 'price', 'product_image_id', 'category_id', 'brand_id', 'co2_rating', 'is_rental');
179+
},
180+
'payment',
181+
'payment.payment_details'
182+
]);
157183

158184
if (!$isAdmin) {
159185
$query->where('user_id', Auth::user()->id);

sprint5-performance/API/database/migrations/2022_01_03_102447_create_products_table.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public function up()
2323
$table->decimal('price', 10, 2);
2424
$table->boolean('is_location_offer');
2525
$table->boolean('is_rental');
26+
$table->enum('co2_rating', ['A', 'B', 'C', 'D', 'E'])->nullable();
2627
$table->timestamp('created_at')->useCurrent();
2728
$table->timestamp('updated_at')->useCurrent();
2829
$table->foreignUlid('brand_id')->references('id')->on('brands');
@@ -36,6 +37,7 @@ public function up()
3637
$table->index('is_rental');
3738
$table->index('name');
3839
$table->index('price');
40+
$table->index('co2_rating');
3941
});
4042
}
4143

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*/
12+
public function up(): void
13+
{
14+
Schema::table('invoices', function (Blueprint $table) {
15+
$table->decimal('eco_discount_percentage', 5, 2)->nullable()->after('additional_discount_amount');
16+
$table->decimal('eco_discount_amount', 10, 2)->nullable()->after('eco_discount_percentage');
17+
});
18+
}
19+
20+
/**
21+
* Reverse the migrations.
22+
*/
23+
public function down(): void
24+
{
25+
Schema::table('invoices', function (Blueprint $table) {
26+
$table->dropColumn(['eco_discount_percentage', 'eco_discount_amount']);
27+
});
28+
}
29+
};

0 commit comments

Comments
 (0)