我正在尝试将PurchaseLine表与SellLine合并以获得 售出数量购买数量库存余额
此查询有效,无需加入采购订单表。 对于采购订单表,它返回确定,但数据为空
我认为我对Where,WhereIn,Orwhere子句有疑问?
请参阅下面的附件图像。为什么数据为空?
if ($request->ajax()) {
$variation_id = $request->get('variation_id', null);
$query = TransactionSellLine::join(
'transactions as t',
'transaction_sell_lines.transaction_id',
'=',
't.id'
)
->join(
'variations as v',
'transaction_sell_lines.variation_id',
'=',
'v.id'
)
->join('product_variations as pv', 'v.product_variation_id', '=', 'pv.id')
->join('purchase_lines as pl', 'pl.transaction_id', '=', 't.id')
->join('products as p', 'pv.product_id', '=', 'p.id')
->join('users as z', 't.created_by', '=', 'z.id')
->leftjoin('units as u', 'p.unit_id', '=', 'u.id')
->where('t.business_id', $business_id)
->whereIn('t.type', ['sell', 'purchase', 'opening_stock'])
->orwhereIn('t.status', ['final', 'receieved'])
->select(
'p.name as product_name',
'z.username as username',
'p.enable_stock',
'p.type as product_type',
'pv.name as product_variation',
'v.name as variation_name',
't.id as transaction_id',
't.transaction_date as transaction_date',
'transaction_sell_lines.unit_price_before_discount as unit_price',
DB::raw('DATE_FORMAT(t.transaction_date, "%Y-%m-%d") as formated_date'),
DB::raw("(SELECT SUM(vld.qty_available) FROM variation_location_details as vld WHERE vld.variation_id=v.id $vld_str) as current_stock"),
DB::raw('SUM(transaction_sell_lines.quantity - transaction_sell_lines.quantity_returned) as total_qty_sold'),
'u.short_name as unit',
DB::raw('SUM(pl.quantity - pl.quantity_returned - pl.quantity_adjusted) as qty_purchased'),
DB::raw('SUM((transaction_sell_lines.quantity - transaction_sell_lines.quantity_returned) * transaction_sell_lines.unit_price_inc_tax) as subtotal')
)
->groupBy('v.id')
->groupBy('pl.id')
->groupBy('formated_date');
return Datatables::of($query)
->rawColumns(['qty_purchased','current_stock', 'subtotal', 'total_qty_sold'])
->make(true);
答案 0 :(得分:0)
我删除了采购线,因为没有必要加入 这行
->join('purchase_lines as pl', 'pl.transaction_id', '=', 't.id')
我应该使用产品ID而不是交易ID来查询采购线表
以下是可行的修改
DB::raw('(SELECT SUM(pl.quantity - pl.quantity_returned - pl.quantity_adjusted) FROM purchase_lines as pl WHERE pl.product_id=pv.id) as qty_purchased')