unit U_ShaOutPut;

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, cxSpinEdit,
  StdCtrls, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator;

type
  TfrmShaOutPut = class(TForm)
    ToolBar1: TToolBar;
    TBAdd: TToolButton;
    TBDel: TToolButton;
    TBSave: TToolButton;
    TBClose: TToolButton;
    DataSource3: TDataSource;
    CDS_Sub: TClientDataSet;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    cxGridPopupMenu2: TcxGridPopupMenu;
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column6: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1CRType: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    v1Column1: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBAddClick(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 v1Column7PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column9PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v2Column6PropertiesEditValueChanged(Sender: TObject);
    procedure v1Column15PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
  private
    { Private declarations }
    function SaveCKData(): Boolean;
  public
    { Public declarations }
    FBCId: string;
  end;

var
  frmShaOutPut: TfrmShaOutPut;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_ShaKCSelList, U_KHList;

{$R *.dfm}

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

procedure TfrmShaOutPut.TBAddClick(Sender: TObject);
begin
  try
    frmShaKCSelList := TfrmShaKCSelList.Create(Application);
    with frmShaKCSelList do
    begin
      ToolButton1.Visible := True;
      if ShowModal = 1 then
      begin
        frmShaKCSelList.CDS_Main.DisableControls;
        with frmShaKCSelList.CDS_Main do
        begin
          First;
          while frmShaKCSelList.CDS_Main.Locate('SSel', True, []) do
          begin
            with Self.CDS_Sub do
            begin
              Append;
              FieldByName('CRTime').Value := Trim(FormatDateTime('yyyy-MM-dd', Now));
              FieldByName('FactoryName').Value := frmShaKCSelList.CDS_Main.fieldbyname('FactoryName').Value;
              FieldByName('FactoryNo').Value := frmShaKCSelList.CDS_Main.fieldbyname('FactoryNo').Value;
              FieldByName('SPName').Value := frmShaKCSelList.CDS_Main.fieldbyname('SPName').Value;
              FieldByName('SPCode').Value := frmShaKCSelList.CDS_Main.fieldbyname('SPCode').Value;
              FieldByName('CPID').Value := frmShaKCSelList.CDS_Main.fieldbyname('CPID').Value;
              FieldByName('SPSpec').Value := frmShaKCSelList.CDS_Main.fieldbyname('SPSpec').Value;
              FieldByName('SXPinPai').Value := frmShaKCSelList.CDS_Main.fieldbyname('SXPinPai').Value;
              FieldByName('QtyUnit').Value := frmShaKCSelList.CDS_Main.fieldbyname('QtyUnit').Value;
              FieldByName('Qty').Value := frmShaKCSelList.CDS_Main.fieldbyname('MXKCQty').Value;
              FieldByName('FZSPID').Value := frmShaKCSelList.CDS_Main.fieldbyname('SPID').Value;
              FieldByName('BatchNo').Value := frmShaKCSelList.CDS_Main.fieldbyname('BatchNo').Value;
              FieldByName('CRType').Value := '正常出库';
              FieldByName('Price').Value := frmShaKCSelList.CDS_Main.fieldbyname('Price').Value;
              FieldByName('Money').Value := frmShaKCSelList.CDS_Main.fieldbyname('KCMoney').Value;
            end;
            frmShaKCSelList.CDS_Main.Delete;
          end;
        end;
        frmShaKCSelList.CDS_Main.EnableControls;

      end;
    end;
  finally
    frmShaKCSelList.Free;
  end;
end;

function TfrmShaOutPut.SaveCKData(): Boolean;
var
  FCRID, Maxno, FSPID, MaxPYNo, FPYID, FFString: string;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    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;
        FSPID := Trim(ADOQueryTemp.fieldbyname('SPID').AsString);
        if Trim(FSPID) = '' then
        begin
          if GetLSNo(ADOQueryCmd, Maxno, 'JC', 'CK_SXPB_CR', 3, 1) = False then
          begin
            CDS_Sub.EnableControls;
            Result := False;
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取机物料出库编号失败!', '提示', 0);
            Exit;
          end;
        end
        else
        begin
          Maxno := Trim(FSPID);
        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(FSPID) = '' then
          begin
            Append;
          end
          else
          begin
            Edit;
          end;
          FieldByName('SPID').Value := Trim(Maxno);
          FieldByName('FZSPID').Value := Trim(CDS_Sub.fieldbyname('FZSPID').AsString);
          FieldByName('FactoryNo').Value := Trim(CDS_Sub.fieldbyname('FactoryNo').AsString);
          FieldByName('FactoryName').Value := Trim(CDS_Sub.fieldbyname('FactoryName').AsString);
          FieldByName('ToFactoryNo').Value := Trim(CDS_Sub.fieldbyname('ToFactoryNo').AsString);
          FieldByName('CPID').Value := Trim(CDS_Sub.fieldbyname('CPID').AsString);
          FieldByName('CRFlag').Value := '出库';
          FieldByName('CRQtyFlag').Value := -1;
          RTSetSaveDataCDS(ADOQueryCmd, Tv1, CDS_Sub, 'CK_SXPB_CR', 2);
          FieldByName('CKName').Value := '机物料';
          if Trim(FSPID) = '' then
            FieldByName('Filler').Value := Trim(DName)
          else
          begin
            FieldByName('Editer').Value := Trim(DName);
            FieldByName('EditTime').Value := SGetServerDate(ADOQueryTemp);
          end;
          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 and A.ZFFlag=0)');
          sql.Add(',MXKCPiQty=(select sum(isnull(PiQty,0)*CRQtyFlag) from CK_SXPB_CR A where A.FZSPID=CK_SXPB_CR.SPID and A.ZFFlag=0)');
          sql.Add(' where SPID=''' + Trim(CDS_Sub.fieldbyname('FZSPID').AsString) + '''');
          ExecSQL;
        end;
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from CK_SXPB_CR where SPID=''' + Trim(CDS_Sub.fieldbyname('FZSPID').AsString) + '''');
          Open;
        end;
        if ADOQueryTemp.FieldByName('MXKCQty').Value < 0 then
        begin
          FFString := Trim(CDS_Sub.fieldbyname('FZSPID').AsString);
          CDS_Sub.EnableControls;
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('出库数大于库存数不能保存!', '提示', 0);
          CDS_Sub.Locate('FZSPID', FFString, []);
          Exit;
        end;
        if Trim(CDS_Sub.fieldbyname('CRType').AsString) = '销售出库' then
        begin
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add(' exec P_ZDYS_JWL @SPID=''' + Trim(Maxno) + '''');
//          ShowMessage(SQL.text);
            ExecSQL;
          end;
        end;

        Edit;
        FieldByName('SPID').Value := Trim(Maxno);
        Post;

        Next;
      end;
    end;
    CDS_Sub.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    Result := True;
  except
    Result := False;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存异常!', '提示', 0);
  end;

end;

procedure TfrmShaOutPut.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('机物料出库登记', Tv1, '机物料仓库');
  Close;
end;

procedure TfrmShaOutPut.FormShow(Sender: TObject);
begin
  ReadCxGrid('机物料出库登记', 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);
end;

procedure TfrmShaOutPut.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;
    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) + '''');
        sql.Add('Update CK_SXPB_CR Set MXKCQty=(select sum(isnullQty,0)*CRQtyFlag) from CK_SXPB_CR A where A.FZSPID=CK_SXPB_CR.SPID and A.ZFFlag=0)');
        sql.Add(',MXKCPiQty=(select sum(isnull(PiQty,0)*CRQtyFlag) from CK_SXPB_CR A where A.FZSPID=CK_SXPB_CR.SPID and A.ZFFlag=0 )');
        sql.Add(' where SPID=''' + Trim(CDS_Sub.fieldbyname('FZSPID').AsString) + '''');
        ExecSQL;
      end;
      ADOQueryCmd.Connection.CommitTrans;
    except
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('删除异常!', '提示', 0);
    end;

  end;
  CDS_Sub.Delete;
end;

procedure TfrmShaOutPut.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('CRType', null, []) = True then
  begin
    Application.MessageBox('出库类型不能为空!', '提示', 0);
    Exit;
  end;
  if CDS_Sub.Locate('Qty', null, []) = True then
  begin
    Application.MessageBox('数量不能为空!', '提示', 0);
    Exit;
  end;
  with CDS_Sub do
  begin
    First;
    while not Eof do
    begin
      if FieldByName('CRType').AsString = '销售出库' then
      begin
        if FieldByName('ToFactoryName').AsString = '' then
        begin
          Application.MessageBox('出到单位不能为空!', '提示', 0);
          Exit;
        end;
      end;
      Next;
    end;
  end;

  if SaveCKData() then
  begin
    Application.MessageBox('保存成功!', '提示', 0);
    //ModalResult:=1;
    Exit;
  end;
end;

procedure TfrmShaOutPut.Tv1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
  FFSPID: string;
begin
  if CDS_Sub.IsEmpty then
    Exit;
  if Trim(CDS_Sub.fieldbyname('SPID').AsString) <> '' then
  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);
    if Trim(FFSPID) <> '' then
    begin
      v1CRType.Options.Editing := False;
    end
    else
    begin
      v1CRType.Options.Editing := True;
    end;
  end
  else
  begin
    v1CRType.Options.Editing := True;
  end;
end;

procedure TfrmShaOutPut.v1Column7PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'llPerson';
      flagname := '领料人';
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('llPerson').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmShaOutPut.v1Column9PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'llDepartment';
      flagname := '领用部门';
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('llDepartment').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmShaOutPut.v2Column6PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FQty, FPrice, FCostPrice, FMoney, FBBMoney, FFieldName: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  if Trim(mvalue) = '' then
  begin
    mvalue := '0';
  end;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_Sub do
  begin
    Edit;
    FieldByName(FFieldName).Value := mvalue;
    Post;
  end;
  FQty := Trim(CDS_Sub.fieldbyname('Qty').AsString);
  if Trim(FQty) = '' then
    FQty := '0';

  FPrice := Trim(CDS_Sub.fieldbyname('Price').AsString);
  if Trim(FPrice) = '' then
    FPrice := '0';

  FCostPrice := Trim(CDS_Sub.fieldbyname('CostPrice').AsString);
  if Trim(FCostPrice) = '' then
    FCostPrice := '0';

  FMoney := Trim(CDS_Sub.fieldbyname('Money').AsString);
  if Trim(FMoney) = '' then
    FMoney := '0';

  FMoney := FloatToStr(StrToFloat(FQty) * StrToFloat(FPrice) + StrToFloat(FCostPrice));
  with CDS_Sub do
  begin
    Edit;
    FieldByName('Money').Value := StrToFloat(FMoney);
    Post;
  end;

  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmShaOutPut.v1Column15PropertiesButtonClick(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('ToFactoryNo').Value := Trim(Order_Main.fieldbyname('CoCode').AsString);
          FieldByName('ToFactoryName').Value := Trim(Order_Main.fieldbyname('CoName').AsString);
            //Post;
        end;
      end;
    end;
  finally
    frmKHList.Free;
  end;

end;

end.

