公布一下工资管理系统中核心部分工资计算的代码

终于完成了工资计算部分的代码,请大家指教一下

用户可以自行定义工资计算公式:例如

实发工资=应发工资-应扣工资
个人所得税=tax(应发工资)

CREATE TABLE pq_unitgzxm (
unit_id char(8) NOT NULL DEFAULT '',
xmbh int NOT NULL DEFAULT 0,
xmmc varchar(200) NOT NULL DEFAULT '',
gs varchar(400) NOT NULL DEFAULT '',
gdx bit NOT NULL DEFAULT '0',
bz varchar(200) NOT NULL DEFAULT '',
xtnd bit NOT NULL DEFAULT '0'
)

Go
INSERT INTO pq_unitgzxm values ('00000001',1,'基本工资' ,'',1,'',0);
INSERT INTO pq_unitgzxm values ('00000001',2,'奖金' ,'',1,'',0);
INSERT INTO pq_unitgzxm values ('00000001',3,'应发工资' ,'',1,'',0);
INSERT INTO pq_unitgzxm values ('00000001',4,'个人所得税','',1,'系统内定,不可修改',1);
INSERT INTO pq_unitgzxm values ('00000001',5,'应扣工资' ,'',1,'',0);
INSERT INTO pq_unitgzxm values ('00000001',6,'实发工资' ,'',1,'系统内定,不可修改',1);

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[pq_gzb]')
and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [pq_gzb]
CREATE TABLE pq_gzb (
gzb_id char(8) NOT NULL PRIMARY KEY,
unit_id char(8) NOT NULL DEFAULT '', /* 单位id */
dwmc varchar(400) NOT NULL DEFAULT '', /* 单位名称 */
gzqjyear char(4) NOT NULL DEFAULT '', /* 工资期间年 */
gzqjperiod char(2) NOT NULL DEFAULT '', /* 工资期间期 */
gzffksrq varchar(10) NOT NULL DEFAULT '', /* 工资发放开始日期 */
gzffjsrq varchar(10) NOT NULL DEFAULT '', /* 工资发放结束日期 */

jsshebao varchar(2) NOT NULL DEFAULT '', /* 计算社保 */
shebaoksrq varchar(10) NOT NULL DEFAULT '', /* 社保开始日期 */
shebaojsrq varchar(10) NOT NULL DEFAULT '', /* 社保结束日期 */
fh varchar(8) NOT NULL DEFAULT '', /* 复合 */
fhr varchar(20) NOT NULL DEFAULT '' /* 复合人 */
)

Go

INSERT INTO pq_gzb (gzb_id,unit_id,dwmc,gzqjyear,gzqjperiod,gzffksrq,gzffjsrq,jsshebao,fh,fhr) values ('00000001','00000001','软件','2006','04','2006-04-01','2006-04-30','1','','');


if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[pq_gzmx]')
and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [pq_gzmx]
CREATE TABLE pq_gzmx (
gzb_id char(8) NOT NULL DEFAULT '',
employee_id char(8) NOT NULL DEFAULT '',
xm varchar(400) NOT NULL DEFAULT '',
dwmc varchar(400) NOT NULL DEFAULT '',
unitcontract_id char(8) NOT NULL DEFAULT '',
employeecontract_id char(8) NOT NULL DEFAULT '',
gzffksrq varchar(10) NOT NULL DEFAULT '', /* 工资发放开始日期 */
gzffjsrq varchar(10) NOT NULL DEFAULT '', /* 工资发放结束日期 */
yff varchar(8) NOT NULL DEFAULT '', /* 已发放 */
gzdata1 decimal NOT NULL DEFAULT '0',
gzdata2 decimal NOT NULL DEFAULT '0',
gzdata3 decimal NOT NULL DEFAULT '0',
gzdata4 decimal NOT NULL DEFAULT '0',
gzdata5 decimal NOT NULL DEFAULT '0',
gzdata6 decimal NOT NULL DEFAULT '0',
gzdata7 decimal NOT NULL DEFAULT '0',
gzdata8 decimal NOT NULL DEFAULT '0',
gzdata9 decimal NOT NULL DEFAULT '0',
gzdata10 decimal NOT NULL DEFAULT '0',
gzdata11 decimal NOT NULL DEFAULT '0',
gzdata12 decimal NOT NULL DEFAULT '0',
gzdata13 decimal NOT NULL DEFAULT '0',
gzdata14 decimal NOT NULL DEFAULT '0',
gzdata15 decimal NOT NULL DEFAULT '0',
gzdata16 decimal NOT NULL DEFAULT '0',
gzdata17 decimal NOT NULL DEFAULT '0',
gzdata18 decimal NOT NULL DEFAULT '0',
gzdata19 decimal NOT NULL DEFAULT '0',
gzdata20 decimal NOT NULL DEFAULT '0'
)

Go
INSERT INTO pq_gzmx (gzb_id,employee_id,xm,dwmc,unitcontract_id,employeecontract_id,gzffksrq,gzffjsrq,gzdata1) values ('00000001','00000001','张三','软件','00000001','00000001','2006-04-01','2006-04-30',0);
INSERT INTO pq_gzmx (gzb_id,employee_id,xm,dwmc,unitcontract_id,employeecontract_id,gzffksrq,gzffjsrq,gzdata1) values ('00000001','00000002','李四','软件','00000001','00000002','2006-04-01','2006-04-30',0);

function cal_salary($gzb_id = '',$error = '') {
global $db, $me, $t,$_POST;


extract($_POST);
if ($gzb_id && !$error) {
$unit = $db->GetRow("select unit_id,dwmc from ".TBL_GZB." where gzb_id = '$gzb_id'");
$unit_id = $unit['unit_id'];
$dwmc = $unit['dwmc'];

//取出该单位所有的工资项
$unitgzxm = $db->GetAll("select xmmc,gdx,gs from ".TBL_UNITGZXM." where unit_id = '$unit_id'");

//取出所有的工资表
$rs = $db->Execute("select * from ".TBL_GZMX." where gzb_id = '$gzb_id'");

while (!$rs->EOF) { //取到工资表的一行,对这一行开始处理计算

debug_echo("Now cal the employee:".$rs->fields['employee_id']);
//先得到一个本行的名值对,如$data['应发工资']['value']=>'1000' $data['实发工资']['value']=>'1000'
for ($i = 0, $count = count($unitgzxm); $i < $count; $i++) {

$data[$unitgzxm[$i]['xmmc']]['value'] = $rs->fields['gzdata'.($i+1)];
$data[$unitgzxm[$i]['xmmc']]['gs'] = $unitgzxm[$i]['gs'];

if($unitgzxm[$i]['gdx']==1) { //是固定项的,用来计算

if(trim($unitgzxm[$i]['gs'])!="") {
$data[$unitgzxm[$i]['xmmc']]['ready'] = 0; //表示该值尚没有计算好
}else {
$data[$unitgzxm[$i]['xmmc']]['ready'] = 1; //固定项,但公式为空,也看成准备就绪
}

} else {

$data[$unitgzxm[$i]['xmmc']]['ready'] = 1; //表示该值准备就绪

}
}

//print_r($data);

$allcal = 0; //所有都计算完的标志,为1时表示计算完毕
while($allcal ==0) {
$havecontinue = 0;
for ($i = 0, $count = count($unitgzxm); $i < $count; $i++) {

if($data[$unitgzxm[$i]['xmmc']]['ready']==0) { //该值尚未计算好,进行计算


$allready = 1; // 公式中所有项都ready了么?

//取出公式里面所有的中文
preg_match_all("/[".chr(0xa1)."-".chr(0xff)."]+/", $data[$unitgzxm[$i]['xmmc']]['gs'],$matches, PREG_SET_ORDER);

//检查每一项ready情况
foreach ($matches as $val) {
if($data[$val[0]]['ready'] == 0) {
$allready = 0;
}
else {
$data[$unitgzxm[$i]['xmmc']]['gs'] = str_replace($val[0], $data[$val[0]]['value'], $data[$unitgzxm[$i]['xmmc']]['gs']);
}

}
debug_echo("the cal is".$data[$unitgzxm[$i]['xmmc']]['gs']);


//所有项都ready,就开始计算,计算完毕,$data[$unitgzxm[$i]['xmmc']]['ready'] = 1
//否则continue,继续for循环,检查下一个变量,并置havecontinue标志

if($allready==1) {
//先替换变量

//开始计算
$expression = $data[$unitgzxm[$i]['xmmc']]['gs'];


//$expression = "(2.33*6)";
$rpn = new Math_Rpn();
$value = $rpn->calculate($expression,'deg',false);
debug_echo("The value is ".$value);

$record["gzdata".($i+1)] = $value;

$data[$unitgzxm[$i]['xmmc']]['ready'] = 1;
} else {
$havecontinue = 1;
continue;
}



}

}

if(!$havecontinue) $allcal = 1;

}

$db->AutoExecute(TBL_GZMX,$record,'UPDATE', "gzb_id = '$gzb_id' and employee_id='".$rs->fields['employee_id']."'");


$rs->MoveNext();
}

}
show_form($gzb_id);
}

pear RPN包中加入了tax()的自定义函数如下

'tax' => array ('tax', 3, 1, '_tax'), //hack by Daniel Summer for rcpq

/**
* Tax function
*
* @param array $temp Temporary array
* @param integer $pos Position of operator
* @return float Function's relult
* @access private
*/
function _tax($temp, $pos) {
global $db;

$value = $temp[$pos-1];
$base = $db->GetOne("select * from ".TBL_TAX_BASE." where name = 'base'");

if($value < $base) return 0; //

$value = $value - $base;
$rates = $db->GetAll("select * from ".TBL_TAX_RATE);


for ($i = 0, $count = count($rates); $i < $count; $i++) {
if($value>=$rates[$i]['bottomline'] && $value<=$rates[$i]['topline']) {
$value = $value * $rates[$i]['rate'];
$value = $value - $rates[$i]['quickcal'];
break;
}

}


return $value;

}

你可能感兴趣的:(Access,Go)