unit U_EmptyRollerRk;

interface

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

type
  TfrmEmptyRollerRk = class(TForm)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1P_CodeName: TcxGridDBColumn;
    v1PRTSpec: TcxGridDBColumn;
    v1Qty: TcxGridDBColumn;
    v1Note: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    ADOQueryCmd: TADOQuery;
    ADOQueryTmp: TADOQuery;
    DataSource1: TDataSource;
    CDS_Main: TClientDataSet;
    cxGridPopupMenu1: TcxGridPopupMenu;
    v1P_Code: TcxGridDBColumn;
    Label6: TLabel;
    v1Column5: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure v1QtyPropertiesEditValueChanged(Sender: TObject);
    procedure v1Column2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure Tv1CustomDrawIndicatorCell(Sender: TcxGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxCustomGridIndicatorItemViewInfo; var ADone: Boolean);
    procedure v1Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column5PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column6PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
  private
    procedure InitGrid();
    function savedate(): Boolean;
    function CheckData(): Boolean;
    { Private declarations }
  public
    fkeyNO: string;
    FType: integer;

    { Public declarations }
  end;

var
  frmEmptyRollerRk: TfrmEmptyRollerRk;

implementation

uses
  U_DataLink, U_Fun10, U_ZDYHelp, U_ZdyAttachment, U_ZdyAttachGYS,
  U_ClothSCHZListSel; 
{$R *.dfm}

function TfrmEmptyRollerRk.CheckData(): Boolean;
begin
  try
    if cds_main.IsEmpty then
      raise Exception.Create('产品信息不能为空！');

    if cds_main.Locate('CRType', NULL, []) then
      raise Exception.Create('入库类型不能为空！');
    if cds_main.Locate('CRType', '', []) then
      raise Exception.Create('入库类型不能为空！');

    if cds_main.Locate('fromFactoryName', NULL, []) then
      raise Exception.Create('来自工厂不能为空！');
    if cds_main.Locate('fromFactoryName', '', []) then
      raise Exception.Create('来自工厂不能为空！');

    if cds_main.Locate('FactoryName', NULL, []) then
      raise Exception.Create('存放工厂不能为空！');
    if cds_main.Locate('FactoryName', '', []) then
      raise Exception.Create('存放工厂不能为空！');

    if cds_main.Locate('Rollnum', NULL, []) then
      raise Exception.Create('数量不能为空！');
    if cds_main.Locate('Rollnum', 0, []) then
      raise Exception.Create('数量不能为0！');

    if cds_main.Locate('P_CodeName', NULL, []) then
      raise Exception.Create('空轴名称不能为空！');
    if cds_main.Locate('P_CodeName', '', []) then
      raise Exception.Create('空轴名称不能为空！');
    Result := True;
  except
    Result := false;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;
end;

function TfrmEmptyRollerRk.savedate(): Boolean;
var
  MaxNo: string;
begin
  result := false;
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_Main.DisableControls;
    CDS_Main.First;
    with CDS_Main do
    begin
      while not eof do
      begin
        with ADOQueryCmd do
        begin
          close;
          sql.Clear;
          sql.Add('select * from CK_WarpRoller_CR');
          sql.Add('where CRNO=' + quotedstr(Trim(CDS_Main.FieldByName('CRNO').AsString)));
          open;
        end;
        if ADOQueryCmd.IsEmpty then
        begin
          if GetLSNo(ADOQueryTmp, MaxNo, 'WR', 'CK_WarpRoller_CR', 3, 1) = False then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取最大号失败!', '提示', 0);
            Exit;
          end;
          ADOQueryCmd.Append;
          ADOQueryCmd.FieldByName('Filler').Value := trim(DName);
          ADOQueryCmd.FieldByName('FillTime').Value := dateTimetostr(SGetServerDateTime(adoqueryTmp));
        end
        else
        begin
          MaxNo := Trim(CDS_Main.FieldByName('CRNO').AsString);

          ADOQueryCmd.Edit;
          ADOQueryCmd.FieldByName('editer').Value := trim(dName);
          ADOQueryCmd.FieldByName('editTime').Value := SGetServerDateTime(adoqueryTmp);
        end;

        //保存主表
        ADOQueryCmd.FieldByName('CRNO').Value := trim(MaxNo);
        ADOQueryCmd.FieldByName('InoutNo').Value := trim(MaxNo);
        ADOQueryCmd.FieldByName('CKName').Value := trim('空轴');
        ADOQueryCmd.FieldByName('CRFlag').Value := '入库';
        ADOQueryCmd.FieldByName('CRQtyFlag').Value := 1;

        //保存明细
        RTSetSaveDataCDS(ADOQueryCmd, Tv1, CDS_Main, 'CK_WarpRoller_CR', 0);

        //保存隐藏ID
        ADOQueryCmd.FieldByName('qty').Value := fieldbyname('qty').Value;
        ADOQueryCmd.FieldByName('frommainID').Value := trim(fieldbyname('frommainID').AsString);
        ADOQueryCmd.FieldByName('fromsubID').Value := trim(fieldbyname('fromsubID').AsString);
        ADOQueryCmd.Post;

        //判断库存
        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          SQL.Add('exec P_Up_EmptyRoller ' + quotedstr(trim(MaxNo)));
          Open;
        end;
        if Trim(ADOQueryCmd.FieldByName('Msg').AsString) = '操作失败' then
        begin
          CDS_Main.EnableControls;
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('入库数量少于出库数量!', '提示', 0);
          Exit;
        end;

        CDS_Main.Edit;
        CDS_Main.FieldByName('CRNO').Value := Trim(MaxNo);
        CDS_Main.FieldByName('InoutNo').Value := Trim(MaxNo);
        CDS_Main.Post;
        Next;
      end;
    end;
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    result := true;
  except
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
  end;
end;

procedure TfrmEmptyRollerRk.InitGrid();
var
  i: Integer;
begin
  try
    with adoqueryTmp do
    begin
      close;
      sql.Clear;
      sql.Add('select A.* ');
      SQL.Add(' from CK_WarpRoller_CR A');
      sql.Add('where  A.CRNO=' + quotedstr(trim(fkeyNO)));
      open;
    end;
    SCreateCDS20(adoqueryTmp, CDS_Main);
    SInitCDSData20(adoqueryTmp, CDS_Main);
    if not CDS_Main.IsEmpty then
    begin

    end;
  finally
  end;
end;

procedure TfrmEmptyRollerRk.FormDestroy(Sender: TObject);
begin
  frmEmptyRollerRk := nil;
end;

procedure TfrmEmptyRollerRk.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := cahide;
end;

procedure TfrmEmptyRollerRk.FormShow(Sender: TObject);
begin
  readCxGrid(trim(self.Caption), Tv1, '仓储管理');
  InitGrid();
end;

procedure TfrmEmptyRollerRk.TBSaveClick(Sender: TObject);
begin
  if CheckData() = false then
    exit;

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

procedure TfrmEmptyRollerRk.TBCloseClick(Sender: TObject);
begin
  writeCxGrid(trim(self.Caption), Tv1, '仓储管理');
  close;
end;

procedure TfrmEmptyRollerRk.v1QtyPropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  if Trim(mvalue) = '' then
  begin
    mvalue := '0';
  end;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_Main do
  begin
    Edit;
    FieldByName(FFieldName).Value := Trim(mvalue);
    FieldbyName('Money').Value := format('%.2f', [FieldByName('Qty').AsFloat * FieldByName('Price').AsFloat]);
    Post;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmEmptyRollerRk.v1Column2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  frmClothSCHZListSel := TfrmClothSCHZListSel.Create(Application);
  with frmClothSCHZListSel do
  begin
    if showmodal = 1 then
    begin
      CDS_Main.Edit;
      CDS_Main.fieldbyname('FromOrderNo').Value := trim(Order_Main.fieldbyname('OrderNo').AsString);
      CDS_Main.fieldbyname('FromMainID').Value := trim(Order_Main.fieldbyname('MainID').AsString);
      CDS_Main.fieldbyname('FromsubID').Value := trim(Order_Main.fieldbyname('subID').AsString);
      CDS_Main.fieldbyname('P_Code').Value := trim(Order_Main.fieldbyname('C_Code').AsString);
      CDS_Main.fieldbyname('P_CodeName').Value := trim(Order_Main.fieldbyname('C_CodeName').AsString);
      CDS_Main.fieldbyname('P_SPEC').Value := trim(Order_Main.fieldbyname('C_Spec').AsString);
      CDS_Main.fieldbyname('P_Color').Value := trim(Order_Main.fieldbyname('C_Color').AsString);
      CDS_Main.fieldbyname('P_MF').Value := trim(Order_Main.fieldbyname('MF').AsString);
      CDS_Main.fieldbyname('P_KZ').Value := trim(Order_Main.fieldbyname('KZ').AsString);
      CDS_Main.Post;
    end;
    free;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

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

procedure TfrmEmptyRollerRk.v1Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'kuwei';
      flagname := '库位';

      if ShowModal = 1 then
      begin
        Self.CDS_Main.Edit;
        Self.CDS_Main.FieldByName('kuwei').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        Self.CDS_Main.Post;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
  self.tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmEmptyRollerRk.v1Column5PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZdyAttachGYS := TfrmZdyAttachGYS.Create(Application);
    with frmZdyAttachGYS do
    begin
      if ShowModal = 1 then
      begin
        Self.CDS_Main.Edit;
        Self.CDS_Main.fieldbyname('fromFactoryName').Value := Trim(CDS_HZ.fieldbyname('CoName').AsString);
        Self.CDS_Main.Post;

      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmEmptyRollerRk.v1Column6PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZdyAttachGYS := TfrmZdyAttachGYS.Create(Application);
    with frmZdyAttachGYS do
    begin
      if ShowModal = 1 then
      begin
        CDS_Main.Edit;
        CDS_Main.fieldbyname('FactoryName').Value := Trim(CDS_HZ.fieldbyname('CoName').AsString);
        CDS_Main.Post;
      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmEmptyRollerRk.ToolButton4Click(Sender: TObject);
begin

  if CDS_Main.IsEmpty then
  begin
    with CDS_Main do
    begin
      append;
      FieldbyName('CRTime').Value := Now;
      FieldbyName('CPType').Value := '正品';
      FieldbyName('QtyUnit').Value := 'M';
      FieldbyName('Rollnum').Value := 0;
      FieldbyName('Price').Value := 0;
      FieldbyName('qty').Value := 0;

      post;
    end;
  end
  else
  begin
    CopyAddRowCDS(CDS_Main);
    with CDS_Main do
    begin
      edit;

      FieldbyName('CRNO').Value := '';
      post;
    end;
  end;
end;

procedure TfrmEmptyRollerRk.ToolButton3Click(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    exit;
  if Trim(CDS_Main.FieldByName('CRNO').AsString) = '' then
  begin
    CDS_Main.Delete;
  end
  else
  begin
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('update CK_WarpRoller_CR set Valid=''N'' where CRNO=' + quotedstr(Trim(CDS_Main.FieldByName('CRNO').AsString)));
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      SQL.Add('exec P_Up_EmptyRoller ' + quotedstr(Trim(CDS_Main.FieldByName('CRNO').AsString)));
      Open;
    end;
    if Trim(ADOQueryCmd.FieldByName('Msg').AsString) = '操作失败' then
    begin
      CDS_Main.EnableControls;
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('出库数量超出库存数量!', '提示', 0);
      Exit;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    CDS_Main.Delete;
  end;
end;

end.

