summaryrefslogtreecommitdiff
path: root/indoteknik_api/models
diff options
context:
space:
mode:
authorstephanchrst <stephanchrst@gmail.com>2023-02-10 10:36:16 +0700
committerstephanchrst <stephanchrst@gmail.com>2023-02-10 10:36:16 +0700
commitbbf176b0ce51ade22b74d0df2023025a4cef3efa (patch)
tree24d2d3c7e3fed5c0672a9063a09e3c4d33ba3dac /indoteknik_api/models
parentbd01d7a842c8b6e4aea6a2fc3615a9d57fbcd470 (diff)
parentb0de64ae769148a009d0a08a957c5c35dee174a9 (diff)
Merge branch 'release' into line_no_sales_order
Diffstat (limited to 'indoteknik_api/models')
-rw-r--r--indoteknik_api/models/__init__.py1
-rw-r--r--indoteknik_api/models/account_move.py30
-rw-r--r--indoteknik_api/models/rest_api.py47
-rw-r--r--indoteknik_api/models/sale_order.py45
4 files changed, 118 insertions, 5 deletions
diff --git a/indoteknik_api/models/__init__.py b/indoteknik_api/models/__init__.py
index 9af9f36e..892d2657 100644
--- a/indoteknik_api/models/__init__.py
+++ b/indoteknik_api/models/__init__.py
@@ -4,6 +4,7 @@ from . import product_pricelist
from . import product_product
from . import product_template
from . import res_users
+from . import rest_api
from . import sale_order
from . import x_manufactures
from . import website_content
diff --git a/indoteknik_api/models/account_move.py b/indoteknik_api/models/account_move.py
index 9fd6fb18..5c31f010 100644
--- a/indoteknik_api/models/account_move.py
+++ b/indoteknik_api/models/account_move.py
@@ -1,13 +1,13 @@
import datetime
from odoo import models
-from pytz import timezone
class AccountMove(models.Model):
_inherit = 'account.move'
- def api_v1_single_response(self, account_move):
+ def api_v1_single_response(self, account_move, context=False):
data = {
+ 'token': self.env['rest.api'].md5_salt(account_move.id, 'account.move'),
'id': account_move.id,
'name': account_move.name,
'purchase_order_name': account_move.ref or '',
@@ -15,6 +15,30 @@ class AccountMove(models.Model):
'sales': account_move.invoice_user_id.name,
'amount_total': account_move.amount_total,
'amount_residual': account_move.amount_residual,
- 'invoice_date': account_move.invoice_date.strftime('%d/%m/%Y') or ''
+ 'invoice_date': '',
+ 'efaktur': True if account_move.efaktur_document else False,
}
+ if isinstance(object, datetime.date):
+ data['invoice_date'] = account_move.invoice_date.strftime('%d/%m/%Y')
+ if context:
+ if context == 'with_detail':
+ res_users = self.env['res.users']
+ data_with_detail = {
+ 'id': account_move.id,
+ 'name': account_move.name,
+ 'purchase_order_name': account_move.ref or '',
+ 'payment_term': account_move.invoice_payment_term_id.name or '',
+ 'sales': account_move.invoice_user_id.name,
+ 'amount_total': account_move.amount_total,
+ 'amount_residual': account_move.amount_residual,
+ 'invoice_date': account_move.invoice_date.strftime('%d/%m/%Y') or '',
+ 'invoice_date_due': account_move.invoice_date_due.strftime('%d/%m/%Y') or '',
+ 'customer': res_users.api_address_response(account_move.partner_id),
+ 'products': [],
+ }
+ for line in account_move.invoice_line_ids:
+ product = self.env['product.product'].api_single_response(line.product_id)
+ product['quantity'] = line.quantity
+ data_with_detail['products'].append(product)
+ data.update(data_with_detail)
return data
diff --git a/indoteknik_api/models/rest_api.py b/indoteknik_api/models/rest_api.py
new file mode 100644
index 00000000..65119b52
--- /dev/null
+++ b/indoteknik_api/models/rest_api.py
@@ -0,0 +1,47 @@
+from odoo import models
+from odoo.http import request
+import datetime
+from pytz import timezone
+import hashlib
+import base64
+
+
+class RestApi(models.TransientModel):
+ _name = 'rest.api'
+
+ def datetime_to_str(self, object, format):
+ time = ''
+ if isinstance(object, datetime.datetime):
+ time = object.astimezone(timezone('Asia/Jakarta')).strftime(format)
+ return time
+
+ def md5_salt(self, value, salt):
+ return hashlib.md5((salt + '$' + str(value)).encode()).hexdigest()
+
+ def md5_salt_valid(self, value, salt, token):
+ return hashlib.md5((salt + '$' + str(value)).encode()).hexdigest() == token
+
+ def get_single_attachment(self, model, field, id):
+ domain = [
+ ('res_model', '=', model),
+ ('res_field', '=', field),
+ ('res_id', '=', id),
+ ]
+ fields = ['datas', 'mimetype']
+ result = self.env['ir.attachment'].sudo().search_read(domain, fields)
+ return result[0] if len(result) > 0 else None
+
+ def response_attachment(self, data = {}):
+ decode_content = data.get('decode_content', False)
+ if decode_content:
+ data['content'] = base64.b64decode(data['content'])
+
+ return request.make_response(
+ data['content'],
+ [
+ ('Content-Type', data['mimetype']),
+ ('Content-Disposition', 'attachment; filename=%s' % data['filename']),
+ ('Content-Length', len(data['content']))
+ ]
+ )
+ \ No newline at end of file
diff --git a/indoteknik_api/models/sale_order.py b/indoteknik_api/models/sale_order.py
index 3359ee6a..c3f3dccb 100644
--- a/indoteknik_api/models/sale_order.py
+++ b/indoteknik_api/models/sale_order.py
@@ -4,11 +4,52 @@ from odoo import models
class SaleOrder(models.Model):
_inherit = 'sale.order'
- def api_v1_single_response(self, sale_order):
+ def api_v1_single_response(self, sale_order, context=False):
data = {
+ 'token': self.env['rest.api'].md5_salt(sale_order.id, 'sale.order'),
'id': sale_order.id,
'name': sale_order.name,
'sales': sale_order.user_id.name,
- 'amount_total': sale_order.amount_total
+ 'amount_total': sale_order.amount_total,
+ 'purchase_order_name': sale_order.partner_purchase_order_name,
+ 'purchase_order_file': True if sale_order.partner_purchase_order_file else False,
+ 'invoice_count': sale_order.invoice_count,
+ 'status': 'draft',
}
+ if sale_order.state == 'cancel':
+ data['status'] = 'cancel'
+ if sale_order.state in ['draft', 'sent']:
+ data['status'] = 'draft'
+ if sale_order.approval_status in ['pengajuan1', 'pengajuan2']:
+ data['status'] = 'waiting'
+ if sale_order.state == 'sale':
+ data['status'] = 'sale'
+ for picking in sale_order.picking_ids:
+ if picking.state == 'assigned':
+ data['status'] = 'shipping'
+ if sale_order.state == 'done':
+ data['status'] = 'done'
+
+ if context:
+ if context == 'with_detail':
+ res_users = self.env['res.users']
+ data_with_detail = {
+ 'payment_term': sale_order.payment_term_id.name or '',
+ 'date_order': self.env['rest.api'].datetime_to_str(sale_order.date_order, '%d/%m/%Y %H:%M:%S'),
+ 'products': [],
+ 'address': {
+ 'customer': res_users.api_address_response(sale_order.partner_id),
+ 'invoice': res_users.api_address_response(sale_order.partner_invoice_id),
+ 'shipping': res_users.api_address_response(sale_order.partner_shipping_id)
+ },
+ 'invoices': []
+ }
+ for line in sale_order.order_line:
+ product = self.env['product.product'].api_single_response(line.product_id)
+ product['quantity'] = line.product_uom_qty
+ data_with_detail['products'].append(product)
+ for invoice in sale_order.invoice_ids:
+ if invoice.state == 'posted':
+ data_with_detail['invoices'].append(self.env['account.move'].api_v1_single_response(invoice))
+ data.update(data_with_detail)
return data