Zend certified PHP/Magento developer

Magento 2 : How to add discount(cart price rule) to programmatically created order?

I’m creating an order programmatically. I have added cart price rule in backend for subtotal above 1000 will get 30% product price discount on product

  1. When generating order(programmatically) as a customer, Discount is not added in order totals in the backend as shown in the screenshot:-
    https://nimb.ws/YD5Exj
  1. When generating an order on the frontend with the same customer, the discount is added to the cart can also see in the order totals in
    the backend as shown in the screenshot:- https://nimb.ws/uwVET1

How can I add discounts while generating order programmatically?

OrderCreate.php

    /**
     *
     * @return void
     */
    public function execute() 
    {
        $firstName = "test1";
        $lastName = "test12345";
        $regionId = "285";
        $region = "Brasov";
        $postcode = "50001";
        $street = "Test Street";
        $city = "Acris";
        $countryId = "RO";
        $telephone = "0555555555";
        $fax = "";
        $lockerId = ""; 
        $courierId = "";
        $courierFixedLocationId = ""; 
        $shippingMethod = "flatrate_flatrate";
        $paymentMethod = "cashondelivery";
        $nextOrderDate = "";

        $store = $this->storeManager->getStore();
        $storeId = $store->getStoreId();
        $websiteId = $this->storeManager->getStore()->getWebsiteId();
        $quote = $this->quote->create();
        $quote->setStore($store);

        $customerId = 228386;
        $customer = $this->customerRepository->getById($customerId);
        $quote->setCurrency();
        $quote->assignCustomer($customer);
        
        $allskus = 'BFSITE03';
        $allskus = explode(",", $allskus);

        foreach($allskus as $sku){
            $qty = 1;
            $product = $this->productRepository->get($sku););

            $params = new DataObject([
                'product' => $productId,
                'qty' => $qty
            ]);
            $quote->addProduct($product, $params);
        }

        // Shipping Address to quote
        $quote->getShippingAddress()->setFirstName($firstName);
        $quote->getShippingAddress()->setLastName($lastName);
        $quote->getShippingAddress()->setRegionId($regionId);
        $quote->getShippingAddress()->setRegion($region);
        $quote->getShippingAddress()->setCity($city); 
        $quote->getShippingAddress()->setCountryId($countryId);
        $quote->getShippingAddress()->setPostcode($postcode);
        $quote->getShippingAddress()->setStreet($street);
        $quote->getShippingAddress()->setTelephone($telephone);
        $quote->getShippingAddress()->setFax($fax);
        $quote->getShippingAddress()->setSaveInAddressBook(1);

        // Billing Address to quote
        $quote->getBillingAddress()->setFirstName($firstName);
        $quote->getBillingAddress()->setLastName($lastName);
        $quote->getBillingAddress()->setRegionId($regionId);
        $quote->getBillingAddress()->setRegion($region);
        $quote->getBillingAddress()->setCity($city); 
        $quote->getBillingAddress()->setCountryId($countryId);
        $quote->getBillingAddress()->setPostcode($postcode);
        $quote->getBillingAddress()->setStreet($street);
        $quote->getBillingAddress()->setTelephone($telephone);
        $quote->getBillingAddress()->setFax($fax);
        $quote->getBillingAddress()->setSaveInAddressBook(1);

        // Set Shipping Method
        $shippingAddress = $quote->getShippingAddress();
        $shippingAddress->setCollectShippingRates(false)
                        ->collectShippingRates()
                        ->setShippingMethod($shippingMethod); 

        $quote->setPaymentMethod($paymentMethod);
        $quote->setInventoryProcessed(false);
        $quote->save();
        $quote->getPayment()->importData(['method' => $paymentMethod]);
        $quote->collectTotals()->save();

        // Create a order from quote
        $order = $this->quoteManagement->submit($quote);
        $this->orderSender->send($order);
        $orderId = $order->getIncrementId();

        if($orderId){
            $result['success'] = $orderId;
        } else {
            $result = [ 'error' => true,'msg' => 'Error occurs for Order placed'];
        }
        print_r($result);
    }

Note:- All classes are mentioned in the construct so don’t worry about it.

I have tried following way, but it’s not working.

if($quote->getAppliedRuleIds()) {
    echo "rule applied";
} else {
    echo " no rule applied";
}

I’m having the same issue with Shipping charges as well on the front it’s showing 0 but while generating an order(programmatically) shipping charges are calculated.

Let me know If anyone has any idea regarding this.