1010use Exception ;
1111use Magento \Framework \App \ResourceConnection ;
1212use Magento \Framework \Stdlib \ArrayManager ;
13+ use Magmodules \Channable \Api \Returns \Data \DataInterface as ReturnData ;
1314use Magmodules \Channable \Api \Returns \RepositoryInterface as ReturnsRepository ;
15+ use Magmodules \Channable \Api \Log \RepositoryInterface as LogRepository ;
16+ use Magmodules \Channable \Api \Config \RepositoryInterface as ConfigProvider ;
1417
1518class ImportReturn
1619{
1720
18- /**
19- * @var ReturnsRepository
20- */
21- private $ returnsRepository ;
22- /**
23- * @var ResourceConnection
24- */
25- private $ resource ;
26- /**
27- * @var ArrayManager
28- */
29- private $ arrayManager ;
21+ public const AUTO_PROCESS = ['complete ' , 'accepted ' ];
22+
23+ private CreateCreditmemo $ createCreditmemo ;
24+ private ReturnsRepository $ returnsRepository ;
25+ private ResourceConnection $ resource ;
26+ private ArrayManager $ arrayManager ;
27+ private ConfigProvider $ configProvider ;
28+ private LogRepository $ logRepository ;
3029
31- /**
32- * @param ResourceConnection $resource
33- * @param ReturnsRepository $returnsRepository
34- * @param ArrayManager $arrayManager
35- */
3630 public function __construct (
31+ CreateCreditmemo $ createCreditmemo ,
3732 ResourceConnection $ resource ,
3833 ReturnsRepository $ returnsRepository ,
39- ArrayManager $ arrayManager
34+ ArrayManager $ arrayManager ,
35+ ConfigProvider $ configProvider ,
36+ LogRepository $ logRepository
4037 ) {
38+ $ this ->createCreditmemo = $ createCreditmemo ;
4139 $ this ->returnsRepository = $ returnsRepository ;
4240 $ this ->resource = $ resource ;
4341 $ this ->arrayManager = $ arrayManager ;
42+ $ this ->configProvider = $ configProvider ;
43+ $ this ->logRepository = $ logRepository ;
4444 }
4545
4646 /**
@@ -59,9 +59,9 @@ public function execute(array $returnData, int $storeId): array
5959 $ address = $ returnData ['address ' ] ?? [];
6060 $ orderIncrementId = $ item ['order_id ' ] ?? null ;
6161
62- $ returns = $ this ->returnsRepository ->create ();
63- $ returns ->setStoreId ($ storeId )
64- ->setOrderId ((int )$ item [ ' order_id ' ] )
62+ $ return = $ this ->returnsRepository ->create ();
63+ $ return ->setStoreId ($ storeId )
64+ ->setOrderId ((int )$ orderIncrementId )
6565 ->setChannableId ((int )$ returnData ['channable_id ' ])
6666 ->setChannelName ($ returnData ['channel_name ' ])
6767 ->setChannelId ($ returnData ['channel_id ' ])
@@ -74,52 +74,61 @@ public function execute(array $returnData, int $storeId): array
7474 ->setComment ($ item ['comment ' ])
7575 ->setMagentoIncrementId ((string )$ orderIncrementId );
7676
77- if ($ orderIncrementId && $ salesOrderGridData = $ this ->getMagentoOrder ((string )$ orderIncrementId )) {
78- $ returns ->setMagentoOrderId (( int ) $ salesOrderGridData [ ' entity_id ' ] );
77+ if ($ orderIncrementId && $ entityId = $ this ->getOrderEntityIdByIncrementId ((string )$ orderIncrementId )) {
78+ $ return ->setMagentoOrderId ($ entityId );
7979 }
8080
8181 if ($ channelReturnId = $ this ->arrayManager ->get ('meta/channel_return_id ' , $ returnData )) {
82- $ returns ->setChannelReturnId ($ channelReturnId );
82+ $ return ->setChannelReturnId ($ channelReturnId );
8383 }
8484
8585 if ($ channelOrderId = $ this ->arrayManager ->get ('meta/channel_order_id ' , $ returnData )) {
86- $ returns ->setChannelOrderId ($ channelOrderId );
86+ $ return ->setChannelOrderId ($ channelOrderId );
8787 }
8888
8989 if ($ channelOrderIdInternal = $ this ->arrayManager ->get ('meta/channel_order_id_internal ' , $ returnData )) {
90- $ returns ->setChannelOrderIdInternal ($ channelOrderIdInternal );
90+ $ return ->setChannelOrderIdInternal ($ channelOrderIdInternal );
9191 }
9292
9393 if ($ platformOrderId = $ this ->arrayManager ->get ('meta/platform_order_id ' , $ returnData )) {
94- $ returns ->setPlatformOrderId ($ platformOrderId );
94+ $ return ->setPlatformOrderId ($ platformOrderId );
9595 }
9696
9797 try {
98- $ returns = $ this ->returnsRepository ->save ($ returns );
98+ $ return = $ this ->returnsRepository ->save ($ return );
9999 $ response ['validated ' ] = 'true ' ;
100- $ response ['return_id ' ] = $ returns ->getEntityId ();
100+ $ response ['return_id ' ] = $ return ->getEntityId ();
101101 } catch (Exception $ e ) {
102+ $ this ->logRepository ->addErrorLog ('ImportReturn ' , $ e ->getMessage ());
102103 $ response ['validated ' ] = 'false ' ;
103104 $ response ['errors ' ] = $ e ->getMessage ();
104105 }
105106
107+ if ($ this ->autoProcessReturn ($ return , $ storeId )) {
108+ try {
109+ $ this ->createCreditmemo ->execute ($ return , $ return ->getStatus ());
110+ } catch (\Exception $ e ) {
111+ $ this ->logRepository ->addErrorLog ('Creditmemo on ImportReturn ' , $ e ->getMessage ());
112+ }
113+ }
114+
106115 return $ response ;
107116 }
108117
109- /**
110- * Get Magento order by increment id
111- *
112- * @param string $incrementId
113- *
114- * @return mixed
115- */
116- public function getMagentoOrder (string $ incrementId )
118+ private function autoProcessReturn (ReturnData $ return , int $ storeId ): bool
119+ {
120+ return in_array ($ return ->getStatus (), self ::AUTO_PROCESS )
121+ && $ this ->configProvider ->autoProcessCompeteReturns ($ storeId );
122+ }
123+
124+ private function getOrderEntityIdByIncrementId (string $ incrementId ): ?int
117125 {
118126 $ connection = $ this ->resource ->getConnection ();
119127 $ select = $ connection ->select ()
120- ->from ($ this ->resource ->getTableName ('sales_order_grid ' ) )
128+ ->from ($ this ->resource ->getTableName ('sales_order ' ), [ ' entity_id ' ] )
121129 ->where ('increment_id = :increment_id ' );
122130 $ bind = [':increment_id ' => $ incrementId ];
123- return $ connection ->fetchRow ($ select , $ bind );
131+ $ result = $ connection ->fetchOne ($ select , $ bind );
132+ return $ result !== false ? (int )$ result : null ;
124133 }
125134}
0 commit comments