unit U_YGYPInPut_CLSQDJ;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, cxGridLevel, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxClasses, cxControls,
  cxGridCustomView, cxGrid, cxMemo, cxRichEdit, ComCtrls, cxContainer,
  cxTextEdit, cxMaskEdit, cxButtonEdit, StdCtrls, ToolWin, DBClient, ADODB,
  ExtCtrls, BtnEdit, cxCalendar,StrUtils, cxDropDownEdit,jpeg,
  IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdFTP, cxPC,
  cxGridCustomPopupMenu, cxGridPopupMenu, Menus, ExtDlgs;

type
  TfrmYGYPInPut_CLSQDJ = class(TForm)
    ToolBar1: TToolBar;
    TBClose: TToolButton;
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    TBSave: TToolButton;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    DataSource1: TDataSource;
    Order_Sub: TClientDataSet;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    ADOQuery2: TADOQuery;
    TADD: TToolButton;
    TDel: TToolButton;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1SQDate: TcxGridDBColumn;
    v1SQName: TcxGridDBColumn;
    v1CLName: TcxGridDBColumn;
    v1CLNO: TcxGridDBColumn;
    v1CCMoney: TcxGridDBColumn;
    v1FeeType: TcxGridDBColumn;
    v1TOPlace: TcxGridDBColumn;
    v1SQNote: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure v1BGUnitPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure TaddClick(Sender: TObject);
    procedure TDelClick(Sender: TObject);
    procedure v1CLNamePropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1TOPlacePropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
  private
    TPInt:Integer;
    procedure InitData();
    function  SaveData():Boolean;
    { Private declarations }
  public
    PState,CopyInt:Integer;
    FCLID:String;
    { Public declarations }
  end;

var
  frmYGYPInPut_CLSQDJ: TfrmYGYPInPut_CLSQDJ;
implementation
uses
  U_DataLink,U_ZDYHelp,U_RTFun,U_ZDYHelpSel,U_YGYPZZChkList_CLDJ;

{$R *.dfm}

procedure TfrmYGYPInPut_CLSQDJ.TBCloseClick(Sender: TObject);
begin
  Close;
end;

procedure TfrmYGYPInPut_CLSQDJ.InitData();
begin
  with ADOCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from OA_YG_CLGLSQ  ');
    sql.add('where CLSQID='''+trim(FCLID)+'''');
    Open;
  end;
  SCreateCDS20(ADOCmd,Order_Sub);
  SInitCDSData20(ADOCmd,Order_Sub);
end;

procedure TfrmYGYPInPut_CLSQDJ.FormShow(Sender: TObject);
begin
  InitData();
end;

function TfrmYGYPInPut_CLSQDJ.SaveData():Boolean;
var
  maxno:String;
begin
  Result:=False;
  try
    ADOCmd.Connection.BeginTrans;
    with Order_Sub do
    begin
      first;
      while not eof do
      begin
        if trim(FCLID)='' then
        begin
          if GetLSNo(ADOTemp,FCLID,'CS','OA_YG_CLGLSQ',4,1)=False then
          begin
            ADOCmd.Connection.RollbackTrans;
            Application.MessageBox('取最大号失败!','提示',0);
            Exit;
          end;
        end;
        with ADOCmd do
        begin
          Close;
          sql.Clear;
          SQL.Add('select * from OA_YG_CLGLSQ where CLSQID='''+Trim(FCLID)+'''');
          Open;
          if isempty then
          begin
            append;
            fieldbyname('SQStatus').Value:='未提交';
            fieldbyname('filler').AsString:=trim(DName);
            fieldbyname('filltime').AsDateTime:=SGetServerDate(ADOTemp);
          end
          else
          edit;
          fieldbyname('CLSQID').Value:=trim(FCLID);
          fieldbyname('CLID').Value:=trim(Order_Sub.fieldbyname('CLID').AsString);
          fieldbyname('CCMoney').AsFloat:=Order_Sub.fieldbyname('CCMoney').AsFloat;
          fieldbyname('LCQty').value:=order_Sub.fieldbyname('LCQty').asfloat;
          RTSetSaveDataCDS(ADOCmd,Tv1,Order_Sub,'OA_YG_CLGLSQ',0);
          post;
        end;
        next;
      end;
    end;
    ADOCmd.Connection.CommitTrans;
    Result:=True;
  except
    Result:=False;
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！','提示',0);
  end;
end;

procedure TfrmYGYPInPut_CLSQDJ.TBSaveClick(Sender: TObject);
var
  FSFNO:String;
begin
  ToolBar1.SetFocus;
  if order_Sub.isempty then
  begin
    Application.MessageBox('明细不能为空','提示',0);
    Exit;
  end;
  if SaveData() then
  begin
    Application.MessageBox('保存成功！','提示',0);
    Modalresult:=1;
  end;
end;

procedure TfrmYGYPInPut_CLSQDJ.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  Action:=caFree;
end;

procedure TfrmYGYPInPut_CLSQDJ.FormDestroy(Sender: TObject);
begin
  frmYGYPInPut_CLSQDJ:=nil;
end;

procedure TfrmYGYPInPut_CLSQDJ.v1BGUnitPropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  frmZDYHelp:=TfrmZDYHelp.Create(self);
  with frmZDYHelp do
  begin
    flag:='BGUnit';
    flagname:='数量单位';
    if showModal=1 then
    begin
      with Order_Sub do
      begin
        edit;
        fieldbyname('BGUnit').Value:=trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
    free;
  end;
end;

procedure TfrmYGYPInPut_CLSQDJ.TaddClick(Sender: TObject);
begin
  frmYGYPZZChkList_CLDJ:=TfrmYGYPZZChkList_CLDJ.create(self);
  with frmYGYPZZChkList_CLDJ do
  begin
    CanShu1:='查询';
    if showModal=1 then
    begin
      with self.Order_Sub do
      begin
        append;
        fieldbyname('SQDate').AsDateTime:=now;
        fieldbyname('CLName').Value:=trim(Order_Main.fieldbyname('CLName').AsString);
        fieldbyname('CLNO').Value:=trim(Order_Main.fieldbyname('CLNO').AsString);
        fieldbyname('CLID').Value:=trim(Order_Main.fieldbyname('CLID').AsString);
        fieldbyname('SQName').Value:=trim(DName);
        post;
      end;
    end;
    free;
  end;
end;

procedure TfrmYGYPInPut_CLSQDJ.TDelClick(Sender: TObject);
begin
  if Order_Sub.IsEmpty then exit;
  if trim(Order_Sub.fieldbyname('CLSQID').AsString)<>'' then
  begin
    with ADOCmd do
    begin
      close;
      sql.Clear;
      sql.Add('delete from OA_YG_CLGLSQ ');
      sql.add('where CLSQID='''+trim(Order_Sub.fieldbyname('CLSQID').AsString)+'''');
      execsql;
    end;
    with ADOQuery2 do
    begin
      close;
      sql.Clear;
      sql.add('update OA_YG_CLGL set ');
      sql.add('CLStatus=''未使用'' ');
      sql.Add(' where CLID='''+trim(Order_Sub.fieldbyname('CLID').AsString)+'''');
      execsql;
    end;
  end;
  Order_Sub.Delete;
end;

procedure TfrmYGYPInPut_CLSQDJ.v1CLNamePropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  frmZDYHelp:=TfrmZDYHelp.Create(self);
  with frmZDYHelp do
  begin
    flag:='CLName';
    flagname:='车辆名称';
    if showModal=1 then
    begin
      with Order_Sub do
      begin
        edit;
        fieldbyname('CLName').Value:=trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
    free;
  end;
end;

procedure TfrmYGYPInPut_CLSQDJ.v1TOPlacePropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  frmZDYHelp:=TfrmZDYHelp.Create(self);
  with frmZDYHelp do
  begin
    flag:='MDD';
    flagname:='目的地';
    if showModal=1 then
    begin
      with Order_Sub do
      begin
        edit;
        fieldbyname('TOPlace').Value:=trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
    free;
  end;
end;

end.
