app/Http/Controllers/Invoicing/Orders/OrderProcessingTrait.php
/**
     * This method makes it possible to convert a products array from the request to the appropriate format for
     * writing to the database.
     *
     * @param Company       $company
     * @param array         $requestedProducts  the products array:[['id' => string, 'quantity' => integer], ...]
     * @param Currency|null $currency           the reference currency to use for finding the appropriate price
     * @param int           $total              the total cost of the requested products
     *
     * @return array
     */
This method makes it possible to convert a products array from the request to the appropriate format for
writing to the database.



@return array
This method makes it possible to convert a products array from the request to the appropriate format for writing to the database . @return array
public function productsToOrderItems(Company $company, array $requestedProducts, Currency $currency = null, int &$total = 0)
    {
        $orderItems = [];
        $currency = $currency ?: new Currency('NGN');
        $productIds = collect($requestedProducts)->mapWithKeys(function ($product) {
            return [$product['id'] => ['qty' => $product['quantity'], 'price' => $product['price'] ?? -1]];
        })->all();
        $products = $company->products()->with(['prices'])->whereIn('uuid', array_keys($productIds))->get();
        foreach ($products as $product) {
            $info = $productIds[$product->uuid] ?? null;
            if (empty($info)) {
                continue;
            }
            $productPrice = null;
            if ($info['price'] === -1) {
                foreach ($product->prices as $price) {
                    if ($price->currency !== $currency->getCode()) {
                        continue;
                    }
                    $productPrice = $price;
                    break;
                }
                if ($productPrice === null) {
                    throw new RecordNotFoundException(
                        'No matching price for the '.$currency->getCode().' currency has been added for the product '.
                        $product->name.'. You should either add a price for the currency, or remove the product from the order.'
                    );
                }
            }
            $orderItems[$product->id] = [
                'uuid' => Uuid::uuid1()->toString(),
                'unit_price' => !empty($productPrice) ? $productPrice->unit_price : $info['price'],
                'quantity' => $productIds[$product->uuid]['qty']
            ];
            $total += $orderItems[$product->id]['quantity'] * $orderItems[$product->id]['unit_price'];
        }
        return $orderItems;
    }
public function productsToOrderItems ( Company $ company , array $ requestedProducts , Currency $ currency = null , int & $ total = 0 ) { $ orderItems = [ ] ; $ currency = $ currency ? : new Currency ( 'NGN' ) ; $ productIds = collect ( $ requestedProducts ) -> mapWithKeys ( function ( $ product ) { return [ $ product [ 'id' ] => [ 'qty' => $ product [ 'quantity' ] , 'price' => $ product [ 'price' ] ?? - 1 ] ] ; } ) -> all ( ) ; $ products = $ company -> products ( ) -> with ( [ 'prices' ] ) -> whereIn ( 'uuid' , array_keys ( $ productIds ) ) -> get ( ) ; foreach ( $ products as $ product ) { $ info = $ productIds [ $ product -> uuid ] ?? null ; if ( empty ( $ info ) ) { continue ; } $ productPrice = null ; if ( $ info [ 'price' ] === - 1 ) { foreach ( $ product -> prices as $ price ) { if ( $ price -> currency !== $ currency -> getCode ( ) ) { continue ; } $ productPrice = $ price ; break ; } if ( $ productPrice === null ) { throw new RecordNotFoundException ( 'No matching price for the ' . $ currency -> getCode ( ) . ' currency has been added for the product ' . $ product -> name . '. You should either add a price for the currency, or remove the product from the order.' ) ; } } $ orderItems [ $ product -> id ] = [ 'uuid' => Uuid :: uuid1 ( ) -> toString ( ) , 'unit_price' => ! empty ( $ productPrice ) ? $ productPrice -> unit_price : $ info [ 'price' ] , 'quantity' => $ productIds [ $ product -> uuid ] [ 'qty' ] ] ; $ total += $ orderItems [ $ product -> id ] [ 'quantity' ] * $ orderItems [ $ product -> id ] [ 'unit_price' ] ; } return $ orderItems ; }
This method makes it possible to convert a products array from the request to the appropriate format for
 writing to the database.
This method makes it possible to convert a products array from the request to the appropriate format for writing to the database .
# container for the products to be synced to the order as order items # we set the default currency, if none is specified # list of product ids # get the requested products # loop through the individual products # get the product entry # the price to use for the product # no price was sent in the request # we could not find a matching price (based on currency) for this product # our array of product items # update the total price