def handle_data_lost(model, pk=None, **kwargs): """ only for Django ORM :param model: -- models Name :param pk: -- primary key of models :param kwargs: -- other lookup for filter query :return: -- tuple of Data and Message error for user """ return Data.Lost, Message.get_lost_info(model=model, pk=pk, **kwargs)
def setInActive(self, idapp, inactive): if self.dataExist(idapp=idapp): data = super( NA_BR_Employee, self).get_queryset().values('inactive').filter(idapp=idapp) if commonFunct.str2bool(data[0]['inactive']) == inactive: return (Data.Changed, Message.has_update_by_other(pk=idapp, table='employee')) else: data.update(inactive=inactive) return (Data.Success, ) else: return (Data.Lost, Message.get_lost_info(pk=idapp, table='employee'))
def retriveData(self, get_idapp, must_check=True): def get_data(): return super(NA_BR_Employee, self).get_queryset()\ .filter(idapp__exact=get_idapp)\ .values('idapp', 'nik', 'employee_name', 'typeapp', 'jobtype', 'gender', 'status', 'telphp', 'territory', 'descriptions', 'inactive', 'createddate', 'createdby') if must_check: if self.dataExist(idapp=get_idapp): return (Data.Success, get_data()[0]) else: return (Data.Lost, Message.get_lost_info(pk=get_idapp, table='employee')) else: return (Data.Success, get_data()[0])
def delete_employee(self, **kwargs): get_idapp = kwargs['idapp'] NA_User = kwargs['NA_User'] if self.dataExist(idapp=get_idapp): if self.hasRef(get_idapp): return (Data.HasRef, Message.HasRef_del.value) else: cur = connection.cursor() # ============== INSERT INTO LOG EVENT ================ data = self.retriveData(get_idapp, False)[1] dataPrms = { 'Nik': data['nik'], 'Employee_Name': data['employee_name'], 'Typeapp': data['typeapp'], 'Jobtype': data['typeapp'], 'Gender': data['gender'], 'Status': data['status'], 'Telphp': data['telphp'], 'Territory': data['territory'], 'Descriptions': data['descriptions'], 'Inactive': data['inactive'] } createddate = data['createddate'] modifieddate = data.get('modifieddate') if isinstance(createddate, datetime): dataPrms['CreatedDate'] = createddate.strftime( '%d %B %Y %H:%M:%S') dataPrms['CreatedBy'] = data['createdby'] if modifieddate is not None: dataPrms['ModifiedDate'] = modifieddate dataPrms['ModifiedBy'] = data['modifiedby'] Query = """INSERT INTO logevent (nameapp,descriptions,createddate,createdby) VALUES(\'Deleted Employee\', JSON_OBJECT(\'deleted\',JSON_ARRAY({})),NOW(),""".format( ','.join('%(' + i + ')s' for i in dataPrms)) dataPrms['NA_User'] = NA_User Query = Query + "%(NA_User)s)" with transaction.atomic(): cur.execute(Query, dataPrms) # ============= End INSERT INTO LOG EVENT ============== cur.execute('''DELETE FROM employee WHERE idapp=%s''', [get_idapp]) cur.close() return (Data.Success, Message.Success.value) else: return (Data.Lost, Message.get_lost_info(get_idapp))
def EntrySuplier(request): if request.method == 'POST': form = NA_Suplier_form(request.POST) if form.is_valid(): mode = request.POST['mode'] data = getData(request, form) result = None if mode == 'Add': data['createddate'] = datetime.datetime.now() data['createdby'] = getCurrentUser(request) result = NASuplier.objects.SaveData(StatusForm.Input, **data) elif mode == 'Edit': data['modifieddate'] = datetime.datetime.now() data['modifiedby'] = getCurrentUser(request) result = NASuplier.objects.SaveData(StatusForm.Edit, **data) elif mode == 'Open': if request.POST['supliername']: return HttpResponse(json.dumps({ 'message': 'Cannot Edit Data with inspect element .. .' }), status=403, content_type='application/json') return commonFunct.response_default(result) elif request.method == 'GET': getSupCode = request.GET['supliercode'] mode = request.GET['mode'] if mode == 'Edit' or mode == 'Open': result = NASuplier.objects.retriveData(getSupCode) #return tuple if result[0] == Data.Success: form = NA_Suplier_form(initial=result[1][0]) form.fields['supliercode'].widget.attrs[ 'disabled'] = 'disabled' return render(request, 'app/MasterData/NA_Entry_Suplier.html', {'form': form}) elif result[0] == Data.Lost: return HttpResponse(json.dumps({ 'message': Message.get_lost_info(pk=getSupCode, table='suplier') }), status=404, content_type='application/json') else: form = NA_Suplier_form() return render(request, 'app/MasterData/NA_Entry_Suplier.html', {'form': form})
def retrieveData(self, idapp): if self.dataExists(idapp=idapp): if self.hasRef(idapp): return (Data.HasRef, Message.HasRef_edit) cur = connection.cursor() query_string = """ CREATE TEMPORARY TABLE T_form_ga_receive ENGINE=InnoDB AS( SELECT ngr.idapp, ngr.fk_goods, g.itemcode, g.goodsname, s.suppliercode, s.suppliername, ngr.fk_p_r_by AS pr_by, emp1.pr_by_nik, emp1.pr_by_name, ngr.fk_receivedby AS received_by, emp2.received_by_nik, emp2.received_by_name, DATE_FORMAT(ngr.datereceived, \'%%d/%%m/%%Y\') AS datereceived, ngr.brand, ngr.invoice_no, ngr.typeapp, ngr.machine_no, ngr.chassis_no, DATE_FORMAT(ngr.year_made, \'%%Y\') AS year_made, ngr.colour, ngr.model, ngr.kind, ngr.cylinder, ngr.fuel, ngr.price, ngr.descriptions, ngh.reg_no, DATE_FORMAT(ngh.date_reg, \'%%d/%%m/%%Y\') AS date_reg, DATE_FORMAT(ngh.expired_reg, \'%%d/%%m/%%Y\') AS expired_reg, DATE_FORMAT(ngh.bpkb_expired, \'%%d/%%m/%%Y\') AS bpkb_expired, ngh.descriptions AS remark FROM n_a_ga_receive AS ngr INNER JOIN n_a_goods AS g ON ngr.fk_goods = g.idapp INNER JOIN n_a_supplier AS s ON ngr.fk_supplier = s.suppliercode LEFT OUTER JOIN ( SELECT idapp, nik AS pr_by_nik, employee_name AS pr_by_name FROM employee ) AS emp1 ON ngr.fk_p_r_by = emp1.idapp LEFT OUTER JOIN ( SELECT idapp, nik AS received_by_nik, employee_name AS received_by_name FROM employee ) AS emp2 ON ngr.fk_receivedby = emp2.idapp LEFT JOIN n_a_ga_vn_history AS ngh ON ngr.idapp = ngh.fk_app WHERE ngr.idapp = %(idapp)s ) """ cur.execute(query_string, {'idapp': idapp}) query_string = """ SELECT * FROM T_form_ga_receive """ cur.execute(query_string) result = query.dictfetchall(cur)[0] return (Data.Success, result) else: return (Data.Lost, Message.get_lost_info(pk=idapp, table='n_a_goods_receive_other'))
def SaveData(self, statusForm, **data): cur = connection.cursor() Params = {'suppliercode':data['suppliercode'], 'suppliername':data['suppliername'], 'address':data['address'], 'telp':data['telp'],\ 'hp':data['hp'], 'contactperson':data['contactperson'], 'inactive':data['inactive']} if statusForm == StatusForm.Input: check_exists = self.dataExist(suppliercode=data['suppliercode'], hp=data['hp'], telp=data['telp']) if check_exists[0]: return (Data.Exists, check_exists[1]) else: Query = '''INSERT INTO n_a_supplier(SupplierCode, SupplierName, Address, Telp, Hp, ContactPerson, Inactive, CreatedDate, CreatedBy) values(%(suppliercode)s,%(suppliername)s,%(address)s,%(telp)s,%(hp)s,%(contactperson)s,%(inactive)s,%(createddate)s,%(createdby)s)''' Params['createddate'] = data['createddate'] Params['createdby'] = data['createdby'] elif statusForm == StatusForm.Edit: if self.dataExist(suppliercode=data['suppliercode'])[0]: if self.HasRef(data['suppliercode']): return (Data.HasRef, Message.HasRef_edit.value) else: check_exists = self.dataExist( 'update', _exclude_supplierCode=data['suppliercode'], hp=data['hp'], telp=data['telp']) if check_exists[0]: return (Data.Exists, check_exists[1]) else: Params['modifieddate'] = data['modifieddate'] Params['modifiedby'] = data['modifiedby'] Query = """UPDATE n_a_supplier SET SupplierName=%(suppliername)s, Address=%(address)s, Telp=%(telp)s, Hp=%(hp)s, ContactPerson=%(contactperson)s, Inactive=%(inactive)s,ModifiedDate=%(modifieddate)s, ModifiedBy=%(modifiedby)s WHERE SupplierCode=%(suppliercode)s""" else: return (Data.Lost, Message.get_lost_info(pk=data['suppliercode'], table='supplier')) cur.execute(Query, Params) rowId = cur.lastrowid connection.close() return (Data.Success, rowId)
def retrieveData(self, idapp): if self.dataExists(idapp=idapp): cur = connection.cursor() Query = """SELECT ngr.idapp,ngr.refno,ngr.FK_goods AS idapp_fk_goods, g.itemcode AS fk_goods, goodsname as goods_desc,g.economiclife,ngr.datereceived, ngr.fk_suplier,sp.supliername,ngr.fk_ReceivedBy as idapp_fk_receivedby, emp1.fk_receivedby,emp1.employee_received,ngr.FK_P_R_By AS idapp_fk_p_r_by, emp2.fk_p_r_by,emp2.employee_pr,ngr.totalpurchase,ngr.totalreceived, ngr.descriptions,ngr.descbysystem FROM n_a_goods_receive_other AS ngr INNER JOIN n_a_suplier AS sp ON sp.SuplierCode = ngr.FK_Suplier LEFT OUTER JOIN (SELECT IDApp,NIK AS fk_receivedby,employee_name AS employee_received FROM employee) AS emp1 ON emp1.IDApp = ngr.FK_ReceivedBy LEFT OUTER JOIN (SELECT IDApp,NIK AS fk_p_r_by,employee_name AS employee_pr FROM employee) AS emp2 ON emp2.IDApp = ngr.FK_P_R_By INNER JOIN n_a_goods as g ON g.IDApp = ngr.FK_goods WHERE ngr.IDApp = %(IDApp)s""" cur.execute(Query, {'IDApp': idapp}) result = query.dictfetchall(cur) return (Data.Success, result[0]) else: return (Data.Lost, Message.get_lost_info(pk=idapp, table='n_a_goods_receive_other'))