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,
  StdCtrls, ExtCtrls, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator;

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;
    v1Column11: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    v1Column19: TcxGridDBColumn;
    v1Column22: TcxGridDBColumn;
    ToolButton1: TToolButton;
    Panel4: TPanel;
    Label14: TLabel;
    Panel10: TPanel;
    Image2: TImage;
    Button1: TButton;
    GroupBox1: TGroupBox;
    CheckBox1: TCheckBox;
    CheckBox2: TCheckBox;
    CheckBox3: TCheckBox;
    CheckBox4: TCheckBox;
    CheckBox5: TCheckBox;
    Button2: TButton;
    CheckBox6: TCheckBox;
    CheckBox7: TCheckBox;
    Panel2: TPanel;
    cxGrid1: TcxGrid;
    TvMX: TcxGridDBTableView;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    ToolBar2: TToolBar;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    CDS_MX: TClientDataSet;
    DS_MX: TDataSource;
    cv1Column1: TcxGridDBColumn;
    cv1Column2: TcxGridDBColumn;
    cv1Column3: 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 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 Image2Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure cxGridDBColumn8PropertiesEditValueChanged(Sender: TObject);
    procedure TvMXCustomDrawIndicatorCell(Sender: TcxGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxCustomGridIndicatorItemViewInfo; var ADone: Boolean);
    procedure TvMXKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
  private
    { Private declarations }
    function SaveCKData(): Boolean;
    procedure CJEWM(MSpid: string);
    procedure InitMXGrid(MCRNO: string);
  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, U_DDDJBCKKCSelList,
  U_KHList, U_DJBCKKCSelList2; //U_DJBCKKCSelList2    U_DJBCKKCSelList,

{$R *.dfm}

procedure TfrmDJBCKOutPut.InitMXGrid(MCRNO: string);
begin
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from CK_SXPB_CRMX where CRNO=' + quotedstr(Trim(MCRNO)));
    sql.Add(' order by MXID');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_MX);
  SInitCDSData20(ADOQueryTemp, CDS_MX);
end;

procedure TfrmDJBCKOutPut.CJEWM(MSpid: string);
var
  Txt, fImagePath, maxNo: string;
  Moudle: THandle;
  Makebar: TMakebar;
  Mixtext: TMixtext;
begin
  try
    Moudle := LoadLibrary('MakeQRBarcode.dll');
    @Makebar := GetProcAddress(Moudle, 'Make');
    @Mixtext := GetProcAddress(Moudle, 'MixText');
    Txt := Trim(MSpid);
    fImagePath := ExtractFilePath(Application.ExeName) + 'image\temp.bmp';
    if not DirectoryExists(pchar(ExtractFilePath(Application.ExeName) + 'image')) then
      CreateDirectory(pchar(ExtractFilePath(Application.ExeName) + 'image'), nil);
    if FileExists(fImagePath) then
      DeleteFile(fImagePath);
    Makebar(pchar(Txt), Length(Txt), 3, 3, 0, PChar(fImagePath), 3);

    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      SQL.Add('select * from TP_File ');
      sql.Add('where TFID=' + quotedstr(trim(Txt)));
      sql.Add('and TFType=''EWM''');
      open;
      if isempty then
      begin
        append;
        fieldbyname('TFID').Value := trim(Txt);
        fieldbyname('WBID').Value := trim(MSpid);
        fieldbyname('TFType').Value := 'EWM';
        fieldbyname('FillTime').Value := SGetServerDateTime(ADOQueryTemp);
        tblobfield(FieldByName('Filesother')).LoadFromFile(fImagePath);
        post;
      end;
    end;
  except
    application.MessageBox('条形码生成失败！', '提示信息', MB_ICONERROR);
    exit;
  end;
end;

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

procedure TfrmDJBCKOutPut.TBAddClick(Sender: TObject);
var
  MMaxno: string;
begin

  try
    frmDJBCKKCSelList2 := tfrmDJBCKKCSelList2.Create(Application);
    with frmDJBCKKCSelList2 do
    begin
      ToolButton1.Visible := True;
      if ShowModal = 1 then
      begin
        frmDJBCKKCSelList2.CDS_Main.DisableControls;
        with frmDJBCKKCSelList2.CDS_Main do
        begin
          First;
          while Locate('SSel', True, []) do
          begin
            if GetLSNo(ADOQueryCmd, MMaxno, 'DC', 'CK_SXPB_CR', 3, 1) = False then
            begin
              Application.MessageBox('取待检布入库编号失败!', '提示', 0);
              Exit;
            end;
            with Self.CDS_Sub do
            begin
              Append;
              FieldByName('CRTime').Value := Trim(FormatDateTime('yyyy-MM-dd', Now));
              FieldByName('SPID').Value := Trim(MMaxno);
              FieldByName('CRType').Value := '销售出库';

 //             FieldByName('CKOrderNo').Value := frmDJBCKKCSelList2.CDS_Main.fieldbyname('OrderNo').Value;
//              FieldByName('CustomerNoName').Value := frmDJBCKKCSelList2.CDS_Main.fieldbyname('CustomerNoName').AsString;
//              FieldByName('spcode').Value := frmDJBCKKCSelList2.CDS_Main.fieldbyname('spcode').AsString;
              FieldByName('SPColor').Value := frmDJBCKKCSelList2.CDS_Main.fieldbyname('SPColor').AsString;
//              FieldByName('SPID').Value := frmDJBCKKCSelList2.CDS_Main.fieldbyname('SPID').AsString;
//              FieldByName('SPHX').Value := frmDJBCKKCSelList2.CDS_Main.fieldbyname('SPHX').AsString;
              FieldByName('FactoryName').Value := frmDJBCKKCSelList2.CDS_Main.fieldbyname('FactoryName').Value;
//              FieldByName('FactoryNo').Value := frmDJBCKKCSelList2.CDS_Main.fieldbyname('FactoryNo').Value;
//              FieldByName('SSType').Value := frmDJBCKKCSelList2.CDS_Main.fieldbyname('SSType').Value;
              FieldByName('SPName').Value := frmDJBCKKCSelList2.CDS_Main.fieldbyname('SPName').Value;
//              FieldByName('SPSpec').Value := frmDJBCKKCSelList2.CDS_Main.fieldbyname('SPSpec').Value;
//              FieldByName('SPCF').Value := frmDJBCKKCSelList2.CDS_Main.fieldbyname('SPCF').Value;
//              FieldByName('SPMF').Value := frmDJBCKKCSelList2.CDS_Main.fieldbyname('SPMF').Value;
              FieldByName('SPKZ').Value := frmDJBCKKCSelList2.CDS_Main.fieldbyname('SPKZ').Value;
//              FieldByName('RCGangNo').Value := frmDJBCKKCSelList2.CDS_Main.fieldbyname('RCGangNo').Value;
//              FieldByName('FromFactoryName').Value := frmDJBCKKCSelList2.CDS_Main.fieldbyname('ToFactoryName').Value;
//              FieldByName('FromFactoryNo').Value := frmDJBCKKCSelList2.CDS_Main.fieldbyname('ToFactoryNo').Value;
//              FieldByName('ToFactoryName').Value := frmDJBCKKCSelList2.CDS_Main.fieldbyname('ToFactoryName').Value;
//              FieldByName('ToFactoryNo').Value := frmDJBCKKCSelList2.CDS_Main.fieldbyname('ToFactoryNo').Value;
              FieldByName('QtyUnit').Value := frmDJBCKKCSelList2.CDS_Main.fieldbyname('QtyUnit').Value;
              FieldByName('PiQty').Value := frmDJBCKKCSelList2.CDS_Main.fieldbyname('kcps').Value;
              FieldByName('Qty').Value := frmDJBCKKCSelList2.CDS_Main.fieldbyname('kcqty').Value;
              FieldByName('phtime').Value := frmDJBCKKCSelList2.CDS_Main.fieldbyname('phtime').Value;
//              FieldByName('SPID').Value := frmDJBCKKCSelList2.CDS_Main.fieldbyname('SPID').Value;
//              FieldByName('GXNO').Value := frmDJBCKKCSelList2.CDS_Main.fieldbyname('GXNO').AsInteger + 1;
            end;
            frmDJBCKKCSelList2.CDS_Main.Delete;
          end;
        end;
        frmDJBCKKCSelList2.CDS_Main.EnableControls;

      end;
    end;
  finally
    frmDJBCKKCSelList2.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 CK_SXPB_CR where SPID=''' + Trim(CDS_Sub.fieldbyname('SPID').AsString) + '''');
          Open;
        end;
        FSPID := Trim(ADOQueryTemp.fieldbyname('SPID').AsString);
        if Trim(FSPID) = '' then
        begin
//          if GetLSNo(ADOQueryCmd, Maxno, 'DC', 'CK_SXPB_CR', 3, 1) = False then
//          begin
//            Result := False;
//            ADOQueryCmd.Connection.RollbackTrans;
//            Application.MessageBox('取待检布出库编号失败!', '提示', 0);
//            Exit;
//          end;
          Maxno := Trim(CDS_Sub.fieldbyname('SPID').AsString);
        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('gxno').Value := Trim(CDS_Sub.fieldbyname('gxno').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('phtime').Value := Trim(CDS_Sub.fieldbyname('phtime').AsString);
          FieldByName('spid').Value := Trim(CDS_Sub.fieldbyname('spid').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 := '成品布';
          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 qty=(select sum(isnull(Qty,0)*CRQtyFlag) from CK_SXPB_CR A where A.FZSPID=CK_SXPB_CR.SPID)');
//          sql.Add(',PiQty=(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 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) + '''');
//          ShowMessage(SQL.Text);
//          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;

        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('  select FactoryName,SPName,QtyUnit,SPColor,phtime,spkz,sum(PiQty*CRQtyFlag) PiQty,sum(Qty*CRQtyFlag) Qty ');
          sql.Add('from CK_SXPB_CR X where CKName=''成品布''  and Valid=''Y'' ');
          sql.Add(' and FactoryName=''' + Trim(CDS_Sub.fieldbyname('FactoryName').AsString) + '''');
          sql.Add(' and SPName=''' + Trim(CDS_Sub.fieldbyname('SPName').AsString) + '''');
          sql.Add(' and QtyUnit=''' + Trim(CDS_Sub.fieldbyname('QtyUnit').AsString) + '''');
          sql.Add(' and phtime=''' + CDS_Sub.fieldbyname('phtime').AsString + '''');
          sql.Add(' and SPColor=''' + Trim(CDS_Sub.fieldbyname('SPColor').AsString) + '''');
          sql.Add(' and isnull(SPkz,'''')=''' + Trim(CDS_Sub.fieldbyname('spkz').AsString) + '''');
//          sql.Add(' and isnull(batchno,'''')=''' + Trim(CDS_Sub.fieldbyname('batchno').AsString) + '''');
          sql.Add(' group by spkz,FactoryName,SPName,QtyUnit,SPColor,phtime');
          sql.Add(' HAVING Sum(crQtyFlag*Qty)<0 or  Sum(crQtyFlag*PiQty)<0  ');

//         ShowMessage(SQL.Text);
          open;
        end;
        if not ADOQueryCmd.IsEmpty then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          CDS_Sub.EnableControls;
          Application.MessageBox('出库数量过多，不能保存!', '提示', 0);
          Exit;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
          sql.Add(' ' + quotedstr(trim(DName)));
          sql.Add(',getdate() ');
          sql.Add(',' + quotedstr(trim(self.Caption)));
          sql.Add(',' + quotedstr(trim('成品出库保存')));
          sql.Add(',' + quotedstr(trim('单号:' + trim(CDS_sub.FieldByName('spid').AsString) + ' 颜色:' + trim(CDS_sub.FieldByName('spcolor').AsString) + '品名:' + trim(CDS_sub.FieldByName('spname').AsString) + '数量:' + trim(CDS_sub.FieldByName('qty').AsString))));
          sql.Add(',' + quotedstr(trim('成功')));
          sql.Add(')');
          ExecSQL;
        end;


//        if ADOQueryTemp.FieldByName('MXKCQty').Value < 0 then
//        begin
//          CDS_Sub.DisableControls;
//          ADOQueryCmd.Connection.RollbackTrans;
//          Application.MessageBox('出库数量大于库存数量不能保存!', '提示', 0);
//          Exit;
//        end;

        CJEWM(Maxno);
        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, 'DR', '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;
            RTSetSaveDataCDS(ADOQueryCmd, Tv1, CDS_Sub, 'CK_SXPB_CR', 2);
            FieldByName('SPID').Value := Trim(MaxPYNo);
            FieldByName('FZSPID').Value := Trim(Maxno);
            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('phtime').Value := Trim(CDS_Sub.fieldbyname('crtime').AsString);
            FieldByName('FactoryNo').Value := Trim(CDS_Sub.fieldbyname('ToFactoryNo').AsString);
            FieldByName('fromFactoryName').Value := Trim(CDS_Sub.fieldbyname('FactoryName').AsString);
            FieldByName('FactoryName').Value := Trim(CDS_Sub.fieldbyname('ToFactoryName').AsString);
//            FieldByName('spkz').Value := Trim(CDS_Sub.fieldbyname('spkz').AsString);
            if Trim(CDS_Sub.fieldbyname('spkz').AsString) = '' then
              FieldByName('spkz').Value := null
            else
            begin
              FieldByName('spkz').Value := Trim(CDS_Sub.fieldbyname('spkz').AsString);
            end;
            if Trim(CDS_Sub.fieldbyname('batchno').AsString) = '' then
              FieldByName('batchno').Value := null
            else
            begin
              FieldByName('batchno').Value := Trim(CDS_Sub.fieldbyname('batchno').AsString);
            end;
//            FieldByName('batchno').Value := Trim(CDS_Sub.fieldbyname('batchno').AsString);
            FieldByName('CRFlag').Value := '入库';
            FieldByName('CRQtyFlag').Value := 1;
            FieldByName('xs').Value := 0;
            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;
//          with ADOQueryTemp do
//          begin
//            Close;
//            sql.Clear;
//            sql.Add(' exec P_View_WGGKC @StatsFlag=''负数库存'' ');
//            open;
//          end;
//          if not ADOQueryTemp.IsEmpty then
//          begin
//            ADOQueryTemp.Connection.RollbackTrans;
//            CDS_Sub.EnableControls;
//            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('成品出库登记', Tv1, '成品出库登记1');
  Close;
end;

procedure TfrmDJBCKOutPut.FormShow(Sender: TObject);
begin
  ReadCxGrid('成品出库登记', Tv1, '成品出库登记1');
  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(' 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('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
  if trim(CDS_Sub.FieldByName('CRType').AsString) = '销售出库' then
  begin
    try
      frmKHList := TfrmKHList.Create(Application);
      with frmKHList do
      begin
        if ShowModal = 1 then
        begin
          with Self.CDS_Sub do
          begin
            Edit;
            FieldByName('ToFactoryNo').Value := Trim(Order_Main.fieldbyname('CoCode').AsString);
            FieldByName('ToFactoryName').Value := Trim(Order_Main.fieldbyname('CoName').AsString);
            //Post;
          end;
        end;
      end;
    finally
      frmKHList.Free;
    end;
  end
  else
  begin
    try
      frmZdyAttachGYS := TfrmZdyAttachGYS.Create(Application);
      with frmZdyAttachGYS do
      begin
        if ShowModal = 1 then
        begin
          with Self.CDS_Sub do
          begin
            edit;
            FieldByName('ToFactoryNo').Value := Trim(frmZdyAttachGYS.CDS_HZ.fieldbyname('COCode').AsString);
            FieldByName('ToFactoryName').Value := Trim(frmZdyAttachGYS.CDS_HZ.fieldbyname('COName').AsString);
          //Post;
          end;
        end;
      end;
    finally
      frmZdyAttachGYS.Free;
    end;
  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('PRTColor').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('PRTColor').AsString);
          FieldByName('PRTHX').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('PRTHX').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;
  InitMXGrid(Trim(CDS_Sub.fieldbyname('SPID').AsString));
end;

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

procedure TfrmDJBCKOutPut.ToolButton1Click(Sender: TObject);
begin
  Panel4.Visible := True;
end;

procedure TfrmDJBCKOutPut.Button1Click(Sender: TObject);
var
  i: Integer;
begin
  for i := 0 to GroupBox1.ControlCount - 1 do
  begin
    if GroupBox1.Controls[i] is TCheckBox then
    begin
      TCheckBox(GroupBox1.Controls[i]).Checked := true;
    end;
  end;
end;

procedure TfrmDJBCKOutPut.Button2Click(Sender: TObject);
var
  CRType, GangNo, Note, RCGangNo, ToFactoryNo, ToFactoryName: string;
  PiQty: Integer;
  Xs, Qty: Double;
begin
  try
    if CDS_Sub.IsEmpty then
      Exit;
    CDS_Sub.DisableControls;
    with CDS_Sub do
    begin
      CRType := Trim(CDS_Sub.fieldbyname('CRType').AsString);
      GangNo := Trim(CDS_Sub.fieldbyname('GangNo').AsString);
     // batchno := Trim(CDS_Sub.fieldbyname('batchno').AsString);
      PiQty := CDS_Sub.fieldbyname('PiQty').AsInteger;
      Qty := CDS_Sub.fieldbyname('Qty').AsFloat;
      Xs := CDS_Sub.fieldbyname('Xs').AsFloat;
      Note := Trim(CDS_Sub.fieldbyname('Note').AsString);
      RCGangNo := Trim(CDS_Sub.fieldbyname('RCGangNo').AsString);
      ToFactoryNo := Trim(CDS_Sub.fieldbyname('ToFactoryNo').AsString);
      ToFactoryName := Trim(CDS_Sub.fieldbyname('ToFactoryName').AsString);
    end;
    with CDS_Sub do
    begin
      First;
      while not eof do
      begin
        Edit;
        if CheckBox1.Checked then
          FieldByName('CRType').Value := CRType;
        if CheckBox2.Checked then
          FieldByName('GangNo').Value := GangNo;
        if CheckBox3.Checked then
          FieldByName('PiQty').Value := PiQty;
        if CheckBox4.Checked then
          FieldByName('Xs').Value := Xs;
        if CheckBox5.Checked then
          FieldByName('Note').Value := Note;
        if CheckBox6.Checked then
          FieldByName('Qty').Value := Qty;
        if CheckBox7.Checked then
        begin
          FieldByName('ToFactoryNo').Value := ToFactoryNo;
          FieldByName('ToFactoryName').Value := ToFactoryName;
        end;
        Post;
        Next;
      end;
    end;
    CDS_Sub.EnableControls;
  except
    Application.MessageBox('替换异常!', '提示', 0);
  end;
end;

procedure TfrmDJBCKOutPut.ToolButton3Click(Sender: TObject);
var
  i: Integer;
  MaxNo, MMXID, MCRNO: string;
begin
  MCRNO := Trim(CDS_Sub.fieldbyname('SPID').AsString);
  if GetLSNo(ADOQueryTemp, MaxNo, 'MCM', 'CK_SXPB_CRMX', 3, 1) = False then
  begin
    Application.MessageBox('取最大号失败!', '提示', 0);
    Exit;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    for i := 1 to 10 do
    begin
      MMXID := Trim(MaxNo) + Trim(inttostr(i));
      with ADOQueryCmd do
      begin
        Close;
        SQL.Clear;
        sql.Add('insert into  CK_SXPB_CRMX (MXID,CRNO,MXQty)');
        sql.Add('values(' + quotedstr(MMXID));
        sql.Add(',' + quotedstr(Trim(MCRNO)));
        sql.Add(',0');
        sql.Add(')');
        ExecSQL;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    InitMXGrid(MCRNO);
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('添加失败!', '提示', 0);
  end;

end;

procedure TfrmDJBCKOutPut.ToolButton4Click(Sender: TObject);
begin
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('delete CK_SXPB_CRMX   where MXID=' + quotedstr(Trim(CDS_MX.FieldByName('MXID').AsString)));
    ExecSQL;
  end;
  CDS_MX.Delete;
end;

procedure TfrmDJBCKOutPut.cxGridDBColumn8PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin

  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(TvMX.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_MX do
  begin
    Edit;
    FieldByName(FFieldName).Value := mvalue;
    Post;
  end;

  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('update CK_SXPB_CRMX set ' + FFieldName + ' = ' + mvalue);
    sql.Add('  where MXID=' + quotedstr(Trim(CDS_MX.FieldByName('MXID').AsString)));
    ExecSQL;
  end;
  TvMX.Controller.EditingController.ShowEdit();
end;

procedure TfrmDJBCKOutPut.TvMXCustomDrawIndicatorCell(Sender: TcxGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxCustomGridIndicatorItemViewInfo; var ADone: Boolean);
var
  FValue: string;
  FBounds: TRect;
begin
  FBounds := AViewInfo.Bounds;
  if (AViewInfo is TcxGridIndicatorRowItemViewInfo) then
  begin
    ACanvas.FillRect(FBounds);
    ACanvas.DrawComplexFrame(FBounds, clBtnHighlight, clBtnShadow, [bBottom, bLeft, bRight], 1);
    FValue := IntToStr(TcxGridIndicatorRowItemViewInfo(AViewInfo).GridRecord.Index + 1);
    InflateRect(FBounds, -1, -1);    //Platform   specific.   May   not   work   on   Linux.
    ACanvas.Font.Color := clBlack;
    ACanvas.Brush.Style := bsClear;
    ACanvas.DrawText(FValue, FBounds, cxAlignCenter or cxAlignTop);
    ADone := True;
  end;
end;

procedure TfrmDJBCKOutPut.TvMXKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
  TvMX.OptionsData.Editing := False;
  if Key = 13 then
    Key := 40;
  TvMX.OptionsData.Editing := True;
end;

end.

