main.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. # -*- coding: utf-8 -*-
  2. from openerp import http
  3. from openerp.http import request as r
  4. from helpers import (
  5. get_date,
  6. get_current_user,
  7. get_currencies_from_journal,
  8. get_journals,
  9. get_suppliers,
  10. get_products,
  11. get_picking_types,
  12. get_payment_terms,
  13. get_banks,
  14. get_bank_payment_types,
  15. get_cheque_types,
  16. get_pickings,
  17. make_gzip_response
  18. )
  19. import logging
  20. LOGGER = logging.getLogger(__name__)
  21. class Purchases(http.Controller):
  22. '''
  23. '''
  24. def make_info_log(self, log):
  25. LOGGER.info('\033[1;34m[INFO] --> \033[m{}'.format(log))
  26. '''
  27. New purchase resource route
  28. '''
  29. @http.route('/eiru_purchases/init', auth='user', methods=['GET'], cors='*')
  30. def init_purchase(self, **kw):
  31. self.make_info_log('Preparing data to {}'.format(kw.get('mode')))
  32. mode = kw.get('mode', 'unknown')
  33. data = None
  34. if mode == 'purchase' or mode == 'expense':
  35. data = {
  36. 'date': get_date(),
  37. 'user': get_current_user(),
  38. 'currencies': get_currencies_from_journal(),
  39. 'journals': get_journals(),
  40. 'suppliers': get_suppliers(),
  41. 'products': get_products(kw.get('mode')),
  42. 'pickingTypes': get_picking_types(),
  43. 'paymentTerms': get_payment_terms(),
  44. 'banks': get_banks(),
  45. 'bankPaymentTypes': get_bank_payment_types(),
  46. 'chequeTypes': get_cheque_types()
  47. }
  48. if mode == 'product_picking':
  49. data = {
  50. 'date': get_date(),
  51. 'user': get_current_user(),
  52. 'currencies': get_currencies_from_journal(),
  53. 'suppliers': get_suppliers(),
  54. 'products': get_products()
  55. }
  56. if mode == 'payment':
  57. data = {
  58. 'currencies': get_currencies_from_journal(),
  59. 'suppliers': get_suppliers(),
  60. 'journals': get_journals(),
  61. 'paymentTerms': get_payment_terms(),
  62. 'banks': get_banks(),
  63. 'bankPaymentTypes': get_bank_payment_types(),
  64. 'chequeTypes': get_cheque_types()
  65. }
  66. if mode == 'product_taking':
  67. data = {
  68. 'stockPickings': get_pickings()
  69. }
  70. return make_gzip_response(data)
  71. # '''
  72. # Create supplier and return data
  73. # '''
  74. # @http.route('/eiru_purchases/create_supplier', type='json', auth='user', methods=['POST'], cors='*')
  75. # def create_supplier(self, **kw):
  76. # self.make_info_log('Creating supplier')
  77. # '''
  78. # Create product and return data
  79. # '''
  80. # @http.route('/eiru_purchases/create_product', type='json', auth='user', methods=['POST'], cors='*')
  81. # def create_product(self, **kw):
  82. # self.make_info_log('Creating product')
  83. # '''
  84. # Number to invoice
  85. # '''
  86. # def validate_invoice(self, invoice_ids, type=None):
  87. # assert len(invoice_ids) == 1
  88. # invoice = request.env['account.invoice'].browse(invoice_ids)
  89. # invoice.action_number()
  90. # invoice.invoice_validate()
  91. # if type != 'purchase':
  92. # name = 'GASTO' + invoice.name[invoice.name.index('/'):]
  93. # invoice.write({
  94. # 'number': name,
  95. # 'internal_number': name
  96. # })
  97. # '''
  98. # Create voucher
  99. # '''
  100. # def create_account_voucher(self, account_move_id, journal_id, currency_id, paid_amount):
  101. # account_move = request.env['account.move'].browse(account_move_id)
  102. # account_journal = request.env['account.journal'].browse(journal_id)
  103. # account_voucher = request.env['account.voucher'].create({
  104. # 'reference': account_move.name,
  105. # 'type': 'payment',
  106. # 'journal_id': account_journal.id,
  107. # 'company_id': account_move.company_id.id,
  108. # 'pre_line': True,
  109. # 'amount': paid_amount,
  110. # 'period_id': account_move.period_id.id,
  111. # 'date': account_move.date,
  112. # 'partner_id': account_move.partner_id.id,
  113. # 'account_id': account_journal.default_debit_account_id.id,
  114. # 'currency_id': currency_id,
  115. # 'line_dr_ids': [[0, False, {
  116. # 'date_due': l.date_maturity,
  117. # 'account_id': l.account_id.id,
  118. # 'date_original': l.invoice.date_invoice,
  119. # 'move_line_id': l.id,
  120. # 'amount_original': abs(l.credit or l.debit or 0.0),
  121. # 'amount_unreconciled': abs(l.amount_residual),
  122. # 'amount': abs(l.credit) if account_move.date == l.date_maturity else 0.0,
  123. # 'reconcile': account_move.date == l.date_maturity,
  124. # 'currency_id': currency_id
  125. # }] for l in account_move.line_id]
  126. # })
  127. # account_voucher.action_move_line_create()
  128. # return account_voucher
  129. # '''
  130. # Close a invoice
  131. # '''
  132. # def close_invoice(self, invoice_ids):
  133. # assert len(invoice_ids) == 1
  134. # invoice = request.env['account.invoice'].browse(invoice_ids)
  135. # if invoice.residual == 0:
  136. # invoice.write({
  137. # 'state': 'paid'
  138. # })
  139. # '''
  140. # Create account bank statement
  141. # '''
  142. # def create_bank_statement(self, account_voucher_id, account_bank_statement_lines, date_today):
  143. # account_voucher = request.env['account.voucher'].browse(account_voucher_id)
  144. # account_bank_statement = request.env['account.bank.statement'].search([('journal_id', '=', account_voucher.journal_id.id), ('date', '=', date_today)])
  145. # account_bank_statement_values = {
  146. # 'date': date_today,
  147. # 'user_id': request.env.user.id,
  148. # 'journal_id': account_voucher.journal_id.id,
  149. # 'period_id': account_voucher.period_id.id,
  150. # 'line_ids': account_bank_statement_lines,
  151. # 'state': 'open' if account_voucher.journal_id.type == 'cash' else 'draft'
  152. # }
  153. # if account_bank_statement:
  154. # size = len(account_bank_statement)
  155. # if size == 1:
  156. # account_bank_statement.write(account_bank_statement_values)
  157. # else:
  158. # account_bank_statement[size - 1].write(account_bank_statement_values)
  159. # else:
  160. # account_bank_statement.create(account_bank_statement_values)
  161. # return account_bank_statement
  162. # '''
  163. # Create account bank statement lines
  164. # '''
  165. # def create_bank_statement_lines(self, account_voucher_id, reference=None):
  166. # account_voucher = request.env['account.voucher'].browse(account_voucher_id)
  167. # amount = account_voucher.amount
  168. # if account_voucher.type == 'payment':
  169. # amount = amount * -1
  170. # return [[0, False, {
  171. # 'name': account_voucher.reference,
  172. # 'amount': amount,
  173. # 'partner_id': account_voucher.partner_id.id,
  174. # 'voucher_id': account_voucher.id,
  175. # 'journal_id': account_voucher.journal_id.id,
  176. # 'account_id': account_voucher.account_id.id,
  177. # 'journal_entry_id': account_voucher.move_id.id,
  178. # 'currency_id': account_voucher.currency_id.id,
  179. # 'ref': 'POS/' + (reference or '')
  180. # }]]
  181. # '''
  182. # Purchase processing resource route
  183. # '''
  184. # @http.route('/eiru_purchases/process', type='json', auth='user', methods=['POST'], cors='*')
  185. # def process_purchase(self, **kw):
  186. # mode = kw.get('mode')
  187. # self.make_info_log('Processing {}'.format(mode))
  188. # # Get date
  189. # date_now = datetime.now(self.get_timezone()).strftime(DATE_FORMAT)
  190. # self.make_info_log('Getting date')
  191. # # Get currency
  192. # currency_id = self.get_currency_id(kw.get('journalId'))
  193. # self.make_info_log('Getting currency')
  194. # # Get pricelist
  195. # pricelist_id = self.get_pricelist_id(currency_id)
  196. # self.make_info_log('Product pricelist checked')
  197. # invoice = None
  198. # if mode == 'purchase':
  199. # # Create purchase order
  200. # purchase_order = self.create_purchase_order(kw.get('supplierId'), kw.get('items'), date_now, currency_id, pricelist_id, kw.get('paymentTermId'))
  201. # self.make_info_log('Purchase order created')
  202. # # Confirm purchase
  203. # self.confirm_purchase_order(purchase_order.id)
  204. # self.make_info_log('Purchase order confirmed')
  205. # invoice = purchase_order.invoice_ids
  206. # invoice.write({
  207. # 'supplier_invoice_number': kw.get('supplierInvoiceNumber', None)
  208. # })
  209. # else:
  210. # invoice = self.create_invoice(kw.get('supplierId'), kw.get('items'), currency_id, kw.get('paymentTermId'), kw.get('supplierInvoiceNumber', None))
  211. # self.make_info_log('Invoice created')
  212. # invoice_ids = invoice.mapped(lambda x: x.id)
  213. # # Validate invoice
  214. # self.prepare_invoice(invoice_ids, currency_id, date_now)
  215. # self.make_info_log('Invoice prepared')
  216. # # Create invoice move lines
  217. # invoice_move_lines = self.create_invoice_move_lines(invoice_ids, float(kw.get('payment')), date_now)
  218. # self.make_info_log('Invoice move lines created')
  219. # # Create account move
  220. # account_move = self.create_account_move(invoice_ids, invoice_move_lines)
  221. # self.make_info_log('Account move created')
  222. # # Validate invoice
  223. # self.validate_invoice(invoice_ids, mode)
  224. # self.make_info_log('Invoice validated')
  225. # # Create account voucher
  226. # account_voucher = self.create_account_voucher(account_move.id, kw.get('journalId'), currency_id, float(kw.get('payment')))
  227. # self.make_info_log('Account voucher created')
  228. # # Close invoice
  229. # self.close_invoice(invoice_ids)
  230. # self.make_info_log('Attempt close invoice')
  231. # # Create account bank statement lines
  232. # account_bank_statement_lines = self.create_bank_statement_lines(account_voucher.id)
  233. # self.make_info_log('Bank statement lines created')
  234. # # Create account bank statement
  235. # self.create_bank_statement(account_voucher.id, account_bank_statement_lines, date_now)
  236. # self.make_info_log('Bank statement created')
  237. # return {
  238. # 'status': 'ok'
  239. # }