Insert fake orders with faker

modifié:         setup/faker.php
This commit is contained in:
Benjamin Perche
2014-06-18 12:31:36 +02:00
parent 8aac09ad90
commit 2fcec08e50

View File

@@ -9,6 +9,8 @@ use Thelia\Coupon\FacadeInterface;
use Thelia\Condition\ConditionCollection; use Thelia\Condition\ConditionCollection;
use Thelia\Coupon\Type\RemoveXAmount; use Thelia\Coupon\Type\RemoveXAmount;
use Thelia\Coupon\Type\RemoveXPercent; use Thelia\Coupon\Type\RemoveXPercent;
use Thelia\Model\ModuleQuery;
use Thelia\Model\OrderAddress;
if (php_sapi_name() != 'cli') { if (php_sapi_name() != 'cli') {
throw new \Exception('this script can only be launched with cli sapi'); throw new \Exception('this script can only be launched with cli sapi');
@@ -492,6 +494,87 @@ try {
} }
} }
echo "Creating orders";
for($i=0; $i < 50; ++$i) {
$placedOrder = new \Thelia\Model\Order();
$deliveryOrderAddress = new OrderAddress();
$deliveryOrderAddress
->setCustomerTitleId(mt_rand(1,3))
->setCompany($faker->text(15))
->setFirstname($faker->firstname)
->setLastname($faker->lastname)
->setAddress1($faker->streetAddress)
->setAddress2($faker->streetAddress)
->setAddress3($faker->streetAddress)
->setPhone($faker->phoneNumber)
->setZipcode($faker->postcode)
->setCity($faker->city)
->setCountryId(64)
->save($con)
;
$invoiceOrderAddress = new OrderAddress();
$invoiceOrderAddress
->setCustomerTitleId(mt_rand(1,3))
->setCompany($faker->text(15))
->setFirstname($faker->firstname)
->setLastname($faker->lastname)
->setAddress1($faker->streetAddress)
->setAddress2($faker->streetAddress)
->setAddress3($faker->streetAddress)
->setPhone($faker->phoneNumber)
->setZipcode($faker->postcode)
->setCity($faker->city)
->setCountryId(64)
->save($con)
;
$placedOrder
->setDeliveryOrderAddressId($deliveryOrderAddress->getId())
->setInvoiceOrderAddressId($invoiceOrderAddress->getId())
->setDeliveryModuleId(ModuleQuery::create()->filterByCode("Colissimo")->findOne()->getId())
->setPaymentModuleId(ModuleQuery::create()->filterByCode("Cheque")->findOne()->getId())
->setStatusId(mt_rand(1, 5))
->setCurrency(\Thelia\Model\CurrencyQuery::create()->findOne())
->setCustomerId(getRandomObject(new \Thelia\Model\CustomerQuery())->getId())
->setDiscount(mt_rand(0, 10))
->setLang(getRandomObject(new \Thelia\Model\LangQuery()))
->setPostage(mt_rand(1, 50))
;
$placedOrder->save($con);
for ($j=0; $j < mt_rand(1, 10); ++$j) {
$pse = getRandomObject(new \Thelia\Model\ProductSaleElementsQuery());
$product = $pse->getProduct();
$orderProduct = new \Thelia\Model\OrderProduct();
$orderProduct
->setOrderId($placedOrder->getId())
->setProductRef($product->getRef())
->setProductSaleElementsRef($pse->getRef())
->setTitle($product->getTitle())
->setChapo($product->getChapo())
->setDescription($product->getDescription())
->setPostscriptum($product->getPostscriptum())
->setQuantity(mt_rand(1, 10))
->setPrice($price=mt_rand(1, 100))
->setPromoPrice(mt_rand(1, $price))
->setWasNew($pse->getNewness())
->setWasInPromo(rand(0,1) == 1)
->setWeight($pse->getWeight())
->setTaxRuleTitle($faker->text(20))
->setTaxRuleDescription($faker->text(50))
->setEanCode($pse->getEanCode())
->save($con);
}
}
echo "Generating coupons fixtures\n"; echo "Generating coupons fixtures\n";
generateCouponFixtures($thelia); generateCouponFixtures($thelia);
@@ -814,3 +897,16 @@ Sed facilisis pellentesque nisl, eu tincidunt erat scelerisque a. Nullam malesua
$coupon3->setIsAvailableOnSpecialOffers(false); $coupon3->setIsAvailableOnSpecialOffers(false);
$coupon3->save(); $coupon3->save();
} }
function getRandomObject(\Propel\Runtime\ActiveQuery\ModelCriteria $query)
{
$max = $query->count();
if($max === 0) {
throw new Exception("There is no entry in ". get_class($query));
}
$first_id = $query->findOne()->getId();
$obj = $query->findPk(mt_rand($first_id, $first_id+$max-1));
return $obj;
}