Completed
Branch BUG/11126/attendee-mover-tax (123043)
by
unknown
41:01 queued 27:47
created
core/db_models/EEM_Line_Item.model.php 1 patch
Indentation   +388 added lines, -388 removed lines patch added patch discarded remove patch
@@ -41,397 +41,397 @@
 block discarded – undo
41 41
 class EEM_Line_Item extends EEM_Base
42 42
 {
43 43
 
44
-    /**
45
-     * Tax sub-total is just the total of all the taxes, which should be children
46
-     * of this line item. There should only ever be one tax sub-total, and it should
47
-     * be a direct child of. Its quantity and LIN_unit_price = 1.
48
-     */
49
-    const type_tax_sub_total = 'tax-sub-total';
50
-
51
-    /**
52
-     * Tax line items indicate a tax applied to all the taxable line items.
53
-     * Should not have any children line items. Its LIN_unit_price = 0. Its LIN_percent is a percent, not a decimal
54
-     * (eg 10% tax = 10, not 0.1). Its LIN_total = LIN_unit_price * pre-tax-total. Quantity = 1.
55
-     */
56
-    const type_tax = 'tax';
57
-
58
-    /**
59
-     * Indicating individual items purchased, or discounts or surcharges.
60
-     * The sum of all the regular line items  plus the tax items should equal
61
-     * the grand total.
62
-     * Possible children are sub-line-items and cancellations.
63
-     * For flat items, LIN_unit_price * LIN_quantity = LIN_total. Its LIN_total is the sum of all the children
64
-     * LIN_totals. Its LIN_percent = 0.
65
-     * For percent items, its LIN_unit_price = 0. Its LIN_percent is a percent, not a decimal (eg 10% = 10, not 0.1).
66
-     * Its LIN_total is LIN_percent / 100 * sum of lower-priority sibling line items. Quantity = 1.
67
-     */
68
-    const type_line_item = 'line-item';
69
-
70
-    /**
71
-     * Line item indicating all the factors that make a single line item.
72
-     * Sub-line items should have NO children line items.
73
-     * For flat sub-items, their quantity should match their parent item, their LIN_unit_price should be this sub-item's
74
-     * contribution towards the price of ONE of their parent items, and its LIN_total should be
75
-     *  = LIN_quantity * LIN_unit_price. Its LIN_percent = 0.
76
-     * For percent sub-items, the quantity should be 1, LIN_unit_price should be 0, and its LIN_total should
77
-     * = LIN_percent / 100 * sum of lower-priority sibling line items..
78
-     */
79
-    const type_sub_line_item = 'sub-item';
80
-
81
-    /**
82
-     * Line item indicating a sub-total (eg total for an event, or pre-tax subtotal).
83
-     * Direct children should be event subtotals.
84
-     * Should have quantity of 1, and a LIN_total and LIN_unit_price of the sum of all its sub-items' LIN_totals.
85
-     *
86
-     */
87
-    const type_sub_total = 'sub-total';
88
-
89
-    /**
90
-     * Line item for the grand total of an order. Its direct children
91
-     * should be tax subtotals and (pre-tax) subtotals, and possibly a regular line item
92
-     * indicating a transaction-wide discount/surcharge. Should have a quantity of 1, a LIN_total and LIN_unit_price of
93
-     * the entire order's mount.
94
-     */
95
-    const type_total = 'total';
96
-
97
-    /**
98
-     * When a line item is cancelled, a sub-line-item of type 'cancellation'
99
-     * should be created, indicating the quantity that were cancelled
100
-     * (because a line item could have a quantity of 1, and its cancellation item
101
-     * could be for 3, indicating that originally 4 were purchased, but 3 have been
102
-     * cancelled, and only one remains).
103
-     * When items are refunded, a cancellation line item should be made, which points
104
-     * to teh payment model object which actually refunded the payment.
105
-     * Cancellations should NOT have any children line items; the should NOT affect
106
-     * any calculations, and are only meant as a record that cancellations have occurred.
107
-     * Their LIN_percent should be 0.
108
-     */
109
-    const type_cancellation = 'cancellation';
110
-
111
-    // private instance of the EEM_Line_Item object
112
-    protected static $_instance = NULL;
113
-
114
-
115
-    /**
116
-     *        private constructor to prevent direct creation
117
-     * @Constructor
118
-     * @access protected
119
-     * @param string $timezone string representing the timezone we want to set for returned Date Time Strings (and any incoming timezone data that gets saved).  Note this just sends the timezone info to the date time model field objects.  Default is NULL (and will be assumed using the set timezone in the 'timezone_string' wp option)
120
-     * @return \EEM_Line_Item
121
-     */
122
-    protected function __construct($timezone)
123
-    {
124
-        $this->singular_item = __('Line Item', 'event_espresso');
125
-        $this->plural_item = __('Line Items', 'event_espresso');
126
-
127
-        $this->_tables = array(
128
-            'Line_Item' => new EE_Primary_Table('esp_line_item', 'LIN_ID')
129
-        );
130
-        $line_items_can_be_for = apply_filters('FHEE__EEM_Line_Item__line_items_can_be_for', array('Ticket', 'Price', 'Event'));
131
-        $this->_fields = array(
132
-            'Line_Item' => array(
133
-                'LIN_ID' => new EE_Primary_Key_Int_Field('LIN_ID', __("ID", "event_espresso")),
134
-                'LIN_code' => new EE_Slug_Field('LIN_code', __("Code for index into Cart", "event_espresso"), TRUE),
135
-                'TXN_ID' => new EE_Foreign_Key_Int_Field('TXN_ID', __("Transaction ID", "event_espresso"), TRUE, NULL, 'Transaction'),
136
-                'LIN_name' => new EE_Full_HTML_Field('LIN_name', __("Line Item Name", "event_espresso"), FALSE, ''),
137
-                'LIN_desc' => new EE_Full_HTML_Field('LIN_desc', __("Line Item Description", "event_espresso"), TRUE),
138
-                'LIN_unit_price' => new EE_Money_Field('LIN_unit_price', __("Unit Price", "event_espresso"), FALSE, 0),
139
-                'LIN_percent' => new EE_Float_Field('LIN_percent', __("Percent", "event_espresso"), FALSE, 0),
140
-                'LIN_is_taxable' => new EE_Boolean_Field('LIN_is_taxable', __("Taxable", "event_espresso"), FALSE, FALSE),
141
-                'LIN_order' => new EE_Integer_Field('LIN_order', __("Order of Application towards total of parent", "event_espresso"), FALSE, 1),
142
-                'LIN_total' => new EE_Money_Field('LIN_total', __("Total (unit price x quantity)", "event_espresso"), FALSE, 0),
143
-                'LIN_quantity' => new EE_Integer_Field('LIN_quantity', __("Quantity", "event_espresso"), TRUE, 1),
144
-                'LIN_parent' => new EE_Integer_Field('LIN_parent', __("Parent ID (this item goes towards that Line Item's total)", "event_espresso"), TRUE, NULL),
145
-                'LIN_type' => new EE_Enum_Text_Field('LIN_type', __("Type", "event_espresso"), FALSE, 'line-item', array(
146
-                        self::type_line_item => __("Line Item", "event_espresso"),
147
-                        self::type_sub_line_item => __("Sub-Item", "event_espresso"),
148
-                        self::type_sub_total => __("Subtotal", "event_espresso"),
149
-                        self::type_tax_sub_total => __("Tax Subtotal", "event_espresso"),
150
-                        self::type_tax => __("Tax", "event_espresso"),
151
-                        self::type_total => __("Total", "event_espresso"),
152
-                        self::type_cancellation => __('Cancellation', 'event_espresso')
153
-                    )
154
-                ),
155
-                'OBJ_ID' => new EE_Foreign_Key_Int_Field('OBJ_ID', __('ID of Item purchased.', 'event_espresso'), TRUE, NULL, $line_items_can_be_for),
156
-                'OBJ_type' => new EE_Any_Foreign_Model_Name_Field('OBJ_type', __("Model Name this Line Item is for", "event_espresso"), TRUE, NULL, $line_items_can_be_for),
157
-                'LIN_timestamp' => new EE_Datetime_Field('LIN_timestamp', __('When the line item was created', 'event_espresso'), false, EE_Datetime_Field::now, $timezone),
158
-            )
159
-        );
160
-        $this->_model_relations = array(
161
-            'Transaction' => new EE_Belongs_To_Relation(),
162
-            'Ticket' => new EE_Belongs_To_Any_Relation(),
163
-            'Price' => new EE_Belongs_To_Any_Relation(),
164
-            'Event' => new EE_Belongs_To_Any_Relation()
165
-        );
166
-        $this->_model_chain_to_wp_user = 'Transaction.Registration.Event';
167
-        $this->_caps_slug = 'transactions';
168
-        parent::__construct($timezone);
169
-    }
170
-
171
-
172
-    /**
173
-     * Gets all the line items for this transaction of the given type
174
-     * @param string $line_item_type like one of EEM_Line_Item::type_*
175
-     * @param EE_Transaction|int $transaction
176
-     * @return EE_Line_Item[]
177
-     */
178
-    public function get_all_of_type_for_transaction($line_item_type, $transaction)
179
-    {
180
-        $transaction = EEM_Transaction::instance()->ensure_is_ID($transaction);
181
-        return $this->get_all(array(array(
182
-            'LIN_type' => $line_item_type,
183
-            'TXN_ID' => $transaction
184
-        )));
185
-    }
186
-
187
-
188
-    /**
189
-     * Gets all line items unrelated to tickets that are normal line items
190
-     * (eg shipping, promotions, and miscellaneous other stuff should probably fit in this category)
191
-     * @param EE_Transaction|int $transaction
192
-     * @return EE_Line_Item[]
193
-     */
194
-    public function get_all_non_ticket_line_items_for_transaction($transaction)
195
-    {
196
-        $transaction = EEM_Transaction::instance()->ensure_is_ID($transaction);
197
-        return $this->get_all(array(array(
198
-            'LIN_type' => self::type_line_item,
199
-            'TXN_ID' => $transaction,
200
-            'OR' => array(
201
-                'OBJ_type*notticket' => array('!=', 'Ticket'),
202
-                'OBJ_type*null' => array('IS_NULL'))
203
-        )));
204
-    }
205
-
206
-    /**
207
-     * Deletes line items with no transaction who have passed the transaction cutoff time.
208
-     * This needs to be very efficient
209
-     * because if there are spam bots afoot there will be LOTS of line items
210
-     * @return int count of how many deleted
211
-     */
212
-    public function delete_line_items_with_no_transaction()
213
-    {
214
-        /** @type WPDB $wpdb */
215
-        global $wpdb;
216
-        $time_to_leave_alone = apply_filters(
217
-            'FHEE__EEM_Line_Item__delete_line_items_with_no_transaction__time_to_leave_alone', WEEK_IN_SECONDS
218
-        );
219
-        $query = $wpdb->prepare(
220
-            'DELETE li
44
+	/**
45
+	 * Tax sub-total is just the total of all the taxes, which should be children
46
+	 * of this line item. There should only ever be one tax sub-total, and it should
47
+	 * be a direct child of. Its quantity and LIN_unit_price = 1.
48
+	 */
49
+	const type_tax_sub_total = 'tax-sub-total';
50
+
51
+	/**
52
+	 * Tax line items indicate a tax applied to all the taxable line items.
53
+	 * Should not have any children line items. Its LIN_unit_price = 0. Its LIN_percent is a percent, not a decimal
54
+	 * (eg 10% tax = 10, not 0.1). Its LIN_total = LIN_unit_price * pre-tax-total. Quantity = 1.
55
+	 */
56
+	const type_tax = 'tax';
57
+
58
+	/**
59
+	 * Indicating individual items purchased, or discounts or surcharges.
60
+	 * The sum of all the regular line items  plus the tax items should equal
61
+	 * the grand total.
62
+	 * Possible children are sub-line-items and cancellations.
63
+	 * For flat items, LIN_unit_price * LIN_quantity = LIN_total. Its LIN_total is the sum of all the children
64
+	 * LIN_totals. Its LIN_percent = 0.
65
+	 * For percent items, its LIN_unit_price = 0. Its LIN_percent is a percent, not a decimal (eg 10% = 10, not 0.1).
66
+	 * Its LIN_total is LIN_percent / 100 * sum of lower-priority sibling line items. Quantity = 1.
67
+	 */
68
+	const type_line_item = 'line-item';
69
+
70
+	/**
71
+	 * Line item indicating all the factors that make a single line item.
72
+	 * Sub-line items should have NO children line items.
73
+	 * For flat sub-items, their quantity should match their parent item, their LIN_unit_price should be this sub-item's
74
+	 * contribution towards the price of ONE of their parent items, and its LIN_total should be
75
+	 *  = LIN_quantity * LIN_unit_price. Its LIN_percent = 0.
76
+	 * For percent sub-items, the quantity should be 1, LIN_unit_price should be 0, and its LIN_total should
77
+	 * = LIN_percent / 100 * sum of lower-priority sibling line items..
78
+	 */
79
+	const type_sub_line_item = 'sub-item';
80
+
81
+	/**
82
+	 * Line item indicating a sub-total (eg total for an event, or pre-tax subtotal).
83
+	 * Direct children should be event subtotals.
84
+	 * Should have quantity of 1, and a LIN_total and LIN_unit_price of the sum of all its sub-items' LIN_totals.
85
+	 *
86
+	 */
87
+	const type_sub_total = 'sub-total';
88
+
89
+	/**
90
+	 * Line item for the grand total of an order. Its direct children
91
+	 * should be tax subtotals and (pre-tax) subtotals, and possibly a regular line item
92
+	 * indicating a transaction-wide discount/surcharge. Should have a quantity of 1, a LIN_total and LIN_unit_price of
93
+	 * the entire order's mount.
94
+	 */
95
+	const type_total = 'total';
96
+
97
+	/**
98
+	 * When a line item is cancelled, a sub-line-item of type 'cancellation'
99
+	 * should be created, indicating the quantity that were cancelled
100
+	 * (because a line item could have a quantity of 1, and its cancellation item
101
+	 * could be for 3, indicating that originally 4 were purchased, but 3 have been
102
+	 * cancelled, and only one remains).
103
+	 * When items are refunded, a cancellation line item should be made, which points
104
+	 * to teh payment model object which actually refunded the payment.
105
+	 * Cancellations should NOT have any children line items; the should NOT affect
106
+	 * any calculations, and are only meant as a record that cancellations have occurred.
107
+	 * Their LIN_percent should be 0.
108
+	 */
109
+	const type_cancellation = 'cancellation';
110
+
111
+	// private instance of the EEM_Line_Item object
112
+	protected static $_instance = NULL;
113
+
114
+
115
+	/**
116
+	 *        private constructor to prevent direct creation
117
+	 * @Constructor
118
+	 * @access protected
119
+	 * @param string $timezone string representing the timezone we want to set for returned Date Time Strings (and any incoming timezone data that gets saved).  Note this just sends the timezone info to the date time model field objects.  Default is NULL (and will be assumed using the set timezone in the 'timezone_string' wp option)
120
+	 * @return \EEM_Line_Item
121
+	 */
122
+	protected function __construct($timezone)
123
+	{
124
+		$this->singular_item = __('Line Item', 'event_espresso');
125
+		$this->plural_item = __('Line Items', 'event_espresso');
126
+
127
+		$this->_tables = array(
128
+			'Line_Item' => new EE_Primary_Table('esp_line_item', 'LIN_ID')
129
+		);
130
+		$line_items_can_be_for = apply_filters('FHEE__EEM_Line_Item__line_items_can_be_for', array('Ticket', 'Price', 'Event'));
131
+		$this->_fields = array(
132
+			'Line_Item' => array(
133
+				'LIN_ID' => new EE_Primary_Key_Int_Field('LIN_ID', __("ID", "event_espresso")),
134
+				'LIN_code' => new EE_Slug_Field('LIN_code', __("Code for index into Cart", "event_espresso"), TRUE),
135
+				'TXN_ID' => new EE_Foreign_Key_Int_Field('TXN_ID', __("Transaction ID", "event_espresso"), TRUE, NULL, 'Transaction'),
136
+				'LIN_name' => new EE_Full_HTML_Field('LIN_name', __("Line Item Name", "event_espresso"), FALSE, ''),
137
+				'LIN_desc' => new EE_Full_HTML_Field('LIN_desc', __("Line Item Description", "event_espresso"), TRUE),
138
+				'LIN_unit_price' => new EE_Money_Field('LIN_unit_price', __("Unit Price", "event_espresso"), FALSE, 0),
139
+				'LIN_percent' => new EE_Float_Field('LIN_percent', __("Percent", "event_espresso"), FALSE, 0),
140
+				'LIN_is_taxable' => new EE_Boolean_Field('LIN_is_taxable', __("Taxable", "event_espresso"), FALSE, FALSE),
141
+				'LIN_order' => new EE_Integer_Field('LIN_order', __("Order of Application towards total of parent", "event_espresso"), FALSE, 1),
142
+				'LIN_total' => new EE_Money_Field('LIN_total', __("Total (unit price x quantity)", "event_espresso"), FALSE, 0),
143
+				'LIN_quantity' => new EE_Integer_Field('LIN_quantity', __("Quantity", "event_espresso"), TRUE, 1),
144
+				'LIN_parent' => new EE_Integer_Field('LIN_parent', __("Parent ID (this item goes towards that Line Item's total)", "event_espresso"), TRUE, NULL),
145
+				'LIN_type' => new EE_Enum_Text_Field('LIN_type', __("Type", "event_espresso"), FALSE, 'line-item', array(
146
+						self::type_line_item => __("Line Item", "event_espresso"),
147
+						self::type_sub_line_item => __("Sub-Item", "event_espresso"),
148
+						self::type_sub_total => __("Subtotal", "event_espresso"),
149
+						self::type_tax_sub_total => __("Tax Subtotal", "event_espresso"),
150
+						self::type_tax => __("Tax", "event_espresso"),
151
+						self::type_total => __("Total", "event_espresso"),
152
+						self::type_cancellation => __('Cancellation', 'event_espresso')
153
+					)
154
+				),
155
+				'OBJ_ID' => new EE_Foreign_Key_Int_Field('OBJ_ID', __('ID of Item purchased.', 'event_espresso'), TRUE, NULL, $line_items_can_be_for),
156
+				'OBJ_type' => new EE_Any_Foreign_Model_Name_Field('OBJ_type', __("Model Name this Line Item is for", "event_espresso"), TRUE, NULL, $line_items_can_be_for),
157
+				'LIN_timestamp' => new EE_Datetime_Field('LIN_timestamp', __('When the line item was created', 'event_espresso'), false, EE_Datetime_Field::now, $timezone),
158
+			)
159
+		);
160
+		$this->_model_relations = array(
161
+			'Transaction' => new EE_Belongs_To_Relation(),
162
+			'Ticket' => new EE_Belongs_To_Any_Relation(),
163
+			'Price' => new EE_Belongs_To_Any_Relation(),
164
+			'Event' => new EE_Belongs_To_Any_Relation()
165
+		);
166
+		$this->_model_chain_to_wp_user = 'Transaction.Registration.Event';
167
+		$this->_caps_slug = 'transactions';
168
+		parent::__construct($timezone);
169
+	}
170
+
171
+
172
+	/**
173
+	 * Gets all the line items for this transaction of the given type
174
+	 * @param string $line_item_type like one of EEM_Line_Item::type_*
175
+	 * @param EE_Transaction|int $transaction
176
+	 * @return EE_Line_Item[]
177
+	 */
178
+	public function get_all_of_type_for_transaction($line_item_type, $transaction)
179
+	{
180
+		$transaction = EEM_Transaction::instance()->ensure_is_ID($transaction);
181
+		return $this->get_all(array(array(
182
+			'LIN_type' => $line_item_type,
183
+			'TXN_ID' => $transaction
184
+		)));
185
+	}
186
+
187
+
188
+	/**
189
+	 * Gets all line items unrelated to tickets that are normal line items
190
+	 * (eg shipping, promotions, and miscellaneous other stuff should probably fit in this category)
191
+	 * @param EE_Transaction|int $transaction
192
+	 * @return EE_Line_Item[]
193
+	 */
194
+	public function get_all_non_ticket_line_items_for_transaction($transaction)
195
+	{
196
+		$transaction = EEM_Transaction::instance()->ensure_is_ID($transaction);
197
+		return $this->get_all(array(array(
198
+			'LIN_type' => self::type_line_item,
199
+			'TXN_ID' => $transaction,
200
+			'OR' => array(
201
+				'OBJ_type*notticket' => array('!=', 'Ticket'),
202
+				'OBJ_type*null' => array('IS_NULL'))
203
+		)));
204
+	}
205
+
206
+	/**
207
+	 * Deletes line items with no transaction who have passed the transaction cutoff time.
208
+	 * This needs to be very efficient
209
+	 * because if there are spam bots afoot there will be LOTS of line items
210
+	 * @return int count of how many deleted
211
+	 */
212
+	public function delete_line_items_with_no_transaction()
213
+	{
214
+		/** @type WPDB $wpdb */
215
+		global $wpdb;
216
+		$time_to_leave_alone = apply_filters(
217
+			'FHEE__EEM_Line_Item__delete_line_items_with_no_transaction__time_to_leave_alone', WEEK_IN_SECONDS
218
+		);
219
+		$query = $wpdb->prepare(
220
+			'DELETE li
221 221
 				FROM ' . $this->table() . ' li
222 222
 				LEFT JOIN ' . EEM_Transaction::instance()->table() . ' t ON li.TXN_ID = t.TXN_ID
223 223
 				WHERE t.TXN_ID IS NULL AND li.LIN_timestamp < %s',
224
-            // use GMT time because that's what TXN_timestamps are in
225
-            date('Y-m-d H:i:s', time() - $time_to_leave_alone)
226
-        );
227
-        return $wpdb->query($query);
228
-    }
229
-
230
-
231
-    /**
232
-     * get_line_item_for_transaction_object
233
-     * Gets a transaction's line item record for a specific object such as a EE_Event or EE_Ticket
234
-     *
235
-     * @param int $TXN_ID
236
-     * @param \EE_Base_Class $object
237
-     * @return EE_Line_Item[]
238
-     */
239
-    public function get_line_item_for_transaction_object($TXN_ID, EE_Base_Class $object)
240
-    {
241
-        return $this->get_all(array(array(
242
-            'TXN_ID' => $TXN_ID,
243
-            'OBJ_type' => str_replace('EE_', '', get_class($object)),
244
-            'OBJ_ID' => $object->ID()
245
-        )));
246
-    }
247
-
248
-
249
-    /**
250
-     * get_object_line_items_for_transaction
251
-     * Gets all of the the object line items for a transaction, based on an object type plus an array of object IDs
252
-     *
253
-     * @param int $TXN_ID
254
-     * @param string $OBJ_type
255
-     * @param array $OBJ_IDs
256
-     * @return EE_Line_Item[]
257
-     */
258
-    public function get_object_line_items_for_transaction($TXN_ID, $OBJ_type = 'Event', $OBJ_IDs = array())
259
-    {
260
-        $query_params = array(
261
-            'OBJ_type' => $OBJ_type,
262
-            // if incoming $OBJ_IDs is an array, then make sure it is formatted correctly for the query
263
-            'OBJ_ID' => is_array($OBJ_IDs) && !isset($OBJ_IDs['IN']) ? array('IN', $OBJ_IDs) : $OBJ_IDs
264
-        );
265
-        if ($TXN_ID) {
266
-            $query_params['TXN_ID'] = $TXN_ID;
267
-        }
268
-        return $this->get_all(array($query_params));
269
-    }
270
-
271
-
272
-    /**
273
-     * get_all_ticket_line_items_for_transaction
274
-     *
275
-     * @param EE_Transaction $transaction
276
-     * @return EE_Line_Item[]
277
-     */
278
-    public function get_all_ticket_line_items_for_transaction(EE_Transaction $transaction)
279
-    {
280
-        return $this->get_all(array(
281
-            array(
282
-                'TXN_ID' => $transaction->ID(),
283
-                'OBJ_type' => 'Ticket',
284
-            )
285
-        ));
286
-    }
287
-
288
-
289
-    /**
290
-     * get_ticket_line_item_for_transaction
291
-     *
292
-     * @param int $TXN_ID
293
-     * @param int $TKT_ID
294
-     * @return \EE_Line_Item
295
-     */
296
-    public function get_ticket_line_item_for_transaction($TXN_ID, $TKT_ID)
297
-    {
298
-        return $this->get_one(array(
299
-            array(
300
-                'TXN_ID' => EEM_Transaction::instance()->ensure_is_ID($TXN_ID),
301
-                'OBJ_ID' => $TKT_ID,
302
-                'OBJ_type' => 'Ticket',
303
-            )
304
-        ));
305
-    }
306
-
307
-
308
-    /**
309
-     * get_existing_promotion_line_item
310
-     * searches the cart for existing line items for the specified promotion
311
-     *
312
-     * @since   1.0.0
313
-     *
314
-     * @param EE_Line_Item $parent_line_item
315
-     * @param EE_Promotion $promotion
316
-     * @return EE_Line_Item
317
-     */
318
-    public function get_existing_promotion_line_item(EE_Line_Item $parent_line_item, EE_Promotion $promotion)
319
-    {
320
-        return $this->get_one(array(
321
-            array(
322
-                'TXN_ID' => $parent_line_item->TXN_ID(),
323
-                'LIN_parent' => $parent_line_item->ID(),
324
-                'OBJ_type' => 'Promotion',
325
-                'OBJ_ID' => $promotion->ID()
326
-            )
327
-        ));
328
-    }
329
-
330
-
331
-    /**
332
-     * get_all_promotion_line_items
333
-     * searches the cart for any and all existing promotion line items
334
-     *
335
-     * @since   1.0.0
336
-     *
337
-     * @param EE_Line_Item $parent_line_item
338
-     * @return EE_Line_Item[]
339
-     */
340
-    public function get_all_promotion_line_items(EE_Line_Item $parent_line_item)
341
-    {
342
-        return $this->get_all(array(
343
-            array(
344
-                'TXN_ID' => $parent_line_item->TXN_ID(),
345
-                'LIN_parent' => $parent_line_item->ID(),
346
-                'OBJ_type' => 'Promotion'
347
-            )
348
-        ));
349
-    }
350
-
351
-    /**
352
-     * Gets the registration's corresponding line item.
353
-     * Note: basically does NOT support having multiple line items for a single ticket,
354
-     * which would happen if some of the registrations had a price modifier while others didn't.
355
-     * In order to support that, we'd probably need a LIN_ID on registrations or something.
356
-     * @param EE_Registration $registration
357
-     * @return EE_Line_ITem
358
-     */
359
-    public function get_line_item_for_registration(EE_Registration $registration)
360
-    {
361
-        return $this->get_one($this->line_item_for_registration_query_params($registration));
362
-    }
363
-
364
-    /**
365
-     * Gets the query params used to retrieve a specific line item for the given registration
366
-     * @param EE_Registration $registration
367
-     * @param array $original_query_params any extra query params you'd like to be merged with
368
-     * @return array like EEM_Base::get_all()'s $query_params
369
-     */
370
-    public function line_item_for_registration_query_params(EE_Registration $registration, $original_query_params = array())
371
-    {
372
-        return array_replace_recursive($original_query_params, array(
373
-            array(
374
-                'OBJ_ID' => $registration->ticket_ID(),
375
-                'OBJ_type' => 'Ticket',
376
-                'TXN_ID' => $registration->transaction_ID()
377
-            )
378
-        ));
379
-    }
380
-
381
-
382
-    /**
383
-     * @return EE_Base_Class[]|EE_Line_Item[]
384
-     * @throws \EE_Error
385
-     */
386
-    public function get_total_line_items_with_no_transaction()
387
-    {
388
-        return $this->get_total_line_items_for_carts();
389
-    }
390
-
391
-
392
-    /**
393
-     * @return EE_Base_Class[]|EE_Line_Item[]
394
-     * @throws \EE_Error
395
-     */
396
-    public function get_total_line_items_for_active_carts()
397
-    {
398
-        return $this->get_total_line_items_for_carts(false);
399
-    }
400
-
401
-
402
-    /**
403
-     * @return EE_Base_Class[]|EE_Line_Item[]
404
-     * @throws \EE_Error
405
-     */
406
-    public function get_total_line_items_for_expired_carts()
407
-    {
408
-        return $this->get_total_line_items_for_carts(true);
409
-    }
410
-
411
-
412
-    /**
413
-     * Returns an array of grand total line items where the TXN_ID is 0.
414
-     * If $expired is set to true, then only line items for expired sessions will be returned.
415
-     * If $expired is set to false, then only line items for active sessions will be returned.
416
-     *
417
-     * @param bool|null $expired
418
-     * @return EE_Base_Class[]|EE_Line_Item[]
419
-     * @throws \EE_Error
420
-     */
421
-    private function get_total_line_items_for_carts($expired = null)
422
-    {
423
-        $where_params = array(
424
-            'TXN_ID' => 0,
425
-            'LIN_type' => 'total',
426
-        );
427
-        if ($expired !== null) {
428
-            $where_params['LIN_timestamp'] = array(
429
-                $expired ? '<=' : '>',
430
-                time() - EE_Registry::instance()->SSN->lifespan(),
431
-            );
432
-        }
433
-        return $this->get_all(array($where_params));
434
-    }
224
+			// use GMT time because that's what TXN_timestamps are in
225
+			date('Y-m-d H:i:s', time() - $time_to_leave_alone)
226
+		);
227
+		return $wpdb->query($query);
228
+	}
229
+
230
+
231
+	/**
232
+	 * get_line_item_for_transaction_object
233
+	 * Gets a transaction's line item record for a specific object such as a EE_Event or EE_Ticket
234
+	 *
235
+	 * @param int $TXN_ID
236
+	 * @param \EE_Base_Class $object
237
+	 * @return EE_Line_Item[]
238
+	 */
239
+	public function get_line_item_for_transaction_object($TXN_ID, EE_Base_Class $object)
240
+	{
241
+		return $this->get_all(array(array(
242
+			'TXN_ID' => $TXN_ID,
243
+			'OBJ_type' => str_replace('EE_', '', get_class($object)),
244
+			'OBJ_ID' => $object->ID()
245
+		)));
246
+	}
247
+
248
+
249
+	/**
250
+	 * get_object_line_items_for_transaction
251
+	 * Gets all of the the object line items for a transaction, based on an object type plus an array of object IDs
252
+	 *
253
+	 * @param int $TXN_ID
254
+	 * @param string $OBJ_type
255
+	 * @param array $OBJ_IDs
256
+	 * @return EE_Line_Item[]
257
+	 */
258
+	public function get_object_line_items_for_transaction($TXN_ID, $OBJ_type = 'Event', $OBJ_IDs = array())
259
+	{
260
+		$query_params = array(
261
+			'OBJ_type' => $OBJ_type,
262
+			// if incoming $OBJ_IDs is an array, then make sure it is formatted correctly for the query
263
+			'OBJ_ID' => is_array($OBJ_IDs) && !isset($OBJ_IDs['IN']) ? array('IN', $OBJ_IDs) : $OBJ_IDs
264
+		);
265
+		if ($TXN_ID) {
266
+			$query_params['TXN_ID'] = $TXN_ID;
267
+		}
268
+		return $this->get_all(array($query_params));
269
+	}
270
+
271
+
272
+	/**
273
+	 * get_all_ticket_line_items_for_transaction
274
+	 *
275
+	 * @param EE_Transaction $transaction
276
+	 * @return EE_Line_Item[]
277
+	 */
278
+	public function get_all_ticket_line_items_for_transaction(EE_Transaction $transaction)
279
+	{
280
+		return $this->get_all(array(
281
+			array(
282
+				'TXN_ID' => $transaction->ID(),
283
+				'OBJ_type' => 'Ticket',
284
+			)
285
+		));
286
+	}
287
+
288
+
289
+	/**
290
+	 * get_ticket_line_item_for_transaction
291
+	 *
292
+	 * @param int $TXN_ID
293
+	 * @param int $TKT_ID
294
+	 * @return \EE_Line_Item
295
+	 */
296
+	public function get_ticket_line_item_for_transaction($TXN_ID, $TKT_ID)
297
+	{
298
+		return $this->get_one(array(
299
+			array(
300
+				'TXN_ID' => EEM_Transaction::instance()->ensure_is_ID($TXN_ID),
301
+				'OBJ_ID' => $TKT_ID,
302
+				'OBJ_type' => 'Ticket',
303
+			)
304
+		));
305
+	}
306
+
307
+
308
+	/**
309
+	 * get_existing_promotion_line_item
310
+	 * searches the cart for existing line items for the specified promotion
311
+	 *
312
+	 * @since   1.0.0
313
+	 *
314
+	 * @param EE_Line_Item $parent_line_item
315
+	 * @param EE_Promotion $promotion
316
+	 * @return EE_Line_Item
317
+	 */
318
+	public function get_existing_promotion_line_item(EE_Line_Item $parent_line_item, EE_Promotion $promotion)
319
+	{
320
+		return $this->get_one(array(
321
+			array(
322
+				'TXN_ID' => $parent_line_item->TXN_ID(),
323
+				'LIN_parent' => $parent_line_item->ID(),
324
+				'OBJ_type' => 'Promotion',
325
+				'OBJ_ID' => $promotion->ID()
326
+			)
327
+		));
328
+	}
329
+
330
+
331
+	/**
332
+	 * get_all_promotion_line_items
333
+	 * searches the cart for any and all existing promotion line items
334
+	 *
335
+	 * @since   1.0.0
336
+	 *
337
+	 * @param EE_Line_Item $parent_line_item
338
+	 * @return EE_Line_Item[]
339
+	 */
340
+	public function get_all_promotion_line_items(EE_Line_Item $parent_line_item)
341
+	{
342
+		return $this->get_all(array(
343
+			array(
344
+				'TXN_ID' => $parent_line_item->TXN_ID(),
345
+				'LIN_parent' => $parent_line_item->ID(),
346
+				'OBJ_type' => 'Promotion'
347
+			)
348
+		));
349
+	}
350
+
351
+	/**
352
+	 * Gets the registration's corresponding line item.
353
+	 * Note: basically does NOT support having multiple line items for a single ticket,
354
+	 * which would happen if some of the registrations had a price modifier while others didn't.
355
+	 * In order to support that, we'd probably need a LIN_ID on registrations or something.
356
+	 * @param EE_Registration $registration
357
+	 * @return EE_Line_ITem
358
+	 */
359
+	public function get_line_item_for_registration(EE_Registration $registration)
360
+	{
361
+		return $this->get_one($this->line_item_for_registration_query_params($registration));
362
+	}
363
+
364
+	/**
365
+	 * Gets the query params used to retrieve a specific line item for the given registration
366
+	 * @param EE_Registration $registration
367
+	 * @param array $original_query_params any extra query params you'd like to be merged with
368
+	 * @return array like EEM_Base::get_all()'s $query_params
369
+	 */
370
+	public function line_item_for_registration_query_params(EE_Registration $registration, $original_query_params = array())
371
+	{
372
+		return array_replace_recursive($original_query_params, array(
373
+			array(
374
+				'OBJ_ID' => $registration->ticket_ID(),
375
+				'OBJ_type' => 'Ticket',
376
+				'TXN_ID' => $registration->transaction_ID()
377
+			)
378
+		));
379
+	}
380
+
381
+
382
+	/**
383
+	 * @return EE_Base_Class[]|EE_Line_Item[]
384
+	 * @throws \EE_Error
385
+	 */
386
+	public function get_total_line_items_with_no_transaction()
387
+	{
388
+		return $this->get_total_line_items_for_carts();
389
+	}
390
+
391
+
392
+	/**
393
+	 * @return EE_Base_Class[]|EE_Line_Item[]
394
+	 * @throws \EE_Error
395
+	 */
396
+	public function get_total_line_items_for_active_carts()
397
+	{
398
+		return $this->get_total_line_items_for_carts(false);
399
+	}
400
+
401
+
402
+	/**
403
+	 * @return EE_Base_Class[]|EE_Line_Item[]
404
+	 * @throws \EE_Error
405
+	 */
406
+	public function get_total_line_items_for_expired_carts()
407
+	{
408
+		return $this->get_total_line_items_for_carts(true);
409
+	}
410
+
411
+
412
+	/**
413
+	 * Returns an array of grand total line items where the TXN_ID is 0.
414
+	 * If $expired is set to true, then only line items for expired sessions will be returned.
415
+	 * If $expired is set to false, then only line items for active sessions will be returned.
416
+	 *
417
+	 * @param bool|null $expired
418
+	 * @return EE_Base_Class[]|EE_Line_Item[]
419
+	 * @throws \EE_Error
420
+	 */
421
+	private function get_total_line_items_for_carts($expired = null)
422
+	{
423
+		$where_params = array(
424
+			'TXN_ID' => 0,
425
+			'LIN_type' => 'total',
426
+		);
427
+		if ($expired !== null) {
428
+			$where_params['LIN_timestamp'] = array(
429
+				$expired ? '<=' : '>',
430
+				time() - EE_Registry::instance()->SSN->lifespan(),
431
+			);
432
+		}
433
+		return $this->get_all(array($where_params));
434
+	}
435 435
 
436 436
 
437 437
 }
438 438
\ No newline at end of file
Please login to merge, or discard this patch.