public function getProductInventory($sku = null, $warehouse = null) { $main_inventory_facility = array('YOUNIQUE', '3PL-NL-MLO', '3PL-MX-EFT'); if (empty($sku)) { $sku = $this->request->data['sku']; } if (empty($warehouse)) { $warehouse = $this->request->data['warehouse']; } if (!empty($this->warehouse_map[$warehouse])) { $warehouse_id = $this->warehouse_map[$warehouse]; } else { $warehouse_id = $warehouse; } $endpoint = 'stocktotals'; $sub_url = $endpoint . '/' . $sku; $total_avail = 0; try { $results = SnapFulfilAPI::call($sub_url, null, false, $warehouse_id); foreach ($results as $inventory_section) { $facility = $inventory_section->Facility; if (in_array($facility, $main_inventory_facility)) { $total_avail = $inventory_section->Qty - $inventory_section->QtyDueOut; continue; } } $output = array('sku' => $sku, 'qty' => (string) $total_avail); $this->sendSuccess($output); } catch (Exception $e) { $this->sendSuccess(0); error_log("Snap API failure: " . $e->getMessage()); } }
/** * Get all on hold items and set to zero * * @param $snap_instance * @param $facility * @throws SnapFulfilAPIError */ private function setHoldItems($snap_instance, $facility) { $snapquery_onhold_endpoint = "StockTotals?\$filter=startswith(Status,'01')"; $onhold_results = SnapFulfilAPI::call($snapquery_onhold_endpoint, NULL, FALSE, $snap_instance); foreach ($onhold_results as $result) { $this->NsProductCount->clear(); if ($result->Status == '01' && $result->Facility == $facility) { $sku = $result->SKUId; try { $record = $this->NsProductCount->find('first', ['conditions' => ['sku' => $sku, 'ns_warehouse_id' => $snap_instance]]); if (!empty($record)) { $new_inventory_count = array('id' => $record['NsProductCount']['id'], 'sku' => $record['NsProductCount']['sku'], 'ns_warehouse_id' => $snap_instance, 'inventory_count' => 0); } else { //Add Record $this->NsProductCount->create(); $new_inventory_count = array('sku' => $sku, 'ns_warehouse_id' => $snap_instance, 'inventory_count' => 0); } try { $this->NsProductCount->save($new_inventory_count); } catch (Exception $e) { $this->out("<error>{$e->getMessage()}</error>"); } } catch (Exception $e) { $this->out("<error>Cant retrieve record for product acount sku: {$sku}</error>"); $this->out("<error>{$e->getMessage()}</error>"); } } } }
/** * Check Shipment * * @param Snap $Model * @param $shipment_id * @return array */ public function checkShipment(Snap $Model, $shipment_id) { $results = "{$Model->name}\n"; $this->OrderShipment = ClassRegistry::init('OrderShipment'); $this->OrderShipment->useDbConfig = "replicated"; unset($this->OrderShipment->virtualFields); $shipment = $this->OrderShipment->find('first', ['contain' => ['Order' => ['OrderCustomer' => ['Email', 'Address' => ['State']], 'User']], 'conditions' => ['OrderShipment.id' => $shipment_id]]); $this->OrderShipment->useDbConfig = "default"; $this->OrderShipment->clear(); $this->OrderShipment->id = $shipment_id; $total = count($shipment); if ($total == 0) { return ['success' => FALSE, 'message' => "Shipment not found"]; } $market_id = $shipment['Order']['order_market_id']; try { if (!empty($shipment['Order']['OrderCustomer']['Address']['State']['ns_warehouse_id'])) { $wh_id = $shipment['Order']['OrderCustomer']['Address']['State']['ns_warehouse_id']; } else { $wh_id = (int) WarehouseUtil::idByMarket($market_id); } $snap_shipment = SnapFulfilAPI::call("shipments/{$shipment_id}", NULL, FALSE, $wh_id); } catch (Exception $e) { return ['success' => FALSE, 'message' => "Shipment not in Snap"]; } if (empty($snap_shipment->CarrierTrackingNumber) && $snap_shipment->Stage != '90') { //has no tracking number $this->OrderShipment->saveField('order_shipment_status_id', OrderShipmentStatus::PROCESSING); return ['success' => FALSE, 'message' => "Shipment is still being picked or packed."]; } elseif ($snap_shipment->Stage == '90') { // order is marked as shipped $results .= "Adding tracking and sending email\n"; $results .= "Adding order_shipment_packages record for order_shipment_id: " . $shipment_id . "\n"; $results .= "Tracking number: " . $snap_shipment->CarrierTrackingNumber . "\n"; if ($snap_shipment->CarrierTrackingNumber && $snap_shipment->ShippingMethod) { $tracking_url = $this->OrderShipment->getTrackingLink($snap_shipment->CarrierTrackingNumber, $snap_shipment->ShippingMethod); } else { $tracking_url = ''; } $email_data = ["name" => $snap_shipment->CustomerName, "order_id" => $snap_shipment->CustomerRef, "tracking_number" => $snap_shipment->CarrierTrackingNumber, "tracking_url" => $tracking_url, "country_id" => $snap_shipment->ShipAddress[0]->Country]; $usable_userLocale = empty($shipment['Order']['User']['user_default_locale']) ? 'en_US' : $shipment['Order']['User']['user_default_locale']; YouniqueEmail::queueEmail($email_data, 'en_us_tracking_numbers', $shipment['Order']['OrderCustomer']['Email']['email'], 'email order tracking', 'purchase-page', $usable_userLocale); $results .= "Tracking email sent: " . $snap_shipment->CarrierTrackingNumber . "\n"; $this->OrderShipment->clear(); $this->OrderShipment->id = $shipment_id; $this->OrderShipment->saveField('tracking_number', $snap_shipment->CarrierTrackingNumber); $this->OrderShipment->saveField('carrier', $snap_shipment->ShippingMethod); $this->OrderShipment->saveField('date_shipped', $snap_shipment->DateClosed); $this->OrderShipment->saveField('order_shipment_status_id', OrderShipmentStatus::FULFILLED); $results .= "Order Shipment updated.\n"; } elseif ($snap_shipment->Stage == '00' && strtotime($snap_shipment->DateDueOut) < strtotime('now')) { // shipment is past scheduled due date $results .= "00: " . $shipment_id . "\n"; $this->OrderShipment->saveField('order_shipment_status_id', OrderShipmentStatus::UNSPECIFIED_ERROR); } else { $results .= "Unspecified error: " . $shipment_id . "\n"; } $results .= "Completed check of order shipping record.\n"; return ['success' => TRUE, 'message' => $results]; }
/** * Call Snap * * @param $endpoint * @param $snap_instance * @param bool $put * @return array|bool|stdClass */ private function callSnap($endpoint, $snap_instance, $put = false) { try { $response = SnapFulfilAPI::call($endpoint, NULL, $put, $snap_instance); } catch (Exception $e) { return false; } return $response; }
private function _attachPackage($wh_id) { $shipment_id = $this->shipment['OrderShipment']['id']; if (isset($this->shipment['OrderShipment']['package'])) { $this->snap_box = ['Table' => self::SNAP_ATTACHMENT_TABLE, 'Type' => self::SNAP_ATTACHMENT_BOX_TYPE, 'Key' => $this->shipment['OrderShipment']['id'], 'KeyLine' => '0', 'LineId' => '2', 'AttachmentText' => $this->shipment['OrderShipment']['package']]; $response = SnapFulfilAPI::call("attachments/", $this->snap_box, FALSE, $wh_id); if (empty($response)) { throw new Exception("Unknown error"); } else { $this->logMsg("=> Successfully Posted Package ID for Shipment id " . $shipment_id); } } else { $this->logMsg("=> No Package ID for Shipment id " . $shipment_id); } }
public function snapFix() { $this->Order = ClassRegistry::init("Order"); // if(isset($this->args[0])) // $order_shipment_ids = $this->args[0]; // $shipment_id_string = $this->makesArrayStingForInClaus($order_shipment_ids); // else { $shipment_id_string = "8684006"; // } $sql = "\r\nSELECT\r\n *\r\nFROM\r\n orders o\r\n JOIN\r\n order_shipments os ON os.order_id = o.id\r\nWHERE\r\n os.id IN ({$shipment_id_string});\r\n "; $affected_orders = $this->Order->query($sql); $count_orders = count($affected_orders); $this->out("Total orders: " . $count_orders); $ittr = 0; foreach ($affected_orders as $order) { usleep(500000); //sleep for 1/2 a second to slowdown the API push try { $ittr++; $this->out("Left: " . $count_orders - $ittr); // query snap for status and existence of the kudo item $order_id = $order['o']['id']; $shipment_id = $order['os']['id']; $market_id = $order['o']['order_market_id']; $wh_id = (int) WarehouseUtil::idByMarket($market_id); $date = date('c'); $user_id = $order['o']['user_id']; $update_shipment = ["ShipmentId" => $shipment_id, "OrderClass" => "BO", "CustomerId" => $user_id, "DateDueOut" => $date]; SnapFulfilAPI::call("shipments/{$shipment_id}", $update_shipment, TRUE, $wh_id); $this->out("<success>O: {$order_id} - S: {$shipment_id} - Shipment class updated</success>"); continue; } catch (Exception $f) { $this->out("<error>O: {$order_id} - S: {$shipment_id} - Update failed. " . $f->getMessage() . "</error>"); sleep(5); continue; } // add item to order } }
public function checkCancelledOrders() { $email = "*****@*****.**"; // $email = "*****@*****.**"; $cancel_status = 91; $cancelled_orders = $this->Order->find('all', ['conditions' => ['order_status_id' => Order::STATUS_CANCELLED, 'date_completed >= DATE_SUB( NOW(), INTERVAL 24 HOUR)'], 'contain' => ['OrderShipment' => ['OrderItem']]]); if (empty($cancelled_orders)) { $this->out("No cancelled orders found"); return; } $this->out("Found orders :" . count($cancelled_orders)); foreach ($cancelled_orders as $order) { $this->hr(); $order_id = $order['Order']['id']; $this->out("Checking cancelled order: " . $order_id, 1, Shell::VERBOSE); foreach ($order['OrderShipment'] as $shipment) { $shipment_id = $shipment['id']; $this->out("Shipment id: " . $shipment_id, 1, Shell::VERBOSE); // $continue = $this->in("Continue to check Snap?", ['y', 'n'], 'y'); // if($continue == 'n') // continue; try { if (!empty($shipment['OrderItem'][0]['ns_warehouse_id'])) { $wh_id = $shipment['OrderItem'][0]['ns_warehouse_id']; } else { $wh_id = (int) WarehouseUtil::idByMarket($order['Order']['order_market_id']); } $response = SnapFulfilAPI::call("shipments/{$shipment_id}", NULL, FALSE, $wh_id); // $this->out(var_export($response, true)); if ($response && $response->Stage != 90 && $response->Stage != 91) { $this->out("Stage: " . $response->Stage); // send notifications to people that it needs to be cancelled. $subject = 'Cancelled order in WMS. Shipment id: ' . $shipment_id; $message = "Cancel order: {$order_id}<br />" . "Shipment id: {$shipment_id}<br />" . "Customer: {$response->CustomerName}<br />" . "WMS instance: {$wh_id}"; $update_shipment_status_params = array("ShipmentId" => $shipment_id, "Status" => $cancel_status); // $do_cancel = $this->in("Cancel shipment in Snap?", ['y', 'n'], 'y'); // if($do_cancel == 'n') // continue; try { SnapFulfilAPI::call("ShipmentStatus/{$shipment_id}", $update_shipment_status_params, TRUE, $wh_id); } catch (Exception $e) { $this->_sendEmail($email, $subject, $message); } $this->out("<info>Shipment cancelled Snap</info>", 1, Shell::VERBOSE); $this->out("<info>Email send</info>", 1, Shell::VERBOSE); } } catch (Exception $e) { // Order should not be in Snap $this->out("<warning>Shipment not in Snap</warning>", 1, Shell::VERBOSE); continue; } } } $this->out("All done."); }
public function snap_direct($wh_id, $formatted = 'netsuite', $retries = 1, $data_set) { App::uses('Snap', 'Model'); $this->Snap = ClassRegistry::init('Snap'); $this->OrderShipmentMethod = ClassRegistry::init('OrderShipmentMethod'); //print_r(array('data_set' => $data_set)); $results = array(); if (!$this->OrderShipment) { $this->OrderShipment = ClassRegistry::init('OrderShipment'); $this->OrderShipment->useDbConfig = "default"; } if (!$this->ItemPrice) { $this->ItemPrice = ClassRegistry::init('ItemPrice'); $this->ItemPrice->useDbConfig = "replicated"; } if ($formatted == 'netsuite' && isset($data_set['addendum']) && isset($data_set['shipment'])) { $order_header = $data_set['order_header']; $order_items = $data_set['items']; $order_shipment = $data_set['shipment']; $shipment_date = date('Y-m-d H:i:s'); $snap_addendum = $data_set['addendum']; $date = date('c', strtotime($shipment_date)); $items = array(); foreach ($order_items as $ordered_item) { $price = $ordered_item['price']; if ($price <= 1) { $item_id = $ordered_item['item_id']; $market_id = $snap_addendum['market_id']; $item_price = $this->ItemPrice->getPrice($item_id, $market_id, $shipment_date); $price = $item_price; } $price = $price > 0 ? $price : 1; $items[] = ["ShipmentId" => $order_shipment['id'], "SKUId" => $ordered_item['sku'], "UnitOfMeasure" => "EA", "QtyOrdered" => $ordered_item['quantity'], "Stage" => "00", "Price" => $price]; } $shipmethodnum = $this->OrderShipmentMethod->getShipmentMethodByMarket($snap_addendum['order_market_id']); $shipmethod = OrderShipmentMethod::$shipment_methods[$shipmethodnum]; $snap_payload = ["ShipmentId" => $order_shipment['id'], "ShippingMethod" => $shipmethod, "Region" => $items[0]['SKUId'], "CarrierId" => "RATELINX", "DateDueOut" => $date, "Stage" => "00", "CustomerId" => $snap_addendum['customer_id'], "CustomerName" => $order_shipment['first_name'] . " " . $order_shipment['last_name'], "CustomerRef" => $order_header['order_id'], "OrderClass" => $order_shipment['shipping_class'], "Status" => "00", "PriorityDespatch" => $order_shipment['priority'], "ShipAddress" => [["Table" => "SHH", "ShipmentId" => $order_shipment['id'], "AddressId" => "ADD1", "Name" => $order_shipment['first_name'] . " " . $order_shipment['last_name'], "Line1" => $order_shipment['address1'], "Line2" => $order_shipment['address2'], "Line3" => $snap_addendum['address3'], "City" => $order_shipment['city'], "State" => $snap_addendum['state_two'], "Postcode" => $order_shipment['postal_code'], "Country" => $snap_addendum['country_code']]], "ShipContacts" => [["Phone" => empty($order_shipment['phone']) ? "N/A" : $order_shipment['phone'], "ShipmentId" => $order_shipment['id'], "LineId" => 0]], "ShipmentLines" => $items]; print "Creating Snap shipment to: " . print_r($snap_payload['ShipAddress'][0], true); } else { $snap_payload = $data_set; } $this->checkAddressLength($snap_payload); $this->OrderShipment->clear(); $this->OrderShipment->id = $snap_payload['ShipmentId']; try { $response = SnapFulfilAPI::call("shipments", $snap_payload, FALSE, $wh_id); $this->OrderShipment->saveField("order_shipment_status_id", OrderShipmentStatus::PROCESSING); $results['added_to_snap']++; print "=> Snapfulfill Success, Shipment ID: " . $order_shipment['id'] . "\n"; return true; } catch (Exception $d) { print "<error>Snap fail: {$d->getMessage()}</error>"; $this->OrderShipment->saveField("order_shipment_status_id", OrderShipmentStatus::WMS_ERROR); $results['added_to_snap_error']++; return false; } }
/** * Snap Outbound numbers */ public function snapOutbound() { ini_set("max_execution_time", 120); $result = []; try { $response = SnapFulfilAPI::call("snapoutbound", NULL, FALSE, 1); $result['outbound']['Lehi'] = count($response); $response = SnapFulfilAPI::call("snapoutbound", NULL, FALSE, 2); $result['outbound']['Netherlands'] = count($response); $response = SnapFulfilAPI::call("snapoutbound", NULL, FALSE, 3); $result['outbound']['Mexico'] = count($response); } catch (Exception $e) { } $command_due_out = 'Receipts?$filter=Stage+eq+%2700%27'; //get due out sums try { $response = SnapFulfilAPI::call($command_due_out, NULL, FALSE, 1); $result['due_out_sum']['Lehi'] = count($response); $response = SnapFulfilAPI::call($command_due_out, NULL, FALSE, 2); $result['due_out_sum']['Netherlands'] = count($response); $response = SnapFulfilAPI::call($command_due_out, NULL, FALSE, 3); $result['due_out_sum']['Mexico'] = count($response); } catch (Exception $e) { } $stages_ary = [10 => 'Allocate', 11 => 'Allocated', 15 => 'Release', 16 => 'Shortage', 20 => 'Pick', 21 => 'Picking', 29 => 'Picked', 30 => 'Packed', 31 => 'Packing', 39 => 'Packed', 49 => 'Marshalled']; foreach ($stages_ary as $stage_num => $stage_name) { $command_released = 'Receipts?$filter=Stage+eq+%27' . $stage_num . '%27'; //get released sums try { $response = SnapFulfilAPI::call($command_released, NULL, FALSE, 1); $result['released_sum']['Lehi'] += count($response); $result['released_' . $stage_name]['Lehi'] += count($response); $response = SnapFulfilAPI::call($command_released, NULL, FALSE, 2); $result['released_sum']['Netherlands'] += count($response); $result['released_' . $stage_name]['Netherlands'] += count($response); $response = SnapFulfilAPI::call($command_released, NULL, FALSE, 3); $result['released_sum']['Mexico'] += count($response); $result['released_' . $stage_name]['Mexico'] += count($response); } catch (Exception $e) { } } $this->sendSuccess($result); }