unit U_CYBCPCKInPut;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, cxCalendar, cxDropDownEdit,
  ComCtrls, ToolWin, cxGridLevel, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxClasses, cxControls, cxGridCustomView, cxGrid,
  cxGridCustomPopupMenu, cxGridPopupMenu, ADODB, DBClient, cxButtonEdit,
  cxTextEdit, StdCtrls;

type
  TfrmCYBCPCKInPut = class(TForm)
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1C1: TcxGridDBColumn;
    v1C3: TcxGridDBColumn;
    v1C12: TcxGridDBColumn;
    v1C13: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    ToolBar1: TToolBar;
    TBCopyAdd: TToolButton;
    TBDel: TToolButton;
    TBSave: TToolButton;
    TBClose: TToolButton;
    DataSource3: TDataSource;
    CDS_Sub: TClientDataSet;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    cxGridPopupMenu2: TcxGridPopupMenu;
    v1C16: TcxGridDBColumn;
    v1FtyName: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1C6: TcxGridDBColumn;
    v1C5: TcxGridDBColumn;
    v1C8: TcxGridDBColumn;
    v1C7: TcxGridDBColumn;
    v1C14: TcxGridDBColumn;
    v1C15: TcxGridDBColumn;
    v1C2: TcxGridDBColumn;
    v1C4: TcxGridDBColumn;
    v1C9: TcxGridDBColumn;
    v1C10: TcxGridDBColumn;
    v1C11: TcxGridDBColumn;
    Label1: TLabel;
    TBAdd: TToolButton;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1FromFactoryName: TcxGridDBColumn;
    v1FromKHName: TcxGridDBColumn;
    v1PSName: TcxGridDBColumn;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCopyAddClick(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure Tv1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure v1C15PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure TBAddClick(Sender: TObject);
  private
    { Private declarations }
    function SaveCKData():Boolean;
    procedure InitGridStatus();
  public
    { Public declarations }
    FBCId,FCRType:String;

  end;

var
  frmCYBCPCKInPut: TfrmCYBCPCKInPut;

implementation
uses
  U_DataLink,U_RTFun,U_ZDYHelp,U_ProductOrderListSel;

{$R *.dfm}

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

procedure TfrmCYBCPCKInPut.TBCopyAddClick(Sender: TObject);
begin
  if CDS_Sub.IsEmpty=False then
  begin
    CopyAddRowCDS(CDS_Sub);
    with CDS_Sub do
    begin
      Edit;
      FieldByName('SPID').Value:=null;
      post;
    end;
  end else
  begin
    with CDS_Sub do
    begin
      Append;
      //FieldByName('CRTime').Value:=Trim(FormatDateTime('yyyy-MM-dd',Now));
      Post;
    end;
  end;
  
end;
function TfrmCYBCPCKInPut.SaveCKData():Boolean;
var
  FCRID,Maxno,FFSPID,FSPNO,FFlag:string;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    if Trim(FCRType)='加工完成入库' then
    begin
      FFlag:='JW';
    end else
    if Trim(FCRType)='加工未做回仓' then
    begin
      FFlag:='JH';
    end else
    if Trim(FCRType)='回修完成入库' then
    begin
      FFlag:='HW';
    end else
    if Trim(FCRType)='回修未做回仓' then
    begin
      FFlag:='HH';
    end else
    if Trim(FCRType)='采购入库' then
    begin
      FFlag:='CG';
    end else
    if Trim(FCRType)='客户退回' then
    begin
      FFlag:='KT';
    end;
    CDS_Sub.DisableControls;
    with CDS_Sub do
    begin
      First;
      while not eof do
      begin
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from CK_SXPB_CR where SPID='''+Trim(CDS_Sub.fieldbyname('SPID').AsString)+'''');
          open;
        end;
        FFSPID:=Trim(ADOQueryTemp.fieldbyname('SPID').AsString);
        FSPNO:=Trim(ADOQueryTemp.fieldbyname('SPNO').AsString);
        if Trim(FFSPID)='' then
        begin
          if GetLSNo(ADOQueryCmd,Maxno,FFlag,'CK_SXPB_CR',3,1)=False then
          begin
            Result:=False;
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取半成品入库编号失败!','提示',0);
            Exit;
          end;
          FSPNO:='CR'+Trim(Maxno);
        end else
        begin
          Maxno:=Trim(FFSPID);
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from CK_SXPB_CR where SPID='''+Trim(Maxno)+'''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(FFSPID)='' then
          begin
            Append;
          end else
          begin
            Edit;
          end;
          FieldByName('SPID').Value:=Trim(Maxno);
          FieldByName('FZSPID').Value:=Trim(Maxno);
          FieldByName('CKName').Value:='半成品仓库';
          FieldByName('DeptName').Value:='长阳总仓';
          FieldByName('FactoryNo').Value:=Trim(CDS_Sub.fieldbyname('FactoryNo').AsString);
          FieldByName('MainId').Value:=Trim(CDS_Sub.fieldbyname('MainId').AsString);
          FieldByName('SubId').Value:=Trim(CDS_Sub.fieldbyname('SubId').AsString);
          FieldByName('KHName').Value:=Trim(CDS_Sub.fieldbyname('FromKHName').AsString);
          FieldByName('CRFlag').Value:='入库';
          FieldByName('CRType').Value:=Trim(FCRType);
          FieldByName('CRQtyFlag').Value:=1;
          RTSetSaveDataCDS(ADOQueryCmd,Tv1,CDS_Sub,'CK_SXPB_CR',2);
          if Trim(FFSPID)='' then
            FieldByName('Filler').Value:=Trim(DName)
          else
          begin
            FieldByName('Editer').Value:=Trim(DName);
            FieldByName('EditTime').Value:=SGetServerDate(ADOQueryTemp);
          end;
          FieldByName('SPNO').Value:=Trim(FSPNO);
          Post;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update CK_SXPB_CR Set MXKCQty=(select sum(isnull(Qty,0)*CRQtyFlag) from CK_SXPB_CR A where A.FZSPID=CK_SXPB_CR.SPID)');
          sql.Add(',MXKCPiQty=(select sum(isnull(PiQty,0)*CRQtyFlag) from CK_SXPB_CR A where A.FZSPID=CK_SXPB_CR.SPID)');
          sql.Add(' where SPID='''+Trim(Maxno)+'''');
          ExecSQL;
        end;
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from CK_SXPB_CR where SPID='''+Trim(Maxno)+'''');
          Open;
        end;
        if ADOQueryTemp.FieldByName('MXKCQty').Value<0 then
        begin
          CDS_Sub.DisableControls;
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('入库数小于出库数不能保存!','提示',0);
          Exit;
        end;
        Edit;
        FieldByName('SPID').Value:=Trim(Maxno);
        FieldByName('SPNO').Value:=Trim(FSPNO);
        Post;
        Next;
      end;
    end;
    CDS_Sub.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    Result:=False;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存异常!','提示',0);
  end;

end;
procedure TfrmCYBCPCKInPut.TBCloseClick(Sender: TObject);
begin
  //WriteCxGrid(Self.Caption+'入库登记'+Trim(FCRType),Tv1,Self.Caption);
  WriteCxGrid('半成品入库登记'+Trim(FCRType),Tv1,'半成品仓库');
  Close;
end;
procedure TfrmCYBCPCKInPut.InitGridStatus();
begin
  if (Trim(FCRType)='加工完成入库') or (Trim(FCRType)='加工未做回仓')
     or (Trim(FCRType)='回修完成入库') or (Trim(FCRType)='回修未做回仓') then
  begin
    v1FtyName.Visible:=False;
    v1FtyName.Hidden:=True;
    v1FromFactoryName.Visible:=True;
    v1FromFactoryName.Hidden:=False;
    v1FromKHName.Visible:=False;
    v1FromKHName.Hidden:=True;
  end else
  if Trim(FCRType)='采购入库' then
  begin
    v1FtyName.Visible:=True;
    v1FtyName.Hidden:=False;
    v1FromFactoryName.Visible:=False;
    v1FromFactoryName.Hidden:=True;
    v1FromKHName.Visible:=False;
    v1FromKHName.Hidden:=True;
  end else
  if Trim(FCRType)='客户退回' then
  begin
    v1FtyName.Visible:=False;
    v1FtyName.Hidden:=True;
    v1FromFactoryName.Visible:=False;
    v1FromFactoryName.Hidden:=True;
    v1FromKHName.Visible:=True;
    v1FromKHName.Hidden:=false;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from CK_SXPB_CR where FZSPID='''+Trim(CDS_Sub.fieldbyname('SPID').AsString)+'''');
    sql.Add(' and CRQtyFlag=-1');
    Open;
  end;
  if ADOQueryTemp.IsEmpty=False then
  begin
    v1FtyName.Options.Focusing:=False;
    v1FromKHName.Options.Focusing:=False;
    v1FromFactoryName.Options.Focusing:=False;
    v1C1.Options.Focusing:=False;
    v1C2.Options.Focusing:=False;
    v1C3.Options.Focusing:=False;
    v1C4.Options.Focusing:=False;
    v1C5.Options.Focusing:=False;
    v1C6.Options.Focusing:=False;
    v1C7.Options.Focusing:=False;
    v1C8.Options.Focusing:=False;
    v1C9.Options.Focusing:=False;
    v1C10.Options.Focusing:=False;
    v1C11.Options.Focusing:=False;
    v1C15.Options.Focusing:=False;
    v1C16.Options.Focusing:=False;
  end;
end;

procedure TfrmCYBCPCKInPut.FormShow(Sender: TObject);
begin
  //ReadCxGrid(Self.Caption+'入库登记'+Trim(FCRType),Tv1,Self.Caption);
  ReadCxGrid('半成品入库登记'+Trim(FCRType),Tv1,'半成品仓库');
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(' select A.* ');
    sql.Add(' from CK_SXPB_CR A');
    sql.Add(' where SPID='''+Trim(FBCId)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_Sub);
  SInitCDSData20(ADOQueryTemp,CDS_Sub);
  InitGridStatus();
end;

procedure TfrmCYBCPCKInPut.TBDelClick(Sender: TObject);
begin
  if CDS_Sub.IsEmpty then Exit;
  if Trim(CDS_Sub.fieldbyname('SPID').AsString)<>'' then
  begin
    if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from CK_SXPB_CR where FZSPID='''+Trim(CDS_Sub.fieldbyname('SPID').AsString)+'''');
      sql.Add(' and CRQtyFlag=-1');
      Open;
    end;
    if ADOQueryTemp.IsEmpty=False then
    begin
      Application.MessageBox('已有出库记录，不能删除!','提示',0);
      Exit;
    end;
    try
      ADOQueryCmd.Connection.BeginTrans;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add(' delete CK_SXPB_CR where SPID='''+Trim(CDS_Sub.fieldbyname('SPID').AsString)+'''');
        ExecSQL;
      end;
      ADOQueryCmd.Connection.CommitTrans;
      CDS_Sub.Delete;
    except
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('删除异常!','提示',0);
    end;

  end else
  begin
    CDS_Sub.Delete;
  end;

end;

procedure TfrmCYBCPCKInPut.TBSaveClick(Sender: TObject);
begin
  if CDS_Sub.IsEmpty then Exit;
  if CDS_Sub.Locate('CRTime',null,[])=True then
  begin
    Application.MessageBox('码单日期不能为空!','提示',0);
    Exit;
  end;
  if CDS_Sub.Locate('PSName',null,[])=True then
  begin
    Application.MessageBox('工序不能为空!','提示',0);
    Exit;
  end;
  if CDS_Sub.Locate('SPName',null,[])=True then
  begin
    Application.MessageBox('品名不能为空!','提示',0);
    Exit;
  end;
  if (Trim(FCRType)='加工完成入库') or (Trim(FCRType)='加工未做回仓') then
  begin
    if CDS_Sub.Locate('FromFactoryName',null,[])=True then
    begin
      Application.MessageBox(PChar(Trim(v1FromFactoryName.Caption)+'不能为空!'),'提示',0);
      Exit;
    end;
  end;
  if (Trim(FCRType)='回修完成入库') or (Trim(FCRType)='回修未做回仓') then
  begin
    if CDS_Sub.Locate('FromFactoryName',null,[])=True then
    begin
      Application.MessageBox(PChar(Trim(v1FromFactoryName.Caption)+'不能为空!'),'提示',0);
      Exit;
    end;
  end;
  if Trim(FCRType)='采购入库' then
  begin
    if CDS_Sub.Locate('FactoryName',null,[])=True then
    begin
      Application.MessageBox(PChar(Trim(v1FtyName.Caption)+'不能为空!'),'提示',0);
      Exit;
    end;
  end;
  if Trim(FCRType)='客户退回' then
  begin
    if CDS_Sub.Locate('FromKHName',null,[])=True then
    begin
      Application.MessageBox(PChar(Trim(v1FromKHName.Caption)+'不能为空!'),'提示',0);
      Exit;
    end;
  end;
  if CDS_Sub.Locate('PiQty',null,[])=True then
  begin
    Application.MessageBox('匹数不能为空!','提示',0);
    Exit;
  end;
  if CDS_Sub.Locate('Qty',null,[])=True then
  begin
    Application.MessageBox('数量不能为空!','提示',0);
    Exit;
  end;
  if CDS_Sub.Locate('QtyUnit',null,[])=True then
  begin
    Application.MessageBox('数量单位不能为空!','提示',0);
    Exit;
  end;
  if SaveCKData() then
  begin
    Application.MessageBox('保存成功!','提示',0);
    //ModalResult:=1;
    Exit;
  end;
end;

procedure TfrmCYBCPCKInPut.Tv1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var
  fsj,FName:string;
begin
  if CDS_Sub.IsEmpty then Exit;
  FName:=Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName;
  if Trim(FName)='PSName' then
  begin
    fsj:='select Code='''',name=B.PSName from JYOrder_Process B '
         +' where RTrim(B.MainId)='''+Trim(CDS_Sub.fieldbyname('MainId').AsString)+'''';
    SInitCxGridComboBoxBySql(ADOQueryTemp,v1PSName,fsj,0,True,'');
    if ADOQueryTemp.IsEmpty then
    begin
      Application.MessageBox('没有对应工序!','提示',0);
    end;
    exit;
  end;
  if (Trim(FCRType)='加工完成入库') or (Trim(FCRType)='加工未做回仓') then
  begin
    if Trim(FName)<>'FromFactoryName' then Exit;
    fsj:='select Code='''',name=FactoryName from JYOrder_PCS_Sub A '
         +' inner join JYOrder_Process B on A.PSID=B.PSID '
         +' where A.SubId='''+Trim(CDS_Sub.fieldbyname('SubId').AsString)+''''
         +' and isnull(B.PSName,'''') not in(''检验包装'') and isnull(A.FactoryName,'''')<>'''''
         +' Group by A.FactoryName';
    SInitCxGridComboBoxBySql(ADOQueryTemp,v1FromFactoryName,fsj,0,True,'');
    if ADOQueryTemp.IsEmpty then
    begin
      Application.MessageBox('检验包装工序没有安排加工厂!','提示',0);
      Exit;
    end;
  end else
  if Trim(FCRType)='客户退回' then
  begin
    if Trim(FName)<>'FromKHName' then Exit;
    fsj:='select Code='''',name=CustomerNoName from JYOrder_Main A '
         +' inner join JYOrder_Main '
         +' where A.MainId='''+Trim(CDS_Sub.fieldbyname('MainId').AsString)+'''';
    SInitCxGridComboBoxBySql(ADOQueryTemp,v1FromKHName,fsj,0,True,'');
  end else
  if Trim(FCRType)='采购入库' then
  begin
    if Trim(FName)<>'FactoryName' then Exit;
    fsj:='select Code='''',name=FactoryName from JYOrder_PCS_Sub A '
         +' inner join JYOrder_Process B on A.PSID=B.PSID '
         +' where A.SubId='''+Trim(CDS_Sub.fieldbyname('SubId').AsString)+''''
         +' and isnull(B.PSName,'''')not in(''检验包装'') and isnull(A.FactoryName,'''')<>'''' '
         +' Group by A.FactoryName';
    SInitCxGridComboBoxBySql(ADOQueryTemp,v1FtyName,fsj,0,True,'');
    if ADOQueryTemp.IsEmpty then
    begin
      Application.MessageBox('工序没有安排采购供应商!','提示',0);
      Exit;
    end;
  end;
end;

procedure TfrmCYBCPCKInPut.v1C15PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='KuWei';
      flagname:='库位';
      if ShowModal=1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('KuWei').Value:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmCYBCPCKInPut.TBAddClick(Sender: TObject);
begin
  try
    frmProductOrderListSel:=TfrmProductOrderListSel.Create(Application);
    with frmProductOrderListSel do
    begin
      if ShowModal=1 then
      begin
        frmProductOrderListSel.Order_Main.DisableControls;
        with frmProductOrderListSel.Order_Main do
        begin
          First;
          while frmProductOrderListSel.Order_Main.Locate('SSel',True,[]) do
          begin
            with Self.CDS_Sub do
            begin
              Append;
              FieldByName('MainId').Value:=Trim(frmProductOrderListSel.Order_Main.fieldbyname('MainId').AsString);
              FieldByName('SubId').Value:=Trim(frmProductOrderListSel.Order_Main.fieldbyname('SubId').AsString);
              FieldByName('OrderNo').Value:=Trim(frmProductOrderListSel.Order_Main.fieldbyname('OrderNo').AsString);
              FieldByName('SPName').Value:=Trim(frmProductOrderListSel.Order_Main.fieldbyname('MPRTCodeName').AsString);
              FieldByName('SPCode').Value:=Trim(frmProductOrderListSel.Order_Main.fieldbyname('MPRTCode').AsString);
              FieldByName('SPSpec').Value:=Trim(frmProductOrderListSel.Order_Main.fieldbyname('MPRTSpec').AsString);
              FieldByName('SPCF').Value:=Trim(frmProductOrderListSel.Order_Main.fieldbyname('MPRTCF').AsString);
              FieldByName('SPMF').Value:=Trim(frmProductOrderListSel.Order_Main.fieldbyname('MPRTMF').AsString);
              FieldByName('SPKZ').Value:=Trim(frmProductOrderListSel.Order_Main.fieldbyname('MPRTKZ').AsString);
              FieldByName('SPColor').Value:=Trim(frmProductOrderListSel.Order_Main.fieldbyname('PRTColor').AsString);
              FieldByName('SPHX').Value:=Trim(frmProductOrderListSel.Order_Main.fieldbyname('PRTHX').AsString);
              FieldByName('SPStyleNo').Value:=Trim(frmProductOrderListSel.Order_Main.fieldbyname('StyleNo').AsString);
              FieldByName('SPColorNo').Value:=Trim(frmProductOrderListSel.Order_Main.fieldbyname('PRTColorNo').AsString);
              FieldByName('SPColorEng').Value:=Trim(frmProductOrderListSel.Order_Main.fieldbyname('PRTColorEng').AsString);
              FieldByName('SPGY').Value:=Trim(frmProductOrderListSel.Order_Main.fieldbyname('MPRTGY').AsString);
              FieldByName('SPGYHZ').Value:=Trim(frmProductOrderListSel.Order_Main.fieldbyname('MPRTGYHZ').AsString);
              FieldByName('QtyUnit').Value:=Trim(frmProductOrderListSel.Order_Main.fieldbyname('OrderUnit').AsString);
            end;
            Delete;
          end;
        end;
        frmProductOrderListSel.Order_Main.EnableControls;
      end;
    end;
  finally
    frmProductOrderListSel.Free;
  end;
end;

end.
