public function create(StoreOrder $request): JsonResource 	{ 		$totalWeight = $base_price = $base_tax = $total_return_fee = 0; 		$discount = Discount::where('code', $request->discount_code)->first(); 		$delivery_price = DeliveryPrice::where('country_code', $request->country_code)->first(); 		$discount_valid = $discount && $discount->isUsable();  		$order = Order::create([ 			'payment_type' => $request->payment_type, 			'email' => $request->email, 			'language' => $request->language, 			'r1' => $request->r1, 			'oib' => $request->r1 ? $request->oib : null, 			'company_name' => $request->r1 ? $request->company_name : null, 			'company_address' => $request->r1 ? $request->company_address : null, 			'company_zip_code' => $request->r1 ? $request->company_zip_code : null, 			'company_city' => $request->r1 ? $request->company_city : null, 			'company_country' => $request->r1 ? $request->company_country : null, 			'delivery_name' => $request->delivery_name, 			'delivery_surname' => $request->delivery_surname, 			'delivery_phone' => $request->delivery_phone, 			'delivery_address' => $request->delivery_address, 			'delivery_city' => $request->delivery_city, 			'delivery_zip_code' => $request->delivery_zip_code, 			'delivery_company_name' => $request->delivery_company_name, 			'delivery_description' => $request->delivery_description, 			'billable_name' => $request->billable_name, 			'billable_surname' => $request->billable_surname, 			'billable_phone' => $request->billable_phone, 			'billable_address' => $request->billable_address, 			'billable_city' => $request->billable_city, 			'billable_zip_code' => $request->billable_zip_code, 			'order_number' => $request->order_number, 			'order_number_timestamp' => time(), 			'discount_id' => $discount_valid ? $discount->id : null, 			'delivery_price_id' => $delivery_price->id, 			'newsletter' => $request->newsletter, 		]);  		foreach ($request->items as $item) { 			$item_object = Product::where('id', $item['id'])->first();  			$order->orderItems()->create([ 				'item_id' => $item_object->id, 				'base_price' => $item_object->price, 				'price' => $item_object->price_with_tax, 				'return_fee' => $item_object->return_fee, 				'total_return_fee' => $item_object->return_fee * $item['quantity'], 				'tax' => $item_object->tax_amount, 				'total_tax' => $item_object->tax_amount * $item['quantity'], 				'total_price_without_tax' => $item_object->price * $item['quantity'], 				'total_price_with_tax' => $item_object->price_with_tax * $item['quantity'], 				'quantity' => $item['quantity'], 				'label' => $item_object->name ? $item_object->getTranslation('name', $request->language, $item_object->name) : '', 			]);  			$total_return_fee += round($item_object->return_fee * $item['quantity'], 2); 			$base_price += round($item_object->price * $item['quantity'], 2); 			$base_tax += round($item_object->tax_amount * $item['quantity'], 2);  			$totalWeight += round($item_object->weight, 2) * $item['quantity']; 		}  		// 1. calculate discount then check if shipping is free etc. 		$priceWithoutTax = $discount_valid ? round($discount->calculate($base_price), 2) : $base_price; 		$tax = $discount_valid ? round($discount->calculate($base_tax), 2) : $base_tax; 		$totalPriceWithoutShipping = $priceWithoutTax + $tax + $total_return_fee; 		// 2. shipping price 		$shippingPrice = $delivery_price->calculateShippingPrice($totalWeight, $totalPriceWithoutShipping, $request->payment_type, $request->country_code);  		// 3. all prices added up 		$finalPrice = round($totalPriceWithoutShipping + $shippingPrice, 2); 		$discount_amount = round($base_price - $priceWithoutTax + $base_tax - $tax, 2);  		$order->update([ 			'base_price' => $base_price, 			'discounted_price_without_tax' => $priceWithoutTax, 			'tax_without_discount' => $base_tax, 			'tax_amount_after_discount' => $tax, 			'discount_amount' => $discount_amount, 			'total_return_fee' => $total_return_fee, 			'shipping_price' => $shippingPrice, 			'total_price' => $finalPrice, 		]);  		if ($order->checkAlowedWeight($totalWeight, $delivery_price->delivery_type)) { 			abort(403, __('orders.max_weight_passed', ['weight' => setting('max_weight')])); 		}  		if ($order->payment_type->value == PaymentType::CashOnDelivery->value) { 			$soap = new SoapHandler;  			$order->generatePDF(); 			$order->sendInvoiceEmail();  			$soap_response = $soap->sendCreatedOrder($order);  			$order->update([ 				'soap_code' => $soap_response ? $soap_response[0]->kod : null, 				'soap_description' => $soap_response ? $soap_response[0]->opis : null, 			]); 		}  		return new OrderSingleResource($order); 	}