unit U_ProductOrderList;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, ComCtrls, ToolWin, cxStyles, cxCustomData,
  cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, DB, cxDBData, ADODB,
  cxGridCustomPopupMenu, cxGridPopupMenu, cxGridLevel, cxClasses,
  cxControls, cxGridCustomView, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxGrid, DBClient, cxCheckBox, cxCalendar, cxSplitter,
  RM_Dataset, RM_System, RM_Common, RM_Class, RM_GridReport, RM_e_Xls;

type
  TfrmProductOrderList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBAdd: TToolButton;
    TBEdit: TToolButton;
    TBDel: TToolButton;
    TBPrint: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    Label1: TLabel;
    Label2: TLabel;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    Label3: TLabel;
    OrderNoM: TEdit;
    Label5: TLabel;
    PRTCodeNameM: TEdit;
    TBExport: TToolButton;
    v1OrderNo: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1OrderDate: TcxGridDBColumn;
    v1DeliveryDate: TcxGridDBColumn;
    v1FactoryNo1Name: TcxGridDBColumn;
    v1FactoryNo2Name: TcxGridDBColumn;
    v1FactoryNo3Name: TcxGridDBColumn;
    v1Note: TcxGridDBColumn;
    v1PRTSpec: TcxGridDBColumn;
    v1PRTColor: TcxGridDBColumn;
    v1PRTMF: TcxGridDBColumn;
    v1PRTKZ: TcxGridDBColumn;
    v1PRTOrderQty: TcxGridDBColumn;
    v1OrderUnit: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1PRTQty: TcxGridDBColumn;
    v1PRTUnit: TcxGridDBColumn;
    Order_Main: TClientDataSet;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    v2ComeDate: TcxGridDBColumn;
    v2BatchNo: TcxGridDBColumn;
    v2MxQty: TcxGridDBColumn;
    v2MxNote: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    TBTP: TToolButton;
    ClientDataSet2: TClientDataSet;
    DataSource2: TDataSource;
    cxSplitter1: TcxSplitter;
    v2Qty1: TcxGridDBColumn;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    v1CustomerNoName: TcxGridDBColumn;
    Label4: TLabel;
    CustomerNoName: TEdit;
    v1Column3: TcxGridDBColumn;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure TBEditClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TBViewClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBPrintClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure OrderNoMChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView;
      APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord;
      ANewItemRecordFocusingChanged: Boolean);
    procedure TBTPClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
  private
    procedure InitGrid();
    procedure InitForm();
    function  DelData():Boolean;
    { Private declarations }
  public
    FFInt:Integer;
    { Public declarations }
  end;

var
  frmProductOrderList: TfrmProductOrderList;

implementation
uses
  U_DataLink,U_OrderInPut,U_Fun;

{$R *.dfm}

procedure TfrmProductOrderList.FormDestroy(Sender: TObject);
begin
  frmProductOrderList:=nil;
end;

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

procedure TfrmProductOrderList.FormCreate(Sender: TObject);
begin
  cxgrid1.Align:=alClient;
  //BegDate.DateTime:=SGetServerDateTime(ADOQueryTemp)-7;
  //EndDate.DateTime:=SGetServerDateTime(ADOQueryTemp);
end;

procedure TfrmProductOrderList.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('指示单列表',Tv1,'指示单管理');
end;

procedure TfrmProductOrderList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add('exec Order_QryList :MainId,:WSql');
      Parameters.ParamByName('WSql').Value:=' and FillTime>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.DateTime))+''''
                                           +' and FillTime<'''+Trim(FormatDateTime('yyyy-MM-dd',EndDate.DateTime+1))+'''';
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmProductOrderList.InitForm();
begin
  ReadCxGrid('指示单列表',Tv1,'指示单管理');
  if FFInt=1 then
  begin
    v1Column3.Hidden:=False;
    v1Column3.Visible:=True;
  end else
  begin
    v1Column3.Hidden:=True;
    v1Column3.Visible:=False;
  end;
  if Trim(DParameters1)='1' then
  begin
    v1CustomerNoName.Visible:=False;
    v1CustomerNoName.Hidden:=True;
  end else
  begin
    v1CustomerNoName.Visible:=True;
    v1CustomerNoName.Hidden:=False;
  end;
  BegDate.DateTime:=SGetServerDate10(ADOQueryTemp)-7;
  EndDate.DateTime:=SGetServerDate10(ADOQueryTemp);
  InitGrid();
end;

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

procedure TfrmProductOrderList.TBEditClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  try
    frmOrderInPut:=TfrmOrderInPut.Create(Application);
    with frmOrderInPut do
    begin
      PState:=1;
      FMainId:=Trim(Self.Order_Main.fieldbyname('MainId').AsString);
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmOrderInPut.Free;
  end;
end;

procedure TfrmProductOrderList.TBDelClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  if DelData() then
  begin
    //TBRafresh.Click;
    //TBFind.Click;
    Order_Main.Delete;
  end;
end;

function TfrmProductOrderList.DelData():Boolean;
begin
  try
    Result:=false;
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete Order_Sub where SubId='''+Trim(Order_Main.fieldbyname('SubId').AsString)+'''');
      ExecSQL;
    end;
    if Trim(Order_Main.fieldbyname('SubId').AsString)='' then
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('delete Order_Main where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
        ExecSQL;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Result:=False;
    Application.MessageBox('数据删除异常！','提示',0);
  end;
end;

procedure TfrmProductOrderList.TBViewClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
  ShowMessage(DCode);
  
end;

procedure TfrmProductOrderList.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
  TcxGridToExcel('指示单列表',cxGrid1);
end;

procedure TfrmProductOrderList.TBPrintClick(Sender: TObject);
var
   fPrintFile:string;
   Porderno:string;
begin
  if Order_Main.IsEmpty then Exit;
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\生产指示单.rmf' ;
  SDofilter(ADOQueryMain,' OrderNoM='''+Trim(Order_Main.fieldbyname('OrderNoM').AsString)+'''');
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
  Porderno:=Trim(Order_Main.fieldbyname('OrderNoM').AsString);
  if FileExists(fPrintFile) then
  begin
    //RMVariables['begindate']:=begindate.DateTime;
    //RMVariables['enddate']:=enddate.DateTime;
    //RMVariables['printtime']:=Now;
    //RMVariables['printer']:=Trim(gUserName);
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\生产指示单.rmf'),'提示',0);
  end;
  SDofilter(ADOQueryMain,'');
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
  Order_Main.Locate('ordernoM',Porderno,[]);
  //SelPrintData(TV4,ADOQueryMain,'合同查询报表');
end;

procedure TfrmProductOrderList.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmProductOrderList.TBAddClick(Sender: TObject);
begin
  try
    frmOrderInPut:=TfrmOrderInPut.Create(Application);
    with frmOrderInPut do
    begin
      PState:=0;
      FMainId:='';
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmOrderInPut.Free;
  end;
end;

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

procedure TfrmProductOrderList.FormShow(Sender: TObject);
begin
  InitForm();
end;

procedure TfrmProductOrderList.Tv1CellDblClick(
  Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  if FFInt=1 then
    ModalResult:=1;
end;

procedure TfrmProductOrderList.Tv1FocusedRecordChanged(
  Sender: TcxCustomGridTableView; APrevFocusedRecord,
  AFocusedRecord: TcxCustomGridRecord;
  ANewItemRecordFocusingChanged: Boolean);
begin
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('select A.*,C.BatchNo from Contract_Sub_MxTo A ');
    sql.Add(' inner join Contract_Sub_Mx C on A.MxId=C.MxId');
    SQL.Add(' where A.OrdSubId='''+Trim(Order_Main.fieldbyname('SubId').AsString)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,ClientDataSet2);
  SInitCDSData20(ADOQueryTemp,ClientDataSet2);
end;

procedure TfrmProductOrderList.TBTPClick(Sender: TObject);
 var
   FQty,FQty1,FMxQty,FPQty,FMxQtyS,FPQtyS:String;
begin
  {if ClientDataSet2.Locate('TPDate',null,[]) then
  begin
    Application.MessageBox('投坯日期不能为空！','提示',0);
    Exit;
  end;
  if ClientDataSet2.FieldByName('TPQty').AsFloat=0 then
  begin
    Application.MessageBox('投坯数量不能为0！','提示',0);
    Exit;
  end;
  }
  if ClientDataSet2.IsEmpty then Exit;
  BegDate.SetFocus;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with ClientDataSet2 do
    begin
      First;
      while not Eof do
      begin
        if Trim(ClientDataSet2.fieldbyname('TPQty').AsString)='' then
          FQty:='0.0'
        else
          FQty:=Trim(ClientDataSet2.fieldbyname('TPQty').AsString);
        if Trim(ClientDataSet2.fieldbyname('Qty1').AsString)='' then
          FQty1:='0.0'
        else
          FQty1:=Trim(ClientDataSet2.fieldbyname('Qty1').AsString);
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          SQL.Add('select MxQty,Qty1  from Contract_Sub_Mx where MxId='''+Trim(ClientDataSet2.fieldbyname('MxId').AsString)+'''');
          Open;
          if not IsEmpty then
          begin
            FMxQty:=Trim(fieldbyname('MxQty').AsString);
            FPQty:=Trim(fieldbyname('Qty1').AsString);
            if Trim(FMxQty)='' then FMxQty:='0.0';
            if Trim(FPQty)='' then FPQty:='0.0';
          end else
          begin
            FMxQty:='0.0';
            FPQty:='0.0';
          end;

        end;
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          SQL.Add('select Sum(isnull(TPQty,0)) TPQty,Sum(isnull(Qty1,0)) Qty1  from Contract_Sub_Mxto where MxId='''+Trim(ClientDataSet2.fieldbyname('MxId').AsString)+'''');
          sql.Add(' and ToId<>'''+Trim(ClientDataSet2.fieldbyname('ToId').AsString)+'''');
          Open;
          if not IsEmpty then
          begin
            FMxQtyS:=Trim(fieldbyname('TPQty').AsString);
            FPQtyS:=Trim(fieldbyname('Qty1').AsString);
            if Trim(FMxQtyS)='' then FMxQtyS:='0.0';
            if Trim(FPQtyS)='' then FPQtyS:='0.0';
          end else
          begin
            FMxQtyS:='0.0';
            FPQtyS:='0.0';
          end;
        end;

        if StrToFloat(FQty)+StrToFloat(FMxQtyS)-StrToFloat(FMxQty)>0 then
        begin
          if Application.MessageBox(PChar('批号为：'+Trim(ClientDataSet2.fieldbyname('BatchNo').AsString)+
                                           '的数量是'+FMxQty+'投坯总数量为'+FloatToStr(StrToFloat(FQty)+StrToFloat(FMxQtyS))
                                           +'，数量不足，确定要投坯吗？'),'提示',32+4)<>IDYES then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            Exit;
          end;
        end;
        if StrToFloat(FQty1)+StrToFloat(FPQtyS)-StrToFloat(FPQty)>0 then
        begin
          if Application.MessageBox(PChar('批号为：'+Trim(ClientDataSet2.fieldbyname('BatchNo').AsString)+
                                           '的匹数是'+FPQty+'投坯总匹数为'+FloatToStr(StrToFloat(FQty1)+StrToFloat(FPQtyS))
                                           +'，匹数不足，确定要投坯吗？'),'提示',32+4)<>IDYES then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            Exit;
          end;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update Contract_Sub_Mxto Set TPQty='+FQty);
          sql.Add(',Qty1='+FQty1);
          if Trim(ClientDataSet2.fieldbyname('TPDate').AsString)<>'' then
            sql.Add(',TPDate='''+Trim(ClientDataSet2.fieldbyname('TPDate').AsString)+'''');
          if Trim(ClientDataSet2.fieldbyname('TPNote').AsString)<>'' then
            sql.Add(',TPNote='''+Trim(ClientDataSet2.fieldbyname('TPNote').AsString)+'''');
          SQL.Add(' where ToId='''+Trim(ClientDataSet2.fieldbyname('ToId').AsString)+'''');
          ExecSQL;
        end;
        Next;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Application.MessageBox('投坯成功！','提示',0);
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('投坯失败！','提示',0);
  end;

end;

procedure TfrmProductOrderList.ToolButton1Click(Sender: TObject);
begin
  Order_Main.DisableControls;
  with Order_Main do
  begin
    First;
    while not Eof do
    begin
      Edit;
      FieldByName('SSel').Value:=1;
      Post;
      Next;
    end;
  end;
  Order_Main.EnableControls;
end;

procedure TfrmProductOrderList.ToolButton2Click(Sender: TObject);
begin
  Order_Main.DisableControls;
  with Order_Main do
  begin
    First;
    while not Eof do
    begin
      Edit;
      FieldByName('SSel').Value:=0;
      Post;
      Next;
    end;
  end;
  Order_Main.EnableControls;
end;

procedure TfrmProductOrderList.ToolButton3Click(Sender: TObject);
begin
  ModalResult:=1;
end;

end.
