unit U_FHInPut;

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
  TfrmFHInPut = 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;
    v1Column6: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column20: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    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
  frmFHInPut: TfrmFHInPut;

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

{$R *.dfm}

procedure TfrmFHInPut.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('发货申请录入',Tv1,'检验管理');
end;

procedure TfrmFHInPut.InitData();
begin
  with ADOQuery1 do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select A.*,C.OrderNo,D.PRTHX,PRTName=isnull(C.MPRTCodeName,'''') ');
    sql.Add(',PRTColor=isnull(D.PRTColorNo,'''')+'' ''+isnull(D.PRTColor,'''')+'' ''+isnull(D.PRTColorEng,'''')');
    SQL.Add(' from JYOrder_FH A');
    sql.Add(' inner join JYOrder_Main C on A.MainId=C.MainId');
    sql.Add(' left join JYOrder_Sub D on A.SubId=D.SubId');
    sql.Add(' where A.FHId='''+Trim(FMainId)+'''');
    Open;
  end;
  SCreateCDS20(ADOQuery1,Order_Sub);
  SInitCDSData20(ADOQuery1,Order_Sub);
  

end;

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

procedure TfrmFHInPut.FormShow(Sender: TObject);
begin
  ReadCxGrid('发货申请录入',Tv1,'检验管理');
  InitData();
end;
procedure TfrmFHInPut.GetMaxConNO();
begin

end;
function TfrmFHInPut.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('FHID').AsString)='' then
        begin
          if GetLSNo(ADOCmd,maxno,'FH','JYOrder_FH',4,1)=False then
          begin
            Order_Sub.EnableControls;
            ADOCmd.Connection.RollbackTrans;
            Application.MessageBox('取规则流水号失败!','提示',0);
            Exit;
          end;
        end else
        begin
          maxno:=Trim(Order_Sub.fieldbyname('FHID').AsString);
        end;
        with ADOCmd do
        begin
          Close;
          sql.Clear;
          SQL.Add('select * from JYOrder_FH where FHID='''+Trim(Order_Sub.fieldbyname('FHID').AsString)+'''');
          Open;
        end;
        with ADOCmd do
        begin
          if Trim(Order_Sub.fieldbyname('FHID').AsString)='' then
          begin
            Append;
          end
          else begin
            Edit;
          end;
          FieldByName('FHID').Value:=Trim(maxno);
          FieldByName('MainId').Value:=Order_Sub.fieldbyname('MainId').Value;
          FieldByName('SubId').Value:=Order_Sub.fieldbyname('SubId').Value;
          RTSetSaveDataCDS(ADOCmd,Tv1,Order_Sub,'JYOrder_FH',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('FHID').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 TfrmFHInPut.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('SQDate',null,[]) then
  begin
    Application.MessageBox('申请日期不能为空!','提示',0);
    Exit;
  end;
  if Order_Sub.Locate('SQQty',null,[]) then
  begin
    Application.MessageBox('申请数量不能为空!','提示',0);
    Exit;
  end;
  if SaveData() then
  begin
    Application.MessageBox('保存成功！','提示',0);
    ModalResult:=1;
  end;

end;

procedure TfrmFHInPut.ToolButton3Click(Sender: TObject);
var
  FSQDate:string;
begin
  FSQDate:=Trim(FormatDateTime('yyyy-MM-dd',SGetServerDate(ADOTemp)));
  try
    frmOrderSunSelList:=TfrmOrderSunSelList.Create(Application);
    with frmOrderSunSelList do
    begin
      if ShowModal=1 then
      begin
        with frmOrderSunSelList.Order_Main do
        begin
          First;
          while frmOrderSunSelList.Order_Main.Locate('SSel',True,[]) do
          begin
            with Self.Order_Sub do
            begin
              Append;
              FieldByName('MainId').Value:=frmOrderSunSelList.Order_Main.FieldByName('MainId').Value;
              FieldByName('SubId').Value:=frmOrderSunSelList.Order_Main.FieldByName('SubId').Value;
              FieldByName('OrderNo').Value:=frmOrderSunSelList.Order_Main.FieldByName('OrderNo').Value;
              FieldByName('PRTName').Value:=Trim(frmOrderSunSelList.Order_Main.FieldByName('MPRTCodeName').AsString);
              FieldByName('PRTColor').Value:=frmOrderSunSelList.Order_Main.FieldByName('PRTColor').Value;
              FieldByName('PRTHX').Value:=frmOrderSunSelList.Order_Main.FieldByName('PRTHX').Value;
              FieldByName('QtyUnit').Value:=frmOrderSunSelList.Order_Main.FieldByName('OrderUnit').Value;
              FieldByName('SQPS').Value:=frmOrderSunSelList.Order_Main.FieldByName('KCPS').Value;
              FieldByName('SQQty').Value:=frmOrderSunSelList.Order_Main.FieldByName('KCQty').Value;
              FieldByName('SQDate').Value:=FSQDate;
              Post;
            end;
            frmOrderSunSelList.Order_Main.Delete;
          end;

        end;
      end;
    end;
  finally
    frmOrderSunSelList.Free;
  end;
  ToolBar1.SetFocus;
end;

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

procedure TfrmFHInPut.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.
