Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix calculation when divided by zero. If anywhere, any value is divid… #23

Merged
merged 2 commits into from
Sep 19, 2024

Conversation

Aunshon
Copy link
Contributor

@Aunshon Aunshon commented Sep 19, 2024

…ed by zero, there can be a fatal.

Summary by CodeRabbit

  • New Features

    • Enhanced calculations for vendor commission rates and shipping amounts to prevent division by zero errors.
    • Improved refund processing logic to ensure valid quantities for shipping calculations.
    • Updated migration progress calculation to avoid runtime errors.
    • Updated metadata for the Dokan Migrator plugin, including changes to the Plugin URI and Author details.
    • Revised contact information in the readme for reporting bugs and feature requests.
  • Bug Fixes

    • Safeguards implemented to ensure accurate financial calculations and shipping distributions.

@Aunshon Aunshon self-assigned this Sep 19, 2024
Copy link

coderabbitai bot commented Sep 19, 2024

Walkthrough

The changes involve modifications to various methods across multiple files to enhance error handling and prevent division by zero errors in calculations related to shipping amounts, commission rates, and migration progress. Key adjustments include ensuring that divisor values default to 1 when associated arrays or totals are empty or invalid. These updates improve the robustness of financial calculations and shipping distributions within the order processing and migration logic.

Changes

Files Change Summary
includes/Helpers/MigrationHelper.php Updated split_parent_order_shipping to prevent division by zero by ensuring $vendors_count defaults to 1.
includes/Integrations/WcVendors/OrderMigrator.php Modified commission rate calculation in get_dokan_order_data to default subtotal to 1 if empty or less than one.
includes/Integrations/Wcfm/OrderMigrator.php Enhanced commission rate calculation and shipping logic in get_dokan_order_data and process_refund for robustness.
includes/Migrator/Manager.php Adjusted migration progress calculation in migrate method to default total_count to 1 if empty, preventing division errors.
dokan-migrator.php Updated plugin metadata including Plugin URI, Author, and Author URI for clarity and consistency.
readme.txt Changed bug report and feature request link to the new GitHub repository for issue tracking.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant OrderMigrator
    participant MigrationHelper
    participant Manager

    User->>OrderMigrator: Request order data
    OrderMigrator->>OrderMigrator: Calculate commission rate
    OrderMigrator->>MigrationHelper: Split shipping costs
    MigrationHelper->>OrderMigrator: Return shipping distribution
    OrderMigrator->>User: Provide order data

    User->>Manager: Start migration
    Manager->>Manager: Calculate migration progress
    Manager->>User: Update migration status
Loading

🐇 "In the meadow, changes bloom,
With careful code, we clear the gloom.
Division by zero, a thing of the past,
Robust and steady, our functions will last.
With shipping and commissions now safe and sound,
Hops of joy for improvements abound!" 🐰


Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 90872e0 and 7acf3bc.

Files selected for processing (2)
  • dokan-migrator.php (1 hunks)
  • readme.txt (1 hunks)
Files skipped from review due to trivial changes (2)
  • dokan-migrator.php
  • readme.txt

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    -- I pushed a fix in commit <commit_id>, please review it.
    -- Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    -- @coderabbitai generate unit testing code for this file.
    -- @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    -- @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    -- @coderabbitai read src/utils.ts and generate unit testing code.
    -- @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    -- @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Outside diff range and nitpick comments (3)
includes/Integrations/WcVendors/OrderMigrator.php (1)

122-124: Good catch on preventing division by zero, but consider more accurate fallback values.

Defaulting the subtotal to 1 when it's empty or less than 1 effectively prevents division by zero errors in the commission rate calculations. This is a positive change that improves the robustness of the code.

However, always defaulting to 1 might not be the most accurate approach in all scenarios. It could potentially lead to incorrect commission calculations in edge cases where the subtotal is legitimately zero or negative.

Consider alternative fallback values or error handling mechanisms based on your specific business requirements. For example:

  • Use a small positive epsilon value close to zero instead of 1.
  • Throw an exception or log an error message to alert about the unexpected subtotal value.
  • Define a default commission rate to use when the subtotal is invalid.

Discuss with the team and stakeholders to determine the most appropriate approach that aligns with your business logic and handles these edge cases accurately.

includes/Integrations/Wcfm/OrderMigrator.php (2)

222-222: Fallback for undefined package quantity.

The change sets the package quantity to 1 if it is not already defined when processing refunds. This provides a fallback value to prevent issues with undefined $package_qty in subsequent shipping calculations.

While setting the package quantity to 1 allows the refund process to proceed without errors, it may not accurately represent the original package quantity. Consider using the actual package quantity if it is available from other sources or order data.


415-421: Safeguard against empty vendor count.

The changes introduce a safeguard to handle cases where the count of vendors is empty. By defaulting $vendors_count to 1 when count($vendors) is empty, the shipping and tax amounts are still split, preventing division by zero errors.

However, when there are no vendors associated with the order, splitting the shipping costs equally among a default count of 1 may not be the most accurate approach. Consider handling the case of no vendors differently, perhaps by assigning the full shipping costs to the parent order or using a different distribution logic.

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 1de2715 and 90872e0.

Files selected for processing (4)
  • includes/Helpers/MigrationHelper.php (1 hunks)
  • includes/Integrations/WcVendors/OrderMigrator.php (1 hunks)
  • includes/Integrations/Wcfm/OrderMigrator.php (3 hunks)
  • includes/Migrator/Manager.php (1 hunks)
Additional comments not posted (4)
includes/Helpers/MigrationHelper.php (2)

206-206: Good catch! This change prevents division by zero errors.

Defaulting $vendors_count to 1 when $vendors array is empty is a crucial safeguard. It ensures that subsequent division operations always have a valid divisor, preventing potential crashes or unexpected behavior.


210-210: Looks good! The shipping costs are now split correctly.

Dividing the total shipping tax and shipping amount by $vendors_count ensures that each vendor suborder gets an equal share of the shipping costs. This maintains fairness in cost distribution among vendors.

The previous safeguard of defaulting $vendors_count to 1 when $vendors is empty guarantees that these divisions are always valid, preventing any potential issues.

Also applies to: 212-212

includes/Migrator/Manager.php (1)

207-209: LGTM! The change effectively prevents division by zero errors.

The introduction of the total_count variable, which defaults to 1 when $this->total_count is empty, ensures that the progress calculation will not encounter division by zero errors. This change enhances the robustness of the migration process by handling scenarios where the total count may be invalid.

The logic flow remains intact, and the modification improves error handling in the progress calculation.

includes/Integrations/Wcfm/OrderMigrator.php (1)

120-122: LGTM!

The changes introduce a safeguard to prevent division by zero errors when calculating the vendor's commission rate. By defaulting the divisor to 1 when $order->item_sub_total is empty or less than 1, the calculation remains robust even in edge cases.

The updated logic ensures the commission rate is calculated correctly without introducing any apparent issues or side effects.

@Aunshon Aunshon merged commit 2a3a16a into develop Sep 19, 2024
1 check failed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Update brand name and github link in wp org
1 participant