Heray-Was-Here
Server : Apache
System : Linux mail.lomejor.cr 6.8.0-1059-azure #65~22.04.1-Ubuntu SMP Thu May 28 16:59:19 UTC 2026 x86_64
User : www-data ( 33)
PHP Version : 8.2.31
Disable Function : NONE
Directory :  /var/www/dev/htdocs/custom/discountprice/class/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /var/www/dev/htdocs/custom/discountprice/class/discountpriceproduct.class.php
<?php
/* Copyright (C) 2007-2012  Laurent Destailleur <eldy@users.sourceforge.net>
 * Copyright (C) 2014       Juanjo Menent       <jmenent@2byte.es>
 * Copyright (C) 2015       Florian Henry       <florian.henry@open-concept.pro>
 * Copyright (C) 2015       Raphaël Doursenaud  <rdoursenaud@gpcsolutions.fr>
 * Copyright (C) 2017       Open-DSI            <support@open-dsi.fr>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * \file    discountprice/class/discountpriceproduct.class.php
 * \ingroup discountprice
 * \brief
 */

require_once DOL_DOCUMENT_ROOT . '/core/class/commonobject.class.php';

/**
 * Class DiscountPriceProduct
 *
 * Put here description of your class
 * @see CommonObject
 */
class DiscountPriceProduct extends CommonObject
{
	public $element = 'discountprice_product';
	public $table_element = 'discountprice_product';
    protected $ismultientitymanaged = 1;	// 0=No test on entity, 1=Test with field entity, 2=Test with link by societe

    /**
     * Error message
     * @var string
     */
    var $error;
    /**
     * List of error message
     * @var array
     */
    var $errors;

    /**
     * ID of the discound
     * @var int
     */
    var $id;
    /**
     * Entity of the discound
     * @var int
     */
    var $entity;
    /**
     * Begin date of the period
     * @var int
     */
    var $date_begin;
    /**
     * End date of the period
     * @var int
     */
    var $date_end;
    /**
     * ID of the product
     * @var int
     */
    var $fk_product;
    /**
     * Label of the discount
     * @var string
     */
    var $label;
    /**
     * Discount
     * @var double
     */
    var $discount;
    /**
     * Price HT
     * @var double
     */
    var $price;
    /**
     * Price TTC
     * @var double
     */
    var $price_ttc;
    /**
     * Added discount with other categories discount
     * @var boolean
     */
   // var $added;
    /**
     * Priority discount with other categories discount
     * @var boolean
     */
    //var $priority;

    var $fk_user_creat;
    var $fk_user_modif;
    var $date_creation;		// Created date
    var $date_modification;	// Updated date

    /**
	 * Constructor
	 *
	 * @param DoliDb $db Database handler
	 */
	public function __construct(DoliDB $db)
	{
		$this->db = $db;
	}

	/**
	 * Create discount price into database
	 *
	 * @param  User $user      User that creates
	 * @param  bool $notrigger false=launch triggers after, true=disable triggers
	 *
	 * @return int <0 if KO, Id of created object if OK
	 */
	public function create(User $user, $notrigger = false)
	{
	    global $conf,$langs;

        dol_syslog(get_class($this)."::create user=".$user->id);

		$error = 0;

		// Clean parameters
        if (!empty($this->label)) $this->label=trim($this->label);
        if (empty($this->entity)) $this->entity = $conf->entity;

		// Check parameters
        if (empty($user->id) || empty($this->date_begin) || empty($this->date_end) ||
            empty($this->fk_product) || empty($this->label) || (empty($this->discount) && empty($this->price) && empty($this->price_ttc)))
        {
            $this->error=$langs->trans("ErrorBadParameters");
            dol_syslog(get_class($this)."::create Try to create an discount price with an empty parameter (user, date_begin, date_end, fk_product, label, discount && price && price_ttc)", LOG_ERR);
            return -3;
        }
        $result=$this->check_period_for_this_discount($this->date_begin, $this->date_end);
        if ($result == -2) {
            dol_syslog(get_class($this)."::update check_period_for_this_discount: id: {$this->id}, entity: {$this->entity}, product: {$this->fk_product}, period: {$this->date_end}", LOG_ERR);
            return -4;
        } elseif ($result <= 0) {
            dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
            return -5;
        }

        $now=dol_now();

		// Insert request
        $sql = "INSERT INTO ".MAIN_DB_PREFIX.$this->table_element." (";
        $sql.= " entity";
        $sql.= ", date_begin";
        $sql.= ", date_end";
        $sql.= ", fk_product";
        $sql.= ", label";
        $sql.= ", discount";
        $sql.= ", price";
        $sql.= ", price_ttc";
        $sql.= ", datec";
        $sql.= ", fk_user_creat";
        $sql.= ")";
        $sql.= " VALUES (";
        $sql.= " ".$this->entity;
        $sql.= ", '".$this->db->idate($this->date_begin)."'";
        $sql.= ", '".$this->db->idate($this->date_end)."'";
        $sql.= ", ".$this->fk_product;
        $sql.= ", '".$this->db->escape($this->label)."'";
        $sql.= ", ".(!empty($this->discount)?$this->discount:'NULL');
        $sql.= ", ".(!empty($this->price)?$this->price:'NULL');
        $sql.= ", ".(!empty($this->price_ttc)?$this->price_ttc:'NULL');
        $sql.= ", '".$this->db->idate($now)."'";
        $sql.= ", ".$user->id;
        $sql.=")";

		$this->db->begin();

		$resql = $this->db->query($sql);
		if (!$resql) {
			$error ++;
			$this->errors[] = 'Error ' . $this->db->lasterror();
            dol_syslog(get_class($this)."::create ".join(',', $this->errors), LOG_ERR);
		}

		if (!$error) {
			$this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . $this->table_element);

			if (!$notrigger) {
				//// Call triggers
				$result=$this->call_trigger('DISCOUNT_PRICE_CREATE',$user);
				if ($result < 0) $error++;
				//// End call triggers
			}
		}

		// Commit or rollback
		if ($error) {
			$this->db->rollback();

			return - 1 * $error;
		} else {
			$this->db->commit();

			return $this->id;
		}
	}

	/**
	 * Load discount price in memory from the database
	 *
     * @param int    $id        Id object
     * @param int    $entity    Id entity
	 *
	 * @return int <0 if KO, 0 if not found, >0 if OK
	 */
	public function fetch($id, $entity=0)
	{
	    global $conf;
        dol_syslog(get_class($this)."::fetch", LOG_DEBUG);

        if (empty($entity)) $entity = $conf->entity;

        $sql = 'SELECT';
		$sql .= ' t.rowid,';
        $sql .= ' t.date_begin,';
        $sql .= ' t.date_end,';
        $sql .= ' t.fk_product,';
        $sql .= ' t.label,';
        $sql .= ' t.discount,';
        $sql .= ' t.price,';
        $sql .= ' t.price_ttc,';
        $sql .= ' t.datec,';
        $sql .= ' t.tms,';
		$sql .= ' t.fk_user_creat,';
		$sql .= ' t.fk_user_modif';
		$sql .= ' FROM ' . MAIN_DB_PREFIX . $this->table_element . ' as t';
		$sql .= ' WHERE t.entity = '.$entity;
        $sql .= ' AND t.rowid = '.$id;

		$resql = $this->db->query($sql);
		if ($resql) {
			$numrows = $this->db->num_rows($resql);
			if ($numrows) {
				$obj = $this->db->fetch_object($resql);

				$this->id                = $obj->rowid;
                $this->date_begin        = $this->db->jdate($obj->date_begin);
                $this->date_end          = $this->db->jdate($obj->date_end);
                $this->fk_product        = $obj->fk_product;
                $this->label             = $obj->label;
                $this->discount          = $obj->discount;
                $this->price             = $obj->price;
                $this->price_ttc         = $obj->price_ttc;
                $this->date_creation     = $this->db->jdate($obj->datec);
                $this->date_modification = $this->db->jdate($obj->tms);
                $this->fk_user_creat     = $obj->fk_user_creat;
                $this->fk_user_modif     = $obj->fk_user_modif;
			}
			$this->db->free($resql);

			if ($numrows) {
				return 1;
			} else {
				return 0;
			}
		} else {
            $this->errors[] = 'Error ' . $this->db->lasterror();
			dol_syslog(get_class($this) . ' ' . join(',', $this->errors), LOG_ERR);

			return - 1;
		}
	}

	/**
	 * Update discount price into database
	 *
	 * @param  User $user      User that modifies
	 * @param  bool $notrigger false=launch triggers after, true=disable triggers
	 *
	 * @return int <0 if KO, >0 if OK
	 */
	public function update(User $user, $notrigger = false)
	{
	    global $conf,$langs;

		$error = 0;

        dol_syslog(get_class($this)."::update", LOG_DEBUG);

		// Clean parameters
        if (!empty($this->label)) $this->label=trim($this->label);
        if (empty($this->entity)) $this->entity = $conf->entity;

		// Check parameters
        if (empty($user->id) || empty($this->date_begin) || empty($this->date_end) ||
            empty($this->fk_product) || empty($this->label) || (empty($this->discount) && empty($this->price) && empty($this->price_ttc)))
        {
            $this->error=$langs->trans("ErrorBadParameters");
            dol_syslog(get_class($this)."::update Try to update an discount price with an empty parameter (user, date_begin, date_end, fk_product, label, discount && price && price_ttc)", LOG_ERR);
            return -3;
        }
        $result=$this->check_period_for_this_discount($this->date_begin, $this->date_end);
        if ($result == -2) {
            dol_syslog(get_class($this)."::update check_period_for_this_discount: id: {$this->id}, entity: {$this->entity}, product: {$this->fk_product}, period: {$this->date_end}", LOG_ERR);
            return -4;
        } elseif ($result <= 0) {
            dol_syslog(get_class($this)."::update ".$this->error, LOG_ERR);
            return -5;
        }

		// Update request
		$sql = 'UPDATE ' . MAIN_DB_PREFIX . $this->table_element . ' SET';
        $sql .= " date_begin='".$this->db->idate($this->date_begin)."',";
        $sql .= " date_end='".$this->db->idate($this->date_end)."',";
        $sql .= " fk_product=".$this->fk_product.",";
        $sql .= " label='".$this->db->escape($this->label)."',";
        $sql .= " discount=".(!empty($this->discount)?$this->discount:'NULL').",";
        $sql .= " price=".(!empty($this->price)?$this->price:'NULL').",";
        $sql .= " price_ttc=".(!empty($this->price_ttc)?$this->price_ttc:'NULL').",";
		$sql .= " fk_user_modif=".$user->id;
        $sql .= ' WHERE entity = '.$this->entity;
        $sql .= ' AND rowid = '.$this->id;

		$this->db->begin();

		$resql = $this->db->query($sql);
		if (!$resql) {
			$error ++;
			$this->errors[] = 'Error ' . $this->db->lasterror();
            dol_syslog(get_class($this)."::update " . ' ' . join(',', $this->errors), LOG_ERR);
		}

		if (!$error && !$notrigger) {
			//// Call triggers
			$result=$this->call_trigger('DISCOUNT_PRICE_MODIFY',$user);
			if ($result < 0) $error++;
			//// End call triggers
		}

		// Commit or rollback
		if ($error) {
			$this->db->rollback();

			return - 1 * $error;
		} else {
			$this->db->commit();

			return 1;
		}
	}

	/**
	 * Delete discount price in database
	 *
     * @param User $user      User that deletes
	 * @param bool $notrigger false=launch triggers after, true=disable triggers
	 *
	 * @return int <0 if KO, >0 if OK
	 */
	public function delete(User $user, $notrigger = false)
	{
	    global $conf;

		$error = 0;

        dol_syslog(get_class($this)."::delete rowid=".$this->id, LOG_DEBUG);

        if (empty($this->entity)) $this->entity = $conf->entity;

        // Check parameters
        if (empty($user->id))
        {
            $this->error="ErrorBadParameters";
            dol_syslog(get_class($this)."::delete Try to delete an discount price with an empty parameter (user)", LOG_ERR);
            return -3;
        }

        $this->db->begin();

		if (!$error) {
			if (!$notrigger) {
				//// Call triggers
				$result=$this->call_trigger('DISCOUNT_PRICE_DELETE',$user);
				if ($result < 0) $error++;
				//// End call triggers
			}
		}

        if (!$error) {
            $sql = 'DELETE FROM ' . MAIN_DB_PREFIX . $this->table_element;
            $sql .= ' WHERE entity = '.$this->entity;
            $sql .= ' AND rowid = '.$this->id;

            $resql = $this->db->query($sql);
            if (!$resql) {
                $error ++;
                $this->errors[] = 'Error ' . $this->db->lasterror();
                dol_syslog(get_class($this)."::delete " . ' ' . join(',', $this->errors), LOG_ERR);
            }
        }

        // Commit or rollback
		if ($error) {
			$this->db->rollback();

			return - 1 * $error;
		} else {
			$this->db->commit();

			return 1;
		}
	}

    /**
     * Check period for this discount price in database
     *
     * @param  int $date_begin  Begin date of the period
     * @param  int $date_end    End date of the period
     *
     * @return int              <0 if KO, >0 if OK
     */
    public function check_period_for_this_discount($date_begin, $date_end)
    {
        global $conf, $langs;

        dol_syslog(get_class($this)."::check_period_for_this_discount", LOG_DEBUG);

        // Clean parameters
        if (empty($this->entity)) $this->entity = $conf->entity;

        $begin = "'".$this->db->idate($date_begin)."'";
        $end = "'".$this->db->idate($date_end)."'";

        $sql = "SELECT * FROM " . MAIN_DB_PREFIX . $this->table_element;
        $sql .= " WHERE entity = ".$this->entity;
        $sql .= " AND fk_product = $this->fk_product";
        if (!empty($this->id)) $sql .= " AND rowid != ".$this->id;
        $sql .= " AND (($begin <= date_begin AND date_begin <= $end)";
        $sql .= "   OR ($begin <= date_end AND date_end <= $end)";
        $sql .= "   OR (date_begin <= $begin AND $begin <= date_end)";
        $sql .= "   OR (date_begin <= $end AND $end <= date_end))";

		$resql = $this->db->query($sql);
		if ($resql) {
			$numrows = $this->db->num_rows($resql);
			$this->db->free($resql);

			if ($numrows == 0) {
				return 1;
			} else {
				return -2;
			}
		} else {
            $this->errors[] = 'Error ' . $this->db->lasterror();
			dol_syslog(get_class($this) . ' ' . join(',', $this->errors), LOG_ERR);

			return - 1;
		}
    }

    /**
     * Get discount price for the product at this date in database
     *
     * @param  int          $product_id     Product ID
     * @param  int          $entity         Entity ID
     *
     * @return array|null                   Array with discount and discount max or null if no discount
     */
    public function get_discount_price_for_product($product_id, $entity)
    {
        global $conf, $langs;

        dol_syslog(get_class($this) . "::get_discount_price_for_product", LOG_DEBUG);

        $now = dol_now();
        $now_text = "'" . $this->db->escape(dol_print_date($now, 'dayrfc')) . "'";

        $sql = "SELECT t.price, t.price_ttc, t.discount,";
        $sql .= " p.price as product_price, p.price_ttc as product_price_ttc,";
        $sql .= " p.price_min as product_price_min";
        $sql .= " FROM " . MAIN_DB_PREFIX . $this->table_element . " AS t";
        $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "product AS p ON p.rowid = t.fk_product";
        $sql .= " WHERE t.entity = $entity";
        $sql .= " AND t.fk_product = $product_id";
        $sql .= " AND t.date_begin <= $now_text AND $now_text <= t.date_end";

        $resql = $this->db->query($sql);
        if ($resql) {
            $discount = 0;
            $discount_max = 0;

            if ($obj = $this->db->fetch_object($resql)) {
                if (!empty($obj->price)) {
                    $discount = 100 - ($obj->price * 100 / $obj->product_price);
                } elseif (!empty($obj->price_ttc)) {
                    $discount = 100 - ($obj->price_ttc * 100 / $obj->product_price_ttc);
                } else {
                    $discount = $obj->discount;
                }

                $discount_max = 100 - ($obj->product_price_min * 100 / $obj->product_price);
            }
            $this->db->free($resql);

            return [ 'discount' => $discount, 'discount_max' => $discount_max ];
        } else {
            $this->errors[] = 'Error ' . $this->db->lasterror();
            dol_syslog(get_class($this) . ' ' . join(',', $this->errors), LOG_ERR);
        }

        return null;
    }
}

Hry