unit U_CiPinChaiJian;

interface

uses
  Windows, Messages, strutils, SysUtils, Variants, Classes, Graphics, Controls,
  Forms, Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, cxGridLevel, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxClasses, cxControls, cxGridCustomView,
  cxGrid, StdCtrls, cxContainer, cxTextEdit, cxCurrencyEdit, BtnEdit, ExtCtrls,
  ComCtrls, ToolWin, DBClient, ADODB, MovePanel, RM_Dataset, RM_System,
  RM_Common, RM_Class, RM_GridReport, cxLookAndFeels, cxLookAndFeelPainters,
  cxNavigator;

function CommOpen(fhandle: hwnd; sCommName: PAnsiChar; IntTime: integer; IsMessage: integer): integer; stdcall; external 'JCYData.DLL';

function CommClose(sCommName: PAnsiChar): integer; stdcall; external 'JCYData.DLL';

type
  TfrmCiPinChaiJian = class(TForm)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    Label1: TLabel;
    ADOQueryMain: TADOQuery;
    ADOQueryCmd: TADOQuery;
    CDS_FromJYID: TClientDataSet;
    CDS_ToJYID: TClientDataSet;
    DS_FromJYID: TDataSource;
    DS_ToJYID: TDataSource;
    SmNO: TEdit;
    ADOQueryTmp: TADOQuery;
    ADOQueryPrint: TADOQuery;
    RM2: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    Panel4: TPanel;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    VC_JYID: TcxGridDBColumn;
    v1P_Code: TcxGridDBColumn;
    v1P_Color: TcxGridDBColumn;
    v1Qty: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    V2Column1: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    V2Column2: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Panel2: TPanel;
    btnChaiPi: TButton;
    Button4: TButton;
    Label5: TLabel;
    Label9: TLabel;
    KJLen: TEdit;
    Tv1Column1: TcxGridDBColumn;
    Button1: TButton;
    Label2: TLabel;
    note: TEdit;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure SmNOKeyPress(Sender: TObject; var Key: Char);
    procedure FormShow(Sender: TObject);
    procedure Tv1DblClick(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure btnChaiPiClick(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    procedure InitGrid();
    procedure InitJYID();
    procedure InitCFGrid();
    procedure Printdata(fCRID: string); 
    { Private declarations }
  public
    iscom: boolean;
    { Public declarations }
  end;

var
  frmCiPinChaiJian: TfrmCiPinChaiJian;

implementation

uses
  U_DataLink, U_Fun, U_UserHelp, U_ZDYHelp;
{$R *.dfm}

procedure TfrmCiPinChaiJian.Printdata(fCRID: string);
var
  fPrintFile: string;
  mvalue: Double;
  Txt, fImagePath: string;
  Moudle: THandle;
  Makebar: TMakebar;
  Mixtext: TMixtext;
begin
  with ADOQueryPrint do
  begin
    Close;
    SQL.Clear;
    sql.Add(' EXEC P_JY_Print_Lab ');
    SQL.Add(' @JYID=''' + Trim(fCRID) + '''');
    Open;
  end;
  if ADOQueryPrint.IsEmpty then
  begin
    application.MessageBox('标签内容未找到！', '提示信息', MB_ICONERROR);

    exit;
  end;
  try
    Moudle := LoadLibrary('MakeQRBarcode.dll');
    @Makebar := GetProcAddress(Moudle, 'Make');
    @Mixtext := GetProcAddress(Moudle, 'MixText');
    Txt := Trim(ADOQueryPrint.fieldbyname('JYID').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;
  ExportFtErpFile(Trim(ADOQueryPrint.fieldbyname('LabName').AsString), ADOQueryTmp);
  if Trim(ADOQueryPrint.fieldbyname('LabName').AsString) <> '' then
  begin
    fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + Trim(ADOQueryPrint.fieldbyname('LabName').AsString) + '.rmf';
  end
  else
  begin
    ExportFtErpFile('通用坯布标签.rmf', ADOQueryTmp);
    fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\通用坯布标签.rmf';
  end;

  if FileExists(fPrintFile) then
  begin
    RMVariables['QRBARCODE'] := fImagePath;
    RM2.LoadFromFile(fPrintFile);
    Rm2.DefaultCopies := 1;
    RM2.PrintReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + fPrintFile), '提示', 0);
  end;

end;

procedure TfrmCiPinChaiJian.InitGrid();
begin
  with ADOQueryMain do
  begin
    Close;
    sql.Clear;
    SQL.Add('select * from JY_Cloth A');
    SQL.Add('where 1=2 ');
    Open;
  end;
  SCreateCDS20(ADOQueryMain, CDS_FromJYID);
  SCreateCDS20(ADOQueryMain, CDS_ToJYID);
end;

procedure TfrmCiPinChaiJian.InitCFGrid();
begin

  with ADOQueryMain do
  begin
    Close;
    sql.Clear;
    SQL.Add('select A.* from JY_Cloth A');
    SQL.Add('where A.JYID=' + quotedstr(trim(CDS_FromJYID.Fieldbyname('JYID').AsString)));
    Open;
    SCreateCDS20(ADOQueryMain, CDS_FromJYID);
    SInitCDSData20(ADOQueryMain, CDS_FromJYID);
  end;

  with ADOQueryTmp do
  begin
    Close;
    sql.Clear;
    SQL.Add('select A.* from JY_Cloth A');
    SQL.Add('where A.OrgJYID=' + quotedstr(trim(CDS_FromJYID.Fieldbyname('JYID').AsString)));
    SQL.Add('order by JYID');
    Open;
  end;
  SCreateCDS20(ADOQueryTmp, CDS_ToJYID);
  SInitCDSData20(ADOQueryTmp, CDS_ToJYID);
end;

procedure TfrmCiPinChaiJian.InitJYID();
var
  fP_No: string;
begin
  with ADOQueryMain do
  begin
    Close;
    sql.Clear;
    SQL.Add('select A.* from JY_Cloth A');
    SQL.Add('where A.JYID=' + quotedstr(trim(SmNO.text)));
    Open;
    if not IsEmpty then
    begin
      if Fieldbyname('JYCRType').AsString = '已出库' then
      begin
        application.MessageBox('此条码已出库！', '提示信息', MB_ICONERROR);
      end
      else
      begin
        SCreateCDS20(ADOQueryMain, CDS_FromJYID);

        SInitCDSData20(ADOQueryMain, CDS_FromJYID);
      end;
    end
    else
    begin
      application.MessageBox('此条码不存在！', '提示信息', MB_ICONERROR);
    end;
  end;
end;

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

end;

procedure TfrmCiPinChaiJian.TBCloseClick(Sender: TObject);
begin
  wRITECxGrid(self.Caption + tV1.Name, Tv1, '成品仓库');
  wRITECxGrid(self.Caption + TV2.Name, Tv2, '成品仓库');

  close;
end;

procedure TfrmCiPinChaiJian.SmNOKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin

    if not CDS_FromJYID.Locate('JYID', trim(smNo.Text), []) then
      initJYID();

    smno.Text := '';
    smno.SetFocus;

  end;
end;

procedure TfrmCiPinChaiJian.FormShow(Sender: TObject);
begin
  ReadCxGrid(self.Caption + tV1.Name, Tv1, '成品仓库');
  ReadCxGrid(self.Caption + TV2.Name, Tv2, '成品仓库');
  InitGrid();

  SmNO.SetFocus;
end;

procedure TfrmCiPinChaiJian.Tv1DblClick(Sender: TObject);
begin
  if CDS_FromJYID.IsEmpty then
    exit;
  CDS_FromJYID.Delete;
end;

procedure TfrmCiPinChaiJian.Button5Click(Sender: TObject);
begin
  if CDS_ToJYID.IsEmpty then
    exit;
  printdata(CDS_ToJYID.fieldbyname('JYID').AsString);
  SmNO.SetFocus;
end;

procedure TfrmCiPinChaiJian.btnChaiPiClick(Sender: TObject);
begin

  if CDS_FromJYID.RecordCount > 1 then
  begin
    application.MessageBox('被拆分的匹数不能大于一匹，请检查扫描到的匹数！', '提示信息', MB_ICONERROR);
    exit;
  end;
  if strtofloatdef(trim(KJLen.Text), 0) > CDS_FromJYID.FieldByName('jylen').AsFloat then
  begin
    application.MessageBox('拆分数量不能大于原数量！', '提示信息', MB_ICONERROR);
    exit;
  end;

  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('exec P_JY_ChaiPi ');
      sql.Add(' @OrgJYID=' + quotedstr(trim(CDS_FromJYID.FieldByName('JYID').AsString)));
      sql.Add(',@KJLen=' + KJLen.text);
      sql.Add(' ,@note=' + quotedstr(trim(note.Text)));
      open;
    end;
    if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
    begin
      application.MessageBox(PChar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)), '提示信息', 0);
      ADOQueryCmd.Connection.RollbackTrans;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    InitCFGrid();
//    CDS_ToJYID.Last;
//    Printdata(CDS_ToJYID.Fieldbyname('JYID').AsString);
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('数据保存失败！', '提示信息', 0);
  end;

  SmNO.SetFocus;
end;

procedure TfrmCiPinChaiJian.Button4Click(Sender: TObject);
begin
  if CDS_FromJYID.IsEmpty then
    exit;
  printdata(CDS_FromJYID.fieldbyname('JYID').AsString);
  SmNO.SetFocus;
end;

procedure TfrmCiPinChaiJian.FormDestroy(Sender: TObject);
begin
  frmCiPinChaiJian := nil;
end;

end.

