main.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. # -*- coding: utf-8 -*-
  2. from openerp import http
  3. from openerp.http import request
  4. from werkzeug.wrappers import Response
  5. from datetime import datetime
  6. import simplejson as json
  7. import logging
  8. LOGGER = logging.getLogger(__name__)
  9. class Purchases(http.Controller):
  10. '''
  11. Get server date to send
  12. '''
  13. def get_server_date(self):
  14. return datetime.now().strftime('%Y-%m-%d')
  15. '''
  16. Get current user information
  17. '''
  18. def get_user(self):
  19. user = request.env.user
  20. return {
  21. 'id': user.id,
  22. 'name': user.name,
  23. 'displayName': user.display_name,
  24. 'currency': {
  25. 'id': user.company_id.currency_id.id,
  26. 'name': user.company_id.currency_id.name,
  27. 'displayName': user.company_id.currency_id.display_name,
  28. 'symbol': user.company_id.currency_id.symbol
  29. }
  30. }
  31. def get_currencies(self):
  32. return [{
  33. 'id': currency.id,
  34. 'name': currency.name,
  35. 'displayName': currency.display_name,
  36. 'base': currency.base,
  37. 'accuracy': currency.accuracy,
  38. 'position': currency.position,
  39. 'rate': currency.rate,
  40. 'rounding': currency.rounding,
  41. 'symbol': currency.symbol
  42. } for currency in request.env['res.currency'].search([('active', '=', True)])]
  43. '''
  44. Get all active journals
  45. '''
  46. def get_journals(self):
  47. return [{
  48. 'id': journal.id,
  49. 'name': journal.name,
  50. 'displayName': journal.display_name,
  51. 'code': journal.code,
  52. 'cashControl': journal.cash_control,
  53. 'type': journal.type,
  54. 'currency': {
  55. 'id': journal.currency.id,
  56. 'name': journal.currency.name,
  57. 'displayName': journal.currency.display_name
  58. },
  59. 'creditAccount': {
  60. 'id': journal.default_credit_account_id.id,
  61. 'name': journal.default_credit_account_id.name,
  62. 'displayName': journal.default_credit_account_id.display_name,
  63. 'code': journal.default_credit_account_id.code,
  64. 'exchangeRate': journal.default_credit_account_id.exchange_rate,
  65. 'foreignBalance': journal.default_credit_account_id.foreign_balance,
  66. 'reconcile': journal.default_credit_account_id.reconcile,
  67. 'debit': journal.default_credit_account_id.debit,
  68. 'credit': journal.default_credit_account_id.credit,
  69. 'currencyMode': journal.default_credit_account_id.currency_mode,
  70. 'companyCurrency': {
  71. 'id': journal.default_credit_account_id.company_currency_id.id,
  72. 'name': journal.default_credit_account_id.company_currency_id.name,
  73. 'displayName': journal.default_credit_account_id.company_currency_id.display_name,
  74. },
  75. 'currency': {
  76. 'id': journal.default_credit_account_id.currency_id.id,
  77. 'name': journal.default_credit_account_id.currency_id.name,
  78. 'displayName': journal.default_credit_account_id.currency_id.display_name
  79. },
  80. },
  81. 'debitAccount': {
  82. 'id': journal.default_debit_account_id.id,
  83. 'name': journal.default_debit_account_id.name,
  84. 'displayName': journal.default_debit_account_id.display_name,
  85. 'code': journal.default_debit_account_id.code,
  86. 'exchange_rate': journal.default_credit_account_id.exchange_rate,
  87. 'foreignBalance': journal.default_credit_account_id.foreign_balance,
  88. 'reconcile': journal.default_credit_account_id.reconcile,
  89. 'debit': journal.default_credit_account_id.debit,
  90. 'credit': journal.default_credit_account_id.credit,
  91. 'currencyMode': journal.default_credit_account_id.currency_mode,
  92. 'companyCurrency': {
  93. 'id': journal.default_credit_account_id.company_currency_id.id,
  94. 'name': journal.default_credit_account_id.company_currency_id.name,
  95. 'displayName': journal.default_credit_account_id.company_currency_id.display_name,
  96. },
  97. 'currency': {
  98. 'id': journal.default_credit_account_id.currency_id.id,
  99. 'name': journal.default_credit_account_id.currency_id.name,
  100. 'displayName': journal.default_credit_account_id.currency_id.display_name
  101. }
  102. }
  103. } for journal in request.env['account.journal'].search([('type', 'in', ['bank', 'cash']), ('active', '=', True)])]
  104. '''
  105. Get all active suppliers
  106. '''
  107. def get_suppliers(self):
  108. return [{
  109. 'id': supplier.id,
  110. 'name': supplier.name,
  111. 'displayName': supplier.display_name,
  112. 'imageMedium': supplier.image_medium,
  113. 'phone': supplier.phone,
  114. 'mobile': supplier.mobile,
  115. 'email': supplier.email
  116. } for supplier in request.env['res.partner'].search([('supplier', '=', True), ('active', '=', True)])]
  117. '''
  118. Get all purchasable and active products
  119. '''
  120. def get_products(self):
  121. return [{
  122. 'id': product.id,
  123. 'name': product.name,
  124. 'displayName': product.display_name,
  125. 'ean13': product.ean13,
  126. 'imageMedium': product.image_medium,
  127. 'variantCount': product.product_variant_count,
  128. 'variants': [{
  129. 'id': variant.id,
  130. 'name': variant.name,
  131. 'displayName': variant.display_name,
  132. 'ean13': variant.ean13,
  133. 'imageMedium': variant.image_medium,
  134. 'listPrice': variant.list_price
  135. } for variant in product.product_variant_ids if variant.active]
  136. } for product in request.env['product.template'].search([('purchase_ok', '=', True), ('active', '=', True)])]
  137. '''
  138. Get all incoming and active picking types
  139. '''
  140. def get_picking_types(self):
  141. return [{
  142. 'id': picking_type.id,
  143. 'name': picking_type.name,
  144. 'displayName': picking_type.display_name
  145. } for picking_type in request.env['stock.picking.type'].search([('code', '=', 'incoming'), ('active', '=', True)])]
  146. '''
  147. Get all active payment terms
  148. '''
  149. def get_payment_terms(self):
  150. return [{
  151. 'id': payment_term.id,
  152. 'name': payment_term.name,
  153. 'displayName': payment_term.display_name,
  154. 'lines': [{
  155. 'id': line.id,
  156. 'days': line.days,
  157. 'days2': line.days2,
  158. 'value': line.value,
  159. 'value_amount': line.value_amount
  160. } for line in payment_term.line_ids]
  161. } for payment_term in request.env['account.payment.term'].search([('active', '=', True)])]
  162. '''
  163. Make JSON response to send
  164. '''
  165. def make_response(self, data, status=200):
  166. return Response(json.dumps(data), status=status, content_type='application/json')
  167. '''
  168. '''
  169. def make_info_log(self, log):
  170. LOGGER.info(log)
  171. '''
  172. New purchase resource route
  173. '''
  174. @http.route('/eiru_purchases/new', auth='user', methods=['GET'], cors='*')
  175. def new_purchase(self, **kw):
  176. self.make_info_log('sending json response')
  177. return self.make_response({
  178. 'date': self.get_server_date(),
  179. 'user': self.get_user(),
  180. 'currencies': self.get_currencies(),
  181. 'journals': self.get_journals(),
  182. 'suppliers': self.get_suppliers(),
  183. 'products': self.get_products(),
  184. 'pickingTypes': self.get_picking_types(),
  185. 'paymentTerms': self.get_payment_terms()
  186. })
  187. '''
  188. Create purchase resource route
  189. '''
  190. @http.route('/eiru_purchases/create', auth='user', methods=['POST'], cors='*')
  191. def create_purchase(self, **kw):
  192. pass