On-hand status orders

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Marius
    Junior Member
    • Jun 2025
    • 5

    #1

    On-hand status orders

    Hi All

    So... As I understand the stock in SalesPack can have 3 statuses:
    Available
    On Hand
    Not Available

    When stock is Not Available the SO can't be created. When stock is Available or On Hand the SO can be created.

    I would like to stop my users from being able to create orders when stock is On Hand. Is that possible? With the nature of my business, sometimes an SO can sit for a few days without being confirmed and a coresponding DO being created. I don't want others to be able to swoop in and take the stock that's already soft-reserved.


    EDIT:
    It looks like even when stock is 'Not Available' the SO can still be created. So to further my enquiry, can this be prevented?

    Thanks.
    Last edited by Marius; 11-27-2025, 08:55 AM.
  • yuri
    EspoCRM product developer
    • Mar 2014
    • 9526

    #2
    Hi,

    You can utilize a before save API script. In the script, iterate through the itemList and check whether the availability quantity is greater than 0 for the productId. You can check availability quantities with formula functions. https://docs.espocrm.com/extensions/...mula-functions
    If you find EspoCRM good, we would greatly appreciate if you could give the project a star on GitHub. We believe our work truly deserves more recognition. Thanks.

    Comment

    • Marius
      Junior Member
      • Jun 2025
      • 5

      #3
      Hi Yuri

      I was afraid you're going to say this. Though my instance of Espo is incorporating coding and Before Save Custom Scripts, I was never able to get it to iterate through the item list.
      Is there a tutorial that you could please point me to?

      Cheers


      Comment

      • lazovic
        Super Moderator
        • Jan 2022
        • 1193

        #4
        Hi Marius,

        Please go to the Administration > Entity Manager > Sales Order > Formula > API Before-Save Script and insert the following formula script:
        Code:
        if (entity\isNew()) {
            $items = itemList ?? list();
            $i = 0;
        
            while ($i < array\length($items)) {
                $item = $items[$i];
                $productId = $item['productId'] ?? null;
            
                if (!$productId) {
                    continue;
                }
        
                $availabilityQuantity = ext\product\quantity($productId);
                $lineQuantity = $item['quantity'] ?? 0;
        
                if ($availabilityQuantity < $lineQuantity) {
                    recordService\throwForbidden("Cannot create as some items are not available in stock.");
                }
        
                $i = $i + 1;
            }
        }
        Therefore, if users want to save a new Sales Order that contains products that are unavailable, they will not be able to do so and will see a corresponding notification.
        Last edited by lazovic; 11-28-2025, 08:54 AM. Reason: Adjustment following Yuri's advice.

        Comment

        • yuri
          EspoCRM product developer
          • Mar 2014
          • 9526

          #5
          Correct path: Administration > Entity Manager > Sales Order > Formula > API Before-Save Script.
          If you find EspoCRM good, we would greatly appreciate if you could give the project a star on GitHub. We believe our work truly deserves more recognition. Thanks.

          Comment

          • Marius
            Junior Member
            • Jun 2025
            • 5

            #6
            Hi Iazovic

            That's amazing!! Works exactly as expected. Thank you!

            And thank you Yuri, I have indeed put it in API Before Save.

            Comment

            Working...