unit U_ProductOrderWJGPrice;

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, StrUtils, Menus, cxTextEdit,
  cxDropDownEdit, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator,
  dxSkinsCore, dxSkinsDefaultPainters, dxSkinscxPCPainter;

type
  TfrmProductOrderWJGPrice = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    Label1: TLabel;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    Label3: TLabel;
    OrderNoM: TEdit;
    v1OrderNo: TcxGridDBColumn;
    v1OrdDate: TcxGridDBColumn;
    Order_Main: TClientDataSet;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    v1MPRTCodeName: TcxGridDBColumn;
    v1MPRTMF: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    N2: TMenuItem;
    ToolButton1: TToolButton;
    v1MPRTKZ: TcxGridDBColumn;
    v1OrdDefStr1: TcxGridDBColumn;
    Label11: TLabel;
    MPRTCode: TEdit;
    ADOQueryPrint: TADOQuery;
    CDS_Print: TClientDataSet;
    v1Column1: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    Label2: TLabel;
    JGFactoryName: TComboBox;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    Label4: TLabel;
    JGFactoryName2: TComboBox;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Label5: TLabel;
    JGFactoryName3: TComboBox;
    ADOQuery1: TADOQuery;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure OrderNoMChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure Tv1StylesGetContentStyle(Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord; AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
    procedure ToolButton1Click(Sender: TObject);
    procedure v1Column3PropertiesEditValueChanged(Sender: TObject);
    procedure v1Column1PropertiesEditValueChanged(Sender: TObject);
    procedure v1Column6PropertiesEditValueChanged(Sender: TObject);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure v1Column8PropertiesChange(Sender: TObject);
    procedure v1Column2PropertiesEditValueChanged(Sender: TObject);
    procedure Tv1Column2PropertiesEditValueChanged(Sender: TObject);
    procedure Tv1Column4PropertiesEditValueChanged(Sender: TObject);
  private
    DQdate: TDateTime;
    procedure InitGrid();
    procedure InitForm();

    { Private declarations }
  public
    FFInt, FCloth: Integer;
    canshu1: string;
    { Public declarations }
  end;

var
  frmProductOrderWJGPrice: TfrmProductOrderWJGPrice;
  newh: hwnd;

implementation

uses
  U_DataLink, U_OrderInPut, U_Fun, U_LabelList;

{$R *.dfm}

procedure TfrmProductOrderWJGPrice.FormDestroy(Sender: TObject);
begin
//  frmProductOrderLBNameSet:=nil;
end;

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

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

procedure TfrmProductOrderWJGPrice.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('坯布采购价格', Tv1, '生产指示单管理0326');
end;

procedure TfrmProductOrderWJGPrice.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.add('exec P_View_Order :begdate,:enddate,:WSql');
      Parameters.ParamByName('begdate').Value := FormatDateTime('yyyy-MM-dd', BegDate.DateTime);
      Parameters.ParamByName('enddate').Value := FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1);
      Parameters.ParamByName('WSql').Value := '';
      ExecSQL;
      Open;
    end;
    SCreateCDS20(ADOQueryMain, Order_Main);
    SInitCDSData20(ADOQueryMain, Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmProductOrderWJGPrice.InitForm();
var
  fsj, FSJ2, FSJ3: string;
begin
  ReadCxGrid('坯布采购价格', Tv1, '生产指示单管理0326');
  BegDate.DateTime := SGetServerDate10(ADOQueryTemp) - 7;
  EndDate.DateTime := SGetServerDate10(ADOQueryTemp);
  fsj := 'select distinct(JGFactoryName) Name from JYORDER_MAIN  order by JGFactoryName ';
  SInitComBoxBySql(ADOQueryCmd, JGFactoryName, False, fsj);

  FSJ2 := 'select distinct(JGFactoryName2) Name from JYORDER_MAIN  order by JGFactoryName2 ';
  SInitComBoxBySql(ADOQueryCmd, JGFactoryName2, False, FSJ2);

  FSJ3 := 'select distinct(JGFactoryName3) Name from JYORDER_MAIN  order by JGFactoryName3 ';
  SInitComBoxBySql(ADOQueryCmd, JGFactoryName3, False, FSJ3);
end;

procedure TfrmProductOrderWJGPrice.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 TfrmProductOrderWJGPrice.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmProductOrderWJGPrice.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 TfrmProductOrderWJGPrice.FormShow(Sender: TObject);
begin
  InitForm();
end;

procedure TfrmProductOrderWJGPrice.CheckBox1Click(Sender: TObject);
begin
  InitGrid();
end;

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

procedure TfrmProductOrderWJGPrice.Tv1StylesGetContentStyle(Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord; AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
var
  id, id10: Integer;
begin
  {try
    if Tv1.GroupedItemCount=0 then
    begin
      Id:=Tv1.GetColumnByFieldName('DeliveryDate').Index-tv1.GroupedItemCount;
      Id10:=Tv1.GetColumnByFieldName('SubStatus').Index-tv1.GroupedItemCount;
      if Trim(VarToStr(ARecord.Values[id]))='' then Exit;
      if Id<0 then Exit;
      if ARecord.Values[id10]='完成' then exit;
      if (ARecord.Values[id]-DQdate)>=4 then Exit;
      if ((ARecord.Values[id]-DQdate)>=0) and ((ARecord.Values[id]-DQdate)<4) then
        AStyle:=DataLink_.QHuangSe
      else
      if ARecord.Values[id]-DQdate<0 then
      begin
        AStyle:=DataLink_OrderManage.FenHongS;
      end;
    end else
    begin

    end;
  except
  end; }
end;

procedure TfrmProductOrderWJGPrice.ToolButton1Click(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;
      TBSave.Visible := False;
      ScrollBox1.Enabled := False;
      Tv1.OptionsSelection.CellSelect := False;
      if ShowModal = 1 then
      begin

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

procedure TfrmProductOrderWJGPrice.v1Column3PropertiesEditValueChanged(Sender: TObject);
var
  mvalues: string;
begin
  mvalues := TCXTextEdit(Sender).Text;

  with Order_Main do
  begin
    edit;
    fieldbyname('NLBName').Value := mvalues;
    post;
  end;
  tv1.Controller.EditingController.ShowEdit();
  if trim(mvalues) = '' then
  begin
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update JYOrder_Main Set NLBName='''' ');
      sql.Add('where Mainid=''' + Trim(Order_Main.fieldbyname('Mainid').AsString) + '''');
      ExecSQL;
    end;
  end
end;

procedure TfrmProductOrderWJGPrice.v1Column1PropertiesEditValueChanged(Sender: TObject);
var
  mvalues, FFieldName: string;
begin
  mvalues := TCXTextEdit(Sender).Text;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  if mvalues = '' then
  begin
    mvalues := '0';
  end;
  with Order_Main do
  begin
    edit;
    fieldbyname(FFieldName).Value := mvalues;
    post;
  end;
  tv1.Controller.EditingController.ShowEdit();
  if trim(mvalues) <> '' then
  begin
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update JYOrder_Main Set jgprice1=' + trim(mvalues) + ' ');
      sql.Add('where Mainid=''' + Trim(Order_Main.fieldbyname('Mainid').AsString) + '''');

      sql.Add('update CK_SXPB_CR set PRICE =' + trim(mvalues) + ' ');
      sql.Add('where ORDMainIdRK=' + quotedstr(Trim(Order_Main.fieldbyname('Mainid').AsString)));
      SQL.Add('AND FactoryName=' + quotedstr(Trim(Order_Main.fieldbyname('JGFactoryName').AsString)));
      SQL.Add('AND CRFLAG=''入库''and ckname=''外加工''');

      sql.Add('update CK_SXPB_CR set   ');
      sql.Add('MONEY=ISNULL(SHANGJIFEI,0)+ISNULL(XIAOGANGFEI,0)+ISNULL(PRICE,0)*QTY');
      sql.Add('where ORDMainIdRK=' + quotedstr(Trim(Order_Main.fieldbyname('Mainid').AsString)));
      SQL.Add('AND FactoryName=' + quotedstr(Trim(Order_Main.fieldbyname('JGFactoryName').AsString)));
      SQL.Add('AND CRFLAG=''入库''and ckname=''外加工''');
      ExecSQL;
    end;
    with ADOQuery1 do
    begin
      Close;
      sql.Clear;
      SQL.Add('SELECT * FROM CK_SXPB_CR WHERE CRFLAG=''入库''and ckname=''外加工'' ');
      sql.Add('AND ORDMainIdRK=' + quotedstr(Trim(Order_Main.fieldbyname('Mainid').AsString)));
      SQL.Add('AND FactoryName=' + quotedstr(Trim(Order_Main.fieldbyname('JGFactoryName').AsString)));
      Open;
    end;

    with ADOQuery1 do
    begin
      First;
      while not Eof do
      begin
        if ADOQuery1.FieldByName('ISJs').AsString = '是' then
        begin
          with ADOQueryTemp do
          begin
            Close;
            sql.Clear;
            sql.Add('select  A.YFID from YF_Money_CR_Sub A INNER  JOIN YF_Money_CR B ON A.YFID=B.YFID where yfpzno=' + QuotedStr(Trim(ADOQuery1.fieldbyname('SPID').AsString)));
            sql.Add('and B.status=1');
            Open;
          end;
          if ADOQueryTemp.IsEmpty = True then
          begin
            with ADOQueryTemp do
            begin
              Close;
              sql.Clear;
              sql.Add('EXEC P_Do_CWSC ''应付'',' + quotedstr(trim(ADOQuery1.fieldbyname('SPID').AsString)));

              ExecSQL;

            end;
          end;
        end;
        Next;
      end;
    end;

  end
end;

procedure TfrmProductOrderWJGPrice.v1Column6PropertiesEditValueChanged(Sender: TObject);
var
  mvalues: string;
begin
  mvalues := TCXTextEdit(Sender).Text;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update JYOrder_Main Set XSType=''' + trim(mvalues) + ''' ');
    sql.Add('where Mainid=''' + Trim(Order_Main.fieldbyname('Mainid').AsString) + '''');
    ExecSQL;
  end;
  with Order_Main do
  begin
    edit;
    fieldbyname('XSType').Value := mvalues;
    post;
  end;

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

procedure TfrmProductOrderWJGPrice.Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  if Order_Main.IsEmpty then
    exit;
  ToolButton1.Click;
end;

procedure TfrmProductOrderWJGPrice.v1Column8PropertiesChange(Sender: TObject);
var
  mvalues: string;
begin
  mvalues := TCXTextEdit(Sender).Text;
  with Order_Main do
  begin
    edit;
    fieldbyname('Orddefstr15').Value := mvalues;
    post;
  end;
  tv1.Controller.EditingController.ShowEdit();

  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update JYOrder_Main Set Orddefstr15=''' + trim(mvalues) + ''' ');
    sql.Add('where Mainid=''' + Trim(Order_Main.fieldbyname('Mainid').AsString) + '''');
    ExecSQL;
  end;
end;

procedure TfrmProductOrderWJGPrice.v1Column2PropertiesEditValueChanged(Sender: TObject);
var
  mvalues, FFieldName: string;
begin
  mvalues := TCXTextEdit(Sender).Text;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  if mvalues = '' then
  begin
    mvalues := '  ';
  end;
  with Order_Main do
  begin
    edit;
    fieldbyname(FFieldName).Value := mvalues;
    post;
  end;
  tv1.Controller.EditingController.ShowEdit();
//  if trim(mvalues) <> '' then
//  begin
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update JYOrder_Main Set PBNOTE1=' + QuotedStr((mvalues)) + ' ');
    sql.Add('where Mainid=''' + Trim(Order_Main.fieldbyname('Mainid').AsString) + '''');

    ExecSQL;
  end;
//  end
end;

procedure TfrmProductOrderWJGPrice.Tv1Column2PropertiesEditValueChanged(Sender: TObject);
var
  mvalues, FFieldName: string;
begin
  mvalues := TCXTextEdit(Sender).Text;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  if mvalues = '' then
  begin
    mvalues := '0';
  end;
  with Order_Main do
  begin
    edit;
    fieldbyname(FFieldName).Value := mvalues;
    post;
  end;
  tv1.Controller.EditingController.ShowEdit();
  if trim(mvalues) <> '' then
  begin
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update JYOrder_Main Set jgprice2=' + trim(mvalues) + ' ');
      sql.Add('where Mainid=''' + Trim(Order_Main.fieldbyname('Mainid').AsString) + '''');

      sql.Add('update CK_SXPB_CR set PRICE =' + trim(mvalues) + ' ');
      sql.Add('where ORDMainIdRK=' + quotedstr(Trim(Order_Main.fieldbyname('Mainid').AsString)));
      SQL.Add('AND FactoryName=' + quotedstr(Trim(Order_Main.fieldbyname('JGFactoryName2').AsString)));
      SQL.Add('AND CRFLAG=''入库''and ckname=''外加工''');

      sql.Add('update CK_SXPB_CR set   ');
      sql.Add('MONEY=ISNULL(SHANGJIFEI,0)+ISNULL(XIAOGANGFEI,0)+ISNULL(PRICE,0)*QTY');
      sql.Add('where ORDMainIdRK=' + quotedstr(Trim(Order_Main.fieldbyname('Mainid').AsString)));
      SQL.Add('AND FactoryName=' + quotedstr(Trim(Order_Main.fieldbyname('JGFactoryName2').AsString)));
      SQL.Add('AND CRFLAG=''入库''and ckname=''外加工''');
      ExecSQL;
    end;
    with ADOQuery1 do
    begin
      Close;
      sql.Clear;
      SQL.Add('SELECT * FROM CK_SXPB_CR WHERE CRFLAG=''入库''and ckname=''外加工'' ');
      sql.Add('AND ORDMainIdRK=' + quotedstr(Trim(Order_Main.fieldbyname('Mainid').AsString)));
      SQL.Add('AND FactoryName=' + quotedstr(Trim(Order_Main.fieldbyname('JGFactoryName2').AsString)));
      Open;
    end;

    with ADOQuery1 do
    begin
      First;
      while not Eof do
      begin
        if ADOQuery1.FieldByName('ISJs').AsString = '是' then
        begin
          with ADOQueryTemp do
          begin
            Close;
            sql.Clear;
            sql.Add('select  A.YFID from YF_Money_CR_Sub A INNER  JOIN YF_Money_CR B ON A.YFID=B.YFID where yfpzno=' + QuotedStr(Trim(ADOQuery1.fieldbyname('SPID').AsString)));
            sql.Add('and B.status=1');
            Open;
          end;
          if ADOQueryTemp.IsEmpty = True then
          begin
            with ADOQueryTemp do
            begin
              Close;
              sql.Clear;
              sql.Add('EXEC P_Do_CWSC ''应付'',' + quotedstr(trim(ADOQuery1.fieldbyname('SPID').AsString)));

              ExecSQL;

            end;
          end;
        end;
        Next;
      end;
    end;

  end
end;

procedure TfrmProductOrderWJGPrice.Tv1Column4PropertiesEditValueChanged(Sender: TObject);
var
  mvalues, FFieldName: string;
begin
  mvalues := TCXTextEdit(Sender).Text;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  if mvalues = '' then
  begin
    mvalues := '0';
  end;
  with Order_Main do
  begin
    edit;
    fieldbyname(FFieldName).Value := mvalues;
    post;
  end;
  tv1.Controller.EditingController.ShowEdit();
  if trim(mvalues) <> '' then
  begin
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update JYOrder_Main Set jgprice3=' + trim(mvalues) + ' ');
      sql.Add('where Mainid=''' + Trim(Order_Main.fieldbyname('Mainid').AsString) + '''');

      sql.Add('update CK_SXPB_CR set PRICE =' + trim(mvalues) + ' ');
      sql.Add('where ORDMainIdRK=' + quotedstr(Trim(Order_Main.fieldbyname('Mainid').AsString)));
      SQL.Add('AND FactoryName=' + quotedstr(Trim(Order_Main.fieldbyname('JGFactoryName3').AsString)));
      SQL.Add('AND CRFLAG=''入库''and ckname=''外加工''');

      sql.Add('update CK_SXPB_CR set   ');
      sql.Add('MONEY=ISNULL(SHANGJIFEI,0)+ISNULL(XIAOGANGFEI,0)+ISNULL(PRICE,0)*QTY');
      sql.Add('where ORDMainIdRK=' + quotedstr(Trim(Order_Main.fieldbyname('Mainid').AsString)));
      SQL.Add('AND FactoryName=' + quotedstr(Trim(Order_Main.fieldbyname('JGFactoryName3').AsString)));
      SQL.Add('AND CRFLAG=''入库''and ckname=''外加工''');
      ExecSQL;
    end;
    with ADOQuery1 do
    begin
      Close;
      sql.Clear;
      SQL.Add('SELECT * FROM CK_SXPB_CR WHERE CRFLAG=''入库''and ckname=''外加工'' ');
      sql.Add('AND ORDMainIdRK=' + quotedstr(Trim(Order_Main.fieldbyname('Mainid').AsString)));
      SQL.Add('AND FactoryName=' + quotedstr(Trim(Order_Main.fieldbyname('JGFactoryName3').AsString)));
      Open;
    end;

    with ADOQuery1 do
    begin
      First;
      while not Eof do
      begin
        if ADOQuery1.FieldByName('ISJs').AsString = '是' then
        begin
          with ADOQueryTemp do
          begin
            Close;
            sql.Clear;
            sql.Add('select  A.YFID from YF_Money_CR_Sub A INNER  JOIN YF_Money_CR B ON A.YFID=B.YFID where yfpzno=' + QuotedStr(Trim(ADOQuery1.fieldbyname('SPID').AsString)));
            sql.Add('and B.status=1');
            Open;
          end;
          if ADOQueryTemp.IsEmpty = True then
          begin
            with ADOQueryTemp do
            begin
              Close;
              sql.Clear;
              sql.Add('EXEC P_Do_CWSC ''应付'',' + quotedstr(trim(ADOQuery1.fieldbyname('SPID').AsString)));

              ExecSQL;

            end;
          end;

        end;
        Next;
      end;
    end;

  end
end;

end.

