-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathsales.py
484 lines (390 loc) · 13.4 KB
/
sales.py
1
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
# -*- coding: UTF-8 -*-
'''
magento.sales
Allows to export/import sales orders from/into Magento,
to create invoices, shipments, credit memos
:license: BSD, see LICENSE for more details
'''
from .api import API
class Order(API):
"""
Allows to import/export orders.
"""
__slots__ = ()
def list(self, filters=None):
"""
Retrieve order list by filters
:param filters: Dictionary of filters.
Format :
`{<attribute>:{<operator>:<value>}}`
Example :
`{'firstname':{'ilike':'sharoon'}}`
:return: `list` of `dict`
"""
return self.call('sales_order.list', [filters])
def search(self, filters=None, fields=None, limit=None, page=1):
"""
Retrieve order list by options using search api. Using this result can
be paginated
:param options: Dictionary of options.
:param filters: `{<attribute>:{<operator>:<value>}}`
:param fields: [<String: magento field names>, ...]
:param limit: `page limit`
:param page: `current page`
:return: `list` of `dict`
"""
options = {
'imported': False,
'filters': filters or {},
'fields': fields or [],
'limit': limit or 1000,
'page': page,
}
return self.call('sales_order.search', [options])
def info(self, order_increment_id):
"""
Retrieve order info
:param order_increment_id: Order ID
"""
return self.call(
'sales_order.info', [order_increment_id]
)
def info_multi(self, order_ids):
"""
This is multicall version of 'order.info'
"""
return self.multiCall([
[
'sales_order.info', [order_id]
]
for order_id in order_ids
])
def addcomment(self, order_increment_id,
status, comment=None, notify=False):
"""
Add comment to order or change its state
:param order_increment_id: Order ID
TODO: Identify possible values for status
"""
if comment is None:
comment = ""
return bool(self.call(
'sales_order.addComment',
[order_increment_id, status, comment, notify]
)
)
#: A proxy for :meth:`addcomment`
addComment = addcomment
def hold(self, order_increment_id):
"""
Hold order
:param order_increment_id: Order ID
"""
return bool(self.call('sales_order.hold', [order_increment_id]))
def unhold(self, order_increment_id):
"""
Unhold order
:param order_increment_id: Order ID
"""
return bool(self.call('sales_order.unhold', [order_increment_id]))
def cancel(self, order_increment_id):
"""
Cancel an order
:param order_increment_id: Order ID
"""
return bool(self.call('sales_order.cancel', [order_increment_id]))
class CreditMemo(API):
"""
Allows create/export order credit memos.
"""
__slots__ = ()
def list(self, filters=None):
"""
Retrieve credit memo list by filters
:param filters: Dictionary of filters.
Format :
`{<attribute>:{<operator>:<value>}}`
Example :
`{'firstname':{'ilike':'sharoon'}}`
:return: `list` of `dict`
"""
return self.call('sales_order_creditmemo.list', [filters])
def info(self, creditmemo_increment_id):
"""
Retrieve credit memo info
:param creditmemo_increment_id: Credit memo increment ID
:return dict, credit memo data
"""
return self.call('sales_order_creditmemo.info', [creditmemo_increment_id])
def create(
self,
order_increment_id,
creditmemo_data=None,
comment=None,
email=False,
include_comment=False,
refund_to_store_credit_amount=None):
"""
Create new credit_memo for order
:param order_increment_id: Order Increment ID
:type order_increment_id: str
:param creditmemo_data: Sales order credit memo data (optional)
:type creditmemo_data: associative array as dict
{
'qtys': [
{
'order_item_id': str, # Order item ID to be refunded
'qty': int # Items quantity to be refunded
},
...
],
'shipping_amount': float # refund shipping amount (optional)
'adjustment_positive': float # adjustment refund amount (optional)
'adjustment_negative': float # adjustment fee amount (optional)
}
:param comment: Credit memo Comment
:type comment: str
:param email: send e-mail flag (optional)
:type email: bool
:param include_comment: include comment in e-mail flag (optional)
:type include_comment: bool
:param refund_to_store_credit_amount: amount to refund to store credit
:type refund_to_store_credit_amount: float
:return str, increment id of credit memo created
"""
if comment is None:
comment = ''
return self.call(
'sales_order_creditmemo.create', [
order_increment_id, creditmemo_data, comment, email, include_comment, refund_to_store_credit_amount
]
)
def addcomment(self, creditmemo_increment_id,
comment, email=True, include_in_email=False):
"""
Add new comment to credit memo
:param creditmemo_increment_id: Credit memo increment ID
:return: bool
"""
return bool(
self.call(
'sales_order_creditmemo.addComment',
[creditmemo_increment_id, comment, email, include_in_email]
)
)
#: A proxy for :meth:`addcomment`
addComment = addcomment
def cancel(self, creditmemo_increment_id):
"""
Cancel credit memo
:param creditmemo_increment_id: Credit memo ID
:return: bool
"""
return bool(
self.call('sales_order_creditmemo.cancel', [creditmemo_increment_id])
)
class Shipment(API):
"""
Allows create/export order shipments.
"""
__slots__ = ()
def list(self, filters=None):
"""
Retrieve shipment list by filters
:param filters: Dictionary of filters.
Format :
`{<attribute>:{<operator>:<value>}}`
Example :
`{'firstname':{'ilike':'sharoon'}}`
:return: `list` of `dict`
"""
return self.call('sales_order_shipment.list', [filters])
def info(self, shipment_increment_id):
"""
Retrieve shipment info
:param shipment_increment_id: Order ID
"""
return self.call('sales_order_shipment.info', [shipment_increment_id])
def create(self, order_increment_id,
items_qty, comment=None, email=True, include_comment=False):
"""
Create new shipment for order
:param order_increment_id: Order Increment ID
:type order_increment_id: str
:param items_qty: items qty to ship
:type items_qty: associative array (order_item_id ⇒ qty) as dict
:param comment: Shipment Comment
:type comment: str
:param email: send e-mail flag (optional)
:type email: bool
:param include_comment: include comment in e-mail flag (optional)
:type include_comment: bool
"""
if comment is None:
comment = ''
return self.call(
'sales_order_shipment.create', [
order_increment_id, items_qty, comment, email, include_comment
]
)
def addcomment(self, shipment_increment_id,
comment, email=True, include_in_email=False):
"""
Add new comment to shipment
:param shipment_increment_id: Shipment ID
"""
return bool(
self.call(
'sales_order_shipment.addComment',
[shipment_increment_id, comment, email, include_in_email]
)
)
#: A proxy for :meth:`addcomment`
addComment = addcomment
def addtrack(self, shipment_increment_id, carrier, title, track_number):
"""
Add new tracking number
:param shipment_increment_id: Shipment ID
:param carrier: Carrier Code
:param title: Tracking title
:param track_number: Tracking Number
"""
return self.call(
'sales_order_shipment.addTrack',
[shipment_increment_id, carrier, title, track_number]
)
#: A proxy for :meth:`addtrack`
addTrack = addtrack
def removetrack(self, shipment_increment_id, track_id):
"""
Remove tracking number
:param shipment_increment_id: SHipment ID
:param track_id: Tracking number to remove
"""
return bool(
self.call(
'sales_order_shipment.removeTrack',
[shipment_increment_id, track_id]
)
)
#: A proxy for :meth:`removetrack`
removeTrack = removetrack
def getcarriers(self, order_increment_id):
"""
Retrieve list of allowed carriers for order
:param order_increment_id: Order ID
"""
return self.call(
'sales_order_shipment.getCarriers', [order_increment_id]
)
#: A proxy for :meth:`getcarriers`
getCarriers = getcarriers
def sendinfo(self, shipment_increment_id, comment=''):
"""
Send email with shipment data to customer
:param order_increment_id: Order ID
"""
return self.call(
'sales_order_shipment.sendInfo', [shipment_increment_id, comment]
)
#: A proxy for :meth:`sendinfo`
sendInfo = sendinfo
class Invoice(API):
"""
Allows create/export order invoices
"""
__slots__ = ()
def list(self, filters=None):
"""
Retrieve invoice list by filters
:param filters: Dictionary of filters.
Format :
`{<attribute>:{<operator>:<value>}}`
Example :
`{'firstname':{'ilike':'sharoon'}}`
:return: `list` of `dict`
"""
return self.call('sales_order_invoice.list', [filters])
def info(self, invoice_increment_id):
"""
Retrieve invoice info
:param invoice_increment_id: Invoice ID
"""
return self.call(
'sales_order_invoice.info', [invoice_increment_id]
)
def create(self, order_increment_id, items_qty,
comment=None, email=True, include_comment=False):
"""
Create new invoice for order
:param order_increment_id: Order increment ID
:type order_increment_id: str
:param items_qty: Items quantity to invoice
:type items_qty: dict
:param comment: Invoice Comment
:type comment: str
:param email: send invoice on e-mail
:type email: bool
:param include_comment: Include comments in email
:type include_comment: bool
:rtype: str
"""
return self.call(
'sales_order_invoice.create',
[order_increment_id, items_qty, comment, email, include_comment]
)
def addcomment(self, invoice_increment_id,
comment=None, email=False, include_comment=False):
"""
Add comment to invoice or change its state
:param invoice_increment_id: Invoice ID
"""
if comment is None:
comment = ""
return bool(
self.call(
'sales_order_invoice.addComment',
[invoice_increment_id, comment, email, include_comment]
)
)
#: Add a proxy for :meth:`addcomment`
addComment = addcomment
def capture(self, invoice_increment_id):
"""
Capture Invoice
:attention: You should check the invoice to see if can be
captured before attempting to capture an invoice, otherwise
the API call with generate an error.
Invoices have states as defined in the model
Mage_Sales_Model_Order_Invoice:
STATE_OPEN = 1
STATE_PAID = 2
STATE_CANCELED = 3
Also note there is a method call in the model that checks this
for you canCapture(), and it also verifies that the payment is
able to be captured, so the invoice state might not be the only
condition that’s required to allow it to be captured.
:param invoice_increment_id: Invoice ID
:rtype: bool
"""
return bool(
self.call('sales_order_invoice.capture', [invoice_increment_id])
)
def void(self, invoice_increment_id):
"""
Void an invoice
:param invoice_increment_id: Invoice ID
:rtype: bool
"""
return bool(
self.call('sales_order_invoice.void', [invoice_increment_id])
)
def cancel(self, invoice_increment_id):
"""
Cancel invoice
:param invoice_increment_id: Invoice ID
:rtype: bool
"""
return bool(
self.call('sales_order_invoice.cancel', [invoice_increment_id])
)