/**
  * List of outstanding shipments by date and status
  *
  * @param OrderShipment $Model
  * @param bool $count
  * @param null $date
  * @param null $status
  * @return array|null
  */
 public function outstandingShipments(OrderShipment $Model, $count = true, $date = null, $status = null)
 {
     $Model->useDbConfig = 'replicated';
     $Model->virtualFields = [];
     if ($count == true) {
         $find = 'all';
         $fields = ['COUNT(DISTINCT OrderShipment.id) as count', 'OrderShipment.order_shipment_status_id as status', 'DATE_FORMAT(OrderShipment.created, \'%Y-%m-%d\') as count_date'];
         $group = ['count_date', 'status'];
     } else {
         $find = 'all';
         $fields = [];
         $group = [];
     }
     if (!empty($date)) {
         $conditions = ['OrderShipment.order_shipment_status_id' => $status, 'DATE_FORMAT(OrderShipment.created, \'%Y-%m-%d\')' => $date];
     } else {
         $conditions = ['NOT' => ['OrderShipment.order_shipment_status_id' => [OrderShipmentStatus::FULFILLED, OrderShipmentStatus::CANCELLED, OrderShipmentStatus::UN_SHIPPABLE, OrderShipmentStatus::PENDING]]];
     }
     if ($count) {
         $counts = $Model->find($find, ['fields' => $fields, 'conditions' => $conditions, 'group' => ['count_date', 'status'], 'order' => ['OrderShipment.created DESC']]);
         $results = [];
         foreach ($counts as $result) {
             $current_count = $result[0]['count'];
             $current_status = $result['OrderShipment']['status'];
             $current_date = $result[0]['count_date'];
             $results[$current_date][] = ['status' => $current_status, 'count' => $current_count];
         }
     } else {
         $results = $Model->find($find, ['fields' => $fields, 'conditions' => $conditions, 'contain' => ['OrderShipment', 'Order' => ['fields' => ['Order.id', 'Order.order_market_id', 'Order.date_completed']]], 'group' => $group, 'order' => ['OrderShipment.created DESC'], 'limit' => 100]);
     }
     return $results;
 }
 /**
  * Close a shipment
  *
  * @param OrderShipment $Model
  * @param null $id
  * @param null $snap_shipment
  * @param null $date_created
  * @return bool
  * @throws Exception
  */
 public function closeShipment(OrderShipment $Model, $id = null, $snap_shipment = null, $date_created = null)
 {
     if (empty($id)) {
         $id = $Model->id;
     }
     if (empty($snap_shipment)) {
         throw new Exception('No Snap Shipment');
     }
     $this->User = ClassRegistry::init('User');
     $now = new DateTime();
     $is_backorder = FALSE;
     $charm_order = FALSE;
     $order_shipment = $Model->find('first', ['contain' => ['Order' => ['OrderCustomer' => ['Email']], 'OrderItem'], 'conditions' => ['OrderShipment.id' => $id]]);
     foreach ($order_shipment['OrderShipment']['OrderItem'] as $item) {
         if ($item['order_item_type_id'] == OrderItemType::TYPE_BACK_ORDER) {
             $is_backorder = TRUE;
         }
     }
     if ($order_shipment['Order']['order_type_id'] == Order::TYPE_CHARM) {
         $charm_order = TRUE;
     }
     $shipment_tracking_number = $order_shipment['OrderShipment']['tracking_number'];
     $shipment_date_shipped = $order_shipment['OrderShipment']['date_shipped'];
     /** @noinspection PhpUndefinedFieldInspection */
     $snap_tracking_number = $snap_shipment->CarrierTrackingNumber;
     /** @noinspection PhpUndefinedFieldInspection */
     $snap_shipping_method = $snap_shipment->ShippingMethod;
     /** @noinspection PhpUndefinedFieldInspection */
     $snap_date_closed = $snap_shipment->DateClosed;
     /** @noinspection PhpUndefinedFieldInspection */
     $snap_customer_name = $snap_shipment->CustomerName;
     /** @noinspection PhpUndefinedFieldInspection */
     $snap_order_id = $snap_shipment->CustomerRef;
     $order_status_id = $order_shipment['Order']['order_status_id'];
     $country_id = $order_shipment['OrderShipment']['country_id'];
     $user_id = $order_shipment['Order']['user_id'];
     $email = $order_shipment['Order']['OrderCustomer']['Email']['email'];
     if (empty($shipment_tracking_number) && empty($shipment_date_shipped)) {
         $Model->saveField("tracking_number", $snap_tracking_number);
         $Model->saveField("carrier", $snap_shipping_method);
         $Model->saveField("date_shipped", $snap_date_closed);
         $Model->saveField('order_shipment_status_id', OrderShipmentStatus::FULFILLED);
     }
     //to try to avoid sending too many emails, check the order status.
     $notification_date_obj = new DateTime($date_created);
     $interval = $now->diff($notification_date_obj);
     if ($interval->days >= 2) {
         return true;
     }
     if ($order_status_id == Order::STATUS_SHIPPED && !$is_backorder && $interval->days >= 2) {
         return true;
     }
     if ($snap_tracking_number && $snap_shipping_method) {
         $tracking_url = $Model->getTrackingLink($snap_tracking_number, $snap_shipping_method);
     } else {
         $tracking_url = '';
     }
     if (!$charm_order) {
         /** @noinspection PhpUndefinedFieldInspection */
         $email_data = ["name" => $snap_customer_name, "order_id" => $snap_order_id, "tracking_number" => $snap_shipment->CarrierTrackingNumber, "tracking_url" => $tracking_url, "country_id" => $country_id];
         $userLocale = $this->User->userLocale($user_id);
         $usable_userLocale = empty($userLocale['User']['locale']) ? 'en_US' : $userLocale['User']['locale'];
         if (!empty($email)) {
             YouniqueEmail::queueEmail($email_data, 'en_us_tracking_numbers', $email, 'email order tracking', 'purchase-page', $usable_userLocale);
         }
     }
     return true;
 }