<?php
require_once('modules/EcmStockDocOuts/EcmStockDocOut.php');
require_once('modules/EcmReceipts/EcmInvoiceOut.php');

$cids=explode(",",$_REQUEST['uid']);
$count=count($cids);
//echo $_REQUEST['uid'];
//die();
for($i=0;$i<$count;$i++){
  if(mysql_num_rows($GLOBALS[db]->query("select id from ecmreceipts where wz_id='".$cids[$i]."' and deleted='0'"))>0)continue;
  $source = new EcmStockDocOut();
  $source->retrieve($cids[$i]);
  if(isset($source->id) && $source->id != '') {
    $focus=new EcmReceipt();
    $focus->name = $source->name;
    $focus->position_list = $source->getPositionList(true);
    $focus->template_id = $source->template_id;
    $focus->template_name = $source->template_name;
    $focus->total = $source->total;
    $focus->subtotal = $source->subtotal;
	$focus->type='normal';
	
    
	$r=$GLOBALS[db]->fetchByAssoc($GLOBALS[db]->query("select parent_id from accounts where id='".$source->parent_id."'"));
	if($r['parent_id'])$parent_id=$r['parent_id'];
	else $parent_id=$source->parent_id;
	$r=$GLOBALS[db]->fetchByAssoc($GLOBALS[db]->query("select name,billing_address_street,billing_address_city,billing_address_country,billing_address_postalcode,supplier_code,ecmpaymentcondition_id,ecmpaymentcondition_name,supplier_code from accounts where id='".$parent_id."'"));
	$parent_name=$r['name'];
	$parent_address_street=$r['billing_address_street'];
	$parent_address_city=$r['billing_address_city'];
	$parent_address_postalcode=$r['billing_address_postalcode'];
	$parent_address_country=$r['billing_address_country'];
	$supplier_code=$r['supplier_code'];
	
	if(!$parent_id){
		$parent_id=$source->parent_id;
		$parent_name=$source->parent_name;
		$parent_address_street = $source->parent_address_street;
    	$parent_address_postalcode = $source->parent_address_postalcode;
    	$parent_address_city = $source->parent_address_city;
    	$parent_address_country = $source->parent_address_country;
	}
		
    $focus->parent_type = $source->parent_type;
    $focus->parent_id = $parent_id;
    $focus->parent_name = $parent_name;
	$focus->contact_id = $source->contact_id;
	$focus->contact_name = $source->contact_name;
	
	$focus->order_no=$source->order_no;
	$focus->supplier_code=$source->supplier_code;
    
    $focus->parent_address_street = $parent_address_street;
    $focus->parent_address_postalcode = $parent_address_postalcode;
    $focus->parent_address_city = $parent_address_city;
    $focus->parent_address_country = $parent_address_country;
	$focus->supplier_code=$supplier_code;
	$focus->ecmpaymentcondition_id=$r['ecmpaymentcondition_id'];
	$focus->ecmpaymentcondition_name=$r['ecmpaymentcondition_name'];
	
	$focus->sell_date=$GLOBALS['timedate']->to_display_date(date($GLOBALS['timedate']->dbDayFormat));
	$focus->register_date=$GLOBALS['timedate']->to_display_date(date($GLOBALS['timedate']->dbDayFormat));
	$rrrrrr=$GLOBALS[db]->fetchByAssoc($GLOBALS[db]->query("select days from ecmpaymentconditions where id='".$r['ecmpaymentcondition_id']."'"));
	$focus->payment_date=$GLOBALS['timedate']->to_display_date(date($GLOBALS['timedate']->dbDayFormat,mktime()+3600*24*$rrrrrr['days']));
	$focus->status="accepted";
	$focus->assigned_user_id=$_SESSION['authenticated_user_id'];
	
	$r=$GLOBALS[db]->fetchByAssoc($GLOBALS[db]->query("select sic_code,vatid from accounts where id='".$parent_id."'"));
    $focus->to_nip = $r['vatid'];
	$focus->ecmlanguage = $source->ecmlanguage;  
	$focus->wz_id=$cids[$i];
	$return_id=$focus->save(); 
	$GLOBALS[db]->query("update ecmreceipts set wz_id='".$cids[$i]."' where id='".$return_id."'");
	
  }
}
header("Location: index.php?module=EcmStockDocOuts&action=index");
?>
