Zend certified PHP/Magento developer

magento creating order programmatically using Cron Jobs

I want to create an order programmatically using Cron jobs, the codes run perfectly but when creating the order i got ‘Error: Type Error occurred when creating object: MagentoInventoryIndexerModelQueueReservationData, Argument 2 passed to MagentoInventoryIndexerModelQueueReservationData::__construct() must be of the type int, null given, called in /var/www/html/magento2/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php on line 121string’

<?php

namespace TechSystemCron;

use MagentoFrameworkHTTPClientCurl;
use MagentoFrameworkExceptionNoSuchEntityException;
use MagentoStoreModelStoreManagerInterface;
use MagentoQuoteApiCartManagementInterface;
use MagentoCustomerApiCustomerRepositoryInterface;
use MagentoQuoteModelQuoteFactory;
use MagentoCatalogApiProductRepositoryInterface;
use MagentoCustomerApiAddressRepositoryInterface;
use MagentoCustomerModelCustomerFactory;
use MagentoStoreModelAppEmulation;
use MagentoInventoryApiApiStockRepositoryInterface;
use MagentoQuoteApiCartRepositoryInterface;

class OrderIntegration
{

/**
* @var MagentoFrameworkHTTPClientCurl
*/
protected $curlClient;

/**
 * @var string
 */
protected $urlPrefix = 'https://';


/**
 * @var string
 */
protected $apiUrl = 'myApi Url';

 /**
 * @var MagentoStoreModelStoreManagerInterface
 */
protected $storeManager;

protected $quoteManagement;

/**
 * @var CustomerRepositoryInterface
 */

protected $customerRepository;

protected $quoteFactory;

protected $productRepository;

/**
 * @var AddressRepositoryInterface
 */

protected $addressRepository;

protected $customerFactory;

protected $stockRepository;

protected $cartRepository;

/**

 * @param StoreManagerInterface      $storeManager
 * @param Curl          $curl
 * @param CartManagementInterface      $cartManagement
 * @param QuoteFactory      $quoteFactory
 * @param AddressRepositoryInterface $addressRepository
 * @param CustomerRepositoryInterface $customerRepository
 * @param ProductRepositoryInterface   $productRepository
 * @param CustomerFactory $customerFactory
 * @param StockRepository $stockRepository     
*/

public function __construct(
StoreManagerInterface $storeManager,
CartManagementInterface $cartManagement,
CustomerRepositoryInterface $customerRepository,
QuoteFactory $quoteFactory,
ProductRepositoryInterface $productRepository,
AddressRepositoryInterface $addressRepository,
Curl $curl,
CustomerFactory $customerFactory,
CartRepositoryInterface $cartRepository,
StockRepositoryInterface $stockRepository
)
{
$this->storeManager = $storeManager;
$this->quoteManagement = $cartManagement;
$this->customerRepository = $customerRepository;
$this->quoteFactory = $quoteFactory;
$this->productRepository = $productRepository;
$this->addressRepository = $addressRepository;
$this->curlClient = $curl;
$this->customerFactory = $customerFactory;
$this->cartRepository = $cartRepository;
$this->stockRepository = $stockRepository;
}

 public function getApiUrl()
{
    return $this->urlPrefix . $this->apiUrl;
}

public function execute()
{

$apiUrl = $this->getApiUrl();

     $this->getCurlClient()->addHeader("Content-Type", "application/json");
    //$this->getCurlClient()->setOption(CURLOPT_SSL_VERIFYHOST,false);
    //$this->getCurlClient()->setOption(CURLOPT_SSL_VERIFYPEER,false);
    $this->getCurlClient()->get($apiUrl);
    $response = json_decode($this->getCurlClient()->getBody());

//$ola = json_encode($response);

    //$ten = json_decode($ola);


        $texts = $response[1];
    //var_dump($texts);
    //die();
    

     foreach($texts as $text) {

if(empty($text->items)) {
    continue;
  }

//var_dump($text->items[0]->productsku);    
//die();    



// Data Information



$orderData =[
        'email'        => $text->member->email, //customer email id
        'currency_id'  => 'USD',
    'address' =>[
            'firstname'    => $text->billingAddress->firstname,
            'lastname'     => $text->billingAddress->lastname,
            'prefix' => '',
            'suffix' => '',
            'street' => $text->billingAddress->address,
            'city' => $text->billingAddress->city,
            'country_id' => 'US',
            'region' => 'california',
            'region_id' => '12', // State region id
            'postcode' => '12345',
            'telephone' => '',
            'fax' => '',
            'save_in_address_book' => 1
        ],
        'items'=>
            [
                //simple product
                [
                    'product_sku' => $text->items[0]->productsku,
                    'qty' => $text->items[0]->quantity                    
        ],
            ]
    ];


    $store = $this->storeManager->getStore();
    $storeId = $store->getStoreId();

    $websiteId = $this->storeManager->getStore()->getWebsiteId();
    $customer = $this->customerFactory->create();
    $customer->setWebsiteId($websiteId);
   $customer = $customer->loadByEmail($orderData['email']);
if(!$customer->getId()){
        //if customer is not exist then skip that customer 
    continue;            
    }
    $quote = $this->quoteFactory->create(); 
    $quote->setStore($store); 
    
    $customer= $this->customerRepository->getById($customer->getId());
    $quote->setCurrency();
    $quote->assignCustomer($customer); // Quote assign to customer
    




try {
    
    $product = $this->productRepository->get($text->items[0]->productsku);
        
var_dump("Product Created with SKU" ."" . $product->getId() . "". "Successfully");

$quote->addProduct($product,intval($text->items[0]->quantity));

 

       } catch (NoSuchEntityException $e) {
    
var_dump("No such product");

continue;

}
   

   
      
//Ending of Foreach loop for getting product


//Starting of Billing and Shipping

  $billingAddress = null;
 $shippingAddress = null;
 try {
     $billingAddress = $this->addressRepository->getById($customer->getDefaultBilling());
     $shippingAddress = $this->addressRepository->getById($customer->getDefaultShipping());

var_dump("customer Found");
 }
 catch (NoSuchEntityException $e) {Var_dump("Customer Not Found");

continue;

}

 if ($billingAddress) {
     $quote->getBillingAddress()->importCustomerAddressData($billingAddress);

var_dump("it is a cool game");  
 }

 if ($shippingAddress) {
     $quote->getShippingAddress()->importCustomerAddressData($shippingAddress);
     $shippingAddress = $quote->getShippingAddress();

    $shippingAddress->setCollectShippingRates(true)->collectShippingRates()->setShippingMethod('flatrate_flatrate');

var_dump("cool");
 }
  //set shipping method
    $quote->setPaymentMethod('checkmo'); //set payment method
    $quote->setInventoryProcessed(false);

    $quote->save(); //quote save 
    
    $quote->getPayment()->importData(['method' => 'checkmo']);

    // Collect Quote Total and Save
   if($quote->collectTotals()->save()) {
var_dump("Are you joking?");
}
    
    // Create Order From Quote Object

try {
    $order = $this->quoteManagement->submit($quote);
          var_dump("Is that real");
} catch (Exception $e) {
    // Log or handle the exception
    echo "Error: " . $e->getMessage();
}
           
    //order id for placed order 
    $orderId = $order->getIncrementId();
    if($orderId){
        $result['success']= $orderId;

   var_dump("Successfully Created Cheers");

    }else{
       $result=['error'=>true,'msg'=>'something went wrong when Order placed'];
    }
    return $result;  
//Ending of Billing and Shipping 
     
}

}

          /**
         * @return Curl
         */
        public function getCurlClient()
        {
            return $this->curlClient;
        }        

}