unit U_CKProductCK_Other;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, cxGridCustomTableView,
  cxGridTableView, cxGridBandedTableView, cxGridDBBandedTableView,
  cxGridLevel, cxClasses, cxControls, cxGridCustomView, cxGridDBTableView,
  cxGrid, StdCtrls, ComCtrls, ExtCtrls, ToolWin, cxGridCustomPopupMenu,
  cxGridPopupMenu, ADODB, DBClient, cxDropDownEdit, MovePanel, cxCheckBox,
  BtnEdit,strutils, cxSplitter;

type
  TfrmCKProductCK_Other = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    CDS_Main: TClientDataSet;
    Tv1: TcxGridDBTableView;
    cxGrid2Level1: TcxGridLevel;
    cxGrid2: TcxGrid;
    v2Column1: TcxGridDBColumn;
    v2SSEL: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    Panel4: TPanel;
    Label11: TLabel;
    Panel10: TPanel;
    Image2: TImage;
    RichEdit1: TRichEdit;
    MovePanel2: TMovePanel;
    ToolButton1: TToolButton;
    v1KCRollNum: TcxGridDBColumn;
    v1gangNO: TcxGridDBColumn;
    v1C_Color: TcxGridDBColumn;
    CDS_SubSel: TClientDataSet;
    DSSubSel: TDataSource;
    cxGridPopupMenu2: TcxGridPopupMenu;
    v1KCKW: TcxGridDBColumn;
    Panel2: TPanel;
    Panel1: TPanel;
    Label3: TLabel;
    Label4: TLabel;
    Label1: TLabel;
    Label2: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Label7: TLabel;
    Label10: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    C_CodeName: TEdit;
    C_Color: TEdit;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    conNo: TEdit;
    MJID: TEdit;
    KZ: TEdit;
    MF: TEdit;
    CPType: TComboBox;
    KCQtyStr: TEdit;
    AOrdDefStr1: TEdit;
    KCKW: TEdit;
    GroupBox1: TGroupBox;
    Label14: TLabel;
    Label15: TLabel;
    CRTIME: TDateTimePicker;
    defstr1: TBtnEditA;
    CRType: TComboBox;
    Label16: TLabel;
    Label17: TLabel;
    NOTE: TEdit;
    Label18: TLabel;
    Label19: TLabel;
    CheckBox1: TCheckBox;
    cxSplitter1: TcxSplitter;
    cxGrid1: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridDBColumn9: TcxGridDBColumn;
    cxGridDBColumn10: TcxGridDBColumn;
    cxGridDBColumn11: TcxGridDBColumn;
    cxGridDBColumn12: TcxGridDBColumn;
    cxGridDBColumn13: TcxGridDBColumn;
    cxGridDBColumn14: TcxGridDBColumn;
    cxGridDBColumn15: TcxGridDBColumn;
    cxGridDBColumn16: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Button1: TButton;
    v1KCnote: TcxGridDBColumn;
    Label20: TLabel;
    Edit1: TEdit;
    Edit2: TEdit;
    Label21: TLabel;
    Label22: TLabel;
    P_CodeName: TBtnEditA;
    P_Color: TBtnEditA;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ConNoMChange(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure C_CodeNameChange(Sender: TObject);
    procedure Panel10MouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure Image2Click(Sender: TObject);
    procedure Tv1MouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure ToolButton1Click(Sender: TObject);
    procedure Tv2CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure defstr1BtnClick(Sender: TObject);
    procedure v2SSELPropertiesChange(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure Button1Click(Sender: TObject);
    procedure P_CodeNameBtnClick(Sender: TObject);
    procedure P_ColorBtnClick(Sender: TObject);
    procedure P_CodeNameExit(Sender: TObject);
    procedure P_ColorExit(Sender: TObject);
    procedure CRTIMEKeyPress(Sender: TObject; var Key: Char);
  private
    FLeft,FTop:Integer;
    IsCreateSel:boolean;
    procedure InitGrid();
  //  procedure InitGridNull();
    function savedate():boolean;
    function SaveJG(fCkNO:string):boolean;
    function SaveJG10(fCkNO:string):boolean;
    function SaveEjK(FNO,FID,fBCID,fCkNO:string):boolean;
    { Private declarations }
  public
    fkeyNo:string;
    { Public declarations }
  end;

var
  frmCKProductCK_Other: TfrmCKProductCK_Other;

implementation
uses
  U_DataLink,U_Fun10, U_ZDYHelp, U_ZdyAttachGYS, U_ZdyAttachCP;

{$R *.dfm}
function TfrmCKProductCK_Other.SaveJG10(fCkNO:string):boolean;
var
  FmainID,fsubID,fconNo:string;
  fRollNum:double;
  fQty:double;
begin
  result:=false;
  fQty:=0;
  fRollNum:=0;

 { with ADOQueryTemp do
  begin
    close;
    sql.Clear;
    sql.Add('select C_Code,C_CodeName,C_Spec,C_Color,BCGangNO,QtyUnit,sum(RollNum) as RollNum,suM(Qty) as Qty ');
    sql.Add('from CK_BanCP_CR A');
    sql.Add('where ckOrdNo='+quotedstr(trim(fCkNO)));
    sql.Add('Group by C_Code,C_CodeName,C_Spec,C_Color,BCGangNO,QtyUnit');
    open;
  end; }

  with ADOQueryTemp do
  begin
    close;
    sql.Clear;
    sql.Add('select BCGangNO='''',QtyUnit=''M'',sum(RollNum) as RollNum,suM(Qty) as Qty ');
    sql.Add('from CK_BanCP_CR1 A');
    sql.Add('where ckOrdNo='+quotedstr(trim(fCkNO)));
  //  sql.Add('Group by C_Code,C_CodeName,C_Spec,C_Color,BCGangNO,QtyUnit');
    open;
  end;



  try

    if GetLSNo(ADOQueryCmd,fmainID,'CM','Contract_Main',3,1)=False then
    begin
    //  ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('生成流水号异常！','提示',0);
      exit;
    end;

    if GetLSNo(ADOQueryCmd,fconNo,'H','Contract_Main',3,1)=False then
    begin
    //  ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('生成流水号异常！','提示',0);
      exit;
    end;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from Contract_Main where 1<>2 ');
      Open;
    end;

    with ADOQueryCmd do
    begin
      append;
      Fieldbyname('MainID').Value:=fmainID;
      Fieldbyname('ConNO').Value:=fconNo;
      Fieldbyname('QDTime').Value:=formatDateTime('yyyy-MM-dd',CRTIME.Date);
      Fieldbyname('deliverydate').Value:=formatDateTime('yyyy-MM-dd',CRTIME.Date);
      Fieldbyname('factoryNO').Value:=trim(defstr1.TxtCode);
      Fieldbyname('factoryNoName').Value:=trim(defstr1.Text);
      FieldByName('Filler').Value:=Trim(DName);
      FieldByName('FillTime').Value:=date();
      FieldByName('ConType').Value:='成品后加工';
      IF trim(CRType.Text)='加工出库' then
      FieldByName('conTk1').Value:='正常加工';
      IF trim(CRType.Text)='回修出库' then
      FieldByName('conTk1').Value:='回修加工';
      FieldByName('conTk2').Value:=trim(fCkNO);
      FieldByName('status').Value:='0';
      post;
    end;


    while not ADOQueryTemp.Eof do
    begin
      if GetLSNo(ADOQueryCmd,fsubID,'CS','Contract_Sub',3,1)=False then
      begin
       // ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('生成流水号异常！','提示',0);
        exit;
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from Contract_sub where 1<>2 ');
        Open;
      end;
      with ADOQueryCmd do
      begin
        append;
        Fieldbyname('MainID').Value:=fmainID;
        Fieldbyname('subID').Value:=fsubID;
        Fieldbyname('ConNO').Value:=fconNo;
        Fieldbyname('C_Code').Value:='';
        Fieldbyname('C_CodeName').Value:=trim(P_CodeName.text);
        Fieldbyname('C_Spec').Value:='';
        Fieldbyname('C_Color').Value:=trim(P_color.text);
        Fieldbyname('BCGangNo').Value:=trim(ADOQueryTemp.fieldbyname('BCGangNo').AsString);
        Fieldbyname('C_UNit').Value:=trim(ADOQueryTemp.fieldbyname('QtyUnit').AsString);
        Fieldbyname('Qty1').Value:=ADOQueryTemp.fieldbyname('RollNum').AsFloat;
        Fieldbyname('C_Qty').Value:=ADOQueryTemp.fieldbyname('Qty').AsFloat;
        Fieldbyname('C_Status').Value:='0';
        post;
      end;
      ADOQueryTemp.Next;
    end;
    result:=true;
  except
  end;
end;
function TfrmCKProductCK_Other.SaveEjK(FNO,FID,fBCID,fCkNO:string):boolean;
var
  fkcID:integer;
  Filler:string;
  FillTime:TdateTime;
begin
  result:=false;
  try

    with ADOQueryCmd  do
    begin
      Close;
      sql.Clear;
      SQL.add('Update BP_Kcid Set kcid=kcid+1');
      sql.Add('select kcid  from BP_Kcid');
      Open;
    end;
    fkcID:=ADOQueryCmd.fieldbyname('kcid').AsInteger;

    with CDS_SubSel do
    begin
       with ADOQueryCmd do
       begin
          close;
          sql.Clear;
          sql.Add('select * from BP_InOut');
          sql.Add('where 1=2 ');
          open;
        end;
      //  fkcID:=strtoint(RightBStr(trim(FNO),length(FNO)-3)+trim(FID));
        ADOQueryCmd.Append;
        ADOQueryCmd.FieldByName('CRNO').Value:=trim(FNO);
        ADOQueryCmd.FieldByName('CRID').Value:=FID;
        ADOQueryCmd.FieldByName('CRTime').Value:=formatdateTime('yyyy-MM-dd',CRTIme.Date);
        IF trim(CRType.Text)='样品出库' then
        begin
        ADOQueryCmd.FieldByName('CKName').Value:='龙运样品仓库';
        ADOQueryCmd.FieldByName('CRType').Value:='龙运样品调拨入库';
        end;
        IF trim(CRType.Text)='广东出库' then
        begin
        ADOQueryCmd.FieldByName('CKName').Value:='广东仓库';
        ADOQueryCmd.FieldByName('CRType').Value:='广东调拨入库';
        end;
        ADOQueryCmd.FieldByName('CRFlag').Value:='入库';
        ADOQueryCmd.FieldByName('mainID').Value:=trim(fieldbyname('mainID').AsString);
        ADOQueryCmd.FieldByName('subID').Value:=trim(fieldbyname('subID').AsString);
        ADOQueryCmd.FieldByName('MJID').Value:=trim(fieldbyname('MJID').AsString);
        ADOQueryCmd.FieldByName('MJXH').Value:=trim(fieldbyname('MJXH').AsString);
        ADOQueryCmd.FieldByName('CPTYPE').Value:=trim(fieldbyname('CPTYPE').AsString);
        ADOQueryCmd.FieldByName('P_Code').Value:=trim(fieldbyname('C_Code').AsString);
        ADOQueryCmd.FieldByName('P_CodeName').Value:=trim(fieldbyname('C_CodeName').AsString);
        ADOQueryCmd.FieldByName('P_spec').Value:=trim(fieldbyname('C_spec').AsString);
        ADOQueryCmd.FieldByName('P_Color').Value:=trim(fieldbyname('C_Color').AsString);
     //   ADOQueryCmd.FieldByName('P_MF').Value:=fieldbyname('P_MF').AsFloat;
     //   ADOQueryCmd.FieldByName('P_KZ').Value:=fieldbyname('P_KZ').AsFloat;
        ADOQueryCmd.FieldByName('RollNum').Value:=fieldbyname('KCRollNum').AsFloat;
        ADOQueryCmd.FieldByName('Qty').Value:=fieldbyname('KCQty').AsFloat;
        ADOQueryCmd.FieldByName('QtyUnit').Value:=trim(fieldbyname('kcQtyUnit').AsString);
        ADOQueryCmd.FieldByName('note').Value:='';
        ADOQueryCmd.FieldByName('Filler').Value:=trim(Filler);
        ADOQueryCmd.FieldByName('FillTime').Value:=SGetServerDateTime(ADOQueryTemp);
        ADOQueryCmd.FieldByName('fromMainID').Value:=trim(fCkNO);
        ADOQueryCmd.FieldByName('FromSubID').Value:=trim(FbcID);
        ADOQueryCmd.FieldByName('gangNO').Value:=trim(fieldbyname('AOrdDefStr1').AsString);
        ADOQueryCmd.FieldByName('kcID').Value:=fkcID;
        ADOQueryCmd.Post;


        with ADOQueryCmd do
        begin
          close;
          sql.Clear;
          sql.Add('select * from BP_KC');
          sql.Add('where 1=2 ');
          open;
        end;
        ADOQueryCmd.Append;
        ADOQueryCmd.FieldByName('kcID').Value:=fkcID;
        IF trim(CRType.Text)='样品出库' then
        begin
        ADOQueryCmd.FieldByName('CKName').Value:='龙运样品仓库';
        ADOQueryCmd.FieldByName('CRType').Value:='龙运样品调拨入库';
        end;
        IF trim(CRType.Text)='广东出库' then
        begin
        ADOQueryCmd.FieldByName('CKName').Value:='广东仓库';
        ADOQueryCmd.FieldByName('CRType').Value:='广东调拨入库';
        end;
        ADOQueryCmd.FieldByName('CRNO').Value:=trim(fNo);
        ADOQueryCmd.FieldByName('CRID').Value:=fID;
        ADOQueryCmd.FieldByName('CRTime').Value:=formatdateTime('yyyy-MM-dd',CRTIme.Date);
        ADOQueryCmd.FieldByName('CRFlag').Value:='入库';
        ADOQueryCmd.FieldByName('mainID').Value:=trim(fieldbyname('mainID').AsString);
        ADOQueryCmd.FieldByName('subID').Value:=trim(fieldbyname('subID').AsString);
        ADOQueryCmd.FieldByName('MJXH').Value:=trim(fieldbyname('MJXH').AsString);
        ADOQueryCmd.FieldByName('MJID').Value:=trim(fieldbyname('MJID').AsString);
        ADOQueryCmd.FieldByName('CPType').Value:=trim(fieldbyname('CPType').AsString);
        ADOQueryCmd.FieldByName('P_Code').Value:=trim(fieldbyname('C_Code').AsString);
        ADOQueryCmd.FieldByName('P_CodeName').Value:=trim(fieldbyname('C_CodeName').AsString);
        ADOQueryCmd.FieldByName('P_spec').Value:=trim(fieldbyname('C_spec').AsString);
        ADOQueryCmd.FieldByName('P_Color').Value:=trim(fieldbyname('C_Color').AsString);
    //    ADOQueryCmd.FieldByName('P_MF').Value:=fieldbyname('P_MF').AsFloat;
    //    ADOQueryCmd.FieldByName('P_KZ').Value:=fieldbyname('P_KZ').AsFloat;
        ADOQueryCmd.FieldByName('RollNum').Value:=fieldbyname('KCRollNum').AsFloat;
        ADOQueryCmd.FieldByName('Qty').Value:=fieldbyname('kCQty').AsFloat;
        ADOQueryCmd.FieldByName('QtyUnit').Value:=trim(fieldbyname('KCQtyUnit').AsString);
        ADOQueryCmd.FieldByName('kcID').Value:=fkcID;
        ADOQueryCmd.Post;
      end;
    result:=true;
  except
  end;
end;
function TfrmCKProductCK_Other.SaveJG(fCkNO:string):boolean;
var
  maxno,LLIdx,dhID,FDW:string;
  fRollNum:double;
  fQty:double;
begin
  result:=false;
  fQty:=0;
  fRollNum:=0;

  with ADOQueryTemp do
  begin
    close;
    sql.Clear;
    sql.Add('select MainID,subID,BCGangNO,C_Color,QtyUnit,sum(RollNum) as RollNum,suM(Qty) as Qty ');
    sql.Add('from CK_BanCP_CR1 A');
    sql.Add('where ckOrdNo='+quotedstr(trim(fCkNO)));
    sql.Add('Group by MainID,subID,BCGangNO,C_Color,QtyUnit');
    open;
  end;


  try
    while not ADOQueryTemp.Eof do
    begin
    if GetLSNo(ADOQueryCmd,maxno,'MX','Contract_Cloth_LLMx',4,1)=False then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('取去向流水号失败！','提示',0);
      Exit;
    end;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from Contract_Cloth_LLMx where 1<>2 ');
      Open;
    end;

    with ADOQueryCmd do
    begin
      Append;
      FieldByName('MainId').Value:=Trim(ADOQueryTemp.fieldbyname('MainId').AsString);
      FieldByName('SubId').Value:=Trim(ADOQueryTemp.fieldbyname('SubId').AsString);
      FieldByName('DHId').Value:=Trim(ADOQueryTemp.fieldbyname('SubId').AsString);
      FieldByName('MXId').Value:=Trim(maxno);
      FieldByName('OrdSubId').Value:=Trim(ADOQueryTemp.fieldbyname('SubId').AsString);
      FieldByName('TPDate').Value:=CRTIME.Date;
      FieldByName('TPPerson').Value:='';
      if trim(CRType.Text)='加工出库' then
      FieldByName('TPNote').Value:='成品后加工';

      if trim(CRType.Text)='回修出库' then
      FieldByName('TPNote').Value:='成品回修';

      FieldByName('ToNo').Value:=trim(defstr1.TxtCode);
      FieldByName('ToName').Value:=trim(defstr1.Text);
      FieldByName('gangNO').Value:='';
      FieldByName('BCgangNO').Value:=ADOQueryTemp.fieldbyname('BCGangNO').Value;
      FieldByName('C_Color').Value:=ADOQueryTemp.fieldbyname('C_Color').Value;
      FieldByName('TPUnit').Value:=ADOQueryTemp.fieldbyname('QtyUnit').Value;
      FieldByName('TPPS').Value:=ADOQueryTemp.fieldbyname('RollNum').Value;
      FieldByName('TPQty').Value:=ADOQueryTemp.fieldbyname('Qty').Value;
      FieldByName('TPMQty').Value:=ADOQueryTemp.fieldbyname('Qty').Value;
      FieldByName('Qty1').Value:=0;
      FieldByName('Qty2').Value:=0;
      FieldByName('Qty3').Value:=0;
      FieldByName('JGPrice').Value:=0;
      FieldByName('GangFee').Value:=0;
      if trim(CRType.Text)='加工出库' then
      FieldByName('JGType').Value:='成品后加工';

      if trim(CRType.Text)='回修出库' then
      FieldByName('JGType').Value:='成品回修';
      Post;
    end;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from Contract_Cloth_LL where 1<>1');
      Open;
    end;

    with ADOQueryCmd do
    begin
      Append;
      FieldByName('LLID').Value:=trim(maxNo);
      FieldByName('DHID').Value:=Trim(ADOQueryTemp.fieldbyname('subID').AsString);
      FieldByName('FirstNo').Value:=trim(defstr1.TxtCode);
      FieldByName('FirstName').Value:=trim(defstr1.Text);
      FieldByName('OrdSubId').Value:=Trim(ADOQueryTemp.fieldbyname('subID').AsString);
      FieldByName('TPUnit').Value:=Trim(ADOQueryTemp.fieldbyname('QtyUnit').AsString);
      FieldByName('LLIdx').Value:=1;
      if trim(CRType.Text)='加工出库' then
      FieldByName('JGType').Value:='成品后加工';
      if trim(CRType.Text)='回修出库' then
      FieldByName('JGType').Value:='成品回修';
      Post;
    end;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('UPdate Contract_Cloth_LL Set TPPS=(select isnull(sum(TPPS),0) from Contract_Cloth_LLMX A where A.MXID=Contract_Cloth_LL.LLID');
      SQL.Add(' and A.OrdSubId=Contract_Cloth_LL.OrdSubId)');
      sql.Add(',TPQty=(select Sum(TPQty) from Contract_Cloth_LLMx A  where A.MXID=Contract_Cloth_LL.LLID');
      SQL.Add(' and A.OrdSubId=Contract_Cloth_LL.OrdSubId)');
      sql.Add(',BCPQty=(select Sum(Qty2) from Contract_Cloth_LLMx A  where A.MXID=Contract_Cloth_LL.LLID');
      SQL.Add(' and A.OrdSubId=Contract_Cloth_LL.OrdSubId)');
      sql.Add(', TPMQty=(select isnull(sum(TPMQty),0)  from Contract_Cloth_LLMX A where A.MXID=Contract_Cloth_LL.LLID');
      SQL.Add(' and A.OrdSubId=Contract_Cloth_LL.OrdSubId)');
      sql.Add(', BCPMQty=(select isnull(sum(MQty2),0)  from Contract_Cloth_LLMX A where A.MXID=Contract_Cloth_LL.LLID');
      SQL.Add(' and A.OrdSubId=Contract_Cloth_LL.OrdSubId)');
      sql.Add('where LLID='''+Trim(maxno)+'''');
      ExecSQL;
    end;
    ADOQueryTemp.Next;
    end;
    result:=true;
  except
  end;
end;
function TfrmCKProductCK_Other.savedate():boolean;
var
  maxno:String;
  CRID:Integer;
  MaxCkNo:String;
  i:integer;
  rkNo:string;
begin
  result:=false;
  ADOQueryCmd.Connection.BeginTrans;
  CDS_SubSel.DisableControls;
  try
    if GetLSNo(ADOQueryCmd,fkeyNo,'CT','CK_BanCP_CR1',4,1)=False then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('取成品仓库最大号失败！','提示',0);
      Exit;
    end;
    if (trim(CRType.Text)='样品出库') OR (trim(CRType.Text)='广东出库') then
    begin
      if GetLSNo(ADOQueryCmd,rkNo,'RT','BP_InOut',3,1)=False then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('取最大号失败!','提示',0);
        Exit;
      end;
    end;
    i:=1;
    with CDS_SubSel do
    begin
      DisableControls;
      First;
      while not Eof do
      begin
      //  if fieldbyname('ssel').AsBoolean then
     //   begin
          if GetLSNo(ADOQueryCmd,MaxCkNo,'CT','CK_BanCP_CR1',4,1)=False then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            Exit;
          end;
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from CK_BanCP_CR1 ');
            sql.Add('where 1=2 ');
            Open;
          end;
          with ADOQueryCmd do
          begin
            if IsEmpty then Append else edit;
            FieldByName('MainId').Value:=Trim(CDS_SubSel.fieldbyname('MainId').AsString);
            FieldByName('SubId').Value:=Trim(CDS_SubSel.fieldbyname('SubId').AsString);
            FieldByName('C_Code').Value:=Trim(CDS_SubSel.fieldbyname('C_Code').AsString);
            FieldByName('C_CodeName').Value:=Trim(CDS_SubSel.fieldbyname('C_CodeName').AsString);
            FieldByName('C_SPec').Value:=Trim(CDS_SubSel.fieldbyname('C_SPec').AsString);
            FieldByName('C_Color').Value:=Trim(CDS_SubSel.fieldbyname('C_Color').AsString);
            FieldByName('APID').Value:=Trim(CDS_SubSel.fieldbyname('APID').AsString);
            FieldByName('MJID').Value:=Trim(CDS_SubSel.fieldbyname('MJId').AsString);
            FieldByName('MJxh').Value:=Trim(CDS_SubSel.fieldbyname('MJxh').AsString);
            FieldByName('kcKw').Value:=Trim(CDS_SubSel.fieldbyname('kcKw').AsString);
            FieldByName('RCGangNo').Value:=Trim(CDS_SubSel.fieldbyname('RCGangNo').AsString);
            FieldByName('BCGangNo').Value:=Trim(CDS_SubSel.fieldbyname('BCGangNo').AsString);
            FieldByName('BCID').Value:=Trim(MaxCkNo);
            FieldByName('ckordNo').Value:=Trim(fkeyNo);
            FieldByName('CRTime').Value:=FormatDateTime('yyyy-MM-dd',CRTime.DateTime);
            FieldByName('CRFlag').Value:='出库';
            FieldByName('CRType').Value:=trim(CRType.Text);
            //FieldByName('JTType').Value:=Trim(XJFlag);
            FieldByName('CRID').Value:=Trim(CDS_SubSel.fieldbyname('CRID').AsString);
            FieldByName('KGQty').Value:=CDS_SubSel.fieldbyname('KCKGQty').AsFloat;
            FieldByName('Qty').Value:=CDS_SubSel.fieldbyname('KCQty').AsFloat;
            FieldByName('RollNum').Value:=CDS_SubSel.fieldbyname('KCRollNum').AsFloat;
            FieldByName('QtyUnit').Value:=CDS_SubSel.fieldbyname('KCQtyUnit').Value;
            FieldByName('CPType').Value:=Trim(CDS_SubSel.fieldbyname('CPType').AsString);
            FieldByName('Filler').Value:=Trim(DName);
            FieldByName('FillTime').Value:=SGetServerDateTime(ADOQueryTemp);
            FieldByName('defstr1').Value:=trim(defstr1.Text);
            FieldByName('note').Value:=trim(note.Text);
            Post;
          end;
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from CK_BanCP_KC1 ');
            sql.Add('where CRID='+quotedstr(trim(CDS_SubSel.fieldbyname('CRID').AsString)));
            Open;
          end;
          with ADOQueryCmd do
          begin
            if IsEmpty then Append else edit;
            FieldByName('KCQty').Value:=FieldByName('KCQty').AsFloat - CDS_SubSel.fieldbyname('KCQty').AsFloat;
            FieldByName('KCRollNum').Value:=FieldByName('KCRollNum').AsFloat - CDS_SubSel.fieldbyname('KCRollNum').AsFloat;
            Post;
          end;

          if (trim(CRType.Text)='样品出库') OR (trim(CRType.Text)='广东出库') then
            begin
            if not SaveEjK(rkNo,inttostr(i),MaxCkNo,fkeyNo) then
            begin
              ADOQueryCmd.Connection.RollbackTrans;
              exit;
            end;
          end;

          i:=i+1;
       // end;
       CDS_SubSel.Next;
      end;
    end;
    CDS_SubSel.EnableControls;
    if (trim(CRType.Text)='加工出库') OR (trim(CRType.Text)='回修出库') then
    begin
      if not saveJG10(fkeyNo) then
      begin
       ADOQueryCmd.Connection.RollbackTrans;
       exit;
      end;
    end;

    ADOQueryCmd.Connection.CommitTrans;
    result:=true;
  except
    CDS_SubSel.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
  end;
end;

procedure TfrmCKProductCK_Other.FormDestroy(Sender: TObject);
begin
  frmCKProductCK_Other:=nil;
end;

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

procedure TfrmCKProductCK_Other.FormCreate(Sender: TObject);
begin
  cxGrid2.Align:=alClient;

  BegDate.DateTime:=SGetServerDateTime(ADOQueryTemp);
  EndDate.DateTime:=BegDate.DateTime;
  CRTIME.DateTime:= BegDate.DateTime;
  IsCreateSel:=false;
//  TBRafresh.Click;
end;

procedure TfrmCKProductCK_Other.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      SQL.Add('exec P_View_CPKCMX_Other');
      SQl.Add('@ConNo='+quotedstr(trim(conNo.Text)));
      SQl.Add(',@C_CodeName='+quotedstr(trim(C_CodeName.Text)));
      SQl.Add(',@C_Color='+quotedstr(trim(C_Color.Text)));
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
    if not IsCreateSel then
    begin
    SCreateCDS20(ADOQueryMain,CDS_SubSel);
    IsCreateSel:=true;
    end;
  finally
    ADOQueryMain.EnableControls;
    TBFind.Click;
  end;
end;
procedure TfrmCKProductCK_Other.TBRafreshClick(Sender: TObject);
begin
 // BegDate.SetFocus;
  MovePanel2.Visible:=True;
  MovePanel2.Refresh;
  InitGrid();
  MovePanel2.Visible:=False;
end;

procedure TfrmCKProductCK_Other.ConNoMChange(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  end;
end;

procedure TfrmCKProductCK_Other.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(self.Caption,Tv1,'成品仓库');
 // WriteCxGrid('库存选择',Tv2,'成品仓库');
  Close;
end;

procedure TfrmCKProductCK_Other.FormShow(Sender: TObject);
begin

  ReadCxGrid(self.Caption,Tv1,'成品仓库');
  //ReadCxGrid('库存选择',Tv2,'成品仓库');
//  InitGridNull();
  //InitGrid();
end;

procedure TfrmCKProductCK_Other.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then exit;
  TcxGridToExcel('库存列表',cxGrid2);
end;

procedure TfrmCKProductCK_Other.TBFindClick(Sender: TObject);
var
  strsql:string;
begin
  if ADOQueryMain.Active then
  begin
    strsql:=SGetFilters(Panel1,1,2);
    if trim(C_Color.Text)<>'' then
    begin
      if strsql='' then strsql:=' C_Color='+quotedstr(trim(C_Color.Text))
      else
      strsql:=strsql+' and C_Color='+quotedstr(trim(C_Color.Text));
    end;

    if trim(edit1.Text)<>'' then
    begin
      if strsql='' then strsql:=' KCQty>='+inttostr(strtointdef(trim(edit1.Text),0))
      else
      strsql:=strsql+' and KCQty>='+inttostr(strtointdef(trim(edit1.Text),0));
    end;
    if trim(edit2.Text)<>'' then
    begin
      if strsql='' then strsql:=' KCQty<='+inttostr(strtointdef(trim(edit2.Text),0))
      else
      strsql:=strsql+' and KCQty<='+inttostr(strtointdef(trim(edit2.Text),0));
    end;
    SDofilter(ADOQueryMain,strsql);
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  end;
end;

procedure TfrmCKProductCK_Other.C_CodeNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmCKProductCK_Other.Panel10MouseMove(Sender: TObject;
  Shift: TShiftState; X, Y: Integer);
begin
  ReleaseCapture;
  TWinControl(Panel4).Perform(WM_SYSCOMMAND,$F012,0);
end;

procedure TfrmCKProductCK_Other.Image2Click(Sender: TObject);
begin
  Panel4.Visible:=False;
end;

procedure TfrmCKProductCK_Other.Tv1MouseUp(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  FLeft:=X;
  FTop:=Y;
end;

procedure TfrmCKProductCK_Other.ToolButton1Click(Sender: TObject);
begin
  if not CDS_SubSel.Active then exit;
  if  CDS_SubSel.IsEmpty then exit;
  CRTIME.SetFocus;
{  if not CDS_SubSel.Locate('SSel',True,[]) then
  begin
    Application.MessageBox('没有选择出库数据!','提示',0);
    Exit;
  end;      }
  if trim(CRType.Text)='' then
  begin
    Application.MessageBox('没有选择出库类型!','提示',0);
    Exit;
  end;
  if (trim(CRType.Text)='坯布退货')  then
  begin
    if trim(defstr1.Text)='' then
    begin
      Application.MessageBox('没有选择坯布厂!','提示',0);
      Exit;
    end;
  end;
  if (trim(CRType.Text)='加工出库')OR (trim(CRType.Text)='回修出库')  then
  begin
    if trim(defstr1.Text)='' then
    begin
      Application.MessageBox('没有选择加工厂!','提示',0);
      Exit;
    end;
    if trim(P_CodeName.Text)='' then
    begin
      Application.MessageBox('没有选择产品名称!','提示',0);
      Exit;
    end;
    if trim(P_Color.Text)='' then
    begin
      Application.MessageBox('没有选择颜色!','提示',0);
      Exit;
    end;
   { with CDS_Main do
    begin
    DisableControls;
    First;
    while not Eof do
    begin
      if fieldbyname('ssel').AsBoolean then
      begin
        if trim(Fieldbyname('MainId').AsString)='' then
        begin
          EnableControls;
          application.MessageBox('期初入库不能加工出库！','提示信息',0);
          exit;
        end;
      end;
      next;
    end;
    EnableControls;
    end; }
  end;



  if savedate() then
  begin
    application.MessageBox('数据保存成功！','提示信息');
    ModalResult:=1;
  end
  else
  application.MessageBox('数据保存失败！','提示信息',0);
end;

procedure TfrmCKProductCK_Other.Tv2CellDblClick(
  Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  IF CDS_SubSel.IsEmpty then exit;
  IF not CDS_Main.Locate('CRID',trim(CDS_SubSel.fieldbyname('CRID').AsString),[]) then
  begin
    InitCDSToCDS(CDS_SubSel,CDS_Main);
  end;
  CDS_SubSel.Delete;
end;

procedure TfrmCKProductCK_Other.defstr1BtnClick(Sender: TObject);
begin
  try
    frmZdyAttachGYS:=TfrmZdyAttachGYS.Create(Application);
    with frmZdyAttachGYS do
    begin
      if ShowModal=1 then
      begin
        defstr1.Text:=Trim(CDS_HZ.fieldbyname('ZDYName').AsString);
        defstr1.TxtCode:=Trim(CDS_HZ.fieldbyname('ZdyCode').AsString);
    //    LXTel.Text:=Trim(CDS_HZ.fieldbyname('DEFstr2').AsString);
    //    LXPerson.Text:=Trim(CDS_HZ.fieldbyname('DEFstr4').AsString);
      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;
end;

procedure TfrmCKProductCK_Other.v2SSELPropertiesChange(Sender: TObject);
var
  fNum:double;
begin
  fnum:= strtofloatdef(Label19.Caption,0);
  if CDS_Main.FieldByName('ssel').AsBoolean then
  begin
    fnum:=fnum + CDS_Main.fieldbyname('KCQty').AsFloat;
  end
  else
  begin
    fnum:=fnum - CDS_Main.fieldbyname('KCQty').AsFloat;
  end;

  Label19.Caption:=floattostr(fnum);
end;

procedure TfrmCKProductCK_Other.CheckBox1Click(Sender: TObject);
var
  fNum:double;
begin
  fNum:=0;
  with CDS_Main do
  begin
    DisableControls;
    first;
    while not eof do
    begin
      edit;
      fieldbyname('Ssel').Value:=CheckBox1.Checked;
      if CheckBox1.Checked then
      fNum:=fNum+ CDS_Main.fieldbyname('KCQty').AsFloat;
      post;
      next;
    end;
    first;
    EnableControls;
  end;
Label19.Caption:=floattostr(fnum);
end;

procedure TfrmCKProductCK_Other.Tv1CellDblClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  IF CDS_Main.IsEmpty then exit;

  IF not CDS_SubSel.Locate('CRID',trim(CDS_Main.fieldbyname('CRID').AsString),[]) then
  begin
    InitCDSToCDS(CDS_Main,CDS_SubSel);
  end;
  CDS_Main.Delete;
end;

procedure TfrmCKProductCK_Other.Button1Click(Sender: TObject);
begin
  IF CDS_Main.IsEmpty then exit;
  CDS_SubSel.DisableControls;
  with CDS_Main do
  begin
     DisableControls;
     first;
     while CDS_Main.Locate('ssel',true,[]) do
     begin
        IF not CDS_SubSel.Locate('CRID',trim(CDS_Main.fieldbyname('CRID').AsString),[]) then
        begin
          InitCDSToCDS(CDS_Main,CDS_SubSel);
        end;
        CDS_Main.Delete;
     end;
    EnableControls;
  end;
  CDS_SubSel.EnableControls;
end;

procedure TfrmCKProductCK_Other.P_CodeNameBtnClick(Sender: TObject);
begin
 try
    frmZdyAttachCP:=TfrmZdyAttachCP.Create(Application);
    with frmZdyAttachCP do
    begin
      if ShowModal=1 then
      begin
        P_CodeName.Text:=Trim(CDS_HZ.fieldbyname('ZDYName').AsString);
        P_Color.Text:=Trim(CDS_HZ.fieldbyname('DEFstr3').AsString);
      end;
    end;
  finally
    frmZdyAttachCP.Free;
  end;
end;

procedure TfrmCKProductCK_Other.P_ColorBtnClick(Sender: TObject);
begin
 try
    frmZdyAttachCP:=TfrmZdyAttachCP.Create(Application);
    with frmZdyAttachCP do
    begin
      if ShowModal=1 then
      begin
     //   P_CodeName.Text:==Trim(CDS_HZ.fieldbyname('ZDYName').AsString);
        P_Color.Text:=Trim(CDS_HZ.fieldbyname('DEFstr3').AsString);
      end;
    end;
  finally
    frmZdyAttachCP.Free;
  end;
end;

procedure TfrmCKProductCK_Other.P_CodeNameExit(Sender: TObject);
var
  mvalue:string;
begin
  mvalue:=trim(P_CodeName.Text);

  IF trim(mvalue)='' then exit;

  IF rightBstr(trim(mvalue),1)<>'#'THEN
  BEGIn
    mvalue:=trim(mvalue)+'#';
  END;

  P_CodeName.Text:=trim(mvalue);


  with ADOQueryCmd do
  begin
    close;
    sql.Clear;
    sql.Add('select *  from KH_Zdy_Attachment A ');
    sql.Add('where zdyName='+quotedstr(trim(P_CodeName.Text)));
    open;
  end;
  IF ADOQueryCmd.IsEmpty then
  begin
    P_CodeName.Text:='';
    application.MessageBox('品名不存在！','提示信息',0);
  end;
end;

procedure TfrmCKProductCK_Other.P_ColorExit(Sender: TObject);
var
  mvalue:string;
begin
  mvalue:=trim(P_Color.Text);

  IF trim(mvalue)='' then exit;

  IF rightBstr(trim(mvalue),1)<>'#'THEN
  BEGIn
    mvalue:=trim(mvalue)+'#';
  END;

  P_Color.Text:=trim(mvalue);


  with ADOQueryCmd do
  begin
    close;
    sql.Clear;
    sql.Add('select *  from KH_Zdy_Attachment A ');
    sql.Add('where zdyName='+quotedstr(trim(P_CodeName.Text)));
    sql.Add('and DEFstr3='+quotedstr(trim(P_Color.Text)));
    open;
  end;
  IF ADOQueryCmd.IsEmpty then
  begin
    P_Color.Text:='';
    application.MessageBox('颜色不存在！','提示信息',0);
  end;
end;

procedure TfrmCKProductCK_Other.CRTIMEKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
	begin
		Key := #0;
	  SelectNext(ActiveControl as TWinControl, True, True);
	end;
end;

end.
