#最初に赤分の受注フローを追加する
#売上報告----------------------------------------
insert into master_app_production.table_rels set 
  order_type_code=5,table_name="sales",disp_sort=1,trans_type_code=1,sign_type_code=0, j_table_name="売上処理",invalid_flag_code=0,created_at=NOW(),updated_at=NOW();
insert into master_app_production.table_rels set 
  order_type_code=5,table_name="purchases",disp_sort=2,trans_type_code=1,sign_type_code=0,j_table_name="仕入処理",invalid_flag_code=0,created_at=NOW(),updated_at=NOW();
insert into master_app_production.table_rels set 
  order_type_code=5,table_name="restorations",disp_sort=3,trans_type_code=1,sign_type_code=0,j_table_name="返却処理",invalid_flag_code=0,created_at=NOW(),updated_at=NOW();
#売上補充----------------------------------------
insert into master_app_production.table_rels set 
  order_type_code=6,table_name="sales",disp_sort=1,trans_type_code=1,sign_type_code=0, j_table_name="売上処理",invalid_flag_code=0,created_at=NOW(),updated_at=NOW();
insert into master_app_production.table_rels set 
  order_type_code=6,table_name="purchases",disp_sort=2,trans_type_code=1,sign_type_code=0,j_table_name="仕入処理",invalid_flag_code=0,created_at=NOW(),updated_at=NOW();
insert into master_app_production.table_rels set 
  order_type_code=6,table_name="restorations",disp_sort=3,trans_type_code=1,sign_type_code=0,j_table_name="返却処理",invalid_flag_code=0,created_at=NOW(),updated_at=NOW();
#注文買取----------------------------------------
insert into master_app_production.table_rels set 
  order_type_code=9,table_name="sales",disp_sort=1,trans_type_code=1,sign_type_code=0, j_table_name="売上処理",invalid_flag_code=0,created_at=NOW(),updated_at=NOW();
insert into master_app_production.table_rels set 
  order_type_code=9,table_name="purchases",disp_sort=2,trans_type_code=1,sign_type_code=0,j_table_name="仕入処理",invalid_flag_code=0,created_at=NOW(),updated_at=NOW();
insert into master_app_production.table_rels set 
  order_type_code=9,table_name="restorations",disp_sort=3,trans_type_code=1,sign_type_code=0,j_table_name="返却処理",invalid_flag_code=0,created_at=NOW(),updated_at=NOW();
#サンプル報告----------------------------------------
insert into master_app_production.table_rels set 
  order_type_code=7,table_name="samples",disp_sort=1,sign_type_code=0, j_table_name="サンプル処理",invalid_flag_code=0,created_at=NOW(),updated_at=NOW();
insert into master_app_production.table_rels set 
  order_type_code=7,table_name="restorations",disp_sort=2,trans_type_code=1,sign_type_code=0,j_table_name="返却処理",invalid_flag_code=0,created_at=NOW(),updated_at=NOW();
#サンプル補充----------------------------------------
insert into master_app_production.table_rels set 
  order_type_code=8,table_name="samples",disp_sort=1,sign_type_code=0, j_table_name="サンプル処理",invalid_flag_code=0,created_at=NOW(),updated_at=NOW();
insert into master_app_production.table_rels set 
  order_type_code=8,table_name="restorations",disp_sort=2,trans_type_code=1,sign_type_code=0,j_table_name="返却処理",invalid_flag_code=0,created_at=NOW(),updated_at=NOW();
#サンプル注文----------------------------------------
insert into master_app_production.table_rels set 
  order_type_code=16,table_name="samples",disp_sort=1,sign_type_code=0, j_table_name="サンプル処理",invalid_flag_code=0,created_at=NOW(),updated_at=NOW();
insert into master_app_production.table_rels set 
  order_type_code=16,table_name="restorations",disp_sort=2,trans_type_code=1,sign_type_code=0,j_table_name="返却処理",invalid_flag_code=0,created_at=NOW(),updated_at=NOW();


#売上を伴う受注区分は赤黒区分を設定する
update master_app_production.table_rels set sign_type_code=1 where order_type_code in (9,16,5,6,7,8) and sign_type_code is NULL;

#受注から発注はとりあえずなくす。
update master_app_production.table_rels set invalid_flag_code=1 where table_name="orders";

#受注から仕入で仕入れ価格を補完するもの
#alter table master_app_production.table_rels add purchase_price_flag_code smallint(6);
#update master_app_production.table_rels set purchase_price_flag_code=1 where table_name="purchases" and order_type_code in (5,6,9,10);

#ロット・シリアルをセットするもの（基本すべて1だが、出荷ロットを手で設定したい場合もあるだろうから、このカラムを用意する）
#alter table master_app_production.table_rels add lot_flag_code smallint(6);
#update master_app_production.table_rels set lot_flag_code=1;

#前の処理に依存するもの
alter table master_app_production.table_rels add dependency_flag_code smallint(6);

#いったん全部0にする。
update master_app_production.table_rels set dependency_flag_code = 0;
#売上報告、売上補充、サンプル報告、サンプル補充、売上、サンプルの出荷より後のものはdependency=1とする。
update master_app_production.table_rels set dependency_flag_code = 1 where order_type_code in (5,6,9,7,8,16) and table_name in ("sales", "samples", "restorations", "purchases");
update master_app_production.table_rels set dependency_flag_code = 1 where order_type_code=10 and table_name="sales";
update master_app_production.table_rels set dependency_flag_code = 1 where order_type_code=12 and table_name="shippings";
#赤伝の売上とサンプルはフロー内の最初のステップなので、dependency=0
update master_app_production.table_rels set dependency_flag_code = 0 where order_type_code in (5,6,9,7,8,16) and table_name in ("sales", "samples") and sign_type_code=0;



#自動で処理を実行できるもの(基本全て1だが、在庫移動の出荷だけは、納品先を選択できないので、0とする)
alter table master_app_production.table_rels add auto_flag_code smallint(6);
update master_app_production.table_rels set auto_flag_code=1;
update master_app_production.table_rels set auto_flag_code=0 where order_type_code=12 and table_name="shippings";

#基本受注からコピー=> null, 出荷からコピーするもの=> shippings
alter table master_app_production.table_rels add from_shipping_flag_code smallint(6);
update master_app_production.table_rels set from_shipping_flag_code=1 where order_type_code in (9,16) and table_name in ("sales","samples","purchases") and sign_type_code=1;

#タイポを直す
update master_app_production.table_rels set table_name="purchases" where table_name="purcahses";

#自動フローによる更新日時
alter table orders add auto_flow_at datetime after updated_at;
alter table accept_orders add auto_flow_at datetime after updated_at;
alter table purchases add auto_flow_at datetime after updated_at;
alter table arrivings add auto_flow_at datetime after updated_at;
alter table sales add auto_flow_at datetime after updated_at;
alter table shippings add auto_flow_at datetime after updated_at;
alter table restorations add auto_flow_at datetime after updated_at;
alter table samples add auto_flow_at datetime after updated_at;
alter table adjustments add auto_flow_at datetime after updated_at;
alter table disposals add auto_flow_at datetime after updated_at;


#dependency_flag_codeを修正-----------------------------
update master_app_production.table_rels set dependency_flag_code = 0 where order_type_code in (5,6,7,8) and table_name in ("sales", "samples", "purchases");
#貸出買い取りも修正
update master_app_production.table_rels set dependency_flag_code = 0 where order_type_code in (10) ;
#赤伝の売上のsales=purchase=0
update master_app_production.table_rels set dependency_flag_code = 0 where order_type_code in (9) and table_name in ("sales", "purchases") and sign_type_code=0;


#table_relsでaccept_ordersとordersとmanufacturesを区別する============================20131204
alter table master_app_production.table_rels add parent_table_name varchar(80) after order_type_code;
update master_app_production.table_rels set parent_table_name='accept_orders' where order_type_code not in (14,15,17,18,19,20);
update master_app_production.table_rels set parent_table_name='orders' where order_type_code in (14,15,17,18);
update master_app_production.table_rels set parent_table_name='manufactures' where order_type_code in (19,20);

insert into master_app_production.table_rels set
  order_type_code=9, 
  parent_table_name='orders',
  table_name='purchases',
  disp_sort=1,
  trans_type_code=2,
  j_table_name='仕入処理',
  invalid_flag_code=0,
  created_at=NOW(),
  updated_at=NOW(),
  dependency_flag_code=0,
  auto_flag_code=1;
insert into master_app_production.table_rels set
  order_type_code=10, 
  parent_table_name='orders',
  table_name='purchases',
  disp_sort=1,
  trans_type_code=3,
  j_table_name='仕入処理',
  invalid_flag_code=0,
  created_at=NOW(),
  updated_at=NOW(),
  dependency_flag_code=0,
  auto_flag_code=1;
