]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - install/seed_data/quotes_SeedData.php
Release 6.5.0
[Github/sugarcrm.git] / install / seed_data / quotes_SeedData.php
1 <?php
2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4  * SugarCRM Community Edition is a customer relationship management program developed by
5  * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
6  * 
7  * This program is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU Affero General Public License version 3 as published by the
9  * Free Software Foundation with the addition of the following permission added
10  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
12  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
13  * 
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
17  * details.
18  * 
19  * You should have received a copy of the GNU Affero General Public License along with
20  * this program; if not, see http://www.gnu.org/licenses or write to the Free
21  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22  * 02110-1301 USA.
23  * 
24  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
25  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
26  * 
27  * The interactive user interfaces in modified source and object code versions
28  * of this program must display Appropriate Legal Notices, as required under
29  * Section 5 of the GNU Affero General Public License version 3.
30  * 
31  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32  * these Appropriate Legal Notices must retain the display of the "Powered by
33  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
34  * technical reasons, the Appropriate Legal Notices must display the words
35  * "Powered by SugarCRM".
36  ********************************************************************************/
37
38
39
40 require_once('modules/Quotes/Quote.php');
41 require_once('modules/ProductBundleNotes/ProductBundleNote.php');
42 require_once('modules/Products/Product.php');
43
44 global $current_user;
45 global $sugar_demodata;
46
47 if(!empty($sugar_demodata['quotes_seed_data']['quotes'])) {
48
49    foreach($sugar_demodata['quotes_seed_data']['quotes'] as $key=>$quote) {
50
51                 $focus = new Quote();
52                 $focus->id = create_guid();
53         $focus->new_with_id = true;
54                 $focus->name = $quote['name'];
55                 $focus->description = !empty($quote['description']) ? $quote['description'] : '';
56                 $focus->quote_stage = !empty($quote['quote_stage']) ? $quote['quote_stage'] : '';
57                 $focus->date_quote_expected_closed = $quote['date_quote_expected_closed'];
58                 if(!empty($quote['purcahse_order_num'])) {
59                    $focus->purchase_order_num = $quote['purcahse_order_num'];
60                 }
61                 
62                 if(!empty($quote['original_po_date'])) {
63                    $focus->original_po_date = $quote['original_po_date'];
64                 }
65
66                 if(!empty($quote['payment_terms'])) {
67                    $focus->payment_terms = $quote['payment_terms'];
68                 }
69                 
70                 $focus->quote_type = 'Quotes';
71                 $focus->calc_grand_total = 1;
72                 $focus->show_line_nums = 1;
73                 $focus->team_id = $current_user->team_id;
74                 $focus->team_set_id = $current_user->team_set_id;
75                 
76                 //Set random account and contact ids
77                 $sql = 'SELECT * FROM accounts WHERE deleted = 0';
78                 $result = $GLOBALS['db']->limitQuery($sql,0,10,true,"Error retrieving Accounts");
79             while ($row = $GLOBALS['db']->fetchByAssoc($result)) {
80                 $focus->billing_account_id = $row['id'];
81                 $focus->name = str_replace('[account name]', $row['name'], $focus->name);
82                 $focus->billing_address_street = $row['billing_address_street'];
83                 $focus->billing_address_city = $row['billing_address_city'];
84                 $focus->billing_address_state = $row['billing_address_state'];
85                 $focus->billing_address_country = $row['billing_address_country'];
86                 $focus->billing_address_postalcode = $row['billing_address_postalcode'];
87                 $focus->shipping_address_street = $row['shipping_address_street'];
88                 $focus->shipping_address_city = $row['shipping_address_city'];
89                 $focus->shipping_address_state = $row['shipping_address_state'];
90                 $focus->shipping_address_country = $row['shipping_address_country'];
91                 $focus->shipping_address_postalcode = $row['shipping_address_postalcode'];
92                 break;
93             }
94
95                 foreach($quote['bundle_data'] as $bundle_key=>$bundle) {
96                         $pb = new ProductBundle();
97                 $pb->team_id = $focus->team_set_id;
98             $pb->team_set_id = $focus->team_set_id;
99             $pb->currency_id = $focus->currency_id;
100                         $pb->bundle_stage = $bundle['bundle_stage'];
101                         $pb->name = $bundle['bundle_name'];
102                         
103             $product_bundle_id = $pb->save();
104             
105             //Save the products
106             foreach($bundle['products'] as $product_key=>$products) {
107                 $sql = 'SELECT * FROM product_templates WHERE name = \'' . $products['name'] . '\'';
108                     $result = $GLOBALS['db']->query($sql);
109                     while ($row = $GLOBALS['db']->fetchByAssoc($result)) {
110                         $product = new Product();
111                         
112                         foreach($product->column_fields as $field) {
113                                                 if(isset($row[$field])) {
114                                $product->$field = $row[$field];
115                                                 }
116                                         }                       
117
118                                         $product->name = $products['name'];
119                                         $product->id = create_guid();
120                                         $product->new_with_id = true;
121                         $product->quantity = $products['quantity'];
122                                         $product->currency_id = $focus->currency_id;
123                                         $product->team_id = $focus->team_id;
124                                         $product->team_set_id = $focus->team_set_id;
125                                         $product->quote_id = $focus->id;
126                                         $product->account_id = $focus->billing_account_id;
127                                         $product->status = 'Quotes';
128                                         
129                                         if ($focus->quote_stage == 'Closed Accepted') {
130                                                 $product->status='Orders';
131                                         }
132
133                                         $pb->subtotal += ($product->list_price * $product->quantity);
134                                         $pb->deal_tot += ($product->list_price * $product->quantity);
135                                         $pb->new_sub += ($product->list_price * $product->quantity);
136                                         $pb->total += ($product->list_price * $product->quantity);
137                                         
138                                         $product_id = $product->save();
139                                         $pb->set_productbundle_product_relationship($product_id, $product_key, $product_bundle_id);
140                                         break;
141                     } //while
142                     
143             } //foreach
144
145             $pb->tax = 0;
146                         $pb->shipping = 0;
147             $pb->save();
148             
149             //Save any product bundle comment
150             if(isset($bundle['comment'])) {
151                                 $product_bundle_note = new ProductBundleNote();
152                                 $product_bundle_note->description = $bundle['comment'];
153                                 $product_bundle_note->save();
154                                 $pb->set_product_bundle_note_relationship($bundle_key, $product_bundle_note->id, $product_bundle_id);
155                 }
156                 
157                 $pb->set_productbundle_quote_relationship($focus->id, $product_bundle_id, $bundle_key);
158                 
159                         $focus->tax += $pb->tax;
160                         $focus->shipping += $pb->shipping;
161                         $focus->subtotal += $pb->subtotal;
162                         $focus->deal_tot += $pb->deal_tot;
163                         $focus->new_sub += $pb->new_sub;
164                         $focus->total += $pb->total;            
165                 
166                 } //foreach
167                 
168                 //Save the quote
169                 $focus->save();
170    } //foreach
171 }
172
173
174 ?>