1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27:
28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121:
122: class Mage_Sales_Model_Order_Invoice extends Mage_Sales_Model_Abstract
123: {
124: 125: 126:
127: const STATE_OPEN = 1;
128: const STATE_PAID = 2;
129: const STATE_CANCELED = 3;
130:
131: const CAPTURE_ONLINE = 'online';
132: const CAPTURE_OFFLINE = 'offline';
133: const NOT_CAPTURE = 'not_capture';
134:
135: const XML_PATH_EMAIL_TEMPLATE = 'sales_email/invoice/template';
136: const XML_PATH_EMAIL_GUEST_TEMPLATE = 'sales_email/invoice/guest_template';
137: const XML_PATH_EMAIL_IDENTITY = 'sales_email/invoice/identity';
138: const XML_PATH_EMAIL_COPY_TO = 'sales_email/invoice/copy_to';
139: const XML_PATH_EMAIL_COPY_METHOD = 'sales_email/invoice/copy_method';
140: const XML_PATH_EMAIL_ENABLED = 'sales_email/invoice/enabled';
141:
142: const XML_PATH_UPDATE_EMAIL_TEMPLATE = 'sales_email/invoice_comment/template';
143: const XML_PATH_UPDATE_EMAIL_GUEST_TEMPLATE = 'sales_email/invoice_comment/guest_template';
144: const XML_PATH_UPDATE_EMAIL_IDENTITY = 'sales_email/invoice_comment/identity';
145: const XML_PATH_UPDATE_EMAIL_COPY_TO = 'sales_email/invoice_comment/copy_to';
146: const XML_PATH_UPDATE_EMAIL_COPY_METHOD = 'sales_email/invoice_comment/copy_method';
147: const XML_PATH_UPDATE_EMAIL_ENABLED = 'sales_email/invoice_comment/enabled';
148:
149: const REPORT_DATE_TYPE_ORDER_CREATED = 'order_created';
150: const REPORT_DATE_TYPE_INVOICE_CREATED = 'invoice_created';
151:
152: 153: 154:
155: const HISTORY_ENTITY_NAME = 'invoice';
156:
157: protected static $_states;
158:
159: protected $_items;
160: protected ;
161: protected $_order;
162:
163: 164: 165: 166: 167:
168: protected $_rounders = array();
169:
170: protected $_saveBeforeDestruct = false;
171:
172: protected $_eventPrefix = 'sales_order_invoice';
173: protected $_eventObject = 'invoice';
174:
175: 176: 177: 178:
179: protected $_wasPayCalled = false;
180:
181: public function __destruct()
182: {
183: if ($this->_saveBeforeDestruct) {
184: $this->save();
185: }
186: }
187:
188: 189: 190:
191: protected function _construct()
192: {
193: $this->_init('sales/order_invoice');
194: }
195:
196: 197: 198: 199: 200:
201: protected function _initOldFieldsMap()
202: {
203: $this->_oldFieldsMap = Mage::helper('sales')->getOldFieldMap('order_invoice');
204: return $this;
205: }
206:
207: 208: 209: 210: 211: 212:
213: public function loadByIncrementId($incrementId)
214: {
215: $ids = $this->getCollection()
216: ->addAttributeToFilter('increment_id', $incrementId)
217: ->getAllIds();
218:
219: if (!empty($ids)) {
220: reset($ids);
221: $this->load(current($ids));
222: }
223: return $this;
224: }
225:
226: 227: 228: 229: 230:
231: public function getConfig()
232: {
233: return Mage::getSingleton('sales/order_invoice_config');
234: }
235:
236: 237: 238: 239: 240:
241: public function getStore()
242: {
243: return $this->getOrder()->getStore();
244: }
245:
246: 247: 248: 249: 250: 251:
252: public function setOrder(Mage_Sales_Model_Order $order)
253: {
254: $this->_order = $order;
255: $this->setOrderId($order->getId())
256: ->setStoreId($order->getStoreId());
257: return $this;
258: }
259:
260: 261: 262: 263: 264:
265: public function getOrder()
266: {
267: if (!$this->_order instanceof Mage_Sales_Model_Order) {
268: $this->_order = Mage::getModel('sales/order')->load($this->getOrderId());
269: }
270: return $this->_order->setHistoryEntityName(self::HISTORY_ENTITY_NAME);
271: }
272:
273: 274: 275: 276: 277:
278: public function getOrderIncrementId()
279: {
280: return Mage::getModel('sales/order')->getResource()->getIncrementId($this->getOrderId());
281: }
282:
283: 284: 285: 286: 287:
288: public function getBillingAddress()
289: {
290: return $this->getOrder()->getBillingAddress();
291: }
292:
293: 294: 295: 296: 297:
298: public function getShippingAddress()
299: {
300: return $this->getOrder()->getShippingAddress();
301: }
302:
303: 304: 305: 306: 307:
308: public function isCanceled()
309: {
310: return $this->getState() == self::STATE_CANCELED;
311: }
312:
313: 314: 315: 316: 317:
318: public function canCapture()
319: {
320: return $this->getState() != self::STATE_CANCELED
321: && $this->getState() != self::STATE_PAID
322: && $this->getOrder()->getPayment()->canCapture();
323: }
324:
325: 326: 327: 328: 329:
330: public function canVoid()
331: {
332: $canVoid = false;
333: if ($this->getState() == self::STATE_PAID) {
334: $canVoid = $this->getCanVoidFlag();
335: 336: 337:
338: if (is_null($canVoid)) {
339: $canVoid = $this->getOrder()->getPayment()->canVoid($this);
340: if ($canVoid === false) {
341: $this->setCanVoidFlag(false);
342: $this->_saveBeforeDestruct = true;
343: }
344: }
345: else {
346: $canVoid = (bool) $canVoid;
347: }
348: }
349: return $canVoid;
350: }
351:
352: 353: 354: 355: 356:
357: public function canCancel()
358: {
359: return $this->getState() == self::STATE_OPEN;
360: }
361:
362: 363: 364: 365: 366:
367: public function canRefund()
368: {
369: if ($this->getState() != self::STATE_PAID) {
370: return false;
371: }
372: if (abs($this->getBaseGrandTotal() - $this->getBaseTotalRefunded()) < .0001) {
373: return false;
374: }
375: return true;
376: }
377:
378: 379: 380: 381: 382:
383: public function capture()
384: {
385: $this->getOrder()->getPayment()->capture($this);
386: if ($this->getIsPaid()) {
387: $this->pay();
388: }
389: return $this;
390: }
391:
392: 393: 394: 395: 396:
397: public function pay()
398: {
399: if ($this->_wasPayCalled) {
400: return $this;
401: }
402: $this->_wasPayCalled = true;
403:
404: $invoiceState = self::STATE_PAID;
405: if ($this->getOrder()->getPayment()->hasForcedState()) {
406: $invoiceState = $this->getOrder()->getPayment()->getForcedState();
407: }
408:
409: $this->setState($invoiceState);
410:
411: $this->getOrder()->getPayment()->pay($this);
412: $this->getOrder()->setTotalPaid(
413: $this->getOrder()->getTotalPaid()+$this->getGrandTotal()
414: );
415: $this->getOrder()->setBaseTotalPaid(
416: $this->getOrder()->getBaseTotalPaid()+$this->getBaseGrandTotal()
417: );
418: Mage::dispatchEvent('sales_order_invoice_pay', array($this->_eventObject=>$this));
419: return $this;
420: }
421:
422: 423: 424: 425:
426: public function wasPayCalled()
427: {
428: return $this->_wasPayCalled;
429: }
430:
431: 432: 433: 434: 435:
436: public function void()
437: {
438: $this->getOrder()->getPayment()->void($this);
439: $this->cancel();
440: return $this;
441: }
442:
443: 444: 445: 446: 447:
448: public function cancel()
449: {
450: $order = $this->getOrder();
451: $order->getPayment()->cancelInvoice($this);
452: foreach ($this->getAllItems() as $item) {
453: $item->cancel();
454: }
455:
456: 457: 458:
459: $order->setTotalInvoiced($order->getTotalInvoiced() - $this->getGrandTotal());
460: $order->setBaseTotalInvoiced($order->getBaseTotalInvoiced() - $this->getBaseGrandTotal());
461:
462: $order->setSubtotalInvoiced($order->getSubtotalInvoiced() - $this->getSubtotal());
463: $order->setBaseSubtotalInvoiced($order->getBaseSubtotalInvoiced() - $this->getBaseSubtotal());
464:
465: $order->setTaxInvoiced($order->getTaxInvoiced() - $this->getTaxAmount());
466: $order->setBaseTaxInvoiced($order->getBaseTaxInvoiced() - $this->getBaseTaxAmount());
467:
468: $order->setHiddenTaxInvoiced($order->getHiddenTaxInvoiced() - $this->getHiddenTaxAmount());
469: $order->setBaseHiddenTaxInvoiced($order->getBaseHiddenTaxInvoiced() - $this->getBaseHiddenTaxAmount());
470:
471: $order->setShippingTaxInvoiced($order->getShippingTaxInvoiced() - $this->getShippingTaxAmount());
472: $order->setBaseShippingTaxInvoiced($order->getBaseShippingTaxInvoiced() - $this->getBaseShippingTaxAmount());
473:
474: $order->setShippingInvoiced($order->getShippingInvoiced() - $this->getShippingAmount());
475: $order->setBaseShippingInvoiced($order->getBaseShippingInvoiced() - $this->getBaseShippingAmount());
476:
477: $order->setDiscountInvoiced($order->getDiscountInvoiced() - $this->getDiscountAmount());
478: $order->setBaseDiscountInvoiced($order->getBaseDiscountInvoiced() - $this->getBaseDiscountAmount());
479: $order->setBaseTotalInvoicedCost($order->getBaseTotalInvoicedCost() - $this->getBaseCost());
480:
481:
482: if ($this->getState() == self::STATE_PAID) {
483: $this->getOrder()->setTotalPaid($this->getOrder()->getTotalPaid()-$this->getGrandTotal());
484: $this->getOrder()->setBaseTotalPaid($this->getOrder()->getBaseTotalPaid()-$this->getBaseGrandTotal());
485: }
486: $this->setState(self::STATE_CANCELED);
487: $this->getOrder()->setState(Mage_Sales_Model_Order::STATE_PROCESSING, true);
488: Mage::dispatchEvent('sales_order_invoice_cancel', array($this->_eventObject=>$this));
489: return $this;
490: }
491:
492: 493: 494: 495: 496:
497: public function collectTotals()
498: {
499: foreach ($this->getConfig()->getTotalModels() as $model) {
500: $model->collect($this);
501: }
502: return $this;
503: }
504:
505: 506: 507: 508: 509: 510: 511: 512:
513: public function roundPrice($price, $type = 'regular', $negative = false)
514: {
515: if ($price) {
516: if (!isset($this->_rounders[$type])) {
517: $this->_rounders[$type] = Mage::getModel('core/calculator', $this->getStore());
518: }
519: $price = $this->_rounders[$type]->deltaRound($price, $negative);
520: }
521: return $price;
522: }
523:
524: 525: 526: 527: 528:
529: public function getItemsCollection()
530: {
531: if (empty($this->_items)) {
532: $this->_items = Mage::getResourceModel('sales/order_invoice_item_collection')
533: ->setInvoiceFilter($this->getId());
534:
535: if ($this->getId()) {
536: foreach ($this->_items as $item) {
537: $item->setInvoice($this);
538: }
539: }
540: }
541: return $this->_items;
542: }
543:
544: public function getAllItems()
545: {
546: $items = array();
547: foreach ($this->getItemsCollection() as $item) {
548: if (!$item->isDeleted()) {
549: $items[] = $item;
550: }
551: }
552: return $items;
553: }
554:
555: public function getItemById($itemId)
556: {
557: foreach ($this->getItemsCollection() as $item) {
558: if ($item->getId()==$itemId) {
559: return $item;
560: }
561: }
562: return false;
563: }
564:
565: public function addItem(Mage_Sales_Model_Order_Invoice_Item $item)
566: {
567: $item->setInvoice($this)
568: ->setParentId($this->getId())
569: ->setStoreId($this->getStoreId());
570:
571: if (!$item->getId()) {
572: $this->getItemsCollection()->addItem($item);
573: }
574: return $this;
575: }
576:
577: 578: 579: 580: 581:
582: public static function getStates()
583: {
584: if (is_null(self::$_states)) {
585: self::$_states = array(
586: self::STATE_OPEN => Mage::helper('sales')->__('Pending'),
587: self::STATE_PAID => Mage::helper('sales')->__('Paid'),
588: self::STATE_CANCELED => Mage::helper('sales')->__('Canceled'),
589: );
590: }
591: return self::$_states;
592: }
593:
594: 595: 596: 597: 598: 599:
600: public function getStateName($stateId = null)
601: {
602: if (is_null($stateId)) {
603: $stateId = $this->getState();
604: }
605:
606: if (is_null(self::$_states)) {
607: self::getStates();
608: }
609: if (isset(self::$_states[$stateId])) {
610: return self::$_states[$stateId];
611: }
612: return Mage::helper('sales')->__('Unknown State');
613: }
614:
615: 616: 617: 618: 619: 620: 621:
622: public function register()
623: {
624: if ($this->getId()) {
625: Mage::throwException(Mage::helper('sales')->__('Cannot register existing invoice'));
626: }
627:
628: foreach ($this->getAllItems() as $item) {
629: if ($item->getQty()>0) {
630: $item->register();
631: }
632: else {
633: $item->isDeleted(true);
634: }
635: }
636:
637: $order = $this->getOrder();
638: $captureCase = $this->getRequestedCaptureCase();
639: if ($this->canCapture()) {
640: if ($captureCase) {
641: if ($captureCase == self::CAPTURE_ONLINE) {
642: $this->capture();
643: }
644: elseif ($captureCase == self::CAPTURE_OFFLINE) {
645: $this->setCanVoidFlag(false);
646: $this->pay();
647: }
648: }
649: } elseif(!$order->getPayment()->getMethodInstance()->isGateway() || $captureCase == self::CAPTURE_OFFLINE) {
650: if (!$order->getPayment()->getIsTransactionPending()) {
651: $this->setCanVoidFlag(false);
652: $this->pay();
653: }
654: }
655:
656: $order->setTotalInvoiced($order->getTotalInvoiced() + $this->getGrandTotal());
657: $order->setBaseTotalInvoiced($order->getBaseTotalInvoiced() + $this->getBaseGrandTotal());
658:
659: $order->setSubtotalInvoiced($order->getSubtotalInvoiced() + $this->getSubtotal());
660: $order->setBaseSubtotalInvoiced($order->getBaseSubtotalInvoiced() + $this->getBaseSubtotal());
661:
662: $order->setTaxInvoiced($order->getTaxInvoiced() + $this->getTaxAmount());
663: $order->setBaseTaxInvoiced($order->getBaseTaxInvoiced() + $this->getBaseTaxAmount());
664:
665: $order->setHiddenTaxInvoiced($order->getHiddenTaxInvoiced() + $this->getHiddenTaxAmount());
666: $order->setBaseHiddenTaxInvoiced($order->getBaseHiddenTaxInvoiced() + $this->getBaseHiddenTaxAmount());
667:
668: $order->setShippingTaxInvoiced($order->getShippingTaxInvoiced() + $this->getShippingTaxAmount());
669: $order->setBaseShippingTaxInvoiced($order->getBaseShippingTaxInvoiced() + $this->getBaseShippingTaxAmount());
670:
671:
672: $order->setShippingInvoiced($order->getShippingInvoiced() + $this->getShippingAmount());
673: $order->setBaseShippingInvoiced($order->getBaseShippingInvoiced() + $this->getBaseShippingAmount());
674:
675: $order->setDiscountInvoiced($order->getDiscountInvoiced() + $this->getDiscountAmount());
676: $order->setBaseDiscountInvoiced($order->getBaseDiscountInvoiced() + $this->getBaseDiscountAmount());
677: $order->setBaseTotalInvoicedCost($order->getBaseTotalInvoicedCost() + $this->getBaseCost());
678:
679: $state = $this->getState();
680: if (is_null($state)) {
681: $this->setState(self::STATE_OPEN);
682: }
683:
684: Mage::dispatchEvent('sales_order_invoice_register', array($this->_eventObject=>$this, 'order' => $order));
685: return $this;
686: }
687:
688: 689: 690: 691: 692:
693: public function isLast()
694: {
695: foreach ($this->getAllItems() as $item) {
696: if (!$item->isLast()) {
697: return false;
698: }
699: }
700: return true;
701: }
702:
703: 704: 705: 706: 707: 708: 709: 710: 711:
712: public function ($comment, $notify=false, $visibleOnFront=false)
713: {
714: if (!($comment instanceof Mage_Sales_Model_Order_Invoice_Comment)) {
715: $comment = Mage::getModel('sales/order_invoice_comment')
716: ->setComment($comment)
717: ->setIsCustomerNotified($notify)
718: ->setIsVisibleOnFront($visibleOnFront);
719: }
720: $comment->setInvoice($this)
721: ->setStoreId($this->getStoreId())
722: ->setParentId($this->getId());
723: if (!$comment->getId()) {
724: $this->getCommentsCollection()->addItem($comment);
725: }
726: $this->_hasDataChanges = true;
727: return $this;
728: }
729:
730: public function ($reload=false)
731: {
732: if (is_null($this->_comments) || $reload) {
733: $this->_comments = Mage::getResourceModel('sales/order_invoice_comment_collection')
734: ->setInvoiceFilter($this->getId())
735: ->setCreatedAtOrder();
736: 737: 738: 739:
740: $this->_comments->load();
741:
742: if ($this->getId()) {
743: foreach ($this->_comments as $comment) {
744: $comment->setInvoice($this);
745: }
746: }
747: }
748: return $this->_comments;
749: }
750:
751: 752: 753: 754: 755: 756: 757:
758: public function sendEmail($notifyCustomer = true, $comment = '')
759: {
760: $order = $this->getOrder();
761: $storeId = $order->getStore()->getId();
762:
763: if (!Mage::helper('sales')->canSendNewInvoiceEmail($storeId)) {
764: return $this;
765: }
766:
767: $copyTo = $this->_getEmails(self::XML_PATH_EMAIL_COPY_TO);
768: $copyMethod = Mage::getStoreConfig(self::XML_PATH_EMAIL_COPY_METHOD, $storeId);
769:
770: if (!$notifyCustomer && !$copyTo) {
771: return $this;
772: }
773:
774:
775: $appEmulation = Mage::getSingleton('core/app_emulation');
776: $initialEnvironmentInfo = $appEmulation->startEnvironmentEmulation($storeId);
777:
778: try {
779:
780: $paymentBlock = Mage::helper('payment')->getInfoBlock($order->getPayment())
781: ->setIsSecureMode(true);
782: $paymentBlock->getMethod()->setStore($storeId);
783: $paymentBlockHtml = $paymentBlock->toHtml();
784: } catch (Exception $exception) {
785:
786: $appEmulation->stopEnvironmentEmulation($initialEnvironmentInfo);
787: throw $exception;
788: }
789:
790:
791: $appEmulation->stopEnvironmentEmulation($initialEnvironmentInfo);
792:
793:
794: if ($order->getCustomerIsGuest()) {
795: $templateId = Mage::getStoreConfig(self::XML_PATH_EMAIL_GUEST_TEMPLATE, $storeId);
796: $customerName = $order->getBillingAddress()->getName();
797: } else {
798: $templateId = Mage::getStoreConfig(self::XML_PATH_EMAIL_TEMPLATE, $storeId);
799: $customerName = $order->getCustomerName();
800: }
801:
802: $mailer = Mage::getModel('core/email_template_mailer');
803: if ($notifyCustomer) {
804: $emailInfo = Mage::getModel('core/email_info');
805: $emailInfo->addTo($order->getCustomerEmail(), $customerName);
806: if ($copyTo && $copyMethod == 'bcc') {
807:
808: foreach ($copyTo as $email) {
809: $emailInfo->addBcc($email);
810: }
811: }
812: $mailer->addEmailInfo($emailInfo);
813: }
814:
815:
816: if ($copyTo && ($copyMethod == 'copy' || !$notifyCustomer)) {
817: foreach ($copyTo as $email) {
818: $emailInfo = Mage::getModel('core/email_info');
819: $emailInfo->addTo($email);
820: $mailer->addEmailInfo($emailInfo);
821: }
822: }
823:
824:
825: $mailer->setSender(Mage::getStoreConfig(self::XML_PATH_EMAIL_IDENTITY, $storeId));
826: $mailer->setStoreId($storeId);
827: $mailer->setTemplateId($templateId);
828: $mailer->setTemplateParams(array(
829: 'order' => $order,
830: 'invoice' => $this,
831: 'comment' => $comment,
832: 'billing' => $order->getBillingAddress(),
833: 'payment_html' => $paymentBlockHtml
834: )
835: );
836: $mailer->send();
837: $this->setEmailSent(true);
838: $this->_getResource()->saveAttribute($this, 'email_sent');
839:
840: return $this;
841: }
842:
843: 844: 845: 846: 847: 848: 849:
850: public function sendUpdateEmail($notifyCustomer = true, $comment = '')
851: {
852: $order = $this->getOrder();
853: $storeId = $order->getStore()->getId();
854:
855: if (!Mage::helper('sales')->canSendInvoiceCommentEmail($storeId)) {
856: return $this;
857: }
858:
859: $copyTo = $this->_getEmails(self::XML_PATH_UPDATE_EMAIL_COPY_TO);
860: $copyMethod = Mage::getStoreConfig(self::XML_PATH_UPDATE_EMAIL_COPY_METHOD, $storeId);
861:
862: if (!$notifyCustomer && !$copyTo) {
863: return $this;
864: }
865:
866:
867: if ($order->getCustomerIsGuest()) {
868: $templateId = Mage::getStoreConfig(self::XML_PATH_UPDATE_EMAIL_GUEST_TEMPLATE, $storeId);
869: $customerName = $order->getBillingAddress()->getName();
870: } else {
871: $templateId = Mage::getStoreConfig(self::XML_PATH_UPDATE_EMAIL_TEMPLATE, $storeId);
872: $customerName = $order->getCustomerName();
873: }
874:
875: $mailer = Mage::getModel('core/email_template_mailer');
876: if ($notifyCustomer) {
877: $emailInfo = Mage::getModel('core/email_info');
878: $emailInfo->addTo($order->getCustomerEmail(), $customerName);
879: if ($copyTo && $copyMethod == 'bcc') {
880:
881: foreach ($copyTo as $email) {
882: $emailInfo->addBcc($email);
883: }
884: }
885: $mailer->addEmailInfo($emailInfo);
886: }
887:
888:
889: if ($copyTo && ($copyMethod == 'copy' || !$notifyCustomer)) {
890: foreach ($copyTo as $email) {
891: $emailInfo = Mage::getModel('core/email_info');
892: $emailInfo->addTo($email);
893: $mailer->addEmailInfo($emailInfo);
894: }
895: }
896:
897:
898: $mailer->setSender(Mage::getStoreConfig(self::XML_PATH_UPDATE_EMAIL_IDENTITY, $storeId));
899: $mailer->setStoreId($storeId);
900: $mailer->setTemplateId($templateId);
901: $mailer->setTemplateParams(array(
902: 'order' => $order,
903: 'invoice' => $this,
904: 'comment' => $comment,
905: 'billing' => $order->getBillingAddress()
906: )
907: );
908: $mailer->send();
909:
910: return $this;
911: }
912:
913: protected function _getEmails($configPath)
914: {
915: $data = Mage::getStoreConfig($configPath, $this->getStoreId());
916: if (!empty($data)) {
917: return explode(',', $data);
918: }
919: return false;
920: }
921:
922: protected function _beforeDelete()
923: {
924: $this->_protectFromNonAdmin();
925: return parent::_beforeDelete();
926: }
927:
928: 929: 930: 931: 932:
933: public function reset()
934: {
935: $this->unsetData();
936: $this->_origData = null;
937: $this->_items = null;
938: $this->_comments = null;
939: $this->_order = null;
940: $this->_saveBeforeDestruct = false;
941: $this->_wasPayCalled = false;
942: return $this;
943: }
944:
945: 946: 947: 948: 949:
950: protected function _beforeSave()
951: {
952: parent::_beforeSave();
953:
954: if (!$this->getOrderId() && $this->getOrder()) {
955: $this->setOrderId($this->getOrder()->getId());
956: $this->setBillingAddressId($this->getOrder()->getBillingAddress()->getId());
957: }
958:
959: return $this;
960: }
961:
962: 963: 964: 965: 966:
967: protected function _afterSave()
968: {
969:
970: if (null !== $this->_items) {
971: 972: 973:
974: foreach ($this->_items as $item) {
975: $item->setOrderItem($item->getOrderItem());
976: $item->save();
977: }
978: }
979:
980: if (null !== $this->_comments) {
981: foreach($this->_comments as $comment) {
982: $comment->save();
983: }
984: }
985:
986: return parent::_afterSave();
987: }
988: }
989: