Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like EEH_Line_Item often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use EEH_Line_Item, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class EEH_Line_Item |
||
| 22 | { |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Adds a simple item (unrelated to any other model object) to the provided PARENT line item. |
||
| 26 | * Does NOT automatically re-calculate the line item totals or update the related transaction. |
||
| 27 | * You should call recalculate_total_including_taxes() on the grant total line item after this |
||
| 28 | * to update the subtotals, and EE_Registration_Processor::calculate_reg_final_prices_per_line_item() |
||
| 29 | * to keep the registration final prices in-sync with the transaction's total. |
||
| 30 | * |
||
| 31 | * @param EE_Line_Item $parent_line_item |
||
| 32 | * @param string $name |
||
| 33 | * @param float $unit_price |
||
| 34 | * @param string $description |
||
| 35 | * @param int $quantity |
||
| 36 | * @param boolean $taxable |
||
| 37 | * @param boolean $code if set to a value, ensures there is only one line item with that code |
||
| 38 | * @return boolean success |
||
| 39 | * @throws EE_Error |
||
| 40 | * @throws InvalidArgumentException |
||
| 41 | * @throws InvalidDataTypeException |
||
| 42 | * @throws InvalidInterfaceException |
||
| 43 | * @throws ReflectionException |
||
| 44 | */ |
||
| 45 | public static function add_unrelated_item( |
||
| 74 | |||
| 75 | |||
| 76 | /** |
||
| 77 | * Adds a simple item ( unrelated to any other model object) to the total line item, |
||
| 78 | * in the correct spot in the line item tree. Automatically |
||
| 79 | * re-calculates the line item totals and updates the related transaction. But |
||
| 80 | * DOES NOT automatically upgrade the transaction's registrations' final prices (which |
||
| 81 | * should probably change because of this). |
||
| 82 | * You should call EE_Registration_Processor::calculate_reg_final_prices_per_line_item() |
||
| 83 | * after using this, to keep the registration final prices in-sync with the transaction's total. |
||
| 84 | * |
||
| 85 | * @param EE_Line_Item $parent_line_item |
||
| 86 | * @param string $name |
||
| 87 | * @param float $percentage_amount |
||
| 88 | * @param string $description |
||
| 89 | * @param boolean $taxable |
||
| 90 | * @return boolean success |
||
| 91 | * @throws EE_Error |
||
| 92 | */ |
||
| 93 | public static function add_percentage_based_item( |
||
| 117 | |||
| 118 | |||
| 119 | /** |
||
| 120 | * Returns the new line item created by adding a purchase of the ticket |
||
| 121 | * ensures that ticket line item is saved, and that cart total has been recalculated. |
||
| 122 | * If this ticket has already been purchased, just increments its count. |
||
| 123 | * Automatically re-calculates the line item totals and updates the related transaction. But |
||
| 124 | * DOES NOT automatically upgrade the transaction's registrations' final prices (which |
||
| 125 | * should probably change because of this). |
||
| 126 | * You should call EE_Registration_Processor::calculate_reg_final_prices_per_line_item() |
||
| 127 | * after using this, to keep the registration final prices in-sync with the transaction's total. |
||
| 128 | * |
||
| 129 | * @param EE_Line_Item $total_line_item grand total line item of type EEM_Line_Item::type_total |
||
| 130 | * @param EE_Ticket $ticket |
||
| 131 | * @param int $qty |
||
| 132 | * @return EE_Line_Item |
||
| 133 | * @throws EE_Error |
||
| 134 | * @throws InvalidArgumentException |
||
| 135 | * @throws InvalidDataTypeException |
||
| 136 | * @throws InvalidInterfaceException |
||
| 137 | * @throws ReflectionException |
||
| 138 | */ |
||
| 139 | public static function add_ticket_purchase(EE_Line_Item $total_line_item, EE_Ticket $ticket, $qty = 1) |
||
| 162 | |||
| 163 | |||
| 164 | /** |
||
| 165 | * Returns the new line item created by adding a purchase of the ticket |
||
| 166 | * |
||
| 167 | * @param EE_Line_Item $total_line_item |
||
| 168 | * @param EE_Ticket $ticket |
||
| 169 | * @param int $qty |
||
| 170 | * @return EE_Line_Item |
||
| 171 | * @throws EE_Error |
||
| 172 | * @throws InvalidArgumentException |
||
| 173 | * @throws InvalidDataTypeException |
||
| 174 | * @throws InvalidInterfaceException |
||
| 175 | * @throws ReflectionException |
||
| 176 | */ |
||
| 177 | public static function increment_ticket_qty_if_already_in_cart( |
||
| 200 | |||
| 201 | |||
| 202 | /** |
||
| 203 | * Increments the line item and all its children's quantity by $qty (but percent line items are unaffected). |
||
| 204 | * Does NOT save or recalculate other line items totals |
||
| 205 | * |
||
| 206 | * @param EE_Line_Item $line_item |
||
| 207 | * @param int $qty |
||
| 208 | * @return void |
||
| 209 | * @throws EE_Error |
||
| 210 | * @throws InvalidArgumentException |
||
| 211 | * @throws InvalidDataTypeException |
||
| 212 | * @throws InvalidInterfaceException |
||
| 213 | * @throws ReflectionException |
||
| 214 | */ |
||
| 215 | View Code Duplication | public static function increment_quantity(EE_Line_Item $line_item, $qty = 1) |
|
| 229 | |||
| 230 | |||
| 231 | /** |
||
| 232 | * Decrements the line item and all its children's quantity by $qty (but percent line items are unaffected). |
||
| 233 | * Does NOT save or recalculate other line items totals |
||
| 234 | * |
||
| 235 | * @param EE_Line_Item $line_item |
||
| 236 | * @param int $qty |
||
| 237 | * @return void |
||
| 238 | * @throws EE_Error |
||
| 239 | * @throws InvalidArgumentException |
||
| 240 | * @throws InvalidDataTypeException |
||
| 241 | * @throws InvalidInterfaceException |
||
| 242 | * @throws ReflectionException |
||
| 243 | */ |
||
| 244 | public static function decrement_quantity(EE_Line_Item $line_item, $qty = 1) |
||
| 259 | |||
| 260 | |||
| 261 | /** |
||
| 262 | * Updates the line item and its children's quantities to the specified number. |
||
| 263 | * Does NOT save them or recalculate totals. |
||
| 264 | * |
||
| 265 | * @param EE_Line_Item $line_item |
||
| 266 | * @param int $new_quantity |
||
| 267 | * @throws EE_Error |
||
| 268 | * @throws InvalidArgumentException |
||
| 269 | * @throws InvalidDataTypeException |
||
| 270 | * @throws InvalidInterfaceException |
||
| 271 | * @throws ReflectionException |
||
| 272 | */ |
||
| 273 | View Code Duplication | public static function update_quantity(EE_Line_Item $line_item, $new_quantity) |
|
| 286 | |||
| 287 | |||
| 288 | /** |
||
| 289 | * Returns the new line item created by adding a purchase of the ticket |
||
| 290 | * |
||
| 291 | * @param EE_Line_Item $total_line_item of type EEM_Line_Item::type_total |
||
| 292 | * @param EE_Ticket $ticket |
||
| 293 | * @param int $qty |
||
| 294 | * @return EE_Line_Item |
||
| 295 | * @throws EE_Error |
||
| 296 | * @throws InvalidArgumentException |
||
| 297 | * @throws InvalidDataTypeException |
||
| 298 | * @throws InvalidInterfaceException |
||
| 299 | * @throws ReflectionException |
||
| 300 | */ |
||
| 301 | public static function create_ticket_line_item(EE_Line_Item $total_line_item, EE_Ticket $ticket, $qty = 1) |
||
| 362 | |||
| 363 | |||
| 364 | /** |
||
| 365 | * Adds the specified item under the pre-tax-sub-total line item. Automatically |
||
| 366 | * re-calculates the line item totals and updates the related transaction. But |
||
| 367 | * DOES NOT automatically upgrade the transaction's registrations' final prices (which |
||
| 368 | * should probably change because of this). |
||
| 369 | * You should call EE_Registration_Processor::calculate_reg_final_prices_per_line_item() |
||
| 370 | * after using this, to keep the registration final prices in-sync with the transaction's total. |
||
| 371 | * |
||
| 372 | * @param EE_Line_Item $total_line_item |
||
| 373 | * @param EE_Line_Item $item to be added |
||
| 374 | * @return boolean |
||
| 375 | * @throws EE_Error |
||
| 376 | * @throws InvalidArgumentException |
||
| 377 | * @throws InvalidDataTypeException |
||
| 378 | * @throws InvalidInterfaceException |
||
| 379 | * @throws ReflectionException |
||
| 380 | */ |
||
| 381 | public static function add_item(EE_Line_Item $total_line_item, EE_Line_Item $item) |
||
| 392 | |||
| 393 | |||
| 394 | /** |
||
| 395 | * cancels an existing ticket line item, |
||
| 396 | * by decrementing it's quantity by 1 and adding a new "type_cancellation" sub-line-item. |
||
| 397 | * ALL totals and subtotals will NEED TO BE UPDATED after performing this action |
||
| 398 | * |
||
| 399 | * @param EE_Line_Item $ticket_line_item |
||
| 400 | * @param int $qty |
||
| 401 | * @return bool success |
||
| 402 | * @throws EE_Error |
||
| 403 | * @throws InvalidArgumentException |
||
| 404 | * @throws InvalidDataTypeException |
||
| 405 | * @throws InvalidInterfaceException |
||
| 406 | * @throws ReflectionException |
||
| 407 | */ |
||
| 408 | public static function cancel_ticket_line_item(EE_Line_Item $ticket_line_item, $qty = 1) |
||
| 490 | |||
| 491 | |||
| 492 | /** |
||
| 493 | * reinstates (un-cancels?) a previously canceled ticket line item, |
||
| 494 | * by incrementing it's quantity by 1, and decrementing it's "type_cancellation" sub-line-item. |
||
| 495 | * ALL totals and subtotals will NEED TO BE UPDATED after performing this action |
||
| 496 | * |
||
| 497 | * @param EE_Line_Item $ticket_line_item |
||
| 498 | * @param int $qty |
||
| 499 | * @return bool success |
||
| 500 | * @throws EE_Error |
||
| 501 | * @throws InvalidArgumentException |
||
| 502 | * @throws InvalidDataTypeException |
||
| 503 | * @throws InvalidInterfaceException |
||
| 504 | * @throws ReflectionException |
||
| 505 | */ |
||
| 506 | public static function reinstate_canceled_ticket_line_item(EE_Line_Item $ticket_line_item, $qty = 1) |
||
| 569 | |||
| 570 | |||
| 571 | /** |
||
| 572 | * calls EEH_Line_Item::find_transaction_grand_total_for_line_item() |
||
| 573 | * then EE_Line_Item::recalculate_total_including_taxes() on the result |
||
| 574 | * |
||
| 575 | * @param EE_Line_Item $line_item |
||
| 576 | * @return float |
||
| 577 | * @throws EE_Error |
||
| 578 | * @throws InvalidArgumentException |
||
| 579 | * @throws InvalidDataTypeException |
||
| 580 | * @throws InvalidInterfaceException |
||
| 581 | * @throws ReflectionException |
||
| 582 | */ |
||
| 583 | public static function get_grand_total_and_recalculate_everything(EE_Line_Item $line_item) |
||
| 588 | |||
| 589 | |||
| 590 | /** |
||
| 591 | * Gets the line item which contains the subtotal of all the items |
||
| 592 | * |
||
| 593 | * @param EE_Line_Item $total_line_item of type EEM_Line_Item::type_total |
||
| 594 | * @return EE_Line_Item |
||
| 595 | * @throws EE_Error |
||
| 596 | * @throws InvalidArgumentException |
||
| 597 | * @throws InvalidDataTypeException |
||
| 598 | * @throws InvalidInterfaceException |
||
| 599 | * @throws ReflectionException |
||
| 600 | */ |
||
| 601 | public static function get_pre_tax_subtotal(EE_Line_Item $total_line_item) |
||
| 608 | |||
| 609 | |||
| 610 | /** |
||
| 611 | * Gets the line item for the taxes subtotal |
||
| 612 | * |
||
| 613 | * @param EE_Line_Item $total_line_item of type EEM_Line_Item::type_total |
||
| 614 | * @return EE_Line_Item |
||
| 615 | * @throws EE_Error |
||
| 616 | * @throws InvalidArgumentException |
||
| 617 | * @throws InvalidDataTypeException |
||
| 618 | * @throws InvalidInterfaceException |
||
| 619 | * @throws ReflectionException |
||
| 620 | */ |
||
| 621 | public static function get_taxes_subtotal(EE_Line_Item $total_line_item) |
||
| 626 | |||
| 627 | |||
| 628 | /** |
||
| 629 | * sets the TXN ID on an EE_Line_Item if passed a valid EE_Transaction object |
||
| 630 | * |
||
| 631 | * @param EE_Line_Item $line_item |
||
| 632 | * @param EE_Transaction $transaction |
||
| 633 | * @return void |
||
| 634 | * @throws EE_Error |
||
| 635 | * @throws InvalidArgumentException |
||
| 636 | * @throws InvalidDataTypeException |
||
| 637 | * @throws InvalidInterfaceException |
||
| 638 | * @throws ReflectionException |
||
| 639 | */ |
||
| 640 | public static function set_TXN_ID(EE_Line_Item $line_item, $transaction = null) |
||
| 649 | |||
| 650 | |||
| 651 | /** |
||
| 652 | * Creates a new default total line item for the transaction, |
||
| 653 | * and its tickets subtotal and taxes subtotal line items (and adds the |
||
| 654 | * existing taxes as children of the taxes subtotal line item) |
||
| 655 | * |
||
| 656 | * @param EE_Transaction $transaction |
||
| 657 | * @return EE_Line_Item of type total |
||
| 658 | * @throws EE_Error |
||
| 659 | * @throws InvalidArgumentException |
||
| 660 | * @throws InvalidDataTypeException |
||
| 661 | * @throws InvalidInterfaceException |
||
| 662 | * @throws ReflectionException |
||
| 663 | */ |
||
| 664 | View Code Duplication | public static function create_total_line_item($transaction = null) |
|
| 681 | |||
| 682 | |||
| 683 | /** |
||
| 684 | * Creates a default items subtotal line item |
||
| 685 | * |
||
| 686 | * @param EE_Line_Item $total_line_item |
||
| 687 | * @param EE_Transaction $transaction |
||
| 688 | * @return EE_Line_Item |
||
| 689 | * @throws EE_Error |
||
| 690 | * @throws InvalidArgumentException |
||
| 691 | * @throws InvalidDataTypeException |
||
| 692 | * @throws InvalidInterfaceException |
||
| 693 | * @throws ReflectionException |
||
| 694 | */ |
||
| 695 | View Code Duplication | protected static function create_pre_tax_subtotal(EE_Line_Item $total_line_item, $transaction = null) |
|
| 711 | |||
| 712 | |||
| 713 | /** |
||
| 714 | * Creates a line item for the taxes subtotal and finds all the tax prices |
||
| 715 | * and applies taxes to it |
||
| 716 | * |
||
| 717 | * @param EE_Line_Item $total_line_item of type EEM_Line_Item::type_total |
||
| 718 | * @param EE_Transaction $transaction |
||
| 719 | * @return EE_Line_Item |
||
| 720 | * @throws EE_Error |
||
| 721 | * @throws InvalidArgumentException |
||
| 722 | * @throws InvalidDataTypeException |
||
| 723 | * @throws InvalidInterfaceException |
||
| 724 | * @throws ReflectionException |
||
| 725 | */ |
||
| 726 | View Code Duplication | protected static function create_taxes_subtotal(EE_Line_Item $total_line_item, $transaction = null) |
|
| 744 | |||
| 745 | |||
| 746 | /** |
||
| 747 | * Creates a default items subtotal line item |
||
| 748 | * |
||
| 749 | * @param EE_Line_Item $pre_tax_line_item |
||
| 750 | * @param EE_Transaction $transaction |
||
| 751 | * @param EE_Event $event |
||
| 752 | * @return EE_Line_Item |
||
| 753 | * @throws EE_Error |
||
| 754 | * @throws InvalidArgumentException |
||
| 755 | * @throws InvalidDataTypeException |
||
| 756 | * @throws InvalidInterfaceException |
||
| 757 | * @throws ReflectionException |
||
| 758 | */ |
||
| 759 | public static function create_event_subtotal(EE_Line_Item $pre_tax_line_item, $transaction = null, $event = null) |
||
| 777 | |||
| 778 | |||
| 779 | /** |
||
| 780 | * Gets what the event ticket's code SHOULD be |
||
| 781 | * |
||
| 782 | * @param EE_Event $event |
||
| 783 | * @return string |
||
| 784 | * @throws EE_Error |
||
| 785 | */ |
||
| 786 | public static function get_event_code($event) |
||
| 790 | |||
| 791 | |||
| 792 | /** |
||
| 793 | * Gets the event name |
||
| 794 | * |
||
| 795 | * @param EE_Event $event |
||
| 796 | * @return string |
||
| 797 | * @throws EE_Error |
||
| 798 | */ |
||
| 799 | public static function get_event_name($event) |
||
| 805 | |||
| 806 | |||
| 807 | /** |
||
| 808 | * Gets the event excerpt |
||
| 809 | * |
||
| 810 | * @param EE_Event $event |
||
| 811 | * @return string |
||
| 812 | * @throws EE_Error |
||
| 813 | */ |
||
| 814 | public static function get_event_desc($event) |
||
| 818 | |||
| 819 | |||
| 820 | /** |
||
| 821 | * Given the grand total line item and a ticket, finds the event sub-total |
||
| 822 | * line item the ticket's purchase should be added onto |
||
| 823 | * |
||
| 824 | * @access public |
||
| 825 | * @param EE_Line_Item $grand_total the grand total line item |
||
| 826 | * @param EE_Ticket $ticket |
||
| 827 | * @return EE_Line_Item |
||
| 828 | * @throws EE_Error |
||
| 829 | * @throws InvalidArgumentException |
||
| 830 | * @throws InvalidDataTypeException |
||
| 831 | * @throws InvalidInterfaceException |
||
| 832 | * @throws ReflectionException |
||
| 833 | */ |
||
| 834 | public static function get_event_line_item_for_ticket(EE_Line_Item $grand_total, EE_Ticket $ticket) |
||
| 872 | |||
| 873 | |||
| 874 | /** |
||
| 875 | * Gets the event line item |
||
| 876 | * |
||
| 877 | * @param EE_Line_Item $grand_total |
||
| 878 | * @param EE_Event $event |
||
| 879 | * @return EE_Line_Item for the event subtotal which is a child of $grand_total |
||
| 880 | * @throws EE_Error |
||
| 881 | * @throws InvalidArgumentException |
||
| 882 | * @throws InvalidDataTypeException |
||
| 883 | * @throws InvalidInterfaceException |
||
| 884 | * @throws ReflectionException |
||
| 885 | */ |
||
| 886 | public static function get_event_line_item(EE_Line_Item $grand_total, $event) |
||
| 916 | |||
| 917 | |||
| 918 | /** |
||
| 919 | * Creates a default items subtotal line item |
||
| 920 | * |
||
| 921 | * @param EE_Line_Item $event_line_item |
||
| 922 | * @param EE_Event $event |
||
| 923 | * @param EE_Transaction $transaction |
||
| 924 | * @return void |
||
| 925 | * @throws EE_Error |
||
| 926 | * @throws InvalidArgumentException |
||
| 927 | * @throws InvalidDataTypeException |
||
| 928 | * @throws InvalidInterfaceException |
||
| 929 | * @throws ReflectionException |
||
| 930 | */ |
||
| 931 | public static function set_event_subtotal_details( |
||
| 944 | |||
| 945 | |||
| 946 | /** |
||
| 947 | * Finds what taxes should apply, adds them as tax line items under the taxes sub-total, |
||
| 948 | * and recalculates the taxes sub-total and the grand total. Resets the taxes, so |
||
| 949 | * any old taxes are removed |
||
| 950 | * |
||
| 951 | * @param EE_Line_Item $total_line_item of type EEM_Line_Item::type_total |
||
| 952 | * @param bool $update_txn_status |
||
| 953 | * @return bool |
||
| 954 | * @throws EE_Error |
||
| 955 | * @throws InvalidArgumentException |
||
| 956 | * @throws InvalidDataTypeException |
||
| 957 | * @throws InvalidInterfaceException |
||
| 958 | * @throws ReflectionException |
||
| 959 | * @throws RuntimeException |
||
| 960 | */ |
||
| 961 | public static function apply_taxes(EE_Line_Item $total_line_item, $update_txn_status = false) |
||
| 1006 | |||
| 1007 | |||
| 1008 | /** |
||
| 1009 | * Ensures that taxes have been applied to the order, if not applies them. |
||
| 1010 | * Returns the total amount of tax |
||
| 1011 | * |
||
| 1012 | * @param EE_Line_Item $total_line_item of type EEM_Line_Item::type_total |
||
| 1013 | * @return float |
||
| 1014 | * @throws EE_Error |
||
| 1015 | * @throws InvalidArgumentException |
||
| 1016 | * @throws InvalidDataTypeException |
||
| 1017 | * @throws InvalidInterfaceException |
||
| 1018 | * @throws ReflectionException |
||
| 1019 | */ |
||
| 1020 | public static function ensure_taxes_applied($total_line_item) |
||
| 1028 | |||
| 1029 | |||
| 1030 | /** |
||
| 1031 | * Deletes ALL children of the passed line item |
||
| 1032 | * |
||
| 1033 | * @param EE_Line_Item $parent_line_item |
||
| 1034 | * @return bool |
||
| 1035 | * @throws EE_Error |
||
| 1036 | * @throws InvalidArgumentException |
||
| 1037 | * @throws InvalidDataTypeException |
||
| 1038 | * @throws InvalidInterfaceException |
||
| 1039 | * @throws ReflectionException |
||
| 1040 | */ |
||
| 1041 | public static function delete_all_child_items(EE_Line_Item $parent_line_item) |
||
| 1058 | |||
| 1059 | |||
| 1060 | /** |
||
| 1061 | * Deletes the line items as indicated by the line item code(s) provided, |
||
| 1062 | * regardless of where they're found in the line item tree. Automatically |
||
| 1063 | * re-calculates the line item totals and updates the related transaction. But |
||
| 1064 | * DOES NOT automatically upgrade the transaction's registrations' final prices (which |
||
| 1065 | * should probably change because of this). |
||
| 1066 | * You should call EE_Registration_Processor::calculate_reg_final_prices_per_line_item() |
||
| 1067 | * after using this, to keep the registration final prices in-sync with the transaction's total. |
||
| 1068 | * |
||
| 1069 | * @param EE_Line_Item $total_line_item of type EEM_Line_Item::type_total |
||
| 1070 | * @param array|bool|string $line_item_codes |
||
| 1071 | * @return int number of items successfully removed |
||
| 1072 | * @throws EE_Error |
||
| 1073 | */ |
||
| 1074 | public static function delete_items(EE_Line_Item $total_line_item, $line_item_codes = false) |
||
| 1107 | |||
| 1108 | |||
| 1109 | /** |
||
| 1110 | * Overwrites the previous tax by clearing out the old taxes, and creates a new |
||
| 1111 | * tax and updates the total line item accordingly |
||
| 1112 | * |
||
| 1113 | * @param EE_Line_Item $total_line_item |
||
| 1114 | * @param float $amount |
||
| 1115 | * @param string $name |
||
| 1116 | * @param string $description |
||
| 1117 | * @param string $code |
||
| 1118 | * @param boolean $add_to_existing_line_item |
||
| 1119 | * if true, and a duplicate line item with the same code is found, |
||
| 1120 | * $amount will be added onto it; otherwise will simply set the taxes to match $amount |
||
| 1121 | * @return EE_Line_Item the new tax line item created |
||
| 1122 | * @throws EE_Error |
||
| 1123 | * @throws InvalidArgumentException |
||
| 1124 | * @throws InvalidDataTypeException |
||
| 1125 | * @throws InvalidInterfaceException |
||
| 1126 | * @throws ReflectionException |
||
| 1127 | */ |
||
| 1128 | public static function set_total_tax_to( |
||
| 1176 | |||
| 1177 | |||
| 1178 | /** |
||
| 1179 | * Makes all the line items which are children of $line_item taxable (or not). |
||
| 1180 | * Does NOT save the line items |
||
| 1181 | * |
||
| 1182 | * @param EE_Line_Item $line_item |
||
| 1183 | * @param boolean $taxable |
||
| 1184 | * @param string $code_substring_for_whitelist if this string is part of the line item's code |
||
| 1185 | * it will be whitelisted (ie, except from becoming taxable) |
||
| 1186 | * @throws EE_Error |
||
| 1187 | */ |
||
| 1188 | public static function set_line_items_taxable( |
||
| 1208 | |||
| 1209 | |||
| 1210 | /** |
||
| 1211 | * Gets all descendants that are event subtotals |
||
| 1212 | * |
||
| 1213 | * @uses EEH_Line_Item::get_subtotals_of_object_type() |
||
| 1214 | * @param EE_Line_Item $parent_line_item - the line item to find descendants of |
||
| 1215 | * @return EE_Line_Item[] |
||
| 1216 | * @throws EE_Error |
||
| 1217 | */ |
||
| 1218 | public static function get_event_subtotals(EE_Line_Item $parent_line_item) |
||
| 1222 | |||
| 1223 | |||
| 1224 | /** |
||
| 1225 | * Gets all descendants subtotals that match the supplied object type |
||
| 1226 | * |
||
| 1227 | * @uses EEH_Line_Item::_get_descendants_by_type_and_object_type() |
||
| 1228 | * @param EE_Line_Item $parent_line_item - the line item to find descendants of |
||
| 1229 | * @param string $obj_type |
||
| 1230 | * @return EE_Line_Item[] |
||
| 1231 | * @throws EE_Error |
||
| 1232 | */ |
||
| 1233 | public static function get_subtotals_of_object_type(EE_Line_Item $parent_line_item, $obj_type = '') |
||
| 1241 | |||
| 1242 | |||
| 1243 | /** |
||
| 1244 | * Gets all descendants that are tickets |
||
| 1245 | * |
||
| 1246 | * @uses EEH_Line_Item::get_line_items_of_object_type() |
||
| 1247 | * @param EE_Line_Item $parent_line_item - the line item to find descendants of |
||
| 1248 | * @return EE_Line_Item[] |
||
| 1249 | * @throws EE_Error |
||
| 1250 | */ |
||
| 1251 | public static function get_ticket_line_items(EE_Line_Item $parent_line_item) |
||
| 1258 | |||
| 1259 | |||
| 1260 | /** |
||
| 1261 | * Gets all descendants subtotals that match the supplied object type |
||
| 1262 | * |
||
| 1263 | * @uses EEH_Line_Item::_get_descendants_by_type_and_object_type() |
||
| 1264 | * @param EE_Line_Item $parent_line_item - the line item to find descendants of |
||
| 1265 | * @param string $obj_type |
||
| 1266 | * @return EE_Line_Item[] |
||
| 1267 | * @throws EE_Error |
||
| 1268 | */ |
||
| 1269 | public static function get_line_items_of_object_type(EE_Line_Item $parent_line_item, $obj_type = '') |
||
| 1277 | |||
| 1278 | |||
| 1279 | /** |
||
| 1280 | * Gets all the descendants (ie, children or children of children etc) that are of the type 'tax' |
||
| 1281 | * |
||
| 1282 | * @uses EEH_Line_Item::get_descendants_of_type() |
||
| 1283 | * @param EE_Line_Item $parent_line_item - the line item to find descendants of |
||
| 1284 | * @return EE_Line_Item[] |
||
| 1285 | * @throws EE_Error |
||
| 1286 | */ |
||
| 1287 | public static function get_tax_descendants(EE_Line_Item $parent_line_item) |
||
| 1294 | |||
| 1295 | |||
| 1296 | /** |
||
| 1297 | * Gets all the real items purchased which are children of this item |
||
| 1298 | * |
||
| 1299 | * @uses EEH_Line_Item::get_descendants_of_type() |
||
| 1300 | * @param EE_Line_Item $parent_line_item - the line item to find descendants of |
||
| 1301 | * @return EE_Line_Item[] |
||
| 1302 | * @throws EE_Error |
||
| 1303 | */ |
||
| 1304 | public static function get_line_item_descendants(EE_Line_Item $parent_line_item) |
||
| 1311 | |||
| 1312 | |||
| 1313 | /** |
||
| 1314 | * Gets all descendants of supplied line item that match the supplied line item type |
||
| 1315 | * |
||
| 1316 | * @uses EEH_Line_Item::_get_descendants_by_type_and_object_type() |
||
| 1317 | * @param EE_Line_Item $parent_line_item - the line item to find descendants of |
||
| 1318 | * @param string $line_item_type one of the EEM_Line_Item constants |
||
| 1319 | * @return EE_Line_Item[] |
||
| 1320 | * @throws EE_Error |
||
| 1321 | */ |
||
| 1322 | public static function get_descendants_of_type(EE_Line_Item $parent_line_item, $line_item_type) |
||
| 1330 | |||
| 1331 | |||
| 1332 | /** |
||
| 1333 | * Gets all descendants of supplied line item that match the supplied line item type and possibly the object type |
||
| 1334 | * as well |
||
| 1335 | * |
||
| 1336 | * @param EE_Line_Item $parent_line_item - the line item to find descendants of |
||
| 1337 | * @param string $line_item_type one of the EEM_Line_Item constants |
||
| 1338 | * @param string | NULL $obj_type object model class name (minus prefix) or NULL to ignore object type when |
||
| 1339 | * searching |
||
| 1340 | * @return EE_Line_Item[] |
||
| 1341 | * @throws EE_Error |
||
| 1342 | */ |
||
| 1343 | protected static function _get_descendants_by_type_and_object_type( |
||
| 1372 | |||
| 1373 | |||
| 1374 | /** |
||
| 1375 | * Gets all descendants subtotals that match the supplied object type |
||
| 1376 | * |
||
| 1377 | * @uses EEH_Line_Item::_get_descendants_by_type_and_object_type() |
||
| 1378 | * @param EE_Line_Item $parent_line_item - the line item to find descendants of |
||
| 1379 | * @param string $OBJ_type object type (like Event) |
||
| 1380 | * @param array $OBJ_IDs array of OBJ_IDs |
||
| 1381 | * @return EE_Line_Item[] |
||
| 1382 | * @throws EE_Error |
||
| 1383 | */ |
||
| 1384 | public static function get_line_items_by_object_type_and_IDs( |
||
| 1395 | |||
| 1396 | |||
| 1397 | /** |
||
| 1398 | * Gets all descendants of supplied line item that match the supplied line item type and possibly the object type |
||
| 1399 | * as well |
||
| 1400 | * |
||
| 1401 | * @param EE_Line_Item $parent_line_item - the line item to find descendants of |
||
| 1402 | * @param string $OBJ_type object type (like Event) |
||
| 1403 | * @param array $OBJ_IDs array of OBJ_IDs |
||
| 1404 | * @return EE_Line_Item[] |
||
| 1405 | * @throws EE_Error |
||
| 1406 | */ |
||
| 1407 | protected static function _get_descendants_by_object_type_and_object_ID( |
||
| 1435 | |||
| 1436 | |||
| 1437 | /** |
||
| 1438 | * Uses a breadth-first-search in order to find the nearest descendant of |
||
| 1439 | * the specified type and returns it, else NULL |
||
| 1440 | * |
||
| 1441 | * @uses EEH_Line_Item::_get_nearest_descendant() |
||
| 1442 | * @param EE_Line_Item $parent_line_item - the line item to find descendants of |
||
| 1443 | * @param string $type like one of the EEM_Line_Item::type_* |
||
| 1444 | * @return EE_Line_Item |
||
| 1445 | * @throws EE_Error |
||
| 1446 | * @throws InvalidArgumentException |
||
| 1447 | * @throws InvalidDataTypeException |
||
| 1448 | * @throws InvalidInterfaceException |
||
| 1449 | * @throws ReflectionException |
||
| 1450 | */ |
||
| 1451 | public static function get_nearest_descendant_of_type(EE_Line_Item $parent_line_item, $type) |
||
| 1455 | |||
| 1456 | |||
| 1457 | /** |
||
| 1458 | * Uses a breadth-first-search in order to find the nearest descendant |
||
| 1459 | * having the specified LIN_code and returns it, else NULL |
||
| 1460 | * |
||
| 1461 | * @uses EEH_Line_Item::_get_nearest_descendant() |
||
| 1462 | * @param EE_Line_Item $parent_line_item - the line item to find descendants of |
||
| 1463 | * @param string $code any value used for LIN_code |
||
| 1464 | * @return EE_Line_Item |
||
| 1465 | * @throws EE_Error |
||
| 1466 | * @throws InvalidArgumentException |
||
| 1467 | * @throws InvalidDataTypeException |
||
| 1468 | * @throws InvalidInterfaceException |
||
| 1469 | * @throws ReflectionException |
||
| 1470 | */ |
||
| 1471 | public static function get_nearest_descendant_having_code(EE_Line_Item $parent_line_item, $code) |
||
| 1475 | |||
| 1476 | |||
| 1477 | /** |
||
| 1478 | * Uses a breadth-first-search in order to find the nearest descendant |
||
| 1479 | * having the specified LIN_code and returns it, else NULL |
||
| 1480 | * |
||
| 1481 | * @param EE_Line_Item $parent_line_item - the line item to find descendants of |
||
| 1482 | * @param string $search_field name of EE_Line_Item property |
||
| 1483 | * @param string $value any value stored in $search_field |
||
| 1484 | * @return EE_Line_Item |
||
| 1485 | * @throws EE_Error |
||
| 1486 | * @throws InvalidArgumentException |
||
| 1487 | * @throws InvalidDataTypeException |
||
| 1488 | * @throws InvalidInterfaceException |
||
| 1489 | * @throws ReflectionException |
||
| 1490 | */ |
||
| 1491 | protected static function _get_nearest_descendant(EE_Line_Item $parent_line_item, $search_field, $value) |
||
| 1510 | |||
| 1511 | |||
| 1512 | /** |
||
| 1513 | * if passed line item has a TXN ID, uses that to jump directly to the grand total line item for the transaction, |
||
| 1514 | * else recursively walks up the line item tree until a parent of type total is found, |
||
| 1515 | * |
||
| 1516 | * @param EE_Line_Item $line_item |
||
| 1517 | * @return EE_Line_Item |
||
| 1518 | * @throws EE_Error |
||
| 1519 | */ |
||
| 1520 | public static function find_transaction_grand_total_for_line_item(EE_Line_Item $line_item) |
||
| 1546 | |||
| 1547 | |||
| 1548 | /** |
||
| 1549 | * Prints out a representation of the line item tree |
||
| 1550 | * |
||
| 1551 | * @param EE_Line_Item $line_item |
||
| 1552 | * @param int $indentation |
||
| 1553 | * @return void |
||
| 1554 | * @throws EE_Error |
||
| 1555 | */ |
||
| 1556 | public static function visualize(EE_Line_Item $line_item, $indentation = 0) |
||
| 1588 | |||
| 1589 | |||
| 1590 | /** |
||
| 1591 | * Calculates the registration's final price, taking into account that they |
||
| 1592 | * need to not only help pay for their OWN ticket, but also any transaction-wide surcharges and taxes, |
||
| 1593 | * and receive a portion of any transaction-wide discounts. |
||
| 1594 | * eg1, if I buy a $1 ticket and brent buys a $9 ticket, and we receive a $5 discount |
||
| 1595 | * then I'll get 1/10 of that $5 discount, which is $0.50, and brent will get |
||
| 1596 | * 9/10ths of that $5 discount, which is $4.50. So my final price should be $0.50 |
||
| 1597 | * and brent's final price should be $5.50. |
||
| 1598 | * In order to do this, we basically need to traverse the line item tree calculating |
||
| 1599 | * the running totals (just as if we were recalculating the total), but when we identify |
||
| 1600 | * regular line items, we need to keep track of their share of the grand total. |
||
| 1601 | * Also, we need to keep track of the TAXABLE total for each ticket purchase, so |
||
| 1602 | * we can know how to apply taxes to it. (Note: "taxable total" does not equal the "pretax total" |
||
| 1603 | * when there are non-taxable items; otherwise they would be the same) |
||
| 1604 | * |
||
| 1605 | * @param EE_Line_Item $line_item |
||
| 1606 | * @param array $billable_ticket_quantities array of EE_Ticket IDs and their corresponding quantity that |
||
| 1607 | * can be included in price calculations at this moment |
||
| 1608 | * @return array keys are line items for tickets IDs and values are their share of the running total, |
||
| 1609 | * plus the key 'total', and 'taxable' which also has keys of all |
||
| 1610 | * the ticket IDs. |
||
| 1611 | * Eg array( |
||
| 1612 | * 12 => 4.3 |
||
| 1613 | * 23 => 8.0 |
||
| 1614 | * 'total' => 16.6, |
||
| 1615 | * 'taxable' => array( |
||
| 1616 | * 12 => 10, |
||
| 1617 | * 23 => 4 |
||
| 1618 | * ). |
||
| 1619 | * So to find which registrations have which final price, we need |
||
| 1620 | * to find which line item is theirs, which can be done with |
||
| 1621 | * `EEM_Line_Item::instance()->get_line_item_for_registration( |
||
| 1622 | * $registration );` |
||
| 1623 | * @throws EE_Error |
||
| 1624 | * @throws InvalidArgumentException |
||
| 1625 | * @throws InvalidDataTypeException |
||
| 1626 | * @throws InvalidInterfaceException |
||
| 1627 | * @throws ReflectionException |
||
| 1628 | */ |
||
| 1629 | public static function calculate_reg_final_prices_per_line_item( |
||
| 1728 | |||
| 1729 | |||
| 1730 | /** |
||
| 1731 | * @param EE_Line_Item $total_line_item |
||
| 1732 | * @param EE_Line_Item $ticket_line_item |
||
| 1733 | * @return float | null |
||
| 1734 | * @throws EE_Error |
||
| 1735 | * @throws InvalidArgumentException |
||
| 1736 | * @throws InvalidDataTypeException |
||
| 1737 | * @throws InvalidInterfaceException |
||
| 1738 | * @throws OutOfRangeException |
||
| 1739 | * @throws ReflectionException |
||
| 1740 | */ |
||
| 1741 | public static function calculate_final_price_for_ticket_line_item( |
||
| 1769 | |||
| 1770 | |||
| 1771 | /** |
||
| 1772 | * Creates a duplicate of the line item tree, except only includes billable items |
||
| 1773 | * and the portion of line items attributed to billable things |
||
| 1774 | * |
||
| 1775 | * @param EE_Line_Item $line_item |
||
| 1776 | * @param EE_Registration[] $registrations |
||
| 1777 | * @return EE_Line_Item |
||
| 1778 | * @throws EE_Error |
||
| 1779 | * @throws InvalidArgumentException |
||
| 1780 | * @throws InvalidDataTypeException |
||
| 1781 | * @throws InvalidInterfaceException |
||
| 1782 | * @throws ReflectionException |
||
| 1783 | */ |
||
| 1784 | public static function billable_line_item_tree(EE_Line_Item $line_item, $registrations) |
||
| 1800 | |||
| 1801 | |||
| 1802 | /** |
||
| 1803 | * Creates a new, unsaved line item from $line_item that factors in the |
||
| 1804 | * number of billable registrations on $registrations. |
||
| 1805 | * |
||
| 1806 | * @param EE_Line_Item $line_item |
||
| 1807 | * @param EE_Registration[] $registrations |
||
| 1808 | * @return EE_Line_Item |
||
| 1809 | * @throws EE_Error |
||
| 1810 | * @throws InvalidArgumentException |
||
| 1811 | * @throws InvalidDataTypeException |
||
| 1812 | * @throws InvalidInterfaceException |
||
| 1813 | * @throws ReflectionException |
||
| 1814 | */ |
||
| 1815 | public static function billable_line_item(EE_Line_Item $line_item, $registrations) |
||
| 1839 | |||
| 1840 | |||
| 1841 | /** |
||
| 1842 | * Returns a modified line item tree where all the subtotals which have a total of 0 |
||
| 1843 | * are removed, and line items with a quantity of 0 |
||
| 1844 | * |
||
| 1845 | * @param EE_Line_Item $line_item |null |
||
| 1846 | * @return EE_Line_Item|null |
||
| 1847 | * @throws EE_Error |
||
| 1848 | * @throws InvalidArgumentException |
||
| 1849 | * @throws InvalidDataTypeException |
||
| 1850 | * @throws InvalidInterfaceException |
||
| 1851 | * @throws ReflectionException |
||
| 1852 | */ |
||
| 1853 | public static function non_empty_line_items(EE_Line_Item $line_item) |
||
| 1884 | |||
| 1885 | |||
| 1886 | /** |
||
| 1887 | * Creates a new, unsaved line item, but if it's a ticket line item |
||
| 1888 | * with a total of 0, or a subtotal of 0, returns null instead |
||
| 1889 | * |
||
| 1890 | * @param EE_Line_Item $line_item |
||
| 1891 | * @return EE_Line_Item |
||
| 1892 | * @throws EE_Error |
||
| 1893 | * @throws InvalidArgumentException |
||
| 1894 | * @throws InvalidDataTypeException |
||
| 1895 | * @throws InvalidInterfaceException |
||
| 1896 | * @throws ReflectionException |
||
| 1897 | */ |
||
| 1898 | public static function non_empty_line_item(EE_Line_Item $line_item) |
||
| 1911 | |||
| 1912 | |||
| 1913 | /** |
||
| 1914 | * Cycles through all of the ticket line items for the supplied total line item |
||
| 1915 | * and ensures that the line item's "is_taxable" field matches that of its corresponding ticket |
||
| 1916 | * |
||
| 1917 | * @param EE_Line_Item $total_line_item |
||
| 1918 | * @since 4.9.79.p |
||
| 1919 | * @throws EE_Error |
||
| 1920 | * @throws InvalidArgumentException |
||
| 1921 | * @throws InvalidDataTypeException |
||
| 1922 | * @throws InvalidInterfaceException |
||
| 1923 | * @throws ReflectionException |
||
| 1924 | */ |
||
| 1925 | public static function resetIsTaxableForTickets(EE_Line_Item $total_line_item) |
||
| 1940 | |||
| 1941 | |||
| 1942 | |||
| 1943 | /**************************************** @DEPRECATED METHODS *************************************** */ |
||
| 1944 | /** |
||
| 1945 | * @deprecated |
||
| 1946 | * @param EE_Line_Item $total_line_item |
||
| 1947 | * @return EE_Line_Item |
||
| 1948 | * @throws EE_Error |
||
| 1949 | * @throws InvalidArgumentException |
||
| 1950 | * @throws InvalidDataTypeException |
||
| 1951 | * @throws InvalidInterfaceException |
||
| 1952 | * @throws ReflectionException |
||
| 1953 | */ |
||
| 1954 | View Code Duplication | public static function get_items_subtotal(EE_Line_Item $total_line_item) |
|
| 1966 | |||
| 1967 | |||
| 1968 | /** |
||
| 1969 | * @deprecated |
||
| 1970 | * @param EE_Transaction $transaction |
||
| 1971 | * @return EE_Line_Item |
||
| 1972 | * @throws EE_Error |
||
| 1973 | * @throws InvalidArgumentException |
||
| 1974 | * @throws InvalidDataTypeException |
||
| 1975 | * @throws InvalidInterfaceException |
||
| 1976 | * @throws ReflectionException |
||
| 1977 | */ |
||
| 1978 | public static function create_default_total_line_item($transaction = null) |
||
| 1990 | |||
| 1991 | |||
| 1992 | /** |
||
| 1993 | * @deprecated |
||
| 1994 | * @param EE_Line_Item $total_line_item |
||
| 1995 | * @param EE_Transaction $transaction |
||
| 1996 | * @return EE_Line_Item |
||
| 1997 | * @throws EE_Error |
||
| 1998 | * @throws InvalidArgumentException |
||
| 1999 | * @throws InvalidDataTypeException |
||
| 2000 | * @throws InvalidInterfaceException |
||
| 2001 | * @throws ReflectionException |
||
| 2002 | */ |
||
| 2003 | View Code Duplication | public static function create_default_tickets_subtotal(EE_Line_Item $total_line_item, $transaction = null) |
|
| 2015 | |||
| 2016 | |||
| 2017 | /** |
||
| 2018 | * @deprecated |
||
| 2019 | * @param EE_Line_Item $total_line_item |
||
| 2020 | * @param EE_Transaction $transaction |
||
| 2021 | * @return EE_Line_Item |
||
| 2022 | * @throws EE_Error |
||
| 2023 | * @throws InvalidArgumentException |
||
| 2024 | * @throws InvalidDataTypeException |
||
| 2025 | * @throws InvalidInterfaceException |
||
| 2026 | * @throws ReflectionException |
||
| 2027 | */ |
||
| 2028 | View Code Duplication | public static function create_default_taxes_subtotal(EE_Line_Item $total_line_item, $transaction = null) |
|
| 2040 | |||
| 2041 | |||
| 2042 | /** |
||
| 2043 | * @deprecated |
||
| 2044 | * @param EE_Line_Item $total_line_item |
||
| 2045 | * @param EE_Transaction $transaction |
||
| 2046 | * @return EE_Line_Item |
||
| 2047 | * @throws EE_Error |
||
| 2048 | * @throws InvalidArgumentException |
||
| 2049 | * @throws InvalidDataTypeException |
||
| 2050 | * @throws InvalidInterfaceException |
||
| 2051 | * @throws ReflectionException |
||
| 2052 | */ |
||
| 2053 | View Code Duplication | public static function create_default_event_subtotal(EE_Line_Item $total_line_item, $transaction = null) |
|
| 2065 | } |
||
| 2066 |
It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.
We recommend to add an additional type check (or disallow null for the parameter):