Showing posts with label magento. Show all posts
Showing posts with label magento. Show all posts

Mar 6, 2013

Magento: Reset Credit Memos

Sometimes in Magento you need to reset your database. The most common situation is when you need to pass your system from testing to production. Then, you may have a lot of test purchases, credit memos, invoices and other stuff in your database that you need to get rid of.

I faced this situation in the past. I found many scripts in order to reset the database, but not ALL the tables of the database. The scripts I found missed to reset one important thing: credit memos.

To solve this issue, I had to take a closer look at the Magento database structure and guess where the credit memo info was stored. The result is this small script, which will reset your credit memos, basically deleting them all:

SET FOREIGN_KEY_CHECKS=0;

TRUNCATE `sales_flat_creditmemo`;
TRUNCATE `sales_flat_creditmemo_grid`;
TRUNCATE `sales_flat_creditmemo_comment`;
TRUNCATE `sales_flat_creditmemo_item`;

ALTER TABLE `sales_flat_creditmemo` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_creditmemo_grid` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_creditmemo_comment` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_creditmemo_item` AUTO_INCREMENT=1;

Of course, your tables might have some sort of prefix. In that case, just change the database table names accordingly.

Hope this is helpful for you!

Oct 14, 2012

Disabling days in Magento calendar

Sometimes you need to modify the way Magento calendar works, so the user can't choose a holiday or a weekend day. This is not specifically explained by the Magento community or documentation, so I decided to post it here.

My version of Magento is 1.7, just to let you know, but should work with others.

Let's say you call your calendar via javascript like this:

    <script type="text/javascript">
        //<![CDATA[
        Calendar.setup({
            inputField: "sdate",
            ifFormat: "%Y-%m-%d",
            showsTime: false,
            button: "_dob_trig",
            align: "Bl",
            singleClick: true      
        });      
        //]]>
    </script>      

Then you get a functional calendar, but the user can choose any day of the year, and that's something we want to prevent.

So, what you have to do is to add a "disableFunc"  function to this calendar setup.

disableFunc: function(date) {
 //return true if you want the day disabled, false otherwise
 ...
}

For example, you could do something like this to disable sundays (remember, in javascript's Date object, Sunday is 0, Monday 1, ... until Saturday that is 6!


    <script type="text/javascript">
        //<![CDATA[
        Calendar.setup({
            inputField: "sdate",
            ifFormat: "%Y-%m-%d",
            showsTime: false,
            button: "_dob_trig",
            align: "Bl",
            singleClick: true,

            disableFunc: function(date) {
                  //disable all sundays and New Year day
                  var month = date.getMonth() + 1; //add 1 because January = 0 and so on
                  var day = date.getDate(); //get day number
                  if (month==1 && day == 1) return true;
                  return date.getDay() == 0;  //0 for sunday, 1 for monday, etc
            }
     });
     //]]>
     </script>

So the code above checks (the disableFunc is called for every day in the current month displayed by the calendar!) if the day is 1-1, if so, the day is disabled. Otherwise, it checks if the day is a Sunday, and if so, the day is also disabled.

Hope this code helps you out with calendar manipulation in Magento!