-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterface3.sql
More file actions
executable file
·358 lines (293 loc) · 11 KB
/
Copy pathinterface3.sql
File metadata and controls
executable file
·358 lines (293 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
concept interface;
define 'procedure', 'interface.debt';
define 'procedure', 'interface.debt_accept';
define 'procedure', 'interface.proce';
define 'service', 'xml';
if not exists (select * from systable where table_name='agent' and user_name(creator)='interface') then
create table interface.agent(
id int default autoincrement, username varchar(128) not null,
foreign key ( palm_salesman ) references dbo.palm_salesman on delete set null,
created datetime default now(), ts timestamp default timestamp,
primary key (id)
);
end if;
if not exists (select * from interface.agent where username='sasha') then
insert into interface.agent (username, palm_salesman) values ('sasha',965);
commit
end if;
if not exists (select * from interface.agent where username='g.vyazova') then
-- insert into interface.agent (username, palm_salesman) values ('g.vyazova',802);
commit
end if;
if not exists (select * from systable where table_name='request' and user_name(creator)='interface') then
create table interface.request(
id uniqueidentifier default newid(), type varchar(32) default 'get', request xml, response xml,
created datetime default now(), ts timestamp default timestamp,
primary key (id)
);
create trigger interface.ti_request before insert on interface.request
referencing new as inserted
for each row
begin
return;
end
end if;
alter trigger interface.ti_request before insert on interface.request
referencing new as inserted
for each row
begin
declare @result xml;
declare @sql text;
declare @view varchar(128);
savepoint;
body: begin
select name,
string('set @result=', name, '.', inserted.type,'(',if data is null then 'null' else ''''+data+'''' endif,')')
into @view, @sql
from openxml (inserted.request,'/*') with (name varchar(128) '@mp:localname', data xml '*:data/@mp:xmltext')
;
execute immediate @sql;
exception when others then
set @result=
xmlelement('exception',
xmlelement('ErrorText',errormsg())
,xmlelement('SQLSTATE',SQLSTATE)
,xmlelement('SQL', @sql)
,xmlelement('Request', inserted.request)
);
rollback to savepoint;
end;
set inserted.response=
xmlelement (@view,
xmlattributes ('http://unact.ru/xmlns/xmlinterface' as "xmlns",
'http://unact.ru/xmlns/xmlinterface' as "xmlns:xi"
),
@result
)
end;
alter procedure interface.proce (in @type varchar(10) default 'get', in @data xml default null)
begin
call dbo.sa_set_http_header( 'Content-Type', 'text/xml' );
call sa_set_http_option ( 'CharsetConversion', 'off');
case @type
when 'get' then
select interface.debt ();
when 'put' then
select interface.debt_accept (@data)
else
select xmlelement('exception',
xmlconcat(xmlelement('ErrorText','Use /get or /put')
)
);
end case;
end;
alter service xml
type 'raw'
authorization off user dba
url elements
as call interface.proce(:url1, :data)
;
alter function interface.debt_accept (in @data xml)
returns xml
begin
declare @result xml;
declare @intid varchar(128);
savepoint;
body: begin
declare @res int;
declare @xml xml;
declare @pays int;
for debt as debt cursor for
select *
from openxml(@data, '//*:data//*:debt[@modified]')
with (debt_id int '@id', cash varchar(32) '@cash',
ndoc varchar(64) '@ndoc', doc_id int '@doc_id',
cdesk int '../../@srv_cashdesk', cash_id int '@cash_id',
@status1 int '@status1', @intid_local varchar(128) '@intId')
do
set @intid=@intid_local;
set cash_id=nullif(cash_id,0);
case
when cash_id is null and cash>0 then
set cash_id=idgenerator('cassa_income')
;
insert into cassa_income with auto name
select cash_id id, $partner.anyCashclient(partner) client,
cash summ, 0 currency,
string('Îïëàòà òîâàðà íàêë. ',ndoc,' îò ', date(payspt.ddate)) info,
1 "number", cdesk, 0 pay_type, today() as ddate, payspt.org as org,
nullif(@status1,0) as status1
from payspt
where payspt.id=debt_id
;
when cash_id is not null and cash>0 then
delete ptplink where pay_id=(select pays from cassa_income where id=cash_id)
;
update cassa_income set summ=cash, status1=isnull(@status1,0)
where id=cash_id
;
when cash_id is not null and isnull(cash,0)=0 then
delete cassa_income
where id=cash_id
;
set @result=xmlconcat(@result,xmlelement ('deleted', xmlattributes (@intid as intId, '' as cash_id)))
;
end case
;
set @result=xmlconcat(@result,(select @intId as intId, cash_id from cassa_income modified where id=cash_id for xml auto))
;
select paidpt.id into @xml
from paidpt join recgoods on pays=paidpt.id and recgoods.id=doc_id for xml auto
;
select pays into @pays from cassa_income where id=cash_id;
set @res=$ptpays.link(@pays,xmlelement('debts',@xml));
if exists (select * from ptpaid join cassa_income ci on ci.pays=ptpaid.id where ci.id=cash_id) then
set @result=
xmlelement('exception',xmlattributes (@intid as "intId"),
xmlconcat(xmlelement('ErrorText',string('Çàäîëæåííîñòü ïî íàêëàäíîé ¹ ', ndoc, ' ìåíüøå ïîëó÷åííîé ñóììû ', cash))
)
)
;
rollback to savepoint;
leave body;
end if;
end for;
set @result=xmlelement ('accepted',@result);
exception
when others then
set @result=
xmlelement('exception',xmlattributes (@intid as "intId"),
xmlconcat(xmlelement('ErrorText',errormsg())
,xmlelement('SQLSTATE',SQLSTATE)
)
);
rollback to savepoint;
end;
return xmlelement ('response',
xmlattributes ('http://unact.ru/database' as "xmlns",
'http://www.w3.org/2001/XMLSchema' as "xmlns:xs"
),
@result
)
end;
alter function interface.debt (in @request xml)
returns xml
begin
declare @result xml;
body: begin
declare @meta xml;
declare @cdesk int;
declare @ddate smalldatetime;
declare @pgroup int;
declare @salesman integer;
declare @agent_username varchar(128);
set @agent_username=_xml.PathContent(@request,'/*/@username/text()');
if @agent_username is null then
set @result=
xmlelement('exception',xmlattributes ('http://unact.ru/database' as "xmlns"),
xmlconcat(xmlelement('ErrorText','Agent not specified')
)
);
leave body
end if;
select palm_salesman into @salesman from interface.agent where username=@agent_username;
if @salesman is null then
set @result=
xmlelement('exception',xmlattributes ('http://unact.ru/database' as "xmlns"),
xmlconcat(xmlelement('ErrorText','Äëÿ ðàáîòû èíòåðôåéñà òðåáóåòñÿ ïðîèçâåñòè íàñòðîéêó - íå çàäàí òîðãîâûé ïðåäñòàâèòåëü äëÿ ïîëüçîâàòåëÿ '+@agent_username)
)
);
leave body
end if;
select srv_cashdesk, today() , srv_pgroup, salesman_id
into @cdesk,@ddate, @pgroup, @salesman
from palm_salesman
where salesman_id=@salesman
;
set @meta='
<xs:schema>
<xs:element name="salesman">
<xs:complexType>
<xs:all>
<xs:element name="buyer">
<xs:complexType>
<xs:all>
<xs:element name="debt">
<xs:complexType>
<!-- xs:attribute name="id" type="xs:integer" /-->
<xs:attribute name="ndoc" type="xs:string" caption="Íàêëàäíàÿ ¹"/>
<xs:attribute name="ddate" type="xs:date" caption="Äàòà" />
<xs:attribute name="summ" type="xs:decimal" class="number" caption="Çàäîëæåííîñòü" />
<xs:attribute name="cash" type="xs:decimal" class="number" caption="Îïëà÷åíî" editable="true" />
<xs:attribute name="status1" type="xs:boolean" class="boolean" caption="×åê" editable="true" />
</xs:complexType >
</xs:element>
</xs:all>
<xs:attribute name="name" type="xs:string" />
<xs:attribute name="address" type="xs:string" />
</xs:complexType>
</xs:element>
</xs:all>
<xs:attribute name="name" type="xs:string" />
</xs:complexType>
</xs:element>
</xs:schema>
';
set temporary option for_xml_null_treatment='empty';
with
debt as
(select recept.ndoc, date(recept.ddate) ddate, recept.client,
isnull(cast(sum(summ) as decimal(18,2)),0)+isnull(cash,0) summ, min(predebt.id) id,
predebt.doc_id, cast(sum(cash) as decimal(18,2)) cash, min(cash_id) cash_id, min(status1) status1
from recept join
(select paidpt.csum as summ, paidpt.id,
recgoods.id doc_id, null cash, null cash_id, null status1
from recgoods
join paidpt on paidpt.id=recgoods.pays
join partners on partners.id=paidpt.partner
where partners.parent=@pgroup
union all
select null, null,
min(recgoods.id), ci.summ, ci.id, ci.status1
from cassa_income ci
join ptplink on ptplink.pay_id=ci.pays
join recgoods on ptplink.upay_id=recgoods.pays
where ci.ddate=today() and ci.cdesk=@cdesk
group by ci.id, ci.summ, ci.status1
having count(distinct recgoods.id) = 1
) as predebt on predebt.doc_id=recept.id
group by recept.ndoc, recept.ddate, recept.client, predebt.doc_id
)
select
salesman.name, salesman.srv_cashdesk,
buyer.name, buyer.loadto address, buyer.id, buyer.partner,
debt.ndoc, debt.ddate, debt.summ, debt.id,
debt.cash, debt.cash_id, debt.doc_id, debt.status1
into @result
from debt
join buyers buyer on buyer.id=debt.client
cross join palm_salesman salesman
where salesman.salesman_id=@salesman
order by 1,2,3,4,5 desc
for xml auto
;
set @result=xmlconcat(
xmlelement ('metadata',@meta),
xmlelement ('data',@result)
);
exception when others then
set @result=
xmlelement('exception',
xmlconcat(xmlelement('ErrorText',errormsg())
,xmlelement('SQLSTATE',SQLSTATE)
)
);
end;
return xmlelement ('response',
xmlattributes ('http://unact.ru/database' as "xmlns",
'http://www.w3.org/2001/XMLSchema' as "xmlns:xs",
'http://unact.ru/database' as "xmlns:db"
),
@result
)
end;