shop.balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

fba.php (5263B)


      1 <?php
      2 class ControllerExtensionOpenbayFba extends Controller {
      3 	public function eventAddOrderHistory($route, $data) {
      4 		$this->openbay->fba->log('eventAddOrderHistory Event fired: ' . $route);
      5 
      6 		if (isset($data[0]) && !empty($data[0])) {
      7 			$this->load->model('checkout/order');
      8 			$this->load->model('account/order');
      9 			$this->load->model('catalog/product');
     10 
     11 			$this->openbay->fba->log('eventAddOrderHistory Event fired for order ID: ' . $data[0]);
     12 
     13 			$order = $this->model_checkout_order->getOrder($data[0]);
     14 
     15 			if ($order['shipping_method']) {
     16 				if ($this->config->get('openbay_fba_order_trigger_status') == $order['order_status_id']) {
     17 					$fba_fulfillment_id = $this->openbay->fba->createFBAFulfillmentID($data[0], 0);
     18 
     19 					$order_products = $this->model_account_order->getOrderProducts($data[0]);
     20 
     21 					$total_order_products = count($order_products);
     22 
     23 					$fulfillment_items = array();
     24 
     25 					foreach ($order_products as $order_product) {
     26 						$product = $this->model_catalog_product->getProduct($order_product['product_id']);
     27 
     28 						if ($product['location'] == 'FBA') {
     29 							$fulfillment_items[] = array(
     30 									'seller_sku' => $product['sku'],
     31 									'quantity' => $order_product['quantity'],
     32 									'seller_fulfillment_order_item_id' => $this->config->get('openbay_fba_order_prefix') . $fba_fulfillment_id . '-' . $order_product['order_product_id'],
     33 									'per_unit_declared_value' => array(
     34 											'currency_code' => $order['currency_code'],
     35 											'value' => number_format($order_product['price'], 2)
     36 									),
     37 							);
     38 						}
     39 					}
     40 
     41 					$total_fulfillment_items = count($fulfillment_items);
     42 
     43 					if (($total_order_products == $total_fulfillment_items) || ($this->config->get('openbay_fba_only_fill_complete') != 1)) {
     44 						if (!empty($fulfillment_items)) {
     45 							$request = array();
     46 
     47 							$datetime = new DateTime($order['date_added']);
     48 							$request['displayable_order_datetime'] = $datetime->format(DateTime::ISO8601);
     49 
     50 							$request['seller_fulfillment_order_id'] = $this->config->get('openbay_fba_order_prefix') . $data[0] . '-' . $fba_fulfillment_id;
     51 							$request['displayable_order_id'] = $data[0];
     52 							$request['displayable_order_comment'] = 'none';
     53 							$request['shipping_speed_category'] = $this->config->get('openbay_fba_shipping_speed');
     54 							$request['fulfillment_action'] = ($this->config->get('openbay_fba_send_orders') == 1 ? 'Ship' : 'Hold');
     55 							$request['fulfillment_policy'] = $this->config->get('openbay_fba_fulfill_policy');
     56 
     57 							$request['destination_address'] = array(
     58 									'name' => $order['shipping_firstname'] . ' ' . $order['shipping_lastname'],
     59 									'line_1' => (!empty($order['shipping_company']) ? $order['shipping_company'] : $order['shipping_address_1']),
     60 									'line_2' => (!empty($order['shipping_company']) ? $order['shipping_address_1'] : $order['shipping_address_2']),
     61 									'line_3' => (!empty($order['shipping_company']) ? $order['shipping_address_2'] : ''),
     62 									'state_or_province_code' => $order['shipping_zone'],
     63 									'city' => $order['shipping_city'],
     64 									'country_code' => $order['shipping_iso_code_2'],
     65 									'postal_code' => $order['shipping_postcode'],
     66 							);
     67 
     68 							$request['items'] = $fulfillment_items;
     69 
     70 							$response = $this->openbay->fba->call("v1/fba/fulfillments/", $request, 'POST');
     71 
     72 							if ($response['response_http'] != 201) {
     73 								/**
     74 								 * @todo notify the admin about any errors
     75 								 */
     76 								$this->openbay->fba->updateFBAOrderStatus($data[0], 1);
     77 							} else {
     78 								if ($this->config->get('openbay_fba_send_orders') == 1) {
     79 									$this->openbay->fba->updateFBAOrderStatus($data[0], 3);
     80 								} else {
     81 									$this->openbay->fba->updateFBAOrderStatus($data[0], 2);
     82 								}
     83 
     84 								$this->openbay->fba->updateFBAOrderRef($data[0], $this->config->get('openbay_fba_order_prefix') . $data[0] . '-' . $fba_fulfillment_id);
     85 							}
     86 
     87 							$this->openbay->fba->populateFBAFulfillment(json_encode($request), json_encode($response), $response['response_http'], $fba_fulfillment_id);
     88 							$this->openbay->fba->updateFBAOrderFulfillmentID($data[0], $fba_fulfillment_id);
     89 						} else {
     90 							$this->openbay->fba->log('No FBA items found for this order');
     91 						}
     92 					} else {
     93 						$this->openbay->fba->log('Products:' . $total_order_products . ', Fulfillment products: ' . $total_fulfillment_items . ' - settings do not allow incomplete order fulfillment.');
     94 					}
     95 				}
     96 			}
     97 
     98 			// how about notifications? does the merchant want a notification that here is a new fulfillment ready to be checked over?
     99 			// alert of any missing products that were not in FBA?
    100 			// any errors returned by FBA?
    101 		}
    102 	}
    103 
    104 	public function eventAddOrder($route, $data) {
    105 		$this->openbay->fba->log('eventAddOrder Event fired: ' . $route);
    106 
    107 		if (isset($data[0]) && !empty($data[0])) {
    108 			$this->load->model('checkout/order');
    109 
    110 			$this->openbay->fba->log('Order ID: ' . (int)$data[0]);
    111 
    112 			$order = $this->model_checkout_order->getOrder((int)$data[0]);
    113 
    114 			if ($order['shipping_method']) {
    115 				$this->openbay->fba->createFBAOrderID((int)$data[0]);
    116 			}
    117 		}
    118 	}
    119 }