formula for calculating VAT

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cmeadv
    Junior Member
    • Feb 2019
    • 2

    formula for calculating VAT

    Hi there,

    I have an attribute "totalNet", an attribute "vat" and an attribute "grossInvoiceAmout". Actually very simple I want to caclulate with the system the values. That means when I put in the value for the "totalNet", ESPO should calculate the "grossInvoiceAmount" based on the VAT factor.
    I wanted to manage this with the formula an the function ifThen(CONDITION, CONSEQUENT). But it doesnt work!?
    Can anyone help me? Is this possible to manage it via such a function and how must the script code be?
    Or must I use a separate PHP Script like this? Thanks for your help in advance!

    kind regards, Christoph

    <?PHP

    function calculateVat ( $price, $calculate = 1, $vat = 19) {

    if (
    $calculate == 1 ) {

    // Berechnet den Brutto-Preis, ausgehend von einem Netto-Preis
    $newPrice = $price + ( ( $price / 100 ) * $vat );

    return
    round($newPrice, 2);
    }
    elseif (
    $calculate == 2 ) {

    // Berechnet den Netto-Preis, ausgehend von einem Brutto-Preis
    $newPrice = ( $price * 100 ) / ( 100 + $vat );

    return
    round($newPrice, 2);

    }
    else {


    // Berechnet die eigentlichen MwSt. in EUR, ausgehend von einem Brutto-Preis
    $newPrice = ( ( $price * 100 ) / ( 100 + $vat ) ) * ( $vat / 100 );

    return
    round($newPrice, 2);

    }

    }



    /*
    * $calculate
    * 1 = Berechnet den Brutto-Preis.
    * 2 = Berechnet den Netto-Preis.
    * 3 = Berechnet die eigentlichen MwSt. in EUR.
    *
    * Standard = 1
    */

    /*
    * $vat
    * Die Mehrwertsteuer
    *
    * Standard = 19
    */

    // Netto-Preis ist 37,77 EUR


    echo calculateVat( '37.77', 1, 19 ) . '<br>';
    echo
    calculateVat( '44.95', 2, 19 ) . '<br>';
    echo
    calculateVat( '44.95', 3, 19 ) . '<br>';

    ?>
  • item
    Active Community Member
    • Mar 2017
    • 1476

    #2
    Hello,

    just write this formula for you.
    you must look now how add formula to ... look documentation on github

    Not tested my code but you can make more simply (many time ) I do $this->evaluate($item->value[2]). .. then you can do one time !

    PHP Code:
    <?php
    /************************************************************************
     * This file is part of EspoCRM.
     *
     * EspoCRM - Open Source CRM application.
     * Copyright (C) 2014-2018 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko
     * Website: http://www.espocrm.com
     *
     * EspoCRM is free software: you can redistribute it and/or modify
     * it under the terms of the GNU General Public License as published by
     * the Free Software Foundation, either version 3 of the License, or
     * (at your option) any later version.
     *
     * EspoCRM is distributed in the hope that it will be useful,
     * but WITHOUT ANY WARRANTY; without even the implied warranty of
     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     * GNU General Public License for more details.
     *
     * You should have received a copy of the GNU General Public License
     * along with EspoCRM. If not, see http://www.gnu.org/licenses/.
     *
     * The interactive user interfaces in modified source and object code versions
     * of this program must display Appropriate Legal Notices, as required under
     * Section 5 of the GNU General Public License version 3.
     *
     * In accordance with Section 7(b) of the GNU General Public License version 3,
     * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
     ************************************************************************/
    
    namespace Espo\Custom\Core\Formula\Functions\NumericGroup;
    
    use \Espo\Core\Exceptions\Error;
    
    class MyVatCalculator extends \Espo\Core\Formula\Functions\Base
    {
        public function process(\StdClass $item)
        {
            if (!property_exists($item, 'value')) {
                throw new Error();
            }
    
            if (!is_array($item->value)) {
                throw new Error();
            }
    
            if (count($item->value) < 2) {
                throw new Error();
            }
    
            switch ($this->evaluate($item->value[1])){
                case 1: 
                    $return round($this->evaluate($item->value[0]) + ( ( $this->evaluate($item->value[0]) / 100 ) * $vat ), 2);
                    break;
    
                case 2:
                    round(( $this->evaluate($item->value[0]) * 100 ) / ( 100 + $this->evaluate($item->value[2]) ), 2);
                    break;
                case 3:
                    return round(( ( $this->evaluate($item->value[0]) * 100 ) / ( 100 + $this->evaluate($item->value[2]) ) ) * ( $this->evaluate($item->value[2]) / 100 ), 2);
                    break;
                default :
                    return 0;
            }
        }
    }
    If you could give the project a star on GitHub. EspoCrm believe our work truly deserves more recognition. Thanks.​

    Comment

    • cmeadv
      Junior Member
      • Feb 2019
      • 2

      #3
      Hey, thank you so much for your help. I will try and let you know!!

      Comment

      Working...