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: class QuotesController extends x2base {
51:
52: public $modelClass = 'Quote';
53:
54: public function behaviors () {
55: return array_merge (parent::behaviors (), array (
56: 'X2MobileControllerBehavior' => array(
57: 'class' =>
58: 'application.modules.mobile.components.behaviors.'.
59: 'X2MobileQuotesControllerBehavior'
60: ),
61: ));
62: }
63:
64: 65: 66: 67:
68: public function actionView($id){
69: $type = 'quotes';
70: $model = $this->getModel($id);
71: if (!$this->checkPermissions($model, 'view')) $this->denied ();
72:
73: $quoteProducts = $model->lineItems;
74:
75:
76: User::addRecentItem('q', $id, Yii::app()->user->getId());
77:
78: $contactNameId = Fields::nameAndId ($model->associatedContacts);
79: $contactId = $contactNameId[1];
80: parent::view($model, $type, array('orders' => $quoteProducts,
81: 'contactId' => $contactId
82: ));
83: }
84:
85: 86: 87: 88: 89:
90: private function duplicateLineItems(Quote $quote) {
91: $lineItems = array();
92: foreach ($quote->lineItems as $item) {
93: $copy = new QuoteProduct;
94: foreach($item->attributes as $name => $value)
95: if ($name !== 'id' && $name !== 'listId') {
96: $copy->$name = $value;
97: }
98: $lineItems[] = $copy;
99: }
100: return $lineItems;
101: }
102:
103: 104: 105: 106: 107: 108: 109:
110: public function actionCreate($quick=false,$duplicate = false){
111: $model = new Quote;
112: if($duplicate && !isset ($_POST['Quote'])) {
113: $copiedModel = Quote::model()->findByPk($duplicate);
114: if(!empty($copiedModel)) {
115: foreach($copiedModel->attributes as $name => $value)
116: if($name != 'id')
117: $model->$name = $value;
118: $model->setLineItems ($this->duplicateLineItems($copiedModel), false, true);
119: }
120: }
121:
122: $users = User::getNames();
123:
124: if($quick && !Yii::app()->request->isAjaxRequest)
125: throw new CHttpException(400);
126:
127: $currency = Yii::app()->params->currency;
128:
129:
130:
131:
132: if(isset($_POST['Quote'])){
133: $model->setX2Fields($_POST['Quote']);
134: $model->currency = $currency;
135: $model->createDate = time();
136: $model->lastUpdated = $model->createDate;
137: $model->createdBy = Yii::app()->user->name;
138: $model->updatedBy = $model->createdBy;
139: if(empty($model->name))
140: $model->name = '';
141: if(isset($_POST['lineitem']))
142: $model->lineItems = $_POST['lineitem'];
143: if(!$model->hasLineItemErrors){
144: if($model->save()){
145: $model->createEventRecord();
146: $model->createActionRecord();
147: $model->saveLineItems();
148: if(!$quick) {
149: $this->redirect(array('view', 'id' => $model->id));
150: } else {
151: if (isset ($_GET['recordId']) && isset ($_GET['recordType'])) {
152: $recordId = $_GET['recordId'];
153: $recordType = $_GET['recordType'];
154: $relatedModel = X2Model::model ($_GET['recordType'])->findByPk ($recordId);
155: $model->createRelationship($relatedModel);
156: }
157: return;
158: }
159: }
160: }
161: }
162:
163:
164: $products = Product::activeProducts();
165: $viewData = array(
166: 'model' => $model,
167: 'users' => $users,
168: 'products' => $products,
169: 'quick' => $quick,
170:
171: );
172:
173: if(!$quick)
174: $this->render('create', $viewData);
175: else {
176: if($model->hasErrors() || $model->hasLineItemErrors) {
177:
178:
179: header('HTTP/1.1 400 Validation Error');
180: }
181: $this->renderPartial('create', $viewData,false,true);
182: }
183: }
184:
185: 186: 187: 188: 189:
190: public function actionUpdate($id,$quick=0){
191: $model = $this->getModel($id);
192:
193: if(isset($_POST['Quote'])){
194: $model->setX2Fields($_POST['Quote']);
195: if(isset($_POST['lineitem']))
196: $model->lineItems = $_POST['lineitem'];
197:
198: if(!$model->hasLineItemErrors) {
199: if($model->save()) {
200: $model->saveLineItems();
201: $this->redirect(array('view','id' => $model->id));
202: }
203: }
204: }
205: $users=User::getNames();
206: $products = $model->activeProducts();
207: $quoteProducts = $model->lineItems;
208: $viewData = array(
209: 'model' => $model,
210: 'users' => $users,
211: 'products' => $products,
212: 'orders' => $quoteProducts,
213: 'quick'=>$quick,
214: );
215:
216: if(!$quick)
217: $this->render('update', $viewData);
218: else {
219: if($model->hasErrors() || $model->hasLineItemErrors) {
220:
221:
222: header('HTTP/1.1 400 Validation Error');
223: }
224: $this->renderPartial('update', $viewData,false,true);
225: }
226: }
227:
228: 229: 230:
231: public function actionPrint($id,$inline=false) {
232: header('Content-type: text/html; charset=utf-8');
233: if (!$inline) {
234: $this->layout = '//layouts/print';
235: } else {
236: $this->layout = false;
237: }
238: $this->render('printQuote', array(
239: 'id' => $id
240: ));
241: return;
242: }
243:
244: 245: 246: 247: 248: 249: 250: 251:
252: public function getPrintQuote($id = null,$email = false) {
253: $this->throwOnNullModel = false;
254: $model = $this->getModel($id);
255: if($model == null)
256: return Yii::t('quotes','Quote {id} does not exist. It may have been deleted.',array('{id}'=>$id));
257: if (! ($model->templateModel instanceof Docs)) {
258: return $this->renderPartial('print', array(
259: 'model' => $model,
260: 'email' => $email
261: ),true);
262: } else {
263: $template = $model->templateModel;
264: if(!($template instanceof Docs)) {
265:
266:
267: $model->template = null;
268: $model->update(array('template'));
269: return $this->getPrintQuote($model->id);
270: }
271: return Docs::replaceVariables($template->text,$model);
272: }
273: }
274:
275: 276: 277:
278: public function actionIndex() {
279: $model=new Quote('search');
280: $this->render('index', array('model'=>$model));
281: }
282:
283: 284: 285: 286: 287: 288: 289: 290:
291: public function actionIndexInvoice() {
292: $model=new Quote('search');
293: $this->render('indexInvoice', array('model'=>$model));
294: }
295:
296: public function delete($id){
297: $model = $this->getModel($id);
298: $dataProvider = new CActiveDataProvider('Actions', array(
299: 'criteria' => array(
300: 'condition' => 'associationId='.$id.' AND associationType=\'quote\'',
301: )));
302: $actions = $dataProvider->getData();
303: foreach($actions as $action){
304: $action->delete();
305: }
306: $this->cleanUpTags($model);
307: $model->delete();
308: }
309:
310: public function actionDelete($id) {
311: $model=$this->getModel($id);
312: if(Yii::app()->request->isPostRequest) {
313: $this->cleanUpTags($model);
314: $model->delete();
315: } else
316: throw new CHttpException(400,Yii::t('app','Invalid request. Please do not repeat this request again.'));
317:
318: if(!isset($_GET['ajax']))
319: $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('index'));
320: }
321:
322: 323: 324: 325: 326: 327: 328: 329: 330:
331: public function actionConvertToInvoice($id) {
332: $model=$this->getModel($id);
333:
334:
335: $model->type = 'invoice';
336: $model->invoiceCreateDate = time();
337:
338:
339: $field = $model->getField('invoiceStatus');
340: if($field) {
341: $dropDownId = $field->linkType;
342: if($dropDownId) {
343: $dropdowns = Dropdowns::getItems($field->linkType);
344: if($dropdowns) {
345: reset($dropdowns);
346: $status = key($dropdowns);
347: if($status) {
348: $model->invoiceStatus = $status;
349: }
350: }
351: }
352: }
353:
354: $model->update();
355:
356:
357:
358: if(isset ($_GET['modelName']) && isset($_GET['recordId'])) {
359:
360:
361: if($model) {
362: Yii::app()->clientScript->scriptMap['*.js'] = false;
363: $this->renderPartial(
364: 'quoteFormWrapper',
365: array(
366: 'modelId'=>$_GET['recordId'],
367: 'modelName'=>$_GET['modelName']
368: ), false, true);
369: return;
370: }
371: }
372:
373: $this->redirect(array('view','id'=>$model->id));
374: }
375:
376: 377: 378: 379: 380:
381: public function actionViewInline($recordId, $recordType){
382: Yii::app()->clientScript->scriptMap['*.js'] = false;
383: $model = X2Model::model ($recordType)->findByPk ($recordId);
384: $this->renderPartial(
385: 'quoteFormWrapper',
386: array(
387: 'modelId' => $model->id,
388: 'modelName' => $recordType
389: ), false, true
390: );
391: }
392:
393: public function updateQuote($model, $oldAttributes, $products) {
394:
395: $model->lastUpdated = time();
396: $model->updatedBy = Yii::app()->user->name;
397:
398: if($model->save()) {
399:
400: $orders = QuoteProduct::model()->findAllByAttributes(array('quoteId'=>$model->id));
401: foreach($orders as $order) {
402: $found = false;
403: foreach($products as $key=>$product) {
404: if($order->productId == $product['id']) {
405: $order->price = $product['price'];
406: $order->quantity = $product['quantity'];
407: $order->adjustment = $product['adjustment'];
408: $order->adjustmentType = $product['adjustmentType'];
409: $order->save();
410: unset($products[$key]);
411: $found = true;
412: break;
413: }
414: }
415: if(!$found)
416: $order->delete();
417: }
418:
419:
420: foreach($products as $product) {
421: $qp = new QuoteProduct;
422: $qp->quoteId = $model->id;
423: $qp->productId = $product['id'];
424: $qp->name = $product['name'];
425: $qp->price = $product['price'];
426: $qp->quantity = $product['quantity'];
427: $qp->adjustment = $product['adjustment'];
428: $qp->adjustmentType = $product['adjustmentType'];
429: $qp->save();
430: }
431:
432: $this->redirect(array('view','id'=>$model->id));
433: } else {
434: return false;
435: }
436: }
437:
438: public function actionQuickDelete($id) {
439: $model=$this->getModel($id);
440:
441: if($model) {
442: $this->cleanUpTags($model);
443: $model->delete();
444:
445: } else
446: throw new CHttpException(400,Yii::t('app','Invalid request. Please do not repeat this request again.'));
447: }
448:
449:
450: public function actionAddProduct($id) {
451: $model=$this->getModel($id);
452:
453: if(isset($_POST['ExistingProducts'])) {
454:
455: $ids = $_POST['ExistingProducts']['id'];
456: $quantities = $_POST['ExistingProducts']['quantity'];
457: $products = array();
458: foreach($ids as $key=>$id) {
459: if($id != 0) {
460: $products[$key]['id'] = $id;
461: $products[$key]['quantity'] = $quantities[$key];
462: }
463: }
464:
465: foreach($products as $product) {
466: $qp = new QuoteProduct;
467: $qp->quoteId = $model->id;
468: $qp->productId = $product['id'];
469: $qp->quantity = $product['quantity'];
470: $qp->save();
471: }
472:
473: if(isset($_POST['recordId'])) {
474: Yii::app()->clientScript->scriptMap['*.js'] = false;
475: $contact = X2Model::model('Contacts')->findByPk($_POST['recordId']);
476: $this->renderPartial(
477: 'quoteFormWrapper',
478: array(
479: 'recordId'=>$contact->id,'accountName'=>$contact->company
480: ), false, true
481: );
482: }
483: }
484: }
485:
486:
487: public function actionDeleteProduct($id) {
488: $model=$this->getModel($id);
489:
490: if(isset($_GET['productId']))
491: QuoteProduct::model()->deleteAllByAttributes(array('quoteId'=>$id, 'productId'=>$_GET['productId']));
492:
493: if($_GET['contactId']) {
494: Yii::app()->clientScript->scriptMap['*.js'] = false;
495: $contact = X2Model::model('Contacts')->findByPk($_GET['contactId']);
496: $this->renderPartial('quoteFormWrapper', array('contactId'=>$contact->id,'accountName'=>$contact->company), false, true);
497: }
498: }
499:
500: public function actionGetTerms(){
501: $sql = 'SELECT id, name as value FROM x2_accounts WHERE name LIKE :qterm ORDER BY name ASC';
502: $command = Yii::app()->db->createCommand($sql);
503: $qterm = $_GET['term'].'%';
504: $command->bindParam(":qterm", $qterm, PDO::PARAM_STR);
505: $result = $command->queryAll();
506: echo CJSON::encode($result); exit;
507: }
508:
509: public function actionGetItems ($term) {
510: X2LinkableBehavior::getItems ($term);
511: }
512:
513: 514: 515: 516: 517: 518:
519: public function ($selectOptions = array(), $model = null, $menuParams = null) {
520: $Quotes = Modules::displayName();
521: $Quote = Modules::displayName(false);
522: $modelId = isset($model) ? $model->id : 0;
523:
524: 525: 526: 527: 528: 529: 530:
531:
532: $menuItems = array(
533: array(
534: 'name'=>'index',
535: 'label'=>Yii::t('quotes','{module} List', array(
536: '{module}'=>$Quotes
537: )),
538: 'url' => array('index'),
539: ),
540: array(
541: 'name'=>'invoices',
542: 'label'=>Yii::t('quotes','Invoice List'),
543: 'url'=>array('indexInvoice')
544: ),
545: array(
546: 'name'=>'create',
547: 'label'=>Yii::t('quotes','Create'),
548: 'url'=>array('create')
549: ),
550: RecordViewLayoutManager::getViewActionMenuListItem ($modelId),
551: array(
552: 'name'=>'email',
553: 'label'=>Yii::t('app','Email {type}', array(
554: '{type}' => ((isset($model) && $model->type=='invoice') ? 'Invoice' : $Quote)
555: )),
556: 'url'=>'#','linkOptions'=>array('onclick'=>'toggleEmailForm(); return false;'),
557: ),
558: array(
559: 'name'=>'editStrictLock',
560: 'label'=>Yii::t('quotes','Update'),
561: 'url'=>'#',
562: 'linkOptions'=>array('onClick'=>'dialogStrictLock();')
563: ),
564: array(
565: 'name'=>'editLock',
566: 'label'=>Yii::t('quotes','Update'),
567: 'url'=>'#',
568: 'linkOptions'=>array('onClick'=>'dialogLock();')
569: ),
570: array(
571: 'name'=>'edit',
572: 'label'=>Yii::t('quotes','Update'),
573: 'url'=>array('update', 'id'=>$modelId),
574: ),
575: array(
576: 'name'=>'delete',
577: 'label'=>Yii::t('quotes','Delete'),
578: 'url'=>'#',
579: 'linkOptions'=>array(
580: 'submit'=>array('delete','id'=>$modelId),
581: 'confirm'=>'Are you sure you want to delete this item?'
582: ),
583: ),
584: ModelFileUploader::menuLink(),
585: array(
586: 'name'=>'print',
587: 'label'=>((isset($model) && $model->type == 'invoice') ?
588: Yii::t('quotes', 'Print Invoice') : Yii::t('quotes','Print {quote}', array(
589: '{quote}' => $Quote,
590: ))),
591: 'url'=>'#',
592: 'linkOptions'=>array(
593: 'onClick'=>"window.open('".
594: Yii::app()->createUrl('/quotes/quotes/print',
595: array('id'=>$modelId)) ."')"
596: )
597: ),
598: array(
599: 'name'=>'import',
600: 'label'=>Yii::t('quotes', 'Import {module}', array(
601: '{module}' =>$Quotes,
602: )),
603: 'url'=>array('admin/importModels', 'model'=>'Quote'),
604: ),
605: array(
606: 'name'=>'export',
607: 'label'=>Yii::t('quotes', 'Export {module}', array(
608: '{module}' => $Quotes,
609: )),
610: 'url'=>array('admin/exportModels', 'model'=>'Quote'),
611: ),
612: array(
613: 'name' => 'convert',
614: 'label' => Yii::t('quotes', 'Convert To Invoice'),
615: 'url' => array ('convertToInvoice', 'id' => $modelId),
616: ),
617: array(
618: 'name' => 'duplicate',
619: 'label' => Yii::t('quotes', 'Duplicate'),
620: 'url' => array ('create', 'duplicate' => $modelId),
621: ),
622: RecordViewLayoutManager::getEditLayoutActionMenuListItem (),
623: );
624:
625: $this->prepareMenu($menuItems, $selectOptions);
626: $this->actionMenu = $this->formatMenu($menuItems, $menuParams);
627: }
628:
629:
630: }
631: