Giter Site home page Giter Site logo

Add week-end exceptions about business-day HOT 8 CLOSED

ha3jo avatar ha3jo commented on June 29, 2024
Add week-end exceptions

from business-day.

Comments (8)

ha3jo avatar ha3jo commented on June 29, 2024 1

Resolved!

define the extradays in config/global.php


return [

    'extraworkdays' => ['2020-12-12', '2021-12-11'],
]

?>

And in BussinessCalendar.php

   public function isBusinessDay()
    {
        $mixin = $this;

        /**
         * Checks the date to see if it is a business day (neither a weekend day nor a holiday).
         *
         * @return bool
         */
        return function ($self = null) use ($mixin) {
            $carbonClass = @get_class() ?: Emulator::getClass(new \Exception());

            /** @var \Carbon\Carbon|\Cmixin\BusinessDay $self */

            $self = $carbonClass::getThisOrToday($self, isset($this) && $this !== $mixin ? $this : null);
            //array of dates put in config global.php 
            $ex = config('global.extraworkdays');
            if (in_array( $self->format('Y-m-d'), $ex)) {$except = true;} else {$except = false; }
    

            return ($self->isWeekday() or $except) && !$self->isHoliday();
        };
    }

that's all.

from business-day.

ha3jo avatar ha3jo commented on June 29, 2024

for multiple date exceptions:

   public function isBusinessDay()
    {
        $mixin = $this;

        /**
         * Checks the date to see if it is a business day (neither a weekend day nor a holiday).
         *
         * @return bool
         */
        return function ($self = null) use ($mixin) {
            $carbonClass = @get_class() ?: Emulator::getClass(new \Exception());

            /** @var \Carbon\Carbon|\Cmixin\BusinessDay $self */

            $self = $carbonClass::getThisOrToday($self, isset($this) && $this !== $mixin ? $this : null);
            //exception by hand need to automate
            //array of dates
            $ex=array('2020-12-12','2021-12-11');
            if (in_array( $self->format('Y-m-d'), $ex)) {$except = true;} else {$except = false; }
    

            return ($self->isWeekday() or $except) && !$self->isHoliday();
        };
    }

from business-day.

kylekatarnls avatar kylekatarnls commented on June 29, 2024

If you modify code on vendor, you'll lose it next time someone will run composer update, and/or on deploy.

Side note, this if-else is strictly equivalent to:

$except = in_array($self->format('Y-m-d'), config('global.extraworkdays'));

from business-day.

ha3jo avatar ha3jo commented on June 29, 2024

Why not just add holidays the normal way following the documentation example?
https://github.com/kylekatarnls/business-day#setholidays

i couldnt find here method for setting a saturday as business day...

from business-day.

kylekatarnls avatar kylekatarnls commented on June 29, 2024

Yes I first believed you were cancelling holidays which is possible, but the isWeekday is based on $weekendDays in Carbon class which is fixed for every week. So short answer:

With the current version of business-day, you can't do that.

You have to add exception in you application code (not the vendor directory code), you should consider the vendor directory as read-only. Else you'll get so many troubles when deploying your app in different environments, working with teams, updating dependencies...

But a really correct way to handle it is to implement an option in the business-day library (via a pull-request here, not modifying files on your side) so it can call a custom method instead of isWeekday() and then you would be able to customize what is a weekday in your business using a closure.

from business-day.

ha3jo avatar ha3jo commented on June 29, 2024

Thanks for reply!

Ok, i will put a pull-request here, my little modification could be implemented anyway?

from business-day.

kylekatarnls avatar kylekatarnls commented on June 29, 2024

No sorry, actually it can't, business-day works well in any framework or PHP application but config() is a Laravel function. If you add this everywhere business-day is used, you'll break the app of many users having business-day running on non-Laravel applications.

from business-day.

kylekatarnls avatar kylekatarnls commented on June 29, 2024

Follow-up in #55.

from business-day.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.