unit U_TJCKTHXSOK;

interface

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

type
  TfrmTJCKTHXSOK = class(TForm)
    Panel1: TPanel;
    Label3: TLabel;
    OrderNo: TEdit;
    cxGrid6: TcxGrid;
    Tv6: TcxGridDBBandedTableView;
    cxGridDBBandedColumn1: TcxGridDBBandedColumn;
    cxGridDBBandedColumn2: TcxGridDBBandedColumn;
    Tv6Column1: TcxGridDBBandedColumn;
    Tv6Column2: TcxGridDBBandedColumn;
    v6DutyPerson: TcxGridDBBandedColumn;
    v6DutyFactoryName: TcxGridDBBandedColumn;
    v6DutyReason: TcxGridDBBandedColumn;
    v6Column4: TcxGridDBBandedColumn;
    v6Column5: TcxGridDBBandedColumn;
    v6Column6: TcxGridDBBandedColumn;
    v6Column9: TcxGridDBBandedColumn;
    v6Column10: TcxGridDBBandedColumn;
    v6Column11: TcxGridDBBandedColumn;
    v6Column12: TcxGridDBBandedColumn;
    v6Column13: TcxGridDBBandedColumn;
    v6ToFactoryName: TcxGridDBBandedColumn;
    v6Column7: TcxGridDBBandedColumn;
    cxGridLevel3: TcxGridLevel;
    v6Column15: TcxGridDBBandedColumn;
    v6Column16: TcxGridDBBandedColumn;
    v6Column17: TcxGridDBBandedColumn;
    v6Column18: TcxGridDBBandedColumn;
    ClientDataSet6: TClientDataSet;
    DataSource6: TDataSource;
    ADOQuery1: TADOQuery;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Label1: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    v6Column20: TcxGridDBBandedColumn;
    v6Column21: TcxGridDBBandedColumn;
    cxTabControl1: TcxTabControl;
    ScrollBox3: TScrollBox;
    Button8: TButton;
    Button9: TButton;
    Button10: TButton;
    v6Column1: TcxGridDBBandedColumn;
    v6Column2: TcxGridDBBandedColumn;
    Label18: TLabel;
    MPRTCode: TEdit;
    Label19: TLabel;
    MPRTCodeName: TEdit;
    v6Column3: TcxGridDBBandedColumn;
    DataSource5: TDataSource;
    ClientDataSet5: TClientDataSet;
    v6Column14: TcxGridDBBandedColumn;
    v6Column19: TcxGridDBBandedColumn;
    ADOQueryMain: TADOQuery;
    Label21: TLabel;
    ClotheType10: TComboBox;
    cxGridPopupMenu1: TcxGridPopupMenu;
    cxGridPopupMenu4: TcxGridPopupMenu;
    Button13: TButton;
    cxGrid5: TcxGrid;
    Tv5: TcxGridDBTableView;
    cxGridDBColumn9: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    v5Column4: TcxGridDBColumn;
    v5Column1: TcxGridDBColumn;
    v5Column9: TcxGridDBColumn;
    v5Column6: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    v5Column3: TcxGridDBColumn;
    v6Column22: TcxGridDBBandedColumn;
    v5Column5: TcxGridDBColumn;
    v5Column7: TcxGridDBColumn;
    v5Column2: TcxGridDBColumn;
    v6Column23: TcxGridDBBandedColumn;
    Panel2: TPanel;
    Button11: TButton;
    Button2: TButton;
    Button1: TButton;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBRafreshClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure OrderNoKeyPress(Sender: TObject; var Key: Char);
    procedure Button10Click(Sender: TObject);
    procedure Button8Click(Sender: TObject);
    procedure Tv6CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure Button2Click(Sender: TObject);
    procedure v2FtyPropertiesEditValueChanged(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure Button9Click(Sender: TObject);
    procedure v3FtyPropertiesEditValueChanged(Sender: TObject);
    procedure Button13Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button11Click(Sender: TObject);
    procedure cxGridDBColumn9PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
  private
    { Private declarations }
    procedure InitGrid();
  public
    { Public declarations }
  end;

var
  frmTJCKTHXSOK: TfrmTJCKTHXSOK;

implementation
uses
  U_DataLink,U_RTFun, U_GYSList,U_ZDYHelp, U_ModuleNote;
{$R *.dfm}

procedure TfrmTJCKTHXSOK.FormDestroy(Sender: TObject);
begin
  frmTJCKTHXSOK:=nil;
end;

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

procedure TfrmTJCKTHXSOK.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
end;
procedure TfrmTJCKTHXSOK.InitGrid();
begin
  with ADOQueryMain do
  begin
    Filtered:=False;
    Close;
    sql.Clear;
    sql.Add('select JM.OrderNo, A.*,B.PSName OrdPSName,C.ConNo,C.FactoryNoName,D.Price, ');
    sql.Add(' JS.PRTHX,JS.PRTColor,JS.PRTColorNo,D.C_Code,D.C_CodeName,ClotheType10=A.ClotheType,');
    sql.Add(' JSKCPS=A.RKPS-A.CKPS,');
    sql.Add(' JSKCQty=A.RKQty-A.CKQty,');
    sql.Add(' JM.MPRTCodeName,MPRTCode=RTrim(JM.MPRTCode)+RTrim(JM.MPRTGY)');
    sql.Add(' from JYOrder_Main_MD  A');
    sql.Add(' inner join JYOrder_Main JM on A.OrdMainId=JM.MainId');
    sql.Add(' left join JYOrder_Sub JS on A.OrdSubId=JS.SubId');
    sql.Add(' inner join JYOrder_Process B on A.PSID=B.PSID');
    sql.Add(' inner join Contract_Main C on A.ConMainid=C.MainID');
    sql.Add(' inner join Contract_Sub D on A.ConSubid=D.SubID');
    if cxTabControl1.TabIndex<>0 then
    begin
      sql.Add(' where A.MDDate>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.Date))+'''');
      sql.Add(' and A.MDDate<'''+Trim(FormatDateTime('yyyy-MM-dd',EndDate.Date+1))+'''');
      SQL.Add(' and isnull(A.CKName,'''')=''调节仓库'' ');
    end else
    begin
      SQL.Add(' where isnull(A.CKName,'''')=''调节仓库'' ');
    end;
    SQL.Add(' and isnull(A.RKYongTu,'''') in(''退货'',''次品销售'')');
    if cxTabControl1.TabIndex=0 then
    begin
      sql.Add(' and (A.RKQty-A.CKQty)>0 ');
    end else
    if cxTabControl1.TabIndex=1 then
    begin
      sql.Add('  and (A.RKQty-A.CKQty)=0 ');
    end;
    Open;
  end;
  SCreateCDS20(ADOQueryMain,ClientDataSet6);
  SInitCDSData20(ADOQueryMain,ClientDataSet6);
end;

procedure TfrmTJCKTHXSOK.FormShow(Sender: TObject);
begin
  ReadCxBandedGrid('Tv6',Tv6,'调节仓库退货销售');
  ReadCxGrid('Tv5',Tv5,'调节仓库退货销售');
  EndDate.Date:=SGetServerDate(ADOQuery1);
  BegDate.Date:=EndDate.Date-60;
  cxTabControl1.TabIndex:=0;
end;

procedure TfrmTJCKTHXSOK.TBCloseClick(Sender: TObject);
begin
  close;
end;

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

procedure TfrmTJCKTHXSOK.OrderNoKeyPress(Sender: TObject; var Key: Char);
begin
  if Key=#13 then
  begin
    if Trim(OrderNo.Text)='' then Exit;
    with ADOQuery1 do
    begin
      Close;
      sql.Clear;
      sql.Add('select JM.OrderNo, A.*,B.PSName OrdPSName,C.ConNo,C.FactoryNoName,D.Price, ');
      sql.Add(' JS.PRTHX,JS.PRTColor,JS.PRTColorNo,');
      sql.Add(' JSKCPS=(select sum(PS*QtyFlag) from JYOrder_Main_MD JMM where JMM.RKMDID=A.MDID ),');
      sql.Add(' JSKCQty=(select sum(Qty*QtyFlag) from JYOrder_Main_MD JMM where JMM.RKMDID=A.MDID )');
      sql.Add(' from JYOrder_Main_MD  A');
      sql.Add(' inner join JYOrder_Main JM on A.OrdMainId=JM.MainId');
      sql.Add(' left join JYOrder_Sub JS on A.OrdSubId=JS.SubId');
      sql.Add(' inner join JYOrder_Process B on A.PSID=B.PSID');
      sql.Add(' inner join Contract_Main C on A.ConMainid=C.MainID');
      sql.Add(' inner join Contract_Sub D on A.ConSubid=D.SubID');
      sql.Add(' where JM.OrderNo like '''+'%'+Trim(OrderNo.Text)+'%'+'''');
      SQL.Add(' and isnull(A.CKName,'''')=''调节仓库'' ');
      SQL.Add(' and isnull(A.RKYongTu,'''') in(''退货'',''次品销售'')');
      if cxTabControl1.TabIndex=0 then
      begin
        sql.Add(' and (A.RKQty-A.CKQty)=0 ');
      end else
      if cxTabControl1.TabIndex=1 then
      begin
        sql.Add('  and (A.RKQty-A.CKQty)>0 ');
      end;
      Open;
    end;
    SCreateCDS20(ADOQuery1,ClientDataSet6);
    SInitCDSData20(ADOQuery1,ClientDataSet6);
  end;

end;

procedure TfrmTJCKTHXSOK.Button10Click(Sender: TObject);
begin
  WriteCxBandedGrid('Tv6',Tv6,'调节仓库退货销售');
  WriteCxGrid('Tv5',Tv5,'调节仓库退货销售');
  close;

end;

procedure TfrmTJCKTHXSOK.Button8Click(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmTJCKTHXSOK.Tv6CellClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrder_Main_MD_Out where MDID='''+Trim(ClientDataSet6.fieldbyname('MDID').AsString)+'''');
    Open;
  end;
  SCreateCDS20(ADOQuery1,ClientDataSet5);
  SInitCDSData20(ADOQuery1,ClientDataSet5);
end;

procedure TfrmTJCKTHXSOK.Button2Click(Sender: TObject);
begin
  if ClientDataSet5.IsEmpty then Exit;

  if Trim(ClientDataSet5.fieldbyname('CKID').AsString)<>'' then
  begin
    if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete JYOrder_Main_MD_Out where CKID='''+Trim(ClientDataSet5.fieldbyname('CKID').AsString)+'''');
      sql.Add('Update JYOrder_Main_MD Set  ');
      sql.Add(' CKPS=(select sum(SJCKPS) from JYOrder_Main_MD_Out A where A.MDID=JYOrder_Main_MD.MDID),');
      sql.Add(' CKQty=(select sum(SJCKQty) from JYOrder_Main_MD_Out A where A.MDID=JYOrder_Main_MD.MDID)');
      sql.Add(' where MDID='''+Trim(ClientDataSet6.fieldbyname('MDID').AsString)+'''');
      ExecSQL;
    end;
  end;
  ClientDataSet5.Delete;
end;

procedure TfrmTJCKTHXSOK.v2FtyPropertiesEditValueChanged(Sender: TObject);
var
  mvalue,fsj:string;
begin
  {mvalue:=TcxComboBox(Sender).EditingText;
  with ClientDataSet2 do
  begin
    Edit;
    FieldByName('ZZFactoryName').Value:=Trim(mvalue);
    fsj:=TA((v2Fty.Properties as TcxComboBoxProperties).Items.Objects[(v2Fty.Properties as TcxComboBoxProperties).Items.IndexOf(Trim(mvalue))]).S;
    FieldByName('ZZFactoryNo').Value:=Trim(fsj);
    Post;
  end;  }

end;

procedure TfrmTJCKTHXSOK.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active=False then Exit;
  SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQueryMain,ClientDataSet6);
  SInitCDSData20(ADOQueryMain,ClientDataSet6);
end;

procedure TfrmTJCKTHXSOK.Button9Click(Sender: TObject);
begin
  if ADOQueryMain.Active=False then Exit;
  SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQueryMain,ClientDataSet6);
  SInitCDSData20(ADOQueryMain,ClientDataSet6);
end;

procedure TfrmTJCKTHXSOK.v3FtyPropertiesEditValueChanged(Sender: TObject);
var
  mvalue,fsj:string;
begin
  {mvalue:=TcxComboBox(Sender).EditingText;
  with ClientDataSet3 do
  begin
    Edit;
    FieldByName('ZZFactoryName').Value:=Trim(mvalue);
    fsj:=TA((v3Fty.Properties as TcxComboBoxProperties).Items.Objects[(v3Fty.Properties as TcxComboBoxProperties).Items.IndexOf(Trim(mvalue))]).S;
    FieldByName('ZZFactoryNo').Value:=Trim(fsj);
    Post;
  end; }

end;

procedure TfrmTJCKTHXSOK.Button13Click(Sender: TObject);
begin
  try
    frmModuleNote:=TfrmModuleNote.Create(Application);
    with frmModuleNote do
    begin
      flag:='调节仓库退货出库管理';
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmModuleNote.Free;
  end;
end;

procedure TfrmTJCKTHXSOK.Button1Click(Sender: TObject);
var
  maxno:string;
begin
  Panel1.SetFocus;
  if ClientDataSet6.IsEmpty then Exit;
  if ClientDataSet5.IsEmpty then Exit;
  if ClientDataSet5.Locate('CKType',null,[])=True then
  begin
    Application.MessageBox('出库类型不能为空!','提示',0);
    Exit;
  end;
  if ClientDataSet5.Locate('CKOutNote',null,[])=True then
  begin
    Application.MessageBox('退货说明不能为空!','提示',0);
    Exit;
  end;
  if ClientDataSet5.Locate('SJCKPS',null,[])=True then
  begin
    Application.MessageBox('出库匹数不能为空!','提示',0);
    Exit;
  end;
  if ClientDataSet5.Locate('SJCKQty',null,[])=True then
  begin
    Application.MessageBox('出库数量不能为空!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要保存数据吗？','提示',32+4)<>IDYES then Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    ClientDataSet5.DisableControls;
    with ClientDataSet5 do
    begin
      First;
      while not Eof do
      begin
        if Trim(ClientDataSet5.fieldbyname('CKType').AsString)='退货' then
        begin
          with ADOQueryTemp do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from JYOrder_PCS_Sub A');
            sql.Add(' inner join JYOrder_Process B on A.PSID=B.PSID');
            sql.Add(' where A.FactoryNo='''+Trim(ClientDataSet5.fieldbyname('CKFactoryNo').AsString)+'''');
            sql.Add(' and B.PSXH=1');
            Open;
          end;
          if ADOQueryTemp.IsEmpty then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('退货供应商录入错误!','提示',0);
            Exit;
          end;
        end;
        if Trim(ClientDataSet5.fieldbyname('CKID').AsString)='' then
        begin
          if GetLSNo(ADOQueryCmd,maxno,'CK','JYOrder_Main_MD_Out',3,1)=False then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取流水号失败!','提示',0);
            Exit;
          end;
        end  else
        begin
          maxno:=Trim(ClientDataSet5.fieldbyname('CKID').AsString);
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from JYOrder_Main_MD_Out where CKID='''+Trim(ClientDataSet5.fieldbyname('CKID').AsString)+'''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(ClientDataSet5.fieldbyname('CKID').AsString)='' then
          begin
            Append;
            FieldByName('Filler').Value:=Trim(DName);
          end else
          begin
            Edit;
            FieldByName('Editer').Value:=Trim(DName);
            FieldByName('EditTime').Value:=SGetServerDateTime(ADOQueryTemp);
          end;
          FieldByName('CKID').Value:=Trim(maxno);
          FieldByName('MDID').Value:=Trim(ClientDataSet6.fieldbyname('MDID').AsString);
          FieldByName('CKFactoryNo').Value:=Trim(ClientDataSet5.fieldbyname('CKFactoryNo').AsString);
          RTSetSaveDataCDS(ADOQueryCmd,Tv5,ClientDataSet5,'JYOrder_Main_MD_Out',2);
          Post;
        end;
        Edit;
        FieldByName('CKID').Value:=Trim(maxno);
        Post;
        Next;
      end;
    end;
    ClientDataSet5.EnableControls;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update JYOrder_Main_MD Set  ');
      sql.Add(' CKPS=(select sum(SJCKPS) from JYOrder_Main_MD_Out A where A.MDID=JYOrder_Main_MD.MDID),');
      sql.Add(' CKQty=(select sum(SJCKQty) from JYOrder_Main_MD_Out A where A.MDID=JYOrder_Main_MD.MDID)');
      sql.Add(' where MDID='''+Trim(ClientDataSet6.fieldbyname('MDID').AsString)+'''');
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Application.MessageBox('保存成功!','提示',0);
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存异常!','提示',0);
  end;
end;

procedure TfrmTJCKTHXSOK.Button11Click(Sender: TObject);
begin
  if ClientDataSet6.IsEmpty then Exit;
  with ClientDataSet5 do
  begin
    Append;
    FieldByName('CKDate').Value:=SGetServerDate(ADOQuery1);
    FieldByName('CKQtyUnit').Value:=Trim(ClientDataSet6.fieldbyname('QtyUnit').AsString);
    FieldByName('SJCKPS').Value:=ClientDataSet6.fieldbyname('JSKCPS').Value;
    FieldByName('SJCKQty').Value:=ClientDataSet6.fieldbyname('JSKCQty').Value;
    FieldByName('CKPrice').Value:=0;
    FieldByName('CKMoney').Value:=0;
    Post;
  end;
end;

procedure TfrmTJCKTHXSOK.cxGridDBColumn9PropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmGYSList:=TfrmGYSList.Create(Application);
    with frmGYSList do
    begin
      if ShowModal=1 then
      begin
        with ClientDataSet5 do
        begin
          Edit;
          FieldByName('CKFactoryNo').Value:=Trim(frmGYSList.Order_Main.fieldbyname('ZKID').AsString) ;
          FieldByName('CKFactoryName').Value:=Trim(frmGYSList.Order_Main.fieldbyname('KHNameJC').AsString);
          //Post;
        end;
      end;
    end;
  finally
    frmGYSList.Free;
  end;
end;

end.
