Browse Source

[ADD] project structure

robert 6 năm trước cách đây
mục cha
commit
4ddf9f2fab

+ 0 - 1
__openerp__.py

@@ -2,7 +2,6 @@
 {
     'name': "Eiru Purchases",
     'author': "Robert Gauto",
-    'website': "http://www.yourcompany.com",
     'category': 'Uncategorized',
     'version': '0.1',
     'depends': [

+ 105 - 1
controllers/helpers/__init__.py

@@ -1,14 +1,118 @@
 # -*- 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 create_purchase_order, confirm_purchase_order
 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
+from res_currency import check_base_currency, get_currency_id
 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
+
+'''
+'''
+def get_data(mode=None):
+    data = {}
+
+    if mode == 'purchase' or mode == 'expense':
+        data = {
+            'date': get_date(),
+            'user': get_current_user(),
+            'currencies': get_currencies_from_journal(),
+            'journals': get_journals(),
+            'suppliers': get_suppliers(),
+            'products': get_products(kw.get('mode')),
+            'pickingTypes': get_picking_types(),
+            'paymentTerms': get_payment_terms(),
+            'banks': get_banks(),
+            'bankPaymentTypes': get_bank_payment_types(),
+            'chequeTypes': get_cheque_types()
+        }
+
+    if mode == 'product_picking':
+        data = {
+            'date': get_date(),
+            'user': get_current_user(),
+            'currencies': get_currencies_from_journal(),
+            'suppliers': get_suppliers(),
+            'products': get_products()
+        }
+
+    if mode == 'payment':
+        data = {
+            'currencies': get_currencies_from_journal(),
+            'suppliers': get_suppliers(),
+            'journals': get_journals(),
+            'paymentTerms': get_payment_terms(),
+            'banks': get_banks(),
+            'bankPaymentTypes': get_bank_payment_types(),
+            'chequeTypes': get_cheque_types()
+        }
+    
+    if mode == 'product_taking':
+        data = {
+            'stockPickings': get_pickings()
+        }
+
+    return make_gzip_response(data)
+
+
+'''
+'''
+def store_data(data=None):
+    if not data:
+        return {}
+    
+    date_now = get_date()
+    mode = data.get('mode')
+    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')
+    payment = float(kw.get('payment'))
+
+    currency_id = get_currency_id(journal_id)
+
+    pricelist_id = get_pricelist_id(currency_id)
+
+    invoice = None
+
+    if mode == 'purchase':
+        purchase_order = create_purchase_order(supplier_id, cart_items, date_now, currency_id, pricelist_id, payment_term_id)
+        confirm_purchase_order(purchase_order.id)
+
+        invoice = purchase_order.invoice_ids
+        invoice.write({
+            'supplier_invoice_number': supplier_invoice_number
+        })
+    else:
+        invoice = create_invoice(supplier_id, cart_items, currency_id, payment_term_id, supplier_invoice_number)
+    
+    invoice_ids = invoice.mapped(lambda x: x.id)
+
+    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)
+
+    statement_lines = create_bank_statement_lines(account_voucher.id)
+    create_bank_statement(account_voucher.id, statement_lines, date_now)
+
+    return {
+        'status': 'ok'
+    }

+ 50 - 0
controllers/helpers/account_bank_statement.py

@@ -0,0 +1,50 @@
+# -*- coding: utf-8 -*-
+from openerp.http import request as r
+
+
+def create_bank_statement_lines(account_voucher_id, reference=None):
+    account_voucher = r.env['account.voucher'].browse(account_voucher_id)
+
+    amount = account_voucher.amount
+    
+    if account_voucher.type == 'payment':
+        amount = amount * -1
+
+    return [[0, False, {
+        'name': account_voucher.reference,
+        'amount': amount,
+        'partner_id': account_voucher.partner_id.id,
+        'voucher_id': account_voucher.id,
+        'journal_id': account_voucher.journal_id.id,
+        'account_id': account_voucher.account_id.id,
+        'journal_entry_id': account_voucher.move_id.id,
+        'currency_id': account_voucher.currency_id.id,
+        'ref': 'POS/' + (reference or '')
+    }]]
+
+
+def create_bank_statement(account_voucher_id, account_bank_statement_lines, date_today):
+    account_voucher = r.env['account.voucher'].browse(account_voucher_id)
+    account_bank_statement = r.env['account.bank.statement'].search([('journal_id', '=', account_voucher.journal_id.id), ('date', '=', date_today)])
+
+    values = {
+        'date': date_today,
+        'user_id': r.env.user.id,
+        'journal_id': account_voucher.journal_id.id,
+        'period_id': account_voucher.period_id.id,
+        'line_ids': account_bank_statement_lines,
+        'state': 'open' if account_voucher.journal_id.type == 'cash' else 'draft'
+    }
+
+    if account_bank_statement:
+        size = len(account_bank_statement)
+
+        if size == 1:
+            account_bank_statement.write(values)
+        else:
+            account_bank_statement[size - 1].write(values)
+    else:
+        account_bank_statement.create(values)
+
+    return account_bank_statement
+    

+ 29 - 7
controllers/helpers/account_invoice.py

@@ -1,13 +1,8 @@
 # -*- coding: utf-8 -*-
 from openerp.http import request as r
 
-def create_invoice(
-    supplier_id, 
-    cart_items, 
-    currency_id, 
-    payment_term_id=None, 
-    supplier_invoice_number=None
-):
+
+def create_invoice(supplier_id, cart_items, currency_id, payment_term_id=None, supplier_invoice_number=None):
     partner = r.env['res.partner'].browse(supplier_id)
     journal = r.env['account.journal'].search([('type', 'in', ['purchase']), ('company_id', '=', partner.company_id.id)])
 
@@ -42,3 +37,30 @@ def prepare_invoice(invoice_ids, currency_id, date_today):
         'date_due': date_due.strftime(DATE_FORMAT),
         'state': 'open'
     })
+
+
+def validate_invoice(invoice_ids, type=None):
+    assert len(invoice_ids) == 1
+
+    invoice = r.env['account.invoice'].browse(invoice_ids)
+
+    invoice.action_number()
+    invoice.invoice_validate()
+
+    if type != 'purchase':
+        name = 'GASTO' + invoice.name[invoice.name.index('/'):]
+        invoice.write({
+            'number': name,
+            'internal_number': name
+        })
+
+
+def close_invoice(invoice_ids):
+    assert len(invoice_ids) == 1
+
+    invoice = r.env['account.invoice'].browse(invoice_ids)
+
+    if invoice.residual == 0:
+        invoice.write({
+            'state': 'paid'
+        })

+ 2 - 0
controllers/helpers/account_journal.py

@@ -1,6 +1,7 @@
 # -*- coding: utf-8 -*-
 from openerp.http import request as r
 
+
 def get_currencies_from_journal():
     domain = [
         ('type', 'in', ['bank', 'cash']), 
@@ -27,6 +28,7 @@ def get_currencies_from_journal():
     
     return {c['id']:c for c in currencies}.values()
 
+
 def get_journals():
     domain = [
         ('type', 'in', ['bank', 'cash']), 

+ 37 - 0
controllers/helpers/account_voucher.py

@@ -0,0 +1,37 @@
+# -*- coding: utf-8 -*-
+from openerp.http import request as r
+
+
+def create_account_voucher(account_move_id, journal_id, currency_id, paid_amount):
+    account_move = r.env['account.move'].browse(account_move_id)
+    account_journal = r.env['account.journal'].browse(journal_id)
+
+    values = {
+        'reference': account_move.name,
+        'type': 'payment',
+        'journal_id': account_journal.id,
+        'company_id': account_move.company_id.id,
+        'pre_line': True,
+        'amount': paid_amount,
+        'period_id': account_move.period_id.id,
+        'date': account_move.date,
+        'partner_id': account_move.partner_id.id,
+        'account_id': account_journal.default_debit_account_id.id,
+        'currency_id': currency_id,
+        'line_dr_ids': [[0, False, {
+            'date_due': l.date_maturity,
+            'account_id': l.account_id.id,
+            'date_original': l.invoice.date_invoice,
+            'move_line_id': l.id,
+            'amount_original': abs(l.credit or l.debit or 0.0),
+            'amount_unreconciled': abs(l.amount_residual),
+            'amount': abs(l.credit) if account_move.date == l.date_maturity else 0.0,
+            'reconcile': account_move.date == l.date_maturity,
+            'currency_id': currency_id
+        }] for l in account_move.line_id]
+    }
+
+    account_voucher = r.env['account.voucher'].create(values)
+    account_voucher.action_move_line_create()
+
+    return account_voucher

+ 1 - 1
controllers/helpers/product_template.py

@@ -70,4 +70,4 @@ def create_product(self, values):
             'minimumPrice': p.minimum_price,
             'maximumPrice': p.maximum_price
         } for v in p.product_variant_ids if v.active]
-    }
+    }

+ 1 - 8
controllers/helpers/purchase_order.py

@@ -1,14 +1,7 @@
 # -*- coding: utf-8 -*-
 from openerp.http import request as r
 
-def create_purchase_order(
-    supplier_id, 
-    cart_items, 
-    date_order, 
-    currency_id, 
-    pricelist_id, 
-    payment_term_id=None
-):
+def create_purchase_order(supplier_id, cart_items, date_order, currency_id, pricelist_id, payment_term_id=None):
 
     def get_product_obj(product_id):
         return r.env['product.product'].browse(product_id)

+ 1 - 1
controllers/helpers/res_partner.py

@@ -37,4 +37,4 @@ def create_supplier(values):
         'phone': s.phone,
         'mobile': s.mobile,
         'email': s.email
-    }
+    }

+ 16 - 264
controllers/main.py

@@ -1,82 +1,24 @@
 # -*- coding: utf-8 -*-
 from openerp import http
 from openerp.http import request as r
-from helpers import (
-    get_date,
-    get_current_user,
-    get_currencies_from_journal,
-    get_journals,
-    get_suppliers,
-    get_products,
-    get_picking_types,
-    get_payment_terms,
-    get_banks,
-    get_bank_payment_types,
-    get_cheque_types,
-    get_pickings,
-    make_gzip_response
-)
+from helpers import get_data, store_data
 import logging
 
 LOGGER = logging.getLogger(__name__)
 
-class Purchases(http.Controller):
-    '''
-    '''
-    def make_info_log(self, log):
-        LOGGER.info('\033[1;34m[INFO] --> \033[m{}'.format(log))
-
-    '''
-        New purchase resource route
-    '''
-    @http.route('/eiru_purchases/init', auth='user', methods=['GET'], cors='*')
-    def init_purchase(self, **kw):
-        self.make_info_log('Preparing data to {}'.format(kw.get('mode')))
+def make_info_log(self, log):
+    LOGGER.info('\033[1;34m[INFO] --> \033[m{}'.format(log))
 
-        mode = kw.get('mode', 'unknown')
-        data = None
 
-        if mode == 'purchase' or mode == 'expense':
-            data = {
-            'date': get_date(),
-            'user': get_current_user(),
-            'currencies': get_currencies_from_journal(),
-            'journals': get_journals(),
-            'suppliers': get_suppliers(),
-            'products': get_products(kw.get('mode')),
-            'pickingTypes': get_picking_types(),
-            'paymentTerms': get_payment_terms(),
-            'banks': get_banks(),
-            'bankPaymentTypes': get_bank_payment_types(),
-            'chequeTypes': get_cheque_types()
-        }
+class Purchases(http.Controller):
 
-        if mode == 'product_picking':
-            data = {
-                'date': get_date(),
-                'user': get_current_user(),
-                'currencies': get_currencies_from_journal(),
-                'suppliers': get_suppliers(),
-                'products': get_products()
-            }
 
-        if mode == 'payment':
-            data = {
-                'currencies': get_currencies_from_journal(),
-                'suppliers': get_suppliers(),
-                'journals': get_journals(),
-                'paymentTerms': get_payment_terms(),
-                'banks': get_banks(),
-                'bankPaymentTypes': get_bank_payment_types(),
-                'chequeTypes': get_cheque_types()
-            }
-        
-        if mode == 'product_taking':
-            data = {
-                'stockPickings': get_pickings()
-            }
+    @http.route('/eiru_purchases/init', auth='user', methods=['GET'])
+    def init_purchase(self, **kw):
+        mode = kw.get('mode')
+        make_info_log('Preparing data to {}'.format(mode))
 
-        return make_gzip_response(data)
+        return get_data(mode)
 
     # '''
     #     Create supplier and return data
@@ -86,7 +28,6 @@ class Purchases(http.Controller):
     #     self.make_info_log('Creating supplier')
 
 
-
     # '''
     #     Create product and return data
     # '''
@@ -95,199 +36,10 @@ class Purchases(http.Controller):
     #     self.make_info_log('Creating product')
 
 
-
-    # '''
-    #     Number to invoice
-    # '''
-    # def validate_invoice(self, invoice_ids, type=None):
-    #     assert len(invoice_ids) == 1
-
-    #     invoice = request.env['account.invoice'].browse(invoice_ids)
-
-    #     invoice.action_number()
-    #     invoice.invoice_validate()
-
-    #     if type != 'purchase':
-    #         name = 'GASTO' + invoice.name[invoice.name.index('/'):]
-    #         invoice.write({
-    #             'number': name,
-    #             'internal_number': name
-    #         })
-
-    # '''
-    #     Create voucher
-    # '''
-    # def create_account_voucher(self, account_move_id, journal_id, currency_id, paid_amount):
-    #     account_move = request.env['account.move'].browse(account_move_id)
-    #     account_journal = request.env['account.journal'].browse(journal_id)
-
-    #     account_voucher = request.env['account.voucher'].create({
-    #         'reference': account_move.name,
-    #         'type': 'payment',
-    #         'journal_id': account_journal.id,
-    #         'company_id': account_move.company_id.id,
-    #         'pre_line': True,
-    #         'amount': paid_amount,
-    #         'period_id': account_move.period_id.id,
-    #         'date': account_move.date,
-    #         'partner_id': account_move.partner_id.id,
-    #         'account_id': account_journal.default_debit_account_id.id,
-    #         'currency_id': currency_id,
-    #         'line_dr_ids': [[0, False, {
-    #             'date_due': l.date_maturity,
-    #             'account_id': l.account_id.id,
-    #             'date_original': l.invoice.date_invoice,
-    #             'move_line_id': l.id,
-    #             'amount_original': abs(l.credit or l.debit or 0.0),
-    #             'amount_unreconciled': abs(l.amount_residual),
-    #             'amount': abs(l.credit) if account_move.date == l.date_maturity else 0.0,
-    #             'reconcile': account_move.date == l.date_maturity,
-    #             'currency_id': currency_id
-    #         }] for l in account_move.line_id]
-    #     })
-
-    #     account_voucher.action_move_line_create()
-
-    #     return account_voucher
-
-    # '''
-    #     Close a invoice
-    # '''
-    # def close_invoice(self, invoice_ids):
-    #     assert len(invoice_ids) == 1
-
-    #     invoice = request.env['account.invoice'].browse(invoice_ids)
-
-    #     if invoice.residual == 0:
-    #         invoice.write({
-    #             'state': 'paid'
-    #         })
-
-    # '''
-    #     Create account bank statement
-    # '''
-    # def create_bank_statement(self, account_voucher_id, account_bank_statement_lines, date_today):
-    #     account_voucher = request.env['account.voucher'].browse(account_voucher_id)
-    #     account_bank_statement = request.env['account.bank.statement'].search([('journal_id', '=', account_voucher.journal_id.id), ('date', '=', date_today)])
-
-    #     account_bank_statement_values = {
-    #         'date': date_today,
-    #         'user_id': request.env.user.id,
-    #         'journal_id': account_voucher.journal_id.id,
-    #         'period_id': account_voucher.period_id.id,
-    #         'line_ids': account_bank_statement_lines,
-    #         'state': 'open' if account_voucher.journal_id.type == 'cash' else 'draft'
-    #     }
-
-    #     if account_bank_statement:
-    #         size = len(account_bank_statement)
-
-    #         if size == 1:
-    #             account_bank_statement.write(account_bank_statement_values)
-    #         else:
-    #             account_bank_statement[size - 1].write(account_bank_statement_values)
-    #     else:
-    #         account_bank_statement.create(account_bank_statement_values)
-
-    #     return account_bank_statement
-
-
-    # '''
-    #     Create account bank statement lines
-    # '''
-    # def create_bank_statement_lines(self, account_voucher_id, reference=None):
-    #     account_voucher = request.env['account.voucher'].browse(account_voucher_id)
-
-    #     amount = account_voucher.amount
-        
-    #     if account_voucher.type == 'payment':
-    #         amount = amount * -1
-
-    #     return [[0, False, {
-    #         'name': account_voucher.reference,
-    #         'amount': amount,
-    #         'partner_id': account_voucher.partner_id.id,
-    #         'voucher_id': account_voucher.id,
-    #         'journal_id': account_voucher.journal_id.id,
-    #         'account_id': account_voucher.account_id.id,
-    #         'journal_entry_id': account_voucher.move_id.id,
-    #         'currency_id': account_voucher.currency_id.id,
-    #         'ref': 'POS/' + (reference or '')
-    #     }]]
-
-    # '''
-    #     Purchase processing resource route
-    # ''' 
-    # @http.route('/eiru_purchases/process', type='json', auth='user', methods=['POST'], cors='*')
-    # def process_purchase(self, **kw):
-    #     mode = kw.get('mode')
-    #     self.make_info_log('Processing {}'.format(mode))
-
-    #     # Get date
-    #     date_now = datetime.now(self.get_timezone()).strftime(DATE_FORMAT)
-    #     self.make_info_log('Getting date')
-
-    #     # Get currency
-    #     currency_id = self.get_currency_id(kw.get('journalId'))
-    #     self.make_info_log('Getting currency')
-
-    #     # Get pricelist
-    #     pricelist_id = self.get_pricelist_id(currency_id)
-    #     self.make_info_log('Product pricelist checked')
-
-    #     invoice = None
-
-    #     if mode == 'purchase':
-    #         # Create purchase order 
-    #         purchase_order = self.create_purchase_order(kw.get('supplierId'), kw.get('items'), date_now, currency_id, pricelist_id, kw.get('paymentTermId'))
-    #         self.make_info_log('Purchase order created')
-
-    #         # Confirm purchase 
-    #         self.confirm_purchase_order(purchase_order.id)
-    #         self.make_info_log('Purchase order confirmed')
-            
-    #         invoice = purchase_order.invoice_ids
-    #         invoice.write({
-    #             'supplier_invoice_number': kw.get('supplierInvoiceNumber', None)
-    #         })
-    #     else:
-    #         invoice = self.create_invoice(kw.get('supplierId'), kw.get('items'), currency_id, kw.get('paymentTermId'), kw.get('supplierInvoiceNumber', None))
-    #         self.make_info_log('Invoice created')
-
-    #     invoice_ids = invoice.mapped(lambda x: x.id) 
-        
-    #     # Validate invoice
-    #     self.prepare_invoice(invoice_ids, currency_id, date_now)
-    #     self.make_info_log('Invoice prepared')
-
-    #     # Create invoice move lines
-    #     invoice_move_lines = self.create_invoice_move_lines(invoice_ids, float(kw.get('payment')), date_now)
-    #     self.make_info_log('Invoice move lines created')
-
-    #     # Create account move
-    #     account_move = self.create_account_move(invoice_ids, invoice_move_lines)
-    #     self.make_info_log('Account move created')
-
-    #     # Validate invoice
-    #     self.validate_invoice(invoice_ids, mode)
-    #     self.make_info_log('Invoice validated')
-
-    #     # Create account voucher
-    #     account_voucher = self.create_account_voucher(account_move.id, kw.get('journalId'), currency_id, float(kw.get('payment')))
-    #     self.make_info_log('Account voucher created')
-
-    #     # Close invoice
-    #     self.close_invoice(invoice_ids)
-    #     self.make_info_log('Attempt close invoice')
-
-    #     # Create account bank statement lines
-    #     account_bank_statement_lines = self.create_bank_statement_lines(account_voucher.id)
-    #     self.make_info_log('Bank statement lines created')
-
-    #     # Create account bank statement
-    #     self.create_bank_statement(account_voucher.id, account_bank_statement_lines, date_now)
-    #     self.make_info_log('Bank statement created')
-
-    #     return {
-    #         'status': 'ok'
-    #     }
+    '''
+        Purchase processing resource route
+    ''' 
+    @http.route('/eiru_purchases/process', type='json', auth='user', methods=['POST'])
+    def process_purchase(self, **kw):
+        make_info_log('Processing {}'.format(kw.get('mode')))
+        return store_data(kw)

+ 18 - 0
templates.xml

@@ -19,6 +19,24 @@
             <field name="params">{'mode': 'expense'}</field>
         </record>
 
+        <record id="eiru_purchases.expenses_rel_1_action" model="ir.actions.client">
+            <field name="name">Eiru Expenses 1</field>
+            <field name="tag">eiru_purchases.action_launch</field>
+            <field name="params">{'mode': 'product_picking'}</field>
+        </record>
+        
+        <record id="eiru_purchases.expenses_rel_2_action" model="ir.actions.client">
+            <field name="name">Eiru Expenses 2</field>
+            <field name="tag">eiru_purchases.action_launch</field>
+            <field name="params">{'mode': 'payment'}</field>
+        </record>
+
+        <record id="eiru_purchases.expenses_rel_3_action" model="ir.actions.client">
+            <field name="name">Eiru Expenses 3</field>
+            <field name="tag">eiru_purchases.action_launch</field>
+            <field name="params">{'mode': 'product_taking'}</field>
+        </record>
+
         <!-- <menuitem id="eiru_purchases.new_purchase" name="Nueva compra" parent="eiru_dashboard.eiru_dashboard_main" action="eiru_purchases.purchases_action" sequence="4" />
         <menuitem id="eiru_purchases.new_expense" name="Nuevo gasto" parent="eiru_dashboard.eiru_dashboard_main" action="eiru_purchases.expenses_action" sequence="5" /> -->
     </data>