unit U_ShaRKInPut;

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, StdCtrls, ExtCtrls,
  RM_Dataset, RM_System, RM_Common, RM_Class, RM_GridReport, BtnEdit;

type
  TfrmShaRKInPut = class(TForm)
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column2: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    ToolBar1: TToolBar;
    TBDel: TToolButton;
    TBSave: TToolButton;
    TBClose: TToolButton;
    v1Column4: TcxGridDBColumn;
    DataSource3: TDataSource;
    CDS_Sub: TClientDataSet;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    cxGridPopupMenu2: TcxGridPopupMenu;
    v1Note: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Price: TcxGridDBColumn;
    RM1: TRMGridReport;
    RMDBHZ: TRMDBDataSet;
    CDS_HZ: TClientDataSet;
    v1Column5: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    ToolButton1: TToolButton;
    v1Column1: TcxGridDBColumn;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure v1Column3PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column14PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v2Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column5PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column11PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column16PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure Tv1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure v1Column10PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ToolButton1Click(Sender: TObject);
  private
    { Private declarations }
    function SaveCKData(): Boolean;
    procedure prindata();
  public
    { Public declarations }
    FBCId, canshu3: string;
  end;

var
  frmShaRKInPut: TfrmShaRKInPut;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_ZdyAttachGYS, U_JTSInfoSelList,
  U_YarnListSel;

{$R *.dfm}

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

procedure TfrmShaRKInput.prindata();
var
  filepath: string;
  mBegdate, mEnddate: string;
  FName, FNameSX, FNameJX: string;
  i: Integer;
  maxno, fPrintFile: string;
  Txt, fImagePath: string;
  Moudle: THandle;
  Makebar: TMakebar;
  Mixtext: TMixtext;
begin
  with ADOQueryCmd do
  begin
    Filtered := False;
    Close;
    sql.Clear;
    sql.Add(' select A.* ');
    sql.Add(',FillNo=(select B.Userid From SY_User B where B.UserName=A.Filler)');
    sql.Add(' from CK_SXPB_CR A');
    sql.add(' where A.Y_ID=''' + Trim(CDS_Sub.Fieldbyname('Y_ID').asstring) + '''');
    sql.Add(' and isnull(CKName,'''')=''纱线''');
    SQL.Add(' and CRFlag=''入库''  ');
    Sql.Add(' and ZFFlag=0 ');
    Open;
  end;
  SCreateCDS20(ADOQueryCmd, CDS_HZ);
  SInitCDSData20(ADOQueryCmd, CDS_HZ);
  try
    Moudle := LoadLibrary('MakeQRBarcode.dll');
    @Makebar := GetProcAddress(Moudle, 'Make');
    @Mixtext := GetProcAddress(Moudle, 'MixText');
    Txt := Trim(CDS_Sub.Fieldbyname('Y_ID').asstring);
    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);
  except
    application.MessageBox('条形码生成失败！', '提示信息', MB_ICONERROR);
    exit;
  end;
  filepath := ExtractFilePath(Application.ExeName) + 'report\纱线入库标签.rmf';
  if not FileExists(Pchar(filepath)) then
  begin
    application.MessageBox(pchar('文件[' + filepath + ']不存在！'), '提示信息', MB_IConError);
    exit;
  end;
  RMVariables['QRBARCODE'] := fImagePath;
  RM1.LoadFromFile(filepath);
  RM1.PrintReport;
end;

function TfrmShaRKInPut.SaveCKData(): Boolean;
var
  FCRID, Maxno, FFY_ID: 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_Yarn_CR where Y_ID=''' + Trim(CDS_Sub.fieldbyname('Y_ID').AsString) + '''');
          open;
        end;
        FFY_ID := Trim(ADOQueryTemp.fieldbyname('Y_ID').AsString);
        if Trim(FFY_ID) = '' then
        begin
          if GetLSNo(ADOQueryCmd, Maxno, 'YR', 'CK_Yarn_CR', 3, 1) = False then
          begin
            Result := False;
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取纱线入库编号失败!', '提示', 0);
            Exit;
          end;
        end
        else
        begin
          Maxno := Trim(FFY_ID);
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from CK_Yarn_CR where Y_ID=''' + Trim(Maxno) + '''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(FFY_ID) = '' then
          begin
            Append;
          end
          else
          begin
            Edit;
          end;
          FieldByName('Y_ID').Value := Trim(Maxno);
          FieldByName('KCID').Value := Trim(Maxno);
          FieldByName('CKName').Value := '纱线';
          FieldByName('Conno').Value := Trim(CDS_Sub.fieldbyname('Conno').AsString);
          FieldByName('FactoryName').Value := Trim(CDS_Sub.fieldbyname('FactoryName').AsString);
          FieldByName('FactoryNo').Value := Trim(CDS_Sub.fieldbyname('FactoryNo').AsString);
          FieldByName('BatchNo').Value := Trim(CDS_Sub.fieldbyname('BatchNo').AsString);
          FieldByName('FromFactoryNo').Value := Trim(CDS_Sub.fieldbyname('FromFactoryNo').AsString);
          FieldByName('ToFactoryNo').Value := Trim(CDS_Sub.fieldbyname('ToFactoryNo').AsString);
          FieldByName('CPID').Value := Trim(CDS_Sub.fieldbyname('CPID').AsString);
          FieldByName('CRFlag').Value := '入库';
          FieldByName('CRQtyFlag').Value := 1;
          RTSetSaveDataCDS(ADOQueryCmd, Tv1, CDS_Sub, 'CK_SXPB_CR', 2);
          if Trim(FFY_ID) = '' then
          begin
            FieldByName('Filler').Value := Trim(DName);
            FieldByName('FillCode').Value := Trim(DCode);
            FieldByName('Filltime').Value := SGetServerDate(ADOQueryTemp);
          end
          else
          begin
            FieldByName('Editer').Value := Trim(DName);
            FieldByName('EditCode').Value := Trim(DCode);
            FieldByName('EditTime').Value := SGetServerDate(ADOQueryTemp);
          end;
          fieldbyname('Price').Value := CDS_Sub.fieldbyname('Price').AsFloat;
          Post;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update CK_Yarn_CR Set MXKCQty=(select sum(isnull(Qty,0)*CRQtyFlag) from CK_Yarn_CR A where A.KCID=CK_Yarn_CR.Y_ID )');
          sql.Add(',MXKCPiQty=(select sum(isnull(PiQty,0)*CRQtyFlag) from CK_Yarn_CR A where A.KCID=CK_Yarn_CR.Y_ID )');
          sql.Add(' where Y_ID=''' + Trim(Maxno) + '''');
          ExecSQL;
        end;
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from CK_Yarn_CR where Y_ID=''' + Trim(Maxno) + '''');
          Open;
        end;
        if ADOQueryTemp.FieldByName('MXKCQty').Value < 0 then
        begin
          CDS_Sub.DisableControls;
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('入库数小于出库数不能保存!', '提示', 0);
          Exit;
        end;
        Edit;
        FieldByName('Y_ID').Value := Trim(Maxno);
        //FieldByName('CRID').Value:=Trim(FCRID);
        Post;
        //prindata();
        Next;
      end;
    end;
    CDS_Sub.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    Result := True;
  except
    Result := False;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存异常!', '提示', 0);
  end;
end;

procedure TfrmShaRKInPut.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('纱线入库登记JTS', Tv1, '纱线仓库');
  Close;
end;

procedure TfrmShaRKInPut.FormShow(Sender: TObject);
var
  fsj: string;
begin
  ReadCxGrid('纱线入库登记JTS', Tv1, '纱线仓库');
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(' select A.* ');
    sql.Add(' from CK_Yarn_CR A');
    sql.Add(' where Y_ID=''' + Trim(FBCId) + '''');
    Open;
  end;

  SCreateCDS20(ADOQueryTemp, CDS_Sub);
  SInitCDSData20(ADOQueryTemp, CDS_Sub);
  if CDS_Sub.IsEmpty then
    Exit;

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from CK_Yarn_CR where KCID=''' + Trim(CDS_Sub.fieldbyname('Y_ID').AsString) + '''');
    sql.Add(' and CRQtyFlag=-1');
    Open;
  end;

end;

procedure TfrmShaRKInPut.TBDelClick(Sender: TObject);
begin
  if CDS_Sub.IsEmpty then
    Exit;
  if Trim(CDS_Sub.fieldbyname('Y_ID').AsString) <> '' then
  begin
    if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
      Exit;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from CK_SXPB_CR where KCID=''' + Trim(CDS_Sub.fieldbyname('Y_ID').AsString) + '''');
      sql.Add(' and CRQtyFlag=-1');
      Open;
    end;
    if ADOQueryTemp.IsEmpty = False then
    begin
      Application.MessageBox('已有出库记录，不能删除!', '提示', 0);
      Exit;
    end;
    try
      ADOQueryCmd.Connection.BeginTrans;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add(' delete CK_SXPB_CR where Y_ID=''' + Trim(CDS_Sub.fieldbyname('Y_ID').AsString) + '''');
        ExecSQL;
      end;
      ADOQueryCmd.Connection.CommitTrans;
      CDS_Sub.Delete;
    except
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('删除异常!', '提示', 0);
    end;

  end
  else
  begin
    CDS_Sub.Delete;
  end;

end;

procedure TfrmShaRKInPut.TBSaveClick(Sender: TObject);
begin
  if CDS_Sub.IsEmpty then
    Exit;
  if CDS_Sub.Locate('BatchNo', null, []) = True then
  begin
    Application.MessageBox('批号不能为空!', '提示', 0);
    Exit;
  end;
  if CDS_Sub.Locate('BatchNo', '', []) = 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('Y_CodeName', null, []) = True then
  begin
    Application.MessageBox('品名不能为空!', '提示', 0);
    Exit;
  end;

  if CDS_Sub.Locate('FactoryName', 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('QtyUnit', null, []) = True then
  begin
    Application.MessageBox('数量单位不能为空!', '提示', 0);
    Exit;
  end;
  if SaveCKData() then
  begin
    Application.MessageBox('保存成功!', '提示', 0);
    //ModalResult:=1;
    Exit;
  end;
end;

procedure TfrmShaRKInPut.v1Column3PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'SXKuWei';
      flagname := '库位';
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('KuWei').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmShaRKInPut.v1Column14PropertiesButtonClick(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('FactoryNo').Value := Trim(CDS_HZ.fieldbyname('ATID').AsString);
          FieldByName('FactoryName').Value := Trim(CDS_HZ.fieldbyname('ZdyName').AsString);
          //Post;
        end;
      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;

end;

procedure TfrmShaRKInPut.v2Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin

  try
    frmJTSInfoSelList := TfrmJTSInfoSelList.Create(Application);
    with frmJTSInfoSelList do
    begin
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('FactoryName').Value := Trim(frmJTSInfoSelList.Order_Main.fieldbyname('FactoryName').AsString);
          FieldByName('Y_CodeName').Value := Trim(frmJTSInfoSelList.Order_Main.fieldbyname('Y_CodeName').AsString);
          FieldByName('Y_Spec').Value := Trim(frmJTSInfoSelList.Order_Main.fieldbyname('Y_Spec').AsString);
          FieldByName('BatchNo').Value := Trim(frmJTSInfoSelList.Order_Main.fieldbyname('BatchNo').AsString);
        end;
      end;
    end;
  finally
    frmJTSInfoSelList.Free;
  end;

end;

procedure TfrmShaRKInPut.v1Column5PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'SXKuWei';
      flagname := '库位';
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('KuWei').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmShaRKInPut.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(CDS_HZ.fieldbyname('Coid').AsString);
          FieldByName('ToFactoryName').Value := Trim(CDS_HZ.fieldbyname('coName').AsString);
          //Post;
        end;
      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;

end;

procedure TfrmShaRKInPut.v1Column16PropertiesButtonClick(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('FromFactoryNo').Value := Trim(CDS_HZ.fieldbyname('CoID').AsString);
          FieldByName('FromFactoryName').Value := Trim(CDS_HZ.fieldbyname('coName').AsString);
          //Post;
        end;
      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;

end;

procedure TfrmShaRKInPut.Tv1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  if CDS_Sub.IsEmpty then
    Exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from CK_Yarn_CR where KCID=''' + Trim(CDS_Sub.fieldbyname('Y_ID').AsString) + '''');
    sql.Add(' and CRQtyFlag=-1');
    Open;
  end;
  if ADOQueryTemp.IsEmpty = False then
  begin
    v1Column4.Options.Editing := False;
    v1Column8.Options.Editing := False;
    v1Column3.Options.Editing := False;
  end
  else
  begin
    v1Column4.Options.Editing := True;
    v1Column8.Options.Editing := True;
    v1Column3.Options.Editing := True;
  end;
end;

procedure TfrmShaRKInPut.v1Column10PropertiesButtonClick(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('FactoryNo').Value := Trim(CDS_HZ.fieldbyname('Coid').AsString);
          FieldByName('FactoryName').Value := Trim(CDS_HZ.fieldbyname('coName').AsString);
          //Post;
        end;
      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;
end;

procedure TfrmShaRKInPut.ToolButton1Click(Sender: TObject);
begin
  with Self.CDS_Sub do
  begin
    Append;
    FieldByName('CRTime').Value := Trim(FormatDateTime('yyyy-MM-dd', Now));
    FieldByName('PiQty').Value := 0;
    FieldByName('Qty').Value := 0;
    FieldByName('Price').Value := 0;
    FieldByName('QtyUnit').Value := 'Kg';
    FieldByName('Price').Value := 0;

  end;
end;

end.

