discount.php 3.99 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 with `id`=$id
     * @param $id
     * @return array
     */
    public function getOneData($id)
    {
        return $this->get(
            '
                SELECT *
                FROM public.discount
                WHERE
                    id = :id
            ',
            [
                'id' => $id
            ],
            -1
        );
    }


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


    /**
     * Delete discount with `id`=$id
     * @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
                        )
                        VALUES
                        (
                          :name,
                          :discount,
                          :description,
                          :start_date,
                          :end_date
                        )
                        RETURNING id
            ',
            [
                'name' => $data['name'],
                'discount' => $data['discount'],
                'description' => $data['description'],
                'start_date' => $data['start_date'],
                'end_date' => $data['end_date']
            ],
            -1
        );


    }

    /**
     * Update discount with `id`=$id
     * @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
                WHERE
                    id  = :id
            ',
            [
                'name' => $data['name'],
                'discount' => $data['discount'],
                'description' => $data['description'],
                'start_date' => $data['start_date'],
                'end_date' => $data['end_date'],
                'id' => $id
            ]
        );
    }

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

            ],
            -1
        );
    }

}