main.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. 'defaultCreditAccount': {
  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. 'defaultDebitAccount': {
  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']), ('default_debit_account_id.currency_id', '=', False), ('active', '=', True)], order='id')]
  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. 'listPrice': product.list_price,
  128. 'variantCount': product.product_variant_count,
  129. 'variants': [{
  130. 'id': variant.id,
  131. 'name': variant.name,
  132. 'displayName': variant.display_name,
  133. 'ean13': variant.ean13,
  134. 'imageMedium': variant.image_medium,
  135. 'listPrice': variant.list_price
  136. } for variant in product.product_variant_ids if variant.active]
  137. } for product in request.env['product.template'].search([('purchase_ok', '=', True), ('active', '=', True)])]
  138. '''
  139. Get all incoming and active picking types
  140. '''
  141. def get_picking_types(self):
  142. return [{
  143. 'id': picking_type.id,
  144. 'name': picking_type.name,
  145. 'displayName': picking_type.display_name
  146. } for picking_type in request.env['stock.picking.type'].search([('code', '=', 'incoming'), ('active', '=', True)])]
  147. '''
  148. Get all active payment terms
  149. '''
  150. def get_payment_terms(self):
  151. return [{
  152. 'id': payment_term.id,
  153. 'name': payment_term.name,
  154. 'displayName': payment_term.display_name,
  155. 'lines': [{
  156. 'id': line.id,
  157. 'days': line.days,
  158. 'days2': line.days2,
  159. 'value': line.value,
  160. 'value_amount': line.value_amount
  161. } for line in payment_term.line_ids]
  162. } for payment_term in request.env['account.payment.term'].search([('active', '=', True)])]
  163. '''
  164. Make JSON response to send
  165. '''
  166. def make_response(self, data, status=200):
  167. return Response(json.dumps(data), status=status, content_type='application/json')
  168. '''
  169. '''
  170. def make_info_log(self, log):
  171. LOGGER.info(log)
  172. '''
  173. New purchase resource route
  174. '''
  175. @http.route('/eiru_purchases/new', auth='user', methods=['GET'], cors='*')
  176. def new_purchase(self, **kw):
  177. self.make_info_log('sending json response')
  178. return self.make_response({
  179. 'date': self.get_server_date(),
  180. 'user': self.get_user(),
  181. 'currencies': self.get_currencies(),
  182. 'journals': self.get_journals(),
  183. 'suppliers': self.get_suppliers(),
  184. 'products': self.get_products(),
  185. 'pickingTypes': self.get_picking_types(),
  186. 'paymentTerms': self.get_payment_terms()
  187. })
  188. '''
  189. Create supplier and return data
  190. '''
  191. @http.route('/eiru_purchases/create_supplier', auth='user', methods=['POST'], cors='*')
  192. def create_supplier(self, **kw):
  193. self.make_info_log('creating supplier')
  194. print(kw)
  195. '''
  196. Create product and return data
  197. '''
  198. @http.route('/eiru_purchases/create_product', auth='user', methods=['POST'], cors='*')
  199. def create_product(self, **kw):
  200. self.make_info_log('creating product')
  201. print(kw)
  202. '''
  203. Create purchase resource route
  204. '''
  205. @http.route('/eiru_purchases/create', auth='user', methods=['POST'], cors='*')
  206. def create_purchase(self, **kw):
  207. pass