unit U_ProductOrderNewList_JD;

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, Menus, cxPC, cxCurrencyEdit,
  Mask, DBCtrls, cxContainer, cxTextEdit, cxDBEdit, cxButtonEdit, cxLookAndFeels,
  cxLookAndFeelPainters, cxNavigator, dxBarBuiltInMenu;

type
  TfrmProductOrderNewList_JD = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    cxGridPopupMenu1: TcxGridPopupMenu;
    DataSource1: TDataSource;
    Label3: TLabel;
    OrderNo: TEdit;
    TBExport: TToolButton;
    v1OrderNo: TcxGridDBColumn;
    v1OrdDate: TcxGridDBColumn;
    v1DeliveryDate: TcxGridDBColumn;
    v1YWY: TcxGridDBColumn;
    v1ConNo: TcxGridDBColumn;
    Order_Main: TClientDataSet;
    Label4: TLabel;
    CustomerNoName: TEdit;
    v1MPRTCodeName: TcxGridDBColumn;
    v1PRTOrderQty: TcxGridDBColumn;
    Label2: TLabel;
    Label5: TLabel;
    YWY: TEdit;
    Label8: TLabel;
    MPRTCodeName: TEdit;
    Label9: TLabel;
    ConNo: TEdit;
    Label11: TLabel;
    OrdDefStr1: TEdit;
    v1Column4: TcxGridDBColumn;
    v1ordderNote1: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    Label6: TLabel;
    ISPH: TComboBox;
    PrtColor: TEdit;
    Label7: TLabel;
    orderType: TComboBox;
    Label24: TLabel;
    v1MPrtSpec: TcxGridDBColumn;
    v1HCCK_RollNum: TcxGridDBColumn;
    khConNo: TEdit;
    Label10: TLabel;
    v1Column12: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    v1Column16: TcxGridDBColumn;
    v1HCCK_Qty: TcxGridDBColumn;
    ComboBox2: TComboBox;
    Label1: TLabel;
    Label12: TLabel;
    Than: TComboBox;
    Order_Sub: TClientDataSet;
    DataSource2: TDataSource;
    ADOQuery1: TADOQuery;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    cxStyle2: TcxStyle;
    cxStyle_gridRow: TcxStyle;
    cxStyle_gridFoot: TcxStyle;
    cxStyle_gridHead: TcxStyle;
    cxStyle_gridGroupBox: TcxStyle;
    cxStyle_yellow: TcxStyle;
    cxStyle_Red: TcxStyle;
    cxStyle_fontBlack: TcxStyle;
    cxStyle_fontclFuchsia: TcxStyle;
    cxStyle_fontclPurple: TcxStyle;
    cxStyle_fontclGreen: TcxStyle;
    cxStyle_fontclBlue: TcxStyle;
    cxStyle_fontclTeal: TcxStyle;
    ADOQueryMain: TADOQuery;
    v1HCCK_Unit: TcxGridDBColumn;
    v1DJKCPS: TcxGridDBColumn;
    v1DJKCQty: TcxGridDBColumn;
    Label13: TLabel;
    MPrtSpec: TEdit;
    v1JY_SH: TcxGridDBColumn;
    v1JY_SL: TcxGridDBColumn;
    v1CPRKPS: TcxGridDBColumn;
    v1CPRKQty: TcxGridDBColumn;
    v1CPCKPS: TcxGridDBColumn;
    v1CPCKQty: TcxGridDBColumn;
    v1CPKCPS: TcxGridDBColumn;
    v1CPKCQty: TcxGridDBColumn;
    DataSource3: TDataSource;
    Order_WJG: TClientDataSet;
    ADOQuery2: TADOQuery;
    Panel2: TPanel;
    TPrint: TToolButton;
    cxTabControl1: TcxTabControl;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    v1IFSH: TcxGridDBColumn;
    TMDCX: TToolButton;
    Label14: TLabel;
    Label15: TLabel;
    Label16: TLabel;
    Label17: TLabel;
    Label18: TLabel;
    Label19: TLabel;
    Label20: TLabel;
    Label21: TLabel;
    Label22: TLabel;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    v1CJQty: TcxGridDBColumn;
    v1CGQty: TcxGridDBColumn;
    v1BZQty: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure OrderNoChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure OrderNoKeyPress(Sender: TObject; var Key: Char);
    procedure v1Column3CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
    procedure ComboBox2Change(Sender: TObject);
    procedure ThanChange(Sender: TObject);
    procedure ConNoKeyPress(Sender: TObject; var Key: Char);
    procedure CustomerNoNameKeyPress(Sender: TObject; var Key: Char);
    procedure YWYKeyPress(Sender: TObject; var Key: Char);
    procedure MPRTCodeNameKeyPress(Sender: TObject; var Key: Char);
    procedure TPrintClick(Sender: TObject);
    procedure v1PBKCQtyCustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
    procedure v1DJKCQtyCustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
    procedure v1CPKCQtyCustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
    procedure cxTabControl1Change(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure v1OrderNoCustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
    procedure TMDCXClick(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
  private
    DQdate: TDateTime;
    procedure InitGrid();
    procedure InitForm();
        { Private declarations }
  public
    canshu1: string;
    { Public declarations }
  end;

var
  frmProductOrderNewList_JD: TfrmProductOrderNewList_JD;

implementation

uses
  U_DataLink, U_Fun, U_ProductOrderNewList_GDJD, U_ConInPutXF, U_OrderInPut,
  U_WJGMDList_CX, U_CPRKList_CX, U_CPCKOutList_CX;

{$R *.dfm}
procedure TfrmProductOrderNewList_JD.FormDestroy(Sender: TObject);
begin
  frmProductOrderNewList_JD := nil;
end;

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

procedure TfrmProductOrderNewList_JD.FormCreate(Sender: TObject);
begin
  canshu1 := trim(DParameters1);
  DQdate := SGetServerDate(ADOQuery1);
end;

procedure TfrmProductOrderNewList_JD.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid(self.Caption + tv1.Name + '1', Tv1, '生产指示单管理');
end;

procedure TfrmProductOrderNewList_JD.InitGrid();
var
  strwhere, Tthan: string;
begin
  Panel2.Visible := true;
  Panel2.Refresh;
  strwhere := '';
  if comboBox2.Text = '回仓日期' then
  begin
    strwhere := strwhere + ' and exists(select X.ordMainIDRK from CK_SXPB_CR X where X.ordMainIDRK=mainID' + ' and X.CKName=''待检布'' and X.CRType=''加工完成'' ' + ' and X.CRTime>=''' + trim(formatDatetime('yyyy-MM-dd', begdate.DateTime)) + '''' + ' and X.CRTime<''' + trim(formatdatetime('yyyy-MM-dd', EndDate.DateTime + 1)) + ''')';
  end
  else
  begin
    strwhere := strwhere + ' and A.OrdDate>=''' + trim(formatDatetime('yyyy-MM-dd', begdate.DateTime)) + '''' + ' and A.OrdDate<''' + trim(formatdatetime('yyyy-MM-dd', EndDate.DateTime + 1)) + '''';
  end;
  if trim(canshu1) <> '高权限' then
  begin
    strwhere := strwhere + ' and (A.YWY=''' + Trim(DName) + ''' or A.Filler=''' + Trim(DName) + ''')';
  end;
  if cxTabControl1.TabIndex = 0 then
  begin
    strwhere := strwhere + ' and isnull(WCStatus,'''')='''' ';
  end;
  if cxTabControl1.TabIndex = 1 then
  begin
    strwhere := strwhere + ' and isnull(WCStatus,'''')=''已完成'' ';
  end;
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_View_order_Jd :WSql,:Than');
      Parameters.ParamByName('WSql').Value := strwhere;
      Parameters.ParamByName('Than').Value := Tthan;
      Open;
    end;
    SCreateCDS20(ADOQueryMain, Order_Main);
    SInitCDSData20(ADOQueryMain, Order_Main);
    Order_Main.DisableControls;
    with Order_Main do
    begin
      first;
      while not eof do
      begin
        with ADOQuery2 do
        begin
          close;
          sql.Clear;
          sql.add('select SunHao=TPQty-BCPMQty,Blv=(TPQty-BCPMQty)/TPQty*100 ');
          sql.add(',YJSH=(select Top 1 C.PSSH from JYOrder_Process C ');
          sql.add('where C.Mainid=AA.OrdMainidRK and C.PSName=AA.FromGX) ');
          sql.add('from (select A.FactoryName,A.FromGX,A.ToFactoryName,A.OrdMainidRK,PXDate=min(PXTime),');
          sql.add('TPQty=(select Sum(case when B.QtyUnit=''Y'' then B.Qty/1.0936 else B.Qty end) from CK_SXPB_CR B where B.OrdMainidCK=A.OrdMainidRK and B.TOGX=A.FromGX ');
          sql.add('and B.ToFactoryName=A.TOFactoryName and B.FromFactoryName=A.FactoryName and B.CRFlag=''出库''),');
          sql.add('BCPMQty=(case when A.QtyUnit=''Y'' then Sum(A.Qty)/1.0936 else Sum(A.Qty) end),');
          sql.add('Sum(A.PiQty) PiQty,Sum(A.Qty) Qty,A.QtyUnit');
          sql.add(',BCPCKPS=Sum(BCPCKPS),BCPCKQty=Sum(BCPCKQty)');
          sql.add(' from (select C.PiQty,C.Qty,C.QtyUnit,C.TOFactoryName,C.FactoryName,C.OrdMainidRK,C.FromGX,Sum(B.PiQty) BCPCKPS,Sum(B.Qty) BCPCKQty');
          sql.Add(' ,PXTime=min(C.CRTime)');
          sql.add(' from CK_SXPB_CR B inner join CK_SXPB_CR C on B.FromFactoryName=C.TOFactoryName and B.OrdMainidRK=C.OrdMainidRK and C.FromGX=B.FromGX and C.FactoryName=B.FactoryName ');
          SQL.ADD('where B.OrdMainidCK=''' + trim(Order_Main.fieldbyname('Mainid').asstring) + ''' and isnull(C.CKName,'''')=''外加工'' and isnull(B.CKName,'''')=''外加工'' and C.CRFlag=''入库'' and B.CRFlag=''出库'' ');
          SQL.ADD('group by C.PiQty,C.Qty,C.QtyUnit,C.TOFactoryName,C.FactoryName,C.OrdMainidRK,C.FromGX) A ');
          sql.add('group by A.FactoryName,A.FromGX,A.ToFactoryName,A.OrdMainidRK,A.QtyUnit) AA');
          open;
        end;
        if ADOQuery2.IsEmpty = false then
        begin
          with ADOQuery2 do
          begin
            First;
            while not Eof do
            begin
              if (FieldByName('YJSH').AsFloat < FieldByName('Blv').AsFloat) or (FieldByName('Blv').AsFloat < 0) then
              begin
                with Order_Main do
                begin
                  edit;
                  FieldByName('IFSH').Value := '1';
                end;
              end;
              next;
            end;
          end;
        end;
        next;
      end;
    end;
    Order_Main.EnableControls;
  finally
    ;
    ADOQueryMain.EnableControls;
    //TBFind.Click;
  end;
  Panel2.Visible := false;
end;

procedure TfrmProductOrderNewList_JD.InitForm();
begin
  readCxGrid(self.Caption + tv1.Name + '1', Tv1, '生产指示单管理');
  BegDate.DateTime := SGetServerDate10(ADOQuery1) - 15;
  EndDate.DateTime := SGetServerDate10(ADOQuery1);
 // InitGrid();
end;

procedure TfrmProductOrderNewList_JD.TBFindClick(Sender: TObject);
var
  strwhere: string;
begin
  if ADOQueryMain.Active = False then
    Exit;
  strwhere := SGetFilters(Panel1, 1, 2);
  SDofilter(ADOQueryMain, strwhere);
  SCreateCDS20(ADOQueryMain, Order_Main);
  SInitCDSData20(ADOQueryMain, Order_Main);
  Order_Main.DisableControls;
  with Order_Main do
  begin
    first;
    while not eof do
    begin
      with ADOQuery2 do
      begin
        close;
        sql.Clear;
        sql.add('select SunHao=TPQty-BCPMQty,Blv=(TPQty-BCPMQty)/TPQty*100 ');
        sql.add(',YJSH=(select Top 1 C.PSSH from JYOrder_Process C ');
        sql.add('where C.Mainid=AA.OrdMainidRK and C.PSName=AA.FromGX) ');
        sql.add('from (select A.FactoryName,A.FromGX,A.ToFactoryName,A.OrdMainidRK,PXDate=min(PXTime),');
        sql.add('TPQty=(select Sum(case when B.QtyUnit=''Y'' then B.Qty/1.0936 else B.Qty end) from CK_SXPB_CR B where B.OrdMainidCK=A.OrdMainidRK and B.TOGX=A.FromGX ');
        sql.add('and B.ToFactoryName=A.TOFactoryName and B.FromFactoryName=A.FactoryName and B.CRFlag=''出库''),');
        sql.add('BCPMQty=(case when A.QtyUnit=''Y'' then Sum(A.Qty)/1.0936 else Sum(A.Qty) end),');
        sql.add('Sum(A.PiQty) PiQty,Sum(A.Qty) Qty,A.QtyUnit');
        sql.add(',BCPCKPS=Sum(BCPCKPS),BCPCKQty=Sum(BCPCKQty)');
        sql.add(' from (select C.PiQty,C.Qty,C.QtyUnit,C.TOFactoryName,C.FactoryName,C.OrdMainidRK,C.FromGX,Sum(B.PiQty) BCPCKPS,Sum(B.Qty) BCPCKQty');
        sql.Add(' ,PXTime=min(C.CRTime)');
        sql.add(' from CK_SXPB_CR B inner join CK_SXPB_CR C on B.FromFactoryName=C.TOFactoryName and B.OrdMainidRK=C.OrdMainidRK and C.FromGX=B.FromGX and C.FactoryName=B.FactoryName ');
        SQL.ADD('where B.OrdMainidCK=''' + trim(Order_Main.fieldbyname('Mainid').asstring) + ''' and isnull(C.CKName,'''')=''外加工'' and isnull(B.CKName,'''')=''外加工'' and C.CRFlag=''入库'' and B.CRFlag=''出库'' ');
        SQL.ADD('group by C.PiQty,C.Qty,C.QtyUnit,C.TOFactoryName,C.FactoryName,C.OrdMainidRK,C.FromGX) A ');
        sql.add('group by A.FactoryName,A.FromGX,A.ToFactoryName,A.OrdMainidRK,A.QtyUnit) AA');
        open;
      end;
      if ADOQuery2.IsEmpty = false then
      begin
        with ADOQuery2 do
        begin
          First;
          while not Eof do
          begin
            if (FieldByName('YJSH').AsFloat < FieldByName('Blv').AsFloat) or (FieldByName('Blv').AsFloat < 0) then
            begin
              with Order_Main do
              begin
                edit;
                FieldByName('IFSH').Value := '1';
              end;
            end;
            next;
          end;
        end;
      end;
      next;
    end;
  end;
  Order_Main.EnableControls;
end;

procedure TfrmProductOrderNewList_JD.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    Exit;
  SelExportData(Tv1, ADOQueryMain, '生产指示单列表');
end;

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

procedure TfrmProductOrderNewList_JD.OrderNoChange(Sender: TObject);
begin
  TBFind.Click;
end;

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

procedure TfrmProductOrderNewList_JD.CheckBox1Click(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmProductOrderNewList_JD.CheckBox2Click(Sender: TObject);
begin
  TBRafresh.Click;
end;

procedure TfrmProductOrderNewList_JD.OrderNoKeyPress(Sender: TObject; var Key: Char);
var
  Tthan: string;
begin
  Tthan := '';
  if Key = #13 then
  begin
    if trim(than.Text) = '大于' then
    begin
      Tthan := '1';
    end;
    if trim(than.Text) = '小于或等于' then
    begin
      Tthan := '2';
    end;
    if Length(Trim(OrderNo.Text)) < 3 then
      Exit;
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered := False;
        Close;
        sql.Clear;
        sql.add('exec P_View_order_Jd :WSql,:Than');
        parameters.ParamByName('WSql').Value := ' and OrderNo like ''' + '%' + Trim(OrderNo.Text) + '%' + '''';
        parameters.ParamByName('Than').Value := Tthan;
        Open;
      end;
      SCreateCDS20(ADOQueryMain, Order_Main);
      SInitCDSData20(ADOQueryMain, Order_Main);
      Order_Main.DisableControls;
      with Order_Main do
      begin
        first;
        while not eof do
        begin
          with ADOQuery2 do
          begin
            close;
            sql.Clear;
            sql.add('select SunHao=TPQty-BCPMQty,Blv=(TPQty-BCPMQty)/TPQty*100 ');
            sql.add(',YJSH=(select Top 1 C.PSSH from JYOrder_Process C ');
            sql.add('where C.Mainid=AA.OrdMainidRK and C.PSName=AA.FromGX) ');
            sql.add('from (select A.FactoryName,A.FromGX,A.ToFactoryName,A.OrdMainidRK,PXDate=min(PXTime),');
            sql.add('TPQty=(select Sum(case when B.QtyUnit=''Y'' then B.Qty/1.0936 else B.Qty end) from CK_SXPB_CR B where B.OrdMainidCK=A.OrdMainidRK and B.TOGX=A.FromGX ');
            sql.add('and B.ToFactoryName=A.TOFactoryName and B.FromFactoryName=A.FactoryName and B.CRFlag=''出库''),');
            sql.add('BCPMQty=(case when A.QtyUnit=''Y'' then Sum(A.Qty)/1.0936 else Sum(A.Qty) end),');
            sql.add('Sum(A.PiQty) PiQty,Sum(A.Qty) Qty,A.QtyUnit');
            sql.add(',BCPCKPS=Sum(BCPCKPS),BCPCKQty=Sum(BCPCKQty)');
            sql.add(' from (select C.PiQty,C.Qty,C.QtyUnit,C.TOFactoryName,C.FactoryName,C.OrdMainidRK,C.FromGX,Sum(B.PiQty) BCPCKPS,Sum(B.Qty) BCPCKQty');
            sql.Add(' ,PXTime=min(C.CRTime)');
            sql.add(' from CK_SXPB_CR B inner join CK_SXPB_CR C on B.FromFactoryName=C.TOFactoryName and B.OrdMainidRK=C.OrdMainidRK and C.FromGX=B.FromGX and C.FactoryName=B.FactoryName ');
            SQL.ADD('where B.OrdMainidCK=''' + trim(Order_Main.fieldbyname('Mainid').asstring) + ''' and isnull(C.CKName,'''')=''外加工'' and isnull(B.CKName,'''')=''外加工'' and C.CRFlag=''入库'' and B.CRFlag=''出库'' ');
            SQL.ADD('group by C.PiQty,C.Qty,C.QtyUnit,C.TOFactoryName,C.FactoryName,C.OrdMainidRK,C.FromGX) A ');
            sql.add('group by A.FactoryName,A.FromGX,A.ToFactoryName,A.OrdMainidRK,A.QtyUnit) AA');
            open;
          end;
          if ADOQuery2.IsEmpty = false then
          begin
            with ADOQuery2 do
            begin
              First;
              while not Eof do
              begin
                if (FieldByName('YJSH').AsFloat < FieldByName('Blv').AsFloat) or (FieldByName('Blv').AsFloat < 0) then
                begin
                  with Order_Main do
                  begin
                    edit;
                    FieldByName('IFSH').Value := '1';
                  end;
                end;
                next;
              end;
            end;
          end;
          next;
        end;
      end;
      Order_Main.EnableControls;
    finally
      ADOQueryMain.EnableControls;
    end;
  end;
end;

procedure TfrmProductOrderNewList_JD.v1Column3CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
{  iF AViewInfo.GridRecord.Values[tv1.GetColumnByFieldName('RK_KGQty').Index]>
  AViewInfo.GridRecord.Values[tv1.GetColumnByFieldName('HC_Qty').Index]*1.05 then
  begin
    ACanvas.Brush.Color:=Clred;
  end;  }
end;

procedure TfrmProductOrderNewList_JD.ComboBox2Change(Sender: TObject);
begin
  initGrid();
end;

procedure TfrmProductOrderNewList_JD.ThanChange(Sender: TObject);
begin
  initgrid();
end;

procedure TfrmProductOrderNewList_JD.ConNoKeyPress(Sender: TObject; var Key: Char);
var
  Tthan: string;
begin
  Tthan := '';
  if Key = #13 then
  begin
    if trim(than.Text) = '大于' then
    begin
      Tthan := '1';
    end;
    if trim(than.Text) = '小于或等于' then
    begin
      Tthan := '2';
    end;
    if Length(Trim(ConNo.Text)) < 4 then
      Exit;
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered := False;
        Close;
        sql.Clear;
        sql.add('exec P_View_order_Jd :WSql,:Than');
        parameters.ParamByName('WSql').Value := ' and [dbo].[F_Get_Order_SubStr](A.MainId,''ConNo'') like ''' + '%' + Trim(ConNo.Text) + '%' + '''';
        parameters.ParamByName('Than').Value := Tthan;
        Open;
      end;
      SCreateCDS20(ADOQueryMain, Order_Main);
      SInitCDSData20(ADOQueryMain, Order_Main);
      Order_Main.DisableControls;
      with Order_Main do
      begin
        first;
        while not eof do
        begin
          with ADOQuery2 do
          begin
            close;
            sql.Clear;
            sql.add('select SunHao=TPQty-BCPMQty,Blv=(TPQty-BCPMQty)/TPQty*100 ');
            sql.add(',YJSH=(select Top 1 C.PSSH from JYOrder_Process C ');
            sql.add('where C.Mainid=AA.OrdMainidRK and C.PSName=AA.FromGX) ');
            sql.add('from (select A.FactoryName,A.FromGX,A.ToFactoryName,A.OrdMainidRK,PXDate=min(PXTime),');
            sql.add('TPQty=(select Sum(case when B.QtyUnit=''Y'' then B.Qty/1.0936 else B.Qty end) from CK_SXPB_CR B where B.OrdMainidCK=A.OrdMainidRK and B.TOGX=A.FromGX ');
            sql.add('and B.ToFactoryName=A.TOFactoryName and B.FromFactoryName=A.FactoryName and B.CRFlag=''出库''),');
            sql.add('BCPMQty=(case when A.QtyUnit=''Y'' then Sum(A.Qty)/1.0936 else Sum(A.Qty) end),');
            sql.add('Sum(A.PiQty) PiQty,Sum(A.Qty) Qty,A.QtyUnit');
            sql.add(',BCPCKPS=Sum(BCPCKPS),BCPCKQty=Sum(BCPCKQty)');
            sql.add(' from (select C.PiQty,C.Qty,C.QtyUnit,C.TOFactoryName,C.FactoryName,C.OrdMainidRK,C.FromGX,Sum(B.PiQty) BCPCKPS,Sum(B.Qty) BCPCKQty');
            sql.Add(' ,PXTime=min(C.CRTime)');
            sql.add(' from CK_SXPB_CR B inner join CK_SXPB_CR C on B.FromFactoryName=C.TOFactoryName and B.OrdMainidRK=C.OrdMainidRK and C.FromGX=B.FromGX and C.FactoryName=B.FactoryName ');
            SQL.ADD('where B.OrdMainidCK=''' + trim(Order_Main.fieldbyname('Mainid').asstring) + ''' and isnull(C.CKName,'''')=''外加工'' and isnull(B.CKName,'''')=''外加工'' and C.CRFlag=''入库'' and B.CRFlag=''出库'' ');
            SQL.ADD('group by C.PiQty,C.Qty,C.QtyUnit,C.TOFactoryName,C.FactoryName,C.OrdMainidRK,C.FromGX) A ');
            sql.add('group by A.FactoryName,A.FromGX,A.ToFactoryName,A.OrdMainidRK,A.QtyUnit) AA');
            open;
          end;
          if ADOQuery2.IsEmpty = false then
          begin
            with ADOQuery2 do
            begin
              First;
              while not Eof do
              begin
                if (FieldByName('YJSH').AsFloat < FieldByName('Blv').AsFloat) or (FieldByName('Blv').AsFloat < 0) then
                begin
                  with Order_Main do
                  begin
                    edit;
                    FieldByName('IFSH').Value := '1';
                  end;
                end;
                next;
              end;
            end;
          end;
          next;
        end;
      end;
      Order_Main.EnableControls;
    finally
      ADOQueryMain.EnableControls;
    end;
  end;
end;

procedure TfrmProductOrderNewList_JD.CustomerNoNameKeyPress(Sender: TObject; var Key: Char);
var
  Tthan: string;
begin
  Tthan := '';
  if trim(than.Text) = '大于' then
  begin
    Tthan := '1';
  end;
  if trim(than.Text) = '小于或等于' then
  begin
    Tthan := '2';
  end;
  if Key = #13 then
  begin
    if Length(Trim(CustomerNoName.Text)) < 4 then
      Exit;
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered := False;
        Close;
        sql.Clear;
        sql.add('exec P_View_order_Jd :WSql,:Than');
        parameters.ParamByName('WSql').Value := ' and CustomerNoName like ''' + '%' + Trim(CustomerNoName.Text) + '%' + ''' ' + 'and filltime>=''' + trim(formatdatetime('yyyy-MM-dd', Begdate.DateTime)) + ''' ' + 'and filltime<''' + trim(formatdatetime('yyyy-MM-dd', enddate.DateTime + 1)) + '''';
        parameters.ParamByName('Than').Value := Tthan;
        Open;
      end;
      SCreateCDS20(ADOQueryMain, Order_Main);
      SInitCDSData20(ADOQueryMain, Order_Main);
    finally
      ADOQueryMain.EnableControls;
    end;
  end;
end;

procedure TfrmProductOrderNewList_JD.YWYKeyPress(Sender: TObject; var Key: Char);
var
  Tthan: string;
begin
  Tthan := '';
  if trim(than.Text) = '大于' then
  begin
    Tthan := '1';
  end;
  if trim(than.Text) = '小于或等于' then
  begin
    Tthan := '2';
  end;
  if Key = #13 then
  begin
    if Length(Trim(YWY.Text)) < 4 then
      Exit;
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered := False;
        Close;
        sql.Clear;
        sql.add('exec P_View_order_Jd :WSql,:Than');
        parameters.ParamByName('WSql').Value := ' and YWY like ''' + '%' + Trim(YWY.Text) + '%' + ''' ' + 'and filltime>=''' + trim(formatdatetime('yyyy-MM-dd', Begdate.DateTime)) + ''' ' + 'and filltime<''' + trim(formatdatetime('yyyy-MM-dd', enddate.DateTime + 1)) + '''';
        parameters.ParamByName('Than').Value := Tthan;
        Open;
      end;
      SCreateCDS20(ADOQueryMain, Order_Main);
      SInitCDSData20(ADOQueryMain, Order_Main);
    finally
      ADOQueryMain.EnableControls;
    end;
  end;
end;

procedure TfrmProductOrderNewList_JD.MPRTCodeNameKeyPress(Sender: TObject; var Key: Char);
var
  Tthan, FName: string;
begin
  FName := TEdit(Sender).Name;
  if Key = #13 then
  begin
    Tthan := '';
    if trim(than.Text) = '大于' then
    begin
      Tthan := '1';
    end;
    if trim(than.Text) = '小于或等于' then
    begin
      Tthan := '2';
    end;
    if Length(Trim(TEdit(Sender).Text)) < 4 then
      Exit;
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered := False;
        Close;
        sql.Clear;
        sql.add('exec P_View_order_Jd :WSql,:Than');
        parameters.ParamByName('WSql').Value := ' and ' + FName + '  like ''' + '%' + Trim(TEdit(Sender).Text) + '%' + ''' ' + 'and filltime>=''' + trim(formatdatetime('yyyy-MM-dd', Begdate.DateTime)) + ''' ' + 'and filltime<''' + trim(formatdatetime('yyyy-MM-dd', enddate.DateTime + 1)) + '''';
        parameters.ParamByName('Than').Value := Tthan;
        Open;
      end;
      SCreateCDS20(ADOQueryMain, Order_Main);
      SInitCDSData20(ADOQueryMain, Order_Main);
      Order_Main.DisableControls;
      with Order_Main do
      begin
        first;
        while not eof do
        begin
          with ADOQuery2 do
          begin
            close;
            sql.Clear;
            sql.add('select SunHao=TPQty-BCPMQty,Blv=(TPQty-BCPMQty)/TPQty*100 ');
            sql.add(',YJSH=(select Top 1 C.PSSH from JYOrder_Process C ');
            sql.add('where C.Mainid=AA.OrdMainidRK and C.PSName=AA.FromGX) ');
            sql.add('from (select A.FactoryName,A.FromGX,A.ToFactoryName,A.OrdMainidRK,PXDate=min(PXTime),');
            sql.add('TPQty=(select Sum(case when B.QtyUnit=''Y'' then B.Qty/1.0936 else B.Qty end) from CK_SXPB_CR B where B.OrdMainidCK=A.OrdMainidRK and B.TOGX=A.FromGX ');
            sql.add('and B.ToFactoryName=A.TOFactoryName and B.FromFactoryName=A.FactoryName and B.CRFlag=''出库''),');
            sql.add('BCPMQty=(case when A.QtyUnit=''Y'' then Sum(A.Qty)/1.0936 else Sum(A.Qty) end),');
            sql.add('Sum(A.PiQty) PiQty,Sum(A.Qty) Qty,A.QtyUnit');
            sql.add(',BCPCKPS=Sum(BCPCKPS),BCPCKQty=Sum(BCPCKQty)');
            sql.add(' from (select C.PiQty,C.Qty,C.QtyUnit,C.TOFactoryName,C.FactoryName,C.OrdMainidRK,C.FromGX,Sum(B.PiQty) BCPCKPS,Sum(B.Qty) BCPCKQty');
            sql.Add(' ,PXTime=min(C.CRTime)');
            sql.add(' from CK_SXPB_CR B inner join CK_SXPB_CR C on B.FromFactoryName=C.TOFactoryName and B.OrdMainidRK=C.OrdMainidRK and C.FromGX=B.FromGX and C.FactoryName=B.FactoryName ');
            SQL.ADD('where B.OrdMainidCK=''' + trim(Order_Main.fieldbyname('Mainid').asstring) + ''' and isnull(C.CKName,'''')=''外加工'' and isnull(B.CKName,'''')=''外加工'' and C.CRFlag=''入库'' and B.CRFlag=''出库'' ');
            SQL.ADD('group by C.PiQty,C.Qty,C.QtyUnit,C.TOFactoryName,C.FactoryName,C.OrdMainidRK,C.FromGX) A ');
            sql.add('group by A.FactoryName,A.FromGX,A.ToFactoryName,A.OrdMainidRK,A.QtyUnit) AA');
            open;
          end;
          if ADOQuery2.IsEmpty = false then
          begin
            with ADOQuery2 do
            begin
              First;
              while not Eof do
              begin
                if (FieldByName('YJSH').AsFloat < FieldByName('Blv').AsFloat) or (FieldByName('Blv').AsFloat < 0) then
                begin
                  with Order_Main do
                  begin
                    edit;
                    FieldByName('IFSH').Value := '1';
                  end;
                end;
                next;
              end;
            end;
          end;
          next;
        end;
      end;
      Order_Main.EnableControls;
    finally
      ADOQueryMain.EnableControls;
    end;
  end;
end;

procedure TfrmProductOrderNewList_JD.TPrintClick(Sender: TObject);
begin
  frmProductOrderNewList_GDJD := TfrmProductOrderNewList_GDJD.Create(self);
  with frmProductOrderNewList_GDJD do
  begin
    if showmodal = 1 then
    begin

    end;
    free;
  end;
end;

procedure TfrmProductOrderNewList_JD.v1PBKCQtyCustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
var
  id, id10, id20: Integer;
begin
  id := TV1.GetColumnByFieldName('PBRKQty').Index; //;-TV1.GroupedItemCount;
  id10 := TV1.GetColumnByFieldName('TP_Qty').Index;
  id20 := TV1.GetColumnByFieldName('PBKCQty').Index;
  if AViewInfo.GridRecord.Values[id] - AViewInfo.GridRecord.Values[id10] <> AViewInfo.GridRecord.Values[id20] then
  begin
    ACanvas.Brush.Color := clRed;
  end;
end;

procedure TfrmProductOrderNewList_JD.v1DJKCQtyCustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
var
  id, id10, id20: Integer;
begin
  id := TV1.GetColumnByFieldName('HC_Qty').Index; //;-TV1.GroupedItemCount;
  id10 := TV1.GetColumnByFieldName('HCCK_Qty').Index;
  id20 := TV1.GetColumnByFieldName('DJKCQty').Index;
  if AViewInfo.GridRecord.Values[id] - AViewInfo.GridRecord.Values[id10] <> AViewInfo.GridRecord.Values[id20] then
  begin
    ACanvas.Brush.Color := clRed;
  end;
end;

procedure TfrmProductOrderNewList_JD.v1CPKCQtyCustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
var
  id, id10, id20: Integer;
begin
  id := TV1.GetColumnByFieldName('CPRKQty').Index; //;-TV1.GroupedItemCount;
  id10 := TV1.GetColumnByFieldName('CPCKQty').Index;
  id20 := TV1.GetColumnByFieldName('CPKCQty').Index;
  if AViewInfo.GridRecord.Values[id] - AViewInfo.GridRecord.Values[id10] <> AViewInfo.GridRecord.Values[id20] then
  begin
    ACanvas.Brush.Color := clRed;
  end;
end;

procedure TfrmProductOrderNewList_JD.cxTabControl1Change(Sender: TObject);
begin
  TBRafresh.Click;
end;

procedure TfrmProductOrderNewList_JD.ToolButton1Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  try
    frmConInPutXF := TfrmConInPutXF.Create(Application);
    with frmConInPutXF do
    begin
      PState := 1;
      FMainId := Trim(Self.Order_Main.fieldbyname('ConMainId').AsString);
      ToolBar2.Visible := False;
      TBSave.Visible := False;
      ScrollBox1.Enabled := False;
      TConFJ.Visible := False;
      Tv1.OptionsSelection.CellSelect := False;
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmConInPutXF.Free;
  end;
end;

procedure TfrmProductOrderNewList_JD.ToolButton2Click(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);
      ToolBar2.Visible := False;
      ToolBar3.Visible := False;
      ToolBar4.Visible := False;
      TBSave.Visible := False;
      ScrollBox1.Enabled := False;
      //Tv1.OptionsSelection.CellSelect:=False;
      if ShowModal = 1 then
      begin

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

procedure TfrmProductOrderNewList_JD.v1OrderNoCustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
var
  Id: integer;
begin
  if cxTabControl1.TabIndex = 1 then
  begin
    Id := TV1.GetColumnByFieldName('IFSH').Index;
    if Id < 0 then
      Exit;
    if AViewInfo.GridRecord.Values[Id] <> '' then
    begin
      ACanvas.Brush.Color := clGreen;
    end;
  end;
end;

procedure TfrmProductOrderNewList_JD.TMDCXClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    exit;
  frmWJGMDList := TfrmWJGMDList.create(self);
  with frmWJGMDList do
  begin
    FormID := '1';
    FConNO := Trim(self.Order_Main.fieldbyname('OrderNo').AsString);
    if showmodal = 1 then
    begin

    end;
    free;
  end;
end;

procedure TfrmProductOrderNewList_JD.ToolButton3Click(Sender: TObject);
begin
  if Order_Main.isempty then
    Exit;
  frmCPRKList_CX := TfrmCPRKList_CX.Create(self);
  with frmCPRKList_CX do
  begin
    FConNO := Trim(self.Order_Main.fieldbyname('OrderNO').AsString);
    FormID := '1';
    if ShowModal = 1 then
    begin

    end;
    free;
  end;
end;

procedure TfrmProductOrderNewList_JD.ToolButton4Click(Sender: TObject);
begin
  if Order_Main.isempty then
    Exit;
  frmCPCKOutList_CX := TfrmCPCKOutList_CX.Create(self);
  with frmCPCKOutList_CX do
  begin
    formid := '1';
    FConNO := Trim(self.Order_Main.fieldbyname('OrderNO').AsString);
    if ShowModal = 1 then
    begin

    end;
    free;
  end;
end;

end.

