<?php
/**
* Product: Katyshop
* @version 0.3.2.1
* @author Catalin Hulea - hide@address.com
* @copyright Copyright (C) 2007 Catalin Hulea
* @license GNU General Public License version 3
* You can find a copy of GNU GPL v3 at this path: /docs/LICENSE
* @link https://sourceforge.net/projects/katyshop
*
* 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/>.
*
*/
class Order extends LogicObject
{
var $id = 0;
var $code = "";
var $title = "Comanda noua";
var $id_user = 0;
var $status = "in cos";
var $date_ordered = "";
var $date_updated = "";
// cache everything about the user and addresses.
// In case a user or address record gets deleted or modified,
// we keep the record here about the original data, when the order was sent.
var $username = "";
var $user_short_description = "";
var $user_details = "";
var $id_delivery_address = 0;
var $id_invoice_address = 0;
var $delivery_address = "";
var $invoice_address = "";
var $value_without_vat = 0;
var $value_vat = 0;
var $value_total = 0;
var $orderProducts = array();
function Order()
{
parent::LogicObject();
$this->date_ordered = date("Y-m-d H:i:s");
$this->date_updated = date("Y-m-d H:i:s");
}
function getInstanceProps($myClass = "")
{
if(empty($myClass))
$myClass = __CLASS__;
return parent::getInstanceProps($myClass);
}
function validate()
{
$errors = array();
if(!Tools::validateInt($this->id) || $this->id < 0)
$errors[] = "Proprietatea ID a comenzii trebuie sa fie un intreg pozitiv";
if(strlen($this->title) < 3 || strlen($this->title) > 255)
$errors[] = "Titlul comenzii trebuie sa aiba intre 3 si 255 caractere";
if(!Tools::validateInt($this->id_user, true, true))
$errors[] = "Proprietatea id_user a comenzii trebuie sa fie intreg pozitiv";
if(!in_array($this->status, Order::getPossibleStatuses()))
$errors[] = "Statusul comenzii nu este valid";
$df = new DateFormat();
$df->readDateTime($this->date_ordered);
if(!$df->validate())
$errors[] = "Data comenzii nu este valida";
$df->readDateTime($this->date_updated);
if(!$df->validate())
$errors[] = "Data actualizarii comenzii nu este valida";
if(!Tools::validateFloat($this->value_without_vat) || $this->value_without_vat < 0)
$errors[] = "Valoarea comenzii trebuie sa fie pozitiva";
if(!Tools::validateFloat($this->value_vat) || $this->value_vat < 0)
$errors[] = "Valoarea TVA trebuie sa fie pozitiva";
if(!Tools::validateFloat($this->value_total) || $this->value_total < 0)
$errors[] = "Valoarea totala a comenzii trebuie sa fie pozitiva";
if(count($this->orderProducts) < 1)
$errors[] = "Comanda este goala";
for($i = 0; $i < count($this->orderProducts); $i++)
{
if(!is_a($this->orderProducts[$i], "OrderProduct"))
$errors[] = "O linie din comanda nu a putut fi identificata ca fiind un produs";
}
$productsHaveErrors = false;
if(count($errors) == 0)
{
// db validations
$db = Application::getDb();
$u = $db->tbUser->getUserById($this->id_user);
$u = Factory::instantiateUser($u);
if(!is_a($u, "UserPerson") && !is_a($u, "UserCompany"))
$errors[] = "Numai persoanele fizice si companiile pot trimite comenzi";
for($i = 0; $i < count($this->orderProducts); $i++)
{
if(!$this->orderProducts[$i]->validate())
$productsHaveErrors = true;
}
}
Application::appendErrors($errors);
return (count($errors) == 0 || $productsHaveErrors);
}
function validateDeliveryAddress()
{
$errors = array();
if(strlen($this->delivery_address) < 10)
$errors[] = "Adresa de livrare trebuie sa aiba cel putin 10 caractere";
if(count($errors) == 0)
{
$db = Application::getDb();
$a = $db->tbAddress->getRecordById($this->id_delivery_address);
if($a->id_user != $this->id_user)
$errors[] = "Adresa de livrare selectata nu apartine userului care a facut comanda";
}
Application::appendErrors($errors);
return (count($errors) == 0);
}
function validateInvoiceAddress()
{
$errors = array();
if(strlen($this->invoice_address) < 10)
$errors[] = "Adresa de facturare trebuie sa aiba cel putin 10 caractere";
if(count($errors) == 0)
{
$db = Application::getDb();
$a = $db->tbAddress->getRecordById($this->id_invoice_address);
if($a->id_user != $this->id_user)
$errors[] = "Adresa de facturare selectata nu apartine utilizatorului care a facut comanda";
}
Application::appendErrors($errors);
return (count($errors) == 0);
}
function getPossibleStatuses()
{
return array("in cos", "trimisa", "in lucru", "respinsa", "livrata", "platita");
}
function generateCode()
{
$db = Application::getDb();
$u = $db->tbUser->getUserById($this->id_user);
if($u->id == 0)
Logger::err("User not found in Order::generateCode()", __FILE__, __LINE__);
$usr = strtolower($u->username);
$s = "O" . str_pad($this->id, 5, "0", STR_PAD_LEFT);
$s .= "U" . str_pad($this->id_user, 5, "0", STR_PAD_LEFT);
if(Tools::validateAlphanumeric($usr))
$s .= substr($usr, 0, 3);
else
Logger::err("Username not alphanumeric in Order::generateCode() ", __FILE__, __LINE__);
$this->code = $s;
}
function generateProforma()
{
SessionHandler::set("generateProforma", $this);
ob_get_clean();
ob_start();
require_once(WEB_DIR . "/includes/generate_proforma.php");
$s = ob_get_clean();
if(file_exists(DATA_DIR . "/orders/{$this->code}.html"))
{
Application::addError("Nu am putut scrie fisierul cu proforma pentru {$this->code} deoarece deja exista un alt fisier cu acelasi nume");
Logger::err("Could not write the proforma file for {$this->code} because another file with the same name already exists", __FILE__, __LINE__);
}
else
{
$f = fopen(DATA_DIR . "/orders/{$this->code}.html", "w");
if(fwrite($f, $s) === false)
{
Application::addError("A fost o eroare la scrierea fisierului {$this->code}.html");
Logger::err("There was an error while trying to write file {$this->code}.html", __FILE__, __LINE__);
}
fclose($f);
}
ob_start();
}
//#######################################
//# ORDER PRODUCT RELATED METHODS #
//#######################################
/**
* @param OrderProduct $orderProduct
*/
function addOrderProduct($orderProduct)
{
$alreadyExists = false;
for($i = 0; $i < count($this->orderProducts); $i++)
{
if($orderProduct->id_product == $this->orderProducts[$i]->id_product)
{
$alreadyExists = true;
$q = $this->orderProducts[$i]->quantity;
$this->orderProducts[$i]->copyFromObject($orderProduct);
$this->orderProducts[$i]->quantity += $q;
$this->orderProducts[$i]->line_number = $i + 1;
}
}
if(!$alreadyExists)
{
$op = new OrderProduct();
$op->copyFromObject($orderProduct);
$op->line_number = count($this->orderProducts) + 1;
$this->orderProducts[] = $op;
}
}
/**
* @return OrderProduct
*/
function getOrderProduct($line_number)
{
$ret = new OrderProduct();
if($line_number <= count($this->orderProducts) && $line_number > 0)
$ret->copyFromObject($this->orderProducts[$line_number - 1]);
return $ret;
}
function removeOrderProduct($line_number)
{
if($line_number <= count($this->orderProducts) && $line_number > 0)
{
$x = count($this->orderProducts);
for($i = $line_number - 1; $i < $x - 1; $i++)
{
$this->orderProducts[$i]->copyFromObject($this->orderProducts[$i + 1]);
$this->orderProducts[$i]->line_number = $i + 1;
}
unset($this->orderProducts[$x - 1]);
}
}
function resetOrderProducts()
{
$this->orderProducts = array();
}
function getProductsCount()
{
return count($this->orderProducts);
}
function setProductsIds()
{
for($i = 0; $i < count($this->orderProducts); $i++)
$this->orderProducts[$i]->id_order = $this->id;
}
function computeValue()
{
$this->value_without_vat = 0;
$this->value_vat = 0;
$this->value_total = 0;
for($i = 0; $i < count($this->orderProducts); $i++)
{
$this->orderProducts[$i]->computeValue();
$this->value_without_vat += $this->orderProducts[$i]->value_without_vat;
$this->value_vat += $this->orderProducts[$i]->value_vat;
$this->value_total += $this->orderProducts[$i]->value_total;
}
}
}
?>