unit U_PBSQSMCK;

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, dxSkinsCore, 
      dxSkinDarkRoom,
    
     
     
    
    
    
    
   dxSkinOffice2013White, 
     dxSkinSharpPlus,
   dxSkinSpringTime,  
   dxSkinsDefaultPainters, 
     dxSkinscxPCPainter,
  cxNavigator;

type
  TfrmPBSQSMCK = class(TForm)
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    DS_MainSel: TDataSource;
    CDS_MainSel: TClientDataSet;
    cxGridPopupMenu4: TcxGridPopupMenu;
    cxGrid3: TcxGrid;
    Tv3: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    v3Column1: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    v3Column2: TcxGridDBColumn;
    Panel2: TPanel;
    Label5: TLabel;
    CKOrdNo: TEdit;
    SMID: TEdit;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    Label12: TLabel;
    CDS_Main: TClientDataSet;
    CRType: TEdit;
    Label2: TLabel;
    Label3: TLabel;
    Edit1: TEdit;
    cxGrid1: TcxGrid;
    TV2: TcxGridDBTableView;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    V2Column1: TcxGridDBColumn;
    DS_FHSQ: TDataSource;
    CDS_FHSQ: TClientDataSet;
    cxGridPopupMenu1: TcxGridPopupMenu;
    CDS_DCK: TClientDataSet;
    v3Column3: TcxGridDBColumn;
    V2Column2: TcxGridDBColumn;
    V2Column3: TcxGridDBColumn;
    Button2: TButton;
    Button1: TButton;
    Button3: TButton;
    Label1: TLabel;
    CDS_DB: TClientDataSet;
    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);
  private
    { Private declarations }
    FCKOrdNo, FSMStr, JXFlag, FMainId, FSubId, FIDType: string;
    FFHPS, FSQPS: integer;
    procedure InitFHMX();
    procedure CheckCkd();
    procedure CheckPB(MMJID: string);
    procedure SaveCK(MMJID: string);
    procedure InitFHPS();
  public
    { Public declarations }
  end;

var
  frmPBSQSMCK: TfrmPBSQSMCK;

implementation

uses
  U_DataLink, U_Fun, MMSystem, U_FHSQListSel, U_PBKCListSel;

{$R *.dfm}

procedure TfrmPBSQSMCK.InitFHPS();
begin
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select FHPS=Sum(FHPS) from FHSQ_Sub where FSID=''' + Trim(CKOrdNo.Text) + '''');
    Open;
  end;
  Edit1.Text := ADOQueryTemp.fieldbyname('FHPS').AsString;
  {with CDS_FHSQ do
  begin
    Locate('SubId', FSubId, []);
    Edit;
    FieldByName('FHPS').Value := FFHPS;

    Edit1.Text := IntToStr(FFHPS)
  end; }

end;

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

  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('exec P_Do_MLJYCK_MJID ');
      sql.Add(' @MJID= ' + Quotedstr(MMJID));
      sql.Add(',@CKOrdNo= ' + Quotedstr(CKOrdNo.Text));
      sql.Add(',@DName=' + Quotedstr(Trim(DName)));
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add(' exec P_Update_OrderSub :SubId');
      Parameters.ParamByName('SubId').Value := Trim(FSubId);
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
  except
    PlaySound('wav\数据保存失败暂停发货.wav', 0, SND_FILENAME or SND_ASYNC);
    ADOQueryCmd.Connection.RollbackTrans;
    Exit;
  end;

  with ADOQueryTemp do
  begin
    close;
    sql.Clear;
    sql.Add(' select A.*,B.CRID from JTJY_New A');
    sql.Add(' inner join CK_MLCP_CR B on A.JYID=B.MJID and B.CRFlag=''入库'' ');
    sql.Add(' where A. jyid=''' + Trim(MMJID) + '''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_Main);
  SInitCDSData20(ADOQueryTemp, CDS_Main);

  FMainId := Trim(CDS_Main.fieldbyname('NewMLID').AsString);
  FSubId := Trim(CDS_Main.fieldbyname('NewSubid').AsString);
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('exec P_Do_FHSQ @CKOrdNo=' + quotedstr(trim(CKOrdNo.Text)));
    ExecSQL;
  end;

  with CDS_MainSel do
  begin
    Append;
    FieldByName('NewMLID').Value := Self.CDS_Main.fieldbyname('NewMLID').Value;
    FieldByName('NewSubid').Value := Self.CDS_Main.fieldbyname('NewSubid').Value;
    FieldByName('JYKG').Value := Self.CDS_Main.fieldbyname('JYKG').Value;
    FieldByName('JYLen').Value := Self.CDS_Main.fieldbyname('JYLen').Value;
    FieldByName('JYID').Value := Self.CDS_Main.fieldbyname('JYID').Value;
    FieldByName('JYNo').Value := Self.CDS_Main.fieldbyname('JYNo').Value;
    FieldByName('GangNo').Value := Self.CDS_Main.fieldbyname('GangNo').Value;
    Post;
  end;
  FFHPS := FFHPS + 1;
  if FIDType <> '入库单' then
    PlaySound('wav\正确.wav', 0, SND_FILENAME or SND_ASYNC);

end;

procedure TfrmPBSQSMCK.CheckCkd();
begin
  if CKOrdNo.Text = Trim(FSMStr) then
    exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select  * from FHSQ_Main A inner join FHSQ_Sub B on A.FSId=B.FSId  where A.FSID=''' + Trim(FSMStr) + '''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty then
  begin
    JXFlag := '999';
    PlaySound('wav\申请单不存在暂停发货.wav', 0, SND_FILENAME or SND_ASYNC);
    Exit;
  end
  else
  begin
    SCreateCDS20(ADOQueryTemp, CDS_FHSQ);
    SInitCDSData20(ADOQueryTemp, CDS_FHSQ);
    CKOrdNo.Text := Trim(FSMStr);
    InitFHMX();
  end;
  JXFlag := '扫描申请单成功';
end;

procedure TfrmPBSQSMCK.CheckPB(MMJID: string);
begin
  if Trim(CKOrdNo.Text) = '' then
  begin
    PlaySound('wav\未扫描申请单暂停发货.wav', 0, SND_FILENAME or SND_ASYNC);
    Exit;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YS_Money_CR where isnull(CKNO,'''')=''' + Trim(CKOrdNo.Text) + '''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty = False then
  begin
    PlaySound('wav\已对账暂停发货.wav', 0, SND_FILENAME or SND_ASYNC);
    Exit;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(' select A.SQPS,A.FHPS,B.* from FHSQ_Sub A');
    sql.Add(' inner join JTJY_New  B on A.Subid=B.NewSubid ');
    sql.Add(' where A.FSID=''' + Trim(CKOrdNo.Text) + '''');
    sql.Add(' and B.JYID=''' + Trim(MMJID) + '''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty then
  begin
    PlaySound('wav\条码错误暂停发货.wav', 0, SND_FILENAME or SND_ASYNC);
    Exit;
  end
  else
  begin
    FSQPS := ADOQueryTemp.fieldbyname('SQPS').AsInteger;
    FFHPS := ADOQueryTemp.fieldbyname('FHPS').AsInteger;
    if FSQPS <= FFHPS then
    begin
      PlaySound('wav\超出发货上限暂停发货.wav', 0, SND_FILENAME or SND_ASYNC);
      Exit;
    end;
    FSubId := Trim(ADOQueryTemp.fieldbyname('NewSubid').AsString);
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(' select MJID from CK_MLCP_CR C   ');
    sql.Add(' where C.CRFlag=''入库'' and  C.MJID=''' + Trim(MMJID) + '''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty then
  begin
    PlaySound('wav\未入库暂停发货.wav', 0, SND_FILENAME or SND_ASYNC);
    Exit;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(' select C.CKOrdNo  from  CK_MLCP_CR C   ');
    sql.Add(' where  C.CRFlag=''出库''  and  C.MJID=' + quotedstr(Trim(MMJID)));
    Open;
  end;
  if ADOQueryTemp.IsEmpty = False then
  begin
    if Trim(ADOQueryTemp.fieldbyname('CKOrdNo').AsString) = Trim(CKOrdNo.Text) then
    begin
      PlaySound('wav\已扫描.wav', 0, SND_FILENAME or SND_ASYNC);
      Exit;
    end
    else
    begin
      PlaySound('wav\已出库暂停发货.wav', 0, SND_FILENAME or SND_ASYNC);
      Exit;
    end;
  end;

  JXFlag := '坯布可正常发货';
end;

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

procedure TfrmPBSQSMCK.FormDestroy(Sender: TObject);
begin
  frmPBSQSMCK := nil;
end;

procedure TfrmPBSQSMCK.FormShow(Sender: TObject);
begin
  SMID.SetFocus;
  ReadCxGrid('扫描出库', Tv3, '坯布仓库');
end;

procedure TfrmPBSQSMCK.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_MLCP_CR A');
    sql.Add(' inner join JTJY_New B on A.MJID=B.JYID ');
    sql.Add(' where A.CKOrdNO=' + quotedstr(Trim(CKOrdNo.Text)));
    sql.Add(' and A.MainId=' + quotedstr(Trim(CDS_FHSQ.FieldByName('MainId').AsString)));
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_MainSel);
  SInitCDSData20(ADOQueryTemp, CDS_MainSel);
  if CDS_MainSel.IsEmpty = False then
    MFHPS := Tv3.DataController.Summary.FooterSummaryValues[2]
  else
    MFHPS := 0;

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

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

    JXFlag := '';
    FSMStr := Trim(SMID.Text);
    SMID.Text := '';
    if (Copy(FSMStr, 1, 2) = 'CK') or (Copy(FSMStr, 1, 2) = 'NK') then
    begin
      FIDType := '发货申请';
      CheckCkd()
    end
    else if (Copy(FSMStr, 1, 2) = 'DB') then
    begin
      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from  JTJY_New where baoID=''' + Trim(FSMStr) + '''');
        Open;
      end;

      SCreateCDS20(ADOQueryTemp, CDS_DB);
      SInitCDSData20(ADOQueryTemp, CDS_DB);

      with CDS_DB do
      begin
        First;
        while not eof do
        begin
          FIDType := '卷条码';
          CheckPB(CDS_DB.FieldByName('jyid').AsString);
          if JXFlag = '坯布可正常发货' then
          begin
            SaveCK(CDS_DB.FieldByName('jyid').AsString);
            InitFHPS();
          end;
          Next;
        end;
      end;
    end
    else
    begin
      FIDType := '卷条码';
      CheckPB(FSMStr);
      if JXFlag = '坯布可正常发货' then
      begin
        SaveCK(FSMStr);
        InitFHPS();
      end;
    end;
  end;
end;

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

procedure TfrmPBSQSMCK.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('FSID').AsString);
        SMIDKeyPress(SMID, MKey);
      end;
    end;
  finally
    frmFHSQListSel.Free;
  end;
end;

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

procedure TfrmPBSQSMCK.Button3Click(Sender: TObject);
var
  MKey: Char;
begin
  MKey := #13;
  if TRIM(SELF.CKOrdNo.Text) = '' then
    EXIT;
  try
    frmPBKCListSel := TfrmPBKCListSel.Create(self);
    with frmPBKCListSel do
    begin
      FCKOrdNo := TRIM(SELF.CKOrdNo.Text);
      if ShowModal = 1 then
      begin
        while frmPBKCListSel.CDS_Main.Locate('SSel', true, []) do
        begin
          self.SMID.Text := Trim(frmPBKCListSel.CDS_Main.fieldbyname('MJID').AsString);
          self.SMIDKeyPress(SMID, MKey);
          frmPBKCListSel.CDS_Main.Delete;
          sleep(500);
        end;
      end;
    end;
  finally
    frmPBKCListSel.Free;
  end;
end;

end.

