Ejemplo n.º 1
0
def fifo_assign(unit_sale_id, to_assign):
    ship_lines = inventory_api.shipmentline({})
    avail_slines = {}
    for l in ship_lines:
        if l['inventory_item_label'] not in avail_slines:
            avail_slines[l['inventory_item_label']] = l['id']

    for sku in to_assign:
        fifo_info = {}
        fifo_info['shipment_line_id'] = avail_slines[sku]
        fifo_info['unit_sale_id'] = unit_sale_id
        fifo_info['quantity'] = to_assign[sku]
        COGSAssignment(**fifo_info).save()
Ejemplo n.º 2
0
def _assign_FIFO_job(*args, **kwargs):

    # 1 get list of all COGS to be assigned sorted by data
    to_do = sorted(acctg_api.fifo_unassigned({}), key=lambda x: x['id'])
    
    # 2 get an order list of FIFO's available
    available =  pd.DataFrame(acctg_api.fifo_available({})).sort_index(by='arrival_date')

    #3 loop thru the COGS needed and pull from FIFO as necessary using oldest first
    new_fifo_list = []
    for u in to_do:
        for item, qty in u['unassigned'].iteritems():
            if qty > 0:
                rmg_qty = qty
                for i in available.index:
                    if available.loc[i, item] > 0:
                        use_cnt = min(available.loc[i, item], rmg_qty)
                        new_fifo_list.append({'order': available.loc[i, 'order'],
                                              'item': item,
                                              'qty': use_cnt,
                                              'unit_sale': u['id'],
                                              'sale': u['sale']})
                        available.loc[i, item] -= use_cnt
                        rmg_qty -= use_cnt
                        if rmg_qty == 0:
                            break
                        
    # 4 get all the shipment lines that we will need
    all_shipments = dict((sl['unit_label'] + sl['shipment_label'], sl['id']) for sl in inv_api.shipmentline({}))

    # 5 save the COGS assignments
    for new_fifo in new_fifo_list:
        flds = {}
        flds['shipment_line_id'] = all_shipments[new_fifo['item'] + new_fifo['order']]
        flds['unit_sale_id'] = new_fifo['unit_sale']
        flds['quantity'] = new_fifo['qty']
        output = COGSAssignment(**flds).save()
    
    # 6 force a gl entry calc for all affected Sale objects
    for sale_id in set([u['sale'] for u in new_fifo_list]):
        Sale.objects.get(id=sale_id).update_gl()
    return