Esempio n. 1
0
    def add_deliver(self):
        ids = _gl('order_ids')

        if not ids:
            ids = [_g('order_ids'), ]

        order_headers = DBSession.query(OrderHeader).filter(OrderHeader.id.in_(ids))
        if order_headers.count() == 1:
            h = order_headers[0]
            destination_province_id = h.destination_province_id
            destination_city_id = h.destination_city_id
            destination_address = h.destination_address
            destination_contact = h.destination_contact
            destination_tel = h.destination_tel
            destination_mobile = h.destination_mobile
            payment_id = h.payment_id
            pickup_type_id = h.pickup_type_id
        else:
            destination_province_id = destination_city_id = destination_address = destination_contact = destination_tel = destination_mobile = None
            payment_id = pickup_type_id = None

        total_qty = total_vol = total_weight = 0
        for h in order_headers:
            if h.status >= SORTING[0]:
                flash(MSG_ORDER_NOT_FIT_FOR_DELIVER, MESSAGE_ERROR)
                if request.referrer:
                    return redirect(request.referrer)
                else:
                    return redirect(self.default())
            else:
                total_qty += h.qty or 0
                total_vol += h.vol or 0
                total_weight += h.weight or 0


        suppliers = getMasterAll(Supplier)

        return {'result' : order_headers,
                'suppliers' : suppliers,
                'destination_province_id' : destination_province_id,
                'destination_city_id' : destination_city_id,
                'destination_address' : destination_address,
                'destination_contact' : destination_contact,
                'destination_tel' : destination_tel,
                'destination_mobile' : destination_mobile,
                'payment_id' : payment_id,
                'pickup_type_id' : pickup_type_id,
                'total_qty' : total_qty,
                'total_vol' : total_vol,
                'total_weight' : total_weight,
                }
Esempio n. 2
0
    def supplier(self):
        method = _g('m', 'LIST')
        if method not in ['LIST', 'NEW', 'UPDATE', 'DELETE', 'SAVE_NEW', 'SAVE_UPDATE',
                          'PRICE_LIST_LIST', 'PRICE_LIST_NEW', 'PRICE_LIST_UPDATE', 'PRICE_LIST_SAVE_NEW',
                          'PRICE_LIST_SAVE_UPDATE',
                          ]:
            flash(MSG_NO_SUCH_ACTION, MESSAGE_ERROR);
            return redirect(url_for('.view', action = 'index'))
        if method == 'LIST':
            if _g('SEARCH_SUBMIT'):  # come from search
                values = {'page' : 1}
                for f in ['no', 'name', 'contact_person', 'phone', 'mobile', ] :
                    values[f] = _g(f)
                values['field'] = _g('field', None) or 'create_time'
                values['direction'] = _g('direction', None) or 'desc'
            else: #come from paginate or return
                values = session.get('master_supplier_values', {})
                if _g('page') : values['page'] = int(_g('page'))
                elif 'page' not in values : values['page'] = 1

            session['master_supplier_values'] = values

            conditions = [Supplier.active == 0]
            if values.get('no', None):  conditions.append(Supplier.no.op('like')('%%%s%%' % values['no']))
            if values.get('name', None):  conditions.append(Supplier.name.op('like')('%%%s%%' % values['name']))
            if values.get('contact_person', None):  conditions.append(Supplier.contact_person.op('like')('%%%s%%' % values['contact_person']))
            if values.get('phone', None):  conditions.append(Supplier.phone.op('like')('%%%s%%' % values['phone']))
            if values.get('mobile', None):  conditions.append(Supplier.mobile.op('like')('%%%s%%' % values['mobile']))

            field = values.get('field', 'create_time')
            if values.get('direction', 'desc') == 'desc':
                result = DBSession.query(Supplier).filter(and_(*conditions)).order_by(desc(getattr(Supplier, field)))
            else:
                result = DBSession.query(Supplier).filter(and_(*conditions)).order_by(getattr(Supplier, field))
            def url_for_page(**params): return url_for('bpAdmin.view', action = 'supplier', m = 'LIST', page = params['page'])
            records = paginate.Page(result, values['page'], show_if_single_page = True, items_per_page = PAGINATE_PER_PAGE, url = url_for_page)

            return render_template('admin/supplier_index.html', records = records, values = values)

        elif method == 'NEW':
            return render_template('admin/supplier_new.html', payment = getMasterAll(Payment))

        elif method == 'UPDATE':
            id = _g('id', None)
            if not id :
                flash(MSG_NO_ID_SUPPLIED, MESSAGE_ERROR)
                return redirect(url_for('.view', action = 'supplier'))
            obj = DBSession.query(Supplier).get(id)
            if not obj :
                flash(MSG_RECORD_NOT_EXIST, MESSAGE_ERROR)
                return redirect(url_for('.view', action = 'supplier'))
            return render_template('admin/supplier_update.html', obj = obj, payment = getMasterAll(Payment))
        elif method == 'DELETE':
            id = _g('id', None)
            if not id :
                flash(MSG_NO_ID_SUPPLIED, MESSAGE_ERROR)
                return redirect(url_for('.view', action = 'supplier'))
            obj = DBSession.query(Supplier).get(id)
            if not obj :
                flash(MSG_RECORD_NOT_EXIST, MESSAGE_ERROR)
                return redirect(url_for('.view', action = 'supplier'))
            obj.active = 1
            DBSession.commit()
            flash(MSG_DELETE_SUCC, MESSAGE_INFO)
            return redirect(url_for('.view', action = 'supplier'))
        elif method == 'SAVE_NEW':
            obj = Supplier(
                                no = _g('no'),
                                name = _g('name'),
                                address = _g('address'),
                                phone = _g('phone'),
                                mobile = _g('mobile'),
                                email = _g('email'),
                                contact_person = _g('contact_person'),
                                remark = _g('remark'),
                                payment_id = _g('payment_id'),
                                )
            DBSession.add(obj)
            DBSession.commit()
            flash(MSG_SAVE_SUCC, MESSAGE_INFO)
            return redirect(url_for('.view', action = 'supplier'))

        elif method == 'SAVE_UPDATE':
            id = _g('id', None)
            if not id :
                flash(MSG_NO_ID_SUPPLIED, MESSAGE_ERROR)
                return redirect(url_for('.view', action = 'supplier'))

            try:
                obj = DBSession.query(Supplier).get(id)
                fields = ['no', 'name', 'address', 'phone', 'mobile', 'contact_person', 'remark', 'email', 'payment_id']
                old_info = obj.serialize(fields)

                if not obj :
                    flash(MSG_RECORD_NOT_EXIST, MESSAGE_ERROR)
                    return redirect(url_for('.view', action = 'supplier'))
                for f in fields:
                    setattr(obj, f, _g(f))
                DBSession.commit()

                #handle the system log
                new_info = obj.serialize(fields)
                change_result = obj.compare(old_info, new_info)
                obj.insert_system_logs(change_result)


                flash(MSG_UPDATE_SUCC, MESSAGE_INFO)
                return redirect(url_for('.view', action = 'supplier'))
            except:
                _error(traceback.print_exc())
                DBSession.rollback()
                flash(MSG_SERVER_ERROR, MESSAGE_ERROR)
                return redirect(url_for('.view', action = 'supplier'))

        elif method == 'PRICE_LIST_LIST':
            id = _g('id')
            c = DBSession.query(Supplier).get(id)
            result = DBSession.query(SupplierDiquRatio).filter(and_(SupplierDiquRatio.active == 0,
                                                                    SupplierDiquRatio.supplier_id == c.id,
                                                        )).order_by(SupplierDiquRatio.province_id)
            page = _g('page', 1)
            def url_for_page(**params): return url_for('.view', action = "supplier", m = method, page = params['page'], id = id)
            records = paginate.Page(result, page, show_if_single_page = True, items_per_page = PAGINATE_PER_PAGE, url = url_for_page)
            return render_template("admin/supplier_pricelist_index.html", records = records, obj = c)

        elif method == 'PRICE_LIST_NEW':
            supplier_id = _g('supplier_id')
            return render_template("admin/supplier_pricelist_new.html", supplier_id = supplier_id)

        elif method == 'PRICE_LIST_SAVE_NEW':
            try:
                params = {}
                for f in ['supplier_id', 'province_id', 'city_id', 'qty_ratio', 'weight_ratio', 'vol_ratio', 'remark'] :
                    params[f] = _g(f)
                obj = SupplierDiquRatio(**params)
                DBSession.add(obj)
                DBSession.commit()
                flash(MSG_SAVE_SUCC, MESSAGE_INFO)
            except:
                DBSession.rollback()
                flash(MSG_SERVER_ERROR, MESSAGE_ERROR)
            return redirect(url_for('.view', action = 'supplier', m = 'PRICE_LIST_LIST', id = obj.supplier_id))

        elif method == 'PRICE_LIST_UPDATE':
            obj = DBSession.query(SupplierDiquRatio).get(_g('id'))
            if obj.province_id:
                cities = obj.province.children()
            else:
                cities = []
            return render_template("admin/supplier_pricelist_update.html", cities = cities, obj = obj)

        elif method == 'PRICE_LIST_SAVE_UPDATE':
            id = _g('id', None)
            if not id :
                flash(MSG_NO_ID_SUPPLIED, MESSAGE_ERROR)
                return redirect(url_for('.view', action = 'supplier'))
            obj = DBSession.query(SupplierDiquRatio).get(id)
            if not obj :
                flash(MSG_RECORD_NOT_EXIST, MESSAGE_ERROR)
                return redirect(url_for('.view', action = 'supplier'))
            for f in ['province_id', 'city_id', 'qty_ratio', 'weight_ratio', 'vol_ratio', 'remark'] :
                setattr(obj, f, _g(f))
            DBSession.commit()
            flash(MSG_UPDATE_SUCC, MESSAGE_INFO)
            return redirect(url_for('.view', action = 'supplier', m = 'PRICE_LIST_LIST', id = obj.supplier_id))
Esempio n. 3
0
    def index(self):
        if _g('SEARCH_SUBMIT'):  # come from search
            values = {'page' : 1}
            for f in ['no', 'create_time_from', 'create_time_to', 'ref_no', 'customer_id', 'source_company_id',
                      'source_province_id', 'source_city_id', 'destination_province_id', 'destination_city_id',
                      'approve', 'paid', 'is_exception', 'is_less_qty', 'is_return_note'] :
                values[f] = _g(f)
                values['field'] = _g('field', None) or 'create_time'
                values['direction'] = _g('direction', None) or 'desc'
        else:  # come from paginate or return
            values = session.get('fin_values', {})
            if _g('page') : values['page'] = int(_g('page'))
            elif 'page' not in values : values['page'] = 1


        if not values.get('create_time_from', None) and not values.get('create_time_to', None):
            values['create_time_to'] = dt.now().strftime("%Y-%m-%d")
            values['create_time_from'] = (dt.now() - timedelta(days = 30)).strftime("%Y-%m-%d")

        session['fin_values'] = values


        conditions = [OrderHeader.active == 0,
                      OrderHeader.customer_id == Customer.id,
                      OrderHeader.source_company_id == CustomerSource.id,
                      OrderHeader.destination_company_id == CustomerTarget.id,
                      OrderHeader.create_by_id == User.id, ]
        if values.get('create_time_from', None):       conditions.append(OrderHeader.order_time > values['create_time_from'])
        if values.get('create_time_to', None):         conditions.append(OrderHeader.order_time < '%s 23:59' % values['create_time_to'])
        if values.get('ref_no', None):                 conditions.append(OrderHeader.ref_no.op('like')('%%%s%%' % values['ref_no']))
        if values.get('no', None):                     conditions.append(OrderHeader.no.op('like')('%%%s%%' % values['no']))
        if values.get('source_province_id', None):
            conditions.append(OrderHeader.source_province_id == values['source_province_id'])
            sp = DBSession.query(Province).get(values['source_province_id'])
            source_cites = sp.children()
        else: source_cites = []
        if values.get('source_city_id', None):          conditions.append(OrderHeader.source_city_id == values['source_city_id'])
        if values.get('destination_province_id', None):
            conditions.append(OrderHeader.destination_province_id == values['destination_province_id'])
            dp = DBSession.query(Province).get(values['destination_province_id'])
            destination_cites = dp.children()
        else: destination_cites = []
        if values.get('destination_city_id', None):  conditions.append(OrderHeader.destination_city_id == values['destination_city_id'])

        if values.get('customer_id', None):
            conditions.append(OrderHeader.customer_id == values['customer_id'])
            sources = DBSession.query(CustomerSource).filter(and_(CustomerSource.active == 0, CustomerSource.customer_id == values['customer_id']))
        else:
            sources = []

        if values.get('source_company_id', None):       conditions.append(OrderHeader.source_company_id == values['source_company_id'])
        if values.get('approve', None):        conditions.append(OrderHeader.approve == values['approve'])
        if values.get('paid', None):           conditions.append(OrderHeader.paid == values['paid'])
        if values.get('is_exception', None):   conditions.append(OrderHeader.is_exception == values['is_exception'])
        if values.get('is_less_qty', None):    conditions.append(OrderHeader.is_less_qty == values['is_less_qty'])
        if values.get('is_return_note', None): conditions.append(OrderHeader.is_return_note == values['is_return_note'])

        # for the sort function
        field = values.get('field', 'create_time')
        if values.get('direction', 'desc') == 'desc':
            result = DBSession.query(OrderHeader, Customer, CustomerSource, CustomerTarget, User)\
            .filter(and_(*conditions)).order_by(desc(getattr(OrderHeader, field)))
        else:
            result = DBSession.query(OrderHeader, Customer, CustomerSource, CustomerTarget, User)\
            .filter(and_(*conditions)).order_by(getattr(OrderHeader, field))

        def url_for_page(**params): return url_for('bpFin.view', action = "index", page = params['page'])
        records = paginate.Page(result, values['page'], show_if_single_page = True, items_per_page = PAGINATE_PER_PAGE, url = url_for_page)

        return {
                'values' : values ,
                'customers' : getMasterAll(Customer),
                'sources' : sources,
                'records' : records,
                'source_cites' : source_cites,
                'destination_cites' : destination_cites,
                }
Esempio n. 4
0
def register():
    return {'customers' : getMasterAll(Customer), }