<?php
class autoCreateInvoiceFromWZ {
	private $wz_id;
	private $account_id;
	private $db;
	private $inv;
	public function autoCreateInvoiceFromWZ($wz_id) {
		$this->wz_id = $wz_id;
		$this->db = $GLOBALS ['db'];
		$this->account_id = $this->db->fetchByAssoc ( $this->db->query ( "SELECT parent_id FROM ecmstockdocouts WHERE id='$this->wz_id'" ) )['parent_id'];
		$inv = new EcmInvoiceOut ();
	}
	public function fillInvoice() {
		$wz = new EcmStockDocOut ();
		$wz->retrieve ( $this->wz_id );
		$wz_id = $this->wz_id;
		$so_id = $wz->ecmsale_id;
		$s = new EcmSale ();
		$s->retrieve ( $so_id );
		$focus = $this->inv;
		$focus->currency_id = $s->currency_id;
		$wz_name = $wz->document_no;
		// load from zs
		$focus->parent_id = $s->parent_id;
		$focus->parent_name = $s->parent_name;
		$focus->order_no = $s->parent_document_no;
		$focus->supplier_code = $s->parent_supplier_code;
		// dates
		$focus->stock_id = $wz->stock_id;
		$focus->stock_name = $wz->stock_name;
		$focus->register_date = date ( "d.m.Y" );
		$focus->sell_date = $wz->register_date;
		$focus->validtill_date = date ( "d.m.Y" );
		$focus->parent_shipping_address_name = $s->shipping_address_name;
		$focus->parent_shipping_address_street = $s->shipping_address_street;
		$focus->parent_shipping_address_postalcode = $s->shipping_address_postalcode;
		$focus->parent_shipping_address_city = $s->shipping_address_city;
		$focus->parent_shipping_address_country = $s->shipping_address_country;
		$focus->parent_shipping_address_nip = $s->shipping_nip;
		$focus->parent_shipping_address_iln = $s->shipping_iln;
		
		// saturn??
		$tmp_a = new Account ();
		$tmp_a->retrieve ( $focus->parent_id );
		if ($tmp_a->parent_id == '1249') {
			$a = new Account ();
			$a->retrieve ( '1249' );
			
			$focus->parent_payer_address_name = $a->name;
			$focus->parent_payer_address_street = $a->shipping_address_street;
			$focus->parent_payer_address_postalcode = $a->shipping_address_postalcode;
			$focus->parent_payer_address_city = $a->shipping_address_city;
			$focus->parent_payer_address_country = $a->shipping_address_country;
			$focus->parent_payer_address_nip = $a->to_vatid;
			$focus->parent_payer_address_iln = $a->iln;
		}
		
		$pc = new EcmPaymentCondition ();
		$pc->retrieve ( $s->ecmpaymentcondition_id );
		$focus->ecmpaymentcondition_id = $pc->id;
		$focus->ecmpaymentcondition_name = $pc->name;
		if ($tmp_a->parent_id != '1249')
			$focus->payment_date = $GLOBALS ['timedate']->to_display_date ( date ( "Y-m-d", mktime () + 3600 * 24 * $pc->days ) );
		else {
			$ss = $GLOBALS ['db']->fetchByAssoc ( $GLOBALS ['db']->query ( "SELECT delivery_date FROM ecmsales WHERE id='" . $r ['ecmsale_id'] . "'" ) );
			$d = date ( 'Y-m-t', strtotime ( $ss ['delivery_date'] ) );
			$focus->payment_date = $GLOBALS ['timedate']->to_display_date ( date ( "Y-m-d", mktime () + 3600 * 24 * 60 ) );
		}
		//position list && calculate totals
		$pl = $wz->getPositionList(true);
		$positions = array();
		foreach ($pl as $p) {
			$tmp = $p;
			$tmp['price_netto'] = $p['price_start'];
			$tmp['total_netto'] = round($p['quantity'] * $p['price_start'],2);
			$tmp['total_vat'] = round(($tmp['total_netto']*$p['ecmvat_value'])/100,2);
			$tmp['total_brutto'] = round($tmp['total_netto']+$tmp['total_vat'],2);
			$tmp['price_brutto'] = round($tmp['total_brutto'] / $tmp['quantity'],2);
			
			$positions[] = $tmp;
		}	
		echo '<pre>';
		var_dump($positions);
		echo '</pre>';
		//$focus->position_list = $wz->getPositionList(true);	
	//$this->inv = $focus;
	}
}
?>