__init__.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. # -*- coding: utf-8 -*-
  2. from account_bank_statement import create_bank_statement, create_bank_statement_lines
  3. from account_invoice import create_invoice, prepare_invoice, validate_invoice, close_invoice
  4. from account_journal import get_journals, get_currencies_from_journal
  5. from account_move import create_invoice_move_lines, create_account_move
  6. from account_payment_term import get_payment_terms
  7. from account_voucher import create_account_voucher
  8. from http_response import make_gzip_response
  9. from product_pricelist import get_pricelist_id
  10. from product_template import get_products
  11. from purchase_order import (
  12. get_purchase_orders,
  13. get_purchase_order,
  14. create_purchase_order,
  15. confirm_purchase_order,
  16. done_purchase_order,
  17. create_picking,
  18. force_assign_picking,
  19. done_picking
  20. )
  21. from res_bank_cheque_type import get_cheque_types
  22. from res_bank_payments_type import get_bank_payment_types
  23. from res_bank import get_banks
  24. from res_currency import check_base_currency, get_base_currency
  25. from res_partner import get_suppliers
  26. from res_users import get_current_user
  27. from server_date import get_date
  28. from stock_picking_type import get_picking_types
  29. from stock_picking import get_pickings, confirm_picking
  30. from stock_warehouse import get_warehouses
  31. def get_data(mode=None):
  32. data = {}
  33. check_base_currency()
  34. warehouses = get_warehouses()
  35. location_ids = map(lambda x: x.get('locationStock').get('id'), warehouses)
  36. currencies = get_currencies_from_journal()
  37. if len(currencies) == 0:
  38. currencies = [get_base_currency()]
  39. if mode == 'purchase' or mode == 'expense':
  40. data = {
  41. 'date': get_date(),
  42. 'user': get_current_user(),
  43. 'currencies': currencies,
  44. 'journals': get_journals(),
  45. 'suppliers': get_suppliers(),
  46. 'products': get_products(mode, location_ids),
  47. 'pickingTypes': get_picking_types(),
  48. 'paymentTerms': get_payment_terms(),
  49. 'banks': get_banks(),
  50. 'bankPaymentTypes': get_bank_payment_types(),
  51. 'chequeTypes': get_cheque_types(),
  52. 'warehouses': warehouses
  53. }
  54. if mode == 'product_picking':
  55. data = {
  56. 'date': get_date(),
  57. 'user': get_current_user(),
  58. 'currencies': currencies,
  59. 'suppliers': get_suppliers(),
  60. 'products': get_products('purchase', location_ids),
  61. 'warehouses': warehouses
  62. }
  63. if mode == 'payment':
  64. data = {
  65. 'purchaseOrders': get_purchase_orders(),
  66. 'currencies': currencies,
  67. 'suppliers': get_suppliers(),
  68. 'journals': get_journals(),
  69. 'paymentTerms': get_payment_terms(),
  70. 'banks': get_banks(),
  71. 'bankPaymentTypes': get_bank_payment_types(),
  72. 'chequeTypes': get_cheque_types(),
  73. 'warehouses': warehouses
  74. }
  75. if mode == 'product_taking':
  76. data = {
  77. 'stockPickings': get_pickings()
  78. }
  79. return make_gzip_response(data)
  80. def process_data(data=None):
  81. if not data:
  82. return {}
  83. def drafting_order(vals={}):
  84. currency_id = vals.get('currency_id', None)
  85. supplier_id = vals.get('supplier_id', None)
  86. cart_items = vals.get(cart_items, [])
  87. date_now = vals.get('date_now', None)
  88. payment_term_id = vals.get('payment_term_id', None)
  89. warehouse_id = vals.get('warehouse_id', None)
  90. user_id = vals.get('user_id', None)
  91. pricelist_id = get_pricelist_id(currency_id)
  92. order = create_purchase_order(supplier_id, cart_items, date_now, currency_id, pricelist_id, payment_term_id, warehouse_id, user_id)
  93. return order.id, currency_id
  94. def making_order(order_id):
  95. if not order_id:
  96. return None
  97. order = get_purchase_order(order_id)
  98. confirm_purchase_order(order.id)
  99. return order.id
  100. def paying_order(vals={}):
  101. order_id = vals.get('order_id', None)
  102. cart_items = vals.get('cart_items', [])
  103. currency_id = vals.get('currency_id', None)
  104. supplier_id = vals.get('supplier_id', None)
  105. supplier_invoice_number = vals.get('supplier_invoice_number', None)
  106. date_now = vals.get('date_now', None)
  107. payment_term_id = vals.get('payment_term_id', None)
  108. user_id = vals.get('user_id', None)
  109. mode = vals.get('mode', None)
  110. order_name = None
  111. order_lines = None
  112. if order_id:
  113. order = get_purchase_order(order_id)
  114. order_name = order.name
  115. order_lines = [
  116. {
  117. 'name': l.name,
  118. 'id': l.product_id.id,
  119. 'quantity': l.product_qty,
  120. 'price': l.price_unit
  121. } for l in order.order_line
  122. ]
  123. supplier_id = order.partner_id.id
  124. currency_id = order.currency_id.id
  125. else:
  126. order_lines = [
  127. {
  128. 'name': item.get('name'),
  129. 'id': item.get('id'),
  130. 'quantity': item.get('quantity'),
  131. 'price': item.get('price')
  132. } for item in cart_items
  133. ]
  134. invoice = create_invoice(order_name, supplier_id, order_lines, currency_id, payment_term_id, supplier_invoice_number, user_id)
  135. invoice_ids = invoice.mapped(lambda x: x.id)
  136. if order_id:
  137. order.write({
  138. 'invoice_ids': [[4, invoice_ids, False]],
  139. 'invoiced': True
  140. })
  141. prepare_invoice(invoice_ids, currency_id, date_now)
  142. move_lines = create_invoice_move_lines(invoice_ids, payment, date_now)
  143. account_move = create_account_move(invoice_ids, move_lines)
  144. validate_invoice(invoice_ids, mode)
  145. account_voucher = create_account_voucher(account_move.id, journal_id, currency_id, payment)
  146. close_invoice(invoice_ids)
  147. bank_statement = invoice.create_bank_statement(date_now, user_id, account_voucher, None)
  148. if order_id and len(order.picking_ids) == 0:
  149. create_picking(order_id)
  150. return bank_statement.id
  151. def complete_order(order_id):
  152. if not order_id:
  153. return None
  154. force_assign_picking(order_id)
  155. done_picking(order_id)
  156. date_now = get_date()
  157. mode = data.get('mode')
  158. currency_id = data.get('currencyId')
  159. journal_id = data.get('journalId')
  160. supplier_id = data.get('supplierId')
  161. supplier_invoice_number = data.get('supplierInvoiceNumber', None)
  162. cart_items = data.get('items')
  163. payment_term_id = data.get('paymentTermId')
  164. warehouse_id = data.get('warehouseId', None)
  165. user_id = data.get('userId', None)
  166. payment = float(data.get('payment', 0.0))
  167. if mode == 'purchase':
  168. order_id, currency_id = drafting_order({
  169. 'currency_id': currency_id,
  170. 'supplier_id': supplier_id,
  171. 'cart_items': cart_items,
  172. 'date_now': date_now,
  173. 'payment_term_id': payment_term_id,
  174. 'warehouse_id': warehouse_id,
  175. 'user_id': user_id
  176. })
  177. confirm_purchase_order(order_id)
  178. paying_order({
  179. 'order_id': order_id,
  180. 'supplier_invoice_number': supplier_invoice_number,
  181. 'date_now': date_now,
  182. 'payment_term_id': payment_term_id,
  183. 'user_id': user_id,
  184. 'mode': mode
  185. })
  186. done_purchase_order(order_id)
  187. if mode == 'expense':
  188. paying_order({
  189. 'supplier_id': supplier_id,
  190. 'cart_items': cart_items,
  191. 'currency_id': currency_id,
  192. 'supplier_invoice_number': supplier_invoice_number,
  193. 'date_now': date_now,
  194. 'payment_term_id': payment_term_id,
  195. 'user_id': user_id,
  196. 'mode': mode
  197. })
  198. if mode == 'product_picking':
  199. order_id, currency_id = drafting_order({
  200. 'currency_id': currency_id,
  201. 'supplier_id': supplier_id,
  202. 'cart_items': cart_items,
  203. 'date_now': date_now,
  204. 'payment_term_id': payment_term_id,
  205. 'warehouse_id': warehouse_id,
  206. 'user_id': user_id
  207. })
  208. confirm_purchase_order(order_id)
  209. if mode == 'payment':
  210. order_id = data.get('purchaseOrderId', None)
  211. if not order_id:
  212. return
  213. paying_order({
  214. 'order_id': order_id,
  215. 'supplier_invoice_number': supplier_invoice_number,
  216. 'date_now': date_now,
  217. 'payment_term_id': payment_term_id,
  218. 'user_id': user_id,
  219. 'mode': mode
  220. })
  221. done_purchase_order(order_id)
  222. if mode == 'product_taking':
  223. stock_picking_id = data.get('stockPickingId')
  224. confirm_picking(stock_picking_id)
  225. return {
  226. 'status': 'ok'
  227. }