unit U_PDSMEdit;

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,MMSystem;

type
  TfrmPDSMEdit = class(TForm)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    PHYG: TBtnEditA;
    defstr2: TComboBox;
    Label16: TLabel;
    Label15: TLabel;
    packNo: TcxCurrencyEdit;
    Label14: TLabel;
    Label1: TLabel;
    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;
    CDSMJID: TClientDataSet;
    DataSource2: TDataSource;
    SmNO: TEdit;
    ADOQueryTmp: TADOQuery;
    V2Column1: TcxGridDBColumn;
    kcKw: TBtnEditA;
    Label5: TLabel;
    MovePanel1: TMovePanel;
    Edit2: TEdit;
    Button4: TButton;
    Label2: TLabel;
    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;
    ADOQuery1: TADOQuery;
    DataSource1: TDataSource;
    ADOQueryTm: TADOQuery;
    DSTm: TDataSource;
    V2Column2: 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 kcKwBtnClick(Sender: TObject);
    procedure Button4Click(Sender: TObject);
  private
    procedure InitGrid();
    procedure InitMJID();
    procedure InitKw();
    function  SaveRK():Boolean;
    function  SaveKW():Boolean;
    Procedure QueryRk();
    Procedure QueryTM();
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmPDSMEdit: TfrmPDSMEdit;

implementation
uses
  U_DataLink,U_Fun, U_UserHelp, U_ZDYHelp, U_CPKWListSel;
{$R *.dfm}
procedure TfrmPDSMEdit.InitKw();
begin
  kcKw.text:='';
  CDSMJID.EmptyDataSet;
  with ADOQueryTmp  do
  begin
      Close;
      sql.Clear;
      SQL.add('select * from KH_Zdy_Attachment where ATID='+quotedstr(trim(smNO.Text)));
      sql.Add('and Type=''CPKW''');
      Open;
  end;
  IF not ADOQueryTmp.IsEmpty then
  begin
    kcKw.text:=trim(ADOQueryTmp.fieldbyname('ZdyCode').AsString);
  end;
  
end;
Procedure TfrmPDSMEdit.QueryTm();
begin
{  with ADOQueryTM do
  begin
    close;
    sql.Clear;
    sql.Add('select TOP 50 MJID');
    sql.Add('from WFB_MJJY A   ');
    SQL.Add('WHERE exists(select MJID from WFB_MJJY X where X.APID=A.APID and X.MJID='+quotedstr(trim(smNO.Text)));
    sql.Add(') and A.mjstr2=''未入库'' ');
    open;
  end;  }
end;
Procedure TfrmPDSMEdit.QueryRk();
begin
{  with ADOQuery1 do
  begin
    close;
    sql.Clear;
    sql.Add('select A.*,hC_ps=A.AordQty2,JY_ps=(select count(MainID) from WFB_MJJY X where X.APID=A.APID),');
    sql.Add('JY_Qty=(select sum(MJLen) from WFB_MJJY X where X.APID=A.APID), ');
    sql.Add('RK_RollNum=(select sum(RollNum) from CK_BanCP_CR X where X.APID=A.APID and X.CRType=''检验入库''),');
    sql.Add('RK_Qty=(select sum(Qty) from CK_BanCP_CR X where X.APID=A.APID and X.CRType=''检验入库'') ');
    sql.Add('from JYOrder_sub_anPai A   ');
    SQL.Add('WHERE exists(select MJID from WFB_MJJY X where X.APID=A.APID and X.MJID='+quotedstr(trim(smNO.Text)));
    sql.Add(')');
    open;
  end;  }
end;
function TfrmPDSMEdit.SaveKW():Boolean;
begin
  Result:=false;
  ADOQueryCmd.Connection.BeginTrans;
  try

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('update CK_BanCP_CR SET KCKW='+quotedstr(trim(kcKw.Text)));
      sql.Add('where MJID='''+Trim(ADOQueryMain.fieldbyname('MJId').AsString)+''' ');

      sql.Add('update CK_BanCP_KC SET KCKW='+quotedstr(trim(kcKw.Text)));
      sql.Add('where MJID='''+Trim(ADOQueryMain.fieldbyname('MJId').AsString)+''' ');

      sql.Add('update CK_BanCP_PD SET PRollNum=A.PRollNum+1,PQty=A.PQty+B.KCQty ');
      sql.Add('from CK_BanCP_PD A');
      sql.Add('inner join CK_BanCP_KC B on B.C_CodeName=A.C_CodeName and B.C_Color=A.C_Color and B.kcQtyUnit=A.QtyUnit ');
      sql.Add('where B.MJID='''+Trim(ADOQueryMain.fieldbyname('MJId').AsString)+''' ');
      sql.Add('and B.PDFlag=1 ');
      sql.Add('and A.Status=0 ');

      sql.Add('update CK_BanCP_KC SET PDFlag=2 ');
      sql.Add('where MJID='''+Trim(ADOQueryMain.fieldbyname('MJId').AsString)+''' ');
      sql.Add('and PDFlag=1 ');
      
      execsql;
    end;

    ADOQueryCmd.Connection.CommitTrans;
    Result:=true;
  except
    ADOQueryCmd.Connection.RollbackTrans;
  end;
end;
function TfrmPDSMEdit.SaveRK():Boolean;
var
  maxno:String;
  CRID:Integer;
  MaxCkNo,MaxCkSubNo:String;
  MhcTime:TdateTime;
begin
  Result:=false;
  ADOQueryCmd.Connection.BeginTrans;

  try
    with ADOQueryTmp  do
    begin
      Close;
      sql.Clear;
      SQL.add('Update CK_BanCP_CRID Set CRID=CRID+1');
      sql.Add('select * from CK_BanCP_CRID');
      Open;
    end;
    CRID:=ADOQueryTmp.fieldbyname('CRID').AsInteger;
    if GetLSNo(ADOQueryTmp,MaxCkNo,'JR','CK_BanCP_CR',4,1)=False then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('取成品仓库最大号失败！','提示',0);
      Exit;
    end;

          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from CK_BanCP_CR where 1<>1');
            Open;
          end;
        {   with ADOQueryTmp do
           begin
            close;
            sql.Clear;
            sql.Add('select * from JYOrder_Sub_AnPai ');
            sql.Add(' where APID='+quotedstr(trim(ADOQueryMain.fieldbyname('APID').AsString)));
            open;
            IF not ADOQueryTmp.IsEmpty then
            begin
              mhcTime:=ADOQueryTmp.fieldbyname('ADefDate1').AsDateTime;
            end
            else
            begin
              mhcTime:=date();
            end;
          end;     }
          mhcTime:=SGetServerDateTime(ADOQueryTmp);

          with ADOQueryCmd do
          begin
            Append;
            FieldByName('MainId').Value:=Trim(ADOQueryMain.fieldbyname('MainId').AsString);
            FieldByName('SubId').Value:=Trim(ADOQueryMain.fieldbyname('SubId').AsString);
            FieldByName('C_Code').Value:=Trim(ADOQueryMain.fieldbyname('C_Code').AsString);
            FieldByName('C_CodeName').Value:=Trim(ADOQueryMain.fieldbyname('C_CodeName').AsString);
            FieldByName('C_SPec').Value:=Trim(ADOQueryMain.fieldbyname('C_SPec').AsString);
            FieldByName('C_Color').Value:=Trim(ADOQueryMain.fieldbyname('C_Color').AsString);
            FieldByName('APID').Value:=Trim(ADOQueryMain.fieldbyname('APID').AsString);
            FieldByName('MJID').Value:=Trim(ADOQueryMain.fieldbyname('MJId').AsString);
            FieldByName('mjxh').Value:=Trim(ADOQueryMain.fieldbyname('mjxh').AsString);
            FieldByName('BCGangNO').Value:=Trim(ADOQueryMain.fieldbyname('BCGangNO').AsString);
            FieldByName('RCGangNO').Value:=Trim(ADOQueryMain.fieldbyname('RCGangNO').AsString);
            FieldByName('BCID').Value:=Trim(MaxCkNo);
            FieldByName('CRTime').Value:=FormatDateTime('yyyy-MM-dd',mhcTime);
            FieldByName('CRFlag').Value:='入库';
            FieldByName('CRType').Value:='检验入库';
            FieldByName('CRID').Value:=CRID;
            FieldByName('KGQty').Value:=0;
            FieldByName('Qty').Value:=ADOQueryMain.fieldbyname('MJLen').Value;
            FieldByName('RollNum').Value:=1;
            FieldByName('QtyUnit').Value:=ADOQueryMain.fieldbyname('MJTypeOther').Value;
            FieldByName('CPType').Value:=ADOQueryMain.fieldbyname('MJType').Value;
            FieldByName('Note').Value:=ADOQueryMain.fieldbyname('Note').Value;
            FieldByName('Filler').Value:=Trim(DName);
            FieldByName('FillTime').Value:=SGetServerDateTime(ADOQueryTmp);
         //   FieldByName('kcKw').Value:=Trim(kcKw.Text);
            fieldbyname('kcKw').Value:=GetKw(ADOQueryTmp,ADOQueryMain.fieldbyname('C_CodeName').AsString,ADOQueryMain.fieldbyname('C_Color').AsString);
            Post;
          end;

          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from CK_BanCP_KC where 1<>1');
            Open;
          end;
          with ADOQueryCmd do
          begin
            Append;
            FieldByName('CRID').Value:=CRID;
            FieldByName('BCID').Value:=Trim(MaxCkNo);
            FieldByName('CRTime').Value:=FormatDateTime('yyyy-MM-dd',mhcTime);
            FieldByName('CRType').Value:='检验入库';
            FieldByName('CPType').Value:=ADOQueryMain.fieldbyname('MJType').Value;
            FieldByName('C_Code').Value:=Trim(ADOQueryMain.fieldbyname('C_Code').AsString);
            FieldByName('C_CodeName').Value:=Trim(ADOQueryMain.fieldbyname('C_CodeName').AsString);
            FieldByName('C_SPec').Value:=Trim(ADOQueryMain.fieldbyname('C_SPec').AsString);
            FieldByName('C_Color').Value:=Trim(ADOQueryMain.fieldbyname('C_Color').AsString);
            FieldByName('BCGangNO').Value:=Trim(ADOQueryMain.fieldbyname('BCGangNO').AsString);
            FieldByName('RCGangNO').Value:=Trim(ADOQueryMain.fieldbyname('RCGangNO').AsString);
            FieldByName('MJID').Value:=Trim(ADOQueryMain.fieldbyname('MJID').AsString);
            FieldByName('KCKGQty').Value:=0;
            FieldByName('KCQty').Value:=ADOQueryMain.fieldbyname('MJlen').Value;
            FieldByName('KCRollNum').Value:=1;
            FieldByName('KCQtyUnit').Value:=ADOQueryMain.fieldbyname('MJTypeOther').Value;
            FieldByName('mjxh').Value:=Trim(ADOQueryMain.fieldbyname('mjxh').AsString);
            FieldByName('KCNote').Value:=ADOQueryMain.fieldbyname('Note').Value;
          //  FieldByName('kcKw').Value:=Trim(kcKw.Text);
            fieldbyname('kcKw').Value:=GetKw(ADOQueryTmp,ADOQueryMain.fieldbyname('C_CodeName').AsString,ADOQueryMain.fieldbyname('C_Color').AsString);
            Post;
          end;
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('Update WFB_MJJY Set MJStr2=''已入库'' where MJID='''+Trim(ADOQueryMain.fieldbyname('MJID').AsString)+'''');
            ExecSQL;
          end;

    ADOQueryCmd.Connection.CommitTrans;
    Result:=true;
  except
    ADOQueryCmd.Connection.RollbackTrans;
  end;
end;

procedure TfrmPDSMEdit.InitGrid();
begin
  with ADOQueryMain do
  begin
    Close;
    sql.Clear;
    SQL.Add('select A.* from CK_BanCP_CR A ');
    SQL.Add('where 1=2 ');
    Open;
  end;
  SCreateCDS20(ADOQueryMain,CDSMJID);
  SInitCDSData20(ADOQueryMain,CDSMJID);
end;

procedure TfrmPDSMEdit.InitMJID();
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('union all ');
    sql.Add('select MJID from CK_BanCP_KC A');
    sql.Add('where A.MJID='+quotedstr(trim(SmNO.Text)));
    open;
  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);
    SmNO.Text:='';
    exit;
  end;

  with  ADOQueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from WFB_MJJY A');
    sql.Add('where A.MJID='+quotedstr(trim(SmNO.Text)));
    sql.Add('and MJstr2=''未入库''');
    open;
  end;
  IF not 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);
    SmNO.Text:='';
    exit;
  end;
  with ADOQueryMain do
  begin
    Close;
    sql.Clear;
    SQL.Add('select A.* ');
    SQL.Add('from CK_BanCP_KC A');
    sql.Add('inner join CK_BanCP_CR B on B.BCID=A.BCID');
    sql.Add('where A.MJID='+quotedstr(trim(SmNO.Text)));
    Open;
  end;
  IF ADOQueryMain.fieldbyname('KCQty').AsFloat<=0 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
  IF ADOQueryMain.fieldbyname('PDflag').AsInteger=2 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 not CDSMJID.Locate('MJID',trim(ADOQueryMain.Fieldbyname('MJID').AsString),[]) then
    begin
      IF saveKW() then
      begin
              CDSMJID.Append;
              CDSMJID.FieldByName('SDefNote').Value:='盘点成功';
              CDSMJID.FieldByName('MJID').Value:=trim(ADOQueryMain.Fieldbyname('MJID').AsString);
              CDSMJID.FieldByName('KCKw').Value:=trim(KCKw.Text);
//            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:=1;
              CDSMJID.FieldByName('Qty').Value:=ADOQueryMain.Fieldbyname('KCQty').AsFloat;
              CDSMJID.FieldByName('QtyUnit').Value:=trim(ADOQueryMain.Fieldbyname('KCQtyUnit').AsString);
         //   CDSMJID.FieldByName('mainID').Value:=trim(ADOQueryMain.Fieldbyname('mainID').AsString);
         //   CDSMJID.FieldByName('subID').Value:=trim(ADOQueryMain.Fieldbyname('subID').AsString);
              CDSMJID.FieldByName('RCGangNO').Value:=trim(ADOQueryMain.Fieldbyname('RCGangNO').AsString);
              CDSMJID.FieldByName('BCgangNO').Value:=trim(ADOQueryMain.Fieldbyname('BCgangNO').AsString);
              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 FileExists(PChar(ExtractFilePath(Application.ExeName)+'wav\保存失败.wav')) then
        PlaySound(PChar(ExtractFilePath(Application.ExeName)+'wav\保存失败.wav'),0, SND_ASYNC);
      end;
    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:='';
end;

procedure TfrmPDSMEdit.FormDestroy(Sender: TObject);
begin
  frmPDSMEdit:=nil;
end;

procedure TfrmPDSMEdit.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin

 // writeCxGrid(self.Caption+tv1.Name,Tv1,'成品仓库');
  writeCxGrid(self.Caption+Tv2.Name,Tv2,'成品仓库');
 // writeCxGrid(self.Caption+Tv3.Name,Tv3,'成品仓库');
  Action:=cafree;
end;

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

procedure TfrmPDSMEdit.FormCreate(Sender: TObject);
begin
  Panel2.Align:=alClient;
 // readCxGrid(self.Caption+tv1.Name,Tv1,'成品仓库');
  readCxGrid(self.Caption+Tv2.Name,Tv2,'成品仓库');
  //readCxGrid(self.Caption+Tv3.Name,Tv3,'成品仓库');
end;

procedure TfrmPDSMEdit.SmNOKeyPress(Sender: TObject; var Key: Char);
begin
  IF key=#13 then
  begin
    if trim(smno.Text)='' then exit;
    IF uppercase(leftBstr(trim(smno.Text),2))='KW' then
    begin
      InitKw();
    end
    else
    begin
      IF trim(kcKw.Text)='' 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;
      initMJID();
    end;
    smno.Text:='';
    smno.SetFocus;
  end;
end;

procedure TfrmPDSMEdit.FormShow(Sender: TObject);
begin
  MovePanel1.Left:=(Width-MovePanel1.Width) div 2;
  MovePanel1.top:=(Height-MovePanel1.Height-200) div 2;
  InitGrid();
end;

procedure TfrmPDSMEdit.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 TfrmPDSMEdit.kcKwBtnClick(Sender: TObject);
begin
  try
    frmCPKWListSel:=TfrmCPKWListSel.Create(Application);
    with frmCPKWListSel do
    begin
      if ShowModal=1 then
      begin
        kcKw.Text:=Trim(CDS_HZ.fieldbyname('ZdyCode').AsString);
      end;
    end;
  finally
    frmCPKWListSel.Free;
  end;
end;

procedure TfrmPDSMEdit.Button4Click(Sender: TObject);
begin
  MovePanel1.Visible:=False;
  SmNO.SetFocus;
end;

end.
