unit U_CKSMEdit;

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, cxCheckBox, RM_Common, RM_Class, RM_e_Xls, RM_GridReport,
  RM_System, RM_Dataset,MMSystem;

type
  TfrmCKSMEdit = class(TForm)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    PHYG: TBtnEditA;
    defstr2: TComboBox;
    Label16: TLabel;
    Label15: TLabel;
    packNo: TcxCurrencyEdit;
    Label14: TLabel;
    Label1: TLabel;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1MJID: TcxGridDBColumn;
    v1MJXH: TcxGridDBColumn;
    v1P_Code: TcxGridDBColumn;
    v1P_Color: TcxGridDBColumn;
    v1RollNum: TcxGridDBColumn;
    v1Qty: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    v1PHRollNum: TcxGridDBColumn;
    v1PHQty: TcxGridDBColumn;
    Panel2: TPanel;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    ADOQueryMain: TADOQuery;
    ADOQueryCmd: TADOQuery;
    CDSOrder: TClientDataSet;
    CDSMJID: TClientDataSet;
    DataSource1: TDataSource;
    DataSource2: TDataSource;
    SmNO: TEdit;
    ADOQueryTmp: TADOQuery;
    MovePanel1: TMovePanel;
    Label2: TLabel;
    Edit2: TEdit;
    Button4: TButton;
    V2Column1: TcxGridDBColumn;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    cxStyle2: TcxStyle;
    cxStyle_gridRow: TcxStyle;
    cxStyle_gridFoot: TcxStyle;
    cxStyle_gridHead: TcxStyle;
    cxStyle_gridGroupBox: TcxStyle;
    cxStyle_yellow: TcxStyle;
    cxStyle_Red: TcxStyle;
    cxStyle_fontBlack: TcxStyle;
    cxStyle_fontclFuchsia: TcxStyle;
    cxStyle_fontclPurple: TcxStyle;
    cxStyle_fontclGreen: TcxStyle;
    cxStyle_fontclBlue: TcxStyle;
    cxStyle_fontclTeal: TcxStyle;
    cxStyle_fontclOlive: TcxStyle;
    ToolButton1: TToolButton;
    V2Column2: TcxGridDBColumn;
    CheckBox1: TCheckBox;
    ToolButton2: TToolButton;
    RMGridReport1: TRMGridReport;
    RMXLSExport2: TRMXLSExport;
    ADOQueryPrint: TADOQuery;
    RMDB_Main: TRMDBDataSet;
    ADOQuery1: TADOQuery;
    v1Column3: TcxGridDBColumn;
    V2Column3: TcxGridDBColumn;
    V2Column4: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);

    procedure FormCreate(Sender: TObject);
    procedure SmNOKeyPress(Sender: TObject; var Key: Char);
    procedure FormShow(Sender: TObject);
    procedure TV2CustomDrawCell(Sender: TcxCustomGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
      var ADone: Boolean);
    procedure TBSaveClick(Sender: TObject);
    procedure PHYGBtnClick(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  private
    procedure InitGrid();
    procedure InitOrder();
    procedure InitMJID();
    procedure InitMJID10();
    function  SavePH():Boolean;
    { Private declarations }
  public
    fckName,fSyrName:string;
    { Public declarations }
  end;

var
  frmCKSMEdit: TfrmCKSMEdit;

implementation
uses
  U_DataLink,U_Fun, U_UserHelp, U_KCEdit;
{$R *.dfm}
procedure TfrmCKSMEdit.InitMJID10();
var
  fP_No:string;
  i:integer;
begin
  MovePanel1.Visible:=false;

  with ADOQueryTmp do
  begin
    Close;
    sql.Clear;
    SQL.Add('select MJID from WFB_MJJY A');
    SQL.Add('where A.MJID='+quotedstr(trim(SmNO.text)));
    SQL.Add('and MJstr2=''未入库''');
    Open;
    IF not IsEmpty then
    begin
      CDSMJID.Append;
      CDSMJID.FieldByName('SDefNote').Value:='此条码未入库';
      CDSMJID.FieldByName('MJID').Value:=trim(SmNO.Text);
      CDSMJID.Post;
      IF FileExists(PChar(ExtractFilePath(Application.ExeName)+'wav\此条码未入库.wav')) then
      PlaySound(PChar(ExtractFilePath(Application.ExeName)+'wav\此条码未入库.wav'),0, SND_ASYNC);
      Exit;
    end;
  end;

  with ADOQueryTmp do
  begin
    Close;
    sql.Clear;
    SQL.Add('select MJID from CK_BanCP_CR A');
    SQL.Add('where A.MJID='+quotedstr(trim(SmNO.text)));
    SQL.Add('and CRFlag=''出库''');
    Open;
    IF not IsEmpty then
    begin
      CDSMJID.Append;
      CDSMJID.FieldByName('SDefNote').Value:='此条码已出库';
      CDSMJID.FieldByName('MJID').Value:=trim(SmNO.Text);
      CDSMJID.Post;
      IF FileExists(PChar(ExtractFilePath(Application.ExeName)+'wav\此条码已出库.wav')) then
      PlaySound(PChar(ExtractFilePath(Application.ExeName)+'wav\此条码已出库.wav'),0, SND_ASYNC);
      Exit;
    end;
  end;

  with ADOQueryMain do
  begin
    Close;
    sql.Clear;
    SQL.Add('select A.*,B.mainID,B.subID,B.BCKZ,B.BCFK from CK_BanCP_KC A');
    sql.add('left join  CK_BanCP_CR B on B.BCID=A.BCID and B.CRFlag=''入库'' ');
    SQL.Add('where A.MJID='+quotedstr(trim(SmNO.text)));
    sql.Add('and isnull(A.KCStatus,''0'')=''0''  ');
    SQL.Add('and A.ckName='''+trim(fckName)+''' ');
    sql.Add('and KCQty>0  ');
    Open;
  end;
  IF ADOQueryMain.IsEmpty then
  begin
    CDSMJID.Append;
    CDSMJID.FieldByName('SDefNote').Value:='此条码不存在或已经出库';
    CDSMJID.FieldByName('MJID').Value:=trim(SmNO.Text);
    CDSMJID.Post;
    IF FileExists(PChar(ExtractFilePath(Application.ExeName)+'wav\扫描错误.wav')) then
      PlaySound(PChar(ExtractFilePath(Application.ExeName)+'wav\扫描错误.wav'),0, SND_ASYNC);
    exit;
  end;
  with ADOQueryTmp do
  begin
    Close;
    sql.Clear;
    SQL.Add('select B.mainID,B.subID from CK_BanCP_KC A');
    sql.Add('inner join CK_BanCP_CR A1 on A1.BCID=A.BCID ');
    sql.Add('inner join JYorder_Sub B on B.PRTCodeName=A.C_CodeName and B.PRtColor=A.C_Color  and cast(B.PRTKZ as int)=cast(A1.BCKZ AS int) ');
    SQL.Add('where A.MJID='+quotedstr(trim(SmNO.text)));
    SQL.Add('and B.mainID='+quotedstr(trim(CDSOrder.Fieldbyname('mainID').AsString)));
 //   SQL.ADD('and isnull(B.PRTKZ,'''')<>'''' ');
    Open;
  end;
 { IF  ADOQueryTmp.IsEmpty then
  begin
    with ADOQueryTmp do
    begin
    Close;
    sql.Clear;
    SQL.Add('select B.mainID,B.subID from CK_BanCP_KC A');
    sql.Add('inner join JYorder_Sub B on B.PRTCodeName=A.C_CodeName and B.PRtColor=A.C_Color');
    SQL.Add('where A.MJID='+quotedstr(trim(SmNO.text)));
    SQL.Add('and B.mainID='+quotedstr(trim(CDSOrder.Fieldbyname('mainID').AsString)));
    SQL.ADD('and isnull(B.PRTKZ,'''')='''' ');
    Open;
    end;
  end;
  }
  IF ADOQueryTmp.IsEmpty then
  begin
    CDSMJID.Append;
    CDSMJID.FieldByName('SDefNote').Value:='品名和颜色克重不对应';
    CDSMJID.FieldByName('MJID').Value:=trim(SmNO.Text);
    CDSMJID.Post;
    IF FileExists(PChar(ExtractFilePath(Application.ExeName)+'wav\品名颜色不对应.wav')) then
    PlaySound(PChar(ExtractFilePath(Application.ExeName)+'wav\品名颜色不对应.wav'),0, SND_ASYNC);
    exit;
  end;
  i:=0;
  while not ADOQueryTmp.eof do
  begin
    if CDSOrder.Locate('subID',trim(ADOQueryTmp.fieldbyname('subID').AsString),[])  then
    begin
      IF (CDSOrder.fieldbyName('PRTRollNum').AsInteger=CDSOrder.FieldByName('PHRollNum').AsInteger)
         and (CDSOrder.fieldbyName('PRTRollNum').AsInteger>0) then
      begin
      end
      else
      begin
        i:=1;
        fP_No:=CDSOrder.fieldbyname('P_NO').AsString;
        CDSMJID.Append;
        CDSMJID.FieldByName('SDefNote').Value:='扫描成功';
        CDSMJID.FieldByName('MJID').Value:=trim(ADOQueryMain.Fieldbyname('MJID').AsString);
        CDSMJID.FieldByName('CRID').Value:=trim(ADOQueryMain.Fieldbyname('CRID').AsString);
        CDSMJID.FieldByName('C_CodeName').Value:=trim(ADOQueryMain.Fieldbyname('C_CodeName').AsString);
        CDSMJID.FieldByName('C_Color').Value:=trim(ADOQueryMain.Fieldbyname('C_Color').AsString);
        CDSMJID.FieldByName('CPType').Value:=trim(ADOQueryMain.Fieldbyname('CPType').AsString);
        CDSMJID.FieldByName('RollNum').Value:=ADOQueryMain.Fieldbyname('kcRollNum').AsFloat;
        CDSMJID.FieldByName('Qty').Value:=ADOQueryMain.Fieldbyname('kcQty').AsFloat;
        CDSMJID.FieldByName('QtyUnit').Value:=trim(ADOQueryMain.Fieldbyname('kcQtyUnit').AsString);
        CDSMJID.FieldByName('mainID').Value:=trim(ADOQueryTmp.Fieldbyname('mainID').AsString);
        CDSMJID.FieldByName('subID').Value:=trim(ADOQueryTmp.Fieldbyname('subID').AsString);
        CDSMJID.FieldByName('BCFK').Value:=ADOQueryMain.Fieldbyname('BCFK').AsFloat;
        CDSMJID.FieldByName('BCKZ').Value:=ADOQueryMain.Fieldbyname('BCKZ').AsFloat;
        CDSMJID.FieldByName('P_NO').Value:=trim(fP_No);
        CDSMJID.FieldByName('PmainID').Value:=trim(ADOQueryMain.Fieldbyname('mainID').AsString);
        CDSMJID.FieldByName('PsubID').Value:=trim(ADOQueryMain.Fieldbyname('subID').AsString);
        CDSMJID.FieldByName('BCgangNO').Value:=trim(ADOQueryMain.Fieldbyname('BCgangNO').AsString);
          //  CDSMJID.FieldByName('conNo').Value:=trim(ADOQueryMain.Fieldbyname('conNo').AsString);
        CDSMJID.Post;
        IF FileExists(PChar(ExtractFilePath(Application.ExeName)+'wav\正确.wav')) then
            PlaySound(PChar(ExtractFilePath(Application.ExeName)+'wav\正确.wav'),0, SND_ASYNC);
        CDSOrder.Edit;
        CDSOrder.fieldbyName('PHRollNum').Value:=CDSOrder.fieldbyName('PHRollNum').AsInteger+1;
        CDSOrder.fieldbyName('PHQty').Value:=CDSOrder.fieldbyName('PHQty').AsFloat+ADOQueryMain.Fieldbyname('kcQty').AsFloat;
        CDSOrder.Post;
        IF tv1.DataController.Summary.FooterSummaryValues[0]<>0 then
        begin
          IF tv1.DataController.Summary.FooterSummaryValues[0]= tv2.DataController.Summary.FooterSummaryValues[0] then
          begin
            TBSave.Click;
            exit;
          end;
        end;
        exit;
      end;
    end;
    ADOQueryTmp.Next;
  end;

  IF i=0 then
  begin
    IF FileExists(PChar(ExtractFilePath(Application.ExeName)+'wav\超出下单匹数.wav')) then
    PlaySound(PChar(ExtractFilePath(Application.ExeName)+'wav\超出下单匹数.wav'),0, SND_ASYNC);
  end;
end;

function TfrmCKSMEdit.SavePH():Boolean;
var
  phID:string;
  P_ID:integer;
begin

  Result:=false;
  ADOQueryCmd.Connection.BeginTrans;
  P_ID:=1;
  try
    with CDSMJID do
    begin
      DisableControls;
      first;
      while not eof do
      begin
        if trim(CDSMJID.fieldbyname('SDefNote').AsString)='扫描成功' then
        begin
        if GetLSNo(ADOQueryTmp,phID,'PH','CK_BanCP_PH',4,1)=False then
        begin
          Application.MessageBox('取配货单号失败！','提示',0);
          Exit;
        end;
        with ADOQueryCmd do
        begin
          close;
          sql.Clear;
          sql.Add('select * from CK_BanCP_PH ');
          sql.Add('where 1=2 ');
          open;
        end;
        ADOQueryCmd.Append;
        ADOQueryCmd.FieldByName('phID').Value:=trim(phID);
        ADOQueryCmd.FieldByName('p_No').Value:=trim(fieldbyname('p_No').AsString);
        ADOQueryCmd.FieldByName('p_ID').Value:=P_ID;
//        ADOQueryCmd.FieldByName('conNo').Value:=trim(fieldbyname('conNo').AsString);
        ADOQueryCmd.FieldByName('MainID').Value:=trim(fieldbyname('MainID').AsString);
        ADOQueryCmd.FieldByName('SubID').Value:=trim(fieldbyname('SubID').AsString);
        ADOQueryCmd.FieldByName('PMainID').Value:=trim(fieldbyname('PMainID').AsString);
        ADOQueryCmd.FieldByName('PSubID').Value:=trim(fieldbyname('PSubID').AsString);
        ADOQueryCmd.FieldByName('mjID').Value:=trim(fieldbyname('mjID').AsString);
        ADOQueryCmd.FieldByName('CRID').Value:=trim(fieldbyname('CRID').AsString);
        ADOQueryCmd.FieldByName('qtyunit').Value:=trim(fieldbyname('qtyunit').AsString);
        ADOQueryCmd.FieldByName('defstr2').Value:=trim(defstr2.Text);
        ADOQueryCmd.FieldByName('packNo').Value:=trim(packNO.Text);
        ADOQueryCmd.FieldByName('qty').Value:=fieldbyname('qty').AsFloat;
        ADOQueryCmd.FieldByName('Rollnum').Value:=fieldbyname('Rollnum').AsFloat;
        ADOQueryCmd.FieldByName('Filler').Value:=trim(DName);
        ADOQueryCmd.FieldByName('FillTime').Value:=SGetServerDateTime(ADOQueryTmp);
        ADOQueryCmd.FieldByName('BCGangNo').Value:=trim(fieldbyname('BCgangNO').AsString);
        ADOQueryCmd.FieldByName('PHYG').Value:=trim(PHYG.Text);
        ADOQueryCmd.FieldByName('PHType').Value:='扫描配货';
        ADOQueryCmd.Post;

        with ADOQueryCmd do
        begin
          close;
          sql.Clear;
          sql.Add('select * from CK_BanCP_KC ');
          sql.Add('where mjid='+quotedstr(trim(CDSMJID.fieldbyname('mjid').AsString)));
          sql.Add('and CRID='+quotedstr(trim(CDSMJID.fieldbyname('CRID').AsString)));
          open;
          edit;
          fieldbyname('kcstatus').Value:='1';
          post;
        end;
        with ADOQueryCmd do
        begin
          close;
          sql.Clear;
          sql.Add('select * from JYOrder_sub ');
          sql.Add('where mainID='+quotedstr(trim(CDSMJID.fieldbyname('mainID').AsString)));
          sql.Add('and subID='+quotedstr(trim(CDSMJID.fieldbyname('subID').AsString)));
          open;
          edit;
          fieldbyname('substatus').Value:='2';
          post;
        end;
        P_ID:=P_ID+1;
        end;
        next;
      end;
      first;
      EnableControls;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result:=true;
  except
    ADOQueryCmd.Connection.RollbackTrans;
  end;
end;

procedure TfrmCKSMEdit.InitGrid();
begin
  with ADOQueryMain do
  begin
    Close;
    sql.Clear;
    SQL.Add('select A.*,B.CustomerNoName,');
    sql.Add('PHRollNum=(select count(MJID) from CK_BanCP_PH X where X.MainId=A.mainID and X.subID=A.subID),');
    sql.Add('PHQty=(select sum(Qty) from CK_BanCP_PH X where X.MainId=A.mainID and X.subID=A.subID)');
    sql.Add('from JYOrder_sub A');
    SQL.Add('inner join JYOrder_main B on B.mainID=A.mainID');
    SQL.Add('where 1=2 ');
    Open;
  end;
  SCreateCDS20(ADOQueryMain,CDSOrder);
  SInitCDSData20(ADOQueryMain,CDSOrder);

  with ADOQueryMain do
  begin
    Close;
    sql.Clear;
    SQL.Add('select A.*,B.PmainID,PSubID,P_NO from CK_BanCP_CR A ');
    SQL.Add('inner join CK_BanCP_PH B on B.PHID=A.PHID  ');
    SQL.Add('where 1=2 ');
    Open;
  end;
  SCreateCDS20(ADOQueryMain,CDSMJID);
  SInitCDSData20(ADOQueryMain,CDSMJID);


end;
procedure TfrmCKSMEdit.InitOrder();
var
  fP_No:string;
begin
  MovePanel1.Visible:=false;
  CDSMJID.EmptyDataSet;
  with ADOQueryMain do
  begin
    Close;
    sql.Clear;
    SQL.Add(' select A.*,B.CustomerNoName,');
    sql.Add(' PHRollNum=(select count(MJID) from CK_BanCP_PH X where X.MainId=A.mainID and X.subID=A.subID),');
    sql.Add(' PHQty=(select sum(Qty) from CK_BanCP_PH X where X.MainId=A.mainID and X.subID=A.subID),');
    sql.Add(' P_No=(select top 1 P_No from CK_BanCP_PH X where X.MainId=A.mainID and X.subID=A.subID)');
    SQL.Add(' from JYOrder_sub A');
    SQL.Add(' inner join JYOrder_main B on B.mainID=A.mainID and isnull(B.status,''0'')>''0'' ');
    SQL.Add(' where A.mainID='+quotedstr(trim(SmNO.text)));
 //   SQL.Add(' and B.SYRName='''+trim(fSYRName)+''' ');
    SQL.Add(' and B.CKName='''+trim(fckName)+''' ');
    SQL.Add(' and isnull(A.substatus,''0'')<=''2'' and A.PrtRollNum<>0 ');
  //  sql.Add(' and not exists(select subID from CK_BanCP_PH X where X.subID=A.subID)');
    Open;
  end;
  IF ADOQueryMain.IsEmpty then
  begin
    MovePanel1.Visible:=true;
    Edit2.text:=SmNO.Text;
    Label2.Caption:='订单号未审核或者不存在或已发货！';
    IF FileExists(PChar(ExtractFilePath(Application.ExeName)+'wav\订单不存在.wav')) then
    PlaySound(PChar(ExtractFilePath(Application.ExeName)+'wav\订单不存在.wav'),0, SND_ASYNC);
    exit;
  end
  else
  begin
  SCreateCDS20(ADOQueryMain,CDSOrder);
  SInitCDSData20(ADOQueryMain,CDSOrder);
  with CDSOrder do
  begin
    DisableControls;
    first;
    while not eof do
    begin
      if GetLSNo(ADOQueryTmp,fP_No,'PN','CK_BanCP_PH',4,1) then
      begin
        edit;
        fieldbyname('P_No').Value:=trim(fP_No);
        post;
      end
      else
      begin
        MovePanel1.Visible:=true;
        Edit2.Enabled:=false;
        Label2.Caption:='生成编号错误！';
        EnableControls;
        exit;
      end;
      next;
    end;
    first;
    EnableControls;
  end;
  end;

end;
procedure TfrmCKSMEdit.InitMJID();
var
  fP_No:string;
begin
  MovePanel1.Visible:=false;

  with ADOQueryTmp do
  begin
    Close;
    sql.Clear;
    SQL.Add('select MJID from WFB_MJJY A');
    SQL.Add('where A.MJID='+quotedstr(trim(SmNO.text)));
    SQL.Add('and MJstr2=''未入库''');
    Open;
    IF not IsEmpty then
    begin
      CDSMJID.Append;
      CDSMJID.FieldByName('SDefNote').Value:='此条码未入库';
      CDSMJID.FieldByName('MJID').Value:=trim(SmNO.Text);
      CDSMJID.Post;
      IF FileExists(PChar(ExtractFilePath(Application.ExeName)+'wav\此条码未入库.wav')) then
      PlaySound(PChar(ExtractFilePath(Application.ExeName)+'wav\此条码未入库.wav'),0, SND_ASYNC);
      Exit;
    end;
  end;
  with ADOQueryTmp do
  begin
    Close;
    sql.Clear;
    SQL.Add('select MJID from CK_BanCP_CR A');
    SQL.Add('where A.MJID='+quotedstr(trim(SmNO.text)));
    SQL.Add('and CRFlag=''出库''');
    Open;
    IF not IsEmpty then
    begin
      CDSMJID.Append;
      CDSMJID.FieldByName('SDefNote').Value:='此条码已出库';
      CDSMJID.FieldByName('MJID').Value:=trim(SmNO.Text);
      CDSMJID.Post;
      IF FileExists(PChar(ExtractFilePath(Application.ExeName)+'wav\此条码已出库.wav')) then
      PlaySound(PChar(ExtractFilePath(Application.ExeName)+'wav\此条码已出库.wav'),0, SND_ASYNC);
      Exit;
    end;
  end;

  with ADOQueryMain do
  begin
    Close;
    sql.Clear;
    SQL.Add('select A.*,B.mainID,B.subID from CK_BanCP_KC A');
    sql.add('left join  CK_BanCP_CR B on B.BCID=A.BCID and B.CRFlag=''入库'' ');
    SQL.Add('where A.MJID='+quotedstr(trim(SmNO.text)));
    sql.Add('and isnull(A.KCStatus,''0'')=''0''  ');
    SQL.Add('and A.ckName='''+trim(fckName)+''' ');
    sql.Add('and KCQty>0  ');
    Open;
    if not IsEmpty then
    begin
        with ADOQueryTmp do
        begin
          Close;
          sql.Clear;
          SQL.Add('select B.mainID,B.subID from CK_BanCP_KC A');
          sql.Add('inner join JYorder_Sub B on B.PRTCodeName=A.C_CodeName and B.PRtColor=A.C_Color');
          SQL.Add('where A.MJID='+quotedstr(trim(SmNO.text)));
          SQL.Add('and B.mainID='+quotedstr(trim(CDSOrder.Fieldbyname('mainID').AsString)));
          Open;
          IF ADOQueryTmp.IsEmpty then
          begin
            CDSMJID.Append;
            CDSMJID.FieldByName('SDefNote').Value:='品名和颜色不对应';
            CDSMJID.FieldByName('MJID').Value:=trim(SmNO.Text);
            CDSMJID.Post;
            IF FileExists(PChar(ExtractFilePath(Application.ExeName)+'wav\品名颜色不对应.wav')) then
             PlaySound(PChar(ExtractFilePath(Application.ExeName)+'wav\品名颜色不对应.wav'),0, SND_ASYNC);
          end
          else
          begin
            if CDSOrder.Locate('subID',trim(ADOQueryTmp.fieldbyname('subID').AsString),[])  then
            begin
              fP_No:=CDSOrder.fieldbyname('P_NO').AsString;
            end;
            IF CDSOrder.fieldbyName('PRTRollNum').AsInteger=CDSOrder.FieldByName('PHRollNum').AsInteger then
            begin
              IF FileExists(PChar(ExtractFilePath(Application.ExeName)+'wav\超出下单匹数.wav')) then
              PlaySound(PChar(ExtractFilePath(Application.ExeName)+'wav\超出下单匹数.wav'),0, SND_ASYNC);
            end
            else
            begin
            if CDSMJID.Locate('MJID',trim(ADOQueryMain.fieldbyname('MJID').AsString),[]) then
            CDSMJID.Edit
              else
            CDSMJID.Append;
            CDSMJID.FieldByName('SDefNote').Value:='扫描成功';
            CDSMJID.FieldByName('MJID').Value:=trim(ADOQueryMain.Fieldbyname('MJID').AsString);
            CDSMJID.FieldByName('CRID').Value:=trim(ADOQueryMain.Fieldbyname('CRID').AsString);
            CDSMJID.FieldByName('C_CodeName').Value:=trim(ADOQueryMain.Fieldbyname('C_CodeName').AsString);
            CDSMJID.FieldByName('C_Color').Value:=trim(ADOQueryMain.Fieldbyname('C_Color').AsString);
            CDSMJID.FieldByName('CPType').Value:=trim(ADOQueryMain.Fieldbyname('CPType').AsString);
            CDSMJID.FieldByName('RollNum').Value:=ADOQueryMain.Fieldbyname('kcRollNum').AsFloat;
            CDSMJID.FieldByName('Qty').Value:=ADOQueryMain.Fieldbyname('kcQty').AsFloat;
            CDSMJID.FieldByName('QtyUnit').Value:=trim(ADOQueryMain.Fieldbyname('kcQtyUnit').AsString);
            CDSMJID.FieldByName('mainID').Value:=trim(ADOQueryTmp.Fieldbyname('mainID').AsString);
            CDSMJID.FieldByName('subID').Value:=trim(ADOQueryTmp.Fieldbyname('subID').AsString);
            CDSMJID.FieldByName('P_NO').Value:=trim(fP_No);
            CDSMJID.FieldByName('PmainID').Value:=trim(ADOQueryMain.Fieldbyname('mainID').AsString);
            CDSMJID.FieldByName('PsubID').Value:=trim(ADOQueryMain.Fieldbyname('subID').AsString);
            CDSMJID.FieldByName('BCgangNO').Value:=trim(ADOQueryMain.Fieldbyname('BCgangNO').AsString);
          //  CDSMJID.FieldByName('conNo').Value:=trim(ADOQueryMain.Fieldbyname('conNo').AsString);
            CDSMJID.Post;
            IF FileExists(PChar(ExtractFilePath(Application.ExeName)+'wav\正确.wav')) then
            PlaySound(PChar(ExtractFilePath(Application.ExeName)+'wav\正确.wav'),0, SND_ASYNC);
            CDSOrder.Edit;
            CDSOrder.fieldbyName('PHRollNum').Value:=CDSOrder.fieldbyName('PHRollNum').AsInteger+1;
            CDSOrder.fieldbyName('PHQty').Value:=CDSOrder.fieldbyName('PHQty').AsFloat+ADOQueryMain.Fieldbyname('kcQty').AsFloat;
            CDSOrder.Post;
            IF tv1.DataController.Summary.FooterSummaryValues[0]<>0 then
            begin
              IF tv1.DataController.Summary.FooterSummaryValues[0]= tv2.DataController.Summary.FooterSummaryValues[0] then
              begin
                TBSave.Click;
                exit;
              end;
            end;

            end;

          end;
        end;

    end
    else
    begin
      CDSMJID.Append;
      CDSMJID.FieldByName('SDefNote').Value:='此条码不存在或已经出库';
      CDSMJID.FieldByName('MJID').Value:=trim(SmNO.Text);
      CDSMJID.Post;
      IF FileExists(PChar(ExtractFilePath(Application.ExeName)+'wav\扫描错误.wav')) then
      PlaySound(PChar(ExtractFilePath(Application.ExeName)+'wav\扫描错误.wav'),0, SND_ASYNC);
    end;
  end;
 { IF trim(CDSMJID.FieldByName('SDefNote').AsString)<>'扫描成功' then
  begin
    MovePanel1.Visible:=true;
    Edit2.Text:=SmNO.Text;
    Label2.Caption:=trim(CDSMJID.FieldByName('SDefNote').AsString);
  end;}
  
end;

procedure TfrmCKSMEdit.FormDestroy(Sender: TObject);
begin
  wRITECxGrid(self.Caption+tV1.Name,Tv1,'成品仓库');
  wRITECxGrid(self.Caption+TV2.Name+'2',Tv2,'成品仓库');
  frmCKSMEdit:=nil;
end;

procedure TfrmCKSMEdit.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  Action:=cafree;
end;

procedure TfrmCKSMEdit.TBCloseClick(Sender: TObject);
begin
  close;
end;

procedure TfrmCKSMEdit.FormCreate(Sender: TObject);
begin
  Panel2.Align:=alClient;
end;

procedure TfrmCKSMEdit.SmNOKeyPress(Sender: TObject; var Key: Char);
begin
  IF key=#13 then
  begin
    MovePanel1.Visible:=false;
    if trim(smno.Text)='' then exit;
    IF uppercase(leftBstr(trim(smno.Text),2))='JM' then
    begin
      IF not CDSMJID.IsEmpty then
      begin
        IF FileExists(PChar(ExtractFilePath(Application.ExeName)+'wav\数据未保存.wav')) then
        PlaySound(PChar(ExtractFilePath(Application.ExeName)+'wav\数据未保存.wav'),0, SND_ASYNC);
        exit;
      end;
      inItorder();      
    end
    else
    begin
      IF CDSOrder.IsEmpty then
      begin
        MovePanel1.Visible:=true;
        Edit2.Text:=SmNO.Text;
        Label2.Caption:='订单不能为空！';
        smno.Text:='';
        IF FileExists(PChar(ExtractFilePath(Application.ExeName)+'wav\订单不能为空.wav')) then
        PlaySound(PChar(ExtractFilePath(Application.ExeName)+'wav\订单不能为空.wav'),0, SND_ASYNC);
        exit;
      end;
      if not CDSMJID.Locate('MJID',trim(smNo.Text),[]) then
      begin
        initMjid10();
      end
      else
      begin
        IF FileExists(PChar(ExtractFilePath(Application.ExeName)+'wav\重复扫描.wav')) then
        PlaySound(PChar(ExtractFilePath(Application.ExeName)+'wav\重复扫描.wav'),0, SND_ASYNC);
      end;
    end;
    smno.Text:='';
    smno.SetFocus;
  end;
end;

procedure TfrmCKSMEdit.FormShow(Sender: TObject);
begin
  ReadCxGrid(self.Caption+tV1.Name,Tv1,'成品仓库');
  ReadCxGrid(self.Caption+TV2.Name+'2',Tv2,'成品仓库');
  MovePanel1.Left:=(Width-MovePanel1.Width) div 2;
  MovePanel1.top:=(Height-MovePanel1.Height-200) div 2;
  InitGrid();
end;

procedure TfrmCKSMEdit.TV2CustomDrawCell(Sender: TcxCustomGridTableView;
  ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
  var ADone: Boolean);
var
  i:integer;
begin
  i:=tv2.GetColumnByFieldName('SDefNote').Index;
  if (AViewInfo.GridRecord.Values[i]<>'扫描成功')  then
  ACanvas.Brush.Color:=clred;
end;

procedure TfrmCKSMEdit.TBSaveClick(Sender: TObject);
begin
  IF CDSOrder.IsEmpty then exit;
  if CDSMJID.IsEmpty then exit;

 {  IF tv1.DataController.Summary.FooterSummaryValues[0]<>0 then
  begin
    IF tv1.DataController.Summary.FooterSummaryValues[0]<> tv2.DataController.Summary.FooterSummaryValues[0] then
    begin
      application.MessageBox('配货匹数不相同，不能保存数据！','提示信息',MB_ICONERROR);
      exit;
    end;
  end;
 IF tv1.DataController.Summary.FooterSummaryValues[1]<>0 then
  begin
    IF tv1.DataController.Summary.FooterSummaryValues[1]<> tv2.DataController.Summary.FooterSummaryValues[1] then
    begin
     // if Application.MessageBox('配货数量不相同，是否保存？','提示',32+4)<>IDYES then Exit;
      application.MessageBox('配货数量不相同，不能保存数据！','提示信息',MB_ICONERROR);
      exit;
    end;
  end;
 }
  try
  //  ADOQueryCmd.Connection.BeginTrans;
    IF not savePh() then
    begin
      IF FileExists(PChar(ExtractFilePath(Application.ExeName)+'wav\保存失败.wav')) then
      PlaySound(PChar(ExtractFilePath(Application.ExeName)+'wav\保存失败.wav'),0, SND_ASYNC);
      exit;
    end
    else
    begin
      IF FileExists(PChar(ExtractFilePath(Application.ExeName)+'wav\保存成功.wav')) then
      PlaySound(PChar(ExtractFilePath(Application.ExeName)+'wav\保存成功.wav'),0, SND_ASYNC);
      initGrid();
      exit;
    end;
  //  ADOQueryCmd.Connection.CommitTrans;
  except
   // ADOQueryCmd.Connection.RollbackTrans;
  end;
end;

procedure TfrmCKSMEdit.PHYGBtnClick(Sender: TObject);
begin
  frmUserHelp:=TfrmUserHelp.Create(self);
  with frmUserHelp do
  begin
    if showmodal=1 then
    begin
      PHYG.Text:=trim(ADOQueryHelp.fieldbyname('userName').AsString);
    end;
    free;
  end;
end;

procedure TfrmCKSMEdit.Button4Click(Sender: TObject);
begin
  MovePanel1.Visible:=false;  
end;

procedure TfrmCKSMEdit.ToolButton1Click(Sender: TObject);
var
  kcQty,num1:double;
  i:integer;
  isEdit:boolean;
begin
  if not CDSMJID.Active then exit;
  if CDSMJID.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  isEdit:=false;

  frmKCEdit:=TfrmKCEdit.create(self);
  with frmKCEdit do
  begin
    if showmodal=1 then
    begin
      num1:=strtofloatdef(cxCurrencyEdit2.Text,0);
      i:=RadioGroup1.ItemIndex;
      isEdit:=true;
    end;
    free;
  end;

  IF Isedit=false then exit;

  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDSMJID do
    begin
      while CDSMJID.Locate('SSel',True,[]) do
      begin
        IF trim(Fieldbyname('SDefNote').AsString)='扫描成功' then
        begin
          if i=0 then
          kcQty:=CDSMJID.fieldbyname('Qty').AsFloat + num1 ;
          if i=1 then
          kcQty:=CDSMJID.fieldbyname('Qty').AsFloat - num1 ;
          if i=2 then
          kcQty:=strtofloat(format('%.1f',[CDSMJID.fieldbyname('Qty').AsFloat * num1])) ;
          if i=3 then
          kcQty:=strtofloat(format('%.1f',[CDSMJID.fieldbyname('Qty').AsFloat / num1])) ;
          with ADOQueryCmd do
          begin
          Close;
          sql.Clear;
          sql.Add(' Update CK_BanCP_KC Set ZDYFlote1=KCqty ');
          sql.Add(' where CRID='+CDSMJID.Fieldbyname('CRID').AsString);
          sql.Add(' and ZDYFlote1=0 ');
          sql.Add(' Update CK_BanCP_KC Set kcqty='''+floattostr(kcQty)+''',ZDYStr1=''库存调整'' ');
          sql.Add(' where CRID='+CDSMJID.Fieldbyname('CRID').AsString);
          sql.Add(' Update CK_BanCP_KC Set kcRollNum=0 ');
          sql.Add(' where CRID='+CDSMJID.Fieldbyname('CRID').AsString);
          sql.Add(' and kcqty=0 and kcRollNum<>0 ');
          ExecSQL;
          end;
          Edit;
          FieldByName('qty').Value:=kcQty;
          FieldByName('SSel').Value:=False;
          post;
        end
        else
        begin
          Edit;
          FieldByName('SSel').Value:=False;
          post;
        end;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
   // initGrid();
    Application.MessageBox('操作成功!','提示',0);
    Exit;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常!','提示',0);
  end;
end;

procedure TfrmCKSMEdit.CheckBox1Click(Sender: TObject);
begin
  IF  CDSMJID.IsEmpty then exit;
  with CDSMJID do
  begin
    DisableControls;
    first;
    while not eof do
    begin
      edit;
      fieldbyname('ssel').Value:=checkbox1.Checked;
      post;
      next;
    end;
    First;
    EnableControls;
  end;
end;

procedure TfrmCKSMEdit.ToolButton2Click(Sender: TObject);
var
  fPrintFile:string;
  Txt,fImagePath:string;
  Moudle: THandle;
  Makebar:TMakebar;
  Mixtext:TMixtext;
begin
  if CDSMJID.IsEmpty then Exit;
  if CDSMJID.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  CDSMJID.DisableControls;
  with CDSMJID do
  begin
    First;
    while not Eof do
    begin
      if CDSMJID.FieldByName('SSel').AsBoolean=True then
      begin
        try
          Moudle:=LoadLibrary('MakeQRBarcode.dll');
          @Makebar:=GetProcAddress(Moudle,'Make');
          @Mixtext:=GetProcAddress(Moudle,'MixText');
          Txt:=Trim(CDSMJID.fieldbyname('MJID').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);
          CDSMJID.EnableControls;
          exit;
        end;
        with ADOQueryPrint do     //英文名称不要在样品表中读取，要在客户自定义表中读取。note字段。
        begin
          Close;
          SQL.Clear;
          sql.Add('select A.*,kcQty as mjlen,kcQtyUnit as MJTypeother ');
          sql.Add(' from CK_BanCP_KC A') ;
          SQL.Add(' where A.MJID='''+Trim(CDSMJID.fieldbyname('MJID').AsString)+'''');
          SQL.Add(' and KCQty>0 ');
          Open;
        end;
          fPrintFile:=ExtractFilePath(Application.ExeName)+'Report\通用标签.rmf' ;
        if FileExists(fPrintFile) then
        begin
          RMVariables['QRBARCODE']:=fImagePath;
          RMGridReport1.LoadFromFile(fPrintFile);
       //   RMGridReport1.ShowReport;
          RMGridReport1.PrintReport;
        end else
        begin
          CDSMJID.EnableControls;
          Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\卷标签.rmf'),'提示',0);
          Exit;
        end;
      end;
      Next;
    end;
  end;
  CDSMJID.EnableControls;
end;

procedure TfrmCKSMEdit.FormCloseQuery(Sender: TObject;
  var CanClose: Boolean);
begin
  IF not CDSMJID.IsEmpty then
  begin
    if application.MessageBox('您有数据未保存？您是否要退出？','提示信息!',MB_YESNO+MB_ICONQUESTION)=IDYES
    then   CanClose:= True
    else   CanClose:= False;
  end;
end;

end.
