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 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359
|
# This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
from collections import defaultdict
from decimal import Decimal
from sql import Literal, Union
from sql.aggregate import Sum
from sql.conditionals import Coalesce
from sql.functions import CharLength
from sql.operators import Equal
from trytond.i18n import gettext
from trytond.model import (
DeactivableMixin, Exclude, Index, ModelSQL, ModelView, Workflow, fields)
from trytond.model.exceptions import AccessError
from trytond.modules.account.tax import TaxableMixin
from trytond.modules.currency.fields import Monetary
from trytond.modules.product import price_digits, round_price
from trytond.pool import Pool
from trytond.pyson import Bool, Eval, Id, If
from trytond.tools import grouped_slice, reduce_ids
from trytond.transaction import Transaction
from trytond.wizard import Button, StateTransition, StateView, Wizard
from .exceptions import SaleValidationError, SessionValidationError
class POS(ModelSQL, ModelView):
"Point of Sale"
__name__ = 'sale.point'
name = fields.Char("Name", required=True)
company = fields.Many2One('company.company', "Company", required=True)
tax_included = fields.Boolean(
"Tax Included",
help="Check if unit Price includes tax.")
sequence = fields.Many2One(
'ir.sequence.strict', "Sequence", required=True,
domain=[
('company', '=', Eval('company', -1)),
('sequence_type', '=', Id('sale_point', 'sequence_type_sale')),
])
address = fields.Many2One('party.address', "Address")
storage_location = fields.Many2One(
'stock.location', "Storage Location", required=True,
domain=[('type', '=', 'storage')],
help="The location from where goods are taken when sold.")
return_location = fields.Many2One(
'stock.location', "Return Location",
domain=[('type', '=', 'storage')],
help="The location where goods are put when returned.\n"
"If empty the storage location is used.")
customer_location = fields.Many2One(
'stock.location', "To Location", required=True,
domain=[('type', '=', 'customer')],
help="The location where goods are put when sold.")
journal = fields.Many2One(
'account.journal', "Journal", required=True,
domain=[
('type', '=', 'revenue'),
])
@classmethod
def default_company(cls):
return Transaction().context.get('company')
@classmethod
def default_tax_included(cls):
return True
@classmethod
def write(cls, *args):
pool = Pool()
Sale = pool.get('sale.point.sale')
transaction = Transaction()
if transaction.user and transaction.check_access:
actions = iter(args)
for points, values in zip(actions, actions):
if 'tax_included' in values:
for sub_points in grouped_slice(points):
if Sale.search([
('point', 'in',
list(map(int, sub_points))),
],
limit=1, order=[]):
raise AccessError(gettext(
'sale_point.msg_point_change_tax_included')
)
super().write(*args)
class POSSale(Workflow, ModelSQL, ModelView, TaxableMixin):
"POS Sale"
__name__ = 'sale.point.sale'
_rec_name = 'number'
_states = {
'readonly': Eval('state') != 'open',
}
company = fields.Many2One(
'company.company', "Company", required=True, states=_states)
employee = fields.Many2One(
'company.employee', "Employee",
domain=[
('company', '=', Eval('company', -1)),
],
states=_states)
point = fields.Many2One(
'sale.point', "Point", required=True, ondelete='RESTRICT',
states={
'readonly': ((Eval('id', 0) > 0)
| Bool(Eval('lines', [0]))
| _states['readonly']),
},
domain=[
('company', '=', Eval('company', -1)),
])
number = fields.Char(
"Number", readonly=True,
states={
'required': Eval('state').in_(['done', 'posted']),
})
date = fields.Date("Date", required=True, states=_states)
lines = fields.One2Many(
'sale.point.sale.line', 'sale', "Lines",
states={
'readonly': (~Eval('point')
| _states['readonly']),
})
payments = fields.One2Many(
'sale.point.payment', 'sale', "Payments", states=_states)
total_tax = fields.Function(Monetary(
"Total Tax", currency='currency', digits='currency'),
'on_change_with_total_tax')
total = fields.Function(Monetary(
"Total", currency='currency', digits='currency'),
'on_change_with_total')
amount_paid = fields.Function(Monetary(
"Paid", currency='currency', digits='currency'),
'on_change_with_amount_paid')
amount_to_pay = fields.Function(Monetary(
"To Pay", currency='currency', digits='currency'),
'on_change_with_amount_to_pay')
state = fields.Selection([
('open', "Open"),
('done', "Done"),
('posted', "Posted"),
('cancelled', "Cancelled"),
], "State", readonly=True, sort=False)
move = fields.Many2One(
'account.move', "Move", readonly=True,
states={
'invisible': ~Eval('move'),
})
currency = fields.Function(fields.Many2One(
'currency.currency', "Currency"), 'on_change_with_currency')
del _states
@classmethod
def __setup__(cls):
cls.number.search_unaccented = False
super().__setup__()
t = cls.__table__()
cls._sql_indexes.add(
Index(
t, (t.state, Index.Equality()),
where=t.state.in_(['open', 'done'])))
cls._transitions |= {
('open', 'done'),
('done', 'posted'),
('done', 'open'),
('open', 'cancelled'),
('cancelled', 'open'),
}
cls._buttons.update(
pay={
'invisible': Eval('state') != 'open',
'readonly': ~Eval('amount_to_pay'),
'depends': ['amount_to_pay'],
},
cancel={
'invisible': Eval('state') != 'open',
'depends': ['state'],
},
open={
'invisible': ~Eval('state').in_(['done', 'cancelled']),
'depends': ['state'],
},
process={
'invisible': Eval('state') != 'open',
'readonly': (
Bool(Eval('amount_to_pay', 0))
| ~Eval('lines', [-1])),
'depends': ['state', 'amount_to_pay'],
},
post={
'invisible': Eval('state') != 'done',
'depends': ['state'],
},
)
@classmethod
def order_number(cls, tables):
table, _ = tables[None]
return [CharLength(table.number), table.number]
@classmethod
def default_company(cls):
return Transaction().context.get('company')
@classmethod
def default_employee(cls):
User = Pool().get('res.user')
if Transaction().context.get('employee'):
return Transaction().context['employee']
else:
user = User(Transaction().user)
if user.employee:
return user.employee.id
@classmethod
def default_date(cls):
return Pool().get('ir.date').today()
@classmethod
def default_state(cls):
return 'open'
@fields.depends('company')
def on_change_with_currency(self, name=None):
return self.company.currency if self.company else None
@fields.depends('lines')
def on_change_with_total_tax(self, name=None):
return sum(line.tax_amount for line in self.lines)
@fields.depends('company', 'lines')
def on_change_with_total(self, name=None):
return sum(line.gross_amount for line in self.lines)
@fields.depends('payments')
def on_change_with_amount_paid(self, name=None):
return sum(p.amount or Decimal('0') for p in self.payments)
@fields.depends(methods=[
'on_change_with_total', 'on_change_with_amount_paid'])
def on_change_with_amount_to_pay(self, name=None):
return (self.on_change_with_total()
- self.on_change_with_amount_paid())
def get_rec_name(self, name):
if self.number:
return self.number
else:
return '(%s)' % self.id
@classmethod
def copy(cls, sales, default=None):
if default is None:
default = {}
else:
default = default.copy()
default.setdefault('employee')
default.setdefault('number')
default.setdefault('payments')
default.setdefault('move')
return super().copy(sales, default=default)
@classmethod
def delete(cls, sales):
for sale in sales:
if sale.number:
raise AccessError(
gettext('sale_point.msg_sale_delete_numbered',
sale=sale.rec_name))
super().delete(sales)
@classmethod
def validate(cls, sales):
super().validate(sales)
for sale in sales:
if sale.state in {'done', 'posted'}:
if sale.amount_to_pay:
raise SaleValidationError(gettext(
'sale_point.msg_sale_amount_to_pay_done_posted',
sale=sale.rec_name))
elif sale.state == 'cancelled':
if sale.amount_paid:
raise SaleValidationError(gettext(
'sale_point.msg_sale_amount_paid_cancelled',
sale=sale.rec_name))
@classmethod
@ModelView.button_action('sale_point.wizard_pay')
def pay(cls, sales):
pass
@classmethod
@ModelView.button
@Workflow.transition('cancelled')
def cancel(cls, sales):
pass
@classmethod
@ModelView.button
@Workflow.transition('open')
def open(cls, sales):
pass
@classmethod
@ModelView.button
def process(cls, sales):
cls.do([sale for sale in sales if not sale.amount_to_pay])
@classmethod
@Workflow.transition('done')
def do(cls, sales):
for sale in sales:
with Transaction().set_context(company=sale.company.id):
sale.number = sale.point.sequence.get()
cls.save(sales)
@classmethod
@Workflow.transition('posted')
def post(cls, sales):
pool = Pool()
StockMove = pool.get('stock.move')
AccountMove = pool.get('account.move')
stock_moves = []
account_moves = []
for sale in sales:
assert not sale.move
for line in sale.lines:
move = line.get_stock_move()
if move:
stock_moves.append(move)
account_move = sale.get_account_move()
sale.move = account_move
account_moves.append(account_move)
if stock_moves:
StockMove.save(stock_moves)
StockMove.do(stock_moves)
if account_moves:
AccountMove.save(account_moves)
AccountMove.post(account_moves)
cls.save(sales)
def get_account_move(self):
'Return account move'
pool = Pool()
Move = pool.get('account.move')
Period = pool.get('account.period')
period = Period.find(self.company, date=self.date)
lines = []
for line in self.lines:
move_lines = line.get_account_move_lines()
if move_lines:
lines.extend(move_lines)
move_lines = self.get_tax_move_lines()
if move_lines:
lines.extend(move_lines)
for payment in self.payments:
move_lines = payment.get_account_move_lines()
if move_lines:
lines.extend(move_lines)
move = Move()
move.journal = self.point.journal
move.period = period
move.date = self.date
move.origin = self
move.company = self.company
move.lines = lines
return move
def get_tax_move_lines(self):
"Return account move lines for taxes"
pool = Pool()
Line = pool.get('account.move.line')
TaxLine = pool.get('account.tax.line')
lines = []
taxes = self._get_taxes()
# Distribute rounding error from tax computation
tax_amount = sum(t['amount'] for t in taxes)
difference = self.total_tax - tax_amount
if difference:
remaining = difference
for tax in taxes:
if tax['amount']:
if tax_amount:
ratio = tax['amount'] / tax_amount
else:
ratio = 1 / len(taxes)
value = self.currency.round(difference * ratio)
tax['amount'] += value
remaining -= value
# Add remaining rounding error to the first tax
if remaining:
for tax in taxes:
if tax['amount']:
tax['amount'] += remaining
break
for tax in taxes:
amount = tax['amount']
if not amount:
continue
line = Line()
line.description = tax['description']
if amount >= 0:
line.debit, line.credit = Decimal(0), amount
else:
line.debit, line.credit = -amount, 0
line.account = tax['account']
if tax['tax']:
tax_line = TaxLine()
tax_line.amount = amount
tax_line.type = 'tax'
tax_line.tax = tax['tax']
line.tax_lines = [tax_line]
lines.append(line)
return lines
@property
def taxable_lines(self):
return sum((line.taxable_lines for line in self.lines), [])
@property
def tax_date(self):
return self.date
class POSSaleLine(ModelSQL, ModelView, TaxableMixin):
"POS Sale Line"
__name__ = 'sale.point.sale.line'
_states = {
'readonly': Eval('sale_state') != 'open',
}
sale = fields.Many2One(
'sale.point.sale', "Sale", required=True, ondelete='CASCADE',
states=_states)
product = fields.Many2One(
'product.product', "Product", required=True,
domain=[
If((Eval('sale_state') == 'open')
& ~(Eval('quantity', 0) < 0),
('salable', '=', True),
()),
],
context={
'company': Eval('company'),
},
states=_states, depends={'company'})
quantity = fields.Float(
"Quantity", digits='unit', required=True, states=_states)
unit = fields.Function(
fields.Many2One('product.uom', "Unit"), 'on_change_with_unit')
unit_list_price = fields.Numeric(
"Unit List Price", digits=price_digits, required=True,
states={
'readonly': True, # Allow client to sent value
})
unit_gross_price = fields.Numeric(
"Unit Gross Price", digits=price_digits, required=True,
states={
'readonly': True, # Allow client to sent value
})
unit_price = fields.Function(fields.Numeric(
"Unit Price", digits=price_digits,
depends={'unit_list_price', 'unit_gross_price'}),
'on_change_with_unit_price')
amount = fields.Function(Monetary(
"Amount", currency='currency', digits='currency'),
'on_change_with_amount')
moves = fields.One2Many(
'stock.move', 'origin', 'Moves', readonly=True,
states={
'invisible': ~Eval('moves', []),
})
sale_state = fields.Function(fields.Selection(
'get_sale_states', "Sale State"), 'on_change_with_sale_state')
company = fields.Function(
fields.Many2One('company.company', "Company"),
'on_change_with_company', searcher='search_company')
currency = fields.Function(
fields.Many2One('currency.currency', "Currency"),
'on_change_with_currency')
del _states
@classmethod
def __setup__(cls):
super().__setup__()
cls.__access__.add('sale')
@fields.depends('product')
def on_change_with_unit(self, name=None):
# TODO packaging UOM
return self.product.sale_uom if self.product else None
@classmethod
def get_sale_states(cls):
pool = Pool()
Sale = pool.get('sale.point.sale')
return Sale.fields_get(['state'])['state']['selection']
@fields.depends('sale', '_parent_sale.state')
def on_change_with_sale_state(self, name=None):
if self.sale:
return self.sale.state
@fields.depends(
'product', 'sale', '_parent_sale.point', '_parent_sale.date',
methods=['on_change_with_unit_price', 'on_change_with_amount'])
def on_change_product(self):
pool = Pool()
Tax = pool.get('account.tax')
self.unit_gross_price = None
self.unit_list_price = None
if self.product and self.sale and self.sale.point:
if (self.sale.point.tax_included
and self.product.gross_price is not None):
self.unit_gross_price = self.product.gross_price
self.unit_list_price = round_price(Tax.reverse_compute(
self.unit_gross_price, self.taxes,
date=self.sale.date))
elif self.product.list_price is not None:
self.unit_list_price = self.product.list_price
taxes = Tax.compute(
self.taxes, self.unit_list_price, 1,
date=self.sale.date)
tax_amount = sum(t['amount'] for t in taxes)
self.unit_gross_price = (
self.unit_list_price + tax_amount).quantize(
Decimal(1)
/ 10 ** self.__class__.unit_gross_price.digits[1])
self.unit_price = self.on_change_with_unit_price()
self.amount = self.on_change_with_amount()
@fields.depends('unit_list_price', 'unit_gross_price',
'sale', '_parent_sale.point')
def on_change_with_unit_price(self, name=None):
if self.sale and self.sale.point:
if self.sale.point.tax_included:
return self.unit_gross_price
else:
return self.unit_list_price
else:
return
@fields.depends('currency', 'quantity', 'unit_price')
def on_change_with_amount(self, name=None):
amount = (Decimal(str(self.quantity or 0))
* (self.unit_price or Decimal('0')))
if self.currency:
return self.currency.round(amount)
return amount
@fields.depends('sale', '_parent_sale.company')
def on_change_with_company(self, name=None):
return self.sale.company if self.sale else None
@classmethod
def search_company(cls, name, clause):
return [('sale.company',) + tuple(clause[1:])]
@fields.depends('sale', '_parent_sale.currency')
def on_change_with_currency(self, name=None):
return self.sale.currency if self.sale else None
@property
def untax_amount(self):
amount = (Decimal(str(self.quantity or 0))
* (self.unit_list_price or Decimal('0')))
if getattr(self, 'currency', None):
amount = self.currency.round(amount)
return amount
@property
def tax_amount(self):
amount = self.gross_amount - self.untax_amount
if getattr(self, 'currency', None):
amount = self.currency.round(amount)
return amount
@property
def gross_amount(self):
amount = (Decimal(str(self.quantity or 0))
* (self.unit_gross_price or Decimal('0')))
if getattr(self, 'currency', None):
amount = self.currency.round(amount)
return amount
def get_rec_name(self, name):
return '%s @ %s' % (self.product.rec_name, self.sale.rec_name)
@classmethod
def search_rec_name(cls, name, clause):
_, operator, value = clause
if operator.startswith('!') or operator.startswith('not '):
bool_op = 'AND'
else:
bool_op = 'OR'
return [bool_op,
('sale.rec_name', *clause[1:]),
('product.rec_name', *clause[1:]),
]
@classmethod
def copy(cls, lines, default=None):
if default is None:
default = {}
else:
default = default.copy()
default.setdefault('moves')
return super().copy(lines, default=default)
def get_stock_move(self):
'Return stock move'
pool = Pool()
Move = pool.get('stock.move')
if self.product.type == 'service':
return
move = Move()
move.quantity = abs(self.quantity)
move.unit = self.unit
move.product = self.product
move.from_location = self.from_location
move.to_location = self.to_location
move.company = self.company
move.unit_price = self.unit_list_price
move.currency = self.currency
move.effective_date = self.sale.date
move.origin = self
return move
@property
def from_location(self):
if self.quantity >= 0:
return self.sale.point.storage_location
else:
return self.sale.point.customer_location
@property
def to_location(self):
if self.quantity >= 0:
return self.sale.point.customer_location
else:
return self.sale.point.return_location
def get_account_move_lines(self):
"Return account move lines"
pool = Pool()
Line = pool.get('account.move.line')
TaxLine = pool.get('account.tax.line')
line = Line()
if self.quantity >= 0:
line.debit, line.credit = Decimal(0), self.untax_amount
else:
line.debit, line.credit = -self.untax_amount, Decimal(0)
with Transaction().set_context(company=self.company.id):
line.account = self.product.account_revenue_used
line.origin = self
tax_lines = []
for tax in self._get_taxes():
tax_line = TaxLine()
tax_line.amount = tax['base']
tax_line.type = 'base'
tax_line.tax = tax['tax']
tax_lines.append(tax_line)
line.tax_lines = tax_lines
return [line]
@property
def taxes(self):
pool = Pool()
Product = pool.get('product.product')
with Transaction().set_context(company=self.company.id):
return Product(self.product).customer_taxes_used
@property
def taxable_lines(self):
return [
(self.taxes, self.unit_list_price, self.quantity, self.tax_date)]
@property
def tax_type(self):
if self.quantity >= 0:
return 'invoice'
else:
return 'credit_note'
@property
def tax_date(self):
return self.sale.date
class POSCashSession(Workflow, ModelSQL, ModelView):
"POS Cash Session"
__name__ = 'sale.point.cash.session'
point = fields.Many2One(
'sale.point', "Point", required=True, ondelete='CASCADE')
previous_session = fields.Many2One(
'sale.point.cash.session', "Previous Session",
readonly=True, ondelete='RESTRICT',
domain=[
('point', '=', Eval('point', -1)),
])
next_session = fields.One2One(
'sale.point.cash.session.relation', 'previous', 'next', "Next Session",
readonly=True,
domain=[
('point', '=', Eval('point', -1)),
])
payments = fields.One2Many(
'sale.point.payment', 'session', "Payments", readonly=True,
domain=[
('sale.point', '=', Eval('point', -1)),
])
transfers = fields.One2Many(
'sale.point.cash.transfer', 'session', "Transfers",
domain=[
('point', '=', Eval('point', -1)),
],
states={
'readonly': ~Eval('point') | (Eval('state') != 'open'),
})
start_amount = fields.Function(
Monetary("Start Amount", currency='currency', digits='currency'),
'get_start_amount')
balance = fields.Function(Monetary(
"Balance", currency='currency', digits='currency'),
'get_balance')
end_amount = Monetary(
"End Amount", currency='currency', digits='currency', required=True,
states={
'required': Eval('state').in_(['closed', 'posted']),
'readonly': Eval('state') != 'open',
})
state = fields.Selection([
('open', "Open"),
('closed', "Closed"),
('posted', "Posted"),
], "State", readonly=True, required=True, sort=False)
currency = fields.Function(fields.Many2One(
'currency.currency', "Currency"),
'on_change_with_currency')
@classmethod
def __setup__(cls):
super().__setup__()
t = cls.__table__()
cls._sql_constraints = [
('point_cash_session_open_unique',
Exclude(t, (t.point, Equal),
where=t.state == 'open'),
'sale_point.msg_point_cash_session_open_unique'),
('point_cash_session_previous_unique',
Exclude(t, (Coalesce(t.previous_session, -t.point), Equal)),
'sale_point.msg_cash_session_previous_unique'),
]
cls._sql_indexes.add(
Index(t, (t.state, Index.Equality()), where=t.state == 'open'))
cls._transitions |= {
('open', 'closed'),
('closed', 'open'),
('closed', 'posted'),
}
cls._buttons.update(
open={
'invisible': Eval('state') != 'closed',
'depends': ['state'],
},
close={
'pre_validate': [
('end_amount', '!=', None),
],
'invisible': Eval('state') != 'open',
'depends': ['state'],
},
post={
'invisible': Eval('state') != 'closed',
'depends': ['state'],
},
)
@classmethod
def __register__(cls, module_name):
super().__register__(module_name)
table_h = cls.__table_handler__(module_name)
# Migration from 6.2: add point to previous session uniqueness.
table_h.drop_constraint('previous_session_unique')
@classmethod
def default_state(cls):
return 'open'
@fields.depends('point')
def on_change_point(self):
if self.point:
last = self.__class__.search([
('point', '=', self.point.id),
('next_session', '=', None),
], limit=1)
if last:
self.previous_session, = last
def get_start_amount(self, name):
if self.previous_session:
return self.previous_session.end_amount
else:
return Decimal(0)
@classmethod
def get_balance(cls, sessions, name):
pool = Pool()
Payment = pool.get('sale.point.payment')
Transfer = pool.get('sale.point.cash.transfer')
payment = Payment.__table__()
transfer = Transfer.__table__()
cursor = Transaction().connection.cursor()
balances = defaultdict(Decimal)
for sub_sessions in grouped_slice(sessions):
sub_ids = [s.id for s in sub_sessions]
query = Union(
payment.select(
payment.session.as_('session'),
Sum(payment.amount).as_('amount'),
where=reduce_ids(payment.session, sub_ids),
group_by=[payment.session]),
transfer.select(
transfer.session.as_('session'),
Sum(transfer.amount).as_('amount'),
where=reduce_ids(transfer.session, sub_ids),
group_by=[transfer.session]),
all_=True)
query = query.select(
query.session, Sum(query.amount),
group_by=[query.session])
cursor.execute(*query)
for session_id, balance in cursor:
if not isinstance(balance, Decimal):
balance = Decimal(str(balance))
balances[session_id] = balance
for session in sessions:
balances[session.id] = session.currency.round(balances[session.id])
return balances
@classmethod
def default_end_amount(cls):
return Decimal(0)
@fields.depends('point')
def on_change_with_currency(self, name=None):
return self.point.company.currency if self.point else None
@classmethod
@ModelView.button
@Workflow.transition('open')
def open(cls, sessions):
pass
@classmethod
@ModelView.button
@Workflow.transition('closed')
def close(cls, sessions):
pool = Pool()
Lang = pool.get('ir.lang')
for session in sessions:
if session.end_amount - session.start_amount != session.balance:
lang = Lang.get()
raise SessionValidationError(gettext(
'sale_point.msg_cash_session_wrong_end_amount',
session=session.rec_name,
end_amount=lang.currency(
session.end_amount, session.currency),
amount=lang.currency(
session.start_amount + session.balance,
session.currency)))
@classmethod
def delete(cls, sessions):
cls.open(sessions)
for session in sessions:
if session.state != 'open':
raise AccessError(
gettext('sale_point.msg_cash_session_delete_open',
session=session.rec_name))
super().delete(sessions)
@classmethod
@ModelView.button
@Workflow.transition('posted')
def post(cls, sessions):
pool = Pool()
Transfer = pool.get('sale.point.cash.transfer')
Transfer.post(sum((s.transfers for s in sessions), ()))
@classmethod
def get_current(cls, point):
sessions = cls.search([
('point', '=', point.id),
('state', '=', 'open'),
], limit=1)
if sessions:
session, = sessions
else:
cls.lock()
last = cls.search([
('point', '=', point.id),
('next_session', '=', None),
], limit=1)
if last:
last, = last
else:
last = None
session = cls(point=point, previous_session=last)
session.save()
return session
class POSCashSessionRelation(ModelSQL):
"POS Session Relation"
__name__ = 'sale.point.cash.session.relation'
previous = fields.Many2One('sale.point.cash.session', "Previous")
next = fields.Many2One('sale.point.cash.session', "Next")
@classmethod
def table_query(cls):
pool = Pool()
Session = pool.get('sale.point.cash.session')
session = Session.__table__()
return session.select(
session.id.as_('id'),
session.create_date.as_('create_date'),
session.create_uid.as_('create_uid'),
session.write_date.as_('write_date'),
session.write_uid.as_('write_uid'),
session.previous_session.as_('previous'),
session.id.as_('next'))
class POSPayment(ModelSQL, ModelView):
"POS Payment"
__name__ = 'sale.point.payment'
_states = {
'readonly': Eval('sale_state') != 'open',
}
sale = fields.Many2One(
'sale.point.sale', "Sale", required=True, ondelete='CASCADE',
states=_states)
method = fields.Many2One(
'sale.point.payment.method', "Method", required=True,
domain=[
('company', '=', Eval('company')),
If(Eval('amount', 0) < 0,
('cash', '=', True),
()),
],
states=_states)
amount = Monetary(
"Amount", currency='currency', digits='currency', states=_states)
session = fields.Many2One(
'sale.point.cash.session', "Session",
ondelete='RESTRICT', readonly=True,
domain=[
('point', '=', Eval('point', -1)),
],
states={
'invisible': ~Eval('cash'),
'required': Eval('cash', False),
})
currency = fields.Function(fields.Many2One(
'currency.currency', "Currency"), 'on_change_with_currency')
company = fields.Function(
fields.Many2One('company.company', "Company"),
'on_change_with_company', searcher='search_company')
sale_state = fields.Function(fields.Selection(
'get_sale_states', "Sale State"), 'on_change_with_sale_state')
point = fields.Function(fields.Many2One(
'sale.point', "Point"), 'on_change_with_point')
cash = fields.Function(fields.Boolean("Cash"), 'on_change_with_cash')
del _states
@classmethod
def __setup__(cls):
super().__setup__()
cls.__access__.add('sale')
@fields.depends('sale', '_parent_sale.amount_to_pay')
def on_change_sale(self):
if self.sale:
self.amount = self.sale.amount_to_pay
@fields.depends('company')
def on_change_with_currency(self, name=None):
return self.company.currency if self.company else None
@fields.depends('sale', '_parent_sale.company')
def on_change_with_company(self, name=None):
return self.sale.company if self.sale else None
@classmethod
def search_company(cls, name, clause):
return [('sale.company',) + tuple(clause[1:])]
@classmethod
def get_sale_states(cls):
pool = Pool()
Sale = pool.get('sale.point.sale')
return Sale.fields_get(['state'])['state']['selection']
@fields.depends('sale', '_parent_sale.state')
def on_change_with_sale_state(self, name=None):
if self.sale:
return self.sale.state
@fields.depends('sale', '_parent_sale.point')
def on_change_with_point(self, name=None):
return self.sale.point if self.sale else None
@fields.depends('method')
def on_change_with_cash(self, name=None):
if self.method:
return self.method.cash
@classmethod
def create(cls, vlist):
pool = Pool()
Method = pool.get('sale.point.payment.method')
Sale = pool.get('sale.point.sale')
Session = pool.get('sale.point.cash.session')
vlist = [v.copy() for v in vlist]
for values in vlist:
if values.get('method') and values.get('sale'):
method = Method(values['method'])
if method.cash:
sale = Sale(values['sale'])
values['session'] = Session.get_current(sale.point)
return super().create(vlist)
@classmethod
def delete(cls, payments):
for payment in payments:
if payment.sale.state != 'open':
raise AccessError(
gettext('sale_point.msg_payment_delete_sale_open',
payment=payment.rec_name,
sale=payment.sale.rec_name))
if payment.session and payment.session.state != 'open':
raise AccessError(
gettext('sale_point.msg_payment_delete_session_open',
payment=payment.rec_name,
session=payment.session.rec_name))
super().delete(payments)
def get_account_move_lines(self):
"Return account move lines"
pool = Pool()
Line = pool.get('account.move.line')
line = Line()
if self.sale.total >= 0:
line.debit, line.credit = self.amount, Decimal(0)
else:
line.debit, line.credit = Decimal(0), -self.amount
line.account = self.method.account
line.origin = self
return [line]
class POSPaymentMethod(DeactivableMixin, ModelSQL, ModelView):
"POS Payment Method"
__name__ = 'sale.point.payment.method'
company = fields.Many2One('company.company', "Company", required=True)
name = fields.Char("Name", required=True, translate=True)
account = fields.Many2One(
'account.account', "Account", required=True,
domain=[
('type', '!=', None),
('closed', '!=', True),
('company', '=', Eval('company')),
])
cash = fields.Boolean("Cash")
@classmethod
def __setup__(cls):
super().__setup__()
t = cls.__table__()
cls._sql_constraints = [
('cash_company_unique',
Exclude(t, (t.company, Equal),
where=(t.active == Literal(True))
& (t.cash == Literal(True))),
'sale_point.msg_payment_method_cash_unique'),
]
cls._order.insert(0, ('name', 'ASC'))
@classmethod
def default_company(cls):
return Transaction().context.get('company')
class POSPay(Wizard):
"POS Pay"
__name__ = 'sale.point.sale.pay'
start_state = 'payment'
payment = StateView(
'sale.point.payment',
'sale_point.payment_view_form_wizard', [
Button("Cancel", 'end', 'tryton-cancel'),
Button("OK", 'pay', 'tryton-ok', default=True),
])
pay = StateTransition()
@classmethod
def __setup__(cls):
super().__setup__()
cls.__rpc__['create'].fresh_session = True
def default_payment(self, fields):
pool = Pool()
PaymentMethod = pool.get('sale.point.payment.method')
default = {
'sale': self.record.id,
'amount': self.record.amount_to_pay,
}
if default['amount'] < 0:
methods = PaymentMethod.search([
('company', '=', self.record.company.id),
('cash', '=', True),
])
if methods:
method, = methods
default['method'] = method.id
return default
def transition_pay(self):
self.payment.sale = self.record
self.payment.save()
if self.record.amount_to_pay:
return 'payment'
else:
self.model.process([self.record])
return 'end'
class POSCashTransfer(Workflow, ModelSQL, ModelView):
"POS Cash Transfer"
__name__ = 'sale.point.cash.transfer'
_states = {
'readonly': Eval('state') == 'posted',
}
point = fields.Many2One(
'sale.point', "Point", required=True, states=_states)
session = fields.Many2One(
'sale.point.cash.session', "Session", required=True,
domain=[
('point', '=', Eval('point', -1)),
If(Eval('state') == 'draft',
('state', '=', 'open'),
()),
],
states={
'required': Eval('id', -1) >= 0,
'readonly': _states['readonly'],
'invisible': ~Eval('point'),
})
type = fields.Many2One(
'sale.point.cash.transfer.type', "Type", required=True,
domain=[
('company', '=', Eval('company', -1)),
])
amount = Monetary(
"Amount", currency='currency', digits='currency', states=_states)
date = fields.Date("Date", required=True)
state = fields.Selection([
('draft', "Draft"),
('posted', "Posted"),
], "State", readonly=True, required=True, sort=False)
move = fields.Many2One(
'account.move', "Move", readonly=True,
states={
'invisible': ~Eval('move'),
})
company = fields.Function(fields.Many2One(
'company.company', "Company"),
'on_change_with_company')
currency = fields.Function(fields.Many2One(
'currency.currency', "Currency"),
'on_change_with_currency')
del _states
@classmethod
def __setup__(cls):
super().__setup__()
t = cls.__table__()
cls._sql_indexes.add(
Index(t, (t.state, Index.Equality()), where=t.state == 'draft'))
cls._transitions |= {
('draft', 'posted'),
}
cls._buttons.update(
post={
'invisible': Eval('state') != 'draft',
'depends': ['state'],
},
)
@classmethod
def default_state(cls):
return 'draft'
@classmethod
def default_date(cls):
return Pool().get('ir.date').today()
@fields.depends('point')
def on_change_with_company(self, name=None):
return self.point.company if self.point else None
@fields.depends('point')
def on_change_with_currency(self, name=None):
return self.point.company.currency if self.point else None
@classmethod
def delete(cls, transfers):
for transfer in transfers:
if transfer.state == 'posted':
raise AccessError(
gettext('sale_point.msg_transfer_delete_posted',
transfer=transfer.rec_name))
if transfer.session.state != 'open':
raise AccessError(
gettext('sale_point.msg_transfer_delete_session_open',
transfer=transfer.rec_name,
session=transfer.session.rec_name))
super().delete(transfers)
@classmethod
@ModelView.button
@Workflow.transition('posted')
def post(cls, transfers):
pool = Pool()
Move = pool.get('account.move')
PaymentMethod = pool.get('sale.point.payment.method')
methods = PaymentMethod.search([('cash', '=', True)])
company2account = {m.company: m.account for m in methods}
moves = []
for transfer in transfers:
assert not transfer.move
move = transfer.get_move(
account=company2account.get(transfer.company))
transfer.move = move
moves.append(move)
Move.save(moves)
Move.post(moves)
cls.save(transfers)
def get_move(self, account):
pool = Pool()
Line = pool.get('account.move.line')
Move = pool.get('account.move')
Period = pool.get('account.period')
period = Period.find(self.company, date=self.date)
line = Line()
if self.amount >= 0:
line.debit, line.credit = self.amount, Decimal(0)
else:
line.debit, line.credit = Decimal(0), -self.amount
line.account = account
counterpart = Line()
counterpart.debit, counterpart.credit = line.credit, line.debit
counterpart.account = self.type.account
move = Move()
move.journal = self.type.journal
move.period = period
move.date = self.date
move.origin = self
move.company = self.company
move.lines = [line, counterpart]
return move
@classmethod
def create(cls, vlist):
pool = Pool()
Point = pool.get('sale.point')
Session = pool.get('sale.point.cash.session')
vlist = [v.copy() for v in vlist]
for values in vlist:
if values.get('point') and not values.get('session'):
point = Point(values['point'])
values['session'] = Session.get_current(point)
return super().create(vlist)
class POSCashTransferType(ModelSQL, ModelView):
"POS Cash Transfer Type"
__name__ = 'sale.point.cash.transfer.type'
company = fields.Many2One('company.company', "Company", required=True)
name = fields.Char("Name", required=True, translate=True)
journal = fields.Many2One(
'account.journal', "Journal", required=True,
context={
'company': Eval('company', -1),
},
depends={'company'})
account = fields.Many2One(
'account.account', "Account", required=True,
domain=[
('type', '!=', None),
('closed', '!=', True),
('company', '=', Eval('company', -1)),
])
@classmethod
def default_company(cls):
return Transaction().context.get('company')
# TODO: wizard to create an invoice from sale
|