Modify How Quotes Amount is Computed

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Triggerz
    Senior Member
    • May 2024
    • 145

    #1

    Modify How Quotes Amount is Computed

    Hi,

    I am looking for a way to modify how the Quotes amount is computed.

    I plan to add a custom field called additionalDiscount in the Quote entity. This discount should be added to the total discount including any discounts placed in the unit price of Quoteitems.

    I also want to add a markup field for each Quoteitems and be added to the computation as such (qty x unitprice x markup)

    I have the Sales pack and not sure if this customization is possible to achieve.

    Please help and thanks in advance...
  • rabii
    Active Community Member
    • Jun 2016
    • 1384

    #2
    this should help

    Rabii
    EspoCRM Custom Development

    🔗 Portfolio & Builds

    Comment

    • yuri
      EspoCRM product developer
      • Mar 2014
      • 9679

      #3
      Maybe consider adding an extra line item (with formula?) with the negative unit price and quantity 1. This would be the only proper way to do it. The computed total net amount should equal net amounts of all line items. Otherwise, it breaks the logic.

      Comment

      • Triggerz
        Senior Member
        • May 2024
        • 145

        #4
        Originally posted by rabii

        Thanks rabii. I tried to implement the instruction in the link but the total amount does not get updated. Not sure what I am doing wroing, please help.

        Below is the modified code in the CalculateItems.php file

        Thanks and Regards...

        Code:
        <?php
        namespace Espo\Custom\Hooks\Quote;
        
        use Espo\ORM\Entity;
        
        class CalculateItems
        {
        public static int $order = 10;
        
        public function __construct(
        // Define needed dependencies.
        ) {}
        
        public function beforeSave(Entity $entity, array $options): void
        {
        if (!$entity->has('itemList')) {
        $entity->loadItemListField();
        }
        
        $itemList = $entity->get('itemList');
        
        $amount = 0.0;
        foreach ($itemList as $item) {
        $amount += $item->quantity * $item->unitPrice * $item->factor;
        }
        
        $bulkDisk = $entity->cBulkDiscount;
        $amount = $amount - $bulkDisk;
        $entity->set('amount', $amount);
        }
        }
        ​

        Comment

        • rabii
          Active Community Member
          • Jun 2016
          • 1384

          #5
          heyTriggerz

          NB: MADE CHANGES HERE TO MEET YOUR LOGIC - ADD ADDITIONAL DISCOUNT TO THE EXISTING DISCOUNTAMOUNT

          I guess you just need to make a tiny change to get the cBulkDiscount from the entity - it should be $entity->get('cBulkDiscount') - also you don't need to recalculate the amount you are using the same code from the example at documentation which is not necessary since you just want to reduce bulkdiscount from amount - below is a simple code call the file CalculateTotalDiscount.php

          PHP Code:
          <?php
          namespace Espo\Custom\Hooks\Quote;

          use 
          Espo\ORM\Entity;

          class 
          CalculateTotalDiscount
          {
              public static 
          int $order 10;
              
              public function 
          beforeSave(Entity $entity, array $options): void
              
          {
                  if (!
          $entity->get('amount')) {
                       return;
                  }
                  
                  
          $discountAmount $entity->get('discountAmount');
                  
          $additionalDiscount $entity->get('cBulkDiscount');
                  
                  
          $totalDiscountAmount $discountAmount $additionalDiscount;
                  
                  
          $entity->set('discountAmount'$totalDiscountAmount);
              }
          }

          Also don't forget to implement a client side calculation to see numbers change on front end https://docs.espocrm.com/development...de-calculation

          Cheers
          Last edited by rabii; Today, 08:53 AM.
          Rabii
          EspoCRM Custom Development

          🔗 Portfolio & Builds

          Comment

          • Triggerz
            Senior Member
            • May 2024
            • 145

            #6
            Thanks much rabii. Can you also share a sample code on how to implement the client side calculation? Thanks again...

            Comment

            • rabii
              Active Community Member
              • Jun 2016
              • 1384

              #7
              It should be something as below create quote-total-discount-calculation.js under client/custom/src folder

              PHP Code:
              define(['custom:quote-total-discount-calculation'], (Dep) => {

                  return class extends 
              Dep {

                      
              /**
                       * Calculate total discountamount with additional discount.
                       *
                       * @param {import('model').default} model An order.
                       */
                      
              calculate(model) {
                          
              super.calculate(model);

                          if(
              model.get('cBulkDiscount')) {
                              const 
              discountAmount model.get('discountAmount');
                              const 
              additionalDiscount model.get('cBulkDiscount');
                              
              let totalDiscountAmount discountAmount additionalDiscount;

                              
              model.set('discountAmount'totalDiscountAmount);
                          }
                      }
                  }
              }); 

              based on what you shared you wanted to add an additional discount from the field cBulkDiscount to the existing discountAmount. This will do the job both front end and backend - please make changes in the backend - see updated code above for php hook class.

              Hope this helps
              Rabii
              EspoCRM Custom Development

              🔗 Portfolio & Builds

              Comment

              Working...