unit U_ShaRKInPutWSQ;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage,
  cxEdit, DB, cxDBData, cxCalendar, cxDropDownEdit, ComCtrls, ToolWin,
  cxGridLevel, cxGridCustomTableView, cxGridTableView, cxGridDBTableView,
  cxClasses, cxControls, cxGridCustomView, cxGrid, cxGridCustomPopupMenu,
  cxGridPopupMenu, ADODB, DBClient, cxButtonEdit, cxTextEdit, StdCtrls, ExtCtrls,
  cxLookAndFeels, cxLookAndFeelPainters, cxNavigator;

type
  TfrmShaRKInPutWSQ = class(TForm)
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column2: TcxGridDBColumn;
    v2SPName: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    ToolBar1: TToolBar;
    TBAdd: TToolButton;
    TBDel: TToolButton;
    TBSave: TToolButton;
    TBClose: TToolButton;
    v1CRType: TcxGridDBColumn;
    DataSource3: TDataSource;
    CDS_Sub: TClientDataSet;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    cxGridPopupMenu2: TcxGridPopupMenu;
    v1Column12: TcxGridDBColumn;
    v1FactoryName: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1SPCF: TcxGridDBColumn;
    v1SPSpec: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1ToFactoryName: TcxGridDBColumn;
    v1FromFactoryName: TcxGridDBColumn;
    v1BatchNo: TcxGridDBColumn;
    Panel1: TPanel;
    Label1: TLabel;
    v1Column10: TcxGridDBColumn;
    v1SXPinPai: TcxGridDBColumn;
    v1SPNameGYS: TcxGridDBColumn;
    Panel2: TPanel;
    Panel3: TPanel;
    Label9: TLabel;
    MXQty: TEdit;
    v1Column1: TcxGridDBColumn;
    cxGrid1: TcxGrid;
    TvMX: TcxGridDBTableView;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    vMXColumn1: TcxGridDBColumn;
    CDS_MX: TClientDataSet;
    DSMX: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Label2: TLabel;
    PiQty: TEdit;
    v1Column4: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBAddClick(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure v1Column3PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column14PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v2Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column5PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column11PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column16PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure Tv1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure v1Column10PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column18PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column8PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column7PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column15PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure MXQtyKeyPress(Sender: TObject; var Key: Char);
    procedure TvMXCellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure v1PBNOPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column7PropertiesEditValueChanged(Sender: TObject);
    procedure v1Column4PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1BatchNoPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column12PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
  private
    { Private declarations }
    FSPType, FDelStr: string;
    function SaveCKData(): Boolean;
  public
    { Public declarations }
    FBCId, canshu3, FCRType, FHZCRType, canshu88, ZHFlag: string;
    FCHEJIAN: string;
    wsql2: string;
  end;

var
  frmShaRKInPutWSQ: TfrmShaRKInPutWSQ;

implementation

uses
  U_DataLink, U_RTFun, U_ClothContractListHZCX, U_ZDYHelp, U_BPShengChanListSel,
  U_ZdyAttachCP, U_ContractCGJGSelList, U_GYSList, U_ZdyAttachGYS,
  U_CaiGouSQChkListSel, U_ShaOutListSel, U_KHList, U_ClothSCHZListSel;

{$R *.dfm}

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

procedure TfrmShaRKInPutWSQ.TBAddClick(Sender: TObject);
begin
  if Trim(ZHFlag) = '1' then
  begin
    with CDS_Sub do
    begin
      Append;
      FieldByName('CRTime').Value := Trim(FormatDateTime('yyyy-MM-dd', Now));
      FieldByName('QtyUnit').Value := 'Kg';
      FieldByName('CRType').Value := FCRType;
      FieldByName('ToFactoryName').Value := '本厂仓库';
      FieldByName('CHEJIAN').Value := FCHEJIAN;

      Post;
    end;
  end
  else if Trim(ZHFlag) = '0' then
  begin
    try
      frmShaOutListSel := TfrmShaOutListSel.Create(Application);
      with frmShaOutListSel do
      begin
        if ShowModal = 1 then
        begin
          with Self.CDS_Sub do
          begin
            Append;
            FieldByName('CRTime').Value := Trim(FormatDateTime('yyyy-MM-dd', Now));
            FieldByName('QtyUnit').Value := 'Kg';
            FieldByName('CRType').Value := Trim(FCRType);
            FieldByName('PBNO').Value := Trim(frmShaOutListSel.CDS_Main.fieldbyname('ToPBNO').AsString);
            FieldByName('FactoryName').Value := Trim(frmShaOutListSel.CDS_Main.fieldbyname('FactoryName').AsString);
            FieldByName('FactoryNo').Value := Trim(frmShaOutListSel.CDS_Main.fieldbyname('FactoryNo').AsString);
            FieldByName('SXPinPai').Value := Trim(frmShaOutListSel.CDS_Main.fieldbyname('SXPinPai').AsString);
            FieldByName('SPName').Value := Trim(frmShaOutListSel.CDS_Main.fieldbyname('SPName').AsString);
            FieldByName('SPNameGYS').Value := Trim(frmShaOutListSel.CDS_Main.fieldbyname('SPNameGYS').AsString);
            FieldByName('SPSpec').Value := Trim(frmShaOutListSel.CDS_Main.fieldbyname('SPSpec').AsString);
            FieldByName('SPCF').Value := Trim(frmShaOutListSel.CDS_Main.fieldbyname('SPCF').AsString);
            FieldByName('Price').Value := frmShaOutListSel.CDS_Main.fieldbyname('Price').Value;
            FieldByName('SQID').Value := frmShaOutListSel.CDS_Main.fieldbyname('SQID').Value;
            FieldByName('FromFactoryName').Value := frmShaOutListSel.CDS_Main.fieldbyname('ToFactoryName').Value;
            FieldByName('FromFactoryNo').Value := frmShaOutListSel.CDS_Main.fieldbyname('ToFactoryNo').Value;
            FieldByName('FromSPID').Value := frmShaOutListSel.CDS_Main.fieldbyname('SPID').Value;
            FieldByName('BatchNo').Value := frmShaOutListSel.CDS_Main.fieldbyname('BatchNo').Value;
            FieldByName('CHEJIAN').Value := FCHEJIAN;
            Post;
          end;
        end;
      end;
    finally
      frmShaOutListSel.Free;
    end;

  end;
end;

function TfrmShaRKInPutWSQ.SaveCKData(): Boolean;
var
  FCRID, Maxno, FFSPID, FQMID, maxMXId: string;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_Sub.DisableControls;
    with CDS_Sub do
    begin
      First;
      while not eof do
      begin
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from CK_SXPB_CR where SPID=''' + Trim(CDS_Sub.fieldbyname('SPID').AsString) + '''');
          open;
        end;
        FFSPID := Trim(ADOQueryTemp.fieldbyname('SPID').AsString);
        if Trim(FFSPID) = '' then
        begin
          if GetLSNo(ADOQueryCmd, Maxno, 'SR', 'CK_SXPB_CR', 3, 1) = False then
          begin
            Result := False;
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取纱线入库编号失败!', '提示', 0);
            Exit;
          end;
        end
        else
        begin
          Maxno := Trim(FFSPID);
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from CK_SXPB_CR where SPID=''' + Trim(Maxno) + '''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(FFSPID) = '' then
          begin
            Append;
          end
          else
          begin
            Edit;
          end;
          FieldByName('SPID').Value := Trim(Maxno);
          FieldByName('FZSPID').Value := Trim(Maxno);
          FieldByName('CKName').Value := '纱线';
          FieldByName('FactoryNo').Value := Trim(CDS_Sub.fieldbyname('FactoryNo').AsString);
          FieldByName('khcode').Value := Trim(CDS_Sub.fieldbyname('KHCODE').AsString);
          FieldByName('FromFactoryNo').Value := Trim(CDS_Sub.fieldbyname('FromFactoryNo').AsString);
          FieldByName('ToFactoryNo').Value := Trim(CDS_Sub.fieldbyname('ToFactoryNo').AsString);
          FieldByName('CPID').Value := Trim(CDS_Sub.fieldbyname('CPID').AsString);
          FieldByName('SQID').Value := CDS_Sub.fieldbyname('SQID').Value;
          FieldByName('Price').Value := CDS_Sub.fieldbyname('Price').Value;
          FieldByName('FromSPID').Value := CDS_Sub.fieldbyname('FromSPID').Value; //回仓有值
          FieldByName('QtySXStr').Value := CDS_Sub.fieldbyname('QtySXStr').Value; //回仓有值
          FieldByName('CRFlag').Value := '入库';
          FieldByName('HZCRType').Value := Trim(FHZCRType);
          FieldByName('CRQtyFlag').Value := 1;
          //FieldByName('CRID').Value:=FCRID;
          RTSetSaveDataCDS(ADOQueryCmd, Tv1, CDS_Sub, 'CK_SXPB_CR', 2);
          if Trim(FFSPID) = '' then
            FieldByName('Filler').Value := Trim(DName)
          else
          begin
            FieldByName('Editer').Value := Trim(DName);
            FieldByName('EditTime').Value := SGetServerDate(ADOQueryTemp);
          end;
          Post;
        end;
        if Trim(FHZCRType) = '加工未做回仓' then
        begin
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('Update CK_SXPB_CR Set HCPS=(select Sum(PiQty) from CK_SXPB_CR A where A.FromSPID=CK_SXPB_CR.SPID and A.ZFFlag=0)');
            sql.Add(', HCQty=(select Sum(Qty) from CK_SXPB_CR A where A.FromSPID=CK_SXPB_CR.SPID and A.ZFFlag=0)');
            sql.Add(' where SPID=''' + Trim(CDS_Sub.fieldbyname('FromSPID').AsString) + '''');
            ExecSQL;
          end;
          with ADOQueryTemp do
          begin
            Close;
            sql.Clear;
            sql.Add(' select A.* ');
            SQL.Add(' from CK_SXPB_CR A  ');
            sql.Add(' where isnull(Qty,0)<isnull(HCQty,0)');
            sql.Add(' and SPID=''' + Trim(CDS_Sub.fieldbyname('FromSPID').AsString) + '''');
            Open;
          end;
          if ADOQueryTemp.IsEmpty = False then
          begin
            CDS_Sub.EnableControls;
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox(PChar('纱线：' + Trim(CDS_Sub.fieldbyname('SPName').AsString) + ' 回仓数量大于出库数量不能保存！'), '提示', 0);
            exit;
          end;
        end;
//        with ADOQueryCmd do
//        begin
//          Close;
//          sql.Clear;
//          sql.Add(' select SUM(CRQtyFlag*piqty)MXKCPiQty,SUM(CRQtyFlag*qty) MXKCQty,SPName  ');
//
//          sql.Add(' from CK_SXPB_CR A where isnull(CKName,'''')=''纱线'' ');
//          SQL.Add('and spname=' + QuotedStr(Trim(CDS_Sub.fieldbyname('SPName').asstring)));
//          SQL.Add('  and ZFFlag=0 ');
//          sql.Add('group by SPName');
//          SQL.ADD('having  SUM(CRQtyFlag*qty) <0');
//          open;
//        end;
//
//        if ADOQueryCmd.IsEmpty = False then
//        begin
//
//          CDS_Sub.EnableControls;
//          ADOQueryCmd.Connection.RollbackTrans;
//          Application.MessageBox('出库数大于库存数不能保存!', '提示', 0);
//
//          Exit;
//        end;
        Edit;
        FieldByName('SPID').Value := Trim(Maxno);
        //FieldByName('CRID').Value:=Trim(FCRID);
        Post;
        Next;
      end;
    end;
    CDS_Sub.EnableControls;
    //保存明细数据
    CDS_MX.DisableControls;
    with CDS_MX do
    begin
      First;
      while not Eof do
      begin
        with ADOQueryTemp do
        begin
          close;
          SQL.Clear;
          sql.Add('select * from GYS_Qty_MX where QMID=''' + Trim(CDS_MX.fieldbyname('QMID').AsString) + '''');
          Open;
        end;
        FQMID := Trim(ADOQueryTemp.fieldbyname('QMID').AsString);
        if Trim(FQMID) = '' then
        begin
          if GetLSNo(ADOQueryCmd, maxMXId, 'QM', 'GYS_Qty_MX', 4, 1) = False then
          begin
            Result := False;
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取最大号失败！', '提示', 0);
            Exit;
          end;
        end
        else
        begin
          maxMXId := Trim(FQMID);
        end;
        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from GYS_Qty_MX where QMID=''' + Trim(FQMID) + '''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(FQMID) = '' then
            Append
          else
            Edit;
          FieldByName('WBID').Value := Trim(Maxno);
          FieldByName('QMID').Value := Trim(maxMXId);
          FieldByName('Filler').Value := Trim(DName);
          if Trim(CDS_Sub.fieldbyname('BaoType').AsString) = '均重' then
          begin
            FieldByName('SMID').Value := Trim(Maxno);
          end
          else
          begin
            FieldByName('SMID').Value := Trim(maxMXId);
          end;
          FieldByName('MXPS').Value := 1;
          RTSetSaveDataCDS(ADOQueryCmd, TvMX, CDS_MX, 'GYS_Qty_MX', 0);
          Post;
        end;
        Next;
      end;
    end;
    CDS_MX.EnableControls;
    //保存明细数据
    ADOQueryCmd.Connection.CommitTrans;
    Result := True;
  except
    Result := False;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存异常!', '提示', 0);
  end;

end;

procedure TfrmShaRKInPutWSQ.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(FCRType, Tv1, '纱线仓库1');
  Close;
end;

procedure TfrmShaRKInPutWSQ.FormShow(Sender: TObject);
var
  fsj: string;
begin
  ReadCxGrid(FCRType, Tv1, '纱线仓库1');
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(' select A.* ');
    sql.Add(' from CK_SXPB_CR A');
    if FBCId <> '' then
    begin
      sql.Add('WHERE A.SPID in ' + (wsql2));
    end
    else
    begin
      sql.Add('WHERE 1<>1');
    end;

    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_Sub);
  SInitCDSData20(ADOQueryTemp, CDS_Sub);
//  v1FromFactoryName.Visible := False;
//  v1FromFactoryName.Hidden := True;
//
//  if Trim(ZHFlag) = '0' then
//  begin
//    v1FromFactoryName.Visible := True;
//    v1FromFactoryName.Hidden := False;
//    v1FromFactoryName.Options.Editing := False;
//
//    v1FactoryName.Options.Editing := False;
//    v1SXPinPai.Options.Editing := False;
//    v2SPName.Options.Editing := False;
//    v1SPNameGYS.Options.Editing := False;
//    v1SPSpec.Options.Editing := False;
//    v1SPCF.Options.Editing := False;
//    v1BatchNo.Options.Editing := False;
//  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from GYS_Qty_MX where WBID=''' + Trim(FBCId) + '''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_MX);
  SInitCDSData20(ADOQueryTemp, CDS_MX);
end;

procedure TfrmShaRKInPutWSQ.TBDelClick(Sender: TObject);
begin
  if CDS_Sub.IsEmpty then
    Exit;
  if Trim(CDS_Sub.fieldbyname('SPID').AsString) <> '' then
  begin
    if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
      Exit;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from CK_SXPB_CR where FZSPID=''' + Trim(CDS_Sub.fieldbyname('SPID').AsString) + '''');
      sql.Add(' and CRQtyFlag=-1');
      Open;
    end;
    if ADOQueryTemp.IsEmpty = False then
    begin
      Application.MessageBox('已有出库记录，不能删除!', '提示', 0);
      Exit;
    end;
    try
      ADOQueryCmd.Connection.BeginTrans;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add(' delete CK_SXPB_CR where SPID=''' + Trim(CDS_Sub.fieldbyname('SPID').AsString) + '''');
        ExecSQL;
      end;
      {with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update CaiGou_SQ Set DHPS=(select Sum(PiQty) from CK_SXPB_CR A where A.SQID=CaiGou_SQ.SQID and A.ZFFlag=0');
        sql.Add(' and isnull(A.HZCRType,'''')=''采购加工入库'') ');
        sql.Add(', DHQty=(select Sum(Qty) from CK_SXPB_CR A where A.SQID=CaiGou_SQ.SQID and A.ZFFlag=0 ');
        sql.Add(' and isnull(A.HZCRType,'''')=''采购加工入库'') ');
        sql.Add(' where SQID='''+Trim(CDS_Sub.fieldbyname('SQID').AsString)+'''');
        ExecSQL;
      end; }
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update CK_SXPB_CR Set HCPS=(select Sum(PiQty) from CK_SXPB_CR A where A.FromSPID=CK_SXPB_CR.SPID and A.ZFFlag=0)');
        sql.Add(', HCQty=(select Sum(Qty) from CK_SXPB_CR A where A.FromSPID=CK_SXPB_CR.SPID and A.ZFFlag=0)');
        sql.Add(' where SPID=''' + Trim(CDS_Sub.fieldbyname('FromSPID').AsString) + '''');
        ExecSQL;
      end;
      ADOQueryCmd.Connection.CommitTrans;
      CDS_Sub.Delete;
    except
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('删除异常!', '提示', 0);
    end;

  end
  else
  begin
    CDS_Sub.Delete;
  end;

end;

procedure TfrmShaRKInPutWSQ.TBSaveClick(Sender: TObject);
begin
  if CDS_Sub.IsEmpty then
    Exit;
  //if CDS_MX.IsEmpty then Exit;
  if CDS_Sub.Locate('CRTime', null, []) = True then
  begin
    Application.MessageBox('入库时间不能为空!', '提示', 0);
    Exit;
  end;
  if CDS_Sub.Locate('CRType', null, []) = True then
  begin
    Application.MessageBox('入库类型不能为空!', '提示', 0);
    Exit;
  end;

  if CDS_Sub.Locate('BATCHNO', null, []) = True then
  begin
    Application.MessageBox('批号不能为空!', '提示', 0);
    Exit;
  end;
  if CDS_Sub.Locate('BATCHNO', '', []) = True then
  begin
    Application.MessageBox('批号不能为空!', '提示', 0);
    Exit;
  end;

  if CDS_Sub.Locate('chejian', null, []) = True then
  begin
    Application.MessageBox('车间不能为空!', '提示', 0);
    Exit;
  end;
  if CDS_Sub.Locate('chejian', '', []) = True then
  begin
    Application.MessageBox('车间不能为空!', '提示', 0);
    Exit;
  end;

  if CDS_Sub.Locate('SPName', null, []) = True then
  begin
    Application.MessageBox('品名不能为空!', '提示', 0);
    Exit;

  end;
  if CDS_Sub.Locate('CRType;FromFactoryName', VarArrayOf(['外发退回', Null]), [loPartialKey]) = True then
  begin
    Application.MessageBox('外发退回的数据，来自工厂不能为空!', '提示', 0);
    Exit;
  end;
  if CDS_Sub.Locate('CRType;FromFactoryName', VarArrayOf(['蒸丝未做回仓', Null]), [loPartialKey]) = True then
  begin
    Application.MessageBox('蒸丝未做回仓的数据，来自工厂不能为空!', '提示', 0);
    Exit;
  end;
  if CDS_Sub.Locate('CRType;FromFactoryName', VarArrayOf(['染纱未做回仓', Null]), [loPartialKey]) = True then
  begin
    Application.MessageBox('染纱未做回仓的数据，来自工厂不能为空!', '提示', 0);
    Exit;
  end;
  if CDS_Sub.Locate('CRType;FromFactoryName', VarArrayOf(['并线未做回仓', Null]), [loPartialKey]) = True then
  begin
    Application.MessageBox('并线未做回仓的数据，来自工厂不能为空!', '提示', 0);
    Exit;
  end;
  if CDS_Sub.Locate('CRType;FromFactoryName', VarArrayOf(['加捻未做回仓', Null]), [loPartialKey]) = True then
  begin
    Application.MessageBox('加捻未做回仓的数据，来自工厂不能为空!', '提示', 0);
    Exit;
  end;
  if CDS_Sub.Locate('CRType;FromFactoryName', VarArrayOf(['织造未做回仓', Null]), [loPartialKey]) = True then
  begin
    Application.MessageBox('织造未做回仓的数据，来自工厂不能为空!', '提示', 0);
    Exit;
  end;
  if CDS_Sub.Locate('CRType;PBNo', VarArrayOf(['蒸丝未做回仓', Null]), [loPartialKey]) = True then
  begin
    Application.MessageBox('蒸丝未做回仓的数据，白坯单号不能为空!', '提示', 0);
    Exit;
  end;
  if CDS_Sub.Locate('CRType;PBNo', VarArrayOf(['染纱未做回仓', Null]), [loPartialKey]) = True then
  begin
    Application.MessageBox('染纱未做回仓的数据，白坯单号不能为空!', '提示', 0);
    Exit;
  end;
  if CDS_Sub.Locate('CRType;PBNo', VarArrayOf(['并线未做回仓', Null]), [loPartialKey]) = True then
  begin
    Application.MessageBox('并线未做回仓的数据，白坯单号不能为空!', '提示', 0);
    Exit;
  end;
  if CDS_Sub.Locate('CRType;PBNo', VarArrayOf(['加捻未做回仓', Null]), [loPartialKey]) = True then
  begin
    Application.MessageBox('加捻未做回仓的数据，白坯单号不能为空!', '提示', 0);
    Exit;
  end;
  if CDS_Sub.Locate('CRType;PBNo', VarArrayOf(['织造未做回仓', Null]), [loPartialKey]) = True then
  begin
    Application.MessageBox('织造未做回仓的数据，白坯单号不能为空!', '提示', 0);
    Exit;
  end;
  if CDS_Sub.Locate('ToFactoryName', null, []) = True then
  begin
    Application.MessageBox('存放工厂不能为空!', '提示', 0);
    Exit;
  end;
  if CDS_Sub.Locate('FactoryName', null, []) = True then
  begin
    Application.MessageBox('纱线供应商不能为空!', '提示', 0);
    Exit;
  end;
  if CDS_Sub.Locate('PiQty', null, []) = True then
  begin
    Application.MessageBox('箱/件数不能为空!', '提示', 0);
    Exit;
  end;
  if CDS_Sub.Locate('Qty', null, []) = True then
  begin
    Application.MessageBox('数量不能为空!', '提示', 0);
    Exit;
  end;
  if CDS_Sub.Locate('QtyUnit', null, []) = True then
  begin
    Application.MessageBox('数量单位不能为空!', '提示', 0);
    Exit;
  end;
  if SaveCKData() then
  begin
    if Trim(FDelStr) <> '有提示' then
      Application.MessageBox('保存成功!', '提示', 0);
    //ModalResult:=1;
    Exit;
  end;
end;

procedure TfrmShaRKInPutWSQ.v1Column3PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'SXKuWei';
      flagname := '库位';
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('KuWei').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmShaRKInPutWSQ.v1Column14PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  if FCRType = '客户自带入库' then
  begin
    try
      frmKHList := TfrmKHList.Create(Application);
      with frmKHList do
      begin
        if ShowModal = 1 then
        begin
          with Self.CDS_Sub do
          begin
            Edit;
            FieldByName('FactoryNo').Value := Trim(Order_Main.fieldbyname('CoCode').AsString);
            FieldByName('FactoryName').Value := Trim(Order_Main.fieldbyname('CoName').AsString);
            //Post;
          end;
        end;
      end;
    finally
      frmKHList.Free;
    end;
  end
  else
  begin
    try
      frmZdyAttachGYS := TfrmZdyAttachGYS.Create(Application);
      with frmZdyAttachGYS do
      begin
        if ShowModal = 1 then
        begin
          with Self.CDS_Sub do
          begin
            Edit;
            FieldByName('FactoryNo').Value := Trim(CDS_HZ.fieldbyname('CoCode').AsString);
            FieldByName('FactoryName').Value := Trim(CDS_HZ.fieldbyname('CoName').AsString);
            //Post;
          end;
        end;
      end;
    finally
      frmZdyAttachGYS.Free;
    end;
  end;
  {try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='YFFactory';
      flagname:='供应商/加工厂';
      if ShowModal=1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('FactoryNo').Value:=Trim(ClientDataSet1.fieldbyname('ZdyNo').AsString);
          FieldByName('FactoryName').Value:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
          //Post;
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end; }

end;

procedure TfrmShaRKInPutWSQ.v2Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  if Trim(canshu3) = '' then
  begin
    try
      frmZDYHelp := TfrmZDYHelp.Create(Application);
      with frmZDYHelp do
      begin
        flag := 'SXYCLName';
        flagname := '纱线名称';
        fnote := True;
        V1Note.Caption := '预警数量';
//        ViewFlag:=True;
        if ShowModal = 1 then
        begin
          with Self.CDS_Sub do
          begin
            Edit;
            FieldByName('SPName').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
            FieldByName('SPNameGYS').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
          end;
        end;
      end;
    finally
      frmZDYHelp.Free;
    end;
  end
  else
  begin
    try
      frmZdyAttachCP := TfrmZdyAttachCP.Create(Application);
      with frmZdyAttachCP do
      begin
        if ShowModal = 1 then
        begin
          with Self.CDS_Sub do
          begin
            Edit;
            FieldByName('SPName').Value := Trim(frmZdyAttachCP.CDS_HZ.fieldbyname('ZdyName').AsString);
            FieldByName('SPSpec').Value := Trim(frmZdyAttachCP.CDS_HZ.fieldbyname('DEFstr1').AsString);
            FieldByName('SPCF').Value := Trim(frmZdyAttachCP.CDS_HZ.fieldbyname('DEFstr2').AsString);
            FieldByName('SPMF').Value := Trim(frmZdyAttachCP.CDS_HZ.fieldbyname('DEFstr7').AsString);
            FieldByName('SPKZ').Value := Trim(frmZdyAttachCP.CDS_HZ.fieldbyname('DEFstr8').AsString);
            FieldByName('SPKZ').Value := Trim(frmZdyAttachCP.CDS_HZ.fieldbyname('DEFstr8').AsString);
            FieldByName('SPCode').Value := Trim(frmZdyAttachCP.CDS_HZ.fieldbyname('ZdyCode').AsString);
            FieldByName('CPID').Value := Trim(frmZdyAttachCP.CDS_HZ.fieldbyname('ATID').AsString);
          end;
        end;
      end;
    finally
      frmZdyAttachCP.Free;
    end;
  end;

end;

procedure TfrmShaRKInPutWSQ.v1Column5PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'SXKuWei';
      flagname := '库位';
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('KuWei').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmShaRKInPutWSQ.v1Column11PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin

  try
    frmKHList := TfrmKHList.Create(Application);
    with frmKHList do
    begin
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
//          FieldByName('ToFactoryno').Value := Trim(Order_Main.fieldbyname('CoCode').AsString);
          FieldByName('KHNAME').Value := Trim(Order_Main.fieldbyname('CoName').AsString);

            //Post;
        end;
      end;
    end;
  finally
    frmKHList.Free;
  end;
end;

procedure TfrmShaRKInPutWSQ.v1Column16PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZdyAttachGYS := TfrmZdyAttachGYS.Create(Application);
    with frmZdyAttachGYS do
    begin
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('FromFactoryNo').Value := Trim(CDS_HZ.fieldbyname('CoCode').AsString);
          FieldByName('FromFactoryName').Value := Trim(CDS_HZ.fieldbyname('CoName').AsString);
          //Post;
        end;
      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;
  
  {try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='YFFactory';
      flagname:='加工厂';
      if ShowModal=1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('FromFactoryNo').Value:=Trim(ClientDataSet1.fieldbyname('ZdyNo').AsString);
          FieldByName('FromFactoryName').Value:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
          //Post;
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end; }
end;

procedure TfrmShaRKInPutWSQ.Tv1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
//  if CDS_Sub.IsEmpty then
//    Exit;
//  with ADOQueryTemp do
//  begin
//    Close;
//    sql.Clear;
//    sql.Add('select * from CK_SXPB_CR where FZSPID=''' + Trim(CDS_Sub.fieldbyname('SPID').AsString) + '''');
//    sql.Add(' and CRQtyFlag=-1 and ZFFlag=0 ');
//    Open;
//  end;
//  if ADOQueryTemp.IsEmpty = False then
//  begin
//
//    v1FactoryName.Options.Editing := False;
//    v1SXPinPai.Options.Editing := False;
//    v2SPName.Options.Editing := False;
//    v1SPNameGYS.Options.Editing := False;
//    v1SPSpec.Options.Editing := False;
//    v1SPCF.Options.Editing := False;
//    v1BatchNo.Options.Editing := False;
//    v1ToFactoryName.Options.Editing := False;
//  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    if CDS_Sub.IsEmpty = false then
    begin
      if Trim(CDS_MX.fieldbyname('WBID').AsString) <> Trim(CDS_Sub.fieldbyname('SPID').AsString) then
      begin
        sql.Add('select * from GYS_Qty_MX where WBID=''' + Trim(CDS_Sub.fieldbyname('SPID').AsString) + '''');
      end
      else
      begin
        Exit;
      end;
    end
    else
      sql.Add('select * from GYS_Qty_MX where 1=2');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_MX);
  SInitCDSData20(ADOQueryTemp, CDS_MX);
end;

procedure TfrmShaRKInPutWSQ.v1Column10PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'GSTT';
      flagname := '公司抬头';
      frmZDYHelp.TBAdd.Visible := False;
      frmZDYHelp.TBDel.Visible := False;
      frmZDYHelp.TBEdit.Visible := False;
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('ComTaiTou').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmShaRKInPutWSQ.v1Column18PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmContractCGJGSelList := TfrmContractCGJGSelList.Create(Application);
    with frmContractCGJGSelList do
    begin
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('ConNo').Value := frmContractCGJGSelList.Order_Main.fieldbyname('ConNo').Value;
          FieldByName('CGMainId').Value := Trim(frmContractCGJGSelList.Order_Main.fieldbyname('MainId').AsString);
          FieldByName('CGSubId').Value := Trim(frmContractCGJGSelList.Order_Main.fieldbyname('SubId').AsString);
          FieldByName('FactoryName').Value := Trim(frmContractCGJGSelList.Order_Main.fieldbyname('FactoryNoName').AsString);
          FieldByName('FactoryNo').Value := Trim(frmContractCGJGSelList.Order_Main.fieldbyname('FactoryNo').AsString);
          FieldByName('SPName').Value := Trim(frmContractCGJGSelList.Order_Main.fieldbyname('C_CodeName').AsString);
          FieldByName('SPSpec').Value := Trim(frmContractCGJGSelList.Order_Main.fieldbyname('C_Spec').AsString);
          FieldByName('SPCF').Value := Trim(frmContractCGJGSelList.Order_Main.fieldbyname('C_CF').AsString);
        end;
      end;
    end;
  finally
    frmContractCGJGSelList.Free;
  end;
end;

procedure TfrmShaRKInPutWSQ.v1Column8PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'SXSPSpec';
      flagname := '纱线规格';
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('SPSpec').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmShaRKInPutWSQ.v1Column7PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'SXSPCF';
      flagname := '纱线成分';
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('SPCF').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmShaRKInPutWSQ.v1Column15PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'PinPai';
      flagname := '品牌';
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('SXPinPai').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmShaRKInPutWSQ.MXQtyKeyPress(Sender: TObject; var Key: Char);
var
  FReal: Double;
  i: Integer;
begin
  if Key = #13 then
  begin
    if CDS_Sub.IsEmpty then
    begin
      Application.MessageBox('没有新增数据!', '提示', 0);
      MXQty.Text := '';
      MXQty.SetFocus;
      Exit;
    end;
    if Trim(CDS_Sub.fieldbyname('BaoType').AsString) = '' then
    begin
      Application.MessageBox('类型不能为空!', '提示', 0);
      Exit;
    end;
    if Trim(MXQty.Text) = '0' then
    begin
     // MXQty.Text:='';
      //Tv1.Controller.FocusedColumn:=v1Column4;
    end
    else
    begin
      if TryStrToFloat(MXQty.Text, FReal) = False then
      begin
        Application.MessageBox('数量非法数字!', '提示', 0);
        MXQty.Text := '';
        MXQty.SetFocus;
        Exit;
      end;
      if Trim(CDS_Sub.fieldbyname('BaoType').AsString) <> '均重' then
      begin
        with CDS_MX do
        begin
          Append;
          FieldByName('MXPS').Value := 1;
          FieldByName('MXInt').Value := CDS_MX.RecordCount + 1;
          FieldByName('MXQty').Value := MXQty.Text;
          FieldByName('WBID').Value := CDS_Sub.fieldbyname('SPID').AsString;
          Post;
        end;
      end
      else
      begin
        if Trim(PiQty.Text) = '' then
        begin
          Application.MessageBox('均重码单，包数不能为空!', '提示', 0);
          Exit;
        end;
        if TryStrToInt(PiQty.Text, i) = False then
        begin
          Application.MessageBox('包数非法数字!', '提示', 0);
          PiQty.SetFocus;
          Exit;
        end;
        for i := 1 to StrToInt(PiQty.Text) do
        begin
          with CDS_MX do
          begin
            Append;
            FieldByName('MXPS').Value := 1;
            FieldByName('MXInt').Value := CDS_MX.RecordCount + 1;
            FieldByName('MXQty').Value := MXQty.Text;
            FieldByName('WBID').Value := CDS_Sub.fieldbyname('SPID').AsString;
            Post;
          end;
        end;
      end;
      PiQty.Text := '';
      MXQty.Text := '';
      MXQty.SetFocus;
      with CDS_Sub do
      begin
        Edit;
        Post;
      end;
      with CDS_Sub do
      begin
        Edit;
        FieldByName('PiQty').Value := TvMX.DataController.Summary.FooterSummaryValues[0];
        FieldByName('Qty').Value := TvMX.DataController.Summary.FooterSummaryValues[1];
        Post;
      end;
    end;
  end;
end;

procedure TfrmShaRKInPutWSQ.TvMXCellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  if CDS_MX.IsEmpty then
    Exit;
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Delete GYS_Qty_MX where QMID=''' + Trim(CDS_MX.fieldbyname('QMID').AsString) + '''');
    ExecSQL;
  end;
  CDS_MX.Delete;
  with CDS_Sub do
  begin
    Edit;
    FieldByName('PiQty').Value := TvMX.DataController.Summary.FooterSummaryValues[0];
    FieldByName('Qty').Value := TvMX.DataController.Summary.FooterSummaryValues[1];
    Post;
  end;
  FDelStr := '有提示';
  TBSave.Click;
end;

procedure TfrmShaRKInPutWSQ.v1PBNOPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmClothSCHZListSel := TfrmClothSCHZListSel.Create(Application);
    with frmClothSCHZListSel do
    begin
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('PBNO').Value := frmClothSCHZListSel.Order_Main.fieldbyname('ConNo').Value;
        end;
      end;
    end;
  finally
    frmClothSCHZListSel.Free;
  end;
end;

procedure TfrmShaRKInPutWSQ.v1Column7PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
  FQty, FPrice, FCostPrice, FMoney, FBBMoney, fyunfei: double;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  if Trim(mvalue) = '' then
  begin
    mvalue := '0';
  end;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_Sub do
  begin
    Edit;
    FieldByName(FFieldName).Value := mvalue;
    Post;
  end;
  FQty := CDS_Sub.fieldbyname('Qty').AsFloat;
  FPrice := CDS_Sub.fieldbyname('Price').AsFloat;
  FCostPrice := CDS_Sub.fieldbyname('YUNPRICE').AsFloat;
  FMoney := CDS_Sub.fieldbyname('Money').AsFloat;
  if FQty > 0 then
  begin
   // FPrice := RoundFloat(FMoney / FQty, 2)
    FMoney := RoundFloat(FPrice * FQty, 2);
    fyunfei := RoundFloat(FCostPrice * FQty, 2);
  end
  else
    FMoney := 0;

  with CDS_Sub do
  begin
    Edit;
   // FieldByName('Price').Value := FPrice;
    FieldByName('MONEY').Value := FMoney;
    FieldByName('YunFei').Value := fyunfei;
    Post;
  end;

  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmShaRKInPutWSQ.v1Column4PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
var
  fsj: string;
  FWZ: Integer;
begin

  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'SXSPCOLOR';
      flagname := '纱线颜色';

      if ShowModal = 1 then
      begin
        with cds_sub do
        begin
          edit;
          FieldByName('spcolor').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);

        end;

      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmShaRKInPutWSQ.v1BatchNoPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'SXbatchno';
      flagname := '纱线批号';
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('batchno').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmShaRKInPutWSQ.v1Column12PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'SXnote';
      flagname := '纱线备注';
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('note').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

end.

