unit U_DyeClothOutPut;

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, dxSkinsCore,
  dxSkinsDefaultPainters, cxNavigator, dxDateRanges, dxBarBuiltInMenu,
  U_BaseInput, System.ImageList, Vcl.ImgList, dxScrollbarAnnotations, cxCheckBox;

type
  TfrmDyeClothOutPut = class(TfrmBaseInput)
    ToolBar1: TToolBar;
    TBAdd: TToolButton;
    TBDel: TToolButton;
    TBSave: TToolButton;
    TBClose: TToolButton;
    DataSource3: TDataSource;
    CDS_Sub: TClientDataSet;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    cxGridPopupMenu2: TcxGridPopupMenu;
    ToolButton1: TToolButton;
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    Tv1Column2: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    Tv1OrderNo: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    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 Tv1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure ToolButton1Click(Sender: TObject);
    procedure Tv1Column2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column13PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
  private
    { Private declarations }
    function SaveCKData(): Boolean;
  public
    { Public declarations }
    FIOID, FStkName, FKHName, FWorkshop: string;
  end;

var
  frmDyeClothOutPut: TfrmDyeClothOutPut;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_CompanySel, U_DyeClothStkSel;

{$R *.dfm}

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

procedure TfrmDyeClothOutPut.TBAddClick(Sender: TObject);
begin
  try
    frmDyeClothStkSel := TfrmDyeClothStkSel.Create(Application);
    with frmDyeClothStkSel do
    begin
      FstkName := Self.FstkName;
      if ShowModal = 1 then
      begin
        frmDyeClothStkSel.CDS_Main.DisableControls;
        with frmDyeClothStkSel.CDS_Main do
        begin
          First;
          while frmDyeClothStkSel.CDS_Main.Locate('SSel', True, []) do
          begin
            with Self.CDS_Sub do
            begin
              Append;
              FieldByName('IOTime').Value := Trim(FormatDateTime('yyyy-MM-dd hh:mm:ss',SGetServerDateTime(ADOQueryCmd)));
              FieldByName('STKID').Value := frmDyeClothStkSel.CDS_Main.fieldbyname('BCIOID').Value;
              FieldByName('IOType').Value := '加工出库';
              FieldByName('IONO').Value := frmDyeClothStkSel.CDS_Main.fieldbyname('OrderNo').Value;
              FieldByName('C_Name').Value := frmDyeClothStkSel.CDS_Main.fieldbyname('C_Name').Value;

              FieldByName('Piece').Value := frmDyeClothStkSel.CDS_Main.fieldbyname('StkPiece').Value;
              FieldByName('Meter').Value := frmDyeClothStkSel.CDS_Main.fieldbyname('StkMeter').Value;
              FieldByName('C_Width').Value := frmDyeClothStkSel.CDS_Main.fieldbyname('C_Width').Value;

            end;
            frmDyeClothStkSel.CDS_Main.Delete;
          end;
        end;
        frmDyeClothStkSel.CDS_Main.EnableControls;
      end;
    end;
  finally
    frmDyeClothStkSel.Free;
  end;

end;

function TfrmDyeClothOutPut.SaveCKData(): Boolean;
var
  MBCIOID, Maxno: 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 Dye_Cloth_IO where IOID=''' + Trim(CDS_Sub.fieldbyname('IOID').AsString) + '''');
          Open;
        end;
        MBCIOID := Trim(ADOQueryTemp.fieldbyname('IOID').AsString);
        if Trim(MBCIOID) = '' then
        begin
          if not GetLSNo(ADOQueryCmd, Maxno, 'C', 'Dye_Cloth_IO', 5, 1) then
            raise Exception.Create('取成品出库编号失败！');
        end
        else
        begin
          Maxno := Trim(MBCIOID);
        end;

        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from Dye_Cloth_IO where IOID=''' + Trim(Maxno) + '''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(MBCIOID) = '' then
          begin
            Append;
            FieldByName('Filler').Value := Trim(DName);
            FieldByName('FillTime').Value := SGetServerDate(ADOQueryTemp);
          end
          else
          begin
            Edit;
            FieldByName('Editer').Value := Trim(DName);
            FieldByName('EditTime').Value := SGetServerDate(ADOQueryTemp);
          end;
//          RTSetSaveDataCDS(ADOQueryCmd, Tv1, CDS_Sub, 'Dye_Cloth_IO', 2);

          FieldByName('IOID').Value := Trim(Maxno);
          FieldByName('IONo').Value := CDS_Sub.FieldByName('IONo').asString;
          FieldByName('IOTime').Value := CDS_Sub.FieldByName('IOTime').asString;
          FieldByName('IOType').Value := CDS_Sub.FieldByName('IOType').asString;
          FieldByName('Meter').Value := CDS_Sub.FieldByName('Meter').asString;
          FieldByName('Piece').Value := CDS_Sub.FieldByName('Piece').asString;

          FieldByName('IOFlag').Value := '出库';
          FieldByName('IOQtyFlag').Value := -1;
          Post;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update Dye_Cloth_Stock Set StkPiece=(select Sum(isnull(Piece,0)*IOQtyFlag) from Dye_Cloth_IO A where A.IONO=Dye_Cloth_Stock.OrderNo)');
          sql.Add(',STKMeter=(select Sum(isnull(Meter,0)*IOQtyFlag) from Dye_Cloth_IO A where A.IONO=Dye_Cloth_Stock.OrderNo )');
          sql.Add('where OrderNo=''' + Trim(CDS_Sub.fieldbyname('IONO').AsString) + '''');
          ExecSQL;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from Dye_Cloth_Stock where OrderNo=''' + Trim(CDS_Sub.fieldbyname('IONO').AsString) + '''');
          Open;
        end;

        if (ADOQueryCmd.FieldByName('StkPiece').AsString = '0') and (ADOQueryCmd.FieldByName('StkMeter').AsString = '0') then
        begin
          ADOQueryCmd.Edit;
          ADOQueryCmd.FieldByName('StkExists').Value := 0;
          ADOQueryCmd.Post;
        end
        else
        begin
          ADOQueryCmd.Edit;
          ADOQueryCmd.FieldByName('StkExists').Value := 1;
          ADOQueryCmd.Post;
        end;

        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from Dye_Cloth_Stock where OrderNo=''' + Trim(CDS_Sub.fieldbyname('IONO').AsString) + '''');
          Open;
        end;

        if ADOQueryTemp.FieldByName('StkMeter').Value < 0 then
          raise Exception.Create('出库数量大于入库数量不能保存！');

        if ADOQueryTemp.FieldByName('StkPiece').Value < 0 then
          raise Exception.Create('出库匹数大于入库匹数不能保存！');

        Edit;
        FieldByName('IOID').Value := Trim(Maxno);
        Post;
        Next;
      end;
    end;
    CDS_Sub.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    Result := True;
  except
    Result := False;
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;

end;

procedure TfrmDyeClothOutPut.TBCloseClick(Sender: TObject);
begin

  Close;
end;

procedure TfrmDyeClothOutPut.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if Key = VK_ESCAPE then  // VK_ESCAPE 是 ESC 键的常量
  begin
    Key := 0;  // 清除按键，防止继续传递
    Close;     // 或者 ModalResult := mrCancel;
  end;
end;

procedure TfrmDyeClothOutPut.FormShow(Sender: TObject);
var
  fsj: string;
begin
  inherited;
  ReadCxGrid(FSTKName + '成品出库录入', Tv1, '成品出库录入');
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(' select A.* ');
    sql.Add(' from V_Dye_Cloth_IO A');
    sql.Add(' where IOID=''' + Trim(FIOID) + '''');
    Open;
  end;
  SCreateCDS(ADOQueryTemp, CDS_Sub);
  SInitCDSData(ADOQueryTemp, CDS_Sub);
  KeyPreview := True;  // 确保表单可以预览按键
//  fsj:='select name=CJName ,Code=null from CheJian';
//  SInitCxGridComboBoxBySql(ADOQueryTemp,v1CheJian,fsj,0,True,'');
end;

procedure TfrmDyeClothOutPut.TBDelClick(Sender: TObject);
begin
  if CDS_Sub.IsEmpty then
    Exit;
  if Trim(CDS_Sub.fieldbyname('IOID').AsString) <> '' then
  begin
    if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
      Exit;

    try
      ADOQueryCmd.Connection.BeginTrans;
//      with ADOQueryCmd do
//      begin
//        Close;
//        Sql.Clear;
//        Sql.Add('exec P_BS_Cloth_Out_Del ');
//        Sql.Add(' @BCIOIDS=' + quotedstr(Trim(CDS_Sub.fieldbyname('BCIOID').AsString)));
//        Sql.Add(',@DCode=' + quotedstr(Trim(DCode)));
//        Sql.Add(',@DName=' + quotedstr(Trim(DName)));
//        Open;
//      end;
//      if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
//        raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));

      ADOQueryCmd.Connection.CommitTrans;
      CDS_Sub.Delete;
    except
      ADOQueryCmd.Connection.RollbackTrans;
      application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
    end;
  end
  else
    CDS_Sub.Delete;
end;

procedure TfrmDyeClothOutPut.TBSaveClick(Sender: TObject);
begin


  if CDS_Sub.IsEmpty then
    Exit;
  if CDS_Sub.Locate('IOTime', null, []) = True then
  begin
    Application.MessageBox('出库时间不能为空!', '提示', 0);
    Exit;
  end;
  if CDS_Sub.Locate('IOType', null, []) = True then
  begin
    Application.MessageBox('出库类型不能为空!', '提示', 0);
    Exit;
  end;

  if CDS_Sub.Locate('Piece', null, []) = True then
  begin
    Application.MessageBox('匹数不能为空!', '提示', 0);
    Exit;
  end;
  if CDS_Sub.Locate('Meter', null, []) = True then
  begin
    Application.MessageBox('米数不能为空!', '提示', 0);
    Exit;
  end;

//  if CDS_Sub.Locate('Meter', 0, []) = True then
//  begin
//    Application.MessageBox('米数不能为0!', '提示', 0);
//    Exit;
//  end;
//  if CDS_Sub.Locate('Piece', 0, []) = True then
//  begin
//    Application.MessageBox('匹数不能为0!', '提示', 0);
//    Exit;
//  end;

  CDS_Sub.First;
  while not CDS_Sub.Eof do
  begin
    if (CDS_Sub.FieldByName('Meter').AsFloat = 0) and (CDS_Sub.FieldByName('Piece').AsFloat = 0) then
    begin
      Application.MessageBox('数量匹数不能都为0!', '提示', 0);
      Exit;
    end;
    CDS_Sub.Next;
  end;

  if CDS_Sub.Locate('IONO', null, []) = True then
  begin
    Application.MessageBox('计划单号不能为空!', '提示', 0);
    Exit;
  end;
  if SaveCKData() then
  begin
    Application.MessageBox('保存成功!', '提示', 0);
    //ModalResult:=1;
    Exit;
  end;
end;

procedure TfrmDyeClothOutPut.ToolButton1Click(Sender: TObject);
begin
  WriteCxGrid(fSTKName + '成品出库录入', Tv1, '成品出库录入');

end;

procedure TfrmDyeClothOutPut.Tv1Column2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin

  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      if Trim(self.CDS_Sub.fieldbyname('IOType').AsString) = '退还' then
        FCoType := '客户'
      else
        FCoType := '供应商';
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('ToCoNo').Value := Trim(CDS_1.fieldbyname('CoCode').AsString);
          FieldByName('ToCoName').Value := Trim(CDS_1.fieldbyname('CoAbbrName').AsString);
        end;
      end;
    end;
  finally
    frmCompanySel.Free;
  end;

end;

procedure TfrmDyeClothOutPut.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('BCIOID').AsString) <> '' then
//  begin
//    with ADOQueryTemp do
//    begin
//      Close;
//      sql.Clear;
//      sql.Add('select * from BS_Cloth_IO where BCIOID=''' + Trim(CDS_Sub.fieldbyname('BCIOID').AsString) + '''');
//      Open;
//    end;
//    FFSPID := Trim(ADOQueryTemp.fieldbyname('BCIOID').AsString);
//    if Trim(FFSPID) <> '' then
//    begin
//      v1CRType.Options.Editing := False;
//    end
//    else
//    begin
//      v1CRType.Options.Editing := True;
//    end;
//  end
//  else
//  begin
//    v1CRType.Options.Editing := True;
//  end;
end;

procedure TfrmDyeClothOutPut.v1Column13PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
//  with ADOQueryTemp do
//  begin
//    close;
//    sql.Clear;
//    sql.Add('exec P_OrderCR');
//
////    ShowMessage(sql.Text);
//    execsql;
//  end;
//
//  frmProductOrderListSel := TfrmProductOrderListSel.create(self);
//  with frmProductOrderListSel do
//  begin
//    FWorkshop := self.FWorkshop;
//    if ShowModal = 1 then
//    begin
//      Self.CDS_Sub.FieldByName('OrderNo').Value := Order_Main.fieldbyname('OrderNo').asstring;
////      initGlide();
//    end;
//    free;
//  end;
end;

end.

