unit U_CPSQSMCK;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage,
  cxEdit, DB, cxDBData, StdCtrls, ExtCtrls, ADODB, DBClient,
  cxGridCustomPopupMenu, cxGridPopupMenu, cxGridLevel, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxClasses, cxControls, cxGridCustomView,
  cxGrid, MovePanel, cxCheckBox, Menus, ComCtrls, BtnEdit, ShellAPI, cxTextEdit,
  cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, dxSkinsCore,
  dxSkinsDefaultPainters, dxSkinscxPCPainter, dxSkinBlack, dxSkinBlue,
  dxSkinBlueprint, dxSkinCaramel, dxSkinCoffee, dxSkinDarkRoom,
  dxSkinDarkSide, dxSkinDevExpressDarkStyle, dxSkinDevExpressStyle,
  dxSkinFoggy, dxSkinGlassOceans, dxSkinHighContrast, dxSkiniMaginary,
  dxSkinLilian, dxSkinLiquidSky, dxSkinLondonLiquidSky, dxSkinMcSkin,
  dxSkinMetropolis, dxSkinMetropolisDark, dxSkinMoneyTwins,
  dxSkinOffice2007Black, dxSkinOffice2007Blue, dxSkinOffice2007Green,
  dxSkinOffice2007Pink, dxSkinOffice2007Silver, dxSkinOffice2010Black,
  dxSkinOffice2010Blue, dxSkinOffice2010Silver, dxSkinOffice2013DarkGray,
  dxSkinOffice2013LightGray, dxSkinOffice2013White, dxSkinPumpkin,
  dxSkinSeven, dxSkinSevenClassic, dxSkinSharp, dxSkinSharpPlus,
  dxSkinSilver, dxSkinSpringTime, dxSkinStardust, dxSkinSummer2008,
  dxSkinTheAsphaltWorld, dxSkinValentine, dxSkinVS2010, dxSkinWhiteprint,
  dxSkinXmas2008Blue;

type
  TfrmCPSQSMCK = class(TForm)
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    DS_MainSel: TDataSource;
    CDS_MainSel: TClientDataSet;
    cxGridPopupMenu4: TcxGridPopupMenu;
    cxGrid3: TcxGrid;
    Tv3: TcxGridDBTableView;
    cxGridDBColumn4: TcxGridDBColumn;
    v3Column1: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    v3Column2: TcxGridDBColumn;
    Panel2: TPanel;
    Label5: TLabel;
    Label4: TLabel;
    CKOrdNo: TEdit;
    SMID: TEdit;
    CRTime: TDateTimePicker;
    Button2: TButton;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    Label12: TLabel;
    CDS_Main: TClientDataSet;
    Button1: TButton;
    Label2: TLabel;
    Label3: TLabel;
    Edit1: TEdit;
    cxGrid1: TcxGrid;
    TV2: TcxGridDBTableView;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    V2Column1: TcxGridDBColumn;
    DS_FHSQ: TDataSource;
    CDS_FHSQ: TClientDataSet;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Button3: TButton;
    CRType: TEdit;
    Label1: TLabel;
    Label6: TLabel;
    edtIsSMTH: TEdit;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure SMIDKeyPress(Sender: TObject; var Key: Char);
    procedure Button2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure TV2MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure Button3Click(Sender: TObject);
    procedure edtIsSMTHClick(Sender: TObject);
  private
    { Private declarations }
    FSMStr, JXFlag, FSubId: string;
    FFHPS, FSQPS: integer;
    procedure InitFHMX();
    function CheckCkd(): Boolean;
    function CheckPB(): Boolean;
    function JudgeJTMCX(): Boolean;
    procedure SaveCKDataJTMCX();
    procedure SaveCK();
    procedure InitFHPS();
  public
    { Public declarations }
  end;

var
  frmCPSQSMCK: TfrmCPSQSMCK;

implementation

uses
  U_DataLink, U_Fun, MMSystem, U_CPFHSQListSel, U_FinishedClothKCList_SMCK;        //, U_CKProductBCPKCList

//var
//  frmPBKCListSel: TfrmCKProductBCPKCListPB;
{$R *.dfm}

procedure TfrmCPSQSMCK.SaveCKDataJTMCX();
begin
  try
    ADOQueryCmd.Connection.BeginTrans;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update CK_Cloth_CR Set CRFlag=''待出库'',CRType=NULL,CRTime=NULL,CRNote=NULL,CKNo=NULL ');
      sql.Add(' where  CK_Cloth_CR.JYID=' + Quotedstr(Trim(FSMStr)));
      sql.Add(' and CRFlag=''出库''');

      sql.Add(' UPdate CK_Cloth_KC Set Valid=''Y'' ');
      sql.Add(' where  CK_Cloth_KC.JYID=' + Quotedstr(Trim(FSMStr)));
      sql.Add(' and Valid=''N'' ');

      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('exec P_FHSQ_UP_QTY  ' + quotedstr(trim(CKOrdNo.Text)));
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
      sql.Add(' ' + quotedstr(trim(DName)));
      sql.Add(',getdate() ');
      sql.Add(',' + quotedstr(''));
      sql.Add(',' + quotedstr(trim('出库扫描撤销')));
      sql.Add(',' + quotedstr('卷条码：' + trim(FSMStr) + ' 出库单号：' + trim(CKOrdNo.Text)));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    FFHPS := FFHPS - 1;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('撤销出库失败！', '提示信息', 0);
  end;
end;

function TfrmCPSQSMCK.JudgeJTMCX(): Boolean;
begin
  try
    if Trim(CKOrdNo.Text) = '' then
      raise Exception.Create('出库单号不能为空');

    if CDS_MainSel.IsEmpty then
      raise Exception.Create('无可撤销的数据');

    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add(' select A.SQPiQty,A.FHPiQty,B.* from SQ_FH_Sub A');
      sql.Add(' inner join CK_Cloth_CR B on A.p_code=B.p_code');
      sql.Add(' where A.FHMainID=''' + Trim(CKOrdNo.Text) + '''');
      sql.Add(' and  B.CRFlag=''出库'' and B.JYID=''' + Trim(FSMStr) + '''');
      Open;
    end;

    if ADOQueryTemp.IsEmpty then
    begin
      raise Exception.Create('无可撤销的数据');
    end
    else
    begin
      FSubId := ADOQueryTemp.fieldbyname('P_CodeName').AsString;
      FSQPS := ADOQueryTemp.fieldbyname('SQPiQty').AsInteger;
      FFHPS := ADOQueryTemp.fieldbyname('FHPiQty').AsInteger;
    end;
    Result := True;
  except
    Result := false;
    PlaySound(PChar('wav\' + Exception(ExceptObject).Message + '.wav'), 0, SND_FILENAME or SND_ASYNC);
  end;
end;

procedure TfrmCPSQSMCK.InitFHPS();
begin
  with CDS_FHSQ do
  begin
    Locate('P_CodeName', trim(FSubId), []);
    Edit;
    FieldByName('FHPiQty').Value := FFHPS;
    Post;
    Edit1.Text := IntToStr(FFHPS)
  end;

end;

procedure TfrmCPSQSMCK.SaveCK();
var
  maxno, SMFlag, KYCKFlag: string;
  FFQty: Integer;
begin

  try
    ADOQueryCmd.Connection.BeginTrans;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('UPDATE CK_Cloth_CR Set  ');
      sql.add(' CRTime=getdate()  ');
      sql.add(',CRType=' + quotedstr(Trim(CRType.Text)));
      sql.add(',CRFlag=''出库''  ');
      sql.add(',CKNo=' + Quotedstr(CKOrdNo.Text));
      sql.Add(' where CK_Cloth_CR.JYID=' + Quotedstr(Trim(FSMStr)));
      sql.Add('and CK_Cloth_CR.CRFlag=''待出库''  ');
      ExecSQL;
    end;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('UPDATE CK_Cloth_KC Set  ');
      sql.add(' Valid=''N''  ');
      sql.Add(' where CK_Cloth_KC.JYID=' + Quotedstr(Trim(FSMStr)));
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('exec P_FHSQ_UP_QTY1  ' + quotedstr(trim(CKOrdNo.Text)));
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    FFHPS := FFHPS + 1;

    PlaySound('wav\正确.wav', 0, SND_FILENAME or SND_ASYNC);
    Exit;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('出库失败！', '提示信息', 0);
    PlaySound('wav\出库失败.wav', 0, SND_FILENAME or SND_ASYNC);
  end;
end;

function TfrmCPSQSMCK.CheckCkd(): Boolean;
begin
  try
    if CKOrdNo.Text = Trim(FSMStr) then
      raise Exception.Create('重复扫描申请单!.wav');

    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from SQ_FH_Main A inner join SQ_FH_Sub B on A.FHMainId=B.FHMainId  where A.FHMainId=''' + Trim(FSMStr) + '''');
      Open;
    end;
    if ADOQueryTemp.IsEmpty then
    begin
      raise Exception.Create('申请单不存在暂停发货.wav');
    end
    else
    begin
      SCreateCDS20(ADOQueryTemp, CDS_FHSQ);
      SInitCDSData20(ADOQueryTemp, CDS_FHSQ);
      CKOrdNo.Text := Trim(FSMStr);
      CRType.Text := Trim(CDS_FHSQ.FieldByName('SQType').AsString);
      InitFHMX();
    end;
    Result := True;
  except
    Result := false;
//    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
    PlaySound(PChar('wav\' + Exception(ExceptObject).Message + '.wav'), 0, SND_FILENAME or SND_ASYNC);
  end;

end;

function TfrmCPSQSMCK.CheckPB(): Boolean;
begin

  try

    if Trim(CKOrdNo.Text) = '' then
      raise Exception.Create('未扫描申请单暂停发货');

    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add(' select A.SQPiQty,A.FHPiQty,B.* from SQ_FH_Sub A');
      sql.Add(' inner join JY_Cloth B on A.P_CODEname=B.P_CODEname ');
      sql.Add(' where A.FHMainID=''' + Trim(CKOrdNo.Text) + '''');
      sql.Add(' and B.JYID=''' + Trim(FSMStr) + '''');

//      SQL.Add('and A.BatchNo = B.BatchNo and A.WXPH =B.WXPH');
//      showmessage(sql.text);
      Open;
    end;
    if ADOQueryTemp.IsEmpty then
    begin
      raise Exception.Create('条码错误暂停发货');
    end
    else
    begin
//      FSubId := ADOQueryTemp.fieldbyname('SubId').AsString;
      FSQPS := ADOQueryTemp.fieldbyname('SQPiQty').AsInteger;
      FFHPS := ADOQueryTemp.fieldbyname('FHPiQty').AsInteger;
      if FSQPS <= FFHPS then
      begin
        raise Exception.Create('超出发货上限暂停发货');
      end;
    end;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add(' select C.CKNo from SQ_FH_Sub A');
      sql.Add(' inner join JY_Cloth B on A.FromMainId=B.MainId');
      sql.Add(' inner join CK_Cloth_CR C on B.JYID=C.JYID and C.CRFlag=''出库'' ');
      sql.Add(' where B.JYID=''' + Trim(FSMStr) + '''');
      sql.Add(' and A.FHMainId=''' + Trim(CKOrdNo.Text) + '''');
      Open;
    end;
    if ADOQueryTemp.IsEmpty = False then
    begin
      if Trim(ADOQueryTemp.fieldbyname('CKNo').AsString) = Trim(CKOrdNo.Text) then
      begin
        raise Exception.Create('已扫描');
      end
      else
      begin
        raise Exception.Create('已出库暂停发货');
      end;
    end;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add(' select RKNo from CK_Cloth_CR ');
      sql.Add(' where CRFlag=''入库'' and JYID=''' + Trim(FSMStr) + '''');
      Open;
    end;
    if ADOQueryTemp.IsEmpty then
    begin
      raise Exception.Create('未入库暂停发货');
    end;
    Result := True;
  except
    Result := false;
//    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
    PlaySound(PChar('wav\' + Exception(ExceptObject).Message + '.wav'), 0, SND_FILENAME or SND_ASYNC);
  end;
end;

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

procedure TfrmCPSQSMCK.FormDestroy(Sender: TObject);
begin
  frmCPSQSMCK := nil;
end;

procedure TfrmCPSQSMCK.FormShow(Sender: TObject);
begin
  SMID.SetFocus;
  ReadCxGrid('扫描出库', Tv3, '坯布仓库');
  CRTime.DateTime := SGetServerDate(ADOQueryTemp);
end;

procedure TfrmCPSQSMCK.InitFHMX();
var
  MFHPS: integer;
begin
  if CDS_FHSQ.IsEmpty then
    Exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select B.* ');
    sql.Add(' from CK_Cloth_CR A');
    sql.Add(' inner join JY_Cloth B on A.JYID=B.JYID');
    sql.Add(' where A.CKNO=' + quotedstr(Trim(CKOrdNo.Text)));
    sql.Add(' and A.p_codename=' + quotedstr(Trim(CDS_FHSQ.FieldByName('p_codename').AsString)));
//    ShowMessage(sql.Text);
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_MainSel);
  SInitCDSData20(ADOQueryTemp, CDS_MainSel);
  if CDS_MainSel.IsEmpty = False then
    MFHPS := Tv3.DataController.Summary.FooterSummaryValues[0]
  else
    MFHPS := 0;

  Edit1.Text := IntToStr(MFHPS);
  with CDS_FHSQ do
  begin
    Edit;
    FieldByName('FHPiQty').Value := MFHPS;
  end;
end;

procedure TfrmCPSQSMCK.SMIDKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    FSMStr := Trim(SMID.Text);
    SMID.Text := '';

    if edtIsSMTH.Text = '' then
    begin
      if Copy(FSMStr, 1, 2) = 'FC' then
      begin
        if CheckCkd() then
        begin

        end;
      end
      else
      begin
        if CheckPB() then
        begin
          SaveCK();
          InitFHPS();
          InitFHMX();
        end;
      end;
    end
    else
    begin
      if JudgeJTMCX() then  //检测卷条码
        SaveCKDataJTMCX();
      InitFHPS();
      InitFHMX
    end;
  end;
end;

procedure TfrmCPSQSMCK.Button2Click(Sender: TObject);
begin
  Close;
  WriteCxGrid('扫描出库', Tv3, '坯布仓库');
end;

procedure TfrmCPSQSMCK.Button1Click(Sender: TObject);
var
  MKey: Char;
begin
  MKey := #13;
  try
    frmFHSQListSel := TfrmFHSQListSel.Create(self);
    with frmFHSQListSel do
    begin
      if ShowModal = 1 then
      begin
        SMID.Text := Trim(Order_Main.fieldbyname('FHMainId').AsString);
        SMIDKeyPress(SMID, MKey);
      end;
    end;
  finally
    frmFHSQListSel.Free;
  end;
end;

procedure TfrmCPSQSMCK.TV2MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  InitFHMX();
end;

procedure TfrmCPSQSMCK.Button3Click(Sender: TObject);
var
  MKey: Char;
begin
  MKey := #13;
  if TRIM(SELF.CKOrdNo.Text) = '' then
    EXIT;
  try
    frmFinishedClothKCList_SMCK := TfrmFinishedClothKCList_SMCK.Create(self);
    with frmFinishedClothKCList_SMCK do
    begin

      if ShowModal = 1 then
      begin
        while frmFinishedClothKCList_SMCK.CDS_Main.Locate('SSel', true, []) do
        begin
          self.SMID.Text := Trim(frmFinishedClothKCList_SMCK.CDS_Main.fieldbyname('JYID').AsString);
          self.SMIDKeyPress(self.SMID, MKey);
          frmFinishedClothKCList_SMCK.CDS_Main.Delete;
          sleep(500);
        end;
      end;
    end;
  finally
    frmFinishedClothKCList_SMCK.Free;
  end;
end;

procedure TfrmCPSQSMCK.edtIsSMTHClick(Sender: TObject);
begin
  SMID.SetFocus;
  if edtIsSMTH.Text = '√' then
  begin
    edtIsSMTH.Text := '';
  end
  else
  begin
    if Application.MessageBox('确定要扫描退货吗？', '提示', 32 + 4) = IDYES then
      edtIsSMTH.Text := '√';
  end;
end;

end.

