unit U_ZXBInPut;

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, cxDropDownEdit, cxCalendar, cxGridCustomPopupMenu,
  cxGridPopupMenu;

type
  TfrmZXBInPut = class(TForm)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    DataSource1: TDataSource;
    Order_Sub: TClientDataSet;
    DataSource2: TDataSource;
    ADOZDY: TADOQuery;
    CDS_ZDY: TClientDataSet;
    ADOQuery1: TADOQuery;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    ClientDataSet1: TClientDataSet;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column5: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1ShipDate: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    v1Column3: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure v1Column6PropertiesEditValueChanged(Sender: TObject);
  private
    FXS:Integer;
    procedure InitData();
    procedure ZDYHelp(FButn:TcxButtonEdit;LType:string);
    function  SaveData():Boolean;
    procedure GetMaxConNO();
    { Private declarations }
  public
    PState,PCopyInt:Integer;
    FMainId,FConNo,FConType,FYLType:String;

    { Public declarations }
  end;

var
  frmZXBInPut: TfrmZXBInPut;

implementation
uses
  U_DataLink,U_ZDYHelp,U_RTFun,U_GYSList,U_ZDYHelpSel;

{$R *.dfm}

procedure TfrmZXBInPut.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('检验安排录入',Tv1,'检验管理');
end;

procedure TfrmZXBInPut.InitData();
begin
  with ADOQuery1 do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select A.*,B.PS,C.OrderNo');
    SQL.Add(' from JYOrder_Main_MD_CJ A ');
    sql.Add(' inner join JYOrder_Main_MD B on A.MDID=B.MDID');
    sql.Add(' inner join JYOrder_Main C on B.ORDMainId=C.MainId');
    sql.Add(' where A.CJId='''+Trim(FMainId)+'''');
    Open;
  end;
  SCreateCDS20(ADOQuery1,Order_Sub);
  SInitCDSData20(ADOQuery1,Order_Sub);
  

end;

procedure TfrmZXBInPut.ZDYHelp(FButn:TcxButtonEdit;LType:string);
begin
end;

procedure TfrmZXBInPut.FormShow(Sender: TObject);
begin
  ReadCxGrid('检验安排录入',Tv1,'检验管理');
  InitData();
end;
procedure TfrmZXBInPut.GetMaxConNO();
begin

end;
function TfrmZXBInPut.SaveData():Boolean;
var
  maxno:String;
begin
  try
    Result:=False;
    ADOCmd.Connection.BeginTrans;
    //保存主表
    Order_Sub.DisableControls;
    with Order_Sub do
    begin
      First;
      while not Eof do
      begin
        if Trim(Order_Sub.fieldbyname('CJID').AsString)='' then
        begin
          if GetLSNo(ADOCmd,maxno,'CJ','JYOrder_Main_MD_CJ',4,1)=False then
          begin
            Order_Sub.EnableControls;
            ADOCmd.Connection.RollbackTrans;
            Application.MessageBox('取规则流水号失败!','提示',0);
            Exit;
          end;
        end else
        begin
          maxno:=Trim(Order_Sub.fieldbyname('CJID').AsString);
        end;
        with ADOCmd do
        begin
          Close;
          sql.Clear;
          SQL.Add('select * from JYOrder_Main_MD_CJ where CJID='''+Trim(Order_Sub.fieldbyname('CJID').AsString)+'''');
          Open;
        end;
        with ADOCmd do
        begin
          if Trim(Order_Sub.fieldbyname('CJID').AsString)='' then
          begin
            Append;
          end
          else begin
            Edit;
          end;
          FieldByName('CJID').Value:=Trim(maxno);
          RTSetSaveDataCDS(ADOCmd,Tv1,Order_Sub,'JYOrder_Main_MD_CJ',0) ;
          if Trim(FMainId)='' then
          begin
            FieldByName('Filler').Value:=Trim(DName);
          end else
          begin
            FieldByName('Editer').Value:=Trim(DName);
            FieldByName('EditTime').Value:=SGetServerDateTime(ADOTemp);
          end;
          Post;
        end;
        Edit;
        FieldByName('CJID').Value:=Trim(maxno);
        Next;
      end;
    end;
    Order_Sub.EnableControls;
    ADOCmd.Connection.CommitTrans;
    Result:=True;
  except
    Result:=False;
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！','提示',0);
  end;
end;

procedure TfrmZXBInPut.TBSaveClick(Sender: TObject);
var
  FPPrice,PRTPrice,BCMoney,FPQty,ChaMoney:string;
begin
  ToolBar1.SetFocus;

  if Order_Sub.IsEmpty then
  begin
    Application.MessageBox('明细不能为空！','提示',0);
    exit;
  end;
  if Order_Sub.Locate('MDID',null,[]) then
  begin
    Application.MessageBox('条码不能为空!','提示',0);
    Exit;
  end;
  if SaveData() then
  begin
    Application.MessageBox('保存成功！','提示',0);
    ModalResult:=1;
  end;

end;

procedure TfrmZXBInPut.ToolButton3Click(Sender: TObject);
begin
  with Order_Sub do
  begin
    Append;
    Post;
  end;
end;

procedure TfrmZXBInPut.ToolButton4Click(Sender: TObject);
begin
  if Order_Sub.IsEmpty then Exit;
  if Trim(Order_Sub.fieldbyname('CJId').AsString)<>'' then
  begin
    if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete JYOrder_Main_MD_CJ where CJId='''+Trim(Order_Sub.fieldbyname('CJId').AsString)+'''');
      ExecSQL;
    end;
  end;
  Order_Sub.Delete;
end;

procedure TfrmZXBInPut.v1Column6PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue:string;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  with Order_Sub do
  begin
    Edit;
    FieldByName('MDID').Value:=Trim(mvalue);
    Post;
  end;
  with ADOTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('select B.OrderNo,A.PS,A.MDDate from JYOrder_Main_MD A');
    sql.Add(' inner join JYorder_Main B on A.ORDMainId=B.MainId');
    sql.Add(' and A.MDID='''+Trim(mvalue)+'''');
    Open;
  end;
  if ADOTemp.IsEmpty then
  begin
    Application.MessageBox('条码录入错误!','提示',0);
    Exit;
  end;
  with Order_Sub do
  begin
    Edit;
    FieldByName('OrderNo').Value:=ADOTemp.fieldbyname('OrderNo').Value;
    FieldByName('PS').Value:=ADOTemp.fieldbyname('PS').Value;
    Post;
  end;
end;

end.
