Passed
Push — master ( 55fff1...2692d4 )
by Brian
16:18
created
includes/payments/class-getpaid-payment-form-submission-discount.php 1 patch
Indentation   +159 added lines, -159 removed lines patch added patch discarded remove patch
@@ -12,167 +12,167 @@
 block discarded – undo
12 12
  */
13 13
 class GetPaid_Payment_Form_Submission_Discount {
14 14
 
15
-	/**
16
-	 * Submission discounts.
17
-	 * @var array
18
-	 */
19
-	public $discounts = array();
15
+    /**
16
+     * Submission discounts.
17
+     * @var array
18
+     */
19
+    public $discounts = array();
20
+
21
+    /**
22
+     * Class constructor
23
+     *
24
+     * @param GetPaid_Payment_Form_Submission $submission
25
+     * @param float                           $initial_total
26
+     * @param float                           $recurring_total
27
+     */
28
+    public function __construct( $submission, $initial_total, $recurring_total ) {
29
+
30
+        // Process any existing invoice discounts.
31
+        if ( $submission->has_invoice() ) {
32
+            $this->discounts = $submission->get_invoice()->get_discounts();
33
+        }
34
+
35
+        // Do we have a discount?
36
+        $discount = $submission->get_field( 'discount' );
37
+
38
+        if ( empty( $discount ) ) {
39
+
40
+            if ( isset( $this->discounts['discount_code'] ) ) {
41
+                unset( $this->discounts['discount_code'] );
42
+            }
43
+
44
+            return;
45
+        }
46
+
47
+        // Processes the discount code.
48
+        $amount = max( $initial_total, $recurring_total );
49
+        $this->process_discount( $submission, $discount, $amount );
50
+
51
+    }
20 52
 
21 53
     /**
22
-	 * Class constructor
23
-	 *
24
-	 * @param GetPaid_Payment_Form_Submission $submission
25
-	 * @param float                           $initial_total
26
-	 * @param float                           $recurring_total
27
-	 */
28
-	public function __construct( $submission, $initial_total, $recurring_total ) {
29
-
30
-		// Process any existing invoice discounts.
31
-		if ( $submission->has_invoice() ) {
32
-			$this->discounts = $submission->get_invoice()->get_discounts();
33
-		}
34
-
35
-		// Do we have a discount?
36
-		$discount = $submission->get_field( 'discount' );
37
-
38
-		if ( empty( $discount ) ) {
39
-
40
-			if ( isset( $this->discounts['discount_code'] ) ) {
41
-				unset( $this->discounts['discount_code'] );
42
-			}
43
-
44
-			return;
45
-		}
46
-
47
-		// Processes the discount code.
48
-		$amount = max( $initial_total, $recurring_total );
49
-		$this->process_discount( $submission, $discount, $amount );
50
-
51
-	}
52
-
53
-	/**
54
-	 * Processes a submission discount.
55
-	 *
56
-	 * @param GetPaid_Payment_Form_Submission $submission
57
-	 * @param string                          $discount
58
-	 * @param float                           $amount
59
-	 */
60
-	public function process_discount( $submission, $discount, $amount ) {
61
-
62
-		// Fetch the discount.
63
-		$discount = new WPInv_Discount( $discount );
64
-
65
-		// Ensure it is active.
54
+     * Processes a submission discount.
55
+     *
56
+     * @param GetPaid_Payment_Form_Submission $submission
57
+     * @param string                          $discount
58
+     * @param float                           $amount
59
+     */
60
+    public function process_discount( $submission, $discount, $amount ) {
61
+
62
+        // Fetch the discount.
63
+        $discount = new WPInv_Discount( $discount );
64
+
65
+        // Ensure it is active.
66 66
         if ( ! $this->is_discount_active( $discount ) ) {
67
-			throw new GetPaid_Payment_Exception( '.getpaid-discount-field .getpaid-custom-payment-form-errors', __( 'Invalid or expired discount code', 'invoicing' ) );
68
-		}
69
-
70
-		// Required items.
71
-		if ( ! $discount->is_required_items_met( array_keys( $submission->get_items() ) ) ) {
72
-			throw new GetPaid_Payment_Exception( '.getpaid-discount-field .getpaid-custom-payment-form-errors', __( 'You are not allowed to use this discount code.', 'invoicing' ) );
73
-		}
74
-
75
-		// Exceeded limit.
76
-		if ( $discount->has_exceeded_limit() ) {
77
-			throw new GetPaid_Payment_Exception( '.getpaid-discount-field .getpaid-custom-payment-form-errors', __( 'This discount code has been used up', 'invoicing' ) );
78
-		}
79
-
80
-		// Validate usages.
81
-		$this->validate_single_use_discount( $submission, $discount );
82
-
83
-		// Validate amount.
84
-		$this->validate_discount_amount( $submission, $discount, $amount );
85
-
86
-		// Save the discount.
87
-		$this->discounts['discount_code'] = $this->calculate_discount( $submission, $discount );
88
-	}
89
-
90
-	/**
91
-	 * Validates a single use discount.
92
-	 *
93
-	 * @param WPInv_Discount                  $discount
94
-	 * @return bool
95
-	 */
96
-	public function is_discount_active( $discount ) {
97
-		return $discount->exists() && $discount->is_active() && $discount->has_started() && ! $discount->is_expired();
98
-	}
99
-
100
-	/**
101
-	 * Returns a user's id or email.
102
-	 *
103
-	 * @param string $email
104
-	 * @return int|string|false
105
-	 */
106
-	public function get_user_id_or_email( $email ) {
107
-
108
-		if ( is_user_logged_in() ) {
109
-			return get_current_user_id();
110
-		}
111
-
112
-		return empty( $email ) ? false : sanitize_email( $email );
113
-	}
114
-
115
-	/**
116
-	 * Validates a single use discount.
117
-	 *
118
-	 * @param GetPaid_Payment_Form_Submission $submission
119
-	 * @param WPInv_Discount                  $discount
120
-	 */
121
-	public function validate_single_use_discount( $submission, $discount ) {
122
-
123
-		// Abort if it is not a single use discount.
124
-		if ( ! $discount->is_single_use() ) {
125
-			return;
126
-		}
127
-
128
-		// Ensure there is a valid billing email.
129
-		$user = $this->get_user_id_or_email( $submission->get_billing_email() );
130
-
131
-		if ( empty( $user ) ) {
132
-			throw new GetPaid_Payment_Exception( '.getpaid-discount-field .getpaid-custom-payment-form-errors', __( 'You need to either log in or enter your billing email before applying this discount', 'invoicing' ) );
133
-		}
134
-
135
-		// Has the user used this discount code before?
136
-		if ( ! $discount->is_valid_for_user( $user ) ) {
137
-			throw new GetPaid_Payment_Exception( '.getpaid-discount-field .getpaid-custom-payment-form-errors', __( 'You have already used this discount', 'invoicing' ) );
138
-		}
139
-
140
-	}
141
-
142
-	/**
143
-	 * Validates the discount's amount.
144
-	 *
145
-	 * @param GetPaid_Payment_Form_Submission $submission
146
-	 * @param WPInv_Discount         $discount
147
-	 * @param float                  $amount
148
-	 */
149
-	public function validate_discount_amount( $submission, $discount, $amount ) {
150
-
151
-		// Validate minimum amount.
152
-		if ( ! $discount->is_minimum_amount_met( $amount ) ) {
153
-			$min = wpinv_price( $discount->get_minimum_total(), $submission->get_currency() );
154
-			throw new GetPaid_Payment_Exception( '.getpaid-discount-field .getpaid-custom-payment-form-errors', sprintf( __( 'The minimum total for using this discount is %s', 'invoicing' ), $min ) );
155
-		}
156
-
157
-		// Validate the maximum amount.
158
-		if ( ! $discount->is_maximum_amount_met( $amount ) ) {
159
-			$max = wpinv_price( $discount->get_maximum_total(), $submission->get_currency() );
160
-			throw new GetPaid_Payment_Exception( '.getpaid-discount-field .getpaid-custom-payment-form-errors', sprintf( __( 'The maximum total for using this discount is %s', 'invoicing' ), $max ) );
161
-		}
162
-
163
-	}
164
-
165
-	/**
166
-	 * Calculates the discount code's amount.
167
-	 *
168
-	 * Ensure that the discount exists and has been validated before calling this method.
169
-	 *
170
-	 * @param GetPaid_Payment_Form_Submission $submission
171
-	 * @param WPInv_Discount                  $discount
172
-	 * @return array
173
-	 */
174
-	public function calculate_discount( $submission, $discount ) {
175
-		return getpaid_calculate_invoice_discount( $submission, $discount );
176
-	}
67
+            throw new GetPaid_Payment_Exception( '.getpaid-discount-field .getpaid-custom-payment-form-errors', __( 'Invalid or expired discount code', 'invoicing' ) );
68
+        }
69
+
70
+        // Required items.
71
+        if ( ! $discount->is_required_items_met( array_keys( $submission->get_items() ) ) ) {
72
+            throw new GetPaid_Payment_Exception( '.getpaid-discount-field .getpaid-custom-payment-form-errors', __( 'You are not allowed to use this discount code.', 'invoicing' ) );
73
+        }
74
+
75
+        // Exceeded limit.
76
+        if ( $discount->has_exceeded_limit() ) {
77
+            throw new GetPaid_Payment_Exception( '.getpaid-discount-field .getpaid-custom-payment-form-errors', __( 'This discount code has been used up', 'invoicing' ) );
78
+        }
79
+
80
+        // Validate usages.
81
+        $this->validate_single_use_discount( $submission, $discount );
82
+
83
+        // Validate amount.
84
+        $this->validate_discount_amount( $submission, $discount, $amount );
85
+
86
+        // Save the discount.
87
+        $this->discounts['discount_code'] = $this->calculate_discount( $submission, $discount );
88
+    }
89
+
90
+    /**
91
+     * Validates a single use discount.
92
+     *
93
+     * @param WPInv_Discount                  $discount
94
+     * @return bool
95
+     */
96
+    public function is_discount_active( $discount ) {
97
+        return $discount->exists() && $discount->is_active() && $discount->has_started() && ! $discount->is_expired();
98
+    }
99
+
100
+    /**
101
+     * Returns a user's id or email.
102
+     *
103
+     * @param string $email
104
+     * @return int|string|false
105
+     */
106
+    public function get_user_id_or_email( $email ) {
107
+
108
+        if ( is_user_logged_in() ) {
109
+            return get_current_user_id();
110
+        }
111
+
112
+        return empty( $email ) ? false : sanitize_email( $email );
113
+    }
114
+
115
+    /**
116
+     * Validates a single use discount.
117
+     *
118
+     * @param GetPaid_Payment_Form_Submission $submission
119
+     * @param WPInv_Discount                  $discount
120
+     */
121
+    public function validate_single_use_discount( $submission, $discount ) {
122
+
123
+        // Abort if it is not a single use discount.
124
+        if ( ! $discount->is_single_use() ) {
125
+            return;
126
+        }
127
+
128
+        // Ensure there is a valid billing email.
129
+        $user = $this->get_user_id_or_email( $submission->get_billing_email() );
130
+
131
+        if ( empty( $user ) ) {
132
+            throw new GetPaid_Payment_Exception( '.getpaid-discount-field .getpaid-custom-payment-form-errors', __( 'You need to either log in or enter your billing email before applying this discount', 'invoicing' ) );
133
+        }
134
+
135
+        // Has the user used this discount code before?
136
+        if ( ! $discount->is_valid_for_user( $user ) ) {
137
+            throw new GetPaid_Payment_Exception( '.getpaid-discount-field .getpaid-custom-payment-form-errors', __( 'You have already used this discount', 'invoicing' ) );
138
+        }
139
+
140
+    }
141
+
142
+    /**
143
+     * Validates the discount's amount.
144
+     *
145
+     * @param GetPaid_Payment_Form_Submission $submission
146
+     * @param WPInv_Discount         $discount
147
+     * @param float                  $amount
148
+     */
149
+    public function validate_discount_amount( $submission, $discount, $amount ) {
150
+
151
+        // Validate minimum amount.
152
+        if ( ! $discount->is_minimum_amount_met( $amount ) ) {
153
+            $min = wpinv_price( $discount->get_minimum_total(), $submission->get_currency() );
154
+            throw new GetPaid_Payment_Exception( '.getpaid-discount-field .getpaid-custom-payment-form-errors', sprintf( __( 'The minimum total for using this discount is %s', 'invoicing' ), $min ) );
155
+        }
156
+
157
+        // Validate the maximum amount.
158
+        if ( ! $discount->is_maximum_amount_met( $amount ) ) {
159
+            $max = wpinv_price( $discount->get_maximum_total(), $submission->get_currency() );
160
+            throw new GetPaid_Payment_Exception( '.getpaid-discount-field .getpaid-custom-payment-form-errors', sprintf( __( 'The maximum total for using this discount is %s', 'invoicing' ), $max ) );
161
+        }
162
+
163
+    }
164
+
165
+    /**
166
+     * Calculates the discount code's amount.
167
+     *
168
+     * Ensure that the discount exists and has been validated before calling this method.
169
+     *
170
+     * @param GetPaid_Payment_Form_Submission $submission
171
+     * @param WPInv_Discount                  $discount
172
+     * @return array
173
+     */
174
+    public function calculate_discount( $submission, $discount ) {
175
+        return getpaid_calculate_invoice_discount( $submission, $discount );
176
+    }
177 177
 
178 178
 }
Please login to merge, or discard this patch.
includes/payments/class-getpaid-payment-form-submission-fees.php 1 patch
Indentation   +104 added lines, -104 removed lines patch added patch discarded remove patch
@@ -12,113 +12,113 @@
 block discarded – undo
12 12
  */
13 13
 class GetPaid_Payment_Form_Submission_Fees {
14 14
 
15
-	/**
16
-	 * The fee validation error.
17
-	 * @var string
18
-	 */
19
-	public $fee_error;
20
-
21
-	/**
22
-	 * Submission fees.
23
-	 * @var array
24
-	 */
25
-	public $fees = array();
15
+    /**
16
+     * The fee validation error.
17
+     * @var string
18
+     */
19
+    public $fee_error;
20
+
21
+    /**
22
+     * Submission fees.
23
+     * @var array
24
+     */
25
+    public $fees = array();
26 26
 
27 27
     /**
28
-	 * Class constructor
29
-	 *
30
-	 * @param GetPaid_Payment_Form_Submission $submission
31
-	 */
32
-	public function __construct( $submission ) {
33
-
34
-		// Process any existing invoice fees.
35
-		if ( $submission->has_invoice() ) {
36
-			$this->fees = $submission->get_invoice()->get_fees();
37
-		}
38
-
39
-		// Process price fields.
40
-		$data         = $submission->get_data();
41
-		$payment_form = $submission->get_payment_form();
42
-
43
-		foreach ( $payment_form->get_elements() as $element ) {
44
-
45
-			if ( 'price_input' == $element['type'] ) {
46
-				$this->process_price_input( $element, $data, $submission );
47
-			}
48
-
49
-			if ( 'price_select' == $element['type'] ) {
50
-				$this->process_price_select( $element, $data );
51
-			}
28
+     * Class constructor
29
+     *
30
+     * @param GetPaid_Payment_Form_Submission $submission
31
+     */
32
+    public function __construct( $submission ) {
33
+
34
+        // Process any existing invoice fees.
35
+        if ( $submission->has_invoice() ) {
36
+            $this->fees = $submission->get_invoice()->get_fees();
37
+        }
38
+
39
+        // Process price fields.
40
+        $data         = $submission->get_data();
41
+        $payment_form = $submission->get_payment_form();
42
+
43
+        foreach ( $payment_form->get_elements() as $element ) {
44
+
45
+            if ( 'price_input' == $element['type'] ) {
46
+                $this->process_price_input( $element, $data, $submission );
47
+            }
48
+
49
+            if ( 'price_select' == $element['type'] ) {
50
+                $this->process_price_select( $element, $data );
51
+            }
52 52
 }
53 53
 
54
-	}
55
-
56
-	/**
57
-	 * Process a price input field.
58
-	 *
59
-	 * @param array $element
60
-	 * @param array $data
61
-	 * @param GetPaid_Payment_Form_Submission $submission
62
-	 */
63
-	public function process_price_input( $element, $data, $submission ) {
64
-
65
-		// Abort if not passed.
66
-		if ( empty( $data[ $element['id'] ] ) ) {
67
-			return;
68
-		}
69
-
70
-		$amount  = (float) wpinv_sanitize_amount( $data[ $element['id'] ] );
71
-		$minimum = empty( $element['minimum'] ) ? 0 : (float) wpinv_sanitize_amount( $element['minimum'] );
72
-
73
-		if ( $amount < $minimum ) {
74
-			throw new Exception( sprintf( __( 'The minimum allowed amount is %s', 'invoicing' ), getpaid_unstandardize_amount( $minimum, $submission->get_currency() ) ) );
75
-		}
76
-
77
-		$this->fees[ $element['label'] ] = array(
78
-			'name'          => $element['label'],
79
-			'initial_fee'   => $amount,
80
-			'recurring_fee' => 0,
81
-		);
82
-
83
-	}
84
-
85
-	/**
86
-	 * Process a price select field.
87
-	 *
88
-	 * @param array $element
89
-	 * @param array $data
90
-	 */
91
-	public function process_price_select( $element, $data ) {
92
-
93
-		// Abort if not passed.
94
-		if ( empty( $data[ $element['id'] ] ) ) {
95
-			return;
96
-		}
97
-
98
-		$options    = getpaid_convert_price_string_to_options( $element['options'] );
99
-		$selected   = array_filter( array_map( 'trim', explode( ',', $data[ $element['id'] ] ) ) );
100
-		$total      = 0;
101
-		$sub_labels = array();
102
-
103
-		foreach ( $selected as $price ) {
104
-
105
-			if ( ! isset( $options[ $price ] ) ) {
106
-				throw new Exception( __( 'You have selected an invalid amount', 'invoicing' ) );
107
-			}
108
-
109
-			$price = explode( '|', $price );
110
-
111
-			$sub_labels[] = $price[0];
112
-			$total += (float) wpinv_sanitize_amount( $price[1] );
113
-		}
114
-
115
-		$this->fees[ $element['label'] ] = array(
116
-			'name'          => $element['label'],
117
-			'initial_fee'   => $total,
118
-			'recurring_fee' => 0,
119
-			'description'   => implode( ', ', $sub_labels ),
120
-		);
121
-
122
-	}
54
+    }
55
+
56
+    /**
57
+     * Process a price input field.
58
+     *
59
+     * @param array $element
60
+     * @param array $data
61
+     * @param GetPaid_Payment_Form_Submission $submission
62
+     */
63
+    public function process_price_input( $element, $data, $submission ) {
64
+
65
+        // Abort if not passed.
66
+        if ( empty( $data[ $element['id'] ] ) ) {
67
+            return;
68
+        }
69
+
70
+        $amount  = (float) wpinv_sanitize_amount( $data[ $element['id'] ] );
71
+        $minimum = empty( $element['minimum'] ) ? 0 : (float) wpinv_sanitize_amount( $element['minimum'] );
72
+
73
+        if ( $amount < $minimum ) {
74
+            throw new Exception( sprintf( __( 'The minimum allowed amount is %s', 'invoicing' ), getpaid_unstandardize_amount( $minimum, $submission->get_currency() ) ) );
75
+        }
76
+
77
+        $this->fees[ $element['label'] ] = array(
78
+            'name'          => $element['label'],
79
+            'initial_fee'   => $amount,
80
+            'recurring_fee' => 0,
81
+        );
82
+
83
+    }
84
+
85
+    /**
86
+     * Process a price select field.
87
+     *
88
+     * @param array $element
89
+     * @param array $data
90
+     */
91
+    public function process_price_select( $element, $data ) {
92
+
93
+        // Abort if not passed.
94
+        if ( empty( $data[ $element['id'] ] ) ) {
95
+            return;
96
+        }
97
+
98
+        $options    = getpaid_convert_price_string_to_options( $element['options'] );
99
+        $selected   = array_filter( array_map( 'trim', explode( ',', $data[ $element['id'] ] ) ) );
100
+        $total      = 0;
101
+        $sub_labels = array();
102
+
103
+        foreach ( $selected as $price ) {
104
+
105
+            if ( ! isset( $options[ $price ] ) ) {
106
+                throw new Exception( __( 'You have selected an invalid amount', 'invoicing' ) );
107
+            }
108
+
109
+            $price = explode( '|', $price );
110
+
111
+            $sub_labels[] = $price[0];
112
+            $total += (float) wpinv_sanitize_amount( $price[1] );
113
+        }
114
+
115
+        $this->fees[ $element['label'] ] = array(
116
+            'name'          => $element['label'],
117
+            'initial_fee'   => $total,
118
+            'recurring_fee' => 0,
119
+            'description'   => implode( ', ', $sub_labels ),
120
+        );
121
+
122
+    }
123 123
 
124 124
 }
Please login to merge, or discard this patch.
includes/payments/class-getpaid-payment-form-submission-taxes.php 1 patch
Indentation   +206 added lines, -206 removed lines patch added patch discarded remove patch
@@ -12,227 +12,227 @@
 block discarded – undo
12 12
  */
13 13
 class GetPaid_Payment_Form_Submission_Taxes {
14 14
 
15
-	/**
16
-	 * Submission taxes.
17
-	 * @var array
18
-	 */
19
-	public $taxes = array();
20
-
21
-	/**
22
-	 * Whether or not we should skip the taxes.
23
-	 * @var bool
24
-	 */
25
-	protected $skip_taxes = false;
15
+    /**
16
+     * Submission taxes.
17
+     * @var array
18
+     */
19
+    public $taxes = array();
20
+
21
+    /**
22
+     * Whether or not we should skip the taxes.
23
+     * @var bool
24
+     */
25
+    protected $skip_taxes = false;
26
+
27
+    /**
28
+     * Class constructor
29
+     *
30
+     * @param GetPaid_Payment_Form_Submission $submission
31
+     */
32
+    public function __construct( $submission ) {
33
+
34
+        // Validate VAT number.
35
+        $this->validate_vat( $submission );
36
+
37
+        if ( $this->skip_taxes ) {
38
+            return;
39
+        }
40
+
41
+        foreach ( $submission->get_items() as $item ) {
42
+            $this->process_item_tax( $item, $submission );
43
+        }
44
+
45
+        // Process any existing invoice taxes.
46
+        if ( $submission->has_invoice() ) {
47
+            $this->taxes = array_replace( $submission->get_invoice()->get_taxes(), $this->taxes );
48
+        }
49
+
50
+    }
51
+
52
+    /**
53
+     * Maybe process tax.
54
+     *
55
+     * @since 1.0.19
56
+     * @param GetPaid_Form_Item $item
57
+     * @param GetPaid_Payment_Form_Submission $submission
58
+     */
59
+    public function process_item_tax( $item, $submission ) {
60
+
61
+        $rates    = getpaid_get_item_tax_rates( $item, $submission->country, $submission->state );
62
+        $rates    = getpaid_filter_item_tax_rates( $item, $rates );
63
+        $taxes    = getpaid_calculate_item_taxes( getpaid_get_taxable_amount( $item, false ), $rates );
64
+        $r_taxes  = getpaid_calculate_item_taxes( getpaid_get_taxable_amount( $item, true ), $rates );
65
+
66
+        foreach ( $taxes as $name => $amount ) {
67
+            $recurring = isset( $r_taxes[ $name ] ) ? $r_taxes[ $name ] : 0;
68
+            $tax       = getpaid_prepare_item_tax( $item, $name, $amount, $recurring );
69
+
70
+            $item->item_tax += wpinv_sanitize_amount( $tax['initial_tax'] );
71
+
72
+            if ( ! isset( $this->taxes[ $name ] ) ) {
73
+                $this->taxes[ $name ] = $tax;
74
+                continue;
75
+            }
76
+
77
+            $this->taxes[ $name ]['initial_tax']   += $tax['initial_tax'];
78
+            $this->taxes[ $name ]['recurring_tax'] += $tax['recurring_tax'];
79
+
80
+        }
81
+
82
+    }
26 83
 
27 84
     /**
28
-	 * Class constructor
29
-	 *
30
-	 * @param GetPaid_Payment_Form_Submission $submission
31
-	 */
32
-	public function __construct( $submission ) {
33
-
34
-		// Validate VAT number.
35
-		$this->validate_vat( $submission );
36
-
37
-		if ( $this->skip_taxes ) {
38
-			return;
39
-		}
40
-
41
-		foreach ( $submission->get_items() as $item ) {
42
-			$this->process_item_tax( $item, $submission );
43
-		}
44
-
45
-		// Process any existing invoice taxes.
46
-		if ( $submission->has_invoice() ) {
47
-			$this->taxes = array_replace( $submission->get_invoice()->get_taxes(), $this->taxes );
48
-		}
49
-
50
-	}
51
-
52
-	/**
53
-	 * Maybe process tax.
54
-	 *
55
-	 * @since 1.0.19
56
-	 * @param GetPaid_Form_Item $item
57
-	 * @param GetPaid_Payment_Form_Submission $submission
58
-	 */
59
-	public function process_item_tax( $item, $submission ) {
60
-
61
-		$rates    = getpaid_get_item_tax_rates( $item, $submission->country, $submission->state );
62
-		$rates    = getpaid_filter_item_tax_rates( $item, $rates );
63
-		$taxes    = getpaid_calculate_item_taxes( getpaid_get_taxable_amount( $item, false ), $rates );
64
-		$r_taxes  = getpaid_calculate_item_taxes( getpaid_get_taxable_amount( $item, true ), $rates );
65
-
66
-		foreach ( $taxes as $name => $amount ) {
67
-			$recurring = isset( $r_taxes[ $name ] ) ? $r_taxes[ $name ] : 0;
68
-			$tax       = getpaid_prepare_item_tax( $item, $name, $amount, $recurring );
69
-
70
-			$item->item_tax += wpinv_sanitize_amount( $tax['initial_tax'] );
71
-
72
-			if ( ! isset( $this->taxes[ $name ] ) ) {
73
-				$this->taxes[ $name ] = $tax;
74
-				continue;
75
-			}
76
-
77
-			$this->taxes[ $name ]['initial_tax']   += $tax['initial_tax'];
78
-			$this->taxes[ $name ]['recurring_tax'] += $tax['recurring_tax'];
79
-
80
-		}
81
-
82
-	}
83
-
84
-	/**
85
-	 * Checks if the submission has a digital item.
86
-	 *
87
-	 * @param GetPaid_Payment_Form_Submission $submission
88
-	 * @since 1.0.19
89
-	 * @return bool
90
-	 */
91
-	public function has_digital_item( $submission ) {
92
-
93
-		foreach ( $submission->get_items() as $item ) {
94
-
95
-			if ( 'digital' == $item->get_vat_rule() ) {
96
-				return true;
97
-			}
85
+     * Checks if the submission has a digital item.
86
+     *
87
+     * @param GetPaid_Payment_Form_Submission $submission
88
+     * @since 1.0.19
89
+     * @return bool
90
+     */
91
+    public function has_digital_item( $submission ) {
92
+
93
+        foreach ( $submission->get_items() as $item ) {
94
+
95
+            if ( 'digital' == $item->get_vat_rule() ) {
96
+                return true;
97
+            }
98 98
 }
99 99
 
100
-		return false;
101
-	}
102
-
103
-	/**
104
-	 * Checks if this is an eu store.
105
-	 *
106
-	 * @since 1.0.19
107
-	 * @return bool
108
-	 */
109
-	public static function is_eu_store() {
110
-		return self::is_eu_country( wpinv_get_default_country() );
111
-	}
112
-
113
-	/**
114
-	 * Checks if this is an eu country.
115
-	 *
116
-	 * @param string $country
117
-	 * @since 1.0.19
118
-	 * @return bool
119
-	 */
120
-	public static function is_eu_country( $country ) {
121
-		return getpaid_is_eu_state( $country );
122
-	}
123
-
124
-	/**
125
-	 * Checks if this is an eu purchase.
126
-	 *
127
-	 * @param string $customer_country
128
-	 * @since 1.0.19
129
-	 * @return bool
130
-	 */
131
-	public static function is_eu_transaction( $customer_country ) {
132
-		return self::is_eu_country( $customer_country ) && self::is_eu_store();
133
-	}
134
-
135
-	/**
136
-	 * Retrieves the vat number.
137
-	 *
138
-	 * @param GetPaid_Payment_Form_Submission $submission
139
-	 * @since 1.0.19
140
-	 * @return string
141
-	 */
142
-	public function get_vat_number( $submission ) {
143
-
144
-		// Retrieve from the posted number.
145
-		$vat_number = $submission->get_field( 'wpinv_vat_number', 'billing' );
146
-		if ( ! is_null( $vat_number ) ) {
147
-			return wpinv_clean( $vat_number );
148
-		}
149
-
150
-		return $submission->has_invoice() ? $submission->get_invoice()->get_vat_number() : '';
151
-	}
152
-
153
-	/**
154
-	 * Retrieves the company.
155
-	 *
156
-	 * @param GetPaid_Payment_Form_Submission $submission
157
-	 * @since 1.0.19
158
-	 * @return string
159
-	 */
160
-	public function get_company( $submission ) {
161
-
162
-		// Retrieve from the posted data.
163
-		$company = $submission->get_field( 'wpinv_company', 'billing' );
164
-		if ( ! empty( $company ) ) {
165
-			return wpinv_clean( $company );
166
-		}
167
-
168
-		// Retrieve from the invoice.
169
-		return $submission->has_invoice() ? $submission->get_invoice()->get_company() : '';
170
-	}
171
-
172
-	/**
173
-	 * Checks if we require a VAT number.
174
-	 *
175
-	 * @param bool $ip_in_eu Whether the customer IP is from the EU
176
-	 * @param bool $country_in_eu Whether the customer country is from the EU
177
-	 * @since 1.0.19
178
-	 * @return string
179
-	 */
180
-	public function requires_vat( $ip_in_eu, $country_in_eu ) {
181
-
182
-		$prevent_b2c = wpinv_get_option( 'vat_prevent_b2c_purchase' );
183
-		$prevent_b2c = ! empty( $prevent_b2c );
184
-		$is_eu       = $ip_in_eu || $country_in_eu;
185
-
186
-		return $prevent_b2c && $is_eu;
187
-	}
188
-
189
-	/**
190
-	 * Validate VAT data.
191
-	 *
192
-	 * @param GetPaid_Payment_Form_Submission $submission
193
-	 * @since 1.0.19
194
-	 */
195
-	public function validate_vat( $submission ) {
196
-
197
-		$in_eu = $this->is_eu_transaction( $submission->country );
198
-
199
-		// Abort if we are not validating vat numbers.
200
-		if ( ! $in_eu ) {
100
+        return false;
101
+    }
102
+
103
+    /**
104
+     * Checks if this is an eu store.
105
+     *
106
+     * @since 1.0.19
107
+     * @return bool
108
+     */
109
+    public static function is_eu_store() {
110
+        return self::is_eu_country( wpinv_get_default_country() );
111
+    }
112
+
113
+    /**
114
+     * Checks if this is an eu country.
115
+     *
116
+     * @param string $country
117
+     * @since 1.0.19
118
+     * @return bool
119
+     */
120
+    public static function is_eu_country( $country ) {
121
+        return getpaid_is_eu_state( $country );
122
+    }
123
+
124
+    /**
125
+     * Checks if this is an eu purchase.
126
+     *
127
+     * @param string $customer_country
128
+     * @since 1.0.19
129
+     * @return bool
130
+     */
131
+    public static function is_eu_transaction( $customer_country ) {
132
+        return self::is_eu_country( $customer_country ) && self::is_eu_store();
133
+    }
134
+
135
+    /**
136
+     * Retrieves the vat number.
137
+     *
138
+     * @param GetPaid_Payment_Form_Submission $submission
139
+     * @since 1.0.19
140
+     * @return string
141
+     */
142
+    public function get_vat_number( $submission ) {
143
+
144
+        // Retrieve from the posted number.
145
+        $vat_number = $submission->get_field( 'wpinv_vat_number', 'billing' );
146
+        if ( ! is_null( $vat_number ) ) {
147
+            return wpinv_clean( $vat_number );
148
+        }
149
+
150
+        return $submission->has_invoice() ? $submission->get_invoice()->get_vat_number() : '';
151
+    }
152
+
153
+    /**
154
+     * Retrieves the company.
155
+     *
156
+     * @param GetPaid_Payment_Form_Submission $submission
157
+     * @since 1.0.19
158
+     * @return string
159
+     */
160
+    public function get_company( $submission ) {
161
+
162
+        // Retrieve from the posted data.
163
+        $company = $submission->get_field( 'wpinv_company', 'billing' );
164
+        if ( ! empty( $company ) ) {
165
+            return wpinv_clean( $company );
166
+        }
167
+
168
+        // Retrieve from the invoice.
169
+        return $submission->has_invoice() ? $submission->get_invoice()->get_company() : '';
170
+    }
171
+
172
+    /**
173
+     * Checks if we require a VAT number.
174
+     *
175
+     * @param bool $ip_in_eu Whether the customer IP is from the EU
176
+     * @param bool $country_in_eu Whether the customer country is from the EU
177
+     * @since 1.0.19
178
+     * @return string
179
+     */
180
+    public function requires_vat( $ip_in_eu, $country_in_eu ) {
181
+
182
+        $prevent_b2c = wpinv_get_option( 'vat_prevent_b2c_purchase' );
183
+        $prevent_b2c = ! empty( $prevent_b2c );
184
+        $is_eu       = $ip_in_eu || $country_in_eu;
185
+
186
+        return $prevent_b2c && $is_eu;
187
+    }
188
+
189
+    /**
190
+     * Validate VAT data.
191
+     *
192
+     * @param GetPaid_Payment_Form_Submission $submission
193
+     * @since 1.0.19
194
+     */
195
+    public function validate_vat( $submission ) {
196
+
197
+        $in_eu = $this->is_eu_transaction( $submission->country );
198
+
199
+        // Abort if we are not validating vat numbers.
200
+        if ( ! $in_eu ) {
201 201
             return;
202
-		}
202
+        }
203 203
 
204
-		// Prepare variables.
205
-		$vat_number  = $this->get_vat_number( $submission );
206
-		$ip_country  = getpaid_get_ip_country();
204
+        // Prepare variables.
205
+        $vat_number  = $this->get_vat_number( $submission );
206
+        $ip_country  = getpaid_get_ip_country();
207 207
         $is_eu       = $this->is_eu_country( $submission->country );
208 208
         $is_ip_eu    = $this->is_eu_country( $ip_country );
209 209
 
210
-		// Maybe abort early for initial fetches.
211
-		if ( $submission->is_initial_fetch() && empty( $vat_number ) ) {
212
-			return;
213
-		}
210
+        // Maybe abort early for initial fetches.
211
+        if ( $submission->is_initial_fetch() && empty( $vat_number ) ) {
212
+            return;
213
+        }
214 214
 
215
-		// If we're preventing business to consumer purchases,
216
-		if ( $this->requires_vat( $is_ip_eu, $is_eu ) && empty( $vat_number ) ) {
215
+        // If we're preventing business to consumer purchases,
216
+        if ( $this->requires_vat( $is_ip_eu, $is_eu ) && empty( $vat_number ) ) {
217 217
 
218
-			// Ensure that a vat number has been specified.
219
-			throw new GetPaid_Payment_Exception( '.getpaid-error-billingwpinv_vat_number.getpaid-custom-payment-form-errors', __( 'Please enter your VAT number to verify your purchase is by an EU business.', 'invoicing' ) );
218
+            // Ensure that a vat number has been specified.
219
+            throw new GetPaid_Payment_Exception( '.getpaid-error-billingwpinv_vat_number.getpaid-custom-payment-form-errors', __( 'Please enter your VAT number to verify your purchase is by an EU business.', 'invoicing' ) );
220 220
 
221
-		}
221
+        }
222 222
 
223
-		if ( empty( $vat_number ) ) {
224
-			return;
225
-		}
223
+        if ( empty( $vat_number ) ) {
224
+            return;
225
+        }
226 226
 
227
-		if ( wpinv_should_validate_vat_number() && ! wpinv_validate_vat_number( $vat_number, $submission->country ) ) {
228
-			throw new GetPaid_Payment_Exception( '.getpaid-error-billingwpinv_vat_number.getpaid-custom-payment-form-errors', __( 'Your VAT number is invalid', 'invoicing' ) );
229
-		}
227
+        if ( wpinv_should_validate_vat_number() && ! wpinv_validate_vat_number( $vat_number, $submission->country ) ) {
228
+            throw new GetPaid_Payment_Exception( '.getpaid-error-billingwpinv_vat_number.getpaid-custom-payment-form-errors', __( 'Your VAT number is invalid', 'invoicing' ) );
229
+        }
230 230
 
231
-		if ( wpinv_default_billing_country() == $submission->country && 'vat_too' == wpinv_get_option( 'vat_same_country_rule', 'vat_too' ) ) {
232
-			return;
233
-		}
231
+        if ( wpinv_default_billing_country() == $submission->country && 'vat_too' == wpinv_get_option( 'vat_same_country_rule', 'vat_too' ) ) {
232
+            return;
233
+        }
234 234
 
235
-		$this->skip_taxes = true;
236
-	}
235
+        $this->skip_taxes = true;
236
+    }
237 237
 
238 238
 }
Please login to merge, or discard this patch.
includes/reports/class-getpaid-reports-abstract-report.php 1 patch
Indentation   +177 added lines, -177 removed lines patch added patch discarded remove patch
@@ -12,69 +12,69 @@  discard block
 block discarded – undo
12 12
  */
13 13
 abstract class GetPaid_Reports_Abstract_Report {
14 14
 
15
-	/**
16
-	 * @var array
17
-	 */
18
-	public $stats;
19
-
20
-	/**
21
-	 * Class constructor.
22
-	 *
23
-	 */
24
-	public function __construct() {
25
-		$this->prepare_stats();
26
-	}
27
-
28
-	/**
29
-	 * Retrieves the current range.
30
-	 *
31
-	 */
32
-	public function get_range() {
33
-		$valid_ranges = $this->get_periods();
34
-
35
-		if ( isset( $_GET['date_range'] ) && array_key_exists( $_GET['date_range'], $valid_ranges ) ) {
36
-			return sanitize_key( $_GET['date_range'] );
37
-		}
38
-
39
-		return '7_days';
40
-	}
41
-
42
-	/**
43
-	 * Returns an array of date ranges.
44
-	 *
45
-	 * @return array
46
-	 */
47
-	public function get_periods() {
48
-
49
-		$periods = array(
15
+    /**
16
+     * @var array
17
+     */
18
+    public $stats;
19
+
20
+    /**
21
+     * Class constructor.
22
+     *
23
+     */
24
+    public function __construct() {
25
+        $this->prepare_stats();
26
+    }
27
+
28
+    /**
29
+     * Retrieves the current range.
30
+     *
31
+     */
32
+    public function get_range() {
33
+        $valid_ranges = $this->get_periods();
34
+
35
+        if ( isset( $_GET['date_range'] ) && array_key_exists( $_GET['date_range'], $valid_ranges ) ) {
36
+            return sanitize_key( $_GET['date_range'] );
37
+        }
38
+
39
+        return '7_days';
40
+    }
41
+
42
+    /**
43
+     * Returns an array of date ranges.
44
+     *
45
+     * @return array
46
+     */
47
+    public function get_periods() {
48
+
49
+        $periods = array(
50 50
             'today'     => __( 'Today', 'invoicing' ),
51 51
             'yesterday' => __( 'Yesterday', 'invoicing' ),
52 52
             '7_days'    => __( 'Last 7 days', 'invoicing' ),
53
-			'30_days'   => __( 'Last 30 days', 'invoicing' ),
54
-			'60_days'   => __( 'Last 60 days', 'invoicing' ),
55
-			'90_days'   => __( 'Last 90 days', 'invoicing' ),
56
-			'180_days'  => __( 'Last 180 days', 'invoicing' ),
57
-			'360_days'  => __( 'Last 360 days', 'invoicing' ),
58
-		);
59
-
60
-		return apply_filters( 'getpaid_earning_periods', $periods );
61
-	}
62
-
63
-	/**
64
-	 * Retrieves the current range's sql.
65
-	 *
66
-	 */
67
-	public function get_range_sql( $range, $date = 'CAST(meta.completed_date AS DATE)', $datetime = 'meta.comlpeted_date' ) {
53
+            '30_days'   => __( 'Last 30 days', 'invoicing' ),
54
+            '60_days'   => __( 'Last 60 days', 'invoicing' ),
55
+            '90_days'   => __( 'Last 90 days', 'invoicing' ),
56
+            '180_days'  => __( 'Last 180 days', 'invoicing' ),
57
+            '360_days'  => __( 'Last 360 days', 'invoicing' ),
58
+        );
59
+
60
+        return apply_filters( 'getpaid_earning_periods', $periods );
61
+    }
62
+
63
+    /**
64
+     * Retrieves the current range's sql.
65
+     *
66
+     */
67
+    public function get_range_sql( $range, $date = 'CAST(meta.completed_date AS DATE)', $datetime = 'meta.comlpeted_date' ) {
68 68
 
69 69
         // Prepare durations.
70 70
         $today                = current_time( 'Y-m-d' );
71
-		$yesterday            = date( 'Y-m-d', strtotime( '-1 day', current_time( 'timestamp' ) ) );
72
-		$seven_days_ago       = date( 'Y-m-d', strtotime( '-7 days', current_time( 'timestamp' ) ) );
73
-		$thirty_days_ago      = date( 'Y-m-d', strtotime( '-30 days', current_time( 'timestamp' ) ) );
74
-		$ninety_days_ago      = date( 'Y-m-d', strtotime( '-90 days', current_time( 'timestamp' ) ) );
75
-		$sixty_days_ago       = date( 'Y-m-d', strtotime( '-60 days', current_time( 'timestamp' ) ) );
76
-		$one_eighty_days_ago  = date( 'Y-m-d', strtotime( '-180 days', current_time( 'timestamp' ) ) );
77
-		$three_sixty_days_ago = date( 'Y-m-d', strtotime( '-360 days', current_time( 'timestamp' ) ) );
71
+        $yesterday            = date( 'Y-m-d', strtotime( '-1 day', current_time( 'timestamp' ) ) );
72
+        $seven_days_ago       = date( 'Y-m-d', strtotime( '-7 days', current_time( 'timestamp' ) ) );
73
+        $thirty_days_ago      = date( 'Y-m-d', strtotime( '-30 days', current_time( 'timestamp' ) ) );
74
+        $ninety_days_ago      = date( 'Y-m-d', strtotime( '-90 days', current_time( 'timestamp' ) ) );
75
+        $sixty_days_ago       = date( 'Y-m-d', strtotime( '-60 days', current_time( 'timestamp' ) ) );
76
+        $one_eighty_days_ago  = date( 'Y-m-d', strtotime( '-180 days', current_time( 'timestamp' ) ) );
77
+        $three_sixty_days_ago = date( 'Y-m-d', strtotime( '-360 days', current_time( 'timestamp' ) ) );
78 78
 
79 79
         $ranges = array(
80 80
 
@@ -91,130 +91,130 @@  discard block
 block discarded – undo
91 91
             '7_days'    => array(
92 92
                 "DATE($datetime)",
93 93
                 "$date BETWEEN '$seven_days_ago' AND '$today'",
94
-			),
94
+            ),
95 95
 
96
-			'30_days'   => array(
96
+            '30_days'   => array(
97 97
                 "DATE($datetime)",
98 98
                 "$date BETWEEN '$thirty_days_ago' AND '$today'",
99
-			),
99
+            ),
100 100
 
101
-			'60_days'   => array(
101
+            '60_days'   => array(
102 102
                 "DATE($datetime)",
103 103
                 "$date BETWEEN '$sixty_days_ago' AND '$today'",
104
-			),
104
+            ),
105 105
 
106
-			'90_days'   => array(
106
+            '90_days'   => array(
107 107
                 "WEEK($datetime)",
108 108
                 "$date BETWEEN '$ninety_days_ago' AND '$today'",
109
-			),
109
+            ),
110 110
 
111
-			'180_days'  => array(
111
+            '180_days'  => array(
112 112
                 "WEEK($datetime)",
113 113
                 "$date BETWEEN '$one_eighty_days_ago' AND '$today'",
114
-			),
114
+            ),
115 115
 
116
-			'360_days'  => array(
116
+            '360_days'  => array(
117 117
                 "WEEK($datetime)",
118 118
                 "$date BETWEEN '$three_sixty_days_ago' AND '$today'",
119 119
             ),
120 120
 
121 121
         );
122 122
 
123
-		$sql = isset( $ranges[ $range ] ) ? $ranges[ $range ] : $ranges['7_days'];
124
-		return apply_filters( 'getpaid_earning_graphs_get_range_sql', $sql, $range );
125
-
126
-	}
127
-
128
-	/**
129
-	 * Retrieves the hours in a day
130
-	 *
131
-	 */
132
-	public function get_hours_in_a_day() {
133
-
134
-		return array(
135
-			'12AM' => __( '12 AM', 'invoicing' ),
136
-			'1AM'  => __( '1 AM', 'invoicing' ),
137
-			'2AM'  => __( '2 AM', 'invoicing' ),
138
-			'3AM'  => __( '3 AM', 'invoicing' ),
139
-			'4AM'  => __( '4 AM', 'invoicing' ),
140
-			'5AM'  => __( '5 AM', 'invoicing' ),
141
-			'6AM'  => __( '6 AM', 'invoicing' ),
142
-			'7AM'  => __( '7 AM', 'invoicing' ),
143
-			'8AM'  => __( '8 AM', 'invoicing' ),
144
-			'9AM'  => __( '9 AM', 'invoicing' ),
145
-			'10AM' => __( '10 AM', 'invoicing' ),
146
-			'11AM' => __( '11 AM', 'invoicing' ),
147
-			'12pm' => __( '12 PM', 'invoicing' ),
148
-			'1PM'  => __( '1 PM', 'invoicing' ),
149
-			'2PM'  => __( '2 PM', 'invoicing' ),
150
-			'3PM'  => __( '3 PM', 'invoicing' ),
151
-			'4PM'  => __( '4 PM', 'invoicing' ),
152
-			'5PM'  => __( '5 PM', 'invoicing' ),
153
-			'6PM'  => __( '6 PM', 'invoicing' ),
154
-			'7PM'  => __( '7 PM', 'invoicing' ),
155
-			'8PM'  => __( '8 PM', 'invoicing' ),
156
-			'9PM'  => __( '9 PM', 'invoicing' ),
157
-			'10PM' => __( '10 PM', 'invoicing' ),
158
-			'11PM' => __( '11 PM', 'invoicing' ),
159
-		);
160
-
161
-	}
162
-
163
-	/**
164
-	 * Retrieves the days in a period
165
-	 *
166
-	 */
167
-	public function get_days_in_period( $days ) {
168
-
169
-		$return = array();
170
-		$format = 'Y-m-d';
171
-
172
-		if ( $days < 8 ) {
173
-			$format = 'D';
174
-		}
175
-
176
-		if ( $days < 32 ) {
177
-			$format = 'M j';
178
-		}
179
-
180
-		while ( $days > 0 ) {
181
-
182
-			$key            = date( 'Y-m-d', strtotime( "-$days days", current_time( 'timestamp' ) ) );
183
-			$label          = date_i18n( $format, strtotime( "-$days days", current_time( 'timestamp' ) ) );
184
-			$return[ $key ] = $label;
185
-			$days--;
186
-
187
-		}
188
-
189
-		return $return;
190
-	}
191
-
192
-	/**
193
-	 * Retrieves the weeks in a period
194
-	 *
195
-	 */
196
-	public function get_weeks_in_period( $days ) {
197
-
198
-		$return = array();
199
-
200
-		while ( $days > 0 ) {
201
-
202
-			$key            = date( 'W', strtotime( "-$days days", current_time( 'timestamp' ) ) );
203
-			$label          = date_i18n( 'Y-m-d', strtotime( "-$days days", current_time( 'timestamp' ) ) );
204
-			$return[ $key ] = $label;
205
-			$days--;
206
-
207
-		}
208
-
209
-		return $return;
210
-	}
211
-
212
-	/**
213
-	 * Displays the report card.
214
-	 *
215
-	 */
216
-	public function display() {
217
-		?>
123
+        $sql = isset( $ranges[ $range ] ) ? $ranges[ $range ] : $ranges['7_days'];
124
+        return apply_filters( 'getpaid_earning_graphs_get_range_sql', $sql, $range );
125
+
126
+    }
127
+
128
+    /**
129
+     * Retrieves the hours in a day
130
+     *
131
+     */
132
+    public function get_hours_in_a_day() {
133
+
134
+        return array(
135
+            '12AM' => __( '12 AM', 'invoicing' ),
136
+            '1AM'  => __( '1 AM', 'invoicing' ),
137
+            '2AM'  => __( '2 AM', 'invoicing' ),
138
+            '3AM'  => __( '3 AM', 'invoicing' ),
139
+            '4AM'  => __( '4 AM', 'invoicing' ),
140
+            '5AM'  => __( '5 AM', 'invoicing' ),
141
+            '6AM'  => __( '6 AM', 'invoicing' ),
142
+            '7AM'  => __( '7 AM', 'invoicing' ),
143
+            '8AM'  => __( '8 AM', 'invoicing' ),
144
+            '9AM'  => __( '9 AM', 'invoicing' ),
145
+            '10AM' => __( '10 AM', 'invoicing' ),
146
+            '11AM' => __( '11 AM', 'invoicing' ),
147
+            '12pm' => __( '12 PM', 'invoicing' ),
148
+            '1PM'  => __( '1 PM', 'invoicing' ),
149
+            '2PM'  => __( '2 PM', 'invoicing' ),
150
+            '3PM'  => __( '3 PM', 'invoicing' ),
151
+            '4PM'  => __( '4 PM', 'invoicing' ),
152
+            '5PM'  => __( '5 PM', 'invoicing' ),
153
+            '6PM'  => __( '6 PM', 'invoicing' ),
154
+            '7PM'  => __( '7 PM', 'invoicing' ),
155
+            '8PM'  => __( '8 PM', 'invoicing' ),
156
+            '9PM'  => __( '9 PM', 'invoicing' ),
157
+            '10PM' => __( '10 PM', 'invoicing' ),
158
+            '11PM' => __( '11 PM', 'invoicing' ),
159
+        );
160
+
161
+    }
162
+
163
+    /**
164
+     * Retrieves the days in a period
165
+     *
166
+     */
167
+    public function get_days_in_period( $days ) {
168
+
169
+        $return = array();
170
+        $format = 'Y-m-d';
171
+
172
+        if ( $days < 8 ) {
173
+            $format = 'D';
174
+        }
175
+
176
+        if ( $days < 32 ) {
177
+            $format = 'M j';
178
+        }
179
+
180
+        while ( $days > 0 ) {
181
+
182
+            $key            = date( 'Y-m-d', strtotime( "-$days days", current_time( 'timestamp' ) ) );
183
+            $label          = date_i18n( $format, strtotime( "-$days days", current_time( 'timestamp' ) ) );
184
+            $return[ $key ] = $label;
185
+            $days--;
186
+
187
+        }
188
+
189
+        return $return;
190
+    }
191
+
192
+    /**
193
+     * Retrieves the weeks in a period
194
+     *
195
+     */
196
+    public function get_weeks_in_period( $days ) {
197
+
198
+        $return = array();
199
+
200
+        while ( $days > 0 ) {
201
+
202
+            $key            = date( 'W', strtotime( "-$days days", current_time( 'timestamp' ) ) );
203
+            $label          = date_i18n( 'Y-m-d', strtotime( "-$days days", current_time( 'timestamp' ) ) );
204
+            $return[ $key ] = $label;
205
+            $days--;
206
+
207
+        }
208
+
209
+        return $return;
210
+    }
211
+
212
+    /**
213
+     * Displays the report card.
214
+     *
215
+     */
216
+    public function display() {
217
+        ?>
218 218
 
219 219
 			<div class="row">
220 220
 				<div class="col-12">
@@ -228,20 +228,20 @@  discard block
 block discarded – undo
228 228
 
229 229
 		<?php
230 230
 
231
-	}
232
-
233
-	/**
234
-	 * Prepares the report stats.
235
-	 *
236
-	 * Extend this in child classes.
237
-	 */
238
-	abstract public function prepare_stats();
239
-
240
-	/**
241
-	 * Displays the actual report.
242
-	 *
243
-	 * Extend this in child classes.
244
-	 */
245
-	abstract public function display_stats();
231
+    }
232
+
233
+    /**
234
+     * Prepares the report stats.
235
+     *
236
+     * Extend this in child classes.
237
+     */
238
+    abstract public function prepare_stats();
239
+
240
+    /**
241
+     * Displays the actual report.
242
+     *
243
+     * Extend this in child classes.
244
+     */
245
+    abstract public function display_stats();
246 246
 
247 247
 }
Please login to merge, or discard this patch.
includes/reports/class-getpaid-reports-report-items.php 1 patch
Indentation   +82 added lines, -82 removed lines patch added patch discarded remove patch
@@ -12,23 +12,23 @@  discard block
 block discarded – undo
12 12
  */
13 13
 class GetPaid_Reports_Report_Items extends GetPaid_Reports_Abstract_Report {
14 14
 
15
-	/**
16
-	 * @var string
17
-	 */
18
-	public $field = 'item_name';
19
-
20
-	/**
21
-	 * Retrieves the earning sql.
22
-	 *
23
-	 */
24
-	public function get_sql( $range ) {
25
-		global $wpdb;
26
-
27
-		$table      = $wpdb->prefix . 'getpaid_invoices';
28
-		$table2     = $wpdb->prefix . 'getpaid_invoice_items';
29
-		$clauses    = $this->get_range_sql( $range );
30
-
31
-		$sql        = "SELECT
15
+    /**
16
+     * @var string
17
+     */
18
+    public $field = 'item_name';
19
+
20
+    /**
21
+     * Retrieves the earning sql.
22
+     *
23
+     */
24
+    public function get_sql( $range ) {
25
+        global $wpdb;
26
+
27
+        $table      = $wpdb->prefix . 'getpaid_invoices';
28
+        $table2     = $wpdb->prefix . 'getpaid_invoice_items';
29
+        $clauses    = $this->get_range_sql( $range );
30
+
31
+        $sql        = "SELECT
32 32
 				item.item_name AS item_name,
33 33
 				item.item_id AS item_id,
34 34
 				SUM(price) as total
@@ -43,91 +43,91 @@  discard block
 block discarded – undo
43 43
 			ORDER BY total DESC
44 44
         ";
45 45
 
46
-		return apply_filters( 'getpaid_items_graphs_get_sql', $sql, $range );
46
+        return apply_filters( 'getpaid_items_graphs_get_sql', $sql, $range );
47 47
 
48
-	}
48
+    }
49 49
 
50
-	/**
51
-	 * Prepares the report stats.
52
-	 *
53
-	 */
54
-	public function prepare_stats() {
55
-		global $wpdb;
56
-		$this->stats = $wpdb->get_results( $this->get_sql( $this->get_range() ) );
57
-		$this->stats = $this->normalize_stats( $this->stats );
58
-	}
50
+    /**
51
+     * Prepares the report stats.
52
+     *
53
+     */
54
+    public function prepare_stats() {
55
+        global $wpdb;
56
+        $this->stats = $wpdb->get_results( $this->get_sql( $this->get_range() ) );
57
+        $this->stats = $this->normalize_stats( $this->stats );
58
+    }
59 59
 
60
-	/**
61
-	 * Normalizes the report stats.
62
-	 *
63
-	 */
64
-	public function normalize_stats( $stats ) {
65
-		$normalized = array();
66
-		$others     = 0;
67
-		$did        = 0;
60
+    /**
61
+     * Normalizes the report stats.
62
+     *
63
+     */
64
+    public function normalize_stats( $stats ) {
65
+        $normalized = array();
66
+        $others     = 0;
67
+        $did        = 0;
68 68
 
69
-		foreach ( $stats as $stat ) {
69
+        foreach ( $stats as $stat ) {
70 70
 
71
-			if ( $did > 4 ) {
71
+            if ( $did > 4 ) {
72 72
 
73
-				$others += wpinv_round_amount( wpinv_sanitize_amount( $stat->total ) );
73
+                $others += wpinv_round_amount( wpinv_sanitize_amount( $stat->total ) );
74 74
 
75
-			} else {
75
+            } else {
76 76
 
77
-				$normalized[] = array(
78
-					'total'     => wpinv_round_amount( wpinv_sanitize_amount( $stat->total ) ),
79
-					'item_name' => strip_tags( $stat->item_name ),
80
-				);
77
+                $normalized[] = array(
78
+                    'total'     => wpinv_round_amount( wpinv_sanitize_amount( $stat->total ) ),
79
+                    'item_name' => strip_tags( $stat->item_name ),
80
+                );
81 81
 
82
-			}
82
+            }
83 83
 
84
-			$did++;
85
-		}
84
+            $did++;
85
+        }
86 86
 
87
-		if ( $others > 0 ) {
87
+        if ( $others > 0 ) {
88 88
 
89
-			$normalized[] = array(
90
-				'total'     => wpinv_round_amount( wpinv_sanitize_amount( $others ) ),
91
-				'item_name' => esc_html__( 'Others', 'invoicing' ),
92
-			);
89
+            $normalized[] = array(
90
+                'total'     => wpinv_round_amount( wpinv_sanitize_amount( $others ) ),
91
+                'item_name' => esc_html__( 'Others', 'invoicing' ),
92
+            );
93 93
 
94
-		}
94
+        }
95 95
 
96
-		return $normalized;
97
-	}
96
+        return $normalized;
97
+    }
98 98
 
99
-	/**
100
-	 * Retrieves report data.
101
-	 *
102
-	 */
103
-	public function get_data() {
99
+    /**
100
+     * Retrieves report data.
101
+     *
102
+     */
103
+    public function get_data() {
104 104
 
105
-		$data     = wp_list_pluck( $this->stats, 'total' );
106
-		$colors   = array( '#009688', '#4caf50', '#8bc34a', '#00bcd4', '#03a9f4', '#2196f3' );
105
+        $data     = wp_list_pluck( $this->stats, 'total' );
106
+        $colors   = array( '#009688', '#4caf50', '#8bc34a', '#00bcd4', '#03a9f4', '#2196f3' );
107 107
 
108
-		shuffle( $colors );
108
+        shuffle( $colors );
109 109
 
110
-		return array(
111
-			'data'            => $data,
112
-			'backgroundColor' => $colors,
113
-		);
110
+        return array(
111
+            'data'            => $data,
112
+            'backgroundColor' => $colors,
113
+        );
114 114
 
115
-	}
115
+    }
116 116
 
117
-	/**
118
-	 * Retrieves report labels.
119
-	 *
120
-	 */
121
-	public function get_labels() {
122
-		return wp_list_pluck( $this->stats, 'item_name' );
123
-	}
117
+    /**
118
+     * Retrieves report labels.
119
+     *
120
+     */
121
+    public function get_labels() {
122
+        return wp_list_pluck( $this->stats, 'item_name' );
123
+    }
124 124
 
125
-	/**
126
-	 * Displays the actual report.
127
-	 *
128
-	 */
129
-	public function display_stats() {
130
-		?>
125
+    /**
126
+     * Displays the actual report.
127
+     *
128
+     */
129
+    public function display_stats() {
130
+        ?>
131 131
 
132 132
 			<canvas id="getpaid-chartjs-earnings-items"></canvas>
133 133
 
@@ -156,6 +156,6 @@  discard block
 block discarded – undo
156 156
 			</script>
157 157
 
158 158
 		<?php
159
-	}
159
+    }
160 160
 
161 161
 }
Please login to merge, or discard this patch.
includes/reports/class-getpaid-reports-report-earnings.php 1 patch
Indentation   +133 added lines, -133 removed lines patch added patch discarded remove patch
@@ -12,43 +12,43 @@  discard block
 block discarded – undo
12 12
  */
13 13
 class GetPaid_Reports_Report_Earnings extends GetPaid_Reports_Abstract_Report {
14 14
 
15
-	/**
16
-	 * Retrieves the earning graphs.
17
-	 *
18
-	 */
19
-	public function get_graphs() {
15
+    /**
16
+     * Retrieves the earning graphs.
17
+     *
18
+     */
19
+    public function get_graphs() {
20 20
 
21
-		$graphs = array(
21
+        $graphs = array(
22 22
 
23 23
             'total'      => __( 'Earnings', 'invoicing' ),
24 24
             'discount'   => __( 'Discount', 'invoicing' ),
25
-			'fees_total' => __( 'Fees', 'invoicing' ),
26
-			'tax'        => __( 'Tax', 'invoicing' ),
25
+            'fees_total' => __( 'Fees', 'invoicing' ),
26
+            'tax'        => __( 'Tax', 'invoicing' ),
27 27
 
28
-		);
28
+        );
29 29
 
30
-		return apply_filters( 'getpaid_earning_graphs', $graphs );
30
+        return apply_filters( 'getpaid_earning_graphs', $graphs );
31 31
 
32
-	}
32
+    }
33 33
 
34
-	/**
35
-	 * Retrieves the earning sql.
36
-	 *
37
-	 */
38
-	public function get_sql( $range ) {
39
-		global $wpdb;
34
+    /**
35
+     * Retrieves the earning sql.
36
+     *
37
+     */
38
+    public function get_sql( $range ) {
39
+        global $wpdb;
40 40
 
41
-		$table      = $wpdb->prefix . 'getpaid_invoices';
42
-		$clauses    = $this->get_range_sql( $range );
43
-		$graphs     = array_keys( $this->get_graphs() );
44
-		$graphs_sql = array();
41
+        $table      = $wpdb->prefix . 'getpaid_invoices';
42
+        $clauses    = $this->get_range_sql( $range );
43
+        $graphs     = array_keys( $this->get_graphs() );
44
+        $graphs_sql = array();
45 45
 
46
-		foreach ( $graphs as $graph ) {
47
-			$graphs_sql[] = "SUM( meta.$graph ) AS $graph";
48
-		}
46
+        foreach ( $graphs as $graph ) {
47
+            $graphs_sql[] = "SUM( meta.$graph ) AS $graph";
48
+        }
49 49
 
50
-		$graphs_sql = implode( ', ', $graphs_sql );
51
-		$sql        = "SELECT {$clauses[0]} AS completed_date, $graphs_sql
50
+        $graphs_sql = implode( ', ', $graphs_sql );
51
+        $sql        = "SELECT {$clauses[0]} AS completed_date, $graphs_sql
52 52
             FROM $wpdb->posts
53 53
             LEFT JOIN $table as meta ON meta.post_id = $wpdb->posts.ID
54 54
             WHERE meta.post_id IS NOT NULL
@@ -58,94 +58,94 @@  discard block
 block discarded – undo
58 58
             GROUP BY {$clauses[0]}
59 59
         ";
60 60
 
61
-		return apply_filters( 'getpaid_earning_graphs_get_sql', $sql, $range );
62
-
63
-	}
64
-
65
-	/**
66
-	 * Prepares the report stats.
67
-	 *
68
-	 */
69
-	public function prepare_stats() {
70
-		global $wpdb;
71
-		$this->stats = $wpdb->get_results( $this->get_sql( $this->get_range() ) );
72
-	}
73
-
74
-	/**
75
-	 * Retrieves report labels.
76
-	 *
77
-	 */
78
-	public function get_labels( $range ) {
79
-
80
-		$labels = array(
81
-			'today'     => $this->get_hours_in_a_day(),
82
-			'yesterday' => $this->get_hours_in_a_day(),
83
-			'7_days'    => $this->get_days_in_period( 7 ),
84
-			'30_days'   => $this->get_days_in_period( 30 ),
85
-			'60_days'   => $this->get_days_in_period( 60 ),
86
-			'90_days'   => $this->get_weeks_in_period( 90 ),
87
-			'180_days'  => $this->get_weeks_in_period( 180 ),
88
-			'360_days'  => $this->get_weeks_in_period( 360 ),
89
-		);
90
-
91
-		$label = isset( $labels[ $range ] ) ? $labels[ $range ] : $labels['7_days'];
92
-		return apply_filters( 'getpaid_earning_graphs_get_labels', $label, $range );
93
-	}
94
-
95
-	/**
96
-	 * Retrieves report datasets.
97
-	 *
98
-	 */
99
-	public function get_datasets( $labels ) {
100
-
101
-		$datasets = array();
102
-
103
-		foreach ( $this->get_graphs() as $key => $label ) {
104
-			$datasets[ $key ] = array(
105
-				'label' => $label,
106
-				'data'  => $this->get_data( $key, $labels ),
107
-			);
108
-		}
109
-
110
-		return apply_filters( 'getpaid_earning_graphs_get_datasets', $datasets, $labels );
111
-	}
112
-
113
-	/**
114
-	 * Retrieves report data.
115
-	 *
116
-	 */
117
-	public function get_data( $key, $labels ) {
118
-
119
-		$data     = wp_list_pluck( $this->stats, $key, 'completed_date' );
120
-		$prepared = array();
121
-
122
-		foreach ( $labels as $label ) {
123
-
124
-			$value = 0;
125
-			if ( isset( $data[ $label ] ) ) {
126
-				$value = wpinv_round_amount( wpinv_sanitize_amount( $data[ $label ] ) );
127
-			}
128
-
129
-			$prepared[] = $value;
130
-		}
131
-
132
-		return apply_filters( 'getpaid_earning_graphs_get_data', $prepared, $key, $labels );
133
-
134
-	}
135
-
136
-	/**
137
-	 * Displays the report card.
138
-	 *
139
-	 */
140
-	public function display() {
141
-
142
-		$labels     = $this->get_labels( $this->get_range() );
143
-		$chart_data = array(
144
-			'labels'   => array_values( $labels ),
145
-			'datasets' => $this->get_datasets( array_keys( $labels ) ),
146
-		);
147
-
148
-		?>
61
+        return apply_filters( 'getpaid_earning_graphs_get_sql', $sql, $range );
62
+
63
+    }
64
+
65
+    /**
66
+     * Prepares the report stats.
67
+     *
68
+     */
69
+    public function prepare_stats() {
70
+        global $wpdb;
71
+        $this->stats = $wpdb->get_results( $this->get_sql( $this->get_range() ) );
72
+    }
73
+
74
+    /**
75
+     * Retrieves report labels.
76
+     *
77
+     */
78
+    public function get_labels( $range ) {
79
+
80
+        $labels = array(
81
+            'today'     => $this->get_hours_in_a_day(),
82
+            'yesterday' => $this->get_hours_in_a_day(),
83
+            '7_days'    => $this->get_days_in_period( 7 ),
84
+            '30_days'   => $this->get_days_in_period( 30 ),
85
+            '60_days'   => $this->get_days_in_period( 60 ),
86
+            '90_days'   => $this->get_weeks_in_period( 90 ),
87
+            '180_days'  => $this->get_weeks_in_period( 180 ),
88
+            '360_days'  => $this->get_weeks_in_period( 360 ),
89
+        );
90
+
91
+        $label = isset( $labels[ $range ] ) ? $labels[ $range ] : $labels['7_days'];
92
+        return apply_filters( 'getpaid_earning_graphs_get_labels', $label, $range );
93
+    }
94
+
95
+    /**
96
+     * Retrieves report datasets.
97
+     *
98
+     */
99
+    public function get_datasets( $labels ) {
100
+
101
+        $datasets = array();
102
+
103
+        foreach ( $this->get_graphs() as $key => $label ) {
104
+            $datasets[ $key ] = array(
105
+                'label' => $label,
106
+                'data'  => $this->get_data( $key, $labels ),
107
+            );
108
+        }
109
+
110
+        return apply_filters( 'getpaid_earning_graphs_get_datasets', $datasets, $labels );
111
+    }
112
+
113
+    /**
114
+     * Retrieves report data.
115
+     *
116
+     */
117
+    public function get_data( $key, $labels ) {
118
+
119
+        $data     = wp_list_pluck( $this->stats, $key, 'completed_date' );
120
+        $prepared = array();
121
+
122
+        foreach ( $labels as $label ) {
123
+
124
+            $value = 0;
125
+            if ( isset( $data[ $label ] ) ) {
126
+                $value = wpinv_round_amount( wpinv_sanitize_amount( $data[ $label ] ) );
127
+            }
128
+
129
+            $prepared[] = $value;
130
+        }
131
+
132
+        return apply_filters( 'getpaid_earning_graphs_get_data', $prepared, $key, $labels );
133
+
134
+    }
135
+
136
+    /**
137
+     * Displays the report card.
138
+     *
139
+     */
140
+    public function display() {
141
+
142
+        $labels     = $this->get_labels( $this->get_range() );
143
+        $chart_data = array(
144
+            'labels'   => array_values( $labels ),
145
+            'datasets' => $this->get_datasets( array_keys( $labels ) ),
146
+        );
147
+
148
+        ?>
149 149
 
150 150
 			<?php foreach ( $chart_data['datasets'] as $key => $dataset ) : ?>
151 151
 				<div class="row mb-4">
@@ -165,15 +165,15 @@  discard block
 block discarded – undo
165 165
 
166 166
 		<?php
167 167
 
168
-	}
168
+    }
169 169
 
170
-	/**
171
-	 * Displays the actual report.
172
-	 *
173
-	 */
174
-	public function display_graph( $key, $dataset, $labels ) {
170
+    /**
171
+     * Displays the actual report.
172
+     *
173
+     */
174
+    public function display_graph( $key, $dataset, $labels ) {
175 175
 
176
-		?>
176
+        ?>
177 177
 
178 178
 		<canvas id="getpaid-chartjs-earnings-<?php echo esc_attr( $key ); ?>"></canvas>
179 179
 
@@ -223,20 +223,20 @@  discard block
 block discarded – undo
223 223
 		</script>
224 224
 
225 225
 		<?php
226
-	}
226
+    }
227 227
 
228
-	/**
229
-	 * Displays the actual report.
230
-	 *
231
-	 */
232
-	public function display_stats() {}
228
+    /**
229
+     * Displays the actual report.
230
+     *
231
+     */
232
+    public function display_stats() {}
233 233
 
234
-	/**
235
-	 * Displays the range selector.
236
-	 *
237
-	 */
238
-	public function display_range_selector() {
234
+    /**
235
+     * Displays the range selector.
236
+     *
237
+     */
238
+    public function display_range_selector() {
239 239
 
240
-	}
240
+    }
241 241
 
242 242
 }
Please login to merge, or discard this patch.
includes/reports/class-getpaid-reports-report-discounts.php 1 patch
Indentation   +81 added lines, -81 removed lines patch added patch discarded remove patch
@@ -12,22 +12,22 @@  discard block
 block discarded – undo
12 12
  */
13 13
 class GetPaid_Reports_Report_Discounts extends GetPaid_Reports_Abstract_Report {
14 14
 
15
-	/**
16
-	 * @var string
17
-	 */
18
-	public $field = 'discount_code';
19
-
20
-	/**
21
-	 * Retrieves the discounts sql.
22
-	 *
23
-	 */
24
-	public function get_sql( $range ) {
25
-		global $wpdb;
26
-
27
-		$table      = $wpdb->prefix . 'getpaid_invoices';
28
-		$clauses    = $this->get_range_sql( $range );
29
-
30
-		$sql        = "SELECT
15
+    /**
16
+     * @var string
17
+     */
18
+    public $field = 'discount_code';
19
+
20
+    /**
21
+     * Retrieves the discounts sql.
22
+     *
23
+     */
24
+    public function get_sql( $range ) {
25
+        global $wpdb;
26
+
27
+        $table      = $wpdb->prefix . 'getpaid_invoices';
28
+        $clauses    = $this->get_range_sql( $range );
29
+
30
+        $sql        = "SELECT
31 31
 				meta.discount_code AS discount_code,
32 32
 				SUM(total) as total
33 33
             FROM $wpdb->posts
@@ -41,91 +41,91 @@  discard block
 block discarded – undo
41 41
 			ORDER BY total DESC
42 42
         ";
43 43
 
44
-		return apply_filters( 'getpaid_discounts_graphs_get_sql', $sql, $range );
44
+        return apply_filters( 'getpaid_discounts_graphs_get_sql', $sql, $range );
45 45
 
46
-	}
46
+    }
47 47
 
48
-	/**
49
-	 * Prepares the report stats.
50
-	 *
51
-	 */
52
-	public function prepare_stats() {
53
-		global $wpdb;
54
-		$this->stats = $wpdb->get_results( $this->get_sql( $this->get_range() ) );
55
-		$this->stats = $this->normalize_stats( $this->stats );
56
-	}
48
+    /**
49
+     * Prepares the report stats.
50
+     *
51
+     */
52
+    public function prepare_stats() {
53
+        global $wpdb;
54
+        $this->stats = $wpdb->get_results( $this->get_sql( $this->get_range() ) );
55
+        $this->stats = $this->normalize_stats( $this->stats );
56
+    }
57 57
 
58
-	/**
59
-	 * Normalizes the report stats.
60
-	 *
61
-	 */
62
-	public function normalize_stats( $stats ) {
63
-		$normalized = array();
64
-		$others     = 0;
65
-		$did        = 0;
58
+    /**
59
+     * Normalizes the report stats.
60
+     *
61
+     */
62
+    public function normalize_stats( $stats ) {
63
+        $normalized = array();
64
+        $others     = 0;
65
+        $did        = 0;
66 66
 
67
-		foreach ( $stats as $stat ) {
67
+        foreach ( $stats as $stat ) {
68 68
 
69
-			if ( $did > 4 ) {
69
+            if ( $did > 4 ) {
70 70
 
71
-				$others += wpinv_round_amount( wpinv_sanitize_amount( $stat->total ) );
71
+                $others += wpinv_round_amount( wpinv_sanitize_amount( $stat->total ) );
72 72
 
73
-			} else {
73
+            } else {
74 74
 
75
-				$normalized[] = array(
76
-					'total'         => wpinv_round_amount( wpinv_sanitize_amount( $stat->total ) ),
77
-					'discount_code' => strip_tags( $stat->discount_code ),
78
-				);
75
+                $normalized[] = array(
76
+                    'total'         => wpinv_round_amount( wpinv_sanitize_amount( $stat->total ) ),
77
+                    'discount_code' => strip_tags( $stat->discount_code ),
78
+                );
79 79
 
80
-			}
80
+            }
81 81
 
82
-			$did++;
83
-		}
82
+            $did++;
83
+        }
84 84
 
85
-		if ( $others > 0 ) {
85
+        if ( $others > 0 ) {
86 86
 
87
-			$normalized[] = array(
88
-				'total'         => wpinv_round_amount( wpinv_sanitize_amount( $others ) ),
89
-				'discount_code' => esc_html__( 'Others', 'invoicing' ),
90
-			);
87
+            $normalized[] = array(
88
+                'total'         => wpinv_round_amount( wpinv_sanitize_amount( $others ) ),
89
+                'discount_code' => esc_html__( 'Others', 'invoicing' ),
90
+            );
91 91
 
92
-		}
92
+        }
93 93
 
94
-		return $normalized;
95
-	}
94
+        return $normalized;
95
+    }
96 96
 
97
-	/**
98
-	 * Retrieves report data.
99
-	 *
100
-	 */
101
-	public function get_data() {
97
+    /**
98
+     * Retrieves report data.
99
+     *
100
+     */
101
+    public function get_data() {
102 102
 
103
-		$data     = wp_list_pluck( $this->stats, 'total' );
104
-		$colors   = array( '#009688', '#4caf50', '#8bc34a', '#00bcd4', '#03a9f4', '#2196f3' );
103
+        $data     = wp_list_pluck( $this->stats, 'total' );
104
+        $colors   = array( '#009688', '#4caf50', '#8bc34a', '#00bcd4', '#03a9f4', '#2196f3' );
105 105
 
106
-		shuffle( $colors );
106
+        shuffle( $colors );
107 107
 
108
-		return array(
109
-			'data'            => $data,
110
-			'backgroundColor' => $colors,
111
-		);
108
+        return array(
109
+            'data'            => $data,
110
+            'backgroundColor' => $colors,
111
+        );
112 112
 
113
-	}
113
+    }
114 114
 
115
-	/**
116
-	 * Retrieves report labels.
117
-	 *
118
-	 */
119
-	public function get_labels() {
120
-		return wp_list_pluck( $this->stats, 'discount_code' );
121
-	}
115
+    /**
116
+     * Retrieves report labels.
117
+     *
118
+     */
119
+    public function get_labels() {
120
+        return wp_list_pluck( $this->stats, 'discount_code' );
121
+    }
122 122
 
123
-	/**
124
-	 * Displays the actual report.
125
-	 *
126
-	 */
127
-	public function display_stats() {
128
-		?>
123
+    /**
124
+     * Displays the actual report.
125
+     *
126
+     */
127
+    public function display_stats() {
128
+        ?>
129 129
 
130 130
 			<canvas id="getpaid-chartjs-earnings-discount_code"></canvas>
131 131
 
@@ -154,6 +154,6 @@  discard block
 block discarded – undo
154 154
 			</script>
155 155
 
156 156
 		<?php
157
-	}
157
+    }
158 158
 
159 159
 }
Please login to merge, or discard this patch.
includes/reports/class-getpaid-reports-report.php 1 patch
Indentation   +180 added lines, -180 removed lines patch added patch discarded remove patch
@@ -12,88 +12,88 @@  discard block
 block discarded – undo
12 12
  */
13 13
 class GetPaid_Reports_Report {
14 14
 
15
-	/**
16
-	 * @var array
17
-	 */
18
-	public $views;
15
+    /**
16
+     * @var array
17
+     */
18
+    public $views;
19 19
 
20
-	/**
21
-	 * Class constructor.
22
-	 *
23
-	 */
24
-	public function __construct() {
20
+    /**
21
+     * Class constructor.
22
+     *
23
+     */
24
+    public function __construct() {
25 25
 
26
-		$this->views        = array(
26
+        $this->views        = array(
27 27
 
28 28
             'items'     => array(
29
-				'label' => __( 'Items', 'invoicing' ),
30
-				'class' => 'GetPaid_Reports_Report_Items',
31
-			),
29
+                'label' => __( 'Items', 'invoicing' ),
30
+                'class' => 'GetPaid_Reports_Report_Items',
31
+            ),
32 32
 
33
-			'gateways'  => array(
34
-				'label' => __( 'Payment Methods', 'invoicing' ),
35
-				'class' => 'GetPaid_Reports_Report_Gateways',
36
-			),
33
+            'gateways'  => array(
34
+                'label' => __( 'Payment Methods', 'invoicing' ),
35
+                'class' => 'GetPaid_Reports_Report_Gateways',
36
+            ),
37 37
 
38
-			'discounts' => array(
39
-				'label' => __( 'Discount Codes', 'invoicing' ),
40
-				'class' => 'GetPaid_Reports_Report_Discounts',
41
-			),
38
+            'discounts' => array(
39
+                'label' => __( 'Discount Codes', 'invoicing' ),
40
+                'class' => 'GetPaid_Reports_Report_Discounts',
41
+            ),
42 42
 
43 43
         );
44 44
 
45
-		$this->views        = apply_filters( 'wpinv_report_views', $this->views );
46
-
47
-	}
48
-
49
-	/**
50
-	 * Retrieves the current range.
51
-	 *
52
-	 */
53
-	public function get_range() {
54
-		$valid_ranges = $this->get_periods();
55
-
56
-		if ( isset( $_GET['date_range'] ) && array_key_exists( $_GET['date_range'], $valid_ranges ) ) {
57
-			return sanitize_key( $_GET['date_range'] );
58
-		}
59
-
60
-		return '7_days';
61
-	}
62
-
63
-	/**
64
-	 * Returns an array of date ranges.
65
-	 *
66
-	 * @return array
67
-	 */
68
-	public function get_periods() {
69
-
70
-		$periods = array(
71
-			'today'        => __( 'Today', 'invoicing' ),
72
-			'yesterday'    => __( 'Yesterday', 'invoicing' ),
73
-			'week'         => __( 'This week', 'invoicing' ),
74
-			'last_week'    => __( 'Last week', 'invoicing' ),
75
-			'7_days'       => __( 'Last 7 days', 'invoicing' ),
76
-			'month'        => __( 'This month', 'invoicing' ),
77
-			'last_month'   => __( 'Last month', 'invoicing' ),
78
-			'30_days'      => __( 'Last 30 days', 'invoicing' ),
79
-			'quarter'      => __( 'This Quarter', 'invoicing' ),
80
-			'last_quarter' => __( 'Last Quarter', 'invoicing' ),
81
-			'year'         => __( 'This year', 'invoicing' ),
82
-			'last_year'    => __( 'Last Year', 'invoicing' ),
83
-			'custom'       => __( 'Custom Date Range', 'invoicing' ),
84
-		);
85
-
86
-		return apply_filters( 'getpaid_earning_periods', $periods );
87
-	}
88
-
89
-	/**
90
-	 * Displays the range selector.
91
-	 *
92
-	 */
93
-	public function display_range_selector() {
94
-
95
-		$range = $this->get_range();
96
-		?>
45
+        $this->views        = apply_filters( 'wpinv_report_views', $this->views );
46
+
47
+    }
48
+
49
+    /**
50
+     * Retrieves the current range.
51
+     *
52
+     */
53
+    public function get_range() {
54
+        $valid_ranges = $this->get_periods();
55
+
56
+        if ( isset( $_GET['date_range'] ) && array_key_exists( $_GET['date_range'], $valid_ranges ) ) {
57
+            return sanitize_key( $_GET['date_range'] );
58
+        }
59
+
60
+        return '7_days';
61
+    }
62
+
63
+    /**
64
+     * Returns an array of date ranges.
65
+     *
66
+     * @return array
67
+     */
68
+    public function get_periods() {
69
+
70
+        $periods = array(
71
+            'today'        => __( 'Today', 'invoicing' ),
72
+            'yesterday'    => __( 'Yesterday', 'invoicing' ),
73
+            'week'         => __( 'This week', 'invoicing' ),
74
+            'last_week'    => __( 'Last week', 'invoicing' ),
75
+            '7_days'       => __( 'Last 7 days', 'invoicing' ),
76
+            'month'        => __( 'This month', 'invoicing' ),
77
+            'last_month'   => __( 'Last month', 'invoicing' ),
78
+            '30_days'      => __( 'Last 30 days', 'invoicing' ),
79
+            'quarter'      => __( 'This Quarter', 'invoicing' ),
80
+            'last_quarter' => __( 'Last Quarter', 'invoicing' ),
81
+            'year'         => __( 'This year', 'invoicing' ),
82
+            'last_year'    => __( 'Last Year', 'invoicing' ),
83
+            'custom'       => __( 'Custom Date Range', 'invoicing' ),
84
+        );
85
+
86
+        return apply_filters( 'getpaid_earning_periods', $periods );
87
+    }
88
+
89
+    /**
90
+     * Displays the range selector.
91
+     *
92
+     */
93
+    public function display_range_selector() {
94
+
95
+        $range = $this->get_range();
96
+        ?>
97 97
 
98 98
 			<form method="get" class="getpaid-filter-earnings float-right">
99 99
 				<?php getpaid_hidden_field( 'page', isset( $_GET['page'] ) ? sanitize_text_field( $_GET['page'] ) : 'wpinv-reports' ); ?>
@@ -115,14 +115,14 @@  discard block
 block discarded – undo
115 115
 			</form>
116 116
 
117 117
 		<?php
118
-	}
118
+    }
119 119
 
120
-	/**
121
-	 * Displays the reports tab.
122
-	 *
123
-	 */
124
-	public function display() {
125
-		?>
120
+    /**
121
+     * Displays the reports tab.
122
+     *
123
+     */
124
+    public function display() {
125
+        ?>
126 126
 
127 127
 		<div class="mt-4">
128 128
 
@@ -202,16 +202,16 @@  discard block
 block discarded – undo
202 202
 
203 203
 		<?php
204 204
 
205
-	}
205
+    }
206 206
 
207
-	/**
208
-	 * Displays the left side.
209
-	 *
210
-	 */
211
-	public function display_left() {
212
-		$graphs = wpinv_get_report_graphs();
207
+    /**
208
+     * Displays the left side.
209
+     *
210
+     */
211
+    public function display_left() {
212
+        $graphs = wpinv_get_report_graphs();
213 213
 
214
-		?>
214
+        ?>
215 215
 
216 216
 			<?php foreach ( $graphs as $key => $graph ) : ?>
217 217
 				<div class="row mb-4">
@@ -230,35 +230,35 @@  discard block
 block discarded – undo
230 230
 
231 231
 		<?php
232 232
 
233
-	}
234
-
235
-	/**
236
-	 * Retrieves the download url.
237
-	 *
238
-	 */
239
-	public function get_download_url( $graph, $file_type ) {
240
-
241
-		return wp_nonce_url(
242
-			add_query_arg(
243
-				array(
244
-					'getpaid-admin-action' => 'download_graph',
245
-					'file_type'            => urlencode( $file_type ),
246
-					'graph'                => urlencode( $graph ),
247
-				)
248
-			),
249
-			'getpaid-nonce',
250
-			'getpaid-nonce'
251
-		);
233
+    }
234
+
235
+    /**
236
+     * Retrieves the download url.
237
+     *
238
+     */
239
+    public function get_download_url( $graph, $file_type ) {
240
+
241
+        return wp_nonce_url(
242
+            add_query_arg(
243
+                array(
244
+                    'getpaid-admin-action' => 'download_graph',
245
+                    'file_type'            => urlencode( $file_type ),
246
+                    'graph'                => urlencode( $graph ),
247
+                )
248
+            ),
249
+            'getpaid-nonce',
250
+            'getpaid-nonce'
251
+        );
252 252
 
253
-	}
253
+    }
254 254
 
255
-	/**
256
-	 * Displays the right side.
257
-	 *
258
-	 */
259
-	public function display_right() {
255
+    /**
256
+     * Displays the right side.
257
+     *
258
+     */
259
+    public function display_right() {
260 260
 
261
-		?>
261
+        ?>
262 262
 
263 263
 			<?php foreach ( $this->views as $key => $view ) : ?>
264 264
 				<div class="row mb-4">
@@ -287,10 +287,10 @@  discard block
 block discarded – undo
287 287
 							</div>
288 288
 							<div class="card-body">
289 289
 								<?php
290
-									$class = $view['class'];
291
-									$class = new $class();
292
-									$class->display_stats();
293
-								?>
290
+                                    $class = $view['class'];
291
+                                    $class = new $class();
292
+                                    $class->display_stats();
293
+                                ?>
294 294
 							</div>
295 295
 						</div>
296 296
 					</div>
@@ -299,68 +299,68 @@  discard block
 block discarded – undo
299 299
 
300 300
 		<?php
301 301
 
302
-		do_action( 'getpaid_reports_display_right', $this );
303
-	}
304
-
305
-	/**
306
-	 * Returns a list of report cards.
307
-	 *
308
-	 */
309
-	public function get_cards() {
310
-
311
-		$cards = array(
312
-			'total_sales'         => array(
313
-				'description' => __( 'Gross sales in the period.', 'invoicing' ),
314
-				'label'       => __( 'Gross Revenue', 'invoicing' ),
315
-			),
316
-			'net_sales'           => array(
317
-				'description' => __( 'Net sales in the period.', 'invoicing' ),
318
-				'label'       => __( 'Net Revenue', 'invoicing' ),
319
-			),
320
-			'average_sales'       => array(
321
-				'description' => __( 'Average net daily/monthly sales.', 'invoicing' ),
322
-				'label'       => __( 'Avg. Net Sales', 'invoicing' ),
323
-			),
324
-			'average_total_sales' => array(
325
-				'description' => __( 'Average gross daily/monthly sales.', 'invoicing' ),
326
-				'label'       => __( 'Avg. Gross Sales', 'invoicing' ),
327
-			),
328
-			'total_invoices'      => array(
329
-				'description' => __( 'Number of paid invoices.', 'invoicing' ),
330
-				'label'       => __( 'Paid Invoices', 'invoicing' ),
331
-			),
332
-			'total_items'         => array(
333
-				'description' => __( 'Number of items purchased.', 'invoicing' ),
334
-				'label'       => __( 'Purchased Items', 'invoicing' ),
335
-			),
336
-			'refunded_items'      => array(
337
-				'description' => __( 'Number of items refunded.', 'invoicing' ),
338
-				'label'       => __( 'Refunded Items', 'invoicing' ),
339
-			),
340
-			'total_tax'           => array(
341
-				'description' => __( 'Total charged for taxes.', 'invoicing' ),
342
-				'label'       => __( 'Tax', 'invoicing' ),
343
-			),
344
-			'total_refunded_tax'  => array(
345
-				'description' => __( 'Total refunded for taxes.', 'invoicing' ),
346
-				'label'       => __( 'Refunded Tax', 'invoicing' ),
347
-			),
348
-			'total_fees'          => array(
349
-				'description' => __( 'Total fees charged.', 'invoicing' ),
350
-				'label'       => __( 'Fees', 'invoicing' ),
351
-			),
352
-			'total_refunds'       => array(
353
-				'description' => __( 'Total of refunded invoices.', 'invoicing' ),
354
-				'label'       => __( 'Refunded', 'invoicing' ),
355
-			),
356
-			'total_discount'      => array(
357
-				'description' => __( 'Total of discounts used.', 'invoicing' ),
358
-				'label'       => __( 'Discounted', 'invoicing' ),
359
-			),
360
-		);
361
-
362
-		return apply_filters( 'wpinv_report_cards', $cards );
363
-	}
302
+        do_action( 'getpaid_reports_display_right', $this );
303
+    }
304
+
305
+    /**
306
+     * Returns a list of report cards.
307
+     *
308
+     */
309
+    public function get_cards() {
310
+
311
+        $cards = array(
312
+            'total_sales'         => array(
313
+                'description' => __( 'Gross sales in the period.', 'invoicing' ),
314
+                'label'       => __( 'Gross Revenue', 'invoicing' ),
315
+            ),
316
+            'net_sales'           => array(
317
+                'description' => __( 'Net sales in the period.', 'invoicing' ),
318
+                'label'       => __( 'Net Revenue', 'invoicing' ),
319
+            ),
320
+            'average_sales'       => array(
321
+                'description' => __( 'Average net daily/monthly sales.', 'invoicing' ),
322
+                'label'       => __( 'Avg. Net Sales', 'invoicing' ),
323
+            ),
324
+            'average_total_sales' => array(
325
+                'description' => __( 'Average gross daily/monthly sales.', 'invoicing' ),
326
+                'label'       => __( 'Avg. Gross Sales', 'invoicing' ),
327
+            ),
328
+            'total_invoices'      => array(
329
+                'description' => __( 'Number of paid invoices.', 'invoicing' ),
330
+                'label'       => __( 'Paid Invoices', 'invoicing' ),
331
+            ),
332
+            'total_items'         => array(
333
+                'description' => __( 'Number of items purchased.', 'invoicing' ),
334
+                'label'       => __( 'Purchased Items', 'invoicing' ),
335
+            ),
336
+            'refunded_items'      => array(
337
+                'description' => __( 'Number of items refunded.', 'invoicing' ),
338
+                'label'       => __( 'Refunded Items', 'invoicing' ),
339
+            ),
340
+            'total_tax'           => array(
341
+                'description' => __( 'Total charged for taxes.', 'invoicing' ),
342
+                'label'       => __( 'Tax', 'invoicing' ),
343
+            ),
344
+            'total_refunded_tax'  => array(
345
+                'description' => __( 'Total refunded for taxes.', 'invoicing' ),
346
+                'label'       => __( 'Refunded Tax', 'invoicing' ),
347
+            ),
348
+            'total_fees'          => array(
349
+                'description' => __( 'Total fees charged.', 'invoicing' ),
350
+                'label'       => __( 'Fees', 'invoicing' ),
351
+            ),
352
+            'total_refunds'       => array(
353
+                'description' => __( 'Total of refunded invoices.', 'invoicing' ),
354
+                'label'       => __( 'Refunded', 'invoicing' ),
355
+            ),
356
+            'total_discount'      => array(
357
+                'description' => __( 'Total of discounts used.', 'invoicing' ),
358
+                'label'       => __( 'Discounted', 'invoicing' ),
359
+            ),
360
+        );
361
+
362
+        return apply_filters( 'wpinv_report_cards', $cards );
363
+    }
364 364
 
365 365
 
366 366
 
Please login to merge, or discard this patch.
includes/reports/class-getpaid-invoice-exporter.php 1 patch
Indentation   +189 added lines, -189 removed lines patch added patch discarded remove patch
@@ -12,194 +12,194 @@
 block discarded – undo
12 12
  */
13 13
 class GetPaid_Invoice_Exporter extends GetPaid_Graph_Downloader {
14 14
 
15
-	/**
16
-	 * Retrieves invoices query args.
17
-	 *
18
-	 * @param string $post_type post type to retrieve.
19
-	 * @param array $args Args to search for.
20
-	 * @return array
21
-	 */
22
-	public function get_invoice_query_args( $post_type, $args ) {
23
-
24
-		$query_args = array(
25
-			'post_type'              => $post_type,
26
-			'post_status'            => array_keys( wpinv_get_invoice_statuses( true, false, $post_type ) ),
27
-			'posts_per_page'         => -1,
28
-			'no_found_rows'          => true,
29
-			'update_post_term_cache' => false,
30
-			'fields'                 => 'ids',
31
-		);
32
-
33
-		if ( ! empty( $args['status'] ) && in_array( $args['status'], $query_args['post_status'], true ) ) {
34
-			$query_args['post_status'] = wpinv_clean( wpinv_parse_list( $args['status'] ) );
35
-		}
36
-
37
-		$date_query = array();
38
-		if ( ! empty( $args['to_date'] ) ) {
39
-			$date_query['before'] = wpinv_clean( $args['to_date'] );
40
-		}
41
-
42
-		if ( ! empty( $args['from_date'] ) ) {
43
-			$date_query['after'] = wpinv_clean( $args['from_date'] );
44
-		}
45
-
46
-		if ( ! empty( $date_query ) ) {
47
-			$date_query['inclusive']  = true;
48
-			$query_args['date_query'] = array( $date_query );
49
-		}
50
-
51
-		return $query_args;
52
-	}
53
-
54
-	/**
55
-	 * Retrieves invoices.
56
-	 *
57
-	 * @param array $query_args WP_Query args.
58
-	 * @return WPInv_Invoice[]
59
-	 */
60
-	public function get_invoices( $query_args ) {
61
-
62
-		// Get invoices.
63
-		$invoices = new WP_Query( $query_args );
64
-
65
-		// Prepare the results.
66
-		return array_map( 'wpinv_get_invoice', $invoices->posts );
67
-
68
-	}
69
-
70
-	/**
71
-	 * Handles the actual download.
72
-	 *
73
-	 */
74
-	public function export( $post_type, $args ) {
75
-
76
-		$invoices  = $this->get_invoices( $this->get_invoice_query_args( $post_type, $args ) );
77
-		$stream    = $this->prepare_output();
78
-		$headers   = $this->get_export_fields( $post_type );
79
-		$file_type = $this->prepare_file_type( strtolower( getpaid_get_post_type_label( $post_type ) ) );
80
-
81
-		if ( 'csv' == $file_type ) {
82
-			$this->download_csv( $invoices, $stream, $headers );
83
-		} elseif ( 'xml' == $file_type ) {
84
-			$this->download_xml( $invoices, $stream, $headers );
85
-		} else {
86
-			$this->download_json( $invoices, $stream, $headers );
87
-		}
88
-
89
-		fclose( $stream );
90
-		exit;
91
-	}
92
-
93
-	/**
94
-	 * Prepares a single invoice for download.
95
-	 *
96
-	 * @param WPInv_Invoice $invoice The invoice to prepare..
97
-	 * @param array $fields The fields to stream.
98
-	 * @since       1.0.19
99
-	 * @return array
100
-	 */
101
-	public function prepare_row( $invoice, $fields ) {
102
-
103
-		$prepared      = array();
104
-		$amount_fields = $this->get_amount_fields( $invoice->get_post_type() );
105
-
106
-		foreach ( $fields as $field ) {
107
-
108
-			$value  = '';
109
-			$method = "get_$field";
110
-
111
-			if ( method_exists( $invoice, $method ) ) {
112
-				$value  = $invoice->$method();
113
-			}
114
-
115
-			if ( in_array( $field, $amount_fields ) ) {
116
-				$value  = wpinv_round_amount( wpinv_sanitize_amount( $value ) );
117
-			}
118
-
119
-			$prepared[ $field ] = wpinv_clean( $value );
120
-
121
-		}
122
-
123
-		return $prepared;
124
-	}
125
-
126
-	/**
127
-	 * Retrieves export fields.
128
-	 *
129
-	 * @param string $post_type
130
-	 * @since       1.0.19
131
-	 * @return array
132
-	 */
133
-	public function get_export_fields( $post_type ) {
134
-
135
-		$fields = array(
136
-			'id',
137
-			'parent_id',
138
-			'status',
139
-			'date_created',
140
-			'date_modified',
141
-			'date_due',
142
-			'date_completed',
143
-			'number',
144
-			'key',
145
-			'description',
146
-			'post_type',
147
-			'mode',
148
-			'customer_id',
149
-			'customer_first_name',
150
-			'customer_last_name',
151
-			'customer_phone',
152
-			'customer_email',
153
-			'customer_country',
154
-			'customer_city',
155
-			'customer_state',
156
-			'customer_zip',
157
-			'customer_company',
158
-			'customer_vat_number',
159
-			'customer_address',
160
-			'subtotal',
161
-			'total_discount',
162
-			'total_tax',
163
-			'total_fees',
164
-			'fees',
165
-			'discounts',
166
-			'taxes',
167
-			'cart_details',
168
-			'item_ids',
169
-			'payment_form',
170
-			'discount_code',
171
-			'gateway',
172
-			'transaction_id',
173
-			'currency',
174
-			'disable_taxes',
175
-			'subscription_id',
176
-			'remote_subscription_id',
177
-			'is_viewed',
178
-			'email_cc',
179
-			'template',
180
-			'created_via',
181
-    	);
182
-
183
-		return apply_filters( 'getpaid_invoice_exporter_get_fields', $fields, $post_type );
184
-	}
185
-
186
-	/**
187
-	 * Retrieves amount fields.
188
-	 *
189
-	 * @param string $post_type
190
-	 * @since       1.0.19
191
-	 * @return array
192
-	 */
193
-	public function get_amount_fields( $post_type ) {
194
-
195
-		$fields = array(
196
-			'subtotal',
197
-			'total_discount',
198
-			'total_tax',
199
-			'total_fees',
200
-    	);
201
-
202
-		return apply_filters( 'getpaid_invoice_exporter_get_amount_fields', $fields, $post_type );
203
-	}
15
+    /**
16
+     * Retrieves invoices query args.
17
+     *
18
+     * @param string $post_type post type to retrieve.
19
+     * @param array $args Args to search for.
20
+     * @return array
21
+     */
22
+    public function get_invoice_query_args( $post_type, $args ) {
23
+
24
+        $query_args = array(
25
+            'post_type'              => $post_type,
26
+            'post_status'            => array_keys( wpinv_get_invoice_statuses( true, false, $post_type ) ),
27
+            'posts_per_page'         => -1,
28
+            'no_found_rows'          => true,
29
+            'update_post_term_cache' => false,
30
+            'fields'                 => 'ids',
31
+        );
32
+
33
+        if ( ! empty( $args['status'] ) && in_array( $args['status'], $query_args['post_status'], true ) ) {
34
+            $query_args['post_status'] = wpinv_clean( wpinv_parse_list( $args['status'] ) );
35
+        }
36
+
37
+        $date_query = array();
38
+        if ( ! empty( $args['to_date'] ) ) {
39
+            $date_query['before'] = wpinv_clean( $args['to_date'] );
40
+        }
41
+
42
+        if ( ! empty( $args['from_date'] ) ) {
43
+            $date_query['after'] = wpinv_clean( $args['from_date'] );
44
+        }
45
+
46
+        if ( ! empty( $date_query ) ) {
47
+            $date_query['inclusive']  = true;
48
+            $query_args['date_query'] = array( $date_query );
49
+        }
50
+
51
+        return $query_args;
52
+    }
53
+
54
+    /**
55
+     * Retrieves invoices.
56
+     *
57
+     * @param array $query_args WP_Query args.
58
+     * @return WPInv_Invoice[]
59
+     */
60
+    public function get_invoices( $query_args ) {
61
+
62
+        // Get invoices.
63
+        $invoices = new WP_Query( $query_args );
64
+
65
+        // Prepare the results.
66
+        return array_map( 'wpinv_get_invoice', $invoices->posts );
67
+
68
+    }
69
+
70
+    /**
71
+     * Handles the actual download.
72
+     *
73
+     */
74
+    public function export( $post_type, $args ) {
75
+
76
+        $invoices  = $this->get_invoices( $this->get_invoice_query_args( $post_type, $args ) );
77
+        $stream    = $this->prepare_output();
78
+        $headers   = $this->get_export_fields( $post_type );
79
+        $file_type = $this->prepare_file_type( strtolower( getpaid_get_post_type_label( $post_type ) ) );
80
+
81
+        if ( 'csv' == $file_type ) {
82
+            $this->download_csv( $invoices, $stream, $headers );
83
+        } elseif ( 'xml' == $file_type ) {
84
+            $this->download_xml( $invoices, $stream, $headers );
85
+        } else {
86
+            $this->download_json( $invoices, $stream, $headers );
87
+        }
88
+
89
+        fclose( $stream );
90
+        exit;
91
+    }
92
+
93
+    /**
94
+     * Prepares a single invoice for download.
95
+     *
96
+     * @param WPInv_Invoice $invoice The invoice to prepare..
97
+     * @param array $fields The fields to stream.
98
+     * @since       1.0.19
99
+     * @return array
100
+     */
101
+    public function prepare_row( $invoice, $fields ) {
102
+
103
+        $prepared      = array();
104
+        $amount_fields = $this->get_amount_fields( $invoice->get_post_type() );
105
+
106
+        foreach ( $fields as $field ) {
107
+
108
+            $value  = '';
109
+            $method = "get_$field";
110
+
111
+            if ( method_exists( $invoice, $method ) ) {
112
+                $value  = $invoice->$method();
113
+            }
114
+
115
+            if ( in_array( $field, $amount_fields ) ) {
116
+                $value  = wpinv_round_amount( wpinv_sanitize_amount( $value ) );
117
+            }
118
+
119
+            $prepared[ $field ] = wpinv_clean( $value );
120
+
121
+        }
122
+
123
+        return $prepared;
124
+    }
125
+
126
+    /**
127
+     * Retrieves export fields.
128
+     *
129
+     * @param string $post_type
130
+     * @since       1.0.19
131
+     * @return array
132
+     */
133
+    public function get_export_fields( $post_type ) {
134
+
135
+        $fields = array(
136
+            'id',
137
+            'parent_id',
138
+            'status',
139
+            'date_created',
140
+            'date_modified',
141
+            'date_due',
142
+            'date_completed',
143
+            'number',
144
+            'key',
145
+            'description',
146
+            'post_type',
147
+            'mode',
148
+            'customer_id',
149
+            'customer_first_name',
150
+            'customer_last_name',
151
+            'customer_phone',
152
+            'customer_email',
153
+            'customer_country',
154
+            'customer_city',
155
+            'customer_state',
156
+            'customer_zip',
157
+            'customer_company',
158
+            'customer_vat_number',
159
+            'customer_address',
160
+            'subtotal',
161
+            'total_discount',
162
+            'total_tax',
163
+            'total_fees',
164
+            'fees',
165
+            'discounts',
166
+            'taxes',
167
+            'cart_details',
168
+            'item_ids',
169
+            'payment_form',
170
+            'discount_code',
171
+            'gateway',
172
+            'transaction_id',
173
+            'currency',
174
+            'disable_taxes',
175
+            'subscription_id',
176
+            'remote_subscription_id',
177
+            'is_viewed',
178
+            'email_cc',
179
+            'template',
180
+            'created_via',
181
+        );
182
+
183
+        return apply_filters( 'getpaid_invoice_exporter_get_fields', $fields, $post_type );
184
+    }
185
+
186
+    /**
187
+     * Retrieves amount fields.
188
+     *
189
+     * @param string $post_type
190
+     * @since       1.0.19
191
+     * @return array
192
+     */
193
+    public function get_amount_fields( $post_type ) {
194
+
195
+        $fields = array(
196
+            'subtotal',
197
+            'total_discount',
198
+            'total_tax',
199
+            'total_fees',
200
+        );
201
+
202
+        return apply_filters( 'getpaid_invoice_exporter_get_amount_fields', $fields, $post_type );
203
+    }
204 204
 
205 205
 }
Please login to merge, or discard this patch.