1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 
<?php
/* --------------------------------------------------------------
   OrderItemRepositoryWriter.inc.php 2016-06-22
   Gambio GmbH
   http://www.gambio.de
   Copyright (c) 2016 Gambio GmbH
   Released under the GNU General Public License (Version 2)
   [http://www.gnu.org/licenses/gpl-2.0.html]
   --------------------------------------------------------------
*/

MainFactory::load_class('OrderItemRepositoryWriterInterface');

/**
 * Class OrderItemRepositoryWriter
 *
 * @category   System
 * @package    Order
 * @subpackage Repositories
 */
class OrderItemRepositoryWriter implements OrderItemRepositoryWriterInterface
{
    /**
     * Query builder.
     * @var CI_DB_query_builder
     */
    protected $db;
    
    
    /**
     * OrderItemRepositoryWriter constructor.
     *
     * @param CI_DB_query_builder $dbQueryBuilder Query builder.
     */
    public function __construct(CI_DB_query_builder $dbQueryBuilder)
    {
        $this->db = $dbQueryBuilder;
    }
    
    
    /**
     * Inserts an order item to an order by the given order ID.
     *
     * @param IdType             $orderId   ID of the order.
     * @param OrderItemInterface $orderItem Order item to insert.
     *
     * @return int ID of inserted item.
     */
    public function insertIntoOrder(IdType $orderId, OrderItemInterface $orderItem)
    {
        $orderItemArray = array(
            'orders_id'              => $orderId->asInt(),
            'products_model'         => $orderItem->getProductModel(),
            'products_name'          => $orderItem->getName(),
            'products_price'         => $orderItem->getPrice(),
            'products_quantity'      => $orderItem->getQuantity(),
            'final_price'            => $orderItem->getFinalPrice(),
            'products_tax'           => $orderItem->getTax(),
            'allow_tax'              => $orderItem->isTaxAllowed(),
            'products_discount_made' => $orderItem->getDiscountMade(),
            'products_shipping_time' => $orderItem->getShippingTimeInfo(),
            'checkout_information'   => $orderItem->getCheckoutInformation()
        );
        
        $this->db->insert('orders_products', $orderItemArray);
        $orderItemId = $this->db->insert_id();
        
        $downloadInfo = $orderItem->getDownloadInformation();
        foreach($downloadInfo->getArray() as $download)
        {
            $orderItemDownloadArray = array(
                'orders_id'                => $orderId->asInt(),
                'orders_products_id'       => $orderItemId,
                'orders_products_filename' => $download->getFilename(),
                'download_maxdays'         => $download->getMaxDaysAllowed(),
                'download_count'           => $download->getCountAvailable()
            );
            
            $this->db->insert('orders_products_download', $orderItemDownloadArray);
        }
        
        if($orderItem->getQuantityUnitName() !== '')
        {
            $orderItemQuantityUnitArray = array(
                'orders_products_id' => $orderItemId,
                'unit_name'          => $orderItem->getQuantityUnitName()
            );
            
            $this->db->insert('orders_products_quantity_units', $orderItemQuantityUnitArray);
        }
        
        return $orderItemId;
    }
    
    
    /**
     * Update the passed order item.
     *
     * @param StoredOrderItemInterface $orderItem Order item to update.
     *
     * @return OrderItemRepositoryWriter Same instance for method chaining.
     */
    public function update(StoredOrderItemInterface $orderItem)
    {
        $orderItemArray = array(
            'products_model'         => $orderItem->getProductModel(),
            'products_name'          => $orderItem->getName(),
            'products_price'         => $orderItem->getPrice(),
            'products_quantity'      => $orderItem->getQuantity(),
            'final_price'            => $orderItem->getFinalPrice(),
            'products_tax'           => $orderItem->getTax(),
            'allow_tax'              => $orderItem->isTaxAllowed(),
            'products_discount_made' => $orderItem->getDiscountMade(),
            'products_shipping_time' => $orderItem->getShippingTimeInfo(),
            'checkout_information'   => $orderItem->getCheckoutInformation()
        );
        
        $this->db->update('orders_products', $orderItemArray,
                          array('orders_products_id' => $orderItem->getOrderItemId()));
        
        
        $downloadInfo = $orderItem->getDownloadInformation();
        
        // Get order record ID.
        $orderId = $this->db->select('orders_id')
                               ->get_where('orders_products',
                                           array('orders_products_id' => $orderItem->getOrderItemId()))
                               ->row()->orders_id;
        
        // Remove the old download entries (will be re-inserted cause OrderItemDownloadInformation does not have an ID). 
        $this->db->delete('orders_products_download', array('orders_products_id' => $orderItem->getOrderItemId())); 
                
        foreach($downloadInfo->getArray() as $download)
        {
            $orderItemDownloadArray = array(
                'orders_id'                => $orderId,
                'orders_products_id'       => $orderItem->getOrderItemId(),
                'orders_products_filename' => $download->getFilename(),
                'download_maxdays'         => $download->getMaxDaysAllowed(),
                'download_count'           => $download->getCountAvailable()
            );
            
            $this->db->insert('orders_products_download', $orderItemDownloadArray);
        }
        
        if($orderItem->getQuantityUnitName() !== '')
        {
            $quantityUnit = $this->db->get_where('orders_products_quantity_units',
                                                 array('orders_products_id' => $orderItem->getOrderItemId()))
                                     ->result_array();
            
            if(count($quantityUnit))
            {
                $orderItemQuantityUnitArray = array(
                    'unit_name' => $orderItem->getQuantityUnitName()
                );
                
                $this->db->update('orders_products_quantity_units', $orderItemQuantityUnitArray,
                                  array('orders_products_id' => $orderItem->getOrderItemId()));
            }
            else
            {
                $orderItemQuantityUnitArray = array(
                    'orders_products_id' => $orderItem->getOrderItemId(),
                    'unit_name'          => $orderItem->getQuantityUnitName()
                );
                
                $this->db->insert('orders_products_quantity_units', $orderItemQuantityUnitArray);
            }
        }
        else
        {
            $this->db->delete('orders_products_quantity_units',
                              array('orders_products_id' => $orderItem->getOrderItemId()));
        }
    }
}