| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace alekciy\ofd\providers\yandex; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use alekciy\ofd\interfaces\CashDeskInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use alekciy\ofd\interfaces\OutletInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use alekciy\ofd\interfaces\ProviderInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use alekciy\ofd\interfaces\ShiftInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use alekciy\ofd\providers\yandex\Model\CashDesk; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use alekciy\ofd\providers\yandex\Model\Document; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use alekciy\ofd\providers\yandex\Model\Outlet; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use alekciy\ofd\providers\yandex\Model\Shift; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use alekciy\ofd\providers\yandex\Request\CashDeskList; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use alekciy\ofd\providers\yandex\Request\DocumentList; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use alekciy\ofd\providers\yandex\Request\OutletList; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use alekciy\ofd\providers\yandex\Request\ShiftList; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use DateTime; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Exception; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use GuzzleHttp\Exception\GuzzleException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * @see https://yandex.ru/dev/ofd/doc/dg/concepts/about-docpage/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | class Yandex implements ProviderInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 	/** @var Client */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 	protected $client = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 	 * @param Client $client | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 	public function __construct(Client $client) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 		$this->client = $client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 	 * Для запросов с постраничной навигацией загрузит все возможные элементы. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 	 * @param RequestPage $endpoint | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 	 * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 	 * @throws Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 	 * @throws GuzzleException | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 44 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  | 	private function getAllItemList(RequestPage $endpoint): array | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  | 	{ | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  | 		$result = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  | 		do { | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  | 			$response = $this->client->request($endpoint); | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  | 			$recordList = isset($response['data']) | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  | 				? $response['data'] | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  | 				: $response; | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  | 			foreach ($recordList as $record) { | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  | 				$result[] = $record; | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  | 			} | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  | 			++$endpoint->pageNumber; | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  | 		} while (!empty($response)); | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  | 		return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 	 * @inheritDoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 	 * @return OutletInterface[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 	 * @throws GuzzleException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 	 * @throws Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 	public function getOutletList(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 		$result = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 		$responseOutletList = $this->getAllItemList(new OutletList([])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 		foreach ($responseOutletList as $responseOutlet) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 			$outlet = new Outlet($responseOutlet); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 			$result[$outlet->getId()] = $outlet; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 		return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 	 * @inheritDoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 	 * @throws GuzzleException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 	 * @throws Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 	public function getCashDeskList(OutletInterface $outlet = null): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 		$result = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 		$outletList = $outlet instanceof OutletInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 			? [$outlet] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 			: $this->getOutletList(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 		foreach ($outletList as $outlet) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 			$responseCashDeskList = $this->getAllItemList(new CashDeskList([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 				'outletId' => $outlet->getId(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 			])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 			foreach ($responseCashDeskList as $responseCashDesk) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 				$result[] = new CashDesk($responseCashDesk); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 		return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 	 * Информация по кассе. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 	 * @param int $id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 	 * @return CashDesk | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | 	 * @throws GuzzleException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 	 * @throws Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 	public function getCashDesk(int $id): CashDesk | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 		$responseCashDesk = $this->client->request(new Request\CashDesk(['id' => $id])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 		return new CashDesk($responseCashDesk); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 	 * @inheritDoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | 	 * @throws Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 	 * @throws GuzzleException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 	public function getShiftList(CashDeskInterface $cashDesk = null, DateTime $start = null, DateTime $end = null): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  | 		$result = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  | 		// Задаем время | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  | 		if (!$start instanceof DateTime) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  | 			$start = new DateTime('today'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  | 		if (!$end instanceof DateTime) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  | 			$end = new DateTime('tomorrow'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  | 		$cashDeskList = $cashDesk instanceof CashDeskInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  | 			? [$cashDesk] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  | 			: $this->getCashDeskList(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  | 		$responseShiftList = $this->getAllItemList(new ShiftList([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  | 			'cashBoxIdList' => array_column($cashDeskList, 'id'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  | 			'start'         => $start->format('Y-m-d H:i:s'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  | 			'end'           => $end->format('Y-m-d H:i:s'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  | 		])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  | 		// Получаем результат | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  | 		foreach ($responseShiftList as $responseShift) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  | 			$result[] = new Shift($responseShift); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  | 		return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  | 	 * @inheritDoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  | 	 * @throws Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  | 	 * @throws GuzzleException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  | 	public function getDocumentList(ShiftInterface $shift = null, DateTime $start = null, DateTime $end = null): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  | 		$result = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  | 		// Задаем время | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  | 		if (!$start instanceof DateTime) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  | 			$start = new DateTime('today'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  | 		if (!$end instanceof DateTime) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  | 			$end = new DateTime('tomorrow'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  | 		$shiftList = $shift instanceof ShiftInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  | 			? [$shift] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  | 			: $this->getShiftList(null, $start, $end); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  | 		foreach ($shiftList as $shift) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  | 			$responseDocumentList = $this->getAllItemList(new DocumentList([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  | 				'shiftNumber' => $shift->getShiftNumber(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  | 				'start'       => $start->format('Y-m-d H:i:s'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  | 				'end'         => $end->format('Y-m-d H:i:s'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  | 			])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  | 			foreach ($responseDocumentList as $responseDocument) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  | 				$result[] = new Document($responseDocument); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  | 		return $result; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 182 |  |  | 	} | 
            
                                                        
            
                                    
            
            
                | 183 |  |  | } |