123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- # -*- coding: utf-8 -*-
- from account_bank_statement import create_bank_statement, create_bank_statement_lines
- from account_invoice import create_invoice, prepare_invoice, validate_invoice, close_invoice
- from account_journal import get_journals, get_currencies_from_journal
- from account_move import create_invoice_move_lines, create_account_move
- from account_payment_term import get_payment_terms
- from account_voucher import create_account_voucher
- from http_response import make_gzip_response
- from product_pricelist import get_pricelist_id
- from product_template import get_products
- from purchase_order import (
- get_purchase_orders,
- get_purchase_order,
- create_purchase_order,
- confirm_purchase_order,
- done_purchase_order,
- create_picking,
- force_assign_picking,
- done_picking
- )
- from res_bank_cheque_type import get_cheque_types
- from res_bank_payments_type import get_bank_payment_types
- from res_bank import get_banks
- from res_currency import check_base_currency, get_base_currency
- from res_partner import get_suppliers
- from res_users import get_current_user
- from server_date import get_date
- from stock_picking_type import get_picking_types
- from stock_picking import get_pickings, confirm_picking
- from stock_warehouse import get_warehouses
- def get_data(mode=None):
- data = {}
- check_base_currency()
- warehouses = get_warehouses()
- location_ids = map(lambda x: x.get('locationStock').get('id'), warehouses)
- currencies = get_currencies_from_journal()
- if len(currencies) == 0:
- currencies = [get_base_currency()]
- if mode == 'purchase' or mode == 'expense':
- data = {
- 'date': get_date(),
- 'user': get_current_user(),
- 'currencies': currencies,
- 'journals': get_journals(),
- 'suppliers': get_suppliers(),
- 'products': get_products(mode, location_ids),
- 'pickingTypes': get_picking_types(),
- 'paymentTerms': get_payment_terms(),
- 'banks': get_banks(),
- 'bankPaymentTypes': get_bank_payment_types(),
- 'chequeTypes': get_cheque_types(),
- 'warehouses': warehouses
- }
- if mode == 'product_picking':
- data = {
- 'date': get_date(),
- 'user': get_current_user(),
- 'currencies': currencies,
- 'suppliers': get_suppliers(),
- 'products': get_products('purchase', location_ids),
- 'warehouses': warehouses
- }
- if mode == 'payment':
- data = {
- 'purchaseOrders': get_purchase_orders(),
- 'currencies': currencies,
- 'suppliers': get_suppliers(),
- 'journals': get_journals(),
- 'paymentTerms': get_payment_terms(),
- 'banks': get_banks(),
- 'bankPaymentTypes': get_bank_payment_types(),
- 'chequeTypes': get_cheque_types(),
- 'warehouses': warehouses
- }
-
- if mode == 'product_taking':
- data = {
- 'stockPickings': get_pickings()
- }
- return make_gzip_response(data)
- def process_data(data=None):
- if not data:
- return {}
- def drafting_order(vals={}):
- currency_id = vals.get('currency_id', None)
- supplier_id = vals.get('supplier_id', None)
- cart_items = vals.get(cart_items, [])
- date_now = vals.get('date_now', None)
- payment_term_id = vals.get('payment_term_id', None)
- warehouse_id = vals.get('warehouse_id', None)
- user_id = vals.get('user_id', None)
-
- pricelist_id = get_pricelist_id(currency_id)
- order = create_purchase_order(supplier_id, cart_items, date_now, currency_id, pricelist_id, payment_term_id, warehouse_id, user_id)
- return order.id, currency_id
- def making_order(order_id):
- if not order_id:
- return None
- order = get_purchase_order(order_id)
- confirm_purchase_order(order.id)
- return order.id
- def paying_order(vals={}):
- order_id = vals.get('order_id', None)
- cart_items = vals.get('cart_items', [])
- currency_id = vals.get('currency_id', None)
- supplier_id = vals.get('supplier_id', None)
- supplier_invoice_number = vals.get('supplier_invoice_number', None)
- date_now = vals.get('date_now', None)
- payment_term_id = vals.get('payment_term_id', None)
- user_id = vals.get('user_id', None)
- mode = vals.get('mode', None)
- order_name = None
- order_lines = None
- if order_id:
- order = get_purchase_order(order_id)
-
- order_name = order.name
- order_lines = [
- {
- 'name': l.name,
- 'id': l.product_id.id,
- 'quantity': l.product_qty,
- 'price': l.price_unit
- } for l in order.order_line
- ]
- supplier_id = order.partner_id.id
- currency_id = order.currency_id.id
- else:
- order_lines = [
- {
- 'name': item.get('name'),
- 'id': item.get('id'),
- 'quantity': item.get('quantity'),
- 'price': item.get('price')
- } for item in cart_items
- ]
- invoice = create_invoice(order_name, supplier_id, order_lines, currency_id, payment_term_id, supplier_invoice_number, user_id)
- invoice_ids = invoice.mapped(lambda x: x.id)
- if order_id:
- order.write({
- 'invoice_ids': [[4, invoice_ids, False]],
- 'invoiced': True
- })
- prepare_invoice(invoice_ids, currency_id, date_now)
- move_lines = create_invoice_move_lines(invoice_ids, payment, date_now)
- account_move = create_account_move(invoice_ids, move_lines)
- validate_invoice(invoice_ids, mode)
- account_voucher = create_account_voucher(account_move.id, journal_id, currency_id, payment)
- close_invoice(invoice_ids)
- bank_statement = invoice.create_bank_statement(date_now, user_id, account_voucher, None)
- if order_id and len(order.picking_ids) == 0:
- create_picking(order_id)
- return bank_statement.id
-
- def complete_order(order_id):
- if not order_id:
- return None
- force_assign_picking(order_id)
- done_picking(order_id)
- date_now = get_date()
- mode = data.get('mode')
- currency_id = data.get('currencyId')
- journal_id = data.get('journalId')
- supplier_id = data.get('supplierId')
- supplier_invoice_number = data.get('supplierInvoiceNumber', None)
- cart_items = data.get('items')
- payment_term_id = data.get('paymentTermId')
- warehouse_id = data.get('warehouseId', None)
- user_id = data.get('userId', None)
- payment = float(data.get('payment', 0.0))
- if mode == 'purchase':
- order_id, currency_id = drafting_order({
- 'currency_id': currency_id,
- 'supplier_id': supplier_id,
- 'cart_items': cart_items,
- 'date_now': date_now,
- 'payment_term_id': payment_term_id,
- 'warehouse_id': warehouse_id,
- 'user_id': user_id
- })
- confirm_purchase_order(order_id)
- paying_order({
- 'order_id': order_id,
- 'supplier_invoice_number': supplier_invoice_number,
- 'date_now': date_now,
- 'payment_term_id': payment_term_id,
- 'user_id': user_id,
- 'mode': mode
- })
- done_purchase_order(order_id)
- if mode == 'expense':
- paying_order({
- 'supplier_id': supplier_id,
- 'cart_items': cart_items,
- 'currency_id': currency_id,
- 'supplier_invoice_number': supplier_invoice_number,
- 'date_now': date_now,
- 'payment_term_id': payment_term_id,
- 'user_id': user_id,
- 'mode': mode
- })
- if mode == 'product_picking':
- order_id, currency_id = drafting_order({
- 'currency_id': currency_id,
- 'supplier_id': supplier_id,
- 'cart_items': cart_items,
- 'date_now': date_now,
- 'payment_term_id': payment_term_id,
- 'warehouse_id': warehouse_id,
- 'user_id': user_id
- })
- confirm_purchase_order(order_id)
- if mode == 'payment':
- order_id = data.get('purchaseOrderId', None)
- if not order_id:
- return
- paying_order({
- 'order_id': order_id,
- 'supplier_invoice_number': supplier_invoice_number,
- 'date_now': date_now,
- 'payment_term_id': payment_term_id,
- 'user_id': user_id,
- 'mode': mode
- })
- done_purchase_order(order_id)
- if mode == 'product_taking':
- stock_picking_id = data.get('stockPickingId')
- confirm_picking(stock_picking_id)
- return {
- 'status': 'ok'
- }
|