unit U_SXCKMXLR;

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, BtnEdit, cxContainer,
  cxTextEdit, cxCurrencyEdit, cxCheckBox, Menus, RM_Common, RM_Class, RM_e_Xls,
  RM_Dataset, RM_System, RM_GridReport;

type
  TfrmSXCKMXLR = class(TForm)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    ADOQueryCmd: TADOQuery;
    ADOQueryTmp: TADOQuery;
    DataSource1: TDataSource;
    CDS_Main: TClientDataSet;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Panel3: TPanel;
    ADOQueryMain: TADOQuery;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    ADOQueryPrint: TADOQuery;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    cxGrid2: TcxGrid;
    TvMX: TcxGridDBTableView;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    vMXColumn1: TcxGridDBColumn;
    ToolButton3: TToolButton;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure v1P_CodeNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure TBCloseClick(Sender: TObject);
    procedure FHAddressBtnDnClick(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure TvMXCustomDrawIndicatorCell(Sender: TcxGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxCustomGridIndicatorItemViewInfo; var ADone: Boolean);
    procedure ToolButton3Click(Sender: TObject);
  private
    procedure InitGrid();
    function savedate(): Boolean;
    { Private declarations }
  public
    fkeyNO, FMXGangHao: string;
    FType: integer;
    fCKName: string;
    { Public declarations }
  end;

var
  frmSXCKMXLR: TfrmSXCKMXLR;

implementation

uses
  U_DataLink, U_Fun10, U_ZDYHelp, U_CPKcList, U_ZdyAttachment, U_ZDYHelpSel,
  U_ClothGMListSel, U_ZdyAttachGYS, U_CPManageSel;
{$R *.dfm}

function TfrmSXCKMXLR.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_ML_CRMX');
          sql.Add('where MXID=' + quotedstr(Trim(CDS_Main.FieldByName('MXID').AsString)));
          open;
        end;
        if ADOQueryCmd.IsEmpty then
        begin
          if GetLSNo(ADOQueryTmp, MaxNo, 'MCM', 'CK_ML_CRMX', 4, 1) = False then
          begin
            Application.MessageBox('取最大号失败!', '提示', 0);
            Exit;
          end;
          ADOQueryCmd.Append;
        end
        else
        begin
          MaxNo := Trim(CDS_Main.FieldByName('MXID').AsString);
          ADOQueryCmd.Edit;
        end;



        //保存明细
        RTSetSaveDataCDS(ADOQueryCmd, TvMX, CDS_Main, 'CK_ML_CRMX', 0);

         //保存隐藏ID
        ADOQueryCmd.FieldByName('CRNO').Value := trim(fkeyNo);
        ADOQueryCmd.FieldByName('MXID').Value := trim(MaxNo);

        ADOQueryCmd.Post;

        CDS_Main.Edit;
        CDS_Main.FieldByName('MXID').Value := trim(MaxNo);
        CDS_Main.post;

         //更新库存
        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          SQL.Add('exec P_Up_MlKcByMx ' + quotedstr(trim(fkeyNo)));
          Open;
        end;

        if Trim(ADOQueryCmd.FieldByName('Msg').AsString) = '操作失败' then
        begin
          CDS_Main.EnableControls;
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('入库数量少于出库数量!', '提示', 0);
          Exit;
        end;

        CDS_Main.next;
      end;
    end;

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

procedure TfrmSXCKMXLR.InitGrid();
begin
  try
    with adoqueryMain do
    begin
      close;
      sql.Clear;
      sql.Add('select * from CK_ML_CRMX where CRNO=' + quotedstr(trim(fkeyNO)));
      sql.Add(' order by MXID');
      open;
    end;
    SCreateCDS20(adoqueryMain, CDS_Main);
    SInitCDSData20(adoqueryMain, CDS_Main);

  finally
  end;
end;

procedure TfrmSXCKMXLR.FormDestroy(Sender: TObject);
begin
  frmSXCKMXLR := nil;
end;

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

procedure TfrmSXCKMXLR.FormShow(Sender: TObject);
var
  fbhType, fInoutNo: string;
begin
  readCxGrid(self.Caption, TvMX, '面料仓库');
  InitGrid();
end;

procedure TfrmSXCKMXLR.TBSaveClick(Sender: TObject);
begin
  if cds_main.IsEmpty then
    exit;
  ToolBar1.SetFocus;
  if savedate() then
  begin
    application.MessageBox('数据保存成功！', '提示信息');
  end
  else
  begin
    application.MessageBox('数据保存失败！', '提示信息', MB_ICONERROR);
  end;
end;

procedure TfrmSXCKMXLR.v1P_CodeNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'Cloth';
      flagname := '坯布名称';
      if ShowModal = 1 then
      begin
        Self.CDS_Main.Edit;
        Self.CDS_Main.FieldByName('P_CodeName').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        Self.CDS_Main.FieldByName('P_Code').Value := Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmSXCKMXLR.TBCloseClick(Sender: TObject);
begin
  writeCxGrid(self.Caption, TvMX, '面料仓库');
  ModalResult := 1;
end;

procedure TfrmSXCKMXLR.FHAddressBtnDnClick(Sender: TObject);
begin
  TBtnEditC(Sender).Text := '';
  TBtnEditC(Sender).TxtCode := '';
end;

procedure TfrmSXCKMXLR.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_Main, True);
end;

procedure TfrmSXCKMXLR.N2Click(Sender: TObject);
begin
  SelOKNo(CDS_Main, False);
end;

procedure TfrmSXCKMXLR.ToolButton1Click(Sender: TObject);
var
  i: Integer;
begin
  for i := 1 to 10 do
  begin
    with CDS_Main do
    begin
      Append;
      FieldByName('MXGangHao').Value := FMXGangHao;
      FieldByName('MXQty').Value := 0;
      Post;
    end;
  end;
end;

procedure TfrmSXCKMXLR.ToolButton2Click(Sender: TObject);
begin
  if cds_main.IsEmpty then
    exit;
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('delete CK_ML_CRMX where MXID=' + quotedstr(Trim(CDS_Main.FieldByName('MXID').AsString)));
    ExecSQL;
  end;
  CDS_Main.Delete;
end;

procedure TfrmSXCKMXLR.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 TfrmSXCKMXLR.ToolButton3Click(Sender: TObject);

begin
    if CDS_Main.IsEmpty then Exit;
  OneKeyPost(TvMX,CDS_Main);
end;


end.

