1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
from odoo import fields, models, api, _
from odoo.exceptions import AccessError, UserError, ValidationError
class PurchaseOrder(models.Model):
_inherit = 'purchase.order'
sale_order_id = fields.Many2one('sale.order', string='Sale Order')
procurement_status = fields.Char(string='Procurement Status', compute='get_procurement_status', readonly=True)
approval_status = fields.Selection([
('pengajuan1', 'Approval Manager'), #siapa? darren - 11
('pengajuan2', 'Approval Pimpinan'), #akbar - 7 temporary not used
('approved', 'Approved'),
], string='Approval Status', readonly=True, copy=False, index=True, tracking=3)
delivery_amount = fields.Float('Delivery Amount', compute='compute_delivery_amount')
total_margin = fields.Float(
'Margin', compute='compute_total_margin',
help="Total Margin in Sales Order Header")
total_percent_margin = fields.Float(
'Margin%', compute='compute_total_margin',
help="Total % Margin in Sales Order Header")
total_so_margin = fields.Float(
'SO Margin', compute='compute_total_margin',
help="Total Margin in Sales Order Header")
total_so_percent_margin = fields.Float(
'SO Margin%', compute='compute_total_margin',
help="Total % Margin in Sales Order Header")
amount_total_without_service = fields.Float('AmtTotalWithoutService', compute='compute_amt_total_without_service')
def get_procurement_status(self):
for purchase_order in self:
product_uom_qty = sum_qty_received = 0
for order_line in purchase_order.order_line:
product_uom_qty += order_line.product_uom_qty
sum_qty_received += order_line.qty_received
if product_uom_qty == sum_qty_received:
status = 'Terproses'
elif product_uom_qty > sum_qty_received > 0:
status = 'Sebagian Diproses'
else:
status = 'Menunggu Diproses'
purchase_order.procurement_status = status
def sale_order_sync(self):
if not self.sale_order_id:
return
purchase_orders = self.search(['&', ('sale_order_id', '=', self.sale_order_id.id), ('id', '!=', self.id)])
products_exception = []
for purchase_order in purchase_orders:
for order_line in purchase_order.order_line:
products_exception.append(order_line.product_id.id)
self.order_line.unlink()
for order_line in self.sale_order_id.order_line:
if order_line.product_id.id and order_line.product_id.id not in products_exception:
values = {
'order_id': self.id,
'product_id': order_line.product_id.id,
'name': order_line.product_id.display_name,
'product_qty': order_line.product_qty,
}
self.env['purchase.order.line'].sudo().create(values)
# def compute_count_line_product(self):
# for order in self:
# count = 0
# for line in order.order_line:
# if line.product_id.type == 'product':
# count += 1
# if count == 0:
# order.count_line_product = 1
# else:
# order.count_line_product = count
def compute_delivery_amount(self):
for order in self:
amount = 0
for line in order.order_line:
if line.product_id.type == 'service':
amount += line.price_total
order.delivery_amount = amount
def button_confirm(self):
res = super(PurchaseOrder, self).button_confirm()
test = self.env.user.is_leader
test2 = self.env.user.is_purchasing_manager
if self.total_percent_margin < self.total_so_percent_margin and not self.env.user.is_purchasing_manager and not self.env.user.is_leader:
raise UserError("Beda Margin dengan Sales, harus approval Manager")
if not self.sale_order_id and not self.env.user.is_purchasing_manager and not self.env.user.is_leader:
raise UserError("Tidak ada link dengan SO, harus approval Manager")
self.approval_status = 'approved'
# for line in self.order_line:
# if not line.product_id:
# continue
# if line.product_id.type == 'service' and self.env.user.id != 6:
# raise UserError("Ada tambahan Ongkos kirim, harus Approval Manager")
# sale_order_line = self.env['sale.order.line'].search(
# [('product_id', '=', line.product_id.id),
# ('order_id', '=', line.order_id.sale_order_id.id)], limit=1, order='price_reduce_taxexcl')
#
# est_purchase_price = sale_order_line.purchase_price
# real_purchase_price = line.price_unit
# real_tax = real_tax_amount = count_real_tax = 0
# for tax in line.taxes_id:
# count_real_tax += 1
# real_tax = tax
# real_tax_amount += tax.amount
# if (sale_order_line.purchase_tax_id.amount != real_tax_amount or count_real_tax > 1 \
# or real_tax.price_include != sale_order_line.purchase_tax_id.price_include) \
# and (self.env.user.id != 6 and self.env.user.id != 7):
# raise UserError("Beda tax amount dengan Sales, harus Approval Manager")
# elif est_purchase_price < real_purchase_price and self.env.user.id != 6 and self.env.user.id != 7:
# raise UserError("Beda Price dengan Sales, harus Approval Manager")
# self.approval_status = 'approved'
return res
def po_approve(self):
if self.env.user.is_leader or self.env.user.is_purchasing_manager:
raise UserError("Bisa langsung Confirm")
elif self.total_percent_margin == self.total_so_percent_margin and self.sale_order_id:
raise UserError("Bisa langsung Confirm")
else:
self.approval_status = 'pengajuan1'
# approval = 0
# for line in self.order_line:
# if not line.product_id:
# continue
# elif line.product_id.type == 'service' and self.env.user.id != 6:
# approval += 1
# else:
# sale_order_line = self.env['sale.order.line'].search(
# [('product_id', '=', line.product_id.id),
# ('order_id', '=', line.order_id.sale_order_id.id)], limit=1, order='price_reduce_taxexcl')
#
# est_purchase_price = sale_order_line.purchase_price
# real_purchase_price = line.price_unit
# real_tax = real_tax_amount = count_real_tax = 0
# for tax in line.taxes_id:
# count_real_tax += 1
# real_tax = tax
# real_tax_amount += tax.amount
# if (sale_order_line.purchase_tax_id.amount != real_tax_amount or count_real_tax > 1 \
# or real_tax.price_include != sale_order_line.purchase_tax_id.price_include) \
# and (self.env.user.id != 6 and self.env.user.id != 7):
# approval += 1
# elif est_purchase_price != real_purchase_price and self.env.user.id != 6 and self.env.user.id != 7:
# approval += 1
# elif line.product_id.type == 'service' and self.env.user.id != 6 and self.env.user.id != 7:
# approval += 1
# if approval > 0:
# self.approval_status = "pengajuan1"
# else:
# raise UserError("Bisa langsung Confirm")
def button_cancel(self):
res = super(PurchaseOrder, self).button_cancel()
self.approval_status = False
return res
def compute_total_margin(self):
sum_so_margin = sum_sales_price = sum_margin = 0
for line in self.order_line:
sale_order_line = self.env['sale.order.line'].search(
[('product_id', '=', line.product_id.id),
('order_id', '=', line.order_id.sale_order_id.id)], limit=1, order='price_reduce_taxexcl')
sum_so_margin += sale_order_line.item_margin
sales_price = sale_order_line.price_reduce_taxexcl * sale_order_line.product_uom_qty
if sale_order_line.order_id.shipping_cost_covered == 'indoteknik':
sales_price -= sale_order_line.delivery_amt_line
sum_sales_price += sales_price
purchase_price = line.price_subtotal
if line.order_id.delivery_amount > 0:
purchase_price += line.delivery_amt_line
real_item_margin = sales_price - purchase_price
sum_margin += real_item_margin
if sum_so_margin != 0 and sum_sales_price != 0 and sum_margin != 0:
self.total_so_margin = sum_so_margin
self.total_so_percent_margin = round((sum_so_margin / sum_sales_price), 2) * 100
self.total_margin = sum_margin
self.total_percent_margin = round((sum_margin / sum_sales_price), 2) * 100
else:
self.total_margin = 0
self.total_percent_margin = 0
self.total_so_margin = 0
self.total_so_percent_margin = 0
# if not self.order_line or not self.sale_order_id:
# self.total_margin = 0
# self.total_percent_margin = 0
# self.total_so_margin = 0
# self.total_so_percent_margin = 0
# return
# for line in self.order_line:
# line.compute_item_margin()
def compute_amt_total_without_service(self):
for order in self:
sum_price_total = 0
for line in order.order_line:
if line.product_id.type == 'product':
sum_price_total += line.price_total
order.amount_total_without_service = sum_price_total
|