unit U_CYPBCKInPutW;

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
  TfrmCYPBCKInPutW = 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;
    Label1: TLabel;
    TBAdd: TToolButton;
    v1FromFtyName: TcxGridDBColumn;
    v1PBNO: TcxGridDBColumn;
    v1FromKHName: 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 v1C15PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure TBAddClick(Sender: TObject);
    procedure v1FtyNamePropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1FromFtyNamePropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1FromKHNamePropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
  private
    { Private declarations }
    function SaveCKData():Boolean;
    procedure InitGridStatus();
  public
    { Public declarations }
    FBCId,FCRType:String;

  end;

var
  frmCYPBCKInPutW: TfrmCYPBCKInPutW;

implementation
uses
  U_DataLink,U_RTFun,U_ZDYHelp,U_ProductOrderListSel,U_GYSList,U_KHList;

{$R *.dfm}

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

procedure TfrmCYPBCKInPutW.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 TfrmCYPBCKInPutW.SaveCKData():Boolean;
var
  FCRID,Maxno,FFSPID,FSPNO,FFlag:string;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    if Trim(FCRType)='采购入库' then
    begin
      FFlag:='CG';
    end else
    if Trim(FCRType)='织造入库' then
    begin
      FFlag:='ZZ';
    end else
    if Trim(FCRType)='半加工完成入库' then
    begin
      FFlag:='BW';
    end else
    if Trim(FCRType)='半加工未做回仓' then
    begin
      FFlag:='BH';
    end else
    if Trim(FCRType)='客户退回' then
    begin
      FFlag:='KT';
    end;
    CDS_Sub.DisableControls;
    with CDS_Sub do
    begin
      First;
      while not eof do
      begin
        if Trim(CDS_Sub.fieldbyname('PBNO').AsString)<>'' then
        begin
          with ADOQueryTemp do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from Cloth_Main where isnull(ConNo,'''')='''+Trim(CDS_Sub.fieldbyname('PBNO').AsString)+'''');
            open;
          end;
          if ADOQueryTemp.IsEmpty=True then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            CDS_Sub.EnableControls;
            Application.MessageBox(PChar('PB单号:'+Trim(CDS_Sub.fieldbyname('PBNO').AsString)+' 不存在!'),'提示',0);
            Exit;
          end;
        end;
        if Trim(CDS_Sub.fieldbyname('OrderNo').AsString)<>'' then
        begin
          with ADOQueryTemp do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from JYOrder_Main where isnull(OrderNo,'''')='''+Trim(CDS_Sub.fieldbyname('OrderNo').AsString)+'''');
            open;
          end;
          if ADOQueryTemp.IsEmpty=True then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            CDS_Sub.EnableControls;
            Application.MessageBox(PChar('订单号:'+Trim(CDS_Sub.fieldbyname('OrderNo').AsString)+' 不存在!'),'提示',0);
            Exit;
          end;
        end;
        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:='PR'+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('KHName').Value:=Trim(CDS_Sub.fieldbyname('KHName').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 TfrmCYPBCKInPutW.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('坯布入库登记'+Trim(FCRType),Tv1,Self.Caption);
  Close;
end;
procedure TfrmCYPBCKInPutW.InitGridStatus();
begin
  if Trim(FCRType)='采购入库' then
  begin
    v1FtyName.Visible:=True;
    v1FtyName.Hidden:=false;
    v1FromFtyName.Visible:=False;
    v1FromFtyName.Hidden:=True;
    v1FromKHName.Visible:=False;
    v1FromKHName.Hidden:=True;
  end else
  if Trim(FCRType)='织造入库' then
  begin
    v1FtyName.Visible:=True;
    v1FtyName.Hidden:=false;
    v1FromFtyName.Visible:=False;
    v1FromFtyName.Hidden:=True;
    v1FromKHName.Visible:=False;
    v1FromKHName.Hidden:=True;
  end else
  if Trim(FCRType)='半加工完成入库' then
  begin
    v1FtyName.Visible:=True;
    v1FtyName.Hidden:=false;
    v1FromFtyName.Visible:=True;
    v1FromFtyName.Hidden:=false;
    v1FromFtyName.Caption:='加工厂';
    v1FromKHName.Visible:=False;
    v1FromKHName.Hidden:=True;
  end else
  if Trim(FCRType)='半加工未做回仓' then
  begin
    v1FtyName.Visible:=True;
    v1FtyName.Hidden:=false;
    v1FromFtyName.Visible:=True;
    v1FromFtyName.Hidden:=false;
    v1FromFtyName.Caption:='加工厂';
    v1FromKHName.Visible:=False;
    v1FromKHName.Hidden:=True;
  end else
  if Trim(FCRType)='再加工未做回仓' then
  begin
    v1FtyName.Visible:=True;
    v1FtyName.Hidden:=false;
    v1FromFtyName.Visible:=True;
    v1FromFtyName.Hidden:=false;
    v1FromFtyName.Caption:='加工厂';
    v1FromKHName.Visible:=False;
    v1FromKHName.Hidden:=True;
  end else
  if Trim(FCRType)='客户退回' then
  begin
    v1FtyName.Visible:=True;
    v1FtyName.Hidden:=false;
    v1FromFtyName.Visible:=False;
    v1FromFtyName.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;
    v1FromFtyName.Options.Focusing:=False;
    v1PBNO.Options.Focusing:=False;
    v1FromKHName.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;
    v1C15.Options.Focusing:=False;
    v1C16.Options.Focusing:=False;
  end;
end;

procedure TfrmCYPBCKInPutW.FormShow(Sender: TObject);
begin
  ReadCxGrid('坯布入库登记'+Trim(FCRType),Tv1,Self.Caption);
  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 TfrmCYPBCKInPutW.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 TfrmCYPBCKInPutW.TBSaveClick(Sender: TObject);
begin
  if CDS_Sub.IsEmpty then Exit;
  if CDS_Sub.Locate('OrderNo',null,[])=True then
  begin
    Application.MessageBox('订单号不能为空!','提示',0);
    Exit;
  end;
  if CDS_Sub.Locate('CRTime',null,[])=True then
  begin
    Application.MessageBox('码单日期不能为空!','提示',0);
    Exit;
  end;
  if CDS_Sub.Locate('SPName',null,[])=True then
  begin
    Application.MessageBox('品名不能为空!','提示',0);
    Exit;
  end;
  if CDS_Sub.Locate('FactoryName',null,[])=True then
  begin
    Application.MessageBox(PChar(Trim(v1FtyName.Caption)+'不能为空!'),'提示',0);
    Exit;
  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 (Trim(FCRType)='半加工完成入库') or (Trim(FCRType)='半加工未做回仓') then
  begin
    if CDS_Sub.Locate('FactoryName',null,[])=True then
    begin
      Application.MessageBox(PChar(Trim(v1FromFtyName.Caption)+'不能为空!'),'提示',0);
      Exit;
    end;
  end;
  if Trim(FCRType)='织造入库' then
  begin
    if CDS_Sub.Locate('PBNO',null,[])=True then
    begin
      Application.MessageBox(PChar(Trim(v1PBNO.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 CDS_Sub.Locate('ToFactoryName',null,[])=True then
  begin
    Application.MessageBox('存放地点不能为空!','提示',0);
    Exit;
  end;
  if SaveCKData() then
  begin
    Application.MessageBox('保存成功!','提示',0);
    //ModalResult:=1;
    Exit;
  end;
end;

procedure TfrmCYPBCKInPutW.v1C15PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmGYSList:=TfrmGYSList.Create(Application);
    with frmGYSList do
    begin
      if ShowModal=1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('ToFactoryName').Value:=Trim(frmGYSList.Order_Main.fieldbyname('KHNameJC').AsString);
         // Post;
        end;
      end;
    end;
  finally
    frmGYSList.Free;
  end;
end;

procedure TfrmCYPBCKInPutW.TBAddClick(Sender: TObject);
begin
  with CDS_Sub do
  begin
    Append;
    Post;
  end;
end;

procedure TfrmCYPBCKInPutW.v1FtyNamePropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmGYSList:=TfrmGYSList.Create(Application);
    with frmGYSList do
    begin
      if ShowModal=1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('FactoryName').Value:=Trim(frmGYSList.Order_Main.fieldbyname('KHNameJC').AsString);
         // Post;
        end;
      end;
    end;
  finally
    frmGYSList.Free;
  end;
end;

procedure TfrmCYPBCKInPutW.v1FromFtyNamePropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmGYSList:=TfrmGYSList.Create(Application);
    with frmGYSList do
    begin
      if ShowModal=1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('FromFactoryName').Value:=Trim(frmGYSList.Order_Main.fieldbyname('KHNameJC').AsString);
          //Post;
        end;
      end;
    end;
  finally
    frmGYSList.Free;
  end;
end;

procedure TfrmCYPBCKInPutW.v1FromKHNamePropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmKHList:=TfrmKHList.Create(Application);
    with frmKHList do
    begin
      if ShowModal=1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('FromKHName').Value:=Trim(frmKHList.Order_Main.fieldbyname('KHNameJC').AsString);
          FieldByName('KHName').Value:=Trim(frmKHList.Order_Main.fieldbyname('KHNameJC').AsString);
          //Post;
        end;
      end;
    end;
  finally
    frmKHList.Free;
  end;
end;

end.
