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: class InlineQuotes extends X2Widget {
47:
48: public $recordId;
49: private $_contactId;
50: public $contact;
51: public $account = null;
52: public $modelName;
53:
54: public $errors = array();
55: public $startHidden = false;
56:
57: public function init() {
58: $quotesAssetsUrl = $this->module->assetsUrl;
59:
60: if(isset($_POST))
61: $startHidden = false;
62:
63: if($this->startHidden) {
64: if($this->startHidden)
65: Yii::app()->clientScript->registerScript(
66: 'startQuotesHidden',"$('#quotes-form').hide();" ,CClientScript::POS_READY);
67:
68:
69: Yii::app()->clientScript->registerScriptFile(
70: $quotesAssetsUrl.'/js/inlineQuotes.js', CClientScript::POS_HEAD);
71: Yii::app()->clientScript->registerScriptFile(
72: $quotesAssetsUrl.'/js/LineItems.js', CClientScript::POS_HEAD);
73:
74: Yii::app()->clientScript->registerCssFiles('InlineQuotesCss', array (
75: $quotesAssetsUrl.'/css/inlineQuotes.css',
76: $quotesAssetsUrl.'/css/lineItemsMain.css',
77: $quotesAssetsUrl.'/css/lineItemsWrite.css',
78: ), false);
79: Yii::app()->clientScript->registerCoreScript('jquery.ui');
80:
81: $this->contact = X2Model::model ('Contacts')->findByPk ($this->contactId);
82:
83:
84: $iqConfig = array(
85: 'contact' => ($this->contact instanceof Contacts) ?
86: CHtml::encode($this->contact->name) : '',
87: 'account' => $this->account,
88: 'sendingQuote' => false,
89: 'lockedMessage' => Yii::t(
90: 'quotes','This quote is locked. Are you sure you want to update this quote?'),
91: 'deniedMessage' => Yii::t('quotes','This quote is locked.'),
92: 'lockedDialogTitle' => Yii::t('quotes','Locked'),
93: 'failMessage' => Yii::t('quotes', 'Could not save quote.'),
94: 'reloadAction' => CHtml::normalizeUrl(
95: array(
96: '/quotes/quotes/viewInline',
97: 'recordId' => $this->recordId,
98: 'recordType' => CHtml::encode($this->modelName),
99: )
100: ),
101: 'createAction' => CHtml::normalizeUrl(
102: array(
103: '/quotes/quotes/create',
104: 'quick' => 1,
105: 'recordId' => $this->recordId,
106: 'recordType' => $this->modelName,
107: )
108: ),
109: 'updateAction' => CHtml::normalizeUrl(
110: array(
111: '/quotes/quotes/update',
112: 'quick' => 1,
113: )
114: ),
115: );
116: Yii::app()->clientScript->registerScript('quickquote-vars', '
117: ;(function () {
118: if(typeof x2 == "undefined"){
119: x2 = {};
120: }
121: var iqConfig = '.CJSON::encode($iqConfig).';
122: if(typeof x2.inlineQuotes=="undefined") {
123: x2.inlineQuotes = iqConfig;
124: } else {
125: $.extend(x2.inlineQuotes,iqConfig);
126: }
127: }) ();', CClientScript::POS_HEAD);
128: }
129: parent::init();
130: }
131:
132: 133: 134: 135:
136: public function getContactId() {
137: return $this->_contactId;
138: }
139:
140: public function setContactId($value) {
141: $this->_contactId = $value;
142: $this->recordId = $value;
143: }
144:
145: 146: 147: 148: 149:
150: public function getRelatedQuotes ($invoices=false) {
151:
152: if ($invoices) {
153: $invoiceCondition = "type='invoice'";
154: } else {
155: $invoiceCondition = "type IS NULL OR type!='invoice'";
156: }
157:
158: 159: 160:
161: $quotes = Yii::app()->db->createCommand ()
162: ->select ('quotes.id')
163: ->from ('x2_quotes as quotes, x2_relationships as relationships')
164: ->where ("(".$invoiceCondition.") AND ".
165: "((relationships.firstType='Quote' AND ".
166: "relationships.secondType=:recordType AND relationships.secondId=:recordId) OR ".
167: "(relationships.secondType='Quote' AND ".
168: "relationships.firstType=:recordType AND relationships.firstId=:recordId)) AND ".
169: '((quotes.id=relationships.firstId AND relationships.firstType="Quote") OR '.
170: '(quotes.id=relationships.secondId AND relationships.secondType="Quote"))',
171: array (
172: ':recordId' => $this->recordId,
173: ':recordType' => $this->modelName
174: ))
175: ->queryAll ();
176:
177:
178: $getId = function ($a) { return $a['id']; };
179: $quotes = X2Model::model('Quote')->findAllByPk (array_map ($getId, $quotes));
180:
181: return $quotes;
182: }
183:
184: public function run() {
185:
186: $canDo = array();
187: foreach(array('QuickDelete','QuickUpdate','QuickCreate') as $subAction) {
188: $canDo[$subAction] = Yii::app()->user->checkAccess('Quotes'.$subAction);
189: }
190:
191: 192: 193: 194: 195:
196:
197: echo
198: '<div id="quotes-form" style="display: none;">
199: <div id="wide-quote-form" class="wide x2-layout-island"
200: style="overflow: visible;">
201: <a class="widget-close-button x2-icon-button" href="#">
202: <span class="fa fa-times fa-lg"
203: title="'.CHtml::encode (Yii::t('app', 'Close Widget')).'">
204: </span>
205: </a>
206: <div id="quote-create-form-wrapper" style="display:none"></div>
207: <span class="quotes-section-title"
208: style="font-weight:bold; font-size: 1.5em;">'.
209: CHtml::encode (Yii::t('quotes','Quotes')) .
210: '</span>
211: <br /><br />';
212:
213:
214: $model = new Quote;
215:
216: if($canDo['QuickCreate']){
217: $this->render('createQuote');
218: echo '<br /><hr />';
219: }
220:
221: $quotes = $this->getRelatedQuotes ();
222:
223: foreach($quotes as $quote) {
224: $this->render('viewQuotes', array(
225: 'quote'=>$quote,
226: 'recordId'=>$this->recordId,
227: 'modelName'=>$this->modelName,
228: 'canDo' => $canDo
229: ));
230: }
231:
232:
233: echo '<br /><br />
234: <span class="quotes-section-title"
235: style="font-weight:bold; font-size: 1.5em;">'.
236: Yii::t('quotes','Invoices').'</span>
237: <br /><br />';
238:
239: $quotes = $this->getRelatedQuotes (true);
240:
241: foreach($quotes as $quote) {
242: $this->render('viewQuotes', array(
243: 'quote'=>$quote,
244: 'recordId'=>$this->recordId,
245: 'modelName'=>$this->modelName,
246: 'canDo' => $canDo,
247: ));
248: }
249:
250:
251: echo "</div>";
252: echo "</div>";
253: }
254: }
255: