unit Upf;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Grids, DBGridEh, ComCtrls, Buttons, 
  DBLookupEh,upreview,udm, DBSumLst, DBCtrlsEh, sncCurrency, Mask;

type
  Tfpf = class(TForm)
    DBGridEh1: TDBGridEh;
    Label1: TLabel;
    TabControl1: TTabControl;
    TabControl2: TTabControl;
    Label2: TLabel;
    Label3: TLabel;
    dw: TDBLookupComboboxEh;
    Label4: TLabel;
    Edit1: TEdit;
    Label5: TLabel;
    Label6: TLabel;
    ren: TDBLookupComboboxEh;
    Edit2: TEdit;
    print: TCheckBox;
    save: TBitBtn;
    ret: TBitBtn;
    Label7: TLabel;
    Label8: TLabel;
    sum1: TDBSumList;
    sncCurrencyLabel1: TsncCurrencyLabel;
    sf: TsncCurrencyEdit;
    zq: TsncCurrencyEdit;
    BitBtn3: TBitBtn;
    procedure FormShow(Sender: TObject);
    procedure DBGridEh1ColExit(Sender: TObject);
    procedure saveClick(Sender: TObject);
    procedure retClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure DBGridEh1KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure sfExit(Sender: TObject);
    procedure DBGridEh1ColEnter(Sender: TObject);
    procedure dwChange(Sender: TObject);
    procedure dwExit(Sender: TObject);
    procedure renChange(Sender: TObject);
    procedure BitBtn3Click(Sender: TObject);
    procedure renExit(Sender: TObject);
  private
    function  check():boolean;

    { Private declarations }
  public
    { Public declarations }
  end;
  var dwfilter:string;


implementation

{$R *.dfm}

procedure Tfpf.FormShow(Sender: TObject);
begin
 // showmessage(inttostr(height));
  try
  adodm.tcr.Close;
  adodm.tcr.Open;
  except
  showmessage('mistake;');
  end;
  dwfilter:= ' 出入<>1 and clientid>=0';
  label3.caption:=datetimetostr(date());
  adodm.tdw.Filter:=' 出入<>1 and clientid>=0' ;
  adodm.tdw.Filtered:=true;
  dw.Text:='顾客';
end;

procedure Tfpf.DBGridEh1ColExit(Sender: TObject);
begin
 if (trim(adodm.tcr.FieldByName('ypid').AsString)<>'') and (trim(adodm.tcr.FieldByName('数量').AsString)<>'')  then

   if  adodm.tkc.Locate('ypid',adodm.tcr.Fieldvalues['ypid'],[])  then
      begin
       if adodm.tkc.FieldByName('数量').AsInteger<adodm.tcr.FieldByName('数量').AsInteger then
        begin
          showmessage('库存数量:'+adodm.tkc.FieldByName('数量').Asstring+'    不足以支付');
          adodm.tcr.Edit;
          adodm.tcr.FieldByName('数量').AsString:='';
          dbgrideh1.col:=2;
        end
        else
        begin
        if  adodm.typzdk.Locate('id',adodm.tcr.Fieldvalues['ypid'],[]) then
        begin
        adodm.tcr.edit;
        adodm.tcr.FieldByName('单价').Asfloat:=adodm.typzdk.fieldbyname('批发价').asfloat
        end
        else showmessage('发生错误，请手工录入该商品的单价');
        end
      end
   else
    begin
    showmessage('该药品没有库存');
    adodm.tcr.Edit;
    adodm.tcr.FieldByName('药品简码').AsString:='';
    dbgrideh1.col:=1;
   end;


  if (adodm.tcr.FieldByName('单价').AsString<>'') and  (adodm.tcr.FieldByName('数量').Asstring<>'') then
    begin
            adodm.tcr.edit;
    adodm.tcr.FieldByName('金额').AsFloat:=adodm.tcr.FieldByName('数量').Asinteger*adodm.tcr.FieldByName('单价').Asfloat ;
    end  ;



end;




function Tfpf.check: boolean;
begin
  result:=true;
 if trim(dw.Text)='' then
   begin
    showmessage('请选择一个购药单位，如果没有出现在列表中，请添加！');
    result:=false;
    exit;
    end;
 if trim(ren.Text)='' then
   begin
    showmessage('请选择开票人，如果没有出现在列表中，请添加！');
    result:=false;
    exit;
    end;

 if adodm.tcr.RecordCount<=0 then
   begin
    showmessage('请录入销售的药品信息！');
    result:=false;
    exit;
   end;
end;

procedure Tfpf.saveClick(Sender: TObject);
begin
 
  if not check then
  exit;
  adodm.ctemp.Parameters.ParamValues['@lx']:='pf';
  adodm.ctemp.Execute;
  edit2.Text:=adodm.ctemp.Parameters.ParamValues['@bh'];
    {GETbh}
  adodm.tcr.First;
  while not adodm.tcr.Eof do
  begin
    adodm.tcr.Edit;
    adodm.tcr.FieldByName('编号').AsString:=trim(edit2.Text);
    if trim(dw.Text)<>'' then
    adodm.tcr.FieldByName('入库单位').AsInteger:=adodm.tdw.fieldbyname('clientid').AsInteger;
    
    adodm.tcr.FieldByName('开票人').Asinteger:=adodm.tyg.fieldbyname('ygid').Asinteger;
    adodm.tcr.FieldByName('操作人').Asstring:=trim(edit1.Text);
    adodm.tcr.FieldByName('类型').Asstring:='批发';
    adodm.tcr.fieldbyname('出入').asboolean:=false;
    adodm.tcr.FieldByName('日期').Asdatetime:=date;
    
    if adodm.tkc.Locate('ypid',adodm.tcr.FieldValues['ypid'],[]) then
    begin
      adodm.tkc.Edit;
      adodm.tkc.FieldByName('数量').AsInteger:=adodm.tkc.FieldByName('数量').AsInteger-adodm.tcr.FieldByName('数量').AsInteger;
      adodm.tkc.FieldByName('购进金额').Asfloat:=adodm.tkc.FieldByName('购进单价').Asfloat*adodm.tkc.FieldByName('数量').Asfloat;
      adodm.tcr.FieldByName('成本金额').AsFloat:=adodm.tkc.FieldByName('购进单价').Asfloat*adodm.tcr.FieldByName('数量').Asfloat;
      adodm.tkc.Post;
    end  ;

   adodm.tcr.Next;
   end;
   adodm.tcr.UpdateBatch();
   dbgrideh1.Enabled:=false;
   if print.Checked then
   begin
   fpreview.frrk.FindObject('memo27').Memo.Text:='开票人：'+ren.text;
   fpreview.frrk.ShowReport;
   fpreview.ShowModal;
   fpreview.frrk.FindObject('memo27').Memo.Text:='经办人（签字）：'  ;
   end;
   save.Enabled:=false;




end;

procedure Tfpf.retClick(Sender: TObject);
begin
 dbgrideh1.Enabled:=true;
 adodm.tcr.close;
 adodm.tcr.Open;
 edit2.Text:='';
 save.Enabled:=true;
 sf.Value:=0;
 zq.Value:=0;
 dw.Text:='顾客';
end;

procedure Tfpf.FormClose(Sender: TObject; var Action: TCloseAction);
begin
try
adodm.tcr.CancelBatch();
except
showmessage('取消失败');
end;
try
adodm.tcr.Close;
except
showmessage('关闭失败');
end;
action:=cafree;
end;

procedure Tfpf.DBGridEh1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
if (key=vk_down) and adodm.tcr.Eof   then
BEGIN
 sendmessage(dbgrideh1.Handle,wm_keydown,vk_tab,0); 
 adodm.tcr.Append;
 dbgrideh1.Col:=1;
end;
end;

procedure Tfpf.sfExit(Sender: TObject);
var aa:real;
begin

sum1.Activate(true);
//showmessage(floattostr(sum1.SumCollection[0].SumValue));
zq.Value:=0;
if sf.Value=0 then
begin
exit;
sum1.Active:=false;
end;
try
  aa:=sf.Value;
  aa:=aa-sum1.SumCollection[0].SumValue;
  if aa<0 then
  begin
  showmessage('所付金额不足！！');
  sf.SetFocus;
  end
  else
  zq.value:=aa;

except
  sf.SetFocus;
end;
 sum1.Active:=false;

end;

procedure Tfpf.DBGridEh1ColEnter(Sender: TObject);
begin
sf.Value:=0;
zq.Value:=0;
end;

procedure Tfpf.dwChange(Sender: TObject);
begin
if trim(dw.Text)<>'' then
 begin
  adodm.tdw.DisableControls;
  adodm.tdw.filtered:=false;
  adodm.tdw.Filter:='(简码 like '''+trim(dw.text)+'%'' and '+dwfilter+') or (名称 like '''+trim(dw.text)+'%'' and '+dwfilter+')';
  //showmessage(adodm.tdw.Filter);
  adodm.tdw.Filtered:=true;
  adodm.tdw.EnableControls;
 end
else
  adodm.tdw.Filter:=dwfilter;
  adodm.tdw.Filtered:=true;
end;

procedure Tfpf.dwExit(Sender: TObject);
begin
if not adodm.tdw.Locate('名称',dw.Text,[]) then
begin
  dw.SetFocus;
  showmessage('没有此单位，请重新录入！');
end;
end;

procedure Tfpf.renChange(Sender: TObject);
begin
if trim(ren.Text)<>'' then
 begin
  adodm.tyg.DisableControls;
  adodm.tyg.filtered:=false;
  adodm.tyg.Filter:=' 姓名 like '''+trim(ren.text)+'%'' or '+'员工编号 like '''+trim(ren.text)+'%''';
  //showmessage(adodm.tdw.Filter);
  adodm.tyg.Filtered:=true;
  adodm.tyg.EnableControls;
 end
else
  adodm.tyg.Filtered:=false;

end;

procedure Tfpf.BitBtn3Click(Sender: TObject);
begin
close;
end;
procedure Tfpf.renExit(Sender: TObject);
begin
 if not adodm.tyg.Locate('姓名',ren.Text,[]) then
begin
  ren.SetFocus;
  showmessage('没有此人，请重新录入！');
end;
end;

end.
