00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 class Mage_Sales_Model_Order_Shipment extends Mage_Sales_Model_Abstract
00029 {
00030 const STATUS_NEW = 1;
00031
00032 const XML_PATH_EMAIL_TEMPLATE = 'sales_email/shipment/template';
00033 const XML_PATH_EMAIL_GUEST_TEMPLATE = 'sales_email/shipment/guest_template';
00034 const XML_PATH_EMAIL_IDENTITY = 'sales_email/shipment/identity';
00035 const XML_PATH_EMAIL_COPY_TO = 'sales_email/shipment/copy_to';
00036 const XML_PATH_EMAIL_COPY_METHOD = 'sales_email/shipment/copy_method';
00037 const XML_PATH_EMAIL_ENABLED = 'sales_email/shipment/enabled';
00038
00039 const XML_PATH_UPDATE_EMAIL_TEMPLATE = 'sales_email/shipment_comment/template';
00040 const XML_PATH_UPDATE_EMAIL_GUEST_TEMPLATE = 'sales_email/shipment_comment/guest_template';
00041 const XML_PATH_UPDATE_EMAIL_IDENTITY = 'sales_email/shipment_comment/identity';
00042 const XML_PATH_UPDATE_EMAIL_COPY_TO = 'sales_email/shipment_comment/copy_to';
00043 const XML_PATH_UPDATE_EMAIL_COPY_METHOD = 'sales_email/shipment_comment/copy_method';
00044 const XML_PATH_UPDATE_EMAIL_ENABLED = 'sales_email/shipment_comment/enabled';
00045
00046 protected $_items;
00047 protected $_tracks;
00048 protected $_order;
00049
00050 protected $_eventPrefix = 'sales_order_shipment';
00051 protected $_eventObject = 'shipment';
00052
00053
00054
00055
00056 protected function _construct()
00057 {
00058 $this->_init('sales/order_shipment');
00059 }
00060
00061
00062
00063
00064
00065
00066
00067 public function loadByIncrementId($incrementId)
00068 {
00069 $ids = $this->getCollection()
00070 ->addAttributeToFilter('increment_id', $incrementId)
00071 ->getAllIds();
00072
00073 if (!empty($ids)) {
00074 reset($ids);
00075 $this->load(current($ids));
00076 }
00077 return $this;
00078 }
00079
00080
00081
00082
00083
00084
00085
00086
00087 public function setOrder(Mage_Sales_Model_Order $order)
00088 {
00089 $this->_order = $order;
00090 $this->setOrderId($order->getId())
00091 ->setStoreId($order->getStoreId());
00092 return $this;
00093 }
00094
00095
00096
00097
00098
00099
00100 public function getOrder()
00101 {
00102 if (!$this->_order instanceof Mage_Sales_Model_Order) {
00103 $this->_order = Mage::getModel('sales/order')->load($this->getOrderId());
00104 }
00105 return $this->_order;
00106 }
00107
00108
00109
00110
00111
00112
00113 public function getBillingAddress()
00114 {
00115 return $this->getOrder()->getBillingAddress();
00116 }
00117
00118
00119
00120
00121
00122
00123 public function getShippingAddress()
00124 {
00125 return $this->getOrder()->getShippingAddress();
00126 }
00127
00128
00129
00130
00131
00132
00133
00134
00135 public function register()
00136 {
00137 if ($this->getId()) {
00138 Mage::throwException(
00139 Mage::helper('sales')->__('Can not register existing shipment')
00140 );
00141 }
00142
00143 $totalQty = 0;
00144 foreach ($this->getAllItems() as $item) {
00145 if ($item->getQty()>0) {
00146 $item->register();
00147 if (!$item->getOrderItem()->isDummy(true)) {
00148 $totalQty+= $item->getQty();
00149 }
00150 }
00151 else {
00152 $item->isDeleted(true);
00153 }
00154 }
00155 $this->setTotalQty($totalQty);
00156
00157 return $this;
00158 }
00159
00160 public function getItemsCollection()
00161 {
00162 if (empty($this->_items)) {
00163 $this->_items = Mage::getResourceModel('sales/order_shipment_item_collection')
00164 ->addAttributeToSelect('*')
00165 ->setShipmentFilter($this->getId());
00166
00167 if ($this->getId()) {
00168 foreach ($this->_items as $item) {
00169 $item->setShipment($this);
00170 }
00171 }
00172 }
00173 return $this->_items;
00174 }
00175
00176 public function getAllItems()
00177 {
00178 $items = array();
00179 foreach ($this->getItemsCollection() as $item) {
00180 if (!$item->isDeleted()) {
00181 $items[] = $item;
00182 }
00183 }
00184 return $items;
00185 }
00186
00187 public function getItemById($itemId)
00188 {
00189 foreach ($this->getItemsCollection() as $item) {
00190 if ($item->getId()==$itemId) {
00191 return $item;
00192 }
00193 }
00194 return false;
00195 }
00196
00197 public function addItem(Mage_Sales_Model_Order_Shipment_Item $item)
00198 {
00199 $item->setShipment($this)
00200 ->setParentId($this->getId())
00201 ->setStoreId($this->getStoreId());
00202 if (!$item->getId()) {
00203 $this->getItemsCollection()->addItem($item);
00204 }
00205 return $this;
00206 }
00207
00208
00209 public function getTracksCollection()
00210 {
00211 if (empty($this->_tracks)) {
00212 $this->_tracks = Mage::getResourceModel('sales/order_shipment_track_collection')
00213 ->addAttributeToSelect('*')
00214 ->setShipmentFilter($this->getId());
00215
00216 if ($this->getId()) {
00217 foreach ($this->_tracks as $track) {
00218 $track->setShipment($this);
00219 }
00220 }
00221 }
00222 return $this->_tracks;
00223 }
00224
00225 public function getAllTracks()
00226 {
00227 $tracks = array();
00228 foreach ($this->getTracksCollection() as $track) {
00229 if (!$track->isDeleted()) {
00230 $tracks[] = $track;
00231 }
00232 }
00233 return $tracks;
00234 }
00235
00236 public function getTrackById($trackId)
00237 {
00238 foreach ($this->getTracksCollection() as $track) {
00239 if ($track->getId()==$trackId) {
00240 return $track;
00241 }
00242 }
00243 return false;
00244 }
00245
00246 public function addTrack(Mage_Sales_Model_Order_Shipment_Track $track)
00247 {
00248 $track->setShipment($this)
00249 ->setParentId($this->getId())
00250 ->setOrderId($this->getOrderId())
00251 ->setStoreId($this->getStoreId());
00252 if (!$track->getId()) {
00253 $this->getTracksCollection()->addItem($track);
00254 }
00255 return $this;
00256 }
00257
00258 public function addComment($comment, $notify=false)
00259 {
00260 if (!($comment instanceof Mage_Sales_Model_Order_Shipment_Comment)) {
00261 $comment = Mage::getModel('sales/order_shipment_comment')
00262 ->setComment($comment)
00263 ->setIsCustomerNotified($notify);
00264 }
00265 $comment->setShipment($this)
00266 ->setParentId($this->getId())
00267 ->setStoreId($this->getStoreId());
00268 if (!$comment->getId()) {
00269 $this->getCommentsCollection()->addItem($comment);
00270 }
00271 return $this;
00272 }
00273
00274 public function getCommentsCollection($reload=false)
00275 {
00276 if (is_null($this->_comments) || $reload) {
00277 $this->_comments = Mage::getResourceModel('sales/order_shipment_comment_collection')
00278 ->addAttributeToSelect('*')
00279 ->setShipmentFilter($this->getId())
00280 ->setCreatedAtOrder();
00281 if ($this->getId()) {
00282 foreach ($this->_comments as $comment) {
00283 $comment->setShipment($this);
00284 }
00285 }
00286 }
00287 return $this->_comments;
00288 }
00289
00290
00291
00292
00293
00294
00295 public function sendEmail($notifyCustomer=true, $comment='')
00296 {
00297 if (!Mage::helper('sales')->canSendNewShipmentEmail($this->getOrder()->getStore()->getId())) {
00298 return $this;
00299 }
00300
00301 $currentDesign = Mage::getDesign()->setAllGetOld(array(
00302 'package' => Mage::getStoreConfig('design/package/name', $this->getStoreId()),
00303 'store' => $this->getStoreId()
00304 ));
00305
00306 $translate = Mage::getSingleton('core/translate');
00307
00308 $translate->setTranslateInline(false);
00309
00310 $order = $this->getOrder();
00311 $copyTo = $this->_getEmails(self::XML_PATH_EMAIL_COPY_TO);
00312 $copyMethod = Mage::getStoreConfig(self::XML_PATH_EMAIL_COPY_METHOD, $this->getStoreId());
00313
00314 if (!$notifyCustomer && !$copyTo) {
00315 return $this;
00316 }
00317 $paymentBlock = Mage::helper('payment')->getInfoBlock($order->getPayment())
00318 ->setIsSecureMode(true);
00319 $mailTemplate = Mage::getModel('core/email_template');
00320
00321 if ($order->getCustomerIsGuest()) {
00322 $template = Mage::getStoreConfig(self::XML_PATH_EMAIL_GUEST_TEMPLATE, $order->getStoreId());
00323 $customerName = $order->getBillingAddress()->getName();
00324 } else {
00325 $template = Mage::getStoreConfig(self::XML_PATH_EMAIL_TEMPLATE, $order->getStoreId());
00326 $customerName = $order->getCustomerName();
00327 }
00328
00329 if ($notifyCustomer) {
00330 $sendTo[] = array(
00331 'name' => $customerName,
00332 'email' => $order->getCustomerEmail()
00333 );
00334 if ($copyTo && $copyMethod == 'bcc') {
00335 foreach ($copyTo as $email) {
00336 $mailTemplate->addBcc($email);
00337 }
00338 }
00339
00340 }
00341
00342 if ($copyTo && ($copyMethod == 'copy' || !$notifyCustomer)) {
00343 foreach ($copyTo as $email) {
00344 $sendTo[] = array(
00345 'name' => null,
00346 'email' => $email
00347 );
00348 }
00349 }
00350
00351 foreach ($sendTo as $recipient) {
00352 $mailTemplate->setDesignConfig(array('area'=>'frontend', 'store'=>$order->getStoreId()))
00353 ->sendTransactional(
00354 $template,
00355 Mage::getStoreConfig(self::XML_PATH_EMAIL_IDENTITY, $order->getStoreId()),
00356 $recipient['email'],
00357 $recipient['name'],
00358 array(
00359 'order' => $order,
00360 'shipment' => $this,
00361 'comment' => $comment,
00362 'billing' => $order->getBillingAddress(),
00363 'payment_html'=> $paymentBlock->toHtml(),
00364 )
00365 );
00366 }
00367
00368 $translate->setTranslateInline(true);
00369
00370 Mage::getDesign()->setAllGetOld($currentDesign);
00371
00372 return $this;
00373 }
00374
00375
00376
00377
00378
00379
00380 public function sendUpdateEmail($notifyCustomer = true, $comment='')
00381 {
00382 if (!Mage::helper('sales')->canSendShipmentCommentEmail($this->getOrder()->getStore()->getId())) {
00383 return $this;
00384 }
00385
00386 $currentDesign = Mage::getDesign()->setAllGetOld(array(
00387 'package' => Mage::getStoreConfig('design/package/name', $this->getStoreId()),
00388 ));
00389
00390 $translate = Mage::getSingleton('core/translate');
00391
00392 $translate->setTranslateInline(false);
00393
00394 $order = $this->getOrder();
00395
00396 $copyTo = $this->_getEmails(self::XML_PATH_UPDATE_EMAIL_COPY_TO);
00397 $copyMethod = Mage::getStoreConfig(self::XML_PATH_UPDATE_EMAIL_COPY_METHOD, $this->getStoreId());
00398
00399 if (!$notifyCustomer && !$copyTo) {
00400 return $this;
00401 }
00402
00403 $mailTemplate = Mage::getModel('core/email_template');
00404
00405 if ($order->getCustomerIsGuest()) {
00406 $template = Mage::getStoreConfig(self::XML_PATH_UPDATE_EMAIL_GUEST_TEMPLATE, $order->getStoreId());
00407 $customerName = $order->getBillingAddress()->getName();
00408 } else {
00409 $template = Mage::getStoreConfig(self::XML_PATH_UPDATE_EMAIL_TEMPLATE, $order->getStoreId());
00410 $customerName = $order->getCustomerName();
00411 }
00412
00413 if ($notifyCustomer) {
00414 $sendTo[] = array(
00415 'name' => $customerName,
00416 'email' => $order->getCustomerEmail()
00417 );
00418 if ($copyTo && $copyMethod == 'bcc') {
00419 foreach ($copyTo as $email) {
00420 $mailTemplate->addBcc($email);
00421 }
00422 }
00423
00424 }
00425
00426 if ($copyTo && ($copyMethod == 'copy' || !$notifyCustomer)) {
00427 foreach ($copyTo as $email) {
00428 $sendTo[] = array(
00429 'name' => null,
00430 'email' => $email
00431 );
00432 }
00433 }
00434
00435 foreach ($sendTo as $recipient) {
00436 $mailTemplate->setDesignConfig(array('area'=>'frontend', 'store'=>$order->getStoreId()))
00437 ->sendTransactional(
00438 $template,
00439 Mage::getStoreConfig(self::XML_PATH_UPDATE_EMAIL_IDENTITY, $order->getStoreId()),
00440 $recipient['email'],
00441 $recipient['name'],
00442 array(
00443 'order' => $order,
00444 'billing' => $order->getBillingAddress(),
00445 'shipment'=> $this,
00446 'comment' => $comment
00447 )
00448 );
00449 }
00450
00451 $translate->setTranslateInline(true);
00452
00453 Mage::getDesign()->setAllGetOld($currentDesign);
00454
00455 return $this;
00456 }
00457
00458 protected function _getEmails($configPath)
00459 {
00460 $data = Mage::getStoreConfig($configPath, $this->getStoreId());
00461 if (!empty($data)) {
00462 return explode(',', $data);
00463 }
00464 return false;
00465 }
00466
00467 protected function _beforeSave()
00468 {
00469 if (!count($this->getAllItems())) {
00470 Mage::throwException(
00471 Mage::helper('sales')->__('Cannot create an empty shipment.')
00472 );
00473 }
00474 }
00475
00476 protected function _beforeDelete()
00477 {
00478 $this->_protectFromNonAdmin();
00479 return parent::_beforeDelete();
00480 }
00481
00482
00483
00484
00485
00486
00487 public function getStore()
00488 {
00489 return $this->getOrder()->getStore();
00490 }
00491 }