@ -460,28 +460,37 @@ public class DocumentApi extends BaseHandler implements DocumentService { 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
						private  boolean  postDocument ( HttpExchange  ex ,  UmbrellaUser  user )  throws  IOException ,  UmbrellaException  {   
					 
					 
					 
						private  boolean  postDocument ( HttpExchange  ex ,  UmbrellaUser  user )  throws  IOException ,  UmbrellaException  {   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
							var  json  =  json ( ex ) ;   
					 
					 
					 
							var  json  =  json ( ex ) ;   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
							if  ( ! ( json . has ( SENDER )  & &  json . get ( SENDER )  instanceof  JSONObject  senderData ) )  throw  missingFieldException ( SENDER ) ;   
					 
					 
					 
							if  ( ! ( json . has ( SENDER )  & &  json . get ( SENDER )  instanceof  JSONObject  senderData ) )  throw  missingFieldException ( SENDER ) ;   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
							if  ( ! senderData . has ( COMPANY )  | |  ! ( senderData . get ( COMPANY )  instanceof  Number  company Id) )  throw  missingFieldException ( COMPANY ) ;   
					 
					 
					 
							if  ( ! senderData . has ( COMPANY )  | |  ! ( senderData . get ( COMPANY )  instanceof  Number  rawComp Id) )  throw  missingFieldException ( COMPANY ) ;   
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
					
  
					 
					 
					 
							var  companyId  =  rawCompId . longValue ( ) ;   
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							var  company  =  companyService ( ) . get ( companyId . longValue ( ) ) ;   
					 
					 
					 
							var  company  =  companyService ( ) . get ( companyId ) ;   
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							if  ( ! companyService ( ) . membership ( companyId . longValue ( ) , user . id ( ) ) )  throw  forbidden ( "You are mot a member of company {0}" , company ) ;   
					 
					 
					 
							if  ( ! companyService ( ) . membership ( companyId , user . id ( ) ) )  throw  forbidden ( "You are mot a member of company {0}" , company ) ;   
				
			 
			
				
				
			
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
							if  ( ! json . has ( CUSTOMER )  | |  ! ( json . get ( CUSTOMER )  instanceof  JSONObject  customerData ) )  throw  missingFieldException ( CUSTOMER ) ;   
					 
					 
					 
							if  ( ! json . has ( CUSTOMER )  | |  ! ( json . get ( CUSTOMER )  instanceof  JSONObject  customerData ) )  throw  missingFieldException ( CUSTOMER ) ;   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
							if  ( ! json . has ( TYPE )  | |  ! ( json . get ( TYPE )  instanceof  Number  docTypeId ) )  throw  missingFieldException ( TYPE ) ;   
					 
					 
					 
							if  ( ! json . has ( TYPE )  | |  ! ( json . get ( TYPE )  instanceof  Number  docTypeId ) )  throw  missingFieldException ( TYPE ) ;   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
							var  type  =  db . getType ( docTypeId . intValue ( ) ) ;   
					 
					 
					 
							var  type  =  db . getType ( docTypeId . intValue ( ) ) ;   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
							var  customer  =  Customer . of ( customerData ) ;   
					 
					 
					 
							var  customer  =  Customer . of ( customerData ) ;   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
							Template  template  =  new  Template ( 6 , companyId . longValue ( ) , "unknwon" , null ) ;   
					 
					 
					 
							Template  template  =  new  Template ( 6 , companyId , "unknwon" , null ) ;   
				
			 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					 
					 
					 
							String  currency  =  company . currency ( ) ;   
					 
					 
					 
							String  currency  =  company . currency ( ) ;   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
							String  sep  =  company . decimalSeparator ( ) ;   
					 
					 
					 
							String  sep  =  company . decimalSeparator ( ) ;   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
							var  settings  =  db . getCustomerSettings ( companyId . longValue ( ) , type , customer . id ( ) ) ;   
					 
					 
					 
							var  settings  =  db . getCustomerSettings ( companyId , type , customer . id ( ) ) ;   
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							if  ( settings  = =  null )  settings  =  CustomerSettings . empty ( ) ;   
					 
					 
					 
							var  newCustomer  =  settings  = =  null ;   
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							var  companySettings  =  db . getCompanySettings ( companyId . longValue ( ) , type ) ;   
					 
					 
					 
							if  ( newCustomer )  settings  =  CustomerSettings . empty ( ) ;   
				
			 
			
				
				
			
		
	
		
		
	
		
		
	
		
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
							var  companySettings  =  db . getCompanySettings ( companyId , type ) ;   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
							var  nextNumber  =  companySettings . nextDocId ( ) ;   
					 
					 
					 
							var  nextNumber  =  companySettings . nextDocId ( ) ;   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
							String  lastHead  =  settings . header ( ) ;   
					 
					 
					 
							String  lastHead  =  settings . header ( ) ;   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
							String  lastFooter  =  settings . footer ( ) ;   
					 
					 
					 
							String  lastFooter  =  settings . footer ( ) ;   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
							var  sender  =  Sender . of ( senderData ) ;   
					 
					 
					 
							var  sender  =  Sender . of ( senderData ) ;   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
							LOG . log ( DEBUG , json . toString ( 2 ) ) ;   
					 
					 
					 
							LOG . log ( DEBUG , json . toString ( 2 ) ) ;   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
							var  doc  =  new  Document ( 0 , companyId . longValue ( ) , nextNumber , type ,  LocalDate . now ( ) ,  NEW , template , null , lastHead , lastFooter , currency , sep , sender , customer , new  PositionList ( ) ) ;   
					 
					 
					 
							var  doc  =  new  Document ( 0 , companyId , nextNumber , type ,  LocalDate . now ( ) ,  NEW , template , null , lastHead , lastFooter , currency , sep , sender , customer , new  PositionList ( ) ) ;   
				
			 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					 
					 
					 
							var  saved  =  db . save ( doc ) ;   
					 
					 
					 
							var  saved  =  db . save ( doc ) ;   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
							if  ( newCustomer )  {   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
								if  ( customerData . get ( CONTACT_ID )  instanceof  Number  contactId )  {   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
									var  contacts  =  contactService ( ) ;   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
									var  contact  =  contacts . load ( user . id ( ) ,  contactId . longValue ( ) ) ;   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
									contacts . save ( contact . setCustomerNumber ( customer . id ( ) ) ) ;   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
								}   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
								companyService ( ) . saveNewCustomer ( companyId , customer . id ( ) ) ;   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
							}   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
							db . step ( companySettings ) ;   
					 
					 
					 
							db . step ( companySettings ) ;   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
							return  sendContent ( ex , saved ) ;   
					 
					 
					 
							return  sendContent ( ex , saved ) ;   
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
						}   
					 
					 
					 
						}