unit U_FCSMCK;

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;

type
  TfrmFCSMCK = class(TForm)
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    DS_MainSel: TDataSource;
    CDS_MainSel: TClientDataSet;
    cxGridPopupMenu4: TcxGridPopupMenu;
    cxGrid3: TcxGrid;
    Tv3: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    v3Column3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Panel2: TPanel;
    Label5: TLabel;
    Label4: TLabel;
    Label9: TLabel;
    CKOrdNo: TEdit;
    BaoID: TEdit;
    CRTime: TDateTimePicker;
    KHName: TEdit;
    Button2: TButton;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    Label12: TLabel;
    CDS_Main: TClientDataSet;
    Button1: TButton;
    v3Column2: TcxGridDBColumn;
    Button3: TButton;
    CKType: TComboBox;
    Label1: TLabel;
    Edit1: TEdit;
    Label2: TLabel;
    Button4: TButton;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure BaoIDKeyPress(Sender: TObject; var Key: Char);
    procedure Button2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure CKTypeChange(Sender: TObject);
    procedure Edit1Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
  private
    { Private declarations }
    FCKOrdNo, FRKOrdID: string;
    function JudgeSQD(): Boolean;
    function JudgeRKD(): Boolean;
    function JudgeRKDCX(): Boolean;
    procedure SaveCKData();
    procedure SaveCKDataCX();
    procedure InitCKGrid();
  public
    FSMStr: string;
    { Public declarations }
  end;

var
  frmFCSMCK: TfrmFCSMCK;

implementation

uses
  U_DataLink, U_Fun, MMSystem, U_ZdyAttachment, U_ZdyAttachGYS,
  U_FinishedClothKCList_SMCK;

{$R *.dfm}
function TfrmFCSMCK.JudgeSQD(): Boolean;
begin
  Result := false;
  try

    if Trim(KHName.Text) = '' then
      raise Exception.Create('请选择收货单位!');

    if Trim(CKOrdNo.Text) = '' then
      raise Exception.Create('请先取号!');

    Result := True;
  except
    Result := false;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
//      PlaySound('未扫描申请单暂停发货.wav', 0, SND_FILENAME or SND_ASYNC);
  end;
end;

function TfrmFCSMCK.JudgeRKD(): Boolean;
begin
  try
    if CKType.Text = '回修出库' then
    begin
      if Trim(KHName.Text) = '' then
        raise Exception.Create('请选择收货单位!');
    end;

    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select   * from WFB_MJJY A   ');
      sql.Add('where  RKOrdID=' + quotedstr(trim(FRKOrdID)));
      Open;
    end;

    if ADOQueryTemp.IsEmpty then
      raise Exception.Create('条码错误暂停发货!');

    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('SELECT RKOrdID,CKOrdNo FROM CK_BanCP_CR where CRFlag=''出库'' and RKOrdID=' + quotedstr(trim(FRKOrdID)));
      Open;
    end;
    if not ADOQueryTemp.IsEmpty then
      raise Exception.Create('已扫描!');

    Result := True;
  except
    Result := false;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;
end;

function TfrmFCSMCK.JudgeRKDCX(): 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  ckno FROM CK_Cloth_CR where CRFlag=''出库'' and jyid=' + quotedstr(FSMStr));
      Open;
    end;

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

    Result := True;
  except
    Result := false;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;
end;

procedure TfrmFCSMCK.SaveCKData();
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(CKType.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;
    ADOQueryCmd.Connection.CommitTrans;
    PlaySound('wav\正确.wav', 0, SND_FILENAME or SND_ASYNC);
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('出库失败！', '提示信息', 0);
  end;
end;

procedure TfrmFCSMCK.SaveCKDataCX();
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('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;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('撤销出库失败！', '提示信息', 0);
  end;
end;

procedure TfrmFCSMCK.InitCKGrid();
begin
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('SELECT sum(jylen)Qty,CKNo,a.p_code,count(*)js FROM CK_Cloth_CR A  ');
    sql.Add(' WHERE CRFlag=''出库''');

    sql.Add('  and ckno=' + quotedstr(FCKOrdNo));
    sql.Add('group     by CKNo,a.p_code ');
 
//    ShowMessage(SQL.Text);
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_MainSel);
  SInitCDSData20(ADOQueryTemp, CDS_MainSel);

end;

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

procedure TfrmFCSMCK.FormDestroy(Sender: TObject);
begin
  frmFCSMCK := nil;
end;

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

procedure TfrmFCSMCK.BaoIDKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    FRKOrdID := TRIM(BaoID.Text);
    FSMStr := Trim(BAOID.Text);
    BaoID.Text := '';
    if Edit1.Text = '' then
    begin

      if not JudgeSQD() then
        exit;

      SaveCKData();
    end
    else
    begin
      if not JudgeRKDCX() then
        exit;
      SaveCKDataCX();
    end;

    InitCKGrid();
  end;
end;

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

procedure TfrmFCSMCK.Button1Click(Sender: TObject);
begin
  try
    frmZdyAttachment := TfrmZdyAttachment.Create(Application);
    with frmZdyAttachment do
    begin

      if ShowModal = 1 then
      begin
        KHName.Text := Trim(CDS_HZ.fieldbyname('Name').AsString);
      end;
    end;
  finally
    frmZdyAttachment.Free;
  end;
//  if CKType.Text = '销售出库' then
//  begin
//    try
//      frmZdyAttachment := TfrmZdyAttachment.Create(Application);
//      with frmZdyAttachment do
//      begin
//
//        if ShowModal = 1 then
//        begin
//          KHName.Text := Trim(CDS_HZ.fieldbyname('Name').AsString);
//        end;
//      end;
//    finally
//      frmZdyAttachment.Free;
//    end;
//  end
//  else
//  begin
//    try
//      frmZdyAttachGYS := TfrmZdyAttachGYS.Create(Application);
//      with frmZdyAttachGYS do
//      begin
//
//        if ShowModal = 1 then
//        begin
//          KHName.Text := Trim(CDS_HZ.fieldbyname('Name').AsString);
//
//        end;
//      end;
//    finally
//      frmZdyAttachGYS.Free;
//    end;
//  end;
end;

procedure TfrmFCSMCK.Button3Click(Sender: TObject);
begin
  if GetLSNo(ADOQueryTemp, FCKOrdNo, 'CK', 'CK_BanCP_CR', 4, 1) = False then
  begin
    Application.MessageBox('生成流水号异常！', '提示', 0);
    exit;
  end;
  CKOrdNo.TEXT := FCKOrdNo;

end;

procedure TfrmFCSMCK.CKTypeChange(Sender: TObject);
begin
  KHName.Text := '';
end;

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

procedure TfrmFCSMCK.Button4Click(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.baoID.Text := Trim(frmFinishedClothKCList_SMCK.CDS_Main.fieldbyname('JYID').AsString);
          self.baoIDKeyPress(self.baoID, MKey);
          frmFinishedClothKCList_SMCK.CDS_Main.Delete;
          sleep(500);
        end;
      end;
    end;
  finally
    frmFinishedClothKCList_SMCK.Free;
  end;
end;

end.

