Posts Tagged ‘magento’

Annual Tax Reports based on a Magento 1.4.1.0+ store.

// January 3rd, 2011 // Programming, Web Development

I had a client request for a more detailed annual tax report in Magento 1.4.1.0. They wanted to break down all their income, between taxable and non-taxable regions, with appropriate annotations of how many adjustments were made based on refunds/credits.

Because the client was only using this a few times a year, I didn’t want to waste too much time writing it as an actual Magento extension, as that can become time consuming. Instead I just wrote a separate PHP script to traverse the MySQL database and perform the necessary calculations.

Since this specific client only charged sales tax to 1 state, I cheated a bit by just splitting it into 2 groups: taxed and non-taxed. If you charge sales-tax in multiple states, then you may need to alter this solution to fit your needs.

  1. <?php
  2.  
  3. // set MySQL connection info
  4. mysql_connect("localhost", "mysql_user", "mysql_password") or die(mysql_error());
  5. mysql_select_db("mysql_database");
  6.  
  7. // quick reference array
  8. $month = array (
  9.         1 => ‘January’,
  10.         2 => ‘February’,
  11.         3 => ‘March’,
  12.         4 => ‘April’,
  13.         5 => ‘May’,
  14.         6 => ‘June’,
  15.         7 => ‘July’,
  16.         8 => ‘August’,
  17.         9 => ‘September’,
  18.         10 => ‘October’,
  19.         11 => ‘November’,
  20.         12 => ‘December’
  21.     );
  22.  
  23. // determine today’s date and split it into tokens
  24. $today = date("Y-m-d");
  25. list($y,$m,$d) = explode("-", $today);
  26.  
  27. // if a date has been posted, then use that value, otherwise, use the default value.  
  28. // note:   using   intval as a quick way to prevent sql injection.  if i force the input to be an integer, then nobody can insert pesky symbols in there to screw with my db
  29. if (isset($_POST[‘start_day’])) { $start_day = intval($_POST[‘start_day’]); } else { $start_day = 1; }
  30. if (isset($_POST[‘start_month’])) { $start_month = intval($_POST[‘start_month’]); } else { $start_month = 1; }
  31. if (isset($_POST[‘start_year’])) { $start_year = intval($_POST[‘start_year’]); } else { $start_year = $y1; }
  32.  
  33. if (isset($_POST[‘end_month’])) { $end_month = intval($_POST[‘end_month’]); } else {  $end_month = 12;  }
  34. if (isset($_POST[‘end_day’])) { $end_day = intval($_POST[‘end_day’]); } else { $end_day = 31; }
  35. if (isset($_POST[‘end_year’])) { $end_year = intval($_POST[‘end_year’]); } else { $end_year = $start_year; }
  36.  
  37. // begin HTML portion
  38. ?>
  39. <html>
  40. <head>
  41. <title>Magento: Online Sales Tax Report</title>
  42. <style type="text/css">
  43. table { margin-top: 25px; border: 1px solid #000; }
  44. td { padding: 3px 20px; }
  45. th { padding: 5px 20px; border-bottom: 1px solid #000; background: #e8e8e8; }
  46. </style>
  47. </head>
  48. <body>
  49. <h2>Magento: Online Sales Tax Report</h2>
  50.  
  51. <form method="post" action="sales_tax_report.php">
  52. <b>Start Date:</b>
  53. <select name="start_month">
  54. <?php for ($i=1; $i<=12; $i++) {?>
  55. <option value="<?php echo $i; ?>" <?php if ($i == $start_month) { echo ‘selected="selected"’; } ?>><?php echo $month[$i]; ?></option>
  56. <?php } ?>
  57. </select>
  58.  
  59. <select name="start_day">
  60. <?php for ($i=1; $i<=31; $i++) { ?>
  61. <option value="<?php echo $i; ?>" <?php if ($i == $start_day) { echo ‘selected="selected"’; } ?>><?php echo $i; ?></option>
  62. <?php } ?>
  63. </select>
  64.  
  65. <select name="start_year">
  66. <?php for ($i=2010; $i<=date("Y"); $i++) { ?>
  67. <option value="<?php echo $i; ?>" <?php if ($i == $start_year) { echo ‘selected="selected"’; } ?>><?php echo $i; ?></option>
  68. <?php }?>
  69. </select>
  70.  
  71.  
  72.  
  73.  
  74. <b>End Date:</b>
  75. <select name="end_month">
  76. <?php for ($i=1; $i<=12; $i++) {?>
  77. <option value="<?php echo $i; ?>" <?php if ($i == $end_month) { echo ‘selected="selected"’; } ?>><?php echo $month[$i]; ?></option>
  78. <?php } ?>
  79. </select>
  80.  
  81. <select name="end_day">
  82. <?php for ($i=1; $i<=31; $i++) { ?>
  83. <option value="<?php echo $i; ?>" <?php if ($i == $end_day) { echo ‘selected="selected"’; } ?>><?php echo $i; ?></option>
  84. <?php } ?>
  85. </select>
  86.  
  87. <select name="end_year">
  88. <?php for ($i=2010; $i<=date("Y"); $i++) { ?>
  89. <option value="<?php echo $i; ?>" <?php if ($i == $end_year) { echo ‘selected="selected"’; } ?>><?php echo $i; ?></option>
  90. <?php }?>
  91. </select>
  92.  
  93.  
  94. <input type="submit" value="Generate Report" />
  95. </form>
  96.  
  97. <?php
  98. // if we’ve come here through a post, then run the query
  99. if (isset($_POST[‘start_year’])) {
  100.         // specify our date ranges
  101.         $start = intval($start_year).‘-’.intval($start_month).‘-’.intval($start_day).‘ 00:00:00′;
  102.         $end = intval($end_year).‘-’.intval($end_month).‘-’.intval($end_day).‘ 23:59:59′;
  103.  
  104.         $query = "SELECT count(*) as order_total, fi.tax_amount > 0 as `taxed`, sum(fi.grand_total) as `sales_total`, sum(fi.tax_amount) as `sales_tax`, sum(fi.shipping_tax_amount) as `shipping_tax`, sum(fc.grand_total) as `credit_total`, sum(fc.tax_amount) as `credit_sales_tax`, sum(fc.shipping_tax_amount) as `credit_shipping_tax` FROM `mag_sales_flat_invoice` fi LEFT JOIN `mag_sales_flat_creditmemo` fc ON fc.order_id = fi.order_id LEFT JOIN `mag_sales_flat_order` fo ON fo.entity_id = fi.order_id WHERE fo.created_at <= ‘$end’ AND fo.created_at >= ‘$start’ GROUP BY fi.tax_amount > 0";
  105.  
  106.         $results = mysql_query($query);
  107.  
  108.         // output query results
  109.         print ‘<table cellspacing="0" cellpadding="0">’;
  110.         print "<tr><th>Region</th><th> </th><th>Sales</th><th>Shipping</th><th>State Tax</th><th>Total</th></tr>\n";
  111.         $regions = array(’0′ => ‘Non-Taxed’, ’1′ =>‘Taxed’);
  112.         while ($row = mysql_fetch_assoc($results)) {
  113.                 print ‘<tr>’;
  114.                 print ‘<td>’.$regions[$row[‘taxed’]].‘</td>’;
  115.                 print ‘<td>Gross</td>’;
  116.                 print ‘<td>$’.number_format($row[‘sales_total’],2).‘</td>’;
  117.                 print ‘<td>$’.number_format($row[‘shipping_tax’],2).‘</td>’;
  118.                 print ‘<td>$’.number_format($row[‘sales_tax’],2).‘</td>’;
  119.                 print ‘<td>$’.number_format($row[‘shipping_tax’]+$row[‘sales_tax’],2).‘</td>’;
  120.                 print ‘</tr>’;
  121.                 print ‘<tr>’;
  122.                 print ‘<td><i>(‘.$row[‘order_total’].‘ invoices)</td><td>Credits</td>’;
  123.                 print ‘<td>$’.number_format($row[‘credit_total’],2).‘</td>’;
  124.                 print ‘<td>$’.number_format($row[‘credit_shipping_tax’],2).‘</td>’;
  125.                 print ‘<td>$’.number_format($row[‘credit_sales_tax’],2).‘</td>’;
  126.                 print ‘<td>$’.number_format($row[‘credit_shipping_tax’]+$row[‘credit_sales_tax’],2).‘</td>’;
  127.                 print ‘</tr>’;
  128.                 print ‘<td> </td><td><b>Total</b></td>’;
  129.                 print ‘<td><b>$’.number_format($row[‘sales_total’]-$row[‘credit_total’],2).‘</b></td>’;
  130.                 print ‘<td><b>$’.number_format($row[‘shipping_tax’]-$row[‘credit_shipping_tax’],2).‘</b></td>’;
  131.                 print ‘<td><b>$’.number_format($row[‘sales_tax’]-$row[‘credit_sales_tax’],2).‘</b></td>’;
  132.                 print ‘<td><b>$’.number_format($row[‘shipping_tax’]+$row[‘sales_tax’]-$row[‘credit_shipping_tax’]-$row[‘credit_sales_tax’],2).‘</b></td>’;
  133.                 print ‘</tr>’;
  134.         }
  135.         print ‘</table>’;
  136.  
  137. }
  138. ?>
  139.  
  140. </body>
  141. </html>
  142.  

This is by no means a perfect script by any stretch of the imagination. But for something that’s run a couple times a year, it will do what you need.

Fixing “Customer Order Comment” extension in Magento 1.4.1.0

// July 26th, 2010 // Programming, Web Development

The “Customer Order Comment” extension written by TanRambun allowed stores to add a comment box for customers upon checkout. Changes made in Magento 1.4.1.0 caused EAV data to be stored differently, resulting in this extension breaking.

To get make it work in 1.4.1.0, you simply need to add a column to the sales_flat_order table. To do so, you can execute the following SQL command:

  1. ALTER TABLE  `sales_flat_order` ADD  `biebersdorf_customerordercomment` TEXT NOT NULL

After doing this, some people have had to clear the Magento cache to get the orders to start storing comments.

Also, according to Remco, in 1.4.1.1:

The plugin was already initialized, so I had to logout/login from the admin again to make it setup again. It works now, thanks!

David Stinemetze is the Lead Developer and Director of Social Media for San Antonio Web Design, SEO and Hosting firm, Internet Direct.

Website | Facebook | Twitter

Adding Extra Fields to Order and Invoice Grid in Magento 1.4.1

// June 28th, 2010 // Programming, Technical, Web Development

Magento is a great eCommerce package. Customizing it, on the other hand, can be rather painful as the documentation provided by Magento is rather limited. But that said, it’s manageable… assuming you don’t ever wish to upgrade. Things are constantly changing in Magento. Things that work in one version of the code, break in the next. The best support you can find is in the Magento forums but often what happens when you find the specific problem you wish to address, it’s for a completely different version of the software. This helps setup kind of a nightmare scenario I had for a client today.

They requested I add a “Subtotal” column to the Order and Invoice grids in the admin panel. Seems simple enough right? So I began looking for documentation on how to do it. The first part involved creating a column in the tables for sub total. This part was easy enough, and rather intuitive:

(more…)