unit U_TradeClothTotalCXJYOutList;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Vcl.Printers, Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, cxGridCustomTableView, cxGridTableView,
  cxGridBandedTableView, cxGridDBBandedTableView, cxGridLevel, cxClasses,
  cxControls, cxGridCustomView, cxGridDBTableView, cxGrid, StdCtrls, ComCtrls,
  ExtCtrls, ToolWin, cxGridCustomPopupMenu, cxGridPopupMenu, ADODB, DBClient,
  cxDropDownEdit, cxCheckBox, RM_Common, RM_Class, RM_e_Xls, RM_Dataset,
  RM_System, RM_GridReport, Menus, cxCalendar, cxButtonEdit, cxTextEdit, cxPC,
  cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, dxDateRanges,
  dxBarBuiltInMenu, U_BaseList, Clipbrd, dxScrollbarAnnotations, FrameDateSel,
  cxContainer, cxMaskEdit, cxProgressBar, cxButtons, dxSkinsCore,
  dxSkinsDefaultPainters;

type
  TfrmTradeClothTotalCXJYOutList = class(TfrmBaseList)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBExport: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    GPM_1: TcxGridPopupMenu;
    CDS_Main: TClientDataSet;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column6: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    Label3: TLabel;
    Label5: TLabel;
    Label8: TLabel;
    Label12: TLabel;
    C_NAME: TcxTextEdit;
    BCIONO: TcxTextEdit;
    C_spec: TcxTextEdit;
    v1Column20: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    ToolButton1: TToolButton;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    Tv1Column9: TcxGridDBColumn;
    N3: TMenuItem;
    Label4: TLabel;
    Label9: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    FromCoName: TcxTextEdit;
    ToCoName: TcxTextEdit;
    ToOrdNo: TcxTextEdit;
    FromOrdNo: TcxTextEdit;
    frmFrameDateSel1: TfrmFrameDateSel;
    IOType: TcxComboBox;
    Label1: TLabel;
    Label2: TLabel;
    GC_Name: TcxTextEdit;
    CraftCode: TcxTextEdit;
    Tv1Column30: TcxGridDBColumn;
    ADOQueryPrint: TADOQuery;
    cxTabControl1: TcxTabControl;
    Tv1Column33: TcxGridDBColumn;
    Tv1Column34: TcxGridDBColumn;
    Tv1Column35: TcxGridDBColumn;
    cxgrdbclmnTv1Column1: TcxGridDBColumn;
    DS_2: TDataSource;
    CDS_2: TClientDataSet;
    ADO_2: TADOQuery;
    GPM_2: TcxGridPopupMenu;
    pnl1: TPanel;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    cxgrdbclmn1: TcxGridDBColumn;
    cxgrdbclmnTV2Column1: TcxGridDBColumn;
    cxgrdbclmnTV_2Column2: TcxGridDBColumn;
    cxgrdbclmnTV_2Column1: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    tlb1: TToolBar;
    btn1: TToolButton;
    btn2: TToolButton;
    cxgrdbclmnTv1Column2: TcxGridDBColumn;
    btn3: TToolButton;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ConNoMChange(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure SPNameChange(Sender: TObject);
    procedure BCIOIDChange(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure C_specChange(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure N3Click(Sender: TObject);
    procedure BCIOIDPropertiesChange(Sender: TObject);
    procedure Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure btn1Click(Sender: TObject);
    procedure btn2Click(Sender: TObject);
  private
    canshu1, FstkName, canshu3: string;
    procedure InitGrid();
    { Private declarations }
  public
    FFSPID, FC_Degree, FC_Pack: string;
    { Public declarations }
  end;

//var
 // frmPBOutList: TfrmPBOutList;

implementation

uses
  U_DataLink, U_RTFun, U_LabelPrint, U_BatchMdyData;

{$R *.dfm}

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

procedure TfrmTradeClothTotalCXJYOutList.FormCreate(Sender: TObject);
begin
  inherited;
  canshu1 := Trim(self.fParameters1);
  FSTKName := Trim(self.fParameters2);
  canshu3 := Trim(self.fParameters3);
  frmFrameDateSel1.EndDate.Date := SGetServerDate(ADOQueryTemp);
  frmFrameDateSel1.BegDate.Date := frmFrameDateSel1.EndDate.Date - 90;
end;

procedure TfrmTradeClothTotalCXJYOutList.InitGrid();
var
  SqlStr: string;
begin

  SqlStr := ' exec P_Trade_Insp_Recheck_View  ';

  SqlStr := SqlStr + ' @BegDate=''' + Trim(FormatDateTime('yyyy-MM-dd', frmFrameDateSel1.BegDate.Date)) + '''';
  SqlStr := SqlStr + ' ,@Enddate=''' + Trim(FormatDateTime('yyyy-MM-dd', frmFrameDateSel1.enddate.Date + 1)) + '''';


//  case cxTabControl1.TabIndex of
//    0:
//      begin
//        SqlStr := SqlStr + ' and NOT exists(select ciid  from Trade_Cloth_Inspect X where X.BCIOID=A.BCIOID)   ';
//      end;
//    1:
//      begin
//        SqlStr := SqlStr + ' and exists(select ciid  from Trade_Cloth_Inspect X where X.BCIOID=A.BCIOID)   ';
//      end;
//  end;


  InitCDSData(ADOQueryMain, CDS_Main, Tv1, SqlStr, SGetFilters(Panel1, 1, 2), 'BCIOID');

end;

procedure TfrmTradeClothTotalCXJYOutList.TBRafreshClick(Sender: TObject);
begin
  frmFrameDateSel1.BegDate.SetFocus;
  InitGrid();
end;

procedure TfrmTradeClothTotalCXJYOutList.ToolButton1Click(Sender: TObject);
begin
  WriteCxGrid(trim(Self.Caption), Tv1, '贸易汇总仓库');
end;

procedure TfrmTradeClothTotalCXJYOutList.Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
var
  SqlStr: string;
begin

  SqlStr := ' select A.BCIOID,A.MainID,A.CIID,A.GrossWeight,A.Tare,A.NetWeight,A.Meter,A.Yardage,PieceNo1=A.PieceNo,Qty1=A.Qty,Qty2= B.Qty  ';

  SqlStr := SqlStr + ' from Trade_Cloth_Inspect A  left join Trade_Cloth_Inspect_recheck B on A.CIID=B.CIID ';
  SqlStr := SqlStr + ' where A.BCIOID=''' + Trim(CDS_Main.FieldByName('BCIOID').AsString) + '''';

  InitCDSData(ADO_2, CDS_2, Tv2, SqlStr, '', '');

end;

procedure TfrmTradeClothTotalCXJYOutList.BCIOIDPropertiesChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmTradeClothTotalCXJYOutList.btn1Click(Sender: TObject);
var
  isOk: boolean;
  mtzfs: string;
  mtzsl, mvalue: double;
  decimalY: integer;
begin
  if CDS_2.IsEmpty then
    Exit;

  if CDS_2.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('请先选择数据记录', '提示', 0);
    Exit;
  end;

  with ADOQueryBaseTemp do
  begin
    Close;
    SQL.Clear;
    SQL.Add('select * from BS_Cloth_IO  A  ');

    sql.Add(' where  EXISTS(select X.RTValue from [dbo].[F_Tool_SplitString](' + quotedstr(SelCDSKey(CDS_2, ['cIId'])[0]) + ','','')  X where X.RTValue=A.StkId ) ');
    Open;

    if not IsEmpty then
    begin
      Application.MessageBox('已产生入库数据不能操作!', '提示', 0);
      exit;
    end;
  end;

  isOk := false;
  frmBatchMdyData := TfrmBatchMdyData.create(self);
  with frmBatchMdyData do
  begin
    fTabCaption := '调整米数';
    if ShowModal = 1 then
    begin
      mtzfs := Trim(tzfs.Text);
      mtzsl := strTofloatdef(Trim(tzsl.Text), 0);
      isOk := true;
    end;
    free;
  end;
  if not isOk then
    exit;

  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select * from Trade_Cloth_Inspect_Rule where  RuleID=' + quotedstr(Trim(CDS_2.fieldbyname('mainId').AsString)));
    Open;
    if not IsEmpty then
    begin
      if fieldByname('decimalY').AsInteger > 0 then
        decimalY := fieldByname('decimalY').AsInteger
      else
        decimalY := 1;
    end;

  end;
  try
    CDS_2.DisableControls;
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_2 do
    begin
      First;
      while not Eof do
      begin
        if CDS_2.FieldByName('SSel').AsBoolean = True then
        begin
          if mtzfs = '增加' then
          begin
            mvalue := CDS_2.fieldbyname('Meter').AsFloat + mtzsl;
          end;
          if mtzfs = '减少' then
          begin
            mvalue := CDS_2.fieldbyname('Meter').AsFloat - mtzsl;
          end;
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('Update Trade_Cloth_Inspect Set Meter=' + floatTostr(mvalue));
            sql.Add('where cIId=''' + Trim(CDS_2.fieldbyname('cIId').AsString) + '''');

            sql.Add('Update Trade_Cloth_Inspect Set yardage=round(meter/0.9144,' + inttostr(decimalY) + ')');
            sql.Add('where cIId=''' + Trim(CDS_2.fieldbyname('cIId').AsString) + '''');

            sql.Add('Update Trade_Cloth_Inspect Set Qty=CASE WHEN QtyUnit=''M'' THEN Meter WHEN QtyUnit=''Y'' THEN Yardage WHEN QtyUnit=''kg'' THEN GrossWeight ELSE 0 END ');
            sql.Add('where cIId=''' + Trim(CDS_2.fieldbyname('cIId').AsString) + '''');
            ExecSQL;
          end;
        end;
        Next;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    CDS_2.EnableControls;
    application.MessageBox('数据操作成功！', '提示信息');
    TBRafresh.Click;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    CDS_2.EnableControls;
    application.MessageBox('数据操作失败！', '提示信息', 0);
  end;
end;

procedure TfrmTradeClothTotalCXJYOutList.btn2Click(Sender: TObject);
var
  isOk: boolean;
  mtzfs: string;
  mtzsl, mvalue: double;
  decimalY: integer;
  mfield: string;
begin
  if CDS_2.IsEmpty then
    Exit;

  if CDS_2.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('请先选择数据记录', '提示', 0);
    Exit;
  end;
  with ADOQueryBaseTemp do
  begin
    Close;
    SQL.Clear;
    SQL.Add('select * from BS_Cloth_IO  A  ');

    sql.Add(' where  EXISTS(select X.RTValue from [dbo].[F_Tool_SplitString](' + quotedstr(SelCDSKey(CDS_2, ['cIId'])[0]) + ','','')  X where X.RTValue=A.StkId ) ');
    Open;

    if not IsEmpty then
    begin
      Application.MessageBox('已产生入库数据不能操作!', '提示', 0);
      exit;
    end;
  end;

  isOk := false;
  frmBatchMdyData := TfrmBatchMdyData.create(self);
  with frmBatchMdyData do
  begin
    fTabCaption := '独立调整重量';
    if ShowModal = 1 then
    begin
      mtzfs := Trim(tzfs1.Text);
      if Trim(tzfield.Text) = '毛重' then
      begin
        mfield := 'grossWeight';
      end
      else if Trim(tzfield.Text) = '净重' then
      begin
        mfield := 'netWeight';
      end
      else if Trim(tzfield.Text) = '皮重' then
      begin
        mfield := 'tare';
      end
      else
      begin
        free;
        exit;
      end;
      mtzsl := strTofloatdef(Trim(tzsl1.Text), 0);
      isOk := true;
    end;
    free;
  end;
  if not isOk then
    exit;

  try
    CDS_2.DisableControls;
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_2 do
    begin
      First;
      while not Eof do
      begin
        if CDS_2.FieldByName('SSel').AsBoolean = True then
        begin
          if mtzfs = '增加' then
          begin
            mvalue := CDS_2.fieldbyname(mfield).AsFloat + mtzsl;
          end;
          if mtzfs = '减少' then
          begin
            mvalue := CDS_2.fieldbyname(mfield).AsFloat - mtzsl;
          end;
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('Update Trade_Cloth_Inspect Set ' + mfield + '=' + floatTostr(mvalue));
            sql.Add('where cIId=''' + Trim(CDS_2.fieldbyname('cIId').AsString) + '''');

            sql.Add('Update Trade_Cloth_Inspect Set Qty=CASE WHEN QtyUnit=''M'' THEN Meter WHEN QtyUnit=''Y'' THEN Yardage WHEN QtyUnit=''kg'' THEN GrossWeight ELSE 0 END ');
            sql.Add('where cIId=''' + Trim(CDS_2.fieldbyname('cIId').AsString) + '''');
            ExecSQL;
          end;

        end;
        Next;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    CDS_2.EnableControls;
    application.MessageBox('数据操作成功！', '提示信息');
    TBRafresh.Click;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    CDS_2.EnableControls;
    application.MessageBox('数据操作失败！', '提示信息', 0);
  end;

end;

procedure TfrmTradeClothTotalCXJYOutList.ConNoMChange(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  end;
end;

procedure TfrmTradeClothTotalCXJYOutList.TBCloseClick(Sender: TObject);
begin

  Close;
end;

procedure TfrmTradeClothTotalCXJYOutList.FormShow(Sender: TObject);
var
  mSql: string;
begin
  inherited;
  mSql := 'select distinct name=A.IOType from BS_Cloth_IO A where IOFlag=''出库'' and isnull(STKNAME,'''')=''' + Trim(FSTKNAME) + ''' ';
  SInitTcxComBoxBySql(ADOQueryTemp, IOType, false, mSql);
  ReadCxGrid(trim(Self.Caption), Tv1, '贸易汇总仓库');
  InitGrid();
end;

procedure TfrmTradeClothTotalCXJYOutList.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    exit;
  TcxGridToExcel(FstkName + '出库列表', cxGrid1);
end;

procedure TfrmTradeClothTotalCXJYOutList.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS(ADOQueryMain, CDS_Main);
    SInitCDSData(ADOQueryMain, CDS_Main);
  end;
end;

procedure TfrmTradeClothTotalCXJYOutList.SPNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmTradeClothTotalCXJYOutList.BCIOIDChange(Sender: TObject);
begin
  if Length(Trim(BCIOno.Text)) < 4 then
  begin
    if Trim(BCIOno.Text) <> '' then
      Exit;
  end;
  TBFind.Click;
end;

procedure TfrmTradeClothTotalCXJYOutList.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_Main, True);
end;

procedure TfrmTradeClothTotalCXJYOutList.N2Click(Sender: TObject);
begin
  SelOKNo(CDS_Main, False);
end;

procedure TfrmTradeClothTotalCXJYOutList.N3Click(Sender: TObject);
begin
  Clipboard.SetTextBuf(PChar(Trim(CDS_Main.fieldbyname(TV1.Controller.FocusedColumn.DataBinding.FilterFieldName).AsString)));
end;

procedure TfrmTradeClothTotalCXJYOutList.C_specChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmTradeClothTotalCXJYOutList.cxTabControl1Change(Sender: TObject);
begin
  InitGrid();
end;

end.

