<?xml version="1.0" encoding="utf-8"?>
<Forms>
    <Form>
        <FormName>Выгрузка сводной продажи в текстовый файл</FormName>
        <FormType>11</FormType>
        <FormScript>var qOpers=query.newq();
var qItem=query.newq();
var qCode=query.newq();

var isSelected=false;
if(typeof idList != "undefined"){
	if(idList.length>0){
		isSelected=confirm("Выводить только для выделенных записей?");
	}
}

var isaccept=confirm("Выводить данные только по проведенным заказам?");
var isDescr=confirm("Выводить описания товаров?");


var d1,d2;

d1=prompt('getdate');
d2=prompt('getdate');

if(d1==null || d2==null){
	throw("Не задан интервал!");
}

if(f.dateparse(d1)> f.dateparse(d2) ){
	throw("Не верно задан интервал!");
}

doc.openoutfile('orders'+f.today(0)+'.txt');
doc.wl('Vvs data for exchange');
doc.wl('##partner');
doc.wl('code:0001');
doc.wl('name:Розничная реализация');
doc.wl('##endpartner');
doc.wl('##order');
doc.wl('type:1'); 
/*doc.wl('number:'+qInv.value('inv_num'));*/
doc.wl('date:'+f.dateDMY(d1)); 
doc.wl('##endorder');


var qstr= 'select items.*, group_name, unit_name, country_name '+
	' from items left outer join groups on groups._id = item_group_id '+
	' left outer join units on units._id = item_unit_id '+
	' left outer join vendors on vendors._id = items.item_vendor_id '+
	' left outer join countries on countries._id = item_country_id ';

var qOne;
/*get data for items*/
if(isSelected){
 for(k=0;k&lt;idList.length;k++){
 	if(mess.iscancel()) break;

	qOne=qstr+'  where items._id='+idList[k];
 	if(qItem.select(qOne) &amp;&amp; qItem.first()){
		doit();
		mess.say(qItem.getstring('item_name'));
	}
 }

}
else{
 qstr+='  where '+doc.itemsfilter();

 isOk = qItem.select(qstr);

 if(isOk &amp;&amp; qItem.first()){
 do{
 	if(mess.iscancel()) break;

	doit();
 	mess.say(qItem.getstring('item_name'));
 }while(qItem.next());
 }
} 

doc.outclose();
mess.close();
doc.send("Продажи")
doc.setExtEditor("notepad");
doc.open();
/*-------------- end of main part --------------*/


function doit()
{

var id = qItem.getint("_id");



str="select invcitem.*,inv_type,inv_date, inv_num, account_name from invcitem "+
" left outer join invoices on invoices._id=invcitem_invoice_id "+
" left outer join accounts on accounts._id=invoices.inv_partner_id "+
" where invcitem_item_id="+id+" and inv_type=1 "+
" and (invoices.inv_date between '"+d1+"' and '"+d2+"') ";

if(isaccept) str+=" and inv_accept=1";

/*str+=" order by inv_date,inv_type";*/
var qty=0,total=0;


if(qOpers.select(str) &amp;&amp; qOpers.first()){
	do{
		qty+=qOpers.getfloat("invcitem_qty");
		total+=qOpers.getfloat("invcitem_total");
	}while(qOpers.next());

	if( qty!=0.0){
	   if(isDescr){
		doc.wl('##goods');
		doc.wl('code:'+qItem.value('item_code'));
		doc.wl('name:'+qItem.value('item_name'));
		doc.wl('group:'+qItem.value('group_name'));
		if( qItem.value('item_art')!="" ) doc.wl('article:'+qItem.value('item_art'));
		if( qItem.value('item_note')!="" ) doc.wl('note:'+qItem.value('item_note'));
		if( qItem.value('item_vendor_code')!="" ) doc.wl('vendor_code:'+qItem.value('item_vendor_code'));
		if( qItem.value('item_num_in_pack')!="" ) doc.wl('in_pack:'+qItem.value('item_num_in_pack'));
		if( qItem.value('item_weight')!="" ) doc.wl('weight:'+qItem.value('item_weight'));
		if( qItem.value('item_gtd')!="" ) doc.wl('gtd:'+qItem.value('item_gtd'));
		if( qItem.value('item_expiry')!="" ) doc.wl('expiry:'+f.datestr(qItem.value('item_expiry')));
		if( qItem.value('unit_name')!="" ) doc.wl('unit:'+qItem.value('unit_name'));
		if( qItem.value('vendor_name')!="" ) doc.wl('vendor:'+qItem.value('vendor_name'));
		if( qItem.value('country_name')!="" ) doc.wl('country:'+qItem.value('country_name'));
   
		if(qCode.select('select code from barcodes where item_id='+qItem.value('_id'))){
			do{
	          	doc.wl('barcode:'+qCode.value('code'));
			}while(qCode.next());
		}
   		doc.wl('##endgoods');
	   }
 	   doc.wl('##item');
 	   price=f.round(total/qty,2);
	   doc.wl('code:'+qItem.value('item_code')); 
	   doc.wl('qty:'+ qty); 
	   doc.wl('price:'+ price); 
	   doc.wl('##enditem');
	}

}



}/*doit()*/




</FormScript>
    </Form>


    <Form>
        <FormName>Переоценка  -  копирование цен по заказу в закупочные цены</FormName>
        <FormType>0</FormType>
        <FormScript>var isUpdBuy=confirm("Обновлять у товаров цену поставщика?")
if(!isUpdBuy){
	throw "Обработка прервана!";
}

var q =  query.newinstance();
var qUpd=query.newinstance();


ch = q.select("select invcitem.*, items.item_price "+
", item_code, item_name, item_price_discount, item_price_vendor "+
" from invcitem "+
" left outer join items on items._id=invcitem_item_id "+
"  where invcitem_invoice_id="+doc.id()+
" order by invcitem_linenumber");
if(!ch){
	throw new Error('Error getting order items!\n'+q.lasterror());
}
mess.show();
doc.outopen();
 doc.wl('&lt;html>');
 doc.wl('&lt;head>');
 doc.wl('&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" />');
 doc.wl('&lt;/head>');
 doc.wl('&lt;body style="font-size:10pt" >');
 doc.wl('&lt;p>Изменение цен поставщика для товаров из заказа. &lt;/p>'); 
 doc.wl('&lt;table border="1" align="left" width="100%" cellspacing="0" cellpadding="4" style="border-style:solid;border-width:1px;border-color:black;">');
doc.wl('&lt;tr>');
doc.wl('&lt;th>Код&lt;/th>');
doc.wl('&lt;th>Товар&lt;/th>');
doc.wl('&lt;th>Старая цена&lt;/th>');
doc.wl('&lt;th>Новая цена&lt;/th>&lt;/tr>');
doc.wl('&lt;/tr>');
do{
 if(mess.iscancel()) break;

	old3=q.getfloat('item_price_vendor');
	price3= q.getfloat('invcitem_price');

		

s="update items set item_price_vendor="+price3;
s+=" where _id="+q.value('invcitem_item_id');
	
doc.wl('&lt;tr>'); 
ok= qUpd.exec(s);
  if(!ok){ 
	 doc.wl('&lt;td colspan="4">'+qUpd.lasterror()+'&lt;/td>');
  }
  else{
  	doc.wl('&lt;td>'+q.value('item_code')+'&lt;/td>');
  	doc.wl('&lt;td>'+q.value('item_name')+'&lt;/td>');
	doc.wl('&lt;td align="right">'+old3+'&lt;/td>');
 	doc.wl('&lt;td align="right">'+price3+'&lt;/td>');

  }

  doc.wl('&lt;/tr>');
 	
 }while(q.next());




q.close();


 doc.wl('&lt;/table>&lt;/body>&lt;/html>');
 doc.outclose();
 mess.close();
 doc.open();
</FormScript>
    </Form>


    <Form>
        <FormName>Переоценка товаров - для 3 цен</FormName>
        <FormType>0</FormType>
        <FormScript>var percent1=prompt('Введите процент наценки для розничной цены:',30);
if(percent1==null){
	throw "Обработка прервана!";
}
var percent2=prompt('Введите процент наценки для цены со скидкой:',20);
if(percent2==null){
	throw "Обработка прервана!";
}
var isUpdBuy=confirm("Обновлять у товаров цену поставщика?")


if(percent1=='') percent1=0;
if(percent2=='') percent2=0;

percent1=parseFloat(percent1);
percent2=parseFloat(percent2);

var q =  query.newinstance();
var qUpd=query.newinstance();


ch = q.select("select invcitem.*, items.item_price "+
", item_code, item_name, item_price_discount, item_price_vendor "+
" from invcitem "+
" left outer join items on items._id=invcitem_item_id "+
"  where invcitem_invoice_id="+doc.id()+
" order by invcitem_linenumber");
if(!ch){
	throw new Error('Error getting order items!\n'+q.lasterror());
}
mess.show();
doc.outopen();
 doc.wl('&lt;html>');
 doc.wl('&lt;head>');
 doc.wl('&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" />');
 doc.wl('&lt;/head>');
 doc.wl('&lt;body style="font-size:10pt" >');
 doc.wl('&lt;p>Переоценка товаров для пунктов заказа. &lt;/p>'); 
 doc.wl('&lt;table border="1" align="left" width="100%" cellspacing="0" cellpadding="4" style="border-style:solid;border-width:1px;border-color:black;">');

doc.wl('&lt;tr>');
doc.wl('&lt;th rowspan="2">Код&lt;/th>');
doc.wl('&lt;th rowspan="2">Товар&lt;/th>');
doc.wl('&lt;th colspan="2">Розничная цена&lt;/th>');
doc.wl('&lt;th colspan="2">Цена со скидкой&lt;/th>');
if(isUpdBuy) doc.wl('&lt;th colspan="2">Цена поставщика&lt;/th>');
doc.wl('&lt;/tr>');
doc.wl('&lt;tr>');
doc.wl('&lt;th>Старая цена&lt;/th>');
doc.wl('&lt;th>Новая цена&lt;/th>');
doc.wl('&lt;th>Старая цена&lt;/th>');
doc.wl('&lt;th>Новая цена&lt;/th>');
if(isUpdBuy){
	doc.wl('&lt;th>Старая цена&lt;/th>');
	doc.wl('&lt;th>Новая цена&lt;/th>');
}
doc.wl('&lt;/tr>');
do{
 if(mess.iscancel()) break;

	price=q.getfloat('invcitem_price');
	old1=q.getfloat('item_price');
	price1= price+price*percent1/100;
	price1=Math.round(price1/10+0.5)*10;

	old2=q.getfloat('item_price_discount');
	price2= price+price*percent2/100;
	price2=Math.round(price2/10+0.5)*10;

	old3=q.getfloat('item_price_vendor');
	price3= price;

		

s="update items set item_price="+price1+",item_price_discount="+price2;
if(isUpdBuy){
	s+=",item_price_vendor="+price;
}
s+=" where _id="+q.value('invcitem_item_id');
	
doc.wl('&lt;tr>'); 
ok= qUpd.exec(s);
  if(!ok){ 
	 doc.wl('&lt;td colspan="4">'+qUpd.lasterror()+'&lt;/td>');
  }
  else{
  	doc.wl('&lt;td>'+q.value('item_code')+'&lt;/td>');
  	doc.wl('&lt;td>'+q.value('item_name')+'&lt;/td>');
  	doc.wl('&lt;td align="right">'+old1+'&lt;/td>');
  	doc.wl('&lt;td align="right">'+price1+'&lt;/td>');
  	doc.wl('&lt;td align="right">'+old2+'&lt;/td>');
  	doc.wl('&lt;td align="right">'+price2+'&lt;/td>');
	if(isUpdBuy){
		doc.wl('&lt;td align="right">'+old3+'&lt;/td>');
  		doc.wl('&lt;td align="right">'+price+'&lt;/td>');
	}

  }

  doc.wl('&lt;/tr>');
 	
 }while(q.next());




q.close();


 doc.wl('&lt;/table>&lt;/body>&lt;/html>');
 doc.outclose();
 mess.close();
 doc.open();
</FormScript>
    </Form>


    <Form>
        <FormName>Чек на чековый принтер (при проведении продажи)</FormName>
        <FormType>15</FormType>
        <FormScript>mess.show();
var qInv =  query.newinstance();
var q =  query.newinstance();
var qPartner =  query.newinstance();
var qOrg =  query.newinstance();

if (!qInv.select("select * from invoices where _id="+doc.id()) || !qInv.first()){
	 mess.close();
	throw new Error('Не найден заказ!');
}

if(qInv.value("inv_type")==1 &amp;&amp; qInv.value("inv_accept")==0){ //for sale and not accepted

qOrg.select("select * from accounts where _id="+qInv.value("inv_org_id"));
qPartner.select("select * from accounts where _id="+qInv.value("inv_partner_id"));


doc.outopen();

doc.wl('&lt;html>');
doc.wl('&lt;header>');
doc.wl('&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" />');
doc.wl('&lt;/header>');
doc.wl('&lt;BODY STYLE="background: #ffffff; margin: 0; font-family: Arial; font-size: 10pt; font-style: normal; ">');
doc.wl('&lt;p>');


doc.wl(qOrg.value('account_name')+'&lt;br />');
doc.wl('ИНН: '+qOrg.value('account_inn')+'&lt;br />');

doc.wl(qOrg.value('account_address')+'&lt;br />');
doc.wl('&lt;/p>');
doc.wl('&lt;p>');
doc.wl('Чек № '+qInv.value('inv_num')+' '+f.dateinwords(qInv.value('inv_date'))+'&lt;br />');

doc.wl('ПРОДАЖА');
doc.wl('&lt;/p>');

ch = q.select("select invcitem.*, items.item_name, items.item_code, items.item_price, items.item_vat, unit_name from invcitem "+
" left outer join items on items._id = invcitem.invcitem_item_id "+
" left outer join units on units._id = items.item_unit_id "+
" where invcitem_invoice_id="+qInv.value("_id")+" order by invcitem_linenumber");

var i=1;


var total=0;
var totalNoVat=0;
var totalVat=0;
if( q.first()){
	do{
		if(mess.iscancel()) break;

		qty=q.getfloat('invcitem_qty');
 		price=q.getfloat('invcitem_price');
 		sum = q.getfloat('invcitem_total');

 		vat = q.getfloat('item_vat');

 		priceNoVat = q.getfloat('invcitem_price_novat');
 		vatInPrice = price - priceNoVat;
 		totalNoVat+=q.getfloat('invcitem_total_novat');

 		totalVat+=sum-q.getfloat('invcitem_total_novat');
 		total+=sum;


		doc.wl(q.value('item_name')+'&lt;br />');
		strout=f.fmt(qty)+'*'+f.sum(price)+' ='+f.sum(sum);
		doc.wl('&lt;b>'+strout+'&lt;/b>&lt;br />');


 		mess.say(q.getstring('item_name'));
	}while(q.next());
}

doc.wl('----------------------&lt;br />');
doc.wl('Итого:   &lt;B>'+f.sum(total)+'&lt;/B>&lt;br />');
doc.wl('  м.п.&lt;br />');
doc.wl('Подпись ______________&lt;br />');
doc.wl('&lt;/body>');
doc.wl('&lt;/html>');
doc.outclose();
mess.close();
doc.open();
}</FormScript>
    </Form>



</Forms>
