OrderRepository::getAddressModel()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
namespace Hideyo\Ecommerce\Framework\Services\Order\Entity;
3
 
4
use Hideyo\Ecommerce\Framework\Services\Order\Entity\Order;
5
use Hideyo\Ecommerce\Framework\Services\Order\Entity\OrderProduct;
6
use Hideyo\Ecommerce\Framework\Services\Order\Entity\OrderAddress;
7
use Hideyo\Ecommerce\Framework\Services\Order\Entity\OrderSendingMethod;
8
use Hideyo\Ecommerce\Framework\Services\Order\Entity\OrderPaymentMethod;
9
use DB;
10
use Carbon\Carbon;
11
use Hideyo\Ecommerce\Framework\Services\BaseRepository;
12
 
13
class OrderRepository extends BaseRepository 
14
{
15
    protected $model;
16
17
    public function __construct(
18
        Order $model,
19
        OrderProduct $modelOrderProduct,
20
        OrderAddress $modelOrderAddress,
21
        OrderSendingMethod $modelOrderSendingMethod,
22
        OrderPaymentMethod $modelOrderPaymentMethod
23
    ) {
24
        $this->model = $model;
25
        $this->modelOrderProduct = $modelOrderProduct;
0 ignored issues
show
Bug Best Practice introduced by
The property modelOrderProduct does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
26
        $this->modelOrderAddress = $modelOrderAddress;
0 ignored issues
show
Bug Best Practice introduced by
The property modelOrderAddress does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
27
        $this->modelOrderSendingMethod = $modelOrderSendingMethod;
0 ignored issues
show
Bug Best Practice introduced by
The property modelOrderSendingMethod does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
28
        $this->modelOrderPaymentMethod = $modelOrderPaymentMethod;
0 ignored issues
show
Bug Best Practice introduced by
The property modelOrderPaymentMethod does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
29
    }
30
  
31
    public function getProductModel()
32
    {
33
        return $this->modelOrderProduct;
34
    }
35
36
    public function getAddressModel()
37
    {
38
        return $this->modelOrderAddress;
39
    }
40
41
    public function getOrderSendingMethodModel()
42
    {
43
        return $this->modelOrderSendingMethod;
44
    }
45
46
    public function getOrderPaymentMethodModel()
47
    {
48
        return $this->modelOrderPaymentMethod;
49
    }
50
51
    public function selectAllByShopIdAndStatusId($orderStatusId, $startDate = false, $endDate = false, $shopId = false)
0 ignored issues
show
Unused Code introduced by
The parameter $shopId is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

51
    public function selectAllByShopIdAndStatusId($orderStatusId, $startDate = false, $endDate = false, /** @scrutinizer ignore-unused */ $shopId = false)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
52
    {
53
        $query = $this->model
54
        ->where('shop_id', auth('hideyobackend')->user()->selected_shop_id)
0 ignored issues
show
Bug introduced by
Accessing selected_shop_id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
55
        ->where('order_status_id', $orderStatusId);
56
57
        if ($startDate) {
58
            $dt = Carbon::createFromFormat('d/m/Y', $startDate);
59
            $query->where('created_at', '>=', $dt->toDateString('Y-m-d'));
0 ignored issues
show
Unused Code introduced by
The call to Carbon\Carbon::toDateString() has too many arguments starting with 'Y-m-d'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

59
            $query->where('created_at', '>=', $dt->/** @scrutinizer ignore-call */ toDateString('Y-m-d'));

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
60
        }
61
62
        if ($endDate) {
63
            $dt = Carbon::createFromFormat('d/m/Y', $endDate);
64
            $query->where('created_at', '<=', $dt->toDateString('Y-m-d'));
65
        }
66
67
        $query->orderBy('created_at', 'ASC');
68
        return $query->get();
69
    }
70
71
    public function selectAllByAllProductsAndProductCategoryId($productCategoryId)
72
    {
73
        return $this->model->select('extra_field.*')->leftJoin('product_category_related_extra_field', 'extra_field.id', '=', 'product_category_related_extra_field.extra_field_id')->where('all_products', '=', 1)->orWhere('product_category_related_extra_field.product_category_id', '=', $productCategoryId)->get();
74
    }
75
76
    public function orderProductsByClientId($clientId, $shopId)
77
    {
78
        return $this->modelOrderProduct->with(array('product'))->whereHas('Order', function ($query) use ($clientId, $shopId) {
79
            $query->where('client_id', '=', $clientId)->where('shop_id', $shopId);
80
        });
81
    }
82
83
    public function selectAllByCompany()
84
    {
85
        return $this->model->leftJoin('shop', 'order.shop_id', '=', 'shop.id')->get();
86
    }
87
88
    public function productsByOrderIds(array $orderIds) 
89
    {
90
        $result = DB::table('order_product')
91
        ->select(DB::raw('DISTINCT(CONCAT_WS(\' - \',order_product.title, IFNULL(order_product.product_attribute_title, \'\'))) as title, order_product.product_attribute_title, order_product.reference_code, order_product.price_with_tax, order_product.price_without_tax,  SUM(order_product.amount) as total_amount'))
92
        ->whereIn('order_product.order_id', $orderIds)
93
        ->whereNotNull('order_product.product_attribute_title')
94
        ->groupBy('order_product.title', 'order_product.product_attribute_title')
95
        ->get();
96
97
98
        $result2 = DB::table('order_product')
99
        ->select(DB::raw('DISTINCT(order_product.title) as title, order_product.product_attribute_title, order_product.reference_code, order_product.price_with_tax, order_product.price_without_tax,  SUM(order_product.amount) as total_amount'))
100
        ->whereIn('order_product.order_id', $orderIds)
101
        ->whereNull('order_product.product_attribute_title')
102
        ->groupBy('order_product.title', 'order_product.product_attribute_title')
103
        ->get();
104
105
        $result = array_merge($result, $result2);
106
        return $result;
107
    }
108
}