<?php

	require_once("modules/EcmInvoiceOutOlds/EcmInvoiceOutOld.php");
	
	class EcmInvoiceOutOldToTransfer extends EcmInvoiceOutOld {
	
		function getPosition($id) {
			if(!isset($id) || $id == '') return '';
			require_once('modules/EcmProductsDocumentsRelations/EcmProductsDocumentsRelation.php');
			$epdr = new EcmProductsDocumentsRelation();
			$epdr->retrieve($id);
			$return_array = array();
			if(isset($epdr->id) && $epdr->id != '') {
				$return_array['id'] = $epdr->product_id;
				$return_array['code'] = $epdr->product_code;
				$return_array['name'] = $epdr->product_name;
				$return_array['quantity'] = $epdr->product_quantity;
				$return_array['price'] = $epdr->product_price;
				$return_array['discount'] = $epdr->product_discount;
				$return_array['total'] = $epdr->product_total;
				$return_array['unit_id'] = $epdr->product_unit_id;
				$return_array['vat_id'] = $epdr->product_vat_id;
				$return_array['category_id'] = $epdr->product_category_id;
				$return_array['project_task_name'] = $epdr->project_task_name;
				$return_array['project_task_id'] = $epdr->project_task_id;
				$return_array['project_name'] = $epdr->project_name;
				$return_array['project_id'] = $epdr->project_id;
				$epdr->fill_in_additional_detail_fields();
				$return_array['currency_id'] = $epdr->currency->id;
				$return_array['currency_symbol'] = $epdr->currency->symbol;
				$return_array['date'] = $epdr->d_date;
				return $return_array;
			}
			return '';
		}
		function getPositionList($array = false) {
			if(isset($this->id) && $this->id != '') {
				$query = "SELECT `id` FROM `ecmproductsdocumentsrelations` WHERE `document_id`='".$this->id."' AND `document_type`='EcmInvoiceOutOlds' AND`deleted`='0' order by product_position asc";
				$r = $this->db->query($query);
				$return_array = array();
				if($r) {
					while($w = $this->db->fetchByAssoc($r)) {
						$return_array [] = $this->getPosition($w['id']);
					}
					$json = getJSONobj();
					return $array ? $return_array : $json->encode($return_array);
				}
			}
			return $array ? false : '[]';
		}
		/*
		function deleteAssignedPositions() {
			if(isset($this->id) && $this->id != '') {
				$query = "DELETE FROM `ecmproductsdocumentsrelations` WHERE `document_id`='".$this->id."' AND `document_type`='EcmInvoiceOutOlds'";
				$r = $this->db->query($query);
				if($r) return true;
			}
			return false;          
		}
		*/
	}
	

	$query = "SELECT `id`, `deleted` FROM `ecminvoiceoutolds` WHERE `deleted`='0'";
	$GLOBALS['db'] = new MysqlManager();
	$GLOBALS['db']->connect();
	$results = $GLOBALS['db']->query($query);
	if(is_resource($results)) {
		while($row = $GLOBALS['db']->fetchByAssoc($results)) {
			if(isset($row['id']) && $row['id'] != '') {
				$focus = new EcmInvoiceOutOldToTransfer();
				$focus->retrieve($row['id']);
				if(isset($focus->id) && $focus->id != '') {
					echo $focus->id.' -> '.$focus->name.'<br />';
				    $focus->position_list = $focus->getPositionList(true);
					$focus->savePositions($focus->id);
				}	
			}
		}
	}

?>