-
Notifications
You must be signed in to change notification settings - Fork 108
/
views.py
820 lines (698 loc) · 30.9 KB
/
views.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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
# -*- Mode: Python; coding: iso-8859-1 -*-
# vi:si:et:sw=4:sts=4:ts=4
##
## Copyright (C) 2007 Async Open Source <http://www.async.com.br>
## All rights reserved
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., or visit: http://www.gnu.org/.
##
## Author(s): Stoq Team <stoq-devel@async.com.br>
##
from stoqlib.database.orm import const, AND, INNERJOINOn, LEFTJOINOn, OR
from stoqlib.database.orm import Viewable, Field, Alias
from stoqlib.database.runtime import get_connection
from stoqlib.domain.commission import CommissionSource
from stoqlib.domain.loan import Loan, LoanItem
from stoqlib.domain.person import (Person, PersonAdaptToSupplier,
PersonAdaptToUser, PersonAdaptToBranch,
PersonAdaptToClient)
from stoqlib.domain.product import (Product,
ProductAdaptToStorable,
ProductStockItem,
ProductHistory,
ProductComponent)
from stoqlib.domain.production import ProductionOrder, ProductionItem
from stoqlib.domain.purchase import (Quotation, QuoteGroup, PurchaseOrder,
PurchaseItem)
from stoqlib.domain.receiving import ReceivingOrderItem, ReceivingOrder
from stoqlib.domain.sale import SaleItem, Sale
from stoqlib.domain.sellable import (Sellable, SellableUnit,
BaseSellableInfo, SellableCategory,
SellableTaxConstant)
class ProductFullStockView(Viewable):
"""Stores information about products.
This view is used to query stock information on a certain branch.
@cvar id: the id of the asellable table
@cvar barcode: the sellable barcode
@cvar status: the sellable status
@cvar cost: the sellable cost
@cvar price: the sellable price
@cvar description: the sellable description
@cvar unit: the unit of the product
@cvar product_id: the id of the product table
@cvar location: the location of the product
@cvar branch_id: the id of person_adapt_to_branch table
@cvar stock: the stock of the product
"""
columns = dict(
id=Sellable.q.id,
code=Sellable.q.code,
barcode=Sellable.q.barcode,
status=Sellable.q.status,
cost=Sellable.q.cost,
description=BaseSellableInfo.q.description,
product_id=Product.q.id,
location=Product.q.location,
tax_description=SellableTaxConstant.q.description,
category_description=SellableCategory.q.description,
total_stock_cost = const.SUM(
ProductStockItem.q.stock_cost*ProductStockItem.q.quantity),
stock=const.SUM(ProductStockItem.q.quantity +
ProductStockItem.q.logic_quantity),
)
joins = [
INNERJOINOn(None, BaseSellableInfo,
BaseSellableInfo.q.id == Sellable.q.base_sellable_infoID),
# Tax Constant
LEFTJOINOn(None, SellableTaxConstant,
SellableTaxConstant.q.id == Sellable.q.tax_constantID),
# Category
LEFTJOINOn(None, SellableCategory,
SellableCategory.q.id == Sellable.q.categoryID),
# Product
INNERJOINOn(None, Product,
Product.q.sellableID == Sellable.q.id),
# Product Stock Item
LEFTJOINOn(None, ProductAdaptToStorable,
ProductAdaptToStorable.q._originalID == Product.q.id),
LEFTJOINOn(None, ProductStockItem,
ProductStockItem.q.storableID ==
ProductAdaptToStorable.q.id),
]
clause = Sellable.q.status != Sellable.STATUS_CLOSED
@classmethod
def select_by_branch(cls, query, branch, having=None, connection=None):
if branch:
# Also show products that were never purchased.
branch_query = OR(ProductStockItem.q.branchID == branch.id,
ProductStockItem.q.branchID == None)
if query:
query = AND(query, branch_query)
else:
query = branch_query
return cls.select(query, having=having, connection=connection)
def get_unit_description(self):
unit = self.product.sellable.get_unit_description()
if unit == u"":
return u"un"
return unit
def get_product_and_category_description(self):
"""Returns the product and the category description in one string.
The category description will be formatted inside square
brackets, if any. Otherwise, only the product description will
be returned.
"""
category_description = ''
if self.category_description:
category_description += '[' + self.category_description + '] '
return category_description + self.description
@property
def sellable(self):
return Sellable.get(self.id, connection=self.get_connection())
@property
def product(self):
return Product.get(self.product_id, connection=self.get_connection())
@property
def stock_cost(self):
if self.stock:
return self.total_stock_cost / self.stock
return 0
@property
def price(self):
# This property is needed because the price value in the view column
# might not be the price used (check OnSaleInfo class).
sellable = Sellable.get(self.id, connection=self.get_connection())
return sellable.price
class ProductFullWithClosedStockView(ProductFullStockView):
"""Stores information about products, showing the closed ones too.
"""
clause = None
class ProductClosedStockView(ProductFullWithClosedStockView):
"""Stores information about products that were closed.
"""
clause = Sellable.q.status == Sellable.STATUS_CLOSED
class ProductComponentView(ProductFullStockView):
columns = ProductFullStockView.columns
clause = AND(ProductFullStockView.clause,
ProductComponent.q.productID == Product.q.id,)
@property
def sellable(self):
return Sellable.get(self.id, connection=self.get_connection())
class ProductWithStockView(ProductFullStockView):
"""Stores information about products, since product has a purchase or sale.
This view is used to query stock information on a certain branch.
@cvar id: the id of the asellable table
@cvar barcode: the sellable barcode
@cvar status: the sellable status
@cvar cost: the sellable cost
@cvar price: the sellable price
@cvar description: the sellable description
@cvar unit: the unit of the product
@cvar product_id: the id of the product table
@cvar branch_id: the id of person_adapt_to_branch table
@cvar stock: the stock of the product
"""
columns = ProductFullStockView.columns
clause = AND(
ProductFullStockView.clause,
ProductStockItem.q.quantity >= 0,
ProductStockItem.q.logic_quantity >= 0,
)
ProductFullStockView.joins
class _PurchaseItemTotal(Viewable):
columns = dict(
id=PurchaseItem.q.sellableID,
purchase_id=PurchaseOrder.q.id,
to_receive=const.SUM(PurchaseItem.q.quantity-
PurchaseItem.q.quantity_received)
)
joins = [
LEFTJOINOn(None, PurchaseOrder,
PurchaseOrder.q.id == PurchaseItem.q.orderID)]
clause = PurchaseOrder.q.status == PurchaseOrder.ORDER_CONFIRMED
class ProductFullStockItemView(ProductFullStockView):
# ProductFullStockView already joins with a 1 to Many table (Sellable
# with ProductStockItem).
#
# This is why we must join PurchaseItem (another 1 to many table) in a
# subquery
_purchase_total = "(%s) AS _purchase_total" % str(
_PurchaseItemTotal.select(connection=get_connection()))
columns = ProductFullStockView.columns.copy()
columns.update(dict(
minimum_quantity=ProductAdaptToStorable.q.minimum_quantity,
maximum_quantity=ProductAdaptToStorable.q.maximum_quantity,
to_receive_quantity=Field('_purchase_total', 'to_receive'),
difference=(const.SUM(
ProductStockItem.q.quantity + ProductStockItem.q.logic_quantity) -
ProductAdaptToStorable.q.minimum_quantity)))
joins = ProductFullStockView.joins[:]
joins.append(LEFTJOINOn(None, _purchase_total,
Field('_purchase_total', 'id') == Sellable.q.id))
class ProductQuantityView(Viewable):
"""Stores information about products solded and received.
@cvar id: the id of the sellable_id of products_quantity table
@cvar description: the product description
@cvar branch_id: the id of person_adapt_to_branch table
@cvar quantity_sold: the quantity solded of product
@cvar quantity_transfered: the quantity transfered of product
@cvar quantity_received: the quantity received of product
@cvar branch: the id of the branch_id of producst_quantity table
@cvar date_sale: the date of product's sale
@cvar date_received: the date of product's received
"""
columns = dict(
id=ProductHistory.q.sellableID,
code=Sellable.q.code,
description=BaseSellableInfo.q.description,
branch=ProductHistory.q.branchID,
sold_date=ProductHistory.q.sold_date,
received_date=ProductHistory.q.received_date,
production_date=ProductHistory.q.production_date,
decreased_date=ProductHistory.q.decreased_date,
quantity_sold=const.SUM(ProductHistory.q.quantity_sold),
quantity_received=const.SUM(ProductHistory.q.quantity_received),
quantity_transfered=const.SUM(ProductHistory.q.quantity_transfered),
quantity_produced=const.SUM(ProductHistory.q.quantity_produced),
quantity_consumed=const.SUM(ProductHistory.q.quantity_consumed),
quantity_lost=const.SUM(ProductHistory.q.quantity_lost),
quantity_decreased=const.SUM(ProductHistory.q.quantity_decreased),
)
hidden_columns = ['sold_date', 'received_date', 'production_date',
'decreased_date']
joins = [
INNERJOINOn(None, Sellable,
ProductHistory.q.sellableID == Sellable.q.id),
INNERJOINOn(None, BaseSellableInfo,
Sellable.q.base_sellable_infoID == BaseSellableInfo.q.id),
]
class SellableFullStockView(Viewable):
"""Stores information about products.
This view is used to query stock information on a certain branch.
@cvar id: the id of the asellable table
@cvar barcode: the sellable barcode
@cvar status: the sellable status
@cvar cost: the sellable cost
@cvar price: the sellable price
@cvar description: the sellable description
@cvar unit: the unit of the product or None
@cvar product_id: the id of the product table or None
@cvar branch_id: the id of person_adapt_to_branch table or None
@cvar stock: the stock of the product or None
"""
columns = dict(
id=Sellable.q.id,
code=Sellable.q.code,
barcode=Sellable.q.barcode,
status=Sellable.q.status,
cost=Sellable.q.cost,
description=BaseSellableInfo.q.description,
unit=SellableUnit.q.description,
product_id=Product.q.id,
category_description=SellableCategory.q.description,
stock=const.SUM(ProductStockItem.q.quantity +
ProductStockItem.q.logic_quantity),
)
joins = [
# Sellable unit
LEFTJOINOn(None, SellableUnit,
SellableUnit.q.id == Sellable.q.unitID),
# Category
LEFTJOINOn(None, SellableCategory,
SellableCategory.q.id == Sellable.q.categoryID),
# Product
LEFTJOINOn(None, Product,
Product.q.sellableID == Sellable.q.id),
# Product Stock Item
LEFTJOINOn(None, ProductAdaptToStorable,
ProductAdaptToStorable.q._originalID == Product.q.id),
LEFTJOINOn(None, ProductStockItem,
ProductStockItem.q.storableID ==
ProductAdaptToStorable.q.id),
]
clause = AND(
BaseSellableInfo.q.id == Sellable.q.base_sellable_infoID,
)
@classmethod
def select_by_branch(cls, query, branch, having=None, connection=None):
if branch:
# We need the OR part to be able to list services
branch_query = OR(ProductStockItem.q.branchID == branch.id,
ProductStockItem.q.branchID == None)
if query:
query = AND(query, branch_query)
else:
query = branch_query
return cls.select(query, having=having, connection=connection)
@property
def sellable(self):
return Sellable.get(self.id, connection=self.get_connection())
@property
def price(self):
# This property is needed because the price value in the view column
# might not be the price used (check OnSaleInfo class).
sellable = Sellable.get(self.id, connection=self.get_connection())
return sellable.price
class SellableCategoryView(Viewable):
"""Stores information about categories.
This view is used to query the category with the related
commission source.
"""
columns = dict(
id=SellableCategory.q.id,
commission=CommissionSource.q.direct_value,
installments_commission=CommissionSource.q.installments_value,
category_id=SellableCategory.q.id,
description=SellableCategory.q.description,
suggested_markup=SellableCategory.q.suggested_markup,
)
joins = [
# commission source
LEFTJOINOn(None, CommissionSource,
CommissionSource.q.categoryID ==
SellableCategory.q.id),
]
@property
def category(self):
return SellableCategory.get(self.category_id,
connection=self.get_connection())
def get_commission(self):
if self.commission:
return self.commission
source = self._get_base_source_commission()
if source:
return source.direct_value
def get_installments_commission(self):
if self.commission:
return self.installments_commission
source = self._get_base_source_commission()
if source:
return source.installments_value
def _get_base_source_commission(self):
base_category = self.category.category
if not base_category:
return
return CommissionSource.selectOneBy(category=base_category,
connection=self.get_connection())
class QuotationView(Viewable):
"""Stores information about the quote group and its quotes.
"""
columns = dict(
id=Quotation.q.id,
purchase_id=Quotation.q.purchaseID,
group_id=Quotation.q.groupID,
group_code=Quotation.q.groupID,
open_date=PurchaseOrder.q.open_date,
deadline=PurchaseOrder.q.quote_deadline,
supplier_name=Person.q.name,
)
joins = [
LEFTJOINOn(None, PurchaseOrder,
PurchaseOrder.q.id == Quotation.q.purchaseID),
LEFTJOINOn(None, PersonAdaptToSupplier,
PersonAdaptToSupplier.q.id == PurchaseOrder.q.supplierID),
LEFTJOINOn(None, Person, Person.q.id ==
PersonAdaptToSupplier.q._originalID),
]
clause = QuoteGroup.q.id == Quotation.q.groupID
@property
def group(self):
return QuoteGroup.get(self.group_id, connection=self.get_connection())
@property
def quotation(self):
return Quotation.get(self.id, connection=self.get_connection())
@property
def purchase(self):
return PurchaseOrder.get(self.purchase_id,
connection=self.get_connection())
class SoldItemView(Viewable):
"""Stores information about all sale items, including the average cost
of the sold items.
"""
columns = dict(
id=Sellable.q.id,
code=Sellable.q.code,
description=BaseSellableInfo.q.description,
quantity=const.SUM(SaleItem.q.quantity),
total_cost=const.SUM(SaleItem.q.quantity * SaleItem.q.average_cost),
)
joins = [
LEFTJOINOn(None, SaleItem,
Sellable.q.id == SaleItem.q.sellableID),
LEFTJOINOn(None, Sale,
SaleItem.q.saleID == Sale.q.id),
INNERJOINOn(None, BaseSellableInfo,
Sellable.q.base_sellable_infoID == BaseSellableInfo.q.id),
]
clause = OR(Sale.q.status == Sale.STATUS_CONFIRMED,
Sale.q.status == Sale.STATUS_PAID,
Sale.q.status == Sale.STATUS_ORDERED,)
@classmethod
def select_by_branch_date(cls, query, branch, date,
having=None, connection=None):
if branch:
branch_query = Sale.q.branchID == branch.id
if query:
query = AND(query, branch_query)
else:
query = branch_query
if date:
if isinstance(date, tuple):
date_query = AND(const.DATE(Sale.q.confirm_date) >= date[0],
const.DATE(Sale.q.confirm_date) <= date[1])
else:
date_query = const.DATE(Sale.q.confirm_date) == date
if query:
query = AND(query, date_query)
else:
query = date_query
return cls.select(query, having=having, connection=connection)
@property
def sale(self):
return Sale.get(self.sale_id, connection=self.get_connection())
@property
def average_cost(self):
if self.quantity:
return self.total_cost / self.quantity
return 0
class SoldItemsByBranchView(SoldItemView):
"""Store information about the all sold items by branch.
"""
columns = SoldItemView.columns.copy()
columns.update(dict(
branch_name=Person.q.name,
total=const.SUM(SaleItem.q.quantity * SaleItem.q.price),
))
joins = SoldItemView.joins[:]
joins.append(LEFTJOINOn(None, PersonAdaptToBranch,
PersonAdaptToBranch.q.id == Sale.q.branchID))
joins.append(LEFTJOINOn(None, Person,
PersonAdaptToBranch.q._originalID == Person.q.id))
clause = OR(SoldItemView.clause,
Sale.q.status == Sale.STATUS_RENEGOTIATED)
class PurchasedItemAndStockView(Viewable):
"""Stores information about the purchase items that will be delivered and
also the quantity that is already in stock.
This view is used to query which products are going to be delivered and if
they are on time or not.
@cvar id: the id of the purchased item
@cvar product_id: the id of the product
@cvar purchased: the quantity purchased
@cvar received: the quantity already received
@cvar stocked: the quantity in stock
@cvar expected_receival_date: the date that the item might be deliverd
@cvar purchase_date: the date when the item was purchased
@cvar branch: the branch where the purchase was done
"""
columns = dict(
id=PurchaseItem.q.id,
product_id=Product.q.id,
description=BaseSellableInfo.q.description,
purchased=PurchaseItem.q.quantity,
received=PurchaseItem.q.quantity_received,
stocked=const.SUM(ProductStockItem.q.quantity +
ProductStockItem.q.logic_quantity),
expected_receival_date=PurchaseItem.q.expected_receival_date,
order_id=PurchaseOrder.q.id,
purchased_date=PurchaseOrder.q.open_date,
branch=PurchaseOrder.q.branchID,
)
joins = [
LEFTJOINOn(None, PurchaseItem,
PurchaseItem.q.orderID == PurchaseOrder.q.id),
LEFTJOINOn(None, Sellable,
Sellable.q.id == PurchaseItem.q.sellableID),
INNERJOINOn(None, BaseSellableInfo,
Sellable.q.base_sellable_infoID == BaseSellableInfo.q.id),
LEFTJOINOn(None, Product,
Product.q.sellableID == PurchaseItem.q.sellableID),
LEFTJOINOn(None, ProductAdaptToStorable,
ProductAdaptToStorable.q._originalID == Product.q.id),
LEFTJOINOn(None, ProductStockItem,
ProductStockItem.q.storableID == ProductAdaptToStorable.q.id),
]
clause = AND(PurchaseOrder.q.status == PurchaseOrder.ORDER_CONFIRMED,
PurchaseOrder.q.branchID == ProductStockItem.q.branchID,
PurchaseItem.q.quantity > PurchaseItem.q.quantity_received,)
@property
def purchase_item(self):
return PurchaseItem.get(self.id, connection=self.get_connection())
class ConsignedItemAndStockView(PurchasedItemAndStockView):
columns = PurchasedItemAndStockView.columns.copy()
columns.update(dict(
sold=PurchaseItem.q.quantity_sold,
returned=PurchaseItem.q.quantity_returned,
))
joins = PurchasedItemAndStockView.joins[:]
clause = AND(PurchaseOrder.q.consigned == True,
PurchaseOrder.q.branchID == ProductStockItem.q.branchID)
class PurchaseReceivingView(Viewable):
"""Stores information about received orders.
@cvar id: the id of the receiving order
@cvar receival_date: the date when the receiving order was closed
@cvar invoice_number: the number of the order that was received
@cvar invoice_total: the total value of the received order
@cvar purchase_id: the id of the received order
@cvar branch_id: the id branch where the order was received
@cvar purchase_responsible_name: the one who have confirmed the purchase
@cvar responsible_name: the one who has received the order
@cvar supplier_name: the supplier name
"""
_Responsible = Alias(Person, "responsible")
_Supplier = Alias(Person, "supplier")
_PurchaseUser = Alias(PersonAdaptToUser, "purchase_user")
_PurchaseResponsible = Alias(Person, "purchase_responsible")
columns = dict(
id=ReceivingOrder.q.id,
receival_date=ReceivingOrder.q.receival_date,
invoice_number=ReceivingOrder.q.invoice_number,
invoice_total=ReceivingOrder.q.invoice_total,
purchase_id=PurchaseOrder.q.id,
branch_id=ReceivingOrder.q.branchID,
purchase_responsible_name=_PurchaseResponsible.q.name,
responsible_name=_Responsible.q.name,
supplier_name=_Supplier.q.name,
)
joins = [
LEFTJOINOn(None, PurchaseOrder,
ReceivingOrder.q.purchaseID == PurchaseOrder.q.id),
LEFTJOINOn(None, _PurchaseUser,
PurchaseOrder.q.responsibleID == _PurchaseUser.q.id),
LEFTJOINOn(None, _PurchaseResponsible,
_PurchaseUser.q._originalID == _PurchaseResponsible.q.id),
LEFTJOINOn(None, PersonAdaptToSupplier,
ReceivingOrder.q.supplierID == PersonAdaptToSupplier.q.id),
LEFTJOINOn(None, _Supplier,
PersonAdaptToSupplier.q._originalID == _Supplier.q.id),
LEFTJOINOn(None, PersonAdaptToUser,
ReceivingOrder.q.responsibleID == PersonAdaptToUser.q.id),
LEFTJOINOn(None, _Responsible,
PersonAdaptToUser.q._originalID == _Responsible.q.id),
]
class SaleItemsView(Viewable):
"""Show information about sold items and about the corresponding sale.
This is slightlig difrent than SoldItemView that groups sold items from
diferent sales.
"""
columns = dict(
id=Sellable.q.id,
code=Sellable.q.code,
description=BaseSellableInfo.q.description,
sale_id=SaleItem.q.saleID,
sale_date = Sale.q.open_date,
client_name = Person.q.name,
quantity=SaleItem.q.quantity,
unit_description=SellableUnit.q.description,
)
joins = [
LEFTJOINOn(None, SaleItem,
Sellable.q.id == SaleItem.q.sellableID),
LEFTJOINOn(None, Sale,
SaleItem.q.saleID == Sale.q.id),
LEFTJOINOn(None, SellableUnit,
Sellable.q.unitID == SellableUnit.q.id),
LEFTJOINOn(None, PersonAdaptToClient,
Sale.q.clientID == PersonAdaptToClient.q.id),
LEFTJOINOn(None, Person,
PersonAdaptToClient.q._originalID == Person.q.id),
INNERJOINOn(None, BaseSellableInfo,
Sellable.q.base_sellable_infoID == BaseSellableInfo.q.id),
]
clause = OR(Sale.q.status == Sale.STATUS_CONFIRMED,
Sale.q.status == Sale.STATUS_PAID,
Sale.q.status == Sale.STATUS_ORDERED)
class ReceivingItemView(Viewable):
"""Stores information about receiving items.
This view is used to query products that are going to be received or was
already received and the information related to that process.
@cvar id: the id of the receiving item
@cvar order_id: the id of the receiving order
@cvar purchase_id: the id of the purchase order
@cvar purchase_item_id: the id of the purchase item
@cvar sellable_id: the id of the sellable related to the received item
@cvar invoice_number: the invoice number of the receiving order
@cvar receival_date: the date when the item was received
@cvar quantity: the received quantity
@cvar cost: the product cost
@cvar unit_description: the product unit description
@cvar supplier_name: the product supplier name
"""
columns = dict(
id=ReceivingOrderItem.q.id,
order_id=ReceivingOrder.q.id,
purchase_id=ReceivingOrder.q.purchaseID,
purchase_item_id=ReceivingOrderItem.q.purchase_itemID,
sellable_id=ReceivingOrderItem.q.sellableID,
invoice_number=ReceivingOrder.q.invoice_number,
receival_date=ReceivingOrder.q.receival_date,
quantity=ReceivingOrderItem.q.quantity,
cost=ReceivingOrderItem.q.cost,
unit_description=SellableUnit.q.description,
supplier_name=Person.q.name,
)
joins = [
LEFTJOINOn(None, ReceivingOrder,
ReceivingOrderItem.q.receiving_orderID == ReceivingOrder.q.id),
LEFTJOINOn(None, Sellable,
ReceivingOrderItem.q.sellableID == Sellable.q.id),
LEFTJOINOn(None, SellableUnit,
Sellable.q.unitID == SellableUnit.q.id),
LEFTJOINOn(None, PersonAdaptToSupplier,
ReceivingOrder.q.supplierID == PersonAdaptToSupplier.q.id),
LEFTJOINOn(None, Person,
PersonAdaptToSupplier.q._originalID == Person.q.id),
]
class ProductionItemView(Viewable):
columns = dict(id=ProductionItem.q.id,
order_id=ProductionOrder.q.id,
order_status=ProductionOrder.q.status,
quantity=ProductionItem.q.quantity,
produced=ProductionItem.q.produced,
lost=ProductionItem.q.lost,
category_description=SellableCategory.q.description,
unit_description=SellableUnit.q.description,
description=BaseSellableInfo.q.description,)
joins = [
LEFTJOINOn(None, ProductionOrder,
ProductionItem.q.orderID == ProductionOrder.q.id),
LEFTJOINOn(None, Product,
ProductionItem.q.productID == Product.q.id),
LEFTJOINOn(None, Sellable,
Sellable.q.id == Product.q.sellableID),
LEFTJOINOn(None, SellableCategory,
SellableCategory.q.id == Sellable.q.categoryID),
LEFTJOINOn(None, SellableUnit,
Sellable.q.unitID == SellableUnit.q.id),
INNERJOINOn(None, BaseSellableInfo,
Sellable.q.base_sellable_infoID == BaseSellableInfo.q.id),]
@property
def production_item(self):
return ProductionItem.get(self.id, connection=self.get_connection())
class LoanView(Viewable):
PersonBranch = Alias(Person, 'person_branch')
PersonResponsible = Alias(Person, 'person_responsible')
PersonClient = Alias(Person, 'person_client')
columns = dict(id=Loan.q.id,
status=Loan.q.status,
open_date=Loan.q.open_date,
close_date=Loan.q.close_date,
expire_date=Loan.q.expire_date,
removed_by=Loan.q.removed_by,
branch_name=PersonBranch.q.name,
responsible_name=PersonResponsible.q.name,
client_name=PersonClient.q.name,
loaned=const.SUM(LoanItem.q.quantity),
total=const.SUM(LoanItem.q.quantity * LoanItem.q.price),)
joins = [
INNERJOINOn(None, LoanItem, Loan.q.id == LoanItem.q.loanID),
LEFTJOINOn(None, PersonAdaptToBranch,
Loan.q.branchID == PersonAdaptToBranch.q.id),
LEFTJOINOn(None, PersonAdaptToUser,
Loan.q.responsibleID == PersonAdaptToUser.q.id),
LEFTJOINOn(None, PersonAdaptToClient,
Loan.q.clientID == PersonAdaptToClient.q.id),
LEFTJOINOn(None, PersonBranch,
PersonAdaptToBranch.q._originalID == PersonBranch.q.id),
LEFTJOINOn(None, PersonResponsible,
PersonAdaptToUser.q._originalID == PersonResponsible.q.id),
LEFTJOINOn(None, PersonClient,
PersonAdaptToClient.q._originalID == PersonClient.q.id),
]
class LoanItemView(Viewable):
columns = dict(id=LoanItem.q.id,
loan_id=Loan.q.id,
loan_status=Loan.q.status,
opened=Loan.q.open_date,
closed=Loan.q.close_date,
quantity=LoanItem.q.quantity,
sale_quantity=LoanItem.q.sale_quantity,
return_quantity=LoanItem.q.return_quantity,
price=LoanItem.q.price,
total=LoanItem.q.quantity * LoanItem.q.price,
sellable_id=Sellable.q.id,
code=Sellable.q.code,
category_description=SellableCategory.q.description,
unit_description=SellableUnit.q.description,
description=BaseSellableInfo.q.description,)
joins = [
LEFTJOINOn(None, Loan, LoanItem.q.loanID == Loan.q.id),
LEFTJOINOn(None, Sellable,
LoanItem.q.sellableID == Sellable.q.id),
LEFTJOINOn(None, SellableUnit,
Sellable.q.unitID == SellableUnit.q.id),
LEFTJOINOn(None, SellableCategory,
SellableCategory.q.id == Sellable.q.categoryID),
INNERJOINOn(None, BaseSellableInfo,
Sellable.q.base_sellable_infoID == BaseSellableInfo.q.id),]