unit U_BPJHInPut;

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
  TfrmBPJHInPut = class(TForm)
    ToolBar2: TToolBar;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    DataSource1: TDataSource;
    Order_Sub: TClientDataSet;
    DataSource2: TDataSource;
    ADOZDY: TADOQuery;
    CDS_ZDY: TClientDataSet;
    ADOQuery1: TADOQuery;
    cxGridPopupMenu1: TcxGridPopupMenu;
    TBSave: TToolButton;
    ToolButton4: TToolButton;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1OrderNo: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1GDPerson: TcxGridDBColumn;
    v1FactoryName: TcxGridDBColumn;
    v1PSName: TcxGridDBColumn;
    v1Column20: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1ToFactoryName: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1QtyUnit: TcxGridDBColumn;
    v1Column21: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    v1Column2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    ToolButton3: TToolButton;
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure v1GDPersonPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1FactoryNamePropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1ToFactoryNamePropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure Tv1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure v1Column10PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure ToolButton3Click(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:String;

    { Public declarations }
  end;

var
  frmBPJHInPut: TfrmBPJHInPut;

implementation
uses
  U_DataLink,U_ZDYHelp,U_RTFun,U_GYSList,U_SYUserView,U_ProductOrderListSel,U_ProductOrderHzListSel;

{$R *.dfm}

procedure TfrmBPJHInPut.InitData();
begin
  with ADOQuery1 do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select A.* ');
    sql.Add(' from XH_Cloth_JH A  ');
    sql.Add(' where A.JHID='''+Trim(FMainId)+'''');
    Open;
  end;
  SCreateCDS20(ADOQuery1,Order_Sub);
  SInitCDSData20(ADOQuery1,Order_Sub);
end;

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

procedure TfrmBPJHInPut.FormShow(Sender: TObject);
begin
  ReadCxGrid('布匹计划信息录入',Tv1,'跟单管理');
  InitData();

end;
procedure TfrmBPJHInPut.GetMaxConNO();
begin

end;
function TfrmBPJHInPut.SaveData():Boolean;
var
  maxSubNo,FGDID:String;
begin
  try
    Result:=False;
    ADOCmd.Connection.BeginTrans;
    //保存数据
    Order_Sub.DisableControls;
    with Order_Sub do
    begin
      First;
      while not Eof do
      begin
        with ADOTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from XH_Cloth_JH where JHID='''+Trim(Order_Sub.fieldbyname('JHID').AsString)+'''');
          Open;
        end;
        FGDID:=Trim(ADOTemp.fieldbyname('JHID').AsString);
        if Trim(FGDID)='' then
        begin
          if GetLSNo(ADOCmd,maxSubNo,'JH','XH_Cloth_JH',3,1)=False then
          begin
            ADOCmd.Connection.RollbackTrans;
            Order_Sub.EnableControls;
            Application.MessageBox('布匹计划流水号异常！','提示',0);
            exit;
          end;
        end else
        begin
          maxSubNo:=Trim(FGDID);
        end;
        with ADOCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from XH_Cloth_JH where ');
          sql.Add('  JHID='''+Trim(maxSubNo)+'''');
          Open;
        end;
        with ADOCmd do
        begin
          if Trim(FGDID)='' then
          begin
            Append;
            FieldByName('Filler').Value:=Trim(DName);
            FieldByName('FillerCode').Value:=Trim(DCode);
          end
          else begin
            Edit;
            FieldByName('Editer').Value:=Trim(DName);
            FieldByName('EditerCode').Value:=Trim(DCode);
            FieldByName('EditTime').Value:=SGetServerDateTime(ADOTemp);
          end;
          FieldByName('JHID').Value:=Trim(maxSubNo);
          RTSetSaveDataCDS(ADOCmd,Tv1,Order_Sub,'XH_Cloth_JH',0);
          FieldByName('GDCode').Value:=Order_Sub.fieldbyname('GDCode').Value;
          FieldByName('FactoryNo').Value:=Order_Sub.fieldbyname('FactoryNo').Value;
          FieldByName('ToFactoryNo').Value:=Order_Sub.fieldbyname('ToFactoryNo').Value;
          Post;
        end;
        Order_Sub.Edit;
        Order_Sub.FieldByName('JHID').Value:=Trim(maxSubNo);
        Next;
      end;
    end;
    ADOCmd.Connection.CommitTrans;
    Order_Sub.EnableControls;
    Result:=True;
  except
    Result:=False;
    ADOCmd.Connection.RollbackTrans;
    Order_Sub.EnableControls;
    Application.MessageBox('保存失败！','提示',0);
  end;
end;

procedure TfrmBPJHInPut.TBSaveClick(Sender: TObject);
begin
  if Order_Sub.IsEmpty then
  begin
    Application.MessageBox('明细不能为空！','提示',0);
    exit;
  end;
  if Order_Sub.Locate('JHDate',null,[]) then
  begin
    Application.MessageBox('日期不能为空!','提示',0);
    Exit;
  end;
  if Order_Sub.Locate('JHBatchNo',null,[]) then
  begin
    Application.MessageBox('批号不能为空!','提示',0);
    Exit;
  end;
  if Order_Sub.Locate('GDName',null,[]) then
  begin
    Application.MessageBox('跟单员不能为空!','提示',0);
    Exit;
  end;
  if Order_Sub.Locate('GDCode',null,[]) then
  begin
    Application.MessageBox('跟单员编号不能为空!','提示',0);
    Exit;
  end;
  if Order_Sub.Locate('BPCode',null,[]) then
  begin
    Application.MessageBox('编号不能为空!','提示',0);
    Exit;
  end;
 { if Order_Sub.Locate('BPName',null,[]) then
  begin
    Application.MessageBox('品名不能为空!','提示',0);
    Exit;
  end;  }
  if Order_Sub.Locate('FactoryName',null,[]) then
  begin
    Application.MessageBox('供应商不能为空!','提示',0);
    Exit;
  end;
  if Order_Sub.Locate('ToFactoryName',null,[]) then
  begin
    Application.MessageBox('存放工厂不能为空!','提示',0);
    Exit;
  end;
  {if Order_Sub.Locate('JHPS',null,[]) then
  begin
    Application.MessageBox('匹数不能为空!','提示',0);
    Exit;
  end;  }
  if Order_Sub.Locate('JHUnit',null,[]) then
  begin
    Application.MessageBox('单位不能为空!','提示',0);
    Exit;
  end;
  if SaveData() then
  begin
    Application.MessageBox('保存成功！','提示',0);
    //ModalResult:=1;
  end;

end;

procedure TfrmBPJHInPut.ToolButton1Click(Sender: TObject);
begin
  //CopyAddRow(Tv1,Order_Sub);
  if Order_Sub.IsEmpty=False then
    CopyAddRowCDS(Order_Sub)
  else
  begin
    with Order_Sub do
    begin
      Append;
      Post;
    end;
  end;
  if Order_Sub.RecordCount=1 then
  begin
    with Order_Sub do
    begin
      Edit;
      FieldByName('JHDate').Value:=SGetServerDate(ADOTemp);
      FieldByName('DlyDate').Value:=SGetServerDate(ADOTemp);
      FieldByName('JHUnit').Value:='Kg';
      Post;
    end;
  end else
  begin
    with Order_Sub do
    begin
      Edit;
      FieldByName('JHID').Value:='';
      Post;
    end;
  end;

end;

procedure TfrmBPJHInPut.ToolButton2Click(Sender: TObject);
begin
  if Order_Sub.IsEmpty then Exit;
  if Trim(Order_Sub.fieldbyname('JHID').AsString)<>'' then
  begin
    with ADOTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from XH_Cloth_DH where JHID='''+Trim(Order_Sub.fieldbyname('JHID').AsString)+'''');
      Open;
    end;
    if ADOTemp.IsEmpty=False then
    begin
      Application.MessageBox('已有到货数据不能删除数据！','提示',0);
      Exit;
    end;
    with ADOTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from XH_Cloth_TP where JHID='''+Trim(Order_Sub.fieldbyname('JHID').AsString)+'''');
      Open;
    end;
    if ADOTemp.IsEmpty=False then
    begin
      Application.MessageBox('已有投坯数据不能删除数据！','提示',0);
      Exit;
    end;
    if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete XH_Cloth_JH where JHID='''+Trim(Order_Sub.fieldbyname('JHID').AsString)+'''');
      ExecSQL;
    end;
  end;
  Order_Sub.Delete;
end;

procedure TfrmBPJHInPut.ToolButton4Click(Sender: TObject);
begin
  Close;
  WriteCxGrid('布匹计划信息录入',Tv1,'跟单管理');
end;

procedure TfrmBPJHInPut.v1GDPersonPropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmSYUserView:=TfrmSYUserView.Create(Application);
    with frmSYUserView do
    begin
      if ShowModal=1 then
      begin
        with Order_Sub do
        begin
          Edit;
          FieldByName('GDName').Value:=Trim(CDS_User.fieldbyname('UserName').AsString);
          FieldByName('GDCode').Value:=Trim(CDS_User.fieldbyname('UserId').AsString);
        end;
      end;
    end;
  finally
    frmSYUserView.Free;
  end;
end;

procedure TfrmBPJHInPut.v1FactoryNamePropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmGYSList:=TfrmGYSList.Create(Application);
    with frmGYSList do
    begin
      if ShowModal=1 then
      begin
        with Self.Order_Sub do
        begin
          Edit;
          FieldByName('FactoryName').Value:=Trim(CDS_HZ.fieldbyname('CoName').AsString);
          FieldByName('FactoryNo').Value:=Trim(CDS_HZ.fieldbyname('CoCode').AsString);
        end;
      end;
    end;
  finally
    frmGYSList.Free;
  end;
end;

procedure TfrmBPJHInPut.v1ToFactoryNamePropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmGYSList:=TfrmGYSList.Create(Application);
    with frmGYSList do
    begin
      if ShowModal=1 then
      begin
        with Self.Order_Sub do
        begin
          Edit;
          FieldByName('ToFactoryName').Value:=Trim(CDS_HZ.fieldbyname('CoName').AsString);
          FieldByName('ToFactoryNo').Value:=Trim(CDS_HZ.fieldbyname('CoCode').AsString);
        end;
      end;
    end;
  finally
    frmGYSList.Free;
  end;
end;

procedure TfrmBPJHInPut.Tv1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if Trim(Order_Sub.fieldbyname('BPCode').AsString)='' then Exit;
  if Trim(Order_Sub.fieldbyname('BPName').AsString)<>'' then Exit;
  with ADOTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select Top 1* from XH_Cloth_JH where BPCode='''+Trim(Order_Sub.fieldbyname('BPCode').AsString)+'''');
    sql.Add(' order by FillTime desc');
    Open;
  end;
  Order_Sub.DisableControls;
  with Order_Sub do
  begin
    Edit;
    FieldByName('BPName').Value:=Trim(ADOTemp.fieldbyname('BPName').AsString);
    //Post;
  end;
  Order_Sub.EnableControls;
end;

procedure TfrmBPJHInPut.v1Column10PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmProductOrderHzListSel:=TfrmProductOrderHzListSel.Create(Application);
    with frmProductOrderHzListSel do
    begin
      if ShowModal=1 then
      begin
        with Self.Order_Sub do
        begin
          Edit;
//          FieldByName('MainId').Value:=Trim(frmProductOrderListSel.Order_Main.fieldbyname('MainId').AsString);
//          FieldByName('SubId').Value:=Trim(frmProductOrderListSel.Order_Main.fieldbyname('SubId').AsString);
          FieldByName('OrderNo').Value:=Trim(frmProductOrderHzListSel.Order_Main.fieldbyname('OrderNo').AsString);
          FieldByName('BPCode').Value:=Trim(frmProductOrderHzListSel.Order_Main.fieldbyname('MPRTCode').AsString);
          FieldByName('BPName').Value:=Trim(frmProductOrderHzListSel.Order_Main.fieldbyname('MPRTCodeName').AsString);
          FieldByName('BPMF').Value:=Trim(frmProductOrderHzListSel.Order_Main.fieldbyname('MPRTMF').AsString);
          FieldByName('BPKZ').Value:=Trim(frmProductOrderHzListSel.Order_Main.fieldbyname('MPRTKZ').AsString);
          FieldByName('BPSpec').Value:=Trim(frmProductOrderHzListSel.Order_Main.fieldbyname('MPRTSpec').AsString);
          FieldByName('BPCF').Value:=Trim(frmProductOrderHzListSel.Order_Main.fieldbyname('MPRTCF').AsString);
          FieldByName('GDName').Value:=Trim(frmProductOrderHzListSel.Order_Main.fieldbyname('JGGDY').AsString);
          FieldByName('JHQty').Value:=frmProductOrderHzListSel.Order_Main.fieldbyname('OrderQty').AsFloat;
          FieldByName('JHUnit').Value:=Trim(frmProductOrderHzListSel.Order_Main.fieldbyname('OrderUnit').AsString);
//          FieldByName('FactoryName').Value:=Trim(frmProductOrderHzListSel.Order_Main.fieldbyname('PBGYS').AsString);
//          FieldByName('ToFactoryName').Value:=Trim(frmProductOrderHzListSel.Order_Main.fieldbyname('RanFactory').AsString);
          FieldByName('DlyDate').Value:=frmProductOrderHzListSel.Order_Main.fieldbyname('DLYDate').AsDateTime;
          with ADOQueryTemp do
          begin
            Close;
            SQL.Clear;
            SQL.Add(' select A.UserId ');
            SQL.Add(' from SY_User A where UserId not in(''Admin'',''Test'') ');
            SQL.Add(' and Valid=''Y'' ');
            sql.Add(' and UDept=''跟单'' ');
            sql.Add(' and UserName='+quotedstr(Trim(frmProductOrderHzListSel.Order_Main.fieldbyname('JGGDY').AsString)));
            Open;
          end;
          FieldByName('GDCode').Value:=Trim(ADOQueryTemp.fieldbyname('UserId').AsString);
        end;
      end;
    end;
  finally
    frmProductOrderHzListSel.Free;
  end;
end;

procedure TfrmBPJHInPut.ToolButton3Click(Sender: TObject);
begin
  try
    frmProductOrderHzListSel:=TfrmProductOrderHzListSel.Create(Application);      //frmProductOrderListSel
    with frmProductOrderHzListSel do
    begin
      if ShowModal=1 then
      begin
        while  frmProductOrderHzListSel.Order_Main.Locate('SSel',True,[]) do
        begin
          with Self.Order_Sub do
          begin
            Append;
  //          FieldByName('MainId').Value:=Trim(frmProductOrderListSel.Order_Main.fieldbyname('MainId').AsString);
  //          FieldByName('SubId').Value:=Trim(frmProductOrderListSel.Order_Main.fieldbyname('SubId').AsString);
            FieldByName('OrderNo').Value:=Trim(frmProductOrderHzListSel.Order_Main.fieldbyname('OrderNo').AsString);
            FieldByName('BPCode').Value:=Trim(frmProductOrderHzListSel.Order_Main.fieldbyname('MPRTCode').AsString);
            FieldByName('BPName').Value:=Trim(frmProductOrderHzListSel.Order_Main.fieldbyname('MPRTCodeName').AsString);
            FieldByName('BPMF').Value:=Trim(frmProductOrderHzListSel.Order_Main.fieldbyname('MPRTMF').AsString);
            FieldByName('BPKZ').Value:=Trim(frmProductOrderHzListSel.Order_Main.fieldbyname('MPRTKZ').AsString);
            FieldByName('BPSpec').Value:=Trim(frmProductOrderHzListSel.Order_Main.fieldbyname('MPRTSpec').AsString);
            FieldByName('BPCF').Value:=Trim(frmProductOrderHzListSel.Order_Main.fieldbyname('MPRTCF').AsString);
            FieldByName('GDName').Value:=Trim(frmProductOrderHzListSel.Order_Main.fieldbyname('OrdPerson1').AsString);
            FieldByName('JHQty').Value:=frmProductOrderHzListSel.Order_Main.fieldbyname('PRTOrderQty').AsFloat;
            FieldByName('JHUnit').Value:=Trim(frmProductOrderHzListSel.Order_Main.fieldbyname('OrderUnit').AsString);
            FieldByName('FactoryName').Value:=Trim(frmProductOrderHzListSel.Order_Main.fieldbyname('PBGYS').AsString);
            FieldByName('ToFactoryName').Value:=Trim(frmProductOrderHzListSel.Order_Main.fieldbyname('RanFactory').AsString);
            FieldByName('JHQty').Value:=frmProductOrderHzListSel.Order_Main.fieldbyname('SOrddefstr5').Value;
            FieldByName('JHUnit').Value:=Trim(frmProductOrderHzListSel.Order_Main.fieldbyname('PBUnit').AsString);
            FieldByName('DlyDate').Value:=frmProductOrderHzListSel.Order_Main.fieldbyname('OrdDefDate4').AsDateTime;
            FieldByName('JHDate').Value:=SGetServerDate(ADOTemp);
            with ADOQueryTemp do
            begin
              Close;
              SQL.Clear;
              SQL.Add(' select A.UserId ');
              SQL.Add(' from SY_User A where UserId not in(''Admin'',''Test'') ');
              SQL.Add(' and Valid=''Y'' ');
              sql.Add(' and UDept=''跟单'' ');
              sql.Add(' and UserName='+quotedstr(Trim(frmProductOrderHzListSel.Order_Main.fieldbyname('OrdPerson1').AsString)));
              Open;
            end;
            FieldByName('GDCode').Value:=Trim(ADOQueryTemp.fieldbyname('UserId').AsString);
          end;
           frmProductOrderHzListSel.Order_Main.Delete;
        end;

      end;
    end;
  finally
    frmProductOrderHzListSel.Free;
  end;
end;

end.
