unit U_DJBCKOutPut;

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, cxSpinEdit,
  cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, StrUtils, dxSkinsCore,
  dxSkinBlack, dxSkinBlue, dxSkinBlueprint, dxSkinCaramel, dxSkinCoffee,
  dxSkinDarkRoom, dxSkinDarkSide, dxSkinDevExpressDarkStyle,
  dxSkinDevExpressStyle, dxSkinFoggy, dxSkinGlassOceans, dxSkinHighContrast,
  dxSkiniMaginary, dxSkinLilian, dxSkinLiquidSky, dxSkinLondonLiquidSky,
  dxSkinMcSkin, dxSkinMetropolis, dxSkinMetropolisDark, dxSkinMoneyTwins,
  dxSkinOffice2007Black, dxSkinOffice2007Blue, dxSkinOffice2007Green,
  dxSkinOffice2007Pink, dxSkinOffice2007Silver, dxSkinOffice2010Black,
  dxSkinOffice2010Blue, dxSkinOffice2010Silver, dxSkinOffice2013DarkGray,
  dxSkinOffice2013LightGray, dxSkinOffice2013White, dxSkinPumpkin, dxSkinSeven,
  dxSkinSevenClassic, dxSkinSharp, dxSkinSharpPlus, dxSkinSilver,
  dxSkinSpringTime, dxSkinStardust, dxSkinSummer2008, dxSkinTheAsphaltWorld,
  dxSkinsDefaultPainters, dxSkinValentine, dxSkinVS2010, dxSkinWhiteprint,
  dxSkinXmas2008Blue, dxSkinscxPCPainter;

type
  TfrmDJBCKOutPut = class(TForm)
    ToolBar1: TToolBar;
    TBAdd: TToolButton;
    TBDel: TToolButton;
    TBSave: TToolButton;
    TBClose: TToolButton;
    DataSource3: TDataSource;
    CDS_Sub: TClientDataSet;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    cxGridPopupMenu2: TcxGridPopupMenu;
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column6: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1CRType: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column16: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    v1Column5: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    v1Column20: TcxGridDBColumn;
    v1Column21: TcxGridDBColumn;
    ToolButton1: TToolButton;
    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 v1Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column11PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column5PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure Tv1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure ToolButton1Click(Sender: TObject);
  private
    { Private declarations }
    function SaveCKData(): Boolean;
    function YSData(Order_Main10: TClientDataSet): Boolean;
  public
    { Public declarations }
    FBCId: string;
  end;

var
  frmDJBCKOutPut: TfrmDJBCKOutPut;

implementation

uses
  U_DataLink, U_RTFun, U_ClothContractListHZCX, U_ZDYHelp, U_DJBCKKCSelList,
  U_GYSSelList, U_ZdyAttachGYS, U_ProductOrderListSel;

{$R *.dfm}
function TfrmDJBCKOutPut.YSData(Order_Main10: TClientDataSet): Boolean;
var
  CRID, YFID, PriceUnit, OrderUnit, FComTaiTou, FromSubID, khName, fywy: string;
  fprice, huilv: double;
begin
  Result := False;
  fprice := 0;
  huilv := 0;
  PriceUnit := 'RMB';
  khName := '';
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('update JYOrder_Sub SET Huilv=isnull((select Huilv from JYOrderCon_Sub X where X.SubId=JYOrder_Sub.SOrddefstr10),1) where Huilv=0');
    execsql;
  end;
  with Order_Main10 do
  begin
    begin
      with ADOQueryTemp do
      begin
        Close;
        SQL.Clear;
        sql.Add(' select A.*,fPrice=dbo.F_Get_Order_Price(A.MainID,A.subID),B.customerNoName,B.OrdPerson2 ');
        sql.Add(' from JYOrder_Sub A  ');
        sql.Add(' inner join  JYOrder_main B on B.mainID=A.mainID  ');
        sql.Add(' Where subID=''' + Trim(Order_Main10.fieldbyname('ORDSubIdCK').AsString) + '''');
        Open;
      end;
      if not ADOQueryTemp.IsEmpty then
      begin
        PriceUnit := Trim(ADOQueryTemp.fieldbyname('PriceUnit').AsString);
        OrderUnit := Trim(ADOQueryTemp.fieldbyname('OrderUnit').AsString);
        fprice := ADOQueryTemp.fieldbyname('fPrice').AsFloat;
        huilv := ADOQueryTemp.fieldbyname('huilv').AsFloat;
        khName := ADOQueryTemp.fieldbyname('customerNoName').AsString;
        fywy := ADOQueryTemp.fieldbyname('OrdPerson2').AsString;
      end;
      if huilv = 0 then
        huilv := 1;
      CRID := '0';
      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        sql.Add(' select * from YF_Money_CR ');
        sql.Add(' where mainID=''' + Trim(CDS_Sub.fieldbyname('SPID').AsString) + '''');
        sql.Add(' and YFName=''成品直接销售'' ');
        Open;
      end;
      if ADOQueryTemp.IsEmpty then
      begin
        if GetLSNo(ADOQueryCmd, YFID, 'CS', 'YF_Money_CR', 3, 1) = False then
        begin
          Application.MessageBox('取成品应收最大号失败!', '提示', 0);
          Exit;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from YF_Money_CR where 1<>1');
          Open;
        end;
        with ADOQueryCmd do
        begin
          Append;
          FieldByName('YFID').Value := Trim(YFID);
          FieldByName('YFTypeId').Value := Trim(Order_Main10.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(khName);
          FieldByName('CRTime').Value := Trim(FormatDateTime('yyyy-MM-dd', CDS_Sub.fieldbyname('CRTime').AsDateTime));
          FieldByName('YFType').Value := '自动生成';
          FieldByName('ps').Value := Order_Main10.fieldbyname('PiQty').AsFloat;
          FieldByName('Qty').Value := Order_Main10.fieldbyname('Qty').AsFloat;
          FieldByName('Price').Value := fprice;
          FieldByName('HuiLv').Value := huilv;
          FieldByName('BZType').Value := Trim(PriceUnit);
         // FieldByName('Money').Value:=-1*format('%.2f',[Order_Main10.fieldbyname('Qty').AsFloat*fprice]);
         // FieldByName('BBMoney').Value:=-1*format('%.2f',[Order_Main10.fieldbyname('Qty').AsFloat*fprice*huilv]);
          FieldByName('QtyUnit').Value := Trim(Order_Main10.fieldbyname('QtyUnit').AsString);
          FieldByName('ComTaiTou').Value := Trim(khName);
          FieldByName('YFName').Value := '成品直接销售';
          FieldByName('MainId').Value := Trim(Order_Main10.fieldbyname('SPID').AsString);
          FieldByName('subID').Value := '';
          FieldByName('OrderNO').Value := Trim(Order_Main10.fieldbyname('CKOrderNo').AsString);
          FieldByName('P_CodeName').Value := Trim(Order_Main10.fieldbyname('SPName').AsString);
          FieldByName('P_Spec').Value := Trim(Order_Main10.fieldbyname('SPSpec').AsString);
          FieldByName('P_color').Value := Trim(Order_Main10.fieldbyname('PRTColor').AsString);
        //  FieldByName('ywy').Value:=Trim(fywy);
          FieldByName('status').Value := '0';
          Post;
        end;
      end
      else
      begin
        YFID := Trim(ADOQueryTemp.fieldbyname('YFID').AsString);
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update YF_Money_CR Set Money=Price*Qty,BBMoney=Price*Qty*HuiLv');
        sql.Add(' where YFID=''' + Trim(YFID) + '''');
        ExecSQL;
      end;

      //Next;
    end;
  end;
  Result := True;
end;

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

procedure TfrmDJBCKOutPut.TBAddClick(Sender: TObject);
var
  s, tmpStr: string;
  I: Integer;
//  i,k:string;
//  j:Double;
begin
  try
    frmDJBCKKCSelList := TfrmDJBCKKCSelList.Create(Application);
    with frmDJBCKKCSelList do
    begin
      ToolButton1.Visible := True;
      if ShowModal = 1 then
      begin
        frmDJBCKKCSelList.CDS_Main.DisableControls;
        with frmDJBCKKCSelList.CDS_Main do
        begin
          First;
          while Locate('SSel', True, []) do
          begin
            with Self.CDS_Sub do
            begin
              Append;
              FieldByName('CRTime').Value := Trim(FormatDateTime('yyyy-MM-dd', Now));
              FieldByName('ORDMainIdRK').Value := Trim(frmDJBCKKCSelList.CDS_Main.fieldbyname('ORDMainIdRK').AsString);
              FieldByName('ORDSubIdRK').Value := Trim(frmDJBCKKCSelList.CDS_Main.fieldbyname('ORDSubIdRK').AsString);
              FieldByName('ORDMainIdCK').Value := Trim(frmDJBCKKCSelList.CDS_Main.fieldbyname('ORDMainIdRK').AsString);
              FieldByName('ORDSubIdCK').Value := Trim(frmDJBCKKCSelList.CDS_Main.fieldbyname('ORDSubIdRK').AsString);
              FieldByName('QCOrderNo').Value := frmDJBCKKCSelList.CDS_Main.fieldbyname('OrderNo').Value;
              FieldByName('CKOrderNo').Value := frmDJBCKKCSelList.CDS_Main.fieldbyname('OrderNo').Value;
              FieldByName('SOrddefstr1').Value := frmDJBCKKCSelList.CDS_Main.fieldbyname('SOrddefstr1').AsString;
              FieldByName('PRTColor').Value := frmDJBCKKCSelList.CDS_Main.fieldbyname('PRTColor').AsString;
              FieldByName('PRTHX').Value := frmDJBCKKCSelList.CDS_Main.fieldbyname('PRTHX').AsString;
              FieldByName('PRTkuanNo').Value := frmDJBCKKCSelList.CDS_Main.fieldbyname('PRTkuanNo').AsString;
              FieldByName('FactoryName').Value := frmDJBCKKCSelList.CDS_Main.fieldbyname('FactoryName').Value;
              FieldByName('FactoryNo').Value := frmDJBCKKCSelList.CDS_Main.fieldbyname('FactoryNo').Value;
              FieldByName('SSType').Value := frmDJBCKKCSelList.CDS_Main.fieldbyname('SSType').Value;
              FieldByName('SPName').Value := frmDJBCKKCSelList.CDS_Main.fieldbyname('SPName').Value;
              FieldByName('SPSpec').Value := frmDJBCKKCSelList.CDS_Main.fieldbyname('SPSpec').Value;
              FieldByName('SPCF').Value := frmDJBCKKCSelList.CDS_Main.fieldbyname('SPCF').Value;
              FieldByName('SPMF').Value := frmDJBCKKCSelList.CDS_Main.fieldbyname('SPMF').Value;
              FieldByName('SPKZ').Value := frmDJBCKKCSelList.CDS_Main.fieldbyname('SPKZ').Value;
              FieldByName('RCGangNo').Value := frmDJBCKKCSelList.CDS_Main.fieldbyname('RCGangNo').Value;
              FieldByName('GangNo').Value := frmDJBCKKCSelList.CDS_Main.fieldbyname('GangNo').Value;
//              s := FieldByName('RCGangNo').Value;
//              for I := 1 to Length(s) do
//                if s[I] in ['0'..'9'] then
//                  tmpStr := tmpStr + s[I];
//              FieldByName('GangNo').Value := RightStr(tmpStr, 6);
              FieldByName('FromFactoryName').Value := frmDJBCKKCSelList.CDS_Main.fieldbyname('ToFactoryName').Value;
              FieldByName('FromFactoryNo').Value := frmDJBCKKCSelList.CDS_Main.fieldbyname('ToFactoryNo').Value;
              FieldByName('ToFactoryName').Value := frmDJBCKKCSelList.CDS_Main.fieldbyname('ToFactoryName').Value;
              FieldByName('ToFactoryNo').Value := frmDJBCKKCSelList.CDS_Main.fieldbyname('ToFactoryNo').Value;
              FieldByName('QtyUnit').Value := frmDJBCKKCSelList.CDS_Main.fieldbyname('QtyUnit').Value;
              FieldByName('PiQty').Value := frmDJBCKKCSelList.CDS_Main.fieldbyname('MXKCPiQty').Value;
              FieldByName('Qty').Value := frmDJBCKKCSelList.CDS_Main.fieldbyname('MXKCQty').Value;
              FieldByName('FZSPID').Value := frmDJBCKKCSelList.CDS_Main.fieldbyname('SPID').Value;
            end;
            frmDJBCKKCSelList.CDS_Main.Delete;
          end;
        end;
        frmDJBCKKCSelList.CDS_Main.EnableControls;

      end;
    end;
  finally
    frmDJBCKKCSelList.Free;
  end;
end;

function TfrmDJBCKOutPut.SaveCKData(): Boolean;
var
  FCRID, Maxno, FSPID, MaxPYNo, FPYID: 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 jyorder_main where orderno=''' + trim(cds_sub.fieldbyname('QCOrderNo').asstring) + ''' ');
//           showmessage(sql.Text);
          open;
        end;

        with ADOQuerycmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from CK_SXPB_CR where SPID=''' + Trim(CDS_Sub.fieldbyname('SPID').AsString) + '''');
          Open;
        end;
        FSPID := Trim(ADOQuerycmd.fieldbyname('SPID').AsString);
        if Trim(FSPID) = '' then
        begin
          if GetLSNo(ADOQueryCmd, Maxno, '', 'CK_SXPB_CR', 3, 1) = False then
          begin
            Result := False;
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取待检布出库编号失败!', '提示', 0);
            Exit;
          end;
        end
        else
        begin
          Maxno := Trim(FSPID);
        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(FSPID) = '' then
          begin
            Append;
          end
          else
          begin
            Edit;
          end;
          FieldByName('SPID').Value := Trim(Maxno);
          FieldByName('ORDMainIdRK').Value := Trim(CDS_Sub.fieldbyname('ORDMainIdRK').AsString);
          FieldByName('ORDSubIdRK').Value := Trim(CDS_Sub.fieldbyname('ORDSubIdRK').AsString);
          FieldByName('ORDMainIdCK').Value := Trim(CDS_Sub.fieldbyname('ORDMainIdCK').AsString);
          FieldByName('ORDSubIdCK').Value := Trim(CDS_Sub.fieldbyname('ORDSubIdCK').AsString);
          FieldByName('FZSPID').Value := Trim(CDS_Sub.fieldbyname('FZSPID').AsString);
          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('CRID').Value:=CDS_Sub.fieldbyname('CRID').Value;
          FieldByName('CRFlag').Value := '出库';
          FieldByName('CRQtyFlag').Value := -1;
//           FieldByName('GangNo').Value :=  RightStr('RCGangNo', 6);

          RTSetSaveDataCDS(ADOQueryCmd, Tv1, CDS_Sub, 'CK_SXPB_CR', 2);
          FieldByName('CKName').Value := '待检布';
          if Trim(CDS_Sub.fieldbyname('CRType').AsString) = '平移出库' then
          begin
            FieldByName('PYID').Value := Trim(Maxno);
          end;
          if Trim(FSPID) = '' then
            FieldByName('Filler').Value := Trim(DName)
          else
          begin
            FieldByName('Editer').Value := Trim(DName);
            FieldByName('EditTime').Value := SGetServerDate(ADOQueryTemp);
          end;
          FieldByName('Xs').Value := CDS_Sub.fieldbyname('Xs').AsFloat;
          FieldByName('PiQty').Value := CDS_Sub.fieldbyname('PiQty').AsFloat;
          FieldByName('Qty').Value := CDS_Sub.fieldbyname('Qty').AsFloat;

          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(' where SPID=''' + Trim(CDS_Sub.fieldbyname('FZSPID').AsString) + '''');
          ExecSQL;
        end;
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from CK_SXPB_CR where SPID=''' + Trim(CDS_Sub.fieldbyname('FZSPID').AsString) + '''');
          Open;
        end;
        if ADOQueryTemp.FieldByName('MXKCQty').Value < 0 then
        begin
          CDS_Sub.DisableControls;
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('出库数大于库存数不能保存!', '提示', 0);
          Exit;
        end;
        if Trim(CDS_Sub.fieldbyname('CRType').AsString) = '平移出库' then
        begin
          with ADOQueryTemp do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from CK_SXPB_CR where PYID=''' + Trim(Maxno) + '''');
            sql.Add(' and isnull(CRType,'''')=''平移入库'' ');
            Open;
          end;
          FPYID := Trim(ADOQueryTemp.fieldbyname('SPID').AsString);
          if Trim(FPYID) = '' then
          begin
            if GetLSNo(ADOQueryCmd, MaxPYNo, 'YR', 'CK_SXPB_CR', 3, 1) = False then
            begin
              Result := False;
              ADOQueryCmd.Connection.RollbackTrans;
              Application.MessageBox('取外加工平移入库编号失败!', '提示', 0);
              Exit;
            end;
          end
          else
          begin
            MaxPYNo := Trim(FPYID);
          end;
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from CK_SXPB_CR where SPID=''' + Trim(MaxPYNo) + '''');
            Open;
          end;
          with ADOQueryCmd do
          begin
            if Trim(FPYID) = '' then
            begin
              Append;
            end
            else
            begin
              Edit;
            end;
            FieldByName('SPID').Value := Trim(MaxPYNo);
            FieldByName('ORDMainIdRK').Value := Trim(CDS_Sub.fieldbyname('ORDMainIdCK').AsString);
            FieldByName('ORDSubIdRK').Value := Trim(CDS_Sub.fieldbyname('ORDSubIdCK').AsString);
            {FieldByName('ORDMainIdCK').Value:=Trim(CDS_Sub.fieldbyname('ORDMainIdCK').AsString);
            FieldByName('ORDSubIdCK').Value:=Trim(CDS_Sub.fieldbyname('ORDSubIdCK').AsString);}
            FieldByName('FZSPID').Value := Trim(MaxPYNo);
            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('CRID').Value:=CDS_Sub.fieldbyname('CRID').Value;
            FieldByName('CRFlag').Value := '入库';
            FieldByName('CRQtyFlag').Value := 1;
            RTSetSaveDataCDS(ADOQueryCmd, Tv1, CDS_Sub, 'CK_SXPB_CR', 2);
            FieldByName('CKName').Value := '外加工';
            FieldByName('CRType').Value := '平移入库';
            FieldByName('PYID').Value := Trim(Maxno);

            if Trim(FPYID) = '' then
              FieldByName('Filler').Value := Trim(DName)
            else
            begin
              FieldByName('Editer').Value := Trim(DName);
              FieldByName('EditTime').Value := SGetServerDate(ADOQueryTemp);
            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(' where SPID=''' + Trim(MaxPYNo) + '''');
            ExecSQL;
          end;
          with ADOQueryTemp do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from CK_SXPB_CR where SPID=''' + Trim(MaxPYNo) + '''');
            Open;
          end;
          if ADOQueryTemp.FieldByName('MXKCQty').Value < 0 then
          begin
            CDS_Sub.DisableControls;
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('出库数大于库存数不能保存!', '提示', 0);
            Exit;
          end;
        end;
        Edit;
        FieldByName('SPID').Value := Trim(Maxno);
        Post;

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

end;

procedure TfrmDJBCKOutPut.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('待检布出库登记PMD', Tv1, '待检布仓库');
  Close;
end;

procedure TfrmDJBCKOutPut.FormShow(Sender: TObject);
begin
  ReadCxGrid('待检布出库登记PMD', 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(' ,OrderNoCK=(select OrderNo from JYOrder_Main JM where JM.MainId=A.OrdMainIdCK )');
    sql.Add(' ,SOrddefstr1=(select SOrddefstr1 from JYOrder_Sub JM where JM.SubId=A.OrdSubIdCK )');
    sql.Add(' ,PRTColor=(select PRTColor from JYOrder_Sub JM where JM.SubId=A.OrdSubIdCK )');
    sql.Add(' ,PRTHX=(select PRTHX from JYOrder_Sub JM where JM.SubId=A.OrdSubIdCK )');
    sql.Add(' ,PRTkuanNO=(select PRTkuanNO from JYOrder_Sub JM where JM.SubId=A.OrdSubIdCK )');
    sql.Add(' from CK_SXPB_CR A');
    sql.Add(' where SPID=''' + Trim(FBCId) + '''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_Sub);
  SInitCDSData20(ADOQueryTemp, CDS_Sub);
end;

procedure TfrmDJBCKOutPut.TBDelClick(Sender: TObject);
begin
  if CDS_Sub.IsEmpty then
    Exit;
  if Trim(CDS_Sub.fieldbyname('SPID').AsString) <> '' then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from CK_SXPB_CR where FZSPID=');
      sql.Add('(select SPID from CK_SXPB_CR where PYID=''' + Trim(CDS_Sub.fieldbyname('SPID').AsString) + '''');
      sql.Add(' and isnull(CRType,'''')=''平移入库'')');
      sql.Add(' and CRQtyFlag=-1');
      Open;
    end;
    if ADOQueryTemp.IsEmpty = False then
    begin
      Application.MessageBox('已有出库记录，不能删除!', '提示', 0);
      Exit;
    end;
    if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
      Exit;
    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) + '''');
        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(' where SPID=''' + Trim(CDS_Sub.fieldbyname('FZSPID').AsString) + '''');
        {sql.Add('Update CK_SXPB_KC Set KCKGQty=(select sum(KGQty*CRQtyFlag) from CK_SXPB_CR A where A.CRID=CK_SXPB_KC.CRID)');
        sql.Add(',KCPiQty=(select sum(PiQty*CRQtyFlag) from CK_SXPB_CR A where A.CRID=CK_SXPB_KC.CRID)');
        sql.Add(' where CRID='+Trim(CDS_Sub.fieldbyname('CRID').AsString)); }
        ExecSQL;
      end;
      ADOQueryCmd.Connection.CommitTrans;
    except
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('删除异常!', '提示', 0);
    end;

  end;
  CDS_Sub.Delete;
end;

procedure TfrmDJBCKOutPut.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('ToFactoryName', null, []) = True then
//  begin
//    Application.MessageBox('出到加工厂不能为空!', '提示', 0);
//    Exit;
//  end;
  if CDS_Sub.Locate('CKOrderNo', 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('PiQty', null, []) = True then
  begin
    Application.MessageBox('匹数不能为空!', '提示', 0);
    Exit;
  end;
  CDS_Sub.DisableControls;
  with CDS_Sub do
  begin
    First;
    while not Eof do
    begin
      if Trim(CDS_Sub.fieldbyname('CRType').AsString) = '平移出库' then
      begin
        if (Trim(CDS_Sub.fieldbyname('FromFactoryName').AsString) = Trim(CDS_Sub.fieldbyname('ToFactoryName').AsString)) and (Trim(CDS_Sub.fieldbyname('ORDSubIdRK').AsString) = Trim(CDS_Sub.fieldbyname('ORDSubIdCK').AsString)) then
        begin
          CDS_Sub.EnableControls;
          Application.MessageBox('同一单号，同一工厂不能平移数据!', '提示', 0);
          Exit;
        end;
      end;
      Next;
    end;
  end;
  CDS_Sub.EnableControls;
  if SaveCKData() then
  begin
    Application.MessageBox('保存成功!', '提示', 0);
    //ModalResult:=1;
    Exit;
  end;
end;

procedure TfrmDJBCKOutPut.v1Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmClothContractListHZCX := TfrmClothContractListHZCX.Create(Application);
    with frmClothContractListHZCX do
    begin
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('CGConNo').Value := Trim(frmClothContractListHZCX.Order_Main.fieldbyname('ConNo').AsString);
          FieldByName('CGConMainId').Value := Trim(frmClothContractListHZCX.Order_Main.fieldbyname('MainId').AsString);
          FieldByName('CGConSubId').Value := Trim(frmClothContractListHZCX.Order_Main.fieldbyname('SubId').AsString);
          FieldByName('FactoryName').Value := Trim(frmClothContractListHZCX.Order_Main.fieldbyname('FactoryNoName').AsString);
          FieldByName('SXName').Value := Trim(frmClothContractListHZCX.Order_Main.fieldbyname('C_CodeName').AsString);
          //Post;
        end;
      end;
    end;
  finally
    frmClothContractListHZCX.Free;
  end;
end;

procedure TfrmDJBCKOutPut.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;
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'CustomerNoName';
      flagname := '客户名称';
      if Trim(DParameters1) <> '高权限' then
        MainType := Trim(DName);
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('ToFactoryName').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmDJBCKOutPut.v1Column5PropertiesButtonClick(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('ORDMainIdCK').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('MainId').AsString);
          FieldByName('ORDSubIdCK').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('SubId').AsString);
          FieldByName('CKorderNo').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('OrderNo').AsString);
          FieldByName('SOrddefstr1').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('SOrddefstr1').AsString);
          FieldByName('PRTColor').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('PRTColor').AsString);
          FieldByName('PRTHX').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('PRTHX').AsString);
          FieldByName('PRTkuanNo').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('PRTkuanNo').AsString);
        end;
      end;
    end;
  finally
    frmProductOrderListSel.Free;
  end;
end;

procedure TfrmDJBCKOutPut.Tv1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
  FFSPID: string;
begin
  if CDS_Sub.IsEmpty then
    Exit;
  if Trim(CDS_Sub.fieldbyname('SPID').AsString) <> '' then
  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
      v1CRType.Options.Editing := False;
    end;
  end;
end;

procedure TfrmDJBCKOutPut.ToolButton1Click(Sender: TObject);
begin
  if CDS_Sub.IsEmpty then
    Exit;
  OneKeyPost(Tv1, CDS_Sub);
end;

end.

