4 replies [Last post]
graper@drupal.org's picture
Offline
Joined: 08/22/2008
Juice: 16
Was this information Helpful?

We have a customer that we built an Drupal + Ubercart site for. They have what I beleive will be a completely unique request when it comes to taxes. They said that they need to be able to apply taxes to all the zip codes in the U.S. because they have people in every state.

We found out that there are little over 40,000 zipcodes total. I set about to enter all the tax rates in through the back end with a few queries and a csv file. Just having that many records in the uc_taxes table alone made the drupal installation take up more then 128megs as php halted the script and said that it hit the limit. I upped the limit and got it working, but that would not happen in a production environment.

Needless to say, that method of having 40k+ entries in uc_taxes will not work as it completely choked workflow-ng even when I changed the time limit for php to 300 seconds.

What I did find was that workflow-ng can execute custom php for both the condition and the action, so I turn to the community. While I know I can create the condition code, have it check a custom table or even hit a different db server to find the tax rate, what I am having a hard time with is how to make the code for the action it would take if the condition returns true.

Considering that a few states have decided to make it harder to determine what taxes should be applied, what with the deciding factor being if you use shipper's address or buyer's address to determine taxes, I'd like to open up discussion on how what kind of code needs to be made to make add a tax item based on customer workflow-ng snippet. I figure that if we collectively come up with how to make the action work, we can all point to it and say, "if you have a convoluted tax mess you need to make happen" then point them to the custom workflow-ng snippets, or find a php developer that can create a custom snippit to be used in workflow-ng.

While this method would include the overhead of creating a custom table, or have custom algorithms, it would at least be answered.

So can anyone tell me how I can get a workflow-ng action to apply a custom tax?

Granville

Lyle's picture
Offline
AdministratoreLiTe!
Joined: 08/07/2007
Juice: 6846
Re: Taxes and Workflow-ng

If Workflow-ng would get too bogged down with conditions and actions, then it might be better to write a new tax module that can calculate all of the different taxes more efficiently.

There is now a hook_calculate_tax() that lets other modules to provide taxes. uc_taxes is still needed to provide a way for the line items to be attached to the orders, but it doesn't need to use its own tax rules.

graper@drupal.org's picture
Offline
Joined: 08/22/2008
Juice: 16
Re: Re: Taxes and Workflow-ng

While making a new taxes module wouldn't hurt, I didn't want to go about re-inventing the wheel or suggest any kind of large changes when I felt that making some snippets in workflow-ng would/could work for those strange cases. The current set of tax and workflow works beautifully even if it doesn't scale out to 20k+ tax conditions, but it doesn't need to if this works or if people want to create special modules for those special conditions.

I got to playing around with the workflow-ng's execution of custom php code and I believe that all that would be needed is to have the action return a new array for the $tax_line_item variable. I believe that if this is returned properly, then so long as the condition is met (can return true for everything and let the action processes any real rules) it will provide a fully custom way of evaluating anything needed for any situation.

I'll play around with it more today or over the weekend and see what I can come up with.

Granville

graper's picture
Offline
Joined: 06/27/2008
Juice: 60
Finally got to testing this

Ok,I know I said I would test it last weekend, unfortunately I wasn't able to do that last weekend. I have however, gotten some testing done, and I wanted to report my findings.

If you need a highly customized way of calculating taxes and setting up multiple tax rates doesn't work for you, this method will work, but I imagine that making a new tax module may work better, but I'm not sure.

First: in Administer > Store Administration > Configuration > Tax Rates and Settings : You must have one item set up here to tax the products. I found that if you don't have anything here, you will not be able to setup anything in Workflow_ng for tax rates. Didn't look further into it, but I hope that this will change in the future. For now I setup a a rate of 10% and labeled it "Local Tax Rate". Don't worry about what you call it, it can be changed dynamically with Workflow-ng

Second: In Adminsiter > Workflow-ng : you will find the tax rate you just made. Click on "Local Tax Rate" (not edit), and you will be on a page where you can change the conditions and actions. The one action that is there needs to stay there, if it is removed, the tax calculation won't work. Click on add a new action, and at the bottom of the drop down list is "Execute Custom PHP", choose that and Click on "Add". In the following window you'll want to add the php code that will generate the new tax rate, such as the following.

$new_tax=db_fetch_object(db_query('SELECT tax_rate FROM {taxes} WHERE zipcode="' . $order->delivery_Postal_code . '"'));

$row=db_fetch_object(db_query('SELECT sum(p.sell_price) AS order_total FROM uc_products p INNER JOIN uc_cart_products cp on cp.nid=p.nid WHERE cart_id=' . $order->products[0]->cart_id));

$tax_line_item->amount = ($row->order_total * $new_tax->tax_rate);
$tax_line_item->name = "new tax line item name"

the first line queries the `taxes` table for the new rate based on a zip code, your table and query may vary.

The second line is to get the sub_total. While Workflow-ng passed in the $order variable, it did not however have the order total during the first pass, which happens when you first get to the checkout page. While order total is given after you click on "Review Order" and taxes were calculated properly on that page, when you first get to the checkout page where you put in your payment details the taxes were not calculated properly if $order->order_total was used. So the second line was needed to get the sub_total.

The third and forth line change the $tax_line_item to reflect the new tax rate and a new name for the tax line item. This can be change to reflect "Tax Rate (zip)" or whatever.

So there it is. a completely customized way to get tax rates without having to write a module. The only cons I see with this is that the taxes table that you query from will need to be administered in some fashion.

Granville
Kirkham Systems Inc.

z3b
z3b's picture
Offline
Joined: 08/15/2007
Juice: 51
Re: Finally got to testing this

I had many troubles getting uc_taxes to work in Quebec, hook_calculate_tax() saved my life Eye-wink