def index(request): currentProfile = Profile.getProfile(request.user) parent_user_id = currentProfile.parent_user.id if 'purchase_id' in request.GET : purchase_id = request.GET['purchase_id'] else: return { "success": False, "total": 0, "errors": "purchase_id is Required" } #logger.info('purchase_id:%s, parent_user_id: %s' % (purchase_id,parent_user_id)) purchaseDetails = PurchaseDetail.objects.select_related('item').filter(purchase=purchase_id, user=parent_user_id) #count = PurchaseDetail.objects.get(id=purchase_id, user=parent_user_id).count() i = 0 for purchaseDetail in purchaseDetails: #logger.info(str(purchaseDetail.list())) if purchaseDetail.inventory_updated: continue try: # update qt in stock if purchaseDetail.item.inventory_no_count: purchaseDetail.inventory_updated = True purchaseDetail.save() i = i + 1 continue else: if purchaseDetail.item.qt_in_stock is None: qt_in_stock = purchaseDetail.total_units else: qt_in_stock = purchaseDetail.item.qt_in_stock + purchaseDetail.total_units if qt_in_stock < 0: qt_in_stock = 0 purchaseDetail.item.qt_in_stock = qt_in_stock purchaseDetail.inventory_updated = True #logger.info('qt_in_stock: %s' % (qt_in_stock)) purchaseDetail.item.save() purchaseDetail.save() if settings.HGM == True: # UPDATE qt_in_stock in WWW updateWwwQtInStock(purchaseDetail.item) i = i + 1 except Exception, err: logger.error(err) continue # continue the loop even if there was a problem with 1 of the records
def update(self, request, id=None): ready_data_true = {'success': True } ready_data_false = {'success': False} if not request.user.is_authenticated(): ready_data_false["error"] = "Authorization is failed" return ready_data_false if id: try: currentProfile = Profile.getProfile(request.user) hgm_www_host = settings.HGM_WWW_HOST hgm_www_port = settings.HGM_WWW_PORT hgm_www_user = settings.HGM_WWW_USER hgm_www_password = settings.HGM_WWW_PASSWORD hgm_www_db = settings.HGM_WWW_DB # CONNECTION TO www.hudsongreenemarket.com 52.0.0.79 myDB = MySQLdb.connect(host=hgm_www_host,port=hgm_www_port,user=hgm_www_user,passwd=hgm_www_password,db=hgm_www_db) cHandler = myDB.cursor() item_id = None ext_posted_data = simplejson.loads(request.POST.get('items')) attrs = self.flatten_dict(ext_posted_data) try: del attrs['id'] except KeyError: pass packed_statement = '' if 'packed_qt' in attrs: packed_statement = "SET packed_qt='" + str(attrs['packed_qt']) + "'" order_status_statement = '' if 'order_status' in attrs and attrs['order_status'] == 'Packed': order_status_statement = ", order_status='A'" # Packed sql = "UPDATE emk8r_virtuemart_order_items " + \ packed_statement + order_status_statement + \ " WHERE virtuemart_order_item_id='" + str(id) + "';" cHandler.execute(sql) myDB.commit() # EVERY TIME packed_qt has changed NEED TO DEPLETE INVENTORY IN BO try: if 'bo_item_id' in attrs: bo_item_id = str(attrs['bo_item_id']) item = Item.objects.get(id=bo_item_id, user=currentProfile.parent_user) if item.inventory_no_count == False: # if no_count True, do not change qt_in_stock qt_in_stock = float(item.qt_in_stock) - float(1) if qt_in_stock < 0: qt_in_stock = 0 item.qt_in_stock = str("%.2f" % qt_in_stock) item.save() if settings.HGM == True: # UPDATE qt_in_stock in WWW updateWwwQtInStock(item) except ObjectDoesNotExist: raise Exception (u"Item is not found") ready_data_true['total'] = 1 return ready_data_true except Exception, err: ready_data_false['errors'] = str(err) return ready_data_false
def update(profile, args=None, id=None): ready_data_true = {'success': True } ready_data_false = {'success': False} try: doUpdateBestPriced = False if profile == None: raise Exception('No Profile specified') if profile.get_role_display() != 'Customer': # only Customer and Sub-customer are allowed to modify Item if profile.get_role_display() != 'Sub-customer' : logger.error("Wrong User Type: " + profile.get_role_display()) raise Exception('Not Allowed: Wrong User Type') if args == None: raise Exception('No Arguments are given') item = None item_id = None if id: item_id = id try: item = Item.objects.get(id=item_id, user=profile.parent_user) except ObjectDoesNotExist: raise Exception (u"Item is not found") if 'id' in args: item_id = args['id'] try: item = Item.objects.get(id=item_id, user=profile.parent_user) except ObjectDoesNotExist: raise Exception (u"Item is not found") if 'item_id' in args: item_id = args['item_id'] try: item = Item.objects.get(id=item_id, user=profile.parent_user) except ObjectDoesNotExist: raise Exception (u"Item is not found") if not item: raise Exception (u"Item is NOT found") if 'unit_id' in args: unit_id = args['unit_id'] try: item.unit = Unit.objects.get(id=unit_id, user=profile.parent_user) except ObjectDoesNotExist: raise Exception (u"Unit is not found") if 'category_id' in args: category_id = args['category_id'] if category_id.lower() == 'root': item.category = None else: try: item.category = Category.objects.get(id=category_id, user=profile.parent_user) except ObjectDoesNotExist: raise Exception (u"Category is not found") if 'name' in args: item.name = replace(args['name']) if 'upc' in args: upc = replace(args['upc']) if upc != '': exist = Item.objects.filter(user=profile.parent_user, upc=upc). \ exclude(id=item_id).exists() if exist == True: raise Exception ("Duplicate UPC, %s is not UNIQUE, UPDATE FAILED For UPC: %s" % (upc, item.upc)) else: item.upc = upc if 'upc_2' in args: item.upc_2 = replace(args['upc_2']) if 'upc_3' in args: item.upc_3 = replace(args['upc_3']) if 'upc_case' in args: item.upc_case = replace(args['upc_case']) if 'image_url' in args: item.image_url = replace(args['image_url']) if 'notes' in args: item.notes = replace(args['notes']) if 'inventory_no_count' in args and args['inventory_no_count'] != None: if args['inventory_no_count'] == 'false': item.inventory_no_count = False else: item.inventory_no_count = True if 'qt_in_stock' in args: qt_in_stock = args['qt_in_stock'] if qt_in_stock == '' or qt_in_stock == 'NaN': item.qt_in_stock = '0' elif float(item.qt_in_stock) != float(qt_in_stock): # change only if different item.qt_in_stock_last_updated = datetime.now() diff = float(qt_in_stock) - float(item.qt_in_stock) d = str(diff) if d.endswith('.0'): d = d[:-2] if d.endswith('.00'): d = d[:-3] if diff > 0: item.qt_in_stock_last_diff = '+' + str(d) else: item.qt_in_stock_last_diff = str(d) item.qt_in_stock = replace(qt_in_stock) if settings.HGM == True: # UPDATE qt_in_stock in WWW updateWwwQtInStock(item) if 'min_qt_in_stock' in args: min_qt_in_stock = replace(args['min_qt_in_stock']) if min_qt_in_stock == '' or min_qt_in_stock == 'NaN': min_qt_in_stock = '0' item.min_qt_in_stock = min_qt_in_stock if 'qt_to_stock' in args: qt_to_stock = replace(args['qt_to_stock']) if qt_to_stock == '' or qt_to_stock == 'NaN': qt_to_stock = '0' item.qt_to_stock = qt_to_stock if 'temp_stock' in args: temp_stock = replace(args['temp_stock']) if temp_stock == '' or temp_stock == 'NaN' or temp_stock == 'null' or temp_stock == None: item.temp_stock = None else: item.temp_stock = Decimal(temp_stock) if 'best_price' in args: best_price = replace(args['best_price']) if best_price == '' or best_price == 'NaN' or best_price == 'null' or best_price == None: item.best_price = None else: item.best_price = Decimal(best_price) if 'cost_per_unit' in args: cost_per_unit = replace(args['cost_per_unit']) if cost_per_unit == '' or cost_per_unit == 'NaN' or cost_per_unit == 'null' or cost_per_unit == None: item.cost_per_unit = None else: item.cost_per_unit = Decimal(cost_per_unit) if 'loc_stock' in args: item.loc_stock = replace(args['loc_stock']) if 'loc_ret_dept' in args: newDept = replace(args['loc_ret_dept']) if newDept == '' or newDept == 'None': logger.error('ATTEMPT TO ERAZE DEPARTMENT: upc-' + item.upc) else: item.loc_ret_dept = newDept if 'loc_ret_col' in args: item.loc_ret_col = replace(args['loc_ret_col']) if 'loc_ret_row' in args: item.loc_ret_row = replace(args['loc_ret_row']) if 'enabled' in args: if args['enabled'] == 'false': item.enabled = False else: if item.enabled != True: item.enabled = True doUpdateBestPriced = True if 'so_ignore_no_sales' in args and args['so_ignore_no_sales'] != None: if args['so_ignore_no_sales'] == 'false': item.so_ignore_no_sales = False else: item.so_ignore_no_sales = True if 'so_do_not_order' in args and args['so_do_not_order'] != None: if args['so_do_not_order'] == 'false': item.so_do_not_order = False else: item.so_do_not_order = True if 'so_always_order' in args and args['so_always_order'] != None: item.so_always_order = args['so_always_order'] if 'total_units_per_case' in args: item.total_units_per_case = replace(args['total_units_per_case']) #item.sales_price = 0 item.save() # orders from disabled items are not calculated for Best Priced # so if item is changed to enabled, need to calculate best Priced Order if doUpdateBestPriced: updateBestPriced(None, None, str(item.id)) ready_data_true['items'] = item.list(item.id) return ready_data_true except Exception, err: #if str(err).startswith( 'Duplicate UPC' ): # logger.error(err) #else: logger.error(err) raise Exception(err)
def update(self, request, id=None): ready_data_true = {'success': True } ready_data_false = {'success': False} if not request.user.is_authenticated(): ready_data_false["error"] = "Authorization is failed" return ready_data_false parent_user_id = getParentuser(request) if logger.isEnabledFor(logging.DEBUG): logger.debug('update method') if id: ext_posted_data = simplejson.loads(request.POST.get('items')) attrs = self.flatten_dict(ext_posted_data) try: del attrs['id'] except KeyError: pass try: if 'price' in attrs: if attrs['price'] == None: attrs['price'] = '0' attrs['price'] = Decimal(str(attrs['price'])) if 'order_qt' in attrs: if attrs['order_qt'] == None: attrs['order_qt'] = '0' attrs['order_qt'] = Decimal(str(attrs['order_qt'])) if 'total_price' in attrs: if attrs['total_price'] == None: attrs['total_price'] = '0' attrs['total_price'] = Decimal(str(attrs['total_price'])) poDetail = self.model.objects.select_related('item').get(pk=id) if 'total_units' in attrs: if attrs['total_units'] == None: attrs['total_units'] = '0' attrs['total_units'] = Decimal(str(attrs['total_units'])) if poDetail.inventory_updated == True: # need to update qt_in_stock again if Decimal(poDetail.total_units) != attrs['total_units']: change = attrs['total_units'] - Decimal(poDetail.total_units) poDetail.item.qt_in_stock = poDetail.item.qt_in_stock + change poDetail.item.save() if settings.HGM == True: # UPDATE qt_in_stock in WWW updateWwwQtInStock(poDetail.item) if 'upc_case' in attrs: poDetail.item.upc_case=attrs['upc_case'] del attrs['upc_case'] poDetail.item.save() if 'upc' in attrs : if attrs['upc'].strip() != '' and Item.objects.filter(user=parent_user_id, upc=attrs['upc']). \ exclude(id=poDetail.item.id).exists(): raise Exception('Exception: Duplicate UPC- '+ attrs['upc']) Item.objects.filter(pk=poDetail.item.id).update(upc=attrs['upc']) del attrs['upc'] if 'upc_2' in attrs: Item.objects.filter(pk=poDetail.item.id).update(upc_2=attrs['upc_2']) del attrs['upc_2'] if 'upc_3' in attrs: Item.objects.filter(pk=poDetail.item.id).update(upc_3=attrs['upc_3']) del attrs['upc_3'] self.model.objects.filter(pk=id, user=getParentuser(request)).update(**attrs) poDetail = self.model.objects.select_related().get(pk=id) if 'total_price' not in attrs: poDetail.updateTotalPrice() ready_data_true['items'] = poDetail.list() ready_data_true['total'] = 1 return ready_data_true except Exception, err: ready_data_false['errors'] = err.message ready_data_false['msg'] = traceback.format_exc() ready_data_false['attrs'] = attrs #ready_data_false['items'] = poDetail.list() if logger.isEnabledFor(logging.ERROR): logger.error('%s User: %s' % (err, request.user,)) #logger.error('%s' % (traceback.format_exc())) #logger.error('User: %s' % (request.user)) return ready_data_false
user_id = parent_user_id, \ inventory_updated = 1, \ verified = False) except Exception, err: ready_data_false['errors'] = err.message ready_data_false['order'] = order ready_data_false['msg'] = traceback.format_exc() return ready_data_false # update qt_in_stock poDetail.item.qt_in_stock = poDetail.item.qt_in_stock + total_units poDetail.item.save() if settings.HGM == True: # UPDATE qt_in_stock in WWW updateWwwQtInStock(poDetail.item) ready_data_true['items'] = poDetail.list() return ready_data_true def delete(self, request, id=None): ready_data_true = {'success': True } ready_data_false = {'success': False} if not request.user.is_authenticated(): ready_data_false["error"] = "Authorization is failed" return ready_data_false parent_user_id = getParentuser(request)
def worker(req): vendor = req.params()['vendor'] # vendor id number = req.params()['number'] # invoice number purchase_num = req.params()['purchase'] # purchase number date = req.params()['date'] # date of the invoice user = req.params()['user'] parent_user_id = req.params()['parent_user_id'] if not number: number = datetime.now().strftime("%m-%d-%Y %H:%M:%S") if not date: date = datetime.now() with transaction.commit_on_success(): try: if PurchaseDetail.objects.filter(purchase=purchase_num, user=parent_user_id, verified=False).exists() : return { "success": False, "errors": "Not All Records are VERIFIED, Please Verify all records before Archiving" } pd_count = PurchaseDetail.objects.filter(purchase=purchase_num, user=parent_user_id) if pd_count > 0: purchaseDetails = PurchaseDetail.objects.select_related('product', 'item', 'item__unit', 'product__vendor', 'product__user'). \ filter(purchase=purchase_num, user=parent_user_id) #create new invoice invoice = Invoice.objects.create(number=number, date=date, vendor=vendor, user_id=parent_user_id) for purchaseDetail in purchaseDetails: #create invoice product invoiceProduct = getInvoiceProduct(purchaseDetail) #add products into invoice price_per_unit = calculatePricePerUnit(purchaseDetail) InvoiceDetail.objects.create( \ invoice = invoice, \ item = purchaseDetail.item, \ product = invoiceProduct, \ order_qt = purchaseDetail.order_qt, \ price = invoiceProduct.price, \ notes = purchaseDetail.notes, \ mismatch = purchaseDetail.mismatch, \ outofstock = purchaseDetail.outofstock, \ total_price = purchaseDetail.total_price, \ price_per_unit = price_per_unit, \ received_qt = purchaseDetail.received_qt, \ total_units = purchaseDetail.total_units, \ inventory_updated = purchaseDetail.inventory_updated, \ user_id = parent_user_id) # update qt in stock if purchaseDetail.inventory_updated == False: if purchaseDetail.item.inventory_no_count: qt_in_stock = purchaseDetail.item.qt_in_stock else: if purchaseDetail.item.qt_in_stock is None: qt_in_stock = purchaseDetail.total_units else: qt_in_stock = purchaseDetail.item.qt_in_stock + purchaseDetail.total_units if qt_in_stock < 0: qt_in_stock = 0 purchaseDetail.item.qt_in_stock = qt_in_stock purchaseDetail.item.cost_per_unit = price_per_unit purchaseDetail.item.save() if settings.HGM == True: # UPDATE qt_in_stock in WWW updateWwwQtInStock(purchaseDetail.item) #drop purchase product PurchaseDetail.objects.filter(purchase=purchase_num, user=parent_user_id).delete() #delete blank purchase purchase = Purchase.objects.get(pk=purchase_num) removePurchase(purchase) except Exception, err: logger.error(err)