MagentoSchema.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. <?php
  2. namespace Webkul\BagistoApi\Tests\Unit\Migration;
  3. use Illuminate\Database\Schema\Blueprint;
  4. use Illuminate\Support\Facades\DB;
  5. use Illuminate\Support\Facades\Schema;
  6. class MagentoSchema
  7. {
  8. public const CUSTOMER_TYPE_ID = 1;
  9. public const ADDRESS_TYPE_ID = 2;
  10. public static function create(string $connection): void
  11. {
  12. $schema = Schema::connection($connection);
  13. $schema->create('eav_entity_type', function (Blueprint $table) {
  14. $table->integer('entity_type_id');
  15. $table->string('entity_type_code');
  16. });
  17. $schema->create('eav_attribute', function (Blueprint $table) {
  18. $table->integer('attribute_id');
  19. $table->integer('entity_type_id');
  20. $table->string('attribute_code');
  21. $table->string('backend_type');
  22. });
  23. $schema->create('customer_entity', function (Blueprint $table) {
  24. $table->integer('entity_id');
  25. $table->string('email')->nullable();
  26. $table->integer('is_active')->default(1);
  27. $table->integer('group_id')->default(1);
  28. $table->string('created_at')->nullable();
  29. $table->string('updated_at')->nullable();
  30. });
  31. foreach (['varchar', 'int', 'datetime', 'text'] as $type) {
  32. $schema->create('customer_entity_'.$type, function (Blueprint $table) {
  33. $table->increments('value_id');
  34. $table->integer('entity_id');
  35. $table->integer('attribute_id');
  36. $table->text('value')->nullable();
  37. });
  38. }
  39. $schema->create('customer_address_entity', function (Blueprint $table) {
  40. $table->integer('entity_id');
  41. $table->integer('parent_id');
  42. $table->integer('is_active')->default(1);
  43. $table->string('created_at')->nullable();
  44. });
  45. foreach (['varchar', 'int', 'text'] as $type) {
  46. $schema->create('customer_address_entity_'.$type, function (Blueprint $table) {
  47. $table->increments('value_id');
  48. $table->integer('entity_id');
  49. $table->integer('attribute_id');
  50. $table->text('value')->nullable();
  51. });
  52. }
  53. $db = DB::connection($connection);
  54. $db->table('eav_entity_type')->insert([
  55. ['entity_type_id' => self::CUSTOMER_TYPE_ID, 'entity_type_code' => 'customer'],
  56. ['entity_type_id' => self::ADDRESS_TYPE_ID, 'entity_type_code' => 'customer_address'],
  57. ]);
  58. $db->table('eav_attribute')->insert([
  59. ['attribute_id' => 5, 'entity_type_id' => self::CUSTOMER_TYPE_ID, 'attribute_code' => 'firstname', 'backend_type' => 'varchar'],
  60. ['attribute_id' => 7, 'entity_type_id' => self::CUSTOMER_TYPE_ID, 'attribute_code' => 'lastname', 'backend_type' => 'varchar'],
  61. ['attribute_id' => 11, 'entity_type_id' => self::CUSTOMER_TYPE_ID, 'attribute_code' => 'dob', 'backend_type' => 'datetime'],
  62. ['attribute_id' => 12, 'entity_type_id' => self::CUSTOMER_TYPE_ID, 'attribute_code' => 'password_hash', 'backend_type' => 'varchar'],
  63. ['attribute_id' => 13, 'entity_type_id' => self::CUSTOMER_TYPE_ID, 'attribute_code' => 'default_billing', 'backend_type' => 'int'],
  64. ['attribute_id' => 14, 'entity_type_id' => self::CUSTOMER_TYPE_ID, 'attribute_code' => 'default_shipping', 'backend_type' => 'int'],
  65. ['attribute_id' => 18, 'entity_type_id' => self::CUSTOMER_TYPE_ID, 'attribute_code' => 'gender', 'backend_type' => 'int'],
  66. ['attribute_id' => 88, 'entity_type_id' => self::CUSTOMER_TYPE_ID, 'attribute_code' => 'telephone', 'backend_type' => 'varchar'],
  67. ['attribute_id' => 20, 'entity_type_id' => self::ADDRESS_TYPE_ID, 'attribute_code' => 'firstname', 'backend_type' => 'varchar'],
  68. ['attribute_id' => 21, 'entity_type_id' => self::ADDRESS_TYPE_ID, 'attribute_code' => 'lastname', 'backend_type' => 'varchar'],
  69. ['attribute_id' => 22, 'entity_type_id' => self::ADDRESS_TYPE_ID, 'attribute_code' => 'company', 'backend_type' => 'varchar'],
  70. ['attribute_id' => 23, 'entity_type_id' => self::ADDRESS_TYPE_ID, 'attribute_code' => 'street', 'backend_type' => 'text'],
  71. ['attribute_id' => 24, 'entity_type_id' => self::ADDRESS_TYPE_ID, 'attribute_code' => 'city', 'backend_type' => 'varchar'],
  72. ['attribute_id' => 25, 'entity_type_id' => self::ADDRESS_TYPE_ID, 'attribute_code' => 'region', 'backend_type' => 'varchar'],
  73. ['attribute_id' => 26, 'entity_type_id' => self::ADDRESS_TYPE_ID, 'attribute_code' => 'postcode', 'backend_type' => 'varchar'],
  74. ['attribute_id' => 27, 'entity_type_id' => self::ADDRESS_TYPE_ID, 'attribute_code' => 'country_id', 'backend_type' => 'varchar'],
  75. ['attribute_id' => 28, 'entity_type_id' => self::ADDRESS_TYPE_ID, 'attribute_code' => 'telephone', 'backend_type' => 'varchar'],
  76. ]);
  77. $schema->create('sales_flat_order', function (Blueprint $table) {
  78. $table->integer('entity_id');
  79. $table->string('increment_id')->nullable();
  80. $table->integer('customer_id')->nullable();
  81. $table->string('customer_email')->nullable();
  82. $table->string('customer_firstname')->nullable();
  83. $table->string('customer_lastname')->nullable();
  84. $table->integer('customer_is_guest')->default(0);
  85. $table->string('status')->nullable();
  86. $table->string('state')->nullable();
  87. $table->integer('store_id')->nullable();
  88. $table->string('base_currency_code')->nullable();
  89. $table->string('order_currency_code')->nullable();
  90. $table->string('store_currency_code')->nullable();
  91. $table->decimal('grand_total', 12, 4)->default(0);
  92. $table->decimal('base_grand_total', 12, 4)->default(0);
  93. $table->decimal('subtotal', 12, 4)->default(0);
  94. $table->decimal('base_subtotal', 12, 4)->default(0);
  95. $table->decimal('subtotal_incl_tax', 12, 4)->nullable();
  96. $table->decimal('base_subtotal_incl_tax', 12, 4)->nullable();
  97. $table->decimal('tax_amount', 12, 4)->default(0);
  98. $table->decimal('base_tax_amount', 12, 4)->default(0);
  99. $table->decimal('discount_amount', 12, 4)->default(0);
  100. $table->decimal('base_discount_amount', 12, 4)->default(0);
  101. $table->decimal('shipping_amount', 12, 4)->default(0);
  102. $table->decimal('base_shipping_amount', 12, 4)->default(0);
  103. $table->decimal('shipping_incl_tax', 12, 4)->nullable();
  104. $table->decimal('base_shipping_incl_tax', 12, 4)->nullable();
  105. $table->decimal('shipping_tax_amount', 12, 4)->default(0);
  106. $table->decimal('base_shipping_tax_amount', 12, 4)->default(0);
  107. $table->string('shipping_method')->nullable();
  108. $table->string('shipping_description')->nullable();
  109. $table->string('coupon_code')->nullable();
  110. $table->integer('total_item_count')->default(0);
  111. $table->decimal('total_qty_ordered', 12, 4)->default(0);
  112. $table->decimal('total_invoiced', 12, 4)->default(0);
  113. $table->decimal('base_total_invoiced', 12, 4)->default(0);
  114. $table->string('created_at')->nullable();
  115. $table->string('updated_at')->nullable();
  116. });
  117. $schema->create('sales_flat_order_item', function (Blueprint $table) {
  118. $table->integer('item_id');
  119. $table->integer('order_id');
  120. $table->integer('parent_item_id')->nullable();
  121. $table->integer('product_id')->nullable();
  122. $table->string('product_type')->nullable();
  123. $table->string('sku')->nullable();
  124. $table->string('name')->nullable();
  125. $table->decimal('qty_ordered', 12, 4)->default(0);
  126. $table->decimal('qty_shipped', 12, 4)->default(0);
  127. $table->decimal('qty_invoiced', 12, 4)->default(0);
  128. $table->decimal('qty_canceled', 12, 4)->default(0);
  129. $table->decimal('qty_refunded', 12, 4)->default(0);
  130. $table->decimal('price', 12, 4)->default(0);
  131. $table->decimal('base_price', 12, 4)->default(0);
  132. $table->decimal('price_incl_tax', 12, 4)->nullable();
  133. $table->decimal('base_price_incl_tax', 12, 4)->nullable();
  134. $table->decimal('row_total', 12, 4)->default(0);
  135. $table->decimal('base_row_total', 12, 4)->default(0);
  136. $table->decimal('row_total_incl_tax', 12, 4)->nullable();
  137. $table->decimal('base_row_total_incl_tax', 12, 4)->nullable();
  138. $table->decimal('tax_amount', 12, 4)->default(0);
  139. $table->decimal('base_tax_amount', 12, 4)->default(0);
  140. $table->decimal('tax_percent', 12, 4)->default(0);
  141. $table->decimal('discount_amount', 12, 4)->default(0);
  142. $table->decimal('base_discount_amount', 12, 4)->default(0);
  143. $table->decimal('discount_percent', 12, 4)->default(0);
  144. $table->decimal('weight', 12, 4)->default(0);
  145. $table->decimal('row_weight', 12, 4)->default(0);
  146. $table->string('created_at')->nullable();
  147. });
  148. $schema->create('sales_flat_order_address', function (Blueprint $table) {
  149. $table->integer('entity_id');
  150. $table->integer('parent_id');
  151. $table->string('address_type')->nullable();
  152. $table->string('firstname')->nullable();
  153. $table->string('lastname')->nullable();
  154. $table->string('company')->nullable();
  155. $table->text('street')->nullable();
  156. $table->string('city')->nullable();
  157. $table->string('region')->nullable();
  158. $table->string('postcode')->nullable();
  159. $table->string('country_id')->nullable();
  160. $table->string('telephone')->nullable();
  161. $table->string('email')->nullable();
  162. });
  163. $schema->create('sales_flat_order_payment', function (Blueprint $table) {
  164. $table->integer('entity_id');
  165. $table->integer('parent_id');
  166. $table->string('method')->nullable();
  167. $table->string('last_trans_id')->nullable();
  168. $table->string('cc_type')->nullable();
  169. $table->string('cc_last4')->nullable();
  170. $table->decimal('amount_ordered', 12, 4)->nullable();
  171. $table->decimal('base_amount_ordered', 12, 4)->nullable();
  172. });
  173. }
  174. /**
  175. * @param array<string, mixed> $data
  176. */
  177. public static function seedCustomer(string $connection, array $data): void
  178. {
  179. $entityId = (int) $data['entity_id'];
  180. DB::connection($connection)->table('customer_entity')->insert([
  181. 'entity_id' => $entityId,
  182. 'email' => $data['email'],
  183. 'is_active' => $data['is_active'] ?? 1,
  184. 'group_id' => $data['group_id'] ?? 99,
  185. 'created_at' => $data['created_at'] ?? '2020-01-01 00:00:00',
  186. 'updated_at' => $data['updated_at'] ?? '2020-01-01 00:00:00',
  187. ]);
  188. $varchar = [
  189. 5 => $data['firstname'] ?? null,
  190. 7 => $data['lastname'] ?? null,
  191. 12 => $data['password_hash'] ?? null,
  192. 88 => $data['telephone'] ?? null,
  193. ];
  194. foreach ($varchar as $attributeId => $value) {
  195. if ($value === null || $value === '') {
  196. continue;
  197. }
  198. DB::connection($connection)->table('customer_entity_varchar')->insert([
  199. 'entity_id' => $entityId,
  200. 'attribute_id' => $attributeId,
  201. 'value' => $value,
  202. ]);
  203. }
  204. if (isset($data['gender'])) {
  205. DB::connection($connection)->table('customer_entity_int')->insert([
  206. 'entity_id' => $entityId,
  207. 'attribute_id' => 18,
  208. 'value' => $data['gender'],
  209. ]);
  210. }
  211. if (! empty($data['default_billing'])) {
  212. DB::connection($connection)->table('customer_entity_int')->insert([
  213. 'entity_id' => $entityId,
  214. 'attribute_id' => 13,
  215. 'value' => $data['default_billing'],
  216. ]);
  217. }
  218. if (! empty($data['default_shipping'])) {
  219. DB::connection($connection)->table('customer_entity_int')->insert([
  220. 'entity_id' => $entityId,
  221. 'attribute_id' => 14,
  222. 'value' => $data['default_shipping'],
  223. ]);
  224. }
  225. if (! empty($data['dob'])) {
  226. DB::connection($connection)->table('customer_entity_datetime')->insert([
  227. 'entity_id' => $entityId,
  228. 'attribute_id' => 11,
  229. 'value' => $data['dob'],
  230. ]);
  231. }
  232. }
  233. /**
  234. * @param array<string, mixed> $data
  235. */
  236. public static function seedAddress(string $connection, array $data): void
  237. {
  238. $entityId = (int) $data['entity_id'];
  239. DB::connection($connection)->table('customer_address_entity')->insert([
  240. 'entity_id' => $entityId,
  241. 'parent_id' => $data['parent_id'],
  242. 'is_active' => 1,
  243. 'created_at' => '2020-01-01 00:00:00',
  244. ]);
  245. $map = [
  246. 20 => ['table' => 'customer_address_entity_varchar', 'value' => $data['firstname'] ?? null],
  247. 21 => ['table' => 'customer_address_entity_varchar', 'value' => $data['lastname'] ?? null],
  248. 22 => ['table' => 'customer_address_entity_varchar', 'value' => $data['company'] ?? null],
  249. 23 => ['table' => 'customer_address_entity_text', 'value' => $data['street'] ?? null],
  250. 24 => ['table' => 'customer_address_entity_varchar', 'value' => $data['city'] ?? null],
  251. 25 => ['table' => 'customer_address_entity_varchar', 'value' => $data['region'] ?? null],
  252. 26 => ['table' => 'customer_address_entity_varchar', 'value' => $data['postcode'] ?? null],
  253. 27 => ['table' => 'customer_address_entity_varchar', 'value' => $data['country_id'] ?? null],
  254. 28 => ['table' => 'customer_address_entity_varchar', 'value' => $data['telephone'] ?? null],
  255. ];
  256. foreach ($map as $attributeId => $item) {
  257. if ($item['value'] === null || $item['value'] === '') {
  258. continue;
  259. }
  260. DB::connection($connection)->table($item['table'])->insert([
  261. 'entity_id' => $entityId,
  262. 'attribute_id' => $attributeId,
  263. 'value' => $item['value'],
  264. ]);
  265. }
  266. }
  267. /**
  268. * @param array<string, mixed> $data
  269. */
  270. public static function seedOrder(string $connection, array $data): void
  271. {
  272. $entityId = (int) $data['entity_id'];
  273. DB::connection($connection)->table('sales_flat_order')->insert([
  274. 'entity_id' => $entityId,
  275. 'increment_id' => $data['increment_id'] ?? (string) (100000000 + $entityId),
  276. 'customer_id' => $data['customer_id'] ?? null,
  277. 'customer_email' => $data['customer_email'] ?? 'guest@example.com',
  278. 'customer_firstname' => $data['customer_firstname'] ?? 'Jane',
  279. 'customer_lastname' => $data['customer_lastname'] ?? 'Doe',
  280. 'customer_is_guest' => $data['customer_is_guest'] ?? 0,
  281. 'status' => $data['status'] ?? 'complete',
  282. 'state' => $data['state'] ?? 'complete',
  283. 'store_id' => $data['store_id'] ?? 1,
  284. 'base_currency_code' => $data['base_currency_code'] ?? 'USD',
  285. 'order_currency_code' => $data['order_currency_code'] ?? 'USD',
  286. 'store_currency_code' => $data['store_currency_code'] ?? 'USD',
  287. 'grand_total' => $data['grand_total'] ?? 110,
  288. 'base_grand_total' => $data['base_grand_total'] ?? 110,
  289. 'subtotal' => $data['subtotal'] ?? 100,
  290. 'base_subtotal' => $data['base_subtotal'] ?? 100,
  291. 'subtotal_incl_tax' => $data['subtotal_incl_tax'] ?? 100,
  292. 'base_subtotal_incl_tax' => $data['base_subtotal_incl_tax'] ?? 100,
  293. 'tax_amount' => $data['tax_amount'] ?? 0,
  294. 'base_tax_amount' => $data['base_tax_amount'] ?? 0,
  295. 'discount_amount' => $data['discount_amount'] ?? 0,
  296. 'base_discount_amount' => $data['base_discount_amount'] ?? 0,
  297. 'shipping_amount' => $data['shipping_amount'] ?? 10,
  298. 'base_shipping_amount' => $data['base_shipping_amount'] ?? 10,
  299. 'shipping_incl_tax' => $data['shipping_incl_tax'] ?? 10,
  300. 'base_shipping_incl_tax' => $data['base_shipping_incl_tax'] ?? 10,
  301. 'shipping_tax_amount' => $data['shipping_tax_amount'] ?? 0,
  302. 'base_shipping_tax_amount'=> $data['base_shipping_tax_amount'] ?? 0,
  303. 'shipping_method' => $data['shipping_method'] ?? 'flatrate_flatrate',
  304. 'shipping_description' => $data['shipping_description'] ?? 'Flat Rate - Fixed',
  305. 'coupon_code' => $data['coupon_code'] ?? null,
  306. 'total_item_count' => $data['total_item_count'] ?? 1,
  307. 'total_qty_ordered' => $data['total_qty_ordered'] ?? 1,
  308. 'total_invoiced' => $data['total_invoiced'] ?? 110,
  309. 'base_total_invoiced' => $data['base_total_invoiced'] ?? 110,
  310. 'created_at' => $data['created_at'] ?? '2021-06-01 12:00:00',
  311. 'updated_at' => $data['updated_at'] ?? '2021-06-01 12:00:00',
  312. ]);
  313. foreach ($data['items'] ?? [] as $item) {
  314. self::seedOrderItem($connection, array_merge(['order_id' => $entityId], $item));
  315. }
  316. foreach ($data['addresses'] ?? [] as $address) {
  317. self::seedOrderAddress($connection, array_merge(['parent_id' => $entityId], $address));
  318. }
  319. if (! empty($data['payment'])) {
  320. self::seedOrderPayment($connection, array_merge(['parent_id' => $entityId], $data['payment']));
  321. }
  322. }
  323. /**
  324. * @param array<string, mixed> $data
  325. */
  326. public static function seedOrderItem(string $connection, array $data): void
  327. {
  328. DB::connection($connection)->table('sales_flat_order_item')->insert([
  329. 'item_id' => $data['item_id'],
  330. 'order_id' => $data['order_id'],
  331. 'parent_item_id' => $data['parent_item_id'] ?? null,
  332. 'product_id' => $data['product_id'] ?? null,
  333. 'product_type' => $data['product_type'] ?? 'simple',
  334. 'sku' => $data['sku'] ?? 'SKU-1',
  335. 'name' => $data['name'] ?? 'Test Product',
  336. 'qty_ordered' => $data['qty_ordered'] ?? 1,
  337. 'qty_shipped' => $data['qty_shipped'] ?? 1,
  338. 'qty_invoiced' => $data['qty_invoiced'] ?? 1,
  339. 'qty_canceled' => $data['qty_canceled'] ?? 0,
  340. 'qty_refunded' => $data['qty_refunded'] ?? 0,
  341. 'price' => $data['price'] ?? 100,
  342. 'base_price' => $data['base_price'] ?? 100,
  343. 'price_incl_tax' => $data['price_incl_tax'] ?? 100,
  344. 'base_price_incl_tax' => $data['base_price_incl_tax'] ?? 100,
  345. 'row_total' => $data['row_total'] ?? 100,
  346. 'base_row_total' => $data['base_row_total'] ?? 100,
  347. 'row_total_incl_tax' => $data['row_total_incl_tax'] ?? 100,
  348. 'base_row_total_incl_tax' => $data['base_row_total_incl_tax'] ?? 100,
  349. 'tax_amount' => $data['tax_amount'] ?? 0,
  350. 'base_tax_amount' => $data['base_tax_amount'] ?? 0,
  351. 'tax_percent' => $data['tax_percent'] ?? 0,
  352. 'discount_amount' => $data['discount_amount'] ?? 0,
  353. 'base_discount_amount' => $data['base_discount_amount'] ?? 0,
  354. 'discount_percent' => $data['discount_percent'] ?? 0,
  355. 'weight' => $data['weight'] ?? 1,
  356. 'row_weight' => $data['row_weight'] ?? 1,
  357. 'created_at' => $data['created_at'] ?? '2021-06-01 12:00:00',
  358. ]);
  359. }
  360. /**
  361. * @param array<string, mixed> $data
  362. */
  363. public static function seedOrderAddress(string $connection, array $data): void
  364. {
  365. DB::connection($connection)->table('sales_flat_order_address')->insert([
  366. 'entity_id' => $data['entity_id'],
  367. 'parent_id' => $data['parent_id'],
  368. 'address_type' => $data['address_type'] ?? 'billing',
  369. 'firstname' => $data['firstname'] ?? 'Jane',
  370. 'lastname' => $data['lastname'] ?? 'Doe',
  371. 'company' => $data['company'] ?? null,
  372. 'street' => $data['street'] ?? '123 Main St',
  373. 'city' => $data['city'] ?? 'Austin',
  374. 'region' => $data['region'] ?? 'TX',
  375. 'postcode' => $data['postcode'] ?? '78701',
  376. 'country_id' => $data['country_id'] ?? 'US',
  377. 'telephone' => $data['telephone'] ?? '5551112222',
  378. 'email' => $data['email'] ?? null,
  379. ]);
  380. }
  381. /**
  382. * @param array<string, mixed> $data
  383. */
  384. public static function seedOrderPayment(string $connection, array $data): void
  385. {
  386. DB::connection($connection)->table('sales_flat_order_payment')->insert([
  387. 'entity_id' => $data['entity_id'],
  388. 'parent_id' => $data['parent_id'],
  389. 'method' => $data['method'] ?? 'paypal_express',
  390. 'last_trans_id' => $data['last_trans_id'] ?? null,
  391. 'cc_type' => $data['cc_type'] ?? null,
  392. 'cc_last4' => $data['cc_last4'] ?? null,
  393. 'amount_ordered' => $data['amount_ordered'] ?? 110,
  394. 'base_amount_ordered' => $data['base_amount_ordered'] ?? 110,
  395. ]);
  396. }
  397. }