summaryrefslogtreecommitdiff
path: root/indoteknik_custom/models/purchasing_job.py
blob: 7ac678c4642bffd3669473a7fe655a8b4a8c052c (plain)
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
from odoo import fields, models, api, tools
import logging

_logger = logging.getLogger(__name__)


class PurchasingJob(models.Model):
    _name = 'v.purchasing.job'
    _auto = False
    _rec_name = 'product_id'

    id = fields.Integer()
    product_id = fields.Many2one('product.product', string="Product")
    brand = fields.Char(string='Brand')
    item_code = fields.Char(string='Item Code')
    product = fields.Char(string='Product Name')
    onhand = fields.Float(string='OnHand')
    incoming = fields.Float(string="Incoming")
    outgoing = fields.Float(string="Outgoing")
    action = fields.Char(string="Status")

    def init(self):
        tools.drop_view_if_exists(self.env.cr, self._table)
        self.env.cr.execute("""
            CREATE OR REPLACE VIEW %s AS (
                select product_id as id, product_id, brand, item_code, product, onhand, incoming, outgoing, action
                from v_procurement_monitoring_by_product
                where action = 'kurang beli'
            )
        """ % self._table)

    def open_form_multi_generate_request_po(self):
        action = self.env['ir.actions.act_window']._for_xml_id('indoteknik_custom.action_purchasing_job_multi_update')
        action['context'] = {
            'product_ids': [x.id for x in self]
        }
        return action
    
    def generate_request_po(self):
        # print(1)
        # TODO create document automatic purchase
        for job in self:
            print(job.product_id.name)


class OutstandingSales(models.Model):
    _name = 'v.sales.outstanding'
    _auto = False
    _rec_name = 'move_id'

    id = fields.Integer()
    move_id = fields.Many2one('stock.move', string='Move')
    picking_id = fields.Many2one('stock.picking', string='Picking')
    product_id = fields.Many2one('product.product', string='Product')
    sale_id = fields.Many2one('sale.order', string='Sale')
    sale_line_id = fields.Many2one('sale.order.line', string='Sale Line')
    partner_id = fields.Many2one('res.partner', string='Partner')
    partner_invoice_id = fields.Many2one('res.partner', string='Invoice Partner')
    salesperson_id = fields.Many2one('res.users', string='Salesperson')
    origin = fields.Char(string='Origin')
    salesperson = fields.Char(string='Sales Name')
    item_code = fields.Char(string='Item Code')
    product = fields.Char(string='Product')
    outgoing = fields.Float(string='Outgoing')
    brand = fields.Char(string='Brand')
    invoice_partner = fields.Char(string='Invoice Partner')

    def init(self):
        tools.drop_view_if_exists(self.env.cr, self._table)
        self.env.cr.execute("""
            CREATE OR REPLACE VIEW v_sales_outstanding AS (
                select sm.id, sm.id as move_id, sp.id as picking_id, sm.product_id, so.id as sale_id, 
                sol.id as sale_line_id, rp.id as partner_id, so.user_id as salesperson_id, so.partner_invoice_id,
                sp.origin, rp2.name as salesperson, coalesce(pp.default_code, pt.default_code) as item_code, pt.name as product,
                sm.product_uom_qty as outgoing, xm.x_name as brand, rp.name as invoice_partner
                from stock_move sm
                join stock_picking sp on sp.id = sm.picking_id
                join sale_order_line sol on sol.id = sm.sale_line_id
                join sale_order so on so.id = sol.order_id
                join res_partner rp on rp.id = so.partner_invoice_id
                join res_users ru on ru.id = so.user_id
                join res_partner rp2 on rp2.id = ru.partner_id
                join product_product pp on pp.id = sm.product_id
                join product_template pt on pt.id = pp.product_tmpl_id
                left join x_manufactures xm on xm.id = pt.x_manufacture
                where sp.state in ('draft', 'waiting', 'confirmed', 'assigned')
                and sp.name like '%OUT%'
            )
        """)