~technofluid-team/openobject-addons/technofluid_multiple_installations

« back to all changes in this revision

Viewing changes to esale_joomla/esale_joomla.py

  • Committer: pinky
  • Date: 2006-12-07 13:41:40 UTC
  • Revision ID: pinky-dedd7f8a42bd4557112a0513082691b8590ad6cc
New trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
##############################################################################
 
2
#
 
3
# Copyright (c) 2004 TINY SPRL. (http://tiny.be) All Rights Reserved.
 
4
#
 
5
# $Id: sale.py 1005 2005-07-25 08:41:42Z nicoe $
 
6
#
 
7
# WARNING: This program as such is intended to be used by professional
 
8
# programmers who take the whole responsability of assessing all potential
 
9
# consequences resulting from its eventual inadequacies and bugs
 
10
# End users who are looking for a ready-to-use solution with commercial
 
11
# garantees and support are strongly adviced to contract a Free Software
 
12
# Service Company
 
13
#
 
14
# This program is Free Software; you can redistribute it and/or
 
15
# modify it under the terms of the GNU General Public License
 
16
# as published by the Free Software Foundation; either version 2
 
17
# of the License, or (at your option) any later version.
 
18
#
 
19
# This program is distributed in the hope that it will be useful,
 
20
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
21
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
22
# GNU General Public License for more details.
 
23
#
 
24
# You should have received a copy of the GNU General Public License
 
25
# along with this program; if not, write to the Free Software
 
26
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
27
#
 
28
##############################################################################
 
29
 
 
30
import netsvc
 
31
from osv import fields,osv,orm
 
32
import ir
 
33
 
 
34
import time
 
35
import xmlrpclib
 
36
 
 
37
from mx import DateTime
 
38
 
 
39
class esale_joomla_web(osv.osv):
 
40
        _name = "esale_joomla.web"
 
41
        _description = "eCommerce Website"
 
42
        _columns = {
 
43
                'name': fields.char('Name',size=64, required=True),
 
44
                'url': fields.char('URL', size=64, required=True),
 
45
                'shop_id': fields.many2one('sale.shop', 'Sale Shop', required=True),
 
46
                'active': fields.boolean('Active'),
 
47
                'product_ids': fields.one2many('esale_joomla.product', 'web_id', string='Products'),
 
48
                'language_ids': fields.one2many('esale_joomla.lang', 'web_id', string='Languages'),
 
49
                'tax_ids': fields.one2many('esale_joomla.tax', 'web_id', string='Taxes'),
 
50
                'category_ids': fields.one2many('esale_joomla.category', 'web_id', string='Categories'),
 
51
        }
 
52
        _defaults = {
 
53
                'active': lambda *a: 1
 
54
        }
 
55
        def tax_import(self, cr, uid, ids, *args):
 
56
                for website in self.browse(cr, uid, ids):
 
57
                        server = xmlrpclib.ServerProxy("%s/tinyerp-synchro.php" % website.url)
 
58
                        taxes = server.get_taxes()
 
59
                        for tax in taxes:
 
60
                                value={
 
61
                                        'web_id'                : website.id,
 
62
                                        'esale_joomla_id'       : tax[0],
 
63
                                        'name'                  : tax[1]
 
64
                                }
 
65
                                self.pool.get('esale_joomla.tax').create(cr, uid, value)
 
66
                return True
 
67
 
 
68
        def lang_import(self, cr, uid, ids, *args):
 
69
                for website in self.browse(cr, uid, ids):
 
70
                        server = xmlrpclib.ServerProxy("%s/tinyerp-synchro.php" % website.url)
 
71
                        languages = server.get_languages()
 
72
                        for language in languages:
 
73
                                value={
 
74
                                        'web_id': website.id,
 
75
                                        'esale_joomla_id': language[0],
 
76
                                        'name': language[1]
 
77
                                }
 
78
                                self.pool.get('esale_joomla.lang').create(cr, uid, value)
 
79
                return True
 
80
 
 
81
        def category_import(self, cr, uid, ids, *args):
 
82
                for website in self.browse(cr, uid, ids):
 
83
                        server = xmlrpclib.ServerProxy("%s/tinyerp-synchro.php" % website.url)
 
84
                        categories = server.get_categories()
 
85
                        category_pool = self.pool.get('esale_joomla.category')
 
86
                        for category in categories:
 
87
                                value={
 
88
                                        'web_id': website.id,
 
89
                                        'esale_joomla_id': category[0],
 
90
                                        'name': category[1]
 
91
                                }
 
92
                                existing = category_pool.search(cr, uid, [('web_id','=',website.id), ('esale_joomla_id', '=', category[0])])
 
93
                                if len(existing)>0:
 
94
                                        category_pool.write(cr, uid, existing, value)
 
95
                                else:
 
96
                                        category_pool.create(cr, uid, value)
 
97
                return True
 
98
esale_joomla_web()
 
99
 
 
100
class esale_joomla_tax(osv.osv):
 
101
        _name = "esale_joomla.tax"
 
102
        _description = "eSale Tax"
 
103
        _columns = {
 
104
                'name'                  : fields.char('Tax name', size=32, required=True),
 
105
                'esale_joomla_id'       : fields.integer('eSale id'),
 
106
                'tax_id'        : fields.many2one('account.tax', 'Tax'),
 
107
                'web_id'                : fields.many2one('esale_joomla.web', 'Website')
 
108
        }
 
109
esale_joomla_tax()
 
110
 
 
111
class esale_joomla_category(osv.osv):
 
112
        _name = "esale_joomla.category"
 
113
        _description = "eSale Category"
 
114
        _columns = {
 
115
                'name': fields.char('Name', size=64, reuired=True),
 
116
                'esale_joomla_id': fields.integer('Web ID', required=True),
 
117
                'web_id': fields.many2one('esale_joomla.web', 'Website'),
 
118
                'category_id': fields.many2one('product.category', 'Category'),
 
119
                'include_childs': fields.boolean('Include Childs', help="If checked, Tiny ERP will also export products from categories that are childs of this one."),
 
120
        }
 
121
esale_joomla_category()
 
122
 
 
123
class esale_joomla_product(osv.osv):
 
124
        _name = "esale_joomla.product"
 
125
        _description = "eSale Product"
 
126
        _columns = {
 
127
                'web_id'                        : fields.many2one('esale_joomla.web', 'Web Ref'),
 
128
                'name'                          : fields.char('Name', size=64, required=True),
 
129
                'product_id'            : fields.many2one('product.product', 'Product', required=True),
 
130
                'esale_joomla_id'               : fields.integer('eSale product id'),
 
131
                'esale_joomla_tax_id'   : fields.many2one('esale_joomla.tax', 'eSale tax'),
 
132
        }
 
133
 
 
134
        def onchange_product_id(self, cr, uid, ids, product_id, web_id=False):
 
135
                value={}
 
136
                if (product_id):
 
137
                        product=self.pool.get('product.product').browse(cr, uid, product_id)
 
138
                        value['name']=product.name
 
139
                return {'value': value}
 
140
esale_joomla_product()
 
141
 
 
142
class esale_joomla_language(osv.osv):
 
143
        _name = "esale_joomla.lang"
 
144
        _description = "eSale Language"
 
145
        _columns = {
 
146
                'name'                  : fields.char('Name', size=32, required=True),
 
147
                'esale_joomla_id'       : fields.integer('Web ID', required=True),
 
148
                'language_id'   : fields.many2one('res.lang', 'Language'),
 
149
                'web_id'                : fields.many2one('esale_joomla.web', 'Website')
 
150
        }
 
151
esale_joomla_language()
 
152
 
 
153
class esale_joomla_partner(osv.osv):
 
154
        _name='esale_joomla.partner'
 
155
        _description = 'eShop Partner'
 
156
        _columns = {
 
157
                'name': fields.char('Name',size=64, required=True),
 
158
                'esale_id': fields.char('eSale ID', size=64),
 
159
                'address': fields.char('Address',size=128),
 
160
                'city': fields.char('City',size=64),
 
161
                'zip': fields.char('Zip',size=64),
 
162
                'country': fields.char('Country',size=64),
 
163
                'email': fields.char('Mail',size=64),
 
164
                'state': fields.char('State',size=64),
 
165
                'address_id': fields.many2one('res.partner.address', 'Partner Address'),
 
166
        }
 
167
        def address_set(self, cr, uid, ids, context={}):
 
168
                for adr in self.browse(cr, uid, ids, context):
 
169
                        if adr.address_id:
 
170
                                continue
 
171
                        country = self.pool.get('res.country').name_search(cr, uid, adr.country)
 
172
                        state = self.pool.get('res.country.state').name_search(cr, uid, adr.state)
 
173
                        create_id = self.pool.get('res.partner').create(cr, uid, {
 
174
                                'name': adr.name,
 
175
                        })
 
176
                        create_id2 = self.pool.get('res.partner.address').create(cr, uid, {
 
177
                                        'street': adr.address,
 
178
                                        'partner_id': create_id,
 
179
                                        'zip': adr.zip,
 
180
                                        'city': adr.city,
 
181
                                        'email': adr.email,
 
182
                                        'country_id': country and country[0][0] or False,
 
183
                                        'state_id': state and state[0][0] or False,
 
184
                        })
 
185
                        self.write(cr, uid, [adr.id], {'address_id': create_id2} )
 
186
                return True
 
187
esale_joomla_partner()
 
188
 
 
189
class esale_joomla_order(osv.osv):
 
190
        _name='esale_joomla.order'
 
191
        _columns = {
 
192
                'name': fields.char('Order Description',size=64, required=True),
 
193
                'state': fields.selection([
 
194
                        ('draft','Draft'),
 
195
                        ('done','Done'),
 
196
                        ('cancel','Cancel')
 
197
                ], 'Order State'),
 
198
                'date_order':fields.date('Date Ordered', required=True),
 
199
 
 
200
                'epartner_shipping_id':fields.many2one('esale_joomla.partner', 'Joomla Shipping Address', required=True),
 
201
                'epartner_invoice_id':fields.many2one('esale_joomla.partner', 'Joomla Invoice Address', required=True),
 
202
 
 
203
                'partner_id':fields.many2one('res.partner', 'Contact Address'),
 
204
                'partner_shipping_id':fields.many2one('res.partner.address', 'Shipping Address'),
 
205
                'partner_invoice_id':fields.many2one('res.partner.address', 'Invoice Address'),
 
206
 
 
207
                'web_id':fields.many2one('esale_joomla.web', 'Web Shop', required=True),
 
208
                'web_ref':fields.integer('Web Ref'),
 
209
 
 
210
                'order_lines': fields.one2many('esale_joomla.order.line', 'order_id', 'Order Lines'),
 
211
                'order_id': fields.many2one('sale.order', 'Sale Order'),
 
212
                'note': fields.text('Notes'),
 
213
        }
 
214
        _defaults = {
 
215
                'date_order': lambda *a: time.strftime('%Y-%m-%d'),
 
216
                'state': lambda *a: 'draft',
 
217
        }
 
218
 
 
219
        def order_create(self, cr, uid, ids, context={}):
 
220
                for order in self.browse(cr, uid, ids, context):
 
221
                        if not (order.partner_id and order.partner_invoice_id and order.partner_shipping_id):
 
222
                                raise osv.except_osv('No addresses !', 'You must assign addresses before creating the order.')
 
223
                        pricelist_id=order.partner_id.property_product_pricelist[0]
 
224
                        order_lines = []
 
225
                        for line in order.order_lines:
 
226
                                val = {
 
227
                                        'name': line.name,
 
228
                                        'product_uom_qty': line.product_qty,
 
229
                                        'product_id': line.product_id.id,
 
230
                                        'product_uom': line.product_uom_id.id,
 
231
                                        'price_unit': line.price_unit,
 
232
                                }
 
233
                                val_new = self.pool.get('sale.order.line').product_id_change(cr, uid, None, pricelist_id, line.product_id.id, line.product_qty, line.product_uom_id.id, name=line.name)['value']
 
234
                                del val_new['price_unit']
 
235
                                del val_new['weight']
 
236
                                val_new['product_uos'] = val_new['product_uos'][0]
 
237
                                val.update( val_new )
 
238
                                val['tax_id'] = [(6,0,val['tax_id'])]
 
239
                                order_lines.append( (0,0,val) )
 
240
                        order_id = self.pool.get('sale.order').create(cr, uid, {
 
241
                                'name': order.name,
 
242
                                'shop_id': order.web_id.shop_id.id,
 
243
                                'origin': 'WEB:'+str(order.web_ref),
 
244
                                'user_id': uid,
 
245
                                'note': order.note or '',
 
246
                                'partner_id': order.partner_id.id,
 
247
                                'partner_invoice_id':order.partner_invoice_id.id,
 
248
                                'partner_order_id':order.partner_invoice_id.id,
 
249
                                'partner_shipping_id':order.partner_shipping_id.id,
 
250
                                'pricelist_id': pricelist_id,
 
251
                                'order_line': order_lines
 
252
                        })
 
253
                        self.write(cr, uid, [order.id], {'state':'done', 'order_id': order_id})
 
254
                        wf_service = netsvc.LocalService("workflow")
 
255
                        wf_service.trg_validate(uid, 'sale.order', order_id, 'order_confirm', cr)
 
256
                return True
 
257
 
 
258
        def address_set(self, cr, uid, ids, *args):
 
259
                done = []
 
260
                for order in self.browse(cr, uid, ids):
 
261
                        for a in [order.epartner_shipping_id.id,order.epartner_invoice_id.id]:
 
262
                                if a not in done:
 
263
                                        done.append(a)
 
264
                                        self.pool.get('esale_joomla.partner').address_set(cr, uid, [a] )
 
265
                        self.write(cr, uid, [order.id], {
 
266
                                'partner_shipping_id': order.epartner_invoice_id.address_id.id,
 
267
                                'partner_id': order.epartner_invoice_id.address_id.partner_id.id,
 
268
                                'partner_invoice_id': order.epartner_shipping_id.address_id.id,
 
269
                        })
 
270
                return True
 
271
 
 
272
        def order_cancel(self, cr, uid, ids, context={}):
 
273
                self.write(cr, uid, ids, {'state':'cancel'})
 
274
                return True
 
275
esale_joomla_order()
 
276
 
 
277
class esale_joomla_order_line(osv.osv):
 
278
        _name = 'esale_joomla.order.line'
 
279
        _description = 'eSale Order line'
 
280
        _columns = {
 
281
                'name': fields.char('Order Line', size=64, required=True),
 
282
                'order_id': fields.many2one('esale_joomla.order', 'eOrder Ref'),
 
283
                'product_qty': fields.float('Quantity', digits=(16,2), required=True),
 
284
                'product_id': fields.many2one('product.product', 'Product', domain=[('sale_ok','=',True)], change_default=True),
 
285
                'product_uom_id': fields.many2one('product.uom', 'Unit of Measure', required=True),
 
286
                'price_unit': fields.float('Unit Price', required=True),
 
287
        }
 
288
        _defaults = {
 
289
        }
 
290
esale_joomla_order_line()
 
291
 
 
292