unit U_SXCKInPut;

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;

type
  TfrmSXCKInPut = class(TForm)
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column2: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    ToolBar1: TToolBar;
    TBAdd: TToolButton;
    TBDel: TToolButton;
    TBSave: TToolButton;
    TBClose: TToolButton;
    v1Column4: TcxGridDBColumn;
    DataSource3: TDataSource;
    CDS_Sub: TClientDataSet;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    cxGridPopupMenu2: TcxGridPopupMenu;
    v1Column12: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    v1Column16: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    v1Column18: 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 v1Column13PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure Tv1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure v1Column17PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1Column8PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1Column7PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1Column1PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1Column10PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
  private
    { Private declarations }
    function SaveCKData():Boolean;
    function  YFData():Boolean;
  public
    { Public declarations }
    FBCId,canshu3:String;


  end;

var
  frmSXCKInPut: TfrmSXCKInPut;

implementation
uses
  U_DataLink,U_RTFun,U_ClothContractListHZCX,U_ZDYHelp, U_ZdyAttachGYS,U_ProductOrderListSel,
  U_ZdyAttachCP;

{$R *.dfm}

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

procedure TfrmSXCKInPut.TBAddClick(Sender: TObject);
begin
  if CDS_Sub.IsEmpty=False then
  begin
    CopyAddRowCDS(CDS_Sub);
    with CDS_Sub do
    begin
      Edit;
      FieldByName('SPID').Value:=null;
      post;
    end;
  end else
  begin
    with CDS_Sub do
    begin
      Append;
      FieldByName('CRTime').Value:=Trim(FormatDateTime('yyyy-MM-dd',Now));
      Post;
    end;
  end;
  
end;
function TfrmSXCKInPut.SaveCKData():Boolean;
var
  FCRID,Maxno,FFSPID: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_KC where BatchNo='''+Trim(CDS_Sub.fieldbyname('BatchNo').AsString)+'''');
          SQL.Add(' and isnull(CKName,'''')=''纱线'' ');
          Open;
        end;
        if ADOQueryTemp.IsEmpty=False then
        begin
          FCRID:=Trim(ADOQueryTemp.fieldbyname('CRID').AsString);
        end else
        begin
          with ADOQueryTemp do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from CK_SXPB_CRID');
            Open;
          end;
          if ADOQueryTemp.IsEmpty then
          begin
            with ADOQueryCmd do
            begin
              Close;
              sql.Clear;
              sql.Add('insert into CK_SXPB_CRID select 0');
              sql.Add(' select * from CK_SXPB_CRID');
              Open;
            end;
            FCRID:=Trim(ADOQueryCmd.fieldbyname('CRID').AsString);
          end else
          begin
            with ADOQueryCmd do
            begin
              Close;
              sql.Clear;
              sql.Add('Update CK_SXPB_CRID Set CRID=CRID+1');
              sql.Add(' select * from CK_SXPB_CRID');
              Open;
            end;
            FCRID:=Trim(ADOQueryCmd.fieldbyname('CRID').AsString);
          end;

          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from CK_SXPB_KC where CRID='+FCRID);
            Open;
          end;
          with ADOQueryCmd do
          begin
            Append;
            FieldByName('CRID').Value:=FCRID;
            FieldByName('CKName').Value:='纱线';
            FieldByName('BatchNo').Value:=CDS_Sub.fieldbyname('BatchNo').Value;
            FieldByName('SPType').Value:=CDS_Sub.fieldbyname('SPType').Value;
            FieldByName('FactoryName').Value:=CDS_Sub.fieldbyname('FactoryName').Value;
            FieldByName('FactoryNo').Value:=CDS_Sub.fieldbyname('FactoryNo').Value;
            FieldByName('SPName').Value:=CDS_Sub.fieldbyname('SPName').Value;
            FieldByName('SPCF').Value:=CDS_Sub.fieldbyname('SPCF').Value;
            FieldByName('ShaZhi').Value:=CDS_Sub.fieldbyname('ShaZhi').Value;
            FieldByName('SeBie').Value:=CDS_Sub.fieldbyname('SeBie').Value;
            FieldByName('KCNote').Value:=CDS_Sub.fieldbyname('Note').Value;
            Post;
          end;
        end; }
        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,'PR','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('FromFactoryNo').Value:=Trim(CDS_Sub.fieldbyname('FromFactoryNo').AsString);
          FieldByName('ToFactoryNo').Value:=Trim(CDS_Sub.fieldbyname('ToFactoryNo').AsString);
          FieldByName('ORDMainIdRK').Value:=Trim(CDS_Sub.fieldbyname('ORDMainIdRK').AsString);
          FieldByName('ORDSubIdRK').Value:=Trim(CDS_Sub.fieldbyname('ORDSubIdRK').AsString);
          FieldByName('CPID').Value:=Trim(CDS_Sub.fieldbyname('CPID').AsString);
          FieldByName('CRFlag').Value:='入库';
          FieldByName('CRQtyFlag').Value:=1;
          FieldByName('price').Value:=CDS_Sub.fieldbyname('price').asfloat;
          FieldByName('money').Value:=cds_sub.fieldbyname('price').AsFloat*cds_sub.fieldbyname('Qty').AsFloat;
          //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;
          if Trim(CDS_Sub.fieldbyname('OrderNo').AsString)<>'' then
          begin
            FieldByName('QCOrderNo').Value:=Trim(CDS_Sub.fieldbyname('OrderNo').AsString);
          end;
          Post;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update CK_SXPB_CR Set MXKCQty=(select sum(isnull(Qty,0)*CRQtyFlag) from CK_SXPB_CR A where A.FZSPID=CK_SXPB_CR.SPID)');
          sql.Add(',MXKCPiQty=(select sum(isnull(PiQty,0)*CRQtyFlag) from CK_SXPB_CR A where A.FZSPID=CK_SXPB_CR.SPID)');
          sql.Add(',MXKCKGQty=(select sum(isnull(kgQty,0)*CRQtyFlag) from CK_SXPB_CR A where A.FZSPID=CK_SXPB_CR.SPID)');
          sql.Add(' where SPID='''+Trim(Maxno)+'''');
          ExecSQL;
        end;


      {  with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from CK_SXPB_CR where SPID='''+Trim(Maxno)+'''');
          Open;
        end;
        if ADOQueryTemp.FieldByName('MXKCQty').Value<0 then
        begin
          CDS_Sub.DisableControls;
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('入库数小于出库数不能保存!','提示',0);
          Exit;
        end;  }
        {with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update CK_SXPB_KC Set KCKGQty=(select sum(isnull(KGQty,0)*CRQtyFlag) from CK_SXPB_CR A where A.CRID=CK_SXPB_KC.CRID)');
          sql.Add(',KCPiQty=(select sum(isnull(PiQty,0)*CRQtyFlag) from CK_SXPB_CR A where A.CRID=CK_SXPB_KC.CRID)');
          sql.Add(' where CRID='+FCRID);
          ExecSQL;
        end;
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from CK_SXPB_KC where CRID='+Trim(FCRID));
          Open;
        end;
        if ADOQueryTemp.FieldByName('KCKGQty').Value<0 then
        begin
          CDS_Sub.DisableControls;
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('入库公斤数小于出库公斤数不能保存!','提示',0);
          Exit;
        end;
         }
       { if ADOQueryTemp.FieldByName('KCKeQty').Value<0 then
        begin
          CDS_Sub.DisableControls;
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('入库颗数小于出库颗数不能保存!','提示',0);
          Exit;
        end; }
        Edit;
        FieldByName('SPID').Value:=Trim(Maxno);
        IF Trim(cds_sub.fieldbyname('CRType').AsString)='采购入库' then
        begin
          IF not YFData() then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            application.MessageBox('生成应付款失败！','提示信息',0);
          end;
        end;

        IF Trim(cds_sub.fieldbyname('CRType').AsString)='加工入库' then
        begin
          IF not YFData() then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            application.MessageBox('生成应付款失败！','提示信息',0);
          end;
        end;

        //FieldByName('CRID').Value:=Trim(FCRID);
        Post;
        Next;
      end;
    end;
    CDS_Sub.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    Result:=False;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存异常!','提示',0);
  end;

end;

function TfrmSXCKInPut.YFData():Boolean;
var
  CRID,OrdMainId,YFID,FComTaiTou,FCRID,FFactoryName:String;
begin
  Result:=False;
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('select * from YF_Money_KC where FactoryName='+quotedstr(Trim(cds_sub.fieldbyname('factoryName').AsString)));
    Open;
  end;
  if ADOQueryTemp.IsEmpty=False then
  begin
    CRID:=ADOQueryTemp.fieldbyname('CRID').AsString;
  end
  else
  begin
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update YF_Money_CRID set CRID=CRID+1');
      sql.Add('select * from YF_Money_CRID ');
      Open;
    end;
    CRID:=ADOQueryCmd.fieldbyname('CRID').AsString;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from YF_Money_KC where 1<>1');
      Open;
    end;
    with ADOQueryCmd do
    begin
      Append;
      FieldByName('CRID').Value:=StrToInt(CRID);
      FieldByName('FactoryName').Value:=Trim(cds_sub.fieldbyname('factoryName').AsString);
      FieldByName('ZdyStr1').Value:='应付付';
      Post;
    end;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YF_Money_CR where FactoryName='+quotedstr(Trim(cds_sub.fieldbyname('factoryName').AsString)));
    sql.Add(' and mainID='''+Trim(cds_sub.fieldbyname('SPID').AsString)+'''');
 //   sql.Add(' and subID='''+Trim(Order_Sub.fieldbyname('subID').AsString)+'''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty then
  begin
    if GetLSNo(ADOQueryCmd,YFID,'YF','YF_Money_CR',3,1)=False then
    begin
      Application.MessageBox('取坯布应付最大号失败!','提示',0);
      Exit;
    end;
  end
  else
  begin
    YFID:=Trim(ADOQueryTemp.fieldbyname('YFID').AsString);
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YF_Money_CR where YFID='''+Trim(YFID)+'''');
    Open;
  end;
  with ADOQueryCmd do
  begin
    if ADOQueryTemp.IsEmpty then
      Append
    else
      Edit;
    FieldByName('YFID').Value:=Trim(YFID);
    FieldByName('YFTypeId').Value:=Trim(cds_sub.fieldbyname('SPID').AsString);
    FieldByName('CRID').Value:=StrToInt(CRID);
    FieldByName('Filler').Value:=Trim(DName);
    FieldByName('CRType').Value:='应付款登记';
    FieldByName('CRFlag').Value:='应付付';
    FieldByName('QtyFlag').Value:=1;
    FieldByName('FactoryName').Value:=Trim(cds_sub.fieldbyname('factoryName').AsString);
    FieldByName('CRTime').Value:=cds_sub.fieldbyname('CRTime').AsDateTime;
    FieldByName('Qty').Value:=cds_sub.fieldbyname('Qty').AsFloat;
    FieldByName('PS').Value:=cds_sub.fieldbyname('PiQty').AsFloat;
    FieldByName('YFType').Value:='自动生成';
    FieldByName('Price').Value:=cds_sub.fieldbyname('price').AsFloat;
    FieldByName('money').Value:=cds_sub.fieldbyname('price').AsFloat*cds_sub.fieldbyname('Qty').AsFloat;
    FieldByName('BBmoney').Value:=cds_sub.fieldbyname('price').AsFloat*cds_sub.fieldbyname('Qty').AsFloat;;
    FieldByName('HuiLv').Value:=1;
    FieldByName('BZType').Value:='RMB';
    FieldByName('ComTaiTou').Value:='';
    FieldByName('QtyUnit').Value:=Trim(cds_sub.fieldbyname('QtyUnit').AsString);
    FieldByName('orderNo').Value:=Trim(cds_sub.fieldbyname('OrderNo').AsString);
    FieldByName('P_CodeName').Value:=Trim(cds_sub.fieldbyname('SPName').AsString);
   // FieldByName('P_Color').Value:=Trim(CDS_Main.fieldbyname('QtyUnit').AsString);
    IF Trim(cds_sub.fieldbyname('CRType').AsString)='染色完成' then
      FieldByName('YFName').Value:='染费'
    else
    IF Trim(cds_sub.fieldbyname('CRType').AsString)='采购入库' then
      FieldByName('YFName').Value:='坯布费'
    else
    IF Trim(cds_sub.fieldbyname('CRType').AsString)='成品采购入库' then
      FieldByName('YFName').Value:='成品采购费'
    else
    IF Trim(cds_sub.fieldbyname('CRType').AsString)='回修完成' then
      FieldByName('YFName').Value:='回修费'
    else
    IF Trim(cds_sub.fieldbyname('CRType').AsString)='退货出库' then
      FieldByName('YFName').Value:='退货'
    else
    FieldByName('YFName').Value:='加工费';
    FieldByName('MainId').Value:=Trim(cds_sub.fieldbyname('SPID').AsString);
  //  FieldByName('subId').Value:=Trim(Order_Sub.fieldbyname('subId').AsString);
    FieldByName('status').Value:='0';
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update YF_Money_KC Set KCMoney=(select isnull(Sum(Money*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID)');
    sql.Add(',KCBBMoney=(select isnull(Sum(BBMoney*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID)');
    sql.Add(' where CRID='+CRID);
    ExecSQL;
  end;
  Result:=True;
end;
procedure TfrmSXCKInPut.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('纱线入库登记',Tv1,'纱线仓库');
  Close;
end;

procedure TfrmSXCKInPut.FormShow(Sender: TObject);
var
  fsj:String;
begin
  ReadCxGrid('坯布入库登记',Tv1,'坯布仓库');
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(' select A.* ');
    sql.Add(',OrderNO=(select OrderNO from JYOrder_Main JM where JM.MainId=A.ORDMainIdRK)');
    sql.Add(' from CK_SXPB_CR A');
    sql.Add(' where SPID='''+Trim(FBCId)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_Sub);
  SInitCDSData20(ADOQueryTemp,CDS_Sub);
  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');
    Open;
  end;
  if ADOQueryTemp.IsEmpty=False then
  begin
    v1Column4.Options.Focusing:=False;
    v1Column15.Options.Focusing:=False;
    v1Column13.Options.Focusing:=False;
    v1Column14.Options.Focusing:=False;
    v2Column1.Options.Focusing:=False;
    v1Column8.Options.Focusing:=False;
    v1Column7.Options.Focusing:=False;
    v1Column1.Options.Focusing:=False;
    v1Column10.Options.Focusing:=False;
    v1Column11.Options.Focusing:=False;
    Exit;
  end else
  begin
    v1Column4.Options.Focusing:=True;
    v1Column15.Options.Focusing:=True;
    v1Column13.Options.Focusing:=True;
    v1Column14.Options.Focusing:=True;
    v2Column1.Options.Focusing:=True;
    v1Column8.Options.Focusing:=True;
    v1Column7.Options.Focusing:=True;
    v1Column1.Options.Focusing:=True;
    v1Column10.Options.Focusing:=True;
    v1Column11.Options.Focusing:=True;
  end;
end;

procedure TfrmSXCKInPut.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;
      ADOQueryCmd.Connection.CommitTrans;
      CDS_Sub.Delete;
    except
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('删除异常!','提示',0);
    end;

  end else
  begin
    CDS_Sub.Delete;
  end;

end;

procedure TfrmSXCKInPut.TBSaveClick(Sender: TObject);
begin
  if CDS_Sub.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('SSType',null,[])=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('SPCF',null,[])=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 CDS_Sub.Locate('CRType;FromFactoryName',VarArrayOf(['生产退回',Null]),[loPartialKey])=True then
  begin
    Application.MessageBox('生产退回的数据，来自工厂不能为空!','提示',0);
    Exit;
  end;
  if SaveCKData() then
  begin
    Application.MessageBox('保存成功!','提示',0);
    //ModalResult:=1;
    Exit;
  end;
end;

procedure TfrmSXCKInPut.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 TfrmSXCKInPut.v1Column14PropertiesButtonClick(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('FactoryNo').Value:=Trim(frmZdyAttachGYS.CDS_HZ.fieldbyname('ZdyCode').AsString) ;
          FieldByName('FactoryName').Value:=Trim(frmZdyAttachGYS.CDS_HZ.fieldbyname('ZdyName').AsString);
          //Post;
        end;
      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;

end;

procedure TfrmSXCKInPut.v2Column1PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  if Trim(canshu3)='' then
  begin
    try
      frmZDYHelp:=TfrmZDYHelp.Create(Application);
      with frmZDYHelp do
      begin
        flag:='PBName';
        flagname:='坯布名称';
        if ShowModal=1 then
        begin
          with Self.CDS_Sub do
          begin
            Edit;
            FieldByName('SPName').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 TfrmSXCKInPut.v1Column5PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='KuWei';
      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 TfrmSXCKInPut.v1Column11PropertiesButtonClick(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('ToFactoryNo').Value:=Trim(frmZdyAttachGYS.CDS_HZ.fieldbyname('ZdyCode').AsString) ;
          FieldByName('ToFactoryName').Value:=Trim(frmZdyAttachGYS.CDS_HZ.fieldbyname('ZdyName').AsString);
          //Post;
        end;
      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;
end;

procedure TfrmSXCKInPut.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(frmZdyAttachGYS.CDS_HZ.fieldbyname('ZdyCode').AsString) ;
          FieldByName('FromFactoryName').Value:=Trim(frmZdyAttachGYS.CDS_HZ.fieldbyname('ZdyName').AsString);
          //Post;
        end;
      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;
end;

procedure TfrmSXCKInPut.v1Column13PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmProductOrderListSel:=TfrmProductOrderListSel.Create(Application);
    with frmProductOrderListSel do
    begin
      if ShowModal=1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('ORDMainIdRK').Value:=Trim(frmProductOrderListSel.Order_Main.fieldbyname('MainId').AsString);
          FieldByName('ORDSubIdRK').Value:=Trim(frmProductOrderListSel.Order_Main.fieldbyname('SubId').AsString);
          FieldByName('OrderNo').Value:=Trim(frmProductOrderListSel.Order_Main.fieldbyname('OrderNo').AsString);
          FieldByName('qty').Value:='0';
        end;
      end;
    end;
  finally
    frmProductOrderListSel.Free;
  end;
end;

procedure TfrmSXCKInPut.Tv1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if CDS_Sub.IsEmpty then Exit;
  if Trim(canshu3)='' then exit;
  v1Column17.Options.Editing:=False;
  v1Column8.Options.Editing:=False;
  v1Column7.Options.Editing:=False;
  (v2Column1.Properties as TcxButtonEditProperties).ReadOnly:=True;
end;

procedure TfrmSXCKInPut.v1Column17PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
    try
      frmZDYHelp:=TfrmZDYHelp.Create(Application);
      with frmZDYHelp do
      begin
        flag:='PBCode';
        flagname:='坯布编号';
        if ShowModal=1 then
        begin
          with Self.CDS_Sub do
          begin
            Edit;
            FieldByName('SPcode').Value:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
          end;
        end;
      end;
    finally
      frmZDYHelp.Free;
    end;
end;

procedure TfrmSXCKInPut.v1Column8PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
    try
      frmZDYHelp:=TfrmZDYHelp.Create(Application);
      with frmZDYHelp do
      begin
        flag:='PBspec';
        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 TfrmSXCKInPut.v1Column7PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
    try
      frmZDYHelp:=TfrmZDYHelp.Create(Application);
      with frmZDYHelp do
      begin
        flag:='PBKF';
        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 TfrmSXCKInPut.v1Column1PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
    try
      frmZDYHelp:=TfrmZDYHelp.Create(Application);
      with frmZDYHelp do
      begin
        flag:='PBMF';
        flagname:='坯布门幅';
        if ShowModal=1 then
        begin
          with Self.CDS_Sub do
          begin
            Edit;
            FieldByName('SPMF').Value:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
          end;
        end;
      end;
    finally
      frmZDYHelp.Free;
    end;
end;

procedure TfrmSXCKInPut.v1Column10PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
    try
      frmZDYHelp:=TfrmZDYHelp.Create(Application);
      with frmZDYHelp do
      begin
        flag:='PBKZ';
        flagname:='坯布克重';
        if ShowModal=1 then
        begin
          with Self.CDS_Sub do
          begin
            Edit;
            FieldByName('SPKZ').Value:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
          end;
        end;
      end;
    finally
      frmZDYHelp.Free;
    end;
end;

end.
