Passed
Push — master ( 03c0bd...d9a7d5 )
by
unknown
05:08
created

OrderProduct::parseResults()   C

Complexity

Conditions 11
Paths 3

Size

Total Lines 82
Code Lines 58

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 58
c 1
b 0
f 0
dl 0
loc 82
rs 6.7696
cc 11
nc 3
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/*************************************************************************************/
3
/*      This file is part of the Thelia package.                                     */
4
/*                                                                                   */
5
/*      Copyright (c) OpenStudio                                                     */
6
/*      email : [email protected]                                                       */
7
/*      web : http://www.thelia.net                                                  */
8
/*                                                                                   */
9
/*      For the full copyright and license information, please view the LICENSE.txt  */
10
/*      file that was distributed with this source code.                             */
11
/*************************************************************************************/
12
13
namespace Thelia\Core\Template\Loop;
14
15
use Propel\Runtime\ActiveQuery\Criteria;
16
use Propel\Runtime\ActiveQuery\Join;
17
use Thelia\Core\Template\Element\BaseLoop;
18
use Thelia\Core\Template\Element\LoopResult;
19
use Thelia\Core\Template\Element\LoopResultRow;
20
use Thelia\Core\Template\Element\PropelSearchLoopInterface;
21
use Thelia\Core\Template\Loop\Argument\ArgumentCollection;
22
use Thelia\Core\Template\Loop\Argument\Argument;
23
use Thelia\Model\ConfigQuery;
24
use Thelia\Model\Map\OrderProductTableMap;
0 ignored issues
show
Bug introduced by
The type Thelia\Model\Map\OrderProductTableMap was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
25
use Thelia\Model\Map\ProductSaleElementsTableMap;
0 ignored issues
show
Bug introduced by
The type Thelia\Model\Map\ProductSaleElementsTableMap was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
26
use Thelia\Model\OrderProductQuery;
27
use Thelia\Type\BooleanOrBothType;
28
29
/**
30
 *
31
 * OrderProduct loop
32
 *
33
 * Class OrderProduct
34
 * @package Thelia\Core\Template\Loop
35
 * @author Etienne Roudeix <[email protected]>
36
 *
37
 * {@inheritdoc}
38
 * @method int getOrder()
39
 * @method int[] getId()
40
 * @method bool|string getVirtual()
41
 */
42
class OrderProduct extends BaseLoop implements PropelSearchLoopInterface
43
{
44
    protected $timestampable = true;
45
46
    /**
47
     * @return ArgumentCollection
48
     */
49
    protected function getArgDefinitions()
50
    {
51
        return new ArgumentCollection(
52
            Argument::createIntTypeArgument('order', null, true),
53
            Argument::createIntListTypeArgument('id'),
54
            Argument::createBooleanOrBothTypeArgument('virtual', BooleanOrBothType::ANY)
55
        );
56
    }
57
58
    public function buildModelCriteria()
59
    {
60
        $search = OrderProductQuery::create();
61
62
        $search->joinOrderProductTax('opt', Criteria::LEFT_JOIN)
63
            ->withColumn('SUM(`opt`.AMOUNT)', 'TOTAL_TAX')
64
            ->withColumn('SUM(`opt`.PROMO_AMOUNT)', 'TOTAL_PROMO_TAX')
65
            ->groupById();
66
67
68
        // new join to get the product id if it exists
69
        $pseJoin = new Join(
70
            OrderProductTableMap::COL_PRODUCT_SALE_ELEMENTS_ID,
71
            ProductSaleElementsTableMap::COL_ID,
72
            Criteria::LEFT_JOIN
73
        );
74
        $search
75
            ->addJoinObject($pseJoin)
76
            ->addAsColumn(
77
                'product_id',
78
                ProductSaleElementsTableMap::COL_PRODUCT_ID
79
            )
80
        ;
81
82
        $order = $this->getOrder();
83
84
        $search->filterByOrderId($order, Criteria::EQUAL);
85
86
        $virtual = $this->getVirtual();
87
        if ($virtual !== BooleanOrBothType::ANY) {
88
            if ($virtual) {
89
                $search
90
                    ->filterByVirtual(1, Criteria::EQUAL)
91
                    ->filterByVirtualDocument(null, Criteria::NOT_EQUAL);
92
            } else {
93
                $search
94
                    ->filterByVirtual(0);
95
            }
96
        }
97
98
        if (null !== $this->getId()) {
0 ignored issues
show
introduced by
The condition null !== $this->getId() is always true.
Loading history...
99
            $search->filterById($this->getId(), Criteria::IN);
100
        }
101
102
        $search->orderById(Criteria::ASC);
103
104
        return $search;
105
    }
106
107
    /**
108
     * @param LoopResult $loopResult
109
     * @return LoopResult
110
     * @throws \Propel\Runtime\Exception\PropelException
111
     */
112
    public function parseResults(LoopResult $loopResult)
113
    {
114
        $lastLegacyRoundingOrderId = ConfigQuery::read('last_legacy_rounding_order_id', 0);
115
116
        /** @var \Thelia\Model\OrderProduct $orderProduct */
117
        foreach ($loopResult->getResultDataCollection() as $orderProduct) {
118
            $loopResultRow = new LoopResultRow($orderProduct);
119
120
            $tax = $orderProduct->getVirtualColumn('TOTAL_TAX');
121
            $promoTax = $orderProduct->getVirtualColumn('TOTAL_PROMO_TAX');
122
123
            $totalTax = round($tax * $orderProduct->getQuantity(), 2);
124
            $totalPromoTax = round($promoTax * $orderProduct->getQuantity(), 2);
125
126
            // To prevent price changes in pre-2.4 orders, use the legacy calculation method
127
            if ($orderProduct->getOrderId() <= $lastLegacyRoundingOrderId) {
128
                $taxedPrice = $orderProduct->getPrice() + $orderProduct->getVirtualColumn('TOTAL_TAX');
129
                $taxedPromoPrice = $orderProduct->getPromoPrice() + $orderProduct->getVirtualColumn('TOTAL_PROMO_TAX');
130
131
                $totalPrice = $orderProduct->getPrice()*$orderProduct->getQuantity();
132
                $totalPromoPrice = $orderProduct->getPromoPrice()*$orderProduct->getQuantity();
133
            } else {
134
                $taxedPrice = $orderProduct->getPrice() + $tax;
135
                $taxedPromoPrice = $orderProduct->getPromoPrice() + $promoTax;
136
137
                // Price calculation should use the same rounding method as in CartItem::getTotalTaxedPromoPrice()
138
                // For each order line, we first round the taxed price, then we multiply by the quantity.
139
                $totalPrice = round($orderProduct->getPrice() * $orderProduct->getQuantity(), 2);
140
                $totalPromoPrice = round($orderProduct->getPromoPrice() * $orderProduct->getQuantity(), 2);
141
            }
142
143
            $totalTaxedPrice = round($taxedPrice * $orderProduct->getQuantity(), 2);
144
            $totalTaxedPromoPrice = round($taxedPromoPrice * $orderProduct->getQuantity(), 2);
145
146
            $loopResultRow->set('ID', $orderProduct->getId())
147
                ->set('REF', $orderProduct->getProductRef())
148
                ->set('PRODUCT_ID', $orderProduct->getVirtualColumn('product_id'))
149
                ->set('PRODUCT_SALE_ELEMENTS_ID', $orderProduct->getProductSaleElementsId())
150
                ->set('PRODUCT_SALE_ELEMENTS_REF', $orderProduct->getProductSaleElementsRef())
151
                ->set('WAS_NEW', $orderProduct->getWasNew() === 1 ? 1 : 0)
152
                ->set('WAS_IN_PROMO', $orderProduct->getWasInPromo() === 1 ? 1 : 0)
153
                ->set('WEIGHT', $orderProduct->getWeight())
154
                ->set('TITLE', $orderProduct->getTitle())
155
                ->set('CHAPO', $orderProduct->getChapo())
156
                ->set('DESCRIPTION', $orderProduct->getDescription())
157
                ->set('POSTSCRIPTUM', $orderProduct->getPostscriptum())
158
                ->set('VIRTUAL', $orderProduct->getVirtual())
159
                ->set('VIRTUAL_DOCUMENT', $orderProduct->getVirtualDocument())
160
                ->set('QUANTITY', $orderProduct->getQuantity())
161
162
                ->set('PRICE', $orderProduct->getPrice())
163
                ->set('PRICE_TAX', $tax)
164
                ->set('TAXED_PRICE', $taxedPrice)
165
                ->set('PROMO_PRICE', $orderProduct->getPromoPrice())
166
                ->set('PROMO_PRICE_TAX', $promoTax)
167
                ->set('TAXED_PROMO_PRICE', $taxedPromoPrice)
168
                ->set('TOTAL_PRICE', $totalPrice)
169
                ->set('TOTAL_TAXED_PRICE', $totalTaxedPrice)
170
                ->set('TOTAL_PROMO_PRICE', $totalPromoPrice)
171
                ->set('TOTAL_TAXED_PROMO_PRICE', $totalTaxedPromoPrice)
172
173
                ->set('TAX_RULE_TITLE', $orderProduct->getTaxRuleTitle())
174
                ->set('TAX_RULE_DESCRIPTION', $orderProduct->getTaxRuledescription())
175
                ->set('PARENT', $orderProduct->getParent())
176
                ->set('EAN_CODE', $orderProduct->getEanCode())
177
                ->set('CART_ITEM_ID', $orderProduct->getCartItemId())
178
179
                ->set('REAL_PRICE', $orderProduct->getWasInPromo() ? $orderProduct->getPromoPrice() : $orderProduct->getPrice())
180
                ->set('REAL_TAXED_PRICE', $orderProduct->getWasInPromo() ? $taxedPromoPrice : $taxedPrice)
181
                ->set('REAL_PRICE_TAX', $orderProduct->getWasInPromo() ? $promoTax : $tax)
182
183
                ->set('REAL_TOTAL_PRICE', $orderProduct->getWasInPromo() ? $totalPromoPrice : $totalPrice)
184
                ->set('REAL_TOTAL_TAXED_PRICE', $orderProduct->getWasInPromo() ? $totalTaxedPromoPrice : $totalTaxedPrice)
185
                ->set('REAL_TOTAL_PRICE_TAX', $orderProduct->getWasInPromo() ? $totalPromoTax : $totalTax)
186
187
            ;
188
            $this->addOutputFields($loopResultRow, $orderProduct);
189
190
            $loopResult->addRow($loopResultRow);
191
        }
192
193
        return $loopResult;
194
    }
195
}
196