PHP-达到一定数量后递增数组

时间:2018-06-20 06:07:30

标签: php sql-server

我对PHP数组有疑问。让我告诉你我的故事。

我有一个报告,称为库存承购。该报告的目的是查看某产品每天平均销售多少。

以下是计算:

(beginning inventory quantity + delivery quantity - ending inventory quantity) / (datediff(ending inventory quantity date - beginning inventory quantity date)))

但这是为期三周的库存:请查看屏幕截图:

enter image description here

正如您在屏幕截图中所看到的,我标记了第一个结果,然后是第二个。我还包括了结果,它旁边是公式。如果您不清楚屏幕截图,请问我,以便我帮助您理解我的复杂问题。

现在这是我的代码:

$index = 0;

foreach($result as $key => $value) {
    $final_array[$key]['sales_group_code'] = $value['sales_group_code'];
    $final_array[$key]['sales_office_code'] = $value['sales_office_code'];
    $final_array[$key]['sales_district_code'] = $value['sales_district_code'];
    $final_array[$key]['customer_code'] = $value['customer_code'];
    $final_array[$key]['customer'] = $value['customer'];
    $final_array[$key]['week_no'] = $value['week_no'];
    $final_array[$key]['product'] = $value['product'];
    $final_array[$key]['delivery_quantity'] = $value['delivery_quantity'];
    $final_array[$key]['inventory_date'] = $value['inventory_date'];
    $final_array[$key]['inventory_quantity'] = $value['inventory_quantity'];
    $final_array[$key]['overall_count'] = $value['overall_count'];
    $inventory_array[] = $value['inventory_quantity'];
    $inventory_date_array[] = $value['inventory_date'];
    if ($previous_product == $value['product']) {
        $delivery_array[] = $value['delivery_quantity'];
        if ($row_ctr > 2) {
            $row_ctr = 0;
            $inventory_ctr++;
            array_shift($delivery_array);
        }

        $row_ctr++;
        $final_delivery_array = array_slice($delivery_array, 0, 3);
        for ($i = 0; $i < $index; $i++) {
            if (array_key_exists($i, $final_delivery_array)) {
                $total_delivery+= $final_delivery_array[$i];
                $date1 = new DateTime(date('Y-m-d', strtotime($inventory_date_array[$inventory_date_ctr])));
                $date2 = new DateTime(date('Y-m-d', strtotime($value['inventory_date'])));
                $diff = $date2->diff($date1)->days;
                if ($diff > 0) {
                    $offtake = ((($total_delivery + $inventory_array[$inventory_ctr]) - $value['inventory_quantity']) / $diff);
                }
                else {
                    $offtake = ((($total_delivery + $inventory_array[$inventory_ctr]) - $value['inventory_quantity']) / 1);
                }

                $final_array[$index]['days'] = $diff;
                $final_array[$index]['offtake'] = number_format((float)$offtake, 2, '.', '');
                $final_array[$index]['days_to_last'] = ($value['inventory_quantity'] / $offtake);
            }
        }

        $total_delivery = 0;
    }
    else {
        $final_array[$index]['days'] = 0;
        $previous_product = $value['product'];
        $final_array[$index]['offtake'] = 0;
        $final_array[$index]['days_to_last'] = 0;
        $total_delivery = 0;
    }

    $index++;
}

我会尝试解释我的代码:

我将所有交货和库存数量存储在一个阵列中。

我创建一个行计数器,最多计数3个。我计数3个,因为请记住我们有3周的移动结果。

如果行计数器达到3,我将取消设置交货和库存阵列的第一个元素。

现在我很好。我可以的一切正常。

更新:这是我的代码的结果:

Array
(
    [0] => Array
        (
            [sales_group_code] => 01
            [sales_office_code] => 013
            [sales_district_code] => 1307
            [customer_code] => BUD001
            [customer] => 1307-873 PPCI-ANTIPOLO SAN JOSE POB
            [week_no] => 10
            [product] => ASSORTED CUPCAKES
            [delivery_quantity] => 13.00
            [inventory_date] => 2018-03-06
            [inventory_quantity] => 4.08
            [overall_count] => 144
            [days] => 0
            [offtake] => 0
            [days_to_last] => 0
        )

    [1] => Array
        (
            [sales_group_code] => 01
            [sales_office_code] => 013
            [sales_district_code] => 1307
            [customer_code] => BUD001
            [customer] => 1307-873 PPCI-ANTIPOLO SAN JOSE POB
            [week_no] => 11
            [product] => ASSORTED CUPCAKES
            [delivery_quantity] => 11.00
            [inventory_date] => 2018-03-13
            [inventory_quantity] => 15.50
            [overall_count] => 144
            [days] => 7
            [offtake] => -0.06
            [days_to_last] => -258.33333333333
        )

    [2] => Array
        (
            [sales_group_code] => 01
            [sales_office_code] => 013
            [sales_district_code] => 1307
            [customer_code] => BUD001
            [customer] => 1307-873 PPCI-ANTIPOLO SAN JOSE POB
            [week_no] => 12
            [product] => ASSORTED CUPCAKES
            [delivery_quantity] => 7.00
            [inventory_date] => 2018-03-18
            [inventory_quantity] => 5.17
            [overall_count] => 144
            [days] => 12
            [offtake] => 1.41
            [days_to_last] => 3.6688350088705
        )

    [3] => Array
        (
            [sales_group_code] => 01
            [sales_office_code] => 013
            [sales_district_code] => 1307
            [customer_code] => BUD001
            [customer] => 1307-873 PPCI-ANTIPOLO SAN JOSE POB
            [week_no] => 14
            [product] => ASSORTED CUPCAKES
            [delivery_quantity] => 12.00
            [inventory_date] => 2018-04-01
            [inventory_quantity] => 5.25
            [overall_count] => 144
            [days] => 26
            [offtake] => 1.11
            [days_to_last] => 4.7346514047867
        )

    [4] => Array
        (
            [sales_group_code] => 01
            [sales_office_code] => 013
            [sales_district_code] => 1307
            [customer_code] => BUD001
            [customer] => 1307-873 PPCI-ANTIPOLO SAN JOSE POB
            [week_no] => 17
            [product] => ASSORTED CUPCAKES
            [delivery_quantity] => 9.00
            [inventory_date] => 2018-04-23
            [inventory_quantity] => 35.67
            [overall_count] => 144
            [days] => 48
            [offtake] => 0.16
            [days_to_last] => 218.66666666667
        )

    [5] => Array
        (
            [sales_group_code] => 01
            [sales_office_code] => 013
            [sales_district_code] => 1307
            [customer_code] => BUD001
            [customer] => 1307-873 PPCI-ANTIPOLO SAN JOSE POB
            [week_no] => 20
            [product] => ASSORTED CUPCAKES
            [delivery_quantity] => 3.00
            [inventory_date] => 2018-05-14
            [inventory_quantity] => 17.67
            [overall_count] => 144
            [days] => 69
            [offtake] => 0.37
            [days_to_last] => 47.202090592334
        )

    [6] => Array
        (
            [sales_group_code] => 01
            [sales_office_code] => 013
            [sales_district_code] => 1307
            [customer_code] => BUD001
            [customer] => 1307-873 PPCI-ANTIPOLO SAN JOSE POB
            [week_no] => 21
            [product] => ASSORTED CUPCAKES
            [delivery_quantity] => 9.00
            [inventory_date] => 2018-05-22
            [inventory_quantity] => 7.17
            [overall_count] => 144
            [days] => 77
            [offtake] => 0.47
            [days_to_last] => 15.196531791908
        )

    [7] => Array
        (
            [sales_group_code] => 01
            [sales_office_code] => 013
            [sales_district_code] => 1307
            [customer_code] => BUD001
            [customer] => 1307-873 PPCI-ANTIPOLO SAN JOSE POB
            [week_no] => 24
            [product] => ASSORTED CUPCAKES
            [delivery_quantity] => 7.00
            [inventory_date] => 2018-06-11
            [inventory_quantity] => 10.58
            [overall_count] => 144
            [days] => 97
            [offtake] => 0.19
            [days_to_last] => 55.204948897257
        )

    [8] => Array
        (
            [sales_group_code] => 01
            [sales_office_code] => 013
            [sales_district_code] => 1307
            [customer_code] => BUD001
            [customer] => 1307-873 PPCI-ANTIPOLO SAN JOSE POB
            [week_no] => 8
            [product] => BROWNIES
            [delivery_quantity] => 2.00
            [inventory_date] => 2018-02-21
            [inventory_quantity] => 4.33
            [overall_count] => 144
            [days] => 0
            [offtake] => 0
            [days_to_last] => 0
        )

    [9] => Array
        (
            [sales_group_code] => 01
            [sales_office_code] => 013
            [sales_district_code] => 1307
            [customer_code] => BUD001
            [customer] => 1307-873 PPCI-ANTIPOLO SAN JOSE POB
            [week_no] => 10
            [product] => BROWNIES
            [delivery_quantity] => 3.00
            [inventory_date] => 2018-03-06
            [inventory_quantity] => 2.33
            [overall_count] => 144
            [days] => 0
            [offtake] => 26.84
            [days_to_last] => 0.086810730253353
        )

    [10] => Array
        (
            [sales_group_code] => 01
            [sales_office_code] => 013
            [sales_district_code] => 1307
            [customer_code] => BUD001
            [customer] => 1307-873 PPCI-ANTIPOLO SAN JOSE POB
            [week_no] => 11
            [product] => BROWNIES
            [delivery_quantity] => 9.00
            [inventory_date] => 2018-03-13
            [inventory_quantity] => 3.92
            [overall_count] => 144
            [days] => 7
            [offtake] => 3.61
            [days_to_last] => 1.0867326732673
        )

    [11] => Array
        (
            [sales_group_code] => 01
            [sales_office_code] => 013
            [sales_district_code] => 1307
            [customer_code] => BUD001
            [customer] => 1307-873 PPCI-ANTIPOLO SAN JOSE POB
            [week_no] => 12
            [product] => BROWNIES
            [delivery_quantity] => 3.00
            [inventory_date] => 2018-03-18
            [inventory_quantity] => 12.67
            [overall_count] => 144
            [days] => 12
            [offtake] => 1.13
            [days_to_last] => 11.19587628866
        )

从我的代码结果中可以看到,days元素不断增长。事实并非如此。

这是我的问题:

在提供的公式中,我们必须获取开始库存日期数量和结束库存数量日期的日期差。

现在,当我完成三个星期的运行结果时,就会出现我的问题。如果我已经完成了三个星期的运行结果,就必须移至下一行,对吗?那将是我的下一个开始的清单。但是我的问题是我总是得到开始的库存日期。即使我已经搬到接下来的三个星期。 因此,您会想象到日期差异总是在增长,因为它总是从第一行开始。

现在是我的问题,如何将库存日期以及交货数量和库存数量一起移动。

我希望我能很好地解释它。如果零件不清楚,请问我,我会向您解释。我已经解决了一个月了。

非常感谢您对我的问题表现出兴趣。希望您能指导我或至少带领我去哪里看看。

谢谢。

更新。我将包含我的ms sql查询。

select	c.SGrp as sales_group_code,
		c.SOffc as sales_office_code,
		c.SDst as  sales_district_code,
		a.customer_code,
		case
		when
		c.CustAlias != '' then c.CustAlias
		else c.CustNm end as customer,
		a.product,		
		b.delivery_week,
		b.delivery_quantity,
		a.inventory_date,
		a.inventory_week as week_no,
		a.inventory_quantity,

		overall_count = count(*) over()

from	(select	a.CustCode as customer_code,
				c.ProdNm as product,
				cast(a.DtRcv as date) as inventory_date,
				datepart(wk, a.DtRcv) as inventory_week,
						CAST(sum(CAST(b.Qty AS FLOAT)/ c.CaseDiv) AS decimal(18,2)) as inventory_quantity
		from	BigEMerchandiser.dbo.tbl_Inventory_H as a

				inner join BigEMerchandiser.dbo.tbl_Inventory_D as b
				on a.TransCtr = b.TransCtr

				inner join BigEMasterData.dbo.tbl_Materials as c
				on b.Material = c.ExtMatGrp

				group by	a.CustCode,
							c.ProdNm,
							cast(a.DtRcv as date),
							datepart(wk, a.DtRcv)) as a
		

		inner join (select	a.Cust as customer_code,
							a.ProdNm as product,
							a.WkNum as delivery_week,
							sum(a.EQCaseQty) as delivery_quantity
					from	BigESales.dbo.tbl_CurrentSales as a


					group by	a.Cust,
								a.ProdNm,
								a.WkNum) as b
		on	a.customer_code = b.customer_code
		and	a.inventory_week = b.delivery_week
		and a.product = b.product

		inner join BigEMasterData.dbo.tbl_Customers as c
		on a.customer_code = c.CustCode


		inner join BigEUsers.dbo.user_role_area as d
		on c.SDst = d.area_id

		inner join BigEUsers.dbo.users as e
		on d.role_id = e.role_id

where	((datepart(wk, a.inventory_week) >= datepart(wk, @date_from) and datepart(wk, a.inventory_week) <= datepart(wk, @date_to))
	
	
or		isnull(@date_from,'')='' OR isnull(@date_to,'')='')

and		((a.product= @product)
or		(isnull(@product, '') = ''))

and		((REPLACE(REPLACE(a.customer_code, CHAR(13), ''), CHAR(10), '')  = @customer_code)
or		(isnull(@customer_code, '') = ''))

and		(e.id = @user_id)

and		e.is_active = 1

and 		b.delivery_quantity >= 0

order by a.customer_code,
         a.product,
		 a.inventory_week
offset @start rows
fetch next @page_size rows only

0 个答案:

没有答案