Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warehouse support #10

Open
deantheiceman opened this issue Oct 31, 2020 · 6 comments
Open

Warehouse support #10

deantheiceman opened this issue Oct 31, 2020 · 6 comments

Comments

@deantheiceman
Copy link

Would it be possible to add warehouse support? Maybe like an extra pivot model so like:

Book + Warehouse = the stock

@gizburdt
Copy link
Member

I thought of this while building the first version, but I didn't need it in that version. Maybe going to look at it later. PR is always welcome :)

@deantheiceman
Copy link
Author

@gizburdt thanks for your reply.

I'm thinking a simple concept may be to use a second "warehouse_id" column then count stock based on the two as a simple way of handling it, do you think that'd work and be acceptable?

@gizburdt
Copy link
Member

Will think about it

@deantheiceman
Copy link
Author

Have you also been using this in production? Wonder if it's worth while implementing a cache functionality so it doesn't need to sum through all the columns every time?

@ahmedgadit
Copy link

plus one for this feature :)

@gogl92
Copy link

gogl92 commented Dec 24, 2021

I think this one can be achieved by using the ReferenceModel not sure what was the intention for this but we can store the warehouse class/name and then just query by the id of the warehouse.

    /**
     * Returns the stock at a given date and warehouse (optionals)
     *
     * @param null $date
     * @param null $warehouse
     * @return int
     */
    public function stock($date = null, $warehouse = null)
    {
        $date = $date ?: Carbon::now();

        if (! $date instanceof DateTimeInterface) {
            $date = Carbon::create($date);
        }

        $mutations = $this->stockMutations()->where('created_at', '<=', $date->format('Y-m-d H:i:s'));

        if ($warehouse !== null) {
            $mutations->where([
                'reference_type' => $warehouse::class,
                'reference_id' => $warehouse->id,
            ]);
        }

        return (int) $mutations->sum('amount');
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants