Location: PHPKode > projects > Katyshop > katyshop_ro_0.3.2.1/web/formparser/order.php
<?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/>.
 *
 */

require_once(dirname(dirname(__FILE__)) . "/init.php");

$db = Application::getDb();
if(@$_GET["action"] == "add_to_cart")
{
	$p = $db->tbProduct->getRecordById(@$_POST["id_product"]);
	if($p->id == 0)
	{
		Application::addError("Nu a putut fi gasit produsul care trebuie adaugat in cos");
	}
	else
	{
		$basket = Application::getShoppingCart();
		$op = new OrderProduct();
		$op->initFromProduct($p);
		$op->quantity = 1;
		$basket->addOrderProduct($op);
		Application::setShoppingCart($basket);
	}
	Tools::redirect("../shopping_cart.php");
}
elseif (@$_GET["action"] == "remove_product")
{
	$basket = Application::getShoppingCart();
	$basket->removeOrderProduct(@$_POST["line_number"]);
	Application::setShoppingCart($basket);
	Tools::redirect("../shopping_cart.php");
}
elseif (@$_GET["action"] == "update_basket")
{
	$basket = Application::getShoppingCart();
	if(intval(@$_POST["next_step"]) != 0)
	{
		$user = Application::getUser();
		if(!$user->isUserLoggedIn())
		{
			SessionHandler::set("login_return_to_cart", "true");
			Application::addError("Trebuie sa fiti autentificat pentru a trimite comanda");
			Tools::redirect("../login.php");
		}
		if($user->isAdminLoggedIn())
		{
			Application::addError("Conturile de tip admin nu pot trimite comenzi");
			Tools::redirect("../shopping_cart.php");
		}
		$basket->id_user = $user->id;
	}

	$basket->title = @$_POST["title"];
	$basket->resetOrderProducts();
	for($i = 0; $i < count(@$_POST["line_number"]); $i++)
	{
		$op = new OrderProduct();
		$p = $db->tbProduct->getRecordById(@$_POST["id_product"][$i]);
		if($p->id == 0)
		{
			Application::addError("Nu a putut fi gasit unul din produse pentru a fi adaugat in cos");
		}
		else
		{
			$op->initFromProduct($p);
			$op->quantity = readPrice(@$_POST["quantity"][$i]);
			$basket->addOrderProduct($op);
		}
	}
	if(intval(@$_POST["next_step"]) == 1)
		$basket->validate();
	Application::setShoppingCart($basket);
	if(intval(@$_POST["next_step"]) == 1 && !Application::hasErrors())
		Tools::redirect("../shopping_cart_address.php?action=select_delivery_address");
	else
		Tools::redirect("../shopping_cart.php");
}
elseif (@$_GET["action"] == "select_delivery_address")
{
	$user = Application::getUser();
	if(!$user->isUserLoggedIn())
	{
		SessionHandler::set("login_return_to_cart", "true");
		Application::addError("Trebuie sa fiti autentificat pentru a putea trimite comanda");
		Tools::redirect("../login.php");
	}
	if($user->isAdminLoggedIn())
	{
		Application::addError("Conturile de tip admin nu pot trimite comenzi");
		Tools::redirect("../shopping_cart.php");
	}
	$a = $db->tbAddress->getRecordById(@$_POST["id_address"]);
	if($a->id == 0)
		Application::addError("Nu a putut fi gasita adresa de livrare selectata de dumneavoastra");
	elseif($a->id_user != $user->id)
		Application::addError("Adresa selectata nu va apartine");
	elseif(intval($a->delivery) != 1)
		Application::addError("Adresa selectata nu este adresa de livrare");

	if(Application::hasErrors())
	{
		Tools::redirect("../shopping_cart_address.php?action=select_delivery_address");
	}
	else
	{
		$basket = Application::getShoppingCart();
		$basket->id_delivery_address = $a->id;
		$basket->delivery_address = $a->toStr(true);
		Application::setShoppingCart($basket);
		Tools::redirect("../shopping_cart_address.php?action=select_invoice_address");
	}
}
elseif (@$_GET["action"] == "select_invoice_address")
{
	$user = Application::getUser();
	if(!$user->isUserLoggedIn())
	{
		SessionHandler::set("login_return_to_cart", "true");
		Application::addError("Trebuie sa fiti autentificat pentru a putea trimite comanda");
		Tools::redirect("../login.php");
	}
	if($user->isAdminLoggedIn())
	{
		Application::addError("Conturile de tip admin nu pot trimite comenzi");
		Tools::redirect("../shopping_cart.php");
	}
	$a = $db->tbAddress->getRecordById(@$_POST["id_address"]);

	if($a->id == 0)
		Application::addError("Nu a putut fi selectata adresa de facturare selectata de dumneavoastra");
	elseif($a->id_user != $user->id)
		Application::addError("Adresa selectata  nu va apartine");
	elseif(intval($a->invoiceing) != 1)
		Application::addError("Adresa selectata nu este adresa de facturare");

	if(Application::hasErrors())
	{
		Tools::redirect("../shopping_cart_address.php?action=select_invoice_address");
	}
	else
	{
		$basket = Application::getShoppingCart();
		$basket->id_invoice_address = $a->id;
		$basket->invoice_address = $a->toStr(true);
		Application::setShoppingCart($basket);
		Tools::redirect("../order.php");
	}
}
elseif (@$_GET["action"] == "finalize")
{
	$user = Application::getUser();
	if(!$user->isUserLoggedIn())
	{
		SessionHandler::set("login_return_to_cart", "true");
		Application::addError("Trebuie sa fiti autentificat pentru a putea trimite comanda");
		Tools::redirect("../login.php");
	}
	if($user->isAdminLoggedIn())
	{
		Application::addError("Conturile de tip admin nu pot trimite comenzi");
		Tools::redirect("../shopping_cart.php");
	}
	$basket = Application::getShoppingCart();
	$basket->id_user = $user->id;
	$basket->username = $user->username;
	$basket->user_short_description = $user->toStr(true, true);
	$basket->user_details = $user->toStr(true);

	if(!$basket->validate())
		Tools::redirect("../shopping_cart.php");
	if(!$basket->validateDeliveryAddress())
		Tools::redirect("../shopping_cart.php?action=select_delivery_address");
	if(!$basket->validateInvoiceAddress())
		Tools::redirect("../shopping_cart.php?action=select_invoice_address");

	$x = $db->tbOrder->insertObj($basket);
	$ma = Application::getMailAgent();
	$ma->newOrder($user, $basket);
	$basket = new Order();
	Application::setShoppingCart($basket);
	Application::addMessage("Comanda a fost trimisa");
	Tools::redirect("../order.php?id=$x");
}




?>
Return current item: Katyshop