discount.php 5.37 KB
<?php
/**
 * Created by PhpStorm.
 * User: Alex Savenko
 * Date: 20.12.2016
 * Time: 13:39
 */

namespace models;


class discount extends \db
{

    /**
     * Get all discounts
     * @return array
     */
    public function getAllData()
    {

        return $this->get(
            '
                SELECT * FROM
                    public.discount
            '
            ,
            [
            ],
            -1
        );
    }


    /**
     * Get discount
     * @param $id
     * @return array
     */
    public function getOneData($id)
    {
        return $this->get(
            '
                SELECT *
                FROM public.discount
                WHERE
                    id = :id
            ',
            [
                'id' => $id
            ],
            -1
        );
    }

    /**
     * Get discount indication status
     * @param $id
     * @return array
     */
    public function getStatus($id) {

        $status = $this->get(
            '
                SELECT status
                FROM public.discount
                WHERE
                    id = :id
            ',
            [
                'id' => $id
            ],
            -1
        );

        return $status[0]['status'];

    }


    /**
     * Get actual discount
     * @return array
     */
    public function getActiveData()
    {
        return $this->get(
            '
                SELECT *
                FROM public.discount
                WHERE
                    status = 1
                    AND
                    current_timestamp > start_date
                    AND
                    current_timestamp < end_date
                LIMIT 1
                    
            ',
            [
            ],
            -1
        );
    }


    /**
     * Delete discount
     * @param $id
     * @return bool
     */
    public function deleteData($id)
    {
        return $this->exec(
            '   DELETE
                FROM
                    public.discount
                WHERE
                    id  = :id
            ',
            [
                'id' => $id
            ]
        );
    }

    /**
     * Add new discount
     * @param $data
     * @return array
     */
    public function addData($data)
    {

        return $this->get(
            '
                INSERT INTO
                    public.discount
                        (
                          name,
                          discount,
                          description,
                          start_date,
                          end_date,
                          status,
                          group_ids
                        )
                        VALUES
                        (
                          :name,
                          :discount,
                          :description,
                          :start_date,
                          :end_date,
                          :status,
                          :group_ids
                        )
                        RETURNING id
            ',
            [
                'name' => $data['name'],
                'discount' => $data['discount'],
                'description' => $data['description'],
                'start_date' => $data['start_date'],
                'end_date' => $data['end_date'],
                'group_ids' => !empty($data['group_ids']) ? '{'. implode(', ', $data['group_ids']) . '}' : null,
                'status' => $data['status']
            ],
            -1
        );


    }

    /**
     * Update discount
     * @param $data
     * @param $id
     * @return bool
     */
    public function updateData($data, $id)
    {

        return $this->exec(
            '
                UPDATE
                    public.discount
                SET
                    name = :name,
                    discount = :discount,
                    description = :description,
                    start_date = :start_date,
                    end_date = :end_date,
                    status = :status,
                    group_ids = :group_ids
                WHERE
                    id  = :id
            ',
            [
                'name' => $data['name'],
                'discount' => $data['discount'],
                'description' => $data['description'],
                'start_date' => $data['start_date'],
                'end_date' => $data['end_date'],
                'status' => $data['status'],
                'group_ids' => !empty($data['group_ids']) ? '{'. implode(', ', $data['group_ids']) . '}' : null,
                'id' => $id
            ]
        );

    }

    /**
     * Switch status indicator
     * @param bool $status
     * @param $id
     * @return bool
     */
    public function updateStatus($status, $id) {

        return $this->exec(
            '
                UPDATE
                    public.discount
                SET
                    status = :status
                WHERE
                    id  = :id
            ',
            [
                'status' => $status,
                'id' => $id
            ]
        );

    }

    /**
     * Count all discounts
     * @return array
     */
    public function countData()
    {
        return $this->get(
            '
                SELECT
                    COUNT(id) AS total
                FROM
                    public.discount
            ',
            [

            ],
            -1
        );
    }

}