Magento1OrderReader.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. 'total_item_count',
  41. 'total_qty_ordered',
  42. 'total_invoiced',
  43. 'base_total_invoiced',
  44. 'subtotal_invoiced',
  45. 'base_subtotal_invoiced',
  46. 'tax_invoiced',
  47. 'base_tax_invoiced',
  48. 'shipping_invoiced',
  49. 'base_shipping_invoiced',
  50. 'discount_invoiced',
  51. 'base_discount_invoiced',
  52. 'total_refunded',
  53. 'base_total_refunded',
  54. 'subtotal_refunded',
  55. 'base_subtotal_refunded',
  56. 'tax_refunded',
  57. 'base_tax_refunded',
  58. 'shipping_refunded',
  59. 'base_shipping_refunded',
  60. 'discount_refunded',
  61. 'base_discount_refunded',
  62. 'created_at',
  63. 'updated_at',
  64. ];
  65. private const ITEM_COLUMNS = [
  66. 'item_id',
  67. 'order_id',
  68. 'parent_item_id',
  69. 'product_id',
  70. 'product_type',
  71. 'sku',
  72. 'name',
  73. 'qty_ordered',
  74. 'qty_shipped',
  75. 'qty_invoiced',
  76. 'qty_canceled',
  77. 'qty_refunded',
  78. 'price',
  79. 'base_price',
  80. 'price_incl_tax',
  81. 'base_price_incl_tax',
  82. 'row_total',
  83. 'base_row_total',
  84. 'row_total_incl_tax',
  85. 'base_row_total_incl_tax',
  86. 'tax_amount',
  87. 'base_tax_amount',
  88. 'tax_percent',
  89. 'discount_amount',
  90. 'base_discount_amount',
  91. 'discount_percent',
  92. 'weight',
  93. 'row_weight',
  94. 'created_at',
  95. ];
  96. private const ADDRESS_COLUMNS = [
  97. 'entity_id',
  98. 'parent_id',
  99. 'address_type',
  100. 'firstname',
  101. 'lastname',
  102. 'company',
  103. 'street',
  104. 'city',
  105. 'region',
  106. 'postcode',
  107. 'country_id',
  108. 'telephone',
  109. 'email',
  110. ];
  111. private const PAYMENT_COLUMNS = [
  112. 'entity_id',
  113. 'parent_id',
  114. 'method',
  115. 'last_trans_id',
  116. 'cc_type',
  117. 'cc_last4',
  118. 'amount_ordered',
  119. 'base_amount_ordered',
  120. ];
  121. private Magento1Schema $schema;
  122. public function __construct(private string $connection = 'asteria')
  123. {
  124. $this->schema = new Magento1Schema($connection);
  125. }
  126. /**
  127. * @return Collection<int, array<string, mixed>>
  128. */
  129. public function fetchOrders(int $afterEntityId, int $limit): Collection
  130. {
  131. $columns = $this->schema->existingColumns('sales_flat_order', self::ORDER_COLUMNS);
  132. if ($columns === [] || ! in_array('entity_id', $columns, true)) {
  133. return collect();
  134. }
  135. $rows = DB::connection($this->connection)
  136. ->table('sales_flat_order')
  137. ->where('entity_id', '>', $afterEntityId)
  138. ->orderBy('entity_id')
  139. ->limit($limit)
  140. ->get($columns);
  141. return $rows->map(fn ($row) => $this->toArray($row, 'entity_id'))->values();
  142. }
  143. /**
  144. * @param array<int, int> $orderEntityIds
  145. * @return Collection<int, array<string, mixed>>
  146. */
  147. public function fetchItems(array $orderEntityIds): Collection
  148. {
  149. if ($orderEntityIds === []) {
  150. return collect();
  151. }
  152. $columns = $this->schema->existingColumns('sales_flat_order_item', self::ITEM_COLUMNS);
  153. if ($columns === [] || ! in_array('order_id', $columns, true)) {
  154. return collect();
  155. }
  156. $rows = DB::connection($this->connection)
  157. ->table('sales_flat_order_item')
  158. ->whereIn('order_id', $orderEntityIds)
  159. ->orderBy('item_id')
  160. ->get($columns);
  161. return $rows->map(function ($row) {
  162. $item = $this->toArray($row, 'item_id');
  163. $item['order_id'] = (int) ($row->order_id ?? 0);
  164. $item['parent_item_id'] = isset($row->parent_item_id) && $row->parent_item_id !== null && $row->parent_item_id !== ''
  165. ? (int) $row->parent_item_id
  166. : null;
  167. return $item;
  168. })->values();
  169. }
  170. /**
  171. * @param array<int, int> $orderEntityIds
  172. * @return Collection<int, array<string, mixed>>
  173. */
  174. public function fetchAddresses(array $orderEntityIds): Collection
  175. {
  176. if ($orderEntityIds === []) {
  177. return collect();
  178. }
  179. $columns = $this->schema->existingColumns('sales_flat_order_address', self::ADDRESS_COLUMNS);
  180. if ($columns === [] || ! in_array('parent_id', $columns, true)) {
  181. return collect();
  182. }
  183. $rows = DB::connection($this->connection)
  184. ->table('sales_flat_order_address')
  185. ->whereIn('parent_id', $orderEntityIds)
  186. ->orderBy('entity_id')
  187. ->get($columns);
  188. return $rows->map(function ($row) {
  189. $address = $this->toArray($row, 'entity_id');
  190. $address['parent_id'] = (int) ($row->parent_id ?? 0);
  191. return $address;
  192. })->values();
  193. }
  194. /**
  195. * @param array<int, int> $orderEntityIds
  196. * @return Collection<int, array<string, mixed>>
  197. */
  198. public function fetchPayments(array $orderEntityIds): Collection
  199. {
  200. if ($orderEntityIds === []) {
  201. return collect();
  202. }
  203. $columns = $this->schema->existingColumns('sales_flat_order_payment', self::PAYMENT_COLUMNS);
  204. if ($columns === [] || ! in_array('parent_id', $columns, true)) {
  205. return collect();
  206. }
  207. $rows = DB::connection($this->connection)
  208. ->table('sales_flat_order_payment')
  209. ->whereIn('parent_id', $orderEntityIds)
  210. ->orderBy('entity_id')
  211. ->get($columns);
  212. return $rows->map(function ($row) {
  213. $payment = $this->toArray($row, 'entity_id');
  214. $payment['parent_id'] = (int) ($row->parent_id ?? 0);
  215. return $payment;
  216. })->values();
  217. }
  218. /**
  219. * @return array<string, mixed>
  220. */
  221. private function toArray(object $row, string $idColumn): array
  222. {
  223. $data = (array) $row;
  224. $data[$idColumn] = (int) ($row->{$idColumn} ?? 0);
  225. return $data;
  226. }
  227. }