暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

001_add_company_schema.sql 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. -- Migration script to add company model to existing schema
  2. -- This script should be run after the Go schema changes have been applied to the database
  3. -- 1. Ensure company dimension types exist (they should be inserted by init_defaults.go, but just in case)
  4. INSERT IGNORE INTO master_company_dimension (dimension_id, dimension_type, dimension_code, dimension_name, dimension_desc, sort_order, is_active, created_at, updated_at, data_date)
  5. VALUES
  6. (1000, 'COMPANY_TYPE', 'SUPPLIER', '供应商', '供应商类型', 1, 1, NOW(), NULL, CURDATE()),
  7. (1001, 'COMPANY_TYPE', 'SUBSIDIARY', '子公司', '子公司类型', 2, 1, NOW(), NULL, CURDATE()),
  8. (1002, 'COMPANY_TYPE', 'AGENT', '代理商', '代理商类型', 3, 1, NOW(), NULL, CURDATE()),
  9. (1003, 'COMPANY_TYPE', 'WHOLESALER', '批发商', '批发商类型', 4, 1, NOW(), NULL, CURDATE()),
  10. (1004, 'COMPANY_TYPE', 'GROUP', '集团', '集团类型', 5, 1, NOW(), NULL, CURDATE()),
  11. (1005, 'COMPANY_TYPE', 'FRANCHISEE', '加盟商', '加盟商类型', 6, 1, NOW(), NULL, CURDATE()),
  12. (1006, 'COMPANY_TYPE', 'CUSTOMER', '客户', '客户类型', 7, 1, NOW(), NULL, CURDATE()),
  13. (1007, 'COMPANY_TYPE', 'LOGISTICS', '物流公司', '物流公司类型', 8, 1, NOW(), NULL, CURDATE());
  14. -- 2. Create a default company for existing stores (if no company exists)
  15. INSERT IGNORE INTO master_company (company_id, company_code, company_name, company_type, tax_id, contact_name, contact_phone, address, is_active, created_at, updated_at, data_date)
  16. SELECT
  17. CONCAT('COMP_DEFAULT_', s.shop_id) as company_id,
  18. CONCAT('DEFAULT_', s.shop_code) as company_code,
  19. CONCAT(s.shop_name, ' (默认公司)') as company_name,
  20. 'SUBSIDIARY' as company_type,
  21. NULL as tax_id,
  22. s.manager_name as contact_name,
  23. s.contact_phone as contact_phone,
  24. s.address,
  25. 1 as is_active,
  26. NOW() as created_at,
  27. NULL as updated_at,
  28. CURDATE() as data_date
  29. FROM master_store s
  30. WHERE NOT EXISTS (SELECT 1 FROM master_company c WHERE c.company_id = s.shop_id)
  31. LIMIT 1; -- Only create one default company for all stores? Actually each store may belong to different companies. We'll assign a single default company for now.
  32. -- 3. Update store table with company_id from default company
  33. UPDATE master_store s
  34. SET company_id = (SELECT company_id FROM master_company WHERE company_code LIKE 'DEFAULT_%' LIMIT 1)
  35. WHERE company_id IS NULL OR company_id = '';
  36. -- 4. Create companies for distinct suppliers from purchase receipts
  37. INSERT IGNORE INTO master_company (company_id, company_code, company_name, company_type, tax_id, contact_name, contact_phone, address, is_active, created_at, updated_at, data_date)
  38. SELECT DISTINCT
  39. supplier_id as company_id,
  40. supplier_code as company_code,
  41. supplier_name as company_name,
  42. 'SUPPLIER' as company_type,
  43. NULL as tax_id,
  44. NULL as contact_name,
  45. NULL as contact_phone,
  46. NULL as address,
  47. 1 as is_active,
  48. NOW() as created_at,
  49. NULL as updated_at,
  50. CURDATE() as data_date
  51. FROM bill_purchase_receipt
  52. WHERE supplier_id IS NOT NULL AND supplier_id != ''
  53. AND NOT EXISTS (SELECT 1 FROM master_company c WHERE c.company_id = bill_purchase_receipt.supplier_id);
  54. -- 5. Update purchase receipt table with from_company_id (supplier) and to_company_id (store's company)
  55. UPDATE bill_purchase_receipt bpr
  56. JOIN master_store s ON bpr.shop_id = s.shop_id
  57. SET bpr.from_company_id = bpr.supplier_id,
  58. bpr.to_company_id = s.company_id
  59. WHERE bpr.from_company_id IS NULL OR bpr.from_company_id = '';
  60. -- 6. Update wholesale sales table with from_company_id (store's company) and to_company_id (customer company)
  61. -- Note: wholesale sales currently have supplier_id? Actually wholesale sales may have customer fields. We'll use shop's company as from_company, and create companies for customers if needed.
  62. -- First create companies for distinct wholesale customers
  63. INSERT IGNORE INTO master_company (company_id, company_code, company_name, company_type, tax_id, contact_name, contact_phone, address, is_active, created_at, updated_at, data_date)
  64. SELECT DISTINCT
  65. customer_id as company_id,
  66. customer_code as company_code,
  67. customer_name as company_name,
  68. 'CUSTOMER' as company_type,
  69. NULL as tax_id,
  70. NULL as contact_name,
  71. NULL as contact_phone,
  72. NULL as address,
  73. 1 as is_active,
  74. NOW() as created_at,
  75. NULL as updated_at,
  76. CURDATE() as data_date
  77. FROM bill_sales_wholesale
  78. WHERE customer_id IS NOT NULL AND customer_id != ''
  79. AND NOT EXISTS (SELECT 1 FROM master_company c WHERE c.company_id = bill_sales_wholesale.customer_id);
  80. -- Update wholesale sales with company IDs
  81. UPDATE bill_sales_wholesale bsw
  82. JOIN master_store s ON bsw.shop_id = s.shop_id
  83. SET bsw.from_company_id = s.company_id,
  84. bsw.to_company_id = bsw.customer_id
  85. WHERE bsw.from_company_id IS NULL OR bsw.from_company_id = '';
  86. -- 7. Update stock transfer tables with company IDs
  87. -- For transfers, from_company_id is the source store's company, to_company_id is destination store's company
  88. UPDATE bill_stock_transfer_ship bsts
  89. JOIN master_store s_src ON bsts.from_shop_id = s_src.shop_id
  90. JOIN master_store s_dest ON bsts.to_shop_id = s_dest.shop_id
  91. SET bsts.from_company_id = s_src.company_id,
  92. bsts.to_company_id = s_dest.company_id
  93. WHERE bsts.from_company_id IS NULL OR bsts.from_company_id = '';
  94. UPDATE bill_stock_transfer_receive bstr
  95. JOIN master_store s_src ON bstr.from_shop_id = s_src.shop_id
  96. JOIN master_store s_dest ON bstr.to_shop_id = s_dest.shop_id
  97. SET bstr.from_company_id = s_src.company_id,
  98. bstr.to_company_id = s_dest.company_id
  99. WHERE bstr.from_company_id IS NULL OR bstr.from_company_id = '';
  100. -- 8. Update inventory count table with company_id from store
  101. UPDATE bill_inventory_count bic
  102. JOIN master_store s ON bic.shop_id = s.shop_id
  103. SET bic.company_id = s.company_id
  104. WHERE bic.company_id IS NULL OR bic.company_id = '';
  105. -- 9. Update retail sales table with company_id from store
  106. UPDATE bill_sales_retail bsr
  107. JOIN master_store s ON bsr.shop_id = s.shop_id
  108. SET bsr.company_id = s.company_id
  109. WHERE bsr.company_id IS NULL OR bsr.company_id = '';
  110. -- 10. Optional: Clean up old supplier fields from product table (if you want to remove them physically, but we already removed from schema)
  111. -- ALTER TABLE master_product DROP COLUMN supplier_id, DROP COLUMN supplier_code, DROP COLUMN supplier_name;
  112. -- Migration completed
  113. SELECT 'Company schema migration completed successfully' as message;