<?php

/**
 * patch actions.
 *
 * @package www.katao.fr
 * @subpackage patch
 * @author Your name here
 * @version SVN: $Id: actions.class.php 9301 2008-05-27 01:08:46Z dwhittle $
 */
class patchActions extends sfActions {
    public function execute2009_11_25() {
        die('already applied');
		// création de la commande fournisseur "ECOBURO" sur le noeud "MOISSAC" pour la période "Novembre 2009"
        $katao_supplier_id = 26;
        $katao_node_id = 2;
        $katao_period_id = 10;

        $criteria = new Criteria();
        $criteria->addJoin(KataoCartPeer::ID, KataoCartProductPeer::KATAO_CART_ID);
        $criteria->addJoin(KataoCartProductPeer::KATAO_PRODUCT_ID, KataoProductPeer::ID);
        $criteria->add(KataoCartPeer::KATAO_PERIOD_ID, $katao_period_id);
        $criteria->add(KataoCartPeer::KATAO_NODE_ID, $katao_node_id);
        $criteria->add(KataoProductPeer::KATAO_SUPPLIER_ID, $katao_supplier_id);

        $products = array();
        foreach (KataoCartProductPeer::doSelectJoinKataoProduct($criteria) as/*(KataoCartProduct)*/ $katao_cart_product) {
            $katao_product = $katao_cart_product->getKataoProduct();

            if (!isset($products[$katao_product->getId()]['quantity'])) {
                $products[$katao_product->getId()]['quantity'] = 0;
            }
            $products[$katao_product->getId()]['quantity'] += $katao_cart_product->getQuantity();

            $products[$katao_product->getId()]['reference'] = $katao_product->getReference();
            $products[$katao_product->getId()]['name'] = $katao_product->getName();
            $products[$katao_product->getId()]['price_euro'] = $katao_product->getUnitPriceEuro();
            $products[$katao_product->getId()]['margin'] = $katao_product->getMargin();
            $products[$katao_product->getId()]['tva_rate'] = $katao_product->getTvaRate();
            $products[$katao_product->getId()]['katao_cart_products'][] = $katao_cart_product;
        }

        if (null != $katao_supplier = KataoSupplierPeer::retrieveByPK($katao_supplier_id)) {
            $katao_order = new KataoOrder();

            $katao_order->setIsSimulated(false);

            $katao_order->setKataoSupplierId($katao_supplier_id);
            $katao_order->setKataoNodeId($katao_node_id);
            $katao_order->setKataoPeriodId($katao_period_id);
            $katao_order->setStatus(KataoOrder::STATUS_GENERATED);

            $katao_order->setSupplierName($katao_supplier->getName());
            $katao_order->setSupplierEmail($katao_supplier->getEmail());
            $katao_order->setSupplierAddress1($katao_supplier->getAddress1());
            $katao_order->setSupplierAddress2($katao_supplier->getAddress2());
            $katao_order->setSupplierZip($katao_supplier->getZip());
            $katao_order->setSupplierCity($katao_supplier->getCity());

            $katao_order->setKataoName(wpConfig::getName());
            $katao_order->setKataoAddress(wpConfig::getAddress());
            $katao_order->setKataoSiretNumber(wpConfig::getSiretNumber());
            $katao_order->setKataoRcsNumber(wpConfig::getRCSNumber());
            $katao_order->setKataoCapital(wpConfig::getCapital());

            $katao_order->save();

            foreach ($products as $katao_product_id => $product_data) {
                $katao_order_product = new KataoOrderProduct();
                $katao_order_product->setKataoOrder($katao_order);
                $katao_order_product->setKataoProductId($katao_product_id);
                $katao_order_product->setProductReference($product_data['reference']);
                $katao_order_product->setProductName($product_data['name']);
                $katao_order_product->setProductPriceEuro($product_data['price_euro']);
                $katao_order_product->setProductMargin($product_data['margin']);
                $katao_order_product->setProductTvaRate($product_data['tva_rate']);
                $katao_order_product->setQuantity($product_data['quantity']);
                $katao_order_product->save();

                foreach ($product_data['katao_cart_products'] as $katao_cart_product) {
                    $katao_cart_product->setKataoOrderProduct($katao_order_product);
                    $katao_cart_product->setQuantityAdjusted($katao_cart_product->getQuantity());
                    $katao_cart_product->setHasBeenAdjusted(true);
                    $katao_cart_product->save();
                }
            }
        }

        die('done');
    }
}