unit MyIPDReport;
procedure main;
var d1, d2: tdatetime;
people_distinct_count1: integer;
people_count1: integer;
ds1, ds2: string;
pttype_list1: string;
pttype, pttype_name: string;
hospcode_list: string;
money1: currency;
i: integer;
trd, tc, tsc: tclientdataset;
fonlineid: string;
begin
if not getdaterange() then exit;
d1 := date_result1();
d2 := date_result2();
fonlineid := report_value(1);
ds1 := formatdatetime('yyyy-mm-dd', d1);
ds2 := formatdatetime('yyyy-mm-dd', d2);
trd := tclientdataset.create(nil);
tc := tclientdataset.create(nil);
tsc := tclientdataset.create(nil);
trd.data := HOSxP_GetDataset('select distinct rxdate from opitemrece where rxdate between "' + ds1 + '" and "' + ds2 + '" ');
tc.data := HOSxP_GetDataset('select d.icode,d.name,d.strength,d.units ,d.packqty as vn_count,d.packqty as qty_count,d.unitprice as unitprice,d.unitprice as saleprice ,d.unitprice as sum_price ,d.unitcost as unitcost from drugitems d');
tc.indexfieldnames := 'icode';
tc.first;
while not tc.eof do
begin
tc.edit;
tc.fieldbyname('vn_count').asinteger:=0;
tc.fieldbyname('qty_count').asinteger :=0;
tc.fieldbyname('sum_price').asfloat := 0;
tc.fieldbyname('saleprice').asfloat := 0;
tc.post;
tc.next;
end;
while not trd.eof do
begin
setstatuslabel('Processing...' + formatdatetime('d mmm eeee', trd.fieldbyname('rxdate').asdatetime));
tsc.data := HOSxP_GetDataset('select a.icode,sum(a.qty) as qty_count,sum(a.sum_price) as sum_price ,(sum(a.qty)*b.unitcost) as sumunitcost from opitemrece a '+
' left outer join drugitems b on a.icode=b.icode where rxdate = "' +
formatdatetime('yyyy-mm-dd', trd.fieldbyname('rxdate').asdatetime) + '" group by icode');
while not tsc.eof do
begin
if tc.findkey([tsc.fieldbyname('icode').asstring]) then
begin
setstatuslabel('Processing...' + formatdatetime('d mmmmm eeee', trd.fieldbyname('rxdate').asdatetime)+' ÃËÑÊÂÒ '+tsc.fieldbyname('icode').asstring +' ª×èÍ '+
tc.fieldbyname('name').asstring+' '+tc.fieldbyname('strength').asstring+' '+tc.fieldbyname('units').asstring);
tc.edit;
tc.fieldbyname('qty_count').asinteger := tc.fieldbyname('qty_count').asinteger + tsc.fieldbyname('qty_count').asinteger;
tc.fieldbyname('sum_price').asfloat := tc.fieldbyname('sum_price').asfloat + tsc.fieldbyname('sum_price').asfloat;
tc.fieldbyname('saleprice').asfloat := tc.fieldbyname('saleprice').asfloat + tsc.fieldbyname('sumunitcost').asfloat;
tc.fieldbyname('vn_count').asinteger := tc.fieldbyname('vn_count').asinteger+
getsqldata('select count(distinct vn) as cc from opitemrece where rxdate = "'+
formatdatetime('yyyy-mm-dd',trd.fieldbyname('rxdate').asdatetime)+'" and icode = "'+tsc.fieldbyname('icode').asstring+'" and vn is not null and vn <> "" ') +
getsqldata('select count(distinct order_no) as cc from opitemrece where rxdate = "'+
formatdatetime('yyyy-mm-dd',trd.fieldbyname('rxdate').asdatetime)+'" and icode = "'+tsc.fieldbyname('icode').asstring+'" and an is not null and an <> "" ');
tc.post;
end;
tsc.next;
end;
trd.next;
end;
tc.first;
while not tc.eof do
begin
if tc.fieldbyname('qty_count').asinteger = 0 then tc.delete else tc.next;
tc.next;
end;
tc.first;
zquery.close;
zquery.sql.text := 'delete from tempreport where id = ''' + fonlineid + '''';
zquery.execsql;
zquery.sql.text := 'select * from tempreport where id = ''' + fonlineid + '''';
zquery.open;
while not tc.eof do
begin
zquery.insert;
zquery['id'] := fonlineid;
zquery['reportname'] := 'CUSTOM-RG20';
zquery['name1'] := tc.fieldbyname('name').asstring ;
zquery['name2']:=tc.fieldbyname('strength').asstring;
zquery['name3']:=tc.fieldbyname('units').asstring;
zquery.fieldbyname('mon1').asfloat := tc.fieldbyname('qty_count').asinteger;
zquery.fieldbyname('mon2').asfloat := tc.fieldbyname('sum_price').asfloat;
zquery.fieldbyname('mon3').asfloat := tc.fieldbyname('vn_count').asinteger;
zquery.fieldbyname('mon4').asfloat := tc.fieldbyname('unitcost').asfloat;
zquery.fieldbyname('mon5').asfloat := tc.fieldbyname('saleprice').asfloat;
zquery.fieldbyname('mon6').asfloat := tc.fieldbyname('unitprice').asfloat;
zquery.fieldbyname('date1').asdatetime := d1;
zquery.fieldbyname('date2').asdatetime := d2;
if zquery.fieldbyname('mon1').asfloat >0 then
zquery.post else
zquery.cancel;
tc.next;
end;
trd.free;
tc.free;
tsc.free;
setstatuslabel('Done.');
end;
end.