/
OrderExport.php
88 lines (82 loc) · 2.66 KB
/
OrderExport.php
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
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\commerce\exports;
use Craft;
use craft\base\ElementExporter;
use craft\commerce\adjusters\Discount;
use craft\commerce\adjusters\Shipping;
use craft\commerce\adjusters\Tax;
use craft\commerce\db\Table;
use craft\db\Query as CraftQuery;
use craft\elements\db\ElementQueryInterface;
class OrderExport extends ElementExporter
{
/**
* @inheritDoc
*/
public static function displayName(): string
{
return Craft::t('commerce', 'Orders (Legacy)');
}
/**
* @inheritDoc
*/
public function export(ElementQueryInterface $query): mixed
{
$orderIds = $query->ids();
$columns = [
'id',
'number',
'email',
'gatewayId',
'paymentSourceId',
'customerId',
'orderStatusId',
'couponCode',
'itemTotal',
'totalTax' => (new CraftQuery())
->select('SUM([[amount]])')
->from(Table::ORDERADJUSTMENTS)
->where('[[orderId]] = ' . Table::ORDERS . '.[[id]]')
->andWhere(['type' => Tax::ADJUSTMENT_TYPE])
->andWhere(['included' => 0]),
'totalTaxIncluded' => (new CraftQuery())
->select('SUM([[amount]])')
->from(Table::ORDERADJUSTMENTS)
->where('[[orderId]] = ' . Table::ORDERS . '.[[id]]')
->andWhere(['type' => Tax::ADJUSTMENT_TYPE])
->andWhere(['included' => 1]),
'totalShipping' => (new CraftQuery())
->select('SUM([[amount]])')
->from(Table::ORDERADJUSTMENTS)
->where('[[orderId]] = ' . Table::ORDERS . '.[[id]]')
->andWhere(['type' => Shipping::ADJUSTMENT_TYPE]),
'totalDiscount' => (new CraftQuery())
->select('SUM([[amount]])')
->from(Table::ORDERADJUSTMENTS)
->where('[[orderId]] = ' . Table::ORDERS . '.[[id]]')
->andWhere(['type' => Discount::ADJUSTMENT_TYPE]),
'totalPrice',
'totalPaid',
'paidStatus',
'isCompleted',
'dateOrdered',
'datePaid',
'currency',
'paymentCurrency',
'lastIp',
'orderLanguage',
'message',
'shippingMethodHandle',
];
return (new CraftQuery())
->select($columns)
->from(Table::ORDERS)
->where(['id' => $orderIds])
->all();
}
}