MagentoSchema.php 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  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 const CATEGORY_TYPE_ID = 3;
  11. public const PRODUCT_TYPE_ID = 4;
  12. public const ATTR_PRODUCT_NAME = 70;
  13. public const ATTR_PRODUCT_DESCRIPTION = 71;
  14. public const ATTR_PRODUCT_SHORT_DESCRIPTION = 72;
  15. public const ATTR_PRODUCT_PRICE = 73;
  16. public const ATTR_PRODUCT_SPECIAL_PRICE = 74;
  17. public const ATTR_PRODUCT_COST = 75;
  18. public const ATTR_PRODUCT_WEIGHT = 76;
  19. public const ATTR_PRODUCT_STATUS = 77;
  20. public const ATTR_PRODUCT_META_TITLE = 78;
  21. public const ATTR_PRODUCT_META_KEYWORD = 79;
  22. public const ATTR_PRODUCT_META_DESCRIPTION = 80;
  23. public const ATTR_PRODUCT_URL_KEY = 81;
  24. public const ATTR_PRODUCT_HAIR_COLORR = 90;
  25. public const ATTR_CATEGORY_NAME = 111;
  26. public const HAIR_COLORR_OPTION_ID = 501;
  27. public static function create(string $connection): void
  28. {
  29. $schema = Schema::connection($connection);
  30. $schema->create('eav_entity_type', function (Blueprint $table) {
  31. $table->integer('entity_type_id');
  32. $table->string('entity_type_code');
  33. });
  34. $schema->create('eav_attribute', function (Blueprint $table) {
  35. $table->integer('attribute_id');
  36. $table->integer('entity_type_id');
  37. $table->string('attribute_code');
  38. $table->string('backend_type');
  39. $table->string('frontend_label')->nullable();
  40. $table->string('frontend_input')->nullable();
  41. $table->integer('is_required')->default(0);
  42. $table->integer('is_user_defined')->default(0);
  43. });
  44. $schema->create('customer_entity', function (Blueprint $table) {
  45. $table->integer('entity_id');
  46. $table->string('email')->nullable();
  47. $table->integer('is_active')->default(1);
  48. $table->integer('group_id')->default(1);
  49. $table->string('created_at')->nullable();
  50. $table->string('updated_at')->nullable();
  51. });
  52. foreach (['varchar', 'int', 'datetime', 'text'] as $type) {
  53. $schema->create('customer_entity_'.$type, function (Blueprint $table) {
  54. $table->increments('value_id');
  55. $table->integer('entity_id');
  56. $table->integer('attribute_id');
  57. $table->text('value')->nullable();
  58. });
  59. }
  60. $schema->create('customer_address_entity', function (Blueprint $table) {
  61. $table->integer('entity_id');
  62. $table->integer('parent_id');
  63. $table->integer('is_active')->default(1);
  64. $table->string('created_at')->nullable();
  65. });
  66. foreach (['varchar', 'int', 'text'] as $type) {
  67. $schema->create('customer_address_entity_'.$type, function (Blueprint $table) {
  68. $table->increments('value_id');
  69. $table->integer('entity_id');
  70. $table->integer('attribute_id');
  71. $table->text('value')->nullable();
  72. });
  73. }
  74. $db = DB::connection($connection);
  75. $db->table('eav_entity_type')->insert([
  76. ['entity_type_id' => self::CUSTOMER_TYPE_ID, 'entity_type_code' => 'customer'],
  77. ['entity_type_id' => self::ADDRESS_TYPE_ID, 'entity_type_code' => 'customer_address'],
  78. ]);
  79. $db->table('eav_attribute')->insert([
  80. ['attribute_id' => 5, 'entity_type_id' => self::CUSTOMER_TYPE_ID, 'attribute_code' => 'firstname', 'backend_type' => 'varchar'],
  81. ['attribute_id' => 7, 'entity_type_id' => self::CUSTOMER_TYPE_ID, 'attribute_code' => 'lastname', 'backend_type' => 'varchar'],
  82. ['attribute_id' => 11, 'entity_type_id' => self::CUSTOMER_TYPE_ID, 'attribute_code' => 'dob', 'backend_type' => 'datetime'],
  83. ['attribute_id' => 12, 'entity_type_id' => self::CUSTOMER_TYPE_ID, 'attribute_code' => 'password_hash', 'backend_type' => 'varchar'],
  84. ['attribute_id' => 13, 'entity_type_id' => self::CUSTOMER_TYPE_ID, 'attribute_code' => 'default_billing', 'backend_type' => 'int'],
  85. ['attribute_id' => 14, 'entity_type_id' => self::CUSTOMER_TYPE_ID, 'attribute_code' => 'default_shipping', 'backend_type' => 'int'],
  86. ['attribute_id' => 18, 'entity_type_id' => self::CUSTOMER_TYPE_ID, 'attribute_code' => 'gender', 'backend_type' => 'int'],
  87. ['attribute_id' => 88, 'entity_type_id' => self::CUSTOMER_TYPE_ID, 'attribute_code' => 'telephone', 'backend_type' => 'varchar'],
  88. ['attribute_id' => 89, 'entity_type_id' => self::CUSTOMER_TYPE_ID, 'attribute_code' => 'customer_source', 'backend_type' => 'int'],
  89. ['attribute_id' => 20, 'entity_type_id' => self::ADDRESS_TYPE_ID, 'attribute_code' => 'firstname', 'backend_type' => 'varchar'],
  90. ['attribute_id' => 21, 'entity_type_id' => self::ADDRESS_TYPE_ID, 'attribute_code' => 'lastname', 'backend_type' => 'varchar'],
  91. ['attribute_id' => 22, 'entity_type_id' => self::ADDRESS_TYPE_ID, 'attribute_code' => 'company', 'backend_type' => 'varchar'],
  92. ['attribute_id' => 23, 'entity_type_id' => self::ADDRESS_TYPE_ID, 'attribute_code' => 'street', 'backend_type' => 'text'],
  93. ['attribute_id' => 24, 'entity_type_id' => self::ADDRESS_TYPE_ID, 'attribute_code' => 'city', 'backend_type' => 'varchar'],
  94. ['attribute_id' => 25, 'entity_type_id' => self::ADDRESS_TYPE_ID, 'attribute_code' => 'region', 'backend_type' => 'varchar'],
  95. ['attribute_id' => 26, 'entity_type_id' => self::ADDRESS_TYPE_ID, 'attribute_code' => 'postcode', 'backend_type' => 'varchar'],
  96. ['attribute_id' => 27, 'entity_type_id' => self::ADDRESS_TYPE_ID, 'attribute_code' => 'country_id', 'backend_type' => 'varchar'],
  97. ['attribute_id' => 28, 'entity_type_id' => self::ADDRESS_TYPE_ID, 'attribute_code' => 'telephone', 'backend_type' => 'varchar'],
  98. ]);
  99. $schema->create('newsletter_subscriber', function (Blueprint $table) {
  100. $table->integer('subscriber_id');
  101. $table->integer('store_id')->default(0);
  102. $table->string('change_status_at')->nullable();
  103. $table->integer('customer_id')->default(0);
  104. $table->string('subscriber_email')->nullable();
  105. $table->integer('subscriber_status')->default(0);
  106. $table->string('subscriber_confirm_code')->nullable();
  107. });
  108. $schema->create('sales_flat_order', function (Blueprint $table) {
  109. $table->integer('entity_id');
  110. $table->string('increment_id')->nullable();
  111. $table->integer('customer_id')->nullable();
  112. $table->string('customer_email')->nullable();
  113. $table->string('customer_firstname')->nullable();
  114. $table->string('customer_lastname')->nullable();
  115. $table->integer('customer_is_guest')->default(0);
  116. $table->string('status')->nullable();
  117. $table->string('state')->nullable();
  118. $table->integer('store_id')->nullable();
  119. $table->string('base_currency_code')->nullable();
  120. $table->string('order_currency_code')->nullable();
  121. $table->string('store_currency_code')->nullable();
  122. $table->decimal('grand_total', 12, 4)->default(0);
  123. $table->decimal('base_grand_total', 12, 4)->default(0);
  124. $table->decimal('subtotal', 12, 4)->default(0);
  125. $table->decimal('base_subtotal', 12, 4)->default(0);
  126. $table->decimal('subtotal_incl_tax', 12, 4)->nullable();
  127. $table->decimal('base_subtotal_incl_tax', 12, 4)->nullable();
  128. $table->decimal('tax_amount', 12, 4)->default(0);
  129. $table->decimal('base_tax_amount', 12, 4)->default(0);
  130. $table->decimal('discount_amount', 12, 4)->default(0);
  131. $table->decimal('base_discount_amount', 12, 4)->default(0);
  132. $table->decimal('shipping_amount', 12, 4)->default(0);
  133. $table->decimal('base_shipping_amount', 12, 4)->default(0);
  134. $table->decimal('shipping_incl_tax', 12, 4)->nullable();
  135. $table->decimal('base_shipping_incl_tax', 12, 4)->nullable();
  136. $table->decimal('shipping_tax_amount', 12, 4)->default(0);
  137. $table->decimal('base_shipping_tax_amount', 12, 4)->default(0);
  138. $table->string('shipping_method')->nullable();
  139. $table->string('shipping_description')->nullable();
  140. $table->string('coupon_code')->nullable();
  141. $table->integer('mw_rewardpoint')->default(0);
  142. $table->decimal('mw_rewardpoint_discount', 12, 4)->default(0);
  143. $table->decimal('mw_rewardpoint_discount_show', 12, 4)->default(0);
  144. $table->decimal('amcheckoutfees_amount', 12, 4)->default(0);
  145. $table->decimal('base_amcheckoutfees_amount', 12, 4)->default(0);
  146. $table->integer('total_item_count')->default(0);
  147. $table->decimal('total_qty_ordered', 12, 4)->default(0);
  148. $table->decimal('total_invoiced', 12, 4)->default(0);
  149. $table->decimal('base_total_invoiced', 12, 4)->default(0);
  150. $table->string('created_at')->nullable();
  151. $table->string('updated_at')->nullable();
  152. });
  153. $schema->create('sales_flat_order_item', function (Blueprint $table) {
  154. $table->integer('item_id');
  155. $table->integer('order_id');
  156. $table->integer('parent_item_id')->nullable();
  157. $table->integer('product_id')->nullable();
  158. $table->string('product_type')->nullable();
  159. $table->string('sku')->nullable();
  160. $table->string('name')->nullable();
  161. $table->decimal('qty_ordered', 12, 4)->default(0);
  162. $table->decimal('qty_shipped', 12, 4)->default(0);
  163. $table->decimal('qty_invoiced', 12, 4)->default(0);
  164. $table->decimal('qty_canceled', 12, 4)->default(0);
  165. $table->decimal('qty_refunded', 12, 4)->default(0);
  166. $table->decimal('price', 12, 4)->default(0);
  167. $table->decimal('base_price', 12, 4)->default(0);
  168. $table->decimal('price_incl_tax', 12, 4)->nullable();
  169. $table->decimal('base_price_incl_tax', 12, 4)->nullable();
  170. $table->decimal('row_total', 12, 4)->default(0);
  171. $table->decimal('base_row_total', 12, 4)->default(0);
  172. $table->decimal('row_total_incl_tax', 12, 4)->nullable();
  173. $table->decimal('base_row_total_incl_tax', 12, 4)->nullable();
  174. $table->decimal('tax_amount', 12, 4)->default(0);
  175. $table->decimal('base_tax_amount', 12, 4)->default(0);
  176. $table->decimal('tax_percent', 12, 4)->default(0);
  177. $table->decimal('discount_amount', 12, 4)->default(0);
  178. $table->decimal('base_discount_amount', 12, 4)->default(0);
  179. $table->decimal('discount_percent', 12, 4)->default(0);
  180. $table->decimal('weight', 12, 4)->default(0);
  181. $table->decimal('row_weight', 12, 4)->default(0);
  182. $table->string('created_at')->nullable();
  183. });
  184. $schema->create('sales_flat_order_address', function (Blueprint $table) {
  185. $table->integer('entity_id');
  186. $table->integer('parent_id');
  187. $table->string('address_type')->nullable();
  188. $table->string('firstname')->nullable();
  189. $table->string('lastname')->nullable();
  190. $table->string('company')->nullable();
  191. $table->text('street')->nullable();
  192. $table->string('city')->nullable();
  193. $table->string('region')->nullable();
  194. $table->string('postcode')->nullable();
  195. $table->string('country_id')->nullable();
  196. $table->string('telephone')->nullable();
  197. $table->string('email')->nullable();
  198. });
  199. $schema->create('sales_flat_order_payment', function (Blueprint $table) {
  200. $table->integer('entity_id');
  201. $table->integer('parent_id');
  202. $table->string('method')->nullable();
  203. $table->string('last_trans_id')->nullable();
  204. $table->string('cc_type')->nullable();
  205. $table->string('cc_last4')->nullable();
  206. $table->decimal('amount_ordered', 12, 4)->nullable();
  207. $table->decimal('base_amount_ordered', 12, 4)->nullable();
  208. });
  209. $schema->create('mw_reward_point_order', function (Blueprint $table) {
  210. $table->integer('order_id');
  211. $table->integer('reward_point')->default(0);
  212. $table->integer('rewardpoint_sell_product')->default(0);
  213. $table->integer('earn_rewardpoint')->default(0);
  214. $table->float('money')->default(0);
  215. $table->string('reward_point_money_rate')->nullable();
  216. });
  217. self::createProductCatalog($connection);
  218. }
  219. /**
  220. * @param array<string, mixed> $data
  221. */
  222. public static function seedCustomer(string $connection, array $data): void
  223. {
  224. $entityId = (int) $data['entity_id'];
  225. DB::connection($connection)->table('customer_entity')->insert([
  226. 'entity_id' => $entityId,
  227. 'email' => $data['email'],
  228. 'is_active' => $data['is_active'] ?? 1,
  229. 'group_id' => $data['group_id'] ?? 99,
  230. 'created_at' => $data['created_at'] ?? '2020-01-01 00:00:00',
  231. 'updated_at' => $data['updated_at'] ?? '2020-01-01 00:00:00',
  232. ]);
  233. $varchar = [
  234. 5 => $data['firstname'] ?? null,
  235. 7 => $data['lastname'] ?? null,
  236. 12 => $data['password_hash'] ?? null,
  237. 88 => $data['telephone'] ?? null,
  238. ];
  239. foreach ($varchar as $attributeId => $value) {
  240. if ($value === null || $value === '') {
  241. continue;
  242. }
  243. DB::connection($connection)->table('customer_entity_varchar')->insert([
  244. 'entity_id' => $entityId,
  245. 'attribute_id' => $attributeId,
  246. 'value' => $value,
  247. ]);
  248. }
  249. if (isset($data['gender'])) {
  250. DB::connection($connection)->table('customer_entity_int')->insert([
  251. 'entity_id' => $entityId,
  252. 'attribute_id' => 18,
  253. 'value' => $data['gender'],
  254. ]);
  255. }
  256. if (isset($data['customer_source'])) {
  257. DB::connection($connection)->table('customer_entity_int')->insert([
  258. 'entity_id' => $entityId,
  259. 'attribute_id' => 89,
  260. 'value' => $data['customer_source'],
  261. ]);
  262. }
  263. if (! empty($data['default_billing'])) {
  264. DB::connection($connection)->table('customer_entity_int')->insert([
  265. 'entity_id' => $entityId,
  266. 'attribute_id' => 13,
  267. 'value' => $data['default_billing'],
  268. ]);
  269. }
  270. if (! empty($data['default_shipping'])) {
  271. DB::connection($connection)->table('customer_entity_int')->insert([
  272. 'entity_id' => $entityId,
  273. 'attribute_id' => 14,
  274. 'value' => $data['default_shipping'],
  275. ]);
  276. }
  277. if (! empty($data['dob'])) {
  278. DB::connection($connection)->table('customer_entity_datetime')->insert([
  279. 'entity_id' => $entityId,
  280. 'attribute_id' => 11,
  281. 'value' => $data['dob'],
  282. ]);
  283. }
  284. }
  285. /**
  286. * @param array<string, mixed> $data
  287. */
  288. public static function seedAddress(string $connection, array $data): void
  289. {
  290. $entityId = (int) $data['entity_id'];
  291. DB::connection($connection)->table('customer_address_entity')->insert([
  292. 'entity_id' => $entityId,
  293. 'parent_id' => $data['parent_id'],
  294. 'is_active' => 1,
  295. 'created_at' => '2020-01-01 00:00:00',
  296. ]);
  297. $map = [
  298. 20 => ['table' => 'customer_address_entity_varchar', 'value' => $data['firstname'] ?? null],
  299. 21 => ['table' => 'customer_address_entity_varchar', 'value' => $data['lastname'] ?? null],
  300. 22 => ['table' => 'customer_address_entity_varchar', 'value' => $data['company'] ?? null],
  301. 23 => ['table' => 'customer_address_entity_text', 'value' => $data['street'] ?? null],
  302. 24 => ['table' => 'customer_address_entity_varchar', 'value' => $data['city'] ?? null],
  303. 25 => ['table' => 'customer_address_entity_varchar', 'value' => $data['region'] ?? null],
  304. 26 => ['table' => 'customer_address_entity_varchar', 'value' => $data['postcode'] ?? null],
  305. 27 => ['table' => 'customer_address_entity_varchar', 'value' => $data['country_id'] ?? null],
  306. 28 => ['table' => 'customer_address_entity_varchar', 'value' => $data['telephone'] ?? null],
  307. ];
  308. foreach ($map as $attributeId => $item) {
  309. if ($item['value'] === null || $item['value'] === '') {
  310. continue;
  311. }
  312. DB::connection($connection)->table($item['table'])->insert([
  313. 'entity_id' => $entityId,
  314. 'attribute_id' => $attributeId,
  315. 'value' => $item['value'],
  316. ]);
  317. }
  318. }
  319. /**
  320. * @param array<string, mixed> $data
  321. */
  322. public static function seedSubscriber(string $connection, array $data): void
  323. {
  324. DB::connection($connection)->table('newsletter_subscriber')->insert([
  325. 'subscriber_id' => $data['subscriber_id'],
  326. 'store_id' => $data['store_id'] ?? 1,
  327. 'change_status_at' => $data['change_status_at'] ?? '2020-01-01 00:00:00',
  328. 'customer_id' => $data['customer_id'] ?? 0,
  329. 'subscriber_email' => $data['subscriber_email'],
  330. 'subscriber_status' => $data['subscriber_status'] ?? 1,
  331. 'subscriber_confirm_code' => $data['subscriber_confirm_code'] ?? 'confirmcode',
  332. ]);
  333. }
  334. /**
  335. * @param array<string, mixed> $data
  336. */
  337. public static function seedOrder(string $connection, array $data): void
  338. {
  339. $entityId = (int) $data['entity_id'];
  340. DB::connection($connection)->table('sales_flat_order')->insert([
  341. 'entity_id' => $entityId,
  342. 'increment_id' => $data['increment_id'] ?? (string) (100000000 + $entityId),
  343. 'customer_id' => $data['customer_id'] ?? null,
  344. 'customer_email' => $data['customer_email'] ?? 'guest@example.com',
  345. 'customer_firstname' => $data['customer_firstname'] ?? 'Jane',
  346. 'customer_lastname' => $data['customer_lastname'] ?? 'Doe',
  347. 'customer_is_guest' => $data['customer_is_guest'] ?? 0,
  348. 'status' => $data['status'] ?? 'complete',
  349. 'state' => $data['state'] ?? 'complete',
  350. 'store_id' => $data['store_id'] ?? 1,
  351. 'base_currency_code' => $data['base_currency_code'] ?? 'USD',
  352. 'order_currency_code' => $data['order_currency_code'] ?? 'USD',
  353. 'store_currency_code' => $data['store_currency_code'] ?? 'USD',
  354. 'grand_total' => $data['grand_total'] ?? 110,
  355. 'base_grand_total' => $data['base_grand_total'] ?? 110,
  356. 'subtotal' => $data['subtotal'] ?? 100,
  357. 'base_subtotal' => $data['base_subtotal'] ?? 100,
  358. 'subtotal_incl_tax' => $data['subtotal_incl_tax'] ?? 100,
  359. 'base_subtotal_incl_tax' => $data['base_subtotal_incl_tax'] ?? 100,
  360. 'tax_amount' => $data['tax_amount'] ?? 0,
  361. 'base_tax_amount' => $data['base_tax_amount'] ?? 0,
  362. 'discount_amount' => $data['discount_amount'] ?? 0,
  363. 'base_discount_amount' => $data['base_discount_amount'] ?? 0,
  364. 'shipping_amount' => $data['shipping_amount'] ?? 10,
  365. 'base_shipping_amount' => $data['base_shipping_amount'] ?? 10,
  366. 'shipping_incl_tax' => $data['shipping_incl_tax'] ?? 10,
  367. 'base_shipping_incl_tax' => $data['base_shipping_incl_tax'] ?? 10,
  368. 'shipping_tax_amount' => $data['shipping_tax_amount'] ?? 0,
  369. 'base_shipping_tax_amount'=> $data['base_shipping_tax_amount'] ?? 0,
  370. 'shipping_method' => $data['shipping_method'] ?? 'flatrate_flatrate',
  371. 'shipping_description' => $data['shipping_description'] ?? 'Flat Rate - Fixed',
  372. 'coupon_code' => $data['coupon_code'] ?? null,
  373. 'mw_rewardpoint' => $data['mw_rewardpoint'] ?? 0,
  374. 'mw_rewardpoint_discount' => $data['mw_rewardpoint_discount'] ?? 0,
  375. 'mw_rewardpoint_discount_show' => $data['mw_rewardpoint_discount_show'] ?? 0,
  376. 'amcheckoutfees_amount' => $data['amcheckoutfees_amount'] ?? 0,
  377. 'base_amcheckoutfees_amount' => $data['base_amcheckoutfees_amount'] ?? 0,
  378. 'total_item_count' => $data['total_item_count'] ?? 1,
  379. 'total_qty_ordered' => $data['total_qty_ordered'] ?? 1,
  380. 'total_invoiced' => $data['total_invoiced'] ?? 110,
  381. 'base_total_invoiced' => $data['base_total_invoiced'] ?? 110,
  382. 'created_at' => $data['created_at'] ?? '2021-06-01 12:00:00',
  383. 'updated_at' => $data['updated_at'] ?? '2021-06-01 12:00:00',
  384. ]);
  385. foreach ($data['items'] ?? [] as $item) {
  386. self::seedOrderItem($connection, array_merge(['order_id' => $entityId], $item));
  387. }
  388. foreach ($data['addresses'] ?? [] as $address) {
  389. self::seedOrderAddress($connection, array_merge(['parent_id' => $entityId], $address));
  390. }
  391. if (! empty($data['payment'])) {
  392. self::seedOrderPayment($connection, array_merge(['parent_id' => $entityId], $data['payment']));
  393. }
  394. if (! empty($data['reward_point_order'])) {
  395. self::seedRewardPointOrder($connection, array_merge(['order_id' => $entityId], $data['reward_point_order']));
  396. }
  397. }
  398. /**
  399. * @param array<string, mixed> $data
  400. */
  401. public static function seedRewardPointOrder(string $connection, array $data): void
  402. {
  403. DB::connection($connection)->table('mw_reward_point_order')->insert([
  404. 'order_id' => $data['order_id'],
  405. 'reward_point' => $data['reward_point'] ?? 0,
  406. 'rewardpoint_sell_product' => $data['rewardpoint_sell_product'] ?? 0,
  407. 'earn_rewardpoint' => $data['earn_rewardpoint'] ?? 0,
  408. 'money' => $data['money'] ?? 0,
  409. 'reward_point_money_rate' => $data['reward_point_money_rate'] ?? '100/1',
  410. ]);
  411. }
  412. /**
  413. * @param array<string, mixed> $data
  414. */
  415. public static function seedOrderItem(string $connection, array $data): void
  416. {
  417. DB::connection($connection)->table('sales_flat_order_item')->insert([
  418. 'item_id' => $data['item_id'],
  419. 'order_id' => $data['order_id'],
  420. 'parent_item_id' => $data['parent_item_id'] ?? null,
  421. 'product_id' => $data['product_id'] ?? null,
  422. 'product_type' => $data['product_type'] ?? 'simple',
  423. 'sku' => $data['sku'] ?? 'SKU-1',
  424. 'name' => $data['name'] ?? 'Test Product',
  425. 'qty_ordered' => $data['qty_ordered'] ?? 1,
  426. 'qty_shipped' => $data['qty_shipped'] ?? 1,
  427. 'qty_invoiced' => $data['qty_invoiced'] ?? 1,
  428. 'qty_canceled' => $data['qty_canceled'] ?? 0,
  429. 'qty_refunded' => $data['qty_refunded'] ?? 0,
  430. 'price' => $data['price'] ?? 100,
  431. 'base_price' => $data['base_price'] ?? 100,
  432. 'price_incl_tax' => $data['price_incl_tax'] ?? 100,
  433. 'base_price_incl_tax' => $data['base_price_incl_tax'] ?? 100,
  434. 'row_total' => $data['row_total'] ?? 100,
  435. 'base_row_total' => $data['base_row_total'] ?? 100,
  436. 'row_total_incl_tax' => $data['row_total_incl_tax'] ?? 100,
  437. 'base_row_total_incl_tax' => $data['base_row_total_incl_tax'] ?? 100,
  438. 'tax_amount' => $data['tax_amount'] ?? 0,
  439. 'base_tax_amount' => $data['base_tax_amount'] ?? 0,
  440. 'tax_percent' => $data['tax_percent'] ?? 0,
  441. 'discount_amount' => $data['discount_amount'] ?? 0,
  442. 'base_discount_amount' => $data['base_discount_amount'] ?? 0,
  443. 'discount_percent' => $data['discount_percent'] ?? 0,
  444. 'weight' => $data['weight'] ?? 1,
  445. 'row_weight' => $data['row_weight'] ?? 1,
  446. 'created_at' => $data['created_at'] ?? '2021-06-01 12:00:00',
  447. ]);
  448. }
  449. /**
  450. * @param array<string, mixed> $data
  451. */
  452. public static function seedOrderAddress(string $connection, array $data): void
  453. {
  454. DB::connection($connection)->table('sales_flat_order_address')->insert([
  455. 'entity_id' => $data['entity_id'],
  456. 'parent_id' => $data['parent_id'],
  457. 'address_type' => $data['address_type'] ?? 'billing',
  458. 'firstname' => $data['firstname'] ?? 'Jane',
  459. 'lastname' => $data['lastname'] ?? 'Doe',
  460. 'company' => $data['company'] ?? null,
  461. 'street' => $data['street'] ?? '123 Main St',
  462. 'city' => $data['city'] ?? 'Austin',
  463. 'region' => $data['region'] ?? 'TX',
  464. 'postcode' => $data['postcode'] ?? '78701',
  465. 'country_id' => $data['country_id'] ?? 'US',
  466. 'telephone' => $data['telephone'] ?? '5551112222',
  467. 'email' => $data['email'] ?? null,
  468. ]);
  469. }
  470. /**
  471. * @param array<string, mixed> $data
  472. */
  473. public static function seedOrderPayment(string $connection, array $data): void
  474. {
  475. DB::connection($connection)->table('sales_flat_order_payment')->insert([
  476. 'entity_id' => $data['entity_id'],
  477. 'parent_id' => $data['parent_id'],
  478. 'method' => $data['method'] ?? 'paypal_express',
  479. 'last_trans_id' => $data['last_trans_id'] ?? null,
  480. 'cc_type' => $data['cc_type'] ?? null,
  481. 'cc_last4' => $data['cc_last4'] ?? null,
  482. 'amount_ordered' => $data['amount_ordered'] ?? 110,
  483. 'base_amount_ordered' => $data['base_amount_ordered'] ?? 110,
  484. ]);
  485. }
  486. public static function createProductCatalog(string $connection): void
  487. {
  488. $schema = Schema::connection($connection);
  489. $db = DB::connection($connection);
  490. $db->table('eav_entity_type')->insert([
  491. ['entity_type_id' => self::CATEGORY_TYPE_ID, 'entity_type_code' => 'catalog_category'],
  492. ['entity_type_id' => self::PRODUCT_TYPE_ID, 'entity_type_code' => 'catalog_product'],
  493. ]);
  494. $schema->create('catalog_eav_attribute', function (Blueprint $table) {
  495. $table->integer('attribute_id');
  496. $table->integer('is_searchable')->default(0);
  497. $table->integer('is_filterable')->default(0);
  498. $table->integer('is_comparable')->default(0);
  499. $table->integer('is_configurable')->default(0);
  500. $table->integer('position')->default(0);
  501. });
  502. $schema->create('eav_attribute_option', function (Blueprint $table) {
  503. $table->integer('option_id');
  504. $table->integer('attribute_id');
  505. $table->integer('sort_order')->default(0);
  506. });
  507. $schema->create('eav_attribute_option_value', function (Blueprint $table) {
  508. $table->increments('value_id');
  509. $table->integer('option_id');
  510. $table->integer('store_id')->default(0);
  511. $table->string('value')->nullable();
  512. });
  513. $schema->create('catalog_product_entity', function (Blueprint $table) {
  514. $table->integer('entity_id');
  515. $table->string('sku')->nullable();
  516. $table->string('type_id')->default('simple');
  517. $table->integer('attribute_set_id')->default(4);
  518. $table->string('created_at')->nullable();
  519. $table->string('updated_at')->nullable();
  520. });
  521. foreach (['varchar', 'int', 'decimal', 'text', 'datetime'] as $type) {
  522. $schema->create('catalog_product_entity_'.$type, function (Blueprint $table) {
  523. $table->increments('value_id');
  524. $table->integer('entity_id');
  525. $table->integer('attribute_id');
  526. $table->integer('store_id')->default(0);
  527. $table->text('value')->nullable();
  528. });
  529. }
  530. $schema->create('catalog_product_entity_media_gallery', function (Blueprint $table) {
  531. $table->increments('value_id');
  532. $table->integer('entity_id');
  533. $table->string('value')->nullable();
  534. });
  535. $schema->create('catalog_product_entity_media_gallery_value', function (Blueprint $table) {
  536. $table->integer('value_id');
  537. $table->integer('store_id')->default(0);
  538. $table->string('label')->nullable();
  539. $table->integer('position')->default(0);
  540. $table->integer('disabled')->default(0);
  541. });
  542. $schema->create('cataloginventory_stock_item', function (Blueprint $table) {
  543. $table->increments('item_id');
  544. $table->integer('product_id');
  545. $table->integer('stock_id')->default(1);
  546. $table->decimal('qty', 12, 4)->default(0);
  547. $table->integer('is_in_stock')->default(0);
  548. });
  549. $schema->create('catalog_category_entity', function (Blueprint $table) {
  550. $table->integer('entity_id');
  551. $table->string('path')->nullable();
  552. });
  553. $schema->create('catalog_category_entity_varchar', function (Blueprint $table) {
  554. $table->increments('value_id');
  555. $table->integer('entity_id');
  556. $table->integer('attribute_id');
  557. $table->integer('store_id')->default(0);
  558. $table->string('value')->nullable();
  559. });
  560. $schema->create('catalog_category_product', function (Blueprint $table) {
  561. $table->integer('category_id');
  562. $table->integer('product_id');
  563. $table->integer('position')->default(0);
  564. });
  565. $schema->create('catalog_product_option', function (Blueprint $table) {
  566. $table->increments('option_id');
  567. $table->integer('product_id');
  568. $table->string('type')->nullable();
  569. $table->integer('is_require')->default(0);
  570. $table->integer('sort_order')->default(0);
  571. });
  572. $schema->create('catalog_product_option_title', function (Blueprint $table) {
  573. $table->increments('option_title_id');
  574. $table->integer('option_id');
  575. $table->integer('store_id')->default(0);
  576. $table->string('title')->nullable();
  577. });
  578. $schema->create('catalog_product_option_type_value', function (Blueprint $table) {
  579. $table->increments('option_type_id');
  580. $table->integer('option_id');
  581. $table->string('sku')->nullable();
  582. $table->integer('sort_order')->default(0);
  583. });
  584. $schema->create('catalog_product_option_type_title', function (Blueprint $table) {
  585. $table->increments('option_type_title_id');
  586. $table->integer('option_type_id');
  587. $table->integer('store_id')->default(0);
  588. $table->string('title')->nullable();
  589. });
  590. $schema->create('catalog_product_option_type_price', function (Blueprint $table) {
  591. $table->increments('option_type_price_id');
  592. $table->integer('option_type_id');
  593. $table->integer('store_id')->default(0);
  594. $table->decimal('price', 12, 4)->default(0);
  595. $table->string('price_type')->default('fixed');
  596. });
  597. $db->table('eav_attribute')->insert([
  598. ['attribute_id' => self::ATTR_PRODUCT_NAME, 'entity_type_id' => self::PRODUCT_TYPE_ID, 'attribute_code' => 'name', 'backend_type' => 'varchar', 'frontend_label' => 'Name', 'frontend_input' => 'text', 'is_required' => 1, 'is_user_defined' => 0],
  599. ['attribute_id' => self::ATTR_PRODUCT_DESCRIPTION, 'entity_type_id' => self::PRODUCT_TYPE_ID, 'attribute_code' => 'description', 'backend_type' => 'text', 'frontend_label' => 'Description', 'frontend_input' => 'textarea', 'is_required' => 0, 'is_user_defined' => 0],
  600. ['attribute_id' => self::ATTR_PRODUCT_SHORT_DESCRIPTION, 'entity_type_id' => self::PRODUCT_TYPE_ID, 'attribute_code' => 'short_description', 'backend_type' => 'text', 'frontend_label' => 'Short Description', 'frontend_input' => 'textarea', 'is_required' => 0, 'is_user_defined' => 0],
  601. ['attribute_id' => self::ATTR_PRODUCT_PRICE, 'entity_type_id' => self::PRODUCT_TYPE_ID, 'attribute_code' => 'price', 'backend_type' => 'decimal', 'frontend_label' => 'Price', 'frontend_input' => 'price', 'is_required' => 1, 'is_user_defined' => 0],
  602. ['attribute_id' => self::ATTR_PRODUCT_SPECIAL_PRICE, 'entity_type_id' => self::PRODUCT_TYPE_ID, 'attribute_code' => 'special_price', 'backend_type' => 'decimal', 'frontend_label' => 'Special Price', 'frontend_input' => 'price', 'is_required' => 0, 'is_user_defined' => 0],
  603. ['attribute_id' => self::ATTR_PRODUCT_COST, 'entity_type_id' => self::PRODUCT_TYPE_ID, 'attribute_code' => 'cost', 'backend_type' => 'decimal', 'frontend_label' => 'Cost', 'frontend_input' => 'price', 'is_required' => 0, 'is_user_defined' => 0],
  604. ['attribute_id' => self::ATTR_PRODUCT_WEIGHT, 'entity_type_id' => self::PRODUCT_TYPE_ID, 'attribute_code' => 'weight', 'backend_type' => 'decimal', 'frontend_label' => 'Weight', 'frontend_input' => 'weight', 'is_required' => 0, 'is_user_defined' => 0],
  605. ['attribute_id' => self::ATTR_PRODUCT_STATUS, 'entity_type_id' => self::PRODUCT_TYPE_ID, 'attribute_code' => 'status', 'backend_type' => 'int', 'frontend_label' => 'Status', 'frontend_input' => 'select', 'is_required' => 1, 'is_user_defined' => 0],
  606. ['attribute_id' => self::ATTR_PRODUCT_META_TITLE, 'entity_type_id' => self::PRODUCT_TYPE_ID, 'attribute_code' => 'meta_title', 'backend_type' => 'varchar', 'frontend_label' => 'Meta Title', 'frontend_input' => 'text', 'is_required' => 0, 'is_user_defined' => 0],
  607. ['attribute_id' => self::ATTR_PRODUCT_META_KEYWORD, 'entity_type_id' => self::PRODUCT_TYPE_ID, 'attribute_code' => 'meta_keyword', 'backend_type' => 'text', 'frontend_label' => 'Meta Keywords', 'frontend_input' => 'textarea', 'is_required' => 0, 'is_user_defined' => 0],
  608. ['attribute_id' => self::ATTR_PRODUCT_META_DESCRIPTION, 'entity_type_id' => self::PRODUCT_TYPE_ID, 'attribute_code' => 'meta_description', 'backend_type' => 'varchar', 'frontend_label' => 'Meta Description', 'frontend_input' => 'textarea', 'is_required' => 0, 'is_user_defined' => 0],
  609. ['attribute_id' => self::ATTR_PRODUCT_URL_KEY, 'entity_type_id' => self::PRODUCT_TYPE_ID, 'attribute_code' => 'url_key', 'backend_type' => 'varchar', 'frontend_label' => 'URL Key', 'frontend_input' => 'text', 'is_required' => 0, 'is_user_defined' => 0],
  610. ['attribute_id' => self::ATTR_PRODUCT_HAIR_COLORR, 'entity_type_id' => self::PRODUCT_TYPE_ID, 'attribute_code' => 'hair_colorr', 'backend_type' => 'int', 'frontend_label' => 'Hair Color', 'frontend_input' => 'select', 'is_required' => 0, 'is_user_defined' => 1],
  611. ['attribute_id' => self::ATTR_CATEGORY_NAME, 'entity_type_id' => self::CATEGORY_TYPE_ID, 'attribute_code' => 'name', 'backend_type' => 'varchar', 'frontend_label' => 'Name', 'frontend_input' => 'text', 'is_required' => 1, 'is_user_defined' => 0],
  612. ]);
  613. $db->table('catalog_eav_attribute')->insert([
  614. ['attribute_id' => self::ATTR_PRODUCT_HAIR_COLORR, 'is_searchable' => 1, 'is_filterable' => 1, 'is_comparable' => 0, 'is_configurable' => 0, 'position' => 10],
  615. ]);
  616. $db->table('eav_attribute_option')->insert([
  617. ['option_id' => self::HAIR_COLORR_OPTION_ID, 'attribute_id' => self::ATTR_PRODUCT_HAIR_COLORR, 'sort_order' => 1],
  618. ]);
  619. $db->table('eav_attribute_option_value')->insert([
  620. ['option_id' => self::HAIR_COLORR_OPTION_ID, 'store_id' => 0, 'value' => 'Natural Black'],
  621. ]);
  622. }
  623. /**
  624. * @param array<string, mixed> $data
  625. */
  626. public static function seedProduct(string $connection, array $data): void
  627. {
  628. $entityId = (int) $data['entity_id'];
  629. $db = DB::connection($connection);
  630. $db->table('catalog_product_entity')->insert([
  631. 'entity_id' => $entityId,
  632. 'sku' => $data['sku'] ?? 'SKU-'.$entityId,
  633. 'type_id' => $data['type_id'] ?? 'simple',
  634. 'attribute_set_id' => $data['attribute_set_id'] ?? 4,
  635. 'created_at' => $data['created_at'] ?? '2020-01-01 00:00:00',
  636. 'updated_at' => $data['updated_at'] ?? '2020-01-01 00:00:00',
  637. ]);
  638. $map = [
  639. 'name' => [self::ATTR_PRODUCT_NAME, 'varchar'],
  640. 'description' => [self::ATTR_PRODUCT_DESCRIPTION, 'text'],
  641. 'short_description' => [self::ATTR_PRODUCT_SHORT_DESCRIPTION, 'text'],
  642. 'price' => [self::ATTR_PRODUCT_PRICE, 'decimal'],
  643. 'special_price' => [self::ATTR_PRODUCT_SPECIAL_PRICE, 'decimal'],
  644. 'cost' => [self::ATTR_PRODUCT_COST, 'decimal'],
  645. 'weight' => [self::ATTR_PRODUCT_WEIGHT, 'decimal'],
  646. 'status' => [self::ATTR_PRODUCT_STATUS, 'int'],
  647. 'meta_title' => [self::ATTR_PRODUCT_META_TITLE, 'varchar'],
  648. 'meta_keyword' => [self::ATTR_PRODUCT_META_KEYWORD, 'text'],
  649. 'meta_description' => [self::ATTR_PRODUCT_META_DESCRIPTION, 'varchar'],
  650. 'url_key' => [self::ATTR_PRODUCT_URL_KEY, 'varchar'],
  651. 'hair_colorr' => [self::ATTR_PRODUCT_HAIR_COLORR, 'int'],
  652. ];
  653. $values = $data['eav'] ?? $data;
  654. foreach ($map as $code => [$attributeId, $type]) {
  655. if (! array_key_exists($code, $values) || $values[$code] === null || $values[$code] === '') {
  656. continue;
  657. }
  658. $db->table('catalog_product_entity_'.$type)->insert([
  659. 'entity_id' => $entityId,
  660. 'attribute_id' => $attributeId,
  661. 'store_id' => 0,
  662. 'value' => $values[$code],
  663. ]);
  664. }
  665. if (! array_key_exists('status', $values)) {
  666. $db->table('catalog_product_entity_int')->insert([
  667. 'entity_id' => $entityId,
  668. 'attribute_id' => self::ATTR_PRODUCT_STATUS,
  669. 'store_id' => 0,
  670. 'value' => 1,
  671. ]);
  672. }
  673. foreach ($data['images'] ?? [] as $position => $path) {
  674. $valueId = $db->table('catalog_product_entity_media_gallery')->insertGetId([
  675. 'entity_id' => $entityId,
  676. 'value' => $path,
  677. ]);
  678. $db->table('catalog_product_entity_media_gallery_value')->insert([
  679. 'value_id' => $valueId,
  680. 'store_id' => 0,
  681. 'label' => '',
  682. 'position' => $position,
  683. 'disabled' => 0,
  684. ]);
  685. }
  686. if (isset($data['qty']) || isset($data['is_in_stock'])) {
  687. $db->table('cataloginventory_stock_item')->insert([
  688. 'product_id' => $entityId,
  689. 'stock_id' => 1,
  690. 'qty' => $data['qty'] ?? 0,
  691. 'is_in_stock' => $data['is_in_stock'] ?? 0,
  692. ]);
  693. }
  694. foreach ($data['categories'] ?? [] as $category) {
  695. $categoryId = (int) $category['entity_id'];
  696. if (! $db->table('catalog_category_entity')->where('entity_id', $categoryId)->exists()) {
  697. $db->table('catalog_category_entity')->insert([
  698. 'entity_id' => $categoryId,
  699. 'path' => $category['path'] ?? (string) $categoryId,
  700. ]);
  701. $db->table('catalog_category_entity_varchar')->insert([
  702. 'entity_id' => $categoryId,
  703. 'attribute_id' => self::ATTR_CATEGORY_NAME,
  704. 'store_id' => 0,
  705. 'value' => $category['name'] ?? 'Category',
  706. ]);
  707. }
  708. $db->table('catalog_category_product')->insert([
  709. 'category_id' => $categoryId,
  710. 'product_id' => $entityId,
  711. 'position' => $category['position'] ?? 0,
  712. ]);
  713. }
  714. foreach ($data['options'] ?? [] as $option) {
  715. self::seedProductOption($connection, array_merge($option, ['product_id' => $entityId]));
  716. }
  717. }
  718. /**
  719. * @param array<string, mixed> $data
  720. */
  721. public static function seedProductOption(string $connection, array $data): void
  722. {
  723. $db = DB::connection($connection);
  724. $optionId = $db->table('catalog_product_option')->insertGetId([
  725. 'product_id' => $data['product_id'],
  726. 'type' => $data['type'] ?? 'drop_down',
  727. 'is_require' => $data['is_require'] ?? 1,
  728. 'sort_order' => $data['sort_order'] ?? 0,
  729. ]);
  730. $db->table('catalog_product_option_title')->insert([
  731. 'option_id' => $optionId,
  732. 'store_id' => 0,
  733. 'title' => $data['title'] ?? 'Option',
  734. ]);
  735. foreach ($data['values'] ?? [] as $index => $value) {
  736. $typeId = $db->table('catalog_product_option_type_value')->insertGetId([
  737. 'option_id' => $optionId,
  738. 'sku' => $value['sku'] ?? '',
  739. 'sort_order' => $value['sort_order'] ?? $index,
  740. ]);
  741. $db->table('catalog_product_option_type_title')->insert([
  742. 'option_type_id' => $typeId,
  743. 'store_id' => 0,
  744. 'title' => $value['title'] ?? '',
  745. ]);
  746. $db->table('catalog_product_option_type_price')->insert([
  747. 'option_type_id' => $typeId,
  748. 'store_id' => 0,
  749. 'price' => $value['price'] ?? 0,
  750. 'price_type' => $value['price_type'] ?? 'fixed',
  751. ]);
  752. }
  753. }
  754. }