Magento1OrderReader.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <?php
  2. namespace App\Services\Asteria;
  3. use Illuminate\Support\Collection;
  4. use Illuminate\Support\Facades\DB;
  5. class Magento1OrderReader
  6. {
  7. private const ORDER_COLUMNS = [
  8. 'entity_id',
  9. 'increment_id',
  10. 'customer_id',
  11. 'customer_email',
  12. 'customer_firstname',
  13. 'customer_lastname',
  14. 'customer_is_guest',
  15. 'status',
  16. 'state',
  17. 'store_id',
  18. 'base_currency_code',
  19. 'order_currency_code',
  20. 'store_currency_code',
  21. 'grand_total',
  22. 'base_grand_total',
  23. 'subtotal',
  24. 'base_subtotal',
  25. 'subtotal_incl_tax',
  26. 'base_subtotal_incl_tax',
  27. 'tax_amount',
  28. 'base_tax_amount',
  29. 'discount_amount',
  30. 'base_discount_amount',
  31. 'shipping_amount',
  32. 'base_shipping_amount',
  33. 'shipping_incl_tax',
  34. 'base_shipping_incl_tax',
  35. 'shipping_tax_amount',
  36. 'base_shipping_tax_amount',
  37. 'shipping_method',
  38. 'shipping_description',
  39. 'coupon_code',
  40. 'mw_rewardpoint',
  41. 'mw_rewardpoint_discount',
  42. 'mw_rewardpoint_discount_show',
  43. 'amcheckoutfees_amount',
  44. 'base_amcheckoutfees_amount',
  45. 'total_item_count',
  46. 'total_qty_ordered',
  47. 'total_invoiced',
  48. 'base_total_invoiced',
  49. 'subtotal_invoiced',
  50. 'base_subtotal_invoiced',
  51. 'tax_invoiced',
  52. 'base_tax_invoiced',
  53. 'shipping_invoiced',
  54. 'base_shipping_invoiced',
  55. 'discount_invoiced',
  56. 'base_discount_invoiced',
  57. 'total_refunded',
  58. 'base_total_refunded',
  59. 'subtotal_refunded',
  60. 'base_subtotal_refunded',
  61. 'tax_refunded',
  62. 'base_tax_refunded',
  63. 'shipping_refunded',
  64. 'base_shipping_refunded',
  65. 'discount_refunded',
  66. 'base_discount_refunded',
  67. 'created_at',
  68. 'updated_at',
  69. ];
  70. private const ITEM_COLUMNS = [
  71. 'item_id',
  72. 'order_id',
  73. 'parent_item_id',
  74. 'product_id',
  75. 'product_type',
  76. 'sku',
  77. 'name',
  78. 'qty_ordered',
  79. 'qty_shipped',
  80. 'qty_invoiced',
  81. 'qty_canceled',
  82. 'qty_refunded',
  83. 'price',
  84. 'base_price',
  85. 'price_incl_tax',
  86. 'base_price_incl_tax',
  87. 'row_total',
  88. 'base_row_total',
  89. 'row_total_incl_tax',
  90. 'base_row_total_incl_tax',
  91. 'tax_amount',
  92. 'base_tax_amount',
  93. 'tax_percent',
  94. 'discount_amount',
  95. 'base_discount_amount',
  96. 'discount_percent',
  97. 'weight',
  98. 'row_weight',
  99. 'created_at',
  100. ];
  101. private const ADDRESS_COLUMNS = [
  102. 'entity_id',
  103. 'parent_id',
  104. 'address_type',
  105. 'firstname',
  106. 'lastname',
  107. 'company',
  108. 'street',
  109. 'city',
  110. 'region',
  111. 'postcode',
  112. 'country_id',
  113. 'telephone',
  114. 'email',
  115. ];
  116. private const PAYMENT_COLUMNS = [
  117. 'entity_id',
  118. 'parent_id',
  119. 'method',
  120. 'last_trans_id',
  121. 'cc_type',
  122. 'cc_last4',
  123. 'amount_ordered',
  124. 'base_amount_ordered',
  125. ];
  126. private Magento1Schema $schema;
  127. public function __construct(private string $connection = 'asteria')
  128. {
  129. $this->schema = new Magento1Schema($connection);
  130. }
  131. /**
  132. * @return Collection<int, array<string, mixed>>
  133. */
  134. public function fetchOrders(int $afterEntityId, int $limit): Collection
  135. {
  136. $columns = $this->schema->existingColumns('sales_flat_order', self::ORDER_COLUMNS);
  137. if ($columns === [] || ! in_array('entity_id', $columns, true)) {
  138. return collect();
  139. }
  140. $rows = DB::connection($this->connection)
  141. ->table('sales_flat_order')
  142. ->where('entity_id', '>', $afterEntityId)
  143. ->orderBy('entity_id')
  144. ->limit($limit)
  145. ->get($columns);
  146. return $rows->map(fn ($row) => $this->toArray($row, 'entity_id'))->values();
  147. }
  148. /**
  149. * @param array<int, int> $orderEntityIds
  150. * @return Collection<int, array<string, mixed>>
  151. */
  152. public function fetchItems(array $orderEntityIds): Collection
  153. {
  154. if ($orderEntityIds === []) {
  155. return collect();
  156. }
  157. $columns = $this->schema->existingColumns('sales_flat_order_item', self::ITEM_COLUMNS);
  158. if ($columns === [] || ! in_array('order_id', $columns, true)) {
  159. return collect();
  160. }
  161. $rows = DB::connection($this->connection)
  162. ->table('sales_flat_order_item')
  163. ->whereIn('order_id', $orderEntityIds)
  164. ->orderBy('item_id')
  165. ->get($columns);
  166. return $rows->map(function ($row) {
  167. $item = $this->toArray($row, 'item_id');
  168. $item['order_id'] = (int) ($row->order_id ?? 0);
  169. $item['parent_item_id'] = isset($row->parent_item_id) && $row->parent_item_id !== null && $row->parent_item_id !== ''
  170. ? (int) $row->parent_item_id
  171. : null;
  172. return $item;
  173. })->values();
  174. }
  175. /**
  176. * @param array<int, int> $orderEntityIds
  177. * @return Collection<int, array<string, mixed>>
  178. */
  179. public function fetchAddresses(array $orderEntityIds): Collection
  180. {
  181. if ($orderEntityIds === []) {
  182. return collect();
  183. }
  184. $columns = $this->schema->existingColumns('sales_flat_order_address', self::ADDRESS_COLUMNS);
  185. if ($columns === [] || ! in_array('parent_id', $columns, true)) {
  186. return collect();
  187. }
  188. $rows = DB::connection($this->connection)
  189. ->table('sales_flat_order_address')
  190. ->whereIn('parent_id', $orderEntityIds)
  191. ->orderBy('entity_id')
  192. ->get($columns);
  193. return $rows->map(function ($row) {
  194. $address = $this->toArray($row, 'entity_id');
  195. $address['parent_id'] = (int) ($row->parent_id ?? 0);
  196. return $address;
  197. })->values();
  198. }
  199. /**
  200. * @param array<int, int> $orderEntityIds
  201. * @return Collection<int, array<string, mixed>>
  202. */
  203. public function fetchPayments(array $orderEntityIds): Collection
  204. {
  205. if ($orderEntityIds === []) {
  206. return collect();
  207. }
  208. $columns = $this->schema->existingColumns('sales_flat_order_payment', self::PAYMENT_COLUMNS);
  209. if ($columns === [] || ! in_array('parent_id', $columns, true)) {
  210. return collect();
  211. }
  212. $rows = DB::connection($this->connection)
  213. ->table('sales_flat_order_payment')
  214. ->whereIn('parent_id', $orderEntityIds)
  215. ->orderBy('entity_id')
  216. ->get($columns);
  217. return $rows->map(function ($row) {
  218. $payment = $this->toArray($row, 'entity_id');
  219. $payment['parent_id'] = (int) ($row->parent_id ?? 0);
  220. return $payment;
  221. })->values();
  222. }
  223. /**
  224. * MW Reward Points rows keyed later by Magento order entity_id.
  225. *
  226. * @param array<int, int> $orderEntityIds
  227. * @return Collection<int, array<string, mixed>>
  228. */
  229. public function fetchRewardPoints(array $orderEntityIds): Collection
  230. {
  231. if ($orderEntityIds === [] || ! $this->schema->hasTable('mw_reward_point_order')) {
  232. return collect();
  233. }
  234. $columns = $this->schema->existingColumns('mw_reward_point_order', [
  235. 'order_id',
  236. 'reward_point',
  237. 'earn_rewardpoint',
  238. 'money',
  239. 'reward_point_money_rate',
  240. ]);
  241. if ($columns === [] || ! in_array('order_id', $columns, true)) {
  242. return collect();
  243. }
  244. $rows = DB::connection($this->connection)
  245. ->table('mw_reward_point_order')
  246. ->whereIn('order_id', $orderEntityIds)
  247. ->get($columns);
  248. return $rows->map(function ($row) {
  249. $data = $this->toArray($row, 'order_id');
  250. $data['order_id'] = (int) ($row->order_id ?? 0);
  251. return $data;
  252. })->values();
  253. }
  254. /**
  255. * @return array<string, mixed>
  256. */
  257. private function toArray(object $row, string $idColumn): array
  258. {
  259. $data = (array) $row;
  260. $data[$idColumn] = (int) ($row->{$idColumn} ?? 0);
  261. return $data;
  262. }
  263. }