<commit_before><?php

/*
 * This file is part of the Sonata package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Sonata\Bundle\EcommerceDemoBundle\DataFixtures\ORM;

use Application\Sonata\CustomerBundle\Entity\Address;
use Application\Sonata\CustomerBundle\Entity\Customer;
use Application\Sonata\OrderBundle\Entity\OrderElement;
use Application\Sonata\PaymentBundle\Entity\Transaction;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Common\DataFixtures\AbstractFixture;
use Sonata\Component\Currency\Currency;
use Sonata\Component\Order\OrderInterface;
use Sonata\CustomerBundle\Entity\BaseAddress;
use Sonata\CustomerBundle\Entity\BaseCustomer;
use Sonata\OrderBundle\Entity\BaseOrder;
use Sonata\PaymentBundle\Entity\BaseTransaction;
use Sonata\ProductBundle\Entity\BaseProduct;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Application\Sonata\OrderBundle\Entity\Order;
use Application\Sonata\ProductBundle\Entity\Delivery;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Persistence\ObjectManager;
use Faker\Generator;

/**
 * Category fixtures loader.
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 */
class LoadOrderData extends AbstractFixture implements ContainerAwareInterface, OrderedFixtureInterface
{
    /**
     * @var ContainerInterface
     */
    protected $container;

    /**
     * @var ArrayCollection
     */
    protected $orderElements;

    /**
     * {@inheritDoc}
     */
    public function load(ObjectManager $manager)
    {
        $nbCustomers = 500;

        $products = array(
            $this->getReference('php_plush_goodie_product'),
            $this->getReference('sonata_training_goodie_product'),
            $this->getReference('php_mug_goodie_product'),
            $this->getReference('php_teeshirt_goodie_product'),
        );

        for ($i = 1; $i <= $nbCustomers; $i++) {
            $customer = $this->generateCustomer($manager);

            $customerProducts = array();

            $orderProductsKeys = array_rand($products, rand(1, count($products)));

            if (is_array($orderProductsKeys)) {
                foreach ($orderProductsKeys as $orderProductKey) {
                    $customerProducts[] = $products[$orderProductKey];
                }
            } else {
                $customerProducts = array($products[$orderProductsKeys]);
            }

            $order = $this->createOrder($customer, $customerProducts, $manager);

            $this->createTransaction($order, $manager);

            if (!($i % 10)) {
                $manager->flush();
            }
        }
    }

    /**
     * Creates a fake Order.
     *
     * @param Customer      $customer
     * @param array         $products
     * @param ObjectManager $manager
     *
     * @return OrderInterface
     */
    protected function createOrder(Customer $customer, array $products, ObjectManager $manager)
    {
        $orderElements = array();
        $totalExcl     = 0;
        $totalInc      = 0;

        $order = new Order();

        // customer
        $order->setCustomer($customer);
        $order->setLocale($customer->getLocale());
        $order->setUsername($customer->getFullname());

        // Billing
        $customerBillingAddressAddresses = $customer->getAddressesByType(BaseAddress::TYPE_BILLING);
        $order->setBillingAddress1($customerBillingAddressAddresses[0]->getAddress1());
        $order->setBillingAddress2($customerBillingAddressAddresses[0]->getAddress2());
        $order->setBillingAddress3($customerBillingAddressAddresses[0]->getAddress3());
        $order->setBillingCity($customerBillingAddressAddresses[0]->getCity());
        $order->setBillingCountryCode($customerBillingAddressAddresses[0]->getCountryCode());
        $order->setBillingEmail($customer->getEmail());
        $order->setBillingName($customer->getFullname());
        $order->setBillingPhone($customerBillingAddressAddresses[0]->getPhone());
        $order->setBillingFax($customer->getFaxNumber());
        $order->setBillingMobile($customer->getMobileNumber());
        $order->setBillingPostcode($customerBillingAddressAddresses[0]->getPostcode());

        // Shipping
        $customerDeliveryAddressAddresses = $customer->getAddressesByType(BaseAddress::TYPE_DELIVERY);
        $order->setShippingAddress1($customerDeliveryAddressAddresses[0]->getAddress1());
        $order->setShippingAddress2($customerDeliveryAddressAddresses[0]->getAddress2());
        $order->setShippingAddress3($customerDeliveryAddressAddresses[0]->getAddress3());
        $order->setShippingCity($customerDeliveryAddressAddresses[0]->getCity());
        $order->setShippingCountryCode($customerDeliveryAddressAddresses[0]->getCountryCode());
        $order->setShippingEmail($customer->getEmail());
        $order->setShippingName($customer->getFullname());
        $order->setShippingPhone($customerDeliveryAddressAddresses[0]->getPhone());
        $order->setShippingFax($customer->getFaxNumber());
        $order->setShippingMobile($customer->getMobileNumber());
        $order->setShippingPostcode($customerDeliveryAddressAddresses[0]->getPostcode());

        // Delivery
        $order->setDeliveryMethod('free'); // @todo : change Delivery method integration
        $order->setDeliveryCost(rand(5, 40));
        $order->setDeliveryStatus(array_rand(Delivery::getStatusList()));

        // Payment
        $order->setCurrency(new Currency()); // @todo : change Currency integration
        $order->setPaymentMethod('free');    // @todo : change Payment method integration
        $order->setPaymentStatus(array_rand(BaseTransaction::getStatusList()));

        // OrderElements
        foreach ($products as $product) {
            $orderElement = $this->createOrderElement($product);

            $orderElement->setOrder($order);

            $orderElements[] = $orderElement;

            $totalExcl += $orderElement->getUnitPrice(false);
            $totalInc  += $orderElement->getUnitPrice(true);
        }

        $order->setOrderElements($orderElements);

        $order->setTotalExcl($totalExcl);
        $order->setTotalInc($totalInc);

        $order->setStatus(array_rand(BaseOrder::getStatusList()));

        $manager->persist($order);

        return $order;
    }<commit_msg>SONATA-77 - Fixed Currency issue<commit_after><?php

/*
 * This file is part of the Sonata package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Sonata\Bundle\EcommerceDemoBundle\DataFixtures\ORM;

use Application\Sonata\CustomerBundle\Entity\Address;
use Application\Sonata\CustomerBundle\Entity\Customer;
use Application\Sonata\OrderBundle\Entity\OrderElement;
use Application\Sonata\PaymentBundle\Entity\Transaction;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Common\DataFixtures\AbstractFixture;
use Sonata\Component\Currency\Currency;
use Sonata\Component\Order\OrderInterface;
use Sonata\CustomerBundle\Entity\BaseAddress;
use Sonata\CustomerBundle\Entity\BaseCustomer;
use Sonata\OrderBundle\Entity\BaseOrder;
use Sonata\PaymentBundle\Entity\BaseTransaction;
use Sonata\ProductBundle\Entity\BaseProduct;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Application\Sonata\OrderBundle\Entity\Order;
use Application\Sonata\ProductBundle\Entity\Delivery;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Persistence\ObjectManager;
use Faker\Generator;

/**
 * Category fixtures loader.
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 */
class LoadOrderData extends AbstractFixture implements ContainerAwareInterface, OrderedFixtureInterface
{
    /**
     * @var ContainerInterface
     */
    protected $container;

    /**
     * @var ArrayCollection
     */
    protected $orderElements;

    /**
     * {@inheritDoc}
     */
    public function load(ObjectManager $manager)
    {
        $nbCustomers = 500;

        $products = array(
            $this->getReference('php_plush_goodie_product'),
            $this->getReference('sonata_training_goodie_product'),
            $this->getReference('php_mug_goodie_product'),
            $this->getReference('php_teeshirt_goodie_product'),
        );

        for ($i = 1; $i <= $nbCustomers; $i++) {
            $customer = $this->generateCustomer($manager);

            $customerProducts = array();

            $orderProductsKeys = array_rand($products, rand(1, count($products)));

            if (is_array($orderProductsKeys)) {
                foreach ($orderProductsKeys as $orderProductKey) {
                    $customerProducts[] = $products[$orderProductKey];
                }
            } else {
                $customerProducts = array($products[$orderProductsKeys]);
            }

            $order = $this->createOrder($customer, $customerProducts, $manager);

            $this->createTransaction($order, $manager);

            if (!($i % 10)) {
                $manager->flush();
            }
        }
    }

    /**
     * Creates a fake Order.
     *
     * @param Customer      $customer
     * @param array         $products
     * @param ObjectManager $manager
     *
     * @return OrderInterface
     */
    protected function createOrder(Customer $customer, array $products, ObjectManager $manager)
    {
        $orderElements = array();
        $totalExcl     = 0;
        $totalInc      = 0;

        $order = new Order();

        // customer
        $order->setCustomer($customer);
        $order->setLocale($customer->getLocale());
        $order->setUsername($customer->getFullname());

        // Billing
        $customerBillingAddressAddresses = $customer->getAddressesByType(BaseAddress::TYPE_BILLING);
        $order->setBillingAddress1($customerBillingAddressAddresses[0]->getAddress1());
        $order->setBillingAddress2($customerBillingAddressAddresses[0]->getAddress2());
        $order->setBillingAddress3($customerBillingAddressAddresses[0]->getAddress3());
        $order->setBillingCity($customerBillingAddressAddresses[0]->getCity());
        $order->setBillingCountryCode($customerBillingAddressAddresses[0]->getCountryCode());
        $order->setBillingEmail($customer->getEmail());
        $order->setBillingName($customer->getFullname());
        $order->setBillingPhone($customerBillingAddressAddresses[0]->getPhone());
        $order->setBillingFax($customer->getFaxNumber());
        $order->setBillingMobile($customer->getMobileNumber());
        $order->setBillingPostcode($customerBillingAddressAddresses[0]->getPostcode());

        // Shipping
        $customerDeliveryAddressAddresses = $customer->getAddressesByType(BaseAddress::TYPE_DELIVERY);
        $order->setShippingAddress1($customerDeliveryAddressAddresses[0]->getAddress1());
        $order->setShippingAddress2($customerDeliveryAddressAddresses[0]->getAddress2());
        $order->setShippingAddress3($customerDeliveryAddressAddresses[0]->getAddress3());
        $order->setShippingCity($customerDeliveryAddressAddresses[0]->getCity());
        $order->setShippingCountryCode($customerDeliveryAddressAddresses[0]->getCountryCode());
        $order->setShippingEmail($customer->getEmail());
        $order->setShippingName($customer->getFullname());
        $order->setShippingPhone($customerDeliveryAddressAddresses[0]->getPhone());
        $order->setShippingFax($customer->getFaxNumber());
        $order->setShippingMobile($customer->getMobileNumber());
        $order->setShippingPostcode($customerDeliveryAddressAddresses[0]->getPostcode());

        // Delivery
        $order->setDeliveryMethod('free'); // @todo : change Delivery method integration
        $order->setDeliveryCost(rand(5, 40));
        $order->setDeliveryStatus(array_rand(Delivery::getStatusList()));

        $currency = new Currency();
        $currency->setLabel('EUR');

        // Payment
        $order->setCurrency($currency);
        $order->setPaymentMethod('free');    // @todo : change Payment method integration
        $order->setPaymentStatus(array_rand(BaseTransaction::getStatusList()));

        // OrderElements
        foreach ($products as $product) {
            $orderElement = $this->createOrderElement($product);

            $orderElement->setOrder($order);

            $orderElements[] = $orderElement;

            $totalExcl += $orderElement->getUnitPrice(false);
            $totalInc  += $orderElement->getUnitPrice(true);
        }

        $order->setOrderElements($orderElements);

        $order->setTotalExcl($totalExcl);
        $order->setTotalInc($totalInc);

        $order->setStatus(array_rand(BaseOrder::getStatusList()));

        $manager->persist($order);

        return $order;
    }
{'fraction_non_alphanumeric': 0.13193701268919125, 'fraction_numerical': 0.005962391071701574, 'mean_word_length': 3.4545454545454546, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 170, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5937879', 'n_tokens_mistral': 3601, 'n_tokens_neox': 3476, 'n_words': 510}