<?php

class OrderController extends Zend_Controller_Action {

	public function init() {			
	}
	
	public function basketupdateAction() {
		$order = new Zend_Session_Namespace('order');
		$basket = new Zend_Session_Namespace('basket');
		$client = new Zend_Session_Namespace('client');	
		$information = new Zend_Session_Namespace('information');	
		$v = $this->_request->getParams();
		
		$basket->basket_invoice = $v['dane']['basket_invoice'];	
		$basket->basket_invoice_name = $v['dane']['basket_invoice_name'];	
		$basket->basket_invoice_address = $v['dane']['basket_invoice_address'];	
		$basket->basket_invoice_zipcode = $v['dane']['basket_invoice_zipcode'];	
		$basket->basket_invoice_city = $v['dane']['basket_invoice_city'];	
		$basket->basket_invoice_country = $v['dane']['basket_invoice_country'];	
		$basket->basket_invoice_nip = $v['dane']['basket_invoice_nip'];	
		$basket->basket_invoice_nip2 = $v['dane']['basket_invoice_nip2'];	
		$basket->basket_clientinfo = $v['dane']['basket_clientinfo'];	
		$basket->basket_pay = $v['dane']['basket_pay'];	
		$basket->newsletter = $v['dane']['newsletter'];	
		
		$this->_helper->viewRenderer->setNoRender();
	}	
	
	public function sendAction() {
	
		Lib_Basket::update();
	
		$order = new Zend_Session_Namespace('order');
		$basket = new Zend_Session_Namespace('basket');
		$client = new Zend_Session_Namespace('client');	
		$information = new Zend_Session_Namespace('information');	
		$v = $this->_request->getParams();	
		
		$TypePack = new TypePack();
		$TypeTransport = new TypeTransport();
		$TypeLeadTime = new TypeLeadTime();	
		$Config = new Config();
		$Voucher = new Voucher();
		
		
		$vv = $this->_request->getPost();
		if( isset($vv['methodType']) ){	
			$basket->packid = $vv['pack'];
			$basket->transportid = $vv['transport'];
			$basket->leadtimeid = $vv['leadtime'];
			
			$this->_helper->redirector('send','order');			
		}
		
		
		if( isset($vv['voucher_code']) ){	
		
			$code = explode("-",$vv['voucher_code']);
			
			if( isset($code[0]) AND isset($code[1]) ){
				$select = $Voucher->select();		
				$select->where('voucher_id = ?', $code[0]);		
				$select->where('voucher_code = ?', $code[1]);	
				$select->where('voucher_active = ?', 0);					
				$row = $Voucher->fetchRow($select);
				if($row){
					$basket->voucher_id = $row['voucher_id'];
					$basket->voucher_code = $row['voucher_code'];		
				}
			}
			$this->_helper->redirector('send','order');			
		}
		
		
		
		if( !isset($basket->packid) ){
			$select = $TypePack->select();		
			$select->where('def = ?', 1);					
			$row = $TypePack->fetchRow($select);
			$basket->packid = $row['id'];	
		}
		
		if( !isset($basket->transportid) ){
			$select = $TypeTransport->select();		
			$select->where('def = ?', 1);					
			$row = $TypeTransport->fetchRow($select);
			$basket->transportid = $row['id'];	
		}
		
		if( !isset($basket->leadtimeid) ){
			$select = $TypeLeadTime->select();		
			$select->where('def = ?', 1);					
			$row = $TypeLeadTime->fetchRow($select);
			$basket->leadtimeid = $row['id'];	
		}
		
		if( !($basket->basket_pay) ){
			$basket->basket_pay = 1;	
		}
		
		if( !($basket->basket_invoice) ){
			$basket->basket_invoice = 1;	
		}
		
		if( isset($v['idDel']) ){		
			unset($order->product[$v['idDel']]);
			$this->_helper->redirector('send','order');		
		}

		
		
		$select = $TypePack->select();		
		$select->where('id = ?', $basket->packid);					
		$row = $TypePack->fetchRow($select);
		$basket->pack = $row;	
		
		$select = $TypeTransport->select();
		$select->where('id = ?', $basket->transportid);					
		$row = $TypeTransport->fetchRow($select);
		$basket->transport = $row;
		
		$select = $TypeLeadTime->select();
		$select->where('id = ?', $basket->leadtimeid);					
		$row = $TypeLeadTime->fetchRow($select);
		$basket->leadtime = $row;		
			
		
		
		$select = $Config->select();
		$select->where('config_id = ?', 1);					
		$config_time = $Config->fetchRow($select);
		$basket->config_time = $config_time;		
		
		$this->view->pack = $basket->pack;
		$this->view->transport = $basket->transport;
		$this->view->leadtime = $basket->leadtime;
		$this->view->config_time = $basket->config_time;
		
		
		
		if( $client->client_auth != true ){
			$this->view->noLog = true;
		}else if( count($order->product) == 0 ){
			$this->view->noProduct = true;		
		}else{
		
		
		
		
			$Client = new Client();	
			$select = $Client->select();		
			$select->where('client_id = ?', $client->client_id);					
			$rowClient = $Client->fetchRow($select);
			
			if( !($basket->basket_invoice_name) ){
				if( $rowClient->client_type == 1 ) $basket->basket_invoice_name = $rowClient->client_company;	
				else $basket->basket_invoice_name = $rowClient->client_firstname.' '.$rowClient->client_secondname;	
			}
			
			if( !($basket->basket_invoice_address) ){
				$basket->basket_invoice_address = $rowClient->client_address;	
			}
			
			if( !($basket->basket_invoice_zipcode) ){
				$basket->basket_invoice_zipcode = $rowClient->client_zipcode;	
			}
			
			if( !($basket->basket_invoice_city) ){
				$basket->basket_invoice_city = $rowClient->client_city;	
			}
			
			if( !($basket->basket_invoice_country) ){
				$basket->basket_invoice_country = $rowClient->client_country;	
			}
			
			if( !($basket->basket_invoice_nip) ){
				$basket->basket_invoice_nip = $rowClient->client_nip;	
			}
			
			$this->view->basket_invoice = $basket->basket_invoice;
			$this->view->basket_invoice_name = $basket->basket_invoice_name;
			$this->view->basket_invoice_address = $basket->basket_invoice_address;
			$this->view->basket_invoice_zipcode = $basket->basket_invoice_zipcode;
			$this->view->basket_invoice_city = $basket->basket_invoice_city;
			$this->view->basket_invoice_country = $basket->basket_invoice_country;
			$this->view->basket_invoice_nip = $basket->basket_invoice_nip;
			$this->view->basket_invoice_nip2 = $basket->basket_invoice_nip2;
			
			$this->view->basket_clientinfo = $basket->basket_clientinfo;
			$this->view->basket_pay = $basket->basket_pay;
			
			$this->view->orderData = $order->product;
			$Client = new Client();	
			$select = $Client->select();		
			$select->where('client_id = ?', $client->client_id);					
			$row = $Client->fetchRow($select);
			$this->view->rowClient = $row;	
			
			
			$rab = 0;
			if( $basket->voucher_id AND $basket->voucher_code ){
				$select = $Voucher->select();		
				$select->where('voucher_id = ?', $basket->voucher_id);		
				$select->where('voucher_code = ?', $basket->voucher_code);	
				$select->where('voucher_active = ?', 0);					
				$row = $Voucher->fetchRow($select);
				if($row){
					$this->view->voucher_id = $row['voucher_id'];
					$this->view->voucher_code = $row['voucher_code'];
					$this->view->voucher_rabate = $row['voucher_rabate'];	
					$rab = $row['voucher_rabate']/100;
				}			
			}
			
			
			$sum = 0;
			foreach( $order->product as $key => $val ){
				$sum = $sum + $val['amount'] * $val['price'];
			}
			$this->view->sum = $sum;//-$sum*$rab;  			
			
			
			$rpd = 0;
			if( $basket->pack->type == 0 ){
				$rpd = $basket->pack->value;
			}else{
				$rpd = $sum*($basket->pack->value)/100;			
			}
			
			$rtd = 0;
			if( $basket->transport->type == 0 ){
				$rtd = $basket->transport->value;
			}else{
				$rtd = $sum*($basket->transport->value)/100;			
			}
			
			$rld = 0;
			if( $basket->leadtime->type == 0 ){
				$rld = $basket->leadtime->value;
			}else{
				$rld = $sum*($basket->leadtime->value)/100;			
			}
			
			
			
			// obliczanie kosztu transportu
			$sum_pack_m = 0;
			$sum_pack_d = 0;
			foreach( $order->product as $key => $val ){
				$Product = new Product();
				$select = $Product->select();		
				$select->where('product_id = ?', $val['idProduct']);					
				$rowP = $Product->fetchRow($select);
				$sum_pack_m = $sum_pack_m + $val['amount'] * $rowP['product_pack1'];
				$sum_pack_d = $sum_pack_d + $val['amount'] * $rowP['product_pack2'];
			}		
			
		//	echo$sum_pack_m.' '.$sum_pack_d;
			if( $basket->transportid == 2 ){
			
				if( $sum_pack_m <= 30 AND $sum_pack_d == 0 ){ $rtd = 25; }			
				else if( $sum_pack_m > 30 AND $sum_pack_m <= 60 AND $sum_pack_d == 0 ){ $rtd = 45; }
				else if( $sum_pack_m > 60 AND $sum_pack_m <= 500 AND $sum_pack_d == 0 ){ $rtd = 139; }
				else if( $sum_pack_m > 500 AND $sum_pack_m <= 800 AND $sum_pack_d == 0 ){ $rtd = 169; }
				else if( $sum_pack_m > 800 AND $sum_pack_m <= 1200 AND $sum_pack_d == 0 ){ $rtd = 228; }
				else if( $sum_pack_m > 1200 AND $sum_pack_m <= 1800 AND $sum_pack_d == 0 ){ $rtd = 350; }
				else if( $sum_pack_m > 1800 AND $sum_pack_d == 0 ){ $rtd = 0; }
				else if( $sum_pack_d > 0 AND ($sum_pack_m+$sum_pack_d) <= 30 ){ $rtd = 50; }
				else if( $sum_pack_d > 0 AND ($sum_pack_m+$sum_pack_d) > 30 AND ($sum_pack_m+$sum_pack_d) <= 60 ){ $rtd = 70; }
				else if( $sum_pack_d > 0 AND ($sum_pack_m+$sum_pack_d) > 60 AND ($sum_pack_m+$sum_pack_d) <= 500 ){ $rtd = 158; }
				else if( $sum_pack_d > 0 AND ($sum_pack_m+$sum_pack_d) > 500 AND ($sum_pack_m+$sum_pack_d) <= 800 ){ $rtd = 179; }
				else if( $sum_pack_d > 0 AND ($sum_pack_m+$sum_pack_d) > 800 AND ($sum_pack_m+$sum_pack_d) <= 1800 ){ $rtd = 240; }
				else if( $sum_pack_d > 0 AND ($sum_pack_m+$sum_pack_d) > 1800 ){ $rtd = 0; }
			
				
	//			if( $basket->transportid == 2 ){
			
	//			if( $sum_pack_m <= 30 AND $sum_pack_d == 0 ){ $rtd = 0; }			
	//			else if( $sum_pack_m > 30 AND $sum_pack_m <= 60 AND $sum_pack_d == 0 ){ $rtd = 0; }
	//			else if( $sum_pack_m > 60 AND $sum_pack_m <= 500 AND $sum_pack_d == 0 ){ $rtd = 0; }
	//			else if( $sum_pack_m > 500 AND $sum_pack_m <= 800 AND $sum_pack_d == 0 ){ $rtd = 0; }
	//			else if( $sum_pack_m > 800 AND $sum_pack_m <= 1200 AND $sum_pack_d == 0 ){ $rtd = 0; }
	//			else if( $sum_pack_m > 1200 AND $sum_pack_m <= 1800 AND $sum_pack_d == 0 ){ $rtd = 0; }
	//			else if( $sum_pack_m > 1800 AND $sum_pack_d == 0 ){ $rtd = 0; }
	//			else if( $sum_pack_d > 0 AND ($sum_pack_m+$sum_pack_d) <= 30 ){ $rtd = 0; }
	//			else if( $sum_pack_d > 0 AND ($sum_pack_m+$sum_pack_d) > 30 AND ($sum_pack_m+$sum_pack_d) <= 60 ){ $rtd = 0; }
	//			else if( $sum_pack_d > 0 AND ($sum_pack_m+$sum_pack_d) > 60 AND ($sum_pack_m+$sum_pack_d) <= 500 ){ $rtd = 0; }
	//			else if( $sum_pack_d > 0 AND ($sum_pack_m+$sum_pack_d) > 500 AND ($sum_pack_m+$sum_pack_d) <= 800 ){ $rtd = 0; }
	//			else if( $sum_pack_d > 0 AND ($sum_pack_m+$sum_pack_d) > 800 AND ($sum_pack_m+$sum_pack_d) <= 1800 ){ $rtd = 0; }
	//			else if( $sum_pack_d > 0 AND ($sum_pack_m+$sum_pack_d) > 1800 ){ $rtd = 0; }
				
				
				
			}
			
			$sum = round($sum,2);
			$rpd = round($rpd,2);
			$rtd = round($rtd,2);
			$rld = round($rld,2);
			
			$sumC = $sum*(1-$rab) + $rpd + $rtd + $rld;
			$this->view->sumC = $sumC; 
			$this->view->rpd = $rpd; 
			$this->view->rtd = $rtd; 
			$this->view->rld = $rld; 
		
			$basket->sumC = $sumC; 
			$basket->rpd = $rpd; 
			$basket->rtd = $rtd; 
			$basket->rld = $rld; 
			$basket->sum = $sum; 
		
		
			$dataTypePack = $TypePack->fetchAll(null, 'sort ASC');
			$this->view->dataTypePack = $dataTypePack;  
			
			$dataTypeTransport = $TypeTransport->fetchAll(null, 'sort ASC');
			$this->view->dataTypeTransport = $dataTypeTransport;  
		
			$dataTypeLeadTime = $TypeLeadTime->fetchAll(null, 'sort ASC');
			$this->view->dataTypeLeadTime = $dataTypeLeadTime;
			
			
			$leadTimeTable = array();
			// czas realizacji zamowienia
			foreach( $order->product as $key => $val ){			
				$leadTimeTable[$val['idProduct']] = $config_time['config_value']+Lib_Product::leadtime($val['idProduct'],$val['product_front_id'],$val['product_body_id']);					
			}				
				
			$this->view->leadTimeTable = $leadTimeTable;
			
		
		}
		
		$id = 4;		
		$Content = new Content();
		$select = $Content->select();		
		$select->where('content_id = ?', $id);	
		$row = $Content->fetchRow($select);			
		$this->view->rowContent = $row;
		
		$id = 39;		
		$Content = new Content();
		$select = $Content->select();		
		$select->where('content_id = ?', $id);	
		$row = $Content->fetchRow($select);			
		$this->view->rowContent2 = $row;
		
		$id = 14;		
		$Content = new Content();
		$select = $Content->select();		
		$select->where('content_id = ?', $id);	
		$row = $Content->fetchRow($select);			
		$this->view->rowContent3 = $row;
		
		$id = 38;		
		$Content = new Content();
		$select = $Content->select();		
		$select->where('content_id = ?', $id);	
		$row = $Content->fetchRow($select);			
		$this->view->rowContentCrz = $row;
		
		$id = 42;		
		$Content = new Content();
		$select = $Content->select();		
		$select->where('content_id = ?', $id);	
		$row = $Content->fetchRow($select);			
		$this->view->rowContentP1 = $row;
		
		$id = 43;		
		$Content = new Content();
		$select = $Content->select();		
		$select->where('content_id = ?', $id);	
		$row = $Content->fetchRow($select);			
		$this->view->rowContentP2 = $row;
		
		$id = 54;		
		$Content = new Content();
		$select = $Content->select();		
		$select->where('content_id = ?', $id);	
		$row = $Content->fetchRow($select);			
		$this->view->rowContentRODO = $row;
		
		
	}
	
	public function plusAction() {
		$this->_helper->viewRenderer->setNoRender();
		$order = new Zend_Session_Namespace('order');
		$basket = new Zend_Session_Namespace('basket');
		$client = new Zend_Session_Namespace('client');	
		$information = new Zend_Session_Namespace('information');	
		$v = $this->_request->getParams();	
		
		if( $order->product[$v['id']]['amount'] < 10 ) $order->product[$v['id']]['amount'] = $order->product[$v['id']]['amount'] + 1;
		
		$this->_helper->redirector('send','order');
	}
	
	public function minusAction() {
		$this->_helper->viewRenderer->setNoRender();
		$order = new Zend_Session_Namespace('order');
		$basket = new Zend_Session_Namespace('basket');
		$client = new Zend_Session_Namespace('client');	
		$information = new Zend_Session_Namespace('information');	
		$v = $this->_request->getParams();	
		
		if( $order->product[$v['id']]['amount'] > 1 ) $order->product[$v['id']]['amount'] = $order->product[$v['id']]['amount'] - 1;
		
		$this->_helper->redirector('send','order');
	}
	
	public function ordereditproductAction() {	
		$db = Zend_Registry::get('db');
		$v = $this->_request->getParams();
		$order = new Zend_Session_Namespace('order');
		$basket = new Zend_Session_Namespace('basket');
		$client = new Zend_Session_Namespace('client');	
		$information = new Zend_Session_Namespace('information');	
				
		$Product = new Product();
		$select = $Product->select();		
		$select->where('product_id = ?', $order->product[$v['id']]['idProduct']);	
		$rowProduct = $Product->fetchRow($select);

		$data = array(
			'idProduct' => $order->product[$v['id']]['idProduct'],
			'id' => $v['id'],
		
			'order_product_height' => $order->product[$v['id']]['product_height'],
			'order_product_width' => $order->product[$v['id']]['product_width'],
			'order_product_depth' => $order->product[$v['id']]['product_depth'],
		
			'product_shelf' => $rowProduct->product_shelf,
			'order_product_shelfamount' => $order->product[$v['id']]['product_shelfamount'],
		
			'product_wysszerA' => $rowProduct->product_wysszerA,
			'order_product_wysszerA' => $order->product[$v['id']]['product_wysszerAamount'],
		
			'product_wysszerB' => $rowProduct->product_wysszerB,
			'order_product_wysszerB' => $order->product[$v['id']]['product_wysszerBamount'],
		
			'product_wysszerC' => $rowProduct->product_wysszerC,
			'order_product_wysszerC' => $order->product[$v['id']]['product_wysszerCamount'],
		
			'product_wysszerD' => $rowProduct->product_wysszerD,
			'order_product_wysszerD' => $order->product[$v['id']]['product_wysszerDamount'],
		
			'product_wysszerE' => $rowProduct->product_wysszerE,
			'order_product_wysszerE' => $order->product[$v['id']]['product_wysszerEamount'],
		
			'product_body' => $rowProduct->product_body,
			'product_body_id' => $order->product[$v['id']]['product_body_id'],
			'product_rim_id' => $order->product[$v['id']]['product_rim_id'],
			'product_body2_id' => $order->product[$v['id']]['product_body2_id'],
		
			'product_front' => $rowProduct->product_front,
			'product_front_id' => $order->product[$v['id']]['product_front_id'],
			'product_rim2_id' => $order->product[$v['id']]['product_rim2_id'],
		
			'product_type' => $rowProduct->product_type,
			'product_type_id' => $order->product[$v['id']]['product_type_id'],
		
			'product_accessories' => $rowProduct->product_accessories,
			'product_accessories_id' => $order->product[$v['id']]['product_accessories_id'],
			'product_accessories2_id' => $order->product[$v['id']]['product_accessories_id2'],
			'product_accessories3_id' => $order->product[$v['id']]['product_accessories_id3'],
			'product_accessories4_id' => $order->product[$v['id']]['product_accessories_id4'],
		
			'product_legs' => $rowProduct->product_legs,
			'product_legs_id' => $order->product[$v['id']]['product_legs_id'],
		
			'product_handles' => $rowProduct->product_handles,
			'product_handles_id' => $order->product[$v['id']]['product_handles_id'],
		
			'product_options' => $rowProduct->product_options,
			'product_options_1' => $order->product[$v['id']]['product_options_1'],
			'product_options_2' => $order->product[$v['id']]['product_options_2'],
			'product_options_3' => $order->product[$v['id']]['product_options_3'],
			'product_options_4' => $order->product[$v['id']]['product_options_4'],
			'product_options_id' => $order->product[$v['id']]['product_options_id'],
			'product_options_id2' => $order->product[$v['id']]['product_options_id2'],
		
		);
		
		$this->view->product = $data;	
		$this->view->productRow = $rowProduct;		
	}
	
	public function finishAction() {
		$order = new Zend_Session_Namespace('order');
		$basket = new Zend_Session_Namespace('basket');
		$client = new Zend_Session_Namespace('client');	
		$information = new Zend_Session_Namespace('information');	
		$v = $this->_request->getParams();
		 
		
		$TypePack = new TypePack();
		$TypeTransport = new TypeTransport();	
	
		
		if( $client->client_auth != true ){
			$this->_helper->redirector('send','order');
		}else if( count($order->product) == 0 ){
			$this->_helper->redirector('send','order');
		}else{
		
			$price = 0;
			foreach( $order->product as $key => $v ){
				$price = $price + $v['price'] * $v['amount'];
			}
			
			$los = rand(1000,10000);			
			$hash = hash('sha256', $los.'payu'.date('G:i:s.Y-M-d'));
			
		
			$Client = new Client();
			$select = $Client->select();		
			$select->where('client_id = ?', $client->client_id);	
			$rowClient = $Client->fetchRow($select);	
	
				
			$Config = new Config();
			$select = $Config->select();
			$select->where('config_id = ?', 1);					
			$rowConfig = $Config->fetchRow($select);
			$order_time = $rowConfig['config_value'];
		
			$Order = new Order();
			
			if( $rowClient->client_shipment == 0 ){
				$data = array(
					'client_id' => $client->client_id,
					'order_ins' => Lib_Date::datetime(),
					'order_id_pack' => $basket->packid,
					'order_pack_log' => var_export($basket->pack,true),
					'order_id_transport' => $basket->transportid,
					'order_transport_log' => var_export($basket->transport,true),
					'order_id_leadtime' => $basket->leadtimeid,
					'order_price' => $basket->sumC,
					'order_price_pack' => $basket->rpd,
					'order_price_transport' => $basket->rtd,
					'order_price_leadtime' => $basket->rld,
					'order_price_product' => $basket->sum,
					'voucher_id' => isset($basket->voucher_id)?$basket->voucher_id:0,
					'order_session_id' => $hash,
					'order_time' => $order_time,
					
					'order_send_client_type' => $rowClient->client_type,
					'order_send_client_company' => $rowClient->client_company,
					'order_send_client_nip' => $rowClient->client_nip,
					'order_send_client_firstname' => $rowClient->client_firstname,
					'order_send_client_secondname' => $rowClient->client_secondname,
					'order_send_client_address' => $rowClient->client_address,
					'order_send_client_zipcode' => $rowClient->client_zipcode,
					'order_send_client_city' => $rowClient->client_city,
					'order_send_client_country' => $rowClient->client_country,
					'order_send_client_phone' => $rowClient->client_phone,
					
					'order_invoice' => $basket->basket_invoice,
					'order_invoice_name' => $basket->basket_invoice_name,
					'order_invoice_address' => $basket->basket_invoice_address,
					'order_invoice_zipcode' => $basket->basket_invoice_zipcode,
					'order_invoice_city' => $basket->basket_invoice_city,
					'order_invoice_country' => $basket->basket_invoice_country,
					'order_invoice_nip' => $basket->basket_invoice_nip,
					'order_clientinfo' => $basket->basket_clientinfo,
					'order_pay_type' => $basket->basket_pay,				
					
					'order_log' => var_export($order,true),
					
					'order_addedByAdmin' => isset($client->login_in_admin)?1:0
				);
			}else{
				$data = array(
					'client_id' => $client->client_id,
					'order_ins' => Lib_Date::datetime(),
					'order_id_pack' => $basket->packid,
					'order_pack_log' => var_export($basket->pack,true),
					'order_id_transport' => $basket->transportid,
					'order_transport_log' => var_export($basket->transport,true),
					'order_id_leadtime' => $basket->leadtimeid,
					'order_price' => $basket->sumC,
					'order_price_pack' => $basket->rpd,
					'order_price_transport' => $basket->rtd,
					'order_price_leadtime' => $basket->rld,
					'order_price_product' => $basket->sum,
					'voucher_id' => isset($basket->voucher_id)?$basket->voucher_id:0,
					'order_session_id' => $hash,
					'order_time' => $order_time,
					
					'order_send_client_type' => $rowClient->client_type,
					'order_send_client_company' => $rowClient->client_shipment_name,
					'order_send_client_nip' => $rowClient->client_nip,
					'order_send_client_firstname' => $rowClient->client_shipment_name,
					'order_send_client_secondname' => '',
					'order_send_client_address' => $rowClient->client_shipment_address,
					'order_send_client_zipcode' => $rowClient->client_shipment_zipcode,
					'order_send_client_city' => $rowClient->client_shipment_city,
					'order_send_client_country' => $rowClient->client_shipment_country,
					'order_send_client_phone' => $rowClient->client_phone,
					
					'order_invoice' => $basket->basket_invoice,
					'order_invoice_name' => $basket->basket_invoice_name,
					'order_invoice_address' => $basket->basket_invoice_address,
					'order_invoice_zipcode' => $basket->basket_invoice_zipcode,
					'order_invoice_city' => $basket->basket_invoice_city,
					'order_invoice_country' => $basket->basket_invoice_country,
					'order_invoice_nip' => $basket->basket_invoice_nip,
					'order_clientinfo' => $basket->basket_clientinfo,
					'order_pay_type' => $basket->basket_pay,				
					
					'order_log' => var_export($order,true),
					
					'order_addedByAdmin' => isset($client->login_in_admin)?1:0
				);
			}
			$idIns = $Order->insert($data);
			
			if( isset($basket->voucher_id) ){
				$Voucher = new Voucher();
				$data = array(
					'voucher_active' => 1,
					'voucher_date_utilization' => Lib_Date::datetime(),
					'order_id' => $idIns
				);
				$Voucher->update( $data , 'voucher_id='.$basket->voucher_id );
			}
			
			// newsletter - poczatek
			if( $basket->newsletter == 1 ){
			
			$Newsletter = new Newsletter();	
			$select = $Newsletter->select();		
			$select->where('newsletter_email = ?', $client->client_login);	
			$row = $Newsletter->fetchRow($select);
			
			if( !$row ){			
				$hashgen = hash('sha256',time().rand(10000,99999).$v['newsletter_email']);
				$data = array(
					'newsletter_code' => $hashgen,
					'newsletter_activ' => 1,
					'newsletter_email' => $client->client_login
				);
				$idIns = $Newsletter->insert($data);
			}
			
			}
			// newsletter - koniec
			
			
		$id = 44;		
		$Content = new Content();
		$select = $Content->select();		
		$select->where('content_id = ?', $id);	
		$row = $Content->fetchRow($select);			
		$this->view->rowContent = $row;
			
			$OrderProduct = new OrderProduct();
			$Product = new Product();
			
			foreach( $order->product as $key => $v1 ){
			
				$select = $Product->select();		
				$select->where('product_id = ?', $v1['idProduct']);	
				$rowProduct = $Product->fetchRow($select);
				
				if( $rowProduct['product_body'] == 1 ){				
					$ProductBody = new ProductBody();
					
					$select = $ProductBody->select();		
					$select->where('product_body_id = ?', $v1['product_body_id']);	
					if( $v1['product_body_id']!=0 ) $rowProductBody = $ProductBody->fetchRow($select);
					
					$select = $ProductBody->select();		
					$select->where('product_body_id = ?', $v1['product_body2_id']);	
					if( $v1['product_body2_id']!=0 ) $rowProductBody2 = $ProductBody->fetchRow($select);
					
					if( $rowProductBody['product_body_type'] == 0 ){
						$rowProductBodyRim = @Lib_Lang::consta(front_configurator_body_rim.$v1['product_rim_id']);
					}else{
						$v1['product_rim_id'] = 0;
					}
					
				}
				
				if( $rowProduct['product_front'] == 1 ){				
					$ProductFront = new ProductFront();
					
					$select = $ProductFront->select();		
					$select->where('product_front_id = ?', $v1['product_front_id']);	
					if( $v1['product_front_id']!=0 ) $rowProductFront = $ProductFront->fetchRow($select);
					
					if( $rowProductFront['product_front_type'] != 1 ){
						$rowProductFrontRim = @Lib_Lang::consta(front_configurator_front_rim.$v1['product_rim2_id']);
					}else{
						$v1['product_rim2_id'] = 0;
					}
					
				}
				
				if( $rowProduct['product_type'] == 1 ){				
					$ProductType = new ProductType();
					
					$select = $ProductType->select();		
					$select->where('product_type_id = ?', $v1['product_type_id']);	
					if( $v1['product_type_id']!=0 ) $rowProductType = $ProductType->fetchRow($select);
					
				}
				
				if( $rowProduct['product_accessories'] == 1 ){				
					$ProductAccessories = new ProductAccessories();
					
					$select = $ProductAccessories->select();		
					$select->where('product_accessories_id = ?', $v1['product_accessories_id']);	
					if( $v1['product_accessories_id']!=0 ) $rowProductAccessories = $ProductAccessories->fetchRow($select);
					
					$select = $ProductAccessories->select();		
					$select->where('product_accessories_id = ?', $v1['product_accessories_id2']);	
					if( $v1['product_accessories_id2']!=0 ) $rowProductAccessories2 = $ProductAccessories->fetchRow($select);
					
					$select = $ProductAccessories->select();		
					$select->where('product_accessories_id = ?', $v1['product_accessories_id3']);	
					if( $v1['product_accessories_id3']!=0 ) $rowProductAccessories3 = $ProductAccessories->fetchRow($select);
				
					$select = $ProductAccessories->select();		
					$select->where('product_accessories_id = ?', $v1['product_accessories_id4']);	
					if( $v1['product_accessories_id4']!=0 ) $rowProductAccessories4 = $ProductAccessories->fetchRow($select);
					
				}				
					
				if( $rowProduct['product_legs'] == 1 ){				
					$ProductLegs = new ProductLegs();
					
					$select = $ProductLegs->select();		
					$select->where('product_legs_id = ?', $v1['product_legs_id']);	
					if( $v1['product_legs_id']!=0 ) $rowProductLegs = $ProductLegs->fetchRow($select);
					
				}
		
				if( $rowProduct['product_handles'] == 1 ){				
					$ProductHandles = new ProductHandles();
					
					$select = $ProductHandles->select();		
					$select->where('product_handles_id = ?', $v1['product_handles_id']);	
					if( $v1['product_handles_id']!=0 ) $rowProductHandles = $ProductHandles->fetchRow($select);
					
				}
			
				$data = array(
					'order_id' => $idIns,
					'product_id' => $v1['idProduct'],
					'product_shop' => $rowProduct['product_shop'],					
					'product_shelf' => $rowProduct['product_shelf'],
					'product_wysszerA' => $rowProduct['product_wysszerA'],
					'product_wysszerB' => $rowProduct['product_wysszerB'],
					'product_wysszerC' => $rowProduct['product_wysszerC'],
					'product_wysszerD' => $rowProduct['product_wysszerD'],
					'product_wysszerE' => $rowProduct['product_wysszerE'],
					'product_body' => $rowProduct['product_body'],
					'product_front' => $rowProduct['product_front'],
					'product_type' => $rowProduct['product_type'],
					'product_accessories' => $rowProduct['product_accessories'],
					'product_legs' => $rowProduct['product_legs'],
					'product_handles' => $rowProduct['product_handles'],
					'product_options' => $rowProduct['product_options'],					
					'order_product_price' => isset($v1['price'])?$v1['price']:0,
					'order_product_amount' => isset($v1['amount'])?$v1['amount']:0,
					'order_product_height' => isset($v1['product_height'])?$v1['product_height']:0,
					'order_product_width' => isset($v1['product_width'])?$v1['product_width']:0,
					'order_product_depth' => isset($v1['product_depth'])?$v1['product_depth']:0,
					'order_product_shelfamount' => isset($v1['product_shelfamount'])?$v1['product_shelfamount']:0,				
					
					'order_product_wysszerA' => isset($v1['product_wysszerAamount'])?$v1['product_wysszerAamount']:0,
					'order_product_wysszerB' => isset($v1['product_wysszerBamount'])?$v1['product_wysszerBamount']:0,	
					'order_product_wysszerC' => isset($v1['product_wysszerCamount'])?$v1['product_wysszerCamount']:0,	
					'order_product_wysszerD' => isset($v1['product_wysszerDamount'])?$v1['product_wysszerDamount']:0,	
					'order_product_wysszerE' => isset($v1['product_wysszerEamount'])?$v1['product_wysszerEamount']:0,	
					
					'product_body_id' => isset($v1['product_body_id'])?$v1['product_body_id']:0,					
					'product_body2_id' => isset($v1['product_body2_id'])?$v1['product_body2_id']:0,					
					'product_rim_id' => isset($v1['product_rim_id'])?$v1['product_rim_id']:0,					
					'product_front_id' => isset($v1['product_front_id'])?$v1['product_front_id']:0,					
					'product_rim2_id' => isset($v1['product_rim2_id'])?$v1['product_rim2_id']:0,					
					'product_front_client_info' => isset($v1['client_info'])?$v1['client_info']:'',					
					'product_type_id' => isset($v1['product_type_id'])?$v1['product_type_id']:0,					
					'product_accessories_id' => isset($v1['product_accessories_id'])?$v1['product_accessories_id']:0,					
					'product_accessories2_id' => isset($v1['product_accessories_id2'])?$v1['product_accessories_id2']:0,					
					'product_accessories3_id' => isset($v1['product_accessories_id3'])?$v1['product_accessories_id3']:0,					
					'product_accessories4_id' => isset($v1['product_accessories_id4'])?$v1['product_accessories_id4']:0,					
					'product_legs_id' => isset($v1['product_legs_id'])?$v1['product_legs_id']:0,					
					'product_handles_id' => isset($v1['product_handles_id'])?$v1['product_handles_id']:0,
					'product_options_1' => isset($v1['product_options_1'])?$v1['product_options_1']:0,
					'product_options_2' => isset($v1['product_options_2'])?$v1['product_options_2']:0,
					'product_options_3' => isset($v1['product_options_3'])?$v1['product_options_3']:0,
					'product_options_4' => isset($v1['product_options_4'])?$v1['product_options_4']:0,
					'product_options_id' => isset($v1['product_options_id'])?$v1['product_options_id']:0,
					'product_options2_id' => isset($v1['product_options_id2'])?$v1['product_options_id2']:0					
				);
				//var_dump($data); die();echo'<hr />';
				$idOrder = $OrderProduct->insert($data);
			
			/*		
				$OrderProductLog = new OrderProductLog();
				$data = array(			
				//	'order_product_id' => $idOrder,	
					'product_log' => var_export($rowProduct,true),
					'product_body_log' => isset($rowProductBody)?var_export($rowProductBody,true):'',
					'product_body2_log' => isset($rowProductBody2)?var_export($rowProductBody2,true):'',
					'product_rim_log' => isset($rowProductBodyRim)?var_export($rowProductBodyRim,true):'',
					'product_front_log' => isset($rowProductFront)?var_export($rowProductFront,true):'',
					'product_rim2_log' => isset($rowProductFrontRim)?var_export($rowProductFrontRim,true):'',
					'product_type_log' => isset($rowProductType)?var_export($rowProductType,true):'',
					'product_accessories_log' => isset($rowProductAccessories)?var_export($rowProductAccessories,true):'',
					'product_accessories2_log' => isset($rowProductAccessories2)?var_export($rowProductAccessories2,true):'',
					'product_accessories3_log' => isset($rowProductAccessories3)?var_export($rowProductAccessories3,true):'',
					'product_accessories4_log' => isset($rowProductAccessories4)?var_export($rowProductAccessories4,true):'',	
					'product_legs_log' => isset($rowProductLegs)?var_export($rowProductLegs,true):'',
					'product_handles_log' => isset($rowProductHandles)?var_export($rowProductHandles,true):'',	
					'order_product_log' => var_export($v1,true)
				);
				foreach( $data as $key => $val ){				
					$data1 = array(			
						'order_product_id' => $idOrder,	
						'order_product_log_key' => $key,	
						'order_product_log_val' => $val
					);
					$OrderProductLog->insert($data1);
				}
			*/	
				
			}
			
			$desc = "Zamowienie numer ".$idIns;
			$Order = new Order();
			$select = $Order->select();				
			$select->where('order_id = ?', $idIns);			
			$row = $Order->fetchRow($select);			
			$this->view->rowOrder = $row;
			
			$amount = $basket->sumC*100;
			
			$this->view->amount = $amount;
			$order_id = $row['order_id'];
			$this->view->order_id = $order_id;
			$this->view->session_id = $hash;
			$this->view->desc = $desc;
			$this->view->client_ip = $_SERVER['REMOTE_ADDR'];
			
			$ts = time();
			$this->view->ts = $ts;
			
			// sig = md5( pos_id + pay_type + session_id + pos_auth_key + amount + desc + desc2 + trsDesc + order_id + first_name + last_name + street + street_hn + street_an + city + post_code + country + email + phone + language + client_ip + ts + key1 )
			$this->view->sig = md5(PAY_pos_id.$hash.PAY_pos_auth_key.$amount.$desc.$order_id.$_SERVER['REMOTE_ADDR'].$ts.PAY_key);
			
                        /* Notification */
                        $Admin = new Admin();
                        $select = $Admin->select();
                        $list = $Admin->fetchAll($select);
                        $headers = "MIME-Version: 1.0\r\n";
                        $headers.= "From: =?utf-8?b?".base64_encode(MAIL_LOGIN)."?= <".MAIL_LOGIN.">\r\n";
                        $headers.= "Content-Type: text/html;charset=utf-8\r\n";
                        $headers.= "Reply-To: ".MAIL_LOGIN."\r\n";  
                        $headers.= "X-Mailer: PHP/" . phpversion();

                        $body  = $desc;
                        $body .= nl2br("
Prosimy o dokonanie wpłaty w kwocie ".$basket->sumC." zł na dane:

Meble.PW Krzysztof Kubik
ul. Kolonia 129
39-100 Ropczyce

Numer konta bankowego:
Pekao S.A.
PL 19 1240 2669 1111 0010 4494 9997");
                        
                        //send to admins
                        foreach($list as $val)
                        {
                            mail($val->admin_email, $desc, $body, $headers);
                        }
                            
                        //send to client                        
                         mail($client->client_login, $desc, $body, $headers);  
            
			$OrderLog = new OrderLog();
			$data = array(
				'order_id' => $idIns,
				'order_log_order' => json_encode($order->product),
				'order_log_basket' => json_encode($basket->product)
			);
			$idOrderLog = $OrderLog->insert($data);
                        
			Zend_Session::namespaceUnset('order');
			Zend_Session::namespaceUnset('basket');

			$data = array(					
				'client_basket' => '',
				'client_order' => ''
			);
			$Client->update( $data , 'client_id='.$client->client_id);
		}
	
	}
	
}
