Category Archives: General

(JS) Calculate price excluding VAT

Sometimes you need to calculate the price of a product excluding VAT, and the only details you have is the amount including vat and the vat percent. This might be a bit tricky in some applications when there are mixed VAT percentages.

For example, you payed 1000 space credits and in that sum there is a 12% VAT included. If you need to find out how much of the 1000 is actual VAT you can use this simple function:

function getAmountWithoutVat(amountIncludingVat, vatPercent) {

  var amountExVat = amountIncludingVat / (100 + vatPercent) * 100;
  var sumVat = amountIncludingVat - amountExVat;
  var amounts = {
        'priceExVat' : amountExVat.toFixed(2),
        'vat' : sumVat.toFixed(2)
      };

  return amounts;
}

console.log(getAmountWithoutVat(1000, 12));

Output:

{
  priceExVat: "892.86",
  vat: "107.14"
}

Laravel with SSL through Cloudflare on Heroku.

 

I deployed a Laravel app on Heroku, using Cloudflare for SSL. As a quick note, here’s how I did it.

  1. Deploy the app on Heroku and make sure everything works fine using the heroku app url.
  2. Add the domain names to your app in Heroku (in the settings tab for the app). Make sure you add both the root domain and www if you’re using it (example.com, www.example.com). Don’t activate SSL in Heroku.
  3. Add the site to your Cloudflare account (choose the free plan, when asked).
  4. Point your domain to Cloudflare by changing the name servers  (at the registrars control panel) to the ones Cloudflare gives you when adding the site.
  5. Wait for the name server changes to go through. It will be notified under the Overview tab on Cloudflare. When this is done you will administer the domain records on Cloudflare instead of your domain regristrar.
  6. Remove all the DNS records you don’t need, under the DNS tab in Cloudflare.  For the next step to work you need to remove the A records for the root domain – since you won’t point it to an IP address, but a domain on Heroku.
  7. Point Cloudflare to your Heroku app by adding cname records pointing to the Heroku app url.
    Like this.

    Type: CNAME
    Name: jymden.com
    Domain name: myapp.herokuapp.com

    Type: CNAME
    Name: www
    Domain name: myapp.herokuapp.com

  8. In Cloudflare, go to the Crypto tab. Set SSL to Full:
  9. Make sure your Universal SSL certificate is activated. This will happen automatically a little while after adding the site to Cloudflare (up to 24 hours, but usually faster).  When it’s activated you’ll see it a bit down in the Crypto tab, like this:
  10. Prepare your Laravel app to use https by adding this to the boot function AppServiceProvider.php (App/Providers):
    public function boot(UrlGenerator $url)
    {
        if (env('APP_ENV') !== 'local') {
            $url->forceSchema('https');
        }
    }
    

    NOTE: if you’re using Laravel 5.4 or higher it’s forceScheme instead of forceSchema

    Also, you need to set your Laravel environment variable APP_ENV to production (or at least something else than local). Do this in the Heroku app settings tab.

  11. Now try to enter your site with https. It might take a while for it to kick in.
  12. When you see that https is working correctly, go in to the Page Rules tab in Cloudflare. Click Create Page Rule and add the rule to always use https for the domain. Use wildcards to cover all urls. Like this:
  13. Drink coffee.

YouTube Tutorials

My all time favorite tutorial maker Derek Banas has an extensive catalogue of programming tutorials on his Youtube channel. I’ve used those a lot, especially in my studies. His series on design patterns are golden!

I really like the tempo in them. Very hands-on and very organised.

He has a bunch of videos that covers entire techniques in a single video. Perfect if you’re a somewhat experienced developer that need to get up to speed in something.

Here’s and example:

A couple of principles when learning how to code.

Principle 1.
Always have a clear image of what you want to do BEFORE you start writing your code. Then break it down in logical instructions – making an algorithm.  An algorithm is a step by step instruction on how to solve a specific problem. You might think of it as a recipe. If you want to bake bread – you don’t just start shoving any eatable stuff you find into the oven without a plan. First you decide what kind of bread you want to make, then you find out what ingredients is needed, how much and in what order they are added.

So basically – if you can’t solve the problem with logic you can’t solve it with code.

For example:

Continue reading